• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <linux/firmware.h>
25 #include <linux/slab.h>
26 #include <linux/module.h>
27 
28 #include "amdgpu.h"
29 #include "amdgpu_ucode.h"
30 
31 #define AMDGPU_UCODE_NAME_MAX		(128)
32 
33 static const struct kicker_device kicker_device_list[] = {
34 	{0x744B, 0x00},
35 };
36 
amdgpu_ucode_print_common_hdr(const struct common_firmware_header * hdr)37 static void amdgpu_ucode_print_common_hdr(const struct common_firmware_header *hdr)
38 {
39 	DRM_DEBUG("size_bytes: %u\n", le32_to_cpu(hdr->size_bytes));
40 	DRM_DEBUG("header_size_bytes: %u\n", le32_to_cpu(hdr->header_size_bytes));
41 	DRM_DEBUG("header_version_major: %u\n", le16_to_cpu(hdr->header_version_major));
42 	DRM_DEBUG("header_version_minor: %u\n", le16_to_cpu(hdr->header_version_minor));
43 	DRM_DEBUG("ip_version_major: %u\n", le16_to_cpu(hdr->ip_version_major));
44 	DRM_DEBUG("ip_version_minor: %u\n", le16_to_cpu(hdr->ip_version_minor));
45 	DRM_DEBUG("ucode_version: 0x%08x\n", le32_to_cpu(hdr->ucode_version));
46 	DRM_DEBUG("ucode_size_bytes: %u\n", le32_to_cpu(hdr->ucode_size_bytes));
47 	DRM_DEBUG("ucode_array_offset_bytes: %u\n",
48 		  le32_to_cpu(hdr->ucode_array_offset_bytes));
49 	DRM_DEBUG("crc32: 0x%08x\n", le32_to_cpu(hdr->crc32));
50 }
51 
amdgpu_ucode_print_mc_hdr(const struct common_firmware_header * hdr)52 void amdgpu_ucode_print_mc_hdr(const struct common_firmware_header *hdr)
53 {
54 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
55 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
56 
57 	DRM_DEBUG("MC\n");
58 	amdgpu_ucode_print_common_hdr(hdr);
59 
60 	if (version_major == 1) {
61 		const struct mc_firmware_header_v1_0 *mc_hdr =
62 			container_of(hdr, struct mc_firmware_header_v1_0, header);
63 
64 		DRM_DEBUG("io_debug_size_bytes: %u\n",
65 			  le32_to_cpu(mc_hdr->io_debug_size_bytes));
66 		DRM_DEBUG("io_debug_array_offset_bytes: %u\n",
67 			  le32_to_cpu(mc_hdr->io_debug_array_offset_bytes));
68 	} else {
69 		DRM_ERROR("Unknown MC ucode version: %u.%u\n", version_major, version_minor);
70 	}
71 }
72 
amdgpu_ucode_print_smc_hdr(const struct common_firmware_header * hdr)73 void amdgpu_ucode_print_smc_hdr(const struct common_firmware_header *hdr)
74 {
75 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
76 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
77 	const struct smc_firmware_header_v1_0 *v1_0_hdr;
78 	const struct smc_firmware_header_v2_0 *v2_0_hdr;
79 	const struct smc_firmware_header_v2_1 *v2_1_hdr;
80 
81 	DRM_DEBUG("SMC\n");
82 	amdgpu_ucode_print_common_hdr(hdr);
83 
84 	if (version_major == 1) {
85 		v1_0_hdr = container_of(hdr, struct smc_firmware_header_v1_0, header);
86 		DRM_DEBUG("ucode_start_addr: %u\n", le32_to_cpu(v1_0_hdr->ucode_start_addr));
87 	} else if (version_major == 2) {
88 		switch (version_minor) {
89 		case 0:
90 			v2_0_hdr = container_of(hdr, struct smc_firmware_header_v2_0, v1_0.header);
91 			DRM_DEBUG("ppt_offset_bytes: %u\n", le32_to_cpu(v2_0_hdr->ppt_offset_bytes));
92 			DRM_DEBUG("ppt_size_bytes: %u\n", le32_to_cpu(v2_0_hdr->ppt_size_bytes));
93 			break;
94 		case 1:
95 			v2_1_hdr = container_of(hdr, struct smc_firmware_header_v2_1, v1_0.header);
96 			DRM_DEBUG("pptable_count: %u\n", le32_to_cpu(v2_1_hdr->pptable_count));
97 			DRM_DEBUG("pptable_entry_offset: %u\n", le32_to_cpu(v2_1_hdr->pptable_entry_offset));
98 			break;
99 		default:
100 			break;
101 		}
102 
103 	} else {
104 		DRM_ERROR("Unknown SMC ucode version: %u.%u\n", version_major, version_minor);
105 	}
106 }
107 
amdgpu_ucode_print_gfx_hdr(const struct common_firmware_header * hdr)108 void amdgpu_ucode_print_gfx_hdr(const struct common_firmware_header *hdr)
109 {
110 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
111 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
112 
113 	DRM_DEBUG("GFX\n");
114 	amdgpu_ucode_print_common_hdr(hdr);
115 
116 	if (version_major == 1) {
117 		const struct gfx_firmware_header_v1_0 *gfx_hdr =
118 			container_of(hdr, struct gfx_firmware_header_v1_0, header);
119 
120 		DRM_DEBUG("ucode_feature_version: %u\n",
121 			  le32_to_cpu(gfx_hdr->ucode_feature_version));
122 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(gfx_hdr->jt_offset));
123 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(gfx_hdr->jt_size));
124 	} else if (version_major == 2) {
125 		const struct gfx_firmware_header_v2_0 *gfx_hdr =
126 			container_of(hdr, struct gfx_firmware_header_v2_0, header);
127 
128 		DRM_DEBUG("ucode_feature_version: %u\n",
129 			  le32_to_cpu(gfx_hdr->ucode_feature_version));
130 	} else {
131 		DRM_ERROR("Unknown GFX ucode version: %u.%u\n", version_major, version_minor);
132 	}
133 }
134 
amdgpu_ucode_print_rlc_hdr(const struct common_firmware_header * hdr)135 void amdgpu_ucode_print_rlc_hdr(const struct common_firmware_header *hdr)
136 {
137 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
138 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
139 
140 	DRM_DEBUG("RLC\n");
141 	amdgpu_ucode_print_common_hdr(hdr);
142 
143 	if (version_major == 1) {
144 		const struct rlc_firmware_header_v1_0 *rlc_hdr =
145 			container_of(hdr, struct rlc_firmware_header_v1_0, header);
146 
147 		DRM_DEBUG("ucode_feature_version: %u\n",
148 			  le32_to_cpu(rlc_hdr->ucode_feature_version));
149 		DRM_DEBUG("save_and_restore_offset: %u\n",
150 			  le32_to_cpu(rlc_hdr->save_and_restore_offset));
151 		DRM_DEBUG("clear_state_descriptor_offset: %u\n",
152 			  le32_to_cpu(rlc_hdr->clear_state_descriptor_offset));
153 		DRM_DEBUG("avail_scratch_ram_locations: %u\n",
154 			  le32_to_cpu(rlc_hdr->avail_scratch_ram_locations));
155 		DRM_DEBUG("master_pkt_description_offset: %u\n",
156 			  le32_to_cpu(rlc_hdr->master_pkt_description_offset));
157 	} else if (version_major == 2) {
158 		const struct rlc_firmware_header_v2_0 *rlc_hdr =
159 			container_of(hdr, struct rlc_firmware_header_v2_0, header);
160 		const struct rlc_firmware_header_v2_1 *rlc_hdr_v2_1 =
161 			container_of(rlc_hdr, struct rlc_firmware_header_v2_1, v2_0);
162 		const struct rlc_firmware_header_v2_2 *rlc_hdr_v2_2 =
163 			container_of(rlc_hdr_v2_1, struct rlc_firmware_header_v2_2, v2_1);
164 		const struct rlc_firmware_header_v2_3 *rlc_hdr_v2_3 =
165 			container_of(rlc_hdr_v2_2, struct rlc_firmware_header_v2_3, v2_2);
166 		const struct rlc_firmware_header_v2_4 *rlc_hdr_v2_4 =
167 			container_of(rlc_hdr_v2_3, struct rlc_firmware_header_v2_4, v2_3);
168 
169 		switch (version_minor) {
170 		case 0:
171 			/* rlc_hdr v2_0 */
172 			DRM_DEBUG("ucode_feature_version: %u\n",
173 				  le32_to_cpu(rlc_hdr->ucode_feature_version));
174 			DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(rlc_hdr->jt_offset));
175 			DRM_DEBUG("jt_size: %u\n", le32_to_cpu(rlc_hdr->jt_size));
176 			DRM_DEBUG("save_and_restore_offset: %u\n",
177 				  le32_to_cpu(rlc_hdr->save_and_restore_offset));
178 			DRM_DEBUG("clear_state_descriptor_offset: %u\n",
179 				  le32_to_cpu(rlc_hdr->clear_state_descriptor_offset));
180 			DRM_DEBUG("avail_scratch_ram_locations: %u\n",
181 				  le32_to_cpu(rlc_hdr->avail_scratch_ram_locations));
182 			DRM_DEBUG("reg_restore_list_size: %u\n",
183 				  le32_to_cpu(rlc_hdr->reg_restore_list_size));
184 			DRM_DEBUG("reg_list_format_start: %u\n",
185 				  le32_to_cpu(rlc_hdr->reg_list_format_start));
186 			DRM_DEBUG("reg_list_format_separate_start: %u\n",
187 				  le32_to_cpu(rlc_hdr->reg_list_format_separate_start));
188 			DRM_DEBUG("starting_offsets_start: %u\n",
189 				  le32_to_cpu(rlc_hdr->starting_offsets_start));
190 			DRM_DEBUG("reg_list_format_size_bytes: %u\n",
191 				  le32_to_cpu(rlc_hdr->reg_list_format_size_bytes));
192 			DRM_DEBUG("reg_list_format_array_offset_bytes: %u\n",
193 				  le32_to_cpu(rlc_hdr->reg_list_format_array_offset_bytes));
194 			DRM_DEBUG("reg_list_size_bytes: %u\n",
195 				  le32_to_cpu(rlc_hdr->reg_list_size_bytes));
196 			DRM_DEBUG("reg_list_array_offset_bytes: %u\n",
197 				  le32_to_cpu(rlc_hdr->reg_list_array_offset_bytes));
198 			DRM_DEBUG("reg_list_format_separate_size_bytes: %u\n",
199 				  le32_to_cpu(rlc_hdr->reg_list_format_separate_size_bytes));
200 			DRM_DEBUG("reg_list_format_separate_array_offset_bytes: %u\n",
201 				  le32_to_cpu(rlc_hdr->reg_list_format_separate_array_offset_bytes));
202 			DRM_DEBUG("reg_list_separate_size_bytes: %u\n",
203 				  le32_to_cpu(rlc_hdr->reg_list_separate_size_bytes));
204 			DRM_DEBUG("reg_list_separate_array_offset_bytes: %u\n",
205 				  le32_to_cpu(rlc_hdr->reg_list_separate_array_offset_bytes));
206 			break;
207 		case 1:
208 			/* rlc_hdr v2_1 */
209 			DRM_DEBUG("reg_list_format_direct_reg_list_length: %u\n",
210 				  le32_to_cpu(rlc_hdr_v2_1->reg_list_format_direct_reg_list_length));
211 			DRM_DEBUG("save_restore_list_cntl_ucode_ver: %u\n",
212 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_cntl_ucode_ver));
213 			DRM_DEBUG("save_restore_list_cntl_feature_ver: %u\n",
214 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_cntl_feature_ver));
215 			DRM_DEBUG("save_restore_list_cntl_size_bytes %u\n",
216 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_cntl_size_bytes));
217 			DRM_DEBUG("save_restore_list_cntl_offset_bytes: %u\n",
218 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_cntl_offset_bytes));
219 			DRM_DEBUG("save_restore_list_gpm_ucode_ver: %u\n",
220 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_gpm_ucode_ver));
221 			DRM_DEBUG("save_restore_list_gpm_feature_ver: %u\n",
222 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_gpm_feature_ver));
223 			DRM_DEBUG("save_restore_list_gpm_size_bytes %u\n",
224 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_gpm_size_bytes));
225 			DRM_DEBUG("save_restore_list_gpm_offset_bytes: %u\n",
226 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_gpm_offset_bytes));
227 			DRM_DEBUG("save_restore_list_srm_ucode_ver: %u\n",
228 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_srm_ucode_ver));
229 			DRM_DEBUG("save_restore_list_srm_feature_ver: %u\n",
230 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_srm_feature_ver));
231 			DRM_DEBUG("save_restore_list_srm_size_bytes %u\n",
232 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_srm_size_bytes));
233 			DRM_DEBUG("save_restore_list_srm_offset_bytes: %u\n",
234 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_srm_offset_bytes));
235 			break;
236 		case 2:
237 			/* rlc_hdr v2_2 */
238 			DRM_DEBUG("rlc_iram_ucode_size_bytes: %u\n",
239 				  le32_to_cpu(rlc_hdr_v2_2->rlc_iram_ucode_size_bytes));
240 			DRM_DEBUG("rlc_iram_ucode_offset_bytes: %u\n",
241 				  le32_to_cpu(rlc_hdr_v2_2->rlc_iram_ucode_offset_bytes));
242 			DRM_DEBUG("rlc_dram_ucode_size_bytes: %u\n",
243 				  le32_to_cpu(rlc_hdr_v2_2->rlc_dram_ucode_size_bytes));
244 			DRM_DEBUG("rlc_dram_ucode_offset_bytes: %u\n",
245 				  le32_to_cpu(rlc_hdr_v2_2->rlc_dram_ucode_offset_bytes));
246 			break;
247 		case 3:
248 			/* rlc_hdr v2_3 */
249 			DRM_DEBUG("rlcp_ucode_version: %u\n",
250 				  le32_to_cpu(rlc_hdr_v2_3->rlcp_ucode_version));
251 			DRM_DEBUG("rlcp_ucode_feature_version: %u\n",
252 				  le32_to_cpu(rlc_hdr_v2_3->rlcp_ucode_feature_version));
253 			DRM_DEBUG("rlcp_ucode_size_bytes: %u\n",
254 				  le32_to_cpu(rlc_hdr_v2_3->rlcp_ucode_size_bytes));
255 			DRM_DEBUG("rlcp_ucode_offset_bytes: %u\n",
256 				  le32_to_cpu(rlc_hdr_v2_3->rlcp_ucode_offset_bytes));
257 			DRM_DEBUG("rlcv_ucode_version: %u\n",
258 				  le32_to_cpu(rlc_hdr_v2_3->rlcv_ucode_version));
259 			DRM_DEBUG("rlcv_ucode_feature_version: %u\n",
260 				  le32_to_cpu(rlc_hdr_v2_3->rlcv_ucode_feature_version));
261 			DRM_DEBUG("rlcv_ucode_size_bytes: %u\n",
262 				  le32_to_cpu(rlc_hdr_v2_3->rlcv_ucode_size_bytes));
263 			DRM_DEBUG("rlcv_ucode_offset_bytes: %u\n",
264 				  le32_to_cpu(rlc_hdr_v2_3->rlcv_ucode_offset_bytes));
265 			break;
266 		case 4:
267 			/* rlc_hdr v2_4 */
268 			DRM_DEBUG("global_tap_delays_ucode_size_bytes :%u\n",
269 				  le32_to_cpu(rlc_hdr_v2_4->global_tap_delays_ucode_size_bytes));
270 			DRM_DEBUG("global_tap_delays_ucode_offset_bytes: %u\n",
271 				  le32_to_cpu(rlc_hdr_v2_4->global_tap_delays_ucode_offset_bytes));
272 			DRM_DEBUG("se0_tap_delays_ucode_size_bytes :%u\n",
273 				  le32_to_cpu(rlc_hdr_v2_4->se0_tap_delays_ucode_size_bytes));
274 			DRM_DEBUG("se0_tap_delays_ucode_offset_bytes: %u\n",
275 				  le32_to_cpu(rlc_hdr_v2_4->se0_tap_delays_ucode_offset_bytes));
276 			DRM_DEBUG("se1_tap_delays_ucode_size_bytes :%u\n",
277 				  le32_to_cpu(rlc_hdr_v2_4->se1_tap_delays_ucode_size_bytes));
278 			DRM_DEBUG("se1_tap_delays_ucode_offset_bytes: %u\n",
279 				  le32_to_cpu(rlc_hdr_v2_4->se1_tap_delays_ucode_offset_bytes));
280 			DRM_DEBUG("se2_tap_delays_ucode_size_bytes :%u\n",
281 				  le32_to_cpu(rlc_hdr_v2_4->se2_tap_delays_ucode_size_bytes));
282 			DRM_DEBUG("se2_tap_delays_ucode_offset_bytes: %u\n",
283 				  le32_to_cpu(rlc_hdr_v2_4->se2_tap_delays_ucode_offset_bytes));
284 			DRM_DEBUG("se3_tap_delays_ucode_size_bytes :%u\n",
285 				  le32_to_cpu(rlc_hdr_v2_4->se3_tap_delays_ucode_size_bytes));
286 			DRM_DEBUG("se3_tap_delays_ucode_offset_bytes: %u\n",
287 				  le32_to_cpu(rlc_hdr_v2_4->se3_tap_delays_ucode_offset_bytes));
288 			break;
289 		default:
290 			DRM_ERROR("Unknown RLC v2 ucode: v2.%u\n", version_minor);
291 			break;
292 		}
293 	} else {
294 		DRM_ERROR("Unknown RLC ucode version: %u.%u\n", version_major, version_minor);
295 	}
296 }
297 
amdgpu_ucode_print_sdma_hdr(const struct common_firmware_header * hdr)298 void amdgpu_ucode_print_sdma_hdr(const struct common_firmware_header *hdr)
299 {
300 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
301 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
302 
303 	DRM_DEBUG("SDMA\n");
304 	amdgpu_ucode_print_common_hdr(hdr);
305 
306 	if (version_major == 1) {
307 		const struct sdma_firmware_header_v1_0 *sdma_hdr =
308 			container_of(hdr, struct sdma_firmware_header_v1_0, header);
309 
310 		DRM_DEBUG("ucode_feature_version: %u\n",
311 			  le32_to_cpu(sdma_hdr->ucode_feature_version));
312 		DRM_DEBUG("ucode_change_version: %u\n",
313 			  le32_to_cpu(sdma_hdr->ucode_change_version));
314 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(sdma_hdr->jt_offset));
315 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(sdma_hdr->jt_size));
316 		if (version_minor >= 1) {
317 			const struct sdma_firmware_header_v1_1 *sdma_v1_1_hdr =
318 				container_of(sdma_hdr, struct sdma_firmware_header_v1_1, v1_0);
319 			DRM_DEBUG("digest_size: %u\n", le32_to_cpu(sdma_v1_1_hdr->digest_size));
320 		}
321 	} else if (version_major == 2) {
322 		const struct sdma_firmware_header_v2_0 *sdma_hdr =
323 			container_of(hdr, struct sdma_firmware_header_v2_0, header);
324 
325 		DRM_DEBUG("ucode_feature_version: %u\n",
326 			  le32_to_cpu(sdma_hdr->ucode_feature_version));
327 		DRM_DEBUG("ctx_jt_offset: %u\n", le32_to_cpu(sdma_hdr->ctx_jt_offset));
328 		DRM_DEBUG("ctx_jt_size: %u\n", le32_to_cpu(sdma_hdr->ctx_jt_size));
329 		DRM_DEBUG("ctl_ucode_offset: %u\n", le32_to_cpu(sdma_hdr->ctl_ucode_offset));
330 		DRM_DEBUG("ctl_jt_offset: %u\n", le32_to_cpu(sdma_hdr->ctl_jt_offset));
331 		DRM_DEBUG("ctl_jt_size: %u\n", le32_to_cpu(sdma_hdr->ctl_jt_size));
332 	} else if (version_major == 3) {
333 		const struct sdma_firmware_header_v3_0 *sdma_hdr =
334 			container_of(hdr, struct sdma_firmware_header_v3_0, header);
335 
336 		DRM_DEBUG("ucode_reversion: %u\n",
337 			  le32_to_cpu(sdma_hdr->ucode_feature_version));
338 	} else {
339 		DRM_ERROR("Unknown SDMA ucode version: %u.%u\n",
340 			  version_major, version_minor);
341 	}
342 }
343 
amdgpu_ucode_print_psp_hdr(const struct common_firmware_header * hdr)344 void amdgpu_ucode_print_psp_hdr(const struct common_firmware_header *hdr)
345 {
346 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
347 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
348 	uint32_t fw_index;
349 	const struct psp_fw_bin_desc *desc;
350 
351 	DRM_DEBUG("PSP\n");
352 	amdgpu_ucode_print_common_hdr(hdr);
353 
354 	if (version_major == 1) {
355 		const struct psp_firmware_header_v1_0 *psp_hdr =
356 			container_of(hdr, struct psp_firmware_header_v1_0, header);
357 
358 		DRM_DEBUG("ucode_feature_version: %u\n",
359 			  le32_to_cpu(psp_hdr->sos.fw_version));
360 		DRM_DEBUG("sos_offset_bytes: %u\n",
361 			  le32_to_cpu(psp_hdr->sos.offset_bytes));
362 		DRM_DEBUG("sos_size_bytes: %u\n",
363 			  le32_to_cpu(psp_hdr->sos.size_bytes));
364 		if (version_minor == 1) {
365 			const struct psp_firmware_header_v1_1 *psp_hdr_v1_1 =
366 				container_of(psp_hdr, struct psp_firmware_header_v1_1, v1_0);
367 			DRM_DEBUG("toc_header_version: %u\n",
368 				  le32_to_cpu(psp_hdr_v1_1->toc.fw_version));
369 			DRM_DEBUG("toc_offset_bytes: %u\n",
370 				  le32_to_cpu(psp_hdr_v1_1->toc.offset_bytes));
371 			DRM_DEBUG("toc_size_bytes: %u\n",
372 				  le32_to_cpu(psp_hdr_v1_1->toc.size_bytes));
373 			DRM_DEBUG("kdb_header_version: %u\n",
374 				  le32_to_cpu(psp_hdr_v1_1->kdb.fw_version));
375 			DRM_DEBUG("kdb_offset_bytes: %u\n",
376 				  le32_to_cpu(psp_hdr_v1_1->kdb.offset_bytes));
377 			DRM_DEBUG("kdb_size_bytes: %u\n",
378 				  le32_to_cpu(psp_hdr_v1_1->kdb.size_bytes));
379 		}
380 		if (version_minor == 2) {
381 			const struct psp_firmware_header_v1_2 *psp_hdr_v1_2 =
382 				container_of(psp_hdr, struct psp_firmware_header_v1_2, v1_0);
383 			DRM_DEBUG("kdb_header_version: %u\n",
384 				  le32_to_cpu(psp_hdr_v1_2->kdb.fw_version));
385 			DRM_DEBUG("kdb_offset_bytes: %u\n",
386 				  le32_to_cpu(psp_hdr_v1_2->kdb.offset_bytes));
387 			DRM_DEBUG("kdb_size_bytes: %u\n",
388 				  le32_to_cpu(psp_hdr_v1_2->kdb.size_bytes));
389 		}
390 		if (version_minor == 3) {
391 			const struct psp_firmware_header_v1_1 *psp_hdr_v1_1 =
392 				container_of(psp_hdr, struct psp_firmware_header_v1_1, v1_0);
393 			const struct psp_firmware_header_v1_3 *psp_hdr_v1_3 =
394 				container_of(psp_hdr_v1_1, struct psp_firmware_header_v1_3, v1_1);
395 			DRM_DEBUG("toc_header_version: %u\n",
396 				  le32_to_cpu(psp_hdr_v1_3->v1_1.toc.fw_version));
397 			DRM_DEBUG("toc_offset_bytes: %u\n",
398 				  le32_to_cpu(psp_hdr_v1_3->v1_1.toc.offset_bytes));
399 			DRM_DEBUG("toc_size_bytes: %u\n",
400 				  le32_to_cpu(psp_hdr_v1_3->v1_1.toc.size_bytes));
401 			DRM_DEBUG("kdb_header_version: %u\n",
402 				  le32_to_cpu(psp_hdr_v1_3->v1_1.kdb.fw_version));
403 			DRM_DEBUG("kdb_offset_bytes: %u\n",
404 				  le32_to_cpu(psp_hdr_v1_3->v1_1.kdb.offset_bytes));
405 			DRM_DEBUG("kdb_size_bytes: %u\n",
406 				  le32_to_cpu(psp_hdr_v1_3->v1_1.kdb.size_bytes));
407 			DRM_DEBUG("spl_header_version: %u\n",
408 				  le32_to_cpu(psp_hdr_v1_3->spl.fw_version));
409 			DRM_DEBUG("spl_offset_bytes: %u\n",
410 				  le32_to_cpu(psp_hdr_v1_3->spl.offset_bytes));
411 			DRM_DEBUG("spl_size_bytes: %u\n",
412 				  le32_to_cpu(psp_hdr_v1_3->spl.size_bytes));
413 		}
414 	} else if (version_major == 2) {
415 		const struct psp_firmware_header_v2_0 *psp_hdr_v2_0 =
416 			 container_of(hdr, struct psp_firmware_header_v2_0, header);
417 		for (fw_index = 0; fw_index < le32_to_cpu(psp_hdr_v2_0->psp_fw_bin_count); fw_index++) {
418 			desc = &(psp_hdr_v2_0->psp_fw_bin[fw_index]);
419 			switch (desc->fw_type) {
420 			case PSP_FW_TYPE_PSP_SOS:
421 				DRM_DEBUG("psp_sos_version: %u\n",
422 					  le32_to_cpu(desc->fw_version));
423 				DRM_DEBUG("psp_sos_size_bytes: %u\n",
424 					  le32_to_cpu(desc->size_bytes));
425 				break;
426 			case PSP_FW_TYPE_PSP_SYS_DRV:
427 				DRM_DEBUG("psp_sys_drv_version: %u\n",
428 					  le32_to_cpu(desc->fw_version));
429 				DRM_DEBUG("psp_sys_drv_size_bytes: %u\n",
430 					  le32_to_cpu(desc->size_bytes));
431 				break;
432 			case PSP_FW_TYPE_PSP_KDB:
433 				DRM_DEBUG("psp_kdb_version: %u\n",
434 					  le32_to_cpu(desc->fw_version));
435 				DRM_DEBUG("psp_kdb_size_bytes: %u\n",
436 					  le32_to_cpu(desc->size_bytes));
437 				break;
438 			case PSP_FW_TYPE_PSP_TOC:
439 				DRM_DEBUG("psp_toc_version: %u\n",
440 					  le32_to_cpu(desc->fw_version));
441 				DRM_DEBUG("psp_toc_size_bytes: %u\n",
442 					  le32_to_cpu(desc->size_bytes));
443 				break;
444 			case PSP_FW_TYPE_PSP_SPL:
445 				DRM_DEBUG("psp_spl_version: %u\n",
446 					  le32_to_cpu(desc->fw_version));
447 				DRM_DEBUG("psp_spl_size_bytes: %u\n",
448 					  le32_to_cpu(desc->size_bytes));
449 				break;
450 			case PSP_FW_TYPE_PSP_RL:
451 				DRM_DEBUG("psp_rl_version: %u\n",
452 					  le32_to_cpu(desc->fw_version));
453 				DRM_DEBUG("psp_rl_size_bytes: %u\n",
454 					  le32_to_cpu(desc->size_bytes));
455 				break;
456 			case PSP_FW_TYPE_PSP_SOC_DRV:
457 				DRM_DEBUG("psp_soc_drv_version: %u\n",
458 					  le32_to_cpu(desc->fw_version));
459 				DRM_DEBUG("psp_soc_drv_size_bytes: %u\n",
460 					  le32_to_cpu(desc->size_bytes));
461 				break;
462 			case PSP_FW_TYPE_PSP_INTF_DRV:
463 				DRM_DEBUG("psp_intf_drv_version: %u\n",
464 					  le32_to_cpu(desc->fw_version));
465 				DRM_DEBUG("psp_intf_drv_size_bytes: %u\n",
466 					  le32_to_cpu(desc->size_bytes));
467 				break;
468 			case PSP_FW_TYPE_PSP_DBG_DRV:
469 				DRM_DEBUG("psp_dbg_drv_version: %u\n",
470 					  le32_to_cpu(desc->fw_version));
471 				DRM_DEBUG("psp_dbg_drv_size_bytes: %u\n",
472 					  le32_to_cpu(desc->size_bytes));
473 				break;
474 			case PSP_FW_TYPE_PSP_RAS_DRV:
475 				DRM_DEBUG("psp_ras_drv_version: %u\n",
476 					  le32_to_cpu(desc->fw_version));
477 				DRM_DEBUG("psp_ras_drv_size_bytes: %u\n",
478 					  le32_to_cpu(desc->size_bytes));
479 				break;
480 			default:
481 				DRM_DEBUG("Unsupported PSP fw type: %d\n", desc->fw_type);
482 				break;
483 			}
484 		}
485 	} else {
486 		DRM_ERROR("Unknown PSP ucode version: %u.%u\n",
487 			  version_major, version_minor);
488 	}
489 }
490 
amdgpu_ucode_print_gpu_info_hdr(const struct common_firmware_header * hdr)491 void amdgpu_ucode_print_gpu_info_hdr(const struct common_firmware_header *hdr)
492 {
493 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
494 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
495 
496 	DRM_DEBUG("GPU_INFO\n");
497 	amdgpu_ucode_print_common_hdr(hdr);
498 
499 	if (version_major == 1) {
500 		const struct gpu_info_firmware_header_v1_0 *gpu_info_hdr =
501 			container_of(hdr, struct gpu_info_firmware_header_v1_0, header);
502 
503 		DRM_DEBUG("version_major: %u\n",
504 			  le16_to_cpu(gpu_info_hdr->version_major));
505 		DRM_DEBUG("version_minor: %u\n",
506 			  le16_to_cpu(gpu_info_hdr->version_minor));
507 	} else {
508 		DRM_ERROR("Unknown gpu_info ucode version: %u.%u\n", version_major, version_minor);
509 	}
510 }
511 
amdgpu_ucode_validate(const struct firmware * fw)512 static int amdgpu_ucode_validate(const struct firmware *fw)
513 {
514 	const struct common_firmware_header *hdr =
515 		(const struct common_firmware_header *)fw->data;
516 
517 	if (fw->size == le32_to_cpu(hdr->size_bytes))
518 		return 0;
519 
520 	return -EINVAL;
521 }
522 
amdgpu_ucode_hdr_version(union amdgpu_firmware_header * hdr,uint16_t hdr_major,uint16_t hdr_minor)523 bool amdgpu_ucode_hdr_version(union amdgpu_firmware_header *hdr,
524 				uint16_t hdr_major, uint16_t hdr_minor)
525 {
526 	if ((hdr->common.header_version_major == hdr_major) &&
527 		(hdr->common.header_version_minor == hdr_minor))
528 		return true;
529 	return false;
530 }
531 
532 enum amdgpu_firmware_load_type
amdgpu_ucode_get_load_type(struct amdgpu_device * adev,int load_type)533 amdgpu_ucode_get_load_type(struct amdgpu_device *adev, int load_type)
534 {
535 	switch (adev->asic_type) {
536 #ifdef CONFIG_DRM_AMDGPU_SI
537 	case CHIP_TAHITI:
538 	case CHIP_PITCAIRN:
539 	case CHIP_VERDE:
540 	case CHIP_OLAND:
541 	case CHIP_HAINAN:
542 		return AMDGPU_FW_LOAD_DIRECT;
543 #endif
544 #ifdef CONFIG_DRM_AMDGPU_CIK
545 	case CHIP_BONAIRE:
546 	case CHIP_KAVERI:
547 	case CHIP_KABINI:
548 	case CHIP_HAWAII:
549 	case CHIP_MULLINS:
550 		return AMDGPU_FW_LOAD_DIRECT;
551 #endif
552 	case CHIP_TOPAZ:
553 	case CHIP_TONGA:
554 	case CHIP_FIJI:
555 	case CHIP_CARRIZO:
556 	case CHIP_STONEY:
557 	case CHIP_POLARIS10:
558 	case CHIP_POLARIS11:
559 	case CHIP_POLARIS12:
560 	case CHIP_VEGAM:
561 		return AMDGPU_FW_LOAD_SMU;
562 	case CHIP_CYAN_SKILLFISH:
563 		if (!(load_type &&
564 		      adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2))
565 			return AMDGPU_FW_LOAD_DIRECT;
566 		else
567 			return AMDGPU_FW_LOAD_PSP;
568 	default:
569 		if (!load_type)
570 			return AMDGPU_FW_LOAD_DIRECT;
571 		else if (load_type == 3)
572 			return AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO;
573 		else
574 			return AMDGPU_FW_LOAD_PSP;
575 	}
576 }
577 
amdgpu_ucode_name(enum AMDGPU_UCODE_ID ucode_id)578 const char *amdgpu_ucode_name(enum AMDGPU_UCODE_ID ucode_id)
579 {
580 	switch (ucode_id) {
581 	case AMDGPU_UCODE_ID_SDMA0:
582 		return "SDMA0";
583 	case AMDGPU_UCODE_ID_SDMA1:
584 		return "SDMA1";
585 	case AMDGPU_UCODE_ID_SDMA2:
586 		return "SDMA2";
587 	case AMDGPU_UCODE_ID_SDMA3:
588 		return "SDMA3";
589 	case AMDGPU_UCODE_ID_SDMA4:
590 		return "SDMA4";
591 	case AMDGPU_UCODE_ID_SDMA5:
592 		return "SDMA5";
593 	case AMDGPU_UCODE_ID_SDMA6:
594 		return "SDMA6";
595 	case AMDGPU_UCODE_ID_SDMA7:
596 		return "SDMA7";
597 	case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
598 		return "SDMA_CTX";
599 	case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
600 		return "SDMA_CTL";
601 	case AMDGPU_UCODE_ID_CP_CE:
602 		return "CP_CE";
603 	case AMDGPU_UCODE_ID_CP_PFP:
604 		return "CP_PFP";
605 	case AMDGPU_UCODE_ID_CP_ME:
606 		return "CP_ME";
607 	case AMDGPU_UCODE_ID_CP_MEC1:
608 		return "CP_MEC1";
609 	case AMDGPU_UCODE_ID_CP_MEC1_JT:
610 		return "CP_MEC1_JT";
611 	case AMDGPU_UCODE_ID_CP_MEC2:
612 		return "CP_MEC2";
613 	case AMDGPU_UCODE_ID_CP_MEC2_JT:
614 		return "CP_MEC2_JT";
615 	case AMDGPU_UCODE_ID_CP_MES:
616 		return "CP_MES";
617 	case AMDGPU_UCODE_ID_CP_MES_DATA:
618 		return "CP_MES_DATA";
619 	case AMDGPU_UCODE_ID_CP_MES1:
620 		return "CP_MES_KIQ";
621 	case AMDGPU_UCODE_ID_CP_MES1_DATA:
622 		return "CP_MES_KIQ_DATA";
623 	case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
624 		return "RLC_RESTORE_LIST_CNTL";
625 	case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
626 		return "RLC_RESTORE_LIST_GPM_MEM";
627 	case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
628 		return "RLC_RESTORE_LIST_SRM_MEM";
629 	case AMDGPU_UCODE_ID_RLC_IRAM:
630 		return "RLC_IRAM";
631 	case AMDGPU_UCODE_ID_RLC_DRAM:
632 		return "RLC_DRAM";
633 	case AMDGPU_UCODE_ID_RLC_G:
634 		return "RLC_G";
635 	case AMDGPU_UCODE_ID_RLC_P:
636 		return "RLC_P";
637 	case AMDGPU_UCODE_ID_RLC_V:
638 		return "RLC_V";
639 	case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
640 		return "GLOBAL_TAP_DELAYS";
641 	case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
642 		return "SE0_TAP_DELAYS";
643 	case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
644 		return "SE1_TAP_DELAYS";
645 	case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
646 		return "SE2_TAP_DELAYS";
647 	case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
648 		return "SE3_TAP_DELAYS";
649 	case AMDGPU_UCODE_ID_IMU_I:
650 		return "IMU_I";
651 	case AMDGPU_UCODE_ID_IMU_D:
652 		return "IMU_D";
653 	case AMDGPU_UCODE_ID_STORAGE:
654 		return "STORAGE";
655 	case AMDGPU_UCODE_ID_SMC:
656 		return "SMC";
657 	case AMDGPU_UCODE_ID_PPTABLE:
658 		return "PPTABLE";
659 	case AMDGPU_UCODE_ID_P2S_TABLE:
660 		return "P2STABLE";
661 	case AMDGPU_UCODE_ID_UVD:
662 		return "UVD";
663 	case AMDGPU_UCODE_ID_UVD1:
664 		return "UVD1";
665 	case AMDGPU_UCODE_ID_VCE:
666 		return "VCE";
667 	case AMDGPU_UCODE_ID_VCN:
668 		return "VCN";
669 	case AMDGPU_UCODE_ID_VCN1:
670 		return "VCN1";
671 	case AMDGPU_UCODE_ID_DMCU_ERAM:
672 		return "DMCU_ERAM";
673 	case AMDGPU_UCODE_ID_DMCU_INTV:
674 		return "DMCU_INTV";
675 	case AMDGPU_UCODE_ID_VCN0_RAM:
676 		return "VCN0_RAM";
677 	case AMDGPU_UCODE_ID_VCN1_RAM:
678 		return "VCN1_RAM";
679 	case AMDGPU_UCODE_ID_DMCUB:
680 		return "DMCUB";
681 	case AMDGPU_UCODE_ID_CAP:
682 		return "CAP";
683 	case AMDGPU_UCODE_ID_VPE_CTX:
684 		return "VPE_CTX";
685 	case AMDGPU_UCODE_ID_VPE_CTL:
686 		return "VPE_CTL";
687 	case AMDGPU_UCODE_ID_VPE:
688 		return "VPE";
689 	case AMDGPU_UCODE_ID_UMSCH_MM_UCODE:
690 		return "UMSCH_MM_UCODE";
691 	case AMDGPU_UCODE_ID_UMSCH_MM_DATA:
692 		return "UMSCH_MM_DATA";
693 	case AMDGPU_UCODE_ID_UMSCH_MM_CMD_BUFFER:
694 		return "UMSCH_MM_CMD_BUFFER";
695 	case AMDGPU_UCODE_ID_JPEG_RAM:
696 		return "JPEG";
697 	case AMDGPU_UCODE_ID_SDMA_RS64:
698 		return "RS64_SDMA";
699 	case AMDGPU_UCODE_ID_CP_RS64_PFP:
700 		return "RS64_PFP";
701 	case AMDGPU_UCODE_ID_CP_RS64_ME:
702 		return "RS64_ME";
703 	case AMDGPU_UCODE_ID_CP_RS64_MEC:
704 		return "RS64_MEC";
705 	case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
706 		return "RS64_PFP_P0_STACK";
707 	case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
708 		return "RS64_PFP_P1_STACK";
709 	case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
710 		return "RS64_ME_P0_STACK";
711 	case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
712 		return "RS64_ME_P1_STACK";
713 	case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
714 		return "RS64_MEC_P0_STACK";
715 	case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
716 		return "RS64_MEC_P1_STACK";
717 	case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
718 		return "RS64_MEC_P2_STACK";
719 	case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
720 		return "RS64_MEC_P3_STACK";
721 	case AMDGPU_UCODE_ID_ISP:
722 		return "ISP";
723 	default:
724 		return "UNKNOWN UCODE";
725 	}
726 }
727 
amdgpu_ucode_is_valid(uint32_t fw_version)728 static inline int amdgpu_ucode_is_valid(uint32_t fw_version)
729 {
730 	if (!fw_version)
731 		return -EINVAL;
732 
733 	return 0;
734 }
735 
736 #define FW_VERSION_ATTR(name, mode, field)				\
737 static ssize_t show_##name(struct device *dev,				\
738 			   struct device_attribute *attr, char *buf)	\
739 {									\
740 	struct drm_device *ddev = dev_get_drvdata(dev);			\
741 	struct amdgpu_device *adev = drm_to_adev(ddev);			\
742 									\
743 	if (!buf)							\
744 		return amdgpu_ucode_is_valid(adev->field);		\
745 									\
746 	return sysfs_emit(buf, "0x%08x\n", adev->field);		\
747 }									\
748 static DEVICE_ATTR(name, mode, show_##name, NULL)
749 
750 FW_VERSION_ATTR(vce_fw_version, 0444, vce.fw_version);
751 FW_VERSION_ATTR(uvd_fw_version, 0444, uvd.fw_version);
752 FW_VERSION_ATTR(mc_fw_version, 0444, gmc.fw_version);
753 FW_VERSION_ATTR(me_fw_version, 0444, gfx.me_fw_version);
754 FW_VERSION_ATTR(pfp_fw_version, 0444, gfx.pfp_fw_version);
755 FW_VERSION_ATTR(ce_fw_version, 0444, gfx.ce_fw_version);
756 FW_VERSION_ATTR(rlc_fw_version, 0444, gfx.rlc_fw_version);
757 FW_VERSION_ATTR(rlc_srlc_fw_version, 0444, gfx.rlc_srlc_fw_version);
758 FW_VERSION_ATTR(rlc_srlg_fw_version, 0444, gfx.rlc_srlg_fw_version);
759 FW_VERSION_ATTR(rlc_srls_fw_version, 0444, gfx.rlc_srls_fw_version);
760 FW_VERSION_ATTR(mec_fw_version, 0444, gfx.mec_fw_version);
761 FW_VERSION_ATTR(mec2_fw_version, 0444, gfx.mec2_fw_version);
762 FW_VERSION_ATTR(imu_fw_version, 0444, gfx.imu_fw_version);
763 FW_VERSION_ATTR(sos_fw_version, 0444, psp.sos.fw_version);
764 FW_VERSION_ATTR(asd_fw_version, 0444, psp.asd_context.bin_desc.fw_version);
765 FW_VERSION_ATTR(ta_ras_fw_version, 0444, psp.ras_context.context.bin_desc.fw_version);
766 FW_VERSION_ATTR(ta_xgmi_fw_version, 0444, psp.xgmi_context.context.bin_desc.fw_version);
767 FW_VERSION_ATTR(smc_fw_version, 0444, pm.fw_version);
768 FW_VERSION_ATTR(sdma_fw_version, 0444, sdma.instance[0].fw_version);
769 FW_VERSION_ATTR(sdma2_fw_version, 0444, sdma.instance[1].fw_version);
770 FW_VERSION_ATTR(vcn_fw_version, 0444, vcn.fw_version);
771 FW_VERSION_ATTR(dmcu_fw_version, 0444, dm.dmcu_fw_version);
772 FW_VERSION_ATTR(mes_fw_version, 0444, mes.sched_version & AMDGPU_MES_VERSION_MASK);
773 FW_VERSION_ATTR(mes_kiq_fw_version, 0444, mes.kiq_version & AMDGPU_MES_VERSION_MASK);
774 
775 static struct attribute *fw_attrs[] = {
776 	&dev_attr_vce_fw_version.attr, &dev_attr_uvd_fw_version.attr,
777 	&dev_attr_mc_fw_version.attr, &dev_attr_me_fw_version.attr,
778 	&dev_attr_pfp_fw_version.attr, &dev_attr_ce_fw_version.attr,
779 	&dev_attr_rlc_fw_version.attr, &dev_attr_rlc_srlc_fw_version.attr,
780 	&dev_attr_rlc_srlg_fw_version.attr, &dev_attr_rlc_srls_fw_version.attr,
781 	&dev_attr_mec_fw_version.attr, &dev_attr_mec2_fw_version.attr,
782 	&dev_attr_sos_fw_version.attr, &dev_attr_asd_fw_version.attr,
783 	&dev_attr_ta_ras_fw_version.attr, &dev_attr_ta_xgmi_fw_version.attr,
784 	&dev_attr_smc_fw_version.attr, &dev_attr_sdma_fw_version.attr,
785 	&dev_attr_sdma2_fw_version.attr, &dev_attr_vcn_fw_version.attr,
786 	&dev_attr_dmcu_fw_version.attr, &dev_attr_imu_fw_version.attr,
787 	&dev_attr_mes_fw_version.attr, &dev_attr_mes_kiq_fw_version.attr,
788 	NULL
789 };
790 
791 #define to_dev_attr(x) container_of(x, struct device_attribute, attr)
792 
amdgpu_ucode_sys_visible(struct kobject * kobj,struct attribute * attr,int idx)793 static umode_t amdgpu_ucode_sys_visible(struct kobject *kobj,
794 					struct attribute *attr, int idx)
795 {
796 	struct device_attribute *dev_attr = to_dev_attr(attr);
797 	struct device *dev = kobj_to_dev(kobj);
798 
799 	if (dev_attr->show(dev, dev_attr, NULL) == -EINVAL)
800 		return 0;
801 
802 	return attr->mode;
803 }
804 
805 static const struct attribute_group fw_attr_group = {
806 	.name = "fw_version",
807 	.attrs = fw_attrs,
808 	.is_visible = amdgpu_ucode_sys_visible
809 };
810 
amdgpu_ucode_sysfs_init(struct amdgpu_device * adev)811 int amdgpu_ucode_sysfs_init(struct amdgpu_device *adev)
812 {
813 	return sysfs_create_group(&adev->dev->kobj, &fw_attr_group);
814 }
815 
amdgpu_ucode_sysfs_fini(struct amdgpu_device * adev)816 void amdgpu_ucode_sysfs_fini(struct amdgpu_device *adev)
817 {
818 	sysfs_remove_group(&adev->dev->kobj, &fw_attr_group);
819 }
820 
amdgpu_ucode_init_single_fw(struct amdgpu_device * adev,struct amdgpu_firmware_info * ucode,uint64_t mc_addr,void * kptr)821 static int amdgpu_ucode_init_single_fw(struct amdgpu_device *adev,
822 				       struct amdgpu_firmware_info *ucode,
823 				       uint64_t mc_addr, void *kptr)
824 {
825 	const struct common_firmware_header *header = NULL;
826 	const struct gfx_firmware_header_v1_0 *cp_hdr = NULL;
827 	const struct gfx_firmware_header_v2_0 *cpv2_hdr = NULL;
828 	const struct dmcu_firmware_header_v1_0 *dmcu_hdr = NULL;
829 	const struct dmcub_firmware_header_v1_0 *dmcub_hdr = NULL;
830 	const struct mes_firmware_header_v1_0 *mes_hdr = NULL;
831 	const struct sdma_firmware_header_v2_0 *sdma_hdr = NULL;
832 	const struct sdma_firmware_header_v3_0 *sdmav3_hdr = NULL;
833 	const struct imu_firmware_header_v1_0 *imu_hdr = NULL;
834 	const struct vpe_firmware_header_v1_0 *vpe_hdr = NULL;
835 	const struct umsch_mm_firmware_header_v1_0 *umsch_mm_hdr = NULL;
836 	u8 *ucode_addr;
837 
838 	if (!ucode->fw)
839 		return 0;
840 
841 	ucode->mc_addr = mc_addr;
842 	ucode->kaddr = kptr;
843 
844 	if (ucode->ucode_id == AMDGPU_UCODE_ID_STORAGE)
845 		return 0;
846 
847 	header = (const struct common_firmware_header *)ucode->fw->data;
848 	cp_hdr = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
849 	cpv2_hdr = (const struct gfx_firmware_header_v2_0 *)ucode->fw->data;
850 	dmcu_hdr = (const struct dmcu_firmware_header_v1_0 *)ucode->fw->data;
851 	dmcub_hdr = (const struct dmcub_firmware_header_v1_0 *)ucode->fw->data;
852 	mes_hdr = (const struct mes_firmware_header_v1_0 *)ucode->fw->data;
853 	sdma_hdr = (const struct sdma_firmware_header_v2_0 *)ucode->fw->data;
854 	sdmav3_hdr = (const struct sdma_firmware_header_v3_0 *)ucode->fw->data;
855 	imu_hdr = (const struct imu_firmware_header_v1_0 *)ucode->fw->data;
856 	vpe_hdr = (const struct vpe_firmware_header_v1_0 *)ucode->fw->data;
857 	umsch_mm_hdr = (const struct umsch_mm_firmware_header_v1_0 *)ucode->fw->data;
858 
859 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
860 		switch (ucode->ucode_id) {
861 		case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
862 			ucode->ucode_size = le32_to_cpu(sdma_hdr->ctx_ucode_size_bytes);
863 			ucode_addr = (u8 *)ucode->fw->data +
864 				le32_to_cpu(sdma_hdr->header.ucode_array_offset_bytes);
865 			break;
866 		case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
867 			ucode->ucode_size = le32_to_cpu(sdma_hdr->ctl_ucode_size_bytes);
868 			ucode_addr = (u8 *)ucode->fw->data +
869 				le32_to_cpu(sdma_hdr->ctl_ucode_offset);
870 			break;
871 		case AMDGPU_UCODE_ID_SDMA_RS64:
872 			ucode->ucode_size = le32_to_cpu(sdmav3_hdr->ucode_size_bytes);
873 			ucode_addr = (u8 *)ucode->fw->data +
874 				le32_to_cpu(sdmav3_hdr->header.ucode_array_offset_bytes);
875 			break;
876 		case AMDGPU_UCODE_ID_CP_MEC1:
877 		case AMDGPU_UCODE_ID_CP_MEC2:
878 			ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes) -
879 				le32_to_cpu(cp_hdr->jt_size) * 4;
880 			ucode_addr = (u8 *)ucode->fw->data +
881 				le32_to_cpu(header->ucode_array_offset_bytes);
882 			break;
883 		case AMDGPU_UCODE_ID_CP_MEC1_JT:
884 		case AMDGPU_UCODE_ID_CP_MEC2_JT:
885 			ucode->ucode_size = le32_to_cpu(cp_hdr->jt_size) * 4;
886 			ucode_addr = (u8 *)ucode->fw->data +
887 				le32_to_cpu(header->ucode_array_offset_bytes) +
888 				le32_to_cpu(cp_hdr->jt_offset) * 4;
889 			break;
890 		case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
891 			ucode->ucode_size = adev->gfx.rlc.save_restore_list_cntl_size_bytes;
892 			ucode_addr = adev->gfx.rlc.save_restore_list_cntl;
893 			break;
894 		case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
895 			ucode->ucode_size = adev->gfx.rlc.save_restore_list_gpm_size_bytes;
896 			ucode_addr = adev->gfx.rlc.save_restore_list_gpm;
897 			break;
898 		case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
899 			ucode->ucode_size = adev->gfx.rlc.save_restore_list_srm_size_bytes;
900 			ucode_addr = adev->gfx.rlc.save_restore_list_srm;
901 			break;
902 		case AMDGPU_UCODE_ID_RLC_IRAM:
903 			ucode->ucode_size = adev->gfx.rlc.rlc_iram_ucode_size_bytes;
904 			ucode_addr = adev->gfx.rlc.rlc_iram_ucode;
905 			break;
906 		case AMDGPU_UCODE_ID_RLC_DRAM:
907 			ucode->ucode_size = adev->gfx.rlc.rlc_dram_ucode_size_bytes;
908 			ucode_addr = adev->gfx.rlc.rlc_dram_ucode;
909 			break;
910 		case AMDGPU_UCODE_ID_RLC_P:
911 			ucode->ucode_size = adev->gfx.rlc.rlcp_ucode_size_bytes;
912 			ucode_addr = adev->gfx.rlc.rlcp_ucode;
913 			break;
914 		case AMDGPU_UCODE_ID_RLC_V:
915 			ucode->ucode_size = adev->gfx.rlc.rlcv_ucode_size_bytes;
916 			ucode_addr = adev->gfx.rlc.rlcv_ucode;
917 			break;
918 		case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
919 			ucode->ucode_size = adev->gfx.rlc.global_tap_delays_ucode_size_bytes;
920 			ucode_addr = adev->gfx.rlc.global_tap_delays_ucode;
921 			break;
922 		case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
923 			ucode->ucode_size = adev->gfx.rlc.se0_tap_delays_ucode_size_bytes;
924 			ucode_addr = adev->gfx.rlc.se0_tap_delays_ucode;
925 			break;
926 		case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
927 			ucode->ucode_size = adev->gfx.rlc.se1_tap_delays_ucode_size_bytes;
928 			ucode_addr = adev->gfx.rlc.se1_tap_delays_ucode;
929 			break;
930 		case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
931 			ucode->ucode_size = adev->gfx.rlc.se2_tap_delays_ucode_size_bytes;
932 			ucode_addr = adev->gfx.rlc.se2_tap_delays_ucode;
933 			break;
934 		case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
935 			ucode->ucode_size = adev->gfx.rlc.se3_tap_delays_ucode_size_bytes;
936 			ucode_addr = adev->gfx.rlc.se3_tap_delays_ucode;
937 			break;
938 		case AMDGPU_UCODE_ID_CP_MES:
939 			ucode->ucode_size = le32_to_cpu(mes_hdr->mes_ucode_size_bytes);
940 			ucode_addr = (u8 *)ucode->fw->data +
941 				le32_to_cpu(mes_hdr->mes_ucode_offset_bytes);
942 			break;
943 		case AMDGPU_UCODE_ID_CP_MES_DATA:
944 			ucode->ucode_size = le32_to_cpu(mes_hdr->mes_ucode_data_size_bytes);
945 			ucode_addr = (u8 *)ucode->fw->data +
946 				le32_to_cpu(mes_hdr->mes_ucode_data_offset_bytes);
947 			break;
948 		case AMDGPU_UCODE_ID_CP_MES1:
949 			ucode->ucode_size = le32_to_cpu(mes_hdr->mes_ucode_size_bytes);
950 			ucode_addr = (u8 *)ucode->fw->data +
951 				le32_to_cpu(mes_hdr->mes_ucode_offset_bytes);
952 			break;
953 		case AMDGPU_UCODE_ID_CP_MES1_DATA:
954 			ucode->ucode_size = le32_to_cpu(mes_hdr->mes_ucode_data_size_bytes);
955 			ucode_addr = (u8 *)ucode->fw->data +
956 				le32_to_cpu(mes_hdr->mes_ucode_data_offset_bytes);
957 			break;
958 		case AMDGPU_UCODE_ID_DMCU_ERAM:
959 			ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes) -
960 				le32_to_cpu(dmcu_hdr->intv_size_bytes);
961 			ucode_addr = (u8 *)ucode->fw->data +
962 				le32_to_cpu(header->ucode_array_offset_bytes);
963 			break;
964 		case AMDGPU_UCODE_ID_DMCU_INTV:
965 			ucode->ucode_size = le32_to_cpu(dmcu_hdr->intv_size_bytes);
966 			ucode_addr = (u8 *)ucode->fw->data +
967 				le32_to_cpu(header->ucode_array_offset_bytes) +
968 				le32_to_cpu(dmcu_hdr->intv_offset_bytes);
969 			break;
970 		case AMDGPU_UCODE_ID_DMCUB:
971 			ucode->ucode_size = le32_to_cpu(dmcub_hdr->inst_const_bytes);
972 			ucode_addr = (u8 *)ucode->fw->data +
973 				le32_to_cpu(header->ucode_array_offset_bytes);
974 			break;
975 		case AMDGPU_UCODE_ID_PPTABLE:
976 			ucode->ucode_size = ucode->fw->size;
977 			ucode_addr = (u8 *)ucode->fw->data;
978 			break;
979 		case AMDGPU_UCODE_ID_P2S_TABLE:
980 			ucode->ucode_size = ucode->fw->size;
981 			ucode_addr = (u8 *)ucode->fw->data;
982 			break;
983 		case AMDGPU_UCODE_ID_IMU_I:
984 			ucode->ucode_size = le32_to_cpu(imu_hdr->imu_iram_ucode_size_bytes);
985 			ucode_addr = (u8 *)ucode->fw->data +
986 				le32_to_cpu(imu_hdr->header.ucode_array_offset_bytes);
987 			break;
988 		case AMDGPU_UCODE_ID_IMU_D:
989 			ucode->ucode_size = le32_to_cpu(imu_hdr->imu_dram_ucode_size_bytes);
990 			ucode_addr = (u8 *)ucode->fw->data +
991 				le32_to_cpu(imu_hdr->header.ucode_array_offset_bytes) +
992 				le32_to_cpu(imu_hdr->imu_iram_ucode_size_bytes);
993 			break;
994 		case AMDGPU_UCODE_ID_CP_RS64_PFP:
995 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->ucode_size_bytes);
996 			ucode_addr = (u8 *)ucode->fw->data +
997 				le32_to_cpu(header->ucode_array_offset_bytes);
998 			break;
999 		case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
1000 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
1001 			ucode_addr = (u8 *)ucode->fw->data +
1002 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
1003 			break;
1004 		case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
1005 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
1006 			ucode_addr = (u8 *)ucode->fw->data +
1007 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
1008 			break;
1009 		case AMDGPU_UCODE_ID_CP_RS64_ME:
1010 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->ucode_size_bytes);
1011 			ucode_addr = (u8 *)ucode->fw->data +
1012 				le32_to_cpu(header->ucode_array_offset_bytes);
1013 			break;
1014 		case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
1015 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
1016 			ucode_addr = (u8 *)ucode->fw->data +
1017 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
1018 			break;
1019 		case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
1020 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
1021 			ucode_addr = (u8 *)ucode->fw->data +
1022 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
1023 			break;
1024 		case AMDGPU_UCODE_ID_CP_RS64_MEC:
1025 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->ucode_size_bytes);
1026 			ucode_addr = (u8 *)ucode->fw->data +
1027 				le32_to_cpu(header->ucode_array_offset_bytes);
1028 			break;
1029 		case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
1030 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
1031 			ucode_addr = (u8 *)ucode->fw->data +
1032 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
1033 			break;
1034 		case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
1035 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
1036 			ucode_addr = (u8 *)ucode->fw->data +
1037 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
1038 			break;
1039 		case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
1040 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
1041 			ucode_addr = (u8 *)ucode->fw->data +
1042 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
1043 			break;
1044 		case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
1045 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
1046 			ucode_addr = (u8 *)ucode->fw->data +
1047 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
1048 			break;
1049 		case AMDGPU_UCODE_ID_VPE_CTX:
1050 			ucode->ucode_size = le32_to_cpu(vpe_hdr->ctx_ucode_size_bytes);
1051 			ucode_addr = (u8 *)ucode->fw->data +
1052 				le32_to_cpu(vpe_hdr->header.ucode_array_offset_bytes);
1053 			break;
1054 		case AMDGPU_UCODE_ID_VPE_CTL:
1055 			ucode->ucode_size = le32_to_cpu(vpe_hdr->ctl_ucode_size_bytes);
1056 			ucode_addr = (u8 *)ucode->fw->data +
1057 				le32_to_cpu(vpe_hdr->ctl_ucode_offset);
1058 			break;
1059 		case AMDGPU_UCODE_ID_UMSCH_MM_UCODE:
1060 			ucode->ucode_size = le32_to_cpu(umsch_mm_hdr->umsch_mm_ucode_size_bytes);
1061 			ucode_addr = (u8 *)ucode->fw->data +
1062 				le32_to_cpu(umsch_mm_hdr->header.ucode_array_offset_bytes);
1063 			break;
1064 		case AMDGPU_UCODE_ID_UMSCH_MM_DATA:
1065 			ucode->ucode_size = le32_to_cpu(umsch_mm_hdr->umsch_mm_ucode_data_size_bytes);
1066 			ucode_addr = (u8 *)ucode->fw->data +
1067 				le32_to_cpu(umsch_mm_hdr->umsch_mm_ucode_data_offset_bytes);
1068 			break;
1069 		default:
1070 			ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes);
1071 			ucode_addr = (u8 *)ucode->fw->data +
1072 				le32_to_cpu(header->ucode_array_offset_bytes);
1073 			break;
1074 		}
1075 	} else {
1076 		ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes);
1077 		ucode_addr = (u8 *)ucode->fw->data +
1078 			le32_to_cpu(header->ucode_array_offset_bytes);
1079 	}
1080 
1081 	memcpy(ucode->kaddr, ucode_addr, ucode->ucode_size);
1082 
1083 	return 0;
1084 }
1085 
amdgpu_ucode_patch_jt(struct amdgpu_firmware_info * ucode,uint64_t mc_addr,void * kptr)1086 static int amdgpu_ucode_patch_jt(struct amdgpu_firmware_info *ucode,
1087 				uint64_t mc_addr, void *kptr)
1088 {
1089 	const struct gfx_firmware_header_v1_0 *header = NULL;
1090 	const struct common_firmware_header *comm_hdr = NULL;
1091 	uint8_t *src_addr = NULL;
1092 	uint8_t *dst_addr = NULL;
1093 
1094 	if (!ucode->fw)
1095 		return 0;
1096 
1097 	comm_hdr = (const struct common_firmware_header *)ucode->fw->data;
1098 	header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
1099 	dst_addr = ucode->kaddr +
1100 			   ALIGN(le32_to_cpu(comm_hdr->ucode_size_bytes),
1101 			   PAGE_SIZE);
1102 	src_addr = (uint8_t *)ucode->fw->data +
1103 			   le32_to_cpu(comm_hdr->ucode_array_offset_bytes) +
1104 			   (le32_to_cpu(header->jt_offset) * 4);
1105 	memcpy(dst_addr, src_addr, le32_to_cpu(header->jt_size) * 4);
1106 
1107 	return 0;
1108 }
1109 
amdgpu_ucode_create_bo(struct amdgpu_device * adev)1110 int amdgpu_ucode_create_bo(struct amdgpu_device *adev)
1111 {
1112 	if ((adev->firmware.load_type != AMDGPU_FW_LOAD_DIRECT) &&
1113 	    (adev->firmware.load_type != AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO)) {
1114 		amdgpu_bo_create_kernel(adev, adev->firmware.fw_size, PAGE_SIZE,
1115 			(amdgpu_sriov_vf(adev) || adev->debug_use_vram_fw_buf) ?
1116 			AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
1117 			&adev->firmware.fw_buf,
1118 			&adev->firmware.fw_buf_mc,
1119 			&adev->firmware.fw_buf_ptr);
1120 		if (!adev->firmware.fw_buf) {
1121 			dev_err(adev->dev, "failed to create kernel buffer for firmware.fw_buf\n");
1122 			return -ENOMEM;
1123 		} else if (amdgpu_sriov_vf(adev)) {
1124 			memset(adev->firmware.fw_buf_ptr, 0, adev->firmware.fw_size);
1125 		}
1126 	}
1127 	return 0;
1128 }
1129 
amdgpu_ucode_free_bo(struct amdgpu_device * adev)1130 void amdgpu_ucode_free_bo(struct amdgpu_device *adev)
1131 {
1132 	amdgpu_bo_free_kernel(&adev->firmware.fw_buf,
1133 		&adev->firmware.fw_buf_mc,
1134 		&adev->firmware.fw_buf_ptr);
1135 }
1136 
amdgpu_ucode_init_bo(struct amdgpu_device * adev)1137 int amdgpu_ucode_init_bo(struct amdgpu_device *adev)
1138 {
1139 	uint64_t fw_offset = 0;
1140 	int i;
1141 	struct amdgpu_firmware_info *ucode = NULL;
1142 
1143  /* for baremetal, the ucode is allocated in gtt, so don't need to fill the bo when reset/suspend */
1144 	if (!amdgpu_sriov_vf(adev) && (amdgpu_in_reset(adev) || adev->in_suspend))
1145 		return 0;
1146 	/*
1147 	 * if SMU loaded firmware, it needn't add SMC, UVD, and VCE
1148 	 * ucode info here
1149 	 */
1150 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1151 		if (amdgpu_sriov_vf(adev))
1152 			adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM - 3;
1153 		else
1154 			adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM - 4;
1155 	} else {
1156 		adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM;
1157 	}
1158 
1159 	for (i = 0; i < adev->firmware.max_ucodes; i++) {
1160 		ucode = &adev->firmware.ucode[i];
1161 		if (ucode->fw) {
1162 			amdgpu_ucode_init_single_fw(adev, ucode, adev->firmware.fw_buf_mc + fw_offset,
1163 						    adev->firmware.fw_buf_ptr + fw_offset);
1164 			if (i == AMDGPU_UCODE_ID_CP_MEC1 &&
1165 			    adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1166 				const struct gfx_firmware_header_v1_0 *cp_hdr;
1167 
1168 				cp_hdr = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
1169 				amdgpu_ucode_patch_jt(ucode,  adev->firmware.fw_buf_mc + fw_offset,
1170 						    adev->firmware.fw_buf_ptr + fw_offset);
1171 				fw_offset += ALIGN(le32_to_cpu(cp_hdr->jt_size) << 2, PAGE_SIZE);
1172 			}
1173 			fw_offset += ALIGN(ucode->ucode_size, PAGE_SIZE);
1174 		}
1175 	}
1176 	return 0;
1177 }
1178 
amdgpu_ucode_legacy_naming(struct amdgpu_device * adev,int block_type)1179 static const char *amdgpu_ucode_legacy_naming(struct amdgpu_device *adev, int block_type)
1180 {
1181 	if (block_type == MP0_HWIP) {
1182 		switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
1183 		case IP_VERSION(9, 0, 0):
1184 			switch (adev->asic_type) {
1185 			case CHIP_VEGA10:
1186 				return "vega10";
1187 			case CHIP_VEGA12:
1188 				return "vega12";
1189 			default:
1190 				return NULL;
1191 			}
1192 		case IP_VERSION(10, 0, 0):
1193 		case IP_VERSION(10, 0, 1):
1194 			if (adev->asic_type == CHIP_RAVEN) {
1195 				if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1196 					return "raven2";
1197 				else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1198 					return "picasso";
1199 				return "raven";
1200 			}
1201 			break;
1202 		case IP_VERSION(11, 0, 0):
1203 			return "navi10";
1204 		case IP_VERSION(11, 0, 2):
1205 			return "vega20";
1206 		case IP_VERSION(11, 0, 3):
1207 			return "renoir";
1208 		case IP_VERSION(11, 0, 4):
1209 			return "arcturus";
1210 		case IP_VERSION(11, 0, 5):
1211 			return "navi14";
1212 		case IP_VERSION(11, 0, 7):
1213 			return "sienna_cichlid";
1214 		case IP_VERSION(11, 0, 9):
1215 			return "navi12";
1216 		case IP_VERSION(11, 0, 11):
1217 			return "navy_flounder";
1218 		case IP_VERSION(11, 0, 12):
1219 			return "dimgrey_cavefish";
1220 		case IP_VERSION(11, 0, 13):
1221 			return "beige_goby";
1222 		case IP_VERSION(11, 5, 0):
1223 			return "vangogh";
1224 		case IP_VERSION(12, 0, 1):
1225 			return "green_sardine";
1226 		case IP_VERSION(13, 0, 2):
1227 			return "aldebaran";
1228 		case IP_VERSION(13, 0, 1):
1229 		case IP_VERSION(13, 0, 3):
1230 			return "yellow_carp";
1231 		}
1232 	} else if (block_type == MP1_HWIP) {
1233 		switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1234 		case IP_VERSION(9, 0, 0):
1235 		case IP_VERSION(10, 0, 0):
1236 		case IP_VERSION(10, 0, 1):
1237 		case IP_VERSION(11, 0, 2):
1238 			if (adev->asic_type == CHIP_ARCTURUS)
1239 				return "arcturus_smc";
1240 			return NULL;
1241 		case IP_VERSION(11, 0, 0):
1242 			return "navi10_smc";
1243 		case IP_VERSION(11, 0, 5):
1244 			return "navi14_smc";
1245 		case IP_VERSION(11, 0, 9):
1246 			return "navi12_smc";
1247 		case IP_VERSION(11, 0, 7):
1248 			return "sienna_cichlid_smc";
1249 		case IP_VERSION(11, 0, 11):
1250 			return "navy_flounder_smc";
1251 		case IP_VERSION(11, 0, 12):
1252 			return "dimgrey_cavefish_smc";
1253 		case IP_VERSION(11, 0, 13):
1254 			return "beige_goby_smc";
1255 		case IP_VERSION(13, 0, 2):
1256 			return "aldebaran_smc";
1257 		}
1258 	} else if (block_type == SDMA0_HWIP) {
1259 		switch (amdgpu_ip_version(adev, SDMA0_HWIP, 0)) {
1260 		case IP_VERSION(4, 0, 0):
1261 			return "vega10_sdma";
1262 		case IP_VERSION(4, 0, 1):
1263 			return "vega12_sdma";
1264 		case IP_VERSION(4, 1, 0):
1265 		case IP_VERSION(4, 1, 1):
1266 			if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1267 				return "raven2_sdma";
1268 			else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1269 				return "picasso_sdma";
1270 			return "raven_sdma";
1271 		case IP_VERSION(4, 1, 2):
1272 			if (adev->apu_flags & AMD_APU_IS_RENOIR)
1273 				return "renoir_sdma";
1274 			return "green_sardine_sdma";
1275 		case IP_VERSION(4, 2, 0):
1276 			return "vega20_sdma";
1277 		case IP_VERSION(4, 2, 2):
1278 			return "arcturus_sdma";
1279 		case IP_VERSION(4, 4, 0):
1280 			return "aldebaran_sdma";
1281 		case IP_VERSION(5, 0, 0):
1282 			return "navi10_sdma";
1283 		case IP_VERSION(5, 0, 1):
1284 			return "cyan_skillfish2_sdma";
1285 		case IP_VERSION(5, 0, 2):
1286 			return "navi14_sdma";
1287 		case IP_VERSION(5, 0, 5):
1288 			return "navi12_sdma";
1289 		case IP_VERSION(5, 2, 0):
1290 			return "sienna_cichlid_sdma";
1291 		case IP_VERSION(5, 2, 2):
1292 			return "navy_flounder_sdma";
1293 		case IP_VERSION(5, 2, 4):
1294 			return "dimgrey_cavefish_sdma";
1295 		case IP_VERSION(5, 2, 5):
1296 			return "beige_goby_sdma";
1297 		case IP_VERSION(5, 2, 3):
1298 			return "yellow_carp_sdma";
1299 		case IP_VERSION(5, 2, 1):
1300 			return "vangogh_sdma";
1301 		}
1302 	} else if (block_type == UVD_HWIP) {
1303 		switch (amdgpu_ip_version(adev, UVD_HWIP, 0)) {
1304 		case IP_VERSION(1, 0, 0):
1305 		case IP_VERSION(1, 0, 1):
1306 			if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1307 				return "raven2_vcn";
1308 			else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1309 				return "picasso_vcn";
1310 			return "raven_vcn";
1311 		case IP_VERSION(2, 5, 0):
1312 			return "arcturus_vcn";
1313 		case IP_VERSION(2, 2, 0):
1314 			if (adev->apu_flags & AMD_APU_IS_RENOIR)
1315 				return "renoir_vcn";
1316 			return "green_sardine_vcn";
1317 		case IP_VERSION(2, 6, 0):
1318 			return "aldebaran_vcn";
1319 		case IP_VERSION(2, 0, 0):
1320 			return "navi10_vcn";
1321 		case IP_VERSION(2, 0, 2):
1322 			if (adev->asic_type == CHIP_NAVI12)
1323 				return "navi12_vcn";
1324 			return "navi14_vcn";
1325 		case IP_VERSION(3, 0, 0):
1326 		case IP_VERSION(3, 0, 64):
1327 		case IP_VERSION(3, 0, 192):
1328 			if (amdgpu_ip_version(adev, GC_HWIP, 0) ==
1329 			    IP_VERSION(10, 3, 0))
1330 				return "sienna_cichlid_vcn";
1331 			return "navy_flounder_vcn";
1332 		case IP_VERSION(3, 0, 2):
1333 			return "vangogh_vcn";
1334 		case IP_VERSION(3, 0, 16):
1335 			return "dimgrey_cavefish_vcn";
1336 		case IP_VERSION(3, 0, 33):
1337 			return "beige_goby_vcn";
1338 		case IP_VERSION(3, 1, 1):
1339 			return "yellow_carp_vcn";
1340 		}
1341 	} else if (block_type == GC_HWIP) {
1342 		switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
1343 		case IP_VERSION(9, 0, 1):
1344 			return "vega10";
1345 		case IP_VERSION(9, 2, 1):
1346 			return "vega12";
1347 		case IP_VERSION(9, 4, 0):
1348 			return "vega20";
1349 		case IP_VERSION(9, 2, 2):
1350 		case IP_VERSION(9, 1, 0):
1351 			if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1352 				return "raven2";
1353 			else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1354 				return "picasso";
1355 			return "raven";
1356 		case IP_VERSION(9, 4, 1):
1357 			return "arcturus";
1358 		case IP_VERSION(9, 3, 0):
1359 			if (adev->apu_flags & AMD_APU_IS_RENOIR)
1360 				return "renoir";
1361 			return "green_sardine";
1362 		case IP_VERSION(9, 4, 2):
1363 			return "aldebaran";
1364 		case IP_VERSION(10, 1, 10):
1365 			return "navi10";
1366 		case IP_VERSION(10, 1, 1):
1367 			return "navi14";
1368 		case IP_VERSION(10, 1, 2):
1369 			return "navi12";
1370 		case IP_VERSION(10, 3, 0):
1371 			return "sienna_cichlid";
1372 		case IP_VERSION(10, 3, 2):
1373 			return "navy_flounder";
1374 		case IP_VERSION(10, 3, 1):
1375 			return "vangogh";
1376 		case IP_VERSION(10, 3, 4):
1377 			return "dimgrey_cavefish";
1378 		case IP_VERSION(10, 3, 5):
1379 			return "beige_goby";
1380 		case IP_VERSION(10, 3, 3):
1381 			return "yellow_carp";
1382 		case IP_VERSION(10, 1, 3):
1383 		case IP_VERSION(10, 1, 4):
1384 			return "cyan_skillfish2";
1385 		}
1386 	}
1387 	return NULL;
1388 }
1389 
amdgpu_is_kicker_fw(struct amdgpu_device * adev)1390 bool amdgpu_is_kicker_fw(struct amdgpu_device *adev)
1391 {
1392 	int i;
1393 
1394 	for (i = 0; i < ARRAY_SIZE(kicker_device_list); i++) {
1395 		if (adev->pdev->device == kicker_device_list[i].device &&
1396 			adev->pdev->revision == kicker_device_list[i].revision)
1397 		return true;
1398 	}
1399 
1400 	return false;
1401 }
1402 
amdgpu_ucode_ip_version_decode(struct amdgpu_device * adev,int block_type,char * ucode_prefix,int len)1403 void amdgpu_ucode_ip_version_decode(struct amdgpu_device *adev, int block_type, char *ucode_prefix, int len)
1404 {
1405 	int maj, min, rev;
1406 	char *ip_name;
1407 	const char *legacy;
1408 	uint32_t version = amdgpu_ip_version(adev, block_type, 0);
1409 
1410 	legacy = amdgpu_ucode_legacy_naming(adev, block_type);
1411 	if (legacy) {
1412 		snprintf(ucode_prefix, len, "%s", legacy);
1413 		return;
1414 	}
1415 
1416 	switch (block_type) {
1417 	case GC_HWIP:
1418 		ip_name = "gc";
1419 		break;
1420 	case SDMA0_HWIP:
1421 		ip_name = "sdma";
1422 		break;
1423 	case MP0_HWIP:
1424 		ip_name = "psp";
1425 		break;
1426 	case MP1_HWIP:
1427 		ip_name = "smu";
1428 		break;
1429 	case UVD_HWIP:
1430 		ip_name = "vcn";
1431 		break;
1432 	case VPE_HWIP:
1433 		ip_name = "vpe";
1434 		break;
1435 	case ISP_HWIP:
1436 		ip_name = "isp";
1437 		break;
1438 	default:
1439 		BUG();
1440 	}
1441 
1442 	maj = IP_VERSION_MAJ(version);
1443 	min = IP_VERSION_MIN(version);
1444 	rev = IP_VERSION_REV(version);
1445 
1446 	snprintf(ucode_prefix, len, "%s_%d_%d_%d", ip_name, maj, min, rev);
1447 }
1448 
1449 /*
1450  * amdgpu_ucode_request - Fetch and validate amdgpu microcode
1451  *
1452  * @adev: amdgpu device
1453  * @fw: pointer to load firmware to
1454  * @fmt: firmware name format string
1455  * @...: variable arguments
1456  *
1457  * This is a helper that will use request_firmware and amdgpu_ucode_validate
1458  * to load and run basic validation on firmware. If the load fails, remap
1459  * the error code to -ENODEV, so that early_init functions will fail to load.
1460  */
amdgpu_ucode_request(struct amdgpu_device * adev,const struct firmware ** fw,const char * fmt,...)1461 int amdgpu_ucode_request(struct amdgpu_device *adev, const struct firmware **fw,
1462 			 const char *fmt, ...)
1463 {
1464 	char fname[AMDGPU_UCODE_NAME_MAX];
1465 	va_list ap;
1466 	int r;
1467 
1468 	va_start(ap, fmt);
1469 	r = vsnprintf(fname, sizeof(fname), fmt, ap);
1470 	va_end(ap);
1471 	if (r == sizeof(fname)) {
1472 		dev_warn(adev->dev, "amdgpu firmware name buffer overflow\n");
1473 		return -EOVERFLOW;
1474 	}
1475 
1476 	r = request_firmware(fw, fname, adev->dev);
1477 	if (r)
1478 		return -ENODEV;
1479 
1480 	r = amdgpu_ucode_validate(*fw);
1481 	if (r) {
1482 		dev_dbg(adev->dev, "\"%s\" failed to validate\n", fname);
1483 		release_firmware(*fw);
1484 		*fw = NULL;
1485 	}
1486 
1487 	return r;
1488 }
1489 
1490 /*
1491  * amdgpu_ucode_release - Release firmware microcode
1492  *
1493  * @fw: pointer to firmware to release
1494  */
amdgpu_ucode_release(const struct firmware ** fw)1495 void amdgpu_ucode_release(const struct firmware **fw)
1496 {
1497 	release_firmware(*fw);
1498 	*fw = NULL;
1499 }
1500