blob: 68ccaedad0f7531031ae1a8c8d90589856db3709 [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>
27#include "drmP.h"
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"
33
34static void psp_set_funcs(struct amdgpu_device *adev);
35
36static int psp_early_init(void *handle)
37{
38 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
39
40 psp_set_funcs(adev);
41
42 return 0;
43}
44
45static int psp_sw_init(void *handle)
46{
47 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
48 struct psp_context *psp = &adev->psp;
49 int ret;
50
51 switch (adev->asic_type) {
52 case CHIP_VEGA10:
53 psp->init_microcode = psp_v3_1_init_microcode;
54 psp->bootloader_load_sysdrv = psp_v3_1_bootloader_load_sysdrv;
55 psp->bootloader_load_sos = psp_v3_1_bootloader_load_sos;
56 psp->prep_cmd_buf = psp_v3_1_prep_cmd_buf;
57 psp->ring_init = psp_v3_1_ring_init;
Huang Ruibe70bbd2017-03-21 18:36:57 +080058 psp->ring_create = psp_v3_1_ring_create;
Huang Rui0e5ca0d2017-03-03 18:37:23 -050059 psp->cmd_submit = psp_v3_1_cmd_submit;
60 psp->compare_sram_data = psp_v3_1_compare_sram_data;
61 psp->smu_reload_quirk = psp_v3_1_smu_reload_quirk;
62 break;
63 default:
64 return -EINVAL;
65 }
66
67 psp->adev = adev;
68
69 ret = psp_init_microcode(psp);
70 if (ret) {
71 DRM_ERROR("Failed to load psp firmware!\n");
72 return ret;
73 }
74
75 return 0;
76}
77
78static int psp_sw_fini(void *handle)
79{
80 return 0;
81}
82
83int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
84 uint32_t reg_val, uint32_t mask, bool check_changed)
85{
86 uint32_t val;
87 int i;
88 struct amdgpu_device *adev = psp->adev;
89
90 val = RREG32(reg_index);
91
92 for (i = 0; i < adev->usec_timeout; i++) {
93 if (check_changed) {
94 if (val != reg_val)
95 return 0;
96 } else {
97 if ((val & mask) == reg_val)
98 return 0;
99 }
100 udelay(1);
101 }
102
103 return -ETIME;
104}
105
106static int
107psp_cmd_submit_buf(struct psp_context *psp,
108 struct amdgpu_firmware_info *ucode,
109 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr,
110 int index)
111{
112 int ret;
113 struct amdgpu_bo *cmd_buf_bo;
114 uint64_t cmd_buf_mc_addr;
115 struct psp_gfx_cmd_resp *cmd_buf_mem;
116 struct amdgpu_device *adev = psp->adev;
117
118 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
119 AMDGPU_GEM_DOMAIN_VRAM,
120 &cmd_buf_bo, &cmd_buf_mc_addr,
121 (void **)&cmd_buf_mem);
122 if (ret)
123 return ret;
124
125 memset(cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
126
127 memcpy(cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
128
129 ret = psp_cmd_submit(psp, ucode, cmd_buf_mc_addr,
130 fence_mc_addr, index);
131
132 while (*((unsigned int *)psp->fence_buf) != index) {
133 msleep(1);
kbuild test robotca7f65c2017-03-31 18:15:10 +0800134 }
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500135
136 amdgpu_bo_free_kernel(&cmd_buf_bo,
137 &cmd_buf_mc_addr,
138 (void **)&cmd_buf_mem);
139
140 return ret;
141}
142
143static void psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp *cmd,
144 uint64_t tmr_mc, uint32_t size)
145{
146 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
147 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = (uint32_t)tmr_mc;
148 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = (uint32_t)(tmr_mc >> 32);
149 cmd->cmd.cmd_setup_tmr.buf_size = size;
150}
151
152/* Set up Trusted Memory Region */
153static int psp_tmr_init(struct psp_context *psp)
154{
155 int ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500156
157 /*
158 * Allocate 3M memory aligned to 1M from Frame Buffer (local
159 * physical).
160 *
161 * Note: this memory need be reserved till the driver
162 * uninitializes.
163 */
164 ret = amdgpu_bo_create_kernel(psp->adev, 0x300000, 0x100000,
165 AMDGPU_GEM_DOMAIN_VRAM,
166 &psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
Huang Rui6f2b1fc2017-03-21 16:18:11 +0800167
168 return ret;
169}
170
171static int psp_tmr_load(struct psp_context *psp)
172{
173 int ret;
174 struct psp_gfx_cmd_resp *cmd;
175
176 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
177 if (!cmd)
178 return -ENOMEM;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500179
180 psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000);
181
182 ret = psp_cmd_submit_buf(psp, NULL, cmd,
183 psp->fence_buf_mc_addr, 1);
184 if (ret)
Huang Rui6f2b1fc2017-03-21 16:18:11 +0800185 goto failed;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500186
187 kfree(cmd);
188
189 return 0;
190
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500191failed:
192 kfree(cmd);
193 return ret;
194}
195
196static void psp_prep_asd_cmd_buf(struct psp_gfx_cmd_resp *cmd,
197 uint64_t asd_mc, uint64_t asd_mc_shared,
198 uint32_t size, uint32_t shared_size)
199{
200 cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
201 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
202 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
203 cmd->cmd.cmd_load_ta.app_len = size;
204
205 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(asd_mc_shared);
206 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(asd_mc_shared);
207 cmd->cmd.cmd_load_ta.cmd_buf_len = shared_size;
208}
209
Huang Ruif5cfef92017-03-21 18:02:04 +0800210static int psp_asd_init(struct psp_context *psp)
211{
212 int ret;
213
214 /*
215 * Allocate 16k memory aligned to 4k from Frame Buffer (local
216 * physical) for shared ASD <-> Driver
217 */
218 ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE,
219 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
220 &psp->asd_shared_bo,
221 &psp->asd_shared_mc_addr,
222 &psp->asd_shared_buf);
223
224 return ret;
225}
226
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500227static int psp_asd_load(struct psp_context *psp)
228{
229 int ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500230 struct psp_gfx_cmd_resp *cmd;
231
232 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
233 if (!cmd)
234 return -ENOMEM;
235
Huang Rui2b0c3ae2017-03-22 10:16:05 +0800236 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
237 memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500238
Huang Ruif5cfef92017-03-21 18:02:04 +0800239 psp_prep_asd_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_shared_mc_addr,
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500240 psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
241
242 ret = psp_cmd_submit_buf(psp, NULL, cmd,
243 psp->fence_buf_mc_addr, 2);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500244
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500245 kfree(cmd);
246
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500247 return ret;
248}
249
Huang Ruibe70bbd2017-03-21 18:36:57 +0800250static int psp_hw_start(struct psp_context *psp)
251{
252 int ret;
253
254 ret = psp_bootloader_load_sysdrv(psp);
255 if (ret)
256 return ret;
257
258 ret = psp_bootloader_load_sos(psp);
259 if (ret)
260 return ret;
261
262 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
263 if (ret)
264 return ret;
265
266 ret = psp_tmr_load(psp);
267 if (ret)
268 return ret;
269
270 ret = psp_asd_load(psp);
271 if (ret)
272 return ret;
273
274 return 0;
275}
276
277static int psp_np_fw_load(struct psp_context *psp)
278{
279 int i, ret;
280 struct amdgpu_firmware_info *ucode;
281 struct amdgpu_device* adev = psp->adev;
282
283 for (i = 0; i < adev->firmware.max_ucodes; i++) {
284 ucode = &adev->firmware.ucode[i];
285 if (!ucode->fw)
286 continue;
287
288 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
289 psp_smu_reload_quirk(psp))
290 continue;
291
292 ret = psp_prep_cmd_buf(ucode, psp->cmd);
293 if (ret)
294 return ret;
295
296 ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
297 psp->fence_buf_mc_addr, i + 3);
298 if (ret)
299 return ret;
300
301#if 0
302 /* check if firmware loaded sucessfully */
303 if (!amdgpu_psp_check_fw_loading_status(adev, i))
304 return -EINVAL;
305#endif
306 }
307
308 return 0;
309}
310
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500311static int psp_load_fw(struct amdgpu_device *adev)
312{
313 int ret;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500314 struct psp_context *psp = &adev->psp;
Huang Ruibe70bbd2017-03-21 18:36:57 +0800315 struct psp_gfx_cmd_resp *cmd;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500316
317 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
318 if (!cmd)
319 return -ENOMEM;
320
Huang Ruibe70bbd2017-03-21 18:36:57 +0800321 psp->cmd = cmd;
322
Huang Rui53a5cf52017-03-21 16:51:00 +0800323 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
324 AMDGPU_GEM_DOMAIN_GTT,
325 &psp->fw_pri_bo,
326 &psp->fw_pri_mc_addr,
327 &psp->fw_pri_buf);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500328 if (ret)
329 goto failed;
330
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500331 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
332 AMDGPU_GEM_DOMAIN_VRAM,
333 &psp->fence_buf_bo,
334 &psp->fence_buf_mc_addr,
335 &psp->fence_buf);
336 if (ret)
Huang Rui53a5cf52017-03-21 16:51:00 +0800337 goto failed_mem1;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500338
339 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
340
Huang Ruibe70bbd2017-03-21 18:36:57 +0800341 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500342 if (ret)
Huang Ruibe70bbd2017-03-21 18:36:57 +0800343 goto failed_mem1;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500344
Huang Ruibe70bbd2017-03-21 18:36:57 +0800345 ret = psp_tmr_init(psp);
Huang Rui6f2b1fc2017-03-21 16:18:11 +0800346 if (ret)
347 goto failed_mem;
348
Huang Ruif5cfef92017-03-21 18:02:04 +0800349 ret = psp_asd_init(psp);
350 if (ret)
351 goto failed_mem;
352
Huang Ruibe70bbd2017-03-21 18:36:57 +0800353 ret = psp_hw_start(psp);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500354 if (ret)
355 goto failed_mem;
356
Huang Ruibe70bbd2017-03-21 18:36:57 +0800357 ret = psp_np_fw_load(psp);
358 if (ret)
359 goto failed_mem;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500360
361 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
362 &psp->fence_buf_mc_addr, &psp->fence_buf);
363 kfree(cmd);
364
365 return 0;
366
367failed_mem:
368 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
369 &psp->fence_buf_mc_addr, &psp->fence_buf);
Huang Rui53a5cf52017-03-21 16:51:00 +0800370failed_mem1:
371 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
372 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500373failed:
374 kfree(cmd);
375 return ret;
376}
377
378static int psp_hw_init(void *handle)
379{
380 int ret;
381 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
382
383
384 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
385 return 0;
386
387 mutex_lock(&adev->firmware.mutex);
388 /*
389 * This sequence is just used on hw_init only once, no need on
390 * resume.
391 */
392 ret = amdgpu_ucode_init_bo(adev);
393 if (ret)
394 goto failed;
395
396 ret = psp_load_fw(adev);
397 if (ret) {
398 DRM_ERROR("PSP firmware loading failed\n");
399 goto failed;
400 }
401
402 mutex_unlock(&adev->firmware.mutex);
403 return 0;
404
405failed:
406 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
407 mutex_unlock(&adev->firmware.mutex);
408 return -EINVAL;
409}
410
411static int psp_hw_fini(void *handle)
412{
413 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
414 struct psp_context *psp = &adev->psp;
415
416 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)
417 amdgpu_ucode_fini_bo(adev);
418
419 if (psp->tmr_buf)
420 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
421
Huang Rui53a5cf52017-03-21 16:51:00 +0800422 if (psp->fw_pri_buf)
423 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
424 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
425
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500426 return 0;
427}
428
429static int psp_suspend(void *handle)
430{
431 return 0;
432}
433
434static int psp_resume(void *handle)
435{
436 int ret;
437 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Huang Rui93ea9b92017-03-23 11:20:25 +0800438 struct psp_context *psp = &adev->psp;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500439
440 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
441 return 0;
442
Huang Rui93ea9b92017-03-23 11:20:25 +0800443 DRM_INFO("PSP is resuming...\n");
444
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500445 mutex_lock(&adev->firmware.mutex);
446
Huang Rui93ea9b92017-03-23 11:20:25 +0800447 ret = psp_hw_start(psp);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500448 if (ret)
Huang Rui93ea9b92017-03-23 11:20:25 +0800449 goto failed;
450
451 ret = psp_np_fw_load(psp);
452 if (ret)
453 goto failed;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500454
455 mutex_unlock(&adev->firmware.mutex);
456
Huang Rui93ea9b92017-03-23 11:20:25 +0800457 return 0;
458
459failed:
460 DRM_ERROR("PSP resume failed\n");
461 mutex_unlock(&adev->firmware.mutex);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500462 return ret;
463}
464
465static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
466 enum AMDGPU_UCODE_ID ucode_type)
467{
468 struct amdgpu_firmware_info *ucode = NULL;
469
470 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
471 DRM_INFO("firmware is not loaded by PSP\n");
472 return true;
473 }
474
475 if (!adev->firmware.fw_size)
476 return false;
477
478 ucode = &adev->firmware.ucode[ucode_type];
479 if (!ucode->fw || !ucode->ucode_size)
480 return false;
481
482 return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
483}
484
485static int psp_set_clockgating_state(void *handle,
486 enum amd_clockgating_state state)
487{
488 return 0;
489}
490
491static int psp_set_powergating_state(void *handle,
492 enum amd_powergating_state state)
493{
494 return 0;
495}
496
497const struct amd_ip_funcs psp_ip_funcs = {
498 .name = "psp",
499 .early_init = psp_early_init,
500 .late_init = NULL,
501 .sw_init = psp_sw_init,
502 .sw_fini = psp_sw_fini,
503 .hw_init = psp_hw_init,
504 .hw_fini = psp_hw_fini,
505 .suspend = psp_suspend,
506 .resume = psp_resume,
507 .is_idle = NULL,
508 .wait_for_idle = NULL,
509 .soft_reset = NULL,
510 .set_clockgating_state = psp_set_clockgating_state,
511 .set_powergating_state = psp_set_powergating_state,
512};
513
514static const struct amdgpu_psp_funcs psp_funcs = {
515 .check_fw_loading_status = psp_check_fw_loading_status,
516};
517
518static void psp_set_funcs(struct amdgpu_device *adev)
519{
520 if (NULL == adev->firmware.funcs)
521 adev->firmware.funcs = &psp_funcs;
522}
523
524const struct amdgpu_ip_block_version psp_v3_1_ip_block =
525{
526 .type = AMD_IP_BLOCK_TYPE_PSP,
527 .major = 3,
528 .minor = 1,
529 .rev = 0,
530 .funcs = &psp_ip_funcs,
531};