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