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) {
54 psp_v3_1_set_psp_funcs(psp);
57 psp_v10_0_set_psp_funcs(psp);
65 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
68 ret = psp_init_microcode(psp);
70 DRM_ERROR("Failed to load psp firmware!\n");
77 static int psp_sw_fini(void *handle)
79 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
81 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
84 release_firmware(adev->psp.sos_fw);
85 adev->psp.sos_fw = NULL;
86 release_firmware(adev->psp.asd_fw);
87 adev->psp.asd_fw = NULL;
91 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
92 uint32_t reg_val, uint32_t mask, bool check_changed)
96 struct amdgpu_device *adev = psp->adev;
98 for (i = 0; i < adev->usec_timeout; i++) {
99 val = RREG32(reg_index);
104 if ((val & mask) == reg_val)
114 psp_cmd_submit_buf(struct psp_context *psp,
115 struct amdgpu_firmware_info *ucode,
116 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr,
121 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
123 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
125 ret = psp_cmd_submit(psp, ucode, psp->cmd_buf_mc_addr,
126 fence_mc_addr, index);
128 while (*((unsigned int *)psp->fence_buf) != index) {
135 static void psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp *cmd,
136 uint64_t tmr_mc, uint32_t size)
138 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
139 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
140 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
141 cmd->cmd.cmd_setup_tmr.buf_size = size;
144 /* Set up Trusted Memory Region */
145 static int psp_tmr_init(struct psp_context *psp)
150 * Allocate 3M memory aligned to 1M from Frame Buffer (local
153 * Note: this memory need be reserved till the driver
156 ret = amdgpu_bo_create_kernel(psp->adev, 0x300000, 0x100000,
157 AMDGPU_GEM_DOMAIN_VRAM,
158 &psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
163 static int psp_tmr_load(struct psp_context *psp)
166 struct psp_gfx_cmd_resp *cmd;
168 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
172 psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000);
174 ret = psp_cmd_submit_buf(psp, NULL, cmd,
175 psp->fence_buf_mc_addr, 1);
188 static void psp_prep_asd_cmd_buf(struct psp_gfx_cmd_resp *cmd,
189 uint64_t asd_mc, uint64_t asd_mc_shared,
190 uint32_t size, uint32_t shared_size)
192 cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
193 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
194 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
195 cmd->cmd.cmd_load_ta.app_len = size;
197 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(asd_mc_shared);
198 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(asd_mc_shared);
199 cmd->cmd.cmd_load_ta.cmd_buf_len = shared_size;
202 static int psp_asd_init(struct psp_context *psp)
207 * Allocate 16k memory aligned to 4k from Frame Buffer (local
208 * physical) for shared ASD <-> Driver
210 ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE,
211 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
213 &psp->asd_shared_mc_addr,
214 &psp->asd_shared_buf);
219 static int psp_asd_load(struct psp_context *psp)
222 struct psp_gfx_cmd_resp *cmd;
224 /* If PSP version doesn't match ASD version, asd loading will be failed.
225 * add workaround to bypass it for sriov now.
226 * TODO: add version check to make it common
228 if (amdgpu_sriov_vf(psp->adev))
231 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
235 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
236 memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
238 psp_prep_asd_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_shared_mc_addr,
239 psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
241 ret = psp_cmd_submit_buf(psp, NULL, cmd,
242 psp->fence_buf_mc_addr, 2);
249 static int psp_hw_start(struct psp_context *psp)
251 struct amdgpu_device *adev = psp->adev;
254 if (!amdgpu_sriov_vf(adev) || !adev->in_gpu_reset) {
255 ret = psp_bootloader_load_sysdrv(psp);
259 ret = psp_bootloader_load_sos(psp);
264 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
268 ret = psp_tmr_load(psp);
272 ret = psp_asd_load(psp);
279 static int psp_np_fw_load(struct psp_context *psp)
282 struct amdgpu_firmware_info *ucode;
283 struct amdgpu_device* adev = psp->adev;
285 for (i = 0; i < adev->firmware.max_ucodes; i++) {
286 ucode = &adev->firmware.ucode[i];
290 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
291 psp_smu_reload_quirk(psp))
293 if (amdgpu_sriov_vf(adev) &&
294 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
295 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
296 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G))
297 /*skip ucode loading in SRIOV VF */
300 ret = psp_prep_cmd_buf(ucode, psp->cmd);
304 ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
305 psp->fence_buf_mc_addr, i + 3);
310 /* check if firmware loaded sucessfully */
311 if (!amdgpu_psp_check_fw_loading_status(adev, i))
319 static int psp_load_fw(struct amdgpu_device *adev)
322 struct psp_context *psp = &adev->psp;
324 if (amdgpu_sriov_vf(adev) && adev->in_gpu_reset != 0)
327 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
331 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
332 AMDGPU_GEM_DOMAIN_GTT,
334 &psp->fw_pri_mc_addr,
339 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
340 AMDGPU_GEM_DOMAIN_VRAM,
342 &psp->fence_buf_mc_addr,
347 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
348 AMDGPU_GEM_DOMAIN_VRAM,
349 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
350 (void **)&psp->cmd_buf_mem);
354 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
356 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
360 ret = psp_tmr_init(psp);
364 ret = psp_asd_init(psp);
369 ret = psp_hw_start(psp);
373 ret = psp_np_fw_load(psp);
380 amdgpu_bo_free_kernel(&psp->cmd_buf_bo,
381 &psp->cmd_buf_mc_addr,
382 (void **)&psp->cmd_buf_mem);
384 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
385 &psp->fence_buf_mc_addr, &psp->fence_buf);
387 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
388 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
395 static int psp_hw_init(void *handle)
398 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
401 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
404 mutex_lock(&adev->firmware.mutex);
406 * This sequence is just used on hw_init only once, no need on
409 ret = amdgpu_ucode_init_bo(adev);
413 ret = psp_load_fw(adev);
415 DRM_ERROR("PSP firmware loading failed\n");
419 mutex_unlock(&adev->firmware.mutex);
423 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
424 mutex_unlock(&adev->firmware.mutex);
428 static int psp_hw_fini(void *handle)
430 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
431 struct psp_context *psp = &adev->psp;
433 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
436 amdgpu_ucode_fini_bo(adev);
438 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
440 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
441 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
442 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
443 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
444 &psp->fence_buf_mc_addr, &psp->fence_buf);
445 amdgpu_bo_free_kernel(&psp->asd_shared_bo, &psp->asd_shared_mc_addr,
446 &psp->asd_shared_buf);
447 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
448 (void **)&psp->cmd_buf_mem);
456 static int psp_suspend(void *handle)
459 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
460 struct psp_context *psp = &adev->psp;
462 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
465 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
467 DRM_ERROR("PSP ring stop failed\n");
474 static int psp_resume(void *handle)
477 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
478 struct psp_context *psp = &adev->psp;
480 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
483 DRM_INFO("PSP is resuming...\n");
485 mutex_lock(&adev->firmware.mutex);
487 ret = psp_hw_start(psp);
491 ret = psp_np_fw_load(psp);
495 mutex_unlock(&adev->firmware.mutex);
500 DRM_ERROR("PSP resume failed\n");
501 mutex_unlock(&adev->firmware.mutex);
505 int psp_gpu_reset(struct amdgpu_device *adev)
507 return psp_mode1_reset(&adev->psp);
510 static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
511 enum AMDGPU_UCODE_ID ucode_type)
513 struct amdgpu_firmware_info *ucode = NULL;
515 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
516 DRM_INFO("firmware is not loaded by PSP\n");
520 if (!adev->firmware.fw_size)
523 ucode = &adev->firmware.ucode[ucode_type];
524 if (!ucode->fw || !ucode->ucode_size)
527 return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
530 static int psp_set_clockgating_state(void *handle,
531 enum amd_clockgating_state state)
536 static int psp_set_powergating_state(void *handle,
537 enum amd_powergating_state state)
542 const struct amd_ip_funcs psp_ip_funcs = {
544 .early_init = psp_early_init,
546 .sw_init = psp_sw_init,
547 .sw_fini = psp_sw_fini,
548 .hw_init = psp_hw_init,
549 .hw_fini = psp_hw_fini,
550 .suspend = psp_suspend,
551 .resume = psp_resume,
553 .check_soft_reset = NULL,
554 .wait_for_idle = NULL,
556 .set_clockgating_state = psp_set_clockgating_state,
557 .set_powergating_state = psp_set_powergating_state,
560 static const struct amdgpu_psp_funcs psp_funcs = {
561 .check_fw_loading_status = psp_check_fw_loading_status,
564 static void psp_set_funcs(struct amdgpu_device *adev)
566 if (NULL == adev->firmware.funcs)
567 adev->firmware.funcs = &psp_funcs;
570 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
572 .type = AMD_IP_BLOCK_TYPE_PSP,
576 .funcs = &psp_ip_funcs,
579 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
581 .type = AMD_IP_BLOCK_TYPE_PSP,
585 .funcs = &psp_ip_funcs,