Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | * Author: Huang Rui |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <linux/firmware.h> |
Masahiro Yamada | 248a1d6 | 2017-04-24 13:50:21 +0900 | [diff] [blame] | 27 | #include <drm/drmP.h> |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 28 | #include "amdgpu.h" |
| 29 | #include "amdgpu_psp.h" |
| 30 | #include "amdgpu_ucode.h" |
| 31 | #include "soc15_common.h" |
| 32 | #include "psp_v3_1.h" |
Huang Rui | c1798b5 | 2016-12-16 10:08:48 +0800 | [diff] [blame] | 33 | #include "psp_v10_0.h" |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 34 | |
| 35 | static void psp_set_funcs(struct amdgpu_device *adev); |
| 36 | |
| 37 | static int psp_early_init(void *handle) |
| 38 | { |
| 39 | struct amdgpu_device *adev = (struct amdgpu_device *)handle; |
| 40 | |
| 41 | psp_set_funcs(adev); |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static int psp_sw_init(void *handle) |
| 47 | { |
| 48 | struct amdgpu_device *adev = (struct amdgpu_device *)handle; |
| 49 | struct psp_context *psp = &adev->psp; |
| 50 | int ret; |
| 51 | |
| 52 | switch (adev->asic_type) { |
| 53 | case CHIP_VEGA10: |
| 54 | psp->init_microcode = psp_v3_1_init_microcode; |
| 55 | psp->bootloader_load_sysdrv = psp_v3_1_bootloader_load_sysdrv; |
| 56 | psp->bootloader_load_sos = psp_v3_1_bootloader_load_sos; |
| 57 | psp->prep_cmd_buf = psp_v3_1_prep_cmd_buf; |
| 58 | psp->ring_init = psp_v3_1_ring_init; |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 59 | psp->ring_create = psp_v3_1_ring_create; |
Trigger Huang | e3c5e98 | 2017-04-17 08:50:18 -0400 | [diff] [blame] | 60 | psp->ring_destroy = psp_v3_1_ring_destroy; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 61 | psp->cmd_submit = psp_v3_1_cmd_submit; |
| 62 | psp->compare_sram_data = psp_v3_1_compare_sram_data; |
| 63 | psp->smu_reload_quirk = psp_v3_1_smu_reload_quirk; |
| 64 | break; |
Huang Rui | c1798b5 | 2016-12-16 10:08:48 +0800 | [diff] [blame] | 65 | case CHIP_RAVEN: |
Junwei Zhang | 6ab7711 | 2017-07-14 18:31:18 +0800 | [diff] [blame^] | 66 | psp->init_microcode = psp_v10_0_init_microcode; |
Huang Rui | c1798b5 | 2016-12-16 10:08:48 +0800 | [diff] [blame] | 67 | psp->prep_cmd_buf = psp_v10_0_prep_cmd_buf; |
| 68 | psp->ring_init = psp_v10_0_ring_init; |
| 69 | psp->cmd_submit = psp_v10_0_cmd_submit; |
| 70 | psp->compare_sram_data = psp_v10_0_compare_sram_data; |
| 71 | break; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 72 | default: |
| 73 | return -EINVAL; |
| 74 | } |
| 75 | |
| 76 | psp->adev = adev; |
| 77 | |
| 78 | ret = psp_init_microcode(psp); |
| 79 | if (ret) { |
| 80 | DRM_ERROR("Failed to load psp firmware!\n"); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int psp_sw_fini(void *handle) |
| 88 | { |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int psp_wait_for(struct psp_context *psp, uint32_t reg_index, |
| 93 | uint32_t reg_val, uint32_t mask, bool check_changed) |
| 94 | { |
| 95 | uint32_t val; |
| 96 | int i; |
| 97 | struct amdgpu_device *adev = psp->adev; |
| 98 | |
| 99 | val = RREG32(reg_index); |
| 100 | |
| 101 | for (i = 0; i < adev->usec_timeout; i++) { |
| 102 | if (check_changed) { |
| 103 | if (val != reg_val) |
| 104 | return 0; |
| 105 | } else { |
| 106 | if ((val & mask) == reg_val) |
| 107 | return 0; |
| 108 | } |
| 109 | udelay(1); |
| 110 | } |
| 111 | |
| 112 | return -ETIME; |
| 113 | } |
| 114 | |
| 115 | static int |
| 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, |
| 119 | int index) |
| 120 | { |
| 121 | int ret; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 122 | |
Huang Rui | a1952da | 2017-06-11 18:57:08 +0800 | [diff] [blame] | 123 | memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 124 | |
Huang Rui | a1952da | 2017-06-11 18:57:08 +0800 | [diff] [blame] | 125 | memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp)); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 126 | |
Huang Rui | a1952da | 2017-06-11 18:57:08 +0800 | [diff] [blame] | 127 | ret = psp_cmd_submit(psp, ucode, psp->cmd_buf_mc_addr, |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 128 | fence_mc_addr, index); |
| 129 | |
| 130 | while (*((unsigned int *)psp->fence_buf) != index) { |
| 131 | msleep(1); |
kbuild test robot | ca7f65c | 2017-03-31 18:15:10 +0800 | [diff] [blame] | 132 | } |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 133 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | static void psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp *cmd, |
| 138 | uint64_t tmr_mc, uint32_t size) |
| 139 | { |
| 140 | cmd->cmd_id = GFX_CMD_ID_SETUP_TMR; |
Alex Deucher | f03defe | 2017-06-22 18:26:33 -0400 | [diff] [blame] | 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); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 143 | cmd->cmd.cmd_setup_tmr.buf_size = size; |
| 144 | } |
| 145 | |
| 146 | /* Set up Trusted Memory Region */ |
| 147 | static int psp_tmr_init(struct psp_context *psp) |
| 148 | { |
| 149 | int ret; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * Allocate 3M memory aligned to 1M from Frame Buffer (local |
| 153 | * physical). |
| 154 | * |
| 155 | * Note: this memory need be reserved till the driver |
| 156 | * uninitializes. |
| 157 | */ |
| 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); |
Huang Rui | 6f2b1fc | 2017-03-21 16:18:11 +0800 | [diff] [blame] | 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | static int psp_tmr_load(struct psp_context *psp) |
| 166 | { |
| 167 | int ret; |
| 168 | struct psp_gfx_cmd_resp *cmd; |
| 169 | |
| 170 | cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL); |
| 171 | if (!cmd) |
| 172 | return -ENOMEM; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 173 | |
| 174 | psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000); |
| 175 | |
| 176 | ret = psp_cmd_submit_buf(psp, NULL, cmd, |
| 177 | psp->fence_buf_mc_addr, 1); |
| 178 | if (ret) |
Huang Rui | 6f2b1fc | 2017-03-21 16:18:11 +0800 | [diff] [blame] | 179 | goto failed; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 180 | |
| 181 | kfree(cmd); |
| 182 | |
| 183 | return 0; |
| 184 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 185 | failed: |
| 186 | kfree(cmd); |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 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) |
| 193 | { |
| 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; |
| 198 | |
| 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; |
| 202 | } |
| 203 | |
Huang Rui | f5cfef9 | 2017-03-21 18:02:04 +0800 | [diff] [blame] | 204 | static int psp_asd_init(struct psp_context *psp) |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 205 | { |
| 206 | int ret; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * Allocate 16k memory aligned to 4k from Frame Buffer (local |
| 210 | * physical) for shared ASD <-> Driver |
| 211 | */ |
Huang Rui | f5cfef9 | 2017-03-21 18:02:04 +0800 | [diff] [blame] | 212 | ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE, |
| 213 | PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM, |
| 214 | &psp->asd_shared_bo, |
| 215 | &psp->asd_shared_mc_addr, |
| 216 | &psp->asd_shared_buf); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 217 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 218 | return ret; |
| 219 | } |
| 220 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 221 | static int psp_asd_load(struct psp_context *psp) |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 222 | { |
| 223 | int ret; |
| 224 | struct psp_gfx_cmd_resp *cmd; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 225 | |
Xiangliang Yu | 943cafb | 2017-05-04 11:05:13 +0800 | [diff] [blame] | 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 |
| 229 | */ |
| 230 | if (amdgpu_sriov_vf(psp->adev)) |
| 231 | return 0; |
| 232 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 233 | cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL); |
| 234 | if (!cmd) |
| 235 | return -ENOMEM; |
| 236 | |
Huang Rui | 2b0c3ae | 2017-03-22 10:16:05 +0800 | [diff] [blame] | 237 | memset(psp->fw_pri_buf, 0, PSP_1_MEG); |
| 238 | memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 239 | |
Huang Rui | f5cfef9 | 2017-03-21 18:02:04 +0800 | [diff] [blame] | 240 | psp_prep_asd_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_shared_mc_addr, |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 241 | psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE); |
| 242 | |
| 243 | ret = psp_cmd_submit_buf(psp, NULL, cmd, |
| 244 | psp->fence_buf_mc_addr, 2); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 245 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 246 | kfree(cmd); |
| 247 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 248 | return ret; |
| 249 | } |
| 250 | |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 251 | static int psp_hw_start(struct psp_context *psp) |
| 252 | { |
| 253 | int ret; |
| 254 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 255 | ret = psp_bootloader_load_sysdrv(psp); |
| 256 | if (ret) |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 257 | return ret; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 258 | |
| 259 | ret = psp_bootloader_load_sos(psp); |
| 260 | if (ret) |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 261 | return ret; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 262 | |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 263 | ret = psp_ring_create(psp, PSP_RING_TYPE__KM); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 264 | if (ret) |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 265 | return ret; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 266 | |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 267 | ret = psp_tmr_load(psp); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 268 | if (ret) |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 269 | return ret; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 270 | |
| 271 | ret = psp_asd_load(psp); |
| 272 | if (ret) |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 273 | return ret; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int psp_np_fw_load(struct psp_context *psp) |
| 279 | { |
| 280 | int i, ret; |
| 281 | struct amdgpu_firmware_info *ucode; |
| 282 | struct amdgpu_device* adev = psp->adev; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 283 | |
| 284 | for (i = 0; i < adev->firmware.max_ucodes; i++) { |
| 285 | ucode = &adev->firmware.ucode[i]; |
| 286 | if (!ucode->fw) |
| 287 | continue; |
| 288 | |
| 289 | if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC && |
| 290 | psp_smu_reload_quirk(psp)) |
| 291 | continue; |
Daniel Wang | e993ca4 | 2017-04-20 11:45:09 +0800 | [diff] [blame] | 292 | if (amdgpu_sriov_vf(adev) && |
| 293 | (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0 |
| 294 | || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 |
| 295 | || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G)) |
| 296 | /*skip ucode loading in SRIOV VF */ |
| 297 | continue; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 298 | |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 299 | ret = psp_prep_cmd_buf(ucode, psp->cmd); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 300 | if (ret) |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 301 | return ret; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 302 | |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 303 | ret = psp_cmd_submit_buf(psp, ucode, psp->cmd, |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 304 | psp->fence_buf_mc_addr, i + 3); |
| 305 | if (ret) |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 306 | return ret; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 307 | |
| 308 | #if 0 |
| 309 | /* check if firmware loaded sucessfully */ |
| 310 | if (!amdgpu_psp_check_fw_loading_status(adev, i)) |
| 311 | return -EINVAL; |
| 312 | #endif |
| 313 | } |
| 314 | |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 318 | static int psp_load_fw(struct amdgpu_device *adev) |
| 319 | { |
| 320 | int ret; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 321 | struct psp_context *psp = &adev->psp; |
| 322 | |
Huang Rui | 67bef0f | 2017-06-29 14:21:49 +0800 | [diff] [blame] | 323 | psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL); |
| 324 | if (!psp->cmd) |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 325 | return -ENOMEM; |
| 326 | |
Huang Rui | 53a5cf5 | 2017-03-21 16:51:00 +0800 | [diff] [blame] | 327 | ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG, |
| 328 | AMDGPU_GEM_DOMAIN_GTT, |
| 329 | &psp->fw_pri_bo, |
| 330 | &psp->fw_pri_mc_addr, |
| 331 | &psp->fw_pri_buf); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 332 | if (ret) |
| 333 | goto failed; |
| 334 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 335 | ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE, |
| 336 | AMDGPU_GEM_DOMAIN_VRAM, |
| 337 | &psp->fence_buf_bo, |
| 338 | &psp->fence_buf_mc_addr, |
| 339 | &psp->fence_buf); |
| 340 | if (ret) |
Huang Rui | a1952da | 2017-06-11 18:57:08 +0800 | [diff] [blame] | 341 | goto failed_mem2; |
| 342 | |
| 343 | ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE, |
| 344 | AMDGPU_GEM_DOMAIN_VRAM, |
| 345 | &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr, |
| 346 | (void **)&psp->cmd_buf_mem); |
| 347 | if (ret) |
Huang Rui | 53a5cf5 | 2017-03-21 16:51:00 +0800 | [diff] [blame] | 348 | goto failed_mem1; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 349 | |
| 350 | memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE); |
| 351 | |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 352 | ret = psp_ring_init(psp, PSP_RING_TYPE__KM); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 353 | if (ret) |
Huang Rui | a1952da | 2017-06-11 18:57:08 +0800 | [diff] [blame] | 354 | goto failed_mem; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 355 | |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 356 | ret = psp_tmr_init(psp); |
Huang Rui | 6f2b1fc | 2017-03-21 16:18:11 +0800 | [diff] [blame] | 357 | if (ret) |
| 358 | goto failed_mem; |
| 359 | |
Huang Rui | f5cfef9 | 2017-03-21 18:02:04 +0800 | [diff] [blame] | 360 | ret = psp_asd_init(psp); |
| 361 | if (ret) |
| 362 | goto failed_mem; |
| 363 | |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 364 | ret = psp_hw_start(psp); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 365 | if (ret) |
| 366 | goto failed_mem; |
| 367 | |
Huang Rui | be70bbd | 2017-03-21 18:36:57 +0800 | [diff] [blame] | 368 | ret = psp_np_fw_load(psp); |
| 369 | if (ret) |
| 370 | goto failed_mem; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 371 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 372 | return 0; |
| 373 | |
| 374 | failed_mem: |
Huang Rui | a1952da | 2017-06-11 18:57:08 +0800 | [diff] [blame] | 375 | amdgpu_bo_free_kernel(&psp->cmd_buf_bo, |
| 376 | &psp->cmd_buf_mc_addr, |
| 377 | (void **)&psp->cmd_buf_mem); |
| 378 | failed_mem1: |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 379 | amdgpu_bo_free_kernel(&psp->fence_buf_bo, |
| 380 | &psp->fence_buf_mc_addr, &psp->fence_buf); |
Huang Rui | a1952da | 2017-06-11 18:57:08 +0800 | [diff] [blame] | 381 | failed_mem2: |
Huang Rui | 53a5cf5 | 2017-03-21 16:51:00 +0800 | [diff] [blame] | 382 | amdgpu_bo_free_kernel(&psp->fw_pri_bo, |
| 383 | &psp->fw_pri_mc_addr, &psp->fw_pri_buf); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 384 | failed: |
Huang Rui | 67bef0f | 2017-06-29 14:21:49 +0800 | [diff] [blame] | 385 | kfree(psp->cmd); |
| 386 | psp->cmd = NULL; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | static int psp_hw_init(void *handle) |
| 391 | { |
| 392 | int ret; |
| 393 | struct amdgpu_device *adev = (struct amdgpu_device *)handle; |
| 394 | |
| 395 | |
| 396 | if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) |
| 397 | return 0; |
| 398 | |
| 399 | mutex_lock(&adev->firmware.mutex); |
| 400 | /* |
| 401 | * This sequence is just used on hw_init only once, no need on |
| 402 | * resume. |
| 403 | */ |
| 404 | ret = amdgpu_ucode_init_bo(adev); |
| 405 | if (ret) |
| 406 | goto failed; |
| 407 | |
| 408 | ret = psp_load_fw(adev); |
| 409 | if (ret) { |
| 410 | DRM_ERROR("PSP firmware loading failed\n"); |
| 411 | goto failed; |
| 412 | } |
| 413 | |
| 414 | mutex_unlock(&adev->firmware.mutex); |
| 415 | return 0; |
| 416 | |
| 417 | failed: |
| 418 | adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT; |
| 419 | mutex_unlock(&adev->firmware.mutex); |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | |
| 423 | static int psp_hw_fini(void *handle) |
| 424 | { |
| 425 | struct amdgpu_device *adev = (struct amdgpu_device *)handle; |
| 426 | struct psp_context *psp = &adev->psp; |
| 427 | |
Trigger Huang | e3c5e98 | 2017-04-17 08:50:18 -0400 | [diff] [blame] | 428 | if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) |
| 429 | return 0; |
| 430 | |
| 431 | amdgpu_ucode_fini_bo(adev); |
| 432 | |
| 433 | psp_ring_destroy(psp, PSP_RING_TYPE__KM); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 434 | |
Huang Rui | edc4d3d | 2017-06-02 10:42:28 +0800 | [diff] [blame] | 435 | amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf); |
| 436 | amdgpu_bo_free_kernel(&psp->fw_pri_bo, |
| 437 | &psp->fw_pri_mc_addr, &psp->fw_pri_buf); |
| 438 | amdgpu_bo_free_kernel(&psp->fence_buf_bo, |
| 439 | &psp->fence_buf_mc_addr, &psp->fence_buf); |
Huang Rui | 311146c | 2017-06-11 18:28:00 +0800 | [diff] [blame] | 440 | amdgpu_bo_free_kernel(&psp->asd_shared_bo, &psp->asd_shared_mc_addr, |
| 441 | &psp->asd_shared_buf); |
Huang Rui | a1952da | 2017-06-11 18:57:08 +0800 | [diff] [blame] | 442 | amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr, |
| 443 | (void **)&psp->cmd_buf_mem); |
Huang Rui | b4de2c5 | 2017-04-10 15:29:42 +0800 | [diff] [blame] | 444 | |
Huang Rui | 67bef0f | 2017-06-29 14:21:49 +0800 | [diff] [blame] | 445 | kfree(psp->cmd); |
| 446 | psp->cmd = NULL; |
| 447 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | static int psp_suspend(void *handle) |
| 452 | { |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static int psp_resume(void *handle) |
| 457 | { |
| 458 | int ret; |
| 459 | struct amdgpu_device *adev = (struct amdgpu_device *)handle; |
Huang Rui | 93ea9b9 | 2017-03-23 11:20:25 +0800 | [diff] [blame] | 460 | struct psp_context *psp = &adev->psp; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 461 | |
| 462 | if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) |
| 463 | return 0; |
| 464 | |
Huang Rui | 93ea9b9 | 2017-03-23 11:20:25 +0800 | [diff] [blame] | 465 | DRM_INFO("PSP is resuming...\n"); |
| 466 | |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 467 | mutex_lock(&adev->firmware.mutex); |
| 468 | |
Huang Rui | 93ea9b9 | 2017-03-23 11:20:25 +0800 | [diff] [blame] | 469 | ret = psp_hw_start(psp); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 470 | if (ret) |
Huang Rui | 93ea9b9 | 2017-03-23 11:20:25 +0800 | [diff] [blame] | 471 | goto failed; |
| 472 | |
| 473 | ret = psp_np_fw_load(psp); |
| 474 | if (ret) |
| 475 | goto failed; |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 476 | |
| 477 | mutex_unlock(&adev->firmware.mutex); |
| 478 | |
Huang Rui | 93ea9b9 | 2017-03-23 11:20:25 +0800 | [diff] [blame] | 479 | return 0; |
| 480 | |
| 481 | failed: |
| 482 | DRM_ERROR("PSP resume failed\n"); |
| 483 | mutex_unlock(&adev->firmware.mutex); |
Huang Rui | 0e5ca0d | 2017-03-03 18:37:23 -0500 | [diff] [blame] | 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | static bool psp_check_fw_loading_status(struct amdgpu_device *adev, |
| 488 | enum AMDGPU_UCODE_ID ucode_type) |
| 489 | { |
| 490 | struct amdgpu_firmware_info *ucode = NULL; |
| 491 | |
| 492 | if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) { |
| 493 | DRM_INFO("firmware is not loaded by PSP\n"); |
| 494 | return true; |
| 495 | } |
| 496 | |
| 497 | if (!adev->firmware.fw_size) |
| 498 | return false; |
| 499 | |
| 500 | ucode = &adev->firmware.ucode[ucode_type]; |
| 501 | if (!ucode->fw || !ucode->ucode_size) |
| 502 | return false; |
| 503 | |
| 504 | return psp_compare_sram_data(&adev->psp, ucode, ucode_type); |
| 505 | } |
| 506 | |
| 507 | static int psp_set_clockgating_state(void *handle, |
| 508 | enum amd_clockgating_state state) |
| 509 | { |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static int psp_set_powergating_state(void *handle, |
| 514 | enum amd_powergating_state state) |
| 515 | { |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | const struct amd_ip_funcs psp_ip_funcs = { |
| 520 | .name = "psp", |
| 521 | .early_init = psp_early_init, |
| 522 | .late_init = NULL, |
| 523 | .sw_init = psp_sw_init, |
| 524 | .sw_fini = psp_sw_fini, |
| 525 | .hw_init = psp_hw_init, |
| 526 | .hw_fini = psp_hw_fini, |
| 527 | .suspend = psp_suspend, |
| 528 | .resume = psp_resume, |
| 529 | .is_idle = NULL, |
| 530 | .wait_for_idle = NULL, |
| 531 | .soft_reset = NULL, |
| 532 | .set_clockgating_state = psp_set_clockgating_state, |
| 533 | .set_powergating_state = psp_set_powergating_state, |
| 534 | }; |
| 535 | |
| 536 | static const struct amdgpu_psp_funcs psp_funcs = { |
| 537 | .check_fw_loading_status = psp_check_fw_loading_status, |
| 538 | }; |
| 539 | |
| 540 | static void psp_set_funcs(struct amdgpu_device *adev) |
| 541 | { |
| 542 | if (NULL == adev->firmware.funcs) |
| 543 | adev->firmware.funcs = &psp_funcs; |
| 544 | } |
| 545 | |
| 546 | const struct amdgpu_ip_block_version psp_v3_1_ip_block = |
| 547 | { |
| 548 | .type = AMD_IP_BLOCK_TYPE_PSP, |
| 549 | .major = 3, |
| 550 | .minor = 1, |
| 551 | .rev = 0, |
| 552 | .funcs = &psp_ip_funcs, |
| 553 | }; |
Huang Rui | dfbd643 | 2016-12-16 10:01:55 +0800 | [diff] [blame] | 554 | |
| 555 | const struct amdgpu_ip_block_version psp_v10_0_ip_block = |
| 556 | { |
| 557 | .type = AMD_IP_BLOCK_TYPE_PSP, |
| 558 | .major = 10, |
| 559 | .minor = 0, |
| 560 | .rev = 0, |
| 561 | .funcs = &psp_ip_funcs, |
| 562 | }; |