1 /*
2  * Copyright 2012 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 #ifndef __AMDGPU_UCODE_H__
24 #define __AMDGPU_UCODE_H__
25 
26 #include "amdgpu_socbb.h"
27 
28 struct common_firmware_header {
29 	uint32_t size_bytes; /* size of the entire header+image(s) in bytes */
30 	uint32_t header_size_bytes; /* size of just the header in bytes */
31 	uint16_t header_version_major; /* header version */
32 	uint16_t header_version_minor; /* header version */
33 	uint16_t ip_version_major; /* IP version */
34 	uint16_t ip_version_minor; /* IP version */
35 	uint32_t ucode_version;
36 	uint32_t ucode_size_bytes; /* size of ucode in bytes */
37 	uint32_t ucode_array_offset_bytes; /* payload offset from the start of the header */
38 	uint32_t crc32;  /* crc32 checksum of the payload */
39 };
40 
41 /* version_major=1, version_minor=0 */
42 struct mc_firmware_header_v1_0 {
43 	struct common_firmware_header header;
44 	uint32_t io_debug_size_bytes; /* size of debug array in dwords */
45 	uint32_t io_debug_array_offset_bytes; /* payload offset from the start of the header */
46 };
47 
48 /* version_major=1, version_minor=0 */
49 struct smc_firmware_header_v1_0 {
50 	struct common_firmware_header header;
51 	uint32_t ucode_start_addr;
52 };
53 
54 /* version_major=2, version_minor=0 */
55 struct smc_firmware_header_v2_0 {
56 	struct smc_firmware_header_v1_0 v1_0;
57 	uint32_t ppt_offset_bytes; /* soft pptable offset */
58 	uint32_t ppt_size_bytes; /* soft pptable size */
59 };
60 
61 struct smc_soft_pptable_entry {
62         uint32_t id;
63         uint32_t ppt_offset_bytes;
64         uint32_t ppt_size_bytes;
65 };
66 
67 /* version_major=2, version_minor=1 */
68 struct smc_firmware_header_v2_1 {
69         struct smc_firmware_header_v1_0 v1_0;
70         uint32_t pptable_count;
71         uint32_t pptable_entry_offset;
72 };
73 
74 /* version_major=1, version_minor=0 */
75 struct psp_firmware_header_v1_0 {
76 	struct common_firmware_header header;
77 	uint32_t ucode_feature_version;
78 	uint32_t sos_offset_bytes;
79 	uint32_t sos_size_bytes;
80 };
81 
82 /* version_major=1, version_minor=1 */
83 struct psp_firmware_header_v1_1 {
84 	struct psp_firmware_header_v1_0 v1_0;
85 	uint32_t toc_header_version;
86 	uint32_t toc_offset_bytes;
87 	uint32_t toc_size_bytes;
88 	uint32_t kdb_header_version;
89 	uint32_t kdb_offset_bytes;
90 	uint32_t kdb_size_bytes;
91 };
92 
93 /* version_major=1, version_minor=2 */
94 struct psp_firmware_header_v1_2 {
95 	struct psp_firmware_header_v1_0 v1_0;
96 	uint32_t reserve[3];
97 	uint32_t kdb_header_version;
98 	uint32_t kdb_offset_bytes;
99 	uint32_t kdb_size_bytes;
100 };
101 
102 /* version_major=1, version_minor=3 */
103 struct psp_firmware_header_v1_3 {
104 	struct psp_firmware_header_v1_1 v1_1;
105 	uint32_t spl_header_version;
106 	uint32_t spl_offset_bytes;
107 	uint32_t spl_size_bytes;
108 	uint32_t rl_header_version;
109 	uint32_t rl_offset_bytes;
110 	uint32_t rl_size_bytes;
111 };
112 
113 /* version_major=1, version_minor=0 */
114 struct ta_firmware_header_v1_0 {
115 	struct common_firmware_header header;
116 	uint32_t ta_xgmi_ucode_version;
117 	uint32_t ta_xgmi_offset_bytes;
118 	uint32_t ta_xgmi_size_bytes;
119 	uint32_t ta_ras_ucode_version;
120 	uint32_t ta_ras_offset_bytes;
121 	uint32_t ta_ras_size_bytes;
122 	uint32_t ta_hdcp_ucode_version;
123 	uint32_t ta_hdcp_offset_bytes;
124 	uint32_t ta_hdcp_size_bytes;
125 	uint32_t ta_dtm_ucode_version;
126 	uint32_t ta_dtm_offset_bytes;
127 	uint32_t ta_dtm_size_bytes;
128 	uint32_t ta_securedisplay_ucode_version;
129 	uint32_t ta_securedisplay_offset_bytes;
130 	uint32_t ta_securedisplay_size_bytes;
131 };
132 
133 enum ta_fw_type {
134 	TA_FW_TYPE_UNKOWN,
135 	TA_FW_TYPE_PSP_ASD,
136 	TA_FW_TYPE_PSP_XGMI,
137 	TA_FW_TYPE_PSP_RAS,
138 	TA_FW_TYPE_PSP_HDCP,
139 	TA_FW_TYPE_PSP_DTM,
140 	TA_FW_TYPE_PSP_RAP,
141 	TA_FW_TYPE_PSP_SECUREDISPLAY,
142 	TA_FW_TYPE_MAX_INDEX,
143 };
144 
145 struct ta_fw_bin_desc {
146 	uint32_t fw_type;
147 	uint32_t fw_version;
148 	uint32_t offset_bytes;
149 	uint32_t size_bytes;
150 };
151 
152 /* version_major=2, version_minor=0 */
153 struct ta_firmware_header_v2_0 {
154 	struct common_firmware_header header;
155 	uint32_t ta_fw_bin_count;
156 	struct ta_fw_bin_desc ta_fw_bin[];
157 };
158 
159 /* version_major=1, version_minor=0 */
160 struct gfx_firmware_header_v1_0 {
161 	struct common_firmware_header header;
162 	uint32_t ucode_feature_version;
163 	uint32_t jt_offset; /* jt location */
164 	uint32_t jt_size;  /* size of jt */
165 };
166 
167 /* version_major=1, version_minor=0 */
168 struct mes_firmware_header_v1_0 {
169 	struct common_firmware_header header;
170 	uint32_t mes_ucode_version;
171 	uint32_t mes_ucode_size_bytes;
172 	uint32_t mes_ucode_offset_bytes;
173 	uint32_t mes_ucode_data_version;
174 	uint32_t mes_ucode_data_size_bytes;
175 	uint32_t mes_ucode_data_offset_bytes;
176 	uint32_t mes_uc_start_addr_lo;
177 	uint32_t mes_uc_start_addr_hi;
178 	uint32_t mes_data_start_addr_lo;
179 	uint32_t mes_data_start_addr_hi;
180 };
181 
182 /* version_major=1, version_minor=0 */
183 struct rlc_firmware_header_v1_0 {
184 	struct common_firmware_header header;
185 	uint32_t ucode_feature_version;
186 	uint32_t save_and_restore_offset;
187 	uint32_t clear_state_descriptor_offset;
188 	uint32_t avail_scratch_ram_locations;
189 	uint32_t master_pkt_description_offset;
190 };
191 
192 /* version_major=2, version_minor=0 */
193 struct rlc_firmware_header_v2_0 {
194 	struct common_firmware_header header;
195 	uint32_t ucode_feature_version;
196 	uint32_t jt_offset; /* jt location */
197 	uint32_t jt_size;  /* size of jt */
198 	uint32_t save_and_restore_offset;
199 	uint32_t clear_state_descriptor_offset;
200 	uint32_t avail_scratch_ram_locations;
201 	uint32_t reg_restore_list_size;
202 	uint32_t reg_list_format_start;
203 	uint32_t reg_list_format_separate_start;
204 	uint32_t starting_offsets_start;
205 	uint32_t reg_list_format_size_bytes; /* size of reg list format array in bytes */
206 	uint32_t reg_list_format_array_offset_bytes; /* payload offset from the start of the header */
207 	uint32_t reg_list_size_bytes; /* size of reg list array in bytes */
208 	uint32_t reg_list_array_offset_bytes; /* payload offset from the start of the header */
209 	uint32_t reg_list_format_separate_size_bytes; /* size of reg list format array in bytes */
210 	uint32_t reg_list_format_separate_array_offset_bytes; /* payload offset from the start of the header */
211 	uint32_t reg_list_separate_size_bytes; /* size of reg list array in bytes */
212 	uint32_t reg_list_separate_array_offset_bytes; /* payload offset from the start of the header */
213 };
214 
215 /* version_major=2, version_minor=1 */
216 struct rlc_firmware_header_v2_1 {
217 	struct rlc_firmware_header_v2_0 v2_0;
218 	uint32_t reg_list_format_direct_reg_list_length; /* length of direct reg list format array */
219 	uint32_t save_restore_list_cntl_ucode_ver;
220 	uint32_t save_restore_list_cntl_feature_ver;
221 	uint32_t save_restore_list_cntl_size_bytes;
222 	uint32_t save_restore_list_cntl_offset_bytes;
223 	uint32_t save_restore_list_gpm_ucode_ver;
224 	uint32_t save_restore_list_gpm_feature_ver;
225 	uint32_t save_restore_list_gpm_size_bytes;
226 	uint32_t save_restore_list_gpm_offset_bytes;
227 	uint32_t save_restore_list_srm_ucode_ver;
228 	uint32_t save_restore_list_srm_feature_ver;
229 	uint32_t save_restore_list_srm_size_bytes;
230 	uint32_t save_restore_list_srm_offset_bytes;
231 };
232 
233 /* version_major=2, version_minor=1 */
234 struct rlc_firmware_header_v2_2 {
235 	struct rlc_firmware_header_v2_1 v2_1;
236 	uint32_t rlc_iram_ucode_size_bytes;
237 	uint32_t rlc_iram_ucode_offset_bytes;
238 	uint32_t rlc_dram_ucode_size_bytes;
239 	uint32_t rlc_dram_ucode_offset_bytes;
240 };
241 
242 /* version_major=1, version_minor=0 */
243 struct sdma_firmware_header_v1_0 {
244 	struct common_firmware_header header;
245 	uint32_t ucode_feature_version;
246 	uint32_t ucode_change_version;
247 	uint32_t jt_offset; /* jt location */
248 	uint32_t jt_size; /* size of jt */
249 };
250 
251 /* version_major=1, version_minor=1 */
252 struct sdma_firmware_header_v1_1 {
253 	struct sdma_firmware_header_v1_0 v1_0;
254 	uint32_t digest_size;
255 };
256 
257 /* gpu info payload */
258 struct gpu_info_firmware_v1_0 {
259 	uint32_t gc_num_se;
260 	uint32_t gc_num_cu_per_sh;
261 	uint32_t gc_num_sh_per_se;
262 	uint32_t gc_num_rb_per_se;
263 	uint32_t gc_num_tccs;
264 	uint32_t gc_num_gprs;
265 	uint32_t gc_num_max_gs_thds;
266 	uint32_t gc_gs_table_depth;
267 	uint32_t gc_gsprim_buff_depth;
268 	uint32_t gc_parameter_cache_depth;
269 	uint32_t gc_double_offchip_lds_buffer;
270 	uint32_t gc_wave_size;
271 	uint32_t gc_max_waves_per_simd;
272 	uint32_t gc_max_scratch_slots_per_cu;
273 	uint32_t gc_lds_size;
274 };
275 
276 struct gpu_info_firmware_v1_1 {
277 	struct gpu_info_firmware_v1_0 v1_0;
278 	uint32_t num_sc_per_sh;
279 	uint32_t num_packer_per_sc;
280 };
281 
282 /* gpu info payload
283  * version_major=1, version_minor=1 */
284 struct gpu_info_firmware_v1_2 {
285 	struct gpu_info_firmware_v1_1 v1_1;
286 	struct gpu_info_soc_bounding_box_v1_0 soc_bounding_box;
287 };
288 
289 /* version_major=1, version_minor=0 */
290 struct gpu_info_firmware_header_v1_0 {
291 	struct common_firmware_header header;
292 	uint16_t version_major; /* version */
293 	uint16_t version_minor; /* version */
294 };
295 
296 /* version_major=1, version_minor=0 */
297 struct dmcu_firmware_header_v1_0 {
298 	struct common_firmware_header header;
299 	uint32_t intv_offset_bytes; /* interrupt vectors offset from end of header, in bytes */
300 	uint32_t intv_size_bytes;  /* size of interrupt vectors, in bytes */
301 };
302 
303 /* version_major=1, version_minor=0 */
304 struct dmcub_firmware_header_v1_0 {
305 	struct common_firmware_header header;
306 	uint32_t inst_const_bytes; /* size of instruction region, in bytes */
307 	uint32_t bss_data_bytes; /* size of bss/data region, in bytes */
308 };
309 
310 /* header is fixed size */
311 union amdgpu_firmware_header {
312 	struct common_firmware_header common;
313 	struct mc_firmware_header_v1_0 mc;
314 	struct smc_firmware_header_v1_0 smc;
315 	struct smc_firmware_header_v2_0 smc_v2_0;
316 	struct psp_firmware_header_v1_0 psp;
317 	struct psp_firmware_header_v1_1 psp_v1_1;
318 	struct psp_firmware_header_v1_3 psp_v1_3;
319 	struct ta_firmware_header_v1_0 ta;
320 	struct ta_firmware_header_v2_0 ta_v2_0;
321 	struct gfx_firmware_header_v1_0 gfx;
322 	struct rlc_firmware_header_v1_0 rlc;
323 	struct rlc_firmware_header_v2_0 rlc_v2_0;
324 	struct rlc_firmware_header_v2_1 rlc_v2_1;
325 	struct sdma_firmware_header_v1_0 sdma;
326 	struct sdma_firmware_header_v1_1 sdma_v1_1;
327 	struct gpu_info_firmware_header_v1_0 gpu_info;
328 	struct dmcu_firmware_header_v1_0 dmcu;
329 	struct dmcub_firmware_header_v1_0 dmcub;
330 	uint8_t raw[0x100];
331 };
332 
333 #define UCODE_MAX_TA_PACKAGING ((sizeof(union amdgpu_firmware_header) - sizeof(struct common_firmware_header) - 4) / sizeof(struct ta_fw_bin_desc))
334 
335 /*
336  * fw loading support
337  */
338 enum AMDGPU_UCODE_ID {
339 	AMDGPU_UCODE_ID_SDMA0 = 0,
340 	AMDGPU_UCODE_ID_SDMA1,
341 	AMDGPU_UCODE_ID_SDMA2,
342 	AMDGPU_UCODE_ID_SDMA3,
343 	AMDGPU_UCODE_ID_SDMA4,
344 	AMDGPU_UCODE_ID_SDMA5,
345 	AMDGPU_UCODE_ID_SDMA6,
346 	AMDGPU_UCODE_ID_SDMA7,
347 	AMDGPU_UCODE_ID_CP_CE,
348 	AMDGPU_UCODE_ID_CP_PFP,
349 	AMDGPU_UCODE_ID_CP_ME,
350 	AMDGPU_UCODE_ID_CP_MEC1,
351 	AMDGPU_UCODE_ID_CP_MEC1_JT,
352 	AMDGPU_UCODE_ID_CP_MEC2,
353 	AMDGPU_UCODE_ID_CP_MEC2_JT,
354 	AMDGPU_UCODE_ID_CP_MES,
355 	AMDGPU_UCODE_ID_CP_MES_DATA,
356 	AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL,
357 	AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM,
358 	AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM,
359 	AMDGPU_UCODE_ID_RLC_IRAM,
360 	AMDGPU_UCODE_ID_RLC_DRAM,
361 	AMDGPU_UCODE_ID_RLC_G,
362 	AMDGPU_UCODE_ID_STORAGE,
363 	AMDGPU_UCODE_ID_SMC,
364 	AMDGPU_UCODE_ID_UVD,
365 	AMDGPU_UCODE_ID_UVD1,
366 	AMDGPU_UCODE_ID_VCE,
367 	AMDGPU_UCODE_ID_VCN,
368 	AMDGPU_UCODE_ID_VCN1,
369 	AMDGPU_UCODE_ID_DMCU_ERAM,
370 	AMDGPU_UCODE_ID_DMCU_INTV,
371 	AMDGPU_UCODE_ID_VCN0_RAM,
372 	AMDGPU_UCODE_ID_VCN1_RAM,
373 	AMDGPU_UCODE_ID_DMCUB,
374 	AMDGPU_UCODE_ID_MAXIMUM,
375 };
376 
377 /* engine firmware status */
378 enum AMDGPU_UCODE_STATUS {
379 	AMDGPU_UCODE_STATUS_INVALID,
380 	AMDGPU_UCODE_STATUS_NOT_LOADED,
381 	AMDGPU_UCODE_STATUS_LOADED,
382 };
383 
384 enum amdgpu_firmware_load_type {
385 	AMDGPU_FW_LOAD_DIRECT = 0,
386 	AMDGPU_FW_LOAD_SMU,
387 	AMDGPU_FW_LOAD_PSP,
388 	AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO,
389 };
390 
391 /* conform to smu_ucode_xfer_cz.h */
392 #define AMDGPU_SDMA0_UCODE_LOADED	0x00000001
393 #define AMDGPU_SDMA1_UCODE_LOADED	0x00000002
394 #define AMDGPU_CPCE_UCODE_LOADED	0x00000004
395 #define AMDGPU_CPPFP_UCODE_LOADED	0x00000008
396 #define AMDGPU_CPME_UCODE_LOADED	0x00000010
397 #define AMDGPU_CPMEC1_UCODE_LOADED	0x00000020
398 #define AMDGPU_CPMEC2_UCODE_LOADED	0x00000040
399 #define AMDGPU_CPRLC_UCODE_LOADED	0x00000100
400 
401 /* amdgpu firmware info */
402 struct amdgpu_firmware_info {
403 	/* ucode ID */
404 	enum AMDGPU_UCODE_ID ucode_id;
405 	/* request_firmware */
406 	const struct firmware *fw;
407 	/* starting mc address */
408 	uint64_t mc_addr;
409 	/* kernel linear address */
410 	void *kaddr;
411 	/* ucode_size_bytes */
412 	uint32_t ucode_size;
413 	/* starting tmr mc address */
414 	uint32_t tmr_mc_addr_lo;
415 	uint32_t tmr_mc_addr_hi;
416 };
417 
418 struct amdgpu_firmware {
419 	struct amdgpu_firmware_info ucode[AMDGPU_UCODE_ID_MAXIMUM];
420 	enum amdgpu_firmware_load_type load_type;
421 	struct amdgpu_bo *fw_buf;
422 	unsigned int fw_size;
423 	unsigned int max_ucodes;
424 	/* firmwares are loaded by psp instead of smu from vega10 */
425 	const struct amdgpu_psp_funcs *funcs;
426 	struct amdgpu_bo *rbuf;
427 	struct mutex mutex;
428 
429 	/* gpu info firmware data pointer */
430 	const struct firmware *gpu_info_fw;
431 
432 	void *fw_buf_ptr;
433 	uint64_t fw_buf_mc;
434 };
435 
436 void amdgpu_ucode_print_mc_hdr(const struct common_firmware_header *hdr);
437 void amdgpu_ucode_print_smc_hdr(const struct common_firmware_header *hdr);
438 void amdgpu_ucode_print_gfx_hdr(const struct common_firmware_header *hdr);
439 void amdgpu_ucode_print_rlc_hdr(const struct common_firmware_header *hdr);
440 void amdgpu_ucode_print_sdma_hdr(const struct common_firmware_header *hdr);
441 void amdgpu_ucode_print_psp_hdr(const struct common_firmware_header *hdr);
442 void amdgpu_ucode_print_gpu_info_hdr(const struct common_firmware_header *hdr);
443 int amdgpu_ucode_validate(const struct firmware *fw);
444 bool amdgpu_ucode_hdr_version(union amdgpu_firmware_header *hdr,
445 				uint16_t hdr_major, uint16_t hdr_minor);
446 
447 int amdgpu_ucode_init_bo(struct amdgpu_device *adev);
448 int amdgpu_ucode_create_bo(struct amdgpu_device *adev);
449 int amdgpu_ucode_sysfs_init(struct amdgpu_device *adev);
450 void amdgpu_ucode_free_bo(struct amdgpu_device *adev);
451 void amdgpu_ucode_sysfs_fini(struct amdgpu_device *adev);
452 
453 enum amdgpu_firmware_load_type
454 amdgpu_ucode_get_load_type(struct amdgpu_device *adev, int load_type);
455 
456 #endif
457