blob: f70ab550934c67b7e7199b2e8c84afdceade161d [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;
58 psp->cmd_submit = psp_v3_1_cmd_submit;
59 psp->compare_sram_data = psp_v3_1_compare_sram_data;
60 psp->smu_reload_quirk = psp_v3_1_smu_reload_quirk;
61 break;
62 default:
63 return -EINVAL;
64 }
65
66 psp->adev = adev;
67
68 ret = psp_init_microcode(psp);
69 if (ret) {
70 DRM_ERROR("Failed to load psp firmware!\n");
71 return ret;
72 }
73
74 return 0;
75}
76
77static int psp_sw_fini(void *handle)
78{
79 return 0;
80}
81
82int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
83 uint32_t reg_val, uint32_t mask, bool check_changed)
84{
85 uint32_t val;
86 int i;
87 struct amdgpu_device *adev = psp->adev;
88
89 val = RREG32(reg_index);
90
91 for (i = 0; i < adev->usec_timeout; i++) {
92 if (check_changed) {
93 if (val != reg_val)
94 return 0;
95 } else {
96 if ((val & mask) == reg_val)
97 return 0;
98 }
99 udelay(1);
100 }
101
102 return -ETIME;
103}
104
105static int
106psp_cmd_submit_buf(struct psp_context *psp,
107 struct amdgpu_firmware_info *ucode,
108 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr,
109 int index)
110{
111 int ret;
112 struct amdgpu_bo *cmd_buf_bo;
113 uint64_t cmd_buf_mc_addr;
114 struct psp_gfx_cmd_resp *cmd_buf_mem;
115 struct amdgpu_device *adev = psp->adev;
116
117 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
118 AMDGPU_GEM_DOMAIN_VRAM,
119 &cmd_buf_bo, &cmd_buf_mc_addr,
120 (void **)&cmd_buf_mem);
121 if (ret)
122 return ret;
123
124 memset(cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
125
126 memcpy(cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
127
128 ret = psp_cmd_submit(psp, ucode, cmd_buf_mc_addr,
129 fence_mc_addr, index);
130
131 while (*((unsigned int *)psp->fence_buf) != index) {
132 msleep(1);
kbuild test robotca7f65c2017-03-31 18:15:10 +0800133 }
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500134
135 amdgpu_bo_free_kernel(&cmd_buf_bo,
136 &cmd_buf_mc_addr,
137 (void **)&cmd_buf_mem);
138
139 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;
146 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = (uint32_t)tmr_mc;
147 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = (uint32_t)(tmr_mc >> 32);
148 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
209static int psp_asd_load(struct psp_context *psp)
210{
211 int ret;
212 struct amdgpu_bo *asd_bo, *asd_shared_bo;
213 uint64_t asd_mc_addr, asd_shared_mc_addr;
214 void *asd_buf, *asd_shared_buf;
215 struct psp_gfx_cmd_resp *cmd;
216
217 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
218 if (!cmd)
219 return -ENOMEM;
220
221 /*
222 * Allocate 16k memory aligned to 4k from Frame Buffer (local
223 * physical) for shared ASD <-> Driver
224 */
225 ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE, PAGE_SIZE,
226 AMDGPU_GEM_DOMAIN_VRAM,
227 &asd_shared_bo, &asd_shared_mc_addr, &asd_buf);
228 if (ret)
229 goto failed;
230
231 /*
232 * Allocate 256k memory aligned to 4k from Frame Buffer (local
233 * physical) for ASD firmware
234 */
235 ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_BIN_SIZE, PAGE_SIZE,
236 AMDGPU_GEM_DOMAIN_VRAM,
237 &asd_bo, &asd_mc_addr, &asd_buf);
238 if (ret)
239 goto failed_mem;
240
241 memcpy(asd_buf, psp->asd_start_addr, psp->asd_ucode_size);
242
243 psp_prep_asd_cmd_buf(cmd, asd_mc_addr, asd_shared_mc_addr,
244 psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
245
246 ret = psp_cmd_submit_buf(psp, NULL, cmd,
247 psp->fence_buf_mc_addr, 2);
248 if (ret)
249 goto failed_mem1;
250
251 amdgpu_bo_free_kernel(&asd_bo, &asd_mc_addr, &asd_buf);
252 amdgpu_bo_free_kernel(&asd_shared_bo, &asd_shared_mc_addr, &asd_shared_buf);
253 kfree(cmd);
254
255 return 0;
256
257failed_mem1:
258 amdgpu_bo_free_kernel(&asd_bo, &asd_mc_addr, &asd_buf);
259failed_mem:
260 amdgpu_bo_free_kernel(&asd_shared_bo, &asd_shared_mc_addr, &asd_shared_buf);
261failed:
262 kfree(cmd);
263 return ret;
264}
265
266static int psp_load_fw(struct amdgpu_device *adev)
267{
268 int ret;
269 struct psp_gfx_cmd_resp *cmd;
270 int i;
271 struct amdgpu_firmware_info *ucode;
272 struct psp_context *psp = &adev->psp;
273
274 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
275 if (!cmd)
276 return -ENOMEM;
277
Huang Rui53a5cf52017-03-21 16:51:00 +0800278 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
279 AMDGPU_GEM_DOMAIN_GTT,
280 &psp->fw_pri_bo,
281 &psp->fw_pri_mc_addr,
282 &psp->fw_pri_buf);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500283 if (ret)
284 goto failed;
285
Huang Rui53a5cf52017-03-21 16:51:00 +0800286 ret = psp_bootloader_load_sysdrv(psp);
287 if (ret)
288 goto failed_mem1;
289
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500290 ret = psp_bootloader_load_sos(psp);
291 if (ret)
Huang Rui53a5cf52017-03-21 16:51:00 +0800292 goto failed_mem1;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500293
294 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
295 if (ret)
Huang Rui53a5cf52017-03-21 16:51:00 +0800296 goto failed_mem1;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500297
298 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
299 AMDGPU_GEM_DOMAIN_VRAM,
300 &psp->fence_buf_bo,
301 &psp->fence_buf_mc_addr,
302 &psp->fence_buf);
303 if (ret)
Huang Rui53a5cf52017-03-21 16:51:00 +0800304 goto failed_mem1;
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500305
306 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
307
308 ret = psp_tmr_init(psp);
309 if (ret)
310 goto failed_mem;
311
Huang Rui6f2b1fc2017-03-21 16:18:11 +0800312 ret = psp_tmr_load(psp);
313 if (ret)
314 goto failed_mem;
315
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500316 ret = psp_asd_load(psp);
317 if (ret)
318 goto failed_mem;
319
320 for (i = 0; i < adev->firmware.max_ucodes; i++) {
321 ucode = &adev->firmware.ucode[i];
322 if (!ucode->fw)
323 continue;
324
325 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
326 psp_smu_reload_quirk(psp))
327 continue;
328
329 ret = psp_prep_cmd_buf(ucode, cmd);
330 if (ret)
331 goto failed_mem;
332
333 ret = psp_cmd_submit_buf(psp, ucode, cmd,
334 psp->fence_buf_mc_addr, i + 3);
335 if (ret)
336 goto failed_mem;
337
338#if 0
339 /* check if firmware loaded sucessfully */
340 if (!amdgpu_psp_check_fw_loading_status(adev, i))
341 return -EINVAL;
342#endif
343 }
344
345 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
346 &psp->fence_buf_mc_addr, &psp->fence_buf);
347 kfree(cmd);
348
349 return 0;
350
351failed_mem:
352 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
353 &psp->fence_buf_mc_addr, &psp->fence_buf);
Huang Rui53a5cf52017-03-21 16:51:00 +0800354failed_mem1:
355 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
356 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500357failed:
358 kfree(cmd);
359 return ret;
360}
361
362static int psp_hw_init(void *handle)
363{
364 int ret;
365 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
366
367
368 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
369 return 0;
370
371 mutex_lock(&adev->firmware.mutex);
372 /*
373 * This sequence is just used on hw_init only once, no need on
374 * resume.
375 */
376 ret = amdgpu_ucode_init_bo(adev);
377 if (ret)
378 goto failed;
379
380 ret = psp_load_fw(adev);
381 if (ret) {
382 DRM_ERROR("PSP firmware loading failed\n");
383 goto failed;
384 }
385
386 mutex_unlock(&adev->firmware.mutex);
387 return 0;
388
389failed:
390 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
391 mutex_unlock(&adev->firmware.mutex);
392 return -EINVAL;
393}
394
395static int psp_hw_fini(void *handle)
396{
397 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
398 struct psp_context *psp = &adev->psp;
399
400 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)
401 amdgpu_ucode_fini_bo(adev);
402
403 if (psp->tmr_buf)
404 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
405
Huang Rui53a5cf52017-03-21 16:51:00 +0800406 if (psp->fw_pri_buf)
407 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
408 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
409
Huang Rui0e5ca0d2017-03-03 18:37:23 -0500410 return 0;
411}
412
413static int psp_suspend(void *handle)
414{
415 return 0;
416}
417
418static int psp_resume(void *handle)
419{
420 int ret;
421 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
422
423 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
424 return 0;
425
426 mutex_lock(&adev->firmware.mutex);
427
428 ret = psp_load_fw(adev);
429 if (ret)
430 DRM_ERROR("PSP resume failed\n");
431
432 mutex_unlock(&adev->firmware.mutex);
433
434 return ret;
435}
436
437static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
438 enum AMDGPU_UCODE_ID ucode_type)
439{
440 struct amdgpu_firmware_info *ucode = NULL;
441
442 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
443 DRM_INFO("firmware is not loaded by PSP\n");
444 return true;
445 }
446
447 if (!adev->firmware.fw_size)
448 return false;
449
450 ucode = &adev->firmware.ucode[ucode_type];
451 if (!ucode->fw || !ucode->ucode_size)
452 return false;
453
454 return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
455}
456
457static int psp_set_clockgating_state(void *handle,
458 enum amd_clockgating_state state)
459{
460 return 0;
461}
462
463static int psp_set_powergating_state(void *handle,
464 enum amd_powergating_state state)
465{
466 return 0;
467}
468
469const struct amd_ip_funcs psp_ip_funcs = {
470 .name = "psp",
471 .early_init = psp_early_init,
472 .late_init = NULL,
473 .sw_init = psp_sw_init,
474 .sw_fini = psp_sw_fini,
475 .hw_init = psp_hw_init,
476 .hw_fini = psp_hw_fini,
477 .suspend = psp_suspend,
478 .resume = psp_resume,
479 .is_idle = NULL,
480 .wait_for_idle = NULL,
481 .soft_reset = NULL,
482 .set_clockgating_state = psp_set_clockgating_state,
483 .set_powergating_state = psp_set_powergating_state,
484};
485
486static const struct amdgpu_psp_funcs psp_funcs = {
487 .check_fw_loading_status = psp_check_fw_loading_status,
488};
489
490static void psp_set_funcs(struct amdgpu_device *adev)
491{
492 if (NULL == adev->firmware.funcs)
493 adev->firmware.funcs = &psp_funcs;
494}
495
496const struct amdgpu_ip_block_version psp_v3_1_ip_block =
497{
498 .type = AMD_IP_BLOCK_TYPE_PSP,
499 .major = 3,
500 .minor = 1,
501 .rev = 0,
502 .funcs = &psp_ip_funcs,
503};