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) {
137 static void psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp *cmd,
138 uint64_t tmr_mc, uint32_t size)
140 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
141 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
142 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
143 cmd->cmd.cmd_setup_tmr.buf_size = size;
146 /* Set up Trusted Memory Region */
147 static int psp_tmr_init(struct psp_context *psp)
152 * Allocate 3M memory aligned to 1M from Frame Buffer (local
155 * Note: this memory need be reserved till the driver
158 ret = amdgpu_bo_create_kernel(psp->adev, 0x300000, 0x100000,
159 AMDGPU_GEM_DOMAIN_VRAM,
160 &psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
165 static int psp_tmr_load(struct psp_context *psp)
168 struct psp_gfx_cmd_resp *cmd;
170 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
174 psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000);
176 ret = psp_cmd_submit_buf(psp, NULL, cmd,
177 psp->fence_buf_mc_addr, 1);
190 static void psp_prep_asd_cmd_buf(struct psp_gfx_cmd_resp *cmd,
191 uint64_t asd_mc, uint64_t asd_mc_shared,
192 uint32_t size, uint32_t shared_size)
194 cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
195 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
196 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
197 cmd->cmd.cmd_load_ta.app_len = size;
199 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(asd_mc_shared);
200 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(asd_mc_shared);
201 cmd->cmd.cmd_load_ta.cmd_buf_len = shared_size;
204 static int psp_asd_init(struct psp_context *psp)
209 * Allocate 16k memory aligned to 4k from Frame Buffer (local
210 * physical) for shared ASD <-> Driver
212 ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE,
213 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
215 &psp->asd_shared_mc_addr,
216 &psp->asd_shared_buf);
221 static int psp_asd_load(struct psp_context *psp)
224 struct psp_gfx_cmd_resp *cmd;
226 /* If PSP version doesn't match ASD version, asd loading will be failed.
227 * add workaround to bypass it for sriov now.
228 * TODO: add version check to make it common
230 if (amdgpu_sriov_vf(psp->adev))
233 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
237 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
238 memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
240 psp_prep_asd_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_shared_mc_addr,
241 psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
243 ret = psp_cmd_submit_buf(psp, NULL, cmd,
244 psp->fence_buf_mc_addr, 2);
251 static int psp_hw_start(struct psp_context *psp)
253 struct amdgpu_device *adev = psp->adev;
256 if (!amdgpu_sriov_vf(adev) || !adev->in_gpu_reset) {
257 ret = psp_bootloader_load_sysdrv(psp);
261 ret = psp_bootloader_load_sos(psp);
266 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
270 ret = psp_tmr_load(psp);
274 ret = psp_asd_load(psp);
281 static int psp_np_fw_load(struct psp_context *psp)
284 struct amdgpu_firmware_info *ucode;
285 struct amdgpu_device* adev = psp->adev;
287 for (i = 0; i < adev->firmware.max_ucodes; i++) {
288 ucode = &adev->firmware.ucode[i];
292 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
293 psp_smu_reload_quirk(psp))
295 if (amdgpu_sriov_vf(adev) &&
296 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
297 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
298 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G))
299 /*skip ucode loading in SRIOV VF */
302 ret = psp_prep_cmd_buf(ucode, psp->cmd);
306 ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
307 psp->fence_buf_mc_addr, i + 3);
312 /* check if firmware loaded sucessfully */
313 if (!amdgpu_psp_check_fw_loading_status(adev, i))
321 static int psp_load_fw(struct amdgpu_device *adev)
324 struct psp_context *psp = &adev->psp;
326 if (amdgpu_sriov_vf(adev) && adev->in_gpu_reset != 0)
329 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
333 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
334 AMDGPU_GEM_DOMAIN_GTT,
336 &psp->fw_pri_mc_addr,
341 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
342 AMDGPU_GEM_DOMAIN_VRAM,
344 &psp->fence_buf_mc_addr,
349 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
350 AMDGPU_GEM_DOMAIN_VRAM,
351 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
352 (void **)&psp->cmd_buf_mem);
356 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
358 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
362 ret = psp_tmr_init(psp);
366 ret = psp_asd_init(psp);
371 ret = psp_hw_start(psp);
375 ret = psp_np_fw_load(psp);
382 amdgpu_bo_free_kernel(&psp->cmd_buf_bo,
383 &psp->cmd_buf_mc_addr,
384 (void **)&psp->cmd_buf_mem);
386 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
387 &psp->fence_buf_mc_addr, &psp->fence_buf);
389 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
390 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
397 static int psp_hw_init(void *handle)
400 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
403 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
406 mutex_lock(&adev->firmware.mutex);
408 * This sequence is just used on hw_init only once, no need on
411 ret = amdgpu_ucode_init_bo(adev);
415 ret = psp_load_fw(adev);
417 DRM_ERROR("PSP firmware loading failed\n");
421 mutex_unlock(&adev->firmware.mutex);
425 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
426 mutex_unlock(&adev->firmware.mutex);
430 static int psp_hw_fini(void *handle)
432 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
433 struct psp_context *psp = &adev->psp;
435 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
438 amdgpu_ucode_fini_bo(adev);
440 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
442 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
443 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
444 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
445 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
446 &psp->fence_buf_mc_addr, &psp->fence_buf);
447 amdgpu_bo_free_kernel(&psp->asd_shared_bo, &psp->asd_shared_mc_addr,
448 &psp->asd_shared_buf);
449 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
450 (void **)&psp->cmd_buf_mem);
458 static int psp_suspend(void *handle)
461 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
462 struct psp_context *psp = &adev->psp;
464 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
467 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
469 DRM_ERROR("PSP ring stop failed\n");
476 static int psp_resume(void *handle)
479 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
480 struct psp_context *psp = &adev->psp;
482 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
485 DRM_INFO("PSP is resuming...\n");
487 mutex_lock(&adev->firmware.mutex);
489 ret = psp_hw_start(psp);
493 ret = psp_np_fw_load(psp);
497 mutex_unlock(&adev->firmware.mutex);
502 DRM_ERROR("PSP resume failed\n");
503 mutex_unlock(&adev->firmware.mutex);
507 int psp_gpu_reset(struct amdgpu_device *adev)
509 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
512 return psp_mode1_reset(&adev->psp);
515 static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
516 enum AMDGPU_UCODE_ID ucode_type)
518 struct amdgpu_firmware_info *ucode = NULL;
520 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
521 DRM_INFO("firmware is not loaded by PSP\n");
525 if (!adev->firmware.fw_size)
528 ucode = &adev->firmware.ucode[ucode_type];
529 if (!ucode->fw || !ucode->ucode_size)
532 return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
535 static int psp_set_clockgating_state(void *handle,
536 enum amd_clockgating_state state)
541 static int psp_set_powergating_state(void *handle,
542 enum amd_powergating_state state)
547 const struct amd_ip_funcs psp_ip_funcs = {
549 .early_init = psp_early_init,
551 .sw_init = psp_sw_init,
552 .sw_fini = psp_sw_fini,
553 .hw_init = psp_hw_init,
554 .hw_fini = psp_hw_fini,
555 .suspend = psp_suspend,
556 .resume = psp_resume,
558 .check_soft_reset = NULL,
559 .wait_for_idle = NULL,
561 .set_clockgating_state = psp_set_clockgating_state,
562 .set_powergating_state = psp_set_powergating_state,
565 static const struct amdgpu_psp_funcs psp_funcs = {
566 .check_fw_loading_status = psp_check_fw_loading_status,
569 static void psp_set_funcs(struct amdgpu_device *adev)
571 if (NULL == adev->firmware.funcs)
572 adev->firmware.funcs = &psp_funcs;
575 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
577 .type = AMD_IP_BLOCK_TYPE_PSP,
581 .funcs = &psp_ip_funcs,
584 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
586 .type = AMD_IP_BLOCK_TYPE_PSP,
590 .funcs = &psp_ip_funcs,