Home | History | Annotate | Line # | Download | only in amdgpu
amdgpu_ucode.c revision 1.5
      1 /*	$NetBSD: amdgpu_ucode.c,v 1.5 2020/02/14 04:35:19 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2014 Advanced Micro Devices, Inc.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  *
     24  */
     25 
     26 #include <sys/cdefs.h>
     27 __KERNEL_RCSID(0, "$NetBSD: amdgpu_ucode.c,v 1.5 2020/02/14 04:35:19 riastradh Exp $");
     28 
     29 #include <linux/firmware.h>
     30 #include <linux/slab.h>
     31 #include <linux/module.h>
     32 #include <asm/byteorder.h>
     33 #include <drm/drmP.h>
     34 #include "amdgpu.h"
     35 #include "amdgpu_ucode.h"
     36 
     37 #include <linux/nbsd-namespace.h>
     38 
     39 static void amdgpu_ucode_print_common_hdr(const struct common_firmware_header *hdr)
     40 {
     41 	DRM_DEBUG("size_bytes: %u\n", le32_to_cpu(hdr->size_bytes));
     42 	DRM_DEBUG("header_size_bytes: %u\n", le32_to_cpu(hdr->header_size_bytes));
     43 	DRM_DEBUG("header_version_major: %u\n", le16_to_cpu(hdr->header_version_major));
     44 	DRM_DEBUG("header_version_minor: %u\n", le16_to_cpu(hdr->header_version_minor));
     45 	DRM_DEBUG("ip_version_major: %u\n", le16_to_cpu(hdr->ip_version_major));
     46 	DRM_DEBUG("ip_version_minor: %u\n", le16_to_cpu(hdr->ip_version_minor));
     47 	DRM_DEBUG("ucode_version: 0x%08x\n", le32_to_cpu(hdr->ucode_version));
     48 	DRM_DEBUG("ucode_size_bytes: %u\n", le32_to_cpu(hdr->ucode_size_bytes));
     49 	DRM_DEBUG("ucode_array_offset_bytes: %u\n",
     50 		  le32_to_cpu(hdr->ucode_array_offset_bytes));
     51 	DRM_DEBUG("crc32: 0x%08x\n", le32_to_cpu(hdr->crc32));
     52 }
     53 
     54 void amdgpu_ucode_print_mc_hdr(const struct common_firmware_header *hdr)
     55 {
     56 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
     57 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
     58 
     59 	DRM_DEBUG("MC\n");
     60 	amdgpu_ucode_print_common_hdr(hdr);
     61 
     62 	if (version_major == 1) {
     63 		const struct mc_firmware_header_v1_0 *mc_hdr =
     64 			const_container_of(hdr, struct mc_firmware_header_v1_0, header);
     65 
     66 		DRM_DEBUG("io_debug_size_bytes: %u\n",
     67 			  le32_to_cpu(mc_hdr->io_debug_size_bytes));
     68 		DRM_DEBUG("io_debug_array_offset_bytes: %u\n",
     69 			  le32_to_cpu(mc_hdr->io_debug_array_offset_bytes));
     70 	} else {
     71 		DRM_ERROR("Unknown MC ucode version: %u.%u\n", version_major, version_minor);
     72 	}
     73 }
     74 
     75 void amdgpu_ucode_print_smc_hdr(const struct common_firmware_header *hdr)
     76 {
     77 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
     78 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
     79 
     80 	DRM_DEBUG("SMC\n");
     81 	amdgpu_ucode_print_common_hdr(hdr);
     82 
     83 	if (version_major == 1) {
     84 		const struct smc_firmware_header_v1_0 *smc_hdr =
     85 			const_container_of(hdr, struct smc_firmware_header_v1_0, header);
     86 
     87 		DRM_DEBUG("ucode_start_addr: %u\n", le32_to_cpu(smc_hdr->ucode_start_addr));
     88 	} else {
     89 		DRM_ERROR("Unknown SMC ucode version: %u.%u\n", version_major, version_minor);
     90 	}
     91 }
     92 
     93 void amdgpu_ucode_print_gfx_hdr(const struct common_firmware_header *hdr)
     94 {
     95 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
     96 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
     97 
     98 	DRM_DEBUG("GFX\n");
     99 	amdgpu_ucode_print_common_hdr(hdr);
    100 
    101 	if (version_major == 1) {
    102 		const struct gfx_firmware_header_v1_0 *gfx_hdr =
    103 			const_container_of(hdr, struct gfx_firmware_header_v1_0, header);
    104 
    105 		DRM_DEBUG("ucode_feature_version: %u\n",
    106 			  le32_to_cpu(gfx_hdr->ucode_feature_version));
    107 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(gfx_hdr->jt_offset));
    108 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(gfx_hdr->jt_size));
    109 	} else {
    110 		DRM_ERROR("Unknown GFX ucode version: %u.%u\n", version_major, version_minor);
    111 	}
    112 }
    113 
    114 void amdgpu_ucode_print_rlc_hdr(const struct common_firmware_header *hdr)
    115 {
    116 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
    117 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
    118 
    119 	DRM_DEBUG("RLC\n");
    120 	amdgpu_ucode_print_common_hdr(hdr);
    121 
    122 	if (version_major == 1) {
    123 		const struct rlc_firmware_header_v1_0 *rlc_hdr =
    124 			const_container_of(hdr, struct rlc_firmware_header_v1_0, header);
    125 
    126 		DRM_DEBUG("ucode_feature_version: %u\n",
    127 			  le32_to_cpu(rlc_hdr->ucode_feature_version));
    128 		DRM_DEBUG("save_and_restore_offset: %u\n",
    129 			  le32_to_cpu(rlc_hdr->save_and_restore_offset));
    130 		DRM_DEBUG("clear_state_descriptor_offset: %u\n",
    131 			  le32_to_cpu(rlc_hdr->clear_state_descriptor_offset));
    132 		DRM_DEBUG("avail_scratch_ram_locations: %u\n",
    133 			  le32_to_cpu(rlc_hdr->avail_scratch_ram_locations));
    134 		DRM_DEBUG("master_pkt_description_offset: %u\n",
    135 			  le32_to_cpu(rlc_hdr->master_pkt_description_offset));
    136 	} else if (version_major == 2) {
    137 		const struct rlc_firmware_header_v2_0 *rlc_hdr =
    138 			const_container_of(hdr, struct rlc_firmware_header_v2_0, header);
    139 
    140 		DRM_DEBUG("ucode_feature_version: %u\n",
    141 			  le32_to_cpu(rlc_hdr->ucode_feature_version));
    142 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(rlc_hdr->jt_offset));
    143 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(rlc_hdr->jt_size));
    144 		DRM_DEBUG("save_and_restore_offset: %u\n",
    145 			  le32_to_cpu(rlc_hdr->save_and_restore_offset));
    146 		DRM_DEBUG("clear_state_descriptor_offset: %u\n",
    147 			  le32_to_cpu(rlc_hdr->clear_state_descriptor_offset));
    148 		DRM_DEBUG("avail_scratch_ram_locations: %u\n",
    149 			  le32_to_cpu(rlc_hdr->avail_scratch_ram_locations));
    150 		DRM_DEBUG("reg_restore_list_size: %u\n",
    151 			  le32_to_cpu(rlc_hdr->reg_restore_list_size));
    152 		DRM_DEBUG("reg_list_format_start: %u\n",
    153 			  le32_to_cpu(rlc_hdr->reg_list_format_start));
    154 		DRM_DEBUG("reg_list_format_separate_start: %u\n",
    155 			  le32_to_cpu(rlc_hdr->reg_list_format_separate_start));
    156 		DRM_DEBUG("starting_offsets_start: %u\n",
    157 			  le32_to_cpu(rlc_hdr->starting_offsets_start));
    158 		DRM_DEBUG("reg_list_format_size_bytes: %u\n",
    159 			  le32_to_cpu(rlc_hdr->reg_list_format_size_bytes));
    160 		DRM_DEBUG("reg_list_format_array_offset_bytes: %u\n",
    161 			  le32_to_cpu(rlc_hdr->reg_list_format_array_offset_bytes));
    162 		DRM_DEBUG("reg_list_size_bytes: %u\n",
    163 			  le32_to_cpu(rlc_hdr->reg_list_size_bytes));
    164 		DRM_DEBUG("reg_list_array_offset_bytes: %u\n",
    165 			  le32_to_cpu(rlc_hdr->reg_list_array_offset_bytes));
    166 		DRM_DEBUG("reg_list_format_separate_size_bytes: %u\n",
    167 			  le32_to_cpu(rlc_hdr->reg_list_format_separate_size_bytes));
    168 		DRM_DEBUG("reg_list_format_separate_array_offset_bytes: %u\n",
    169 			  le32_to_cpu(rlc_hdr->reg_list_format_separate_array_offset_bytes));
    170 		DRM_DEBUG("reg_list_separate_size_bytes: %u\n",
    171 			  le32_to_cpu(rlc_hdr->reg_list_separate_size_bytes));
    172 		DRM_DEBUG("reg_list_separate_size_bytes: %u\n",
    173 			  le32_to_cpu(rlc_hdr->reg_list_separate_size_bytes));
    174 	} else {
    175 		DRM_ERROR("Unknown RLC ucode version: %u.%u\n", version_major, version_minor);
    176 	}
    177 }
    178 
    179 void amdgpu_ucode_print_sdma_hdr(const struct common_firmware_header *hdr)
    180 {
    181 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
    182 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
    183 
    184 	DRM_DEBUG("SDMA\n");
    185 	amdgpu_ucode_print_common_hdr(hdr);
    186 
    187 	if (version_major == 1) {
    188 		const struct sdma_firmware_header_v1_0 *sdma_hdr =
    189 			const_container_of(hdr, struct sdma_firmware_header_v1_0, header);
    190 
    191 		DRM_DEBUG("ucode_feature_version: %u\n",
    192 			  le32_to_cpu(sdma_hdr->ucode_feature_version));
    193 		DRM_DEBUG("ucode_change_version: %u\n",
    194 			  le32_to_cpu(sdma_hdr->ucode_change_version));
    195 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(sdma_hdr->jt_offset));
    196 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(sdma_hdr->jt_size));
    197 		if (version_minor >= 1) {
    198 			const struct sdma_firmware_header_v1_1 *sdma_v1_1_hdr =
    199 				const_container_of(sdma_hdr, struct sdma_firmware_header_v1_1, v1_0);
    200 			DRM_DEBUG("digest_size: %u\n", le32_to_cpu(sdma_v1_1_hdr->digest_size));
    201 		}
    202 	} else {
    203 		DRM_ERROR("Unknown SDMA ucode version: %u.%u\n",
    204 			  version_major, version_minor);
    205 	}
    206 }
    207 
    208 int amdgpu_ucode_validate(const struct firmware *fw)
    209 {
    210 	const struct common_firmware_header *hdr =
    211 		(const struct common_firmware_header *)fw->data;
    212 
    213 	if (fw->size == le32_to_cpu(hdr->size_bytes))
    214 		return 0;
    215 
    216 	return -EINVAL;
    217 }
    218 
    219 bool amdgpu_ucode_hdr_version(union amdgpu_firmware_header *hdr,
    220 				uint16_t hdr_major, uint16_t hdr_minor)
    221 {
    222 	if ((hdr->common.header_version_major == hdr_major) &&
    223 		(hdr->common.header_version_minor == hdr_minor))
    224 		return false;
    225 	return true;
    226 }
    227 
    228 static int amdgpu_ucode_init_single_fw(struct amdgpu_firmware_info *ucode,
    229 				uint64_t mc_addr, void *kptr)
    230 {
    231 	const struct common_firmware_header *header = NULL;
    232 
    233 	if (NULL == ucode->fw)
    234 		return 0;
    235 
    236 	ucode->mc_addr = mc_addr;
    237 	ucode->kaddr = kptr;
    238 
    239 	header = (const struct common_firmware_header *)ucode->fw->data;
    240 	memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data +
    241 		le32_to_cpu(header->ucode_array_offset_bytes)),
    242 		le32_to_cpu(header->ucode_size_bytes));
    243 
    244 	return 0;
    245 }
    246 
    247 int amdgpu_ucode_init_bo(struct amdgpu_device *adev)
    248 {
    249 	struct amdgpu_bo **bo = &adev->firmware.fw_buf;
    250 	uint64_t fw_mc_addr;
    251 	void *fw_buf_ptr = NULL;
    252 	uint64_t fw_offset = 0;
    253 	int i, err;
    254 	struct amdgpu_firmware_info *ucode = NULL;
    255 	const struct common_firmware_header *header = NULL;
    256 
    257 	err = amdgpu_bo_create(adev, adev->firmware.fw_size, PAGE_SIZE, true,
    258 			AMDGPU_GEM_DOMAIN_GTT, 0, NULL, NULL, bo);
    259 	if (err) {
    260 		dev_err(adev->dev, "(%d) Firmware buffer allocate failed\n", err);
    261 		err = -ENOMEM;
    262 		goto failed;
    263 	}
    264 
    265 	err = amdgpu_bo_reserve(*bo, false);
    266 	if (err) {
    267 		amdgpu_bo_unref(bo);
    268 		dev_err(adev->dev, "(%d) Firmware buffer reserve failed\n", err);
    269 		goto failed;
    270 	}
    271 
    272 	err = amdgpu_bo_pin(*bo, AMDGPU_GEM_DOMAIN_GTT, &fw_mc_addr);
    273 	if (err) {
    274 		amdgpu_bo_unreserve(*bo);
    275 		amdgpu_bo_unref(bo);
    276 		dev_err(adev->dev, "(%d) Firmware buffer pin failed\n", err);
    277 		goto failed;
    278 	}
    279 
    280 	err = amdgpu_bo_kmap(*bo, &fw_buf_ptr);
    281 	if (err) {
    282 		dev_err(adev->dev, "(%d) Firmware buffer kmap failed\n", err);
    283 		amdgpu_bo_unpin(*bo);
    284 		amdgpu_bo_unreserve(*bo);
    285 		amdgpu_bo_unref(bo);
    286 		goto failed;
    287 	}
    288 
    289 	amdgpu_bo_unreserve(*bo);
    290 
    291 	fw_offset = 0;
    292 	for (i = 0; i < AMDGPU_UCODE_ID_MAXIMUM; i++) {
    293 		ucode = &adev->firmware.ucode[i];
    294 		if (ucode->fw) {
    295 			header = (const struct common_firmware_header *)ucode->fw->data;
    296 			amdgpu_ucode_init_single_fw(ucode, fw_mc_addr + fw_offset,
    297 						    fw_buf_ptr + fw_offset);
    298 			fw_offset += ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
    299 		}
    300 	}
    301 
    302 failed:
    303 	if (err)
    304 		adev->firmware.smu_load = false;
    305 
    306 	return err;
    307 }
    308 
    309 int amdgpu_ucode_fini_bo(struct amdgpu_device *adev)
    310 {
    311 	int i;
    312 	struct amdgpu_firmware_info *ucode = NULL;
    313 
    314 	for (i = 0; i < AMDGPU_UCODE_ID_MAXIMUM; i++) {
    315 		ucode = &adev->firmware.ucode[i];
    316 		if (ucode->fw) {
    317 			ucode->mc_addr = 0;
    318 			ucode->kaddr = NULL;
    319 		}
    320 	}
    321 	amdgpu_bo_unref(&adev->firmware.fw_buf);
    322 	adev->firmware.fw_buf = NULL;
    323 
    324 	return 0;
    325 }
    326