1 /* 2 * Copyright 2018-2024 Advanced Micro Devices, Inc. All rights reserved. 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 */ 23 24 #include <linux/firmware.h> 25 #include <linux/kernfs.h> 26 27 #include "amdgpu.h" 28 #include "amdgpu_discovery.h" 29 #include "amdgpu_ualink.h" 30 #include "soc15_hw_ip.h" 31 #include "discovery.h" 32 #include "amdgpu_ras.h" 33 34 #include "soc15.h" 35 #include "gfx_v9_0.h" 36 #include "gfx_v9_4_3.h" 37 #include "gmc_v9_0.h" 38 #include "df_v1_7.h" 39 #include "df_v3_6.h" 40 #include "df_v4_3.h" 41 #include "df_v4_6_2.h" 42 #include "df_v4_15.h" 43 #include "nbio_v6_1.h" 44 #include "nbio_v7_0.h" 45 #include "nbio_v7_4.h" 46 #include "nbio_v7_9.h" 47 #include "nbio_v7_11.h" 48 #include "nbio_v7_11_5.h" 49 #include "hdp_v4_0.h" 50 #include "vega10_ih.h" 51 #include "vega20_ih.h" 52 #include "sdma_v4_0.h" 53 #include "sdma_v4_4_2.h" 54 #include "uvd_v7_0.h" 55 #include "vce_v4_0.h" 56 #include "vcn_v1_0.h" 57 #include "vcn_v2_5.h" 58 #include "jpeg_v2_5.h" 59 #include "smuio_v9_0.h" 60 #include "gmc_v10_0.h" 61 #include "gmc_v11_0.h" 62 #include "gmc_v12_0.h" 63 #include "gfxhub_v2_0.h" 64 #include "mmhub_v2_0.h" 65 #include "nbio_v2_3.h" 66 #include "nbio_v4_3.h" 67 #include "nbio_v7_2.h" 68 #include "nbio_v7_7.h" 69 #include "nbif_v6_3_1.h" 70 #include "nbio_v6_3_2.h" 71 #include "nbif_v7_10.h" 72 #include "hdp_v5_0.h" 73 #include "hdp_v5_2.h" 74 #include "hdp_v6_0.h" 75 #include "hdp_v7_0.h" 76 #include "hdp_v8_0.h" 77 #include "nv.h" 78 #include "soc21.h" 79 #include "soc24.h" 80 #include "soc_v1_0.h" 81 #include "navi10_ih.h" 82 #include "ih_v6_0.h" 83 #include "ih_v6_1.h" 84 #include "ih_v7_0.h" 85 #include "ih_v8_0.h" 86 #include "gfx_v10_0.h" 87 #include "gfx_v11_0.h" 88 #include "gfx_v12_0.h" 89 #include "gfx_v12_1.h" 90 #include "sdma_v5_0.h" 91 #include "sdma_v5_2.h" 92 #include "sdma_v6_0.h" 93 #include "sdma_v7_0.h" 94 #include "sdma_v7_1.h" 95 #include "lsdma_v6_0.h" 96 #include "lsdma_v7_0.h" 97 #include "lsdma_v7_1.h" 98 #include "lsdma_v8_0.h" 99 #include "vcn_v2_0.h" 100 #include "jpeg_v2_0.h" 101 #include "vcn_v3_0.h" 102 #include "jpeg_v3_0.h" 103 #include "vcn_v4_0.h" 104 #include "jpeg_v4_0.h" 105 #include "vcn_v4_0_3.h" 106 #include "jpeg_v4_0_3.h" 107 #include "vcn_v4_0_5.h" 108 #include "jpeg_v4_0_5.h" 109 #include "amdgpu_vkms.h" 110 #include "mes_v11_0.h" 111 #include "mes_v12_0.h" 112 #include "mes_v12_1.h" 113 #include "smuio_v11_0.h" 114 #include "smuio_v11_0_6.h" 115 #include "smuio_v13_0.h" 116 #include "smuio_v13_0_3.h" 117 #include "smuio_v13_0_6.h" 118 #include "smuio_v14_0_2.h" 119 #include "smuio_v15_0_0.h" 120 #include "smuio_v15_0_3.h" 121 #include "smuio_v15_0_8.h" 122 #include "vcn_v5_0_0.h" 123 #include "vcn_v5_0_1.h" 124 #include "vcn_v5_0_2.h" 125 #include "jpeg_v5_0_0.h" 126 #include "jpeg_v5_0_1.h" 127 #include "jpeg_v5_0_2.h" 128 #include "jpeg_v5_3_0.h" 129 130 #include "amdgpu_ras_mgr.h" 131 132 #include "amdgpu_vpe.h" 133 #if defined(CONFIG_DRM_AMD_ISP) 134 #include "amdgpu_isp.h" 135 #endif 136 137 MODULE_FIRMWARE("amdgpu/ip_discovery.bin"); 138 MODULE_FIRMWARE("amdgpu/vega10_ip_discovery.bin"); 139 MODULE_FIRMWARE("amdgpu/vega12_ip_discovery.bin"); 140 MODULE_FIRMWARE("amdgpu/vega20_ip_discovery.bin"); 141 MODULE_FIRMWARE("amdgpu/raven_ip_discovery.bin"); 142 MODULE_FIRMWARE("amdgpu/raven2_ip_discovery.bin"); 143 MODULE_FIRMWARE("amdgpu/picasso_ip_discovery.bin"); 144 MODULE_FIRMWARE("amdgpu/arcturus_ip_discovery.bin"); 145 MODULE_FIRMWARE("amdgpu/aldebaran_ip_discovery.bin"); 146 147 /* Note: These registers are consistent across all the SOCs */ 148 #define mmIP_DISCOVERY_VERSION 0x16A00 149 #define mmRCC_CONFIG_MEMSIZE 0xde3 150 #define mmRCC_HW_DEBUG 0x10c01 151 #define mmMP0_SMN_C2PMSG_33 0x16061 152 #define mmMM_INDEX 0x0 153 #define mmMM_INDEX_HI 0x6 154 #define mmMM_DATA 0x1 155 156 #define mmDRIVER_SCRATCH_0 0x94 157 #define mmDRIVER_SCRATCH_1 0x95 158 #define mmDRIVER_SCRATCH_2 0x96 159 160 struct ip_discovery_top { 161 struct kobject kobj; 162 struct kset die_kset; 163 struct pci_dev *pdev; 164 struct amdgpu_device *adev; 165 uint8_t *discovery_bin; 166 uint32_t bin_size; 167 bool standalone_mode; 168 }; 169 170 /* List to track early-initialized ip_discovery_top entries */ 171 struct early_ip_discovery { 172 struct list_head list; 173 struct pci_dev *pdev; 174 struct ip_discovery_top *ip_top; 175 }; 176 177 static LIST_HEAD(early_ip_discovery_list); 178 static DEFINE_MUTEX(early_ip_discovery_mutex); 179 180 static const char *hw_id_names[HW_ID_MAX] = { 181 [MP1_HWID] = "MP1", 182 [MP2_HWID] = "MP2", 183 [THM_HWID] = "THM", 184 [SMUIO_HWID] = "SMUIO", 185 [FUSE_HWID] = "FUSE", 186 [CLKA_HWID] = "CLKA", 187 [PWR_HWID] = "PWR", 188 [GC_HWID] = "GC", 189 [UVD_HWID] = "UVD", 190 [AUDIO_AZ_HWID] = "AUDIO_AZ", 191 [ACP_HWID] = "ACP", 192 [DCI_HWID] = "DCI", 193 [DMU_HWID] = "DMU", 194 [DCO_HWID] = "DCO", 195 [DIO_HWID] = "DIO", 196 [XDMA_HWID] = "XDMA", 197 [DCEAZ_HWID] = "DCEAZ", 198 [DAZ_HWID] = "DAZ", 199 [SDPMUX_HWID] = "SDPMUX", 200 [NTB_HWID] = "NTB", 201 [IOHC_HWID] = "IOHC", 202 [L2IMU_HWID] = "L2IMU", 203 [VCE_HWID] = "VCE", 204 [MMHUB_HWID] = "MMHUB", 205 [ATHUB_HWID] = "ATHUB", 206 [DBGU_NBIO_HWID] = "DBGU_NBIO", 207 [DFX_HWID] = "DFX", 208 [DBGU0_HWID] = "DBGU0", 209 [DBGU1_HWID] = "DBGU1", 210 [OSSSYS_HWID] = "OSSSYS", 211 [HDP_HWID] = "HDP", 212 [SDMA0_HWID] = "SDMA0", 213 [SDMA1_HWID] = "SDMA1", 214 [SDMA2_HWID] = "SDMA2", 215 [SDMA3_HWID] = "SDMA3", 216 [LSDMA_HWID] = "LSDMA", 217 [ISP_HWID] = "ISP", 218 [DBGU_IO_HWID] = "DBGU_IO", 219 [DF_HWID] = "DF", 220 [CLKB_HWID] = "CLKB", 221 [FCH_HWID] = "FCH", 222 [DFX_DAP_HWID] = "DFX_DAP", 223 [L1IMU_PCIE_HWID] = "L1IMU_PCIE", 224 [L1IMU_NBIF_HWID] = "L1IMU_NBIF", 225 [L1IMU_IOAGR_HWID] = "L1IMU_IOAGR", 226 [L1IMU3_HWID] = "L1IMU3", 227 [L1IMU4_HWID] = "L1IMU4", 228 [L1IMU5_HWID] = "L1IMU5", 229 [L1IMU6_HWID] = "L1IMU6", 230 [L1IMU7_HWID] = "L1IMU7", 231 [L1IMU8_HWID] = "L1IMU8", 232 [L1IMU9_HWID] = "L1IMU9", 233 [L1IMU10_HWID] = "L1IMU10", 234 [L1IMU11_HWID] = "L1IMU11", 235 [L1IMU12_HWID] = "L1IMU12", 236 [L1IMU13_HWID] = "L1IMU13", 237 [L1IMU14_HWID] = "L1IMU14", 238 [L1IMU15_HWID] = "L1IMU15", 239 [WAFLC_HWID] = "WAFLC", 240 [FCH_USB_PD_HWID] = "FCH_USB_PD", 241 [PCIE_HWID] = "PCIE", 242 [PCS_HWID] = "PCS", 243 [DDCL_HWID] = "DDCL", 244 [SST_HWID] = "SST", 245 [IOAGR_HWID] = "IOAGR", 246 [NBIF_HWID] = "NBIF", 247 [IOAPIC_HWID] = "IOAPIC", 248 [SYSTEMHUB_HWID] = "SYSTEMHUB", 249 [NTBCCP_HWID] = "NTBCCP", 250 [UMC_HWID] = "UMC", 251 [SATA_HWID] = "SATA", 252 [USB_HWID] = "USB", 253 [CCXSEC_HWID] = "CCXSEC", 254 [XGMI_HWID] = "XGMI", 255 [XGBE_HWID] = "XGBE", 256 [MP0_HWID] = "MP0", 257 [VPE_HWID] = "VPE", 258 [UMSCH_HWID] = "UMSCH", 259 [ATU_HWID] = "ATU", 260 [AIGC_HWID] = "AIGC", 261 }; 262 263 static int hw_id_map[MAX_HWIP] = { 264 [GC_HWIP] = GC_HWID, 265 [HDP_HWIP] = HDP_HWID, 266 [SDMA0_HWIP] = SDMA0_HWID, 267 [SDMA1_HWIP] = SDMA1_HWID, 268 [SDMA2_HWIP] = SDMA2_HWID, 269 [SDMA3_HWIP] = SDMA3_HWID, 270 [LSDMA_HWIP] = LSDMA_HWID, 271 [MMHUB_HWIP] = MMHUB_HWID, 272 [ATHUB_HWIP] = ATHUB_HWID, 273 [NBIO_HWIP] = NBIF_HWID, 274 [MP0_HWIP] = MP0_HWID, 275 [MP1_HWIP] = MP1_HWID, 276 [UVD_HWIP] = UVD_HWID, 277 [VCE_HWIP] = VCE_HWID, 278 [DF_HWIP] = DF_HWID, 279 [DCE_HWIP] = DMU_HWID, 280 [OSSSYS_HWIP] = OSSSYS_HWID, 281 [SMUIO_HWIP] = SMUIO_HWID, 282 [PWR_HWIP] = PWR_HWID, 283 [NBIF_HWIP] = NBIF_HWID, 284 [THM_HWIP] = THM_HWID, 285 [CLK_HWIP] = CLKA_HWID, 286 [UMC_HWIP] = UMC_HWID, 287 [XGMI_HWIP] = XGMI_HWID, 288 [DCI_HWIP] = DCI_HWID, 289 [PCIE_HWIP] = PCIE_HWID, 290 [VPE_HWIP] = VPE_HWID, 291 [UMSCH_HWIP] = UMSCH_HWID, 292 [ISP_HWIP] = ISP_HWID, 293 [ATU_HWIP] = ATU_HWID, 294 }; 295 296 static u32 amdgpu_discovery_get_config_memsize(struct amdgpu_device *adev) 297 { 298 u32 memsize = RREG32(mmRCC_CONFIG_MEMSIZE); 299 300 /* Fall back to RCC_HW_DEBUG when RCC_CONFIG_MEMSIZE reads 0 on a VF. */ 301 if (!memsize && amdgpu_sriov_vf(adev)) 302 memsize = RREG32(mmRCC_HW_DEBUG); 303 304 return memsize; 305 } 306 307 static int amdgpu_discovery_get_tmr_info(struct amdgpu_device *adev, 308 bool *is_tmr_in_sysmem) 309 { 310 u64 vram_size, tmr_offset, tmr_size; 311 u32 msg, tmr_offset_lo, tmr_offset_hi; 312 int i, ret; 313 314 if (!amdgpu_sriov_vf(adev)) { 315 /* It can take up to two second for IFWI init to complete on some dGPUs, 316 * but generally it should be in the 60-100ms range. Normally this starts 317 * as soon as the device gets power so by the time the OS loads this has long 318 * completed. However, when a card is hotplugged via e.g., USB4, we need to 319 * wait for this to complete. Once the C2PMSG is updated, we can 320 * continue. 321 */ 322 323 for (i = 0; i < 2000; i++) { 324 msg = RREG32(mmMP0_SMN_C2PMSG_33); 325 if (msg & 0x80000000) 326 break; 327 msleep(1); 328 } 329 } 330 331 vram_size = amdgpu_discovery_get_config_memsize(adev); 332 if (vram_size == U32_MAX) 333 return -ENXIO; 334 else if (!vram_size) 335 *is_tmr_in_sysmem = true; 336 else 337 *is_tmr_in_sysmem = false; 338 339 /* init the default tmr size and offset */ 340 adev->discovery.size = DISCOVERY_TMR_SIZE; 341 if (vram_size) 342 adev->discovery.offset = (vram_size << 20) - DISCOVERY_TMR_OFFSET; 343 344 if (amdgpu_sriov_vf(adev)) { 345 if (adev->virt.is_dynamic_crit_regn_enabled) { 346 adev->discovery.offset = 347 adev->virt.crit_regn_tbl[AMD_SRIOV_MSG_IPD_TABLE_ID].offset; 348 adev->discovery.size = 349 adev->virt.crit_regn_tbl[AMD_SRIOV_MSG_IPD_TABLE_ID].size_kb << 10; 350 if (!adev->discovery.size) 351 return -EINVAL; 352 } else { 353 goto out; 354 } 355 } else { 356 if (adev->discovery.offset) { 357 u32 signature; 358 359 /* If VRAM holds a valid discovery signature at the default 360 * discovery offset, use it as-is. 361 */ 362 amdgpu_device_vram_access(adev, adev->discovery.offset, 363 &signature, sizeof(signature), 364 false); 365 if (le32_to_cpu(signature) == BINARY_SIGNATURE) 366 goto out; 367 } 368 369 tmr_size = RREG32(mmDRIVER_SCRATCH_2); 370 if (tmr_size) { 371 /* It's preferred to transition to PSP mailbox reg interface 372 * for both bare-metal and passthrough if available */ 373 adev->discovery.size = (u32)tmr_size; 374 tmr_offset_lo = RREG32(mmDRIVER_SCRATCH_0); 375 tmr_offset_hi = RREG32(mmDRIVER_SCRATCH_1); 376 adev->discovery.offset = ((u64)le32_to_cpu(tmr_offset_hi) << 32 | 377 le32_to_cpu(tmr_offset_lo)); 378 adev->discovery.reserve_tmr = true; 379 } else if (!vram_size) { 380 /* fall back to apci approach to query tmr offset if vram_size is 0 */ 381 ret = amdgpu_acpi_get_tmr_info(adev, &tmr_offset, &tmr_size); 382 if (ret) 383 return ret; 384 adev->discovery.size = DISCOVERY_TMR_SIZE; 385 adev->discovery.offset = tmr_offset + tmr_size - DISCOVERY_TMR_OFFSET; 386 } 387 } 388 out: 389 adev->discovery.bin = kzalloc(adev->discovery.size, GFP_KERNEL); 390 if (!adev->discovery.bin) 391 return -ENOMEM; 392 adev->discovery.debugfs_blob.data = adev->discovery.bin; 393 adev->discovery.debugfs_blob.size = adev->discovery.size; 394 395 return 0; 396 } 397 398 static int amdgpu_discovery_read_binary_from_sysmem(struct amdgpu_device *adev, uint8_t *binary) 399 { 400 void *discv_regn; 401 402 /* This region is read-only and reserved from system use */ 403 discv_regn = memremap(adev->discovery.offset, adev->discovery.size, MEMREMAP_WC); 404 if (discv_regn) { 405 memcpy(binary, discv_regn, adev->discovery.size); 406 memunmap(discv_regn); 407 return 0; 408 } 409 410 return -ENOENT; 411 } 412 413 #define IP_DISCOVERY_V2 2 414 #define IP_DISCOVERY_V4 4 415 416 static int amdgpu_discovery_read_binary_from_mem(struct amdgpu_device *adev, 417 uint8_t *binary, 418 bool is_tmr_in_sysmem) 419 { 420 int ret = 0; 421 422 if (!is_tmr_in_sysmem) { 423 if (amdgpu_sriov_vf(adev) && 424 amdgpu_sriov_xgmi_connected_to_cpu(adev)) { 425 ret = amdgpu_discovery_read_binary_from_sysmem(adev, binary); 426 } else { 427 amdgpu_device_vram_access(adev, adev->discovery.offset, 428 (uint32_t *)binary, 429 adev->discovery.size, false); 430 adev->discovery.reserve_tmr = true; 431 } 432 } else { 433 ret = amdgpu_discovery_read_binary_from_sysmem(adev, binary); 434 } 435 436 return ret; 437 } 438 439 static int amdgpu_discovery_read_binary_from_file(struct amdgpu_device *adev, 440 uint8_t *binary, 441 const char *fw_name) 442 { 443 const struct firmware *fw; 444 int r; 445 446 r = firmware_request_nowarn(&fw, fw_name, adev->dev); 447 if (r) { 448 if (amdgpu_discovery == 2) 449 dev_err(adev->dev, "can't load firmware \"%s\"\n", fw_name); 450 else 451 drm_info(&adev->ddev, "Optional firmware \"%s\" was not found\n", fw_name); 452 return r; 453 } 454 455 if (fw->size > adev->discovery.size) { 456 dev_err(adev->dev, 457 "ip discovery firmware \"%s\" too large (%zu > %u)\n", 458 fw_name, fw->size, adev->discovery.size); 459 release_firmware(fw); 460 return -EINVAL; 461 } 462 463 /* Ensure the firmware is at least large enough to contain the 464 * binary header fields. 465 */ 466 if (fw->size < offsetof(struct binary_header, binary_size) + 467 sizeof(((struct binary_header *)0)->binary_size)) { 468 dev_err(adev->dev, 469 "ip discovery firmware \"%s\" too small (%zu)\n", 470 fw_name, fw->size); 471 release_firmware(fw); 472 return -EINVAL; 473 } 474 475 memcpy((u8 *)binary, (u8 *)fw->data, fw->size); 476 release_firmware(fw); 477 478 return 0; 479 } 480 481 static uint16_t amdgpu_discovery_calculate_checksum(uint8_t *data, uint32_t size) 482 { 483 uint16_t checksum = 0; 484 int i; 485 486 for (i = 0; i < size; i++) 487 checksum += data[i]; 488 489 return checksum; 490 } 491 492 static inline bool amdgpu_discovery_verify_checksum(struct amdgpu_device *adev, 493 uint8_t *data, uint32_t size, 494 uint16_t expected) 495 { 496 uint16_t calculated; 497 498 calculated = amdgpu_discovery_calculate_checksum(data, size); 499 500 if (calculated != expected) { 501 dev_err(adev->dev, "Discovery checksum failed: calc 0x%04x != exp 0x%04x, size %u.\n", 502 calculated, expected, size); 503 return false; 504 } 505 506 return true; 507 } 508 509 static inline bool amdgpu_discovery_verify_binary_signature(uint8_t *binary) 510 { 511 struct binary_header *bhdr; 512 bhdr = (struct binary_header *)binary; 513 514 return (le32_to_cpu(bhdr->binary_signature) == BINARY_SIGNATURE); 515 } 516 517 static void amdgpu_discovery_harvest_config_quirk(struct amdgpu_device *adev) 518 { 519 /* 520 * So far, apply this quirk only on those Navy Flounder boards which 521 * have a bad harvest table of VCN config. 522 */ 523 if ((amdgpu_ip_version(adev, UVD_HWIP, 1) == IP_VERSION(3, 0, 1)) && 524 (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(10, 3, 2))) { 525 switch (adev->pdev->revision) { 526 case 0xC1: 527 case 0xC2: 528 case 0xC3: 529 case 0xC5: 530 case 0xC7: 531 case 0xCF: 532 case 0xDF: 533 adev->vcn.harvest_config |= AMDGPU_VCN_HARVEST_VCN1; 534 adev->vcn.inst_mask &= ~AMDGPU_VCN_HARVEST_VCN1; 535 break; 536 default: 537 break; 538 } 539 } 540 } 541 542 static int amdgpu_discovery_verify_npsinfo(struct amdgpu_device *adev, 543 struct table_info *info) 544 { 545 uint8_t *discovery_bin = adev->discovery.bin; 546 uint16_t checksum; 547 uint16_t offset; 548 549 offset = le16_to_cpu(info->offset); 550 checksum = le16_to_cpu(info->checksum); 551 552 struct nps_info_header *nhdr = 553 (struct nps_info_header *)(discovery_bin + offset); 554 555 if (le32_to_cpu(nhdr->table_id) != NPS_INFO_TABLE_ID) { 556 dev_dbg(adev->dev, "invalid ip discovery nps info table id\n"); 557 return -EINVAL; 558 } 559 560 if (!amdgpu_discovery_verify_checksum(adev, discovery_bin + offset, 561 le32_to_cpu(nhdr->size_bytes), 562 checksum)) { 563 dev_dbg(adev->dev, "invalid nps info data table checksum\n"); 564 return -EINVAL; 565 } 566 567 return 0; 568 } 569 570 static const char *amdgpu_discovery_get_fw_name(struct amdgpu_device *adev) 571 { 572 if (amdgpu_discovery == 2) { 573 /* Assume there is valid discovery TMR in VRAM even if binary is sideloaded */ 574 adev->discovery.reserve_tmr = true; 575 return "amdgpu/ip_discovery.bin"; 576 } 577 578 switch (adev->asic_type) { 579 case CHIP_VEGA10: 580 return "amdgpu/vega10_ip_discovery.bin"; 581 case CHIP_VEGA12: 582 return "amdgpu/vega12_ip_discovery.bin"; 583 case CHIP_RAVEN: 584 if (adev->apu_flags & AMD_APU_IS_RAVEN2) 585 return "amdgpu/raven2_ip_discovery.bin"; 586 else if (adev->apu_flags & AMD_APU_IS_PICASSO) 587 return "amdgpu/picasso_ip_discovery.bin"; 588 else 589 return "amdgpu/raven_ip_discovery.bin"; 590 case CHIP_VEGA20: 591 return "amdgpu/vega20_ip_discovery.bin"; 592 case CHIP_ARCTURUS: 593 return "amdgpu/arcturus_ip_discovery.bin"; 594 case CHIP_ALDEBARAN: 595 return "amdgpu/aldebaran_ip_discovery.bin"; 596 default: 597 return NULL; 598 } 599 } 600 601 static struct table_info * 602 amdgpu_discovery_get_table_info_from_bin(uint8_t *discovery_bin, 603 uint16_t table_id) 604 { 605 struct binary_header *bhdr = (struct binary_header *)discovery_bin; 606 struct binary_header_v2 *bhdrv2; 607 608 switch (bhdr->version_major) { 609 case 2: 610 bhdrv2 = (struct binary_header_v2 *)discovery_bin; 611 return &bhdrv2->table_list[table_id]; 612 case 1: 613 case 0: 614 return &bhdr->table_list[table_id]; 615 default: 616 return NULL; 617 } 618 } 619 620 static int amdgpu_discovery_get_table_info(struct amdgpu_device *adev, 621 struct table_info **info, 622 uint16_t table_id) 623 { 624 struct binary_header *bhdr = 625 (struct binary_header *)adev->discovery.bin; 626 627 *info = amdgpu_discovery_get_table_info_from_bin(adev->discovery.bin, 628 table_id); 629 if (!*info) { 630 dev_err(adev->dev, "Invalid ip discovery table version %d\n", 631 bhdr->version_major); 632 return -EINVAL; 633 } 634 635 return 0; 636 } 637 638 static int amdgpu_discovery_table_check(struct amdgpu_device *adev, 639 uint8_t *discovery_bin, 640 uint16_t table_id) 641 { 642 int r, act_val, exp_val, table_size; 643 uint16_t offset, checksum; 644 struct table_info *info; 645 bool check_table = true; 646 char *table_name; 647 648 r = amdgpu_discovery_get_table_info(adev, &info, table_id); 649 if (r) 650 return r; 651 offset = le16_to_cpu(info->offset); 652 checksum = le16_to_cpu(info->checksum); 653 654 switch (table_id) { 655 case IP_DISCOVERY: { 656 struct ip_discovery_header *ihdr = 657 (struct ip_discovery_header *)(discovery_bin + offset); 658 act_val = le32_to_cpu(ihdr->signature); 659 exp_val = DISCOVERY_TABLE_SIGNATURE; 660 table_size = le16_to_cpu(ihdr->size); 661 table_name = "data table"; 662 break; 663 } 664 case GC: { 665 struct gpu_info_header *ghdr = 666 (struct gpu_info_header *)(discovery_bin + offset); 667 act_val = le32_to_cpu(ghdr->table_id); 668 exp_val = GC_TABLE_ID; 669 table_size = le16_to_cpu(ghdr->size); 670 table_name = "gc table"; 671 break; 672 } 673 case HARVEST_INFO: { 674 struct harvest_info_header *hhdr = 675 (struct harvest_info_header *)(discovery_bin + offset); 676 act_val = le32_to_cpu(hhdr->signature); 677 exp_val = HARVEST_TABLE_SIGNATURE; 678 table_size = sizeof(struct harvest_table); 679 table_name = "harvest table"; 680 break; 681 } 682 case VCN_INFO: { 683 struct vcn_info_header *vhdr = 684 (struct vcn_info_header *)(discovery_bin + offset); 685 act_val = le32_to_cpu(vhdr->table_id); 686 exp_val = VCN_INFO_TABLE_ID; 687 table_size = le32_to_cpu(vhdr->size_bytes); 688 table_name = "vcn table"; 689 break; 690 } 691 case MALL_INFO: { 692 struct mall_info_header *mhdr = 693 (struct mall_info_header *)(discovery_bin + offset); 694 act_val = le32_to_cpu(mhdr->table_id); 695 exp_val = MALL_INFO_TABLE_ID; 696 table_size = le32_to_cpu(mhdr->size_bytes); 697 table_name = "mall table"; 698 check_table = false; 699 break; 700 } 701 case NPS_INFO: { 702 table_name = "nps table"; 703 check_table = false; 704 break; 705 } 706 case MEM_RESERVED_INFO: 707 /* Optional table; signature is validated in get_mem_reserved_info_table(). */ 708 table_name = "mem_reserved table"; 709 check_table = false; 710 break; 711 default: 712 dev_err(adev->dev, "invalid ip discovery table id %d specified\n", table_id); 713 check_table = false; 714 break; 715 } 716 717 if (check_table && offset && table_size) { 718 if (act_val != exp_val) { 719 dev_err(adev->dev, "invalid ip discovery %s signature\n", table_name); 720 return -EINVAL; 721 } 722 723 if (!amdgpu_discovery_verify_checksum(adev, discovery_bin + offset, 724 table_size, checksum)) { 725 dev_err(adev->dev, "invalid ip discovery %s checksum\n", table_name); 726 return -EINVAL; 727 } 728 } 729 730 return 0; 731 } 732 733 static void amdgpu_discovery_dump_mem_reserved_info_table(struct amdgpu_device *adev, 734 struct mem_reserved_info_table_v1_0 *table); 735 736 static void amdgpu_discovery_log_bad_signature(struct amdgpu_device *adev, 737 uint8_t *discovery_bin, 738 const char *fw_name) 739 { 740 dev_err(adev->dev, 741 "get invalid ip discovery binary signature: got 0x%08x, expected 0x%08x (source=%s)\n", 742 le32_to_cpu(((struct binary_header *)discovery_bin)->binary_signature), 743 BINARY_SIGNATURE, fw_name ? "file" : "mem"); 744 print_hex_dump(KERN_ERR, "ip_discovery head: ", DUMP_PREFIX_OFFSET, 745 16, 1, discovery_bin, 64, false); 746 } 747 748 static int amdgpu_discovery_init(struct amdgpu_device *adev) 749 { 750 struct binary_header *bhdr; 751 uint8_t *discovery_bin; 752 const char *fw_name; 753 uint16_t offset; 754 uint16_t size; 755 uint16_t checksum; 756 uint16_t table_id; 757 bool is_tmr_in_sysmem; 758 int r; 759 760 r = amdgpu_discovery_get_tmr_info(adev, &is_tmr_in_sysmem); 761 if (r) 762 return r; 763 764 discovery_bin = adev->discovery.bin; 765 /* Read from file if it is the preferred option */ 766 fw_name = amdgpu_discovery_get_fw_name(adev); 767 if (fw_name != NULL) { 768 drm_dbg(&adev->ddev, "use ip discovery information from file\n"); 769 r = amdgpu_discovery_read_binary_from_file(adev, discovery_bin, 770 fw_name); 771 if (r) 772 goto out; 773 } else { 774 drm_dbg(&adev->ddev, "use ip discovery information from memory\n"); 775 r = amdgpu_discovery_read_binary_from_mem(adev, discovery_bin, 776 is_tmr_in_sysmem); 777 if (r) 778 goto out; 779 } 780 781 /* check the ip discovery binary signature */ 782 if (!amdgpu_discovery_verify_binary_signature(discovery_bin)) { 783 amdgpu_discovery_log_bad_signature(adev, discovery_bin, fw_name); 784 r = -EINVAL; 785 goto out; 786 } 787 788 bhdr = (struct binary_header *)discovery_bin; 789 790 offset = offsetof(struct binary_header, binary_checksum) + 791 sizeof(bhdr->binary_checksum); 792 size = le16_to_cpu(bhdr->binary_size) - offset; 793 checksum = le16_to_cpu(bhdr->binary_checksum); 794 795 if (!amdgpu_discovery_verify_checksum(adev, discovery_bin + offset, size, 796 checksum)) { 797 dev_err(adev->dev, "invalid ip discovery binary checksum\n"); 798 r = -EINVAL; 799 goto out; 800 } 801 802 for (table_id = 0; table_id < TOTAL_TABLES; table_id++) { 803 r = amdgpu_discovery_table_check(adev, discovery_bin, table_id); 804 if (r) 805 goto out; 806 } 807 808 /* 809 * Resolve and dump the MEM_RESERVED_INFO table once at init time. 810 * The table is optional, so missing it is not a fatal error. 811 */ 812 if (!amdgpu_discovery_get_mem_reserved_info_table(adev)) 813 amdgpu_discovery_dump_mem_reserved_info_table(adev, 814 adev->discovery.mem_reserved_table); 815 816 return 0; 817 818 out: 819 kfree(adev->discovery.bin); 820 adev->discovery.bin = NULL; 821 if ((amdgpu_discovery != 2) && 822 (RREG32(mmIP_DISCOVERY_VERSION) == 4)) 823 amdgpu_ras_query_boot_status(adev, 4); 824 return r; 825 } 826 827 void amdgpu_discovery_fini(struct amdgpu_device *adev) 828 { 829 if (adev->discovery.ip_top && !adev->discovery.ip_top->standalone_mode) 830 amdgpu_discovery_sysfs_fini(adev); 831 832 kfree(adev->discovery.bin); 833 adev->discovery.bin = NULL; 834 /* Cached pointer lives inside discovery.bin; drop it to avoid UAF. */ 835 adev->discovery.mem_reserved_table = NULL; 836 } 837 838 static int amdgpu_discovery_validate_ip(struct amdgpu_device *adev, 839 uint8_t instance, uint16_t hw_id) 840 { 841 if (instance >= HWIP_MAX_INSTANCE) { 842 if (adev) 843 dev_err(adev->dev, 844 "Unexpected instance_number (%d) from ip discovery blob\n", 845 instance); 846 return -EINVAL; 847 } 848 if (hw_id >= HW_ID_MAX) { 849 if (adev) 850 dev_err(adev->dev, 851 "Unexpected hw_id (%d) from ip discovery blob\n", 852 hw_id); 853 return -EINVAL; 854 } 855 856 return 0; 857 } 858 859 static void amdgpu_discovery_read_harvest_bit_per_ip(struct amdgpu_device *adev, 860 uint32_t *vcn_harvest_count) 861 { 862 uint8_t *discovery_bin = adev->discovery.bin; 863 struct binary_header *bhdr; 864 struct ip_discovery_header *ihdr; 865 struct die_header *dhdr; 866 struct ip *ip; 867 uint16_t die_offset, ip_offset, num_dies, num_ips; 868 uint16_t hw_id; 869 uint8_t inst; 870 int i, j; 871 872 bhdr = (struct binary_header *)discovery_bin; 873 ihdr = (struct ip_discovery_header 874 *)(discovery_bin + 875 le16_to_cpu(bhdr->table_list[IP_DISCOVERY].offset)); 876 num_dies = le16_to_cpu(ihdr->num_dies); 877 878 /* scan harvest bit of all IP data structures */ 879 for (i = 0; i < num_dies; i++) { 880 die_offset = le16_to_cpu(ihdr->die_info[i].die_offset); 881 dhdr = (struct die_header *)(discovery_bin + die_offset); 882 num_ips = le16_to_cpu(dhdr->num_ips); 883 ip_offset = die_offset + sizeof(*dhdr); 884 885 for (j = 0; j < num_ips; j++) { 886 ip = (struct ip *)(discovery_bin + ip_offset); 887 inst = ip->number_instance; 888 hw_id = le16_to_cpu(ip->hw_id); 889 if (amdgpu_discovery_validate_ip(adev, inst, hw_id)) 890 goto next_ip; 891 892 if (ip->harvest == 1) { 893 switch (hw_id) { 894 case VCN_HWID: 895 (*vcn_harvest_count)++; 896 if (inst == 0) { 897 adev->vcn.harvest_config |= AMDGPU_VCN_HARVEST_VCN0; 898 adev->vcn.inst_mask &= 899 ~AMDGPU_VCN_HARVEST_VCN0; 900 adev->jpeg.inst_mask &= 901 ~AMDGPU_VCN_HARVEST_VCN0; 902 } else { 903 adev->vcn.harvest_config |= AMDGPU_VCN_HARVEST_VCN1; 904 adev->vcn.inst_mask &= 905 ~AMDGPU_VCN_HARVEST_VCN1; 906 adev->jpeg.inst_mask &= 907 ~AMDGPU_VCN_HARVEST_VCN1; 908 } 909 break; 910 case DMU_HWID: 911 adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK; 912 break; 913 default: 914 break; 915 } 916 } 917 next_ip: 918 ip_offset += struct_size(ip, base_address, 919 ip->num_base_address); 920 } 921 } 922 } 923 924 static void amdgpu_discovery_read_from_harvest_table(struct amdgpu_device *adev, 925 uint32_t *vcn_harvest_count, 926 uint32_t *umc_harvest_count) 927 { 928 uint8_t *discovery_bin = adev->discovery.bin; 929 struct table_info *info; 930 struct harvest_table *harvest_info; 931 u16 offset; 932 int i; 933 u64 umc_harvest_config = 0; 934 935 if (amdgpu_discovery_get_table_info(adev, &info, HARVEST_INFO)) 936 return; 937 offset = le16_to_cpu(info->offset); 938 939 if (!offset) { 940 dev_err(adev->dev, "invalid harvest table offset\n"); 941 return; 942 } 943 944 harvest_info = (struct harvest_table *)(discovery_bin + offset); 945 946 for (i = 0; i < 32; i++) { 947 u16 hw_id = le16_to_cpu(harvest_info->list[i].hw_id); 948 u8 inst = harvest_info->list[i].number_instance; 949 950 if (hw_id == 0) 951 break; 952 953 if (inst >= 32) { 954 dev_warn(adev->dev, 955 "bogus harvest instance %u for hw_id %u\n", 956 inst, hw_id); 957 continue; 958 } 959 960 switch (hw_id) { 961 case VCN_HWID: 962 (*vcn_harvest_count)++; 963 adev->vcn.harvest_config |= BIT(inst); 964 adev->jpeg.harvest_config |= BIT(inst); 965 966 adev->vcn.inst_mask &= ~BIT(inst); 967 adev->jpeg.inst_mask &= ~BIT(inst); 968 break; 969 case DMU_HWID: 970 adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK; 971 break; 972 case UMC_HWID: 973 umc_harvest_config |= BIT_ULL(inst); 974 (*umc_harvest_count)++; 975 break; 976 case GC_HWID: 977 adev->gfx.xcc_mask &= ~BIT(inst); 978 break; 979 case SDMA0_HWID: 980 adev->sdma.sdma_mask &= ~BIT(inst); 981 break; 982 case MMHUB_HWID: 983 adev->mmhub.inst_mask &= ~BIT(inst); 984 break; 985 case LSDMA_HWID: 986 adev->lsdma.inst_mask &= ~BIT(inst); 987 break; 988 #if defined(CONFIG_DRM_AMD_ISP) 989 case ISP_HWID: 990 adev->isp.harvest_config |= ~BIT(inst); 991 break; 992 #endif 993 default: 994 break; 995 } 996 } 997 998 adev->umc.active_mask = ((1ULL << adev->umc.node_inst_num) - 1ULL) & 999 ~umc_harvest_config; 1000 } 1001 1002 /* ================================================== */ 1003 1004 struct ip_hw_instance { 1005 struct kobject kobj; /* ip_discovery/die/#die/#hw_id/#instance/<attrs...> */ 1006 1007 int hw_id; 1008 u8 num_instance; 1009 u8 major, minor, revision; 1010 u8 harvest; 1011 1012 int num_base_addresses; 1013 u32 base_addr[] __counted_by(num_base_addresses); 1014 }; 1015 1016 struct ip_hw_id { 1017 struct kset hw_id_kset; /* ip_discovery/die/#die/#hw_id/, contains ip_hw_instance */ 1018 int hw_id; 1019 }; 1020 1021 struct ip_die_entry { 1022 struct kset ip_kset; /* ip_discovery/die/#die/, contains ip_hw_id */ 1023 u16 num_ips; 1024 }; 1025 1026 /* -------------------------------------------------- */ 1027 1028 struct ip_hw_instance_attr { 1029 struct attribute attr; 1030 ssize_t (*show)(struct ip_hw_instance *ip_hw_instance, char *buf); 1031 }; 1032 1033 static ssize_t hw_id_show(struct ip_hw_instance *ip_hw_instance, char *buf) 1034 { 1035 return sysfs_emit(buf, "%d\n", ip_hw_instance->hw_id); 1036 } 1037 1038 static ssize_t num_instance_show(struct ip_hw_instance *ip_hw_instance, char *buf) 1039 { 1040 return sysfs_emit(buf, "%d\n", ip_hw_instance->num_instance); 1041 } 1042 1043 static ssize_t major_show(struct ip_hw_instance *ip_hw_instance, char *buf) 1044 { 1045 return sysfs_emit(buf, "%d\n", ip_hw_instance->major); 1046 } 1047 1048 static ssize_t minor_show(struct ip_hw_instance *ip_hw_instance, char *buf) 1049 { 1050 return sysfs_emit(buf, "%d\n", ip_hw_instance->minor); 1051 } 1052 1053 static ssize_t revision_show(struct ip_hw_instance *ip_hw_instance, char *buf) 1054 { 1055 return sysfs_emit(buf, "%d\n", ip_hw_instance->revision); 1056 } 1057 1058 static ssize_t harvest_show(struct ip_hw_instance *ip_hw_instance, char *buf) 1059 { 1060 return sysfs_emit(buf, "0x%01X\n", ip_hw_instance->harvest); 1061 } 1062 1063 static ssize_t num_base_addresses_show(struct ip_hw_instance *ip_hw_instance, char *buf) 1064 { 1065 return sysfs_emit(buf, "%d\n", ip_hw_instance->num_base_addresses); 1066 } 1067 1068 static ssize_t base_addr_show(struct ip_hw_instance *ip_hw_instance, char *buf) 1069 { 1070 ssize_t at; 1071 int ii; 1072 1073 for (at = ii = 0; ii < ip_hw_instance->num_base_addresses; ii++) { 1074 /* Here we satisfy the condition that, at + size <= PAGE_SIZE. 1075 */ 1076 if (at + 12 > PAGE_SIZE) 1077 break; 1078 at += sysfs_emit_at(buf, at, "0x%08X\n", 1079 ip_hw_instance->base_addr[ii]); 1080 } 1081 1082 return at; 1083 } 1084 1085 static struct ip_hw_instance_attr ip_hw_attr[] = { 1086 __ATTR_RO(hw_id), 1087 __ATTR_RO(num_instance), 1088 __ATTR_RO(major), 1089 __ATTR_RO(minor), 1090 __ATTR_RO(revision), 1091 __ATTR_RO(harvest), 1092 __ATTR_RO(num_base_addresses), 1093 __ATTR_RO(base_addr), 1094 }; 1095 1096 static struct attribute *ip_hw_instance_attrs[ARRAY_SIZE(ip_hw_attr) + 1]; 1097 ATTRIBUTE_GROUPS(ip_hw_instance); 1098 1099 #define to_ip_hw_instance(x) container_of(x, struct ip_hw_instance, kobj) 1100 #define to_ip_hw_instance_attr(x) container_of(x, struct ip_hw_instance_attr, attr) 1101 1102 static ssize_t ip_hw_instance_attr_show(struct kobject *kobj, 1103 struct attribute *attr, 1104 char *buf) 1105 { 1106 struct ip_hw_instance *ip_hw_instance = to_ip_hw_instance(kobj); 1107 struct ip_hw_instance_attr *ip_hw_attr = to_ip_hw_instance_attr(attr); 1108 1109 if (!ip_hw_attr->show) 1110 return -EIO; 1111 1112 return ip_hw_attr->show(ip_hw_instance, buf); 1113 } 1114 1115 static const struct sysfs_ops ip_hw_instance_sysfs_ops = { 1116 .show = ip_hw_instance_attr_show, 1117 }; 1118 1119 static void ip_hw_instance_release(struct kobject *kobj) 1120 { 1121 struct ip_hw_instance *ip_hw_instance = to_ip_hw_instance(kobj); 1122 1123 kfree(ip_hw_instance); 1124 } 1125 1126 static const struct kobj_type ip_hw_instance_ktype = { 1127 .release = ip_hw_instance_release, 1128 .sysfs_ops = &ip_hw_instance_sysfs_ops, 1129 .default_groups = ip_hw_instance_groups, 1130 }; 1131 1132 /* -------------------------------------------------- */ 1133 1134 #define to_ip_hw_id(x) container_of(to_kset(x), struct ip_hw_id, hw_id_kset) 1135 1136 static void ip_hw_id_release(struct kobject *kobj) 1137 { 1138 struct ip_hw_id *ip_hw_id = to_ip_hw_id(kobj); 1139 1140 if (!list_empty(&ip_hw_id->hw_id_kset.list)) 1141 DRM_ERROR("ip_hw_id->hw_id_kset is not empty"); 1142 kfree(ip_hw_id); 1143 } 1144 1145 static const struct kobj_type ip_hw_id_ktype = { 1146 .release = ip_hw_id_release, 1147 .sysfs_ops = &kobj_sysfs_ops, 1148 }; 1149 1150 /* -------------------------------------------------- */ 1151 1152 static void die_kobj_release(struct kobject *kobj); 1153 static void ip_disc_release(struct kobject *kobj); 1154 1155 struct ip_die_entry_attribute { 1156 struct attribute attr; 1157 ssize_t (*show)(struct ip_die_entry *ip_die_entry, char *buf); 1158 }; 1159 1160 #define to_ip_die_entry_attr(x) container_of(x, struct ip_die_entry_attribute, attr) 1161 1162 static ssize_t num_ips_show(struct ip_die_entry *ip_die_entry, char *buf) 1163 { 1164 return sysfs_emit(buf, "%d\n", ip_die_entry->num_ips); 1165 } 1166 1167 /* If there are more ip_die_entry attrs, other than the number of IPs, 1168 * we can make this intro an array of attrs, and then initialize 1169 * ip_die_entry_attrs in a loop. 1170 */ 1171 static struct ip_die_entry_attribute num_ips_attr = 1172 __ATTR_RO(num_ips); 1173 1174 static struct attribute *ip_die_entry_attrs[] = { 1175 &num_ips_attr.attr, 1176 NULL, 1177 }; 1178 ATTRIBUTE_GROUPS(ip_die_entry); /* ip_die_entry_groups */ 1179 1180 #define to_ip_die_entry(x) container_of(to_kset(x), struct ip_die_entry, ip_kset) 1181 1182 static ssize_t ip_die_entry_attr_show(struct kobject *kobj, 1183 struct attribute *attr, 1184 char *buf) 1185 { 1186 struct ip_die_entry_attribute *ip_die_entry_attr = to_ip_die_entry_attr(attr); 1187 struct ip_die_entry *ip_die_entry = to_ip_die_entry(kobj); 1188 1189 if (!ip_die_entry_attr->show) 1190 return -EIO; 1191 1192 return ip_die_entry_attr->show(ip_die_entry, buf); 1193 } 1194 1195 static void ip_die_entry_release(struct kobject *kobj) 1196 { 1197 struct ip_die_entry *ip_die_entry = to_ip_die_entry(kobj); 1198 1199 if (!list_empty(&ip_die_entry->ip_kset.list)) 1200 DRM_ERROR("ip_die_entry->ip_kset is not empty"); 1201 kfree(ip_die_entry); 1202 } 1203 1204 static const struct sysfs_ops ip_die_entry_sysfs_ops = { 1205 .show = ip_die_entry_attr_show, 1206 }; 1207 1208 static const struct kobj_type ip_die_entry_ktype = { 1209 .release = ip_die_entry_release, 1210 .sysfs_ops = &ip_die_entry_sysfs_ops, 1211 .default_groups = ip_die_entry_groups, 1212 }; 1213 1214 static const struct kobj_type die_kobj_ktype = { 1215 .release = die_kobj_release, 1216 .sysfs_ops = &kobj_sysfs_ops, 1217 }; 1218 1219 static const struct kobj_type ip_discovery_ktype = { 1220 .release = ip_disc_release, 1221 .sysfs_ops = &kobj_sysfs_ops, 1222 }; 1223 1224 static void die_kobj_release(struct kobject *kobj) 1225 { 1226 struct ip_discovery_top *ip_top = container_of(to_kset(kobj), 1227 struct ip_discovery_top, 1228 die_kset); 1229 if (!list_empty(&ip_top->die_kset.list)) 1230 DRM_ERROR("ip_top->die_kset is not empty"); 1231 } 1232 1233 static void ip_disc_release(struct kobject *kobj) 1234 { 1235 struct ip_discovery_top *ip_top = container_of(kobj, struct ip_discovery_top, 1236 kobj); 1237 struct amdgpu_device *adev = ip_top->adev; 1238 1239 /* In standalone mode, discovery_bin is managed by devm and will be 1240 * freed automatically when the PCI device is removed. Do not manually 1241 * free it here to avoid double-free. 1242 */ 1243 1244 kfree(ip_top); 1245 if (adev) 1246 adev->discovery.ip_top = NULL; 1247 } 1248 1249 static uint8_t amdgpu_discovery_get_harvest_info(struct amdgpu_device *adev, 1250 uint16_t hw_id, uint8_t inst) 1251 { 1252 uint8_t harvest = 0; 1253 1254 /* In early init mode (adev == NULL), harvest info is not available */ 1255 if (!adev) 1256 return 0; 1257 1258 /* Until a uniform way is figured, get mask based on hwid */ 1259 switch (hw_id) { 1260 case VCN_HWID: 1261 /* VCN vs UVD+VCE */ 1262 if (!amdgpu_ip_version(adev, VCE_HWIP, 0)) 1263 harvest = ((1 << inst) & adev->vcn.inst_mask) == 0; 1264 break; 1265 case DMU_HWID: 1266 if (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK) 1267 harvest = 0x1; 1268 break; 1269 case UMC_HWID: 1270 /* TODO: It needs another parsing; for now, ignore.*/ 1271 break; 1272 case GC_HWID: 1273 harvest = ((1 << inst) & adev->gfx.xcc_mask) == 0; 1274 break; 1275 case SDMA0_HWID: 1276 harvest = ((1 << inst) & adev->sdma.sdma_mask) == 0; 1277 break; 1278 case MMHUB_HWID: 1279 harvest = (BIT(inst) & adev->mmhub.inst_mask) == 0; 1280 break; 1281 case LSDMA_HWID: 1282 harvest = (BIT(inst) & adev->lsdma.inst_mask) == 0; 1283 break; 1284 default: 1285 break; 1286 } 1287 1288 return harvest; 1289 } 1290 1291 static int amdgpu_discovery_sysfs_ips(struct amdgpu_device *adev, 1292 struct ip_discovery_top *ip_top, 1293 struct ip_die_entry *ip_die_entry, 1294 const size_t _ip_offset, const int num_ips, 1295 bool reg_base_64) 1296 { 1297 uint8_t *discovery_bin = ip_top->standalone_mode ? 1298 ip_top->discovery_bin : 1299 adev->discovery.bin; 1300 int ii, jj, kk, res; 1301 uint16_t hw_id; 1302 uint8_t inst; 1303 1304 DRM_DEBUG("num_ips:%d", num_ips); 1305 1306 /* Find all IPs of a given HW ID, and add their instance to 1307 * #die/#hw_id/#instance/<attributes> 1308 */ 1309 for (ii = 0; ii < HW_ID_MAX; ii++) { 1310 struct ip_hw_id *ip_hw_id = NULL; 1311 size_t ip_offset = _ip_offset; 1312 1313 for (jj = 0; jj < num_ips; jj++) { 1314 struct ip_v4 *ip; 1315 struct ip_hw_instance *ip_hw_instance; 1316 1317 ip = (struct ip_v4 *)(discovery_bin + ip_offset); 1318 inst = ip->instance_number; 1319 hw_id = le16_to_cpu(ip->hw_id); 1320 if (amdgpu_discovery_validate_ip(adev, inst, hw_id) || 1321 hw_id != ii) 1322 goto next_ip; 1323 1324 DRM_DEBUG("match:%d @ ip_offset:%zu", ii, ip_offset); 1325 1326 /* We have a hw_id match; register the hw 1327 * block if not yet registered. 1328 */ 1329 if (!ip_hw_id) { 1330 ip_hw_id = kzalloc_obj(*ip_hw_id); 1331 if (!ip_hw_id) 1332 return -ENOMEM; 1333 ip_hw_id->hw_id = ii; 1334 1335 kobject_set_name(&ip_hw_id->hw_id_kset.kobj, "%d", ii); 1336 ip_hw_id->hw_id_kset.kobj.kset = &ip_die_entry->ip_kset; 1337 ip_hw_id->hw_id_kset.kobj.ktype = &ip_hw_id_ktype; 1338 res = kset_register(&ip_hw_id->hw_id_kset); 1339 if (res) { 1340 DRM_ERROR("Couldn't register ip_hw_id kset"); 1341 kfree(ip_hw_id); 1342 return res; 1343 } 1344 if (hw_id_names[ii]) { 1345 res = sysfs_create_link(&ip_die_entry->ip_kset.kobj, 1346 &ip_hw_id->hw_id_kset.kobj, 1347 hw_id_names[ii]); 1348 if (res) { 1349 DRM_ERROR("Couldn't create IP link %s in IP Die:%s\n", 1350 hw_id_names[ii], 1351 kobject_name(&ip_die_entry->ip_kset.kobj)); 1352 } 1353 } 1354 } 1355 1356 /* Now register its instance. 1357 */ 1358 ip_hw_instance = kzalloc_flex(*ip_hw_instance, 1359 base_addr, 1360 ip->num_base_address); 1361 if (!ip_hw_instance) { 1362 DRM_ERROR("no memory for ip_hw_instance"); 1363 return -ENOMEM; 1364 } 1365 ip_hw_instance->hw_id = le16_to_cpu(ip->hw_id); /* == ii */ 1366 ip_hw_instance->num_instance = ip->instance_number; 1367 ip_hw_instance->major = ip->major; 1368 ip_hw_instance->minor = ip->minor; 1369 ip_hw_instance->revision = ip->revision; 1370 ip_hw_instance->harvest = 1371 amdgpu_discovery_get_harvest_info( 1372 adev, ip_hw_instance->hw_id, 1373 ip_hw_instance->num_instance); 1374 ip_hw_instance->num_base_addresses = ip->num_base_address; 1375 1376 for (kk = 0; kk < ip_hw_instance->num_base_addresses; kk++) { 1377 /* 1378 * Standalone mode uses a raw copy of the discovery 1379 * binary; decode 64-bit addresses here. The shared 1380 * bin is already collapsed to 32-bit in place. 1381 */ 1382 if (reg_base_64 && ip_top->standalone_mode) 1383 ip_hw_instance->base_addr[kk] = 1384 lower_32_bits(le64_to_cpu(ip->base_address_64[kk])) & 0x3FFFFFFF; 1385 else 1386 ip_hw_instance->base_addr[kk] = 1387 le32_to_cpu(ip->base_address[kk]); 1388 } 1389 1390 kobject_init(&ip_hw_instance->kobj, &ip_hw_instance_ktype); 1391 ip_hw_instance->kobj.kset = &ip_hw_id->hw_id_kset; 1392 res = kobject_add(&ip_hw_instance->kobj, NULL, 1393 "%d", ip_hw_instance->num_instance); 1394 if (res) 1395 kobject_put(&ip_hw_instance->kobj); 1396 next_ip: 1397 if (reg_base_64) 1398 ip_offset += struct_size(ip, base_address_64, 1399 ip->num_base_address); 1400 else 1401 ip_offset += struct_size(ip, base_address, 1402 ip->num_base_address); 1403 } 1404 } 1405 1406 return 0; 1407 } 1408 1409 static int amdgpu_discovery_sysfs_recurse(struct amdgpu_device *adev, 1410 struct ip_discovery_top *ip_top) 1411 { 1412 uint8_t *discovery_bin = ip_top->standalone_mode ? 1413 ip_top->discovery_bin : 1414 adev->discovery.bin; 1415 struct table_info *info; 1416 struct ip_discovery_header *ihdr; 1417 struct die_header *dhdr; 1418 struct kset *die_kset = &ip_top->die_kset; 1419 u16 num_dies, die_offset, num_ips; 1420 size_t ip_offset; 1421 int ii, res; 1422 1423 info = amdgpu_discovery_get_table_info_from_bin(discovery_bin, 1424 IP_DISCOVERY); 1425 if (!info) 1426 return -EINVAL; 1427 ihdr = (struct ip_discovery_header 1428 *)(discovery_bin + 1429 le16_to_cpu(info->offset)); 1430 num_dies = le16_to_cpu(ihdr->num_dies); 1431 1432 DRM_DEBUG("number of dies: %d\n", num_dies); 1433 1434 for (ii = 0; ii < num_dies; ii++) { 1435 struct ip_die_entry *ip_die_entry; 1436 1437 die_offset = le16_to_cpu(ihdr->die_info[ii].die_offset); 1438 dhdr = (struct die_header *)(discovery_bin + die_offset); 1439 num_ips = le16_to_cpu(dhdr->num_ips); 1440 ip_offset = die_offset + sizeof(*dhdr); 1441 1442 /* Add the die to the kset. 1443 * 1444 * dhdr->die_id == ii, which was checked in 1445 * amdgpu_discovery_reg_base_init(). 1446 */ 1447 1448 ip_die_entry = kzalloc_obj(*ip_die_entry); 1449 if (!ip_die_entry) 1450 return -ENOMEM; 1451 1452 ip_die_entry->num_ips = num_ips; 1453 1454 kobject_set_name(&ip_die_entry->ip_kset.kobj, "%d", le16_to_cpu(dhdr->die_id)); 1455 ip_die_entry->ip_kset.kobj.kset = die_kset; 1456 ip_die_entry->ip_kset.kobj.ktype = &ip_die_entry_ktype; 1457 res = kset_register(&ip_die_entry->ip_kset); 1458 if (res) { 1459 DRM_ERROR("Couldn't register ip_die_entry kset"); 1460 kfree(ip_die_entry); 1461 return res; 1462 } 1463 1464 amdgpu_discovery_sysfs_ips(adev, ip_top, ip_die_entry, ip_offset, 1465 num_ips, !!ihdr->base_addr_64_bit); 1466 } 1467 1468 return 0; 1469 } 1470 1471 #define list_to_kobj(el) container_of(el, struct kobject, entry) 1472 1473 static void amdgpu_discovery_sysfs_update_harvest(struct amdgpu_device *adev) 1474 { 1475 struct ip_discovery_top *ip_top = adev->discovery.ip_top; 1476 struct ip_die_entry *ip_die_entry; 1477 struct list_head *el_die, *el_hw_id, *el_hw_inst; 1478 struct ip_hw_id *hw_id; 1479 struct kset *die_kset; 1480 struct ip_hw_instance *ip_inst; 1481 1482 if (!ip_top) 1483 return; 1484 1485 die_kset = &ip_top->die_kset; 1486 1487 guard(spinlock)(&die_kset->list_lock); 1488 list_for_each(el_die, &die_kset->list) { 1489 ip_die_entry = to_ip_die_entry(list_to_kobj(el_die)); 1490 1491 list_for_each(el_hw_id, &ip_die_entry->ip_kset.list) { 1492 hw_id = to_ip_hw_id(list_to_kobj(el_hw_id)); 1493 1494 list_for_each(el_hw_inst, &hw_id->hw_id_kset.list) { 1495 ip_inst = to_ip_hw_instance(list_to_kobj(el_hw_inst)); 1496 ip_inst->harvest = 1497 amdgpu_discovery_get_harvest_info( 1498 adev, ip_inst->hw_id, 1499 ip_inst->num_instance); 1500 } 1501 } 1502 } 1503 } 1504 1505 static int amdgpu_discovery_sysfs_init(struct amdgpu_device *adev) 1506 { 1507 uint8_t *discovery_bin = adev->discovery.bin; 1508 struct ip_discovery_top *ip_top; 1509 struct kset *die_kset; 1510 int res, ii; 1511 1512 if (!discovery_bin) 1513 return -EINVAL; 1514 1515 /* If early init already created sysfs in standalone mode, update harvest info 1516 * now that adev is fully initialized, then skip the rest of normal init. 1517 */ 1518 if (adev->discovery.ip_top && adev->discovery.ip_top->standalone_mode) { 1519 amdgpu_discovery_sysfs_update_harvest(adev); 1520 return 0; 1521 } 1522 1523 ip_top = kzalloc_obj(*ip_top); 1524 if (!ip_top) 1525 return -ENOMEM; 1526 1527 ip_top->adev = adev; 1528 1529 /* Check if ip_discovery already exists before creating. 1530 * This shouldn't normally happen but handle it gracefully. 1531 */ 1532 if (adev->dev->kobj.sd) { 1533 struct kernfs_node *existing; 1534 1535 existing = kernfs_find_and_get(adev->dev->kobj.sd, "ip_discovery"); 1536 if (existing) { 1537 kernfs_put(existing); 1538 kfree(ip_top); 1539 return 0; 1540 } 1541 } 1542 1543 res = kobject_init_and_add(&ip_top->kobj, &ip_discovery_ktype, 1544 &adev->dev->kobj, "ip_discovery"); 1545 if (res) { 1546 DRM_ERROR("Couldn't init and add ip_discovery/"); 1547 goto Err; 1548 } 1549 1550 adev->discovery.ip_top = ip_top; 1551 1552 die_kset = &ip_top->die_kset; 1553 kobject_set_name(&die_kset->kobj, "%s", "die"); 1554 die_kset->kobj.parent = &ip_top->kobj; 1555 die_kset->kobj.ktype = &die_kobj_ktype; 1556 res = kset_register(&ip_top->die_kset); 1557 if (res) { 1558 DRM_ERROR("Couldn't register die_kset"); 1559 goto Err; 1560 } 1561 1562 for (ii = 0; ii < ARRAY_SIZE(ip_hw_attr); ii++) 1563 ip_hw_instance_attrs[ii] = &ip_hw_attr[ii].attr; 1564 ip_hw_instance_attrs[ii] = NULL; 1565 1566 res = amdgpu_discovery_sysfs_recurse(adev, ip_top); 1567 1568 return res; 1569 Err: 1570 kobject_put(&ip_top->kobj); 1571 return res; 1572 } 1573 1574 /* -------------------------------------------------- */ 1575 1576 static void amdgpu_discovery_sysfs_ip_hw_free(struct ip_hw_id *ip_hw_id) 1577 { 1578 struct list_head *el, *tmp; 1579 struct kset *hw_id_kset; 1580 1581 hw_id_kset = &ip_hw_id->hw_id_kset; 1582 spin_lock(&hw_id_kset->list_lock); 1583 list_for_each_prev_safe(el, tmp, &hw_id_kset->list) { 1584 list_del_init(el); 1585 spin_unlock(&hw_id_kset->list_lock); 1586 /* kobject is embedded in ip_hw_instance */ 1587 kobject_put(list_to_kobj(el)); 1588 spin_lock(&hw_id_kset->list_lock); 1589 } 1590 spin_unlock(&hw_id_kset->list_lock); 1591 kobject_put(&ip_hw_id->hw_id_kset.kobj); 1592 } 1593 1594 static void amdgpu_discovery_sysfs_die_free(struct ip_die_entry *ip_die_entry) 1595 { 1596 struct list_head *el, *tmp; 1597 struct kset *ip_kset; 1598 1599 ip_kset = &ip_die_entry->ip_kset; 1600 spin_lock(&ip_kset->list_lock); 1601 list_for_each_prev_safe(el, tmp, &ip_kset->list) { 1602 list_del_init(el); 1603 spin_unlock(&ip_kset->list_lock); 1604 amdgpu_discovery_sysfs_ip_hw_free(to_ip_hw_id(list_to_kobj(el))); 1605 spin_lock(&ip_kset->list_lock); 1606 } 1607 spin_unlock(&ip_kset->list_lock); 1608 kobject_put(&ip_die_entry->ip_kset.kobj); 1609 } 1610 1611 void amdgpu_discovery_sysfs_fini(struct amdgpu_device *adev) 1612 { 1613 struct ip_discovery_top *ip_top = adev->discovery.ip_top; 1614 struct list_head *el, *tmp; 1615 struct kset *die_kset; 1616 1617 if (!ip_top) 1618 return; 1619 1620 /* 1621 * In standalone mode the sysfs hierarchy is tied to the PCI device 1622 * lifetime and is torn down by amdgpu_discovery_sysfs_early_fini(). 1623 * Freeing it here would leave a dangling pointer in the early 1624 * discovery list, causing a use-after-free on driver unbind. 1625 */ 1626 if (ip_top->standalone_mode) 1627 return; 1628 1629 adev->discovery.ip_top = NULL; 1630 die_kset = &ip_top->die_kset; 1631 spin_lock(&die_kset->list_lock); 1632 list_for_each_prev_safe(el, tmp, &die_kset->list) { 1633 list_del_init(el); 1634 spin_unlock(&die_kset->list_lock); 1635 amdgpu_discovery_sysfs_die_free(to_ip_die_entry(list_to_kobj(el))); 1636 spin_lock(&die_kset->list_lock); 1637 } 1638 spin_unlock(&die_kset->list_lock); 1639 kobject_put(&ip_top->die_kset.kobj); 1640 kobject_put(&ip_top->kobj); 1641 } 1642 1643 /* devcoredump support */ 1644 void amdgpu_discovery_dump(struct amdgpu_device *adev, struct drm_printer *p) 1645 { 1646 struct ip_discovery_top *ip_top = adev->discovery.ip_top; 1647 struct ip_die_entry *ip_die_entry; 1648 struct list_head *el_die, *el_hw_id, *el_hw_inst; 1649 struct ip_hw_id *hw_id; 1650 struct kset *die_kset; 1651 struct ip_hw_instance *ip_inst; 1652 int i = 0, j; 1653 1654 if (!ip_top) 1655 return; 1656 1657 die_kset = &ip_top->die_kset; 1658 1659 drm_printf(p, "\nHW IP Discovery\n"); 1660 1661 spin_lock(&die_kset->list_lock); 1662 list_for_each(el_die, &die_kset->list) { 1663 drm_printf(p, "die %d\n", i++); 1664 ip_die_entry = to_ip_die_entry(list_to_kobj(el_die)); 1665 1666 list_for_each(el_hw_id, &ip_die_entry->ip_kset.list) { 1667 hw_id = to_ip_hw_id(list_to_kobj(el_hw_id)); 1668 drm_printf(p, "hw_id %d %s\n", hw_id->hw_id, hw_id_names[hw_id->hw_id]); 1669 1670 list_for_each(el_hw_inst, &hw_id->hw_id_kset.list) { 1671 ip_inst = to_ip_hw_instance(list_to_kobj(el_hw_inst)); 1672 drm_printf(p, "\tinstance %d\n", ip_inst->num_instance); 1673 drm_printf(p, "\tmajor %d\n", ip_inst->major); 1674 drm_printf(p, "\tminor %d\n", ip_inst->minor); 1675 drm_printf(p, "\trevision %d\n", ip_inst->revision); 1676 drm_printf(p, "\tharvest 0x%01X\n", ip_inst->harvest); 1677 drm_printf(p, "\tnum_base_addresses %d\n", 1678 ip_inst->num_base_addresses); 1679 for (j = 0; j < ip_inst->num_base_addresses; j++) 1680 drm_printf(p, "\tbase_addr[%d] 0x%08X\n", 1681 j, ip_inst->base_addr[j]); 1682 } 1683 } 1684 } 1685 spin_unlock(&die_kset->list_lock); 1686 } 1687 1688 int amdgpu_discovery_sysfs_early_init(struct amdgpu_device *adev, struct pci_dev *pdev) 1689 { 1690 struct ip_discovery_top *ip_top; 1691 struct early_ip_discovery *early_entry, *tmp; 1692 struct kset *die_kset; 1693 uint8_t *discovery_bin; 1694 int res, ii; 1695 1696 if (!adev || !adev->discovery.bin) 1697 return -EINVAL; 1698 1699 if (adev->discovery.ip_top) 1700 return 0; 1701 1702 mutex_lock(&early_ip_discovery_mutex); 1703 list_for_each_entry_safe(early_entry, tmp, &early_ip_discovery_list, list) { 1704 if (early_entry->pdev == pdev) { 1705 adev->discovery.ip_top = early_entry->ip_top; 1706 early_entry->ip_top->adev = adev; 1707 mutex_unlock(&early_ip_discovery_mutex); 1708 return 0; 1709 } 1710 } 1711 mutex_unlock(&early_ip_discovery_mutex); 1712 1713 discovery_bin = adev->discovery.bin; 1714 1715 early_entry = kzalloc_obj(*early_entry); 1716 if (!early_entry) 1717 return -ENOMEM; 1718 1719 ip_top = kzalloc_obj(*ip_top); 1720 if (!ip_top) { 1721 kfree(early_entry); 1722 return -ENOMEM; 1723 } 1724 1725 ip_top->discovery_bin = devm_kmemdup(&pdev->dev, discovery_bin, 1726 DISCOVERY_TMR_SIZE, GFP_KERNEL); 1727 if (!ip_top->discovery_bin) { 1728 kfree(ip_top); 1729 kfree(early_entry); 1730 return -ENOMEM; 1731 } 1732 1733 ip_top->bin_size = DISCOVERY_TMR_SIZE; 1734 ip_top->pdev = pdev; 1735 ip_top->adev = adev; 1736 ip_top->standalone_mode = true; 1737 1738 /* Check if ip_discovery already exists (from previous probe attempt). 1739 * This can happen if the module was unloaded and reloaded but the 1740 * sysfs persisted (tied to PCI device lifetime). 1741 */ 1742 if (pdev->dev.kobj.sd) { 1743 struct kernfs_node *existing; 1744 1745 existing = kernfs_find_and_get(pdev->dev.kobj.sd, "ip_discovery"); 1746 if (existing) { 1747 kernfs_put(existing); 1748 kfree(ip_top); 1749 kfree(early_entry); 1750 return 0; 1751 } 1752 } 1753 1754 res = kobject_init_and_add(&ip_top->kobj, &ip_discovery_ktype, 1755 &pdev->dev.kobj, "ip_discovery"); 1756 if (res) 1757 goto err_put_kobj; 1758 1759 adev->discovery.ip_top = ip_top; 1760 1761 die_kset = &ip_top->die_kset; 1762 kobject_set_name(&die_kset->kobj, "%s", "die"); 1763 die_kset->kobj.parent = &ip_top->kobj; 1764 die_kset->kobj.ktype = &die_kobj_ktype; 1765 res = kset_register(&ip_top->die_kset); 1766 if (res) 1767 goto err_put_die_kset; 1768 1769 for (ii = 0; ii < ARRAY_SIZE(ip_hw_attr); ii++) 1770 ip_hw_instance_attrs[ii] = &ip_hw_attr[ii].attr; 1771 ip_hw_instance_attrs[ii] = NULL; 1772 1773 res = amdgpu_discovery_sysfs_recurse(NULL, ip_top); 1774 if (res) 1775 goto err_put_die_kset; 1776 1777 early_entry->pdev = pdev; 1778 early_entry->ip_top = ip_top; 1779 mutex_lock(&early_ip_discovery_mutex); 1780 list_add(&early_entry->list, &early_ip_discovery_list); 1781 mutex_unlock(&early_ip_discovery_mutex); 1782 1783 return 0; 1784 1785 err_put_die_kset: 1786 kobject_put(&ip_top->die_kset.kobj); 1787 err_put_kobj: 1788 kobject_put(&ip_top->kobj); 1789 kfree(early_entry); 1790 adev->discovery.ip_top = NULL; 1791 return res; 1792 } 1793 1794 void amdgpu_discovery_sysfs_early_fini(struct pci_dev *pdev) 1795 { 1796 struct early_ip_discovery *entry, *tmp_entry; 1797 struct ip_discovery_top *ip_top = NULL; 1798 struct list_head *el, *tmp; 1799 struct kset *die_kset; 1800 1801 /* Find the entry in our tracking list */ 1802 mutex_lock(&early_ip_discovery_mutex); 1803 list_for_each_entry_safe(entry, tmp_entry, &early_ip_discovery_list, list) { 1804 if (entry->pdev == pdev) { 1805 ip_top = entry->ip_top; 1806 list_del(&entry->list); 1807 kfree(entry); 1808 break; 1809 } 1810 } 1811 mutex_unlock(&early_ip_discovery_mutex); 1812 1813 if (!ip_top) 1814 return; 1815 1816 /* Clean up sysfs hierarchy */ 1817 die_kset = &ip_top->die_kset; 1818 1819 spin_lock(&die_kset->list_lock); 1820 list_for_each_prev_safe(el, tmp, &die_kset->list) { 1821 list_del_init(el); 1822 spin_unlock(&die_kset->list_lock); 1823 amdgpu_discovery_sysfs_die_free(to_ip_die_entry(list_to_kobj(el))); 1824 spin_lock(&die_kset->list_lock); 1825 } 1826 spin_unlock(&die_kset->list_lock); 1827 1828 kobject_put(&ip_top->die_kset.kobj); 1829 kobject_put(&ip_top->kobj); 1830 /* ip_top itself will be freed by kobject_put via ip_disc_release */ 1831 } 1832 1833 /* ================================================== */ 1834 1835 static int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev) 1836 { 1837 uint8_t num_base_address, subrev, variant; 1838 struct table_info *info; 1839 struct ip_discovery_header *ihdr; 1840 struct die_header *dhdr; 1841 uint8_t *discovery_bin; 1842 struct ip_v4 *ip; 1843 uint16_t die_offset; 1844 uint16_t ip_offset; 1845 uint16_t num_dies; 1846 uint32_t wafl_ver; 1847 uint16_t num_ips; 1848 uint16_t hw_id; 1849 uint8_t inst; 1850 int hw_ip; 1851 int i, j, k; 1852 int r; 1853 1854 r = amdgpu_discovery_init(adev); 1855 if (r) 1856 return r; 1857 1858 amdgpu_discovery_sysfs_early_init(adev, adev->pdev); 1859 1860 discovery_bin = adev->discovery.bin; 1861 wafl_ver = 0; 1862 adev->gfx.xcc_mask = 0; 1863 adev->sdma.sdma_mask = 0; 1864 adev->vcn.inst_mask = 0; 1865 adev->jpeg.inst_mask = 0; 1866 adev->mmhub.inst_mask = 0; 1867 adev->lsdma.inst_mask = 0; 1868 r = amdgpu_discovery_get_table_info(adev, &info, IP_DISCOVERY); 1869 if (r) 1870 return r; 1871 ihdr = (struct ip_discovery_header 1872 *)(discovery_bin + 1873 le16_to_cpu(info->offset)); 1874 num_dies = le16_to_cpu(ihdr->num_dies); 1875 1876 DRM_DEBUG("number of dies: %d\n", num_dies); 1877 1878 for (i = 0; i < num_dies; i++) { 1879 die_offset = le16_to_cpu(ihdr->die_info[i].die_offset); 1880 dhdr = (struct die_header *)(discovery_bin + die_offset); 1881 num_ips = le16_to_cpu(dhdr->num_ips); 1882 ip_offset = die_offset + sizeof(*dhdr); 1883 1884 if (le16_to_cpu(dhdr->die_id) != i) { 1885 DRM_ERROR("invalid die id %d, expected %d\n", 1886 le16_to_cpu(dhdr->die_id), i); 1887 return -EINVAL; 1888 } 1889 1890 DRM_DEBUG("number of hardware IPs on die%d: %d\n", 1891 le16_to_cpu(dhdr->die_id), num_ips); 1892 1893 for (j = 0; j < num_ips; j++) { 1894 ip = (struct ip_v4 *)(discovery_bin + ip_offset); 1895 1896 inst = ip->instance_number; 1897 hw_id = le16_to_cpu(ip->hw_id); 1898 if (amdgpu_discovery_validate_ip(adev, inst, hw_id)) 1899 goto next_ip; 1900 1901 num_base_address = ip->num_base_address; 1902 1903 DRM_DEBUG("%s(%d) #%d v%d.%d.%d:\n", 1904 hw_id_names[le16_to_cpu(ip->hw_id)], 1905 le16_to_cpu(ip->hw_id), 1906 ip->instance_number, 1907 ip->major, ip->minor, 1908 ip->revision); 1909 1910 if (le16_to_cpu(ip->hw_id) == VCN_HWID) { 1911 /* Bit [5:0]: original revision value 1912 * Bit [7:6]: en/decode capability: 1913 * 0b00 : VCN function normally 1914 * 0b10 : encode is disabled 1915 * 0b01 : decode is disabled 1916 */ 1917 if (adev->vcn.num_vcn_inst < 1918 AMDGPU_MAX_VCN_INSTANCES) { 1919 adev->vcn.inst[adev->vcn.num_vcn_inst].vcn_config = 1920 ip->revision & 0xc0; 1921 adev->vcn.num_vcn_inst++; 1922 adev->vcn.inst_mask |= 1923 (1U << ip->instance_number); 1924 adev->jpeg.inst_mask |= 1925 (1U << ip->instance_number); 1926 } else { 1927 dev_err(adev->dev, "Too many VCN instances: %d vs %d\n", 1928 adev->vcn.num_vcn_inst + 1, 1929 AMDGPU_MAX_VCN_INSTANCES); 1930 } 1931 ip->revision &= ~0xc0; 1932 } 1933 1934 if (le16_to_cpu(ip->hw_id) == MMHUB_HWID) { 1935 if (inst < AMDGPU_MAX_MMHUB_INSTANCES) 1936 adev->mmhub.inst_mask |= BIT(inst); 1937 else 1938 dev_err(adev->dev, "Too many MMHUB instances: %d vs %d\n", 1939 inst + 1, 1940 AMDGPU_MAX_MMHUB_INSTANCES); 1941 } 1942 1943 if (le16_to_cpu(ip->hw_id) == LSDMA_HWID) { 1944 if (inst < AMDGPU_MAX_LSDMA_INSTANCES) 1945 adev->lsdma.inst_mask |= BIT(inst); 1946 else 1947 dev_err(adev->dev, "Too many LSDMA instances: %d vs %d\n", 1948 inst + 1, 1949 AMDGPU_MAX_LSDMA_INSTANCES); 1950 } 1951 1952 if (le16_to_cpu(ip->hw_id) == SDMA0_HWID || 1953 le16_to_cpu(ip->hw_id) == SDMA1_HWID || 1954 le16_to_cpu(ip->hw_id) == SDMA2_HWID || 1955 le16_to_cpu(ip->hw_id) == SDMA3_HWID) { 1956 if (adev->sdma.num_instances < 1957 AMDGPU_MAX_SDMA_INSTANCES) { 1958 adev->sdma.num_instances++; 1959 adev->sdma.sdma_mask |= 1960 (1U << ip->instance_number); 1961 } else { 1962 dev_err(adev->dev, "Too many SDMA instances: %d vs %d\n", 1963 adev->sdma.num_instances + 1, 1964 AMDGPU_MAX_SDMA_INSTANCES); 1965 } 1966 } 1967 1968 if (le16_to_cpu(ip->hw_id) == VPE_HWID) { 1969 if (adev->vpe.num_instances < AMDGPU_MAX_VPE_INSTANCES) 1970 adev->vpe.num_instances++; 1971 else 1972 dev_err(adev->dev, "Too many VPE instances: %d vs %d\n", 1973 adev->vpe.num_instances + 1, 1974 AMDGPU_MAX_VPE_INSTANCES); 1975 } 1976 1977 if (le16_to_cpu(ip->hw_id) == UMC_HWID) { 1978 adev->gmc.num_umc++; 1979 adev->umc.node_inst_num++; 1980 } 1981 1982 if (le16_to_cpu(ip->hw_id) == GC_HWID) 1983 adev->gfx.xcc_mask |= 1984 (1U << ip->instance_number); 1985 1986 if (!wafl_ver && le16_to_cpu(ip->hw_id) == WAFLC_HWID) 1987 wafl_ver = IP_VERSION_FULL(ip->major, ip->minor, 1988 ip->revision, 0, 0); 1989 1990 for (k = 0; k < num_base_address; k++) { 1991 /* 1992 * convert the endianness of base addresses in place, 1993 * so that we don't need to convert them when accessing adev->reg_offset. 1994 */ 1995 if (ihdr->base_addr_64_bit) 1996 /* Truncate the 64bit base address from ip discovery 1997 * and only store lower 32bit ip base in reg_offset[]. 1998 * Bits > 32 follows ASIC specific format, thus just 1999 * discard them and handle it within specific ASIC. 2000 * By this way reg_offset[] and related helpers can 2001 * stay unchanged. 2002 * The base address is in dwords, thus clear the 2003 * highest 2 bits to store. 2004 */ 2005 ip->base_address[k] = 2006 lower_32_bits(le64_to_cpu(ip->base_address_64[k])) & 0x3FFFFFFF; 2007 else 2008 ip->base_address[k] = le32_to_cpu(ip->base_address[k]); 2009 DRM_DEBUG("\t0x%08x\n", ip->base_address[k]); 2010 } 2011 2012 for (hw_ip = 0; hw_ip < MAX_HWIP; hw_ip++) { 2013 if (hw_id_map[hw_ip] == le16_to_cpu(ip->hw_id) && 2014 hw_id_map[hw_ip] != 0) { 2015 DRM_DEBUG("set register base offset for %s\n", 2016 hw_id_names[le16_to_cpu(ip->hw_id)]); 2017 adev->reg_offset[hw_ip][ip->instance_number] = 2018 ip->base_address; 2019 /* Instance support is somewhat inconsistent. 2020 * SDMA is a good example. Sienna cichlid has 4 total 2021 * SDMA instances, each enumerated separately (HWIDs 2022 * 42, 43, 68, 69). Arcturus has 8 total SDMA instances, 2023 * but they are enumerated as multiple instances of the 2024 * same HWIDs (4x HWID 42, 4x HWID 43). UMC is another 2025 * example. On most chips there are multiple instances 2026 * with the same HWID. 2027 */ 2028 2029 if (ihdr->version < 3) { 2030 subrev = 0; 2031 variant = 0; 2032 } else { 2033 subrev = ip->sub_revision; 2034 variant = ip->variant; 2035 } 2036 2037 adev->ip_versions[hw_ip] 2038 [ip->instance_number] = 2039 IP_VERSION_FULL(ip->major, 2040 ip->minor, 2041 ip->revision, 2042 variant, 2043 subrev); 2044 } 2045 } 2046 2047 next_ip: 2048 if (ihdr->base_addr_64_bit) 2049 ip_offset += struct_size(ip, base_address_64, ip->num_base_address); 2050 else 2051 ip_offset += struct_size(ip, base_address, ip->num_base_address); 2052 } 2053 } 2054 2055 if (wafl_ver && !adev->ip_versions[XGMI_HWIP][0]) 2056 adev->ip_versions[XGMI_HWIP][0] = wafl_ver; 2057 2058 return 0; 2059 } 2060 2061 static void amdgpu_discovery_harvest_ip(struct amdgpu_device *adev) 2062 { 2063 uint8_t *discovery_bin = adev->discovery.bin; 2064 struct ip_discovery_header *ihdr; 2065 struct table_info *info; 2066 int vcn_harvest_count = 0; 2067 int umc_harvest_count = 0; 2068 uint16_t ihdr_ver; 2069 2070 if (amdgpu_discovery_get_table_info(adev, &info, IP_DISCOVERY)) 2071 return; 2072 ihdr = (struct ip_discovery_header *)(discovery_bin + 2073 le16_to_cpu(info->offset)); 2074 ihdr_ver = le16_to_cpu(ihdr->version); 2075 /* 2076 * Harvest table does not fit Navi1x and legacy GPUs, 2077 * so read harvest bit per IP data structure to set 2078 * harvest configuration. 2079 */ 2080 if (amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(10, 2, 0) && 2081 ihdr_ver <= 2) { 2082 if ((adev->pdev->device == 0x731E && 2083 (adev->pdev->revision == 0xC6 || 2084 adev->pdev->revision == 0xC7)) || 2085 (adev->pdev->device == 0x7340 && 2086 adev->pdev->revision == 0xC9) || 2087 (adev->pdev->device == 0x7360 && 2088 adev->pdev->revision == 0xC7)) 2089 amdgpu_discovery_read_harvest_bit_per_ip(adev, 2090 &vcn_harvest_count); 2091 } else { 2092 amdgpu_discovery_read_from_harvest_table(adev, 2093 &vcn_harvest_count, 2094 &umc_harvest_count); 2095 } 2096 2097 amdgpu_discovery_harvest_config_quirk(adev); 2098 2099 if (vcn_harvest_count == adev->vcn.num_vcn_inst) { 2100 adev->harvest_ip_mask |= AMD_HARVEST_IP_VCN_MASK; 2101 adev->harvest_ip_mask |= AMD_HARVEST_IP_JPEG_MASK; 2102 } 2103 2104 if (umc_harvest_count < adev->gmc.num_umc) { 2105 adev->gmc.num_umc -= umc_harvest_count; 2106 } 2107 } 2108 2109 union gc_info { 2110 struct gc_info_v1_0 v1; 2111 struct gc_info_v1_1 v1_1; 2112 struct gc_info_v1_2 v1_2; 2113 struct gc_info_v1_3 v1_3; 2114 struct gc_info_v1_4 v1_4; 2115 struct gc_info_v1_5 v1_5; 2116 struct gc_info_v2_0 v2; 2117 struct gc_info_v2_1 v2_1; 2118 }; 2119 2120 static int amdgpu_discovery_get_gfx_info(struct amdgpu_device *adev) 2121 { 2122 uint8_t *discovery_bin = adev->discovery.bin; 2123 struct table_info *info; 2124 union gc_info *gc_info; 2125 uint32_t num_wgps_per_sa, num_wgps_per_sa1; 2126 u16 offset; 2127 2128 if (!discovery_bin) { 2129 DRM_ERROR("ip discovery uninitialized\n"); 2130 return -EINVAL; 2131 } 2132 2133 if (amdgpu_discovery_get_table_info(adev, &info, GC)) 2134 return -EINVAL; 2135 offset = le16_to_cpu(info->offset); 2136 2137 if (!offset) 2138 return 0; 2139 2140 gc_info = (union gc_info *)(discovery_bin + offset); 2141 2142 switch (le16_to_cpu(gc_info->v1.header.version_major)) { 2143 case 1: 2144 adev->gfx.config.max_shader_engines = le32_to_cpu(gc_info->v1.gc_num_se); 2145 adev->gfx.config.max_cu_per_sh = 2 * (le32_to_cpu(gc_info->v1.gc_num_wgp0_per_sa) + 2146 le32_to_cpu(gc_info->v1.gc_num_wgp1_per_sa)); 2147 adev->gfx.config.max_sh_per_se = le32_to_cpu(gc_info->v1.gc_num_sa_per_se); 2148 adev->gfx.config.max_backends_per_se = le32_to_cpu(gc_info->v1.gc_num_rb_per_se); 2149 adev->gfx.config.max_texture_channel_caches = le32_to_cpu(gc_info->v1.gc_num_gl2c); 2150 adev->gfx.config.max_gprs = le32_to_cpu(gc_info->v1.gc_num_gprs); 2151 adev->gfx.config.max_gs_threads = le32_to_cpu(gc_info->v1.gc_num_max_gs_thds); 2152 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gc_info->v1.gc_gs_table_depth); 2153 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gc_info->v1.gc_gsprim_buff_depth); 2154 adev->gfx.config.double_offchip_lds_buf = le32_to_cpu(gc_info->v1.gc_double_offchip_lds_buffer); 2155 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gc_info->v1.gc_wave_size); 2156 adev->gfx.cu_info.max_waves_per_simd = le32_to_cpu(gc_info->v1.gc_max_waves_per_simd); 2157 adev->gfx.cu_info.max_scratch_slots_per_cu = le32_to_cpu(gc_info->v1.gc_max_scratch_slots_per_cu); 2158 adev->gfx.cu_info.lds_size = le32_to_cpu(gc_info->v1.gc_lds_size); 2159 adev->gfx.config.num_sc_per_sh = le32_to_cpu(gc_info->v1.gc_num_sc_per_se) / 2160 le32_to_cpu(gc_info->v1.gc_num_sa_per_se); 2161 adev->gfx.config.num_packer_per_sc = le32_to_cpu(gc_info->v1.gc_num_packer_per_sc); 2162 if (le16_to_cpu(gc_info->v1.header.version_minor) >= 1) { 2163 adev->gfx.config.gc_num_tcp_per_sa = le32_to_cpu(gc_info->v1_1.gc_num_tcp_per_sa); 2164 adev->gfx.config.gc_num_sdp_interface = le32_to_cpu(gc_info->v1_1.gc_num_sdp_interface); 2165 adev->gfx.config.gc_num_tcps = le32_to_cpu(gc_info->v1_1.gc_num_tcps); 2166 } 2167 if (le16_to_cpu(gc_info->v1.header.version_minor) >= 2) { 2168 adev->gfx.config.gc_num_tcp_per_wpg = le32_to_cpu(gc_info->v1_2.gc_num_tcp_per_wpg); 2169 adev->gfx.config.gc_tcp_l1_size = le32_to_cpu(gc_info->v1_2.gc_tcp_l1_size); 2170 adev->gfx.config.gc_num_sqc_per_wgp = le32_to_cpu(gc_info->v1_2.gc_num_sqc_per_wgp); 2171 adev->gfx.config.gc_l1_instruction_cache_size_per_sqc = le32_to_cpu(gc_info->v1_2.gc_l1_instruction_cache_size_per_sqc); 2172 adev->gfx.config.gc_l1_data_cache_size_per_sqc = le32_to_cpu(gc_info->v1_2.gc_l1_data_cache_size_per_sqc); 2173 adev->gfx.config.gc_gl1c_per_sa = le32_to_cpu(gc_info->v1_2.gc_gl1c_per_sa); 2174 adev->gfx.config.gc_gl1c_size_per_instance = le32_to_cpu(gc_info->v1_2.gc_gl1c_size_per_instance); 2175 adev->gfx.config.gc_gl2c_per_gpu = le32_to_cpu(gc_info->v1_2.gc_gl2c_per_gpu); 2176 } 2177 if (le16_to_cpu(gc_info->v1.header.version_minor) == 4) { 2178 num_wgps_per_sa = le32_to_cpu(gc_info->v1_4.gc_num_rwgp0_per_sa) + 2179 le32_to_cpu(gc_info->v1_4.gc_num_rwgp1_per_sa) + 2180 le32_to_cpu(gc_info->v1_4.gc_num_twgp0_per_sa) + 2181 le32_to_cpu(gc_info->v1_4.gc_num_twgp1_per_sa); 2182 num_wgps_per_sa1 = le32_to_cpu(gc_info->v1_4.gc_num_rwgp0_per_sa1) + 2183 le32_to_cpu(gc_info->v1_4.gc_num_rwgp1_per_sa1) + 2184 le32_to_cpu(gc_info->v1_4.gc_num_twgp0_per_sa1) + 2185 le32_to_cpu(gc_info->v1_4.gc_num_twgp1_per_sa1); 2186 adev->gfx.config.max_cu_per_sh = 2 * (num_wgps_per_sa >= num_wgps_per_sa1 ? 2187 num_wgps_per_sa : num_wgps_per_sa1); 2188 } else if (le16_to_cpu(gc_info->v1.header.version_minor) >= 3) { 2189 adev->gfx.config.gc_tcp_size_per_cu = le32_to_cpu(gc_info->v1_3.gc_tcp_size_per_cu); 2190 adev->gfx.config.gc_tcp_cache_line_size = le32_to_cpu(gc_info->v1_3.gc_tcp_cache_line_size); 2191 adev->gfx.config.gc_instruction_cache_size_per_sqc = le32_to_cpu(gc_info->v1_3.gc_instruction_cache_size_per_sqc); 2192 adev->gfx.config.gc_instruction_cache_line_size = le32_to_cpu(gc_info->v1_3.gc_instruction_cache_line_size); 2193 adev->gfx.config.gc_scalar_data_cache_size_per_sqc = le32_to_cpu(gc_info->v1_3.gc_scalar_data_cache_size_per_sqc); 2194 adev->gfx.config.gc_scalar_data_cache_line_size = le32_to_cpu(gc_info->v1_3.gc_scalar_data_cache_line_size); 2195 adev->gfx.config.gc_tcc_size = le32_to_cpu(gc_info->v1_3.gc_tcc_size); 2196 adev->gfx.config.gc_tcc_cache_line_size = le32_to_cpu(gc_info->v1_3.gc_tcc_cache_line_size); 2197 } 2198 if (le16_to_cpu(gc_info->v1.header.version_minor) == 5) { 2199 adev->gfx.config.gc_max_num_residency_ways = le32_to_cpu(gc_info->v1_5.gc_max_num_residency_ways); 2200 adev->gfx.config.gc_cache_ways_size_in_bytes = le32_to_cpu(gc_info->v1_5.gc_cache_ways_size_in_bytes); 2201 adev->gfx.config.gc_reserved = le32_to_cpu(gc_info->v1_5.gc_reserved); 2202 2203 num_wgps_per_sa = le32_to_cpu(gc_info->v1.gc_num_wgp0_per_sa) + 2204 le32_to_cpu(gc_info->v1.gc_num_wgp1_per_sa); 2205 num_wgps_per_sa1 = le32_to_cpu(gc_info->v1_5.gc_num_wgp0_per_sa1) + 2206 le32_to_cpu(gc_info->v1_5.gc_num_wgp1_per_sa1); 2207 adev->gfx.config.max_cu_per_sh = num_wgps_per_sa >= num_wgps_per_sa1 ? 2208 num_wgps_per_sa : num_wgps_per_sa1; 2209 } 2210 break; 2211 case 2: 2212 adev->gfx.config.max_shader_engines = le32_to_cpu(gc_info->v2.gc_num_se); 2213 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gc_info->v2.gc_num_cu_per_sh); 2214 adev->gfx.config.max_sh_per_se = le32_to_cpu(gc_info->v2.gc_num_sh_per_se); 2215 adev->gfx.config.max_backends_per_se = le32_to_cpu(gc_info->v2.gc_num_rb_per_se); 2216 adev->gfx.config.max_texture_channel_caches = le32_to_cpu(gc_info->v2.gc_num_tccs); 2217 adev->gfx.config.max_gprs = le32_to_cpu(gc_info->v2.gc_num_gprs); 2218 adev->gfx.config.max_gs_threads = le32_to_cpu(gc_info->v2.gc_num_max_gs_thds); 2219 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gc_info->v2.gc_gs_table_depth); 2220 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gc_info->v2.gc_gsprim_buff_depth); 2221 adev->gfx.config.double_offchip_lds_buf = le32_to_cpu(gc_info->v2.gc_double_offchip_lds_buffer); 2222 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gc_info->v2.gc_wave_size); 2223 adev->gfx.cu_info.max_waves_per_simd = le32_to_cpu(gc_info->v2.gc_max_waves_per_simd); 2224 adev->gfx.cu_info.max_scratch_slots_per_cu = le32_to_cpu(gc_info->v2.gc_max_scratch_slots_per_cu); 2225 adev->gfx.cu_info.lds_size = le32_to_cpu(gc_info->v2.gc_lds_size); 2226 adev->gfx.config.num_sc_per_sh = le32_to_cpu(gc_info->v2.gc_num_sc_per_se) / 2227 le32_to_cpu(gc_info->v2.gc_num_sh_per_se); 2228 adev->gfx.config.num_packer_per_sc = le32_to_cpu(gc_info->v2.gc_num_packer_per_sc); 2229 if (le16_to_cpu(gc_info->v2.header.version_minor) == 1) { 2230 adev->gfx.config.gc_num_tcp_per_sa = le32_to_cpu(gc_info->v2_1.gc_num_tcp_per_sh); 2231 adev->gfx.config.gc_tcp_size_per_cu = le32_to_cpu(gc_info->v2_1.gc_tcp_size_per_cu); 2232 adev->gfx.config.gc_num_sdp_interface = le32_to_cpu(gc_info->v2_1.gc_num_sdp_interface); /* per XCD */ 2233 adev->gfx.config.gc_num_cu_per_sqc = le32_to_cpu(gc_info->v2_1.gc_num_cu_per_sqc); 2234 adev->gfx.config.gc_l1_instruction_cache_size_per_sqc = le32_to_cpu(gc_info->v2_1.gc_instruction_cache_size_per_sqc); 2235 adev->gfx.config.gc_l1_data_cache_size_per_sqc = le32_to_cpu(gc_info->v2_1.gc_scalar_data_cache_size_per_sqc); 2236 adev->gfx.config.gc_tcc_size = le32_to_cpu(gc_info->v2_1.gc_tcc_size); /* per XCD */ 2237 } 2238 break; 2239 default: 2240 dev_err(adev->dev, 2241 "Unhandled GC info table %d.%d\n", 2242 le16_to_cpu(gc_info->v1.header.version_major), 2243 le16_to_cpu(gc_info->v1.header.version_minor)); 2244 return -EINVAL; 2245 } 2246 return 0; 2247 } 2248 2249 union mall_info { 2250 struct mall_info_v1_0 v1; 2251 struct mall_info_v2_0 v2; 2252 }; 2253 2254 static int amdgpu_discovery_get_mall_info(struct amdgpu_device *adev) 2255 { 2256 uint8_t *discovery_bin = adev->discovery.bin; 2257 struct table_info *info; 2258 union mall_info *mall_info; 2259 u32 u, mall_size_per_umc, m_s_present, half_use; 2260 u64 mall_size; 2261 u16 offset, size; 2262 2263 if (!discovery_bin) { 2264 DRM_ERROR("ip discovery uninitialized\n"); 2265 return -EINVAL; 2266 } 2267 2268 if (amdgpu_discovery_get_table_info(adev, &info, MALL_INFO)) 2269 return -EINVAL; 2270 offset = le16_to_cpu(info->offset); 2271 size = le16_to_cpu(info->size); 2272 2273 if (!offset || !size) 2274 return 0; 2275 2276 mall_info = (union mall_info *)(discovery_bin + offset); 2277 2278 switch (le16_to_cpu(mall_info->v1.header.version_major)) { 2279 case 1: 2280 mall_size = 0; 2281 mall_size_per_umc = le32_to_cpu(mall_info->v1.mall_size_per_m); 2282 m_s_present = le32_to_cpu(mall_info->v1.m_s_present); 2283 half_use = le32_to_cpu(mall_info->v1.m_half_use); 2284 for (u = 0; u < adev->gmc.num_umc; u++) { 2285 if (m_s_present & (1 << u)) 2286 mall_size += mall_size_per_umc * 2; 2287 else if (half_use & (1 << u)) 2288 mall_size += mall_size_per_umc / 2; 2289 else 2290 mall_size += mall_size_per_umc; 2291 } 2292 adev->gmc.mall_size = mall_size; 2293 adev->gmc.m_half_use = half_use; 2294 break; 2295 case 2: 2296 mall_size_per_umc = le32_to_cpu(mall_info->v2.mall_size_per_umc); 2297 adev->gmc.mall_size = (uint64_t)mall_size_per_umc * adev->gmc.num_umc; 2298 break; 2299 default: 2300 dev_err(adev->dev, 2301 "Unhandled MALL info table %d.%d\n", 2302 le16_to_cpu(mall_info->v1.header.version_major), 2303 le16_to_cpu(mall_info->v1.header.version_minor)); 2304 return -EINVAL; 2305 } 2306 return 0; 2307 } 2308 2309 union vcn_info { 2310 struct vcn_info_v1_0 v1; 2311 }; 2312 2313 static int amdgpu_discovery_get_vcn_info(struct amdgpu_device *adev) 2314 { 2315 uint8_t *discovery_bin = adev->discovery.bin; 2316 struct table_info *info; 2317 union vcn_info *vcn_info; 2318 u16 offset, size; 2319 int v; 2320 2321 if (!discovery_bin) { 2322 DRM_ERROR("ip discovery uninitialized\n"); 2323 return -EINVAL; 2324 } 2325 2326 /* num_vcn_inst is currently limited to AMDGPU_MAX_VCN_INSTANCES 2327 * which is smaller than VCN_INFO_TABLE_MAX_NUM_INSTANCES 2328 * but that may change in the future with new GPUs so keep this 2329 * check for defensive purposes. 2330 */ 2331 if (adev->vcn.num_vcn_inst > VCN_INFO_TABLE_MAX_NUM_INSTANCES) { 2332 dev_err(adev->dev, "invalid vcn instances\n"); 2333 return -EINVAL; 2334 } 2335 2336 if (amdgpu_discovery_get_table_info(adev, &info, VCN_INFO)) 2337 return -EINVAL; 2338 offset = le16_to_cpu(info->offset); 2339 size = le16_to_cpu(info->size); 2340 2341 if (!offset || !size) 2342 return 0; 2343 2344 vcn_info = (union vcn_info *)(discovery_bin + offset); 2345 2346 switch (le16_to_cpu(vcn_info->v1.header.version_major)) { 2347 case 1: 2348 /* num_vcn_inst is currently limited to AMDGPU_MAX_VCN_INSTANCES 2349 * so this won't overflow. 2350 */ 2351 for (v = 0; v < adev->vcn.num_vcn_inst; v++) { 2352 adev->vcn.inst[v].vcn_codec_disable_mask = 2353 le32_to_cpu(vcn_info->v1.instance_info[v].fuse_data.all_bits); 2354 } 2355 break; 2356 default: 2357 dev_err(adev->dev, 2358 "Unhandled VCN info table %d.%d\n", 2359 le16_to_cpu(vcn_info->v1.header.version_major), 2360 le16_to_cpu(vcn_info->v1.header.version_minor)); 2361 return -EINVAL; 2362 } 2363 return 0; 2364 } 2365 2366 union nps_info { 2367 struct nps_info_v1_0 v1; 2368 }; 2369 2370 static int amdgpu_discovery_refresh_nps_info(struct amdgpu_device *adev, 2371 union nps_info *nps_data) 2372 { 2373 uint64_t vram_size, pos, offset; 2374 struct nps_info_header *nhdr; 2375 struct binary_header bhdr; 2376 struct binary_header_v2 bhdrv2; 2377 uint16_t checksum; 2378 2379 vram_size = (uint64_t)amdgpu_discovery_get_config_memsize(adev) << 20; 2380 pos = vram_size - DISCOVERY_TMR_OFFSET; 2381 amdgpu_device_vram_access(adev, pos, &bhdr, sizeof(bhdr), false); 2382 2383 switch (bhdr.version_major) { 2384 case 2: 2385 amdgpu_device_vram_access(adev, pos, &bhdrv2, sizeof(bhdrv2), false); 2386 offset = le16_to_cpu(bhdrv2.table_list[NPS_INFO].offset); 2387 checksum = le16_to_cpu(bhdrv2.table_list[NPS_INFO].checksum); 2388 break; 2389 case 1: 2390 offset = le16_to_cpu(bhdr.table_list[NPS_INFO].offset); 2391 checksum = le16_to_cpu(bhdr.table_list[NPS_INFO].checksum); 2392 break; 2393 default: 2394 return -EINVAL; 2395 } 2396 2397 amdgpu_device_vram_access(adev, (pos + offset), nps_data, 2398 sizeof(*nps_data), false); 2399 2400 nhdr = (struct nps_info_header *)(nps_data); 2401 if (!amdgpu_discovery_verify_checksum(adev, (uint8_t *)nps_data, 2402 le32_to_cpu(nhdr->size_bytes), 2403 checksum)) { 2404 dev_err(adev->dev, "nps data refresh, checksum mismatch\n"); 2405 return -EINVAL; 2406 } 2407 2408 return 0; 2409 } 2410 2411 int amdgpu_discovery_get_nps_info(struct amdgpu_device *adev, 2412 uint32_t *nps_type, 2413 struct amdgpu_gmc_memrange *ranges, 2414 int *range_cnt, bool refresh) 2415 { 2416 uint8_t *discovery_bin = adev->discovery.bin; 2417 struct table_info *info; 2418 union nps_info *nps_info; 2419 union nps_info nps_data; 2420 u16 offset; 2421 int i, r; 2422 2423 if (!nps_type || !range_cnt || !ranges) 2424 return -EINVAL; 2425 2426 if (refresh) { 2427 r = amdgpu_discovery_refresh_nps_info(adev, &nps_data); 2428 if (r) 2429 return r; 2430 nps_info = &nps_data; 2431 } else { 2432 if (!discovery_bin) { 2433 dev_err(adev->dev, 2434 "fetch mem range failed, ip discovery uninitialized\n"); 2435 return -EINVAL; 2436 } 2437 2438 if (amdgpu_discovery_get_table_info(adev, &info, NPS_INFO)) 2439 return -EINVAL; 2440 offset = le16_to_cpu(info->offset); 2441 2442 if (!offset) 2443 return -ENOENT; 2444 2445 /* If verification fails, return as if NPS table doesn't exist */ 2446 if (amdgpu_discovery_verify_npsinfo(adev, info)) 2447 return -ENOENT; 2448 2449 nps_info = (union nps_info *)(discovery_bin + offset); 2450 } 2451 2452 switch (le16_to_cpu(nps_info->v1.header.version_major)) { 2453 case 1: 2454 *nps_type = nps_info->v1.nps_type; 2455 if (*range_cnt < nps_info->v1.count) { 2456 dev_dbg(adev->dev, 2457 "not enough space for nps ranges: %d < %d\n", 2458 *range_cnt, nps_info->v1.count); 2459 return -ENOSPC; 2460 } 2461 *range_cnt = nps_info->v1.count; 2462 for (i = 0; i < *range_cnt; i++) { 2463 ranges[i].base_address = 2464 nps_info->v1.instance_info[i].base_address; 2465 ranges[i].limit_address = 2466 nps_info->v1.instance_info[i].limit_address; 2467 ranges[i].nid_mask = -1; 2468 ranges[i].flags = 0; 2469 } 2470 break; 2471 default: 2472 dev_err(adev->dev, "Unhandled NPS info table %d.%d\n", 2473 le16_to_cpu(nps_info->v1.header.version_major), 2474 le16_to_cpu(nps_info->v1.header.version_minor)); 2475 return -EINVAL; 2476 } 2477 2478 return 0; 2479 } 2480 2481 static const char *amdgpu_discovery_mem_reserved_region_name(u32 id) 2482 { 2483 switch (id) { 2484 case NO_RESERVED_REGION_ID: return "NO_RESERVED_REGION"; 2485 case PRE_OS_DISP_FW_REGION_ID: return "PRE_OS_DISP_FW"; 2486 case MASTER_DIE_UMF_REGION_ID: return "MASTER_DIE_UMF"; 2487 case DCC_META_DATA_REGION_ID: return "DCC_META_DATA"; 2488 case VM_PAGE_FAULT_REGION_ID: return "VM_PAGE_FAULT"; 2489 case G7_PSTATE_APERTURE1_ID: return "G7_PSTATE_APERTURE1"; 2490 case G7_PSTATE_APERTURE2_ID: return "G7_PSTATE_APERTURE2"; 2491 case G7_PSTATE_MIRROR_REGION_ID: return "G7_PSTATE_MIRROR"; 2492 case G7_TRAINING_DATA_REGION_ID: return "G7_TRAINING_DATA"; 2493 case SPECIFIC_PURPOSE_REGION_ID: return "SPECIFIC_PURPOSE"; 2494 default: return "UNKNOWN"; 2495 } 2496 } 2497 2498 /* Dump all the mem_reserved_info table entries for debugging */ 2499 static void amdgpu_discovery_dump_mem_reserved_info_table(struct amdgpu_device *adev, 2500 struct mem_reserved_info_table_v1_0 *table) 2501 { 2502 u32 list_num = min_t(u32, le32_to_cpu(table->list_num), MAX_MEM_REGION_NUM); 2503 u32 id, i; 2504 2505 dev_info(adev->dev, "MEM_RESERVED_INFO: list_num=%u\n", list_num); 2506 for (i = 0; i < list_num; i++) { 2507 id = le32_to_cpu(table->list[i].reserved_region_id); 2508 dev_info(adev->dev, " [%u] id=%u (%s) size=0x%llx start=0x%llx\n", 2509 i, id, 2510 amdgpu_discovery_mem_reserved_region_name(id), 2511 le64_to_cpu(table->list[i].reserved_region_size), 2512 le64_to_cpu(table->list[i].reserved_region_start)); 2513 } 2514 } 2515 2516 /** 2517 * Add READ-ONLY mem_reserved_info sysfs entry. 2518 * MEM_RESERVED_INFO table is parsed from the IP discovery binary. 2519 * The file mem_reserved_info dumps every entry as: 2520 * list_num=<N> 2521 * [<i>] id=<id> (<name>) size=<size> start=<start> 2522 */ 2523 static ssize_t mem_reserved_info_show(struct device *dev, 2524 struct device_attribute *attr, char *buf) 2525 { 2526 struct drm_device *ddev = dev_get_drvdata(dev); 2527 struct amdgpu_device *adev = drm_to_adev(ddev); 2528 struct mem_reserved_info_table_v1_0 *table; 2529 u32 list_num, id, i; 2530 ssize_t len = 0; 2531 2532 if (amdgpu_discovery_get_mem_reserved_info_table(adev)) 2533 return sysfs_emit(buf, "MEM_RESERVED_INFO table not available\n"); 2534 2535 table = adev->discovery.mem_reserved_table; 2536 list_num = min_t(u32, le32_to_cpu(table->list_num), MAX_MEM_REGION_NUM); 2537 2538 len += sysfs_emit_at(buf, len, "list_num=%u\n", list_num); 2539 for (i = 0; i < list_num; i++) { 2540 id = le32_to_cpu(table->list[i].reserved_region_id); 2541 len += sysfs_emit_at(buf, len, 2542 "[%u] id=%u (%s) size=0x%llx start=0x%llx\n", 2543 i, id, 2544 amdgpu_discovery_mem_reserved_region_name(id), 2545 le64_to_cpu(table->list[i].reserved_region_size), 2546 le64_to_cpu(table->list[i].reserved_region_start)); 2547 } 2548 2549 return len; 2550 } 2551 2552 static DEVICE_ATTR_RO(mem_reserved_info); 2553 2554 int amdgpu_discovery_mem_reserved_info_sysfs_init(struct amdgpu_device *adev) 2555 { 2556 if (!adev->discovery.mem_reserved_table) 2557 return 0; 2558 2559 return sysfs_create_file(&adev->dev->kobj, 2560 &dev_attr_mem_reserved_info.attr); 2561 } 2562 2563 void amdgpu_discovery_mem_reserved_info_sysfs_fini(struct amdgpu_device *adev) 2564 { 2565 if (!adev->discovery.mem_reserved_table) 2566 return; 2567 2568 sysfs_remove_file(&adev->dev->kobj, 2569 &dev_attr_mem_reserved_info.attr); 2570 } 2571 2572 /* 2573 * Resolve the MEM_RESERVED_INFO table from the IP discovery binary and 2574 * cache it in adev->discovery.mem_reserved_table. 2575 * Return: 0 on success, -ENOENT/-EINVAL if the table unavailable. 2576 */ 2577 int amdgpu_discovery_get_mem_reserved_info_table(struct amdgpu_device *adev) 2578 { 2579 uint8_t *discovery_bin = adev->discovery.bin; 2580 struct mem_reserved_info_header *mrhdr; 2581 struct binary_header *bhdr; 2582 struct table_info *info; 2583 u16 offset; 2584 2585 /* If already queried, do not query again. */ 2586 if (adev->discovery.mem_reserved_table) 2587 return 0; 2588 2589 if (!discovery_bin) { 2590 dev_err(adev->dev, "ip discovery uninitialized\n"); 2591 return -ENOENT; 2592 } 2593 2594 /* MEM_RESERVED_INFO only exists in binary_header >= v2. Skip populating it on v1 */ 2595 bhdr = (struct binary_header *)discovery_bin; 2596 if (le16_to_cpu(bhdr->version_major) < 2) { 2597 dev_dbg(adev->dev, "header version = %d\n", le16_to_cpu(bhdr->version_major)); 2598 return -ENOENT; 2599 } 2600 2601 if (amdgpu_discovery_get_table_info(adev, &info, MEM_RESERVED_INFO)) { 2602 dev_dbg(adev->dev, "MEM_RESERVED_INFO table entry not present\n"); 2603 return -EINVAL; 2604 } 2605 2606 offset = le16_to_cpu(info->offset); 2607 if (!offset) { 2608 dev_dbg(adev->dev, "MEM_RESERVED_INFO table offset is 0, invalid!\n"); 2609 return -EINVAL; 2610 } 2611 2612 /* 2613 * Only populated when the signature matches; skip ASICs whose 2614 * slot points to unrelated data. 2615 */ 2616 mrhdr = (struct mem_reserved_info_header *)(discovery_bin + offset); 2617 if (le32_to_cpu(mrhdr->signature) != MEM_RSV_TABLE_SIGNATURE) { 2618 dev_dbg(adev->dev, "MEM_RESERVED_INFO table signature mismatch, skipping\n"); 2619 return -ENOENT; 2620 } 2621 2622 /* Cache for subsequent lookups. */ 2623 adev->discovery.mem_reserved_table = 2624 (struct mem_reserved_info_table_v1_0 *)mrhdr; 2625 2626 dev_dbg(adev->dev, "MEM_RESERVED_INFO table exist\n"); 2627 return 0; 2628 } 2629 2630 /** 2631 * Query mem_reserved_info by region ID 2632 * 2633 * @reserved_region_id: MEM_RESERVED_REGION_ID to look up 2634 * @info: pass NULL to skip 2635 */ 2636 int amdgpu_discovery_get_mem_reserved_region_by_id(struct amdgpu_device *adev, 2637 u32 reserved_region_id, 2638 struct mem_reserved_info *info) 2639 { 2640 struct mem_reserved_info_table_v1_0 *table; 2641 u32 list_num, i; 2642 2643 if (!adev->discovery.mem_reserved_table) 2644 return -ENOENT; 2645 2646 table = adev->discovery.mem_reserved_table; 2647 list_num = min_t(u32, le32_to_cpu(table->list_num), MAX_MEM_REGION_NUM); 2648 2649 for (i = 0; i < list_num; i++) { 2650 if (le32_to_cpu(table->list[i].reserved_region_id) != reserved_region_id) 2651 continue; 2652 2653 if (info) { 2654 info->reserved_region_id = reserved_region_id; 2655 info->reserved_region_size = 2656 le64_to_cpu(table->list[i].reserved_region_size); 2657 info->reserved_region_start = 2658 le64_to_cpu(table->list[i].reserved_region_start); 2659 } 2660 dev_dbg(adev->dev, "Found reserved region: %s (id=%u)\n", 2661 amdgpu_discovery_mem_reserved_region_name(reserved_region_id), 2662 reserved_region_id); 2663 return 0; 2664 } 2665 return -ENOENT; 2666 } 2667 2668 static int amdgpu_discovery_set_common_ip_blocks(struct amdgpu_device *adev) 2669 { 2670 /* what IP to use for this? */ 2671 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) { 2672 case IP_VERSION(9, 0, 1): 2673 case IP_VERSION(9, 1, 0): 2674 case IP_VERSION(9, 2, 1): 2675 case IP_VERSION(9, 2, 2): 2676 case IP_VERSION(9, 3, 0): 2677 case IP_VERSION(9, 4, 0): 2678 case IP_VERSION(9, 4, 1): 2679 case IP_VERSION(9, 4, 2): 2680 case IP_VERSION(9, 4, 3): 2681 case IP_VERSION(9, 4, 4): 2682 case IP_VERSION(9, 5, 0): 2683 amdgpu_device_ip_block_add(adev, &vega10_common_ip_block); 2684 break; 2685 case IP_VERSION(10, 1, 10): 2686 case IP_VERSION(10, 1, 1): 2687 case IP_VERSION(10, 1, 2): 2688 case IP_VERSION(10, 1, 3): 2689 case IP_VERSION(10, 1, 4): 2690 case IP_VERSION(10, 3, 0): 2691 case IP_VERSION(10, 3, 1): 2692 case IP_VERSION(10, 3, 2): 2693 case IP_VERSION(10, 3, 3): 2694 case IP_VERSION(10, 3, 4): 2695 case IP_VERSION(10, 3, 5): 2696 case IP_VERSION(10, 3, 6): 2697 case IP_VERSION(10, 3, 7): 2698 amdgpu_device_ip_block_add(adev, &nv_common_ip_block); 2699 break; 2700 case IP_VERSION(11, 0, 0): 2701 case IP_VERSION(11, 0, 1): 2702 case IP_VERSION(11, 0, 2): 2703 case IP_VERSION(11, 0, 3): 2704 case IP_VERSION(11, 0, 4): 2705 case IP_VERSION(11, 5, 0): 2706 case IP_VERSION(11, 5, 1): 2707 case IP_VERSION(11, 5, 2): 2708 case IP_VERSION(11, 5, 3): 2709 case IP_VERSION(11, 5, 4): 2710 case IP_VERSION(11, 5, 6): 2711 case IP_VERSION(11, 7, 0): 2712 case IP_VERSION(11, 7, 1): 2713 amdgpu_device_ip_block_add(adev, &soc21_common_ip_block); 2714 break; 2715 case IP_VERSION(12, 0, 0): 2716 case IP_VERSION(12, 0, 1): 2717 amdgpu_device_ip_block_add(adev, &soc24_common_ip_block); 2718 break; 2719 case IP_VERSION(12, 1, 0): 2720 amdgpu_device_ip_block_add(adev, &soc_v1_0_common_ip_block); 2721 break; 2722 default: 2723 dev_err(adev->dev, 2724 "Failed to add common ip block(GC_HWIP:0x%x)\n", 2725 amdgpu_ip_version(adev, GC_HWIP, 0)); 2726 return -EINVAL; 2727 } 2728 return 0; 2729 } 2730 2731 static int amdgpu_discovery_set_gmc_ip_blocks(struct amdgpu_device *adev) 2732 { 2733 /* use GC or MMHUB IP version */ 2734 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) { 2735 case IP_VERSION(9, 0, 1): 2736 case IP_VERSION(9, 1, 0): 2737 case IP_VERSION(9, 2, 1): 2738 case IP_VERSION(9, 2, 2): 2739 case IP_VERSION(9, 3, 0): 2740 case IP_VERSION(9, 4, 0): 2741 case IP_VERSION(9, 4, 1): 2742 case IP_VERSION(9, 4, 2): 2743 case IP_VERSION(9, 4, 3): 2744 case IP_VERSION(9, 4, 4): 2745 case IP_VERSION(9, 5, 0): 2746 amdgpu_device_ip_block_add(adev, &gmc_v9_0_ip_block); 2747 break; 2748 case IP_VERSION(10, 1, 10): 2749 case IP_VERSION(10, 1, 1): 2750 case IP_VERSION(10, 1, 2): 2751 case IP_VERSION(10, 1, 3): 2752 case IP_VERSION(10, 1, 4): 2753 case IP_VERSION(10, 3, 0): 2754 case IP_VERSION(10, 3, 1): 2755 case IP_VERSION(10, 3, 2): 2756 case IP_VERSION(10, 3, 3): 2757 case IP_VERSION(10, 3, 4): 2758 case IP_VERSION(10, 3, 5): 2759 case IP_VERSION(10, 3, 6): 2760 case IP_VERSION(10, 3, 7): 2761 amdgpu_device_ip_block_add(adev, &gmc_v10_0_ip_block); 2762 break; 2763 case IP_VERSION(11, 0, 0): 2764 case IP_VERSION(11, 0, 1): 2765 case IP_VERSION(11, 0, 2): 2766 case IP_VERSION(11, 0, 3): 2767 case IP_VERSION(11, 0, 4): 2768 case IP_VERSION(11, 5, 0): 2769 case IP_VERSION(11, 5, 1): 2770 case IP_VERSION(11, 5, 2): 2771 case IP_VERSION(11, 5, 3): 2772 case IP_VERSION(11, 5, 4): 2773 case IP_VERSION(11, 5, 6): 2774 case IP_VERSION(11, 7, 0): 2775 case IP_VERSION(11, 7, 1): 2776 amdgpu_device_ip_block_add(adev, &gmc_v11_0_ip_block); 2777 break; 2778 case IP_VERSION(12, 0, 0): 2779 case IP_VERSION(12, 0, 1): 2780 case IP_VERSION(12, 1, 0): 2781 amdgpu_device_ip_block_add(adev, &gmc_v12_0_ip_block); 2782 break; 2783 default: 2784 dev_err(adev->dev, "Failed to add gmc ip block(GC_HWIP:0x%x)\n", 2785 amdgpu_ip_version(adev, GC_HWIP, 0)); 2786 return -EINVAL; 2787 } 2788 return 0; 2789 } 2790 2791 static int amdgpu_discovery_set_ih_ip_blocks(struct amdgpu_device *adev) 2792 { 2793 switch (amdgpu_ip_version(adev, OSSSYS_HWIP, 0)) { 2794 case IP_VERSION(4, 0, 0): 2795 case IP_VERSION(4, 0, 1): 2796 case IP_VERSION(4, 1, 0): 2797 case IP_VERSION(4, 1, 1): 2798 case IP_VERSION(4, 3, 0): 2799 amdgpu_device_ip_block_add(adev, &vega10_ih_ip_block); 2800 break; 2801 case IP_VERSION(4, 2, 0): 2802 case IP_VERSION(4, 2, 1): 2803 case IP_VERSION(4, 4, 0): 2804 case IP_VERSION(4, 4, 2): 2805 case IP_VERSION(4, 4, 5): 2806 amdgpu_device_ip_block_add(adev, &vega20_ih_ip_block); 2807 break; 2808 case IP_VERSION(5, 0, 0): 2809 case IP_VERSION(5, 0, 1): 2810 case IP_VERSION(5, 0, 2): 2811 case IP_VERSION(5, 0, 3): 2812 case IP_VERSION(5, 2, 0): 2813 case IP_VERSION(5, 2, 1): 2814 amdgpu_device_ip_block_add(adev, &navi10_ih_ip_block); 2815 break; 2816 case IP_VERSION(6, 0, 0): 2817 case IP_VERSION(6, 0, 1): 2818 case IP_VERSION(6, 0, 2): 2819 amdgpu_device_ip_block_add(adev, &ih_v6_0_ip_block); 2820 break; 2821 case IP_VERSION(6, 1, 0): 2822 case IP_VERSION(6, 1, 1): 2823 case IP_VERSION(6, 4, 0): 2824 amdgpu_device_ip_block_add(adev, &ih_v6_1_ip_block); 2825 break; 2826 case IP_VERSION(7, 0, 0): 2827 case IP_VERSION(7, 1, 0): 2828 amdgpu_device_ip_block_add(adev, &ih_v7_0_ip_block); 2829 break; 2830 case IP_VERSION(8, 0, 1): 2831 amdgpu_device_ip_block_add(adev, &ih_v8_0_ip_block); 2832 break; 2833 default: 2834 dev_err(adev->dev, 2835 "Failed to add ih ip block(OSSSYS_HWIP:0x%x)\n", 2836 amdgpu_ip_version(adev, OSSSYS_HWIP, 0)); 2837 return -EINVAL; 2838 } 2839 return 0; 2840 } 2841 2842 static int amdgpu_discovery_set_psp_ip_blocks(struct amdgpu_device *adev) 2843 { 2844 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) { 2845 case IP_VERSION(9, 0, 0): 2846 amdgpu_device_ip_block_add(adev, &psp_v3_1_ip_block); 2847 break; 2848 case IP_VERSION(10, 0, 0): 2849 case IP_VERSION(10, 0, 1): 2850 amdgpu_device_ip_block_add(adev, &psp_v10_0_ip_block); 2851 break; 2852 case IP_VERSION(11, 0, 0): 2853 case IP_VERSION(11, 0, 2): 2854 case IP_VERSION(11, 0, 4): 2855 case IP_VERSION(11, 0, 5): 2856 case IP_VERSION(11, 0, 9): 2857 case IP_VERSION(11, 0, 7): 2858 case IP_VERSION(11, 0, 11): 2859 case IP_VERSION(11, 0, 12): 2860 case IP_VERSION(11, 0, 13): 2861 case IP_VERSION(11, 5, 0): 2862 case IP_VERSION(11, 5, 2): 2863 amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block); 2864 break; 2865 case IP_VERSION(11, 0, 8): 2866 amdgpu_device_ip_block_add(adev, &psp_v11_0_8_ip_block); 2867 break; 2868 case IP_VERSION(11, 0, 3): 2869 case IP_VERSION(12, 0, 1): 2870 amdgpu_device_ip_block_add(adev, &psp_v12_0_ip_block); 2871 break; 2872 case IP_VERSION(13, 0, 0): 2873 case IP_VERSION(13, 0, 1): 2874 case IP_VERSION(13, 0, 2): 2875 case IP_VERSION(13, 0, 3): 2876 case IP_VERSION(13, 0, 5): 2877 case IP_VERSION(13, 0, 6): 2878 case IP_VERSION(13, 0, 7): 2879 case IP_VERSION(13, 0, 8): 2880 case IP_VERSION(13, 0, 10): 2881 case IP_VERSION(13, 0, 11): 2882 case IP_VERSION(13, 0, 12): 2883 case IP_VERSION(13, 0, 14): 2884 case IP_VERSION(13, 0, 15): 2885 case IP_VERSION(14, 0, 0): 2886 case IP_VERSION(14, 0, 1): 2887 case IP_VERSION(14, 0, 4): 2888 amdgpu_device_ip_block_add(adev, &psp_v13_0_ip_block); 2889 break; 2890 case IP_VERSION(13, 0, 4): 2891 amdgpu_device_ip_block_add(adev, &psp_v13_0_4_ip_block); 2892 break; 2893 case IP_VERSION(14, 0, 2): 2894 case IP_VERSION(14, 0, 3): 2895 case IP_VERSION(14, 0, 5): 2896 amdgpu_device_ip_block_add(adev, &psp_v14_0_ip_block); 2897 break; 2898 case IP_VERSION(15, 0, 0): 2899 case IP_VERSION(15, 0, 5): 2900 case IP_VERSION(15, 0, 9): 2901 amdgpu_device_ip_block_add(adev, &psp_v15_0_ip_block); 2902 break; 2903 case IP_VERSION(15, 0, 3): 2904 amdgpu_device_ip_block_add(adev, &psp_v15_0_3_ip_block); 2905 break; 2906 case IP_VERSION(15, 0, 8): 2907 amdgpu_device_ip_block_add(adev, &psp_v15_0_8_ip_block); 2908 break; 2909 default: 2910 dev_err(adev->dev, 2911 "Failed to add psp ip block(MP0_HWIP:0x%x)\n", 2912 amdgpu_ip_version(adev, MP0_HWIP, 0)); 2913 return -EINVAL; 2914 } 2915 return 0; 2916 } 2917 2918 static int amdgpu_discovery_set_smu_ip_blocks(struct amdgpu_device *adev) 2919 { 2920 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) { 2921 case IP_VERSION(9, 0, 0): 2922 case IP_VERSION(10, 0, 0): 2923 case IP_VERSION(10, 0, 1): 2924 case IP_VERSION(11, 0, 2): 2925 if (adev->asic_type == CHIP_ARCTURUS) 2926 amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block); 2927 else 2928 amdgpu_device_ip_block_add(adev, &pp_smu_ip_block); 2929 break; 2930 case IP_VERSION(11, 0, 0): 2931 case IP_VERSION(11, 0, 5): 2932 case IP_VERSION(11, 0, 9): 2933 case IP_VERSION(11, 0, 7): 2934 case IP_VERSION(11, 0, 11): 2935 case IP_VERSION(11, 0, 12): 2936 case IP_VERSION(11, 0, 13): 2937 case IP_VERSION(11, 5, 0): 2938 case IP_VERSION(11, 5, 2): 2939 amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block); 2940 break; 2941 case IP_VERSION(11, 0, 8): 2942 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) 2943 amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block); 2944 break; 2945 case IP_VERSION(12, 0, 0): 2946 case IP_VERSION(12, 0, 1): 2947 amdgpu_device_ip_block_add(adev, &smu_v12_0_ip_block); 2948 break; 2949 case IP_VERSION(13, 0, 0): 2950 case IP_VERSION(13, 0, 1): 2951 case IP_VERSION(13, 0, 2): 2952 case IP_VERSION(13, 0, 3): 2953 case IP_VERSION(13, 0, 4): 2954 case IP_VERSION(13, 0, 5): 2955 case IP_VERSION(13, 0, 6): 2956 case IP_VERSION(13, 0, 7): 2957 case IP_VERSION(13, 0, 8): 2958 case IP_VERSION(13, 0, 10): 2959 case IP_VERSION(13, 0, 11): 2960 case IP_VERSION(13, 0, 14): 2961 case IP_VERSION(13, 0, 12): 2962 amdgpu_device_ip_block_add(adev, &smu_v13_0_ip_block); 2963 break; 2964 case IP_VERSION(14, 0, 0): 2965 case IP_VERSION(14, 0, 1): 2966 case IP_VERSION(14, 0, 2): 2967 case IP_VERSION(14, 0, 3): 2968 case IP_VERSION(14, 0, 4): 2969 case IP_VERSION(14, 0, 5): 2970 amdgpu_device_ip_block_add(adev, &smu_v14_0_ip_block); 2971 break; 2972 case IP_VERSION(15, 0, 0): 2973 case IP_VERSION(15, 0, 5): 2974 case IP_VERSION(15, 0, 8): 2975 case IP_VERSION(15, 0, 9): 2976 amdgpu_device_ip_block_add(adev, &smu_v15_0_ip_block); 2977 break; 2978 default: 2979 dev_err(adev->dev, 2980 "Failed to add smu ip block(MP1_HWIP:0x%x)\n", 2981 amdgpu_ip_version(adev, MP1_HWIP, 0)); 2982 return -EINVAL; 2983 } 2984 return 0; 2985 } 2986 2987 #if defined(CONFIG_DRM_AMD_DC) 2988 static void amdgpu_discovery_set_sriov_display(struct amdgpu_device *adev) 2989 { 2990 amdgpu_device_set_sriov_virtual_display(adev); 2991 amdgpu_device_ip_block_add(adev, &amdgpu_vkms_ip_block); 2992 } 2993 #endif 2994 2995 static int amdgpu_discovery_set_display_ip_blocks(struct amdgpu_device *adev) 2996 { 2997 if (adev->enable_virtual_display) { 2998 amdgpu_device_ip_block_add(adev, &amdgpu_vkms_ip_block); 2999 return 0; 3000 } 3001 3002 if (!amdgpu_device_has_dc_support(adev)) 3003 return 0; 3004 3005 #if defined(CONFIG_DRM_AMD_DC) 3006 if (amdgpu_ip_version(adev, DCE_HWIP, 0)) { 3007 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) { 3008 case IP_VERSION(1, 0, 0): 3009 case IP_VERSION(1, 0, 1): 3010 case IP_VERSION(2, 0, 2): 3011 case IP_VERSION(2, 0, 0): 3012 case IP_VERSION(2, 0, 3): 3013 case IP_VERSION(2, 1, 0): 3014 case IP_VERSION(3, 0, 0): 3015 case IP_VERSION(3, 0, 2): 3016 case IP_VERSION(3, 0, 3): 3017 case IP_VERSION(3, 0, 1): 3018 case IP_VERSION(3, 1, 2): 3019 case IP_VERSION(3, 1, 3): 3020 case IP_VERSION(3, 1, 4): 3021 case IP_VERSION(3, 1, 5): 3022 case IP_VERSION(3, 1, 6): 3023 case IP_VERSION(3, 2, 0): 3024 case IP_VERSION(3, 2, 1): 3025 case IP_VERSION(3, 5, 0): 3026 case IP_VERSION(3, 5, 1): 3027 case IP_VERSION(3, 6, 0): 3028 case IP_VERSION(4, 1, 0): 3029 case IP_VERSION(4, 2, 0): 3030 case IP_VERSION(4, 2, 1): 3031 case IP_VERSION(6, 0, 0): 3032 /* TODO: Fix IP version. DC code expects version 4.0.1 */ 3033 if (adev->ip_versions[DCE_HWIP][0] == IP_VERSION(4, 1, 0)) 3034 adev->ip_versions[DCE_HWIP][0] = IP_VERSION(4, 0, 1); 3035 3036 if (amdgpu_sriov_vf(adev)) 3037 amdgpu_discovery_set_sriov_display(adev); 3038 else 3039 amdgpu_device_ip_block_add(adev, &dm_ip_block); 3040 break; 3041 default: 3042 dev_err(adev->dev, 3043 "Failed to add dm ip block(DCE_HWIP:0x%x)\n", 3044 amdgpu_ip_version(adev, DCE_HWIP, 0)); 3045 return -EINVAL; 3046 } 3047 } else if (amdgpu_ip_version(adev, DCI_HWIP, 0)) { 3048 switch (amdgpu_ip_version(adev, DCI_HWIP, 0)) { 3049 case IP_VERSION(12, 0, 0): 3050 case IP_VERSION(12, 0, 1): 3051 case IP_VERSION(12, 1, 0): 3052 if (amdgpu_sriov_vf(adev)) 3053 amdgpu_discovery_set_sriov_display(adev); 3054 else 3055 amdgpu_device_ip_block_add(adev, &dm_ip_block); 3056 break; 3057 default: 3058 dev_err(adev->dev, 3059 "Failed to add dm ip block(DCI_HWIP:0x%x)\n", 3060 amdgpu_ip_version(adev, DCI_HWIP, 0)); 3061 return -EINVAL; 3062 } 3063 } 3064 #endif 3065 return 0; 3066 } 3067 3068 static int amdgpu_discovery_set_gc_ip_blocks(struct amdgpu_device *adev) 3069 { 3070 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) { 3071 case IP_VERSION(9, 0, 1): 3072 case IP_VERSION(9, 1, 0): 3073 case IP_VERSION(9, 2, 1): 3074 case IP_VERSION(9, 2, 2): 3075 case IP_VERSION(9, 3, 0): 3076 case IP_VERSION(9, 4, 0): 3077 case IP_VERSION(9, 4, 1): 3078 case IP_VERSION(9, 4, 2): 3079 amdgpu_device_ip_block_add(adev, &gfx_v9_0_ip_block); 3080 break; 3081 case IP_VERSION(9, 4, 3): 3082 case IP_VERSION(9, 4, 4): 3083 case IP_VERSION(9, 5, 0): 3084 amdgpu_device_ip_block_add(adev, &gfx_v9_4_3_ip_block); 3085 break; 3086 case IP_VERSION(10, 1, 10): 3087 case IP_VERSION(10, 1, 2): 3088 case IP_VERSION(10, 1, 1): 3089 case IP_VERSION(10, 1, 3): 3090 case IP_VERSION(10, 1, 4): 3091 case IP_VERSION(10, 3, 0): 3092 case IP_VERSION(10, 3, 2): 3093 case IP_VERSION(10, 3, 1): 3094 case IP_VERSION(10, 3, 4): 3095 case IP_VERSION(10, 3, 5): 3096 case IP_VERSION(10, 3, 6): 3097 case IP_VERSION(10, 3, 3): 3098 case IP_VERSION(10, 3, 7): 3099 amdgpu_device_ip_block_add(adev, &gfx_v10_0_ip_block); 3100 break; 3101 case IP_VERSION(11, 0, 0): 3102 case IP_VERSION(11, 0, 1): 3103 case IP_VERSION(11, 0, 2): 3104 case IP_VERSION(11, 0, 3): 3105 case IP_VERSION(11, 0, 4): 3106 case IP_VERSION(11, 5, 0): 3107 case IP_VERSION(11, 5, 1): 3108 case IP_VERSION(11, 5, 2): 3109 case IP_VERSION(11, 5, 3): 3110 case IP_VERSION(11, 5, 4): 3111 case IP_VERSION(11, 5, 6): 3112 case IP_VERSION(11, 7, 0): 3113 case IP_VERSION(11, 7, 1): 3114 amdgpu_device_ip_block_add(adev, &gfx_v11_0_ip_block); 3115 break; 3116 case IP_VERSION(12, 0, 0): 3117 case IP_VERSION(12, 0, 1): 3118 amdgpu_device_ip_block_add(adev, &gfx_v12_0_ip_block); 3119 break; 3120 case IP_VERSION(12, 1, 0): 3121 amdgpu_device_ip_block_add(adev, &gfx_v12_1_ip_block); 3122 break; 3123 default: 3124 dev_err(adev->dev, "Failed to add gfx ip block(GC_HWIP:0x%x)\n", 3125 amdgpu_ip_version(adev, GC_HWIP, 0)); 3126 return -EINVAL; 3127 } 3128 return 0; 3129 } 3130 3131 static int amdgpu_discovery_set_sdma_ip_blocks(struct amdgpu_device *adev) 3132 { 3133 switch (amdgpu_ip_version(adev, SDMA0_HWIP, 0)) { 3134 case IP_VERSION(4, 0, 0): 3135 case IP_VERSION(4, 0, 1): 3136 case IP_VERSION(4, 1, 0): 3137 case IP_VERSION(4, 1, 1): 3138 case IP_VERSION(4, 1, 2): 3139 case IP_VERSION(4, 2, 0): 3140 case IP_VERSION(4, 2, 2): 3141 case IP_VERSION(4, 4, 0): 3142 amdgpu_device_ip_block_add(adev, &sdma_v4_0_ip_block); 3143 break; 3144 case IP_VERSION(4, 4, 2): 3145 case IP_VERSION(4, 4, 5): 3146 case IP_VERSION(4, 4, 4): 3147 amdgpu_device_ip_block_add(adev, &sdma_v4_4_2_ip_block); 3148 break; 3149 case IP_VERSION(5, 0, 0): 3150 case IP_VERSION(5, 0, 1): 3151 case IP_VERSION(5, 0, 2): 3152 case IP_VERSION(5, 0, 5): 3153 amdgpu_device_ip_block_add(adev, &sdma_v5_0_ip_block); 3154 break; 3155 case IP_VERSION(5, 2, 0): 3156 case IP_VERSION(5, 2, 2): 3157 case IP_VERSION(5, 2, 4): 3158 case IP_VERSION(5, 2, 5): 3159 case IP_VERSION(5, 2, 6): 3160 case IP_VERSION(5, 2, 3): 3161 case IP_VERSION(5, 2, 1): 3162 case IP_VERSION(5, 2, 7): 3163 amdgpu_device_ip_block_add(adev, &sdma_v5_2_ip_block); 3164 break; 3165 case IP_VERSION(6, 0, 0): 3166 case IP_VERSION(6, 0, 1): 3167 case IP_VERSION(6, 0, 2): 3168 case IP_VERSION(6, 0, 3): 3169 case IP_VERSION(6, 1, 0): 3170 case IP_VERSION(6, 1, 1): 3171 case IP_VERSION(6, 1, 2): 3172 case IP_VERSION(6, 1, 3): 3173 case IP_VERSION(6, 1, 4): 3174 case IP_VERSION(6, 4, 0): 3175 amdgpu_device_ip_block_add(adev, &sdma_v6_0_ip_block); 3176 break; 3177 case IP_VERSION(7, 0, 0): 3178 case IP_VERSION(7, 0, 1): 3179 amdgpu_device_ip_block_add(adev, &sdma_v7_0_ip_block); 3180 break; 3181 case IP_VERSION(7, 1, 0): 3182 amdgpu_device_ip_block_add(adev, &sdma_v7_1_ip_block); 3183 break; 3184 default: 3185 dev_err(adev->dev, 3186 "Failed to add sdma ip block(SDMA0_HWIP:0x%x)\n", 3187 amdgpu_ip_version(adev, SDMA0_HWIP, 0)); 3188 return -EINVAL; 3189 } 3190 3191 return 0; 3192 } 3193 3194 static int amdgpu_discovery_set_ras_ip_blocks(struct amdgpu_device *adev) 3195 { 3196 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) { 3197 case IP_VERSION(13, 0, 6): 3198 case IP_VERSION(13, 0, 12): 3199 case IP_VERSION(13, 0, 14): 3200 case IP_VERSION(13, 0, 15): 3201 case IP_VERSION(15, 0, 8): 3202 case IP_VERSION(15, 0, 3): 3203 amdgpu_device_ip_block_add(adev, &ras_v1_0_ip_block); 3204 break; 3205 default: 3206 break; 3207 } 3208 return 0; 3209 } 3210 3211 static int amdgpu_discovery_set_mm_ip_blocks(struct amdgpu_device *adev) 3212 { 3213 if (amdgpu_ip_version(adev, VCE_HWIP, 0)) { 3214 switch (amdgpu_ip_version(adev, UVD_HWIP, 0)) { 3215 case IP_VERSION(7, 0, 0): 3216 case IP_VERSION(7, 2, 0): 3217 /* UVD is not supported on vega20 SR-IOV */ 3218 if (!(adev->asic_type == CHIP_VEGA20 && amdgpu_sriov_vf(adev))) 3219 amdgpu_device_ip_block_add(adev, &uvd_v7_0_ip_block); 3220 break; 3221 default: 3222 dev_err(adev->dev, 3223 "Failed to add uvd v7 ip block(UVD_HWIP:0x%x)\n", 3224 amdgpu_ip_version(adev, UVD_HWIP, 0)); 3225 return -EINVAL; 3226 } 3227 switch (amdgpu_ip_version(adev, VCE_HWIP, 0)) { 3228 case IP_VERSION(4, 0, 0): 3229 case IP_VERSION(4, 1, 0): 3230 /* VCE is not supported on vega20 SR-IOV */ 3231 if (!(adev->asic_type == CHIP_VEGA20 && amdgpu_sriov_vf(adev))) 3232 amdgpu_device_ip_block_add(adev, &vce_v4_0_ip_block); 3233 break; 3234 default: 3235 dev_err(adev->dev, 3236 "Failed to add VCE v4 ip block(VCE_HWIP:0x%x)\n", 3237 amdgpu_ip_version(adev, VCE_HWIP, 0)); 3238 return -EINVAL; 3239 } 3240 } else { 3241 uint32_t vcn_version = amdgpu_ip_version(adev, UVD_HWIP, 0); 3242 3243 /* no VCN discovered; nothing to add */ 3244 if (!vcn_version) 3245 return 0; 3246 switch (vcn_version) { 3247 case IP_VERSION(1, 0, 0): 3248 case IP_VERSION(1, 0, 1): 3249 amdgpu_device_ip_block_add(adev, &vcn_v1_0_ip_block); 3250 break; 3251 case IP_VERSION(2, 0, 0): 3252 case IP_VERSION(2, 0, 2): 3253 case IP_VERSION(2, 2, 0): 3254 amdgpu_device_ip_block_add(adev, &vcn_v2_0_ip_block); 3255 if (!amdgpu_sriov_vf(adev)) 3256 amdgpu_device_ip_block_add(adev, &jpeg_v2_0_ip_block); 3257 break; 3258 case IP_VERSION(2, 0, 3): 3259 break; 3260 case IP_VERSION(2, 5, 0): 3261 amdgpu_device_ip_block_add(adev, &vcn_v2_5_ip_block); 3262 amdgpu_device_ip_block_add(adev, &jpeg_v2_5_ip_block); 3263 break; 3264 case IP_VERSION(2, 6, 0): 3265 amdgpu_device_ip_block_add(adev, &vcn_v2_6_ip_block); 3266 amdgpu_device_ip_block_add(adev, &jpeg_v2_6_ip_block); 3267 break; 3268 case IP_VERSION(3, 0, 0): 3269 case IP_VERSION(3, 0, 16): 3270 case IP_VERSION(3, 1, 1): 3271 case IP_VERSION(3, 1, 2): 3272 case IP_VERSION(3, 0, 2): 3273 amdgpu_device_ip_block_add(adev, &vcn_v3_0_ip_block); 3274 if (!amdgpu_sriov_vf(adev)) 3275 amdgpu_device_ip_block_add(adev, &jpeg_v3_0_ip_block); 3276 break; 3277 case IP_VERSION(3, 0, 33): 3278 amdgpu_device_ip_block_add(adev, &vcn_v3_0_ip_block); 3279 break; 3280 case IP_VERSION(4, 0, 0): 3281 case IP_VERSION(4, 0, 2): 3282 case IP_VERSION(4, 0, 4): 3283 amdgpu_device_ip_block_add(adev, &vcn_v4_0_ip_block); 3284 amdgpu_device_ip_block_add(adev, &jpeg_v4_0_ip_block); 3285 break; 3286 case IP_VERSION(4, 0, 3): 3287 amdgpu_device_ip_block_add(adev, &vcn_v4_0_3_ip_block); 3288 amdgpu_device_ip_block_add(adev, &jpeg_v4_0_3_ip_block); 3289 break; 3290 case IP_VERSION(4, 0, 5): 3291 case IP_VERSION(4, 0, 6): 3292 amdgpu_device_ip_block_add(adev, &vcn_v4_0_5_ip_block); 3293 amdgpu_device_ip_block_add(adev, &jpeg_v4_0_5_ip_block); 3294 break; 3295 case IP_VERSION(5, 0, 0): 3296 amdgpu_device_ip_block_add(adev, &vcn_v5_0_0_ip_block); 3297 amdgpu_device_ip_block_add(adev, &jpeg_v5_0_0_ip_block); 3298 break; 3299 case IP_VERSION(5, 3, 0): 3300 amdgpu_device_ip_block_add(adev, &vcn_v5_0_0_ip_block); 3301 amdgpu_device_ip_block_add(adev, &jpeg_v5_3_0_ip_block); 3302 break; 3303 case IP_VERSION(5, 0, 1): 3304 amdgpu_device_ip_block_add(adev, &vcn_v5_0_1_ip_block); 3305 amdgpu_device_ip_block_add(adev, &jpeg_v5_0_1_ip_block); 3306 break; 3307 case IP_VERSION(5, 0, 2): 3308 amdgpu_device_ip_block_add(adev, &vcn_v5_0_2_ip_block); 3309 amdgpu_device_ip_block_add(adev, &jpeg_v5_0_2_ip_block); 3310 break; 3311 default: 3312 dev_err(adev->dev, 3313 "Failed to add vcn/jpeg ip block(UVD_HWIP:0x%x)\n", 3314 vcn_version); 3315 return -EINVAL; 3316 } 3317 } 3318 return 0; 3319 } 3320 3321 static int amdgpu_discovery_set_mes_ip_blocks(struct amdgpu_device *adev) 3322 { 3323 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) { 3324 case IP_VERSION(11, 0, 0): 3325 case IP_VERSION(11, 0, 1): 3326 case IP_VERSION(11, 0, 2): 3327 case IP_VERSION(11, 0, 3): 3328 case IP_VERSION(11, 0, 4): 3329 case IP_VERSION(11, 5, 0): 3330 case IP_VERSION(11, 5, 1): 3331 case IP_VERSION(11, 5, 2): 3332 case IP_VERSION(11, 5, 3): 3333 case IP_VERSION(11, 5, 4): 3334 case IP_VERSION(11, 5, 6): 3335 case IP_VERSION(11, 7, 0): 3336 case IP_VERSION(11, 7, 1): 3337 amdgpu_device_ip_block_add(adev, &mes_v11_0_ip_block); 3338 adev->enable_mes = true; 3339 adev->enable_mes_kiq = true; 3340 break; 3341 case IP_VERSION(12, 0, 0): 3342 case IP_VERSION(12, 0, 1): 3343 amdgpu_device_ip_block_add(adev, &mes_v12_0_ip_block); 3344 adev->enable_mes = true; 3345 adev->enable_mes_kiq = true; 3346 if (amdgpu_uni_mes) 3347 adev->enable_uni_mes = true; 3348 break; 3349 case IP_VERSION(12, 1, 0): 3350 amdgpu_device_ip_block_add(adev, &mes_v12_1_ip_block); 3351 adev->enable_mes = true; 3352 adev->enable_mes_kiq = true; 3353 adev->enable_uni_mes = true; 3354 break; 3355 default: 3356 break; 3357 } 3358 return 0; 3359 } 3360 3361 static void amdgpu_discovery_init_soc_config(struct amdgpu_device *adev) 3362 { 3363 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) { 3364 case IP_VERSION(9, 4, 3): 3365 case IP_VERSION(9, 4, 4): 3366 case IP_VERSION(9, 5, 0): 3367 aqua_vanjaram_init_soc_config(adev); 3368 break; 3369 case IP_VERSION(12, 1, 0): 3370 soc_v1_0_init_soc_config(adev); 3371 break; 3372 default: 3373 break; 3374 } 3375 } 3376 3377 static int amdgpu_discovery_set_vpe_ip_blocks(struct amdgpu_device *adev) 3378 { 3379 switch (amdgpu_ip_version(adev, VPE_HWIP, 0)) { 3380 case IP_VERSION(6, 1, 0): 3381 case IP_VERSION(6, 1, 1): 3382 case IP_VERSION(6, 1, 3): 3383 amdgpu_device_ip_block_add(adev, &vpe_v6_1_ip_block); 3384 break; 3385 case IP_VERSION(2, 0, 0): 3386 case IP_VERSION(2, 2, 0): 3387 amdgpu_device_ip_block_add(adev, &vpe_v2_0_ip_block); 3388 break; 3389 case IP_VERSION(3, 0, 0): 3390 case IP_VERSION(3, 0, 1): 3391 amdgpu_device_ip_block_add(adev, &vpe_v3_0_ip_block); 3392 break; 3393 default: 3394 break; 3395 } 3396 3397 return 0; 3398 } 3399 3400 static int amdgpu_discovery_set_umsch_mm_ip_blocks(struct amdgpu_device *adev) 3401 { 3402 switch (amdgpu_ip_version(adev, VCN_HWIP, 0)) { 3403 case IP_VERSION(4, 0, 5): 3404 case IP_VERSION(4, 0, 6): 3405 if (amdgpu_umsch_mm & 0x1) { 3406 amdgpu_device_ip_block_add(adev, &umsch_mm_v4_0_ip_block); 3407 adev->enable_umsch_mm = true; 3408 } 3409 break; 3410 default: 3411 break; 3412 } 3413 3414 return 0; 3415 } 3416 3417 static int amdgpu_discovery_set_isp_ip_blocks(struct amdgpu_device *adev) 3418 { 3419 #if defined(CONFIG_DRM_AMD_ISP) 3420 switch (amdgpu_ip_version(adev, ISP_HWIP, 0)) { 3421 case IP_VERSION(4, 1, 0): 3422 amdgpu_device_ip_block_add(adev, &isp_v4_1_0_ip_block); 3423 break; 3424 case IP_VERSION(4, 1, 1): 3425 amdgpu_device_ip_block_add(adev, &isp_v4_1_1_ip_block); 3426 break; 3427 default: 3428 break; 3429 } 3430 #endif 3431 3432 return 0; 3433 } 3434 3435 static int amdgpu_discovery_set_ualink_ip_blocks(struct amdgpu_device *adev) 3436 { 3437 /* TODO: no dedicated IP discovery version for UALink yet; key off the 3438 * GC IP version for now. 3439 */ 3440 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) { 3441 case IP_VERSION(12, 1, 0): 3442 amdgpu_device_ip_block_add(adev, &ualink_v1_0_ip_block); 3443 break; 3444 default: 3445 break; 3446 } 3447 3448 return 0; 3449 } 3450 3451 int amdgpu_discovery_set_ip_blocks(struct amdgpu_device *adev) 3452 { 3453 int r; 3454 3455 switch (adev->asic_type) { 3456 case CHIP_VEGA10: 3457 /* This is not fatal. We only need the discovery 3458 * binary for sysfs. We don't need it for a 3459 * functional system. 3460 */ 3461 amdgpu_discovery_init(adev); 3462 vega10_reg_base_init(adev); 3463 adev->sdma.num_instances = 2; 3464 adev->sdma.sdma_mask = 3; 3465 adev->gmc.num_umc = 4; 3466 adev->gfx.xcc_mask = 1; 3467 adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 0, 0); 3468 adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 0, 0); 3469 adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 0, 0); 3470 adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 0, 0); 3471 adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 0, 0); 3472 adev->ip_versions[SDMA1_HWIP][0] = IP_VERSION(4, 0, 0); 3473 adev->ip_versions[DF_HWIP][0] = IP_VERSION(2, 1, 0); 3474 adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(6, 1, 0); 3475 adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 0, 0); 3476 adev->ip_versions[MP0_HWIP][0] = IP_VERSION(9, 0, 0); 3477 adev->ip_versions[MP1_HWIP][0] = IP_VERSION(9, 0, 0); 3478 adev->ip_versions[THM_HWIP][0] = IP_VERSION(9, 0, 0); 3479 adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(9, 0, 0); 3480 adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 0, 1); 3481 adev->ip_versions[UVD_HWIP][0] = IP_VERSION(7, 0, 0); 3482 adev->ip_versions[VCE_HWIP][0] = IP_VERSION(4, 0, 0); 3483 adev->ip_versions[DCI_HWIP][0] = IP_VERSION(12, 0, 0); 3484 break; 3485 case CHIP_VEGA12: 3486 /* This is not fatal. We only need the discovery 3487 * binary for sysfs. We don't need it for a 3488 * functional system. 3489 */ 3490 amdgpu_discovery_init(adev); 3491 vega10_reg_base_init(adev); 3492 adev->sdma.num_instances = 2; 3493 adev->sdma.sdma_mask = 3; 3494 adev->gmc.num_umc = 4; 3495 adev->gfx.xcc_mask = 1; 3496 adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 3, 0); 3497 adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 3, 0); 3498 adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 0, 1); 3499 adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 0, 1); 3500 adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 0, 1); 3501 adev->ip_versions[SDMA1_HWIP][0] = IP_VERSION(4, 0, 1); 3502 adev->ip_versions[DF_HWIP][0] = IP_VERSION(2, 5, 0); 3503 adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(6, 2, 0); 3504 adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 1, 0); 3505 adev->ip_versions[MP0_HWIP][0] = IP_VERSION(9, 0, 0); 3506 adev->ip_versions[MP1_HWIP][0] = IP_VERSION(9, 0, 0); 3507 adev->ip_versions[THM_HWIP][0] = IP_VERSION(9, 0, 0); 3508 adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(9, 0, 1); 3509 adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 2, 1); 3510 adev->ip_versions[UVD_HWIP][0] = IP_VERSION(7, 0, 0); 3511 adev->ip_versions[VCE_HWIP][0] = IP_VERSION(4, 0, 0); 3512 adev->ip_versions[DCI_HWIP][0] = IP_VERSION(12, 0, 1); 3513 break; 3514 case CHIP_RAVEN: 3515 /* This is not fatal. We only need the discovery 3516 * binary for sysfs. We don't need it for a 3517 * functional system. 3518 */ 3519 amdgpu_discovery_init(adev); 3520 vega10_reg_base_init(adev); 3521 adev->sdma.num_instances = 1; 3522 adev->sdma.sdma_mask = 1; 3523 adev->vcn.num_vcn_inst = 1; 3524 adev->gmc.num_umc = 2; 3525 adev->gfx.xcc_mask = 1; 3526 if (adev->apu_flags & AMD_APU_IS_RAVEN2) { 3527 adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 2, 0); 3528 adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 2, 0); 3529 adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 1, 1); 3530 adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 1, 1); 3531 adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 1, 1); 3532 adev->ip_versions[DF_HWIP][0] = IP_VERSION(2, 1, 1); 3533 adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 0, 1); 3534 adev->ip_versions[UMC_HWIP][0] = IP_VERSION(7, 5, 0); 3535 adev->ip_versions[MP0_HWIP][0] = IP_VERSION(10, 0, 1); 3536 adev->ip_versions[MP1_HWIP][0] = IP_VERSION(10, 0, 1); 3537 adev->ip_versions[THM_HWIP][0] = IP_VERSION(10, 1, 0); 3538 adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(10, 0, 1); 3539 adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 2, 2); 3540 adev->ip_versions[UVD_HWIP][0] = IP_VERSION(1, 0, 1); 3541 adev->ip_versions[DCE_HWIP][0] = IP_VERSION(1, 0, 1); 3542 adev->ip_versions[ISP_HWIP][0] = IP_VERSION(2, 0, 0); 3543 } else { 3544 adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 1, 0); 3545 adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 1, 0); 3546 adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 1, 0); 3547 adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 1, 0); 3548 adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 1, 0); 3549 adev->ip_versions[DF_HWIP][0] = IP_VERSION(2, 1, 0); 3550 adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 0, 0); 3551 adev->ip_versions[UMC_HWIP][0] = IP_VERSION(7, 0, 0); 3552 adev->ip_versions[MP0_HWIP][0] = IP_VERSION(10, 0, 0); 3553 adev->ip_versions[MP1_HWIP][0] = IP_VERSION(10, 0, 0); 3554 adev->ip_versions[THM_HWIP][0] = IP_VERSION(10, 0, 0); 3555 adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(10, 0, 0); 3556 adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 1, 0); 3557 adev->ip_versions[UVD_HWIP][0] = IP_VERSION(1, 0, 0); 3558 adev->ip_versions[DCE_HWIP][0] = IP_VERSION(1, 0, 0); 3559 adev->ip_versions[ISP_HWIP][0] = IP_VERSION(2, 0, 0); 3560 } 3561 break; 3562 case CHIP_VEGA20: 3563 /* This is not fatal. We only need the discovery 3564 * binary for sysfs. We don't need it for a 3565 * functional system. 3566 */ 3567 amdgpu_discovery_init(adev); 3568 vega20_reg_base_init(adev); 3569 adev->sdma.num_instances = 2; 3570 adev->sdma.sdma_mask = 3; 3571 adev->gmc.num_umc = 8; 3572 adev->gfx.xcc_mask = 1; 3573 adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 4, 0); 3574 adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 4, 0); 3575 adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 2, 0); 3576 adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 2, 0); 3577 adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 2, 0); 3578 adev->ip_versions[SDMA1_HWIP][0] = IP_VERSION(4, 2, 0); 3579 adev->ip_versions[DF_HWIP][0] = IP_VERSION(3, 6, 0); 3580 adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 4, 0); 3581 adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 1, 1); 3582 adev->ip_versions[MP0_HWIP][0] = IP_VERSION(11, 0, 2); 3583 adev->ip_versions[MP1_HWIP][0] = IP_VERSION(11, 0, 2); 3584 adev->ip_versions[THM_HWIP][0] = IP_VERSION(11, 0, 2); 3585 adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(11, 0, 2); 3586 adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 4, 0); 3587 adev->ip_versions[UVD_HWIP][0] = IP_VERSION(7, 2, 0); 3588 adev->ip_versions[UVD_HWIP][1] = IP_VERSION(7, 2, 0); 3589 adev->ip_versions[VCE_HWIP][0] = IP_VERSION(4, 1, 0); 3590 adev->ip_versions[DCI_HWIP][0] = IP_VERSION(12, 1, 0); 3591 break; 3592 case CHIP_ARCTURUS: 3593 /* This is not fatal. We only need the discovery 3594 * binary for sysfs. We don't need it for a 3595 * functional system. 3596 */ 3597 amdgpu_discovery_init(adev); 3598 arct_reg_base_init(adev); 3599 adev->sdma.num_instances = 8; 3600 adev->sdma.sdma_mask = 0xff; 3601 adev->vcn.num_vcn_inst = 2; 3602 adev->gmc.num_umc = 8; 3603 adev->gfx.xcc_mask = 1; 3604 adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 4, 1); 3605 adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 4, 1); 3606 adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 2, 1); 3607 adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 2, 1); 3608 adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 2, 2); 3609 adev->ip_versions[SDMA1_HWIP][0] = IP_VERSION(4, 2, 2); 3610 adev->ip_versions[SDMA1_HWIP][1] = IP_VERSION(4, 2, 2); 3611 adev->ip_versions[SDMA1_HWIP][2] = IP_VERSION(4, 2, 2); 3612 adev->ip_versions[SDMA1_HWIP][3] = IP_VERSION(4, 2, 2); 3613 adev->ip_versions[SDMA1_HWIP][4] = IP_VERSION(4, 2, 2); 3614 adev->ip_versions[SDMA1_HWIP][5] = IP_VERSION(4, 2, 2); 3615 adev->ip_versions[SDMA1_HWIP][6] = IP_VERSION(4, 2, 2); 3616 adev->ip_versions[DF_HWIP][0] = IP_VERSION(3, 6, 1); 3617 adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 4, 1); 3618 adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 1, 2); 3619 adev->ip_versions[MP0_HWIP][0] = IP_VERSION(11, 0, 4); 3620 adev->ip_versions[MP1_HWIP][0] = IP_VERSION(11, 0, 2); 3621 adev->ip_versions[THM_HWIP][0] = IP_VERSION(11, 0, 3); 3622 adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(11, 0, 3); 3623 adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 4, 1); 3624 adev->ip_versions[UVD_HWIP][0] = IP_VERSION(2, 5, 0); 3625 adev->ip_versions[UVD_HWIP][1] = IP_VERSION(2, 5, 0); 3626 break; 3627 case CHIP_ALDEBARAN: 3628 /* This is not fatal. We only need the discovery 3629 * binary for sysfs. We don't need it for a 3630 * functional system. 3631 */ 3632 amdgpu_discovery_init(adev); 3633 aldebaran_reg_base_init(adev); 3634 adev->sdma.num_instances = 5; 3635 adev->sdma.sdma_mask = 0x1f; 3636 adev->vcn.num_vcn_inst = 2; 3637 adev->gmc.num_umc = 4; 3638 adev->gfx.xcc_mask = 1; 3639 adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 4, 2); 3640 adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 4, 2); 3641 adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 4, 0); 3642 adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 4, 0); 3643 adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 4, 0); 3644 adev->ip_versions[SDMA0_HWIP][1] = IP_VERSION(4, 4, 0); 3645 adev->ip_versions[SDMA0_HWIP][2] = IP_VERSION(4, 4, 0); 3646 adev->ip_versions[SDMA0_HWIP][3] = IP_VERSION(4, 4, 0); 3647 adev->ip_versions[SDMA0_HWIP][4] = IP_VERSION(4, 4, 0); 3648 adev->ip_versions[DF_HWIP][0] = IP_VERSION(3, 6, 2); 3649 adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 4, 4); 3650 adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 7, 0); 3651 adev->ip_versions[MP0_HWIP][0] = IP_VERSION(13, 0, 2); 3652 adev->ip_versions[MP1_HWIP][0] = IP_VERSION(13, 0, 2); 3653 adev->ip_versions[THM_HWIP][0] = IP_VERSION(13, 0, 2); 3654 adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(13, 0, 2); 3655 adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 4, 2); 3656 adev->ip_versions[UVD_HWIP][0] = IP_VERSION(2, 6, 0); 3657 adev->ip_versions[UVD_HWIP][1] = IP_VERSION(2, 6, 0); 3658 adev->ip_versions[XGMI_HWIP][0] = IP_VERSION(6, 1, 0); 3659 break; 3660 case CHIP_CYAN_SKILLFISH: 3661 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) { 3662 r = amdgpu_discovery_reg_base_init(adev); 3663 if (r) 3664 return -EINVAL; 3665 3666 amdgpu_discovery_harvest_ip(adev); 3667 amdgpu_discovery_get_gfx_info(adev); 3668 amdgpu_discovery_get_mall_info(adev); 3669 amdgpu_discovery_get_vcn_info(adev); 3670 } else { 3671 cyan_skillfish_reg_base_init(adev); 3672 adev->sdma.num_instances = 2; 3673 adev->sdma.sdma_mask = 3; 3674 adev->gfx.xcc_mask = 1; 3675 adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(2, 0, 3); 3676 adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(2, 0, 3); 3677 adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(5, 0, 1); 3678 adev->ip_versions[HDP_HWIP][0] = IP_VERSION(5, 0, 1); 3679 adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(5, 0, 1); 3680 adev->ip_versions[SDMA1_HWIP][1] = IP_VERSION(5, 0, 1); 3681 adev->ip_versions[DF_HWIP][0] = IP_VERSION(3, 5, 0); 3682 adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(2, 1, 1); 3683 adev->ip_versions[UMC_HWIP][0] = IP_VERSION(8, 1, 1); 3684 adev->ip_versions[MP0_HWIP][0] = IP_VERSION(11, 0, 8); 3685 adev->ip_versions[MP1_HWIP][0] = IP_VERSION(11, 0, 8); 3686 adev->ip_versions[THM_HWIP][0] = IP_VERSION(11, 0, 1); 3687 adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(11, 0, 8); 3688 adev->ip_versions[GC_HWIP][0] = IP_VERSION(10, 1, 3); 3689 adev->ip_versions[UVD_HWIP][0] = IP_VERSION(2, 0, 3); 3690 } 3691 break; 3692 default: 3693 r = amdgpu_discovery_reg_base_init(adev); 3694 if (r) { 3695 drm_err(&adev->ddev, "discovery failed: %d\n", r); 3696 return r; 3697 } 3698 3699 amdgpu_discovery_harvest_ip(adev); 3700 amdgpu_discovery_get_gfx_info(adev); 3701 amdgpu_discovery_get_mall_info(adev); 3702 amdgpu_discovery_get_vcn_info(adev); 3703 break; 3704 } 3705 3706 amdgpu_discovery_init_soc_config(adev); 3707 amdgpu_discovery_sysfs_init(adev); 3708 3709 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) { 3710 case IP_VERSION(9, 0, 1): 3711 case IP_VERSION(9, 2, 1): 3712 case IP_VERSION(9, 4, 0): 3713 case IP_VERSION(9, 4, 1): 3714 case IP_VERSION(9, 4, 2): 3715 case IP_VERSION(9, 4, 3): 3716 case IP_VERSION(9, 4, 4): 3717 case IP_VERSION(9, 5, 0): 3718 adev->family = AMDGPU_FAMILY_AI; 3719 break; 3720 case IP_VERSION(9, 1, 0): 3721 case IP_VERSION(9, 2, 2): 3722 case IP_VERSION(9, 3, 0): 3723 adev->family = AMDGPU_FAMILY_RV; 3724 break; 3725 case IP_VERSION(10, 1, 10): 3726 case IP_VERSION(10, 1, 1): 3727 case IP_VERSION(10, 1, 2): 3728 case IP_VERSION(10, 1, 3): 3729 case IP_VERSION(10, 1, 4): 3730 case IP_VERSION(10, 3, 0): 3731 case IP_VERSION(10, 3, 2): 3732 case IP_VERSION(10, 3, 4): 3733 case IP_VERSION(10, 3, 5): 3734 adev->family = AMDGPU_FAMILY_NV; 3735 break; 3736 case IP_VERSION(10, 3, 1): 3737 adev->family = AMDGPU_FAMILY_VGH; 3738 adev->apu_flags |= AMD_APU_IS_VANGOGH; 3739 break; 3740 case IP_VERSION(10, 3, 3): 3741 adev->family = AMDGPU_FAMILY_YC; 3742 break; 3743 case IP_VERSION(10, 3, 6): 3744 adev->family = AMDGPU_FAMILY_GC_10_3_6; 3745 break; 3746 case IP_VERSION(10, 3, 7): 3747 adev->family = AMDGPU_FAMILY_GC_10_3_7; 3748 break; 3749 case IP_VERSION(11, 0, 0): 3750 case IP_VERSION(11, 0, 2): 3751 case IP_VERSION(11, 0, 3): 3752 adev->family = AMDGPU_FAMILY_GC_11_0_0; 3753 break; 3754 case IP_VERSION(11, 0, 1): 3755 case IP_VERSION(11, 0, 4): 3756 adev->family = AMDGPU_FAMILY_GC_11_0_1; 3757 break; 3758 case IP_VERSION(11, 5, 0): 3759 case IP_VERSION(11, 5, 1): 3760 case IP_VERSION(11, 5, 2): 3761 case IP_VERSION(11, 5, 3): 3762 case IP_VERSION(11, 5, 4): 3763 case IP_VERSION(11, 5, 6): 3764 adev->family = AMDGPU_FAMILY_GC_11_5_0; 3765 break; 3766 case IP_VERSION(11, 7, 0): 3767 case IP_VERSION(11, 7, 1): 3768 adev->family = AMDGPU_FAMILY_GC_11_5_4; 3769 break; 3770 case IP_VERSION(12, 0, 0): 3771 case IP_VERSION(12, 0, 1): 3772 case IP_VERSION(12, 1, 0): 3773 adev->family = AMDGPU_FAMILY_GC_12_0_0; 3774 break; 3775 default: 3776 return -EINVAL; 3777 } 3778 3779 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) { 3780 case IP_VERSION(9, 1, 0): 3781 case IP_VERSION(9, 2, 2): 3782 case IP_VERSION(9, 3, 0): 3783 case IP_VERSION(10, 1, 3): 3784 case IP_VERSION(10, 1, 4): 3785 case IP_VERSION(10, 3, 1): 3786 case IP_VERSION(10, 3, 3): 3787 case IP_VERSION(10, 3, 6): 3788 case IP_VERSION(10, 3, 7): 3789 case IP_VERSION(11, 0, 1): 3790 case IP_VERSION(11, 0, 4): 3791 case IP_VERSION(11, 5, 0): 3792 case IP_VERSION(11, 5, 1): 3793 case IP_VERSION(11, 5, 2): 3794 case IP_VERSION(11, 5, 3): 3795 case IP_VERSION(11, 5, 4): 3796 case IP_VERSION(11, 5, 6): 3797 case IP_VERSION(11, 7, 0): 3798 case IP_VERSION(11, 7, 1): 3799 adev->flags |= AMD_IS_APU; 3800 break; 3801 default: 3802 break; 3803 } 3804 3805 /* set NBIO version */ 3806 switch (amdgpu_ip_version(adev, NBIO_HWIP, 0)) { 3807 case IP_VERSION(6, 1, 0): 3808 case IP_VERSION(6, 2, 0): 3809 adev->nbio.funcs = &nbio_v6_1_funcs; 3810 adev->nbio.hdp_flush_reg = &nbio_v6_1_hdp_flush_reg; 3811 break; 3812 case IP_VERSION(7, 0, 0): 3813 case IP_VERSION(7, 0, 1): 3814 case IP_VERSION(2, 5, 0): 3815 adev->nbio.funcs = &nbio_v7_0_funcs; 3816 adev->nbio.hdp_flush_reg = &nbio_v7_0_hdp_flush_reg; 3817 break; 3818 case IP_VERSION(7, 4, 0): 3819 case IP_VERSION(7, 4, 1): 3820 case IP_VERSION(7, 4, 4): 3821 adev->nbio.funcs = &nbio_v7_4_funcs; 3822 adev->nbio.hdp_flush_reg = &nbio_v7_4_hdp_flush_reg; 3823 break; 3824 case IP_VERSION(7, 9, 0): 3825 case IP_VERSION(7, 9, 1): 3826 adev->nbio.funcs = &nbio_v7_9_funcs; 3827 adev->nbio.hdp_flush_reg = &nbio_v7_9_hdp_flush_reg; 3828 break; 3829 case IP_VERSION(7, 11, 0): 3830 case IP_VERSION(7, 11, 1): 3831 case IP_VERSION(7, 11, 2): 3832 case IP_VERSION(7, 11, 3): 3833 adev->nbio.funcs = &nbio_v7_11_funcs; 3834 adev->nbio.hdp_flush_reg = &nbio_v7_11_hdp_flush_reg; 3835 break; 3836 case IP_VERSION(7, 2, 0): 3837 case IP_VERSION(7, 2, 1): 3838 case IP_VERSION(7, 3, 0): 3839 case IP_VERSION(7, 5, 0): 3840 case IP_VERSION(7, 5, 1): 3841 adev->nbio.funcs = &nbio_v7_2_funcs; 3842 adev->nbio.hdp_flush_reg = &nbio_v7_2_hdp_flush_reg; 3843 break; 3844 case IP_VERSION(2, 1, 1): 3845 case IP_VERSION(2, 3, 0): 3846 case IP_VERSION(2, 3, 1): 3847 case IP_VERSION(2, 3, 2): 3848 case IP_VERSION(3, 3, 0): 3849 case IP_VERSION(3, 3, 1): 3850 case IP_VERSION(3, 3, 2): 3851 case IP_VERSION(3, 3, 3): 3852 adev->nbio.funcs = &nbio_v2_3_funcs; 3853 adev->nbio.hdp_flush_reg = &nbio_v2_3_hdp_flush_reg; 3854 break; 3855 case IP_VERSION(4, 3, 0): 3856 case IP_VERSION(4, 3, 1): 3857 if (amdgpu_sriov_vf(adev)) 3858 adev->nbio.funcs = &nbio_v4_3_sriov_funcs; 3859 else 3860 adev->nbio.funcs = &nbio_v4_3_funcs; 3861 adev->nbio.hdp_flush_reg = &nbio_v4_3_hdp_flush_reg; 3862 break; 3863 case IP_VERSION(7, 7, 0): 3864 case IP_VERSION(7, 7, 1): 3865 adev->nbio.funcs = &nbio_v7_7_funcs; 3866 adev->nbio.hdp_flush_reg = &nbio_v7_7_hdp_flush_reg; 3867 break; 3868 case IP_VERSION(6, 3, 1): 3869 case IP_VERSION(7, 11, 4): 3870 adev->nbio.funcs = &nbif_v6_3_1_funcs; 3871 adev->nbio.hdp_flush_reg = &nbif_v6_3_1_hdp_flush_reg; 3872 break; 3873 case IP_VERSION(7, 11, 5): 3874 adev->nbio.funcs = &nbio_v7_11_5_funcs; 3875 adev->nbio.hdp_flush_reg = &nbio_v7_11_5_hdp_flush_reg; 3876 break; 3877 case IP_VERSION(6, 3, 2): 3878 adev->nbio.funcs = &nbio_v6_3_2_funcs; 3879 break; 3880 case IP_VERSION(7, 10, 0): 3881 adev->nbio.funcs = &nbif_v7_10_funcs; 3882 adev->nbio.hdp_flush_reg = &nbif_v7_10_hdp_flush_reg; 3883 break; 3884 default: 3885 break; 3886 } 3887 3888 switch (amdgpu_ip_version(adev, HDP_HWIP, 0)) { 3889 case IP_VERSION(4, 0, 0): 3890 case IP_VERSION(4, 0, 1): 3891 case IP_VERSION(4, 1, 0): 3892 case IP_VERSION(4, 1, 1): 3893 case IP_VERSION(4, 1, 2): 3894 case IP_VERSION(4, 2, 0): 3895 case IP_VERSION(4, 2, 1): 3896 case IP_VERSION(4, 4, 0): 3897 case IP_VERSION(4, 4, 2): 3898 case IP_VERSION(4, 4, 5): 3899 adev->hdp.funcs = &hdp_v4_0_funcs; 3900 break; 3901 case IP_VERSION(5, 0, 0): 3902 case IP_VERSION(5, 0, 1): 3903 case IP_VERSION(5, 0, 2): 3904 case IP_VERSION(5, 0, 3): 3905 case IP_VERSION(5, 0, 4): 3906 case IP_VERSION(5, 2, 0): 3907 adev->hdp.funcs = &hdp_v5_0_funcs; 3908 break; 3909 case IP_VERSION(5, 2, 1): 3910 adev->hdp.funcs = &hdp_v5_2_funcs; 3911 break; 3912 case IP_VERSION(6, 0, 0): 3913 case IP_VERSION(6, 0, 1): 3914 case IP_VERSION(6, 1, 0): 3915 case IP_VERSION(6, 1, 1): 3916 case IP_VERSION(6, 4, 0): 3917 adev->hdp.funcs = &hdp_v6_0_funcs; 3918 break; 3919 case IP_VERSION(7, 0, 0): 3920 adev->hdp.funcs = &hdp_v7_0_funcs; 3921 break; 3922 case IP_VERSION(8, 0, 1): 3923 adev->hdp.funcs = &hdp_v8_0_funcs; 3924 break; 3925 default: 3926 break; 3927 } 3928 3929 switch (amdgpu_ip_version(adev, DF_HWIP, 0)) { 3930 case IP_VERSION(3, 6, 0): 3931 case IP_VERSION(3, 6, 1): 3932 case IP_VERSION(3, 6, 2): 3933 adev->df.funcs = &df_v3_6_funcs; 3934 break; 3935 case IP_VERSION(2, 1, 0): 3936 case IP_VERSION(2, 1, 1): 3937 case IP_VERSION(2, 5, 0): 3938 case IP_VERSION(3, 5, 1): 3939 case IP_VERSION(3, 5, 2): 3940 adev->df.funcs = &df_v1_7_funcs; 3941 break; 3942 case IP_VERSION(4, 3, 0): 3943 adev->df.funcs = &df_v4_3_funcs; 3944 break; 3945 case IP_VERSION(4, 6, 2): 3946 adev->df.funcs = &df_v4_6_2_funcs; 3947 break; 3948 case IP_VERSION(4, 15, 0): 3949 case IP_VERSION(4, 15, 1): 3950 case IP_VERSION(5, 0, 2): 3951 adev->df.funcs = &df_v4_15_funcs; 3952 break; 3953 default: 3954 break; 3955 } 3956 3957 switch (amdgpu_ip_version(adev, SMUIO_HWIP, 0)) { 3958 case IP_VERSION(9, 0, 0): 3959 case IP_VERSION(9, 0, 1): 3960 case IP_VERSION(10, 0, 0): 3961 case IP_VERSION(10, 0, 1): 3962 case IP_VERSION(10, 0, 2): 3963 adev->smuio.funcs = &smuio_v9_0_funcs; 3964 break; 3965 case IP_VERSION(11, 0, 0): 3966 case IP_VERSION(11, 0, 2): 3967 case IP_VERSION(11, 0, 3): 3968 case IP_VERSION(11, 0, 4): 3969 case IP_VERSION(11, 0, 7): 3970 case IP_VERSION(11, 0, 8): 3971 adev->smuio.funcs = &smuio_v11_0_funcs; 3972 break; 3973 case IP_VERSION(11, 0, 6): 3974 case IP_VERSION(11, 0, 10): 3975 case IP_VERSION(11, 0, 11): 3976 case IP_VERSION(11, 5, 0): 3977 case IP_VERSION(11, 5, 2): 3978 case IP_VERSION(13, 0, 1): 3979 case IP_VERSION(13, 0, 9): 3980 case IP_VERSION(13, 0, 10): 3981 adev->smuio.funcs = &smuio_v11_0_6_funcs; 3982 break; 3983 case IP_VERSION(13, 0, 2): 3984 adev->smuio.funcs = &smuio_v13_0_funcs; 3985 break; 3986 case IP_VERSION(13, 0, 3): 3987 case IP_VERSION(13, 0, 11): 3988 adev->smuio.funcs = &smuio_v13_0_3_funcs; 3989 if (adev->smuio.funcs->get_pkg_type(adev) == AMDGPU_PKG_TYPE_APU) { 3990 adev->flags |= AMD_IS_APU; 3991 } 3992 break; 3993 case IP_VERSION(13, 0, 6): 3994 case IP_VERSION(13, 0, 8): 3995 case IP_VERSION(14, 0, 0): 3996 case IP_VERSION(14, 0, 1): 3997 adev->smuio.funcs = &smuio_v13_0_6_funcs; 3998 break; 3999 case IP_VERSION(14, 0, 2): 4000 adev->smuio.funcs = &smuio_v14_0_2_funcs; 4001 break; 4002 case IP_VERSION(15, 0, 0): 4003 case IP_VERSION(15, 0, 5): 4004 adev->smuio.funcs = &smuio_v15_0_0_funcs; 4005 break; 4006 case IP_VERSION(15, 0, 3): 4007 adev->smuio.funcs = &smuio_v15_0_3_funcs; 4008 break; 4009 case IP_VERSION(15, 0, 8): 4010 adev->smuio.funcs = &smuio_v15_0_8_funcs; 4011 break; 4012 default: 4013 break; 4014 } 4015 4016 switch (amdgpu_ip_version(adev, LSDMA_HWIP, 0)) { 4017 case IP_VERSION(6, 0, 0): 4018 case IP_VERSION(6, 0, 1): 4019 case IP_VERSION(6, 0, 2): 4020 case IP_VERSION(6, 0, 3): 4021 adev->lsdma.funcs = &lsdma_v6_0_funcs; 4022 break; 4023 case IP_VERSION(7, 0, 0): 4024 case IP_VERSION(7, 0, 1): 4025 adev->lsdma.funcs = &lsdma_v7_0_funcs; 4026 break; 4027 case IP_VERSION(7, 1, 0): 4028 adev->lsdma.funcs = &lsdma_v7_1_funcs; 4029 break; 4030 case IP_VERSION(8, 0, 0): 4031 adev->lsdma.funcs = &lsdma_v8_0_funcs; 4032 break; 4033 default: 4034 break; 4035 } 4036 4037 r = amdgpu_discovery_set_common_ip_blocks(adev); 4038 if (r) 4039 return r; 4040 4041 r = amdgpu_discovery_set_gmc_ip_blocks(adev); 4042 if (r) 4043 return r; 4044 4045 /* For SR-IOV, PSP needs to be initialized before IH */ 4046 if (amdgpu_sriov_vf(adev)) { 4047 r = amdgpu_discovery_set_psp_ip_blocks(adev); 4048 if (r) 4049 return r; 4050 r = amdgpu_discovery_set_ih_ip_blocks(adev); 4051 if (r) 4052 return r; 4053 } else { 4054 r = amdgpu_discovery_set_ih_ip_blocks(adev); 4055 if (r) 4056 return r; 4057 4058 if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)) { 4059 r = amdgpu_discovery_set_psp_ip_blocks(adev); 4060 if (r) 4061 return r; 4062 } 4063 } 4064 4065 if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)) { 4066 r = amdgpu_discovery_set_smu_ip_blocks(adev); 4067 if (r) 4068 return r; 4069 } 4070 4071 r = amdgpu_discovery_set_display_ip_blocks(adev); 4072 if (r) 4073 return r; 4074 4075 r = amdgpu_discovery_set_gc_ip_blocks(adev); 4076 if (r) 4077 return r; 4078 4079 r = amdgpu_discovery_set_sdma_ip_blocks(adev); 4080 if (r) 4081 return r; 4082 4083 r = amdgpu_discovery_set_ras_ip_blocks(adev); 4084 if (r) 4085 return r; 4086 4087 if ((adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT && 4088 !amdgpu_sriov_vf(adev) && 4089 amdgpu_dpm == 1) || 4090 (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO && 4091 amdgpu_dpm == 1)) { 4092 r = amdgpu_discovery_set_smu_ip_blocks(adev); 4093 if (r) 4094 return r; 4095 } 4096 4097 r = amdgpu_discovery_set_mm_ip_blocks(adev); 4098 if (r) 4099 return r; 4100 4101 r = amdgpu_discovery_set_mes_ip_blocks(adev); 4102 if (r) 4103 return r; 4104 4105 r = amdgpu_discovery_set_vpe_ip_blocks(adev); 4106 if (r) 4107 return r; 4108 4109 r = amdgpu_discovery_set_umsch_mm_ip_blocks(adev); 4110 if (r) 4111 return r; 4112 4113 r = amdgpu_discovery_set_isp_ip_blocks(adev); 4114 if (r) 4115 return r; 4116 4117 r = amdgpu_discovery_set_ualink_ip_blocks(adev); 4118 if (r) 4119 return r; 4120 return 0; 4121 } 4122 4123 int amdgpu_discovery_get_gc_major_minor_version(struct amdgpu_device *adev, 4124 uint16_t *major, uint16_t *minor) 4125 { 4126 uint8_t *discovery_bin = adev->discovery.bin; 4127 struct table_info *info; 4128 union gc_info *gc_info; 4129 u16 offset; 4130 4131 if (!discovery_bin) 4132 return -EINVAL; 4133 if (amdgpu_discovery_get_table_info(adev, &info, GC)) 4134 return -EINVAL; 4135 4136 offset = le16_to_cpu(info->offset); 4137 if (!offset) 4138 return -EINVAL; 4139 4140 gc_info = (union gc_info *)(discovery_bin + offset); 4141 4142 if (major) 4143 *major = le16_to_cpu(gc_info->v1.header.version_major); 4144 if (minor) 4145 *minor = le16_to_cpu(gc_info->v1.header.version_minor); 4146 return 0; 4147 } 4148 4149 int amdgpu_discovery_get_die_rev_id(struct amdgpu_device *adev, 4150 uint16_t *die_rev_id) 4151 { 4152 uint8_t *discovery_bin = adev->discovery.bin; 4153 struct ip_discovery_header *ihdr; 4154 struct table_info *info; 4155 u16 offset; 4156 4157 if (!discovery_bin) 4158 return -EINVAL; 4159 if (amdgpu_discovery_get_table_info(adev, &info, IP_DISCOVERY)) 4160 return -EINVAL; 4161 4162 offset = le16_to_cpu(info->offset); 4163 if (!offset) 4164 return -EINVAL; 4165 4166 ihdr = (struct ip_discovery_header *)(discovery_bin + offset); 4167 4168 if (die_rev_id) 4169 *die_rev_id = le16_to_cpu(ihdr->die_info[0].die_id); 4170 return 0; 4171 } 4172