xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_ucode.c (revision 2b73d18af7a98bc9907041875c671f63165f1d3e)
1*2b73d18aSriastradh /*	$NetBSD: amdgpu_ucode.c,v 1.8 2021/12/19 12:21:29 riastradh Exp $	*/
2efa246c0Sriastradh 
3efa246c0Sriastradh /*
4efa246c0Sriastradh  * Copyright 2014 Advanced Micro Devices, Inc.
5efa246c0Sriastradh  *
6efa246c0Sriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
7efa246c0Sriastradh  * copy of this software and associated documentation files (the "Software"),
8efa246c0Sriastradh  * to deal in the Software without restriction, including without limitation
9efa246c0Sriastradh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10efa246c0Sriastradh  * and/or sell copies of the Software, and to permit persons to whom the
11efa246c0Sriastradh  * Software is furnished to do so, subject to the following conditions:
12efa246c0Sriastradh  *
13efa246c0Sriastradh  * The above copyright notice and this permission notice shall be included in
14efa246c0Sriastradh  * all copies or substantial portions of the Software.
15efa246c0Sriastradh  *
16efa246c0Sriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17efa246c0Sriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18efa246c0Sriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19efa246c0Sriastradh  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20efa246c0Sriastradh  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21efa246c0Sriastradh  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22efa246c0Sriastradh  * OTHER DEALINGS IN THE SOFTWARE.
23efa246c0Sriastradh  *
24efa246c0Sriastradh  */
25efa246c0Sriastradh 
26efa246c0Sriastradh #include <sys/cdefs.h>
27*2b73d18aSriastradh __KERNEL_RCSID(0, "$NetBSD: amdgpu_ucode.c,v 1.8 2021/12/19 12:21:29 riastradh Exp $");
28efa246c0Sriastradh 
29efa246c0Sriastradh #include <linux/firmware.h>
30efa246c0Sriastradh #include <linux/slab.h>
31efa246c0Sriastradh #include <linux/module.h>
3241ec0267Sriastradh 
33efa246c0Sriastradh #include "amdgpu.h"
34efa246c0Sriastradh #include "amdgpu_ucode.h"
35efa246c0Sriastradh 
361b46a69aSriastradh #include <linux/nbsd-namespace.h>
371b46a69aSriastradh 
amdgpu_ucode_print_common_hdr(const struct common_firmware_header * hdr)38efa246c0Sriastradh static void amdgpu_ucode_print_common_hdr(const struct common_firmware_header *hdr)
39efa246c0Sriastradh {
40efa246c0Sriastradh 	DRM_DEBUG("size_bytes: %u\n", le32_to_cpu(hdr->size_bytes));
41efa246c0Sriastradh 	DRM_DEBUG("header_size_bytes: %u\n", le32_to_cpu(hdr->header_size_bytes));
42efa246c0Sriastradh 	DRM_DEBUG("header_version_major: %u\n", le16_to_cpu(hdr->header_version_major));
43efa246c0Sriastradh 	DRM_DEBUG("header_version_minor: %u\n", le16_to_cpu(hdr->header_version_minor));
44efa246c0Sriastradh 	DRM_DEBUG("ip_version_major: %u\n", le16_to_cpu(hdr->ip_version_major));
45efa246c0Sriastradh 	DRM_DEBUG("ip_version_minor: %u\n", le16_to_cpu(hdr->ip_version_minor));
46efa246c0Sriastradh 	DRM_DEBUG("ucode_version: 0x%08x\n", le32_to_cpu(hdr->ucode_version));
47efa246c0Sriastradh 	DRM_DEBUG("ucode_size_bytes: %u\n", le32_to_cpu(hdr->ucode_size_bytes));
48efa246c0Sriastradh 	DRM_DEBUG("ucode_array_offset_bytes: %u\n",
49efa246c0Sriastradh 		  le32_to_cpu(hdr->ucode_array_offset_bytes));
50efa246c0Sriastradh 	DRM_DEBUG("crc32: 0x%08x\n", le32_to_cpu(hdr->crc32));
51efa246c0Sriastradh }
52efa246c0Sriastradh 
amdgpu_ucode_print_mc_hdr(const struct common_firmware_header * hdr)53efa246c0Sriastradh void amdgpu_ucode_print_mc_hdr(const struct common_firmware_header *hdr)
54efa246c0Sriastradh {
55efa246c0Sriastradh 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
56efa246c0Sriastradh 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
57efa246c0Sriastradh 
58efa246c0Sriastradh 	DRM_DEBUG("MC\n");
59efa246c0Sriastradh 	amdgpu_ucode_print_common_hdr(hdr);
60efa246c0Sriastradh 
61efa246c0Sriastradh 	if (version_major == 1) {
62efa246c0Sriastradh 		const struct mc_firmware_header_v1_0 *mc_hdr =
630d50c49dSriastradh 			const_container_of(hdr, struct mc_firmware_header_v1_0, header);
64efa246c0Sriastradh 
65efa246c0Sriastradh 		DRM_DEBUG("io_debug_size_bytes: %u\n",
66efa246c0Sriastradh 			  le32_to_cpu(mc_hdr->io_debug_size_bytes));
67efa246c0Sriastradh 		DRM_DEBUG("io_debug_array_offset_bytes: %u\n",
68efa246c0Sriastradh 			  le32_to_cpu(mc_hdr->io_debug_array_offset_bytes));
69efa246c0Sriastradh 	} else {
70efa246c0Sriastradh 		DRM_ERROR("Unknown MC ucode version: %u.%u\n", version_major, version_minor);
71efa246c0Sriastradh 	}
72efa246c0Sriastradh }
73efa246c0Sriastradh 
amdgpu_ucode_print_smc_hdr(const struct common_firmware_header * hdr)74efa246c0Sriastradh void amdgpu_ucode_print_smc_hdr(const struct common_firmware_header *hdr)
75efa246c0Sriastradh {
76efa246c0Sriastradh 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
77efa246c0Sriastradh 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
78efa246c0Sriastradh 
79efa246c0Sriastradh 	DRM_DEBUG("SMC\n");
80efa246c0Sriastradh 	amdgpu_ucode_print_common_hdr(hdr);
81efa246c0Sriastradh 
82efa246c0Sriastradh 	if (version_major == 1) {
83efa246c0Sriastradh 		const struct smc_firmware_header_v1_0 *smc_hdr =
840d50c49dSriastradh 			const_container_of(hdr, struct smc_firmware_header_v1_0, header);
85efa246c0Sriastradh 
86efa246c0Sriastradh 		DRM_DEBUG("ucode_start_addr: %u\n", le32_to_cpu(smc_hdr->ucode_start_addr));
8741ec0267Sriastradh 	} else if (version_major == 2) {
8841ec0267Sriastradh 		const struct smc_firmware_header_v1_0 *v1_hdr =
89*2b73d18aSriastradh 			const_container_of(hdr, struct smc_firmware_header_v1_0, header);
9041ec0267Sriastradh 		const struct smc_firmware_header_v2_0 *v2_hdr =
91*2b73d18aSriastradh 			const_container_of(v1_hdr, struct smc_firmware_header_v2_0, v1_0);
9241ec0267Sriastradh 
9341ec0267Sriastradh 		DRM_DEBUG("ppt_offset_bytes: %u\n", le32_to_cpu(v2_hdr->ppt_offset_bytes));
9441ec0267Sriastradh 		DRM_DEBUG("ppt_size_bytes: %u\n", le32_to_cpu(v2_hdr->ppt_size_bytes));
95efa246c0Sriastradh 	} else {
96efa246c0Sriastradh 		DRM_ERROR("Unknown SMC ucode version: %u.%u\n", version_major, version_minor);
97efa246c0Sriastradh 	}
98efa246c0Sriastradh }
99efa246c0Sriastradh 
amdgpu_ucode_print_gfx_hdr(const struct common_firmware_header * hdr)100efa246c0Sriastradh void amdgpu_ucode_print_gfx_hdr(const struct common_firmware_header *hdr)
101efa246c0Sriastradh {
102efa246c0Sriastradh 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
103efa246c0Sriastradh 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
104efa246c0Sriastradh 
105efa246c0Sriastradh 	DRM_DEBUG("GFX\n");
106efa246c0Sriastradh 	amdgpu_ucode_print_common_hdr(hdr);
107efa246c0Sriastradh 
108efa246c0Sriastradh 	if (version_major == 1) {
109efa246c0Sriastradh 		const struct gfx_firmware_header_v1_0 *gfx_hdr =
1100d50c49dSriastradh 			const_container_of(hdr, struct gfx_firmware_header_v1_0, header);
111efa246c0Sriastradh 
112efa246c0Sriastradh 		DRM_DEBUG("ucode_feature_version: %u\n",
113efa246c0Sriastradh 			  le32_to_cpu(gfx_hdr->ucode_feature_version));
114efa246c0Sriastradh 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(gfx_hdr->jt_offset));
115efa246c0Sriastradh 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(gfx_hdr->jt_size));
116efa246c0Sriastradh 	} else {
117efa246c0Sriastradh 		DRM_ERROR("Unknown GFX ucode version: %u.%u\n", version_major, version_minor);
118efa246c0Sriastradh 	}
119efa246c0Sriastradh }
120efa246c0Sriastradh 
amdgpu_ucode_print_rlc_hdr(const struct common_firmware_header * hdr)121efa246c0Sriastradh void amdgpu_ucode_print_rlc_hdr(const struct common_firmware_header *hdr)
122efa246c0Sriastradh {
123efa246c0Sriastradh 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
124efa246c0Sriastradh 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
125efa246c0Sriastradh 
126efa246c0Sriastradh 	DRM_DEBUG("RLC\n");
127efa246c0Sriastradh 	amdgpu_ucode_print_common_hdr(hdr);
128efa246c0Sriastradh 
129efa246c0Sriastradh 	if (version_major == 1) {
130efa246c0Sriastradh 		const struct rlc_firmware_header_v1_0 *rlc_hdr =
1310d50c49dSriastradh 			const_container_of(hdr, struct rlc_firmware_header_v1_0, header);
132efa246c0Sriastradh 
133efa246c0Sriastradh 		DRM_DEBUG("ucode_feature_version: %u\n",
134efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->ucode_feature_version));
135efa246c0Sriastradh 		DRM_DEBUG("save_and_restore_offset: %u\n",
136efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->save_and_restore_offset));
137efa246c0Sriastradh 		DRM_DEBUG("clear_state_descriptor_offset: %u\n",
138efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->clear_state_descriptor_offset));
139efa246c0Sriastradh 		DRM_DEBUG("avail_scratch_ram_locations: %u\n",
140efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->avail_scratch_ram_locations));
141efa246c0Sriastradh 		DRM_DEBUG("master_pkt_description_offset: %u\n",
142efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->master_pkt_description_offset));
143efa246c0Sriastradh 	} else if (version_major == 2) {
144efa246c0Sriastradh 		const struct rlc_firmware_header_v2_0 *rlc_hdr =
1450d50c49dSriastradh 			const_container_of(hdr, struct rlc_firmware_header_v2_0, header);
146efa246c0Sriastradh 
147efa246c0Sriastradh 		DRM_DEBUG("ucode_feature_version: %u\n",
148efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->ucode_feature_version));
149efa246c0Sriastradh 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(rlc_hdr->jt_offset));
150efa246c0Sriastradh 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(rlc_hdr->jt_size));
151efa246c0Sriastradh 		DRM_DEBUG("save_and_restore_offset: %u\n",
152efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->save_and_restore_offset));
153efa246c0Sriastradh 		DRM_DEBUG("clear_state_descriptor_offset: %u\n",
154efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->clear_state_descriptor_offset));
155efa246c0Sriastradh 		DRM_DEBUG("avail_scratch_ram_locations: %u\n",
156efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->avail_scratch_ram_locations));
157efa246c0Sriastradh 		DRM_DEBUG("reg_restore_list_size: %u\n",
158efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->reg_restore_list_size));
159efa246c0Sriastradh 		DRM_DEBUG("reg_list_format_start: %u\n",
160efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->reg_list_format_start));
161efa246c0Sriastradh 		DRM_DEBUG("reg_list_format_separate_start: %u\n",
162efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->reg_list_format_separate_start));
163efa246c0Sriastradh 		DRM_DEBUG("starting_offsets_start: %u\n",
164efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->starting_offsets_start));
165efa246c0Sriastradh 		DRM_DEBUG("reg_list_format_size_bytes: %u\n",
166efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->reg_list_format_size_bytes));
167efa246c0Sriastradh 		DRM_DEBUG("reg_list_format_array_offset_bytes: %u\n",
168efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->reg_list_format_array_offset_bytes));
169efa246c0Sriastradh 		DRM_DEBUG("reg_list_size_bytes: %u\n",
170efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->reg_list_size_bytes));
171efa246c0Sriastradh 		DRM_DEBUG("reg_list_array_offset_bytes: %u\n",
172efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->reg_list_array_offset_bytes));
173efa246c0Sriastradh 		DRM_DEBUG("reg_list_format_separate_size_bytes: %u\n",
174efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->reg_list_format_separate_size_bytes));
175efa246c0Sriastradh 		DRM_DEBUG("reg_list_format_separate_array_offset_bytes: %u\n",
176efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->reg_list_format_separate_array_offset_bytes));
177efa246c0Sriastradh 		DRM_DEBUG("reg_list_separate_size_bytes: %u\n",
178efa246c0Sriastradh 			  le32_to_cpu(rlc_hdr->reg_list_separate_size_bytes));
17941ec0267Sriastradh 		DRM_DEBUG("reg_list_separate_array_offset_bytes: %u\n",
18041ec0267Sriastradh 			  le32_to_cpu(rlc_hdr->reg_list_separate_array_offset_bytes));
18141ec0267Sriastradh 		if (version_minor == 1) {
18241ec0267Sriastradh 			const struct rlc_firmware_header_v2_1 *v2_1 =
183*2b73d18aSriastradh 				const_container_of(rlc_hdr, struct rlc_firmware_header_v2_1, v2_0);
18441ec0267Sriastradh 			DRM_DEBUG("reg_list_format_direct_reg_list_length: %u\n",
18541ec0267Sriastradh 				  le32_to_cpu(v2_1->reg_list_format_direct_reg_list_length));
18641ec0267Sriastradh 			DRM_DEBUG("save_restore_list_cntl_ucode_ver: %u\n",
18741ec0267Sriastradh 				  le32_to_cpu(v2_1->save_restore_list_cntl_ucode_ver));
18841ec0267Sriastradh 			DRM_DEBUG("save_restore_list_cntl_feature_ver: %u\n",
18941ec0267Sriastradh 				  le32_to_cpu(v2_1->save_restore_list_cntl_feature_ver));
19041ec0267Sriastradh 			DRM_DEBUG("save_restore_list_cntl_size_bytes %u\n",
19141ec0267Sriastradh 				  le32_to_cpu(v2_1->save_restore_list_cntl_size_bytes));
19241ec0267Sriastradh 			DRM_DEBUG("save_restore_list_cntl_offset_bytes: %u\n",
19341ec0267Sriastradh 				  le32_to_cpu(v2_1->save_restore_list_cntl_offset_bytes));
19441ec0267Sriastradh 			DRM_DEBUG("save_restore_list_gpm_ucode_ver: %u\n",
19541ec0267Sriastradh 				  le32_to_cpu(v2_1->save_restore_list_gpm_ucode_ver));
19641ec0267Sriastradh 			DRM_DEBUG("save_restore_list_gpm_feature_ver: %u\n",
19741ec0267Sriastradh 				  le32_to_cpu(v2_1->save_restore_list_gpm_feature_ver));
19841ec0267Sriastradh 			DRM_DEBUG("save_restore_list_gpm_size_bytes %u\n",
19941ec0267Sriastradh 				  le32_to_cpu(v2_1->save_restore_list_gpm_size_bytes));
20041ec0267Sriastradh 			DRM_DEBUG("save_restore_list_gpm_offset_bytes: %u\n",
20141ec0267Sriastradh 				  le32_to_cpu(v2_1->save_restore_list_gpm_offset_bytes));
20241ec0267Sriastradh 			DRM_DEBUG("save_restore_list_srm_ucode_ver: %u\n",
20341ec0267Sriastradh 				  le32_to_cpu(v2_1->save_restore_list_srm_ucode_ver));
20441ec0267Sriastradh 			DRM_DEBUG("save_restore_list_srm_feature_ver: %u\n",
20541ec0267Sriastradh 				  le32_to_cpu(v2_1->save_restore_list_srm_feature_ver));
20641ec0267Sriastradh 			DRM_DEBUG("save_restore_list_srm_size_bytes %u\n",
20741ec0267Sriastradh 				  le32_to_cpu(v2_1->save_restore_list_srm_size_bytes));
20841ec0267Sriastradh 			DRM_DEBUG("save_restore_list_srm_offset_bytes: %u\n",
20941ec0267Sriastradh 				  le32_to_cpu(v2_1->save_restore_list_srm_offset_bytes));
21041ec0267Sriastradh 		}
211efa246c0Sriastradh 	} else {
212efa246c0Sriastradh 		DRM_ERROR("Unknown RLC ucode version: %u.%u\n", version_major, version_minor);
213efa246c0Sriastradh 	}
214efa246c0Sriastradh }
215efa246c0Sriastradh 
amdgpu_ucode_print_sdma_hdr(const struct common_firmware_header * hdr)216efa246c0Sriastradh void amdgpu_ucode_print_sdma_hdr(const struct common_firmware_header *hdr)
217efa246c0Sriastradh {
218efa246c0Sriastradh 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
219efa246c0Sriastradh 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
220efa246c0Sriastradh 
221efa246c0Sriastradh 	DRM_DEBUG("SDMA\n");
222efa246c0Sriastradh 	amdgpu_ucode_print_common_hdr(hdr);
223efa246c0Sriastradh 
224efa246c0Sriastradh 	if (version_major == 1) {
225efa246c0Sriastradh 		const struct sdma_firmware_header_v1_0 *sdma_hdr =
2260d50c49dSriastradh 			const_container_of(hdr, struct sdma_firmware_header_v1_0, header);
227efa246c0Sriastradh 
228efa246c0Sriastradh 		DRM_DEBUG("ucode_feature_version: %u\n",
229efa246c0Sriastradh 			  le32_to_cpu(sdma_hdr->ucode_feature_version));
230efa246c0Sriastradh 		DRM_DEBUG("ucode_change_version: %u\n",
231efa246c0Sriastradh 			  le32_to_cpu(sdma_hdr->ucode_change_version));
232efa246c0Sriastradh 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(sdma_hdr->jt_offset));
233efa246c0Sriastradh 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(sdma_hdr->jt_size));
234efa246c0Sriastradh 		if (version_minor >= 1) {
235efa246c0Sriastradh 			const struct sdma_firmware_header_v1_1 *sdma_v1_1_hdr =
2360d50c49dSriastradh 				const_container_of(sdma_hdr, struct sdma_firmware_header_v1_1, v1_0);
237efa246c0Sriastradh 			DRM_DEBUG("digest_size: %u\n", le32_to_cpu(sdma_v1_1_hdr->digest_size));
238efa246c0Sriastradh 		}
239efa246c0Sriastradh 	} else {
240efa246c0Sriastradh 		DRM_ERROR("Unknown SDMA ucode version: %u.%u\n",
241efa246c0Sriastradh 			  version_major, version_minor);
242efa246c0Sriastradh 	}
243efa246c0Sriastradh }
244efa246c0Sriastradh 
amdgpu_ucode_print_psp_hdr(const struct common_firmware_header * hdr)24541ec0267Sriastradh void amdgpu_ucode_print_psp_hdr(const struct common_firmware_header *hdr)
24641ec0267Sriastradh {
24741ec0267Sriastradh 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
24841ec0267Sriastradh 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
24941ec0267Sriastradh 
25041ec0267Sriastradh 	DRM_DEBUG("PSP\n");
25141ec0267Sriastradh 	amdgpu_ucode_print_common_hdr(hdr);
25241ec0267Sriastradh 
25341ec0267Sriastradh 	if (version_major == 1) {
25441ec0267Sriastradh 		const struct psp_firmware_header_v1_0 *psp_hdr =
255*2b73d18aSriastradh 			const_container_of(hdr, struct psp_firmware_header_v1_0, header);
25641ec0267Sriastradh 
25741ec0267Sriastradh 		DRM_DEBUG("ucode_feature_version: %u\n",
25841ec0267Sriastradh 			  le32_to_cpu(psp_hdr->ucode_feature_version));
25941ec0267Sriastradh 		DRM_DEBUG("sos_offset_bytes: %u\n",
26041ec0267Sriastradh 			  le32_to_cpu(psp_hdr->sos_offset_bytes));
26141ec0267Sriastradh 		DRM_DEBUG("sos_size_bytes: %u\n",
26241ec0267Sriastradh 			  le32_to_cpu(psp_hdr->sos_size_bytes));
26341ec0267Sriastradh 		if (version_minor == 1) {
26441ec0267Sriastradh 			const struct psp_firmware_header_v1_1 *psp_hdr_v1_1 =
265*2b73d18aSriastradh 				const_container_of(psp_hdr, struct psp_firmware_header_v1_1, v1_0);
26641ec0267Sriastradh 			DRM_DEBUG("toc_header_version: %u\n",
26741ec0267Sriastradh 				  le32_to_cpu(psp_hdr_v1_1->toc_header_version));
26841ec0267Sriastradh 			DRM_DEBUG("toc_offset_bytes: %u\n",
26941ec0267Sriastradh 				  le32_to_cpu(psp_hdr_v1_1->toc_offset_bytes));
27041ec0267Sriastradh 			DRM_DEBUG("toc_size_bytes: %u\n",
27141ec0267Sriastradh 				  le32_to_cpu(psp_hdr_v1_1->toc_size_bytes));
27241ec0267Sriastradh 			DRM_DEBUG("kdb_header_version: %u\n",
27341ec0267Sriastradh 				  le32_to_cpu(psp_hdr_v1_1->kdb_header_version));
27441ec0267Sriastradh 			DRM_DEBUG("kdb_offset_bytes: %u\n",
27541ec0267Sriastradh 				  le32_to_cpu(psp_hdr_v1_1->kdb_offset_bytes));
27641ec0267Sriastradh 			DRM_DEBUG("kdb_size_bytes: %u\n",
27741ec0267Sriastradh 				  le32_to_cpu(psp_hdr_v1_1->kdb_size_bytes));
27841ec0267Sriastradh 		}
27941ec0267Sriastradh 		if (version_minor == 2) {
28041ec0267Sriastradh 			const struct psp_firmware_header_v1_2 *psp_hdr_v1_2 =
281*2b73d18aSriastradh 				const_container_of(psp_hdr, struct psp_firmware_header_v1_2, v1_0);
28241ec0267Sriastradh 			DRM_DEBUG("kdb_header_version: %u\n",
28341ec0267Sriastradh 				  le32_to_cpu(psp_hdr_v1_2->kdb_header_version));
28441ec0267Sriastradh 			DRM_DEBUG("kdb_offset_bytes: %u\n",
28541ec0267Sriastradh 				  le32_to_cpu(psp_hdr_v1_2->kdb_offset_bytes));
28641ec0267Sriastradh 			DRM_DEBUG("kdb_size_bytes: %u\n",
28741ec0267Sriastradh 				  le32_to_cpu(psp_hdr_v1_2->kdb_size_bytes));
28841ec0267Sriastradh 		}
28941ec0267Sriastradh 	} else {
29041ec0267Sriastradh 		DRM_ERROR("Unknown PSP ucode version: %u.%u\n",
29141ec0267Sriastradh 			  version_major, version_minor);
29241ec0267Sriastradh 	}
29341ec0267Sriastradh }
29441ec0267Sriastradh 
amdgpu_ucode_print_gpu_info_hdr(const struct common_firmware_header * hdr)29541ec0267Sriastradh void amdgpu_ucode_print_gpu_info_hdr(const struct common_firmware_header *hdr)
29641ec0267Sriastradh {
29741ec0267Sriastradh 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
29841ec0267Sriastradh 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
29941ec0267Sriastradh 
30041ec0267Sriastradh 	DRM_DEBUG("GPU_INFO\n");
30141ec0267Sriastradh 	amdgpu_ucode_print_common_hdr(hdr);
30241ec0267Sriastradh 
30341ec0267Sriastradh 	if (version_major == 1) {
30441ec0267Sriastradh 		const struct gpu_info_firmware_header_v1_0 *gpu_info_hdr =
305*2b73d18aSriastradh 			const_container_of(hdr, struct gpu_info_firmware_header_v1_0, header);
30641ec0267Sriastradh 
30741ec0267Sriastradh 		DRM_DEBUG("version_major: %u\n",
30841ec0267Sriastradh 			  le16_to_cpu(gpu_info_hdr->version_major));
30941ec0267Sriastradh 		DRM_DEBUG("version_minor: %u\n",
31041ec0267Sriastradh 			  le16_to_cpu(gpu_info_hdr->version_minor));
31141ec0267Sriastradh 	} else {
31241ec0267Sriastradh 		DRM_ERROR("Unknown gpu_info ucode version: %u.%u\n", version_major, version_minor);
31341ec0267Sriastradh 	}
31441ec0267Sriastradh }
31541ec0267Sriastradh 
amdgpu_ucode_validate(const struct firmware * fw)316efa246c0Sriastradh int amdgpu_ucode_validate(const struct firmware *fw)
317efa246c0Sriastradh {
318efa246c0Sriastradh 	const struct common_firmware_header *hdr =
319efa246c0Sriastradh 		(const struct common_firmware_header *)fw->data;
320efa246c0Sriastradh 
321efa246c0Sriastradh 	if (fw->size == le32_to_cpu(hdr->size_bytes))
322efa246c0Sriastradh 		return 0;
323efa246c0Sriastradh 
324efa246c0Sriastradh 	return -EINVAL;
325efa246c0Sriastradh }
326efa246c0Sriastradh 
amdgpu_ucode_hdr_version(union amdgpu_firmware_header * hdr,uint16_t hdr_major,uint16_t hdr_minor)327efa246c0Sriastradh bool amdgpu_ucode_hdr_version(union amdgpu_firmware_header *hdr,
328efa246c0Sriastradh 				uint16_t hdr_major, uint16_t hdr_minor)
329efa246c0Sriastradh {
330efa246c0Sriastradh 	if ((hdr->common.header_version_major == hdr_major) &&
331efa246c0Sriastradh 		(hdr->common.header_version_minor == hdr_minor))
332efa246c0Sriastradh 		return false;
333efa246c0Sriastradh 	return true;
334efa246c0Sriastradh }
335efa246c0Sriastradh 
33641ec0267Sriastradh enum amdgpu_firmware_load_type
amdgpu_ucode_get_load_type(struct amdgpu_device * adev,int load_type)33741ec0267Sriastradh amdgpu_ucode_get_load_type(struct amdgpu_device *adev, int load_type)
33841ec0267Sriastradh {
33941ec0267Sriastradh 	switch (adev->asic_type) {
34041ec0267Sriastradh #ifdef CONFIG_DRM_AMDGPU_SI
34141ec0267Sriastradh 	case CHIP_TAHITI:
34241ec0267Sriastradh 	case CHIP_PITCAIRN:
34341ec0267Sriastradh 	case CHIP_VERDE:
34441ec0267Sriastradh 	case CHIP_OLAND:
34541ec0267Sriastradh 	case CHIP_HAINAN:
34641ec0267Sriastradh 		return AMDGPU_FW_LOAD_DIRECT;
34741ec0267Sriastradh #endif
34841ec0267Sriastradh #ifdef CONFIG_DRM_AMDGPU_CIK
34941ec0267Sriastradh 	case CHIP_BONAIRE:
35041ec0267Sriastradh 	case CHIP_KAVERI:
35141ec0267Sriastradh 	case CHIP_KABINI:
35241ec0267Sriastradh 	case CHIP_HAWAII:
35341ec0267Sriastradh 	case CHIP_MULLINS:
35441ec0267Sriastradh 		return AMDGPU_FW_LOAD_DIRECT;
35541ec0267Sriastradh #endif
35641ec0267Sriastradh 	case CHIP_TOPAZ:
35741ec0267Sriastradh 	case CHIP_TONGA:
35841ec0267Sriastradh 	case CHIP_FIJI:
35941ec0267Sriastradh 	case CHIP_CARRIZO:
36041ec0267Sriastradh 	case CHIP_STONEY:
36141ec0267Sriastradh 	case CHIP_POLARIS10:
36241ec0267Sriastradh 	case CHIP_POLARIS11:
36341ec0267Sriastradh 	case CHIP_POLARIS12:
36441ec0267Sriastradh 	case CHIP_VEGAM:
36541ec0267Sriastradh 		return AMDGPU_FW_LOAD_SMU;
36641ec0267Sriastradh 	case CHIP_VEGA10:
36741ec0267Sriastradh 	case CHIP_RAVEN:
36841ec0267Sriastradh 	case CHIP_VEGA12:
36941ec0267Sriastradh 	case CHIP_VEGA20:
37041ec0267Sriastradh 	case CHIP_ARCTURUS:
37141ec0267Sriastradh 	case CHIP_RENOIR:
37241ec0267Sriastradh 	case CHIP_NAVI10:
37341ec0267Sriastradh 	case CHIP_NAVI14:
37441ec0267Sriastradh 	case CHIP_NAVI12:
37541ec0267Sriastradh 		if (!load_type)
37641ec0267Sriastradh 			return AMDGPU_FW_LOAD_DIRECT;
37741ec0267Sriastradh 		else
37841ec0267Sriastradh 			return AMDGPU_FW_LOAD_PSP;
37941ec0267Sriastradh 
38041ec0267Sriastradh 	default:
38141ec0267Sriastradh 		DRM_ERROR("Unknown firmware load type\n");
38241ec0267Sriastradh 	}
38341ec0267Sriastradh 
38441ec0267Sriastradh 	return AMDGPU_FW_LOAD_DIRECT;
38541ec0267Sriastradh }
38641ec0267Sriastradh 
387*2b73d18aSriastradh #ifndef __NetBSD__		/* XXX amdgpu sysfs */
388*2b73d18aSriastradh 
38941ec0267Sriastradh #define FW_VERSION_ATTR(name, mode, field)				\
39041ec0267Sriastradh static ssize_t show_##name(struct device *dev,				\
39141ec0267Sriastradh 			  struct device_attribute *attr,		\
39241ec0267Sriastradh 			  char *buf)					\
39341ec0267Sriastradh {									\
39441ec0267Sriastradh 	struct drm_device *ddev = dev_get_drvdata(dev);			\
39541ec0267Sriastradh 	struct amdgpu_device *adev = ddev->dev_private;			\
39641ec0267Sriastradh 									\
39741ec0267Sriastradh 	return snprintf(buf, PAGE_SIZE, "0x%08x\n", adev->field);	\
39841ec0267Sriastradh }									\
39941ec0267Sriastradh static DEVICE_ATTR(name, mode, show_##name, NULL)
40041ec0267Sriastradh 
40141ec0267Sriastradh FW_VERSION_ATTR(vce_fw_version, 0444, vce.fw_version);
40241ec0267Sriastradh FW_VERSION_ATTR(uvd_fw_version, 0444, uvd.fw_version);
40341ec0267Sriastradh FW_VERSION_ATTR(mc_fw_version, 0444, gmc.fw_version);
40441ec0267Sriastradh FW_VERSION_ATTR(me_fw_version, 0444, gfx.me_fw_version);
40541ec0267Sriastradh FW_VERSION_ATTR(pfp_fw_version, 0444, gfx.pfp_fw_version);
40641ec0267Sriastradh FW_VERSION_ATTR(ce_fw_version, 0444, gfx.ce_fw_version);
40741ec0267Sriastradh FW_VERSION_ATTR(rlc_fw_version, 0444, gfx.rlc_fw_version);
40841ec0267Sriastradh FW_VERSION_ATTR(rlc_srlc_fw_version, 0444, gfx.rlc_srlc_fw_version);
40941ec0267Sriastradh FW_VERSION_ATTR(rlc_srlg_fw_version, 0444, gfx.rlc_srlg_fw_version);
41041ec0267Sriastradh FW_VERSION_ATTR(rlc_srls_fw_version, 0444, gfx.rlc_srls_fw_version);
41141ec0267Sriastradh FW_VERSION_ATTR(mec_fw_version, 0444, gfx.mec_fw_version);
41241ec0267Sriastradh FW_VERSION_ATTR(mec2_fw_version, 0444, gfx.mec2_fw_version);
41341ec0267Sriastradh FW_VERSION_ATTR(sos_fw_version, 0444, psp.sos_fw_version);
41441ec0267Sriastradh FW_VERSION_ATTR(asd_fw_version, 0444, psp.asd_fw_version);
41541ec0267Sriastradh FW_VERSION_ATTR(ta_ras_fw_version, 0444, psp.ta_fw_version);
41641ec0267Sriastradh FW_VERSION_ATTR(ta_xgmi_fw_version, 0444, psp.ta_fw_version);
41741ec0267Sriastradh FW_VERSION_ATTR(smc_fw_version, 0444, pm.fw_version);
41841ec0267Sriastradh FW_VERSION_ATTR(sdma_fw_version, 0444, sdma.instance[0].fw_version);
41941ec0267Sriastradh FW_VERSION_ATTR(sdma2_fw_version, 0444, sdma.instance[1].fw_version);
42041ec0267Sriastradh FW_VERSION_ATTR(vcn_fw_version, 0444, vcn.fw_version);
42141ec0267Sriastradh FW_VERSION_ATTR(dmcu_fw_version, 0444, dm.dmcu_fw_version);
42241ec0267Sriastradh 
42341ec0267Sriastradh static struct attribute *fw_attrs[] = {
42441ec0267Sriastradh 	&dev_attr_vce_fw_version.attr, &dev_attr_uvd_fw_version.attr,
42541ec0267Sriastradh 	&dev_attr_mc_fw_version.attr, &dev_attr_me_fw_version.attr,
42641ec0267Sriastradh 	&dev_attr_pfp_fw_version.attr, &dev_attr_ce_fw_version.attr,
42741ec0267Sriastradh 	&dev_attr_rlc_fw_version.attr, &dev_attr_rlc_srlc_fw_version.attr,
42841ec0267Sriastradh 	&dev_attr_rlc_srlg_fw_version.attr, &dev_attr_rlc_srls_fw_version.attr,
42941ec0267Sriastradh 	&dev_attr_mec_fw_version.attr, &dev_attr_mec2_fw_version.attr,
43041ec0267Sriastradh 	&dev_attr_sos_fw_version.attr, &dev_attr_asd_fw_version.attr,
43141ec0267Sriastradh 	&dev_attr_ta_ras_fw_version.attr, &dev_attr_ta_xgmi_fw_version.attr,
43241ec0267Sriastradh 	&dev_attr_smc_fw_version.attr, &dev_attr_sdma_fw_version.attr,
43341ec0267Sriastradh 	&dev_attr_sdma2_fw_version.attr, &dev_attr_vcn_fw_version.attr,
43441ec0267Sriastradh 	&dev_attr_dmcu_fw_version.attr, NULL
43541ec0267Sriastradh };
43641ec0267Sriastradh 
43741ec0267Sriastradh static const struct attribute_group fw_attr_group = {
43841ec0267Sriastradh 	.name = "fw_version",
43941ec0267Sriastradh 	.attrs = fw_attrs
44041ec0267Sriastradh };
44141ec0267Sriastradh 
442*2b73d18aSriastradh #endif	/* __NetBSD__ */
443*2b73d18aSriastradh 
amdgpu_ucode_sysfs_init(struct amdgpu_device * adev)44441ec0267Sriastradh int amdgpu_ucode_sysfs_init(struct amdgpu_device *adev)
44541ec0267Sriastradh {
446*2b73d18aSriastradh #ifdef __NetBSD__
447*2b73d18aSriastradh 	return 0;
448*2b73d18aSriastradh #else
44941ec0267Sriastradh 	return sysfs_create_group(&adev->dev->kobj, &fw_attr_group);
450*2b73d18aSriastradh #endif
45141ec0267Sriastradh }
45241ec0267Sriastradh 
amdgpu_ucode_sysfs_fini(struct amdgpu_device * adev)45341ec0267Sriastradh void amdgpu_ucode_sysfs_fini(struct amdgpu_device *adev)
45441ec0267Sriastradh {
455*2b73d18aSriastradh #ifndef __NetBSD__
45641ec0267Sriastradh 	sysfs_remove_group(&adev->dev->kobj, &fw_attr_group);
457*2b73d18aSriastradh #endif
45841ec0267Sriastradh }
45941ec0267Sriastradh 
amdgpu_ucode_init_single_fw(struct amdgpu_device * adev,struct amdgpu_firmware_info * ucode,uint64_t mc_addr,void * kptr)46041ec0267Sriastradh static int amdgpu_ucode_init_single_fw(struct amdgpu_device *adev,
46141ec0267Sriastradh 				       struct amdgpu_firmware_info *ucode,
462efa246c0Sriastradh 				       uint64_t mc_addr, void *kptr)
463efa246c0Sriastradh {
464efa246c0Sriastradh 	const struct common_firmware_header *header = NULL;
46541ec0267Sriastradh 	const struct gfx_firmware_header_v1_0 *cp_hdr = NULL;
46641ec0267Sriastradh 	const struct dmcu_firmware_header_v1_0 *dmcu_hdr = NULL;
46741ec0267Sriastradh 	const struct dmcub_firmware_header_v1_0 *dmcub_hdr = NULL;
468efa246c0Sriastradh 
469efa246c0Sriastradh 	if (NULL == ucode->fw)
470efa246c0Sriastradh 		return 0;
471efa246c0Sriastradh 
472efa246c0Sriastradh 	ucode->mc_addr = mc_addr;
473efa246c0Sriastradh 	ucode->kaddr = kptr;
474efa246c0Sriastradh 
47541ec0267Sriastradh 	if (ucode->ucode_id == AMDGPU_UCODE_ID_STORAGE)
47641ec0267Sriastradh 		return 0;
47741ec0267Sriastradh 
478efa246c0Sriastradh 	header = (const struct common_firmware_header *)ucode->fw->data;
47941ec0267Sriastradh 	cp_hdr = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
48041ec0267Sriastradh 	dmcu_hdr = (const struct dmcu_firmware_header_v1_0 *)ucode->fw->data;
48141ec0267Sriastradh 	dmcub_hdr = (const struct dmcub_firmware_header_v1_0 *)ucode->fw->data;
48241ec0267Sriastradh 
48341ec0267Sriastradh 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP ||
48441ec0267Sriastradh 	    (ucode->ucode_id != AMDGPU_UCODE_ID_CP_MEC1 &&
48541ec0267Sriastradh 	     ucode->ucode_id != AMDGPU_UCODE_ID_CP_MEC2 &&
48641ec0267Sriastradh 	     ucode->ucode_id != AMDGPU_UCODE_ID_CP_MEC1_JT &&
48741ec0267Sriastradh 	     ucode->ucode_id != AMDGPU_UCODE_ID_CP_MEC2_JT &&
48841ec0267Sriastradh 	     ucode->ucode_id != AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL &&
48941ec0267Sriastradh 	     ucode->ucode_id != AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM &&
49041ec0267Sriastradh 	     ucode->ucode_id != AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM &&
49141ec0267Sriastradh 		 ucode->ucode_id != AMDGPU_UCODE_ID_DMCU_ERAM &&
49241ec0267Sriastradh 		 ucode->ucode_id != AMDGPU_UCODE_ID_DMCU_INTV &&
49341ec0267Sriastradh 		 ucode->ucode_id != AMDGPU_UCODE_ID_DMCUB)) {
49441ec0267Sriastradh 		ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes);
49541ec0267Sriastradh 
496efa246c0Sriastradh 		memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data +
497efa246c0Sriastradh 					      le32_to_cpu(header->ucode_array_offset_bytes)),
49841ec0267Sriastradh 		       ucode->ucode_size);
49941ec0267Sriastradh 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1 ||
50041ec0267Sriastradh 		   ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2) {
50141ec0267Sriastradh 		ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes) -
50241ec0267Sriastradh 			le32_to_cpu(cp_hdr->jt_size) * 4;
50341ec0267Sriastradh 
50441ec0267Sriastradh 		memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data +
50541ec0267Sriastradh 					      le32_to_cpu(header->ucode_array_offset_bytes)),
50641ec0267Sriastradh 		       ucode->ucode_size);
50741ec0267Sriastradh 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
50841ec0267Sriastradh 		   ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT) {
50941ec0267Sriastradh 		ucode->ucode_size = le32_to_cpu(cp_hdr->jt_size) * 4;
51041ec0267Sriastradh 
51141ec0267Sriastradh 		memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data +
51241ec0267Sriastradh 					      le32_to_cpu(header->ucode_array_offset_bytes) +
51341ec0267Sriastradh 					      le32_to_cpu(cp_hdr->jt_offset) * 4),
51441ec0267Sriastradh 		       ucode->ucode_size);
51541ec0267Sriastradh 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_DMCU_ERAM) {
51641ec0267Sriastradh 		ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes) -
51741ec0267Sriastradh 				le32_to_cpu(dmcu_hdr->intv_size_bytes);
51841ec0267Sriastradh 
51941ec0267Sriastradh 		memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data +
52041ec0267Sriastradh 					      le32_to_cpu(header->ucode_array_offset_bytes)),
52141ec0267Sriastradh 		       ucode->ucode_size);
52241ec0267Sriastradh 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_DMCU_INTV) {
52341ec0267Sriastradh 		ucode->ucode_size = le32_to_cpu(dmcu_hdr->intv_size_bytes);
52441ec0267Sriastradh 
52541ec0267Sriastradh 		memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data +
52641ec0267Sriastradh 					      le32_to_cpu(header->ucode_array_offset_bytes) +
52741ec0267Sriastradh 					      le32_to_cpu(dmcu_hdr->intv_offset_bytes)),
52841ec0267Sriastradh 		       ucode->ucode_size);
52941ec0267Sriastradh 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_DMCUB) {
53041ec0267Sriastradh 		ucode->ucode_size = le32_to_cpu(dmcub_hdr->inst_const_bytes);
53141ec0267Sriastradh 		memcpy(ucode->kaddr,
53241ec0267Sriastradh 		       (void *)((uint8_t *)ucode->fw->data +
53341ec0267Sriastradh 				le32_to_cpu(header->ucode_array_offset_bytes)),
53441ec0267Sriastradh 		       ucode->ucode_size);
53541ec0267Sriastradh 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL) {
53641ec0267Sriastradh 		ucode->ucode_size = adev->gfx.rlc.save_restore_list_cntl_size_bytes;
53741ec0267Sriastradh 		memcpy(ucode->kaddr, adev->gfx.rlc.save_restore_list_cntl,
53841ec0267Sriastradh 		       ucode->ucode_size);
53941ec0267Sriastradh 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM) {
54041ec0267Sriastradh 		ucode->ucode_size = adev->gfx.rlc.save_restore_list_gpm_size_bytes;
54141ec0267Sriastradh 		memcpy(ucode->kaddr, adev->gfx.rlc.save_restore_list_gpm,
54241ec0267Sriastradh 		       ucode->ucode_size);
54341ec0267Sriastradh 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM) {
54441ec0267Sriastradh 		ucode->ucode_size = adev->gfx.rlc.save_restore_list_srm_size_bytes;
54541ec0267Sriastradh 		memcpy(ucode->kaddr, adev->gfx.rlc.save_restore_list_srm,
54641ec0267Sriastradh 		       ucode->ucode_size);
54741ec0267Sriastradh 	}
548efa246c0Sriastradh 
549efa246c0Sriastradh 	return 0;
550efa246c0Sriastradh }
551efa246c0Sriastradh 
amdgpu_ucode_patch_jt(struct amdgpu_firmware_info * ucode,uint64_t mc_addr,void * kptr)55241ec0267Sriastradh static int amdgpu_ucode_patch_jt(struct amdgpu_firmware_info *ucode,
55341ec0267Sriastradh 				uint64_t mc_addr, void *kptr)
55441ec0267Sriastradh {
55541ec0267Sriastradh 	const struct gfx_firmware_header_v1_0 *header = NULL;
55641ec0267Sriastradh 	const struct common_firmware_header *comm_hdr = NULL;
55741ec0267Sriastradh 	uint8_t* src_addr = NULL;
55841ec0267Sriastradh 	uint8_t* dst_addr = NULL;
55941ec0267Sriastradh 
56041ec0267Sriastradh 	if (NULL == ucode->fw)
56141ec0267Sriastradh 		return 0;
56241ec0267Sriastradh 
56341ec0267Sriastradh 	comm_hdr = (const struct common_firmware_header *)ucode->fw->data;
56441ec0267Sriastradh 	header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
56541ec0267Sriastradh 	dst_addr = ucode->kaddr +
56641ec0267Sriastradh 			   ALIGN(le32_to_cpu(comm_hdr->ucode_size_bytes),
56741ec0267Sriastradh 			   PAGE_SIZE);
56841ec0267Sriastradh 	src_addr = (uint8_t *)ucode->fw->data +
56941ec0267Sriastradh 			   le32_to_cpu(comm_hdr->ucode_array_offset_bytes) +
57041ec0267Sriastradh 			   (le32_to_cpu(header->jt_offset) * 4);
57141ec0267Sriastradh 	memcpy(dst_addr, src_addr, le32_to_cpu(header->jt_size) * 4);
57241ec0267Sriastradh 
57341ec0267Sriastradh 	return 0;
57441ec0267Sriastradh }
57541ec0267Sriastradh 
amdgpu_ucode_create_bo(struct amdgpu_device * adev)57641ec0267Sriastradh int amdgpu_ucode_create_bo(struct amdgpu_device *adev)
57741ec0267Sriastradh {
57841ec0267Sriastradh 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_DIRECT) {
57941ec0267Sriastradh 		amdgpu_bo_create_kernel(adev, adev->firmware.fw_size, PAGE_SIZE,
58041ec0267Sriastradh 			amdgpu_sriov_vf(adev) ? AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
58141ec0267Sriastradh 			&adev->firmware.fw_buf,
58241ec0267Sriastradh 			&adev->firmware.fw_buf_mc,
58341ec0267Sriastradh 			&adev->firmware.fw_buf_ptr);
58441ec0267Sriastradh 		if (!adev->firmware.fw_buf) {
58541ec0267Sriastradh 			dev_err(adev->dev, "failed to create kernel buffer for firmware.fw_buf\n");
58641ec0267Sriastradh 			return -ENOMEM;
58741ec0267Sriastradh 		} else if (amdgpu_sriov_vf(adev)) {
58841ec0267Sriastradh 			memset(adev->firmware.fw_buf_ptr, 0, adev->firmware.fw_size);
58941ec0267Sriastradh 		}
59041ec0267Sriastradh 	}
59141ec0267Sriastradh 	return 0;
59241ec0267Sriastradh }
59341ec0267Sriastradh 
amdgpu_ucode_free_bo(struct amdgpu_device * adev)59441ec0267Sriastradh void amdgpu_ucode_free_bo(struct amdgpu_device *adev)
59541ec0267Sriastradh {
59641ec0267Sriastradh 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_DIRECT)
59741ec0267Sriastradh 		amdgpu_bo_free_kernel(&adev->firmware.fw_buf,
59841ec0267Sriastradh 		&adev->firmware.fw_buf_mc,
59941ec0267Sriastradh 		&adev->firmware.fw_buf_ptr);
60041ec0267Sriastradh }
60141ec0267Sriastradh 
amdgpu_ucode_init_bo(struct amdgpu_device * adev)602efa246c0Sriastradh int amdgpu_ucode_init_bo(struct amdgpu_device *adev)
603efa246c0Sriastradh {
604efa246c0Sriastradh 	uint64_t fw_offset = 0;
605efa246c0Sriastradh 	int i;
606efa246c0Sriastradh 	struct amdgpu_firmware_info *ucode = NULL;
607efa246c0Sriastradh 
60841ec0267Sriastradh  /* for baremetal, the ucode is allocated in gtt, so don't need to fill the bo when reset/suspend */
60941ec0267Sriastradh 	if (!amdgpu_sriov_vf(adev) && (adev->in_gpu_reset || adev->in_suspend))
61041ec0267Sriastradh 		return 0;
61141ec0267Sriastradh 	/*
61241ec0267Sriastradh 	 * if SMU loaded firmware, it needn't add SMC, UVD, and VCE
61341ec0267Sriastradh 	 * ucode info here
61441ec0267Sriastradh 	 */
61541ec0267Sriastradh 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
61641ec0267Sriastradh 		if (amdgpu_sriov_vf(adev))
61741ec0267Sriastradh 			adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM - 3;
61841ec0267Sriastradh 		else
61941ec0267Sriastradh 			adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM - 4;
62041ec0267Sriastradh 	} else {
62141ec0267Sriastradh 		adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM;
62241ec0267Sriastradh 	}
62341ec0267Sriastradh 
62441ec0267Sriastradh 	for (i = 0; i < adev->firmware.max_ucodes; i++) {
625efa246c0Sriastradh 		ucode = &adev->firmware.ucode[i];
626efa246c0Sriastradh 		if (ucode->fw) {
62741ec0267Sriastradh 			amdgpu_ucode_init_single_fw(adev, ucode, adev->firmware.fw_buf_mc + fw_offset,
62841ec0267Sriastradh 						    adev->firmware.fw_buf_ptr + fw_offset);
62941ec0267Sriastradh 			if (i == AMDGPU_UCODE_ID_CP_MEC1 &&
63041ec0267Sriastradh 			    adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
63141ec0267Sriastradh 				const struct gfx_firmware_header_v1_0 *cp_hdr;
63241ec0267Sriastradh 				cp_hdr = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
63341ec0267Sriastradh 				amdgpu_ucode_patch_jt(ucode,  adev->firmware.fw_buf_mc + fw_offset,
63441ec0267Sriastradh 						    adev->firmware.fw_buf_ptr + fw_offset);
63541ec0267Sriastradh 				fw_offset += ALIGN(le32_to_cpu(cp_hdr->jt_size) << 2, PAGE_SIZE);
63641ec0267Sriastradh 			}
63741ec0267Sriastradh 			fw_offset += ALIGN(ucode->ucode_size, PAGE_SIZE);
638efa246c0Sriastradh 		}
639efa246c0Sriastradh 	}
640efa246c0Sriastradh 	return 0;
641efa246c0Sriastradh }
642