2 * Copyright 2016 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.
26 #include <linux/firmware.h>
29 #include "amdgpu_psp.h"
30 #include "amdgpu_ucode.h"
31 #include "soc15_common.h"
33 #include "psp_v10_0.h"
35 static void psp_set_funcs(struct amdgpu_device *adev);
37 static int psp_early_init(void *handle)
39 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
46 static int psp_sw_init(void *handle)
48 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
49 struct psp_context *psp = &adev->psp;
52 switch (adev->asic_type) {
56 psp_v3_1_set_psp_funcs(psp);
59 psp_v10_0_set_psp_funcs(psp);
67 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
70 ret = psp_init_microcode(psp);
72 DRM_ERROR("Failed to load psp firmware!\n");
79 static int psp_sw_fini(void *handle)
81 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
83 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
86 release_firmware(adev->psp.sos_fw);
87 adev->psp.sos_fw = NULL;
88 release_firmware(adev->psp.asd_fw);
89 adev->psp.asd_fw = NULL;
93 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
94 uint32_t reg_val, uint32_t mask, bool check_changed)
98 struct amdgpu_device *adev = psp->adev;
100 for (i = 0; i < adev->usec_timeout; i++) {
101 val = RREG32(reg_index);
106 if ((val & mask) == reg_val)
116 psp_cmd_submit_buf(struct psp_context *psp,
117 struct amdgpu_firmware_info *ucode,
118 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr,
123 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
125 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
127 ret = psp_cmd_submit(psp, ucode, psp->cmd_buf_mc_addr,
128 fence_mc_addr, index);
130 while (*((unsigned int *)psp->fence_buf) != index) {
135 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
136 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
142 static void psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp *cmd,
143 uint64_t tmr_mc, uint32_t size)
145 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
146 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
147 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
148 cmd->cmd.cmd_setup_tmr.buf_size = size;
151 /* Set up Trusted Memory Region */
152 static int psp_tmr_init(struct psp_context *psp)
157 * Allocate 3M memory aligned to 1M from Frame Buffer (local
160 * Note: this memory need be reserved till the driver
163 ret = amdgpu_bo_create_kernel(psp->adev, 0x300000, 0x100000,
164 AMDGPU_GEM_DOMAIN_VRAM,
165 &psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
170 static int psp_tmr_load(struct psp_context *psp)
173 struct psp_gfx_cmd_resp *cmd;
175 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
179 psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000);
181 ret = psp_cmd_submit_buf(psp, NULL, cmd,
182 psp->fence_buf_mc_addr, 1);
195 static void psp_prep_asd_cmd_buf(struct psp_gfx_cmd_resp *cmd,
196 uint64_t asd_mc, uint64_t asd_mc_shared,
197 uint32_t size, uint32_t shared_size)
199 cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
200 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
201 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
202 cmd->cmd.cmd_load_ta.app_len = size;
204 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(asd_mc_shared);
205 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(asd_mc_shared);
206 cmd->cmd.cmd_load_ta.cmd_buf_len = shared_size;
209 static int psp_asd_init(struct psp_context *psp)
214 * Allocate 16k memory aligned to 4k from Frame Buffer (local
215 * physical) for shared ASD <-> Driver
217 ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE,
218 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
220 &psp->asd_shared_mc_addr,
221 &psp->asd_shared_buf);
226 static int psp_asd_load(struct psp_context *psp)
229 struct psp_gfx_cmd_resp *cmd;
231 /* If PSP version doesn't match ASD version, asd loading will be failed.
232 * add workaround to bypass it for sriov now.
233 * TODO: add version check to make it common
235 if (amdgpu_sriov_vf(psp->adev))
238 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
242 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
243 memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
245 psp_prep_asd_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_shared_mc_addr,
246 psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
248 ret = psp_cmd_submit_buf(psp, NULL, cmd,
249 psp->fence_buf_mc_addr, 2);
256 static int psp_hw_start(struct psp_context *psp)
258 struct amdgpu_device *adev = psp->adev;
261 if (!amdgpu_sriov_vf(adev) || !adev->in_gpu_reset) {
262 ret = psp_bootloader_load_sysdrv(psp);
266 ret = psp_bootloader_load_sos(psp);
271 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
275 ret = psp_tmr_load(psp);
279 ret = psp_asd_load(psp);
286 static int psp_np_fw_load(struct psp_context *psp)
289 struct amdgpu_firmware_info *ucode;
290 struct amdgpu_device* adev = psp->adev;
292 for (i = 0; i < adev->firmware.max_ucodes; i++) {
293 ucode = &adev->firmware.ucode[i];
297 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
298 psp_smu_reload_quirk(psp))
300 if (amdgpu_sriov_vf(adev) &&
301 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
302 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
303 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G))
304 /*skip ucode loading in SRIOV VF */
307 ret = psp_prep_cmd_buf(ucode, psp->cmd);
311 ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
312 psp->fence_buf_mc_addr, i + 3);
317 /* check if firmware loaded sucessfully */
318 if (!amdgpu_psp_check_fw_loading_status(adev, i))
326 static int psp_load_fw(struct amdgpu_device *adev)
329 struct psp_context *psp = &adev->psp;
331 if (amdgpu_sriov_vf(adev) && adev->in_gpu_reset != 0)
334 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
338 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
339 AMDGPU_GEM_DOMAIN_GTT,
341 &psp->fw_pri_mc_addr,
346 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
347 AMDGPU_GEM_DOMAIN_VRAM,
349 &psp->fence_buf_mc_addr,
354 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
355 AMDGPU_GEM_DOMAIN_VRAM,
356 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
357 (void **)&psp->cmd_buf_mem);
361 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
363 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
367 ret = psp_tmr_init(psp);
371 ret = psp_asd_init(psp);
376 ret = psp_hw_start(psp);
380 ret = psp_np_fw_load(psp);
387 amdgpu_bo_free_kernel(&psp->cmd_buf_bo,
388 &psp->cmd_buf_mc_addr,
389 (void **)&psp->cmd_buf_mem);
391 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
392 &psp->fence_buf_mc_addr, &psp->fence_buf);
394 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
395 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
402 static int psp_hw_init(void *handle)
405 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
408 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
411 mutex_lock(&adev->firmware.mutex);
413 * This sequence is just used on hw_init only once, no need on
416 ret = amdgpu_ucode_init_bo(adev);
420 ret = psp_load_fw(adev);
422 DRM_ERROR("PSP firmware loading failed\n");
426 mutex_unlock(&adev->firmware.mutex);
430 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
431 mutex_unlock(&adev->firmware.mutex);
435 static int psp_hw_fini(void *handle)
437 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
438 struct psp_context *psp = &adev->psp;
440 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
443 amdgpu_ucode_fini_bo(adev);
445 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
447 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
448 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
449 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
450 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
451 &psp->fence_buf_mc_addr, &psp->fence_buf);
452 amdgpu_bo_free_kernel(&psp->asd_shared_bo, &psp->asd_shared_mc_addr,
453 &psp->asd_shared_buf);
454 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
455 (void **)&psp->cmd_buf_mem);
463 static int psp_suspend(void *handle)
466 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
467 struct psp_context *psp = &adev->psp;
469 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
472 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
474 DRM_ERROR("PSP ring stop failed\n");
481 static int psp_resume(void *handle)
484 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
485 struct psp_context *psp = &adev->psp;
487 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
490 DRM_INFO("PSP is resuming...\n");
492 mutex_lock(&adev->firmware.mutex);
494 ret = psp_hw_start(psp);
498 ret = psp_np_fw_load(psp);
502 mutex_unlock(&adev->firmware.mutex);
507 DRM_ERROR("PSP resume failed\n");
508 mutex_unlock(&adev->firmware.mutex);
512 int psp_gpu_reset(struct amdgpu_device *adev)
514 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
517 return psp_mode1_reset(&adev->psp);
520 static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
521 enum AMDGPU_UCODE_ID ucode_type)
523 struct amdgpu_firmware_info *ucode = NULL;
525 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
526 DRM_INFO("firmware is not loaded by PSP\n");
530 if (!adev->firmware.fw_size)
533 ucode = &adev->firmware.ucode[ucode_type];
534 if (!ucode->fw || !ucode->ucode_size)
537 return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
540 static int psp_set_clockgating_state(void *handle,
541 enum amd_clockgating_state state)
546 static int psp_set_powergating_state(void *handle,
547 enum amd_powergating_state state)
552 const struct amd_ip_funcs psp_ip_funcs = {
554 .early_init = psp_early_init,
556 .sw_init = psp_sw_init,
557 .sw_fini = psp_sw_fini,
558 .hw_init = psp_hw_init,
559 .hw_fini = psp_hw_fini,
560 .suspend = psp_suspend,
561 .resume = psp_resume,
563 .check_soft_reset = NULL,
564 .wait_for_idle = NULL,
566 .set_clockgating_state = psp_set_clockgating_state,
567 .set_powergating_state = psp_set_powergating_state,
570 static const struct amdgpu_psp_funcs psp_funcs = {
571 .check_fw_loading_status = psp_check_fw_loading_status,
574 static void psp_set_funcs(struct amdgpu_device *adev)
576 if (NULL == adev->firmware.funcs)
577 adev->firmware.funcs = &psp_funcs;
580 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
582 .type = AMD_IP_BLOCK_TYPE_PSP,
586 .funcs = &psp_ip_funcs,
589 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
591 .type = AMD_IP_BLOCK_TYPE_PSP,
595 .funcs = &psp_ip_funcs,