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