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