]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
drm/amdkfd: Store xcp partition id to amdgpu bo
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_psp.c
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>
27 #include <drm/drm_drv.h>
28
29 #include "amdgpu.h"
30 #include "amdgpu_psp.h"
31 #include "amdgpu_ucode.h"
32 #include "amdgpu_xgmi.h"
33 #include "soc15_common.h"
34 #include "psp_v3_1.h"
35 #include "psp_v10_0.h"
36 #include "psp_v11_0.h"
37 #include "psp_v11_0_8.h"
38 #include "psp_v12_0.h"
39 #include "psp_v13_0.h"
40 #include "psp_v13_0_4.h"
41
42 #include "amdgpu_ras.h"
43 #include "amdgpu_securedisplay.h"
44 #include "amdgpu_atomfirmware.h"
45
46 #define AMD_VBIOS_FILE_MAX_SIZE_B      (1024*1024*3)
47
48 static int psp_sysfs_init(struct amdgpu_device *adev);
49 static void psp_sysfs_fini(struct amdgpu_device *adev);
50
51 static int psp_load_smu_fw(struct psp_context *psp);
52 static int psp_rap_terminate(struct psp_context *psp);
53 static int psp_securedisplay_terminate(struct psp_context *psp);
54
55 static int psp_ring_init(struct psp_context *psp,
56                          enum psp_ring_type ring_type)
57 {
58         int ret = 0;
59         struct psp_ring *ring;
60         struct amdgpu_device *adev = psp->adev;
61
62         ring = &psp->km_ring;
63
64         ring->ring_type = ring_type;
65
66         /* allocate 4k Page of Local Frame Buffer memory for ring */
67         ring->ring_size = 0x1000;
68         ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
69                                       AMDGPU_GEM_DOMAIN_VRAM |
70                                       AMDGPU_GEM_DOMAIN_GTT,
71                                       &adev->firmware.rbuf,
72                                       &ring->ring_mem_mc_addr,
73                                       (void **)&ring->ring_mem);
74         if (ret) {
75                 ring->ring_size = 0;
76                 return ret;
77         }
78
79         return 0;
80 }
81
82 /*
83  * Due to DF Cstate management centralized to PMFW, the firmware
84  * loading sequence will be updated as below:
85  *   - Load KDB
86  *   - Load SYS_DRV
87  *   - Load tOS
88  *   - Load PMFW
89  *   - Setup TMR
90  *   - Load other non-psp fw
91  *   - Load ASD
92  *   - Load XGMI/RAS/HDCP/DTM TA if any
93  *
94  * This new sequence is required for
95  *   - Arcturus and onwards
96  */
97 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
98 {
99         struct amdgpu_device *adev = psp->adev;
100
101         if (amdgpu_sriov_vf(adev)) {
102                 psp->pmfw_centralized_cstate_management = false;
103                 return;
104         }
105
106         switch (adev->ip_versions[MP0_HWIP][0]) {
107         case IP_VERSION(11, 0, 0):
108         case IP_VERSION(11, 0, 4):
109         case IP_VERSION(11, 0, 5):
110         case IP_VERSION(11, 0, 7):
111         case IP_VERSION(11, 0, 9):
112         case IP_VERSION(11, 0, 11):
113         case IP_VERSION(11, 0, 12):
114         case IP_VERSION(11, 0, 13):
115         case IP_VERSION(13, 0, 0):
116         case IP_VERSION(13, 0, 2):
117         case IP_VERSION(13, 0, 7):
118                 psp->pmfw_centralized_cstate_management = true;
119                 break;
120         default:
121                 psp->pmfw_centralized_cstate_management = false;
122                 break;
123         }
124 }
125
126 static int psp_init_sriov_microcode(struct psp_context *psp)
127 {
128         struct amdgpu_device *adev = psp->adev;
129         char ucode_prefix[30];
130         int ret = 0;
131
132         amdgpu_ucode_ip_version_decode(adev, MP0_HWIP, ucode_prefix, sizeof(ucode_prefix));
133
134         switch (adev->ip_versions[MP0_HWIP][0]) {
135         case IP_VERSION(9, 0, 0):
136         case IP_VERSION(11, 0, 7):
137         case IP_VERSION(11, 0, 9):
138                 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
139                 ret = psp_init_cap_microcode(psp, ucode_prefix);
140                 break;
141         case IP_VERSION(13, 0, 2):
142                 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
143                 ret = psp_init_cap_microcode(psp, ucode_prefix);
144                 ret &= psp_init_ta_microcode(psp, ucode_prefix);
145                 break;
146         case IP_VERSION(13, 0, 0):
147                 adev->virt.autoload_ucode_id = 0;
148                 break;
149         case IP_VERSION(13, 0, 6):
150                 ret = psp_init_cap_microcode(psp, ucode_prefix);
151                 break;
152         case IP_VERSION(13, 0, 10):
153                 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
154                 ret = psp_init_cap_microcode(psp, ucode_prefix);
155                 break;
156         default:
157                 return -EINVAL;
158         }
159         return ret;
160 }
161
162 static int psp_early_init(void *handle)
163 {
164         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
165         struct psp_context *psp = &adev->psp;
166
167         switch (adev->ip_versions[MP0_HWIP][0]) {
168         case IP_VERSION(9, 0, 0):
169                 psp_v3_1_set_psp_funcs(psp);
170                 psp->autoload_supported = false;
171                 break;
172         case IP_VERSION(10, 0, 0):
173         case IP_VERSION(10, 0, 1):
174                 psp_v10_0_set_psp_funcs(psp);
175                 psp->autoload_supported = false;
176                 break;
177         case IP_VERSION(11, 0, 2):
178         case IP_VERSION(11, 0, 4):
179                 psp_v11_0_set_psp_funcs(psp);
180                 psp->autoload_supported = false;
181                 break;
182         case IP_VERSION(11, 0, 0):
183         case IP_VERSION(11, 0, 5):
184         case IP_VERSION(11, 0, 9):
185         case IP_VERSION(11, 0, 7):
186         case IP_VERSION(11, 0, 11):
187         case IP_VERSION(11, 5, 0):
188         case IP_VERSION(11, 0, 12):
189         case IP_VERSION(11, 0, 13):
190                 psp_v11_0_set_psp_funcs(psp);
191                 psp->autoload_supported = true;
192                 break;
193         case IP_VERSION(11, 0, 3):
194         case IP_VERSION(12, 0, 1):
195                 psp_v12_0_set_psp_funcs(psp);
196                 break;
197         case IP_VERSION(13, 0, 2):
198         case IP_VERSION(13, 0, 6):
199                 psp_v13_0_set_psp_funcs(psp);
200                 break;
201         case IP_VERSION(13, 0, 1):
202         case IP_VERSION(13, 0, 3):
203         case IP_VERSION(13, 0, 5):
204         case IP_VERSION(13, 0, 8):
205         case IP_VERSION(13, 0, 10):
206         case IP_VERSION(13, 0, 11):
207                 psp_v13_0_set_psp_funcs(psp);
208                 psp->autoload_supported = true;
209                 break;
210         case IP_VERSION(11, 0, 8):
211                 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
212                         psp_v11_0_8_set_psp_funcs(psp);
213                         psp->autoload_supported = false;
214                 }
215                 break;
216         case IP_VERSION(13, 0, 0):
217         case IP_VERSION(13, 0, 7):
218                 psp_v13_0_set_psp_funcs(psp);
219                 psp->autoload_supported = true;
220                 break;
221         case IP_VERSION(13, 0, 4):
222                 psp_v13_0_4_set_psp_funcs(psp);
223                 psp->autoload_supported = true;
224                 break;
225         default:
226                 return -EINVAL;
227         }
228
229         psp->adev = adev;
230
231         psp_check_pmfw_centralized_cstate_management(psp);
232
233         if (amdgpu_sriov_vf(adev))
234                 return psp_init_sriov_microcode(psp);
235         else
236                 return psp_init_microcode(psp);
237 }
238
239 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
240 {
241         amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
242                               &mem_ctx->shared_buf);
243         mem_ctx->shared_bo = NULL;
244 }
245
246 static void psp_free_shared_bufs(struct psp_context *psp)
247 {
248         void *tmr_buf;
249         void **pptr;
250
251         /* free TMR memory buffer */
252         pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
253         amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
254         psp->tmr_bo = NULL;
255
256         /* free xgmi shared memory */
257         psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
258
259         /* free ras shared memory */
260         psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
261
262         /* free hdcp shared memory */
263         psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
264
265         /* free dtm shared memory */
266         psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
267
268         /* free rap shared memory */
269         psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
270
271         /* free securedisplay shared memory */
272         psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
273
274
275 }
276
277 static void psp_memory_training_fini(struct psp_context *psp)
278 {
279         struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
280
281         ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
282         kfree(ctx->sys_cache);
283         ctx->sys_cache = NULL;
284 }
285
286 static int psp_memory_training_init(struct psp_context *psp)
287 {
288         int ret;
289         struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
290
291         if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
292                 DRM_DEBUG("memory training is not supported!\n");
293                 return 0;
294         }
295
296         ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
297         if (ctx->sys_cache == NULL) {
298                 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
299                 ret = -ENOMEM;
300                 goto Err_out;
301         }
302
303         DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
304                   ctx->train_data_size,
305                   ctx->p2c_train_data_offset,
306                   ctx->c2p_train_data_offset);
307         ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
308         return 0;
309
310 Err_out:
311         psp_memory_training_fini(psp);
312         return ret;
313 }
314
315 /*
316  * Helper funciton to query psp runtime database entry
317  *
318  * @adev: amdgpu_device pointer
319  * @entry_type: the type of psp runtime database entry
320  * @db_entry: runtime database entry pointer
321  *
322  * Return false if runtime database doesn't exit or entry is invalid
323  * or true if the specific database entry is found, and copy to @db_entry
324  */
325 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
326                                      enum psp_runtime_entry_type entry_type,
327                                      void *db_entry)
328 {
329         uint64_t db_header_pos, db_dir_pos;
330         struct psp_runtime_data_header db_header = {0};
331         struct psp_runtime_data_directory db_dir = {0};
332         bool ret = false;
333         int i;
334
335         if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 6))
336                 return false;
337
338         db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
339         db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
340
341         /* read runtime db header from vram */
342         amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
343                         sizeof(struct psp_runtime_data_header), false);
344
345         if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
346                 /* runtime db doesn't exist, exit */
347                 dev_dbg(adev->dev, "PSP runtime database doesn't exist\n");
348                 return false;
349         }
350
351         /* read runtime database entry from vram */
352         amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
353                         sizeof(struct psp_runtime_data_directory), false);
354
355         if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
356                 /* invalid db entry count, exit */
357                 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
358                 return false;
359         }
360
361         /* look up for requested entry type */
362         for (i = 0; i < db_dir.entry_count && !ret; i++) {
363                 if (db_dir.entry_list[i].entry_type == entry_type) {
364                         switch (entry_type) {
365                         case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
366                                 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
367                                         /* invalid db entry size */
368                                         dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
369                                         return false;
370                                 }
371                                 /* read runtime database entry */
372                                 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
373                                                           (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
374                                 ret = true;
375                                 break;
376                         case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
377                                 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
378                                         /* invalid db entry size */
379                                         dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
380                                         return false;
381                                 }
382                                 /* read runtime database entry */
383                                 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
384                                                           (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
385                                 ret = true;
386                                 break;
387                         default:
388                                 ret = false;
389                                 break;
390                         }
391                 }
392         }
393
394         return ret;
395 }
396
397 static int psp_sw_init(void *handle)
398 {
399         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
400         struct psp_context *psp = &adev->psp;
401         int ret;
402         struct psp_runtime_boot_cfg_entry boot_cfg_entry;
403         struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
404         struct psp_runtime_scpm_entry scpm_entry;
405
406         psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
407         if (!psp->cmd) {
408                 DRM_ERROR("Failed to allocate memory to command buffer!\n");
409                 ret = -ENOMEM;
410         }
411
412         adev->psp.xgmi_context.supports_extended_data =
413                 !adev->gmc.xgmi.connected_to_cpu &&
414                         adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
415
416         memset(&scpm_entry, 0, sizeof(scpm_entry));
417         if ((psp_get_runtime_db_entry(adev,
418                                 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
419                                 &scpm_entry)) &&
420             (scpm_entry.scpm_status != SCPM_DISABLE)) {
421                 adev->scpm_enabled = true;
422                 adev->scpm_status = scpm_entry.scpm_status;
423         } else {
424                 adev->scpm_enabled = false;
425                 adev->scpm_status = SCPM_DISABLE;
426         }
427
428         /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
429
430         memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
431         if (psp_get_runtime_db_entry(adev,
432                                 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
433                                 &boot_cfg_entry)) {
434                 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
435                 if ((psp->boot_cfg_bitmask) &
436                     BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
437                         /* If psp runtime database exists, then
438                          * only enable two stage memory training
439                          * when TWO_STAGE_DRAM_TRAINING bit is set
440                          * in runtime database */
441                         mem_training_ctx->enable_mem_training = true;
442                 }
443
444         } else {
445                 /* If psp runtime database doesn't exist or
446                  * is invalid, force enable two stage memory
447                  * training */
448                 mem_training_ctx->enable_mem_training = true;
449         }
450
451         if (mem_training_ctx->enable_mem_training) {
452                 ret = psp_memory_training_init(psp);
453                 if (ret) {
454                         DRM_ERROR("Failed to initialize memory training!\n");
455                         return ret;
456                 }
457
458                 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
459                 if (ret) {
460                         DRM_ERROR("Failed to process memory training!\n");
461                         return ret;
462                 }
463         }
464
465         if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
466             adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
467                 ret = psp_sysfs_init(adev);
468                 if (ret)
469                         return ret;
470         }
471
472         ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
473                                       amdgpu_sriov_vf(adev) ?
474                                       AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
475                                       &psp->fw_pri_bo,
476                                       &psp->fw_pri_mc_addr,
477                                       &psp->fw_pri_buf);
478         if (ret)
479                 return ret;
480
481         ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
482                                       AMDGPU_GEM_DOMAIN_VRAM |
483                                       AMDGPU_GEM_DOMAIN_GTT,
484                                       &psp->fence_buf_bo,
485                                       &psp->fence_buf_mc_addr,
486                                       &psp->fence_buf);
487         if (ret)
488                 goto failed1;
489
490         ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
491                                       AMDGPU_GEM_DOMAIN_VRAM |
492                                       AMDGPU_GEM_DOMAIN_GTT,
493                                       &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
494                                       (void **)&psp->cmd_buf_mem);
495         if (ret)
496                 goto failed2;
497
498         return 0;
499
500 failed2:
501         amdgpu_bo_free_kernel(&psp->fw_pri_bo,
502                               &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
503 failed1:
504         amdgpu_bo_free_kernel(&psp->fence_buf_bo,
505                               &psp->fence_buf_mc_addr, &psp->fence_buf);
506         return ret;
507 }
508
509 static int psp_sw_fini(void *handle)
510 {
511         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
512         struct psp_context *psp = &adev->psp;
513         struct psp_gfx_cmd_resp *cmd = psp->cmd;
514
515         psp_memory_training_fini(psp);
516
517         amdgpu_ucode_release(&psp->sos_fw);
518         amdgpu_ucode_release(&psp->asd_fw);
519         amdgpu_ucode_release(&psp->ta_fw);
520         amdgpu_ucode_release(&psp->cap_fw);
521         amdgpu_ucode_release(&psp->toc_fw);
522
523         if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
524             adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
525                 psp_sysfs_fini(adev);
526
527         kfree(cmd);
528         cmd = NULL;
529
530         psp_free_shared_bufs(psp);
531
532         if (psp->km_ring.ring_mem)
533                 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
534                                       &psp->km_ring.ring_mem_mc_addr,
535                                       (void **)&psp->km_ring.ring_mem);
536
537         amdgpu_bo_free_kernel(&psp->fw_pri_bo,
538                               &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
539         amdgpu_bo_free_kernel(&psp->fence_buf_bo,
540                               &psp->fence_buf_mc_addr, &psp->fence_buf);
541         amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
542                               (void **)&psp->cmd_buf_mem);
543
544         return 0;
545 }
546
547 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
548                  uint32_t reg_val, uint32_t mask, bool check_changed)
549 {
550         uint32_t val;
551         int i;
552         struct amdgpu_device *adev = psp->adev;
553
554         if (psp->adev->no_hw_access)
555                 return 0;
556
557         for (i = 0; i < adev->usec_timeout; i++) {
558                 val = RREG32(reg_index);
559                 if (check_changed) {
560                         if (val != reg_val)
561                                 return 0;
562                 } else {
563                         if ((val & mask) == reg_val)
564                                 return 0;
565                 }
566                 udelay(1);
567         }
568
569         return -ETIME;
570 }
571
572 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
573 {
574         switch (cmd_id) {
575         case GFX_CMD_ID_LOAD_TA:
576                 return "LOAD_TA";
577         case GFX_CMD_ID_UNLOAD_TA:
578                 return "UNLOAD_TA";
579         case GFX_CMD_ID_INVOKE_CMD:
580                 return "INVOKE_CMD";
581         case GFX_CMD_ID_LOAD_ASD:
582                 return "LOAD_ASD";
583         case GFX_CMD_ID_SETUP_TMR:
584                 return "SETUP_TMR";
585         case GFX_CMD_ID_LOAD_IP_FW:
586                 return "LOAD_IP_FW";
587         case GFX_CMD_ID_DESTROY_TMR:
588                 return "DESTROY_TMR";
589         case GFX_CMD_ID_SAVE_RESTORE:
590                 return "SAVE_RESTORE_IP_FW";
591         case GFX_CMD_ID_SETUP_VMR:
592                 return "SETUP_VMR";
593         case GFX_CMD_ID_DESTROY_VMR:
594                 return "DESTROY_VMR";
595         case GFX_CMD_ID_PROG_REG:
596                 return "PROG_REG";
597         case GFX_CMD_ID_GET_FW_ATTESTATION:
598                 return "GET_FW_ATTESTATION";
599         case GFX_CMD_ID_LOAD_TOC:
600                 return "ID_LOAD_TOC";
601         case GFX_CMD_ID_AUTOLOAD_RLC:
602                 return "AUTOLOAD_RLC";
603         case GFX_CMD_ID_BOOT_CFG:
604                 return "BOOT_CFG";
605         default:
606                 return "UNKNOWN CMD";
607         }
608 }
609
610 static int
611 psp_cmd_submit_buf(struct psp_context *psp,
612                    struct amdgpu_firmware_info *ucode,
613                    struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
614 {
615         int ret;
616         int index;
617         int timeout = 20000;
618         bool ras_intr = false;
619         bool skip_unsupport = false;
620
621         if (psp->adev->no_hw_access)
622                 return 0;
623
624         memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
625
626         memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
627
628         index = atomic_inc_return(&psp->fence_value);
629         ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
630         if (ret) {
631                 atomic_dec(&psp->fence_value);
632                 goto exit;
633         }
634
635         amdgpu_device_invalidate_hdp(psp->adev, NULL);
636         while (*((unsigned int *)psp->fence_buf) != index) {
637                 if (--timeout == 0)
638                         break;
639                 /*
640                  * Shouldn't wait for timeout when err_event_athub occurs,
641                  * because gpu reset thread triggered and lock resource should
642                  * be released for psp resume sequence.
643                  */
644                 ras_intr = amdgpu_ras_intr_triggered();
645                 if (ras_intr)
646                         break;
647                 usleep_range(10, 100);
648                 amdgpu_device_invalidate_hdp(psp->adev, NULL);
649         }
650
651         /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
652         skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
653                 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
654
655         memcpy(&cmd->resp, &psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
656
657         /* In some cases, psp response status is not 0 even there is no
658          * problem while the command is submitted. Some version of PSP FW
659          * doesn't write 0 to that field.
660          * So here we would like to only print a warning instead of an error
661          * during psp initialization to avoid breaking hw_init and it doesn't
662          * return -EINVAL.
663          */
664         if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
665                 if (ucode)
666                         DRM_WARN("failed to load ucode %s(0x%X) ",
667                                   amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
668                 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
669                          psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
670                          psp->cmd_buf_mem->resp.status);
671                 /* If any firmware (including CAP) load fails under SRIOV, it should
672                  * return failure to stop the VF from initializing.
673                  * Also return failure in case of timeout
674                  */
675                 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
676                         ret = -EINVAL;
677                         goto exit;
678                 }
679         }
680
681         if (ucode) {
682                 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
683                 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
684         }
685
686 exit:
687         return ret;
688 }
689
690 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
691 {
692         struct psp_gfx_cmd_resp *cmd = psp->cmd;
693
694         mutex_lock(&psp->mutex);
695
696         memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
697
698         return cmd;
699 }
700
701 static void release_psp_cmd_buf(struct psp_context *psp)
702 {
703         mutex_unlock(&psp->mutex);
704 }
705
706 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
707                                  struct psp_gfx_cmd_resp *cmd,
708                                  uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
709 {
710         struct amdgpu_device *adev = psp->adev;
711         uint32_t size = 0;
712         uint64_t tmr_pa = 0;
713
714         if (tmr_bo) {
715                 size = amdgpu_bo_size(tmr_bo);
716                 tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
717         }
718
719         if (amdgpu_sriov_vf(psp->adev))
720                 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
721         else
722                 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
723         cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
724         cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
725         cmd->cmd.cmd_setup_tmr.buf_size = size;
726         cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
727         cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
728         cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
729 }
730
731 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
732                                       uint64_t pri_buf_mc, uint32_t size)
733 {
734         cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
735         cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
736         cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
737         cmd->cmd.cmd_load_toc.toc_size = size;
738 }
739
740 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
741 static int psp_load_toc(struct psp_context *psp,
742                         uint32_t *tmr_size)
743 {
744         int ret;
745         struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
746
747         /* Copy toc to psp firmware private buffer */
748         psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
749
750         psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
751
752         ret = psp_cmd_submit_buf(psp, NULL, cmd,
753                                  psp->fence_buf_mc_addr);
754         if (!ret)
755                 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
756
757         release_psp_cmd_buf(psp);
758
759         return ret;
760 }
761
762 static bool psp_boottime_tmr(struct psp_context *psp)
763 {
764         switch (psp->adev->ip_versions[MP0_HWIP][0]) {
765         case IP_VERSION(13, 0, 6):
766                 return true;
767         default:
768                 return false;
769         }
770 }
771
772 /* Set up Trusted Memory Region */
773 static int psp_tmr_init(struct psp_context *psp)
774 {
775         int ret = 0;
776         int tmr_size;
777         void *tmr_buf;
778         void **pptr;
779
780         /*
781          * According to HW engineer, they prefer the TMR address be "naturally
782          * aligned" , e.g. the start address be an integer divide of TMR size.
783          *
784          * Note: this memory need be reserved till the driver
785          * uninitializes.
786          */
787         tmr_size = PSP_TMR_SIZE(psp->adev);
788
789         /* For ASICs support RLC autoload, psp will parse the toc
790          * and calculate the total size of TMR needed */
791         if (!amdgpu_sriov_vf(psp->adev) &&
792             psp->toc.start_addr &&
793             psp->toc.size_bytes &&
794             psp->fw_pri_buf) {
795                 ret = psp_load_toc(psp, &tmr_size);
796                 if (ret) {
797                         DRM_ERROR("Failed to load toc\n");
798                         return ret;
799                 }
800         }
801
802         if (!psp->tmr_bo) {
803                 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
804                 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
805                                               PSP_TMR_ALIGNMENT,
806                                               AMDGPU_HAS_VRAM(psp->adev) ?
807                                               AMDGPU_GEM_DOMAIN_VRAM :
808                                               AMDGPU_GEM_DOMAIN_GTT,
809                                               &psp->tmr_bo, &psp->tmr_mc_addr,
810                                               pptr);
811         }
812
813         return ret;
814 }
815
816 static bool psp_skip_tmr(struct psp_context *psp)
817 {
818         switch (psp->adev->ip_versions[MP0_HWIP][0]) {
819         case IP_VERSION(11, 0, 9):
820         case IP_VERSION(11, 0, 7):
821         case IP_VERSION(13, 0, 2):
822         case IP_VERSION(13, 0, 10):
823                 return true;
824         default:
825                 return false;
826         }
827 }
828
829 static int psp_tmr_load(struct psp_context *psp)
830 {
831         int ret;
832         struct psp_gfx_cmd_resp *cmd;
833
834         /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
835          * Already set up by host driver.
836          */
837         if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
838                 return 0;
839
840         cmd = acquire_psp_cmd_buf(psp);
841
842         psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
843         if (psp->tmr_bo)
844                 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
845                          amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
846
847         ret = psp_cmd_submit_buf(psp, NULL, cmd,
848                                  psp->fence_buf_mc_addr);
849
850         release_psp_cmd_buf(psp);
851
852         return ret;
853 }
854
855 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
856                                         struct psp_gfx_cmd_resp *cmd)
857 {
858         if (amdgpu_sriov_vf(psp->adev))
859                 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
860         else
861                 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
862 }
863
864 static int psp_tmr_unload(struct psp_context *psp)
865 {
866         int ret;
867         struct psp_gfx_cmd_resp *cmd;
868
869         /* skip TMR unload for Navi12 and CHIP_SIENNA_CICHLID SRIOV,
870          * as TMR is not loaded at all
871          */
872         if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
873                 return 0;
874
875         cmd = acquire_psp_cmd_buf(psp);
876
877         psp_prep_tmr_unload_cmd_buf(psp, cmd);
878         dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
879
880         ret = psp_cmd_submit_buf(psp, NULL, cmd,
881                                  psp->fence_buf_mc_addr);
882
883         release_psp_cmd_buf(psp);
884
885         return ret;
886 }
887
888 static int psp_tmr_terminate(struct psp_context *psp)
889 {
890         return psp_tmr_unload(psp);
891 }
892
893 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
894                                         uint64_t *output_ptr)
895 {
896         int ret;
897         struct psp_gfx_cmd_resp *cmd;
898
899         if (!output_ptr)
900                 return -EINVAL;
901
902         if (amdgpu_sriov_vf(psp->adev))
903                 return 0;
904
905         cmd = acquire_psp_cmd_buf(psp);
906
907         cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
908
909         ret = psp_cmd_submit_buf(psp, NULL, cmd,
910                                  psp->fence_buf_mc_addr);
911
912         if (!ret) {
913                 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
914                               ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
915         }
916
917         release_psp_cmd_buf(psp);
918
919         return ret;
920 }
921
922 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
923 {
924         struct psp_context *psp = &adev->psp;
925         struct psp_gfx_cmd_resp *cmd;
926         int ret;
927
928         if (amdgpu_sriov_vf(adev))
929                 return 0;
930
931         cmd = acquire_psp_cmd_buf(psp);
932
933         cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
934         cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
935
936         ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
937         if (!ret) {
938                 *boot_cfg =
939                         (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
940         }
941
942         release_psp_cmd_buf(psp);
943
944         return ret;
945 }
946
947 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
948 {
949         int ret;
950         struct psp_context *psp = &adev->psp;
951         struct psp_gfx_cmd_resp *cmd;
952
953         if (amdgpu_sriov_vf(adev))
954                 return 0;
955
956         cmd = acquire_psp_cmd_buf(psp);
957
958         cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
959         cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
960         cmd->cmd.boot_cfg.boot_config = boot_cfg;
961         cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
962
963         ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
964
965         release_psp_cmd_buf(psp);
966
967         return ret;
968 }
969
970 static int psp_rl_load(struct amdgpu_device *adev)
971 {
972         int ret;
973         struct psp_context *psp = &adev->psp;
974         struct psp_gfx_cmd_resp *cmd;
975
976         if (!is_psp_fw_valid(psp->rl))
977                 return 0;
978
979         cmd = acquire_psp_cmd_buf(psp);
980
981         memset(psp->fw_pri_buf, 0, PSP_1_MEG);
982         memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
983
984         cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
985         cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
986         cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
987         cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
988         cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
989
990         ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
991
992         release_psp_cmd_buf(psp);
993
994         return ret;
995 }
996
997 int psp_spatial_partition(struct psp_context *psp, int mode)
998 {
999         struct psp_gfx_cmd_resp *cmd;
1000         int ret;
1001
1002         if (amdgpu_sriov_vf(psp->adev))
1003                 return 0;
1004
1005         cmd = acquire_psp_cmd_buf(psp);
1006
1007         cmd->cmd_id = GFX_CMD_ID_SRIOV_SPATIAL_PART;
1008         cmd->cmd.cmd_spatial_part.mode = mode;
1009
1010         dev_info(psp->adev->dev, "Requesting %d paritions through PSP", mode);
1011         ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1012
1013         release_psp_cmd_buf(psp);
1014
1015         return ret;
1016 }
1017
1018 static int psp_asd_initialize(struct psp_context *psp)
1019 {
1020         int ret;
1021
1022         /* If PSP version doesn't match ASD version, asd loading will be failed.
1023          * add workaround to bypass it for sriov now.
1024          * TODO: add version check to make it common
1025          */
1026         if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
1027                 return 0;
1028
1029         psp->asd_context.mem_context.shared_mc_addr  = 0;
1030         psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
1031         psp->asd_context.ta_load_type                = GFX_CMD_ID_LOAD_ASD;
1032
1033         ret = psp_ta_load(psp, &psp->asd_context);
1034         if (!ret)
1035                 psp->asd_context.initialized = true;
1036
1037         return ret;
1038 }
1039
1040 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1041                                        uint32_t session_id)
1042 {
1043         cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
1044         cmd->cmd.cmd_unload_ta.session_id = session_id;
1045 }
1046
1047 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
1048 {
1049         int ret;
1050         struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1051
1052         psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
1053
1054         ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1055
1056         context->resp_status = cmd->resp.status;
1057
1058         release_psp_cmd_buf(psp);
1059
1060         return ret;
1061 }
1062
1063 static int psp_asd_terminate(struct psp_context *psp)
1064 {
1065         int ret;
1066
1067         if (amdgpu_sriov_vf(psp->adev))
1068                 return 0;
1069
1070         if (!psp->asd_context.initialized)
1071                 return 0;
1072
1073         ret = psp_ta_unload(psp, &psp->asd_context);
1074         if (!ret)
1075                 psp->asd_context.initialized = false;
1076
1077         return ret;
1078 }
1079
1080 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1081                 uint32_t id, uint32_t value)
1082 {
1083         cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1084         cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1085         cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1086 }
1087
1088 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1089                 uint32_t value)
1090 {
1091         struct psp_gfx_cmd_resp *cmd;
1092         int ret = 0;
1093
1094         if (reg >= PSP_REG_LAST)
1095                 return -EINVAL;
1096
1097         cmd = acquire_psp_cmd_buf(psp);
1098
1099         psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1100         ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1101         if (ret)
1102                 DRM_ERROR("PSP failed to program reg id %d", reg);
1103
1104         release_psp_cmd_buf(psp);
1105
1106         return ret;
1107 }
1108
1109 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1110                                      uint64_t ta_bin_mc,
1111                                      struct ta_context *context)
1112 {
1113         cmd->cmd_id                             = context->ta_load_type;
1114         cmd->cmd.cmd_load_ta.app_phy_addr_lo    = lower_32_bits(ta_bin_mc);
1115         cmd->cmd.cmd_load_ta.app_phy_addr_hi    = upper_32_bits(ta_bin_mc);
1116         cmd->cmd.cmd_load_ta.app_len            = context->bin_desc.size_bytes;
1117
1118         cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1119                 lower_32_bits(context->mem_context.shared_mc_addr);
1120         cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1121                 upper_32_bits(context->mem_context.shared_mc_addr);
1122         cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1123 }
1124
1125 int psp_ta_init_shared_buf(struct psp_context *psp,
1126                                   struct ta_mem_context *mem_ctx)
1127 {
1128         /*
1129         * Allocate 16k memory aligned to 4k from Frame Buffer (local
1130         * physical) for ta to host memory
1131         */
1132         return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1133                                       PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1134                                       AMDGPU_GEM_DOMAIN_GTT,
1135                                       &mem_ctx->shared_bo,
1136                                       &mem_ctx->shared_mc_addr,
1137                                       &mem_ctx->shared_buf);
1138 }
1139
1140 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1141                                        uint32_t ta_cmd_id,
1142                                        uint32_t session_id)
1143 {
1144         cmd->cmd_id                             = GFX_CMD_ID_INVOKE_CMD;
1145         cmd->cmd.cmd_invoke_cmd.session_id      = session_id;
1146         cmd->cmd.cmd_invoke_cmd.ta_cmd_id       = ta_cmd_id;
1147 }
1148
1149 int psp_ta_invoke(struct psp_context *psp,
1150                   uint32_t ta_cmd_id,
1151                   struct ta_context *context)
1152 {
1153         int ret;
1154         struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1155
1156         psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1157
1158         ret = psp_cmd_submit_buf(psp, NULL, cmd,
1159                                  psp->fence_buf_mc_addr);
1160
1161         context->resp_status = cmd->resp.status;
1162
1163         release_psp_cmd_buf(psp);
1164
1165         return ret;
1166 }
1167
1168 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1169 {
1170         int ret;
1171         struct psp_gfx_cmd_resp *cmd;
1172
1173         cmd = acquire_psp_cmd_buf(psp);
1174
1175         psp_copy_fw(psp, context->bin_desc.start_addr,
1176                     context->bin_desc.size_bytes);
1177
1178         psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1179
1180         ret = psp_cmd_submit_buf(psp, NULL, cmd,
1181                                  psp->fence_buf_mc_addr);
1182
1183         context->resp_status = cmd->resp.status;
1184
1185         if (!ret)
1186                 context->session_id = cmd->resp.session_id;
1187
1188         release_psp_cmd_buf(psp);
1189
1190         return ret;
1191 }
1192
1193 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1194 {
1195         return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1196 }
1197
1198 int psp_xgmi_terminate(struct psp_context *psp)
1199 {
1200         int ret;
1201         struct amdgpu_device *adev = psp->adev;
1202
1203         /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1204         if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1205             (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1206              adev->gmc.xgmi.connected_to_cpu))
1207                 return 0;
1208
1209         if (!psp->xgmi_context.context.initialized)
1210                 return 0;
1211
1212         ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1213
1214         psp->xgmi_context.context.initialized = false;
1215
1216         return ret;
1217 }
1218
1219 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1220 {
1221         struct ta_xgmi_shared_memory *xgmi_cmd;
1222         int ret;
1223
1224         if (!psp->ta_fw ||
1225             !psp->xgmi_context.context.bin_desc.size_bytes ||
1226             !psp->xgmi_context.context.bin_desc.start_addr)
1227                 return -ENOENT;
1228
1229         if (!load_ta)
1230                 goto invoke;
1231
1232         psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1233         psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1234
1235         if (!psp->xgmi_context.context.mem_context.shared_buf) {
1236                 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1237                 if (ret)
1238                         return ret;
1239         }
1240
1241         /* Load XGMI TA */
1242         ret = psp_ta_load(psp, &psp->xgmi_context.context);
1243         if (!ret)
1244                 psp->xgmi_context.context.initialized = true;
1245         else
1246                 return ret;
1247
1248 invoke:
1249         /* Initialize XGMI session */
1250         xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1251         memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1252         xgmi_cmd->flag_extend_link_record = set_extended_data;
1253         xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1254
1255         ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1256
1257         return ret;
1258 }
1259
1260 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1261 {
1262         struct ta_xgmi_shared_memory *xgmi_cmd;
1263         int ret;
1264
1265         xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1266         memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1267
1268         xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1269
1270         /* Invoke xgmi ta to get hive id */
1271         ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1272         if (ret)
1273                 return ret;
1274
1275         *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1276
1277         return 0;
1278 }
1279
1280 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1281 {
1282         struct ta_xgmi_shared_memory *xgmi_cmd;
1283         int ret;
1284
1285         xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1286         memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1287
1288         xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1289
1290         /* Invoke xgmi ta to get the node id */
1291         ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1292         if (ret)
1293                 return ret;
1294
1295         *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1296
1297         return 0;
1298 }
1299
1300 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1301 {
1302         return (psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1303                 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b) ||
1304                 psp->adev->ip_versions[MP0_HWIP][0] >= IP_VERSION(13, 0, 6);
1305 }
1306
1307 /*
1308  * Chips that support extended topology information require the driver to
1309  * reflect topology information in the opposite direction.  This is
1310  * because the TA has already exceeded its link record limit and if the
1311  * TA holds bi-directional information, the driver would have to do
1312  * multiple fetches instead of just two.
1313  */
1314 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1315                                         struct psp_xgmi_node_info node_info)
1316 {
1317         struct amdgpu_device *mirror_adev;
1318         struct amdgpu_hive_info *hive;
1319         uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1320         uint64_t dst_node_id = node_info.node_id;
1321         uint8_t dst_num_hops = node_info.num_hops;
1322         uint8_t dst_num_links = node_info.num_links;
1323
1324         hive = amdgpu_get_xgmi_hive(psp->adev);
1325         list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1326                 struct psp_xgmi_topology_info *mirror_top_info;
1327                 int j;
1328
1329                 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1330                         continue;
1331
1332                 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1333                 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1334                         if (mirror_top_info->nodes[j].node_id != src_node_id)
1335                                 continue;
1336
1337                         mirror_top_info->nodes[j].num_hops = dst_num_hops;
1338                         /*
1339                          * prevent 0 num_links value re-reflection since reflection
1340                          * criteria is based on num_hops (direct or indirect).
1341                          *
1342                          */
1343                         if (dst_num_links)
1344                                 mirror_top_info->nodes[j].num_links = dst_num_links;
1345
1346                         break;
1347                 }
1348
1349                 break;
1350         }
1351
1352         amdgpu_put_xgmi_hive(hive);
1353 }
1354
1355 int psp_xgmi_get_topology_info(struct psp_context *psp,
1356                                int number_devices,
1357                                struct psp_xgmi_topology_info *topology,
1358                                bool get_extended_data)
1359 {
1360         struct ta_xgmi_shared_memory *xgmi_cmd;
1361         struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1362         struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1363         int i;
1364         int ret;
1365
1366         if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1367                 return -EINVAL;
1368
1369         xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1370         memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1371         xgmi_cmd->flag_extend_link_record = get_extended_data;
1372
1373         /* Fill in the shared memory with topology information as input */
1374         topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1375         xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1376         topology_info_input->num_nodes = number_devices;
1377
1378         for (i = 0; i < topology_info_input->num_nodes; i++) {
1379                 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1380                 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1381                 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1382                 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1383         }
1384
1385         /* Invoke xgmi ta to get the topology information */
1386         ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1387         if (ret)
1388                 return ret;
1389
1390         /* Read the output topology information from the shared memory */
1391         topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1392         topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1393         for (i = 0; i < topology->num_nodes; i++) {
1394                 /* extended data will either be 0 or equal to non-extended data */
1395                 if (topology_info_output->nodes[i].num_hops)
1396                         topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1397
1398                 /* non-extended data gets everything here so no need to update */
1399                 if (!get_extended_data) {
1400                         topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1401                         topology->nodes[i].is_sharing_enabled =
1402                                         topology_info_output->nodes[i].is_sharing_enabled;
1403                         topology->nodes[i].sdma_engine =
1404                                         topology_info_output->nodes[i].sdma_engine;
1405                 }
1406
1407         }
1408
1409         /* Invoke xgmi ta again to get the link information */
1410         if (psp_xgmi_peer_link_info_supported(psp)) {
1411                 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1412                 bool requires_reflection =
1413                         (psp->xgmi_context.supports_extended_data && get_extended_data) ||
1414                                 psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 6);
1415
1416                 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1417
1418                 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1419
1420                 if (ret)
1421                         return ret;
1422
1423                 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1424                 for (i = 0; i < topology->num_nodes; i++) {
1425                         /* accumulate num_links on extended data */
1426                         topology->nodes[i].num_links = get_extended_data ?
1427                                         topology->nodes[i].num_links +
1428                                                         link_info_output->nodes[i].num_links :
1429                                         ((requires_reflection && topology->nodes[i].num_links) ? topology->nodes[i].num_links :
1430                                          link_info_output->nodes[i].num_links);
1431
1432                         /* reflect the topology information for bi-directionality */
1433                         if (requires_reflection && topology->nodes[i].num_hops)
1434                                 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1435                 }
1436         }
1437
1438         return 0;
1439 }
1440
1441 int psp_xgmi_set_topology_info(struct psp_context *psp,
1442                                int number_devices,
1443                                struct psp_xgmi_topology_info *topology)
1444 {
1445         struct ta_xgmi_shared_memory *xgmi_cmd;
1446         struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1447         int i;
1448
1449         if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1450                 return -EINVAL;
1451
1452         xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1453         memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1454
1455         topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1456         xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1457         topology_info_input->num_nodes = number_devices;
1458
1459         for (i = 0; i < topology_info_input->num_nodes; i++) {
1460                 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1461                 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1462                 topology_info_input->nodes[i].is_sharing_enabled = 1;
1463                 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1464         }
1465
1466         /* Invoke xgmi ta to set topology information */
1467         return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1468 }
1469
1470 // ras begin
1471 static void psp_ras_ta_check_status(struct psp_context *psp)
1472 {
1473         struct ta_ras_shared_memory *ras_cmd =
1474                 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1475
1476         switch (ras_cmd->ras_status) {
1477         case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1478                 dev_warn(psp->adev->dev,
1479                                 "RAS WARNING: cmd failed due to unsupported ip\n");
1480                 break;
1481         case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1482                 dev_warn(psp->adev->dev,
1483                                 "RAS WARNING: cmd failed due to unsupported error injection\n");
1484                 break;
1485         case TA_RAS_STATUS__SUCCESS:
1486                 break;
1487         case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1488                 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1489                         dev_warn(psp->adev->dev,
1490                                         "RAS WARNING: Inject error to critical region is not allowed\n");
1491                 break;
1492         default:
1493                 dev_warn(psp->adev->dev,
1494                                 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1495                 break;
1496         }
1497 }
1498
1499 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1500 {
1501         struct ta_ras_shared_memory *ras_cmd;
1502         int ret;
1503
1504         ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1505
1506         /*
1507          * TODO: bypass the loading in sriov for now
1508          */
1509         if (amdgpu_sriov_vf(psp->adev))
1510                 return 0;
1511
1512         ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1513
1514         if (amdgpu_ras_intr_triggered())
1515                 return ret;
1516
1517         if (ras_cmd->if_version > RAS_TA_HOST_IF_VER) {
1518                 DRM_WARN("RAS: Unsupported Interface");
1519                 return -EINVAL;
1520         }
1521
1522         if (!ret) {
1523                 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1524                         dev_warn(psp->adev->dev, "ECC switch disabled\n");
1525
1526                         ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1527                 } else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1528                         dev_warn(psp->adev->dev,
1529                                  "RAS internal register access blocked\n");
1530
1531                 psp_ras_ta_check_status(psp);
1532         }
1533
1534         return ret;
1535 }
1536
1537 int psp_ras_enable_features(struct psp_context *psp,
1538                 union ta_ras_cmd_input *info, bool enable)
1539 {
1540         struct ta_ras_shared_memory *ras_cmd;
1541         int ret;
1542
1543         if (!psp->ras_context.context.initialized)
1544                 return -EINVAL;
1545
1546         ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1547         memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1548
1549         if (enable)
1550                 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1551         else
1552                 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1553
1554         ras_cmd->ras_in_message = *info;
1555
1556         ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1557         if (ret)
1558                 return -EINVAL;
1559
1560         return 0;
1561 }
1562
1563 int psp_ras_terminate(struct psp_context *psp)
1564 {
1565         int ret;
1566
1567         /*
1568          * TODO: bypass the terminate in sriov for now
1569          */
1570         if (amdgpu_sriov_vf(psp->adev))
1571                 return 0;
1572
1573         if (!psp->ras_context.context.initialized)
1574                 return 0;
1575
1576         ret = psp_ta_unload(psp, &psp->ras_context.context);
1577
1578         psp->ras_context.context.initialized = false;
1579
1580         return ret;
1581 }
1582
1583 int psp_ras_initialize(struct psp_context *psp)
1584 {
1585         int ret;
1586         uint32_t boot_cfg = 0xFF;
1587         struct amdgpu_device *adev = psp->adev;
1588         struct ta_ras_shared_memory *ras_cmd;
1589
1590         /*
1591          * TODO: bypass the initialize in sriov for now
1592          */
1593         if (amdgpu_sriov_vf(adev))
1594                 return 0;
1595
1596         if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1597             !adev->psp.ras_context.context.bin_desc.start_addr) {
1598                 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1599                 return 0;
1600         }
1601
1602         if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1603                 /* query GECC enablement status from boot config
1604                  * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1605                  */
1606                 ret = psp_boot_config_get(adev, &boot_cfg);
1607                 if (ret)
1608                         dev_warn(adev->dev, "PSP get boot config failed\n");
1609
1610                 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1611                         if (!boot_cfg) {
1612                                 dev_info(adev->dev, "GECC is disabled\n");
1613                         } else {
1614                                 /* disable GECC in next boot cycle if ras is
1615                                  * disabled by module parameter amdgpu_ras_enable
1616                                  * and/or amdgpu_ras_mask, or boot_config_get call
1617                                  * is failed
1618                                  */
1619                                 ret = psp_boot_config_set(adev, 0);
1620                                 if (ret)
1621                                         dev_warn(adev->dev, "PSP set boot config failed\n");
1622                                 else
1623                                         dev_warn(adev->dev, "GECC will be disabled in next boot cycle if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1624                         }
1625                 } else {
1626                         if (boot_cfg == 1) {
1627                                 dev_info(adev->dev, "GECC is enabled\n");
1628                         } else {
1629                                 /* enable GECC in next boot cycle if it is disabled
1630                                  * in boot config, or force enable GECC if failed to
1631                                  * get boot configuration
1632                                  */
1633                                 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1634                                 if (ret)
1635                                         dev_warn(adev->dev, "PSP set boot config failed\n");
1636                                 else
1637                                         dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1638                         }
1639                 }
1640         }
1641
1642         psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1643         psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1644
1645         if (!psp->ras_context.context.mem_context.shared_buf) {
1646                 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1647                 if (ret)
1648                         return ret;
1649         }
1650
1651         ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1652         memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1653
1654         if (amdgpu_ras_is_poison_mode_supported(adev))
1655                 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1656         if (!adev->gmc.xgmi.connected_to_cpu)
1657                 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1658
1659         ret = psp_ta_load(psp, &psp->ras_context.context);
1660
1661         if (!ret && !ras_cmd->ras_status)
1662                 psp->ras_context.context.initialized = true;
1663         else {
1664                 if (ras_cmd->ras_status)
1665                         dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1666
1667                 /* fail to load RAS TA */
1668                 psp->ras_context.context.initialized = false;
1669         }
1670
1671         return ret;
1672 }
1673
1674 int psp_ras_trigger_error(struct psp_context *psp,
1675                           struct ta_ras_trigger_error_input *info)
1676 {
1677         struct ta_ras_shared_memory *ras_cmd;
1678         int ret;
1679
1680         if (!psp->ras_context.context.initialized)
1681                 return -EINVAL;
1682
1683         ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1684         memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1685
1686         ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1687         ras_cmd->ras_in_message.trigger_error = *info;
1688
1689         ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1690         if (ret)
1691                 return -EINVAL;
1692
1693         /* If err_event_athub occurs error inject was successful, however
1694            return status from TA is no long reliable */
1695         if (amdgpu_ras_intr_triggered())
1696                 return 0;
1697
1698         if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1699                 return -EACCES;
1700         else if (ras_cmd->ras_status)
1701                 return -EINVAL;
1702
1703         return 0;
1704 }
1705 // ras end
1706
1707 // HDCP start
1708 static int psp_hdcp_initialize(struct psp_context *psp)
1709 {
1710         int ret;
1711
1712         /*
1713          * TODO: bypass the initialize in sriov for now
1714          */
1715         if (amdgpu_sriov_vf(psp->adev))
1716                 return 0;
1717
1718         if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1719             !psp->hdcp_context.context.bin_desc.start_addr) {
1720                 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1721                 return 0;
1722         }
1723
1724         psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1725         psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1726
1727         if (!psp->hdcp_context.context.mem_context.shared_buf) {
1728                 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1729                 if (ret)
1730                         return ret;
1731         }
1732
1733         ret = psp_ta_load(psp, &psp->hdcp_context.context);
1734         if (!ret) {
1735                 psp->hdcp_context.context.initialized = true;
1736                 mutex_init(&psp->hdcp_context.mutex);
1737         }
1738
1739         return ret;
1740 }
1741
1742 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1743 {
1744         /*
1745          * TODO: bypass the loading in sriov for now
1746          */
1747         if (amdgpu_sriov_vf(psp->adev))
1748                 return 0;
1749
1750         return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1751 }
1752
1753 static int psp_hdcp_terminate(struct psp_context *psp)
1754 {
1755         int ret;
1756
1757         /*
1758          * TODO: bypass the terminate in sriov for now
1759          */
1760         if (amdgpu_sriov_vf(psp->adev))
1761                 return 0;
1762
1763         if (!psp->hdcp_context.context.initialized)
1764                 return 0;
1765
1766         ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1767
1768         psp->hdcp_context.context.initialized = false;
1769
1770         return ret;
1771 }
1772 // HDCP end
1773
1774 // DTM start
1775 static int psp_dtm_initialize(struct psp_context *psp)
1776 {
1777         int ret;
1778
1779         /*
1780          * TODO: bypass the initialize in sriov for now
1781          */
1782         if (amdgpu_sriov_vf(psp->adev))
1783                 return 0;
1784
1785         if (!psp->dtm_context.context.bin_desc.size_bytes ||
1786             !psp->dtm_context.context.bin_desc.start_addr) {
1787                 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1788                 return 0;
1789         }
1790
1791         psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1792         psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1793
1794         if (!psp->dtm_context.context.mem_context.shared_buf) {
1795                 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1796                 if (ret)
1797                         return ret;
1798         }
1799
1800         ret = psp_ta_load(psp, &psp->dtm_context.context);
1801         if (!ret) {
1802                 psp->dtm_context.context.initialized = true;
1803                 mutex_init(&psp->dtm_context.mutex);
1804         }
1805
1806         return ret;
1807 }
1808
1809 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1810 {
1811         /*
1812          * TODO: bypass the loading in sriov for now
1813          */
1814         if (amdgpu_sriov_vf(psp->adev))
1815                 return 0;
1816
1817         return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1818 }
1819
1820 static int psp_dtm_terminate(struct psp_context *psp)
1821 {
1822         int ret;
1823
1824         /*
1825          * TODO: bypass the terminate in sriov for now
1826          */
1827         if (amdgpu_sriov_vf(psp->adev))
1828                 return 0;
1829
1830         if (!psp->dtm_context.context.initialized)
1831                 return 0;
1832
1833         ret = psp_ta_unload(psp, &psp->dtm_context.context);
1834
1835         psp->dtm_context.context.initialized = false;
1836
1837         return ret;
1838 }
1839 // DTM end
1840
1841 // RAP start
1842 static int psp_rap_initialize(struct psp_context *psp)
1843 {
1844         int ret;
1845         enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1846
1847         /*
1848          * TODO: bypass the initialize in sriov for now
1849          */
1850         if (amdgpu_sriov_vf(psp->adev))
1851                 return 0;
1852
1853         if (!psp->rap_context.context.bin_desc.size_bytes ||
1854             !psp->rap_context.context.bin_desc.start_addr) {
1855                 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1856                 return 0;
1857         }
1858
1859         psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1860         psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1861
1862         if (!psp->rap_context.context.mem_context.shared_buf) {
1863                 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1864                 if (ret)
1865                         return ret;
1866         }
1867
1868         ret = psp_ta_load(psp, &psp->rap_context.context);
1869         if (!ret) {
1870                 psp->rap_context.context.initialized = true;
1871                 mutex_init(&psp->rap_context.mutex);
1872         } else
1873                 return ret;
1874
1875         ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1876         if (ret || status != TA_RAP_STATUS__SUCCESS) {
1877                 psp_rap_terminate(psp);
1878                 /* free rap shared memory */
1879                 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1880
1881                 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1882                          ret, status);
1883
1884                 return ret;
1885         }
1886
1887         return 0;
1888 }
1889
1890 static int psp_rap_terminate(struct psp_context *psp)
1891 {
1892         int ret;
1893
1894         if (!psp->rap_context.context.initialized)
1895                 return 0;
1896
1897         ret = psp_ta_unload(psp, &psp->rap_context.context);
1898
1899         psp->rap_context.context.initialized = false;
1900
1901         return ret;
1902 }
1903
1904 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1905 {
1906         struct ta_rap_shared_memory *rap_cmd;
1907         int ret = 0;
1908
1909         if (!psp->rap_context.context.initialized)
1910                 return 0;
1911
1912         if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1913             ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1914                 return -EINVAL;
1915
1916         mutex_lock(&psp->rap_context.mutex);
1917
1918         rap_cmd = (struct ta_rap_shared_memory *)
1919                   psp->rap_context.context.mem_context.shared_buf;
1920         memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1921
1922         rap_cmd->cmd_id = ta_cmd_id;
1923         rap_cmd->validation_method_id = METHOD_A;
1924
1925         ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1926         if (ret)
1927                 goto out_unlock;
1928
1929         if (status)
1930                 *status = rap_cmd->rap_status;
1931
1932 out_unlock:
1933         mutex_unlock(&psp->rap_context.mutex);
1934
1935         return ret;
1936 }
1937 // RAP end
1938
1939 /* securedisplay start */
1940 static int psp_securedisplay_initialize(struct psp_context *psp)
1941 {
1942         int ret;
1943         struct ta_securedisplay_cmd *securedisplay_cmd;
1944
1945         /*
1946          * TODO: bypass the initialize in sriov for now
1947          */
1948         if (amdgpu_sriov_vf(psp->adev))
1949                 return 0;
1950
1951         if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1952             !psp->securedisplay_context.context.bin_desc.start_addr) {
1953                 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1954                 return 0;
1955         }
1956
1957         psp->securedisplay_context.context.mem_context.shared_mem_size =
1958                 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1959         psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1960
1961         if (!psp->securedisplay_context.context.initialized) {
1962                 ret = psp_ta_init_shared_buf(psp,
1963                                              &psp->securedisplay_context.context.mem_context);
1964                 if (ret)
1965                         return ret;
1966         }
1967
1968         ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1969         if (!ret) {
1970                 psp->securedisplay_context.context.initialized = true;
1971                 mutex_init(&psp->securedisplay_context.mutex);
1972         } else
1973                 return ret;
1974
1975         mutex_lock(&psp->securedisplay_context.mutex);
1976
1977         psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1978                         TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1979
1980         ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1981
1982         mutex_unlock(&psp->securedisplay_context.mutex);
1983
1984         if (ret) {
1985                 psp_securedisplay_terminate(psp);
1986                 /* free securedisplay shared memory */
1987                 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1988                 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1989                 return -EINVAL;
1990         }
1991
1992         if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1993                 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1994                 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1995                         securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1996         }
1997
1998         return 0;
1999 }
2000
2001 static int psp_securedisplay_terminate(struct psp_context *psp)
2002 {
2003         int ret;
2004
2005         /*
2006          * TODO:bypass the terminate in sriov for now
2007          */
2008         if (amdgpu_sriov_vf(psp->adev))
2009                 return 0;
2010
2011         if (!psp->securedisplay_context.context.initialized)
2012                 return 0;
2013
2014         ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
2015
2016         psp->securedisplay_context.context.initialized = false;
2017
2018         return ret;
2019 }
2020
2021 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
2022 {
2023         int ret;
2024
2025         if (!psp->securedisplay_context.context.initialized)
2026                 return -EINVAL;
2027
2028         if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
2029             ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
2030                 return -EINVAL;
2031
2032         ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
2033
2034         return ret;
2035 }
2036 /* SECUREDISPLAY end */
2037
2038 static int psp_hw_start(struct psp_context *psp)
2039 {
2040         struct amdgpu_device *adev = psp->adev;
2041         int ret;
2042
2043         if (!amdgpu_sriov_vf(adev)) {
2044                 if ((is_psp_fw_valid(psp->kdb)) &&
2045                     (psp->funcs->bootloader_load_kdb != NULL)) {
2046                         ret = psp_bootloader_load_kdb(psp);
2047                         if (ret) {
2048                                 DRM_ERROR("PSP load kdb failed!\n");
2049                                 return ret;
2050                         }
2051                 }
2052
2053                 if ((is_psp_fw_valid(psp->spl)) &&
2054                     (psp->funcs->bootloader_load_spl != NULL)) {
2055                         ret = psp_bootloader_load_spl(psp);
2056                         if (ret) {
2057                                 DRM_ERROR("PSP load spl failed!\n");
2058                                 return ret;
2059                         }
2060                 }
2061
2062                 if ((is_psp_fw_valid(psp->sys)) &&
2063                     (psp->funcs->bootloader_load_sysdrv != NULL)) {
2064                         ret = psp_bootloader_load_sysdrv(psp);
2065                         if (ret) {
2066                                 DRM_ERROR("PSP load sys drv failed!\n");
2067                                 return ret;
2068                         }
2069                 }
2070
2071                 if ((is_psp_fw_valid(psp->soc_drv)) &&
2072                     (psp->funcs->bootloader_load_soc_drv != NULL)) {
2073                         ret = psp_bootloader_load_soc_drv(psp);
2074                         if (ret) {
2075                                 DRM_ERROR("PSP load soc drv failed!\n");
2076                                 return ret;
2077                         }
2078                 }
2079
2080                 if ((is_psp_fw_valid(psp->intf_drv)) &&
2081                     (psp->funcs->bootloader_load_intf_drv != NULL)) {
2082                         ret = psp_bootloader_load_intf_drv(psp);
2083                         if (ret) {
2084                                 DRM_ERROR("PSP load intf drv failed!\n");
2085                                 return ret;
2086                         }
2087                 }
2088
2089                 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2090                     (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2091                         ret = psp_bootloader_load_dbg_drv(psp);
2092                         if (ret) {
2093                                 DRM_ERROR("PSP load dbg drv failed!\n");
2094                                 return ret;
2095                         }
2096                 }
2097
2098                 if ((is_psp_fw_valid(psp->ras_drv)) &&
2099                     (psp->funcs->bootloader_load_ras_drv != NULL)) {
2100                         ret = psp_bootloader_load_ras_drv(psp);
2101                         if (ret) {
2102                                 DRM_ERROR("PSP load ras_drv failed!\n");
2103                                 return ret;
2104                         }
2105                 }
2106
2107                 if ((is_psp_fw_valid(psp->sos)) &&
2108                     (psp->funcs->bootloader_load_sos != NULL)) {
2109                         ret = psp_bootloader_load_sos(psp);
2110                         if (ret) {
2111                                 DRM_ERROR("PSP load sos failed!\n");
2112                                 return ret;
2113                         }
2114                 }
2115         }
2116
2117         ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2118         if (ret) {
2119                 DRM_ERROR("PSP create ring failed!\n");
2120                 return ret;
2121         }
2122
2123         if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2124                 goto skip_pin_bo;
2125
2126         if (!psp_boottime_tmr(psp)) {
2127                 ret = psp_tmr_init(psp);
2128                 if (ret) {
2129                         DRM_ERROR("PSP tmr init failed!\n");
2130                         return ret;
2131                 }
2132         }
2133
2134 skip_pin_bo:
2135         /*
2136          * For ASICs with DF Cstate management centralized
2137          * to PMFW, TMR setup should be performed after PMFW
2138          * loaded and before other non-psp firmware loaded.
2139          */
2140         if (psp->pmfw_centralized_cstate_management) {
2141                 ret = psp_load_smu_fw(psp);
2142                 if (ret)
2143                         return ret;
2144         }
2145
2146         ret = psp_tmr_load(psp);
2147         if (ret) {
2148                 DRM_ERROR("PSP load tmr failed!\n");
2149                 return ret;
2150         }
2151
2152         return 0;
2153 }
2154
2155 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2156                            enum psp_gfx_fw_type *type)
2157 {
2158         switch (ucode->ucode_id) {
2159         case AMDGPU_UCODE_ID_CAP:
2160                 *type = GFX_FW_TYPE_CAP;
2161                 break;
2162         case AMDGPU_UCODE_ID_SDMA0:
2163                 *type = GFX_FW_TYPE_SDMA0;
2164                 break;
2165         case AMDGPU_UCODE_ID_SDMA1:
2166                 *type = GFX_FW_TYPE_SDMA1;
2167                 break;
2168         case AMDGPU_UCODE_ID_SDMA2:
2169                 *type = GFX_FW_TYPE_SDMA2;
2170                 break;
2171         case AMDGPU_UCODE_ID_SDMA3:
2172                 *type = GFX_FW_TYPE_SDMA3;
2173                 break;
2174         case AMDGPU_UCODE_ID_SDMA4:
2175                 *type = GFX_FW_TYPE_SDMA4;
2176                 break;
2177         case AMDGPU_UCODE_ID_SDMA5:
2178                 *type = GFX_FW_TYPE_SDMA5;
2179                 break;
2180         case AMDGPU_UCODE_ID_SDMA6:
2181                 *type = GFX_FW_TYPE_SDMA6;
2182                 break;
2183         case AMDGPU_UCODE_ID_SDMA7:
2184                 *type = GFX_FW_TYPE_SDMA7;
2185                 break;
2186         case AMDGPU_UCODE_ID_CP_MES:
2187                 *type = GFX_FW_TYPE_CP_MES;
2188                 break;
2189         case AMDGPU_UCODE_ID_CP_MES_DATA:
2190                 *type = GFX_FW_TYPE_MES_STACK;
2191                 break;
2192         case AMDGPU_UCODE_ID_CP_MES1:
2193                 *type = GFX_FW_TYPE_CP_MES_KIQ;
2194                 break;
2195         case AMDGPU_UCODE_ID_CP_MES1_DATA:
2196                 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2197                 break;
2198         case AMDGPU_UCODE_ID_CP_CE:
2199                 *type = GFX_FW_TYPE_CP_CE;
2200                 break;
2201         case AMDGPU_UCODE_ID_CP_PFP:
2202                 *type = GFX_FW_TYPE_CP_PFP;
2203                 break;
2204         case AMDGPU_UCODE_ID_CP_ME:
2205                 *type = GFX_FW_TYPE_CP_ME;
2206                 break;
2207         case AMDGPU_UCODE_ID_CP_MEC1:
2208                 *type = GFX_FW_TYPE_CP_MEC;
2209                 break;
2210         case AMDGPU_UCODE_ID_CP_MEC1_JT:
2211                 *type = GFX_FW_TYPE_CP_MEC_ME1;
2212                 break;
2213         case AMDGPU_UCODE_ID_CP_MEC2:
2214                 *type = GFX_FW_TYPE_CP_MEC;
2215                 break;
2216         case AMDGPU_UCODE_ID_CP_MEC2_JT:
2217                 *type = GFX_FW_TYPE_CP_MEC_ME2;
2218                 break;
2219         case AMDGPU_UCODE_ID_RLC_P:
2220                 *type = GFX_FW_TYPE_RLC_P;
2221                 break;
2222         case AMDGPU_UCODE_ID_RLC_V:
2223                 *type = GFX_FW_TYPE_RLC_V;
2224                 break;
2225         case AMDGPU_UCODE_ID_RLC_G:
2226                 *type = GFX_FW_TYPE_RLC_G;
2227                 break;
2228         case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2229                 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2230                 break;
2231         case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2232                 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2233                 break;
2234         case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2235                 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2236                 break;
2237         case AMDGPU_UCODE_ID_RLC_IRAM:
2238                 *type = GFX_FW_TYPE_RLC_IRAM;
2239                 break;
2240         case AMDGPU_UCODE_ID_RLC_DRAM:
2241                 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2242                 break;
2243         case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2244                 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2245                 break;
2246         case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2247                 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2248                 break;
2249         case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2250                 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2251                 break;
2252         case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2253                 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2254                 break;
2255         case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2256                 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2257                 break;
2258         case AMDGPU_UCODE_ID_SMC:
2259                 *type = GFX_FW_TYPE_SMU;
2260                 break;
2261         case AMDGPU_UCODE_ID_PPTABLE:
2262                 *type = GFX_FW_TYPE_PPTABLE;
2263                 break;
2264         case AMDGPU_UCODE_ID_UVD:
2265                 *type = GFX_FW_TYPE_UVD;
2266                 break;
2267         case AMDGPU_UCODE_ID_UVD1:
2268                 *type = GFX_FW_TYPE_UVD1;
2269                 break;
2270         case AMDGPU_UCODE_ID_VCE:
2271                 *type = GFX_FW_TYPE_VCE;
2272                 break;
2273         case AMDGPU_UCODE_ID_VCN:
2274                 *type = GFX_FW_TYPE_VCN;
2275                 break;
2276         case AMDGPU_UCODE_ID_VCN1:
2277                 *type = GFX_FW_TYPE_VCN1;
2278                 break;
2279         case AMDGPU_UCODE_ID_DMCU_ERAM:
2280                 *type = GFX_FW_TYPE_DMCU_ERAM;
2281                 break;
2282         case AMDGPU_UCODE_ID_DMCU_INTV:
2283                 *type = GFX_FW_TYPE_DMCU_ISR;
2284                 break;
2285         case AMDGPU_UCODE_ID_VCN0_RAM:
2286                 *type = GFX_FW_TYPE_VCN0_RAM;
2287                 break;
2288         case AMDGPU_UCODE_ID_VCN1_RAM:
2289                 *type = GFX_FW_TYPE_VCN1_RAM;
2290                 break;
2291         case AMDGPU_UCODE_ID_DMCUB:
2292                 *type = GFX_FW_TYPE_DMUB;
2293                 break;
2294         case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2295                 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2296                 break;
2297         case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2298                 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2299                 break;
2300         case AMDGPU_UCODE_ID_IMU_I:
2301                 *type = GFX_FW_TYPE_IMU_I;
2302                 break;
2303         case AMDGPU_UCODE_ID_IMU_D:
2304                 *type = GFX_FW_TYPE_IMU_D;
2305                 break;
2306         case AMDGPU_UCODE_ID_CP_RS64_PFP:
2307                 *type = GFX_FW_TYPE_RS64_PFP;
2308                 break;
2309         case AMDGPU_UCODE_ID_CP_RS64_ME:
2310                 *type = GFX_FW_TYPE_RS64_ME;
2311                 break;
2312         case AMDGPU_UCODE_ID_CP_RS64_MEC:
2313                 *type = GFX_FW_TYPE_RS64_MEC;
2314                 break;
2315         case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2316                 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2317                 break;
2318         case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2319                 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2320                 break;
2321         case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2322                 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2323                 break;
2324         case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2325                 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2326                 break;
2327         case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2328                 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2329                 break;
2330         case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2331                 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2332                 break;
2333         case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2334                 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2335                 break;
2336         case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2337                 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2338                 break;
2339         case AMDGPU_UCODE_ID_MAXIMUM:
2340         default:
2341                 return -EINVAL;
2342         }
2343
2344         return 0;
2345 }
2346
2347 static void psp_print_fw_hdr(struct psp_context *psp,
2348                              struct amdgpu_firmware_info *ucode)
2349 {
2350         struct amdgpu_device *adev = psp->adev;
2351         struct common_firmware_header *hdr;
2352
2353         switch (ucode->ucode_id) {
2354         case AMDGPU_UCODE_ID_SDMA0:
2355         case AMDGPU_UCODE_ID_SDMA1:
2356         case AMDGPU_UCODE_ID_SDMA2:
2357         case AMDGPU_UCODE_ID_SDMA3:
2358         case AMDGPU_UCODE_ID_SDMA4:
2359         case AMDGPU_UCODE_ID_SDMA5:
2360         case AMDGPU_UCODE_ID_SDMA6:
2361         case AMDGPU_UCODE_ID_SDMA7:
2362                 hdr = (struct common_firmware_header *)
2363                         adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2364                 amdgpu_ucode_print_sdma_hdr(hdr);
2365                 break;
2366         case AMDGPU_UCODE_ID_CP_CE:
2367                 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2368                 amdgpu_ucode_print_gfx_hdr(hdr);
2369                 break;
2370         case AMDGPU_UCODE_ID_CP_PFP:
2371                 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2372                 amdgpu_ucode_print_gfx_hdr(hdr);
2373                 break;
2374         case AMDGPU_UCODE_ID_CP_ME:
2375                 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2376                 amdgpu_ucode_print_gfx_hdr(hdr);
2377                 break;
2378         case AMDGPU_UCODE_ID_CP_MEC1:
2379                 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2380                 amdgpu_ucode_print_gfx_hdr(hdr);
2381                 break;
2382         case AMDGPU_UCODE_ID_RLC_G:
2383                 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2384                 amdgpu_ucode_print_rlc_hdr(hdr);
2385                 break;
2386         case AMDGPU_UCODE_ID_SMC:
2387                 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2388                 amdgpu_ucode_print_smc_hdr(hdr);
2389                 break;
2390         default:
2391                 break;
2392         }
2393 }
2394
2395 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2396                                        struct psp_gfx_cmd_resp *cmd)
2397 {
2398         int ret;
2399         uint64_t fw_mem_mc_addr = ucode->mc_addr;
2400
2401         cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2402         cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2403         cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2404         cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2405
2406         ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2407         if (ret)
2408                 DRM_ERROR("Unknown firmware type\n");
2409
2410         return ret;
2411 }
2412
2413 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2414                                   struct amdgpu_firmware_info *ucode)
2415 {
2416         int ret = 0;
2417         struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2418
2419         ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2420         if (!ret) {
2421                 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2422                                          psp->fence_buf_mc_addr);
2423         }
2424
2425         release_psp_cmd_buf(psp);
2426
2427         return ret;
2428 }
2429
2430 static int psp_load_smu_fw(struct psp_context *psp)
2431 {
2432         int ret;
2433         struct amdgpu_device *adev = psp->adev;
2434         struct amdgpu_firmware_info *ucode =
2435                         &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2436         struct amdgpu_ras *ras = psp->ras_context.ras;
2437
2438         /*
2439          * Skip SMU FW reloading in case of using BACO for runpm only,
2440          * as SMU is always alive.
2441          */
2442         if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2443                 return 0;
2444
2445         if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2446                 return 0;
2447
2448         if ((amdgpu_in_reset(adev) &&
2449              ras && adev->ras_enabled &&
2450              (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2451               adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2452                 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2453                 if (ret)
2454                         DRM_WARN("Failed to set MP1 state prepare for reload\n");
2455         }
2456
2457         ret = psp_execute_non_psp_fw_load(psp, ucode);
2458
2459         if (ret)
2460                 DRM_ERROR("PSP load smu failed!\n");
2461
2462         return ret;
2463 }
2464
2465 static bool fw_load_skip_check(struct psp_context *psp,
2466                                struct amdgpu_firmware_info *ucode)
2467 {
2468         if (!ucode->fw || !ucode->ucode_size)
2469                 return true;
2470
2471         if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2472             (psp_smu_reload_quirk(psp) ||
2473              psp->autoload_supported ||
2474              psp->pmfw_centralized_cstate_management))
2475                 return true;
2476
2477         if (amdgpu_sriov_vf(psp->adev) &&
2478             amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2479                 return true;
2480
2481         if (psp->autoload_supported &&
2482             (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2483              ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2484                 /* skip mec JT when autoload is enabled */
2485                 return true;
2486
2487         return false;
2488 }
2489
2490 int psp_load_fw_list(struct psp_context *psp,
2491                      struct amdgpu_firmware_info **ucode_list, int ucode_count)
2492 {
2493         int ret = 0, i;
2494         struct amdgpu_firmware_info *ucode;
2495
2496         for (i = 0; i < ucode_count; ++i) {
2497                 ucode = ucode_list[i];
2498                 psp_print_fw_hdr(psp, ucode);
2499                 ret = psp_execute_non_psp_fw_load(psp, ucode);
2500                 if (ret)
2501                         return ret;
2502         }
2503         return ret;
2504 }
2505
2506 static int psp_load_non_psp_fw(struct psp_context *psp)
2507 {
2508         int i, ret;
2509         struct amdgpu_firmware_info *ucode;
2510         struct amdgpu_device *adev = psp->adev;
2511
2512         if (psp->autoload_supported &&
2513             !psp->pmfw_centralized_cstate_management) {
2514                 ret = psp_load_smu_fw(psp);
2515                 if (ret)
2516                         return ret;
2517         }
2518
2519         for (i = 0; i < adev->firmware.max_ucodes; i++) {
2520                 ucode = &adev->firmware.ucode[i];
2521
2522                 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2523                     !fw_load_skip_check(psp, ucode)) {
2524                         ret = psp_load_smu_fw(psp);
2525                         if (ret)
2526                                 return ret;
2527                         continue;
2528                 }
2529
2530                 if (fw_load_skip_check(psp, ucode))
2531                         continue;
2532
2533                 if (psp->autoload_supported &&
2534                     (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2535                      adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2536                      adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2537                     (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2538                      ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2539                      ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2540                         /* PSP only receive one SDMA fw for sienna_cichlid,
2541                          * as all four sdma fw are same */
2542                         continue;
2543
2544                 psp_print_fw_hdr(psp, ucode);
2545
2546                 ret = psp_execute_non_psp_fw_load(psp, ucode);
2547                 if (ret)
2548                         return ret;
2549
2550                 /* Start rlc autoload after psp recieved all the gfx firmware */
2551                 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2552                     adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2553                         ret = psp_rlc_autoload_start(psp);
2554                         if (ret) {
2555                                 DRM_ERROR("Failed to start rlc autoload\n");
2556                                 return ret;
2557                         }
2558                 }
2559         }
2560
2561         return 0;
2562 }
2563
2564 static int psp_load_fw(struct amdgpu_device *adev)
2565 {
2566         int ret;
2567         struct psp_context *psp = &adev->psp;
2568
2569         if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2570                 /* should not destroy ring, only stop */
2571                 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2572         } else {
2573                 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2574
2575                 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2576                 if (ret) {
2577                         DRM_ERROR("PSP ring init failed!\n");
2578                         goto failed;
2579                 }
2580         }
2581
2582         ret = psp_hw_start(psp);
2583         if (ret)
2584                 goto failed;
2585
2586         ret = psp_load_non_psp_fw(psp);
2587         if (ret)
2588                 goto failed1;
2589
2590         ret = psp_asd_initialize(psp);
2591         if (ret) {
2592                 DRM_ERROR("PSP load asd failed!\n");
2593                 goto failed1;
2594         }
2595
2596         ret = psp_rl_load(adev);
2597         if (ret) {
2598                 DRM_ERROR("PSP load RL failed!\n");
2599                 goto failed1;
2600         }
2601
2602         if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2603                 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2604                         ret = psp_xgmi_initialize(psp, false, true);
2605                         /* Warning the XGMI seesion initialize failure
2606                         * Instead of stop driver initialization
2607                         */
2608                         if (ret)
2609                                 dev_err(psp->adev->dev,
2610                                         "XGMI: Failed to initialize XGMI session\n");
2611                 }
2612         }
2613
2614         if (psp->ta_fw) {
2615                 ret = psp_ras_initialize(psp);
2616                 if (ret)
2617                         dev_err(psp->adev->dev,
2618                                         "RAS: Failed to initialize RAS\n");
2619
2620                 ret = psp_hdcp_initialize(psp);
2621                 if (ret)
2622                         dev_err(psp->adev->dev,
2623                                 "HDCP: Failed to initialize HDCP\n");
2624
2625                 ret = psp_dtm_initialize(psp);
2626                 if (ret)
2627                         dev_err(psp->adev->dev,
2628                                 "DTM: Failed to initialize DTM\n");
2629
2630                 ret = psp_rap_initialize(psp);
2631                 if (ret)
2632                         dev_err(psp->adev->dev,
2633                                 "RAP: Failed to initialize RAP\n");
2634
2635                 ret = psp_securedisplay_initialize(psp);
2636                 if (ret)
2637                         dev_err(psp->adev->dev,
2638                                 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2639         }
2640
2641         return 0;
2642
2643 failed1:
2644         psp_free_shared_bufs(psp);
2645 failed:
2646         /*
2647          * all cleanup jobs (xgmi terminate, ras terminate,
2648          * ring destroy, cmd/fence/fw buffers destory,
2649          * psp->cmd destory) are delayed to psp_hw_fini
2650          */
2651         psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2652         return ret;
2653 }
2654
2655 static int psp_hw_init(void *handle)
2656 {
2657         int ret;
2658         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2659
2660         mutex_lock(&adev->firmware.mutex);
2661         /*
2662          * This sequence is just used on hw_init only once, no need on
2663          * resume.
2664          */
2665         ret = amdgpu_ucode_init_bo(adev);
2666         if (ret)
2667                 goto failed;
2668
2669         ret = psp_load_fw(adev);
2670         if (ret) {
2671                 DRM_ERROR("PSP firmware loading failed\n");
2672                 goto failed;
2673         }
2674
2675         mutex_unlock(&adev->firmware.mutex);
2676         return 0;
2677
2678 failed:
2679         adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2680         mutex_unlock(&adev->firmware.mutex);
2681         return -EINVAL;
2682 }
2683
2684 static int psp_hw_fini(void *handle)
2685 {
2686         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2687         struct psp_context *psp = &adev->psp;
2688
2689         if (psp->ta_fw) {
2690                 psp_ras_terminate(psp);
2691                 psp_securedisplay_terminate(psp);
2692                 psp_rap_terminate(psp);
2693                 psp_dtm_terminate(psp);
2694                 psp_hdcp_terminate(psp);
2695
2696                 if (adev->gmc.xgmi.num_physical_nodes > 1)
2697                         psp_xgmi_terminate(psp);
2698         }
2699
2700         psp_asd_terminate(psp);
2701         psp_tmr_terminate(psp);
2702
2703         psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2704
2705         return 0;
2706 }
2707
2708 static int psp_suspend(void *handle)
2709 {
2710         int ret = 0;
2711         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2712         struct psp_context *psp = &adev->psp;
2713
2714         if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2715             psp->xgmi_context.context.initialized) {
2716                 ret = psp_xgmi_terminate(psp);
2717                 if (ret) {
2718                         DRM_ERROR("Failed to terminate xgmi ta\n");
2719                         goto out;
2720                 }
2721         }
2722
2723         if (psp->ta_fw) {
2724                 ret = psp_ras_terminate(psp);
2725                 if (ret) {
2726                         DRM_ERROR("Failed to terminate ras ta\n");
2727                         goto out;
2728                 }
2729                 ret = psp_hdcp_terminate(psp);
2730                 if (ret) {
2731                         DRM_ERROR("Failed to terminate hdcp ta\n");
2732                         goto out;
2733                 }
2734                 ret = psp_dtm_terminate(psp);
2735                 if (ret) {
2736                         DRM_ERROR("Failed to terminate dtm ta\n");
2737                         goto out;
2738                 }
2739                 ret = psp_rap_terminate(psp);
2740                 if (ret) {
2741                         DRM_ERROR("Failed to terminate rap ta\n");
2742                         goto out;
2743                 }
2744                 ret = psp_securedisplay_terminate(psp);
2745                 if (ret) {
2746                         DRM_ERROR("Failed to terminate securedisplay ta\n");
2747                         goto out;
2748                 }
2749         }
2750
2751         ret = psp_asd_terminate(psp);
2752         if (ret) {
2753                 DRM_ERROR("Failed to terminate asd\n");
2754                 goto out;
2755         }
2756
2757         ret = psp_tmr_terminate(psp);
2758         if (ret) {
2759                 DRM_ERROR("Failed to terminate tmr\n");
2760                 goto out;
2761         }
2762
2763         ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2764         if (ret)
2765                 DRM_ERROR("PSP ring stop failed\n");
2766
2767 out:
2768         return ret;
2769 }
2770
2771 static int psp_resume(void *handle)
2772 {
2773         int ret;
2774         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2775         struct psp_context *psp = &adev->psp;
2776
2777         DRM_INFO("PSP is resuming...\n");
2778
2779         if (psp->mem_train_ctx.enable_mem_training) {
2780                 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2781                 if (ret) {
2782                         DRM_ERROR("Failed to process memory training!\n");
2783                         return ret;
2784                 }
2785         }
2786
2787         mutex_lock(&adev->firmware.mutex);
2788
2789         ret = psp_hw_start(psp);
2790         if (ret)
2791                 goto failed;
2792
2793         ret = psp_load_non_psp_fw(psp);
2794         if (ret)
2795                 goto failed;
2796
2797         ret = psp_asd_initialize(psp);
2798         if (ret) {
2799                 DRM_ERROR("PSP load asd failed!\n");
2800                 goto failed;
2801         }
2802
2803         ret = psp_rl_load(adev);
2804         if (ret) {
2805                 dev_err(adev->dev, "PSP load RL failed!\n");
2806                 goto failed;
2807         }
2808
2809         if (adev->gmc.xgmi.num_physical_nodes > 1) {
2810                 ret = psp_xgmi_initialize(psp, false, true);
2811                 /* Warning the XGMI seesion initialize failure
2812                  * Instead of stop driver initialization
2813                  */
2814                 if (ret)
2815                         dev_err(psp->adev->dev,
2816                                 "XGMI: Failed to initialize XGMI session\n");
2817         }
2818
2819         if (psp->ta_fw) {
2820                 ret = psp_ras_initialize(psp);
2821                 if (ret)
2822                         dev_err(psp->adev->dev,
2823                                         "RAS: Failed to initialize RAS\n");
2824
2825                 ret = psp_hdcp_initialize(psp);
2826                 if (ret)
2827                         dev_err(psp->adev->dev,
2828                                 "HDCP: Failed to initialize HDCP\n");
2829
2830                 ret = psp_dtm_initialize(psp);
2831                 if (ret)
2832                         dev_err(psp->adev->dev,
2833                                 "DTM: Failed to initialize DTM\n");
2834
2835                 ret = psp_rap_initialize(psp);
2836                 if (ret)
2837                         dev_err(psp->adev->dev,
2838                                 "RAP: Failed to initialize RAP\n");
2839
2840                 ret = psp_securedisplay_initialize(psp);
2841                 if (ret)
2842                         dev_err(psp->adev->dev,
2843                                 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2844         }
2845
2846         mutex_unlock(&adev->firmware.mutex);
2847
2848         return 0;
2849
2850 failed:
2851         DRM_ERROR("PSP resume failed\n");
2852         mutex_unlock(&adev->firmware.mutex);
2853         return ret;
2854 }
2855
2856 int psp_gpu_reset(struct amdgpu_device *adev)
2857 {
2858         int ret;
2859
2860         if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2861                 return 0;
2862
2863         mutex_lock(&adev->psp.mutex);
2864         ret = psp_mode1_reset(&adev->psp);
2865         mutex_unlock(&adev->psp.mutex);
2866
2867         return ret;
2868 }
2869
2870 int psp_rlc_autoload_start(struct psp_context *psp)
2871 {
2872         int ret;
2873         struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2874
2875         cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2876
2877         ret = psp_cmd_submit_buf(psp, NULL, cmd,
2878                                  psp->fence_buf_mc_addr);
2879
2880         release_psp_cmd_buf(psp);
2881
2882         return ret;
2883 }
2884
2885 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2886                         uint64_t cmd_gpu_addr, int cmd_size)
2887 {
2888         struct amdgpu_firmware_info ucode = {0};
2889
2890         ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2891                 AMDGPU_UCODE_ID_VCN0_RAM;
2892         ucode.mc_addr = cmd_gpu_addr;
2893         ucode.ucode_size = cmd_size;
2894
2895         return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2896 }
2897
2898 int psp_ring_cmd_submit(struct psp_context *psp,
2899                         uint64_t cmd_buf_mc_addr,
2900                         uint64_t fence_mc_addr,
2901                         int index)
2902 {
2903         unsigned int psp_write_ptr_reg = 0;
2904         struct psp_gfx_rb_frame *write_frame;
2905         struct psp_ring *ring = &psp->km_ring;
2906         struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2907         struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2908                 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2909         struct amdgpu_device *adev = psp->adev;
2910         uint32_t ring_size_dw = ring->ring_size / 4;
2911         uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2912
2913         /* KM (GPCOM) prepare write pointer */
2914         psp_write_ptr_reg = psp_ring_get_wptr(psp);
2915
2916         /* Update KM RB frame pointer to new frame */
2917         /* write_frame ptr increments by size of rb_frame in bytes */
2918         /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2919         if ((psp_write_ptr_reg % ring_size_dw) == 0)
2920                 write_frame = ring_buffer_start;
2921         else
2922                 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2923         /* Check invalid write_frame ptr address */
2924         if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2925                 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2926                           ring_buffer_start, ring_buffer_end, write_frame);
2927                 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2928                 return -EINVAL;
2929         }
2930
2931         /* Initialize KM RB frame */
2932         memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2933
2934         /* Update KM RB frame */
2935         write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2936         write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2937         write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2938         write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2939         write_frame->fence_value = index;
2940         amdgpu_device_flush_hdp(adev, NULL);
2941
2942         /* Update the write Pointer in DWORDs */
2943         psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2944         psp_ring_set_wptr(psp, psp_write_ptr_reg);
2945         return 0;
2946 }
2947
2948 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
2949 {
2950         struct amdgpu_device *adev = psp->adev;
2951         char fw_name[PSP_FW_NAME_LEN];
2952         const struct psp_firmware_header_v1_0 *asd_hdr;
2953         int err = 0;
2954
2955         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2956         err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
2957         if (err)
2958                 goto out;
2959
2960         asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2961         adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2962         adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2963         adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2964         adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2965                                 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2966         return 0;
2967 out:
2968         amdgpu_ucode_release(&adev->psp.asd_fw);
2969         return err;
2970 }
2971
2972 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
2973 {
2974         struct amdgpu_device *adev = psp->adev;
2975         char fw_name[PSP_FW_NAME_LEN];
2976         const struct psp_firmware_header_v1_0 *toc_hdr;
2977         int err = 0;
2978
2979         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2980         err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
2981         if (err)
2982                 goto out;
2983
2984         toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2985         adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2986         adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2987         adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2988         adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2989                                 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2990         return 0;
2991 out:
2992         amdgpu_ucode_release(&adev->psp.toc_fw);
2993         return err;
2994 }
2995
2996 static int parse_sos_bin_descriptor(struct psp_context *psp,
2997                                    const struct psp_fw_bin_desc *desc,
2998                                    const struct psp_firmware_header_v2_0 *sos_hdr)
2999 {
3000         uint8_t *ucode_start_addr  = NULL;
3001
3002         if (!psp || !desc || !sos_hdr)
3003                 return -EINVAL;
3004
3005         ucode_start_addr  = (uint8_t *)sos_hdr +
3006                             le32_to_cpu(desc->offset_bytes) +
3007                             le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3008
3009         switch (desc->fw_type) {
3010         case PSP_FW_TYPE_PSP_SOS:
3011                 psp->sos.fw_version        = le32_to_cpu(desc->fw_version);
3012                 psp->sos.feature_version   = le32_to_cpu(desc->fw_version);
3013                 psp->sos.size_bytes        = le32_to_cpu(desc->size_bytes);
3014                 psp->sos.start_addr        = ucode_start_addr;
3015                 break;
3016         case PSP_FW_TYPE_PSP_SYS_DRV:
3017                 psp->sys.fw_version        = le32_to_cpu(desc->fw_version);
3018                 psp->sys.feature_version   = le32_to_cpu(desc->fw_version);
3019                 psp->sys.size_bytes        = le32_to_cpu(desc->size_bytes);
3020                 psp->sys.start_addr        = ucode_start_addr;
3021                 break;
3022         case PSP_FW_TYPE_PSP_KDB:
3023                 psp->kdb.fw_version        = le32_to_cpu(desc->fw_version);
3024                 psp->kdb.feature_version   = le32_to_cpu(desc->fw_version);
3025                 psp->kdb.size_bytes        = le32_to_cpu(desc->size_bytes);
3026                 psp->kdb.start_addr        = ucode_start_addr;
3027                 break;
3028         case PSP_FW_TYPE_PSP_TOC:
3029                 psp->toc.fw_version        = le32_to_cpu(desc->fw_version);
3030                 psp->toc.feature_version   = le32_to_cpu(desc->fw_version);
3031                 psp->toc.size_bytes        = le32_to_cpu(desc->size_bytes);
3032                 psp->toc.start_addr        = ucode_start_addr;
3033                 break;
3034         case PSP_FW_TYPE_PSP_SPL:
3035                 psp->spl.fw_version        = le32_to_cpu(desc->fw_version);
3036                 psp->spl.feature_version   = le32_to_cpu(desc->fw_version);
3037                 psp->spl.size_bytes        = le32_to_cpu(desc->size_bytes);
3038                 psp->spl.start_addr        = ucode_start_addr;
3039                 break;
3040         case PSP_FW_TYPE_PSP_RL:
3041                 psp->rl.fw_version         = le32_to_cpu(desc->fw_version);
3042                 psp->rl.feature_version    = le32_to_cpu(desc->fw_version);
3043                 psp->rl.size_bytes         = le32_to_cpu(desc->size_bytes);
3044                 psp->rl.start_addr         = ucode_start_addr;
3045                 break;
3046         case PSP_FW_TYPE_PSP_SOC_DRV:
3047                 psp->soc_drv.fw_version         = le32_to_cpu(desc->fw_version);
3048                 psp->soc_drv.feature_version    = le32_to_cpu(desc->fw_version);
3049                 psp->soc_drv.size_bytes         = le32_to_cpu(desc->size_bytes);
3050                 psp->soc_drv.start_addr         = ucode_start_addr;
3051                 break;
3052         case PSP_FW_TYPE_PSP_INTF_DRV:
3053                 psp->intf_drv.fw_version        = le32_to_cpu(desc->fw_version);
3054                 psp->intf_drv.feature_version   = le32_to_cpu(desc->fw_version);
3055                 psp->intf_drv.size_bytes        = le32_to_cpu(desc->size_bytes);
3056                 psp->intf_drv.start_addr        = ucode_start_addr;
3057                 break;
3058         case PSP_FW_TYPE_PSP_DBG_DRV:
3059                 psp->dbg_drv.fw_version         = le32_to_cpu(desc->fw_version);
3060                 psp->dbg_drv.feature_version    = le32_to_cpu(desc->fw_version);
3061                 psp->dbg_drv.size_bytes         = le32_to_cpu(desc->size_bytes);
3062                 psp->dbg_drv.start_addr         = ucode_start_addr;
3063                 break;
3064         case PSP_FW_TYPE_PSP_RAS_DRV:
3065                 psp->ras_drv.fw_version         = le32_to_cpu(desc->fw_version);
3066                 psp->ras_drv.feature_version    = le32_to_cpu(desc->fw_version);
3067                 psp->ras_drv.size_bytes         = le32_to_cpu(desc->size_bytes);
3068                 psp->ras_drv.start_addr         = ucode_start_addr;
3069                 break;
3070         default:
3071                 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3072                 break;
3073         }
3074
3075         return 0;
3076 }
3077
3078 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3079 {
3080         const struct psp_firmware_header_v1_0 *sos_hdr;
3081         const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3082         uint8_t *ucode_array_start_addr;
3083
3084         sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3085         ucode_array_start_addr = (uint8_t *)sos_hdr +
3086                 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3087
3088         if (adev->gmc.xgmi.connected_to_cpu ||
3089             (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3090                 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3091                 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3092
3093                 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3094                 adev->psp.sys.start_addr = ucode_array_start_addr;
3095
3096                 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3097                 adev->psp.sos.start_addr = ucode_array_start_addr +
3098                                 le32_to_cpu(sos_hdr->sos.offset_bytes);
3099         } else {
3100                 /* Load alternate PSP SOS FW */
3101                 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3102
3103                 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3104                 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3105
3106                 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3107                 adev->psp.sys.start_addr = ucode_array_start_addr +
3108                         le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3109
3110                 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3111                 adev->psp.sos.start_addr = ucode_array_start_addr +
3112                         le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3113         }
3114
3115         if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3116                 dev_warn(adev->dev, "PSP SOS FW not available");
3117                 return -EINVAL;
3118         }
3119
3120         return 0;
3121 }
3122
3123 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3124 {
3125         struct amdgpu_device *adev = psp->adev;
3126         char fw_name[PSP_FW_NAME_LEN];
3127         const struct psp_firmware_header_v1_0 *sos_hdr;
3128         const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3129         const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3130         const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3131         const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3132         int err = 0;
3133         uint8_t *ucode_array_start_addr;
3134         int fw_index = 0;
3135
3136         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3137         err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
3138         if (err)
3139                 goto out;
3140
3141         sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3142         ucode_array_start_addr = (uint8_t *)sos_hdr +
3143                 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3144         amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3145
3146         switch (sos_hdr->header.header_version_major) {
3147         case 1:
3148                 err = psp_init_sos_base_fw(adev);
3149                 if (err)
3150                         goto out;
3151
3152                 if (sos_hdr->header.header_version_minor == 1) {
3153                         sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3154                         adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3155                         adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3156                                         le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3157                         adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3158                         adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3159                                         le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3160                 }
3161                 if (sos_hdr->header.header_version_minor == 2) {
3162                         sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3163                         adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3164                         adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3165                                                     le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3166                 }
3167                 if (sos_hdr->header.header_version_minor == 3) {
3168                         sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3169                         adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3170                         adev->psp.toc.start_addr = ucode_array_start_addr +
3171                                 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3172                         adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3173                         adev->psp.kdb.start_addr = ucode_array_start_addr +
3174                                 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3175                         adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3176                         adev->psp.spl.start_addr = ucode_array_start_addr +
3177                                 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3178                         adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3179                         adev->psp.rl.start_addr = ucode_array_start_addr +
3180                                 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3181                 }
3182                 break;
3183         case 2:
3184                 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3185
3186                 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3187                         dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3188                         err = -EINVAL;
3189                         goto out;
3190                 }
3191
3192                 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3193                         err = parse_sos_bin_descriptor(psp,
3194                                                        &sos_hdr_v2_0->psp_fw_bin[fw_index],
3195                                                        sos_hdr_v2_0);
3196                         if (err)
3197                                 goto out;
3198                 }
3199                 break;
3200         default:
3201                 dev_err(adev->dev,
3202                         "unsupported psp sos firmware\n");
3203                 err = -EINVAL;
3204                 goto out;
3205         }
3206
3207         return 0;
3208 out:
3209         amdgpu_ucode_release(&adev->psp.sos_fw);
3210
3211         return err;
3212 }
3213
3214 static int parse_ta_bin_descriptor(struct psp_context *psp,
3215                                    const struct psp_fw_bin_desc *desc,
3216                                    const struct ta_firmware_header_v2_0 *ta_hdr)
3217 {
3218         uint8_t *ucode_start_addr  = NULL;
3219
3220         if (!psp || !desc || !ta_hdr)
3221                 return -EINVAL;
3222
3223         ucode_start_addr  = (uint8_t *)ta_hdr +
3224                             le32_to_cpu(desc->offset_bytes) +
3225                             le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3226
3227         switch (desc->fw_type) {
3228         case TA_FW_TYPE_PSP_ASD:
3229                 psp->asd_context.bin_desc.fw_version        = le32_to_cpu(desc->fw_version);
3230                 psp->asd_context.bin_desc.feature_version   = le32_to_cpu(desc->fw_version);
3231                 psp->asd_context.bin_desc.size_bytes        = le32_to_cpu(desc->size_bytes);
3232                 psp->asd_context.bin_desc.start_addr        = ucode_start_addr;
3233                 break;
3234         case TA_FW_TYPE_PSP_XGMI:
3235                 psp->xgmi_context.context.bin_desc.fw_version       = le32_to_cpu(desc->fw_version);
3236                 psp->xgmi_context.context.bin_desc.size_bytes       = le32_to_cpu(desc->size_bytes);
3237                 psp->xgmi_context.context.bin_desc.start_addr       = ucode_start_addr;
3238                 break;
3239         case TA_FW_TYPE_PSP_RAS:
3240                 psp->ras_context.context.bin_desc.fw_version        = le32_to_cpu(desc->fw_version);
3241                 psp->ras_context.context.bin_desc.size_bytes        = le32_to_cpu(desc->size_bytes);
3242                 psp->ras_context.context.bin_desc.start_addr        = ucode_start_addr;
3243                 break;
3244         case TA_FW_TYPE_PSP_HDCP:
3245                 psp->hdcp_context.context.bin_desc.fw_version       = le32_to_cpu(desc->fw_version);
3246                 psp->hdcp_context.context.bin_desc.size_bytes       = le32_to_cpu(desc->size_bytes);
3247                 psp->hdcp_context.context.bin_desc.start_addr       = ucode_start_addr;
3248                 break;
3249         case TA_FW_TYPE_PSP_DTM:
3250                 psp->dtm_context.context.bin_desc.fw_version       = le32_to_cpu(desc->fw_version);
3251                 psp->dtm_context.context.bin_desc.size_bytes       = le32_to_cpu(desc->size_bytes);
3252                 psp->dtm_context.context.bin_desc.start_addr       = ucode_start_addr;
3253                 break;
3254         case TA_FW_TYPE_PSP_RAP:
3255                 psp->rap_context.context.bin_desc.fw_version       = le32_to_cpu(desc->fw_version);
3256                 psp->rap_context.context.bin_desc.size_bytes       = le32_to_cpu(desc->size_bytes);
3257                 psp->rap_context.context.bin_desc.start_addr       = ucode_start_addr;
3258                 break;
3259         case TA_FW_TYPE_PSP_SECUREDISPLAY:
3260                 psp->securedisplay_context.context.bin_desc.fw_version =
3261                         le32_to_cpu(desc->fw_version);
3262                 psp->securedisplay_context.context.bin_desc.size_bytes =
3263                         le32_to_cpu(desc->size_bytes);
3264                 psp->securedisplay_context.context.bin_desc.start_addr =
3265                         ucode_start_addr;
3266                 break;
3267         default:
3268                 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3269                 break;
3270         }
3271
3272         return 0;
3273 }
3274
3275 static int parse_ta_v1_microcode(struct psp_context *psp)
3276 {
3277         const struct ta_firmware_header_v1_0 *ta_hdr;
3278         struct amdgpu_device *adev = psp->adev;
3279
3280         ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3281
3282         if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3283                 return -EINVAL;
3284
3285         adev->psp.xgmi_context.context.bin_desc.fw_version =
3286                 le32_to_cpu(ta_hdr->xgmi.fw_version);
3287         adev->psp.xgmi_context.context.bin_desc.size_bytes =
3288                 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3289         adev->psp.xgmi_context.context.bin_desc.start_addr =
3290                 (uint8_t *)ta_hdr +
3291                 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3292
3293         adev->psp.ras_context.context.bin_desc.fw_version =
3294                 le32_to_cpu(ta_hdr->ras.fw_version);
3295         adev->psp.ras_context.context.bin_desc.size_bytes =
3296                 le32_to_cpu(ta_hdr->ras.size_bytes);
3297         adev->psp.ras_context.context.bin_desc.start_addr =
3298                 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3299                 le32_to_cpu(ta_hdr->ras.offset_bytes);
3300
3301         adev->psp.hdcp_context.context.bin_desc.fw_version =
3302                 le32_to_cpu(ta_hdr->hdcp.fw_version);
3303         adev->psp.hdcp_context.context.bin_desc.size_bytes =
3304                 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3305         adev->psp.hdcp_context.context.bin_desc.start_addr =
3306                 (uint8_t *)ta_hdr +
3307                 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3308
3309         adev->psp.dtm_context.context.bin_desc.fw_version =
3310                 le32_to_cpu(ta_hdr->dtm.fw_version);
3311         adev->psp.dtm_context.context.bin_desc.size_bytes =
3312                 le32_to_cpu(ta_hdr->dtm.size_bytes);
3313         adev->psp.dtm_context.context.bin_desc.start_addr =
3314                 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3315                 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3316
3317         adev->psp.securedisplay_context.context.bin_desc.fw_version =
3318                 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3319         adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3320                 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3321         adev->psp.securedisplay_context.context.bin_desc.start_addr =
3322                 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3323                 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3324
3325         adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3326
3327         return 0;
3328 }
3329
3330 static int parse_ta_v2_microcode(struct psp_context *psp)
3331 {
3332         const struct ta_firmware_header_v2_0 *ta_hdr;
3333         struct amdgpu_device *adev = psp->adev;
3334         int err = 0;
3335         int ta_index = 0;
3336
3337         ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3338
3339         if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3340                 return -EINVAL;
3341
3342         if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3343                 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3344                 return -EINVAL;
3345         }
3346
3347         for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3348                 err = parse_ta_bin_descriptor(psp,
3349                                               &ta_hdr->ta_fw_bin[ta_index],
3350                                               ta_hdr);
3351                 if (err)
3352                         return err;
3353         }
3354
3355         return 0;
3356 }
3357
3358 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3359 {
3360         const struct common_firmware_header *hdr;
3361         struct amdgpu_device *adev = psp->adev;
3362         char fw_name[PSP_FW_NAME_LEN];
3363         int err;
3364
3365         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3366         err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3367         if (err)
3368                 return err;
3369
3370         hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3371         switch (le16_to_cpu(hdr->header_version_major)) {
3372         case 1:
3373                 err = parse_ta_v1_microcode(psp);
3374                 break;
3375         case 2:
3376                 err = parse_ta_v2_microcode(psp);
3377                 break;
3378         default:
3379                 dev_err(adev->dev, "unsupported TA header version\n");
3380                 err = -EINVAL;
3381         }
3382
3383         if (err)
3384                 amdgpu_ucode_release(&adev->psp.ta_fw);
3385
3386         return err;
3387 }
3388
3389 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3390 {
3391         struct amdgpu_device *adev = psp->adev;
3392         char fw_name[PSP_FW_NAME_LEN];
3393         const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3394         struct amdgpu_firmware_info *info = NULL;
3395         int err = 0;
3396
3397         if (!amdgpu_sriov_vf(adev)) {
3398                 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3399                 return -EINVAL;
3400         }
3401
3402         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3403         err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3404         if (err) {
3405                 if (err == -ENODEV) {
3406                         dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3407                         err = 0;
3408                         goto out;
3409                 }
3410                 dev_err(adev->dev, "fail to initialize cap microcode\n");
3411         }
3412
3413         info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3414         info->ucode_id = AMDGPU_UCODE_ID_CAP;
3415         info->fw = adev->psp.cap_fw;
3416         cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3417                 adev->psp.cap_fw->data;
3418         adev->firmware.fw_size += ALIGN(
3419                         le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3420         adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3421         adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3422         adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3423
3424         return 0;
3425
3426 out:
3427         amdgpu_ucode_release(&adev->psp.cap_fw);
3428         return err;
3429 }
3430
3431 static int psp_set_clockgating_state(void *handle,
3432                                      enum amd_clockgating_state state)
3433 {
3434         return 0;
3435 }
3436
3437 static int psp_set_powergating_state(void *handle,
3438                                      enum amd_powergating_state state)
3439 {
3440         return 0;
3441 }
3442
3443 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3444                                          struct device_attribute *attr,
3445                                          char *buf)
3446 {
3447         struct drm_device *ddev = dev_get_drvdata(dev);
3448         struct amdgpu_device *adev = drm_to_adev(ddev);
3449         uint32_t fw_ver;
3450         int ret;
3451
3452         if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3453                 DRM_INFO("PSP block is not ready yet.");
3454                 return -EBUSY;
3455         }
3456
3457         mutex_lock(&adev->psp.mutex);
3458         ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3459         mutex_unlock(&adev->psp.mutex);
3460
3461         if (ret) {
3462                 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3463                 return ret;
3464         }
3465
3466         return sysfs_emit(buf, "%x\n", fw_ver);
3467 }
3468
3469 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3470                                                        struct device_attribute *attr,
3471                                                        const char *buf,
3472                                                        size_t count)
3473 {
3474         struct drm_device *ddev = dev_get_drvdata(dev);
3475         struct amdgpu_device *adev = drm_to_adev(ddev);
3476         int ret, idx;
3477         char fw_name[100];
3478         const struct firmware *usbc_pd_fw;
3479         struct amdgpu_bo *fw_buf_bo = NULL;
3480         uint64_t fw_pri_mc_addr;
3481         void *fw_pri_cpu_addr;
3482
3483         if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3484                 DRM_INFO("PSP block is not ready yet.");
3485                 return -EBUSY;
3486         }
3487
3488         if (!drm_dev_enter(ddev, &idx))
3489                 return -ENODEV;
3490
3491         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3492         ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3493         if (ret)
3494                 goto fail;
3495
3496         /* LFB address which is aligned to 1MB boundary per PSP request */
3497         ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3498                                       AMDGPU_GEM_DOMAIN_VRAM |
3499                                       AMDGPU_GEM_DOMAIN_GTT,
3500                                       &fw_buf_bo, &fw_pri_mc_addr,
3501                                       &fw_pri_cpu_addr);
3502         if (ret)
3503                 goto rel_buf;
3504
3505         memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3506
3507         mutex_lock(&adev->psp.mutex);
3508         ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3509         mutex_unlock(&adev->psp.mutex);
3510
3511         amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3512
3513 rel_buf:
3514         release_firmware(usbc_pd_fw);
3515 fail:
3516         if (ret) {
3517                 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3518                 count = ret;
3519         }
3520
3521         drm_dev_exit(idx);
3522         return count;
3523 }
3524
3525 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3526 {
3527         int idx;
3528
3529         if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3530                 return;
3531
3532         memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3533         memcpy(psp->fw_pri_buf, start_addr, bin_size);
3534
3535         drm_dev_exit(idx);
3536 }
3537
3538 static DEVICE_ATTR(usbc_pd_fw, 0644,
3539                    psp_usbc_pd_fw_sysfs_read,
3540                    psp_usbc_pd_fw_sysfs_write);
3541
3542 int is_psp_fw_valid(struct psp_bin_desc bin)
3543 {
3544         return bin.size_bytes;
3545 }
3546
3547 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3548                                         struct bin_attribute *bin_attr,
3549                                         char *buffer, loff_t pos, size_t count)
3550 {
3551         struct device *dev = kobj_to_dev(kobj);
3552         struct drm_device *ddev = dev_get_drvdata(dev);
3553         struct amdgpu_device *adev = drm_to_adev(ddev);
3554
3555         adev->psp.vbflash_done = false;
3556
3557         /* Safeguard against memory drain */
3558         if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3559                 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3560                 kvfree(adev->psp.vbflash_tmp_buf);
3561                 adev->psp.vbflash_tmp_buf = NULL;
3562                 adev->psp.vbflash_image_size = 0;
3563                 return -ENOMEM;
3564         }
3565
3566         /* TODO Just allocate max for now and optimize to realloc later if needed */
3567         if (!adev->psp.vbflash_tmp_buf) {
3568                 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3569                 if (!adev->psp.vbflash_tmp_buf)
3570                         return -ENOMEM;
3571         }
3572
3573         mutex_lock(&adev->psp.mutex);
3574         memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3575         adev->psp.vbflash_image_size += count;
3576         mutex_unlock(&adev->psp.mutex);
3577
3578         dev_info(adev->dev, "VBIOS flash write PSP done");
3579
3580         return count;
3581 }
3582
3583 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3584                                        struct bin_attribute *bin_attr, char *buffer,
3585                                        loff_t pos, size_t count)
3586 {
3587         struct device *dev = kobj_to_dev(kobj);
3588         struct drm_device *ddev = dev_get_drvdata(dev);
3589         struct amdgpu_device *adev = drm_to_adev(ddev);
3590         struct amdgpu_bo *fw_buf_bo = NULL;
3591         uint64_t fw_pri_mc_addr;
3592         void *fw_pri_cpu_addr;
3593         int ret;
3594
3595         dev_info(adev->dev, "VBIOS flash to PSP started");
3596
3597         ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3598                                         AMDGPU_GPU_PAGE_SIZE,
3599                                         AMDGPU_GEM_DOMAIN_VRAM,
3600                                         &fw_buf_bo,
3601                                         &fw_pri_mc_addr,
3602                                         &fw_pri_cpu_addr);
3603         if (ret)
3604                 goto rel_buf;
3605
3606         memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3607
3608         mutex_lock(&adev->psp.mutex);
3609         ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3610         mutex_unlock(&adev->psp.mutex);
3611
3612         amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3613
3614 rel_buf:
3615         kvfree(adev->psp.vbflash_tmp_buf);
3616         adev->psp.vbflash_tmp_buf = NULL;
3617         adev->psp.vbflash_image_size = 0;
3618
3619         if (ret) {
3620                 dev_err(adev->dev, "Failed to load VBIOS FW, err = %d", ret);
3621                 return ret;
3622         }
3623
3624         dev_info(adev->dev, "VBIOS flash to PSP done");
3625         return 0;
3626 }
3627
3628 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3629                                          struct device_attribute *attr,
3630                                          char *buf)
3631 {
3632         struct drm_device *ddev = dev_get_drvdata(dev);
3633         struct amdgpu_device *adev = drm_to_adev(ddev);
3634         uint32_t vbflash_status;
3635
3636         vbflash_status = psp_vbflash_status(&adev->psp);
3637         if (!adev->psp.vbflash_done)
3638                 vbflash_status = 0;
3639         else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3640                 vbflash_status = 1;
3641
3642         return sysfs_emit(buf, "0x%x\n", vbflash_status);
3643 }
3644
3645 static const struct bin_attribute psp_vbflash_bin_attr = {
3646         .attr = {.name = "psp_vbflash", .mode = 0664},
3647         .size = 0,
3648         .write = amdgpu_psp_vbflash_write,
3649         .read = amdgpu_psp_vbflash_read,
3650 };
3651
3652 static DEVICE_ATTR(psp_vbflash_status, 0444, amdgpu_psp_vbflash_status, NULL);
3653
3654 int amdgpu_psp_sysfs_init(struct amdgpu_device *adev)
3655 {
3656         int ret = 0;
3657         struct psp_context *psp = &adev->psp;
3658
3659         if (amdgpu_sriov_vf(adev))
3660                 return -EINVAL;
3661
3662         switch (adev->ip_versions[MP0_HWIP][0]) {
3663         case IP_VERSION(13, 0, 0):
3664         case IP_VERSION(13, 0, 7):
3665         case IP_VERSION(13, 0, 10):
3666                 if (!psp->adev) {
3667                         psp->adev = adev;
3668                         psp_v13_0_set_psp_funcs(psp);
3669                 }
3670                 ret = sysfs_create_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3671                 if (ret)
3672                         dev_err(adev->dev, "Failed to create device file psp_vbflash");
3673                 ret = device_create_file(adev->dev, &dev_attr_psp_vbflash_status);
3674                 if (ret)
3675                         dev_err(adev->dev, "Failed to create device file psp_vbflash_status");
3676                 return ret;
3677         default:
3678                 return 0;
3679         }
3680 }
3681
3682 const struct amd_ip_funcs psp_ip_funcs = {
3683         .name = "psp",
3684         .early_init = psp_early_init,
3685         .late_init = NULL,
3686         .sw_init = psp_sw_init,
3687         .sw_fini = psp_sw_fini,
3688         .hw_init = psp_hw_init,
3689         .hw_fini = psp_hw_fini,
3690         .suspend = psp_suspend,
3691         .resume = psp_resume,
3692         .is_idle = NULL,
3693         .check_soft_reset = NULL,
3694         .wait_for_idle = NULL,
3695         .soft_reset = NULL,
3696         .set_clockgating_state = psp_set_clockgating_state,
3697         .set_powergating_state = psp_set_powergating_state,
3698 };
3699
3700 static int psp_sysfs_init(struct amdgpu_device *adev)
3701 {
3702         int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3703
3704         if (ret)
3705                 DRM_ERROR("Failed to create USBC PD FW control file!");
3706
3707         return ret;
3708 }
3709
3710 void amdgpu_psp_sysfs_fini(struct amdgpu_device *adev)
3711 {
3712         sysfs_remove_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3713         device_remove_file(adev->dev, &dev_attr_psp_vbflash_status);
3714 }
3715
3716 static void psp_sysfs_fini(struct amdgpu_device *adev)
3717 {
3718         device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3719 }
3720
3721 const struct amdgpu_ip_block_version psp_v3_1_ip_block = {
3722         .type = AMD_IP_BLOCK_TYPE_PSP,
3723         .major = 3,
3724         .minor = 1,
3725         .rev = 0,
3726         .funcs = &psp_ip_funcs,
3727 };
3728
3729 const struct amdgpu_ip_block_version psp_v10_0_ip_block = {
3730         .type = AMD_IP_BLOCK_TYPE_PSP,
3731         .major = 10,
3732         .minor = 0,
3733         .rev = 0,
3734         .funcs = &psp_ip_funcs,
3735 };
3736
3737 const struct amdgpu_ip_block_version psp_v11_0_ip_block = {
3738         .type = AMD_IP_BLOCK_TYPE_PSP,
3739         .major = 11,
3740         .minor = 0,
3741         .rev = 0,
3742         .funcs = &psp_ip_funcs,
3743 };
3744
3745 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3746         .type = AMD_IP_BLOCK_TYPE_PSP,
3747         .major = 11,
3748         .minor = 0,
3749         .rev = 8,
3750         .funcs = &psp_ip_funcs,
3751 };
3752
3753 const struct amdgpu_ip_block_version psp_v12_0_ip_block = {
3754         .type = AMD_IP_BLOCK_TYPE_PSP,
3755         .major = 12,
3756         .minor = 0,
3757         .rev = 0,
3758         .funcs = &psp_ip_funcs,
3759 };
3760
3761 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3762         .type = AMD_IP_BLOCK_TYPE_PSP,
3763         .major = 13,
3764         .minor = 0,
3765         .rev = 0,
3766         .funcs = &psp_ip_funcs,
3767 };
3768
3769 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3770         .type = AMD_IP_BLOCK_TYPE_PSP,
3771         .major = 13,
3772         .minor = 0,
3773         .rev = 4,
3774         .funcs = &psp_ip_funcs,
3775 };
This page took 0.269786 seconds and 4 git commands to generate.