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