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