blob: 0d530524ab21586029e67d717f0935ff2ac53879 [file] [log] [blame]
Huang Rui0e5ca0d2017-03-03 18:37:23 -05001/*
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 Yamada248a1d62017-04-24 13:50:21 +090027#include <drm/drmP.h>
Huang Rui0e5ca0d2017-03-03 18:37:23 -050028#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 Ruic1798b52016-12-16 10:08:48 +080033#include "psp_v10_0.h"
Huang Rui0e5ca0d2017-03-03 18:37:23 -050034
35static void psp_set_funcs(struct amdgpu_device *adev);
36
37static 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
46static 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 Ruibe70bbd2017-03-21 18:36:57 +080059 psp->ring_create = psp_v3_1_ring_create;
Evan Quan4ef72452017-09-08 13:04:52 +080060 psp->ring_stop = psp_v3_1_ring_stop;
Trigger Huange3c5e982017-04-17 08:50:18 -040061 psp->ring_destroy = psp_v3_1_ring_destroy;
Huang Rui0e5ca0d2017-03-03 18:37:23 -050062 psp->cmd_submit = psp_v3_1_cmd_submit;
63 psp->compare_sram_data = psp_v3_1_compare_sram_data;
64 psp->smu_reload_quirk = psp_v3_1_smu_reload_quirk;
65 break;
Huang Ruic1798b52016-12-16 10:08:48 +080066 case CHIP_RAVEN:
Junwei Zhang16a53a02017-07-19 08:23:24 +080067#if 0
Junwei Zhang6ab77112017-07-14 18:31:18 +080068 psp->init_microcode = psp_v10_0_init_microcode;
Junwei Zhang16a53a02017-07-19 08:23:24 +080069#endif
Huang Ruic1798b52016-12-16 10:08:48 +080070 psp->prep_cmd_buf = psp_v10_0_prep_cmd_buf;
71 psp->ring_init = psp_v10_0_ring_init;
Junwei Zhangccce0552017-07-14 18:34:48 +080072 psp->ring_create = psp_v10_0_ring_create;
Evan Quan4ef72452017-09-08 13:04:52 +080073 psp->ring_stop = psp_v10_0_ring_stop;
Junwei Zhanga4f478b2017-07-14 18:37:44 +080074 psp->ring_destroy = psp_v10_0_ring_destroy;
Huang Ruic1798b52016-12-16 10:08:48 +080075 psp->cmd_submit = psp_v10_0_cmd_submit;
76 psp->compare_sram_data = psp_v10_0_compare_sram_data;
77 break;
Huang Rui0e5ca0d2017-03-03 18:37:23 -050078 default:
79 return -EINVAL;
80 }
81
82 psp->adev = adev;
83
84 ret = psp_init_microcode(psp);
85 if (ret) {
86 DRM_ERROR("Failed to load psp firmware!\n");
87 return ret;
88 }
89
90 return 0;
91}
92
93static int psp_sw_fini(void *handle)
94{
95 return 0;
96}
97
98int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
99 uint32_t reg_val, uint32_t mask, bool check_changed)
100{
101 uint32_t val;
102 int i;
103 struct amdgpu_device *adev = psp->adev;
104
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500105 for (i = 0; i < adev->usec_timeout; i++) {
Zhang, Jerry2890dec2017-07-14 18:20:17 +0800106 val = RREG32(reg_index);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500107 if (check_changed) {
108 if (val != reg_val)
109 return 0;
110 } else {
111 if ((val & mask) == reg_val)
112 return 0;
113 }
114 udelay(1);
115 }
116
117 return -ETIME;
118}
119
120static int
121psp_cmd_submit_buf(struct psp_context *psp,
122 struct amdgpu_firmware_info *ucode,
123 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr,
124 int index)
125{
126 int ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500127
Huang Ruia1952da2017-06-11 18:57:08 +0800128 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500129
Huang Ruia1952da2017-06-11 18:57:08 +0800130 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500131
Huang Ruia1952da2017-06-11 18:57:08 +0800132 ret = psp_cmd_submit(psp, ucode, psp->cmd_buf_mc_addr,
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500133 fence_mc_addr, index);
134
135 while (*((unsigned int *)psp->fence_buf) != index) {
136 msleep(1);
kbuild test robotca7f65c2017-03-31 18:15:10 +0800137 }
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500138
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500139 return ret;
140}
141
142static void psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp *cmd,
143 uint64_t tmr_mc, uint32_t size)
144{
145 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
Alex Deucherf03defe2017-06-22 18:26:33 -0400146 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);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500148 cmd->cmd.cmd_setup_tmr.buf_size = size;
149}
150
151/* Set up Trusted Memory Region */
152static int psp_tmr_init(struct psp_context *psp)
153{
154 int ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500155
156 /*
157 * Allocate 3M memory aligned to 1M from Frame Buffer (local
158 * physical).
159 *
160 * Note: this memory need be reserved till the driver
161 * uninitializes.
162 */
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);
Huang Rui6f2b1fc2017-03-21 16:18:11 +0800166
167 return ret;
168}
169
170static int psp_tmr_load(struct psp_context *psp)
171{
172 int ret;
173 struct psp_gfx_cmd_resp *cmd;
174
175 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
176 if (!cmd)
177 return -ENOMEM;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500178
179 psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000);
180
181 ret = psp_cmd_submit_buf(psp, NULL, cmd,
182 psp->fence_buf_mc_addr, 1);
183 if (ret)
Huang Rui6f2b1fc2017-03-21 16:18:11 +0800184 goto failed;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500185
186 kfree(cmd);
187
188 return 0;
189
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500190failed:
191 kfree(cmd);
192 return ret;
193}
194
195static 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)
198{
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;
203
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;
207}
208
Huang Ruif5cfef92017-03-21 18:02:04 +0800209static int psp_asd_init(struct psp_context *psp)
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500210{
211 int ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500212
213 /*
214 * Allocate 16k memory aligned to 4k from Frame Buffer (local
215 * physical) for shared ASD <-> Driver
216 */
Huang Ruif5cfef92017-03-21 18:02:04 +0800217 ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE,
218 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
219 &psp->asd_shared_bo,
220 &psp->asd_shared_mc_addr,
221 &psp->asd_shared_buf);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500222
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500223 return ret;
224}
225
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500226static int psp_asd_load(struct psp_context *psp)
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500227{
228 int ret;
229 struct psp_gfx_cmd_resp *cmd;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500230
Xiangliang Yu943cafb2017-05-04 11:05:13 +0800231 /* 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
234 */
235 if (amdgpu_sriov_vf(psp->adev))
236 return 0;
237
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500238 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
239 if (!cmd)
240 return -ENOMEM;
241
Huang Rui2b0c3ae2017-03-22 10:16:05 +0800242 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
243 memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500244
Huang Ruif5cfef92017-03-21 18:02:04 +0800245 psp_prep_asd_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_shared_mc_addr,
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500246 psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
247
248 ret = psp_cmd_submit_buf(psp, NULL, cmd,
249 psp->fence_buf_mc_addr, 2);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500250
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500251 kfree(cmd);
252
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500253 return ret;
254}
255
Huang Ruibe70bbd2017-03-21 18:36:57 +0800256static int psp_hw_start(struct psp_context *psp)
257{
258 int ret;
259
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500260 ret = psp_bootloader_load_sysdrv(psp);
261 if (ret)
Huang Ruibe70bbd2017-03-21 18:36:57 +0800262 return ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500263
264 ret = psp_bootloader_load_sos(psp);
265 if (ret)
Huang Ruibe70bbd2017-03-21 18:36:57 +0800266 return ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500267
Huang Ruibe70bbd2017-03-21 18:36:57 +0800268 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500269 if (ret)
Huang Ruibe70bbd2017-03-21 18:36:57 +0800270 return ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500271
Huang Ruibe70bbd2017-03-21 18:36:57 +0800272 ret = psp_tmr_load(psp);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500273 if (ret)
Huang Ruibe70bbd2017-03-21 18:36:57 +0800274 return ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500275
276 ret = psp_asd_load(psp);
277 if (ret)
Huang Ruibe70bbd2017-03-21 18:36:57 +0800278 return ret;
279
280 return 0;
281}
282
283static int psp_np_fw_load(struct psp_context *psp)
284{
285 int i, ret;
286 struct amdgpu_firmware_info *ucode;
287 struct amdgpu_device* adev = psp->adev;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500288
289 for (i = 0; i < adev->firmware.max_ucodes; i++) {
290 ucode = &adev->firmware.ucode[i];
291 if (!ucode->fw)
292 continue;
293
294 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
295 psp_smu_reload_quirk(psp))
296 continue;
Daniel Wange993ca42017-04-20 11:45:09 +0800297 if (amdgpu_sriov_vf(adev) &&
298 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
299 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
300 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G))
301 /*skip ucode loading in SRIOV VF */
302 continue;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500303
Huang Ruibe70bbd2017-03-21 18:36:57 +0800304 ret = psp_prep_cmd_buf(ucode, psp->cmd);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500305 if (ret)
Huang Ruibe70bbd2017-03-21 18:36:57 +0800306 return ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500307
Huang Ruibe70bbd2017-03-21 18:36:57 +0800308 ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500309 psp->fence_buf_mc_addr, i + 3);
310 if (ret)
Huang Ruibe70bbd2017-03-21 18:36:57 +0800311 return ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500312
313#if 0
314 /* check if firmware loaded sucessfully */
315 if (!amdgpu_psp_check_fw_loading_status(adev, i))
316 return -EINVAL;
317#endif
318 }
319
Huang Ruibe70bbd2017-03-21 18:36:57 +0800320 return 0;
321}
322
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500323static int psp_load_fw(struct amdgpu_device *adev)
324{
325 int ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500326 struct psp_context *psp = &adev->psp;
327
Huang Rui67bef0f2017-06-29 14:21:49 +0800328 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
329 if (!psp->cmd)
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500330 return -ENOMEM;
331
Huang Rui53a5cf52017-03-21 16:51:00 +0800332 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
333 AMDGPU_GEM_DOMAIN_GTT,
334 &psp->fw_pri_bo,
335 &psp->fw_pri_mc_addr,
336 &psp->fw_pri_buf);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500337 if (ret)
338 goto failed;
339
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500340 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
341 AMDGPU_GEM_DOMAIN_VRAM,
342 &psp->fence_buf_bo,
343 &psp->fence_buf_mc_addr,
344 &psp->fence_buf);
345 if (ret)
Huang Ruia1952da2017-06-11 18:57:08 +0800346 goto failed_mem2;
347
348 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
349 AMDGPU_GEM_DOMAIN_VRAM,
350 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
351 (void **)&psp->cmd_buf_mem);
352 if (ret)
Huang Rui53a5cf52017-03-21 16:51:00 +0800353 goto failed_mem1;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500354
355 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
356
Huang Ruibe70bbd2017-03-21 18:36:57 +0800357 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500358 if (ret)
Huang Ruia1952da2017-06-11 18:57:08 +0800359 goto failed_mem;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500360
Huang Ruibe70bbd2017-03-21 18:36:57 +0800361 ret = psp_tmr_init(psp);
Huang Rui6f2b1fc2017-03-21 16:18:11 +0800362 if (ret)
363 goto failed_mem;
364
Huang Ruif5cfef92017-03-21 18:02:04 +0800365 ret = psp_asd_init(psp);
366 if (ret)
367 goto failed_mem;
368
Huang Ruibe70bbd2017-03-21 18:36:57 +0800369 ret = psp_hw_start(psp);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500370 if (ret)
371 goto failed_mem;
372
Huang Ruibe70bbd2017-03-21 18:36:57 +0800373 ret = psp_np_fw_load(psp);
374 if (ret)
375 goto failed_mem;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500376
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500377 return 0;
378
379failed_mem:
Huang Ruia1952da2017-06-11 18:57:08 +0800380 amdgpu_bo_free_kernel(&psp->cmd_buf_bo,
381 &psp->cmd_buf_mc_addr,
382 (void **)&psp->cmd_buf_mem);
383failed_mem1:
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500384 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
385 &psp->fence_buf_mc_addr, &psp->fence_buf);
Huang Ruia1952da2017-06-11 18:57:08 +0800386failed_mem2:
Huang Rui53a5cf52017-03-21 16:51:00 +0800387 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
388 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500389failed:
Huang Rui67bef0f2017-06-29 14:21:49 +0800390 kfree(psp->cmd);
391 psp->cmd = NULL;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500392 return ret;
393}
394
395static int psp_hw_init(void *handle)
396{
397 int ret;
398 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
399
400
401 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
402 return 0;
403
404 mutex_lock(&adev->firmware.mutex);
405 /*
406 * This sequence is just used on hw_init only once, no need on
407 * resume.
408 */
409 ret = amdgpu_ucode_init_bo(adev);
410 if (ret)
411 goto failed;
412
413 ret = psp_load_fw(adev);
414 if (ret) {
415 DRM_ERROR("PSP firmware loading failed\n");
416 goto failed;
417 }
418
419 mutex_unlock(&adev->firmware.mutex);
420 return 0;
421
422failed:
423 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
424 mutex_unlock(&adev->firmware.mutex);
425 return -EINVAL;
426}
427
428static int psp_hw_fini(void *handle)
429{
430 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
431 struct psp_context *psp = &adev->psp;
432
Trigger Huange3c5e982017-04-17 08:50:18 -0400433 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
434 return 0;
435
436 amdgpu_ucode_fini_bo(adev);
437
438 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500439
Huang Ruiedc4d3db2017-06-02 10:42:28 +0800440 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);
Huang Rui311146c2017-06-11 18:28:00 +0800445 amdgpu_bo_free_kernel(&psp->asd_shared_bo, &psp->asd_shared_mc_addr,
446 &psp->asd_shared_buf);
Huang Ruia1952da2017-06-11 18:57:08 +0800447 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
448 (void **)&psp->cmd_buf_mem);
Huang Ruib4de2c52017-04-10 15:29:42 +0800449
Huang Rui67bef0f2017-06-29 14:21:49 +0800450 kfree(psp->cmd);
451 psp->cmd = NULL;
452
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500453 return 0;
454}
455
456static int psp_suspend(void *handle)
457{
Evan Quanbcd6eab2017-09-08 13:09:50 +0800458 int ret;
459 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
460 struct psp_context *psp = &adev->psp;
461
462 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
463 if (ret) {
464 DRM_ERROR("PSP ring stop failed\n");
465 return ret;
466 }
467
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500468 return 0;
469}
470
471static int psp_resume(void *handle)
472{
473 int ret;
474 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Huang Rui93ea9b92017-03-23 11:20:25 +0800475 struct psp_context *psp = &adev->psp;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500476
477 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
478 return 0;
479
Huang Rui93ea9b92017-03-23 11:20:25 +0800480 DRM_INFO("PSP is resuming...\n");
481
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500482 mutex_lock(&adev->firmware.mutex);
483
Huang Rui93ea9b92017-03-23 11:20:25 +0800484 ret = psp_hw_start(psp);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500485 if (ret)
Huang Rui93ea9b92017-03-23 11:20:25 +0800486 goto failed;
487
488 ret = psp_np_fw_load(psp);
489 if (ret)
490 goto failed;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500491
492 mutex_unlock(&adev->firmware.mutex);
493
Huang Rui93ea9b92017-03-23 11:20:25 +0800494 return 0;
495
496failed:
497 DRM_ERROR("PSP resume failed\n");
498 mutex_unlock(&adev->firmware.mutex);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500499 return ret;
500}
501
502static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
503 enum AMDGPU_UCODE_ID ucode_type)
504{
505 struct amdgpu_firmware_info *ucode = NULL;
506
507 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
508 DRM_INFO("firmware is not loaded by PSP\n");
509 return true;
510 }
511
512 if (!adev->firmware.fw_size)
513 return false;
514
515 ucode = &adev->firmware.ucode[ucode_type];
516 if (!ucode->fw || !ucode->ucode_size)
517 return false;
518
519 return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
520}
521
522static int psp_set_clockgating_state(void *handle,
523 enum amd_clockgating_state state)
524{
525 return 0;
526}
527
528static int psp_set_powergating_state(void *handle,
529 enum amd_powergating_state state)
530{
531 return 0;
532}
533
534const struct amd_ip_funcs psp_ip_funcs = {
535 .name = "psp",
536 .early_init = psp_early_init,
537 .late_init = NULL,
538 .sw_init = psp_sw_init,
539 .sw_fini = psp_sw_fini,
540 .hw_init = psp_hw_init,
541 .hw_fini = psp_hw_fini,
542 .suspend = psp_suspend,
543 .resume = psp_resume,
544 .is_idle = NULL,
545 .wait_for_idle = NULL,
546 .soft_reset = NULL,
547 .set_clockgating_state = psp_set_clockgating_state,
548 .set_powergating_state = psp_set_powergating_state,
549};
550
551static const struct amdgpu_psp_funcs psp_funcs = {
552 .check_fw_loading_status = psp_check_fw_loading_status,
553};
554
555static void psp_set_funcs(struct amdgpu_device *adev)
556{
557 if (NULL == adev->firmware.funcs)
558 adev->firmware.funcs = &psp_funcs;
559}
560
561const struct amdgpu_ip_block_version psp_v3_1_ip_block =
562{
563 .type = AMD_IP_BLOCK_TYPE_PSP,
564 .major = 3,
565 .minor = 1,
566 .rev = 0,
567 .funcs = &psp_ip_funcs,
568};
Huang Ruidfbd6432016-12-16 10:01:55 +0800569
570const struct amdgpu_ip_block_version psp_v10_0_ip_block =
571{
572 .type = AMD_IP_BLOCK_TYPE_PSP,
573 .major = 10,
574 .minor = 0,
575 .rev = 0,
576 .funcs = &psp_ip_funcs,
577};