xref: /openbmc/linux/drivers/gpu/drm/amd/amdgpu/amdgpu_ucode.c (revision 87832e937c808a7ebc41254b408362e3255c87c9)
1d38ceaf9SAlex Deucher /*
2d38ceaf9SAlex Deucher  * Copyright 2014 Advanced Micro Devices, Inc.
3d38ceaf9SAlex Deucher  *
4d38ceaf9SAlex Deucher  * Permission is hereby granted, free of charge, to any person obtaining a
5d38ceaf9SAlex Deucher  * copy of this software and associated documentation files (the "Software"),
6d38ceaf9SAlex Deucher  * to deal in the Software without restriction, including without limitation
7d38ceaf9SAlex Deucher  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8d38ceaf9SAlex Deucher  * and/or sell copies of the Software, and to permit persons to whom the
9d38ceaf9SAlex Deucher  * Software is furnished to do so, subject to the following conditions:
10d38ceaf9SAlex Deucher  *
11d38ceaf9SAlex Deucher  * The above copyright notice and this permission notice shall be included in
12d38ceaf9SAlex Deucher  * all copies or substantial portions of the Software.
13d38ceaf9SAlex Deucher  *
14d38ceaf9SAlex Deucher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15d38ceaf9SAlex Deucher  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16d38ceaf9SAlex Deucher  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17d38ceaf9SAlex Deucher  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18d38ceaf9SAlex Deucher  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19d38ceaf9SAlex Deucher  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20d38ceaf9SAlex Deucher  * OTHER DEALINGS IN THE SOFTWARE.
21d38ceaf9SAlex Deucher  *
22d38ceaf9SAlex Deucher  */
23d38ceaf9SAlex Deucher 
24d38ceaf9SAlex Deucher #include <linux/firmware.h>
25d38ceaf9SAlex Deucher #include <linux/slab.h>
26d38ceaf9SAlex Deucher #include <linux/module.h>
27fdf2f6c5SSam Ravnborg 
28d38ceaf9SAlex Deucher #include "amdgpu.h"
29d38ceaf9SAlex Deucher #include "amdgpu_ucode.h"
30d38ceaf9SAlex Deucher 
amdgpu_ucode_print_common_hdr(const struct common_firmware_header * hdr)31d38ceaf9SAlex Deucher static void amdgpu_ucode_print_common_hdr(const struct common_firmware_header *hdr)
32d38ceaf9SAlex Deucher {
33d38ceaf9SAlex Deucher 	DRM_DEBUG("size_bytes: %u\n", le32_to_cpu(hdr->size_bytes));
34d38ceaf9SAlex Deucher 	DRM_DEBUG("header_size_bytes: %u\n", le32_to_cpu(hdr->header_size_bytes));
35d38ceaf9SAlex Deucher 	DRM_DEBUG("header_version_major: %u\n", le16_to_cpu(hdr->header_version_major));
36d38ceaf9SAlex Deucher 	DRM_DEBUG("header_version_minor: %u\n", le16_to_cpu(hdr->header_version_minor));
37d38ceaf9SAlex Deucher 	DRM_DEBUG("ip_version_major: %u\n", le16_to_cpu(hdr->ip_version_major));
38d38ceaf9SAlex Deucher 	DRM_DEBUG("ip_version_minor: %u\n", le16_to_cpu(hdr->ip_version_minor));
39d38ceaf9SAlex Deucher 	DRM_DEBUG("ucode_version: 0x%08x\n", le32_to_cpu(hdr->ucode_version));
40d38ceaf9SAlex Deucher 	DRM_DEBUG("ucode_size_bytes: %u\n", le32_to_cpu(hdr->ucode_size_bytes));
41d38ceaf9SAlex Deucher 	DRM_DEBUG("ucode_array_offset_bytes: %u\n",
42d38ceaf9SAlex Deucher 		  le32_to_cpu(hdr->ucode_array_offset_bytes));
43d38ceaf9SAlex Deucher 	DRM_DEBUG("crc32: 0x%08x\n", le32_to_cpu(hdr->crc32));
44d38ceaf9SAlex Deucher }
45d38ceaf9SAlex Deucher 
amdgpu_ucode_print_mc_hdr(const struct common_firmware_header * hdr)46d38ceaf9SAlex Deucher void amdgpu_ucode_print_mc_hdr(const struct common_firmware_header *hdr)
47d38ceaf9SAlex Deucher {
48d38ceaf9SAlex Deucher 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
49d38ceaf9SAlex Deucher 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
50d38ceaf9SAlex Deucher 
51d38ceaf9SAlex Deucher 	DRM_DEBUG("MC\n");
52d38ceaf9SAlex Deucher 	amdgpu_ucode_print_common_hdr(hdr);
53d38ceaf9SAlex Deucher 
54d38ceaf9SAlex Deucher 	if (version_major == 1) {
55d38ceaf9SAlex Deucher 		const struct mc_firmware_header_v1_0 *mc_hdr =
56d38ceaf9SAlex Deucher 			container_of(hdr, struct mc_firmware_header_v1_0, header);
57d38ceaf9SAlex Deucher 
58d38ceaf9SAlex Deucher 		DRM_DEBUG("io_debug_size_bytes: %u\n",
59d38ceaf9SAlex Deucher 			  le32_to_cpu(mc_hdr->io_debug_size_bytes));
60d38ceaf9SAlex Deucher 		DRM_DEBUG("io_debug_array_offset_bytes: %u\n",
61d38ceaf9SAlex Deucher 			  le32_to_cpu(mc_hdr->io_debug_array_offset_bytes));
62d38ceaf9SAlex Deucher 	} else {
63d38ceaf9SAlex Deucher 		DRM_ERROR("Unknown MC ucode version: %u.%u\n", version_major, version_minor);
64d38ceaf9SAlex Deucher 	}
65d38ceaf9SAlex Deucher }
66d38ceaf9SAlex Deucher 
amdgpu_ucode_print_smc_hdr(const struct common_firmware_header * hdr)67d38ceaf9SAlex Deucher void amdgpu_ucode_print_smc_hdr(const struct common_firmware_header *hdr)
68d38ceaf9SAlex Deucher {
69d38ceaf9SAlex Deucher 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
70d38ceaf9SAlex Deucher 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
71a6d64c1aSKevin Wang 	const struct smc_firmware_header_v1_0 *v1_0_hdr;
72a6d64c1aSKevin Wang 	const struct smc_firmware_header_v2_0 *v2_0_hdr;
73a6d64c1aSKevin Wang 	const struct smc_firmware_header_v2_1 *v2_1_hdr;
74d38ceaf9SAlex Deucher 
75d38ceaf9SAlex Deucher 	DRM_DEBUG("SMC\n");
76d38ceaf9SAlex Deucher 	amdgpu_ucode_print_common_hdr(hdr);
77d38ceaf9SAlex Deucher 
78d38ceaf9SAlex Deucher 	if (version_major == 1) {
79a6d64c1aSKevin Wang 		v1_0_hdr = container_of(hdr, struct smc_firmware_header_v1_0, header);
80a6d64c1aSKevin Wang 		DRM_DEBUG("ucode_start_addr: %u\n", le32_to_cpu(v1_0_hdr->ucode_start_addr));
81336a1c82SHuang Rui 	} else if (version_major == 2) {
82a6d64c1aSKevin Wang 		switch (version_minor) {
83a6d64c1aSKevin Wang 		case 0:
84a6d64c1aSKevin Wang 			v2_0_hdr = container_of(hdr, struct smc_firmware_header_v2_0, v1_0.header);
85a6d64c1aSKevin Wang 			DRM_DEBUG("ppt_offset_bytes: %u\n", le32_to_cpu(v2_0_hdr->ppt_offset_bytes));
86a6d64c1aSKevin Wang 			DRM_DEBUG("ppt_size_bytes: %u\n", le32_to_cpu(v2_0_hdr->ppt_size_bytes));
87a6d64c1aSKevin Wang 			break;
88a6d64c1aSKevin Wang 		case 1:
89a6d64c1aSKevin Wang 			v2_1_hdr = container_of(hdr, struct smc_firmware_header_v2_1, v1_0.header);
90a6d64c1aSKevin Wang 			DRM_DEBUG("pptable_count: %u\n", le32_to_cpu(v2_1_hdr->pptable_count));
91a6d64c1aSKevin Wang 			DRM_DEBUG("pptable_entry_offset: %u\n", le32_to_cpu(v2_1_hdr->pptable_entry_offset));
92a6d64c1aSKevin Wang 			break;
93a6d64c1aSKevin Wang 		default:
94a6d64c1aSKevin Wang 			break;
95a6d64c1aSKevin Wang 		}
96336a1c82SHuang Rui 
97d38ceaf9SAlex Deucher 	} else {
98d38ceaf9SAlex Deucher 		DRM_ERROR("Unknown SMC ucode version: %u.%u\n", version_major, version_minor);
99d38ceaf9SAlex Deucher 	}
100d38ceaf9SAlex Deucher }
101d38ceaf9SAlex Deucher 
amdgpu_ucode_print_gfx_hdr(const struct common_firmware_header * hdr)102d38ceaf9SAlex Deucher void amdgpu_ucode_print_gfx_hdr(const struct common_firmware_header *hdr)
103d38ceaf9SAlex Deucher {
104d38ceaf9SAlex Deucher 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
105d38ceaf9SAlex Deucher 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
106d38ceaf9SAlex Deucher 
107d38ceaf9SAlex Deucher 	DRM_DEBUG("GFX\n");
108d38ceaf9SAlex Deucher 	amdgpu_ucode_print_common_hdr(hdr);
109d38ceaf9SAlex Deucher 
110d38ceaf9SAlex Deucher 	if (version_major == 1) {
111d38ceaf9SAlex Deucher 		const struct gfx_firmware_header_v1_0 *gfx_hdr =
112d38ceaf9SAlex Deucher 			container_of(hdr, struct gfx_firmware_header_v1_0, header);
113d38ceaf9SAlex Deucher 
114d38ceaf9SAlex Deucher 		DRM_DEBUG("ucode_feature_version: %u\n",
115d38ceaf9SAlex Deucher 			  le32_to_cpu(gfx_hdr->ucode_feature_version));
116d38ceaf9SAlex Deucher 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(gfx_hdr->jt_offset));
117d38ceaf9SAlex Deucher 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(gfx_hdr->jt_size));
118641f053eSLikun Gao 	} else if (version_major == 2) {
119641f053eSLikun Gao 		const struct gfx_firmware_header_v2_0 *gfx_hdr =
120641f053eSLikun Gao 			container_of(hdr, struct gfx_firmware_header_v2_0, header);
121641f053eSLikun Gao 
122641f053eSLikun Gao 		DRM_DEBUG("ucode_feature_version: %u\n",
123641f053eSLikun Gao 			  le32_to_cpu(gfx_hdr->ucode_feature_version));
124d38ceaf9SAlex Deucher 	} else {
125d38ceaf9SAlex Deucher 		DRM_ERROR("Unknown GFX ucode version: %u.%u\n", version_major, version_minor);
126d38ceaf9SAlex Deucher 	}
127d38ceaf9SAlex Deucher }
128d38ceaf9SAlex Deucher 
amdgpu_ucode_print_rlc_hdr(const struct common_firmware_header * hdr)129d38ceaf9SAlex Deucher void amdgpu_ucode_print_rlc_hdr(const struct common_firmware_header *hdr)
130d38ceaf9SAlex Deucher {
131d38ceaf9SAlex Deucher 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
132d38ceaf9SAlex Deucher 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
133d38ceaf9SAlex Deucher 
134d38ceaf9SAlex Deucher 	DRM_DEBUG("RLC\n");
135d38ceaf9SAlex Deucher 	amdgpu_ucode_print_common_hdr(hdr);
136d38ceaf9SAlex Deucher 
137d38ceaf9SAlex Deucher 	if (version_major == 1) {
138d38ceaf9SAlex Deucher 		const struct rlc_firmware_header_v1_0 *rlc_hdr =
139d38ceaf9SAlex Deucher 			container_of(hdr, struct rlc_firmware_header_v1_0, header);
140d38ceaf9SAlex Deucher 
141d38ceaf9SAlex Deucher 		DRM_DEBUG("ucode_feature_version: %u\n",
142d38ceaf9SAlex Deucher 			  le32_to_cpu(rlc_hdr->ucode_feature_version));
143d38ceaf9SAlex Deucher 		DRM_DEBUG("save_and_restore_offset: %u\n",
144d38ceaf9SAlex Deucher 			  le32_to_cpu(rlc_hdr->save_and_restore_offset));
145d38ceaf9SAlex Deucher 		DRM_DEBUG("clear_state_descriptor_offset: %u\n",
146d38ceaf9SAlex Deucher 			  le32_to_cpu(rlc_hdr->clear_state_descriptor_offset));
147d38ceaf9SAlex Deucher 		DRM_DEBUG("avail_scratch_ram_locations: %u\n",
148d38ceaf9SAlex Deucher 			  le32_to_cpu(rlc_hdr->avail_scratch_ram_locations));
149d38ceaf9SAlex Deucher 		DRM_DEBUG("master_pkt_description_offset: %u\n",
150d38ceaf9SAlex Deucher 			  le32_to_cpu(rlc_hdr->master_pkt_description_offset));
151d38ceaf9SAlex Deucher 	} else if (version_major == 2) {
152d38ceaf9SAlex Deucher 		const struct rlc_firmware_header_v2_0 *rlc_hdr =
153d38ceaf9SAlex Deucher 			container_of(hdr, struct rlc_firmware_header_v2_0, header);
154d5c6ad72SHawking Zhang 		const struct rlc_firmware_header_v2_1 *rlc_hdr_v2_1 =
155d5c6ad72SHawking Zhang 			container_of(rlc_hdr, struct rlc_firmware_header_v2_1, v2_0);
156d5c6ad72SHawking Zhang 		const struct rlc_firmware_header_v2_2 *rlc_hdr_v2_2 =
157d5c6ad72SHawking Zhang 			container_of(rlc_hdr_v2_1, struct rlc_firmware_header_v2_2, v2_1);
158d5c6ad72SHawking Zhang 		const struct rlc_firmware_header_v2_3 *rlc_hdr_v2_3 =
159d5c6ad72SHawking Zhang 			container_of(rlc_hdr_v2_2, struct rlc_firmware_header_v2_3, v2_2);
160d5c6ad72SHawking Zhang 		const struct rlc_firmware_header_v2_4 *rlc_hdr_v2_4 =
161d5c6ad72SHawking Zhang 			container_of(rlc_hdr_v2_3, struct rlc_firmware_header_v2_4, v2_3);
162d38ceaf9SAlex Deucher 
163d5c6ad72SHawking Zhang 		switch (version_minor) {
164d5c6ad72SHawking Zhang 		case 0:
165d5c6ad72SHawking Zhang 			/* rlc_hdr v2_0 */
166d38ceaf9SAlex Deucher 			DRM_DEBUG("ucode_feature_version: %u\n",
167d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->ucode_feature_version));
168d38ceaf9SAlex Deucher 			DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(rlc_hdr->jt_offset));
169d38ceaf9SAlex Deucher 			DRM_DEBUG("jt_size: %u\n", le32_to_cpu(rlc_hdr->jt_size));
170d38ceaf9SAlex Deucher 			DRM_DEBUG("save_and_restore_offset: %u\n",
171d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->save_and_restore_offset));
172d38ceaf9SAlex Deucher 			DRM_DEBUG("clear_state_descriptor_offset: %u\n",
173d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->clear_state_descriptor_offset));
174d38ceaf9SAlex Deucher 			DRM_DEBUG("avail_scratch_ram_locations: %u\n",
175d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->avail_scratch_ram_locations));
176d38ceaf9SAlex Deucher 			DRM_DEBUG("reg_restore_list_size: %u\n",
177d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->reg_restore_list_size));
178d38ceaf9SAlex Deucher 			DRM_DEBUG("reg_list_format_start: %u\n",
179d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->reg_list_format_start));
180d38ceaf9SAlex Deucher 			DRM_DEBUG("reg_list_format_separate_start: %u\n",
181d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->reg_list_format_separate_start));
182d38ceaf9SAlex Deucher 			DRM_DEBUG("starting_offsets_start: %u\n",
183d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->starting_offsets_start));
184d38ceaf9SAlex Deucher 			DRM_DEBUG("reg_list_format_size_bytes: %u\n",
185d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->reg_list_format_size_bytes));
186d38ceaf9SAlex Deucher 			DRM_DEBUG("reg_list_format_array_offset_bytes: %u\n",
187d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->reg_list_format_array_offset_bytes));
188d38ceaf9SAlex Deucher 			DRM_DEBUG("reg_list_size_bytes: %u\n",
189d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->reg_list_size_bytes));
190d38ceaf9SAlex Deucher 			DRM_DEBUG("reg_list_array_offset_bytes: %u\n",
191d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->reg_list_array_offset_bytes));
192d38ceaf9SAlex Deucher 			DRM_DEBUG("reg_list_format_separate_size_bytes: %u\n",
193d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->reg_list_format_separate_size_bytes));
194d38ceaf9SAlex Deucher 			DRM_DEBUG("reg_list_format_separate_array_offset_bytes: %u\n",
195d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->reg_list_format_separate_array_offset_bytes));
196d38ceaf9SAlex Deucher 			DRM_DEBUG("reg_list_separate_size_bytes: %u\n",
197d38ceaf9SAlex Deucher 				  le32_to_cpu(rlc_hdr->reg_list_separate_size_bytes));
198d40e9b13SHuang Rui 			DRM_DEBUG("reg_list_separate_array_offset_bytes: %u\n",
199d40e9b13SHuang Rui 				  le32_to_cpu(rlc_hdr->reg_list_separate_array_offset_bytes));
200d5c6ad72SHawking Zhang 			break;
201d5c6ad72SHawking Zhang 		case 1:
202d5c6ad72SHawking Zhang 			/* rlc_hdr v2_1 */
203d40e9b13SHuang Rui 			DRM_DEBUG("reg_list_format_direct_reg_list_length: %u\n",
204d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->reg_list_format_direct_reg_list_length));
205d40e9b13SHuang Rui 			DRM_DEBUG("save_restore_list_cntl_ucode_ver: %u\n",
206d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_cntl_ucode_ver));
207d40e9b13SHuang Rui 			DRM_DEBUG("save_restore_list_cntl_feature_ver: %u\n",
208d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_cntl_feature_ver));
209d40e9b13SHuang Rui 			DRM_DEBUG("save_restore_list_cntl_size_bytes %u\n",
210d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_cntl_size_bytes));
211d40e9b13SHuang Rui 			DRM_DEBUG("save_restore_list_cntl_offset_bytes: %u\n",
212d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_cntl_offset_bytes));
213d40e9b13SHuang Rui 			DRM_DEBUG("save_restore_list_gpm_ucode_ver: %u\n",
214d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_gpm_ucode_ver));
215d40e9b13SHuang Rui 			DRM_DEBUG("save_restore_list_gpm_feature_ver: %u\n",
216d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_gpm_feature_ver));
217d40e9b13SHuang Rui 			DRM_DEBUG("save_restore_list_gpm_size_bytes %u\n",
218d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_gpm_size_bytes));
219d40e9b13SHuang Rui 			DRM_DEBUG("save_restore_list_gpm_offset_bytes: %u\n",
220d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_gpm_offset_bytes));
221d40e9b13SHuang Rui 			DRM_DEBUG("save_restore_list_srm_ucode_ver: %u\n",
222d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_srm_ucode_ver));
223d40e9b13SHuang Rui 			DRM_DEBUG("save_restore_list_srm_feature_ver: %u\n",
224d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_srm_feature_ver));
225d40e9b13SHuang Rui 			DRM_DEBUG("save_restore_list_srm_size_bytes %u\n",
226d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_srm_size_bytes));
227d40e9b13SHuang Rui 			DRM_DEBUG("save_restore_list_srm_offset_bytes: %u\n",
228d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_1->save_restore_list_srm_offset_bytes));
229d5c6ad72SHawking Zhang 			break;
230d5c6ad72SHawking Zhang 		case 2:
231d5c6ad72SHawking Zhang 			/* rlc_hdr v2_2 */
232d5c6ad72SHawking Zhang 			DRM_DEBUG("rlc_iram_ucode_size_bytes: %u\n",
233d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_2->rlc_iram_ucode_size_bytes));
234d5c6ad72SHawking Zhang 			DRM_DEBUG("rlc_iram_ucode_offset_bytes: %u\n",
235d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_2->rlc_iram_ucode_offset_bytes));
236d5c6ad72SHawking Zhang 			DRM_DEBUG("rlc_dram_ucode_size_bytes: %u\n",
237d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_2->rlc_dram_ucode_size_bytes));
238d5c6ad72SHawking Zhang 			DRM_DEBUG("rlc_dram_ucode_offset_bytes: %u\n",
239d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_2->rlc_dram_ucode_offset_bytes));
240d5c6ad72SHawking Zhang 			break;
241d5c6ad72SHawking Zhang 		case 3:
242d5c6ad72SHawking Zhang 			/* rlc_hdr v2_3 */
243d5c6ad72SHawking Zhang 			DRM_DEBUG("rlcp_ucode_version: %u\n",
244d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_3->rlcp_ucode_version));
245d5c6ad72SHawking Zhang 			DRM_DEBUG("rlcp_ucode_feature_version: %u\n",
246d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_3->rlcp_ucode_feature_version));
247d5c6ad72SHawking Zhang 			DRM_DEBUG("rlcp_ucode_size_bytes: %u\n",
248d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_3->rlcp_ucode_size_bytes));
249d5c6ad72SHawking Zhang 			DRM_DEBUG("rlcp_ucode_offset_bytes: %u\n",
250d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_3->rlcp_ucode_offset_bytes));
251d5c6ad72SHawking Zhang 			DRM_DEBUG("rlcv_ucode_version: %u\n",
252d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_3->rlcv_ucode_version));
253d5c6ad72SHawking Zhang 			DRM_DEBUG("rlcv_ucode_feature_version: %u\n",
254d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_3->rlcv_ucode_feature_version));
255d5c6ad72SHawking Zhang 			DRM_DEBUG("rlcv_ucode_size_bytes: %u\n",
256d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_3->rlcv_ucode_size_bytes));
257d5c6ad72SHawking Zhang 			DRM_DEBUG("rlcv_ucode_offset_bytes: %u\n",
258d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_3->rlcv_ucode_offset_bytes));
259d5c6ad72SHawking Zhang 			break;
260d5c6ad72SHawking Zhang 		case 4:
261d5c6ad72SHawking Zhang 			/* rlc_hdr v2_4 */
262d5c6ad72SHawking Zhang 			DRM_DEBUG("global_tap_delays_ucode_size_bytes :%u\n",
263d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_4->global_tap_delays_ucode_size_bytes));
264d5c6ad72SHawking Zhang 			DRM_DEBUG("global_tap_delays_ucode_offset_bytes: %u\n",
265d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_4->global_tap_delays_ucode_offset_bytes));
266d5c6ad72SHawking Zhang 			DRM_DEBUG("se0_tap_delays_ucode_size_bytes :%u\n",
267d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_4->se0_tap_delays_ucode_size_bytes));
268d5c6ad72SHawking Zhang 			DRM_DEBUG("se0_tap_delays_ucode_offset_bytes: %u\n",
269d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_4->se0_tap_delays_ucode_offset_bytes));
270d5c6ad72SHawking Zhang 			DRM_DEBUG("se1_tap_delays_ucode_size_bytes :%u\n",
271d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_4->se1_tap_delays_ucode_size_bytes));
272d5c6ad72SHawking Zhang 			DRM_DEBUG("se1_tap_delays_ucode_offset_bytes: %u\n",
273d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_4->se1_tap_delays_ucode_offset_bytes));
274d5c6ad72SHawking Zhang 			DRM_DEBUG("se2_tap_delays_ucode_size_bytes :%u\n",
275d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_4->se2_tap_delays_ucode_size_bytes));
276d5c6ad72SHawking Zhang 			DRM_DEBUG("se2_tap_delays_ucode_offset_bytes: %u\n",
277d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_4->se2_tap_delays_ucode_offset_bytes));
278d5c6ad72SHawking Zhang 			DRM_DEBUG("se3_tap_delays_ucode_size_bytes :%u\n",
279d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_4->se3_tap_delays_ucode_size_bytes));
280d5c6ad72SHawking Zhang 			DRM_DEBUG("se3_tap_delays_ucode_offset_bytes: %u\n",
281d5c6ad72SHawking Zhang 				  le32_to_cpu(rlc_hdr_v2_4->se3_tap_delays_ucode_offset_bytes));
282d5c6ad72SHawking Zhang 			break;
283d5c6ad72SHawking Zhang 		default:
284d5c6ad72SHawking Zhang 			DRM_ERROR("Unknown RLC v2 ucode: v2.%u\n", version_minor);
285d5c6ad72SHawking Zhang 			break;
286d40e9b13SHuang Rui 		}
287d38ceaf9SAlex Deucher 	} else {
288d38ceaf9SAlex Deucher 		DRM_ERROR("Unknown RLC ucode version: %u.%u\n", version_major, version_minor);
289d38ceaf9SAlex Deucher 	}
290d38ceaf9SAlex Deucher }
291d38ceaf9SAlex Deucher 
amdgpu_ucode_print_sdma_hdr(const struct common_firmware_header * hdr)292d38ceaf9SAlex Deucher void amdgpu_ucode_print_sdma_hdr(const struct common_firmware_header *hdr)
293d38ceaf9SAlex Deucher {
294d38ceaf9SAlex Deucher 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
295d38ceaf9SAlex Deucher 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
296d38ceaf9SAlex Deucher 
297d38ceaf9SAlex Deucher 	DRM_DEBUG("SDMA\n");
298d38ceaf9SAlex Deucher 	amdgpu_ucode_print_common_hdr(hdr);
299d38ceaf9SAlex Deucher 
300d38ceaf9SAlex Deucher 	if (version_major == 1) {
301d38ceaf9SAlex Deucher 		const struct sdma_firmware_header_v1_0 *sdma_hdr =
302d38ceaf9SAlex Deucher 			container_of(hdr, struct sdma_firmware_header_v1_0, header);
303d38ceaf9SAlex Deucher 
304d38ceaf9SAlex Deucher 		DRM_DEBUG("ucode_feature_version: %u\n",
305d38ceaf9SAlex Deucher 			  le32_to_cpu(sdma_hdr->ucode_feature_version));
306d38ceaf9SAlex Deucher 		DRM_DEBUG("ucode_change_version: %u\n",
307d38ceaf9SAlex Deucher 			  le32_to_cpu(sdma_hdr->ucode_change_version));
308d38ceaf9SAlex Deucher 		DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(sdma_hdr->jt_offset));
309d38ceaf9SAlex Deucher 		DRM_DEBUG("jt_size: %u\n", le32_to_cpu(sdma_hdr->jt_size));
310d38ceaf9SAlex Deucher 		if (version_minor >= 1) {
311d38ceaf9SAlex Deucher 			const struct sdma_firmware_header_v1_1 *sdma_v1_1_hdr =
312d38ceaf9SAlex Deucher 				container_of(sdma_hdr, struct sdma_firmware_header_v1_1, v1_0);
313d38ceaf9SAlex Deucher 			DRM_DEBUG("digest_size: %u\n", le32_to_cpu(sdma_v1_1_hdr->digest_size));
314d38ceaf9SAlex Deucher 		}
3158e070831SLikun Gao 	} else if (version_major == 2) {
3168e070831SLikun Gao 		const struct sdma_firmware_header_v2_0 *sdma_hdr =
3178e070831SLikun Gao 			container_of(hdr, struct sdma_firmware_header_v2_0, header);
3188e070831SLikun Gao 
3198e070831SLikun Gao 		DRM_DEBUG("ucode_feature_version: %u\n",
3208e070831SLikun Gao 			  le32_to_cpu(sdma_hdr->ucode_feature_version));
3218e070831SLikun Gao 		DRM_DEBUG("ctx_jt_offset: %u\n", le32_to_cpu(sdma_hdr->ctx_jt_offset));
3228e070831SLikun Gao 		DRM_DEBUG("ctx_jt_size: %u\n", le32_to_cpu(sdma_hdr->ctx_jt_size));
3238e070831SLikun Gao 		DRM_DEBUG("ctl_ucode_offset: %u\n", le32_to_cpu(sdma_hdr->ctl_ucode_offset));
3248e070831SLikun Gao 		DRM_DEBUG("ctl_jt_offset: %u\n", le32_to_cpu(sdma_hdr->ctl_jt_offset));
3258e070831SLikun Gao 		DRM_DEBUG("ctl_jt_size: %u\n", le32_to_cpu(sdma_hdr->ctl_jt_size));
326d38ceaf9SAlex Deucher 	} else {
327d38ceaf9SAlex Deucher 		DRM_ERROR("Unknown SDMA ucode version: %u.%u\n",
328d38ceaf9SAlex Deucher 			  version_major, version_minor);
329d38ceaf9SAlex Deucher 	}
330d38ceaf9SAlex Deucher }
331d38ceaf9SAlex Deucher 
amdgpu_ucode_print_psp_hdr(const struct common_firmware_header * hdr)3326fa40564SHawking Zhang void amdgpu_ucode_print_psp_hdr(const struct common_firmware_header *hdr)
3336fa40564SHawking Zhang {
3346fa40564SHawking Zhang 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
3356fa40564SHawking Zhang 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
3365fea10d5SHawking Zhang 	uint32_t fw_index;
3375fea10d5SHawking Zhang 	const struct psp_fw_bin_desc *desc;
3386fa40564SHawking Zhang 
3396fa40564SHawking Zhang 	DRM_DEBUG("PSP\n");
3406fa40564SHawking Zhang 	amdgpu_ucode_print_common_hdr(hdr);
3416fa40564SHawking Zhang 
3426fa40564SHawking Zhang 	if (version_major == 1) {
3436fa40564SHawking Zhang 		const struct psp_firmware_header_v1_0 *psp_hdr =
3446fa40564SHawking Zhang 			container_of(hdr, struct psp_firmware_header_v1_0, header);
3456fa40564SHawking Zhang 
3466fa40564SHawking Zhang 		DRM_DEBUG("ucode_feature_version: %u\n",
34779a0f441SJohn Clements 			  le32_to_cpu(psp_hdr->sos.fw_version));
3486fa40564SHawking Zhang 		DRM_DEBUG("sos_offset_bytes: %u\n",
34979a0f441SJohn Clements 			  le32_to_cpu(psp_hdr->sos.offset_bytes));
3506fa40564SHawking Zhang 		DRM_DEBUG("sos_size_bytes: %u\n",
35179a0f441SJohn Clements 			  le32_to_cpu(psp_hdr->sos.size_bytes));
352434dbb2aSHawking Zhang 		if (version_minor == 1) {
353434dbb2aSHawking Zhang 			const struct psp_firmware_header_v1_1 *psp_hdr_v1_1 =
354434dbb2aSHawking Zhang 				container_of(psp_hdr, struct psp_firmware_header_v1_1, v1_0);
355434dbb2aSHawking Zhang 			DRM_DEBUG("toc_header_version: %u\n",
35679a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_1->toc.fw_version));
357434dbb2aSHawking Zhang 			DRM_DEBUG("toc_offset_bytes: %u\n",
35879a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_1->toc.offset_bytes));
359434dbb2aSHawking Zhang 			DRM_DEBUG("toc_size_bytes: %u\n",
36079a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_1->toc.size_bytes));
36142989359SHawking Zhang 			DRM_DEBUG("kdb_header_version: %u\n",
36279a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_1->kdb.fw_version));
36342989359SHawking Zhang 			DRM_DEBUG("kdb_offset_bytes: %u\n",
36479a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_1->kdb.offset_bytes));
36542989359SHawking Zhang 			DRM_DEBUG("kdb_size_bytes: %u\n",
36679a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_1->kdb.size_bytes));
367434dbb2aSHawking Zhang 		}
368dc0d9622SJohn Clements 		if (version_minor == 2) {
369dc0d9622SJohn Clements 			const struct psp_firmware_header_v1_2 *psp_hdr_v1_2 =
370dc0d9622SJohn Clements 				container_of(psp_hdr, struct psp_firmware_header_v1_2, v1_0);
371dc0d9622SJohn Clements 			DRM_DEBUG("kdb_header_version: %u\n",
37279a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_2->kdb.fw_version));
373dc0d9622SJohn Clements 			DRM_DEBUG("kdb_offset_bytes: %u\n",
37479a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_2->kdb.offset_bytes));
375dc0d9622SJohn Clements 			DRM_DEBUG("kdb_size_bytes: %u\n",
37679a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_2->kdb.size_bytes));
377dc0d9622SJohn Clements 		}
37843a188e0SLikun Gao 		if (version_minor == 3) {
37943a188e0SLikun Gao 			const struct psp_firmware_header_v1_1 *psp_hdr_v1_1 =
38043a188e0SLikun Gao 				container_of(psp_hdr, struct psp_firmware_header_v1_1, v1_0);
38143a188e0SLikun Gao 			const struct psp_firmware_header_v1_3 *psp_hdr_v1_3 =
38243a188e0SLikun Gao 				container_of(psp_hdr_v1_1, struct psp_firmware_header_v1_3, v1_1);
38343a188e0SLikun Gao 			DRM_DEBUG("toc_header_version: %u\n",
38479a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_3->v1_1.toc.fw_version));
38543a188e0SLikun Gao 			DRM_DEBUG("toc_offset_bytes: %u\n",
38679a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_3->v1_1.toc.offset_bytes));
38743a188e0SLikun Gao 			DRM_DEBUG("toc_size_bytes: %u\n",
38879a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_3->v1_1.toc.size_bytes));
38943a188e0SLikun Gao 			DRM_DEBUG("kdb_header_version: %u\n",
39079a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_3->v1_1.kdb.fw_version));
39143a188e0SLikun Gao 			DRM_DEBUG("kdb_offset_bytes: %u\n",
39279a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_3->v1_1.kdb.offset_bytes));
39343a188e0SLikun Gao 			DRM_DEBUG("kdb_size_bytes: %u\n",
39479a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_3->v1_1.kdb.size_bytes));
39543a188e0SLikun Gao 			DRM_DEBUG("spl_header_version: %u\n",
39679a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_3->spl.fw_version));
39743a188e0SLikun Gao 			DRM_DEBUG("spl_offset_bytes: %u\n",
39879a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_3->spl.offset_bytes));
39943a188e0SLikun Gao 			DRM_DEBUG("spl_size_bytes: %u\n",
40079a0f441SJohn Clements 				  le32_to_cpu(psp_hdr_v1_3->spl.size_bytes));
40143a188e0SLikun Gao 		}
4025fea10d5SHawking Zhang 	} else if (version_major == 2) {
4035fea10d5SHawking Zhang 		const struct psp_firmware_header_v2_0 *psp_hdr_v2_0 =
4045fea10d5SHawking Zhang 			 container_of(hdr, struct psp_firmware_header_v2_0, header);
4055fea10d5SHawking Zhang 		for (fw_index = 0; fw_index < le32_to_cpu(psp_hdr_v2_0->psp_fw_bin_count); fw_index++) {
4065fea10d5SHawking Zhang 			desc = &(psp_hdr_v2_0->psp_fw_bin[fw_index]);
4075fea10d5SHawking Zhang 			switch (desc->fw_type) {
4085fea10d5SHawking Zhang 			case PSP_FW_TYPE_PSP_SOS:
4095fea10d5SHawking Zhang 				DRM_DEBUG("psp_sos_version: %u\n",
4105fea10d5SHawking Zhang 					  le32_to_cpu(desc->fw_version));
4115fea10d5SHawking Zhang 				DRM_DEBUG("psp_sos_size_bytes: %u\n",
4125fea10d5SHawking Zhang 					  le32_to_cpu(desc->size_bytes));
4135fea10d5SHawking Zhang 				break;
4145fea10d5SHawking Zhang 			case PSP_FW_TYPE_PSP_SYS_DRV:
4155fea10d5SHawking Zhang 				DRM_DEBUG("psp_sys_drv_version: %u\n",
4165fea10d5SHawking Zhang 					  le32_to_cpu(desc->fw_version));
4175fea10d5SHawking Zhang 				DRM_DEBUG("psp_sys_drv_size_bytes: %u\n",
4185fea10d5SHawking Zhang 					  le32_to_cpu(desc->size_bytes));
4195fea10d5SHawking Zhang 				break;
4205fea10d5SHawking Zhang 			case PSP_FW_TYPE_PSP_KDB:
4215fea10d5SHawking Zhang 				DRM_DEBUG("psp_kdb_version: %u\n",
4225fea10d5SHawking Zhang 					  le32_to_cpu(desc->fw_version));
4235fea10d5SHawking Zhang 				DRM_DEBUG("psp_kdb_size_bytes: %u\n",
4245fea10d5SHawking Zhang 					  le32_to_cpu(desc->size_bytes));
4255fea10d5SHawking Zhang 				break;
4265fea10d5SHawking Zhang 			case PSP_FW_TYPE_PSP_TOC:
4275fea10d5SHawking Zhang 				DRM_DEBUG("psp_toc_version: %u\n",
4285fea10d5SHawking Zhang 					  le32_to_cpu(desc->fw_version));
4295fea10d5SHawking Zhang 				DRM_DEBUG("psp_toc_size_bytes: %u\n",
4305fea10d5SHawking Zhang 					  le32_to_cpu(desc->size_bytes));
4315fea10d5SHawking Zhang 				break;
4325fea10d5SHawking Zhang 			case PSP_FW_TYPE_PSP_SPL:
4335fea10d5SHawking Zhang 				DRM_DEBUG("psp_spl_version: %u\n",
4345fea10d5SHawking Zhang 					  le32_to_cpu(desc->fw_version));
4355fea10d5SHawking Zhang 				DRM_DEBUG("psp_spl_size_bytes: %u\n",
4365fea10d5SHawking Zhang 					  le32_to_cpu(desc->size_bytes));
4375fea10d5SHawking Zhang 				break;
4385fea10d5SHawking Zhang 			case PSP_FW_TYPE_PSP_RL:
4395fea10d5SHawking Zhang 				DRM_DEBUG("psp_rl_version: %u\n",
4405fea10d5SHawking Zhang 					  le32_to_cpu(desc->fw_version));
4415fea10d5SHawking Zhang 				DRM_DEBUG("psp_rl_size_bytes: %u\n",
4425fea10d5SHawking Zhang 					  le32_to_cpu(desc->size_bytes));
4435fea10d5SHawking Zhang 				break;
4445fea10d5SHawking Zhang 			case PSP_FW_TYPE_PSP_SOC_DRV:
4455fea10d5SHawking Zhang 				DRM_DEBUG("psp_soc_drv_version: %u\n",
4465fea10d5SHawking Zhang 					  le32_to_cpu(desc->fw_version));
4475fea10d5SHawking Zhang 				DRM_DEBUG("psp_soc_drv_size_bytes: %u\n",
4485fea10d5SHawking Zhang 					  le32_to_cpu(desc->size_bytes));
4495fea10d5SHawking Zhang 				break;
4505fea10d5SHawking Zhang 			case PSP_FW_TYPE_PSP_INTF_DRV:
4515fea10d5SHawking Zhang 				DRM_DEBUG("psp_intf_drv_version: %u\n",
4525fea10d5SHawking Zhang 					  le32_to_cpu(desc->fw_version));
4535fea10d5SHawking Zhang 				DRM_DEBUG("psp_intf_drv_size_bytes: %u\n",
4545fea10d5SHawking Zhang 					  le32_to_cpu(desc->size_bytes));
4555fea10d5SHawking Zhang 				break;
4565fea10d5SHawking Zhang 			case PSP_FW_TYPE_PSP_DBG_DRV:
4575fea10d5SHawking Zhang 				DRM_DEBUG("psp_dbg_drv_version: %u\n",
4585fea10d5SHawking Zhang 					  le32_to_cpu(desc->fw_version));
4595fea10d5SHawking Zhang 				DRM_DEBUG("psp_dbg_drv_size_bytes: %u\n",
4605fea10d5SHawking Zhang 					  le32_to_cpu(desc->size_bytes));
4615fea10d5SHawking Zhang 				break;
46205947892SStanley.Yang 			case PSP_FW_TYPE_PSP_RAS_DRV:
46305947892SStanley.Yang 				DRM_DEBUG("psp_ras_drv_version: %u\n",
46405947892SStanley.Yang 					  le32_to_cpu(desc->fw_version));
46505947892SStanley.Yang 				DRM_DEBUG("psp_ras_drv_size_bytes: %u\n",
46605947892SStanley.Yang 					  le32_to_cpu(desc->size_bytes));
46705947892SStanley.Yang 				break;
4685fea10d5SHawking Zhang 			default:
4695fea10d5SHawking Zhang 				DRM_DEBUG("Unsupported PSP fw type: %d\n", desc->fw_type);
4705fea10d5SHawking Zhang 				break;
4715fea10d5SHawking Zhang 			}
4725fea10d5SHawking Zhang 		}
4736fa40564SHawking Zhang 	} else {
4746fa40564SHawking Zhang 		DRM_ERROR("Unknown PSP ucode version: %u.%u\n",
4756fa40564SHawking Zhang 			  version_major, version_minor);
4766fa40564SHawking Zhang 	}
4776fa40564SHawking Zhang }
4786fa40564SHawking Zhang 
amdgpu_ucode_print_gpu_info_hdr(const struct common_firmware_header * hdr)4798ae1a336SAlex Deucher void amdgpu_ucode_print_gpu_info_hdr(const struct common_firmware_header *hdr)
4808ae1a336SAlex Deucher {
4818ae1a336SAlex Deucher 	uint16_t version_major = le16_to_cpu(hdr->header_version_major);
4828ae1a336SAlex Deucher 	uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
4838ae1a336SAlex Deucher 
4848ae1a336SAlex Deucher 	DRM_DEBUG("GPU_INFO\n");
4858ae1a336SAlex Deucher 	amdgpu_ucode_print_common_hdr(hdr);
4868ae1a336SAlex Deucher 
4878ae1a336SAlex Deucher 	if (version_major == 1) {
4888ae1a336SAlex Deucher 		const struct gpu_info_firmware_header_v1_0 *gpu_info_hdr =
4898ae1a336SAlex Deucher 			container_of(hdr, struct gpu_info_firmware_header_v1_0, header);
4908ae1a336SAlex Deucher 
4918ae1a336SAlex Deucher 		DRM_DEBUG("version_major: %u\n",
4928ae1a336SAlex Deucher 			  le16_to_cpu(gpu_info_hdr->version_major));
4938ae1a336SAlex Deucher 		DRM_DEBUG("version_minor: %u\n",
4948ae1a336SAlex Deucher 			  le16_to_cpu(gpu_info_hdr->version_minor));
4958ae1a336SAlex Deucher 	} else {
4968ae1a336SAlex Deucher 		DRM_ERROR("Unknown gpu_info ucode version: %u.%u\n", version_major, version_minor);
4978ae1a336SAlex Deucher 	}
4988ae1a336SAlex Deucher }
4998ae1a336SAlex Deucher 
amdgpu_ucode_validate(const struct firmware * fw)50003000128SMario Limonciello static int amdgpu_ucode_validate(const struct firmware *fw)
501d38ceaf9SAlex Deucher {
502d38ceaf9SAlex Deucher 	const struct common_firmware_header *hdr =
503d38ceaf9SAlex Deucher 		(const struct common_firmware_header *)fw->data;
504d38ceaf9SAlex Deucher 
505d38ceaf9SAlex Deucher 	if (fw->size == le32_to_cpu(hdr->size_bytes))
506d38ceaf9SAlex Deucher 		return 0;
507d38ceaf9SAlex Deucher 
508d38ceaf9SAlex Deucher 	return -EINVAL;
509d38ceaf9SAlex Deucher }
510d38ceaf9SAlex Deucher 
amdgpu_ucode_hdr_version(union amdgpu_firmware_header * hdr,uint16_t hdr_major,uint16_t hdr_minor)511d38ceaf9SAlex Deucher bool amdgpu_ucode_hdr_version(union amdgpu_firmware_header *hdr,
512d38ceaf9SAlex Deucher 				uint16_t hdr_major, uint16_t hdr_minor)
513d38ceaf9SAlex Deucher {
514d38ceaf9SAlex Deucher 	if ((hdr->common.header_version_major == hdr_major) &&
515d38ceaf9SAlex Deucher 		(hdr->common.header_version_minor == hdr_minor))
516d38ceaf9SAlex Deucher 		return true;
5177edda674SLikun Gao 	return false;
518d38ceaf9SAlex Deucher }
519d38ceaf9SAlex Deucher 
520e635ee07SHuang Rui enum amdgpu_firmware_load_type
amdgpu_ucode_get_load_type(struct amdgpu_device * adev,int load_type)521e635ee07SHuang Rui amdgpu_ucode_get_load_type(struct amdgpu_device *adev, int load_type)
522e635ee07SHuang Rui {
523e635ee07SHuang Rui 	switch (adev->asic_type) {
524e635ee07SHuang Rui #ifdef CONFIG_DRM_AMDGPU_SI
525e635ee07SHuang Rui 	case CHIP_TAHITI:
526e635ee07SHuang Rui 	case CHIP_PITCAIRN:
527e635ee07SHuang Rui 	case CHIP_VERDE:
528e635ee07SHuang Rui 	case CHIP_OLAND:
529d9997b64SAlex Deucher 	case CHIP_HAINAN:
530e635ee07SHuang Rui 		return AMDGPU_FW_LOAD_DIRECT;
531e635ee07SHuang Rui #endif
532e635ee07SHuang Rui #ifdef CONFIG_DRM_AMDGPU_CIK
533e635ee07SHuang Rui 	case CHIP_BONAIRE:
534e635ee07SHuang Rui 	case CHIP_KAVERI:
535e635ee07SHuang Rui 	case CHIP_KABINI:
536e635ee07SHuang Rui 	case CHIP_HAWAII:
537e635ee07SHuang Rui 	case CHIP_MULLINS:
538e635ee07SHuang Rui 		return AMDGPU_FW_LOAD_DIRECT;
539e635ee07SHuang Rui #endif
540e635ee07SHuang Rui 	case CHIP_TOPAZ:
541e635ee07SHuang Rui 	case CHIP_TONGA:
542e635ee07SHuang Rui 	case CHIP_FIJI:
543e635ee07SHuang Rui 	case CHIP_CARRIZO:
544e635ee07SHuang Rui 	case CHIP_STONEY:
545e635ee07SHuang Rui 	case CHIP_POLARIS10:
546e635ee07SHuang Rui 	case CHIP_POLARIS11:
547e635ee07SHuang Rui 	case CHIP_POLARIS12:
54834fd54bcSLeo Liu 	case CHIP_VEGAM:
549e635ee07SHuang Rui 		return AMDGPU_FW_LOAD_SMU;
550d594e3ccSTao Zhou 	case CHIP_CYAN_SKILLFISH:
551b8e42844SHuang Rui 		if (!(load_type &&
552b8e42844SHuang Rui 		      adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2))
553d594e3ccSTao Zhou 			return AMDGPU_FW_LOAD_DIRECT;
554b8e42844SHuang Rui 		else
555b8e42844SHuang Rui 			return AMDGPU_FW_LOAD_PSP;
556e635ee07SHuang Rui 	default:
557aa9f8cc3SAlex Deucher 		if (!load_type)
558e635ee07SHuang Rui 			return AMDGPU_FW_LOAD_DIRECT;
559aa9f8cc3SAlex Deucher 		else
560aa9f8cc3SAlex Deucher 			return AMDGPU_FW_LOAD_PSP;
561aa9f8cc3SAlex Deucher 	}
562e635ee07SHuang Rui }
563e635ee07SHuang Rui 
amdgpu_ucode_name(enum AMDGPU_UCODE_ID ucode_id)564aae435c6SLang Yu const char *amdgpu_ucode_name(enum AMDGPU_UCODE_ID ucode_id)
565aae435c6SLang Yu {
566aae435c6SLang Yu 	switch (ucode_id) {
567aae435c6SLang Yu 	case AMDGPU_UCODE_ID_SDMA0:
568aae435c6SLang Yu 		return "SDMA0";
569aae435c6SLang Yu 	case AMDGPU_UCODE_ID_SDMA1:
570aae435c6SLang Yu 		return "SDMA1";
571aae435c6SLang Yu 	case AMDGPU_UCODE_ID_SDMA2:
572aae435c6SLang Yu 		return "SDMA2";
573aae435c6SLang Yu 	case AMDGPU_UCODE_ID_SDMA3:
574aae435c6SLang Yu 		return "SDMA3";
575aae435c6SLang Yu 	case AMDGPU_UCODE_ID_SDMA4:
576aae435c6SLang Yu 		return "SDMA4";
577aae435c6SLang Yu 	case AMDGPU_UCODE_ID_SDMA5:
578aae435c6SLang Yu 		return "SDMA5";
579aae435c6SLang Yu 	case AMDGPU_UCODE_ID_SDMA6:
580aae435c6SLang Yu 		return "SDMA6";
581aae435c6SLang Yu 	case AMDGPU_UCODE_ID_SDMA7:
582aae435c6SLang Yu 		return "SDMA7";
583619c94c3SLikun Gao 	case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
584619c94c3SLikun Gao 		return "SDMA_CTX";
585619c94c3SLikun Gao 	case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
586619c94c3SLikun Gao 		return "SDMA_CTL";
587aae435c6SLang Yu 	case AMDGPU_UCODE_ID_CP_CE:
588aae435c6SLang Yu 		return "CP_CE";
589aae435c6SLang Yu 	case AMDGPU_UCODE_ID_CP_PFP:
590aae435c6SLang Yu 		return "CP_PFP";
591aae435c6SLang Yu 	case AMDGPU_UCODE_ID_CP_ME:
592aae435c6SLang Yu 		return "CP_ME";
593aae435c6SLang Yu 	case AMDGPU_UCODE_ID_CP_MEC1:
594aae435c6SLang Yu 		return "CP_MEC1";
595aae435c6SLang Yu 	case AMDGPU_UCODE_ID_CP_MEC1_JT:
596aae435c6SLang Yu 		return "CP_MEC1_JT";
597aae435c6SLang Yu 	case AMDGPU_UCODE_ID_CP_MEC2:
598aae435c6SLang Yu 		return "CP_MEC2";
599aae435c6SLang Yu 	case AMDGPU_UCODE_ID_CP_MEC2_JT:
600aae435c6SLang Yu 		return "CP_MEC2_JT";
601aae435c6SLang Yu 	case AMDGPU_UCODE_ID_CP_MES:
602aae435c6SLang Yu 		return "CP_MES";
603aae435c6SLang Yu 	case AMDGPU_UCODE_ID_CP_MES_DATA:
604aae435c6SLang Yu 		return "CP_MES_DATA";
605619c94c3SLikun Gao 	case AMDGPU_UCODE_ID_CP_MES1:
606619c94c3SLikun Gao 		return "CP_MES_KIQ";
607619c94c3SLikun Gao 	case AMDGPU_UCODE_ID_CP_MES1_DATA:
608619c94c3SLikun Gao 		return "CP_MES_KIQ_DATA";
609aae435c6SLang Yu 	case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
610aae435c6SLang Yu 		return "RLC_RESTORE_LIST_CNTL";
611aae435c6SLang Yu 	case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
612aae435c6SLang Yu 		return "RLC_RESTORE_LIST_GPM_MEM";
613aae435c6SLang Yu 	case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
614aae435c6SLang Yu 		return "RLC_RESTORE_LIST_SRM_MEM";
615aae435c6SLang Yu 	case AMDGPU_UCODE_ID_RLC_IRAM:
616aae435c6SLang Yu 		return "RLC_IRAM";
617aae435c6SLang Yu 	case AMDGPU_UCODE_ID_RLC_DRAM:
618aae435c6SLang Yu 		return "RLC_DRAM";
619aae435c6SLang Yu 	case AMDGPU_UCODE_ID_RLC_G:
620aae435c6SLang Yu 		return "RLC_G";
621619c94c3SLikun Gao 	case AMDGPU_UCODE_ID_RLC_P:
622619c94c3SLikun Gao 		return "RLC_P";
623619c94c3SLikun Gao 	case AMDGPU_UCODE_ID_RLC_V:
624619c94c3SLikun Gao 		return "RLC_V";
6252207efddSChengming Gui 	case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
6262207efddSChengming Gui 		return "GLOBAL_TAP_DELAYS";
6272207efddSChengming Gui 	case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
6282207efddSChengming Gui 		return "SE0_TAP_DELAYS";
6292207efddSChengming Gui 	case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
6302207efddSChengming Gui 		return "SE1_TAP_DELAYS";
6312207efddSChengming Gui 	case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
6322207efddSChengming Gui 		return "SE2_TAP_DELAYS";
6332207efddSChengming Gui 	case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
6342207efddSChengming Gui 		return "SE3_TAP_DELAYS";
635619c94c3SLikun Gao 	case AMDGPU_UCODE_ID_IMU_I:
636619c94c3SLikun Gao 		return "IMU_I";
637619c94c3SLikun Gao 	case AMDGPU_UCODE_ID_IMU_D:
638619c94c3SLikun Gao 		return "IMU_D";
639aae435c6SLang Yu 	case AMDGPU_UCODE_ID_STORAGE:
640aae435c6SLang Yu 		return "STORAGE";
641aae435c6SLang Yu 	case AMDGPU_UCODE_ID_SMC:
642aae435c6SLang Yu 		return "SMC";
643b37c41f2SEvan Quan 	case AMDGPU_UCODE_ID_PPTABLE:
644b37c41f2SEvan Quan 		return "PPTABLE";
645aae435c6SLang Yu 	case AMDGPU_UCODE_ID_UVD:
646aae435c6SLang Yu 		return "UVD";
647aae435c6SLang Yu 	case AMDGPU_UCODE_ID_UVD1:
648aae435c6SLang Yu 		return "UVD1";
649aae435c6SLang Yu 	case AMDGPU_UCODE_ID_VCE:
650aae435c6SLang Yu 		return "VCE";
651aae435c6SLang Yu 	case AMDGPU_UCODE_ID_VCN:
652aae435c6SLang Yu 		return "VCN";
653aae435c6SLang Yu 	case AMDGPU_UCODE_ID_VCN1:
654aae435c6SLang Yu 		return "VCN1";
655aae435c6SLang Yu 	case AMDGPU_UCODE_ID_DMCU_ERAM:
656aae435c6SLang Yu 		return "DMCU_ERAM";
657aae435c6SLang Yu 	case AMDGPU_UCODE_ID_DMCU_INTV:
658aae435c6SLang Yu 		return "DMCU_INTV";
659aae435c6SLang Yu 	case AMDGPU_UCODE_ID_VCN0_RAM:
660aae435c6SLang Yu 		return "VCN0_RAM";
661aae435c6SLang Yu 	case AMDGPU_UCODE_ID_VCN1_RAM:
662aae435c6SLang Yu 		return "VCN1_RAM";
663aae435c6SLang Yu 	case AMDGPU_UCODE_ID_DMCUB:
664aae435c6SLang Yu 		return "DMCUB";
6653cd658deSBill Liu 	case AMDGPU_UCODE_ID_CAP:
6663cd658deSBill Liu 		return "CAP";
667aae435c6SLang Yu 	default:
668aae435c6SLang Yu 		return "UNKNOWN UCODE";
669aae435c6SLang Yu 	}
670aae435c6SLang Yu }
671aae435c6SLang Yu 
6725bb23532SOri Messinger #define FW_VERSION_ATTR(name, mode, field)				\
6735bb23532SOri Messinger static ssize_t show_##name(struct device *dev,				\
6745bb23532SOri Messinger 			  struct device_attribute *attr,		\
6755bb23532SOri Messinger 			  char *buf)					\
6765bb23532SOri Messinger {									\
6775bb23532SOri Messinger 	struct drm_device *ddev = dev_get_drvdata(dev);			\
6781348969aSLuben Tuikov 	struct amdgpu_device *adev = drm_to_adev(ddev);			\
6795bb23532SOri Messinger 									\
68040320159SQing Wang 	return sysfs_emit(buf, "0x%08x\n", adev->field);	\
6815bb23532SOri Messinger }									\
6825bb23532SOri Messinger static DEVICE_ATTR(name, mode, show_##name, NULL)
6835bb23532SOri Messinger 
6845bb23532SOri Messinger FW_VERSION_ATTR(vce_fw_version, 0444, vce.fw_version);
6855bb23532SOri Messinger FW_VERSION_ATTR(uvd_fw_version, 0444, uvd.fw_version);
6865bb23532SOri Messinger FW_VERSION_ATTR(mc_fw_version, 0444, gmc.fw_version);
6875bb23532SOri Messinger FW_VERSION_ATTR(me_fw_version, 0444, gfx.me_fw_version);
6885bb23532SOri Messinger FW_VERSION_ATTR(pfp_fw_version, 0444, gfx.pfp_fw_version);
6895bb23532SOri Messinger FW_VERSION_ATTR(ce_fw_version, 0444, gfx.ce_fw_version);
6905bb23532SOri Messinger FW_VERSION_ATTR(rlc_fw_version, 0444, gfx.rlc_fw_version);
6915bb23532SOri Messinger FW_VERSION_ATTR(rlc_srlc_fw_version, 0444, gfx.rlc_srlc_fw_version);
6925bb23532SOri Messinger FW_VERSION_ATTR(rlc_srlg_fw_version, 0444, gfx.rlc_srlg_fw_version);
6935bb23532SOri Messinger FW_VERSION_ATTR(rlc_srls_fw_version, 0444, gfx.rlc_srls_fw_version);
6945bb23532SOri Messinger FW_VERSION_ATTR(mec_fw_version, 0444, gfx.mec_fw_version);
6955bb23532SOri Messinger FW_VERSION_ATTR(mec2_fw_version, 0444, gfx.mec2_fw_version);
696b7236296SDavid Francis FW_VERSION_ATTR(imu_fw_version, 0444, gfx.imu_fw_version);
697222e0a71SCandice Li FW_VERSION_ATTR(sos_fw_version, 0444, psp.sos.fw_version);
698de3a1e33SCandice Li FW_VERSION_ATTR(asd_fw_version, 0444, psp.asd_context.bin_desc.fw_version);
6994320e6f8SCandice Li FW_VERSION_ATTR(ta_ras_fw_version, 0444, psp.ras_context.context.bin_desc.fw_version);
7004320e6f8SCandice Li FW_VERSION_ATTR(ta_xgmi_fw_version, 0444, psp.xgmi_context.context.bin_desc.fw_version);
7015bb23532SOri Messinger FW_VERSION_ATTR(smc_fw_version, 0444, pm.fw_version);
7025bb23532SOri Messinger FW_VERSION_ATTR(sdma_fw_version, 0444, sdma.instance[0].fw_version);
7035bb23532SOri Messinger FW_VERSION_ATTR(sdma2_fw_version, 0444, sdma.instance[1].fw_version);
7045bb23532SOri Messinger FW_VERSION_ATTR(vcn_fw_version, 0444, vcn.fw_version);
7055bb23532SOri Messinger FW_VERSION_ATTR(dmcu_fw_version, 0444, dm.dmcu_fw_version);
706557d466bSOri Messinger FW_VERSION_ATTR(mes_fw_version, 0444, mes.sched_version & AMDGPU_MES_VERSION_MASK);
707557d466bSOri Messinger FW_VERSION_ATTR(mes_kiq_fw_version, 0444, mes.kiq_version & AMDGPU_MES_VERSION_MASK);
7085bb23532SOri Messinger 
7095bb23532SOri Messinger static struct attribute *fw_attrs[] = {
7105bb23532SOri Messinger 	&dev_attr_vce_fw_version.attr, &dev_attr_uvd_fw_version.attr,
7115bb23532SOri Messinger 	&dev_attr_mc_fw_version.attr, &dev_attr_me_fw_version.attr,
7125bb23532SOri Messinger 	&dev_attr_pfp_fw_version.attr, &dev_attr_ce_fw_version.attr,
7135bb23532SOri Messinger 	&dev_attr_rlc_fw_version.attr, &dev_attr_rlc_srlc_fw_version.attr,
7145bb23532SOri Messinger 	&dev_attr_rlc_srlg_fw_version.attr, &dev_attr_rlc_srls_fw_version.attr,
7155bb23532SOri Messinger 	&dev_attr_mec_fw_version.attr, &dev_attr_mec2_fw_version.attr,
7165bb23532SOri Messinger 	&dev_attr_sos_fw_version.attr, &dev_attr_asd_fw_version.attr,
7175bb23532SOri Messinger 	&dev_attr_ta_ras_fw_version.attr, &dev_attr_ta_xgmi_fw_version.attr,
7185bb23532SOri Messinger 	&dev_attr_smc_fw_version.attr, &dev_attr_sdma_fw_version.attr,
7195bb23532SOri Messinger 	&dev_attr_sdma2_fw_version.attr, &dev_attr_vcn_fw_version.attr,
720b7236296SDavid Francis 	&dev_attr_dmcu_fw_version.attr, &dev_attr_imu_fw_version.attr,
721557d466bSOri Messinger 	&dev_attr_mes_fw_version.attr, &dev_attr_mes_kiq_fw_version.attr,
722b7236296SDavid Francis 	NULL
7235bb23532SOri Messinger };
7245bb23532SOri Messinger 
7255bb23532SOri Messinger static const struct attribute_group fw_attr_group = {
7265bb23532SOri Messinger 	.name = "fw_version",
7275bb23532SOri Messinger 	.attrs = fw_attrs
7285bb23532SOri Messinger };
7295bb23532SOri Messinger 
amdgpu_ucode_sysfs_init(struct amdgpu_device * adev)7305bb23532SOri Messinger int amdgpu_ucode_sysfs_init(struct amdgpu_device *adev)
7315bb23532SOri Messinger {
7325bb23532SOri Messinger 	return sysfs_create_group(&adev->dev->kobj, &fw_attr_group);
7335bb23532SOri Messinger }
7345bb23532SOri Messinger 
amdgpu_ucode_sysfs_fini(struct amdgpu_device * adev)7355bb23532SOri Messinger void amdgpu_ucode_sysfs_fini(struct amdgpu_device *adev)
7365bb23532SOri Messinger {
7375bb23532SOri Messinger 	sysfs_remove_group(&adev->dev->kobj, &fw_attr_group);
7385bb23532SOri Messinger }
7395bb23532SOri Messinger 
amdgpu_ucode_init_single_fw(struct amdgpu_device * adev,struct amdgpu_firmware_info * ucode,uint64_t mc_addr,void * kptr)7402445b227SHuang Rui static int amdgpu_ucode_init_single_fw(struct amdgpu_device *adev,
7412445b227SHuang Rui 				       struct amdgpu_firmware_info *ucode,
742d38ceaf9SAlex Deucher 				       uint64_t mc_addr, void *kptr)
743d38ceaf9SAlex Deucher {
744d38ceaf9SAlex Deucher 	const struct common_firmware_header *header = NULL;
7452445b227SHuang Rui 	const struct gfx_firmware_header_v1_0 *cp_hdr = NULL;
74614ab2924SLikun Gao 	const struct gfx_firmware_header_v2_0 *cpv2_hdr = NULL;
74701fcfc83SDavid Francis 	const struct dmcu_firmware_header_v1_0 *dmcu_hdr = NULL;
74802350f0bSNicholas Kazlauskas 	const struct dmcub_firmware_header_v1_0 *dmcub_hdr = NULL;
749aa1faaa1SJack Xiao 	const struct mes_firmware_header_v1_0 *mes_hdr = NULL;
7506777c8cfSLikun Gao 	const struct sdma_firmware_header_v2_0 *sdma_hdr = NULL;
751a32fa029SLikun Gao 	const struct imu_firmware_header_v1_0 *imu_hdr = NULL;
75202f958a2SLikun Gao 	u8 *ucode_addr;
753d38ceaf9SAlex Deucher 
754e03f04b8SSrinivasan Shanmugam 	if (!ucode->fw)
755d38ceaf9SAlex Deucher 		return 0;
756d38ceaf9SAlex Deucher 
757d38ceaf9SAlex Deucher 	ucode->mc_addr = mc_addr;
758d38ceaf9SAlex Deucher 	ucode->kaddr = kptr;
759d38ceaf9SAlex Deucher 
760bed5712eSMonk Liu 	if (ucode->ucode_id == AMDGPU_UCODE_ID_STORAGE)
761bed5712eSMonk Liu 		return 0;
762bed5712eSMonk Liu 
763d38ceaf9SAlex Deucher 	header = (const struct common_firmware_header *)ucode->fw->data;
7642445b227SHuang Rui 	cp_hdr = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
76514ab2924SLikun Gao 	cpv2_hdr = (const struct gfx_firmware_header_v2_0 *)ucode->fw->data;
76601fcfc83SDavid Francis 	dmcu_hdr = (const struct dmcu_firmware_header_v1_0 *)ucode->fw->data;
76702350f0bSNicholas Kazlauskas 	dmcub_hdr = (const struct dmcub_firmware_header_v1_0 *)ucode->fw->data;
768aa1faaa1SJack Xiao 	mes_hdr = (const struct mes_firmware_header_v1_0 *)ucode->fw->data;
7696777c8cfSLikun Gao 	sdma_hdr = (const struct sdma_firmware_header_v2_0 *)ucode->fw->data;
770a32fa029SLikun Gao 	imu_hdr = (const struct imu_firmware_header_v1_0 *)ucode->fw->data;
7712445b227SHuang Rui 
77202f958a2SLikun Gao 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
77302f958a2SLikun Gao 		switch (ucode->ucode_id) {
7746777c8cfSLikun Gao 		case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
775aca670e4SLikun Gao 			ucode->ucode_size = le32_to_cpu(sdma_hdr->ctx_ucode_size_bytes);
7766777c8cfSLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
7776777c8cfSLikun Gao 				le32_to_cpu(sdma_hdr->header.ucode_array_offset_bytes);
7786777c8cfSLikun Gao 			break;
7796777c8cfSLikun Gao 		case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
780aca670e4SLikun Gao 			ucode->ucode_size = le32_to_cpu(sdma_hdr->ctl_ucode_size_bytes);
7816777c8cfSLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
7826777c8cfSLikun Gao 				le32_to_cpu(sdma_hdr->ctl_ucode_offset);
7836777c8cfSLikun Gao 			break;
78402f958a2SLikun Gao 		case AMDGPU_UCODE_ID_CP_MEC1:
78502f958a2SLikun Gao 		case AMDGPU_UCODE_ID_CP_MEC2:
7862445b227SHuang Rui 			ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes) -
7872445b227SHuang Rui 				le32_to_cpu(cp_hdr->jt_size) * 4;
78802f958a2SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
78902f958a2SLikun Gao 				le32_to_cpu(header->ucode_array_offset_bytes);
79002f958a2SLikun Gao 			break;
79102f958a2SLikun Gao 		case AMDGPU_UCODE_ID_CP_MEC1_JT:
79202f958a2SLikun Gao 		case AMDGPU_UCODE_ID_CP_MEC2_JT:
7932445b227SHuang Rui 			ucode->ucode_size = le32_to_cpu(cp_hdr->jt_size) * 4;
79402f958a2SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
7952445b227SHuang Rui 				le32_to_cpu(header->ucode_array_offset_bytes) +
79602f958a2SLikun Gao 				le32_to_cpu(cp_hdr->jt_offset) * 4;
79702f958a2SLikun Gao 			break;
79802f958a2SLikun Gao 		case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
79902f958a2SLikun Gao 			ucode->ucode_size = adev->gfx.rlc.save_restore_list_cntl_size_bytes;
80002f958a2SLikun Gao 			ucode_addr = adev->gfx.rlc.save_restore_list_cntl;
80102f958a2SLikun Gao 			break;
80202f958a2SLikun Gao 		case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
80302f958a2SLikun Gao 			ucode->ucode_size = adev->gfx.rlc.save_restore_list_gpm_size_bytes;
80402f958a2SLikun Gao 			ucode_addr = adev->gfx.rlc.save_restore_list_gpm;
80502f958a2SLikun Gao 			break;
80602f958a2SLikun Gao 		case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
80702f958a2SLikun Gao 			ucode->ucode_size = adev->gfx.rlc.save_restore_list_srm_size_bytes;
80802f958a2SLikun Gao 			ucode_addr = adev->gfx.rlc.save_restore_list_srm;
80902f958a2SLikun Gao 			break;
81002f958a2SLikun Gao 		case AMDGPU_UCODE_ID_RLC_IRAM:
81102f958a2SLikun Gao 			ucode->ucode_size = adev->gfx.rlc.rlc_iram_ucode_size_bytes;
81202f958a2SLikun Gao 			ucode_addr = adev->gfx.rlc.rlc_iram_ucode;
81302f958a2SLikun Gao 			break;
81402f958a2SLikun Gao 		case AMDGPU_UCODE_ID_RLC_DRAM:
81502f958a2SLikun Gao 			ucode->ucode_size = adev->gfx.rlc.rlc_dram_ucode_size_bytes;
81602f958a2SLikun Gao 			ucode_addr = adev->gfx.rlc.rlc_dram_ucode;
81702f958a2SLikun Gao 			break;
818a0fe38b4SLikun Gao 		case AMDGPU_UCODE_ID_RLC_P:
819a0fe38b4SLikun Gao 			ucode->ucode_size = adev->gfx.rlc.rlcp_ucode_size_bytes;
820a0fe38b4SLikun Gao 			ucode_addr = adev->gfx.rlc.rlcp_ucode;
821a0fe38b4SLikun Gao 			break;
8228e41a56aSLikun Gao 		case AMDGPU_UCODE_ID_RLC_V:
8238e41a56aSLikun Gao 			ucode->ucode_size = adev->gfx.rlc.rlcv_ucode_size_bytes;
8248e41a56aSLikun Gao 			ucode_addr = adev->gfx.rlc.rlcv_ucode;
8258e41a56aSLikun Gao 			break;
8262207efddSChengming Gui 		case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
8272207efddSChengming Gui 			ucode->ucode_size = adev->gfx.rlc.global_tap_delays_ucode_size_bytes;
8282207efddSChengming Gui 			ucode_addr = adev->gfx.rlc.global_tap_delays_ucode;
8292207efddSChengming Gui 			break;
8302207efddSChengming Gui 		case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
8312207efddSChengming Gui 			ucode->ucode_size = adev->gfx.rlc.se0_tap_delays_ucode_size_bytes;
8322207efddSChengming Gui 			ucode_addr = adev->gfx.rlc.se0_tap_delays_ucode;
8332207efddSChengming Gui 			break;
8342207efddSChengming Gui 		case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
8352207efddSChengming Gui 			ucode->ucode_size = adev->gfx.rlc.se1_tap_delays_ucode_size_bytes;
8362207efddSChengming Gui 			ucode_addr = adev->gfx.rlc.se1_tap_delays_ucode;
8372207efddSChengming Gui 			break;
8382207efddSChengming Gui 		case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
8392207efddSChengming Gui 			ucode->ucode_size = adev->gfx.rlc.se2_tap_delays_ucode_size_bytes;
8402207efddSChengming Gui 			ucode_addr = adev->gfx.rlc.se2_tap_delays_ucode;
8412207efddSChengming Gui 			break;
8422207efddSChengming Gui 		case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
8432207efddSChengming Gui 			ucode->ucode_size = adev->gfx.rlc.se3_tap_delays_ucode_size_bytes;
8442207efddSChengming Gui 			ucode_addr = adev->gfx.rlc.se3_tap_delays_ucode;
8452207efddSChengming Gui 			break;
84602f958a2SLikun Gao 		case AMDGPU_UCODE_ID_CP_MES:
84702f958a2SLikun Gao 			ucode->ucode_size = le32_to_cpu(mes_hdr->mes_ucode_size_bytes);
84802f958a2SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
84902f958a2SLikun Gao 				le32_to_cpu(mes_hdr->mes_ucode_offset_bytes);
85002f958a2SLikun Gao 			break;
85102f958a2SLikun Gao 		case AMDGPU_UCODE_ID_CP_MES_DATA:
85202f958a2SLikun Gao 			ucode->ucode_size = le32_to_cpu(mes_hdr->mes_ucode_data_size_bytes);
85302f958a2SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
85402f958a2SLikun Gao 				le32_to_cpu(mes_hdr->mes_ucode_data_offset_bytes);
85502f958a2SLikun Gao 			break;
856b0f34028SJack Xiao 		case AMDGPU_UCODE_ID_CP_MES1:
857b0f34028SJack Xiao 			ucode->ucode_size = le32_to_cpu(mes_hdr->mes_ucode_size_bytes);
858b0f34028SJack Xiao 			ucode_addr = (u8 *)ucode->fw->data +
859b0f34028SJack Xiao 				le32_to_cpu(mes_hdr->mes_ucode_offset_bytes);
860b0f34028SJack Xiao 			break;
861b0f34028SJack Xiao 		case AMDGPU_UCODE_ID_CP_MES1_DATA:
862b0f34028SJack Xiao 			ucode->ucode_size = le32_to_cpu(mes_hdr->mes_ucode_data_size_bytes);
863b0f34028SJack Xiao 			ucode_addr = (u8 *)ucode->fw->data +
864b0f34028SJack Xiao 				le32_to_cpu(mes_hdr->mes_ucode_data_offset_bytes);
865b0f34028SJack Xiao 			break;
86602f958a2SLikun Gao 		case AMDGPU_UCODE_ID_DMCU_ERAM:
86701fcfc83SDavid Francis 			ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes) -
86801fcfc83SDavid Francis 				le32_to_cpu(dmcu_hdr->intv_size_bytes);
86902f958a2SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
87002f958a2SLikun Gao 				le32_to_cpu(header->ucode_array_offset_bytes);
87102f958a2SLikun Gao 			break;
87202f958a2SLikun Gao 		case AMDGPU_UCODE_ID_DMCU_INTV:
87301fcfc83SDavid Francis 			ucode->ucode_size = le32_to_cpu(dmcu_hdr->intv_size_bytes);
87402f958a2SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
87501fcfc83SDavid Francis 				le32_to_cpu(header->ucode_array_offset_bytes) +
87602f958a2SLikun Gao 				le32_to_cpu(dmcu_hdr->intv_offset_bytes);
87702f958a2SLikun Gao 			break;
87802f958a2SLikun Gao 		case AMDGPU_UCODE_ID_DMCUB:
87902350f0bSNicholas Kazlauskas 			ucode->ucode_size = le32_to_cpu(dmcub_hdr->inst_const_bytes);
88002f958a2SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
88102f958a2SLikun Gao 				le32_to_cpu(header->ucode_array_offset_bytes);
88202f958a2SLikun Gao 			break;
883b37c41f2SEvan Quan 		case AMDGPU_UCODE_ID_PPTABLE:
884b37c41f2SEvan Quan 			ucode->ucode_size = ucode->fw->size;
885b37c41f2SEvan Quan 			ucode_addr = (u8 *)ucode->fw->data;
886b37c41f2SEvan Quan 			break;
887a32fa029SLikun Gao 		case AMDGPU_UCODE_ID_IMU_I:
888a32fa029SLikun Gao 			ucode->ucode_size = le32_to_cpu(imu_hdr->imu_iram_ucode_size_bytes);
889a32fa029SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
890a32fa029SLikun Gao 				le32_to_cpu(imu_hdr->header.ucode_array_offset_bytes);
891a32fa029SLikun Gao 			break;
892a32fa029SLikun Gao 		case AMDGPU_UCODE_ID_IMU_D:
893a32fa029SLikun Gao 			ucode->ucode_size = le32_to_cpu(imu_hdr->imu_dram_ucode_size_bytes);
894a32fa029SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
895a32fa029SLikun Gao 				le32_to_cpu(imu_hdr->header.ucode_array_offset_bytes) +
896a32fa029SLikun Gao 				le32_to_cpu(imu_hdr->imu_iram_ucode_size_bytes);
897a32fa029SLikun Gao 			break;
89814ab2924SLikun Gao 		case AMDGPU_UCODE_ID_CP_RS64_PFP:
89914ab2924SLikun Gao 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->ucode_size_bytes);
90014ab2924SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
90114ab2924SLikun Gao 				le32_to_cpu(header->ucode_array_offset_bytes);
90214ab2924SLikun Gao 			break;
90314ab2924SLikun Gao 		case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
90414ab2924SLikun Gao 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
90514ab2924SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
90614ab2924SLikun Gao 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
90714ab2924SLikun Gao 			break;
90814ab2924SLikun Gao 		case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
90914ab2924SLikun Gao 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
91014ab2924SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
91114ab2924SLikun Gao 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
91214ab2924SLikun Gao 			break;
91314ab2924SLikun Gao 		case AMDGPU_UCODE_ID_CP_RS64_ME:
91414ab2924SLikun Gao 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->ucode_size_bytes);
91514ab2924SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
91614ab2924SLikun Gao 				le32_to_cpu(header->ucode_array_offset_bytes);
91714ab2924SLikun Gao 			break;
91814ab2924SLikun Gao 		case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
91914ab2924SLikun Gao 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
92014ab2924SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
92114ab2924SLikun Gao 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
92214ab2924SLikun Gao 			break;
92314ab2924SLikun Gao 		case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
92414ab2924SLikun Gao 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
92514ab2924SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
92614ab2924SLikun Gao 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
92714ab2924SLikun Gao 			break;
92814ab2924SLikun Gao 		case AMDGPU_UCODE_ID_CP_RS64_MEC:
92914ab2924SLikun Gao 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->ucode_size_bytes);
93014ab2924SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
93114ab2924SLikun Gao 				le32_to_cpu(header->ucode_array_offset_bytes);
93214ab2924SLikun Gao 			break;
93314ab2924SLikun Gao 		case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
93414ab2924SLikun Gao 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
93514ab2924SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
93614ab2924SLikun Gao 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
93714ab2924SLikun Gao 			break;
93814ab2924SLikun Gao 		case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
93914ab2924SLikun Gao 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
94014ab2924SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
94114ab2924SLikun Gao 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
94214ab2924SLikun Gao 			break;
94314ab2924SLikun Gao 		case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
94414ab2924SLikun Gao 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
94514ab2924SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
94614ab2924SLikun Gao 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
94714ab2924SLikun Gao 			break;
94814ab2924SLikun Gao 		case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
94914ab2924SLikun Gao 			ucode->ucode_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
95014ab2924SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
95114ab2924SLikun Gao 				le32_to_cpu(cpv2_hdr->data_offset_bytes);
95214ab2924SLikun Gao 			break;
95302f958a2SLikun Gao 		default:
95402f958a2SLikun Gao 			ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes);
95502f958a2SLikun Gao 			ucode_addr = (u8 *)ucode->fw->data +
95602f958a2SLikun Gao 				le32_to_cpu(header->ucode_array_offset_bytes);
95702f958a2SLikun Gao 			break;
9582445b227SHuang Rui 		}
95902f958a2SLikun Gao 	} else {
96002f958a2SLikun Gao 		ucode->ucode_size = le32_to_cpu(header->ucode_size_bytes);
96102f958a2SLikun Gao 		ucode_addr = (u8 *)ucode->fw->data +
96202f958a2SLikun Gao 			le32_to_cpu(header->ucode_array_offset_bytes);
96302f958a2SLikun Gao 	}
96402f958a2SLikun Gao 
96502f958a2SLikun Gao 	memcpy(ucode->kaddr, ucode_addr, ucode->ucode_size);
966d38ceaf9SAlex Deucher 
967d38ceaf9SAlex Deucher 	return 0;
968d38ceaf9SAlex Deucher }
969d38ceaf9SAlex Deucher 
amdgpu_ucode_patch_jt(struct amdgpu_firmware_info * ucode,uint64_t mc_addr,void * kptr)9704c2b2453SMonk Liu static int amdgpu_ucode_patch_jt(struct amdgpu_firmware_info *ucode,
9714c2b2453SMonk Liu 				uint64_t mc_addr, void *kptr)
9724c2b2453SMonk Liu {
9734c2b2453SMonk Liu 	const struct gfx_firmware_header_v1_0 *header = NULL;
9744c2b2453SMonk Liu 	const struct common_firmware_header *comm_hdr = NULL;
9754c2b2453SMonk Liu 	uint8_t *src_addr = NULL;
9764c2b2453SMonk Liu 	uint8_t *dst_addr = NULL;
9774c2b2453SMonk Liu 
978e03f04b8SSrinivasan Shanmugam 	if (!ucode->fw)
9794c2b2453SMonk Liu 		return 0;
9804c2b2453SMonk Liu 
9814c2b2453SMonk Liu 	comm_hdr = (const struct common_firmware_header *)ucode->fw->data;
9824c2b2453SMonk Liu 	header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
9834c2b2453SMonk Liu 	dst_addr = ucode->kaddr +
9844c2b2453SMonk Liu 			   ALIGN(le32_to_cpu(comm_hdr->ucode_size_bytes),
9854c2b2453SMonk Liu 			   PAGE_SIZE);
9864c2b2453SMonk Liu 	src_addr = (uint8_t *)ucode->fw->data +
9874c2b2453SMonk Liu 			   le32_to_cpu(comm_hdr->ucode_array_offset_bytes) +
9884c2b2453SMonk Liu 			   (le32_to_cpu(header->jt_offset) * 4);
9894c2b2453SMonk Liu 	memcpy(dst_addr, src_addr, le32_to_cpu(header->jt_size) * 4);
9904c2b2453SMonk Liu 
9914c2b2453SMonk Liu 	return 0;
9924c2b2453SMonk Liu }
9934c2b2453SMonk Liu 
amdgpu_ucode_create_bo(struct amdgpu_device * adev)994c8963ea4SRex Zhu int amdgpu_ucode_create_bo(struct amdgpu_device *adev)
995d38ceaf9SAlex Deucher {
996c8963ea4SRex Zhu 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_DIRECT) {
997c8963ea4SRex Zhu 		amdgpu_bo_create_kernel(adev, adev->firmware.fw_size, PAGE_SIZE,
998f501a7e5SFrank Min 			amdgpu_sriov_vf(adev) ? AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
999a95b0275SMonk Liu 			&adev->firmware.fw_buf,
1000a95b0275SMonk Liu 			&adev->firmware.fw_buf_mc,
1001a95b0275SMonk Liu 			&adev->firmware.fw_buf_ptr);
1002c8963ea4SRex Zhu 		if (!adev->firmware.fw_buf) {
1003a95b0275SMonk Liu 			dev_err(adev->dev, "failed to create kernel buffer for firmware.fw_buf\n");
1004c8963ea4SRex Zhu 			return -ENOMEM;
1005c8963ea4SRex Zhu 		} else if (amdgpu_sriov_vf(adev)) {
1006d59c026bSMonk Liu 			memset(adev->firmware.fw_buf_ptr, 0, adev->firmware.fw_size);
1007c8963ea4SRex Zhu 		}
1008c8963ea4SRex Zhu 	}
1009c8963ea4SRex Zhu 	return 0;
1010c8963ea4SRex Zhu }
10112445b227SHuang Rui 
amdgpu_ucode_free_bo(struct amdgpu_device * adev)1012c8963ea4SRex Zhu void amdgpu_ucode_free_bo(struct amdgpu_device *adev)
1013c8963ea4SRex Zhu {
1014c8963ea4SRex Zhu 	amdgpu_bo_free_kernel(&adev->firmware.fw_buf,
1015c8963ea4SRex Zhu 		&adev->firmware.fw_buf_mc,
1016c8963ea4SRex Zhu 		&adev->firmware.fw_buf_ptr);
1017c8963ea4SRex Zhu }
1018c8963ea4SRex Zhu 
amdgpu_ucode_init_bo(struct amdgpu_device * adev)1019c8963ea4SRex Zhu int amdgpu_ucode_init_bo(struct amdgpu_device *adev)
1020c8963ea4SRex Zhu {
1021c8963ea4SRex Zhu 	uint64_t fw_offset = 0;
1022c8963ea4SRex Zhu 	int i;
1023c8963ea4SRex Zhu 	struct amdgpu_firmware_info *ucode = NULL;
1024c8963ea4SRex Zhu 
1025c8963ea4SRex Zhu  /* for baremetal, the ucode is allocated in gtt, so don't need to fill the bo when reset/suspend */
102653b3f8f4SDennis Li 	if (!amdgpu_sriov_vf(adev) && (amdgpu_in_reset(adev) || adev->in_suspend))
1027c8963ea4SRex Zhu 		return 0;
1028e635ee07SHuang Rui 	/*
1029e635ee07SHuang Rui 	 * if SMU loaded firmware, it needn't add SMC, UVD, and VCE
1030e635ee07SHuang Rui 	 * ucode info here
1031e635ee07SHuang Rui 	 */
1032bc108ec7STrigger Huang 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1033bc108ec7STrigger Huang 		if (amdgpu_sriov_vf(adev))
1034bc108ec7STrigger Huang 			adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM - 3;
1035e635ee07SHuang Rui 		else
1036bc108ec7STrigger Huang 			adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM - 4;
1037bc108ec7STrigger Huang 	} else {
10382445b227SHuang Rui 		adev->firmware.max_ucodes = AMDGPU_UCODE_ID_MAXIMUM;
1039bc108ec7STrigger Huang 	}
1040e635ee07SHuang Rui 
10412445b227SHuang Rui 	for (i = 0; i < adev->firmware.max_ucodes; i++) {
1042d38ceaf9SAlex Deucher 		ucode = &adev->firmware.ucode[i];
1043d38ceaf9SAlex Deucher 		if (ucode->fw) {
1044d59c026bSMonk Liu 			amdgpu_ucode_init_single_fw(adev, ucode, adev->firmware.fw_buf_mc + fw_offset,
1045d59c026bSMonk Liu 						    adev->firmware.fw_buf_ptr + fw_offset);
10462445b227SHuang Rui 			if (i == AMDGPU_UCODE_ID_CP_MEC1 &&
10472445b227SHuang Rui 			    adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
10484c2b2453SMonk Liu 				const struct gfx_firmware_header_v1_0 *cp_hdr;
1049e03f04b8SSrinivasan Shanmugam 
10504c2b2453SMonk Liu 				cp_hdr = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
1051d59c026bSMonk Liu 				amdgpu_ucode_patch_jt(ucode,  adev->firmware.fw_buf_mc + fw_offset,
1052d59c026bSMonk Liu 						    adev->firmware.fw_buf_ptr + fw_offset);
10534c2b2453SMonk Liu 				fw_offset += ALIGN(le32_to_cpu(cp_hdr->jt_size) << 2, PAGE_SIZE);
10544c2b2453SMonk Liu 			}
10552445b227SHuang Rui 			fw_offset += ALIGN(ucode->ucode_size, PAGE_SIZE);
1056d38ceaf9SAlex Deucher 		}
1057d38ceaf9SAlex Deucher 	}
1058fd506558SHuang Rui 	return 0;
1059d38ceaf9SAlex Deucher }
10601d5eee7dSLikun Gao 
amdgpu_ucode_legacy_naming(struct amdgpu_device * adev,int block_type)106154a3e032SMario Limonciello static const char *amdgpu_ucode_legacy_naming(struct amdgpu_device *adev, int block_type)
106254a3e032SMario Limonciello {
106354a3e032SMario Limonciello 	if (block_type == MP0_HWIP) {
106454a3e032SMario Limonciello 		switch (adev->ip_versions[MP0_HWIP][0]) {
106554a3e032SMario Limonciello 		case IP_VERSION(9, 0, 0):
106654a3e032SMario Limonciello 			switch (adev->asic_type) {
106754a3e032SMario Limonciello 			case CHIP_VEGA10:
106854a3e032SMario Limonciello 				return "vega10";
106954a3e032SMario Limonciello 			case CHIP_VEGA12:
107054a3e032SMario Limonciello 				return "vega12";
107154a3e032SMario Limonciello 			default:
107254a3e032SMario Limonciello 				return NULL;
107354a3e032SMario Limonciello 			}
107454a3e032SMario Limonciello 		case IP_VERSION(10, 0, 0):
107554a3e032SMario Limonciello 		case IP_VERSION(10, 0, 1):
107654a3e032SMario Limonciello 			if (adev->asic_type == CHIP_RAVEN) {
107754a3e032SMario Limonciello 				if (adev->apu_flags & AMD_APU_IS_RAVEN2)
107854a3e032SMario Limonciello 					return "raven2";
107954a3e032SMario Limonciello 				else if (adev->apu_flags & AMD_APU_IS_PICASSO)
108054a3e032SMario Limonciello 					return "picasso";
108154a3e032SMario Limonciello 				return "raven";
108254a3e032SMario Limonciello 			}
108354a3e032SMario Limonciello 			break;
108454a3e032SMario Limonciello 		case IP_VERSION(11, 0, 0):
108554a3e032SMario Limonciello 			return "navi10";
108654a3e032SMario Limonciello 		case IP_VERSION(11, 0, 2):
108754a3e032SMario Limonciello 			return "vega20";
10880604897bSMario Limonciello 		case IP_VERSION(11, 0, 3):
10890604897bSMario Limonciello 			return "renoir";
109054a3e032SMario Limonciello 		case IP_VERSION(11, 0, 4):
109154a3e032SMario Limonciello 			return "arcturus";
109254a3e032SMario Limonciello 		case IP_VERSION(11, 0, 5):
109354a3e032SMario Limonciello 			return "navi14";
109454a3e032SMario Limonciello 		case IP_VERSION(11, 0, 7):
109554a3e032SMario Limonciello 			return "sienna_cichlid";
109654a3e032SMario Limonciello 		case IP_VERSION(11, 0, 9):
109754a3e032SMario Limonciello 			return "navi12";
109854a3e032SMario Limonciello 		case IP_VERSION(11, 0, 11):
109954a3e032SMario Limonciello 			return "navy_flounder";
110054a3e032SMario Limonciello 		case IP_VERSION(11, 0, 12):
110154a3e032SMario Limonciello 			return "dimgrey_cavefish";
110254a3e032SMario Limonciello 		case IP_VERSION(11, 0, 13):
110354a3e032SMario Limonciello 			return "beige_goby";
110454a3e032SMario Limonciello 		case IP_VERSION(11, 5, 0):
110554a3e032SMario Limonciello 			return "vangogh";
110654a3e032SMario Limonciello 		case IP_VERSION(12, 0, 1):
110754a3e032SMario Limonciello 			return "green_sardine";
110854a3e032SMario Limonciello 		case IP_VERSION(13, 0, 2):
110954a3e032SMario Limonciello 			return "aldebaran";
111054a3e032SMario Limonciello 		case IP_VERSION(13, 0, 1):
111154a3e032SMario Limonciello 		case IP_VERSION(13, 0, 3):
111254a3e032SMario Limonciello 			return "yellow_carp";
111354a3e032SMario Limonciello 		}
111454a3e032SMario Limonciello 	} else if (block_type == MP1_HWIP) {
111554a3e032SMario Limonciello 		switch (adev->ip_versions[MP1_HWIP][0]) {
111654a3e032SMario Limonciello 		case IP_VERSION(9, 0, 0):
111754a3e032SMario Limonciello 		case IP_VERSION(10, 0, 0):
111854a3e032SMario Limonciello 		case IP_VERSION(10, 0, 1):
111954a3e032SMario Limonciello 		case IP_VERSION(11, 0, 2):
112054a3e032SMario Limonciello 			if (adev->asic_type == CHIP_ARCTURUS)
112154a3e032SMario Limonciello 				return "arcturus_smc";
112254a3e032SMario Limonciello 			return NULL;
112354a3e032SMario Limonciello 		case IP_VERSION(11, 0, 0):
112454a3e032SMario Limonciello 			return "navi10_smc";
112554a3e032SMario Limonciello 		case IP_VERSION(11, 0, 5):
112654a3e032SMario Limonciello 			return "navi14_smc";
112754a3e032SMario Limonciello 		case IP_VERSION(11, 0, 9):
112854a3e032SMario Limonciello 			return "navi12_smc";
112954a3e032SMario Limonciello 		case IP_VERSION(11, 0, 7):
113054a3e032SMario Limonciello 			return "sienna_cichlid_smc";
113154a3e032SMario Limonciello 		case IP_VERSION(11, 0, 11):
113254a3e032SMario Limonciello 			return "navy_flounder_smc";
113354a3e032SMario Limonciello 		case IP_VERSION(11, 0, 12):
113454a3e032SMario Limonciello 			return "dimgrey_cavefish_smc";
113554a3e032SMario Limonciello 		case IP_VERSION(11, 0, 13):
113654a3e032SMario Limonciello 			return "beige_goby_smc";
113754a3e032SMario Limonciello 		case IP_VERSION(13, 0, 2):
113854a3e032SMario Limonciello 			return "aldebaran_smc";
113954a3e032SMario Limonciello 		}
114054a3e032SMario Limonciello 	} else if (block_type == SDMA0_HWIP) {
114154a3e032SMario Limonciello 		switch (adev->ip_versions[SDMA0_HWIP][0]) {
114254a3e032SMario Limonciello 		case IP_VERSION(4, 0, 0):
114354a3e032SMario Limonciello 			return "vega10_sdma";
114454a3e032SMario Limonciello 		case IP_VERSION(4, 0, 1):
114554a3e032SMario Limonciello 			return "vega12_sdma";
114654a3e032SMario Limonciello 		case IP_VERSION(4, 1, 0):
114754a3e032SMario Limonciello 		case IP_VERSION(4, 1, 1):
114854a3e032SMario Limonciello 			if (adev->apu_flags & AMD_APU_IS_RAVEN2)
114954a3e032SMario Limonciello 				return "raven2_sdma";
115054a3e032SMario Limonciello 			else if (adev->apu_flags & AMD_APU_IS_PICASSO)
115154a3e032SMario Limonciello 				return "picasso_sdma";
115254a3e032SMario Limonciello 			return "raven_sdma";
115354a3e032SMario Limonciello 		case IP_VERSION(4, 1, 2):
115454a3e032SMario Limonciello 			if (adev->apu_flags & AMD_APU_IS_RENOIR)
115554a3e032SMario Limonciello 				return "renoir_sdma";
115654a3e032SMario Limonciello 			return "green_sardine_sdma";
115754a3e032SMario Limonciello 		case IP_VERSION(4, 2, 0):
115854a3e032SMario Limonciello 			return "vega20_sdma";
115954a3e032SMario Limonciello 		case IP_VERSION(4, 2, 2):
116054a3e032SMario Limonciello 			return "arcturus_sdma";
116154a3e032SMario Limonciello 		case IP_VERSION(4, 4, 0):
116254a3e032SMario Limonciello 			return "aldebaran_sdma";
116354a3e032SMario Limonciello 		case IP_VERSION(5, 0, 0):
116454a3e032SMario Limonciello 			return "navi10_sdma";
116554a3e032SMario Limonciello 		case IP_VERSION(5, 0, 1):
116654a3e032SMario Limonciello 			return "cyan_skillfish2_sdma";
116754a3e032SMario Limonciello 		case IP_VERSION(5, 0, 2):
116854a3e032SMario Limonciello 			return "navi14_sdma";
116954a3e032SMario Limonciello 		case IP_VERSION(5, 0, 5):
117054a3e032SMario Limonciello 			return "navi12_sdma";
117154a3e032SMario Limonciello 		case IP_VERSION(5, 2, 0):
117254a3e032SMario Limonciello 			return "sienna_cichlid_sdma";
117354a3e032SMario Limonciello 		case IP_VERSION(5, 2, 2):
117454a3e032SMario Limonciello 			return "navy_flounder_sdma";
117554a3e032SMario Limonciello 		case IP_VERSION(5, 2, 4):
117654a3e032SMario Limonciello 			return "dimgrey_cavefish_sdma";
117754a3e032SMario Limonciello 		case IP_VERSION(5, 2, 5):
117854a3e032SMario Limonciello 			return "beige_goby_sdma";
117954a3e032SMario Limonciello 		case IP_VERSION(5, 2, 3):
118054a3e032SMario Limonciello 			return "yellow_carp_sdma";
118154a3e032SMario Limonciello 		case IP_VERSION(5, 2, 1):
118254a3e032SMario Limonciello 			return "vangogh_sdma";
118354a3e032SMario Limonciello 		}
118454a3e032SMario Limonciello 	} else if (block_type == UVD_HWIP) {
118554a3e032SMario Limonciello 		switch (adev->ip_versions[UVD_HWIP][0]) {
118654a3e032SMario Limonciello 		case IP_VERSION(1, 0, 0):
118754a3e032SMario Limonciello 		case IP_VERSION(1, 0, 1):
118854a3e032SMario Limonciello 			if (adev->apu_flags & AMD_APU_IS_RAVEN2)
118954a3e032SMario Limonciello 				return "raven2_vcn";
119054a3e032SMario Limonciello 			else if (adev->apu_flags & AMD_APU_IS_PICASSO)
119154a3e032SMario Limonciello 				return "picasso_vcn";
119254a3e032SMario Limonciello 			return "raven_vcn";
119354a3e032SMario Limonciello 		case IP_VERSION(2, 5, 0):
119454a3e032SMario Limonciello 			return "arcturus_vcn";
119554a3e032SMario Limonciello 		case IP_VERSION(2, 2, 0):
119654a3e032SMario Limonciello 			if (adev->apu_flags & AMD_APU_IS_RENOIR)
119754a3e032SMario Limonciello 				return "renoir_vcn";
119854a3e032SMario Limonciello 			return "green_sardine_vcn";
119954a3e032SMario Limonciello 		case IP_VERSION(2, 6, 0):
120054a3e032SMario Limonciello 			return "aldebaran_vcn";
120154a3e032SMario Limonciello 		case IP_VERSION(2, 0, 0):
120254a3e032SMario Limonciello 			return "navi10_vcn";
120354a3e032SMario Limonciello 		case IP_VERSION(2, 0, 2):
120454a3e032SMario Limonciello 			if (adev->asic_type == CHIP_NAVI12)
120554a3e032SMario Limonciello 				return "navi12_vcn";
120654a3e032SMario Limonciello 			return "navi14_vcn";
120754a3e032SMario Limonciello 		case IP_VERSION(3, 0, 0):
120854a3e032SMario Limonciello 		case IP_VERSION(3, 0, 64):
120954a3e032SMario Limonciello 		case IP_VERSION(3, 0, 192):
121054a3e032SMario Limonciello 			if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(10, 3, 0))
121154a3e032SMario Limonciello 				return "sienna_cichlid_vcn";
121254a3e032SMario Limonciello 			return "navy_flounder_vcn";
121354a3e032SMario Limonciello 		case IP_VERSION(3, 0, 2):
121454a3e032SMario Limonciello 			return "vangogh_vcn";
121554a3e032SMario Limonciello 		case IP_VERSION(3, 0, 16):
121654a3e032SMario Limonciello 			return "dimgrey_cavefish_vcn";
121754a3e032SMario Limonciello 		case IP_VERSION(3, 0, 33):
121854a3e032SMario Limonciello 			return "beige_goby_vcn";
121954a3e032SMario Limonciello 		case IP_VERSION(3, 1, 1):
122054a3e032SMario Limonciello 			return "yellow_carp_vcn";
122154a3e032SMario Limonciello 		}
122254a3e032SMario Limonciello 	} else if (block_type == GC_HWIP) {
122354a3e032SMario Limonciello 		switch (adev->ip_versions[GC_HWIP][0]) {
122454a3e032SMario Limonciello 		case IP_VERSION(9, 0, 1):
122554a3e032SMario Limonciello 			return "vega10";
122654a3e032SMario Limonciello 		case IP_VERSION(9, 2, 1):
122754a3e032SMario Limonciello 			return "vega12";
122854a3e032SMario Limonciello 		case IP_VERSION(9, 4, 0):
122954a3e032SMario Limonciello 			return "vega20";
123054a3e032SMario Limonciello 		case IP_VERSION(9, 2, 2):
123154a3e032SMario Limonciello 		case IP_VERSION(9, 1, 0):
123254a3e032SMario Limonciello 			if (adev->apu_flags & AMD_APU_IS_RAVEN2)
123354a3e032SMario Limonciello 				return "raven2";
123454a3e032SMario Limonciello 			else if (adev->apu_flags & AMD_APU_IS_PICASSO)
123554a3e032SMario Limonciello 				return "picasso";
123654a3e032SMario Limonciello 			return "raven";
123754a3e032SMario Limonciello 		case IP_VERSION(9, 4, 1):
123854a3e032SMario Limonciello 			return "arcturus";
123954a3e032SMario Limonciello 		case IP_VERSION(9, 3, 0):
124054a3e032SMario Limonciello 			if (adev->apu_flags & AMD_APU_IS_RENOIR)
124154a3e032SMario Limonciello 				return "renoir";
124254a3e032SMario Limonciello 			return "green_sardine";
124354a3e032SMario Limonciello 		case IP_VERSION(9, 4, 2):
124454a3e032SMario Limonciello 			return "aldebaran";
124554a3e032SMario Limonciello 		case IP_VERSION(10, 1, 10):
124654a3e032SMario Limonciello 			return "navi10";
124754a3e032SMario Limonciello 		case IP_VERSION(10, 1, 1):
124854a3e032SMario Limonciello 			return "navi14";
124954a3e032SMario Limonciello 		case IP_VERSION(10, 1, 2):
125054a3e032SMario Limonciello 			return "navi12";
125154a3e032SMario Limonciello 		case IP_VERSION(10, 3, 0):
125254a3e032SMario Limonciello 			return "sienna_cichlid";
125354a3e032SMario Limonciello 		case IP_VERSION(10, 3, 2):
125454a3e032SMario Limonciello 			return "navy_flounder";
125554a3e032SMario Limonciello 		case IP_VERSION(10, 3, 1):
125654a3e032SMario Limonciello 			return "vangogh";
125754a3e032SMario Limonciello 		case IP_VERSION(10, 3, 4):
125854a3e032SMario Limonciello 			return "dimgrey_cavefish";
125954a3e032SMario Limonciello 		case IP_VERSION(10, 3, 5):
126054a3e032SMario Limonciello 			return "beige_goby";
126154a3e032SMario Limonciello 		case IP_VERSION(10, 3, 3):
126254a3e032SMario Limonciello 			return "yellow_carp";
126354a3e032SMario Limonciello 		case IP_VERSION(10, 1, 3):
126454a3e032SMario Limonciello 		case IP_VERSION(10, 1, 4):
126554a3e032SMario Limonciello 			return "cyan_skillfish2";
126654a3e032SMario Limonciello 		}
126754a3e032SMario Limonciello 	}
126854a3e032SMario Limonciello 	return NULL;
126954a3e032SMario Limonciello }
127054a3e032SMario Limonciello 
amdgpu_ucode_ip_version_decode(struct amdgpu_device * adev,int block_type,char * ucode_prefix,int len)12711d5eee7dSLikun Gao void amdgpu_ucode_ip_version_decode(struct amdgpu_device *adev, int block_type, char *ucode_prefix, int len)
12721d5eee7dSLikun Gao {
12731d5eee7dSLikun Gao 	int maj, min, rev;
12741d5eee7dSLikun Gao 	char *ip_name;
127554a3e032SMario Limonciello 	const char *legacy;
12761d5eee7dSLikun Gao 	uint32_t version = adev->ip_versions[block_type][0];
12771d5eee7dSLikun Gao 
127854a3e032SMario Limonciello 	legacy = amdgpu_ucode_legacy_naming(adev, block_type);
127954a3e032SMario Limonciello 	if (legacy) {
128054a3e032SMario Limonciello 		snprintf(ucode_prefix, len, "%s", legacy);
128154a3e032SMario Limonciello 		return;
128254a3e032SMario Limonciello 	}
128354a3e032SMario Limonciello 
12841d5eee7dSLikun Gao 	switch (block_type) {
12851d5eee7dSLikun Gao 	case GC_HWIP:
12861d5eee7dSLikun Gao 		ip_name = "gc";
12871d5eee7dSLikun Gao 		break;
12881d5eee7dSLikun Gao 	case SDMA0_HWIP:
12891d5eee7dSLikun Gao 		ip_name = "sdma";
12901d5eee7dSLikun Gao 		break;
12911d5eee7dSLikun Gao 	case MP0_HWIP:
12921d5eee7dSLikun Gao 		ip_name = "psp";
12931d5eee7dSLikun Gao 		break;
12941d5eee7dSLikun Gao 	case MP1_HWIP:
12951d5eee7dSLikun Gao 		ip_name = "smu";
12961d5eee7dSLikun Gao 		break;
12971d5eee7dSLikun Gao 	case UVD_HWIP:
12981d5eee7dSLikun Gao 		ip_name = "vcn";
12991d5eee7dSLikun Gao 		break;
13001d5eee7dSLikun Gao 	default:
13011d5eee7dSLikun Gao 		BUG();
13021d5eee7dSLikun Gao 	}
13031d5eee7dSLikun Gao 
13041d5eee7dSLikun Gao 	maj = IP_VERSION_MAJ(version);
13051d5eee7dSLikun Gao 	min = IP_VERSION_MIN(version);
13061d5eee7dSLikun Gao 	rev = IP_VERSION_REV(version);
13071d5eee7dSLikun Gao 
13081d5eee7dSLikun Gao 	snprintf(ucode_prefix, len, "%s_%d_%d_%d", ip_name, maj, min, rev);
13091d5eee7dSLikun Gao }
13102210af50SMario Limonciello 
13112210af50SMario Limonciello /*
13122210af50SMario Limonciello  * amdgpu_ucode_request - Fetch and validate amdgpu microcode
13132210af50SMario Limonciello  *
13142210af50SMario Limonciello  * @adev: amdgpu device
13152210af50SMario Limonciello  * @fw: pointer to load firmware to
13162210af50SMario Limonciello  * @fw_name: firmware to load
13172210af50SMario Limonciello  *
13182210af50SMario Limonciello  * This is a helper that will use request_firmware and amdgpu_ucode_validate
13192210af50SMario Limonciello  * to load and run basic validation on firmware. If the load fails, remap
13202210af50SMario Limonciello  * the error code to -ENODEV, so that early_init functions will fail to load.
13212210af50SMario Limonciello  */
amdgpu_ucode_request(struct amdgpu_device * adev,const struct firmware ** fw,const char * fw_name)13222210af50SMario Limonciello int amdgpu_ucode_request(struct amdgpu_device *adev, const struct firmware **fw,
13232210af50SMario Limonciello 			 const char *fw_name)
13242210af50SMario Limonciello {
13252210af50SMario Limonciello 	int err = request_firmware(fw, fw_name, adev->dev);
13262210af50SMario Limonciello 
13272210af50SMario Limonciello 	if (err)
13282210af50SMario Limonciello 		return -ENODEV;
1329*aa1791b2SSrinivasan Shanmugam 
13302210af50SMario Limonciello 	err = amdgpu_ucode_validate(*fw);
1331*aa1791b2SSrinivasan Shanmugam 	if (err) {
13322210af50SMario Limonciello 		dev_dbg(adev->dev, "\"%s\" failed to validate\n", fw_name);
1333*aa1791b2SSrinivasan Shanmugam 		release_firmware(*fw);
1334*aa1791b2SSrinivasan Shanmugam 		*fw = NULL;
1335*aa1791b2SSrinivasan Shanmugam 	}
13362210af50SMario Limonciello 
13372210af50SMario Limonciello 	return err;
13382210af50SMario Limonciello }
13392210af50SMario Limonciello 
13402210af50SMario Limonciello /*
13412210af50SMario Limonciello  * amdgpu_ucode_release - Release firmware microcode
13422210af50SMario Limonciello  *
13432210af50SMario Limonciello  * @fw: pointer to firmware to release
13442210af50SMario Limonciello  */
amdgpu_ucode_release(const struct firmware ** fw)13452210af50SMario Limonciello void amdgpu_ucode_release(const struct firmware **fw)
13462210af50SMario Limonciello {
13472210af50SMario Limonciello 	release_firmware(*fw);
13482210af50SMario Limonciello 	*fw = NULL;
13492210af50SMario Limonciello }
1350