2 * Copyright 2019 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 #include <linux/firmware.h>
24 #include <linux/module.h>
26 #include "amdgpu_psp.h"
27 #include "amdgpu_ucode.h"
28 #include "soc15_common.h"
29 #include "psp_v12_0.h"
31 #include "mp/mp_12_0_0_offset.h"
32 #include "mp/mp_12_0_0_sh_mask.h"
33 #include "gc/gc_9_0_offset.h"
34 #include "sdma0/sdma0_4_0_offset.h"
35 #include "nbio/nbio_7_4_offset.h"
37 #include "oss/osssys_4_0_offset.h"
38 #include "oss/osssys_4_0_sh_mask.h"
40 MODULE_FIRMWARE("amdgpu/renoir_asd.bin");
42 #define smnMP1_FIRMWARE_FLAGS 0x3010024
44 static int psp_v12_0_init_microcode(struct psp_context
*psp
)
46 struct amdgpu_device
*adev
= psp
->adev
;
47 const char *chip_name
;
50 const struct psp_firmware_header_v1_0
*asd_hdr
;
54 switch (adev
->asic_type
) {
62 snprintf(fw_name
, sizeof(fw_name
), "amdgpu/%s_asd.bin", chip_name
);
63 err
= request_firmware(&adev
->psp
.asd_fw
, fw_name
, adev
->dev
);
67 err
= amdgpu_ucode_validate(adev
->psp
.asd_fw
);
71 asd_hdr
= (const struct psp_firmware_header_v1_0
*)adev
->psp
.asd_fw
->data
;
72 adev
->psp
.asd_fw_version
= le32_to_cpu(asd_hdr
->header
.ucode_version
);
73 adev
->psp
.asd_feature_version
= le32_to_cpu(asd_hdr
->ucode_feature_version
);
74 adev
->psp
.asd_ucode_size
= le32_to_cpu(asd_hdr
->header
.ucode_size_bytes
);
75 adev
->psp
.asd_start_addr
= (uint8_t *)asd_hdr
+
76 le32_to_cpu(asd_hdr
->header
.ucode_array_offset_bytes
);
81 release_firmware(adev
->psp
.asd_fw
);
82 adev
->psp
.asd_fw
= NULL
;
87 static int psp_v12_0_bootloader_load_sysdrv(struct psp_context
*psp
)
90 uint32_t psp_gfxdrv_command_reg
= 0;
91 struct amdgpu_device
*adev
= psp
->adev
;
94 /* Check sOS sign of life register to confirm sys driver and sOS
95 * are already been loaded.
97 sol_reg
= RREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_81
);
99 psp
->sos_fw_version
= RREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_58
);
100 printk("sos fw version = 0x%x.\n", psp
->sos_fw_version
);
104 /* Wait for bootloader to signify that is ready having bit 31 of C2PMSG_35 set to 1 */
105 ret
= psp_wait_for(psp
, SOC15_REG_OFFSET(MP0
, 0, mmMP0_SMN_C2PMSG_35
),
106 0x80000000, 0x80000000, false);
110 memset(psp
->fw_pri_buf
, 0, PSP_1_MEG
);
112 /* Copy PSP System Driver binary to memory */
113 memcpy(psp
->fw_pri_buf
, psp
->sys_start_addr
, psp
->sys_bin_size
);
115 /* Provide the sys driver to bootloader */
116 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_36
,
117 (uint32_t)(psp
->fw_pri_mc_addr
>> 20));
118 psp_gfxdrv_command_reg
= 1 << 16;
119 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_35
,
120 psp_gfxdrv_command_reg
);
122 /* there might be handshake issue with hardware which needs delay */
125 ret
= psp_wait_for(psp
, SOC15_REG_OFFSET(MP0
, 0, mmMP0_SMN_C2PMSG_35
),
126 0x80000000, 0x80000000, false);
131 static int psp_v12_0_bootloader_load_sos(struct psp_context
*psp
)
134 unsigned int psp_gfxdrv_command_reg
= 0;
135 struct amdgpu_device
*adev
= psp
->adev
;
138 /* Check sOS sign of life register to confirm sys driver and sOS
139 * are already been loaded.
141 sol_reg
= RREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_81
);
145 /* Wait for bootloader to signify that is ready having bit 31 of C2PMSG_35 set to 1 */
146 ret
= psp_wait_for(psp
, SOC15_REG_OFFSET(MP0
, 0, mmMP0_SMN_C2PMSG_35
),
147 0x80000000, 0x80000000, false);
151 memset(psp
->fw_pri_buf
, 0, PSP_1_MEG
);
153 /* Copy Secure OS binary to PSP memory */
154 memcpy(psp
->fw_pri_buf
, psp
->sos_start_addr
, psp
->sos_bin_size
);
156 /* Provide the PSP secure OS to bootloader */
157 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_36
,
158 (uint32_t)(psp
->fw_pri_mc_addr
>> 20));
159 psp_gfxdrv_command_reg
= 2 << 16;
160 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_35
,
161 psp_gfxdrv_command_reg
);
163 /* there might be handshake issue with hardware which needs delay */
165 ret
= psp_wait_for(psp
, SOC15_REG_OFFSET(MP0
, 0, mmMP0_SMN_C2PMSG_81
),
166 RREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_81
),
172 static void psp_v12_0_reroute_ih(struct psp_context
*psp
)
174 struct amdgpu_device
*adev
= psp
->adev
;
177 /* Change IH ring for VMC */
178 tmp
= REG_SET_FIELD(0, IH_CLIENT_CFG_DATA
, CREDIT_RETURN_ADDR
, 0x1244b);
179 tmp
= REG_SET_FIELD(tmp
, IH_CLIENT_CFG_DATA
, CLIENT_TYPE
, 1);
180 tmp
= REG_SET_FIELD(tmp
, IH_CLIENT_CFG_DATA
, RING_ID
, 1);
182 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_69
, 3);
183 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_70
, tmp
);
184 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_64
, GFX_CTRL_CMD_ID_GBR_IH_SET
);
187 psp_wait_for(psp
, SOC15_REG_OFFSET(MP0
, 0, mmMP0_SMN_C2PMSG_64
),
188 0x80000000, 0x8000FFFF, false);
190 /* Change IH ring for UMC */
191 tmp
= REG_SET_FIELD(0, IH_CLIENT_CFG_DATA
, CREDIT_RETURN_ADDR
, 0x1216b);
192 tmp
= REG_SET_FIELD(tmp
, IH_CLIENT_CFG_DATA
, RING_ID
, 1);
194 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_69
, 4);
195 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_70
, tmp
);
196 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_64
, GFX_CTRL_CMD_ID_GBR_IH_SET
);
199 psp_wait_for(psp
, SOC15_REG_OFFSET(MP0
, 0, mmMP0_SMN_C2PMSG_64
),
200 0x80000000, 0x8000FFFF, false);
203 static int psp_v12_0_ring_init(struct psp_context
*psp
,
204 enum psp_ring_type ring_type
)
207 struct psp_ring
*ring
;
208 struct amdgpu_device
*adev
= psp
->adev
;
210 psp_v12_0_reroute_ih(psp
);
212 ring
= &psp
->km_ring
;
214 ring
->ring_type
= ring_type
;
216 /* allocate 4k Page of Local Frame Buffer memory for ring */
217 ring
->ring_size
= 0x1000;
218 ret
= amdgpu_bo_create_kernel(adev
, ring
->ring_size
, PAGE_SIZE
,
219 AMDGPU_GEM_DOMAIN_VRAM
,
220 &adev
->firmware
.rbuf
,
221 &ring
->ring_mem_mc_addr
,
222 (void **)&ring
->ring_mem
);
231 static bool psp_v12_0_support_vmr_ring(struct psp_context
*psp
)
233 if (amdgpu_sriov_vf(psp
->adev
) && psp
->sos_fw_version
> 0x80045)
238 static int psp_v12_0_ring_create(struct psp_context
*psp
,
239 enum psp_ring_type ring_type
)
242 unsigned int psp_ring_reg
= 0;
243 struct psp_ring
*ring
= &psp
->km_ring
;
244 struct amdgpu_device
*adev
= psp
->adev
;
246 if (psp_v12_0_support_vmr_ring(psp
)) {
247 /* Write low address of the ring to C2PMSG_102 */
248 psp_ring_reg
= lower_32_bits(ring
->ring_mem_mc_addr
);
249 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_102
, psp_ring_reg
);
250 /* Write high address of the ring to C2PMSG_103 */
251 psp_ring_reg
= upper_32_bits(ring
->ring_mem_mc_addr
);
252 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_103
, psp_ring_reg
);
254 /* Write the ring initialization command to C2PMSG_101 */
255 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_101
,
256 GFX_CTRL_CMD_ID_INIT_GPCOM_RING
);
258 /* there might be handshake issue with hardware which needs delay */
261 /* Wait for response flag (bit 31) in C2PMSG_101 */
262 ret
= psp_wait_for(psp
, SOC15_REG_OFFSET(MP0
, 0, mmMP0_SMN_C2PMSG_101
),
263 0x80000000, 0x8000FFFF, false);
266 /* Write low address of the ring to C2PMSG_69 */
267 psp_ring_reg
= lower_32_bits(ring
->ring_mem_mc_addr
);
268 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_69
, psp_ring_reg
);
269 /* Write high address of the ring to C2PMSG_70 */
270 psp_ring_reg
= upper_32_bits(ring
->ring_mem_mc_addr
);
271 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_70
, psp_ring_reg
);
272 /* Write size of ring to C2PMSG_71 */
273 psp_ring_reg
= ring
->ring_size
;
274 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_71
, psp_ring_reg
);
275 /* Write the ring initialization command to C2PMSG_64 */
276 psp_ring_reg
= ring_type
;
277 psp_ring_reg
= psp_ring_reg
<< 16;
278 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_64
, psp_ring_reg
);
280 /* there might be handshake issue with hardware which needs delay */
283 /* Wait for response flag (bit 31) in C2PMSG_64 */
284 ret
= psp_wait_for(psp
, SOC15_REG_OFFSET(MP0
, 0, mmMP0_SMN_C2PMSG_64
),
285 0x80000000, 0x8000FFFF, false);
291 static int psp_v12_0_ring_stop(struct psp_context
*psp
,
292 enum psp_ring_type ring_type
)
295 struct amdgpu_device
*adev
= psp
->adev
;
297 /* Write the ring destroy command*/
298 if (psp_v12_0_support_vmr_ring(psp
))
299 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_101
,
300 GFX_CTRL_CMD_ID_DESTROY_GPCOM_RING
);
302 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_64
,
303 GFX_CTRL_CMD_ID_DESTROY_RINGS
);
305 /* there might be handshake issue with hardware which needs delay */
308 /* Wait for response flag (bit 31) */
309 if (psp_v12_0_support_vmr_ring(psp
))
310 ret
= psp_wait_for(psp
, SOC15_REG_OFFSET(MP0
, 0, mmMP0_SMN_C2PMSG_101
),
311 0x80000000, 0x80000000, false);
313 ret
= psp_wait_for(psp
, SOC15_REG_OFFSET(MP0
, 0, mmMP0_SMN_C2PMSG_64
),
314 0x80000000, 0x80000000, false);
319 static int psp_v12_0_ring_destroy(struct psp_context
*psp
,
320 enum psp_ring_type ring_type
)
323 struct psp_ring
*ring
= &psp
->km_ring
;
324 struct amdgpu_device
*adev
= psp
->adev
;
326 ret
= psp_v12_0_ring_stop(psp
, ring_type
);
328 DRM_ERROR("Fail to stop psp ring\n");
330 amdgpu_bo_free_kernel(&adev
->firmware
.rbuf
,
331 &ring
->ring_mem_mc_addr
,
332 (void **)&ring
->ring_mem
);
338 psp_v12_0_sram_map(struct amdgpu_device
*adev
,
339 unsigned int *sram_offset
, unsigned int *sram_addr_reg_offset
,
340 unsigned int *sram_data_reg_offset
,
341 enum AMDGPU_UCODE_ID ucode_id
)
346 /* TODO: needs to confirm */
348 case AMDGPU_UCODE_ID_SMC
:
350 *sram_addr_reg_offset
= 0;
351 *sram_data_reg_offset
= 0;
355 case AMDGPU_UCODE_ID_CP_CE
:
357 *sram_addr_reg_offset
= SOC15_REG_OFFSET(GC
, 0, mmCP_CE_UCODE_ADDR
);
358 *sram_data_reg_offset
= SOC15_REG_OFFSET(GC
, 0, mmCP_CE_UCODE_DATA
);
361 case AMDGPU_UCODE_ID_CP_PFP
:
363 *sram_addr_reg_offset
= SOC15_REG_OFFSET(GC
, 0, mmCP_PFP_UCODE_ADDR
);
364 *sram_data_reg_offset
= SOC15_REG_OFFSET(GC
, 0, mmCP_PFP_UCODE_DATA
);
367 case AMDGPU_UCODE_ID_CP_ME
:
369 *sram_addr_reg_offset
= SOC15_REG_OFFSET(GC
, 0, mmCP_HYP_ME_UCODE_ADDR
);
370 *sram_data_reg_offset
= SOC15_REG_OFFSET(GC
, 0, mmCP_HYP_ME_UCODE_DATA
);
373 case AMDGPU_UCODE_ID_CP_MEC1
:
374 *sram_offset
= 0x10000;
375 *sram_addr_reg_offset
= SOC15_REG_OFFSET(GC
, 0, mmCP_MEC_ME1_UCODE_ADDR
);
376 *sram_data_reg_offset
= SOC15_REG_OFFSET(GC
, 0, mmCP_MEC_ME1_UCODE_DATA
);
379 case AMDGPU_UCODE_ID_CP_MEC2
:
380 *sram_offset
= 0x10000;
381 *sram_addr_reg_offset
= SOC15_REG_OFFSET(GC
, 0, mmCP_HYP_MEC2_UCODE_ADDR
);
382 *sram_data_reg_offset
= SOC15_REG_OFFSET(GC
, 0, mmCP_HYP_MEC2_UCODE_DATA
);
385 case AMDGPU_UCODE_ID_RLC_G
:
386 *sram_offset
= 0x2000;
387 *sram_addr_reg_offset
= SOC15_REG_OFFSET(GC
, 0, mmRLC_GPM_UCODE_ADDR
);
388 *sram_data_reg_offset
= SOC15_REG_OFFSET(GC
, 0, mmRLC_GPM_UCODE_DATA
);
391 case AMDGPU_UCODE_ID_SDMA0
:
393 *sram_addr_reg_offset
= SOC15_REG_OFFSET(SDMA0
, 0, mmSDMA0_UCODE_ADDR
);
394 *sram_data_reg_offset
= SOC15_REG_OFFSET(SDMA0
, 0, mmSDMA0_UCODE_DATA
);
397 /* TODO: needs to confirm */
399 case AMDGPU_UCODE_ID_SDMA1
:
401 *sram_addr_reg_offset
= ;
404 case AMDGPU_UCODE_ID_UVD
:
406 *sram_addr_reg_offset
= ;
409 case AMDGPU_UCODE_ID_VCE
:
411 *sram_addr_reg_offset
= ;
415 case AMDGPU_UCODE_ID_MAXIMUM
:
424 static bool psp_v12_0_compare_sram_data(struct psp_context
*psp
,
425 struct amdgpu_firmware_info
*ucode
,
426 enum AMDGPU_UCODE_ID ucode_type
)
429 unsigned int fw_sram_reg_val
= 0;
430 unsigned int fw_sram_addr_reg_offset
= 0;
431 unsigned int fw_sram_data_reg_offset
= 0;
432 unsigned int ucode_size
;
433 uint32_t *ucode_mem
= NULL
;
434 struct amdgpu_device
*adev
= psp
->adev
;
436 err
= psp_v12_0_sram_map(adev
, &fw_sram_reg_val
, &fw_sram_addr_reg_offset
,
437 &fw_sram_data_reg_offset
, ucode_type
);
441 WREG32(fw_sram_addr_reg_offset
, fw_sram_reg_val
);
443 ucode_size
= ucode
->ucode_size
;
444 ucode_mem
= (uint32_t *)ucode
->kaddr
;
446 fw_sram_reg_val
= RREG32(fw_sram_data_reg_offset
);
448 if (*ucode_mem
!= fw_sram_reg_val
)
459 static int psp_v12_0_mode1_reset(struct psp_context
*psp
)
463 struct amdgpu_device
*adev
= psp
->adev
;
465 offset
= SOC15_REG_OFFSET(MP0
, 0, mmMP0_SMN_C2PMSG_64
);
467 ret
= psp_wait_for(psp
, offset
, 0x80000000, 0x8000FFFF, false);
470 DRM_INFO("psp is not working correctly before mode1 reset!\n");
474 /*send the mode 1 reset command*/
475 WREG32(offset
, GFX_CTRL_CMD_ID_MODE1_RST
);
479 offset
= SOC15_REG_OFFSET(MP0
, 0, mmMP0_SMN_C2PMSG_33
);
481 ret
= psp_wait_for(psp
, offset
, 0x80000000, 0x80000000, false);
484 DRM_INFO("psp mode 1 reset failed!\n");
488 DRM_INFO("psp mode1 reset succeed \n");
493 static uint32_t psp_v12_0_ring_get_wptr(struct psp_context
*psp
)
496 struct amdgpu_device
*adev
= psp
->adev
;
498 if (psp_v12_0_support_vmr_ring(psp
))
499 data
= RREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_102
);
501 data
= RREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_67
);
506 static void psp_v12_0_ring_set_wptr(struct psp_context
*psp
, uint32_t value
)
508 struct amdgpu_device
*adev
= psp
->adev
;
510 if (psp_v12_0_support_vmr_ring(psp
)) {
511 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_102
, value
);
512 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_101
, GFX_CTRL_CMD_ID_CONSUME_CMD
);
514 WREG32_SOC15(MP0
, 0, mmMP0_SMN_C2PMSG_67
, value
);
517 static const struct psp_funcs psp_v12_0_funcs
= {
518 .init_microcode
= psp_v12_0_init_microcode
,
519 .bootloader_load_sysdrv
= psp_v12_0_bootloader_load_sysdrv
,
520 .bootloader_load_sos
= psp_v12_0_bootloader_load_sos
,
521 .ring_init
= psp_v12_0_ring_init
,
522 .ring_create
= psp_v12_0_ring_create
,
523 .ring_stop
= psp_v12_0_ring_stop
,
524 .ring_destroy
= psp_v12_0_ring_destroy
,
525 .compare_sram_data
= psp_v12_0_compare_sram_data
,
526 .mode1_reset
= psp_v12_0_mode1_reset
,
527 .ring_get_wptr
= psp_v12_0_ring_get_wptr
,
528 .ring_set_wptr
= psp_v12_0_ring_set_wptr
,
531 void psp_v12_0_set_psp_funcs(struct psp_context
*psp
)
533 psp
->funcs
= &psp_v12_0_funcs
;