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