xref: /dragonfly/sys/dev/drm/amd/amdgpu/amdgpu_ucode.c (revision 78973132)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2014 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  */
23b843c749SSergey Zigachev 
24b843c749SSergey Zigachev #include <linux/firmware.h>
25b843c749SSergey Zigachev #include <linux/slab.h>
26b843c749SSergey Zigachev #include <linux/module.h>
27b843c749SSergey Zigachev #include <drm/drmP.h>
28b843c749SSergey Zigachev #include "amdgpu.h"
29b843c749SSergey Zigachev #include "amdgpu_ucode.h"
30b843c749SSergey Zigachev 
amdgpu_ucode_print_common_hdr(const struct common_firmware_header * hdr)31b843c749SSergey Zigachev static void amdgpu_ucode_print_common_hdr(const struct common_firmware_header *hdr)
32b843c749SSergey Zigachev {
33b843c749SSergey Zigachev 	DRM_DEBUG("size_bytes: %u\n", le32_to_cpu(hdr->size_bytes));
34b843c749SSergey Zigachev 	DRM_DEBUG("header_size_bytes: %u\n", le32_to_cpu(hdr->header_size_bytes));
35b843c749SSergey Zigachev 	DRM_DEBUG("header_version_major: %u\n", le16_to_cpu(hdr->header_version_major));
36b843c749SSergey Zigachev 	DRM_DEBUG("header_version_minor: %u\n", le16_to_cpu(hdr->header_version_minor));
37b843c749SSergey Zigachev 	DRM_DEBUG("ip_version_major: %u\n", le16_to_cpu(hdr->ip_version_major));
38b843c749SSergey Zigachev 	DRM_DEBUG("ip_version_minor: %u\n", le16_to_cpu(hdr->ip_version_minor));
39b843c749SSergey Zigachev 	DRM_DEBUG("ucode_version: 0x%08x\n", le32_to_cpu(hdr->ucode_version));
40b843c749SSergey Zigachev 	DRM_DEBUG("ucode_size_bytes: %u\n", le32_to_cpu(hdr->ucode_size_bytes));
41b843c749SSergey Zigachev 	DRM_DEBUG("ucode_array_offset_bytes: %u\n",
42b843c749SSergey Zigachev 		  le32_to_cpu(hdr->ucode_array_offset_bytes));
43b843c749SSergey Zigachev 	DRM_DEBUG("crc32: 0x%08x\n", le32_to_cpu(hdr->crc32));
44b843c749SSergey Zigachev }
45b843c749SSergey Zigachev 
amdgpu_ucode_print_mc_hdr(const struct common_firmware_header * hdr)46b843c749SSergey Zigachev void amdgpu_ucode_print_mc_hdr(const struct common_firmware_header *hdr)
47b843c749SSergey Zigachev {
48b843c749SSergey Zigachev 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
49b843c749SSergey Zigachev 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
50b843c749SSergey Zigachev 
51b843c749SSergey Zigachev 	DRM_DEBUG("MC\n");
52b843c749SSergey Zigachev 	amdgpu_ucode_print_common_hdr(hdr);
53b843c749SSergey Zigachev 
54b843c749SSergey Zigachev 	if (version_major == 1) {
55b843c749SSergey Zigachev 		const struct mc_firmware_header_v1_0 *mc_hdr =
56b843c749SSergey Zigachev 			container_of(hdr, struct mc_firmware_header_v1_0, header);
57b843c749SSergey Zigachev 
58b843c749SSergey Zigachev 		DRM_DEBUG("io_debug_size_bytes: %u\n",
59b843c749SSergey Zigachev 			  le32_to_cpu(mc_hdr->io_debug_size_bytes));
60b843c749SSergey Zigachev 		DRM_DEBUG("io_debug_array_offset_bytes: %u\n",
61b843c749SSergey Zigachev 			  le32_to_cpu(mc_hdr->io_debug_array_offset_bytes));
62b843c749SSergey Zigachev 	} else {
63b843c749SSergey Zigachev 		DRM_ERROR("Unknown MC ucode version: %u.%u\n", version_major, version_minor);
64b843c749SSergey Zigachev 	}
65b843c749SSergey Zigachev }
66b843c749SSergey Zigachev 
amdgpu_ucode_print_smc_hdr(const struct common_firmware_header * hdr)67b843c749SSergey Zigachev void amdgpu_ucode_print_smc_hdr(const struct common_firmware_header *hdr)
68b843c749SSergey Zigachev {
69b843c749SSergey Zigachev 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
70b843c749SSergey Zigachev 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
71b843c749SSergey Zigachev 
72b843c749SSergey Zigachev 	DRM_DEBUG("SMC\n");
73b843c749SSergey Zigachev 	amdgpu_ucode_print_common_hdr(hdr);
74b843c749SSergey Zigachev 
75b843c749SSergey Zigachev 	if (version_major == 1) {
76b843c749SSergey Zigachev 		const struct smc_firmware_header_v1_0 *smc_hdr =
77b843c749SSergey Zigachev 			container_of(hdr, struct smc_firmware_header_v1_0, header);
78b843c749SSergey Zigachev 
79b843c749SSergey Zigachev 		DRM_DEBUG("ucode_start_addr: %u\n", le32_to_cpu(smc_hdr->ucode_start_addr));
80b843c749SSergey Zigachev 	} else {
81b843c749SSergey Zigachev 		DRM_ERROR("Unknown SMC ucode version: %u.%u\n", version_major, version_minor);
82b843c749SSergey Zigachev 	}
83b843c749SSergey Zigachev }
84b843c749SSergey Zigachev 
amdgpu_ucode_print_gfx_hdr(const struct common_firmware_header * hdr)85b843c749SSergey Zigachev void amdgpu_ucode_print_gfx_hdr(const struct common_firmware_header *hdr)
86b843c749SSergey Zigachev {
87b843c749SSergey Zigachev 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
88b843c749SSergey Zigachev 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
89b843c749SSergey Zigachev 
90b843c749SSergey Zigachev 	DRM_DEBUG("GFX\n");
91b843c749SSergey Zigachev 	amdgpu_ucode_print_common_hdr(hdr);
92b843c749SSergey Zigachev 
93b843c749SSergey Zigachev 	if (version_major == 1) {
94b843c749SSergey Zigachev 		const struct gfx_firmware_header_v1_0 *gfx_hdr =
95b843c749SSergey Zigachev 			container_of(hdr, struct gfx_firmware_header_v1_0, header);
96b843c749SSergey Zigachev 
97b843c749SSergey Zigachev 		DRM_DEBUG("ucode_feature_version: %u\n",
98b843c749SSergey Zigachev 			  le32_to_cpu(gfx_hdr->ucode_feature_version));
99b843c749SSergey Zigachev 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(gfx_hdr->jt_offset));
100b843c749SSergey Zigachev 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(gfx_hdr->jt_size));
101b843c749SSergey Zigachev 	} else {
102b843c749SSergey Zigachev 		DRM_ERROR("Unknown GFX ucode version: %u.%u\n", version_major, version_minor);
103b843c749SSergey Zigachev 	}
104b843c749SSergey Zigachev }
105b843c749SSergey Zigachev 
amdgpu_ucode_print_rlc_hdr(const struct common_firmware_header * hdr)106b843c749SSergey Zigachev void amdgpu_ucode_print_rlc_hdr(const struct common_firmware_header *hdr)
107b843c749SSergey Zigachev {
108b843c749SSergey Zigachev 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
109b843c749SSergey Zigachev 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
110b843c749SSergey Zigachev 
111b843c749SSergey Zigachev 	DRM_DEBUG("RLC\n");
112b843c749SSergey Zigachev 	amdgpu_ucode_print_common_hdr(hdr);
113b843c749SSergey Zigachev 
114b843c749SSergey Zigachev 	if (version_major == 1) {
115b843c749SSergey Zigachev 		const struct rlc_firmware_header_v1_0 *rlc_hdr =
116b843c749SSergey Zigachev 			container_of(hdr, struct rlc_firmware_header_v1_0, header);
117b843c749SSergey Zigachev 
118b843c749SSergey Zigachev 		DRM_DEBUG("ucode_feature_version: %u\n",
119b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->ucode_feature_version));
120b843c749SSergey Zigachev 		DRM_DEBUG("save_and_restore_offset: %u\n",
121b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->save_and_restore_offset));
122b843c749SSergey Zigachev 		DRM_DEBUG("clear_state_descriptor_offset: %u\n",
123b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->clear_state_descriptor_offset));
124b843c749SSergey Zigachev 		DRM_DEBUG("avail_scratch_ram_locations: %u\n",
125b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->avail_scratch_ram_locations));
126b843c749SSergey Zigachev 		DRM_DEBUG("master_pkt_description_offset: %u\n",
127b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->master_pkt_description_offset));
128b843c749SSergey Zigachev 	} else if (version_major == 2) {
129b843c749SSergey Zigachev 		const struct rlc_firmware_header_v2_0 *rlc_hdr =
130b843c749SSergey Zigachev 			container_of(hdr, struct rlc_firmware_header_v2_0, header);
131b843c749SSergey Zigachev 
132b843c749SSergey Zigachev 		DRM_DEBUG("ucode_feature_version: %u\n",
133b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->ucode_feature_version));
134b843c749SSergey Zigachev 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(rlc_hdr->jt_offset));
135b843c749SSergey Zigachev 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(rlc_hdr->jt_size));
136b843c749SSergey Zigachev 		DRM_DEBUG("save_and_restore_offset: %u\n",
137b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->save_and_restore_offset));
138b843c749SSergey Zigachev 		DRM_DEBUG("clear_state_descriptor_offset: %u\n",
139b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->clear_state_descriptor_offset));
140b843c749SSergey Zigachev 		DRM_DEBUG("avail_scratch_ram_locations: %u\n",
141b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->avail_scratch_ram_locations));
142b843c749SSergey Zigachev 		DRM_DEBUG("reg_restore_list_size: %u\n",
143b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->reg_restore_list_size));
144b843c749SSergey Zigachev 		DRM_DEBUG("reg_list_format_start: %u\n",
145b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->reg_list_format_start));
146b843c749SSergey Zigachev 		DRM_DEBUG("reg_list_format_separate_start: %u\n",
147b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->reg_list_format_separate_start));
148b843c749SSergey Zigachev 		DRM_DEBUG("starting_offsets_start: %u\n",
149b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->starting_offsets_start));
150b843c749SSergey Zigachev 		DRM_DEBUG("reg_list_format_size_bytes: %u\n",
151b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->reg_list_format_size_bytes));
152b843c749SSergey Zigachev 		DRM_DEBUG("reg_list_format_array_offset_bytes: %u\n",
153b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->reg_list_format_array_offset_bytes));
154b843c749SSergey Zigachev 		DRM_DEBUG("reg_list_size_bytes: %u\n",
155b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->reg_list_size_bytes));
156b843c749SSergey Zigachev 		DRM_DEBUG("reg_list_array_offset_bytes: %u\n",
157b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->reg_list_array_offset_bytes));
158b843c749SSergey Zigachev 		DRM_DEBUG("reg_list_format_separate_size_bytes: %u\n",
159b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->reg_list_format_separate_size_bytes));
160b843c749SSergey Zigachev 		DRM_DEBUG("reg_list_format_separate_array_offset_bytes: %u\n",
161b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->reg_list_format_separate_array_offset_bytes));
162b843c749SSergey Zigachev 		DRM_DEBUG("reg_list_separate_size_bytes: %u\n",
163b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->reg_list_separate_size_bytes));
164b843c749SSergey Zigachev 		DRM_DEBUG("reg_list_separate_array_offset_bytes: %u\n",
165b843c749SSergey Zigachev 			  le32_to_cpu(rlc_hdr->reg_list_separate_array_offset_bytes));
166b843c749SSergey Zigachev 		if (version_minor == 1) {
167b843c749SSergey Zigachev 			const struct rlc_firmware_header_v2_1 *v2_1 =
168b843c749SSergey Zigachev 				container_of(rlc_hdr, struct rlc_firmware_header_v2_1, v2_0);
169b843c749SSergey Zigachev 			DRM_DEBUG("reg_list_format_direct_reg_list_length: %u\n",
170b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->reg_list_format_direct_reg_list_length));
171b843c749SSergey Zigachev 			DRM_DEBUG("save_restore_list_cntl_ucode_ver: %u\n",
172b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->save_restore_list_cntl_ucode_ver));
173b843c749SSergey Zigachev 			DRM_DEBUG("save_restore_list_cntl_feature_ver: %u\n",
174b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->save_restore_list_cntl_feature_ver));
175b843c749SSergey Zigachev 			DRM_DEBUG("save_restore_list_cntl_size_bytes %u\n",
176b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->save_restore_list_cntl_size_bytes));
177b843c749SSergey Zigachev 			DRM_DEBUG("save_restore_list_cntl_offset_bytes: %u\n",
178b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->save_restore_list_cntl_offset_bytes));
179b843c749SSergey Zigachev 			DRM_DEBUG("save_restore_list_gpm_ucode_ver: %u\n",
180b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->save_restore_list_gpm_ucode_ver));
181b843c749SSergey Zigachev 			DRM_DEBUG("save_restore_list_gpm_feature_ver: %u\n",
182b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->save_restore_list_gpm_feature_ver));
183b843c749SSergey Zigachev 			DRM_DEBUG("save_restore_list_gpm_size_bytes %u\n",
184b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->save_restore_list_gpm_size_bytes));
185b843c749SSergey Zigachev 			DRM_DEBUG("save_restore_list_gpm_offset_bytes: %u\n",
186b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->save_restore_list_gpm_offset_bytes));
187b843c749SSergey Zigachev 			DRM_DEBUG("save_restore_list_srm_ucode_ver: %u\n",
188b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->save_restore_list_srm_ucode_ver));
189b843c749SSergey Zigachev 			DRM_DEBUG("save_restore_list_srm_feature_ver: %u\n",
190b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->save_restore_list_srm_feature_ver));
191b843c749SSergey Zigachev 			DRM_DEBUG("save_restore_list_srm_size_bytes %u\n",
192b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->save_restore_list_srm_size_bytes));
193b843c749SSergey Zigachev 			DRM_DEBUG("save_restore_list_srm_offset_bytes: %u\n",
194b843c749SSergey Zigachev 				  le32_to_cpu(v2_1->save_restore_list_srm_offset_bytes));
195b843c749SSergey Zigachev 		}
196b843c749SSergey Zigachev 	} else {
197b843c749SSergey Zigachev 		DRM_ERROR("Unknown RLC ucode version: %u.%u\n", version_major, version_minor);
198b843c749SSergey Zigachev 	}
199b843c749SSergey Zigachev }
200b843c749SSergey Zigachev 
amdgpu_ucode_print_sdma_hdr(const struct common_firmware_header * hdr)201b843c749SSergey Zigachev void amdgpu_ucode_print_sdma_hdr(const struct common_firmware_header *hdr)
202b843c749SSergey Zigachev {
203b843c749SSergey Zigachev 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
204b843c749SSergey Zigachev 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
205b843c749SSergey Zigachev 
206b843c749SSergey Zigachev 	DRM_DEBUG("SDMA\n");
207b843c749SSergey Zigachev 	amdgpu_ucode_print_common_hdr(hdr);
208b843c749SSergey Zigachev 
209b843c749SSergey Zigachev 	if (version_major == 1) {
210b843c749SSergey Zigachev 		const struct sdma_firmware_header_v1_0 *sdma_hdr =
211b843c749SSergey Zigachev 			container_of(hdr, struct sdma_firmware_header_v1_0, header);
212b843c749SSergey Zigachev 
213b843c749SSergey Zigachev 		DRM_DEBUG("ucode_feature_version: %u\n",
214b843c749SSergey Zigachev 			  le32_to_cpu(sdma_hdr->ucode_feature_version));
215b843c749SSergey Zigachev 		DRM_DEBUG("ucode_change_version: %u\n",
216b843c749SSergey Zigachev 			  le32_to_cpu(sdma_hdr->ucode_change_version));
217b843c749SSergey Zigachev 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(sdma_hdr->jt_offset));
218b843c749SSergey Zigachev 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(sdma_hdr->jt_size));
219b843c749SSergey Zigachev 		if (version_minor >= 1) {
220b843c749SSergey Zigachev 			const struct sdma_firmware_header_v1_1 *sdma_v1_1_hdr =
221b843c749SSergey Zigachev 				container_of(sdma_hdr, struct sdma_firmware_header_v1_1, v1_0);
222b843c749SSergey Zigachev 			DRM_DEBUG("digest_size: %u\n", le32_to_cpu(sdma_v1_1_hdr->digest_size));
223b843c749SSergey Zigachev 		}
224b843c749SSergey Zigachev 	} else {
225b843c749SSergey Zigachev 		DRM_ERROR("Unknown SDMA ucode version: %u.%u\n",
226b843c749SSergey Zigachev 			  version_major, version_minor);
227b843c749SSergey Zigachev 	}
228b843c749SSergey Zigachev }
229b843c749SSergey Zigachev 
amdgpu_ucode_print_gpu_info_hdr(const struct common_firmware_header * hdr)230b843c749SSergey Zigachev void amdgpu_ucode_print_gpu_info_hdr(const struct common_firmware_header *hdr)
231b843c749SSergey Zigachev {
232b843c749SSergey Zigachev 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
233b843c749SSergey Zigachev 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
234b843c749SSergey Zigachev 
235b843c749SSergey Zigachev 	DRM_DEBUG("GPU_INFO\n");
236b843c749SSergey Zigachev 	amdgpu_ucode_print_common_hdr(hdr);
237b843c749SSergey Zigachev 
238b843c749SSergey Zigachev 	if (version_major == 1) {
239b843c749SSergey Zigachev 		const struct gpu_info_firmware_header_v1_0 *gpu_info_hdr =
240b843c749SSergey Zigachev 			container_of(hdr, struct gpu_info_firmware_header_v1_0, header);
241b843c749SSergey Zigachev 
242b843c749SSergey Zigachev 		DRM_DEBUG("version_major: %u\n",
243b843c749SSergey Zigachev 			  le16_to_cpu(gpu_info_hdr->version_major));
244b843c749SSergey Zigachev 		DRM_DEBUG("version_minor: %u\n",
245b843c749SSergey Zigachev 			  le16_to_cpu(gpu_info_hdr->version_minor));
246b843c749SSergey Zigachev 	} else {
247b843c749SSergey Zigachev 		DRM_ERROR("Unknown gpu_info ucode version: %u.%u\n", version_major, version_minor);
248b843c749SSergey Zigachev 	}
249b843c749SSergey Zigachev }
250b843c749SSergey Zigachev 
amdgpu_ucode_validate(const struct firmware * fw)251b843c749SSergey Zigachev int amdgpu_ucode_validate(const struct firmware *fw)
252b843c749SSergey Zigachev {
253b843c749SSergey Zigachev 	const struct common_firmware_header *hdr =
254b843c749SSergey Zigachev 		(const struct common_firmware_header *)fw->data;
255b843c749SSergey Zigachev 
256*78973132SSergey Zigachev 	if (fw->datasize == le32_to_cpu(hdr->size_bytes))
257b843c749SSergey Zigachev 		return 0;
258b843c749SSergey Zigachev 
259b843c749SSergey Zigachev 	return -EINVAL;
260b843c749SSergey Zigachev }
261b843c749SSergey Zigachev 
amdgpu_ucode_hdr_version(union amdgpu_firmware_header * hdr,uint16_t hdr_major,uint16_t hdr_minor)262b843c749SSergey Zigachev bool amdgpu_ucode_hdr_version(union amdgpu_firmware_header *hdr,
263b843c749SSergey Zigachev 				uint16_t hdr_major, uint16_t hdr_minor)
264b843c749SSergey Zigachev {
265b843c749SSergey Zigachev 	if ((hdr->common.header_version_major == hdr_major) &&
266b843c749SSergey Zigachev 		(hdr->common.header_version_minor == hdr_minor))
267b843c749SSergey Zigachev 		return false;
268b843c749SSergey Zigachev 	return true;
269b843c749SSergey Zigachev }
270b843c749SSergey Zigachev 
271b843c749SSergey Zigachev enum amdgpu_firmware_load_type
amdgpu_ucode_get_load_type(struct amdgpu_device * adev,int load_type)272b843c749SSergey Zigachev amdgpu_ucode_get_load_type(struct amdgpu_device *adev, int load_type)
273b843c749SSergey Zigachev {
274b843c749SSergey Zigachev 	switch (adev->asic_type) {
275b843c749SSergey Zigachev #ifdef CONFIG_DRM_AMDGPU_SI
276b843c749SSergey Zigachev 	case CHIP_TAHITI:
277b843c749SSergey Zigachev 	case CHIP_PITCAIRN:
278b843c749SSergey Zigachev 	case CHIP_VERDE:
279b843c749SSergey Zigachev 	case CHIP_OLAND:
280b843c749SSergey Zigachev 	case CHIP_HAINAN:
281b843c749SSergey Zigachev 		return AMDGPU_FW_LOAD_DIRECT;
282b843c749SSergey Zigachev #endif
283b843c749SSergey Zigachev #ifdef CONFIG_DRM_AMDGPU_CIK
284b843c749SSergey Zigachev 	case CHIP_BONAIRE:
285b843c749SSergey Zigachev 	case CHIP_KAVERI:
286b843c749SSergey Zigachev 	case CHIP_KABINI:
287b843c749SSergey Zigachev 	case CHIP_HAWAII:
288b843c749SSergey Zigachev 	case CHIP_MULLINS:
289b843c749SSergey Zigachev 		return AMDGPU_FW_LOAD_DIRECT;
290b843c749SSergey Zigachev #endif
291b843c749SSergey Zigachev 	case CHIP_TOPAZ:
292b843c749SSergey Zigachev 	case CHIP_TONGA:
293b843c749SSergey Zigachev 	case CHIP_FIJI:
294b843c749SSergey Zigachev 	case CHIP_CARRIZO:
295b843c749SSergey Zigachev 	case CHIP_STONEY:
296b843c749SSergey Zigachev 	case CHIP_POLARIS10:
297b843c749SSergey Zigachev 	case CHIP_POLARIS11:
298b843c749SSergey Zigachev 	case CHIP_POLARIS12:
299b843c749SSergey Zigachev 	case CHIP_VEGAM:
300b843c749SSergey Zigachev 		if (!load_type)
301b843c749SSergey Zigachev 			return AMDGPU_FW_LOAD_DIRECT;
302b843c749SSergey Zigachev 		else
303b843c749SSergey Zigachev 			return AMDGPU_FW_LOAD_SMU;
304b843c749SSergey Zigachev 	case CHIP_VEGA10:
305b843c749SSergey Zigachev 	case CHIP_RAVEN:
306b843c749SSergey Zigachev 	case CHIP_VEGA12:
307b843c749SSergey Zigachev 		if (!load_type)
308b843c749SSergey Zigachev 			return AMDGPU_FW_LOAD_DIRECT;
309b843c749SSergey Zigachev 		else
310b843c749SSergey Zigachev 			return AMDGPU_FW_LOAD_PSP;
311b843c749SSergey Zigachev 	case CHIP_VEGA20:
312b843c749SSergey Zigachev 		return AMDGPU_FW_LOAD_DIRECT;
313b843c749SSergey Zigachev 	default:
314b843c749SSergey Zigachev 		DRM_ERROR("Unknown firmware load type\n");
315b843c749SSergey Zigachev 	}
316b843c749SSergey Zigachev 
317b843c749SSergey Zigachev 	return AMDGPU_FW_LOAD_DIRECT;
318b843c749SSergey Zigachev }
319b843c749SSergey Zigachev 
amdgpu_ucode_init_single_fw(struct amdgpu_device * adev,struct amdgpu_firmware_info * ucode,uint64_t mc_addr,void * kptr)320b843c749SSergey Zigachev static int amdgpu_ucode_init_single_fw(struct amdgpu_device *adev,
321b843c749SSergey Zigachev 				       struct amdgpu_firmware_info *ucode,
322b843c749SSergey Zigachev 				       uint64_t mc_addr, void *kptr)
323b843c749SSergey Zigachev {
324b843c749SSergey Zigachev 	const struct common_firmware_header *header = NULL;
325b843c749SSergey Zigachev 	const struct gfx_firmware_header_v1_0 *cp_hdr = NULL;
326b843c749SSergey Zigachev 
327b843c749SSergey Zigachev 	if (NULL == ucode->fw)
328b843c749SSergey Zigachev 		return 0;
329b843c749SSergey Zigachev 
330b843c749SSergey Zigachev 	ucode->mc_addr = mc_addr;
331b843c749SSergey Zigachev 	ucode->kaddr = kptr;
332b843c749SSergey Zigachev 
333b843c749SSergey Zigachev 	if (ucode->ucode_id == AMDGPU_UCODE_ID_STORAGE)
334b843c749SSergey Zigachev 		return 0;
335b843c749SSergey Zigachev 
336b843c749SSergey Zigachev 	header = (const struct common_firmware_header *)ucode->fw->data;
337b843c749SSergey Zigachev 
338b843c749SSergey Zigachev 	cp_hdr = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
339b843c749SSergey Zigachev 
340b843c749SSergey Zigachev 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP ||
341b843c749SSergey Zigachev 	    (ucode->ucode_id != AMDGPU_UCODE_ID_CP_MEC1 &&
342b843c749SSergey Zigachev 	     ucode->ucode_id != AMDGPU_UCODE_ID_CP_MEC2 &&
343b843c749SSergey Zigachev 	     ucode->ucode_id != AMDGPU_UCODE_ID_CP_MEC1_JT &&
344b843c749SSergey Zigachev 	     ucode->ucode_id != AMDGPU_UCODE_ID_CP_MEC2_JT &&
345b843c749SSergey Zigachev 	     ucode->ucode_id != AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL &&
346b843c749SSergey Zigachev 	     ucode->ucode_id != AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM &&
347b843c749SSergey Zigachev 	     ucode->ucode_id != AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM)) {
348b843c749SSergey Zigachev 		ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes);
349b843c749SSergey Zigachev 
350b843c749SSergey Zigachev 		memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data +
351b843c749SSergey Zigachev 					      le32_to_cpu(header->ucode_array_offset_bytes)),
352b843c749SSergey Zigachev 		       ucode->ucode_size);
353b843c749SSergey Zigachev 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1 ||
354b843c749SSergey Zigachev 		   ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2) {
355b843c749SSergey Zigachev 		ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes) -
356b843c749SSergey Zigachev 			le32_to_cpu(cp_hdr->jt_size) * 4;
357b843c749SSergey Zigachev 
358b843c749SSergey Zigachev 		memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data +
359b843c749SSergey Zigachev 					      le32_to_cpu(header->ucode_array_offset_bytes)),
360b843c749SSergey Zigachev 		       ucode->ucode_size);
361b843c749SSergey Zigachev 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
362b843c749SSergey Zigachev 		   ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT) {
363b843c749SSergey Zigachev 		ucode->ucode_size = le32_to_cpu(cp_hdr->jt_size) * 4;
364b843c749SSergey Zigachev 
365b843c749SSergey Zigachev 		memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data +
366b843c749SSergey Zigachev 					      le32_to_cpu(header->ucode_array_offset_bytes) +
367b843c749SSergey Zigachev 					      le32_to_cpu(cp_hdr->jt_offset) * 4),
368b843c749SSergey Zigachev 		       ucode->ucode_size);
369b843c749SSergey Zigachev 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL) {
370b843c749SSergey Zigachev 		ucode->ucode_size = adev->gfx.rlc.save_restore_list_cntl_size_bytes;
371b843c749SSergey Zigachev 		memcpy(ucode->kaddr, adev->gfx.rlc.save_restore_list_cntl,
372b843c749SSergey Zigachev 		       ucode->ucode_size);
373b843c749SSergey Zigachev 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM) {
374b843c749SSergey Zigachev 		ucode->ucode_size = adev->gfx.rlc.save_restore_list_gpm_size_bytes;
375b843c749SSergey Zigachev 		memcpy(ucode->kaddr, adev->gfx.rlc.save_restore_list_gpm,
376b843c749SSergey Zigachev 		       ucode->ucode_size);
377b843c749SSergey Zigachev 	} else if (ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM) {
378b843c749SSergey Zigachev 		ucode->ucode_size = adev->gfx.rlc.save_restore_list_srm_size_bytes;
379b843c749SSergey Zigachev 		memcpy(ucode->kaddr, adev->gfx.rlc.save_restore_list_srm,
380b843c749SSergey Zigachev 		       ucode->ucode_size);
381b843c749SSergey Zigachev 	}
382b843c749SSergey Zigachev 
383b843c749SSergey Zigachev 	return 0;
384b843c749SSergey Zigachev }
385b843c749SSergey Zigachev 
amdgpu_ucode_patch_jt(struct amdgpu_firmware_info * ucode,uint64_t mc_addr,void * kptr)386b843c749SSergey Zigachev static int amdgpu_ucode_patch_jt(struct amdgpu_firmware_info *ucode,
387b843c749SSergey Zigachev 				uint64_t mc_addr, void *kptr)
388b843c749SSergey Zigachev {
389b843c749SSergey Zigachev 	const struct gfx_firmware_header_v1_0 *header = NULL;
390b843c749SSergey Zigachev 	const struct common_firmware_header *comm_hdr = NULL;
391b843c749SSergey Zigachev 	uint8_t* src_addr = NULL;
392b843c749SSergey Zigachev 	uint8_t* dst_addr = NULL;
393b843c749SSergey Zigachev 
394b843c749SSergey Zigachev 	if (NULL == ucode->fw)
395b843c749SSergey Zigachev 		return 0;
396b843c749SSergey Zigachev 
397b843c749SSergey Zigachev 	comm_hdr = (const struct common_firmware_header *)ucode->fw->data;
398b843c749SSergey Zigachev 	header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
399b843c749SSergey Zigachev 	dst_addr = ucode->kaddr +
400b843c749SSergey Zigachev 			   ALIGN(le32_to_cpu(comm_hdr->ucode_size_bytes),
401b843c749SSergey Zigachev 			   PAGE_SIZE);
402b843c749SSergey Zigachev 	src_addr = (uint8_t *)ucode->fw->data +
403b843c749SSergey Zigachev 			   le32_to_cpu(comm_hdr->ucode_array_offset_bytes) +
404b843c749SSergey Zigachev 			   (le32_to_cpu(header->jt_offset) * 4);
405b843c749SSergey Zigachev 	memcpy(dst_addr, src_addr, le32_to_cpu(header->jt_size) * 4);
406b843c749SSergey Zigachev 
407b843c749SSergey Zigachev 	return 0;
408b843c749SSergey Zigachev }
409b843c749SSergey Zigachev 
amdgpu_ucode_init_bo(struct amdgpu_device * adev)410b843c749SSergey Zigachev int amdgpu_ucode_init_bo(struct amdgpu_device *adev)
411b843c749SSergey Zigachev {
412b843c749SSergey Zigachev 	uint64_t fw_offset = 0;
413b843c749SSergey Zigachev 	int i, err;
414b843c749SSergey Zigachev 	struct amdgpu_firmware_info *ucode = NULL;
415b843c749SSergey Zigachev 	const struct common_firmware_header *header = NULL;
416b843c749SSergey Zigachev 
417b843c749SSergey Zigachev 	if (!adev->firmware.fw_size) {
418b843c749SSergey Zigachev 		dev_warn(adev->dev, "No ip firmware need to load\n");
419b843c749SSergey Zigachev 		return 0;
420b843c749SSergey Zigachev 	}
421b843c749SSergey Zigachev 
422b843c749SSergey Zigachev 	if (!adev->in_gpu_reset) {
423b843c749SSergey Zigachev 		err = amdgpu_bo_create_kernel(adev, adev->firmware.fw_size, PAGE_SIZE,
424b843c749SSergey Zigachev 					amdgpu_sriov_vf(adev) ? AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
425b843c749SSergey Zigachev 					&adev->firmware.fw_buf,
426*78973132SSergey Zigachev 					(u64 *)&adev->firmware.fw_buf_mc,
427b843c749SSergey Zigachev 					&adev->firmware.fw_buf_ptr);
428b843c749SSergey Zigachev 		if (err) {
429b843c749SSergey Zigachev 			dev_err(adev->dev, "failed to create kernel buffer for firmware.fw_buf\n");
430b843c749SSergey Zigachev 			goto failed;
431b843c749SSergey Zigachev 		}
432b843c749SSergey Zigachev 	}
433b843c749SSergey Zigachev 
434b843c749SSergey Zigachev 	memset(adev->firmware.fw_buf_ptr, 0, adev->firmware.fw_size);
435b843c749SSergey Zigachev 
436b843c749SSergey Zigachev 	/*
437b843c749SSergey Zigachev 	 * if SMU loaded firmware, it needn't add SMC, UVD, and VCE
438b843c749SSergey Zigachev 	 * ucode info here
439b843c749SSergey Zigachev 	 */
440b843c749SSergey Zigachev 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
441b843c749SSergey Zigachev 		if (amdgpu_sriov_vf(adev))
442b843c749SSergey Zigachev 			adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM - 3;
443b843c749SSergey Zigachev 		else
444b843c749SSergey Zigachev 			adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM - 4;
445b843c749SSergey Zigachev 	} else {
446b843c749SSergey Zigachev 		adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM;
447b843c749SSergey Zigachev 	}
448b843c749SSergey Zigachev 
449b843c749SSergey Zigachev 	for (i = 0; i < adev->firmware.max_ucodes; i++) {
450b843c749SSergey Zigachev 		ucode = &adev->firmware.ucode[i];
451b843c749SSergey Zigachev 		if (ucode->fw) {
452b843c749SSergey Zigachev 			header = (const struct common_firmware_header *)ucode->fw->data;
453b843c749SSergey Zigachev 			amdgpu_ucode_init_single_fw(adev, ucode, adev->firmware.fw_buf_mc + fw_offset,
454b843c749SSergey Zigachev 						    adev->firmware.fw_buf_ptr + fw_offset);
455b843c749SSergey Zigachev 			if (i == AMDGPU_UCODE_ID_CP_MEC1 &&
456b843c749SSergey Zigachev 			    adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
457b843c749SSergey Zigachev 				const struct gfx_firmware_header_v1_0 *cp_hdr;
458b843c749SSergey Zigachev 				cp_hdr = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
459b843c749SSergey Zigachev 				amdgpu_ucode_patch_jt(ucode,  adev->firmware.fw_buf_mc + fw_offset,
460b843c749SSergey Zigachev 						    adev->firmware.fw_buf_ptr + fw_offset);
461b843c749SSergey Zigachev 				fw_offset += ALIGN(le32_to_cpu(cp_hdr->jt_size) << 2, PAGE_SIZE);
462b843c749SSergey Zigachev 			}
463b843c749SSergey Zigachev 			fw_offset += ALIGN(ucode->ucode_size, PAGE_SIZE);
464b843c749SSergey Zigachev 		}
465b843c749SSergey Zigachev 	}
466b843c749SSergey Zigachev 	return 0;
467b843c749SSergey Zigachev 
468b843c749SSergey Zigachev failed:
469b843c749SSergey Zigachev 	if (err)
470b843c749SSergey Zigachev 		adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
471b843c749SSergey Zigachev 
472b843c749SSergey Zigachev 	return err;
473b843c749SSergey Zigachev }
474b843c749SSergey Zigachev 
amdgpu_ucode_fini_bo(struct amdgpu_device * adev)475b843c749SSergey Zigachev int amdgpu_ucode_fini_bo(struct amdgpu_device *adev)
476b843c749SSergey Zigachev {
477b843c749SSergey Zigachev 	int i;
478b843c749SSergey Zigachev 	struct amdgpu_firmware_info *ucode = NULL;
479b843c749SSergey Zigachev 
480b843c749SSergey Zigachev 	if (!adev->firmware.fw_size)
481b843c749SSergey Zigachev 		return 0;
482b843c749SSergey Zigachev 
483b843c749SSergey Zigachev 	for (i = 0; i < adev->firmware.max_ucodes; i++) {
484b843c749SSergey Zigachev 		ucode = &adev->firmware.ucode[i];
485b843c749SSergey Zigachev 		if (ucode->fw) {
486b843c749SSergey Zigachev 			ucode->mc_addr = 0;
487b843c749SSergey Zigachev 			ucode->kaddr = NULL;
488b843c749SSergey Zigachev 		}
489b843c749SSergey Zigachev 	}
490b843c749SSergey Zigachev 
491b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&adev->firmware.fw_buf,
492*78973132SSergey Zigachev 				(u64 *)&adev->firmware.fw_buf_mc,
493b843c749SSergey Zigachev 				&adev->firmware.fw_buf_ptr);
494b843c749SSergey Zigachev 
495b843c749SSergey Zigachev 	return 0;
496b843c749SSergey Zigachev }
497