xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c (revision 25396684b57f7d16306ca149c545db60b2d08dda)
1 /*
2  * Copyright 2018 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <linux/firmware.h>
25 
26 #include "amdgpu.h"
27 #include "amdgpu_discovery.h"
28 #include "soc15_hw_ip.h"
29 #include "discovery.h"
30 
31 #include "soc15.h"
32 #include "gfx_v9_0.h"
33 #include "gfx_v9_4_3.h"
34 #include "gmc_v9_0.h"
35 #include "df_v1_7.h"
36 #include "df_v3_6.h"
37 #include "df_v4_3.h"
38 #include "nbio_v6_1.h"
39 #include "nbio_v7_0.h"
40 #include "nbio_v7_4.h"
41 #include "nbio_v7_9.h"
42 #include "nbio_v7_11.h"
43 #include "hdp_v4_0.h"
44 #include "vega10_ih.h"
45 #include "vega20_ih.h"
46 #include "sdma_v4_0.h"
47 #include "sdma_v4_4_2.h"
48 #include "uvd_v7_0.h"
49 #include "vce_v4_0.h"
50 #include "vcn_v1_0.h"
51 #include "vcn_v2_5.h"
52 #include "jpeg_v2_5.h"
53 #include "smuio_v9_0.h"
54 #include "gmc_v10_0.h"
55 #include "gmc_v11_0.h"
56 #include "gfxhub_v2_0.h"
57 #include "mmhub_v2_0.h"
58 #include "nbio_v2_3.h"
59 #include "nbio_v4_3.h"
60 #include "nbio_v7_2.h"
61 #include "nbio_v7_7.h"
62 #include "hdp_v5_0.h"
63 #include "hdp_v5_2.h"
64 #include "hdp_v6_0.h"
65 #include "nv.h"
66 #include "soc21.h"
67 #include "navi10_ih.h"
68 #include "ih_v6_0.h"
69 #include "ih_v6_1.h"
70 #include "gfx_v10_0.h"
71 #include "gfx_v11_0.h"
72 #include "sdma_v5_0.h"
73 #include "sdma_v5_2.h"
74 #include "sdma_v6_0.h"
75 #include "lsdma_v6_0.h"
76 #include "vcn_v2_0.h"
77 #include "jpeg_v2_0.h"
78 #include "vcn_v3_0.h"
79 #include "jpeg_v3_0.h"
80 #include "vcn_v4_0.h"
81 #include "jpeg_v4_0.h"
82 #include "vcn_v4_0_3.h"
83 #include "jpeg_v4_0_3.h"
84 #include "vcn_v4_0_5.h"
85 #include "jpeg_v4_0_5.h"
86 #include "amdgpu_vkms.h"
87 #include "mes_v10_1.h"
88 #include "mes_v11_0.h"
89 #include "smuio_v11_0.h"
90 #include "smuio_v11_0_6.h"
91 #include "smuio_v13_0.h"
92 #include "smuio_v13_0_3.h"
93 #include "smuio_v13_0_6.h"
94 
95 #include "amdgpu_vpe.h"
96 
97 #define FIRMWARE_IP_DISCOVERY "amdgpu/ip_discovery.bin"
98 MODULE_FIRMWARE(FIRMWARE_IP_DISCOVERY);
99 
100 #define mmRCC_CONFIG_MEMSIZE	0xde3
101 #define mmMM_INDEX		0x0
102 #define mmMM_INDEX_HI		0x6
103 #define mmMM_DATA		0x1
104 
105 static const char *hw_id_names[HW_ID_MAX] = {
106 	[MP1_HWID]		= "MP1",
107 	[MP2_HWID]		= "MP2",
108 	[THM_HWID]		= "THM",
109 	[SMUIO_HWID]		= "SMUIO",
110 	[FUSE_HWID]		= "FUSE",
111 	[CLKA_HWID]		= "CLKA",
112 	[PWR_HWID]		= "PWR",
113 	[GC_HWID]		= "GC",
114 	[UVD_HWID]		= "UVD",
115 	[AUDIO_AZ_HWID]		= "AUDIO_AZ",
116 	[ACP_HWID]		= "ACP",
117 	[DCI_HWID]		= "DCI",
118 	[DMU_HWID]		= "DMU",
119 	[DCO_HWID]		= "DCO",
120 	[DIO_HWID]		= "DIO",
121 	[XDMA_HWID]		= "XDMA",
122 	[DCEAZ_HWID]		= "DCEAZ",
123 	[DAZ_HWID]		= "DAZ",
124 	[SDPMUX_HWID]		= "SDPMUX",
125 	[NTB_HWID]		= "NTB",
126 	[IOHC_HWID]		= "IOHC",
127 	[L2IMU_HWID]		= "L2IMU",
128 	[VCE_HWID]		= "VCE",
129 	[MMHUB_HWID]		= "MMHUB",
130 	[ATHUB_HWID]		= "ATHUB",
131 	[DBGU_NBIO_HWID]	= "DBGU_NBIO",
132 	[DFX_HWID]		= "DFX",
133 	[DBGU0_HWID]		= "DBGU0",
134 	[DBGU1_HWID]		= "DBGU1",
135 	[OSSSYS_HWID]		= "OSSSYS",
136 	[HDP_HWID]		= "HDP",
137 	[SDMA0_HWID]		= "SDMA0",
138 	[SDMA1_HWID]		= "SDMA1",
139 	[SDMA2_HWID]		= "SDMA2",
140 	[SDMA3_HWID]		= "SDMA3",
141 	[LSDMA_HWID]		= "LSDMA",
142 	[ISP_HWID]		= "ISP",
143 	[DBGU_IO_HWID]		= "DBGU_IO",
144 	[DF_HWID]		= "DF",
145 	[CLKB_HWID]		= "CLKB",
146 	[FCH_HWID]		= "FCH",
147 	[DFX_DAP_HWID]		= "DFX_DAP",
148 	[L1IMU_PCIE_HWID]	= "L1IMU_PCIE",
149 	[L1IMU_NBIF_HWID]	= "L1IMU_NBIF",
150 	[L1IMU_IOAGR_HWID]	= "L1IMU_IOAGR",
151 	[L1IMU3_HWID]		= "L1IMU3",
152 	[L1IMU4_HWID]		= "L1IMU4",
153 	[L1IMU5_HWID]		= "L1IMU5",
154 	[L1IMU6_HWID]		= "L1IMU6",
155 	[L1IMU7_HWID]		= "L1IMU7",
156 	[L1IMU8_HWID]		= "L1IMU8",
157 	[L1IMU9_HWID]		= "L1IMU9",
158 	[L1IMU10_HWID]		= "L1IMU10",
159 	[L1IMU11_HWID]		= "L1IMU11",
160 	[L1IMU12_HWID]		= "L1IMU12",
161 	[L1IMU13_HWID]		= "L1IMU13",
162 	[L1IMU14_HWID]		= "L1IMU14",
163 	[L1IMU15_HWID]		= "L1IMU15",
164 	[WAFLC_HWID]		= "WAFLC",
165 	[FCH_USB_PD_HWID]	= "FCH_USB_PD",
166 	[PCIE_HWID]		= "PCIE",
167 	[PCS_HWID]		= "PCS",
168 	[DDCL_HWID]		= "DDCL",
169 	[SST_HWID]		= "SST",
170 	[IOAGR_HWID]		= "IOAGR",
171 	[NBIF_HWID]		= "NBIF",
172 	[IOAPIC_HWID]		= "IOAPIC",
173 	[SYSTEMHUB_HWID]	= "SYSTEMHUB",
174 	[NTBCCP_HWID]		= "NTBCCP",
175 	[UMC_HWID]		= "UMC",
176 	[SATA_HWID]		= "SATA",
177 	[USB_HWID]		= "USB",
178 	[CCXSEC_HWID]		= "CCXSEC",
179 	[XGMI_HWID]		= "XGMI",
180 	[XGBE_HWID]		= "XGBE",
181 	[MP0_HWID]		= "MP0",
182 	[VPE_HWID]		= "VPE",
183 };
184 
185 static int hw_id_map[MAX_HWIP] = {
186 	[GC_HWIP]	= GC_HWID,
187 	[HDP_HWIP]	= HDP_HWID,
188 	[SDMA0_HWIP]	= SDMA0_HWID,
189 	[SDMA1_HWIP]	= SDMA1_HWID,
190 	[SDMA2_HWIP]    = SDMA2_HWID,
191 	[SDMA3_HWIP]    = SDMA3_HWID,
192 	[LSDMA_HWIP]    = LSDMA_HWID,
193 	[MMHUB_HWIP]	= MMHUB_HWID,
194 	[ATHUB_HWIP]	= ATHUB_HWID,
195 	[NBIO_HWIP]	= NBIF_HWID,
196 	[MP0_HWIP]	= MP0_HWID,
197 	[MP1_HWIP]	= MP1_HWID,
198 	[UVD_HWIP]	= UVD_HWID,
199 	[VCE_HWIP]	= VCE_HWID,
200 	[DF_HWIP]	= DF_HWID,
201 	[DCE_HWIP]	= DMU_HWID,
202 	[OSSSYS_HWIP]	= OSSSYS_HWID,
203 	[SMUIO_HWIP]	= SMUIO_HWID,
204 	[PWR_HWIP]	= PWR_HWID,
205 	[NBIF_HWIP]	= NBIF_HWID,
206 	[THM_HWIP]	= THM_HWID,
207 	[CLK_HWIP]	= CLKA_HWID,
208 	[UMC_HWIP]	= UMC_HWID,
209 	[XGMI_HWIP]	= XGMI_HWID,
210 	[DCI_HWIP]	= DCI_HWID,
211 	[PCIE_HWIP]	= PCIE_HWID,
212 	[VPE_HWIP]	= VPE_HWID,
213 };
214 
215 static int amdgpu_discovery_read_binary_from_sysmem(struct amdgpu_device *adev, uint8_t *binary)
216 {
217 	u64 tmr_offset, tmr_size, pos;
218 	void *discv_regn;
219 	int ret;
220 
221 	ret = amdgpu_acpi_get_tmr_info(adev, &tmr_offset, &tmr_size);
222 	if (ret)
223 		return ret;
224 
225 	pos = tmr_offset + tmr_size - DISCOVERY_TMR_OFFSET;
226 
227 	/* This region is read-only and reserved from system use */
228 	discv_regn = memremap(pos, adev->mman.discovery_tmr_size, MEMREMAP_WC);
229 	if (discv_regn) {
230 		memcpy(binary, discv_regn, adev->mman.discovery_tmr_size);
231 		memunmap(discv_regn);
232 		return 0;
233 	}
234 
235 	return -ENOENT;
236 }
237 
238 static int amdgpu_discovery_read_binary_from_mem(struct amdgpu_device *adev,
239 						 uint8_t *binary)
240 {
241 	uint64_t vram_size = (uint64_t)RREG32(mmRCC_CONFIG_MEMSIZE) << 20;
242 	int ret = 0;
243 
244 	if (vram_size) {
245 		uint64_t pos = vram_size - DISCOVERY_TMR_OFFSET;
246 		amdgpu_device_vram_access(adev, pos, (uint32_t *)binary,
247 					  adev->mman.discovery_tmr_size, false);
248 	} else {
249 		ret = amdgpu_discovery_read_binary_from_sysmem(adev, binary);
250 	}
251 
252 	return ret;
253 }
254 
255 static int amdgpu_discovery_read_binary_from_file(struct amdgpu_device *adev, uint8_t *binary)
256 {
257 	const struct firmware *fw;
258 	const char *fw_name;
259 	int r;
260 
261 	switch (amdgpu_discovery) {
262 	case 2:
263 		fw_name = FIRMWARE_IP_DISCOVERY;
264 		break;
265 	default:
266 		dev_warn(adev->dev, "amdgpu_discovery is not set properly\n");
267 		return -EINVAL;
268 	}
269 
270 	r = request_firmware(&fw, fw_name, adev->dev);
271 	if (r) {
272 		dev_err(adev->dev, "can't load firmware \"%s\"\n",
273 			fw_name);
274 		return r;
275 	}
276 
277 	memcpy((u8 *)binary, (u8 *)fw->data, fw->size);
278 	release_firmware(fw);
279 
280 	return 0;
281 }
282 
283 static uint16_t amdgpu_discovery_calculate_checksum(uint8_t *data, uint32_t size)
284 {
285 	uint16_t checksum = 0;
286 	int i;
287 
288 	for (i = 0; i < size; i++)
289 		checksum += data[i];
290 
291 	return checksum;
292 }
293 
294 static inline bool amdgpu_discovery_verify_checksum(uint8_t *data, uint32_t size,
295 						    uint16_t expected)
296 {
297 	return !!(amdgpu_discovery_calculate_checksum(data, size) == expected);
298 }
299 
300 static inline bool amdgpu_discovery_verify_binary_signature(uint8_t *binary)
301 {
302 	struct binary_header *bhdr;
303 	bhdr = (struct binary_header *)binary;
304 
305 	return (le32_to_cpu(bhdr->binary_signature) == BINARY_SIGNATURE);
306 }
307 
308 static void amdgpu_discovery_harvest_config_quirk(struct amdgpu_device *adev)
309 {
310 	/*
311 	 * So far, apply this quirk only on those Navy Flounder boards which
312 	 * have a bad harvest table of VCN config.
313 	 */
314 	if ((amdgpu_ip_version(adev, UVD_HWIP, 1) == IP_VERSION(3, 0, 1)) &&
315 	    (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(10, 3, 2))) {
316 		switch (adev->pdev->revision) {
317 		case 0xC1:
318 		case 0xC2:
319 		case 0xC3:
320 		case 0xC5:
321 		case 0xC7:
322 		case 0xCF:
323 		case 0xDF:
324 			adev->vcn.harvest_config |= AMDGPU_VCN_HARVEST_VCN1;
325 			adev->vcn.inst_mask &= ~AMDGPU_VCN_HARVEST_VCN1;
326 			break;
327 		default:
328 			break;
329 		}
330 	}
331 }
332 
333 static int amdgpu_discovery_init(struct amdgpu_device *adev)
334 {
335 	struct table_info *info;
336 	struct binary_header *bhdr;
337 	uint16_t offset;
338 	uint16_t size;
339 	uint16_t checksum;
340 	int r;
341 
342 	adev->mman.discovery_tmr_size = DISCOVERY_TMR_SIZE;
343 	adev->mman.discovery_bin = kzalloc(adev->mman.discovery_tmr_size, GFP_KERNEL);
344 	if (!adev->mman.discovery_bin)
345 		return -ENOMEM;
346 
347 	/* Read from file if it is the preferred option */
348 	if (amdgpu_discovery == 2) {
349 		dev_info(adev->dev, "use ip discovery information from file");
350 		r = amdgpu_discovery_read_binary_from_file(adev, adev->mman.discovery_bin);
351 
352 		if (r) {
353 			dev_err(adev->dev, "failed to read ip discovery binary from file\n");
354 			r = -EINVAL;
355 			goto out;
356 		}
357 
358 	} else {
359 		r = amdgpu_discovery_read_binary_from_mem(
360 			adev, adev->mman.discovery_bin);
361 		if (r)
362 			goto out;
363 	}
364 
365 	/* check the ip discovery binary signature */
366 	if (!amdgpu_discovery_verify_binary_signature(adev->mman.discovery_bin)) {
367 		dev_err(adev->dev,
368 			"get invalid ip discovery binary signature\n");
369 		r = -EINVAL;
370 		goto out;
371 	}
372 
373 	bhdr = (struct binary_header *)adev->mman.discovery_bin;
374 
375 	offset = offsetof(struct binary_header, binary_checksum) +
376 		sizeof(bhdr->binary_checksum);
377 	size = le16_to_cpu(bhdr->binary_size) - offset;
378 	checksum = le16_to_cpu(bhdr->binary_checksum);
379 
380 	if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
381 					      size, checksum)) {
382 		dev_err(adev->dev, "invalid ip discovery binary checksum\n");
383 		r = -EINVAL;
384 		goto out;
385 	}
386 
387 	info = &bhdr->table_list[IP_DISCOVERY];
388 	offset = le16_to_cpu(info->offset);
389 	checksum = le16_to_cpu(info->checksum);
390 
391 	if (offset) {
392 		struct ip_discovery_header *ihdr =
393 			(struct ip_discovery_header *)(adev->mman.discovery_bin + offset);
394 		if (le32_to_cpu(ihdr->signature) != DISCOVERY_TABLE_SIGNATURE) {
395 			dev_err(adev->dev, "invalid ip discovery data table signature\n");
396 			r = -EINVAL;
397 			goto out;
398 		}
399 
400 		if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
401 						      le16_to_cpu(ihdr->size), checksum)) {
402 			dev_err(adev->dev, "invalid ip discovery data table checksum\n");
403 			r = -EINVAL;
404 			goto out;
405 		}
406 	}
407 
408 	info = &bhdr->table_list[GC];
409 	offset = le16_to_cpu(info->offset);
410 	checksum = le16_to_cpu(info->checksum);
411 
412 	if (offset) {
413 		struct gpu_info_header *ghdr =
414 			(struct gpu_info_header *)(adev->mman.discovery_bin + offset);
415 
416 		if (le32_to_cpu(ghdr->table_id) != GC_TABLE_ID) {
417 			dev_err(adev->dev, "invalid ip discovery gc table id\n");
418 			r = -EINVAL;
419 			goto out;
420 		}
421 
422 		if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
423 						      le32_to_cpu(ghdr->size), checksum)) {
424 			dev_err(adev->dev, "invalid gc data table checksum\n");
425 			r = -EINVAL;
426 			goto out;
427 		}
428 	}
429 
430 	info = &bhdr->table_list[HARVEST_INFO];
431 	offset = le16_to_cpu(info->offset);
432 	checksum = le16_to_cpu(info->checksum);
433 
434 	if (offset) {
435 		struct harvest_info_header *hhdr =
436 			(struct harvest_info_header *)(adev->mman.discovery_bin + offset);
437 
438 		if (le32_to_cpu(hhdr->signature) != HARVEST_TABLE_SIGNATURE) {
439 			dev_err(adev->dev, "invalid ip discovery harvest table signature\n");
440 			r = -EINVAL;
441 			goto out;
442 		}
443 
444 		if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
445 						      sizeof(struct harvest_table), checksum)) {
446 			dev_err(adev->dev, "invalid harvest data table checksum\n");
447 			r = -EINVAL;
448 			goto out;
449 		}
450 	}
451 
452 	info = &bhdr->table_list[VCN_INFO];
453 	offset = le16_to_cpu(info->offset);
454 	checksum = le16_to_cpu(info->checksum);
455 
456 	if (offset) {
457 		struct vcn_info_header *vhdr =
458 			(struct vcn_info_header *)(adev->mman.discovery_bin + offset);
459 
460 		if (le32_to_cpu(vhdr->table_id) != VCN_INFO_TABLE_ID) {
461 			dev_err(adev->dev, "invalid ip discovery vcn table id\n");
462 			r = -EINVAL;
463 			goto out;
464 		}
465 
466 		if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
467 						      le32_to_cpu(vhdr->size_bytes), checksum)) {
468 			dev_err(adev->dev, "invalid vcn data table checksum\n");
469 			r = -EINVAL;
470 			goto out;
471 		}
472 	}
473 
474 	info = &bhdr->table_list[MALL_INFO];
475 	offset = le16_to_cpu(info->offset);
476 	checksum = le16_to_cpu(info->checksum);
477 
478 	if (0 && offset) {
479 		struct mall_info_header *mhdr =
480 			(struct mall_info_header *)(adev->mman.discovery_bin + offset);
481 
482 		if (le32_to_cpu(mhdr->table_id) != MALL_INFO_TABLE_ID) {
483 			dev_err(adev->dev, "invalid ip discovery mall table id\n");
484 			r = -EINVAL;
485 			goto out;
486 		}
487 
488 		if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
489 						      le32_to_cpu(mhdr->size_bytes), checksum)) {
490 			dev_err(adev->dev, "invalid mall data table checksum\n");
491 			r = -EINVAL;
492 			goto out;
493 		}
494 	}
495 
496 	return 0;
497 
498 out:
499 	kfree(adev->mman.discovery_bin);
500 	adev->mman.discovery_bin = NULL;
501 
502 	return r;
503 }
504 
505 static void amdgpu_discovery_sysfs_fini(struct amdgpu_device *adev);
506 
507 void amdgpu_discovery_fini(struct amdgpu_device *adev)
508 {
509 	amdgpu_discovery_sysfs_fini(adev);
510 	kfree(adev->mman.discovery_bin);
511 	adev->mman.discovery_bin = NULL;
512 }
513 
514 static int amdgpu_discovery_validate_ip(const struct ip_v4 *ip)
515 {
516 	if (ip->instance_number >= HWIP_MAX_INSTANCE) {
517 		DRM_ERROR("Unexpected instance_number (%d) from ip discovery blob\n",
518 			  ip->instance_number);
519 		return -EINVAL;
520 	}
521 	if (le16_to_cpu(ip->hw_id) >= HW_ID_MAX) {
522 		DRM_ERROR("Unexpected hw_id (%d) from ip discovery blob\n",
523 			  le16_to_cpu(ip->hw_id));
524 		return -EINVAL;
525 	}
526 
527 	return 0;
528 }
529 
530 static void amdgpu_discovery_read_harvest_bit_per_ip(struct amdgpu_device *adev,
531 						uint32_t *vcn_harvest_count)
532 {
533 	struct binary_header *bhdr;
534 	struct ip_discovery_header *ihdr;
535 	struct die_header *dhdr;
536 	struct ip_v4 *ip;
537 	uint16_t die_offset, ip_offset, num_dies, num_ips;
538 	int i, j;
539 
540 	bhdr = (struct binary_header *)adev->mman.discovery_bin;
541 	ihdr = (struct ip_discovery_header *)(adev->mman.discovery_bin +
542 			le16_to_cpu(bhdr->table_list[IP_DISCOVERY].offset));
543 	num_dies = le16_to_cpu(ihdr->num_dies);
544 
545 	/* scan harvest bit of all IP data structures */
546 	for (i = 0; i < num_dies; i++) {
547 		die_offset = le16_to_cpu(ihdr->die_info[i].die_offset);
548 		dhdr = (struct die_header *)(adev->mman.discovery_bin + die_offset);
549 		num_ips = le16_to_cpu(dhdr->num_ips);
550 		ip_offset = die_offset + sizeof(*dhdr);
551 
552 		for (j = 0; j < num_ips; j++) {
553 			ip = (struct ip_v4 *)(adev->mman.discovery_bin + ip_offset);
554 
555 			if (amdgpu_discovery_validate_ip(ip))
556 				goto next_ip;
557 
558 			if (le16_to_cpu(ip->variant) == 1) {
559 				switch (le16_to_cpu(ip->hw_id)) {
560 				case VCN_HWID:
561 					(*vcn_harvest_count)++;
562 					if (ip->instance_number == 0) {
563 						adev->vcn.harvest_config |= AMDGPU_VCN_HARVEST_VCN0;
564 						adev->vcn.inst_mask &=
565 							~AMDGPU_VCN_HARVEST_VCN0;
566 						adev->jpeg.inst_mask &=
567 							~AMDGPU_VCN_HARVEST_VCN0;
568 					} else {
569 						adev->vcn.harvest_config |= AMDGPU_VCN_HARVEST_VCN1;
570 						adev->vcn.inst_mask &=
571 							~AMDGPU_VCN_HARVEST_VCN1;
572 						adev->jpeg.inst_mask &=
573 							~AMDGPU_VCN_HARVEST_VCN1;
574 					}
575 					break;
576 				case DMU_HWID:
577 					adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK;
578 					break;
579 				default:
580 					break;
581 				}
582 			}
583 next_ip:
584 			if (ihdr->base_addr_64_bit)
585 				ip_offset += struct_size(ip, base_address_64, ip->num_base_address);
586 			else
587 				ip_offset += struct_size(ip, base_address, ip->num_base_address);
588 		}
589 	}
590 }
591 
592 static void amdgpu_discovery_read_from_harvest_table(struct amdgpu_device *adev,
593 						     uint32_t *vcn_harvest_count,
594 						     uint32_t *umc_harvest_count)
595 {
596 	struct binary_header *bhdr;
597 	struct harvest_table *harvest_info;
598 	u16 offset;
599 	int i;
600 	uint32_t umc_harvest_config = 0;
601 
602 	bhdr = (struct binary_header *)adev->mman.discovery_bin;
603 	offset = le16_to_cpu(bhdr->table_list[HARVEST_INFO].offset);
604 
605 	if (!offset) {
606 		dev_err(adev->dev, "invalid harvest table offset\n");
607 		return;
608 	}
609 
610 	harvest_info = (struct harvest_table *)(adev->mman.discovery_bin + offset);
611 
612 	for (i = 0; i < 32; i++) {
613 		if (le16_to_cpu(harvest_info->list[i].hw_id) == 0)
614 			break;
615 
616 		switch (le16_to_cpu(harvest_info->list[i].hw_id)) {
617 		case VCN_HWID:
618 			(*vcn_harvest_count)++;
619 			adev->vcn.harvest_config |=
620 				(1 << harvest_info->list[i].number_instance);
621 			adev->jpeg.harvest_config |=
622 				(1 << harvest_info->list[i].number_instance);
623 
624 			adev->vcn.inst_mask &=
625 				~(1U << harvest_info->list[i].number_instance);
626 			adev->jpeg.inst_mask &=
627 				~(1U << harvest_info->list[i].number_instance);
628 			break;
629 		case DMU_HWID:
630 			adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK;
631 			break;
632 		case UMC_HWID:
633 			umc_harvest_config |=
634 				1 << (le16_to_cpu(harvest_info->list[i].number_instance));
635 			(*umc_harvest_count)++;
636 			break;
637 		case GC_HWID:
638 			adev->gfx.xcc_mask &=
639 				~(1U << harvest_info->list[i].number_instance);
640 			break;
641 		case SDMA0_HWID:
642 			adev->sdma.sdma_mask &=
643 				~(1U << harvest_info->list[i].number_instance);
644 			break;
645 		default:
646 			break;
647 		}
648 	}
649 
650 	adev->umc.active_mask = ((1 << adev->umc.node_inst_num) - 1) &
651 				~umc_harvest_config;
652 }
653 
654 /* ================================================== */
655 
656 struct ip_hw_instance {
657 	struct kobject kobj; /* ip_discovery/die/#die/#hw_id/#instance/<attrs...> */
658 
659 	int hw_id;
660 	u8  num_instance;
661 	u8  major, minor, revision;
662 	u8  harvest;
663 
664 	int num_base_addresses;
665 	u32 base_addr[];
666 };
667 
668 struct ip_hw_id {
669 	struct kset hw_id_kset;  /* ip_discovery/die/#die/#hw_id/, contains ip_hw_instance */
670 	int hw_id;
671 };
672 
673 struct ip_die_entry {
674 	struct kset ip_kset;     /* ip_discovery/die/#die/, contains ip_hw_id  */
675 	u16 num_ips;
676 };
677 
678 /* -------------------------------------------------- */
679 
680 struct ip_hw_instance_attr {
681 	struct attribute attr;
682 	ssize_t (*show)(struct ip_hw_instance *ip_hw_instance, char *buf);
683 };
684 
685 static ssize_t hw_id_show(struct ip_hw_instance *ip_hw_instance, char *buf)
686 {
687 	return sysfs_emit(buf, "%d\n", ip_hw_instance->hw_id);
688 }
689 
690 static ssize_t num_instance_show(struct ip_hw_instance *ip_hw_instance, char *buf)
691 {
692 	return sysfs_emit(buf, "%d\n", ip_hw_instance->num_instance);
693 }
694 
695 static ssize_t major_show(struct ip_hw_instance *ip_hw_instance, char *buf)
696 {
697 	return sysfs_emit(buf, "%d\n", ip_hw_instance->major);
698 }
699 
700 static ssize_t minor_show(struct ip_hw_instance *ip_hw_instance, char *buf)
701 {
702 	return sysfs_emit(buf, "%d\n", ip_hw_instance->minor);
703 }
704 
705 static ssize_t revision_show(struct ip_hw_instance *ip_hw_instance, char *buf)
706 {
707 	return sysfs_emit(buf, "%d\n", ip_hw_instance->revision);
708 }
709 
710 static ssize_t harvest_show(struct ip_hw_instance *ip_hw_instance, char *buf)
711 {
712 	return sysfs_emit(buf, "0x%01X\n", ip_hw_instance->harvest);
713 }
714 
715 static ssize_t num_base_addresses_show(struct ip_hw_instance *ip_hw_instance, char *buf)
716 {
717 	return sysfs_emit(buf, "%d\n", ip_hw_instance->num_base_addresses);
718 }
719 
720 static ssize_t base_addr_show(struct ip_hw_instance *ip_hw_instance, char *buf)
721 {
722 	ssize_t res, at;
723 	int ii;
724 
725 	for (res = at = ii = 0; ii < ip_hw_instance->num_base_addresses; ii++) {
726 		/* Here we satisfy the condition that, at + size <= PAGE_SIZE.
727 		 */
728 		if (at + 12 > PAGE_SIZE)
729 			break;
730 		res = sysfs_emit_at(buf, at, "0x%08X\n",
731 				    ip_hw_instance->base_addr[ii]);
732 		if (res <= 0)
733 			break;
734 		at += res;
735 	}
736 
737 	return res < 0 ? res : at;
738 }
739 
740 static struct ip_hw_instance_attr ip_hw_attr[] = {
741 	__ATTR_RO(hw_id),
742 	__ATTR_RO(num_instance),
743 	__ATTR_RO(major),
744 	__ATTR_RO(minor),
745 	__ATTR_RO(revision),
746 	__ATTR_RO(harvest),
747 	__ATTR_RO(num_base_addresses),
748 	__ATTR_RO(base_addr),
749 };
750 
751 static struct attribute *ip_hw_instance_attrs[ARRAY_SIZE(ip_hw_attr) + 1];
752 ATTRIBUTE_GROUPS(ip_hw_instance);
753 
754 #define to_ip_hw_instance(x) container_of(x, struct ip_hw_instance, kobj)
755 #define to_ip_hw_instance_attr(x) container_of(x, struct ip_hw_instance_attr, attr)
756 
757 static ssize_t ip_hw_instance_attr_show(struct kobject *kobj,
758 					struct attribute *attr,
759 					char *buf)
760 {
761 	struct ip_hw_instance *ip_hw_instance = to_ip_hw_instance(kobj);
762 	struct ip_hw_instance_attr *ip_hw_attr = to_ip_hw_instance_attr(attr);
763 
764 	if (!ip_hw_attr->show)
765 		return -EIO;
766 
767 	return ip_hw_attr->show(ip_hw_instance, buf);
768 }
769 
770 static const struct sysfs_ops ip_hw_instance_sysfs_ops = {
771 	.show = ip_hw_instance_attr_show,
772 };
773 
774 static void ip_hw_instance_release(struct kobject *kobj)
775 {
776 	struct ip_hw_instance *ip_hw_instance = to_ip_hw_instance(kobj);
777 
778 	kfree(ip_hw_instance);
779 }
780 
781 static const struct kobj_type ip_hw_instance_ktype = {
782 	.release = ip_hw_instance_release,
783 	.sysfs_ops = &ip_hw_instance_sysfs_ops,
784 	.default_groups = ip_hw_instance_groups,
785 };
786 
787 /* -------------------------------------------------- */
788 
789 #define to_ip_hw_id(x)  container_of(to_kset(x), struct ip_hw_id, hw_id_kset)
790 
791 static void ip_hw_id_release(struct kobject *kobj)
792 {
793 	struct ip_hw_id *ip_hw_id = to_ip_hw_id(kobj);
794 
795 	if (!list_empty(&ip_hw_id->hw_id_kset.list))
796 		DRM_ERROR("ip_hw_id->hw_id_kset is not empty");
797 	kfree(ip_hw_id);
798 }
799 
800 static const struct kobj_type ip_hw_id_ktype = {
801 	.release = ip_hw_id_release,
802 	.sysfs_ops = &kobj_sysfs_ops,
803 };
804 
805 /* -------------------------------------------------- */
806 
807 static void die_kobj_release(struct kobject *kobj);
808 static void ip_disc_release(struct kobject *kobj);
809 
810 struct ip_die_entry_attribute {
811 	struct attribute attr;
812 	ssize_t (*show)(struct ip_die_entry *ip_die_entry, char *buf);
813 };
814 
815 #define to_ip_die_entry_attr(x)  container_of(x, struct ip_die_entry_attribute, attr)
816 
817 static ssize_t num_ips_show(struct ip_die_entry *ip_die_entry, char *buf)
818 {
819 	return sysfs_emit(buf, "%d\n", ip_die_entry->num_ips);
820 }
821 
822 /* If there are more ip_die_entry attrs, other than the number of IPs,
823  * we can make this intro an array of attrs, and then initialize
824  * ip_die_entry_attrs in a loop.
825  */
826 static struct ip_die_entry_attribute num_ips_attr =
827 	__ATTR_RO(num_ips);
828 
829 static struct attribute *ip_die_entry_attrs[] = {
830 	&num_ips_attr.attr,
831 	NULL,
832 };
833 ATTRIBUTE_GROUPS(ip_die_entry); /* ip_die_entry_groups */
834 
835 #define to_ip_die_entry(x) container_of(to_kset(x), struct ip_die_entry, ip_kset)
836 
837 static ssize_t ip_die_entry_attr_show(struct kobject *kobj,
838 				      struct attribute *attr,
839 				      char *buf)
840 {
841 	struct ip_die_entry_attribute *ip_die_entry_attr = to_ip_die_entry_attr(attr);
842 	struct ip_die_entry *ip_die_entry = to_ip_die_entry(kobj);
843 
844 	if (!ip_die_entry_attr->show)
845 		return -EIO;
846 
847 	return ip_die_entry_attr->show(ip_die_entry, buf);
848 }
849 
850 static void ip_die_entry_release(struct kobject *kobj)
851 {
852 	struct ip_die_entry *ip_die_entry = to_ip_die_entry(kobj);
853 
854 	if (!list_empty(&ip_die_entry->ip_kset.list))
855 		DRM_ERROR("ip_die_entry->ip_kset is not empty");
856 	kfree(ip_die_entry);
857 }
858 
859 static const struct sysfs_ops ip_die_entry_sysfs_ops = {
860 	.show = ip_die_entry_attr_show,
861 };
862 
863 static const struct kobj_type ip_die_entry_ktype = {
864 	.release = ip_die_entry_release,
865 	.sysfs_ops = &ip_die_entry_sysfs_ops,
866 	.default_groups = ip_die_entry_groups,
867 };
868 
869 static const struct kobj_type die_kobj_ktype = {
870 	.release = die_kobj_release,
871 	.sysfs_ops = &kobj_sysfs_ops,
872 };
873 
874 static const struct kobj_type ip_discovery_ktype = {
875 	.release = ip_disc_release,
876 	.sysfs_ops = &kobj_sysfs_ops,
877 };
878 
879 struct ip_discovery_top {
880 	struct kobject kobj;    /* ip_discovery/ */
881 	struct kset die_kset;   /* ip_discovery/die/, contains ip_die_entry */
882 	struct amdgpu_device *adev;
883 };
884 
885 static void die_kobj_release(struct kobject *kobj)
886 {
887 	struct ip_discovery_top *ip_top = container_of(to_kset(kobj),
888 						       struct ip_discovery_top,
889 						       die_kset);
890 	if (!list_empty(&ip_top->die_kset.list))
891 		DRM_ERROR("ip_top->die_kset is not empty");
892 }
893 
894 static void ip_disc_release(struct kobject *kobj)
895 {
896 	struct ip_discovery_top *ip_top = container_of(kobj, struct ip_discovery_top,
897 						       kobj);
898 	struct amdgpu_device *adev = ip_top->adev;
899 
900 	adev->ip_top = NULL;
901 	kfree(ip_top);
902 }
903 
904 static uint8_t amdgpu_discovery_get_harvest_info(struct amdgpu_device *adev,
905 						 uint16_t hw_id, uint8_t inst)
906 {
907 	uint8_t harvest = 0;
908 
909 	/* Until a uniform way is figured, get mask based on hwid */
910 	switch (hw_id) {
911 	case VCN_HWID:
912 		harvest = ((1 << inst) & adev->vcn.inst_mask) == 0;
913 		break;
914 	case DMU_HWID:
915 		if (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK)
916 			harvest = 0x1;
917 		break;
918 	case UMC_HWID:
919 		/* TODO: It needs another parsing; for now, ignore.*/
920 		break;
921 	case GC_HWID:
922 		harvest = ((1 << inst) & adev->gfx.xcc_mask) == 0;
923 		break;
924 	case SDMA0_HWID:
925 		harvest = ((1 << inst) & adev->sdma.sdma_mask) == 0;
926 		break;
927 	default:
928 		break;
929 	}
930 
931 	return harvest;
932 }
933 
934 static int amdgpu_discovery_sysfs_ips(struct amdgpu_device *adev,
935 				      struct ip_die_entry *ip_die_entry,
936 				      const size_t _ip_offset, const int num_ips,
937 				      bool reg_base_64)
938 {
939 	int ii, jj, kk, res;
940 
941 	DRM_DEBUG("num_ips:%d", num_ips);
942 
943 	/* Find all IPs of a given HW ID, and add their instance to
944 	 * #die/#hw_id/#instance/<attributes>
945 	 */
946 	for (ii = 0; ii < HW_ID_MAX; ii++) {
947 		struct ip_hw_id *ip_hw_id = NULL;
948 		size_t ip_offset = _ip_offset;
949 
950 		for (jj = 0; jj < num_ips; jj++) {
951 			struct ip_v4 *ip;
952 			struct ip_hw_instance *ip_hw_instance;
953 
954 			ip = (struct ip_v4 *)(adev->mman.discovery_bin + ip_offset);
955 			if (amdgpu_discovery_validate_ip(ip) ||
956 			    le16_to_cpu(ip->hw_id) != ii)
957 				goto next_ip;
958 
959 			DRM_DEBUG("match:%d @ ip_offset:%zu", ii, ip_offset);
960 
961 			/* We have a hw_id match; register the hw
962 			 * block if not yet registered.
963 			 */
964 			if (!ip_hw_id) {
965 				ip_hw_id = kzalloc(sizeof(*ip_hw_id), GFP_KERNEL);
966 				if (!ip_hw_id)
967 					return -ENOMEM;
968 				ip_hw_id->hw_id = ii;
969 
970 				kobject_set_name(&ip_hw_id->hw_id_kset.kobj, "%d", ii);
971 				ip_hw_id->hw_id_kset.kobj.kset = &ip_die_entry->ip_kset;
972 				ip_hw_id->hw_id_kset.kobj.ktype = &ip_hw_id_ktype;
973 				res = kset_register(&ip_hw_id->hw_id_kset);
974 				if (res) {
975 					DRM_ERROR("Couldn't register ip_hw_id kset");
976 					kfree(ip_hw_id);
977 					return res;
978 				}
979 				if (hw_id_names[ii]) {
980 					res = sysfs_create_link(&ip_die_entry->ip_kset.kobj,
981 								&ip_hw_id->hw_id_kset.kobj,
982 								hw_id_names[ii]);
983 					if (res) {
984 						DRM_ERROR("Couldn't create IP link %s in IP Die:%s\n",
985 							  hw_id_names[ii],
986 							  kobject_name(&ip_die_entry->ip_kset.kobj));
987 					}
988 				}
989 			}
990 
991 			/* Now register its instance.
992 			 */
993 			ip_hw_instance = kzalloc(struct_size(ip_hw_instance,
994 							     base_addr,
995 							     ip->num_base_address),
996 						 GFP_KERNEL);
997 			if (!ip_hw_instance) {
998 				DRM_ERROR("no memory for ip_hw_instance");
999 				return -ENOMEM;
1000 			}
1001 			ip_hw_instance->hw_id = le16_to_cpu(ip->hw_id); /* == ii */
1002 			ip_hw_instance->num_instance = ip->instance_number;
1003 			ip_hw_instance->major = ip->major;
1004 			ip_hw_instance->minor = ip->minor;
1005 			ip_hw_instance->revision = ip->revision;
1006 			ip_hw_instance->harvest =
1007 				amdgpu_discovery_get_harvest_info(
1008 					adev, ip_hw_instance->hw_id,
1009 					ip_hw_instance->num_instance);
1010 			ip_hw_instance->num_base_addresses = ip->num_base_address;
1011 
1012 			for (kk = 0; kk < ip_hw_instance->num_base_addresses; kk++) {
1013 				if (reg_base_64)
1014 					ip_hw_instance->base_addr[kk] =
1015 						lower_32_bits(le64_to_cpu(ip->base_address_64[kk])) & 0x3FFFFFFF;
1016 				else
1017 					ip_hw_instance->base_addr[kk] = ip->base_address[kk];
1018 			}
1019 
1020 			kobject_init(&ip_hw_instance->kobj, &ip_hw_instance_ktype);
1021 			ip_hw_instance->kobj.kset = &ip_hw_id->hw_id_kset;
1022 			res = kobject_add(&ip_hw_instance->kobj, NULL,
1023 					  "%d", ip_hw_instance->num_instance);
1024 next_ip:
1025 			if (reg_base_64)
1026 				ip_offset += struct_size(ip, base_address_64,
1027 							 ip->num_base_address);
1028 			else
1029 				ip_offset += struct_size(ip, base_address,
1030 							 ip->num_base_address);
1031 		}
1032 	}
1033 
1034 	return 0;
1035 }
1036 
1037 static int amdgpu_discovery_sysfs_recurse(struct amdgpu_device *adev)
1038 {
1039 	struct binary_header *bhdr;
1040 	struct ip_discovery_header *ihdr;
1041 	struct die_header *dhdr;
1042 	struct kset *die_kset = &adev->ip_top->die_kset;
1043 	u16 num_dies, die_offset, num_ips;
1044 	size_t ip_offset;
1045 	int ii, res;
1046 
1047 	bhdr = (struct binary_header *)adev->mman.discovery_bin;
1048 	ihdr = (struct ip_discovery_header *)(adev->mman.discovery_bin +
1049 					      le16_to_cpu(bhdr->table_list[IP_DISCOVERY].offset));
1050 	num_dies = le16_to_cpu(ihdr->num_dies);
1051 
1052 	DRM_DEBUG("number of dies: %d\n", num_dies);
1053 
1054 	for (ii = 0; ii < num_dies; ii++) {
1055 		struct ip_die_entry *ip_die_entry;
1056 
1057 		die_offset = le16_to_cpu(ihdr->die_info[ii].die_offset);
1058 		dhdr = (struct die_header *)(adev->mman.discovery_bin + die_offset);
1059 		num_ips = le16_to_cpu(dhdr->num_ips);
1060 		ip_offset = die_offset + sizeof(*dhdr);
1061 
1062 		/* Add the die to the kset.
1063 		 *
1064 		 * dhdr->die_id == ii, which was checked in
1065 		 * amdgpu_discovery_reg_base_init().
1066 		 */
1067 
1068 		ip_die_entry = kzalloc(sizeof(*ip_die_entry), GFP_KERNEL);
1069 		if (!ip_die_entry)
1070 			return -ENOMEM;
1071 
1072 		ip_die_entry->num_ips = num_ips;
1073 
1074 		kobject_set_name(&ip_die_entry->ip_kset.kobj, "%d", le16_to_cpu(dhdr->die_id));
1075 		ip_die_entry->ip_kset.kobj.kset = die_kset;
1076 		ip_die_entry->ip_kset.kobj.ktype = &ip_die_entry_ktype;
1077 		res = kset_register(&ip_die_entry->ip_kset);
1078 		if (res) {
1079 			DRM_ERROR("Couldn't register ip_die_entry kset");
1080 			kfree(ip_die_entry);
1081 			return res;
1082 		}
1083 
1084 		amdgpu_discovery_sysfs_ips(adev, ip_die_entry, ip_offset, num_ips, !!ihdr->base_addr_64_bit);
1085 	}
1086 
1087 	return 0;
1088 }
1089 
1090 static int amdgpu_discovery_sysfs_init(struct amdgpu_device *adev)
1091 {
1092 	struct kset *die_kset;
1093 	int res, ii;
1094 
1095 	if (!adev->mman.discovery_bin)
1096 		return -EINVAL;
1097 
1098 	adev->ip_top = kzalloc(sizeof(*adev->ip_top), GFP_KERNEL);
1099 	if (!adev->ip_top)
1100 		return -ENOMEM;
1101 
1102 	adev->ip_top->adev = adev;
1103 
1104 	res = kobject_init_and_add(&adev->ip_top->kobj, &ip_discovery_ktype,
1105 				   &adev->dev->kobj, "ip_discovery");
1106 	if (res) {
1107 		DRM_ERROR("Couldn't init and add ip_discovery/");
1108 		goto Err;
1109 	}
1110 
1111 	die_kset = &adev->ip_top->die_kset;
1112 	kobject_set_name(&die_kset->kobj, "%s", "die");
1113 	die_kset->kobj.parent = &adev->ip_top->kobj;
1114 	die_kset->kobj.ktype = &die_kobj_ktype;
1115 	res = kset_register(&adev->ip_top->die_kset);
1116 	if (res) {
1117 		DRM_ERROR("Couldn't register die_kset");
1118 		goto Err;
1119 	}
1120 
1121 	for (ii = 0; ii < ARRAY_SIZE(ip_hw_attr); ii++)
1122 		ip_hw_instance_attrs[ii] = &ip_hw_attr[ii].attr;
1123 	ip_hw_instance_attrs[ii] = NULL;
1124 
1125 	res = amdgpu_discovery_sysfs_recurse(adev);
1126 
1127 	return res;
1128 Err:
1129 	kobject_put(&adev->ip_top->kobj);
1130 	return res;
1131 }
1132 
1133 /* -------------------------------------------------- */
1134 
1135 #define list_to_kobj(el) container_of(el, struct kobject, entry)
1136 
1137 static void amdgpu_discovery_sysfs_ip_hw_free(struct ip_hw_id *ip_hw_id)
1138 {
1139 	struct list_head *el, *tmp;
1140 	struct kset *hw_id_kset;
1141 
1142 	hw_id_kset = &ip_hw_id->hw_id_kset;
1143 	spin_lock(&hw_id_kset->list_lock);
1144 	list_for_each_prev_safe(el, tmp, &hw_id_kset->list) {
1145 		list_del_init(el);
1146 		spin_unlock(&hw_id_kset->list_lock);
1147 		/* kobject is embedded in ip_hw_instance */
1148 		kobject_put(list_to_kobj(el));
1149 		spin_lock(&hw_id_kset->list_lock);
1150 	}
1151 	spin_unlock(&hw_id_kset->list_lock);
1152 	kobject_put(&ip_hw_id->hw_id_kset.kobj);
1153 }
1154 
1155 static void amdgpu_discovery_sysfs_die_free(struct ip_die_entry *ip_die_entry)
1156 {
1157 	struct list_head *el, *tmp;
1158 	struct kset *ip_kset;
1159 
1160 	ip_kset = &ip_die_entry->ip_kset;
1161 	spin_lock(&ip_kset->list_lock);
1162 	list_for_each_prev_safe(el, tmp, &ip_kset->list) {
1163 		list_del_init(el);
1164 		spin_unlock(&ip_kset->list_lock);
1165 		amdgpu_discovery_sysfs_ip_hw_free(to_ip_hw_id(list_to_kobj(el)));
1166 		spin_lock(&ip_kset->list_lock);
1167 	}
1168 	spin_unlock(&ip_kset->list_lock);
1169 	kobject_put(&ip_die_entry->ip_kset.kobj);
1170 }
1171 
1172 static void amdgpu_discovery_sysfs_fini(struct amdgpu_device *adev)
1173 {
1174 	struct list_head *el, *tmp;
1175 	struct kset *die_kset;
1176 
1177 	die_kset = &adev->ip_top->die_kset;
1178 	spin_lock(&die_kset->list_lock);
1179 	list_for_each_prev_safe(el, tmp, &die_kset->list) {
1180 		list_del_init(el);
1181 		spin_unlock(&die_kset->list_lock);
1182 		amdgpu_discovery_sysfs_die_free(to_ip_die_entry(list_to_kobj(el)));
1183 		spin_lock(&die_kset->list_lock);
1184 	}
1185 	spin_unlock(&die_kset->list_lock);
1186 	kobject_put(&adev->ip_top->die_kset.kobj);
1187 	kobject_put(&adev->ip_top->kobj);
1188 }
1189 
1190 /* ================================================== */
1191 
1192 static int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)
1193 {
1194 	struct binary_header *bhdr;
1195 	struct ip_discovery_header *ihdr;
1196 	struct die_header *dhdr;
1197 	struct ip_v4 *ip;
1198 	uint16_t die_offset;
1199 	uint16_t ip_offset;
1200 	uint16_t num_dies;
1201 	uint16_t num_ips;
1202 	uint8_t num_base_address;
1203 	int hw_ip;
1204 	int i, j, k;
1205 	int r;
1206 
1207 	r = amdgpu_discovery_init(adev);
1208 	if (r) {
1209 		DRM_ERROR("amdgpu_discovery_init failed\n");
1210 		return r;
1211 	}
1212 
1213 	adev->gfx.xcc_mask = 0;
1214 	adev->sdma.sdma_mask = 0;
1215 	adev->vcn.inst_mask = 0;
1216 	adev->jpeg.inst_mask = 0;
1217 	bhdr = (struct binary_header *)adev->mman.discovery_bin;
1218 	ihdr = (struct ip_discovery_header *)(adev->mman.discovery_bin +
1219 			le16_to_cpu(bhdr->table_list[IP_DISCOVERY].offset));
1220 	num_dies = le16_to_cpu(ihdr->num_dies);
1221 
1222 	DRM_DEBUG("number of dies: %d\n", num_dies);
1223 
1224 	for (i = 0; i < num_dies; i++) {
1225 		die_offset = le16_to_cpu(ihdr->die_info[i].die_offset);
1226 		dhdr = (struct die_header *)(adev->mman.discovery_bin + die_offset);
1227 		num_ips = le16_to_cpu(dhdr->num_ips);
1228 		ip_offset = die_offset + sizeof(*dhdr);
1229 
1230 		if (le16_to_cpu(dhdr->die_id) != i) {
1231 			DRM_ERROR("invalid die id %d, expected %d\n",
1232 					le16_to_cpu(dhdr->die_id), i);
1233 			return -EINVAL;
1234 		}
1235 
1236 		DRM_DEBUG("number of hardware IPs on die%d: %d\n",
1237 				le16_to_cpu(dhdr->die_id), num_ips);
1238 
1239 		for (j = 0; j < num_ips; j++) {
1240 			ip = (struct ip_v4 *)(adev->mman.discovery_bin + ip_offset);
1241 
1242 			if (amdgpu_discovery_validate_ip(ip))
1243 				goto next_ip;
1244 
1245 			num_base_address = ip->num_base_address;
1246 
1247 			DRM_DEBUG("%s(%d) #%d v%d.%d.%d:\n",
1248 				  hw_id_names[le16_to_cpu(ip->hw_id)],
1249 				  le16_to_cpu(ip->hw_id),
1250 				  ip->instance_number,
1251 				  ip->major, ip->minor,
1252 				  ip->revision);
1253 
1254 			if (le16_to_cpu(ip->hw_id) == VCN_HWID) {
1255 				/* Bit [5:0]: original revision value
1256 				 * Bit [7:6]: en/decode capability:
1257 				 *     0b00 : VCN function normally
1258 				 *     0b10 : encode is disabled
1259 				 *     0b01 : decode is disabled
1260 				 */
1261 				adev->vcn.vcn_config[adev->vcn.num_vcn_inst] =
1262 					ip->revision & 0xc0;
1263 				ip->revision &= ~0xc0;
1264 				if (adev->vcn.num_vcn_inst <
1265 				    AMDGPU_MAX_VCN_INSTANCES) {
1266 					adev->vcn.num_vcn_inst++;
1267 					adev->vcn.inst_mask |=
1268 						(1U << ip->instance_number);
1269 					adev->jpeg.inst_mask |=
1270 						(1U << ip->instance_number);
1271 				} else {
1272 					dev_err(adev->dev, "Too many VCN instances: %d vs %d\n",
1273 						adev->vcn.num_vcn_inst + 1,
1274 						AMDGPU_MAX_VCN_INSTANCES);
1275 				}
1276 			}
1277 			if (le16_to_cpu(ip->hw_id) == SDMA0_HWID ||
1278 			    le16_to_cpu(ip->hw_id) == SDMA1_HWID ||
1279 			    le16_to_cpu(ip->hw_id) == SDMA2_HWID ||
1280 			    le16_to_cpu(ip->hw_id) == SDMA3_HWID) {
1281 				if (adev->sdma.num_instances <
1282 				    AMDGPU_MAX_SDMA_INSTANCES) {
1283 					adev->sdma.num_instances++;
1284 					adev->sdma.sdma_mask |=
1285 						(1U << ip->instance_number);
1286 				} else {
1287 					dev_err(adev->dev, "Too many SDMA instances: %d vs %d\n",
1288 						adev->sdma.num_instances + 1,
1289 						AMDGPU_MAX_SDMA_INSTANCES);
1290 				}
1291 			}
1292 
1293 			if (le16_to_cpu(ip->hw_id) == UMC_HWID) {
1294 				adev->gmc.num_umc++;
1295 				adev->umc.node_inst_num++;
1296 			}
1297 
1298 			if (le16_to_cpu(ip->hw_id) == GC_HWID)
1299 				adev->gfx.xcc_mask |=
1300 					(1U << ip->instance_number);
1301 
1302 			for (k = 0; k < num_base_address; k++) {
1303 				/*
1304 				 * convert the endianness of base addresses in place,
1305 				 * so that we don't need to convert them when accessing adev->reg_offset.
1306 				 */
1307 				if (ihdr->base_addr_64_bit)
1308 					/* Truncate the 64bit base address from ip discovery
1309 					 * and only store lower 32bit ip base in reg_offset[].
1310 					 * Bits > 32 follows ASIC specific format, thus just
1311 					 * discard them and handle it within specific ASIC.
1312 					 * By this way reg_offset[] and related helpers can
1313 					 * stay unchanged.
1314 					 * The base address is in dwords, thus clear the
1315 					 * highest 2 bits to store.
1316 					 */
1317 					ip->base_address[k] =
1318 						lower_32_bits(le64_to_cpu(ip->base_address_64[k])) & 0x3FFFFFFF;
1319 				else
1320 					ip->base_address[k] = le32_to_cpu(ip->base_address[k]);
1321 				DRM_DEBUG("\t0x%08x\n", ip->base_address[k]);
1322 			}
1323 
1324 			for (hw_ip = 0; hw_ip < MAX_HWIP; hw_ip++) {
1325 				if (hw_id_map[hw_ip] == le16_to_cpu(ip->hw_id) &&
1326 				    hw_id_map[hw_ip] != 0) {
1327 					DRM_DEBUG("set register base offset for %s\n",
1328 							hw_id_names[le16_to_cpu(ip->hw_id)]);
1329 					adev->reg_offset[hw_ip][ip->instance_number] =
1330 						ip->base_address;
1331 					/* Instance support is somewhat inconsistent.
1332 					 * SDMA is a good example.  Sienna cichlid has 4 total
1333 					 * SDMA instances, each enumerated separately (HWIDs
1334 					 * 42, 43, 68, 69).  Arcturus has 8 total SDMA instances,
1335 					 * but they are enumerated as multiple instances of the
1336 					 * same HWIDs (4x HWID 42, 4x HWID 43).  UMC is another
1337 					 * example.  On most chips there are multiple instances
1338 					 * with the same HWID.
1339 					 */
1340 					adev->ip_versions[hw_ip][ip->instance_number] =
1341 						IP_VERSION(ip->major, ip->minor, ip->revision);
1342 				}
1343 			}
1344 
1345 next_ip:
1346 			if (ihdr->base_addr_64_bit)
1347 				ip_offset += struct_size(ip, base_address_64, ip->num_base_address);
1348 			else
1349 				ip_offset += struct_size(ip, base_address, ip->num_base_address);
1350 		}
1351 	}
1352 
1353 	return 0;
1354 }
1355 
1356 static void amdgpu_discovery_harvest_ip(struct amdgpu_device *adev)
1357 {
1358 	int vcn_harvest_count = 0;
1359 	int umc_harvest_count = 0;
1360 
1361 	/*
1362 	 * Harvest table does not fit Navi1x and legacy GPUs,
1363 	 * so read harvest bit per IP data structure to set
1364 	 * harvest configuration.
1365 	 */
1366 	if (amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(10, 2, 0) &&
1367 	    amdgpu_ip_version(adev, GC_HWIP, 0) != IP_VERSION(9, 4, 3)) {
1368 		if ((adev->pdev->device == 0x731E &&
1369 			(adev->pdev->revision == 0xC6 ||
1370 			 adev->pdev->revision == 0xC7)) ||
1371 			(adev->pdev->device == 0x7340 &&
1372 			 adev->pdev->revision == 0xC9) ||
1373 			(adev->pdev->device == 0x7360 &&
1374 			 adev->pdev->revision == 0xC7))
1375 			amdgpu_discovery_read_harvest_bit_per_ip(adev,
1376 				&vcn_harvest_count);
1377 	} else {
1378 		amdgpu_discovery_read_from_harvest_table(adev,
1379 							 &vcn_harvest_count,
1380 							 &umc_harvest_count);
1381 	}
1382 
1383 	amdgpu_discovery_harvest_config_quirk(adev);
1384 
1385 	if (vcn_harvest_count == adev->vcn.num_vcn_inst) {
1386 		adev->harvest_ip_mask |= AMD_HARVEST_IP_VCN_MASK;
1387 		adev->harvest_ip_mask |= AMD_HARVEST_IP_JPEG_MASK;
1388 	}
1389 
1390 	if (umc_harvest_count < adev->gmc.num_umc) {
1391 		adev->gmc.num_umc -= umc_harvest_count;
1392 	}
1393 }
1394 
1395 union gc_info {
1396 	struct gc_info_v1_0 v1;
1397 	struct gc_info_v1_1 v1_1;
1398 	struct gc_info_v1_2 v1_2;
1399 	struct gc_info_v2_0 v2;
1400 	struct gc_info_v2_1 v2_1;
1401 };
1402 
1403 static int amdgpu_discovery_get_gfx_info(struct amdgpu_device *adev)
1404 {
1405 	struct binary_header *bhdr;
1406 	union gc_info *gc_info;
1407 	u16 offset;
1408 
1409 	if (!adev->mman.discovery_bin) {
1410 		DRM_ERROR("ip discovery uninitialized\n");
1411 		return -EINVAL;
1412 	}
1413 
1414 	bhdr = (struct binary_header *)adev->mman.discovery_bin;
1415 	offset = le16_to_cpu(bhdr->table_list[GC].offset);
1416 
1417 	if (!offset)
1418 		return 0;
1419 
1420 	gc_info = (union gc_info *)(adev->mman.discovery_bin + offset);
1421 
1422 	switch (le16_to_cpu(gc_info->v1.header.version_major)) {
1423 	case 1:
1424 		adev->gfx.config.max_shader_engines = le32_to_cpu(gc_info->v1.gc_num_se);
1425 		adev->gfx.config.max_cu_per_sh = 2 * (le32_to_cpu(gc_info->v1.gc_num_wgp0_per_sa) +
1426 						      le32_to_cpu(gc_info->v1.gc_num_wgp1_per_sa));
1427 		adev->gfx.config.max_sh_per_se = le32_to_cpu(gc_info->v1.gc_num_sa_per_se);
1428 		adev->gfx.config.max_backends_per_se = le32_to_cpu(gc_info->v1.gc_num_rb_per_se);
1429 		adev->gfx.config.max_texture_channel_caches = le32_to_cpu(gc_info->v1.gc_num_gl2c);
1430 		adev->gfx.config.max_gprs = le32_to_cpu(gc_info->v1.gc_num_gprs);
1431 		adev->gfx.config.max_gs_threads = le32_to_cpu(gc_info->v1.gc_num_max_gs_thds);
1432 		adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gc_info->v1.gc_gs_table_depth);
1433 		adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gc_info->v1.gc_gsprim_buff_depth);
1434 		adev->gfx.config.double_offchip_lds_buf = le32_to_cpu(gc_info->v1.gc_double_offchip_lds_buffer);
1435 		adev->gfx.cu_info.wave_front_size = le32_to_cpu(gc_info->v1.gc_wave_size);
1436 		adev->gfx.cu_info.max_waves_per_simd = le32_to_cpu(gc_info->v1.gc_max_waves_per_simd);
1437 		adev->gfx.cu_info.max_scratch_slots_per_cu = le32_to_cpu(gc_info->v1.gc_max_scratch_slots_per_cu);
1438 		adev->gfx.cu_info.lds_size = le32_to_cpu(gc_info->v1.gc_lds_size);
1439 		adev->gfx.config.num_sc_per_sh = le32_to_cpu(gc_info->v1.gc_num_sc_per_se) /
1440 			le32_to_cpu(gc_info->v1.gc_num_sa_per_se);
1441 		adev->gfx.config.num_packer_per_sc = le32_to_cpu(gc_info->v1.gc_num_packer_per_sc);
1442 		if (le16_to_cpu(gc_info->v1.header.version_minor) >= 1) {
1443 			adev->gfx.config.gc_num_tcp_per_sa = le32_to_cpu(gc_info->v1_1.gc_num_tcp_per_sa);
1444 			adev->gfx.config.gc_num_sdp_interface = le32_to_cpu(gc_info->v1_1.gc_num_sdp_interface);
1445 			adev->gfx.config.gc_num_tcps = le32_to_cpu(gc_info->v1_1.gc_num_tcps);
1446 		}
1447 		if (le16_to_cpu(gc_info->v1.header.version_minor) >= 2) {
1448 			adev->gfx.config.gc_num_tcp_per_wpg = le32_to_cpu(gc_info->v1_2.gc_num_tcp_per_wpg);
1449 			adev->gfx.config.gc_tcp_l1_size = le32_to_cpu(gc_info->v1_2.gc_tcp_l1_size);
1450 			adev->gfx.config.gc_num_sqc_per_wgp = le32_to_cpu(gc_info->v1_2.gc_num_sqc_per_wgp);
1451 			adev->gfx.config.gc_l1_instruction_cache_size_per_sqc = le32_to_cpu(gc_info->v1_2.gc_l1_instruction_cache_size_per_sqc);
1452 			adev->gfx.config.gc_l1_data_cache_size_per_sqc = le32_to_cpu(gc_info->v1_2.gc_l1_data_cache_size_per_sqc);
1453 			adev->gfx.config.gc_gl1c_per_sa = le32_to_cpu(gc_info->v1_2.gc_gl1c_per_sa);
1454 			adev->gfx.config.gc_gl1c_size_per_instance = le32_to_cpu(gc_info->v1_2.gc_gl1c_size_per_instance);
1455 			adev->gfx.config.gc_gl2c_per_gpu = le32_to_cpu(gc_info->v1_2.gc_gl2c_per_gpu);
1456 		}
1457 		break;
1458 	case 2:
1459 		adev->gfx.config.max_shader_engines = le32_to_cpu(gc_info->v2.gc_num_se);
1460 		adev->gfx.config.max_cu_per_sh = le32_to_cpu(gc_info->v2.gc_num_cu_per_sh);
1461 		adev->gfx.config.max_sh_per_se = le32_to_cpu(gc_info->v2.gc_num_sh_per_se);
1462 		adev->gfx.config.max_backends_per_se = le32_to_cpu(gc_info->v2.gc_num_rb_per_se);
1463 		adev->gfx.config.max_texture_channel_caches = le32_to_cpu(gc_info->v2.gc_num_tccs);
1464 		adev->gfx.config.max_gprs = le32_to_cpu(gc_info->v2.gc_num_gprs);
1465 		adev->gfx.config.max_gs_threads = le32_to_cpu(gc_info->v2.gc_num_max_gs_thds);
1466 		adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gc_info->v2.gc_gs_table_depth);
1467 		adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gc_info->v2.gc_gsprim_buff_depth);
1468 		adev->gfx.config.double_offchip_lds_buf = le32_to_cpu(gc_info->v2.gc_double_offchip_lds_buffer);
1469 		adev->gfx.cu_info.wave_front_size = le32_to_cpu(gc_info->v2.gc_wave_size);
1470 		adev->gfx.cu_info.max_waves_per_simd = le32_to_cpu(gc_info->v2.gc_max_waves_per_simd);
1471 		adev->gfx.cu_info.max_scratch_slots_per_cu = le32_to_cpu(gc_info->v2.gc_max_scratch_slots_per_cu);
1472 		adev->gfx.cu_info.lds_size = le32_to_cpu(gc_info->v2.gc_lds_size);
1473 		adev->gfx.config.num_sc_per_sh = le32_to_cpu(gc_info->v2.gc_num_sc_per_se) /
1474 			le32_to_cpu(gc_info->v2.gc_num_sh_per_se);
1475 		adev->gfx.config.num_packer_per_sc = le32_to_cpu(gc_info->v2.gc_num_packer_per_sc);
1476 		if (le16_to_cpu(gc_info->v2.header.version_minor == 1)) {
1477 			adev->gfx.config.gc_num_tcp_per_sa = le32_to_cpu(gc_info->v2_1.gc_num_tcp_per_sh);
1478 			adev->gfx.config.gc_tcp_size_per_cu = le32_to_cpu(gc_info->v2_1.gc_tcp_size_per_cu);
1479 			adev->gfx.config.gc_num_sdp_interface = le32_to_cpu(gc_info->v2_1.gc_num_sdp_interface); /* per XCD */
1480 			adev->gfx.config.gc_num_cu_per_sqc = le32_to_cpu(gc_info->v2_1.gc_num_cu_per_sqc);
1481 			adev->gfx.config.gc_l1_instruction_cache_size_per_sqc = le32_to_cpu(gc_info->v2_1.gc_instruction_cache_size_per_sqc);
1482 			adev->gfx.config.gc_l1_data_cache_size_per_sqc = le32_to_cpu(gc_info->v2_1.gc_scalar_data_cache_size_per_sqc);
1483 			adev->gfx.config.gc_tcc_size = le32_to_cpu(gc_info->v2_1.gc_tcc_size); /* per XCD */
1484 		}
1485 		break;
1486 	default:
1487 		dev_err(adev->dev,
1488 			"Unhandled GC info table %d.%d\n",
1489 			le16_to_cpu(gc_info->v1.header.version_major),
1490 			le16_to_cpu(gc_info->v1.header.version_minor));
1491 		return -EINVAL;
1492 	}
1493 	return 0;
1494 }
1495 
1496 union mall_info {
1497 	struct mall_info_v1_0 v1;
1498 	struct mall_info_v2_0 v2;
1499 };
1500 
1501 static int amdgpu_discovery_get_mall_info(struct amdgpu_device *adev)
1502 {
1503 	struct binary_header *bhdr;
1504 	union mall_info *mall_info;
1505 	u32 u, mall_size_per_umc, m_s_present, half_use;
1506 	u64 mall_size;
1507 	u16 offset;
1508 
1509 	if (!adev->mman.discovery_bin) {
1510 		DRM_ERROR("ip discovery uninitialized\n");
1511 		return -EINVAL;
1512 	}
1513 
1514 	bhdr = (struct binary_header *)adev->mman.discovery_bin;
1515 	offset = le16_to_cpu(bhdr->table_list[MALL_INFO].offset);
1516 
1517 	if (!offset)
1518 		return 0;
1519 
1520 	mall_info = (union mall_info *)(adev->mman.discovery_bin + offset);
1521 
1522 	switch (le16_to_cpu(mall_info->v1.header.version_major)) {
1523 	case 1:
1524 		mall_size = 0;
1525 		mall_size_per_umc = le32_to_cpu(mall_info->v1.mall_size_per_m);
1526 		m_s_present = le32_to_cpu(mall_info->v1.m_s_present);
1527 		half_use = le32_to_cpu(mall_info->v1.m_half_use);
1528 		for (u = 0; u < adev->gmc.num_umc; u++) {
1529 			if (m_s_present & (1 << u))
1530 				mall_size += mall_size_per_umc * 2;
1531 			else if (half_use & (1 << u))
1532 				mall_size += mall_size_per_umc / 2;
1533 			else
1534 				mall_size += mall_size_per_umc;
1535 		}
1536 		adev->gmc.mall_size = mall_size;
1537 		adev->gmc.m_half_use = half_use;
1538 		break;
1539 	case 2:
1540 		mall_size_per_umc = le32_to_cpu(mall_info->v2.mall_size_per_umc);
1541 		adev->gmc.mall_size = mall_size_per_umc * adev->gmc.num_umc;
1542 		break;
1543 	default:
1544 		dev_err(adev->dev,
1545 			"Unhandled MALL info table %d.%d\n",
1546 			le16_to_cpu(mall_info->v1.header.version_major),
1547 			le16_to_cpu(mall_info->v1.header.version_minor));
1548 		return -EINVAL;
1549 	}
1550 	return 0;
1551 }
1552 
1553 union vcn_info {
1554 	struct vcn_info_v1_0 v1;
1555 };
1556 
1557 static int amdgpu_discovery_get_vcn_info(struct amdgpu_device *adev)
1558 {
1559 	struct binary_header *bhdr;
1560 	union vcn_info *vcn_info;
1561 	u16 offset;
1562 	int v;
1563 
1564 	if (!adev->mman.discovery_bin) {
1565 		DRM_ERROR("ip discovery uninitialized\n");
1566 		return -EINVAL;
1567 	}
1568 
1569 	/* num_vcn_inst is currently limited to AMDGPU_MAX_VCN_INSTANCES
1570 	 * which is smaller than VCN_INFO_TABLE_MAX_NUM_INSTANCES
1571 	 * but that may change in the future with new GPUs so keep this
1572 	 * check for defensive purposes.
1573 	 */
1574 	if (adev->vcn.num_vcn_inst > VCN_INFO_TABLE_MAX_NUM_INSTANCES) {
1575 		dev_err(adev->dev, "invalid vcn instances\n");
1576 		return -EINVAL;
1577 	}
1578 
1579 	bhdr = (struct binary_header *)adev->mman.discovery_bin;
1580 	offset = le16_to_cpu(bhdr->table_list[VCN_INFO].offset);
1581 
1582 	if (!offset)
1583 		return 0;
1584 
1585 	vcn_info = (union vcn_info *)(adev->mman.discovery_bin + offset);
1586 
1587 	switch (le16_to_cpu(vcn_info->v1.header.version_major)) {
1588 	case 1:
1589 		/* num_vcn_inst is currently limited to AMDGPU_MAX_VCN_INSTANCES
1590 		 * so this won't overflow.
1591 		 */
1592 		for (v = 0; v < adev->vcn.num_vcn_inst; v++) {
1593 			adev->vcn.vcn_codec_disable_mask[v] =
1594 				le32_to_cpu(vcn_info->v1.instance_info[v].fuse_data.all_bits);
1595 		}
1596 		break;
1597 	default:
1598 		dev_err(adev->dev,
1599 			"Unhandled VCN info table %d.%d\n",
1600 			le16_to_cpu(vcn_info->v1.header.version_major),
1601 			le16_to_cpu(vcn_info->v1.header.version_minor));
1602 		return -EINVAL;
1603 	}
1604 	return 0;
1605 }
1606 
1607 static int amdgpu_discovery_set_common_ip_blocks(struct amdgpu_device *adev)
1608 {
1609 	/* what IP to use for this? */
1610 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
1611 	case IP_VERSION(9, 0, 1):
1612 	case IP_VERSION(9, 1, 0):
1613 	case IP_VERSION(9, 2, 1):
1614 	case IP_VERSION(9, 2, 2):
1615 	case IP_VERSION(9, 3, 0):
1616 	case IP_VERSION(9, 4, 0):
1617 	case IP_VERSION(9, 4, 1):
1618 	case IP_VERSION(9, 4, 2):
1619 	case IP_VERSION(9, 4, 3):
1620 		amdgpu_device_ip_block_add(adev, &vega10_common_ip_block);
1621 		break;
1622 	case IP_VERSION(10, 1, 10):
1623 	case IP_VERSION(10, 1, 1):
1624 	case IP_VERSION(10, 1, 2):
1625 	case IP_VERSION(10, 1, 3):
1626 	case IP_VERSION(10, 1, 4):
1627 	case IP_VERSION(10, 3, 0):
1628 	case IP_VERSION(10, 3, 1):
1629 	case IP_VERSION(10, 3, 2):
1630 	case IP_VERSION(10, 3, 3):
1631 	case IP_VERSION(10, 3, 4):
1632 	case IP_VERSION(10, 3, 5):
1633 	case IP_VERSION(10, 3, 6):
1634 	case IP_VERSION(10, 3, 7):
1635 		amdgpu_device_ip_block_add(adev, &nv_common_ip_block);
1636 		break;
1637 	case IP_VERSION(11, 0, 0):
1638 	case IP_VERSION(11, 0, 1):
1639 	case IP_VERSION(11, 0, 2):
1640 	case IP_VERSION(11, 0, 3):
1641 	case IP_VERSION(11, 0, 4):
1642 	case IP_VERSION(11, 5, 0):
1643 		amdgpu_device_ip_block_add(adev, &soc21_common_ip_block);
1644 		break;
1645 	default:
1646 		dev_err(adev->dev,
1647 			"Failed to add common ip block(GC_HWIP:0x%x)\n",
1648 			amdgpu_ip_version(adev, GC_HWIP, 0));
1649 		return -EINVAL;
1650 	}
1651 	return 0;
1652 }
1653 
1654 static int amdgpu_discovery_set_gmc_ip_blocks(struct amdgpu_device *adev)
1655 {
1656 	/* use GC or MMHUB IP version */
1657 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
1658 	case IP_VERSION(9, 0, 1):
1659 	case IP_VERSION(9, 1, 0):
1660 	case IP_VERSION(9, 2, 1):
1661 	case IP_VERSION(9, 2, 2):
1662 	case IP_VERSION(9, 3, 0):
1663 	case IP_VERSION(9, 4, 0):
1664 	case IP_VERSION(9, 4, 1):
1665 	case IP_VERSION(9, 4, 2):
1666 	case IP_VERSION(9, 4, 3):
1667 		amdgpu_device_ip_block_add(adev, &gmc_v9_0_ip_block);
1668 		break;
1669 	case IP_VERSION(10, 1, 10):
1670 	case IP_VERSION(10, 1, 1):
1671 	case IP_VERSION(10, 1, 2):
1672 	case IP_VERSION(10, 1, 3):
1673 	case IP_VERSION(10, 1, 4):
1674 	case IP_VERSION(10, 3, 0):
1675 	case IP_VERSION(10, 3, 1):
1676 	case IP_VERSION(10, 3, 2):
1677 	case IP_VERSION(10, 3, 3):
1678 	case IP_VERSION(10, 3, 4):
1679 	case IP_VERSION(10, 3, 5):
1680 	case IP_VERSION(10, 3, 6):
1681 	case IP_VERSION(10, 3, 7):
1682 		amdgpu_device_ip_block_add(adev, &gmc_v10_0_ip_block);
1683 		break;
1684 	case IP_VERSION(11, 0, 0):
1685 	case IP_VERSION(11, 0, 1):
1686 	case IP_VERSION(11, 0, 2):
1687 	case IP_VERSION(11, 0, 3):
1688 	case IP_VERSION(11, 0, 4):
1689 	case IP_VERSION(11, 5, 0):
1690 		amdgpu_device_ip_block_add(adev, &gmc_v11_0_ip_block);
1691 		break;
1692 	default:
1693 		dev_err(adev->dev, "Failed to add gmc ip block(GC_HWIP:0x%x)\n",
1694 			amdgpu_ip_version(adev, GC_HWIP, 0));
1695 		return -EINVAL;
1696 	}
1697 	return 0;
1698 }
1699 
1700 static int amdgpu_discovery_set_ih_ip_blocks(struct amdgpu_device *adev)
1701 {
1702 	switch (amdgpu_ip_version(adev, OSSSYS_HWIP, 0)) {
1703 	case IP_VERSION(4, 0, 0):
1704 	case IP_VERSION(4, 0, 1):
1705 	case IP_VERSION(4, 1, 0):
1706 	case IP_VERSION(4, 1, 1):
1707 	case IP_VERSION(4, 3, 0):
1708 		amdgpu_device_ip_block_add(adev, &vega10_ih_ip_block);
1709 		break;
1710 	case IP_VERSION(4, 2, 0):
1711 	case IP_VERSION(4, 2, 1):
1712 	case IP_VERSION(4, 4, 0):
1713 	case IP_VERSION(4, 4, 2):
1714 		amdgpu_device_ip_block_add(adev, &vega20_ih_ip_block);
1715 		break;
1716 	case IP_VERSION(5, 0, 0):
1717 	case IP_VERSION(5, 0, 1):
1718 	case IP_VERSION(5, 0, 2):
1719 	case IP_VERSION(5, 0, 3):
1720 	case IP_VERSION(5, 2, 0):
1721 	case IP_VERSION(5, 2, 1):
1722 		amdgpu_device_ip_block_add(adev, &navi10_ih_ip_block);
1723 		break;
1724 	case IP_VERSION(6, 0, 0):
1725 	case IP_VERSION(6, 0, 1):
1726 	case IP_VERSION(6, 0, 2):
1727 		amdgpu_device_ip_block_add(adev, &ih_v6_0_ip_block);
1728 		break;
1729 	case IP_VERSION(6, 1, 0):
1730 		amdgpu_device_ip_block_add(adev, &ih_v6_1_ip_block);
1731 		break;
1732 	default:
1733 		dev_err(adev->dev,
1734 			"Failed to add ih ip block(OSSSYS_HWIP:0x%x)\n",
1735 			amdgpu_ip_version(adev, OSSSYS_HWIP, 0));
1736 		return -EINVAL;
1737 	}
1738 	return 0;
1739 }
1740 
1741 static int amdgpu_discovery_set_psp_ip_blocks(struct amdgpu_device *adev)
1742 {
1743 	switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
1744 	case IP_VERSION(9, 0, 0):
1745 		amdgpu_device_ip_block_add(adev, &psp_v3_1_ip_block);
1746 		break;
1747 	case IP_VERSION(10, 0, 0):
1748 	case IP_VERSION(10, 0, 1):
1749 		amdgpu_device_ip_block_add(adev, &psp_v10_0_ip_block);
1750 		break;
1751 	case IP_VERSION(11, 0, 0):
1752 	case IP_VERSION(11, 0, 2):
1753 	case IP_VERSION(11, 0, 4):
1754 	case IP_VERSION(11, 0, 5):
1755 	case IP_VERSION(11, 0, 9):
1756 	case IP_VERSION(11, 0, 7):
1757 	case IP_VERSION(11, 0, 11):
1758 	case IP_VERSION(11, 0, 12):
1759 	case IP_VERSION(11, 0, 13):
1760 	case IP_VERSION(11, 5, 0):
1761 		amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block);
1762 		break;
1763 	case IP_VERSION(11, 0, 8):
1764 		amdgpu_device_ip_block_add(adev, &psp_v11_0_8_ip_block);
1765 		break;
1766 	case IP_VERSION(11, 0, 3):
1767 	case IP_VERSION(12, 0, 1):
1768 		amdgpu_device_ip_block_add(adev, &psp_v12_0_ip_block);
1769 		break;
1770 	case IP_VERSION(13, 0, 0):
1771 	case IP_VERSION(13, 0, 1):
1772 	case IP_VERSION(13, 0, 2):
1773 	case IP_VERSION(13, 0, 3):
1774 	case IP_VERSION(13, 0, 5):
1775 	case IP_VERSION(13, 0, 6):
1776 	case IP_VERSION(13, 0, 7):
1777 	case IP_VERSION(13, 0, 8):
1778 	case IP_VERSION(13, 0, 10):
1779 	case IP_VERSION(13, 0, 11):
1780 	case IP_VERSION(14, 0, 0):
1781 		amdgpu_device_ip_block_add(adev, &psp_v13_0_ip_block);
1782 		break;
1783 	case IP_VERSION(13, 0, 4):
1784 		amdgpu_device_ip_block_add(adev, &psp_v13_0_4_ip_block);
1785 		break;
1786 	default:
1787 		dev_err(adev->dev,
1788 			"Failed to add psp ip block(MP0_HWIP:0x%x)\n",
1789 			amdgpu_ip_version(adev, MP0_HWIP, 0));
1790 		return -EINVAL;
1791 	}
1792 	return 0;
1793 }
1794 
1795 static int amdgpu_discovery_set_smu_ip_blocks(struct amdgpu_device *adev)
1796 {
1797 	switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1798 	case IP_VERSION(9, 0, 0):
1799 	case IP_VERSION(10, 0, 0):
1800 	case IP_VERSION(10, 0, 1):
1801 	case IP_VERSION(11, 0, 2):
1802 		if (adev->asic_type == CHIP_ARCTURUS)
1803 			amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
1804 		else
1805 			amdgpu_device_ip_block_add(adev, &pp_smu_ip_block);
1806 		break;
1807 	case IP_VERSION(11, 0, 0):
1808 	case IP_VERSION(11, 0, 5):
1809 	case IP_VERSION(11, 0, 9):
1810 	case IP_VERSION(11, 0, 7):
1811 	case IP_VERSION(11, 0, 8):
1812 	case IP_VERSION(11, 0, 11):
1813 	case IP_VERSION(11, 0, 12):
1814 	case IP_VERSION(11, 0, 13):
1815 	case IP_VERSION(11, 5, 0):
1816 		amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
1817 		break;
1818 	case IP_VERSION(12, 0, 0):
1819 	case IP_VERSION(12, 0, 1):
1820 		amdgpu_device_ip_block_add(adev, &smu_v12_0_ip_block);
1821 		break;
1822 	case IP_VERSION(13, 0, 0):
1823 	case IP_VERSION(13, 0, 1):
1824 	case IP_VERSION(13, 0, 2):
1825 	case IP_VERSION(13, 0, 3):
1826 	case IP_VERSION(13, 0, 4):
1827 	case IP_VERSION(13, 0, 5):
1828 	case IP_VERSION(13, 0, 6):
1829 	case IP_VERSION(13, 0, 7):
1830 	case IP_VERSION(13, 0, 8):
1831 	case IP_VERSION(13, 0, 10):
1832 	case IP_VERSION(13, 0, 11):
1833 		amdgpu_device_ip_block_add(adev, &smu_v13_0_ip_block);
1834 		break;
1835 	default:
1836 		dev_err(adev->dev,
1837 			"Failed to add smu ip block(MP1_HWIP:0x%x)\n",
1838 			amdgpu_ip_version(adev, MP1_HWIP, 0));
1839 		return -EINVAL;
1840 	}
1841 	return 0;
1842 }
1843 
1844 #if defined(CONFIG_DRM_AMD_DC)
1845 static void amdgpu_discovery_set_sriov_display(struct amdgpu_device *adev)
1846 {
1847 	amdgpu_device_set_sriov_virtual_display(adev);
1848 	amdgpu_device_ip_block_add(adev, &amdgpu_vkms_ip_block);
1849 }
1850 #endif
1851 
1852 static int amdgpu_discovery_set_display_ip_blocks(struct amdgpu_device *adev)
1853 {
1854 	if (adev->enable_virtual_display) {
1855 		amdgpu_device_ip_block_add(adev, &amdgpu_vkms_ip_block);
1856 		return 0;
1857 	}
1858 
1859 	if (!amdgpu_device_has_dc_support(adev))
1860 		return 0;
1861 
1862 #if defined(CONFIG_DRM_AMD_DC)
1863 	if (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1864 		switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) {
1865 		case IP_VERSION(1, 0, 0):
1866 		case IP_VERSION(1, 0, 1):
1867 		case IP_VERSION(2, 0, 2):
1868 		case IP_VERSION(2, 0, 0):
1869 		case IP_VERSION(2, 0, 3):
1870 		case IP_VERSION(2, 1, 0):
1871 		case IP_VERSION(3, 0, 0):
1872 		case IP_VERSION(3, 0, 2):
1873 		case IP_VERSION(3, 0, 3):
1874 		case IP_VERSION(3, 0, 1):
1875 		case IP_VERSION(3, 1, 2):
1876 		case IP_VERSION(3, 1, 3):
1877 		case IP_VERSION(3, 1, 4):
1878 		case IP_VERSION(3, 1, 5):
1879 		case IP_VERSION(3, 1, 6):
1880 		case IP_VERSION(3, 2, 0):
1881 		case IP_VERSION(3, 2, 1):
1882 			if (amdgpu_sriov_vf(adev))
1883 				amdgpu_discovery_set_sriov_display(adev);
1884 			else
1885 				amdgpu_device_ip_block_add(adev, &dm_ip_block);
1886 			break;
1887 		default:
1888 			dev_err(adev->dev,
1889 				"Failed to add dm ip block(DCE_HWIP:0x%x)\n",
1890 				amdgpu_ip_version(adev, DCE_HWIP, 0));
1891 			return -EINVAL;
1892 		}
1893 	} else if (amdgpu_ip_version(adev, DCI_HWIP, 0)) {
1894 		switch (amdgpu_ip_version(adev, DCI_HWIP, 0)) {
1895 		case IP_VERSION(12, 0, 0):
1896 		case IP_VERSION(12, 0, 1):
1897 		case IP_VERSION(12, 1, 0):
1898 			if (amdgpu_sriov_vf(adev))
1899 				amdgpu_discovery_set_sriov_display(adev);
1900 			else
1901 				amdgpu_device_ip_block_add(adev, &dm_ip_block);
1902 			break;
1903 		default:
1904 			dev_err(adev->dev,
1905 				"Failed to add dm ip block(DCI_HWIP:0x%x)\n",
1906 				amdgpu_ip_version(adev, DCI_HWIP, 0));
1907 			return -EINVAL;
1908 		}
1909 	}
1910 #endif
1911 	return 0;
1912 }
1913 
1914 static int amdgpu_discovery_set_gc_ip_blocks(struct amdgpu_device *adev)
1915 {
1916 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
1917 	case IP_VERSION(9, 0, 1):
1918 	case IP_VERSION(9, 1, 0):
1919 	case IP_VERSION(9, 2, 1):
1920 	case IP_VERSION(9, 2, 2):
1921 	case IP_VERSION(9, 3, 0):
1922 	case IP_VERSION(9, 4, 0):
1923 	case IP_VERSION(9, 4, 1):
1924 	case IP_VERSION(9, 4, 2):
1925 		amdgpu_device_ip_block_add(adev, &gfx_v9_0_ip_block);
1926 		break;
1927 	case IP_VERSION(9, 4, 3):
1928 		if (!amdgpu_exp_hw_support)
1929 			return -EINVAL;
1930 		amdgpu_device_ip_block_add(adev, &gfx_v9_4_3_ip_block);
1931 		break;
1932 	case IP_VERSION(10, 1, 10):
1933 	case IP_VERSION(10, 1, 2):
1934 	case IP_VERSION(10, 1, 1):
1935 	case IP_VERSION(10, 1, 3):
1936 	case IP_VERSION(10, 1, 4):
1937 	case IP_VERSION(10, 3, 0):
1938 	case IP_VERSION(10, 3, 2):
1939 	case IP_VERSION(10, 3, 1):
1940 	case IP_VERSION(10, 3, 4):
1941 	case IP_VERSION(10, 3, 5):
1942 	case IP_VERSION(10, 3, 6):
1943 	case IP_VERSION(10, 3, 3):
1944 	case IP_VERSION(10, 3, 7):
1945 		amdgpu_device_ip_block_add(adev, &gfx_v10_0_ip_block);
1946 		break;
1947 	case IP_VERSION(11, 0, 0):
1948 	case IP_VERSION(11, 0, 1):
1949 	case IP_VERSION(11, 0, 2):
1950 	case IP_VERSION(11, 0, 3):
1951 	case IP_VERSION(11, 0, 4):
1952 	case IP_VERSION(11, 5, 0):
1953 		amdgpu_device_ip_block_add(adev, &gfx_v11_0_ip_block);
1954 		break;
1955 	default:
1956 		dev_err(adev->dev, "Failed to add gfx ip block(GC_HWIP:0x%x)\n",
1957 			amdgpu_ip_version(adev, GC_HWIP, 0));
1958 		return -EINVAL;
1959 	}
1960 	return 0;
1961 }
1962 
1963 static int amdgpu_discovery_set_sdma_ip_blocks(struct amdgpu_device *adev)
1964 {
1965 	switch (amdgpu_ip_version(adev, SDMA0_HWIP, 0)) {
1966 	case IP_VERSION(4, 0, 0):
1967 	case IP_VERSION(4, 0, 1):
1968 	case IP_VERSION(4, 1, 0):
1969 	case IP_VERSION(4, 1, 1):
1970 	case IP_VERSION(4, 1, 2):
1971 	case IP_VERSION(4, 2, 0):
1972 	case IP_VERSION(4, 2, 2):
1973 	case IP_VERSION(4, 4, 0):
1974 		amdgpu_device_ip_block_add(adev, &sdma_v4_0_ip_block);
1975 		break;
1976 	case IP_VERSION(4, 4, 2):
1977 		amdgpu_device_ip_block_add(adev, &sdma_v4_4_2_ip_block);
1978 		break;
1979 	case IP_VERSION(5, 0, 0):
1980 	case IP_VERSION(5, 0, 1):
1981 	case IP_VERSION(5, 0, 2):
1982 	case IP_VERSION(5, 0, 5):
1983 		amdgpu_device_ip_block_add(adev, &sdma_v5_0_ip_block);
1984 		break;
1985 	case IP_VERSION(5, 2, 0):
1986 	case IP_VERSION(5, 2, 2):
1987 	case IP_VERSION(5, 2, 4):
1988 	case IP_VERSION(5, 2, 5):
1989 	case IP_VERSION(5, 2, 6):
1990 	case IP_VERSION(5, 2, 3):
1991 	case IP_VERSION(5, 2, 1):
1992 	case IP_VERSION(5, 2, 7):
1993 		amdgpu_device_ip_block_add(adev, &sdma_v5_2_ip_block);
1994 		break;
1995 	case IP_VERSION(6, 0, 0):
1996 	case IP_VERSION(6, 0, 1):
1997 	case IP_VERSION(6, 0, 2):
1998 	case IP_VERSION(6, 0, 3):
1999 	case IP_VERSION(6, 1, 0):
2000 		amdgpu_device_ip_block_add(adev, &sdma_v6_0_ip_block);
2001 		break;
2002 	default:
2003 		dev_err(adev->dev,
2004 			"Failed to add sdma ip block(SDMA0_HWIP:0x%x)\n",
2005 			amdgpu_ip_version(adev, SDMA0_HWIP, 0));
2006 		return -EINVAL;
2007 	}
2008 	return 0;
2009 }
2010 
2011 static int amdgpu_discovery_set_mm_ip_blocks(struct amdgpu_device *adev)
2012 {
2013 	if (amdgpu_ip_version(adev, VCE_HWIP, 0)) {
2014 		switch (amdgpu_ip_version(adev, UVD_HWIP, 0)) {
2015 		case IP_VERSION(7, 0, 0):
2016 		case IP_VERSION(7, 2, 0):
2017 			/* UVD is not supported on vega20 SR-IOV */
2018 			if (!(adev->asic_type == CHIP_VEGA20 && amdgpu_sriov_vf(adev)))
2019 				amdgpu_device_ip_block_add(adev, &uvd_v7_0_ip_block);
2020 			break;
2021 		default:
2022 			dev_err(adev->dev,
2023 				"Failed to add uvd v7 ip block(UVD_HWIP:0x%x)\n",
2024 				amdgpu_ip_version(adev, UVD_HWIP, 0));
2025 			return -EINVAL;
2026 		}
2027 		switch (amdgpu_ip_version(adev, VCE_HWIP, 0)) {
2028 		case IP_VERSION(4, 0, 0):
2029 		case IP_VERSION(4, 1, 0):
2030 			/* VCE is not supported on vega20 SR-IOV */
2031 			if (!(adev->asic_type == CHIP_VEGA20 && amdgpu_sriov_vf(adev)))
2032 				amdgpu_device_ip_block_add(adev, &vce_v4_0_ip_block);
2033 			break;
2034 		default:
2035 			dev_err(adev->dev,
2036 				"Failed to add VCE v4 ip block(VCE_HWIP:0x%x)\n",
2037 				amdgpu_ip_version(adev, VCE_HWIP, 0));
2038 			return -EINVAL;
2039 		}
2040 	} else {
2041 		switch (amdgpu_ip_version(adev, UVD_HWIP, 0)) {
2042 		case IP_VERSION(1, 0, 0):
2043 		case IP_VERSION(1, 0, 1):
2044 			amdgpu_device_ip_block_add(adev, &vcn_v1_0_ip_block);
2045 			break;
2046 		case IP_VERSION(2, 0, 0):
2047 		case IP_VERSION(2, 0, 2):
2048 		case IP_VERSION(2, 2, 0):
2049 			amdgpu_device_ip_block_add(adev, &vcn_v2_0_ip_block);
2050 			if (!amdgpu_sriov_vf(adev))
2051 				amdgpu_device_ip_block_add(adev, &jpeg_v2_0_ip_block);
2052 			break;
2053 		case IP_VERSION(2, 0, 3):
2054 			break;
2055 		case IP_VERSION(2, 5, 0):
2056 			amdgpu_device_ip_block_add(adev, &vcn_v2_5_ip_block);
2057 			amdgpu_device_ip_block_add(adev, &jpeg_v2_5_ip_block);
2058 			break;
2059 		case IP_VERSION(2, 6, 0):
2060 			amdgpu_device_ip_block_add(adev, &vcn_v2_6_ip_block);
2061 			amdgpu_device_ip_block_add(adev, &jpeg_v2_6_ip_block);
2062 			break;
2063 		case IP_VERSION(3, 0, 0):
2064 		case IP_VERSION(3, 0, 16):
2065 		case IP_VERSION(3, 1, 1):
2066 		case IP_VERSION(3, 1, 2):
2067 		case IP_VERSION(3, 0, 2):
2068 			amdgpu_device_ip_block_add(adev, &vcn_v3_0_ip_block);
2069 			if (!amdgpu_sriov_vf(adev))
2070 				amdgpu_device_ip_block_add(adev, &jpeg_v3_0_ip_block);
2071 			break;
2072 		case IP_VERSION(3, 0, 33):
2073 			amdgpu_device_ip_block_add(adev, &vcn_v3_0_ip_block);
2074 			break;
2075 		case IP_VERSION(4, 0, 0):
2076 		case IP_VERSION(4, 0, 2):
2077 		case IP_VERSION(4, 0, 4):
2078 			amdgpu_device_ip_block_add(adev, &vcn_v4_0_ip_block);
2079 			amdgpu_device_ip_block_add(adev, &jpeg_v4_0_ip_block);
2080 			break;
2081 		case IP_VERSION(4, 0, 3):
2082 			amdgpu_device_ip_block_add(adev, &vcn_v4_0_3_ip_block);
2083 			amdgpu_device_ip_block_add(adev, &jpeg_v4_0_3_ip_block);
2084 			break;
2085 		case IP_VERSION(4, 0, 5):
2086 			amdgpu_device_ip_block_add(adev, &vcn_v4_0_5_ip_block);
2087 			amdgpu_device_ip_block_add(adev, &jpeg_v4_0_5_ip_block);
2088 			break;
2089 		default:
2090 			dev_err(adev->dev,
2091 				"Failed to add vcn/jpeg ip block(UVD_HWIP:0x%x)\n",
2092 				amdgpu_ip_version(adev, UVD_HWIP, 0));
2093 			return -EINVAL;
2094 		}
2095 	}
2096 	return 0;
2097 }
2098 
2099 static int amdgpu_discovery_set_mes_ip_blocks(struct amdgpu_device *adev)
2100 {
2101 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
2102 	case IP_VERSION(10, 1, 10):
2103 	case IP_VERSION(10, 1, 1):
2104 	case IP_VERSION(10, 1, 2):
2105 	case IP_VERSION(10, 1, 3):
2106 	case IP_VERSION(10, 1, 4):
2107 	case IP_VERSION(10, 3, 0):
2108 	case IP_VERSION(10, 3, 1):
2109 	case IP_VERSION(10, 3, 2):
2110 	case IP_VERSION(10, 3, 3):
2111 	case IP_VERSION(10, 3, 4):
2112 	case IP_VERSION(10, 3, 5):
2113 	case IP_VERSION(10, 3, 6):
2114 		if (amdgpu_mes) {
2115 			amdgpu_device_ip_block_add(adev, &mes_v10_1_ip_block);
2116 			adev->enable_mes = true;
2117 			if (amdgpu_mes_kiq)
2118 				adev->enable_mes_kiq = true;
2119 		}
2120 		break;
2121 	case IP_VERSION(11, 0, 0):
2122 	case IP_VERSION(11, 0, 1):
2123 	case IP_VERSION(11, 0, 2):
2124 	case IP_VERSION(11, 0, 3):
2125 	case IP_VERSION(11, 0, 4):
2126 	case IP_VERSION(11, 5, 0):
2127 		amdgpu_device_ip_block_add(adev, &mes_v11_0_ip_block);
2128 		adev->enable_mes = true;
2129 		adev->enable_mes_kiq = true;
2130 		break;
2131 	default:
2132 		break;
2133 	}
2134 	return 0;
2135 }
2136 
2137 static void amdgpu_discovery_init_soc_config(struct amdgpu_device *adev)
2138 {
2139 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
2140 	case IP_VERSION(9, 4, 3):
2141 		aqua_vanjaram_init_soc_config(adev);
2142 		break;
2143 	default:
2144 		break;
2145 	}
2146 }
2147 
2148 static int amdgpu_discovery_set_vpe_ip_blocks(struct amdgpu_device *adev)
2149 {
2150 	switch (amdgpu_ip_version(adev, VPE_HWIP, 0)) {
2151 	case IP_VERSION(6, 1, 0):
2152 		amdgpu_device_ip_block_add(adev, &vpe_v6_1_ip_block);
2153 		break;
2154 	default:
2155 		break;
2156 	}
2157 
2158 	return 0;
2159 }
2160 
2161 static int amdgpu_discovery_set_umsch_mm_ip_blocks(struct amdgpu_device *adev)
2162 {
2163 	switch (amdgpu_ip_version(adev, VCN_HWIP, 0)) {
2164 	case IP_VERSION(4, 0, 5):
2165 		if (amdgpu_umsch_mm & 0x1) {
2166 			amdgpu_device_ip_block_add(adev, &umsch_mm_v4_0_ip_block);
2167 			adev->enable_umsch_mm = true;
2168 		}
2169 		break;
2170 	default:
2171 		break;
2172 	}
2173 
2174 	return 0;
2175 }
2176 
2177 int amdgpu_discovery_set_ip_blocks(struct amdgpu_device *adev)
2178 {
2179 	int r;
2180 
2181 	switch (adev->asic_type) {
2182 	case CHIP_VEGA10:
2183 		vega10_reg_base_init(adev);
2184 		adev->sdma.num_instances = 2;
2185 		adev->gmc.num_umc = 4;
2186 		adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 0, 0);
2187 		adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 0, 0);
2188 		adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 0, 0);
2189 		adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 0, 0);
2190 		adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 0, 0);
2191 		adev->ip_versions[SDMA1_HWIP][0] = IP_VERSION(4, 0, 0);
2192 		adev->ip_versions[DF_HWIP][0] = IP_VERSION(2, 1, 0);
2193 		adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(6, 1, 0);
2194 		adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 0, 0);
2195 		adev->ip_versions[MP0_HWIP][0] = IP_VERSION(9, 0, 0);
2196 		adev->ip_versions[MP1_HWIP][0] = IP_VERSION(9, 0, 0);
2197 		adev->ip_versions[THM_HWIP][0] = IP_VERSION(9, 0, 0);
2198 		adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(9, 0, 0);
2199 		adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 0, 1);
2200 		adev->ip_versions[UVD_HWIP][0] = IP_VERSION(7, 0, 0);
2201 		adev->ip_versions[VCE_HWIP][0] = IP_VERSION(4, 0, 0);
2202 		adev->ip_versions[DCI_HWIP][0] = IP_VERSION(12, 0, 0);
2203 		break;
2204 	case CHIP_VEGA12:
2205 		vega10_reg_base_init(adev);
2206 		adev->sdma.num_instances = 2;
2207 		adev->gmc.num_umc = 4;
2208 		adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 3, 0);
2209 		adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 3, 0);
2210 		adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 0, 1);
2211 		adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 0, 1);
2212 		adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 0, 1);
2213 		adev->ip_versions[SDMA1_HWIP][0] = IP_VERSION(4, 0, 1);
2214 		adev->ip_versions[DF_HWIP][0] = IP_VERSION(2, 5, 0);
2215 		adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(6, 2, 0);
2216 		adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 1, 0);
2217 		adev->ip_versions[MP0_HWIP][0] = IP_VERSION(9, 0, 0);
2218 		adev->ip_versions[MP1_HWIP][0] = IP_VERSION(9, 0, 0);
2219 		adev->ip_versions[THM_HWIP][0] = IP_VERSION(9, 0, 0);
2220 		adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(9, 0, 1);
2221 		adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 2, 1);
2222 		adev->ip_versions[UVD_HWIP][0] = IP_VERSION(7, 0, 0);
2223 		adev->ip_versions[VCE_HWIP][0] = IP_VERSION(4, 0, 0);
2224 		adev->ip_versions[DCI_HWIP][0] = IP_VERSION(12, 0, 1);
2225 		break;
2226 	case CHIP_RAVEN:
2227 		vega10_reg_base_init(adev);
2228 		adev->sdma.num_instances = 1;
2229 		adev->vcn.num_vcn_inst = 1;
2230 		adev->gmc.num_umc = 2;
2231 		if (adev->apu_flags & AMD_APU_IS_RAVEN2) {
2232 			adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 2, 0);
2233 			adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 2, 0);
2234 			adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 1, 1);
2235 			adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 1, 1);
2236 			adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 1, 1);
2237 			adev->ip_versions[DF_HWIP][0] = IP_VERSION(2, 1, 1);
2238 			adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 0, 1);
2239 			adev->ip_versions[UMC_HWIP][0] = IP_VERSION(7, 5, 0);
2240 			adev->ip_versions[MP0_HWIP][0] = IP_VERSION(10, 0, 1);
2241 			adev->ip_versions[MP1_HWIP][0] = IP_VERSION(10, 0, 1);
2242 			adev->ip_versions[THM_HWIP][0] = IP_VERSION(10, 1, 0);
2243 			adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(10, 0, 1);
2244 			adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 2, 2);
2245 			adev->ip_versions[UVD_HWIP][0] = IP_VERSION(1, 0, 1);
2246 			adev->ip_versions[DCE_HWIP][0] = IP_VERSION(1, 0, 1);
2247 		} else {
2248 			adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 1, 0);
2249 			adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 1, 0);
2250 			adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 1, 0);
2251 			adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 1, 0);
2252 			adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 1, 0);
2253 			adev->ip_versions[DF_HWIP][0] = IP_VERSION(2, 1, 0);
2254 			adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 0, 0);
2255 			adev->ip_versions[UMC_HWIP][0] = IP_VERSION(7, 0, 0);
2256 			adev->ip_versions[MP0_HWIP][0] = IP_VERSION(10, 0, 0);
2257 			adev->ip_versions[MP1_HWIP][0] = IP_VERSION(10, 0, 0);
2258 			adev->ip_versions[THM_HWIP][0] = IP_VERSION(10, 0, 0);
2259 			adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(10, 0, 0);
2260 			adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 1, 0);
2261 			adev->ip_versions[UVD_HWIP][0] = IP_VERSION(1, 0, 0);
2262 			adev->ip_versions[DCE_HWIP][0] = IP_VERSION(1, 0, 0);
2263 		}
2264 		break;
2265 	case CHIP_VEGA20:
2266 		vega20_reg_base_init(adev);
2267 		adev->sdma.num_instances = 2;
2268 		adev->gmc.num_umc = 8;
2269 		adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 4, 0);
2270 		adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 4, 0);
2271 		adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 2, 0);
2272 		adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 2, 0);
2273 		adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 2, 0);
2274 		adev->ip_versions[SDMA1_HWIP][0] = IP_VERSION(4, 2, 0);
2275 		adev->ip_versions[DF_HWIP][0] = IP_VERSION(3, 6, 0);
2276 		adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 4, 0);
2277 		adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 1, 1);
2278 		adev->ip_versions[MP0_HWIP][0] = IP_VERSION(11, 0, 2);
2279 		adev->ip_versions[MP1_HWIP][0] = IP_VERSION(11, 0, 2);
2280 		adev->ip_versions[THM_HWIP][0] = IP_VERSION(11, 0, 2);
2281 		adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(11, 0, 2);
2282 		adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 4, 0);
2283 		adev->ip_versions[UVD_HWIP][0] = IP_VERSION(7, 2, 0);
2284 		adev->ip_versions[UVD_HWIP][1] = IP_VERSION(7, 2, 0);
2285 		adev->ip_versions[VCE_HWIP][0] = IP_VERSION(4, 1, 0);
2286 		adev->ip_versions[DCI_HWIP][0] = IP_VERSION(12, 1, 0);
2287 		break;
2288 	case CHIP_ARCTURUS:
2289 		arct_reg_base_init(adev);
2290 		adev->sdma.num_instances = 8;
2291 		adev->vcn.num_vcn_inst = 2;
2292 		adev->gmc.num_umc = 8;
2293 		adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 4, 1);
2294 		adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 4, 1);
2295 		adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 2, 1);
2296 		adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 2, 1);
2297 		adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 2, 2);
2298 		adev->ip_versions[SDMA1_HWIP][0] = IP_VERSION(4, 2, 2);
2299 		adev->ip_versions[SDMA1_HWIP][1] = IP_VERSION(4, 2, 2);
2300 		adev->ip_versions[SDMA1_HWIP][2] = IP_VERSION(4, 2, 2);
2301 		adev->ip_versions[SDMA1_HWIP][3] = IP_VERSION(4, 2, 2);
2302 		adev->ip_versions[SDMA1_HWIP][4] = IP_VERSION(4, 2, 2);
2303 		adev->ip_versions[SDMA1_HWIP][5] = IP_VERSION(4, 2, 2);
2304 		adev->ip_versions[SDMA1_HWIP][6] = IP_VERSION(4, 2, 2);
2305 		adev->ip_versions[DF_HWIP][0] = IP_VERSION(3, 6, 1);
2306 		adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 4, 1);
2307 		adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 1, 2);
2308 		adev->ip_versions[MP0_HWIP][0] = IP_VERSION(11, 0, 4);
2309 		adev->ip_versions[MP1_HWIP][0] = IP_VERSION(11, 0, 2);
2310 		adev->ip_versions[THM_HWIP][0] = IP_VERSION(11, 0, 3);
2311 		adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(11, 0, 3);
2312 		adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 4, 1);
2313 		adev->ip_versions[UVD_HWIP][0] = IP_VERSION(2, 5, 0);
2314 		adev->ip_versions[UVD_HWIP][1] = IP_VERSION(2, 5, 0);
2315 		break;
2316 	case CHIP_ALDEBARAN:
2317 		aldebaran_reg_base_init(adev);
2318 		adev->sdma.num_instances = 5;
2319 		adev->vcn.num_vcn_inst = 2;
2320 		adev->gmc.num_umc = 4;
2321 		adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 4, 2);
2322 		adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 4, 2);
2323 		adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 4, 0);
2324 		adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 4, 0);
2325 		adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 4, 0);
2326 		adev->ip_versions[SDMA0_HWIP][1] = IP_VERSION(4, 4, 0);
2327 		adev->ip_versions[SDMA0_HWIP][2] = IP_VERSION(4, 4, 0);
2328 		adev->ip_versions[SDMA0_HWIP][3] = IP_VERSION(4, 4, 0);
2329 		adev->ip_versions[SDMA0_HWIP][4] = IP_VERSION(4, 4, 0);
2330 		adev->ip_versions[DF_HWIP][0] = IP_VERSION(3, 6, 2);
2331 		adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 4, 4);
2332 		adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 7, 0);
2333 		adev->ip_versions[MP0_HWIP][0] = IP_VERSION(13, 0, 2);
2334 		adev->ip_versions[MP1_HWIP][0] = IP_VERSION(13, 0, 2);
2335 		adev->ip_versions[THM_HWIP][0] = IP_VERSION(13, 0, 2);
2336 		adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(13, 0, 2);
2337 		adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 4, 2);
2338 		adev->ip_versions[UVD_HWIP][0] = IP_VERSION(2, 6, 0);
2339 		adev->ip_versions[UVD_HWIP][1] = IP_VERSION(2, 6, 0);
2340 		adev->ip_versions[XGMI_HWIP][0] = IP_VERSION(6, 1, 0);
2341 		break;
2342 	default:
2343 		r = amdgpu_discovery_reg_base_init(adev);
2344 		if (r)
2345 			return -EINVAL;
2346 
2347 		amdgpu_discovery_harvest_ip(adev);
2348 		amdgpu_discovery_get_gfx_info(adev);
2349 		amdgpu_discovery_get_mall_info(adev);
2350 		amdgpu_discovery_get_vcn_info(adev);
2351 		break;
2352 	}
2353 
2354 	amdgpu_discovery_init_soc_config(adev);
2355 	amdgpu_discovery_sysfs_init(adev);
2356 
2357 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
2358 	case IP_VERSION(9, 0, 1):
2359 	case IP_VERSION(9, 2, 1):
2360 	case IP_VERSION(9, 4, 0):
2361 	case IP_VERSION(9, 4, 1):
2362 	case IP_VERSION(9, 4, 2):
2363 	case IP_VERSION(9, 4, 3):
2364 		adev->family = AMDGPU_FAMILY_AI;
2365 		break;
2366 	case IP_VERSION(9, 1, 0):
2367 	case IP_VERSION(9, 2, 2):
2368 	case IP_VERSION(9, 3, 0):
2369 		adev->family = AMDGPU_FAMILY_RV;
2370 		break;
2371 	case IP_VERSION(10, 1, 10):
2372 	case IP_VERSION(10, 1, 1):
2373 	case IP_VERSION(10, 1, 2):
2374 	case IP_VERSION(10, 1, 3):
2375 	case IP_VERSION(10, 1, 4):
2376 	case IP_VERSION(10, 3, 0):
2377 	case IP_VERSION(10, 3, 2):
2378 	case IP_VERSION(10, 3, 4):
2379 	case IP_VERSION(10, 3, 5):
2380 		adev->family = AMDGPU_FAMILY_NV;
2381 		break;
2382 	case IP_VERSION(10, 3, 1):
2383 		adev->family = AMDGPU_FAMILY_VGH;
2384 		adev->apu_flags |= AMD_APU_IS_VANGOGH;
2385 		break;
2386 	case IP_VERSION(10, 3, 3):
2387 		adev->family = AMDGPU_FAMILY_YC;
2388 		break;
2389 	case IP_VERSION(10, 3, 6):
2390 		adev->family = AMDGPU_FAMILY_GC_10_3_6;
2391 		break;
2392 	case IP_VERSION(10, 3, 7):
2393 		adev->family = AMDGPU_FAMILY_GC_10_3_7;
2394 		break;
2395 	case IP_VERSION(11, 0, 0):
2396 	case IP_VERSION(11, 0, 2):
2397 	case IP_VERSION(11, 0, 3):
2398 		adev->family = AMDGPU_FAMILY_GC_11_0_0;
2399 		break;
2400 	case IP_VERSION(11, 0, 1):
2401 	case IP_VERSION(11, 0, 4):
2402 		adev->family = AMDGPU_FAMILY_GC_11_0_1;
2403 		break;
2404 	case IP_VERSION(11, 5, 0):
2405 		adev->family = AMDGPU_FAMILY_GC_11_5_0;
2406 		break;
2407 	default:
2408 		return -EINVAL;
2409 	}
2410 
2411 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
2412 	case IP_VERSION(9, 1, 0):
2413 	case IP_VERSION(9, 2, 2):
2414 	case IP_VERSION(9, 3, 0):
2415 	case IP_VERSION(10, 1, 3):
2416 	case IP_VERSION(10, 1, 4):
2417 	case IP_VERSION(10, 3, 1):
2418 	case IP_VERSION(10, 3, 3):
2419 	case IP_VERSION(10, 3, 6):
2420 	case IP_VERSION(10, 3, 7):
2421 	case IP_VERSION(11, 0, 1):
2422 	case IP_VERSION(11, 0, 4):
2423 	case IP_VERSION(11, 5, 0):
2424 		adev->flags |= AMD_IS_APU;
2425 		break;
2426 	default:
2427 		break;
2428 	}
2429 
2430 	if (amdgpu_ip_version(adev, XGMI_HWIP, 0) == IP_VERSION(4, 8, 0))
2431 		adev->gmc.xgmi.supported = true;
2432 
2433 	/* set NBIO version */
2434 	switch (amdgpu_ip_version(adev, NBIO_HWIP, 0)) {
2435 	case IP_VERSION(6, 1, 0):
2436 	case IP_VERSION(6, 2, 0):
2437 		adev->nbio.funcs = &nbio_v6_1_funcs;
2438 		adev->nbio.hdp_flush_reg = &nbio_v6_1_hdp_flush_reg;
2439 		break;
2440 	case IP_VERSION(7, 0, 0):
2441 	case IP_VERSION(7, 0, 1):
2442 	case IP_VERSION(2, 5, 0):
2443 		adev->nbio.funcs = &nbio_v7_0_funcs;
2444 		adev->nbio.hdp_flush_reg = &nbio_v7_0_hdp_flush_reg;
2445 		break;
2446 	case IP_VERSION(7, 4, 0):
2447 	case IP_VERSION(7, 4, 1):
2448 	case IP_VERSION(7, 4, 4):
2449 		adev->nbio.funcs = &nbio_v7_4_funcs;
2450 		adev->nbio.hdp_flush_reg = &nbio_v7_4_hdp_flush_reg;
2451 		break;
2452 	case IP_VERSION(7, 9, 0):
2453 		adev->nbio.funcs = &nbio_v7_9_funcs;
2454 		adev->nbio.hdp_flush_reg = &nbio_v7_9_hdp_flush_reg;
2455 		break;
2456 	case IP_VERSION(7, 11, 0):
2457 		adev->nbio.funcs = &nbio_v7_11_funcs;
2458 		adev->nbio.hdp_flush_reg = &nbio_v7_11_hdp_flush_reg;
2459 		break;
2460 	case IP_VERSION(7, 2, 0):
2461 	case IP_VERSION(7, 2, 1):
2462 	case IP_VERSION(7, 3, 0):
2463 	case IP_VERSION(7, 5, 0):
2464 	case IP_VERSION(7, 5, 1):
2465 		adev->nbio.funcs = &nbio_v7_2_funcs;
2466 		adev->nbio.hdp_flush_reg = &nbio_v7_2_hdp_flush_reg;
2467 		break;
2468 	case IP_VERSION(2, 1, 1):
2469 	case IP_VERSION(2, 3, 0):
2470 	case IP_VERSION(2, 3, 1):
2471 	case IP_VERSION(2, 3, 2):
2472 	case IP_VERSION(3, 3, 0):
2473 	case IP_VERSION(3, 3, 1):
2474 	case IP_VERSION(3, 3, 2):
2475 	case IP_VERSION(3, 3, 3):
2476 		adev->nbio.funcs = &nbio_v2_3_funcs;
2477 		adev->nbio.hdp_flush_reg = &nbio_v2_3_hdp_flush_reg;
2478 		break;
2479 	case IP_VERSION(4, 3, 0):
2480 	case IP_VERSION(4, 3, 1):
2481 		if (amdgpu_sriov_vf(adev))
2482 			adev->nbio.funcs = &nbio_v4_3_sriov_funcs;
2483 		else
2484 			adev->nbio.funcs = &nbio_v4_3_funcs;
2485 		adev->nbio.hdp_flush_reg = &nbio_v4_3_hdp_flush_reg;
2486 		break;
2487 	case IP_VERSION(7, 7, 0):
2488 	case IP_VERSION(7, 7, 1):
2489 		adev->nbio.funcs = &nbio_v7_7_funcs;
2490 		adev->nbio.hdp_flush_reg = &nbio_v7_7_hdp_flush_reg;
2491 		break;
2492 	default:
2493 		break;
2494 	}
2495 
2496 	switch (amdgpu_ip_version(adev, HDP_HWIP, 0)) {
2497 	case IP_VERSION(4, 0, 0):
2498 	case IP_VERSION(4, 0, 1):
2499 	case IP_VERSION(4, 1, 0):
2500 	case IP_VERSION(4, 1, 1):
2501 	case IP_VERSION(4, 1, 2):
2502 	case IP_VERSION(4, 2, 0):
2503 	case IP_VERSION(4, 2, 1):
2504 	case IP_VERSION(4, 4, 0):
2505 	case IP_VERSION(4, 4, 2):
2506 		adev->hdp.funcs = &hdp_v4_0_funcs;
2507 		break;
2508 	case IP_VERSION(5, 0, 0):
2509 	case IP_VERSION(5, 0, 1):
2510 	case IP_VERSION(5, 0, 2):
2511 	case IP_VERSION(5, 0, 3):
2512 	case IP_VERSION(5, 0, 4):
2513 	case IP_VERSION(5, 2, 0):
2514 		adev->hdp.funcs = &hdp_v5_0_funcs;
2515 		break;
2516 	case IP_VERSION(5, 2, 1):
2517 		adev->hdp.funcs = &hdp_v5_2_funcs;
2518 		break;
2519 	case IP_VERSION(6, 0, 0):
2520 	case IP_VERSION(6, 0, 1):
2521 	case IP_VERSION(6, 1, 0):
2522 		adev->hdp.funcs = &hdp_v6_0_funcs;
2523 		break;
2524 	default:
2525 		break;
2526 	}
2527 
2528 	switch (amdgpu_ip_version(adev, DF_HWIP, 0)) {
2529 	case IP_VERSION(3, 6, 0):
2530 	case IP_VERSION(3, 6, 1):
2531 	case IP_VERSION(3, 6, 2):
2532 		adev->df.funcs = &df_v3_6_funcs;
2533 		break;
2534 	case IP_VERSION(2, 1, 0):
2535 	case IP_VERSION(2, 1, 1):
2536 	case IP_VERSION(2, 5, 0):
2537 	case IP_VERSION(3, 5, 1):
2538 	case IP_VERSION(3, 5, 2):
2539 		adev->df.funcs = &df_v1_7_funcs;
2540 		break;
2541 	case IP_VERSION(4, 3, 0):
2542 		adev->df.funcs = &df_v4_3_funcs;
2543 		break;
2544 	default:
2545 		break;
2546 	}
2547 
2548 	switch (amdgpu_ip_version(adev, SMUIO_HWIP, 0)) {
2549 	case IP_VERSION(9, 0, 0):
2550 	case IP_VERSION(9, 0, 1):
2551 	case IP_VERSION(10, 0, 0):
2552 	case IP_VERSION(10, 0, 1):
2553 	case IP_VERSION(10, 0, 2):
2554 		adev->smuio.funcs = &smuio_v9_0_funcs;
2555 		break;
2556 	case IP_VERSION(11, 0, 0):
2557 	case IP_VERSION(11, 0, 2):
2558 	case IP_VERSION(11, 0, 3):
2559 	case IP_VERSION(11, 0, 4):
2560 	case IP_VERSION(11, 0, 7):
2561 	case IP_VERSION(11, 0, 8):
2562 		adev->smuio.funcs = &smuio_v11_0_funcs;
2563 		break;
2564 	case IP_VERSION(11, 0, 6):
2565 	case IP_VERSION(11, 0, 10):
2566 	case IP_VERSION(11, 0, 11):
2567 	case IP_VERSION(11, 5, 0):
2568 	case IP_VERSION(13, 0, 1):
2569 	case IP_VERSION(13, 0, 9):
2570 	case IP_VERSION(13, 0, 10):
2571 		adev->smuio.funcs = &smuio_v11_0_6_funcs;
2572 		break;
2573 	case IP_VERSION(13, 0, 2):
2574 		adev->smuio.funcs = &smuio_v13_0_funcs;
2575 		break;
2576 	case IP_VERSION(13, 0, 3):
2577 		adev->smuio.funcs = &smuio_v13_0_3_funcs;
2578 		if (adev->smuio.funcs->get_pkg_type(adev) == AMDGPU_PKG_TYPE_APU) {
2579 			adev->flags |= AMD_IS_APU;
2580 		}
2581 		break;
2582 	case IP_VERSION(13, 0, 6):
2583 	case IP_VERSION(13, 0, 8):
2584 	case IP_VERSION(14, 0, 0):
2585 		adev->smuio.funcs = &smuio_v13_0_6_funcs;
2586 		break;
2587 	default:
2588 		break;
2589 	}
2590 
2591 	switch (amdgpu_ip_version(adev, LSDMA_HWIP, 0)) {
2592 	case IP_VERSION(6, 0, 0):
2593 	case IP_VERSION(6, 0, 1):
2594 	case IP_VERSION(6, 0, 2):
2595 	case IP_VERSION(6, 0, 3):
2596 		adev->lsdma.funcs = &lsdma_v6_0_funcs;
2597 		break;
2598 	default:
2599 		break;
2600 	}
2601 
2602 	r = amdgpu_discovery_set_common_ip_blocks(adev);
2603 	if (r)
2604 		return r;
2605 
2606 	r = amdgpu_discovery_set_gmc_ip_blocks(adev);
2607 	if (r)
2608 		return r;
2609 
2610 	/* For SR-IOV, PSP needs to be initialized before IH */
2611 	if (amdgpu_sriov_vf(adev)) {
2612 		r = amdgpu_discovery_set_psp_ip_blocks(adev);
2613 		if (r)
2614 			return r;
2615 		r = amdgpu_discovery_set_ih_ip_blocks(adev);
2616 		if (r)
2617 			return r;
2618 	} else {
2619 		r = amdgpu_discovery_set_ih_ip_blocks(adev);
2620 		if (r)
2621 			return r;
2622 
2623 		if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)) {
2624 			r = amdgpu_discovery_set_psp_ip_blocks(adev);
2625 			if (r)
2626 				return r;
2627 		}
2628 	}
2629 
2630 	if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)) {
2631 		r = amdgpu_discovery_set_smu_ip_blocks(adev);
2632 		if (r)
2633 			return r;
2634 	}
2635 
2636 	r = amdgpu_discovery_set_display_ip_blocks(adev);
2637 	if (r)
2638 		return r;
2639 
2640 	r = amdgpu_discovery_set_gc_ip_blocks(adev);
2641 	if (r)
2642 		return r;
2643 
2644 	r = amdgpu_discovery_set_sdma_ip_blocks(adev);
2645 	if (r)
2646 		return r;
2647 
2648 	if ((adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT &&
2649 	     !amdgpu_sriov_vf(adev)) ||
2650 	    (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO && amdgpu_dpm == 1)) {
2651 		r = amdgpu_discovery_set_smu_ip_blocks(adev);
2652 		if (r)
2653 			return r;
2654 	}
2655 
2656 	r = amdgpu_discovery_set_mm_ip_blocks(adev);
2657 	if (r)
2658 		return r;
2659 
2660 	r = amdgpu_discovery_set_mes_ip_blocks(adev);
2661 	if (r)
2662 		return r;
2663 
2664 	r = amdgpu_discovery_set_vpe_ip_blocks(adev);
2665 	if (r)
2666 		return r;
2667 
2668 	r = amdgpu_discovery_set_umsch_mm_ip_blocks(adev);
2669 	if (r)
2670 		return r;
2671 
2672 	return 0;
2673 }
2674 
2675