amdgpu_discovery.c 70.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2018 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

24 25
#include <linux/firmware.h>

26 27 28 29 30
#include "amdgpu.h"
#include "amdgpu_discovery.h"
#include "soc15_hw_ip.h"
#include "discovery.h"

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "soc15.h"
#include "gfx_v9_0.h"
#include "gmc_v9_0.h"
#include "df_v1_7.h"
#include "df_v3_6.h"
#include "nbio_v6_1.h"
#include "nbio_v7_0.h"
#include "nbio_v7_4.h"
#include "hdp_v4_0.h"
#include "vega10_ih.h"
#include "vega20_ih.h"
#include "sdma_v4_0.h"
#include "uvd_v7_0.h"
#include "vce_v4_0.h"
#include "vcn_v1_0.h"
#include "vcn_v2_5.h"
#include "jpeg_v2_5.h"
#include "smuio_v9_0.h"
49
#include "gmc_v10_0.h"
50
#include "gmc_v11_0.h"
51 52 53
#include "gfxhub_v2_0.h"
#include "mmhub_v2_0.h"
#include "nbio_v2_3.h"
54
#include "nbio_v4_3.h"
55
#include "nbio_v7_2.h"
56
#include "nbio_v7_7.h"
57
#include "hdp_v5_0.h"
58
#include "hdp_v5_2.h"
L
Likun Gao 已提交
59
#include "hdp_v6_0.h"
60
#include "nv.h"
61
#include "soc21.h"
62
#include "navi10_ih.h"
L
Likun Gao 已提交
63
#include "ih_v6_0.h"
64
#include "gfx_v10_0.h"
65
#include "gfx_v11_0.h"
66 67
#include "sdma_v5_0.h"
#include "sdma_v5_2.h"
68
#include "sdma_v6_0.h"
L
Likun Gao 已提交
69
#include "lsdma_v6_0.h"
70 71 72 73
#include "vcn_v2_0.h"
#include "jpeg_v2_0.h"
#include "vcn_v3_0.h"
#include "jpeg_v3_0.h"
74 75
#include "vcn_v4_0.h"
#include "jpeg_v4_0.h"
76 77
#include "amdgpu_vkms.h"
#include "mes_v10_1.h"
78
#include "mes_v11_0.h"
79 80
#include "smuio_v11_0.h"
#include "smuio_v11_0_6.h"
81
#include "smuio_v13_0.h"
82
#include "smuio_v13_0_6.h"
83

84 85
#define FIRMWARE_IP_DISCOVERY "amdgpu/ip_discovery.bin"
MODULE_FIRMWARE(FIRMWARE_IP_DISCOVERY);
86

87 88 89 90
#define mmRCC_CONFIG_MEMSIZE	0xde3
#define mmMM_INDEX		0x0
#define mmMM_INDEX_HI		0x6
#define mmMM_DATA		0x1
91

92
static const char *hw_id_names[HW_ID_MAX] = {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	[MP1_HWID]		= "MP1",
	[MP2_HWID]		= "MP2",
	[THM_HWID]		= "THM",
	[SMUIO_HWID]		= "SMUIO",
	[FUSE_HWID]		= "FUSE",
	[CLKA_HWID]		= "CLKA",
	[PWR_HWID]		= "PWR",
	[GC_HWID]		= "GC",
	[UVD_HWID]		= "UVD",
	[AUDIO_AZ_HWID]		= "AUDIO_AZ",
	[ACP_HWID]		= "ACP",
	[DCI_HWID]		= "DCI",
	[DMU_HWID]		= "DMU",
	[DCO_HWID]		= "DCO",
	[DIO_HWID]		= "DIO",
	[XDMA_HWID]		= "XDMA",
	[DCEAZ_HWID]		= "DCEAZ",
	[DAZ_HWID]		= "DAZ",
	[SDPMUX_HWID]		= "SDPMUX",
	[NTB_HWID]		= "NTB",
	[IOHC_HWID]		= "IOHC",
	[L2IMU_HWID]		= "L2IMU",
	[VCE_HWID]		= "VCE",
	[MMHUB_HWID]		= "MMHUB",
	[ATHUB_HWID]		= "ATHUB",
	[DBGU_NBIO_HWID]	= "DBGU_NBIO",
	[DFX_HWID]		= "DFX",
	[DBGU0_HWID]		= "DBGU0",
	[DBGU1_HWID]		= "DBGU1",
	[OSSSYS_HWID]		= "OSSSYS",
	[HDP_HWID]		= "HDP",
	[SDMA0_HWID]		= "SDMA0",
	[SDMA1_HWID]		= "SDMA1",
126 127
	[SDMA2_HWID]		= "SDMA2",
	[SDMA3_HWID]		= "SDMA3",
L
Likun Gao 已提交
128
	[LSDMA_HWID]		= "LSDMA",
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	[ISP_HWID]		= "ISP",
	[DBGU_IO_HWID]		= "DBGU_IO",
	[DF_HWID]		= "DF",
	[CLKB_HWID]		= "CLKB",
	[FCH_HWID]		= "FCH",
	[DFX_DAP_HWID]		= "DFX_DAP",
	[L1IMU_PCIE_HWID]	= "L1IMU_PCIE",
	[L1IMU_NBIF_HWID]	= "L1IMU_NBIF",
	[L1IMU_IOAGR_HWID]	= "L1IMU_IOAGR",
	[L1IMU3_HWID]		= "L1IMU3",
	[L1IMU4_HWID]		= "L1IMU4",
	[L1IMU5_HWID]		= "L1IMU5",
	[L1IMU6_HWID]		= "L1IMU6",
	[L1IMU7_HWID]		= "L1IMU7",
	[L1IMU8_HWID]		= "L1IMU8",
	[L1IMU9_HWID]		= "L1IMU9",
	[L1IMU10_HWID]		= "L1IMU10",
	[L1IMU11_HWID]		= "L1IMU11",
	[L1IMU12_HWID]		= "L1IMU12",
	[L1IMU13_HWID]		= "L1IMU13",
	[L1IMU14_HWID]		= "L1IMU14",
	[L1IMU15_HWID]		= "L1IMU15",
	[WAFLC_HWID]		= "WAFLC",
	[FCH_USB_PD_HWID]	= "FCH_USB_PD",
	[PCIE_HWID]		= "PCIE",
	[PCS_HWID]		= "PCS",
	[DDCL_HWID]		= "DDCL",
	[SST_HWID]		= "SST",
	[IOAGR_HWID]		= "IOAGR",
	[NBIF_HWID]		= "NBIF",
	[IOAPIC_HWID]		= "IOAPIC",
	[SYSTEMHUB_HWID]	= "SYSTEMHUB",
	[NTBCCP_HWID]		= "NTBCCP",
	[UMC_HWID]		= "UMC",
	[SATA_HWID]		= "SATA",
	[USB_HWID]		= "USB",
	[CCXSEC_HWID]		= "CCXSEC",
	[XGMI_HWID]		= "XGMI",
	[XGBE_HWID]		= "XGBE",
	[MP0_HWID]		= "MP0",
};

static int hw_id_map[MAX_HWIP] = {
	[GC_HWIP]	= GC_HWID,
	[HDP_HWIP]	= HDP_HWID,
	[SDMA0_HWIP]	= SDMA0_HWID,
	[SDMA1_HWIP]	= SDMA1_HWID,
176 177
	[SDMA2_HWIP]    = SDMA2_HWID,
	[SDMA3_HWIP]    = SDMA3_HWID,
L
Likun Gao 已提交
178
	[LSDMA_HWIP]    = LSDMA_HWID,
179 180
	[MMHUB_HWIP]	= MMHUB_HWID,
	[ATHUB_HWIP]	= ATHUB_HWID,
181
	[NBIO_HWIP]	= NBIF_HWID,
182 183 184 185 186
	[MP0_HWIP]	= MP0_HWID,
	[MP1_HWIP]	= MP1_HWID,
	[UVD_HWIP]	= UVD_HWID,
	[VCE_HWIP]	= VCE_HWID,
	[DF_HWIP]	= DF_HWID,
187
	[DCE_HWIP]	= DMU_HWID,
188 189 190 191 192 193
	[OSSSYS_HWIP]	= OSSSYS_HWID,
	[SMUIO_HWIP]	= SMUIO_HWID,
	[PWR_HWIP]	= PWR_HWID,
	[NBIF_HWIP]	= NBIF_HWID,
	[THM_HWIP]	= THM_HWID,
	[CLK_HWIP]	= CLKA_HWID,
194
	[UMC_HWIP]	= UMC_HWID,
A
Alex Deucher 已提交
195
	[XGMI_HWIP]	= XGMI_HWID,
A
Alex Deucher 已提交
196
	[DCI_HWIP]	= DCI_HWID,
197
	[PCIE_HWIP]	= PCIE_HWID,
198 199
};

200
static int amdgpu_discovery_read_binary_from_vram(struct amdgpu_device *adev, uint8_t *binary)
201
{
202
	uint64_t vram_size = (uint64_t)RREG32(mmRCC_CONFIG_MEMSIZE) << 20;
203
	uint64_t pos = vram_size - DISCOVERY_TMR_OFFSET;
204

205
	amdgpu_device_vram_access(adev, pos, (uint32_t *)binary,
206
				  adev->mman.discovery_tmr_size, false);
207 208 209
	return 0;
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
static int amdgpu_discovery_read_binary_from_file(struct amdgpu_device *adev, uint8_t *binary)
{
	const struct firmware *fw;
	const char *fw_name;
	int r;

	switch (amdgpu_discovery) {
	case 2:
		fw_name = FIRMWARE_IP_DISCOVERY;
		break;
	default:
		dev_warn(adev->dev, "amdgpu_discovery is not set properly\n");
		return -EINVAL;
	}

	r = request_firmware(&fw, fw_name, adev->dev);
	if (r) {
		dev_err(adev->dev, "can't load firmware \"%s\"\n",
			fw_name);
		return r;
	}

232
	memcpy((u8 *)binary, (u8 *)fw->data, fw->size);
233 234 235 236 237
	release_firmware(fw);

	return 0;
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
static uint16_t amdgpu_discovery_calculate_checksum(uint8_t *data, uint32_t size)
{
	uint16_t checksum = 0;
	int i;

	for (i = 0; i < size; i++)
		checksum += data[i];

	return checksum;
}

static inline bool amdgpu_discovery_verify_checksum(uint8_t *data, uint32_t size,
						    uint16_t expected)
{
	return !!(amdgpu_discovery_calculate_checksum(data, size) == expected);
}

255 256 257 258 259 260 261 262
static inline bool amdgpu_discovery_verify_binary_signature(uint8_t *binary)
{
	struct binary_header *bhdr;
	bhdr = (struct binary_header *)binary;

	return (le32_to_cpu(bhdr->binary_signature) == BINARY_SIGNATURE);
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
static void amdgpu_discovery_harvest_config_quirk(struct amdgpu_device *adev)
{
	/*
	 * So far, apply this quirk only on those Navy Flounder boards which
	 * have a bad harvest table of VCN config.
	 */
	if ((adev->ip_versions[UVD_HWIP][1] == IP_VERSION(3, 0, 1)) &&
		(adev->ip_versions[GC_HWIP][0] == IP_VERSION(10, 3, 2))) {
		switch (adev->pdev->revision) {
		case 0xC1:
		case 0xC2:
		case 0xC3:
		case 0xC5:
		case 0xC7:
		case 0xCF:
		case 0xDF:
			adev->vcn.harvest_config |= AMDGPU_VCN_HARVEST_VCN1;
			break;
		default:
			break;
		}
	}
}

287
static int amdgpu_discovery_init(struct amdgpu_device *adev)
288 289 290 291 292 293 294 295
{
	struct table_info *info;
	struct binary_header *bhdr;
	uint16_t offset;
	uint16_t size;
	uint16_t checksum;
	int r;

296 297 298
	adev->mman.discovery_tmr_size = DISCOVERY_TMR_SIZE;
	adev->mman.discovery_bin = kzalloc(adev->mman.discovery_tmr_size, GFP_KERNEL);
	if (!adev->mman.discovery_bin)
299 300
		return -ENOMEM;

301 302 303 304 305 306 307
	r = amdgpu_discovery_read_binary_from_vram(adev, adev->mman.discovery_bin);
	if (r) {
		dev_err(adev->dev, "failed to read ip discovery binary from vram\n");
		r = -EINVAL;
		goto out;
	}

308 309 310 311 312 313 314
	if (!amdgpu_discovery_verify_binary_signature(adev->mman.discovery_bin) || amdgpu_discovery == 2) {
		/* ignore the discovery binary from vram if discovery=2 in kernel module parameter */
		if (amdgpu_discovery == 2)
			dev_info(adev->dev,"force read ip discovery binary from file");
		else
			dev_warn(adev->dev, "get invalid ip discovery binary signature from vram\n");

315 316
		/* retry read ip discovery binary from file */
		r = amdgpu_discovery_read_binary_from_file(adev, adev->mman.discovery_bin);
317
		if (r) {
318 319 320 321 322 323 324 325
			dev_err(adev->dev, "failed to read ip discovery binary from file\n");
			r = -EINVAL;
			goto out;
		}
		/* check the ip discovery binary signature */
		if(!amdgpu_discovery_verify_binary_signature(adev->mman.discovery_bin)) {
			dev_warn(adev->dev, "get invalid ip discovery binary signature from file\n");
			r = -EINVAL;
326 327
			goto out;
		}
328 329
	}

330
	bhdr = (struct binary_header *)adev->mman.discovery_bin;
331

332
	offset = offsetof(struct binary_header, binary_checksum) +
333
		sizeof(bhdr->binary_checksum);
334 335
	size = le16_to_cpu(bhdr->binary_size) - offset;
	checksum = le16_to_cpu(bhdr->binary_checksum);
336

337
	if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
338
					      size, checksum)) {
339
		dev_err(adev->dev, "invalid ip discovery binary checksum\n");
340 341 342 343 344 345 346 347
		r = -EINVAL;
		goto out;
	}

	info = &bhdr->table_list[IP_DISCOVERY];
	offset = le16_to_cpu(info->offset);
	checksum = le16_to_cpu(info->checksum);

348 349 350 351 352 353 354 355
	if (offset) {
		struct ip_discovery_header *ihdr =
			(struct ip_discovery_header *)(adev->mman.discovery_bin + offset);
		if (le32_to_cpu(ihdr->signature) != DISCOVERY_TABLE_SIGNATURE) {
			dev_err(adev->dev, "invalid ip discovery data table signature\n");
			r = -EINVAL;
			goto out;
		}
356

357 358 359 360 361 362
		if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
						      le16_to_cpu(ihdr->size), checksum)) {
			dev_err(adev->dev, "invalid ip discovery data table checksum\n");
			r = -EINVAL;
			goto out;
		}
363 364 365 366 367 368
	}

	info = &bhdr->table_list[GC];
	offset = le16_to_cpu(info->offset);
	checksum = le16_to_cpu(info->checksum);

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	if (offset) {
		struct gpu_info_header *ghdr =
			(struct gpu_info_header *)(adev->mman.discovery_bin + offset);

		if (le32_to_cpu(ghdr->table_id) != GC_TABLE_ID) {
			dev_err(adev->dev, "invalid ip discovery gc table id\n");
			r = -EINVAL;
			goto out;
		}

		if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
						      le32_to_cpu(ghdr->size), checksum)) {
			dev_err(adev->dev, "invalid gc data table checksum\n");
			r = -EINVAL;
			goto out;
		}
	}

	info = &bhdr->table_list[HARVEST_INFO];
	offset = le16_to_cpu(info->offset);
	checksum = le16_to_cpu(info->checksum);

	if (offset) {
		struct harvest_info_header *hhdr =
			(struct harvest_info_header *)(adev->mman.discovery_bin + offset);

		if (le32_to_cpu(hhdr->signature) != HARVEST_TABLE_SIGNATURE) {
			dev_err(adev->dev, "invalid ip discovery harvest table signature\n");
			r = -EINVAL;
			goto out;
		}

		if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
						      sizeof(struct harvest_table), checksum)) {
			dev_err(adev->dev, "invalid harvest data table checksum\n");
			r = -EINVAL;
			goto out;
		}
	}

	info = &bhdr->table_list[VCN_INFO];
	offset = le16_to_cpu(info->offset);
	checksum = le16_to_cpu(info->checksum);

	if (offset) {
		struct vcn_info_header *vhdr =
			(struct vcn_info_header *)(adev->mman.discovery_bin + offset);

		if (le32_to_cpu(vhdr->table_id) != VCN_INFO_TABLE_ID) {
			dev_err(adev->dev, "invalid ip discovery vcn table id\n");
			r = -EINVAL;
			goto out;
		}

		if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
						      le32_to_cpu(vhdr->size_bytes), checksum)) {
			dev_err(adev->dev, "invalid vcn data table checksum\n");
			r = -EINVAL;
			goto out;
		}
	}

	info = &bhdr->table_list[MALL_INFO];
	offset = le16_to_cpu(info->offset);
	checksum = le16_to_cpu(info->checksum);

	if (0 && offset) {
		struct mall_info_header *mhdr =
			(struct mall_info_header *)(adev->mman.discovery_bin + offset);

		if (le32_to_cpu(mhdr->table_id) != MALL_INFO_TABLE_ID) {
			dev_err(adev->dev, "invalid ip discovery mall table id\n");
			r = -EINVAL;
			goto out;
		}

		if (!amdgpu_discovery_verify_checksum(adev->mman.discovery_bin + offset,
						      le32_to_cpu(mhdr->size_bytes), checksum)) {
			dev_err(adev->dev, "invalid mall data table checksum\n");
			r = -EINVAL;
			goto out;
		}
451 452 453 454 455
	}

	return 0;

out:
456 457
	kfree(adev->mman.discovery_bin);
	adev->mman.discovery_bin = NULL;
458 459 460 461

	return r;
}

462 463
static void amdgpu_discovery_sysfs_fini(struct amdgpu_device *adev);

464 465
void amdgpu_discovery_fini(struct amdgpu_device *adev)
{
466
	amdgpu_discovery_sysfs_fini(adev);
467 468
	kfree(adev->mman.discovery_bin);
	adev->mman.discovery_bin = NULL;
469 470
}

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
static int amdgpu_discovery_validate_ip(const struct ip *ip)
{
	if (ip->number_instance >= HWIP_MAX_INSTANCE) {
		DRM_ERROR("Unexpected number_instance (%d) from ip discovery blob\n",
			  ip->number_instance);
		return -EINVAL;
	}
	if (le16_to_cpu(ip->hw_id) >= HW_ID_MAX) {
		DRM_ERROR("Unexpected hw_id (%d) from ip discovery blob\n",
			  le16_to_cpu(ip->hw_id));
		return -EINVAL;
	}

	return 0;
}

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
static void amdgpu_discovery_read_harvest_bit_per_ip(struct amdgpu_device *adev,
						uint32_t *vcn_harvest_count)
{
	struct binary_header *bhdr;
	struct ip_discovery_header *ihdr;
	struct die_header *dhdr;
	struct ip *ip;
	uint16_t die_offset, ip_offset, num_dies, num_ips;
	int i, j;

	bhdr = (struct binary_header *)adev->mman.discovery_bin;
	ihdr = (struct ip_discovery_header *)(adev->mman.discovery_bin +
			le16_to_cpu(bhdr->table_list[IP_DISCOVERY].offset));
	num_dies = le16_to_cpu(ihdr->num_dies);

	/* scan harvest bit of all IP data structures */
	for (i = 0; i < num_dies; i++) {
		die_offset = le16_to_cpu(ihdr->die_info[i].die_offset);
		dhdr = (struct die_header *)(adev->mman.discovery_bin + die_offset);
		num_ips = le16_to_cpu(dhdr->num_ips);
		ip_offset = die_offset + sizeof(*dhdr);

		for (j = 0; j < num_ips; j++) {
			ip = (struct ip *)(adev->mman.discovery_bin + ip_offset);

			if (amdgpu_discovery_validate_ip(ip))
				goto next_ip;

			if (le16_to_cpu(ip->harvest) == 1) {
				switch (le16_to_cpu(ip->hw_id)) {
				case VCN_HWID:
					(*vcn_harvest_count)++;
					if (ip->number_instance == 0)
						adev->vcn.harvest_config |= AMDGPU_VCN_HARVEST_VCN0;
					else
						adev->vcn.harvest_config |= AMDGPU_VCN_HARVEST_VCN1;
					break;
				case DMU_HWID:
					adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK;
					break;
				default:
					break;
                                }
                        }
next_ip:
532
			ip_offset += struct_size(ip, base_address, ip->num_base_address);
533 534 535 536
		}
	}
}

537
static void amdgpu_discovery_read_from_harvest_table(struct amdgpu_device *adev,
538 539
						     uint32_t *vcn_harvest_count,
						     uint32_t *umc_harvest_count)
540 541 542
{
	struct binary_header *bhdr;
	struct harvest_table *harvest_info;
543
	u16 offset;
544 545 546
	int i;

	bhdr = (struct binary_header *)adev->mman.discovery_bin;
547 548 549 550 551 552 553 554 555
	offset = le16_to_cpu(bhdr->table_list[HARVEST_INFO].offset);

	if (!offset) {
		dev_err(adev->dev, "invalid harvest table offset\n");
		return;
	}

	harvest_info = (struct harvest_table *)(adev->mman.discovery_bin + offset);

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
	for (i = 0; i < 32; i++) {
		if (le16_to_cpu(harvest_info->list[i].hw_id) == 0)
			break;

		switch (le16_to_cpu(harvest_info->list[i].hw_id)) {
		case VCN_HWID:
			(*vcn_harvest_count)++;
			if (harvest_info->list[i].number_instance == 0)
				adev->vcn.harvest_config |= AMDGPU_VCN_HARVEST_VCN0;
			else
				adev->vcn.harvest_config |= AMDGPU_VCN_HARVEST_VCN1;
			break;
		case DMU_HWID:
			adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK;
			break;
571 572 573
		case UMC_HWID:
			(*umc_harvest_count)++;
			break;
574 575 576 577 578 579
		default:
			break;
		}
	}
}

580 581 582 583 584 585 586 587
/* ================================================== */

struct ip_hw_instance {
	struct kobject kobj; /* ip_discovery/die/#die/#hw_id/#instance/<attrs...> */

	int hw_id;
	u8  num_instance;
	u8  major, minor, revision;
588
	u8  harvest;
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

	int num_base_addresses;
	u32 base_addr[];
};

struct ip_hw_id {
	struct kset hw_id_kset;  /* ip_discovery/die/#die/#hw_id/, contains ip_hw_instance */
	int hw_id;
};

struct ip_die_entry {
	struct kset ip_kset;     /* ip_discovery/die/#die/, contains ip_hw_id  */
	u16 num_ips;
};

/* -------------------------------------------------- */

struct ip_hw_instance_attr {
	struct attribute attr;
	ssize_t (*show)(struct ip_hw_instance *ip_hw_instance, char *buf);
};

static ssize_t hw_id_show(struct ip_hw_instance *ip_hw_instance, char *buf)
{
	return sysfs_emit(buf, "%d\n", ip_hw_instance->hw_id);
}

static ssize_t num_instance_show(struct ip_hw_instance *ip_hw_instance, char *buf)
{
	return sysfs_emit(buf, "%d\n", ip_hw_instance->num_instance);
}

static ssize_t major_show(struct ip_hw_instance *ip_hw_instance, char *buf)
{
	return sysfs_emit(buf, "%d\n", ip_hw_instance->major);
}

static ssize_t minor_show(struct ip_hw_instance *ip_hw_instance, char *buf)
{
	return sysfs_emit(buf, "%d\n", ip_hw_instance->minor);
}

static ssize_t revision_show(struct ip_hw_instance *ip_hw_instance, char *buf)
{
	return sysfs_emit(buf, "%d\n", ip_hw_instance->revision);
}

636 637 638 639 640
static ssize_t harvest_show(struct ip_hw_instance *ip_hw_instance, char *buf)
{
	return sysfs_emit(buf, "0x%01X\n", ip_hw_instance->harvest);
}

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
static ssize_t num_base_addresses_show(struct ip_hw_instance *ip_hw_instance, char *buf)
{
	return sysfs_emit(buf, "%d\n", ip_hw_instance->num_base_addresses);
}

static ssize_t base_addr_show(struct ip_hw_instance *ip_hw_instance, char *buf)
{
	ssize_t res, at;
	int ii;

	for (res = at = ii = 0; ii < ip_hw_instance->num_base_addresses; ii++) {
		/* Here we satisfy the condition that, at + size <= PAGE_SIZE.
		 */
		if (at + 12 > PAGE_SIZE)
			break;
		res = sysfs_emit_at(buf, at, "0x%08X\n",
				    ip_hw_instance->base_addr[ii]);
		if (res <= 0)
			break;
		at += res;
	}

	return res < 0 ? res : at;
}

static struct ip_hw_instance_attr ip_hw_attr[] = {
	__ATTR_RO(hw_id),
	__ATTR_RO(num_instance),
	__ATTR_RO(major),
	__ATTR_RO(minor),
	__ATTR_RO(revision),
672
	__ATTR_RO(harvest),
673 674 675 676
	__ATTR_RO(num_base_addresses),
	__ATTR_RO(base_addr),
};

677
static struct attribute *ip_hw_instance_attrs[ARRAY_SIZE(ip_hw_attr) + 1];
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
ATTRIBUTE_GROUPS(ip_hw_instance);

#define to_ip_hw_instance(x) container_of(x, struct ip_hw_instance, kobj)
#define to_ip_hw_instance_attr(x) container_of(x, struct ip_hw_instance_attr, attr)

static ssize_t ip_hw_instance_attr_show(struct kobject *kobj,
					struct attribute *attr,
					char *buf)
{
	struct ip_hw_instance *ip_hw_instance = to_ip_hw_instance(kobj);
	struct ip_hw_instance_attr *ip_hw_attr = to_ip_hw_instance_attr(attr);

	if (!ip_hw_attr->show)
		return -EIO;

	return ip_hw_attr->show(ip_hw_instance, buf);
}

static const struct sysfs_ops ip_hw_instance_sysfs_ops = {
	.show = ip_hw_instance_attr_show,
};

static void ip_hw_instance_release(struct kobject *kobj)
{
	struct ip_hw_instance *ip_hw_instance = to_ip_hw_instance(kobj);

	kfree(ip_hw_instance);
}

static struct kobj_type ip_hw_instance_ktype = {
	.release = ip_hw_instance_release,
	.sysfs_ops = &ip_hw_instance_sysfs_ops,
	.default_groups = ip_hw_instance_groups,
};

/* -------------------------------------------------- */

#define to_ip_hw_id(x)  container_of(to_kset(x), struct ip_hw_id, hw_id_kset)

static void ip_hw_id_release(struct kobject *kobj)
{
	struct ip_hw_id *ip_hw_id = to_ip_hw_id(kobj);

	if (!list_empty(&ip_hw_id->hw_id_kset.list))
		DRM_ERROR("ip_hw_id->hw_id_kset is not empty");
	kfree(ip_hw_id);
}

static struct kobj_type ip_hw_id_ktype = {
	.release = ip_hw_id_release,
	.sysfs_ops = &kobj_sysfs_ops,
};

/* -------------------------------------------------- */

static void die_kobj_release(struct kobject *kobj);
static void ip_disc_release(struct kobject *kobj);

struct ip_die_entry_attribute {
	struct attribute attr;
	ssize_t (*show)(struct ip_die_entry *ip_die_entry, char *buf);
};

#define to_ip_die_entry_attr(x)  container_of(x, struct ip_die_entry_attribute, attr)

static ssize_t num_ips_show(struct ip_die_entry *ip_die_entry, char *buf)
{
	return sysfs_emit(buf, "%d\n", ip_die_entry->num_ips);
}

/* If there are more ip_die_entry attrs, other than the number of IPs,
 * we can make this intro an array of attrs, and then initialize
 * ip_die_entry_attrs in a loop.
 */
static struct ip_die_entry_attribute num_ips_attr =
	__ATTR_RO(num_ips);

static struct attribute *ip_die_entry_attrs[] = {
	&num_ips_attr.attr,
	NULL,
};
ATTRIBUTE_GROUPS(ip_die_entry); /* ip_die_entry_groups */

#define to_ip_die_entry(x) container_of(to_kset(x), struct ip_die_entry, ip_kset)

static ssize_t ip_die_entry_attr_show(struct kobject *kobj,
				      struct attribute *attr,
				      char *buf)
{
	struct ip_die_entry_attribute *ip_die_entry_attr = to_ip_die_entry_attr(attr);
	struct ip_die_entry *ip_die_entry = to_ip_die_entry(kobj);

	if (!ip_die_entry_attr->show)
		return -EIO;

	return ip_die_entry_attr->show(ip_die_entry, buf);
}

static void ip_die_entry_release(struct kobject *kobj)
{
	struct ip_die_entry *ip_die_entry = to_ip_die_entry(kobj);

	if (!list_empty(&ip_die_entry->ip_kset.list))
		DRM_ERROR("ip_die_entry->ip_kset is not empty");
	kfree(ip_die_entry);
}

static const struct sysfs_ops ip_die_entry_sysfs_ops = {
	.show = ip_die_entry_attr_show,
};

static struct kobj_type ip_die_entry_ktype = {
	.release = ip_die_entry_release,
	.sysfs_ops = &ip_die_entry_sysfs_ops,
	.default_groups = ip_die_entry_groups,
};

static struct kobj_type die_kobj_ktype = {
	.release = die_kobj_release,
	.sysfs_ops = &kobj_sysfs_ops,
};

static struct kobj_type ip_discovery_ktype = {
	.release = ip_disc_release,
	.sysfs_ops = &kobj_sysfs_ops,
};

struct ip_discovery_top {
	struct kobject kobj;    /* ip_discovery/ */
	struct kset die_kset;   /* ip_discovery/die/, contains ip_die_entry */
	struct amdgpu_device *adev;
};

static void die_kobj_release(struct kobject *kobj)
{
	struct ip_discovery_top *ip_top = container_of(to_kset(kobj),
						       struct ip_discovery_top,
						       die_kset);
	if (!list_empty(&ip_top->die_kset.list))
		DRM_ERROR("ip_top->die_kset is not empty");
}

static void ip_disc_release(struct kobject *kobj)
{
	struct ip_discovery_top *ip_top = container_of(kobj, struct ip_discovery_top,
						       kobj);
	struct amdgpu_device *adev = ip_top->adev;

	adev->ip_top = NULL;
	kfree(ip_top);
}

static int amdgpu_discovery_sysfs_ips(struct amdgpu_device *adev,
				      struct ip_die_entry *ip_die_entry,
				      const size_t _ip_offset, const int num_ips)
{
	int ii, jj, kk, res;

	DRM_DEBUG("num_ips:%d", num_ips);

	/* Find all IPs of a given HW ID, and add their instance to
	 * #die/#hw_id/#instance/<attributes>
	 */
	for (ii = 0; ii < HW_ID_MAX; ii++) {
		struct ip_hw_id *ip_hw_id = NULL;
		size_t ip_offset = _ip_offset;

		for (jj = 0; jj < num_ips; jj++) {
			struct ip *ip;
			struct ip_hw_instance *ip_hw_instance;

			ip = (struct ip *)(adev->mman.discovery_bin + ip_offset);
			if (amdgpu_discovery_validate_ip(ip) ||
			    le16_to_cpu(ip->hw_id) != ii)
				goto next_ip;

854
			DRM_DEBUG("match:%d @ ip_offset:%zu", ii, ip_offset);
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

			/* We have a hw_id match; register the hw
			 * block if not yet registered.
			 */
			if (!ip_hw_id) {
				ip_hw_id = kzalloc(sizeof(*ip_hw_id), GFP_KERNEL);
				if (!ip_hw_id)
					return -ENOMEM;
				ip_hw_id->hw_id = ii;

				kobject_set_name(&ip_hw_id->hw_id_kset.kobj, "%d", ii);
				ip_hw_id->hw_id_kset.kobj.kset = &ip_die_entry->ip_kset;
				ip_hw_id->hw_id_kset.kobj.ktype = &ip_hw_id_ktype;
				res = kset_register(&ip_hw_id->hw_id_kset);
				if (res) {
					DRM_ERROR("Couldn't register ip_hw_id kset");
					kfree(ip_hw_id);
					return res;
				}
				if (hw_id_names[ii]) {
					res = sysfs_create_link(&ip_die_entry->ip_kset.kobj,
								&ip_hw_id->hw_id_kset.kobj,
								hw_id_names[ii]);
					if (res) {
						DRM_ERROR("Couldn't create IP link %s in IP Die:%s\n",
							  hw_id_names[ii],
							  kobject_name(&ip_die_entry->ip_kset.kobj));
					}
				}
			}

			/* Now register its instance.
			 */
			ip_hw_instance = kzalloc(struct_size(ip_hw_instance,
							     base_addr,
							     ip->num_base_address),
						 GFP_KERNEL);
			if (!ip_hw_instance) {
				DRM_ERROR("no memory for ip_hw_instance");
				return -ENOMEM;
			}
			ip_hw_instance->hw_id = le16_to_cpu(ip->hw_id); /* == ii */
			ip_hw_instance->num_instance = ip->number_instance;
			ip_hw_instance->major = ip->major;
			ip_hw_instance->minor = ip->minor;
			ip_hw_instance->revision = ip->revision;
901
			ip_hw_instance->harvest = ip->harvest;
902 903 904 905 906 907 908 909 910 911
			ip_hw_instance->num_base_addresses = ip->num_base_address;

			for (kk = 0; kk < ip_hw_instance->num_base_addresses; kk++)
				ip_hw_instance->base_addr[kk] = ip->base_address[kk];

			kobject_init(&ip_hw_instance->kobj, &ip_hw_instance_ktype);
			ip_hw_instance->kobj.kset = &ip_hw_id->hw_id_kset;
			res = kobject_add(&ip_hw_instance->kobj, NULL,
					  "%d", ip_hw_instance->num_instance);
next_ip:
912
			ip_offset += struct_size(ip, base_address, ip->num_base_address);
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
		}
	}

	return 0;
}

static int amdgpu_discovery_sysfs_recurse(struct amdgpu_device *adev)
{
	struct binary_header *bhdr;
	struct ip_discovery_header *ihdr;
	struct die_header *dhdr;
	struct kset *die_kset = &adev->ip_top->die_kset;
	u16 num_dies, die_offset, num_ips;
	size_t ip_offset;
	int ii, res;

	bhdr = (struct binary_header *)adev->mman.discovery_bin;
	ihdr = (struct ip_discovery_header *)(adev->mman.discovery_bin +
					      le16_to_cpu(bhdr->table_list[IP_DISCOVERY].offset));
	num_dies = le16_to_cpu(ihdr->num_dies);

	DRM_DEBUG("number of dies: %d\n", num_dies);

	for (ii = 0; ii < num_dies; ii++) {
		struct ip_die_entry *ip_die_entry;

		die_offset = le16_to_cpu(ihdr->die_info[ii].die_offset);
		dhdr = (struct die_header *)(adev->mman.discovery_bin + die_offset);
		num_ips = le16_to_cpu(dhdr->num_ips);
		ip_offset = die_offset + sizeof(*dhdr);

		/* Add the die to the kset.
		 *
		 * dhdr->die_id == ii, which was checked in
		 * amdgpu_discovery_reg_base_init().
		 */

		ip_die_entry = kzalloc(sizeof(*ip_die_entry), GFP_KERNEL);
		if (!ip_die_entry)
			return -ENOMEM;

		ip_die_entry->num_ips = num_ips;

		kobject_set_name(&ip_die_entry->ip_kset.kobj, "%d", le16_to_cpu(dhdr->die_id));
		ip_die_entry->ip_kset.kobj.kset = die_kset;
		ip_die_entry->ip_kset.kobj.ktype = &ip_die_entry_ktype;
		res = kset_register(&ip_die_entry->ip_kset);
		if (res) {
			DRM_ERROR("Couldn't register ip_die_entry kset");
			kfree(ip_die_entry);
			return res;
		}

		amdgpu_discovery_sysfs_ips(adev, ip_die_entry, ip_offset, num_ips);
	}

	return 0;
}

static int amdgpu_discovery_sysfs_init(struct amdgpu_device *adev)
{
	struct kset *die_kset;
975
	int res, ii;
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

	adev->ip_top = kzalloc(sizeof(*adev->ip_top), GFP_KERNEL);
	if (!adev->ip_top)
		return -ENOMEM;

	adev->ip_top->adev = adev;

	res = kobject_init_and_add(&adev->ip_top->kobj, &ip_discovery_ktype,
				   &adev->dev->kobj, "ip_discovery");
	if (res) {
		DRM_ERROR("Couldn't init and add ip_discovery/");
		goto Err;
	}

	die_kset = &adev->ip_top->die_kset;
	kobject_set_name(&die_kset->kobj, "%s", "die");
	die_kset->kobj.parent = &adev->ip_top->kobj;
	die_kset->kobj.ktype = &die_kobj_ktype;
	res = kset_register(&adev->ip_top->die_kset);
	if (res) {
		DRM_ERROR("Couldn't register die_kset");
		goto Err;
	}

1000 1001 1002 1003
	for (ii = 0; ii < ARRAY_SIZE(ip_hw_attr); ii++)
		ip_hw_instance_attrs[ii] = &ip_hw_attr[ii].attr;
	ip_hw_instance_attrs[ii] = NULL;

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
	res = amdgpu_discovery_sysfs_recurse(adev);

	return res;
Err:
	kobject_put(&adev->ip_top->kobj);
	return res;
}

/* -------------------------------------------------- */

#define list_to_kobj(el) container_of(el, struct kobject, entry)

static void amdgpu_discovery_sysfs_ip_hw_free(struct ip_hw_id *ip_hw_id)
{
	struct list_head *el, *tmp;
	struct kset *hw_id_kset;

	hw_id_kset = &ip_hw_id->hw_id_kset;
	spin_lock(&hw_id_kset->list_lock);
	list_for_each_prev_safe(el, tmp, &hw_id_kset->list) {
		list_del_init(el);
		spin_unlock(&hw_id_kset->list_lock);
		/* kobject is embedded in ip_hw_instance */
		kobject_put(list_to_kobj(el));
		spin_lock(&hw_id_kset->list_lock);
	}
	spin_unlock(&hw_id_kset->list_lock);
	kobject_put(&ip_hw_id->hw_id_kset.kobj);
}

static void amdgpu_discovery_sysfs_die_free(struct ip_die_entry *ip_die_entry)
{
	struct list_head *el, *tmp;
	struct kset *ip_kset;

	ip_kset = &ip_die_entry->ip_kset;
	spin_lock(&ip_kset->list_lock);
	list_for_each_prev_safe(el, tmp, &ip_kset->list) {
		list_del_init(el);
		spin_unlock(&ip_kset->list_lock);
		amdgpu_discovery_sysfs_ip_hw_free(to_ip_hw_id(list_to_kobj(el)));
		spin_lock(&ip_kset->list_lock);
	}
	spin_unlock(&ip_kset->list_lock);
	kobject_put(&ip_die_entry->ip_kset.kobj);
}

static void amdgpu_discovery_sysfs_fini(struct amdgpu_device *adev)
{
	struct list_head *el, *tmp;
	struct kset *die_kset;

	die_kset = &adev->ip_top->die_kset;
	spin_lock(&die_kset->list_lock);
	list_for_each_prev_safe(el, tmp, &die_kset->list) {
		list_del_init(el);
		spin_unlock(&die_kset->list_lock);
		amdgpu_discovery_sysfs_die_free(to_ip_die_entry(list_to_kobj(el)));
		spin_lock(&die_kset->list_lock);
	}
	spin_unlock(&die_kset->list_lock);
	kobject_put(&adev->ip_top->die_kset.kobj);
	kobject_put(&adev->ip_top->kobj);
}

/* ================================================== */

1071
static int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
{
	struct binary_header *bhdr;
	struct ip_discovery_header *ihdr;
	struct die_header *dhdr;
	struct ip *ip;
	uint16_t die_offset;
	uint16_t ip_offset;
	uint16_t num_dies;
	uint16_t num_ips;
	uint8_t num_base_address;
	int hw_ip;
	int i, j, k;
1084
	int r;
1085

1086 1087 1088 1089
	r = amdgpu_discovery_init(adev);
	if (r) {
		DRM_ERROR("amdgpu_discovery_init failed\n");
		return r;
1090 1091
	}

1092 1093
	bhdr = (struct binary_header *)adev->mman.discovery_bin;
	ihdr = (struct ip_discovery_header *)(adev->mman.discovery_bin +
1094 1095 1096
			le16_to_cpu(bhdr->table_list[IP_DISCOVERY].offset));
	num_dies = le16_to_cpu(ihdr->num_dies);

1097
	DRM_DEBUG("number of dies: %d\n", num_dies);
1098

1099 1100
	for (i = 0; i < num_dies; i++) {
		die_offset = le16_to_cpu(ihdr->die_info[i].die_offset);
1101
		dhdr = (struct die_header *)(adev->mman.discovery_bin + die_offset);
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
		num_ips = le16_to_cpu(dhdr->num_ips);
		ip_offset = die_offset + sizeof(*dhdr);

		if (le16_to_cpu(dhdr->die_id) != i) {
			DRM_ERROR("invalid die id %d, expected %d\n",
					le16_to_cpu(dhdr->die_id), i);
			return -EINVAL;
		}

		DRM_DEBUG("number of hardware IPs on die%d: %d\n",
				le16_to_cpu(dhdr->die_id), num_ips);

		for (j = 0; j < num_ips; j++) {
1115
			ip = (struct ip *)(adev->mman.discovery_bin + ip_offset);
1116 1117 1118 1119

			if (amdgpu_discovery_validate_ip(ip))
				goto next_ip;

1120 1121 1122 1123 1124 1125 1126 1127 1128
			num_base_address = ip->num_base_address;

			DRM_DEBUG("%s(%d) #%d v%d.%d.%d:\n",
				  hw_id_names[le16_to_cpu(ip->hw_id)],
				  le16_to_cpu(ip->hw_id),
				  ip->number_instance,
				  ip->major, ip->minor,
				  ip->revision);

1129
			if (le16_to_cpu(ip->hw_id) == VCN_HWID) {
1130 1131 1132 1133 1134 1135 1136 1137 1138
				/* Bit [5:0]: original revision value
				 * Bit [7:6]: en/decode capability:
				 *     0b00 : VCN function normally
				 *     0b10 : encode is disabled
				 *     0b01 : decode is disabled
				 */
				adev->vcn.vcn_config[adev->vcn.num_vcn_inst] =
					ip->revision & 0xc0;
				ip->revision &= ~0xc0;
1139 1140 1141 1142 1143 1144
				if (adev->vcn.num_vcn_inst < AMDGPU_MAX_VCN_INSTANCES)
					adev->vcn.num_vcn_inst++;
				else
					dev_err(adev->dev, "Too many VCN instances: %d vs %d\n",
						adev->vcn.num_vcn_inst + 1,
						AMDGPU_MAX_VCN_INSTANCES);
1145
			}
1146 1147 1148
			if (le16_to_cpu(ip->hw_id) == SDMA0_HWID ||
			    le16_to_cpu(ip->hw_id) == SDMA1_HWID ||
			    le16_to_cpu(ip->hw_id) == SDMA2_HWID ||
1149 1150 1151 1152 1153 1154 1155 1156
			    le16_to_cpu(ip->hw_id) == SDMA3_HWID) {
				if (adev->sdma.num_instances < AMDGPU_MAX_SDMA_INSTANCES)
					adev->sdma.num_instances++;
				else
					dev_err(adev->dev, "Too many SDMA instances: %d vs %d\n",
						adev->sdma.num_instances + 1,
						AMDGPU_MAX_SDMA_INSTANCES);
			}
1157

1158 1159 1160
			if (le16_to_cpu(ip->hw_id) == UMC_HWID)
				adev->gmc.num_umc++;

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
			for (k = 0; k < num_base_address; k++) {
				/*
				 * convert the endianness of base addresses in place,
				 * so that we don't need to convert them when accessing adev->reg_offset.
				 */
				ip->base_address[k] = le32_to_cpu(ip->base_address[k]);
				DRM_DEBUG("\t0x%08x\n", ip->base_address[k]);
			}

			for (hw_ip = 0; hw_ip < MAX_HWIP; hw_ip++) {
				if (hw_id_map[hw_ip] == le16_to_cpu(ip->hw_id)) {
1172
					DRM_DEBUG("set register base offset for %s\n",
1173
							hw_id_names[le16_to_cpu(ip->hw_id)]);
1174 1175
					adev->reg_offset[hw_ip][ip->number_instance] =
						ip->base_address;
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
					/* Instance support is somewhat inconsistent.
					 * SDMA is a good example.  Sienna cichlid has 4 total
					 * SDMA instances, each enumerated separately (HWIDs
					 * 42, 43, 68, 69).  Arcturus has 8 total SDMA instances,
					 * but they are enumerated as multiple instances of the
					 * same HWIDs (4x HWID 42, 4x HWID 43).  UMC is another
					 * example.  On most chips there are multiple instances
					 * with the same HWID.
					 */
					adev->ip_versions[hw_ip][ip->number_instance] =
1186
						IP_VERSION(ip->major, ip->minor, ip->revision);
1187 1188
				}
			}
1189

1190
next_ip:
1191
			ip_offset += struct_size(ip, base_address, ip->num_base_address);
1192 1193 1194
		}
	}

1195 1196
	amdgpu_discovery_sysfs_init(adev);

1197 1198 1199
	return 0;
}

1200
int amdgpu_discovery_get_ip_version(struct amdgpu_device *adev, int hw_id, int number_instance,
1201
				    int *major, int *minor, int *revision)
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
{
	struct binary_header *bhdr;
	struct ip_discovery_header *ihdr;
	struct die_header *dhdr;
	struct ip *ip;
	uint16_t die_offset;
	uint16_t ip_offset;
	uint16_t num_dies;
	uint16_t num_ips;
	int i, j;

1213
	if (!adev->mman.discovery_bin) {
1214 1215 1216 1217
		DRM_ERROR("ip discovery uninitialized\n");
		return -EINVAL;
	}

1218 1219
	bhdr = (struct binary_header *)adev->mman.discovery_bin;
	ihdr = (struct ip_discovery_header *)(adev->mman.discovery_bin +
1220 1221 1222 1223 1224
			le16_to_cpu(bhdr->table_list[IP_DISCOVERY].offset));
	num_dies = le16_to_cpu(ihdr->num_dies);

	for (i = 0; i < num_dies; i++) {
		die_offset = le16_to_cpu(ihdr->die_info[i].die_offset);
1225
		dhdr = (struct die_header *)(adev->mman.discovery_bin + die_offset);
1226 1227 1228 1229
		num_ips = le16_to_cpu(dhdr->num_ips);
		ip_offset = die_offset + sizeof(*dhdr);

		for (j = 0; j < num_ips; j++) {
1230
			ip = (struct ip *)(adev->mman.discovery_bin + ip_offset);
1231

1232
			if ((le16_to_cpu(ip->hw_id) == hw_id) && (ip->number_instance == number_instance)) {
1233 1234 1235 1236
				if (major)
					*major = ip->major;
				if (minor)
					*minor = ip->minor;
1237 1238
				if (revision)
					*revision = ip->revision;
1239 1240
				return 0;
			}
1241
			ip_offset += struct_size(ip, base_address, ip->num_base_address);
1242 1243 1244 1245 1246 1247
		}
	}

	return -EINVAL;
}

1248
static void amdgpu_discovery_harvest_ip(struct amdgpu_device *adev)
1249
{
1250
	int vcn_harvest_count = 0;
1251
	int umc_harvest_count = 0;
1252

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
	/*
	 * Harvest table does not fit Navi1x and legacy GPUs,
	 * so read harvest bit per IP data structure to set
	 * harvest configuration.
	 */
	if (adev->ip_versions[GC_HWIP][0] < IP_VERSION(10, 2, 0)) {
		if ((adev->pdev->device == 0x731E &&
			(adev->pdev->revision == 0xC6 ||
			 adev->pdev->revision == 0xC7)) ||
			(adev->pdev->device == 0x7340 &&
			 adev->pdev->revision == 0xC9) ||
			(adev->pdev->device == 0x7360 &&
			 adev->pdev->revision == 0xC7))
			amdgpu_discovery_read_harvest_bit_per_ip(adev,
				&vcn_harvest_count);
	} else {
1269
		amdgpu_discovery_read_from_harvest_table(adev,
1270 1271
							 &vcn_harvest_count,
							 &umc_harvest_count);
1272
	}
1273 1274 1275

	amdgpu_discovery_harvest_config_quirk(adev);

1276 1277 1278 1279
	if (vcn_harvest_count == adev->vcn.num_vcn_inst) {
		adev->harvest_ip_mask |= AMD_HARVEST_IP_VCN_MASK;
		adev->harvest_ip_mask |= AMD_HARVEST_IP_JPEG_MASK;
	}
1280 1281 1282 1283

	if (umc_harvest_count < adev->gmc.num_umc) {
		adev->gmc.num_umc -= umc_harvest_count;
	}
1284 1285
}

1286 1287
union gc_info {
	struct gc_info_v1_0 v1;
1288 1289
	struct gc_info_v1_1 v1_1;
	struct gc_info_v1_2 v1_2;
1290 1291 1292
	struct gc_info_v2_0 v2;
};

1293
static int amdgpu_discovery_get_gfx_info(struct amdgpu_device *adev)
1294 1295
{
	struct binary_header *bhdr;
1296
	union gc_info *gc_info;
1297
	u16 offset;
1298

1299
	if (!adev->mman.discovery_bin) {
1300 1301 1302 1303
		DRM_ERROR("ip discovery uninitialized\n");
		return -EINVAL;
	}

1304
	bhdr = (struct binary_header *)adev->mman.discovery_bin;
1305 1306
	offset = le16_to_cpu(bhdr->table_list[GC].offset);

1307 1308
	if (!offset)
		return 0;
1309 1310 1311

	gc_info = (union gc_info *)(adev->mman.discovery_bin + offset);

1312
	switch (le16_to_cpu(gc_info->v1.header.version_major)) {
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
	case 1:
		adev->gfx.config.max_shader_engines = le32_to_cpu(gc_info->v1.gc_num_se);
		adev->gfx.config.max_cu_per_sh = 2 * (le32_to_cpu(gc_info->v1.gc_num_wgp0_per_sa) +
						      le32_to_cpu(gc_info->v1.gc_num_wgp1_per_sa));
		adev->gfx.config.max_sh_per_se = le32_to_cpu(gc_info->v1.gc_num_sa_per_se);
		adev->gfx.config.max_backends_per_se = le32_to_cpu(gc_info->v1.gc_num_rb_per_se);
		adev->gfx.config.max_texture_channel_caches = le32_to_cpu(gc_info->v1.gc_num_gl2c);
		adev->gfx.config.max_gprs = le32_to_cpu(gc_info->v1.gc_num_gprs);
		adev->gfx.config.max_gs_threads = le32_to_cpu(gc_info->v1.gc_num_max_gs_thds);
		adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gc_info->v1.gc_gs_table_depth);
		adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gc_info->v1.gc_gsprim_buff_depth);
		adev->gfx.config.double_offchip_lds_buf = le32_to_cpu(gc_info->v1.gc_double_offchip_lds_buffer);
		adev->gfx.cu_info.wave_front_size = le32_to_cpu(gc_info->v1.gc_wave_size);
		adev->gfx.cu_info.max_waves_per_simd = le32_to_cpu(gc_info->v1.gc_max_waves_per_simd);
		adev->gfx.cu_info.max_scratch_slots_per_cu = le32_to_cpu(gc_info->v1.gc_max_scratch_slots_per_cu);
		adev->gfx.cu_info.lds_size = le32_to_cpu(gc_info->v1.gc_lds_size);
		adev->gfx.config.num_sc_per_sh = le32_to_cpu(gc_info->v1.gc_num_sc_per_se) /
			le32_to_cpu(gc_info->v1.gc_num_sa_per_se);
		adev->gfx.config.num_packer_per_sc = le32_to_cpu(gc_info->v1.gc_num_packer_per_sc);
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
		if (gc_info->v1.header.version_minor >= 1) {
			adev->gfx.config.gc_num_tcp_per_sa = le32_to_cpu(gc_info->v1_1.gc_num_tcp_per_sa);
			adev->gfx.config.gc_num_sdp_interface = le32_to_cpu(gc_info->v1_1.gc_num_sdp_interface);
			adev->gfx.config.gc_num_tcps = le32_to_cpu(gc_info->v1_1.gc_num_tcps);
		}
		if (gc_info->v1.header.version_minor >= 2) {
			adev->gfx.config.gc_num_tcp_per_wpg = le32_to_cpu(gc_info->v1_2.gc_num_tcp_per_wpg);
			adev->gfx.config.gc_tcp_l1_size = le32_to_cpu(gc_info->v1_2.gc_tcp_l1_size);
			adev->gfx.config.gc_num_sqc_per_wgp = le32_to_cpu(gc_info->v1_2.gc_num_sqc_per_wgp);
			adev->gfx.config.gc_l1_instruction_cache_size_per_sqc = le32_to_cpu(gc_info->v1_2.gc_l1_instruction_cache_size_per_sqc);
			adev->gfx.config.gc_l1_data_cache_size_per_sqc = le32_to_cpu(gc_info->v1_2.gc_l1_data_cache_size_per_sqc);
			adev->gfx.config.gc_gl1c_per_sa = le32_to_cpu(gc_info->v1_2.gc_gl1c_per_sa);
			adev->gfx.config.gc_gl1c_size_per_instance = le32_to_cpu(gc_info->v1_2.gc_gl1c_size_per_instance);
			adev->gfx.config.gc_gl2c_per_gpu = le32_to_cpu(gc_info->v1_2.gc_gl2c_per_gpu);
		}
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
		break;
	case 2:
		adev->gfx.config.max_shader_engines = le32_to_cpu(gc_info->v2.gc_num_se);
		adev->gfx.config.max_cu_per_sh = le32_to_cpu(gc_info->v2.gc_num_cu_per_sh);
		adev->gfx.config.max_sh_per_se = le32_to_cpu(gc_info->v2.gc_num_sh_per_se);
		adev->gfx.config.max_backends_per_se = le32_to_cpu(gc_info->v2.gc_num_rb_per_se);
		adev->gfx.config.max_texture_channel_caches = le32_to_cpu(gc_info->v2.gc_num_tccs);
		adev->gfx.config.max_gprs = le32_to_cpu(gc_info->v2.gc_num_gprs);
		adev->gfx.config.max_gs_threads = le32_to_cpu(gc_info->v2.gc_num_max_gs_thds);
		adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gc_info->v2.gc_gs_table_depth);
		adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gc_info->v2.gc_gsprim_buff_depth);
		adev->gfx.config.double_offchip_lds_buf = le32_to_cpu(gc_info->v2.gc_double_offchip_lds_buffer);
		adev->gfx.cu_info.wave_front_size = le32_to_cpu(gc_info->v2.gc_wave_size);
		adev->gfx.cu_info.max_waves_per_simd = le32_to_cpu(gc_info->v2.gc_max_waves_per_simd);
		adev->gfx.cu_info.max_scratch_slots_per_cu = le32_to_cpu(gc_info->v2.gc_max_scratch_slots_per_cu);
		adev->gfx.cu_info.lds_size = le32_to_cpu(gc_info->v2.gc_lds_size);
		adev->gfx.config.num_sc_per_sh = le32_to_cpu(gc_info->v2.gc_num_sc_per_se) /
			le32_to_cpu(gc_info->v2.gc_num_sh_per_se);
		adev->gfx.config.num_packer_per_sc = le32_to_cpu(gc_info->v2.gc_num_packer_per_sc);
		break;
	default:
		dev_err(adev->dev,
			"Unhandled GC info table %d.%d\n",
1370 1371
			le16_to_cpu(gc_info->v1.header.version_major),
			le16_to_cpu(gc_info->v1.header.version_minor));
1372 1373
		return -EINVAL;
	}
1374
	return 0;
1375 1376 1377 1378 1379 1380
}

union mall_info {
	struct mall_info_v1_0 v1;
};

1381
static int amdgpu_discovery_get_mall_info(struct amdgpu_device *adev)
1382 1383 1384 1385 1386
{
	struct binary_header *bhdr;
	union mall_info *mall_info;
	u32 u, mall_size_per_umc, m_s_present, half_use;
	u64 mall_size;
1387
	u16 offset;
1388 1389 1390 1391 1392 1393 1394

	if (!adev->mman.discovery_bin) {
		DRM_ERROR("ip discovery uninitialized\n");
		return -EINVAL;
	}

	bhdr = (struct binary_header *)adev->mman.discovery_bin;
1395 1396
	offset = le16_to_cpu(bhdr->table_list[MALL_INFO].offset);

1397 1398
	if (!offset)
		return 0;
1399 1400

	mall_info = (union mall_info *)(adev->mman.discovery_bin + offset);
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425

	switch (le16_to_cpu(mall_info->v1.header.version_major)) {
	case 1:
		mall_size = 0;
		mall_size_per_umc = le32_to_cpu(mall_info->v1.mall_size_per_m);
		m_s_present = le32_to_cpu(mall_info->v1.m_s_present);
		half_use = le32_to_cpu(mall_info->v1.m_half_use);
		for (u = 0; u < adev->gmc.num_umc; u++) {
			if (m_s_present & (1 << u))
				mall_size += mall_size_per_umc * 2;
			else if (half_use & (1 << u))
				mall_size += mall_size_per_umc / 2;
			else
				mall_size += mall_size_per_umc;
		}
		adev->gmc.mall_size = mall_size;
		break;
	default:
		dev_err(adev->dev,
			"Unhandled MALL info table %d.%d\n",
			le16_to_cpu(mall_info->v1.header.version_major),
			le16_to_cpu(mall_info->v1.header.version_minor));
		return -EINVAL;
	}
	return 0;
1426 1427 1428 1429 1430 1431
}

union vcn_info {
	struct vcn_info_v1_0 v1;
};

1432
static int amdgpu_discovery_get_vcn_info(struct amdgpu_device *adev)
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
{
	struct binary_header *bhdr;
	union vcn_info *vcn_info;
	u16 offset;
	int v;

	if (!adev->mman.discovery_bin) {
		DRM_ERROR("ip discovery uninitialized\n");
		return -EINVAL;
	}

1444 1445 1446 1447 1448
	/* num_vcn_inst is currently limited to AMDGPU_MAX_VCN_INSTANCES
	 * which is smaller than VCN_INFO_TABLE_MAX_NUM_INSTANCES
	 * but that may change in the future with new GPUs so keep this
	 * check for defensive purposes.
	 */
1449 1450 1451 1452 1453 1454 1455 1456
	if (adev->vcn.num_vcn_inst > VCN_INFO_TABLE_MAX_NUM_INSTANCES) {
		dev_err(adev->dev, "invalid vcn instances\n");
		return -EINVAL;
	}

	bhdr = (struct binary_header *)adev->mman.discovery_bin;
	offset = le16_to_cpu(bhdr->table_list[VCN_INFO].offset);

1457 1458
	if (!offset)
		return 0;
1459 1460 1461 1462 1463

	vcn_info = (union vcn_info *)(adev->mman.discovery_bin + offset);

	switch (le16_to_cpu(vcn_info->v1.header.version_major)) {
	case 1:
1464 1465 1466
		/* num_vcn_inst is currently limited to AMDGPU_MAX_VCN_INSTANCES
		 * so this won't overflow.
		 */
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
		for (v = 0; v < adev->vcn.num_vcn_inst; v++) {
			adev->vcn.vcn_codec_disable_mask[v] =
				le32_to_cpu(vcn_info->v1.instance_info[v].fuse_data.all_bits);
		}
		break;
	default:
		dev_err(adev->dev,
			"Unhandled VCN info table %d.%d\n",
			le16_to_cpu(vcn_info->v1.header.version_major),
			le16_to_cpu(vcn_info->v1.header.version_minor));
		return -EINVAL;
	}
	return 0;
1480
}
1481

1482
static int amdgpu_discovery_set_common_ip_blocks(struct amdgpu_device *adev)
1483
{
1484
	/* what IP to use for this? */
1485
	switch (adev->ip_versions[GC_HWIP][0]) {
1486
	case IP_VERSION(9, 0, 1):
1487
	case IP_VERSION(9, 1, 0):
1488
	case IP_VERSION(9, 2, 1):
1489 1490
	case IP_VERSION(9, 2, 2):
	case IP_VERSION(9, 3, 0):
1491 1492 1493
	case IP_VERSION(9, 4, 0):
	case IP_VERSION(9, 4, 1):
	case IP_VERSION(9, 4, 2):
1494
		amdgpu_device_ip_block_add(adev, &vega10_common_ip_block);
1495
		break;
1496 1497 1498 1499
	case IP_VERSION(10, 1, 10):
	case IP_VERSION(10, 1, 1):
	case IP_VERSION(10, 1, 2):
	case IP_VERSION(10, 1, 3):
L
Lang Yu 已提交
1500
	case IP_VERSION(10, 1, 4):
1501
	case IP_VERSION(10, 3, 0):
1502
	case IP_VERSION(10, 3, 1):
1503
	case IP_VERSION(10, 3, 2):
1504
	case IP_VERSION(10, 3, 3):
1505 1506
	case IP_VERSION(10, 3, 4):
	case IP_VERSION(10, 3, 5):
1507
	case IP_VERSION(10, 3, 6):
1508
	case IP_VERSION(10, 3, 7):
1509
		amdgpu_device_ip_block_add(adev, &nv_common_ip_block);
1510
		break;
1511
	case IP_VERSION(11, 0, 0):
1512
	case IP_VERSION(11, 0, 1):
1513
	case IP_VERSION(11, 0, 2):
1514
	case IP_VERSION(11, 0, 3):
1515
	case IP_VERSION(11, 0, 4):
1516 1517
		amdgpu_device_ip_block_add(adev, &soc21_common_ip_block);
		break;
1518
	default:
1519 1520 1521
		dev_err(adev->dev,
			"Failed to add common ip block(GC_HWIP:0x%x)\n",
			adev->ip_versions[GC_HWIP][0]);
1522 1523
		return -EINVAL;
	}
1524 1525
	return 0;
}
1526

1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
static int amdgpu_discovery_set_gmc_ip_blocks(struct amdgpu_device *adev)
{
	/* use GC or MMHUB IP version */
	switch (adev->ip_versions[GC_HWIP][0]) {
	case IP_VERSION(9, 0, 1):
	case IP_VERSION(9, 1, 0):
	case IP_VERSION(9, 2, 1):
	case IP_VERSION(9, 2, 2):
	case IP_VERSION(9, 3, 0):
	case IP_VERSION(9, 4, 0):
	case IP_VERSION(9, 4, 1):
	case IP_VERSION(9, 4, 2):
		amdgpu_device_ip_block_add(adev, &gmc_v9_0_ip_block);
1540
		break;
1541 1542 1543 1544
	case IP_VERSION(10, 1, 10):
	case IP_VERSION(10, 1, 1):
	case IP_VERSION(10, 1, 2):
	case IP_VERSION(10, 1, 3):
L
Lang Yu 已提交
1545
	case IP_VERSION(10, 1, 4):
1546 1547 1548 1549 1550 1551
	case IP_VERSION(10, 3, 0):
	case IP_VERSION(10, 3, 1):
	case IP_VERSION(10, 3, 2):
	case IP_VERSION(10, 3, 3):
	case IP_VERSION(10, 3, 4):
	case IP_VERSION(10, 3, 5):
1552
	case IP_VERSION(10, 3, 6):
1553
	case IP_VERSION(10, 3, 7):
1554
		amdgpu_device_ip_block_add(adev, &gmc_v10_0_ip_block);
1555
		break;
1556
	case IP_VERSION(11, 0, 0):
1557
	case IP_VERSION(11, 0, 1):
1558
	case IP_VERSION(11, 0, 2):
1559
	case IP_VERSION(11, 0, 3):
1560
	case IP_VERSION(11, 0, 4):
1561 1562
		amdgpu_device_ip_block_add(adev, &gmc_v11_0_ip_block);
		break;
1563
	default:
1564 1565 1566
		dev_err(adev->dev,
			"Failed to add gmc ip block(GC_HWIP:0x%x)\n",
			adev->ip_versions[GC_HWIP][0]);
1567
		return -EINVAL;
1568
	}
1569 1570
	return 0;
}
1571

1572 1573 1574
static int amdgpu_discovery_set_ih_ip_blocks(struct amdgpu_device *adev)
{
	switch (adev->ip_versions[OSSSYS_HWIP][0]) {
1575 1576 1577 1578
	case IP_VERSION(4, 0, 0):
	case IP_VERSION(4, 0, 1):
	case IP_VERSION(4, 1, 0):
	case IP_VERSION(4, 1, 1):
1579 1580 1581
	case IP_VERSION(4, 3, 0):
		amdgpu_device_ip_block_add(adev, &vega10_ih_ip_block);
		break;
1582 1583 1584
	case IP_VERSION(4, 2, 0):
	case IP_VERSION(4, 2, 1):
	case IP_VERSION(4, 4, 0):
1585
		amdgpu_device_ip_block_add(adev, &vega20_ih_ip_block);
1586
		break;
1587 1588 1589 1590 1591
	case IP_VERSION(5, 0, 0):
	case IP_VERSION(5, 0, 1):
	case IP_VERSION(5, 0, 2):
	case IP_VERSION(5, 0, 3):
	case IP_VERSION(5, 2, 0):
1592 1593
	case IP_VERSION(5, 2, 1):
		amdgpu_device_ip_block_add(adev, &navi10_ih_ip_block);
1594
		break;
L
Likun Gao 已提交
1595
	case IP_VERSION(6, 0, 0):
1596
	case IP_VERSION(6, 0, 1):
1597
	case IP_VERSION(6, 0, 2):
L
Likun Gao 已提交
1598 1599
		amdgpu_device_ip_block_add(adev, &ih_v6_0_ip_block);
		break;
1600
	default:
1601 1602 1603
		dev_err(adev->dev,
			"Failed to add ih ip block(OSSSYS_HWIP:0x%x)\n",
			adev->ip_versions[OSSSYS_HWIP][0]);
1604
		return -EINVAL;
1605
	}
1606 1607
	return 0;
}
1608

1609 1610 1611
static int amdgpu_discovery_set_psp_ip_blocks(struct amdgpu_device *adev)
{
	switch (adev->ip_versions[MP0_HWIP][0]) {
1612
	case IP_VERSION(9, 0, 0):
1613 1614
		amdgpu_device_ip_block_add(adev, &psp_v3_1_ip_block);
		break;
1615 1616
	case IP_VERSION(10, 0, 0):
	case IP_VERSION(10, 0, 1):
1617
		amdgpu_device_ip_block_add(adev, &psp_v10_0_ip_block);
1618
		break;
1619
	case IP_VERSION(11, 0, 0):
1620
	case IP_VERSION(11, 0, 2):
1621
	case IP_VERSION(11, 0, 4):
1622 1623
	case IP_VERSION(11, 0, 5):
	case IP_VERSION(11, 0, 9):
1624 1625
	case IP_VERSION(11, 0, 7):
	case IP_VERSION(11, 0, 11):
1626 1627
	case IP_VERSION(11, 0, 12):
	case IP_VERSION(11, 0, 13):
1628
	case IP_VERSION(11, 5, 0):
1629 1630 1631 1632
		amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block);
		break;
	case IP_VERSION(11, 0, 8):
		amdgpu_device_ip_block_add(adev, &psp_v11_0_8_ip_block);
1633
		break;
1634 1635 1636 1637
	case IP_VERSION(11, 0, 3):
	case IP_VERSION(12, 0, 1):
		amdgpu_device_ip_block_add(adev, &psp_v12_0_ip_block);
		break;
1638
	case IP_VERSION(13, 0, 0):
1639
	case IP_VERSION(13, 0, 1):
1640
	case IP_VERSION(13, 0, 2):
1641
	case IP_VERSION(13, 0, 3):
1642
	case IP_VERSION(13, 0, 5):
1643
	case IP_VERSION(13, 0, 7):
1644
	case IP_VERSION(13, 0, 8):
1645
	case IP_VERSION(13, 0, 10):
1646
	case IP_VERSION(13, 0, 11):
1647
		amdgpu_device_ip_block_add(adev, &psp_v13_0_ip_block);
1648 1649 1650
		break;
	case IP_VERSION(13, 0, 4):
		amdgpu_device_ip_block_add(adev, &psp_v13_0_4_ip_block);
1651
		break;
1652
	default:
1653 1654 1655
		dev_err(adev->dev,
			"Failed to add psp ip block(MP0_HWIP:0x%x)\n",
			adev->ip_versions[MP0_HWIP][0]);
1656
		return -EINVAL;
1657
	}
1658 1659
	return 0;
}
1660

1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
static int amdgpu_discovery_set_smu_ip_blocks(struct amdgpu_device *adev)
{
	switch (adev->ip_versions[MP1_HWIP][0]) {
	case IP_VERSION(9, 0, 0):
	case IP_VERSION(10, 0, 0):
	case IP_VERSION(10, 0, 1):
	case IP_VERSION(11, 0, 2):
		if (adev->asic_type == CHIP_ARCTURUS)
			amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
		else
			amdgpu_device_ip_block_add(adev, &pp_smu_ip_block);
1672
		break;
1673
	case IP_VERSION(11, 0, 0):
1674
	case IP_VERSION(11, 0, 5):
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
	case IP_VERSION(11, 0, 9):
	case IP_VERSION(11, 0, 7):
	case IP_VERSION(11, 0, 8):
	case IP_VERSION(11, 0, 11):
	case IP_VERSION(11, 0, 12):
	case IP_VERSION(11, 0, 13):
	case IP_VERSION(11, 5, 0):
		amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
		break;
	case IP_VERSION(12, 0, 0):
	case IP_VERSION(12, 0, 1):
		amdgpu_device_ip_block_add(adev, &smu_v12_0_ip_block);
		break;
1688
	case IP_VERSION(13, 0, 0):
1689 1690 1691
	case IP_VERSION(13, 0, 1):
	case IP_VERSION(13, 0, 2):
	case IP_VERSION(13, 0, 3):
1692
	case IP_VERSION(13, 0, 4):
1693
	case IP_VERSION(13, 0, 5):
1694
	case IP_VERSION(13, 0, 7):
1695
	case IP_VERSION(13, 0, 8):
1696
	case IP_VERSION(13, 0, 10):
1697
		amdgpu_device_ip_block_add(adev, &smu_v13_0_ip_block);
1698 1699
		break;
	default:
1700 1701 1702
		dev_err(adev->dev,
			"Failed to add smu ip block(MP1_HWIP:0x%x)\n",
			adev->ip_versions[MP1_HWIP][0]);
1703 1704
		return -EINVAL;
	}
1705 1706 1707
	return 0;
}

R
Ren Zhijie 已提交
1708
#if defined(CONFIG_DRM_AMD_DC)
1709 1710 1711 1712 1713
static void amdgpu_discovery_set_sriov_display(struct amdgpu_device *adev)
{
	amdgpu_device_set_sriov_virtual_display(adev);
	amdgpu_device_ip_block_add(adev, &amdgpu_vkms_ip_block);
}
R
Ren Zhijie 已提交
1714
#endif
1715

1716 1717
static int amdgpu_discovery_set_display_ip_blocks(struct amdgpu_device *adev)
{
1718
	if (adev->enable_virtual_display) {
1719
		amdgpu_device_ip_block_add(adev, &amdgpu_vkms_ip_block);
1720 1721 1722 1723 1724 1725
		return 0;
	}

	if (!amdgpu_device_has_dc_support(adev))
		return 0;

1726
#if defined(CONFIG_DRM_AMD_DC)
1727
	if (adev->ip_versions[DCE_HWIP][0]) {
1728 1729 1730 1731 1732
		switch (adev->ip_versions[DCE_HWIP][0]) {
		case IP_VERSION(1, 0, 0):
		case IP_VERSION(1, 0, 1):
		case IP_VERSION(2, 0, 2):
		case IP_VERSION(2, 0, 0):
1733
		case IP_VERSION(2, 0, 3):
1734 1735 1736 1737 1738 1739 1740
		case IP_VERSION(2, 1, 0):
		case IP_VERSION(3, 0, 0):
		case IP_VERSION(3, 0, 2):
		case IP_VERSION(3, 0, 3):
		case IP_VERSION(3, 0, 1):
		case IP_VERSION(3, 1, 2):
		case IP_VERSION(3, 1, 3):
1741
		case IP_VERSION(3, 1, 4):
1742
		case IP_VERSION(3, 1, 5):
1743
		case IP_VERSION(3, 1, 6):
1744 1745
		case IP_VERSION(3, 2, 0):
		case IP_VERSION(3, 2, 1):
1746 1747 1748 1749
			if (amdgpu_sriov_vf(adev))
				amdgpu_discovery_set_sriov_display(adev);
			else
				amdgpu_device_ip_block_add(adev, &dm_ip_block);
1750 1751
			break;
		default:
1752 1753 1754
			dev_err(adev->dev,
				"Failed to add dm ip block(DCE_HWIP:0x%x)\n",
				adev->ip_versions[DCE_HWIP][0]);
1755 1756 1757 1758 1759 1760 1761
			return -EINVAL;
		}
	} else if (adev->ip_versions[DCI_HWIP][0]) {
		switch (adev->ip_versions[DCI_HWIP][0]) {
		case IP_VERSION(12, 0, 0):
		case IP_VERSION(12, 0, 1):
		case IP_VERSION(12, 1, 0):
1762 1763 1764 1765
			if (amdgpu_sriov_vf(adev))
				amdgpu_discovery_set_sriov_display(adev);
			else
				amdgpu_device_ip_block_add(adev, &dm_ip_block);
1766 1767
			break;
		default:
1768 1769 1770
			dev_err(adev->dev,
				"Failed to add dm ip block(DCI_HWIP:0x%x)\n",
				adev->ip_versions[DCI_HWIP][0]);
1771 1772 1773
			return -EINVAL;
		}
	}
1774
#endif
1775 1776
	return 0;
}
1777

1778 1779
static int amdgpu_discovery_set_gc_ip_blocks(struct amdgpu_device *adev)
{
1780
	switch (adev->ip_versions[GC_HWIP][0]) {
1781 1782 1783 1784 1785 1786 1787 1788
	case IP_VERSION(9, 0, 1):
	case IP_VERSION(9, 1, 0):
	case IP_VERSION(9, 2, 1):
	case IP_VERSION(9, 2, 2):
	case IP_VERSION(9, 3, 0):
	case IP_VERSION(9, 4, 0):
	case IP_VERSION(9, 4, 1):
	case IP_VERSION(9, 4, 2):
1789
		amdgpu_device_ip_block_add(adev, &gfx_v9_0_ip_block);
1790
		break;
1791 1792
	case IP_VERSION(10, 1, 10):
	case IP_VERSION(10, 1, 2):
1793
	case IP_VERSION(10, 1, 1):
1794
	case IP_VERSION(10, 1, 3):
L
Lang Yu 已提交
1795
	case IP_VERSION(10, 1, 4):
1796 1797
	case IP_VERSION(10, 3, 0):
	case IP_VERSION(10, 3, 2):
1798
	case IP_VERSION(10, 3, 1):
1799 1800
	case IP_VERSION(10, 3, 4):
	case IP_VERSION(10, 3, 5):
Y
Yifan Zhang 已提交
1801
	case IP_VERSION(10, 3, 6):
1802
	case IP_VERSION(10, 3, 3):
1803
	case IP_VERSION(10, 3, 7):
1804
		amdgpu_device_ip_block_add(adev, &gfx_v10_0_ip_block);
1805
		break;
1806
	case IP_VERSION(11, 0, 0):
1807
	case IP_VERSION(11, 0, 1):
1808
	case IP_VERSION(11, 0, 2):
1809
	case IP_VERSION(11, 0, 3):
1810
	case IP_VERSION(11, 0, 4):
1811 1812
		amdgpu_device_ip_block_add(adev, &gfx_v11_0_ip_block);
		break;
1813
	default:
1814 1815 1816
		dev_err(adev->dev,
			"Failed to add gfx ip block(GC_HWIP:0x%x)\n",
			adev->ip_versions[GC_HWIP][0]);
1817 1818
		return -EINVAL;
	}
1819 1820
	return 0;
}
1821

1822 1823 1824
static int amdgpu_discovery_set_sdma_ip_blocks(struct amdgpu_device *adev)
{
	switch (adev->ip_versions[SDMA0_HWIP][0]) {
1825 1826 1827 1828
	case IP_VERSION(4, 0, 0):
	case IP_VERSION(4, 0, 1):
	case IP_VERSION(4, 1, 0):
	case IP_VERSION(4, 1, 1):
1829
	case IP_VERSION(4, 1, 2):
1830
	case IP_VERSION(4, 2, 0):
1831
	case IP_VERSION(4, 2, 2):
1832
	case IP_VERSION(4, 4, 0):
1833
		amdgpu_device_ip_block_add(adev, &sdma_v4_0_ip_block);
1834
		break;
1835 1836 1837
	case IP_VERSION(5, 0, 0):
	case IP_VERSION(5, 0, 1):
	case IP_VERSION(5, 0, 2):
1838 1839 1840
	case IP_VERSION(5, 0, 5):
		amdgpu_device_ip_block_add(adev, &sdma_v5_0_ip_block);
		break;
1841
	case IP_VERSION(5, 2, 0):
1842 1843 1844
	case IP_VERSION(5, 2, 2):
	case IP_VERSION(5, 2, 4):
	case IP_VERSION(5, 2, 5):
1845
	case IP_VERSION(5, 2, 6):
1846
	case IP_VERSION(5, 2, 3):
1847
	case IP_VERSION(5, 2, 1):
1848
	case IP_VERSION(5, 2, 7):
1849
		amdgpu_device_ip_block_add(adev, &sdma_v5_2_ip_block);
1850
		break;
1851
	case IP_VERSION(6, 0, 0):
1852
	case IP_VERSION(6, 0, 1):
1853
	case IP_VERSION(6, 0, 2):
1854
	case IP_VERSION(6, 0, 3):
1855 1856
		amdgpu_device_ip_block_add(adev, &sdma_v6_0_ip_block);
		break;
1857
	default:
1858 1859 1860
		dev_err(adev->dev,
			"Failed to add sdma ip block(SDMA0_HWIP:0x%x)\n",
			adev->ip_versions[SDMA0_HWIP][0]);
1861 1862
		return -EINVAL;
	}
1863 1864
	return 0;
}
1865

1866 1867 1868 1869 1870 1871
static int amdgpu_discovery_set_mm_ip_blocks(struct amdgpu_device *adev)
{
	if (adev->ip_versions[VCE_HWIP][0]) {
		switch (adev->ip_versions[UVD_HWIP][0]) {
		case IP_VERSION(7, 0, 0):
		case IP_VERSION(7, 2, 0):
1872 1873 1874
			/* UVD is not supported on vega20 SR-IOV */
			if (!(adev->asic_type == CHIP_VEGA20 && amdgpu_sriov_vf(adev)))
				amdgpu_device_ip_block_add(adev, &uvd_v7_0_ip_block);
1875 1876
			break;
		default:
1877 1878 1879
			dev_err(adev->dev,
				"Failed to add uvd v7 ip block(UVD_HWIP:0x%x)\n",
				adev->ip_versions[UVD_HWIP][0]);
1880 1881
			return -EINVAL;
		}
1882 1883 1884
		switch (adev->ip_versions[VCE_HWIP][0]) {
		case IP_VERSION(4, 0, 0):
		case IP_VERSION(4, 1, 0):
1885 1886 1887
			/* VCE is not supported on vega20 SR-IOV */
			if (!(adev->asic_type == CHIP_VEGA20 && amdgpu_sriov_vf(adev)))
				amdgpu_device_ip_block_add(adev, &vce_v4_0_ip_block);
1888 1889
			break;
		default:
1890 1891 1892
			dev_err(adev->dev,
				"Failed to add VCE v4 ip block(VCE_HWIP:0x%x)\n",
				adev->ip_versions[VCE_HWIP][0]);
1893 1894
			return -EINVAL;
		}
1895 1896
	} else {
		switch (adev->ip_versions[UVD_HWIP][0]) {
1897 1898
		case IP_VERSION(1, 0, 0):
		case IP_VERSION(1, 0, 1):
1899 1900
			amdgpu_device_ip_block_add(adev, &vcn_v1_0_ip_block);
			break;
1901
		case IP_VERSION(2, 0, 0):
1902 1903 1904
		case IP_VERSION(2, 0, 2):
		case IP_VERSION(2, 2, 0):
			amdgpu_device_ip_block_add(adev, &vcn_v2_0_ip_block);
1905 1906
			if (!amdgpu_sriov_vf(adev))
				amdgpu_device_ip_block_add(adev, &jpeg_v2_0_ip_block);
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
			break;
		case IP_VERSION(2, 0, 3):
			break;
		case IP_VERSION(2, 5, 0):
			amdgpu_device_ip_block_add(adev, &vcn_v2_5_ip_block);
			amdgpu_device_ip_block_add(adev, &jpeg_v2_5_ip_block);
			break;
		case IP_VERSION(2, 6, 0):
			amdgpu_device_ip_block_add(adev, &vcn_v2_6_ip_block);
			amdgpu_device_ip_block_add(adev, &jpeg_v2_6_ip_block);
			break;
1918
		case IP_VERSION(3, 0, 0):
1919 1920
		case IP_VERSION(3, 0, 16):
		case IP_VERSION(3, 1, 1):
1921
		case IP_VERSION(3, 1, 2):
1922
		case IP_VERSION(3, 0, 2):
1923
		case IP_VERSION(3, 0, 192):
1924
			amdgpu_device_ip_block_add(adev, &vcn_v3_0_ip_block);
1925 1926
			if (!amdgpu_sriov_vf(adev))
				amdgpu_device_ip_block_add(adev, &jpeg_v3_0_ip_block);
1927
			break;
1928 1929
		case IP_VERSION(3, 0, 33):
			amdgpu_device_ip_block_add(adev, &vcn_v3_0_ip_block);
1930
			break;
1931
		case IP_VERSION(4, 0, 0):
1932
		case IP_VERSION(4, 0, 2):
1933
		case IP_VERSION(4, 0, 4):
1934 1935
			amdgpu_device_ip_block_add(adev, &vcn_v4_0_ip_block);
			if (!amdgpu_sriov_vf(adev))
1936
				amdgpu_device_ip_block_add(adev, &jpeg_v4_0_ip_block);
1937
			break;
1938
		default:
1939 1940 1941
			dev_err(adev->dev,
				"Failed to add vcn/jpeg ip block(UVD_HWIP:0x%x)\n",
				adev->ip_versions[UVD_HWIP][0]);
1942
			return -EINVAL;
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
		}
	}
	return 0;
}

static int amdgpu_discovery_set_mes_ip_blocks(struct amdgpu_device *adev)
{
	switch (adev->ip_versions[GC_HWIP][0]) {
	case IP_VERSION(10, 1, 10):
	case IP_VERSION(10, 1, 1):
	case IP_VERSION(10, 1, 2):
	case IP_VERSION(10, 1, 3):
L
Lang Yu 已提交
1955
	case IP_VERSION(10, 1, 4):
1956 1957 1958 1959 1960 1961
	case IP_VERSION(10, 3, 0):
	case IP_VERSION(10, 3, 1):
	case IP_VERSION(10, 3, 2):
	case IP_VERSION(10, 3, 3):
	case IP_VERSION(10, 3, 4):
	case IP_VERSION(10, 3, 5):
Y
Yifan Zhang 已提交
1962
	case IP_VERSION(10, 3, 6):
1963 1964 1965 1966 1967 1968 1969 1970
		if (amdgpu_mes) {
			amdgpu_device_ip_block_add(adev, &mes_v10_1_ip_block);
			adev->enable_mes = true;
			if (amdgpu_mes_kiq)
				adev->enable_mes_kiq = true;
		}
		break;
	case IP_VERSION(11, 0, 0):
1971
	case IP_VERSION(11, 0, 1):
1972
	case IP_VERSION(11, 0, 2):
1973
	case IP_VERSION(11, 0, 3):
1974
	case IP_VERSION(11, 0, 4):
1975 1976 1977
		amdgpu_device_ip_block_add(adev, &mes_v11_0_ip_block);
		adev->enable_mes = true;
		adev->enable_mes_kiq = true;
1978 1979
		break;
	default:
G
Guchun Chen 已提交
1980
		break;
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
	}
	return 0;
}

int amdgpu_discovery_set_ip_blocks(struct amdgpu_device *adev)
{
	int r;

	switch (adev->asic_type) {
	case CHIP_VEGA10:
		vega10_reg_base_init(adev);
		adev->sdma.num_instances = 2;
1993
		adev->gmc.num_umc = 4;
1994 1995 1996 1997 1998
		adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 0, 0);
		adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 0, 0);
		adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 0, 0);
		adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 0, 0);
		adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 0, 0);
1999
		adev->ip_versions[SDMA1_HWIP][0] = IP_VERSION(4, 0, 0);
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
		adev->ip_versions[DF_HWIP][0] = IP_VERSION(2, 1, 0);
		adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(6, 1, 0);
		adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 0, 0);
		adev->ip_versions[MP0_HWIP][0] = IP_VERSION(9, 0, 0);
		adev->ip_versions[MP1_HWIP][0] = IP_VERSION(9, 0, 0);
		adev->ip_versions[THM_HWIP][0] = IP_VERSION(9, 0, 0);
		adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(9, 0, 0);
		adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 0, 1);
		adev->ip_versions[UVD_HWIP][0] = IP_VERSION(7, 0, 0);
		adev->ip_versions[VCE_HWIP][0] = IP_VERSION(4, 0, 0);
		adev->ip_versions[DCI_HWIP][0] = IP_VERSION(12, 0, 0);
		break;
	case CHIP_VEGA12:
		vega10_reg_base_init(adev);
		adev->sdma.num_instances = 2;
2015
		adev->gmc.num_umc = 4;
2016 2017 2018 2019 2020
		adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 3, 0);
		adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 3, 0);
		adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 0, 1);
		adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 0, 1);
		adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 0, 1);
2021
		adev->ip_versions[SDMA1_HWIP][0] = IP_VERSION(4, 0, 1);
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
		adev->ip_versions[DF_HWIP][0] = IP_VERSION(2, 5, 0);
		adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(6, 2, 0);
		adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 1, 0);
		adev->ip_versions[MP0_HWIP][0] = IP_VERSION(9, 0, 0);
		adev->ip_versions[MP1_HWIP][0] = IP_VERSION(9, 0, 0);
		adev->ip_versions[THM_HWIP][0] = IP_VERSION(9, 0, 0);
		adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(9, 0, 1);
		adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 2, 1);
		adev->ip_versions[UVD_HWIP][0] = IP_VERSION(7, 0, 0);
		adev->ip_versions[VCE_HWIP][0] = IP_VERSION(4, 0, 0);
		adev->ip_versions[DCI_HWIP][0] = IP_VERSION(12, 0, 1);
		break;
	case CHIP_RAVEN:
		vega10_reg_base_init(adev);
		adev->sdma.num_instances = 1;
		adev->vcn.num_vcn_inst = 1;
2038
		adev->gmc.num_umc = 2;
2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
		if (adev->apu_flags & AMD_APU_IS_RAVEN2) {
			adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 2, 0);
			adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 2, 0);
			adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 1, 1);
			adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 1, 1);
			adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 1, 1);
			adev->ip_versions[DF_HWIP][0] = IP_VERSION(2, 1, 1);
			adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 0, 1);
			adev->ip_versions[UMC_HWIP][0] = IP_VERSION(7, 5, 0);
			adev->ip_versions[MP0_HWIP][0] = IP_VERSION(10, 0, 1);
			adev->ip_versions[MP1_HWIP][0] = IP_VERSION(10, 0, 1);
			adev->ip_versions[THM_HWIP][0] = IP_VERSION(10, 1, 0);
			adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(10, 0, 1);
			adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 2, 2);
			adev->ip_versions[UVD_HWIP][0] = IP_VERSION(1, 0, 1);
			adev->ip_versions[DCE_HWIP][0] = IP_VERSION(1, 0, 1);
		} else {
			adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 1, 0);
			adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 1, 0);
			adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 1, 0);
			adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 1, 0);
			adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 1, 0);
			adev->ip_versions[DF_HWIP][0] = IP_VERSION(2, 1, 0);
			adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 0, 0);
			adev->ip_versions[UMC_HWIP][0] = IP_VERSION(7, 0, 0);
			adev->ip_versions[MP0_HWIP][0] = IP_VERSION(10, 0, 0);
			adev->ip_versions[MP1_HWIP][0] = IP_VERSION(10, 0, 0);
			adev->ip_versions[THM_HWIP][0] = IP_VERSION(10, 0, 0);
			adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(10, 0, 0);
			adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 1, 0);
			adev->ip_versions[UVD_HWIP][0] = IP_VERSION(1, 0, 0);
			adev->ip_versions[DCE_HWIP][0] = IP_VERSION(1, 0, 0);
		}
		break;
	case CHIP_VEGA20:
		vega20_reg_base_init(adev);
		adev->sdma.num_instances = 2;
2076
		adev->gmc.num_umc = 8;
2077 2078 2079 2080 2081
		adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 4, 0);
		adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 4, 0);
		adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 2, 0);
		adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 2, 0);
		adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 2, 0);
2082
		adev->ip_versions[SDMA1_HWIP][0] = IP_VERSION(4, 2, 0);
2083 2084 2085 2086 2087 2088 2089 2090 2091
		adev->ip_versions[DF_HWIP][0] = IP_VERSION(3, 6, 0);
		adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 4, 0);
		adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 1, 1);
		adev->ip_versions[MP0_HWIP][0] = IP_VERSION(11, 0, 2);
		adev->ip_versions[MP1_HWIP][0] = IP_VERSION(11, 0, 2);
		adev->ip_versions[THM_HWIP][0] = IP_VERSION(11, 0, 2);
		adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(11, 0, 2);
		adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 4, 0);
		adev->ip_versions[UVD_HWIP][0] = IP_VERSION(7, 2, 0);
2092
		adev->ip_versions[UVD_HWIP][1] = IP_VERSION(7, 2, 0);
2093 2094 2095 2096 2097 2098 2099
		adev->ip_versions[VCE_HWIP][0] = IP_VERSION(4, 1, 0);
		adev->ip_versions[DCI_HWIP][0] = IP_VERSION(12, 1, 0);
		break;
	case CHIP_ARCTURUS:
		arct_reg_base_init(adev);
		adev->sdma.num_instances = 8;
		adev->vcn.num_vcn_inst = 2;
2100
		adev->gmc.num_umc = 8;
2101 2102 2103 2104 2105
		adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 4, 1);
		adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 4, 1);
		adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 2, 1);
		adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 2, 1);
		adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 2, 2);
2106 2107 2108 2109 2110 2111 2112
		adev->ip_versions[SDMA1_HWIP][0] = IP_VERSION(4, 2, 2);
		adev->ip_versions[SDMA1_HWIP][1] = IP_VERSION(4, 2, 2);
		adev->ip_versions[SDMA1_HWIP][2] = IP_VERSION(4, 2, 2);
		adev->ip_versions[SDMA1_HWIP][3] = IP_VERSION(4, 2, 2);
		adev->ip_versions[SDMA1_HWIP][4] = IP_VERSION(4, 2, 2);
		adev->ip_versions[SDMA1_HWIP][5] = IP_VERSION(4, 2, 2);
		adev->ip_versions[SDMA1_HWIP][6] = IP_VERSION(4, 2, 2);
2113 2114 2115 2116 2117 2118 2119 2120 2121
		adev->ip_versions[DF_HWIP][0] = IP_VERSION(3, 6, 1);
		adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 4, 1);
		adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 1, 2);
		adev->ip_versions[MP0_HWIP][0] = IP_VERSION(11, 0, 4);
		adev->ip_versions[MP1_HWIP][0] = IP_VERSION(11, 0, 2);
		adev->ip_versions[THM_HWIP][0] = IP_VERSION(11, 0, 3);
		adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(11, 0, 3);
		adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 4, 1);
		adev->ip_versions[UVD_HWIP][0] = IP_VERSION(2, 5, 0);
2122
		adev->ip_versions[UVD_HWIP][1] = IP_VERSION(2, 5, 0);
2123 2124 2125 2126 2127
		break;
	case CHIP_ALDEBARAN:
		aldebaran_reg_base_init(adev);
		adev->sdma.num_instances = 5;
		adev->vcn.num_vcn_inst = 2;
2128
		adev->gmc.num_umc = 4;
2129 2130 2131 2132 2133
		adev->ip_versions[MMHUB_HWIP][0] = IP_VERSION(9, 4, 2);
		adev->ip_versions[ATHUB_HWIP][0] = IP_VERSION(9, 4, 2);
		adev->ip_versions[OSSSYS_HWIP][0] = IP_VERSION(4, 4, 0);
		adev->ip_versions[HDP_HWIP][0] = IP_VERSION(4, 4, 0);
		adev->ip_versions[SDMA0_HWIP][0] = IP_VERSION(4, 4, 0);
2134 2135 2136 2137
		adev->ip_versions[SDMA0_HWIP][1] = IP_VERSION(4, 4, 0);
		adev->ip_versions[SDMA0_HWIP][2] = IP_VERSION(4, 4, 0);
		adev->ip_versions[SDMA0_HWIP][3] = IP_VERSION(4, 4, 0);
		adev->ip_versions[SDMA0_HWIP][4] = IP_VERSION(4, 4, 0);
2138 2139 2140 2141 2142 2143 2144 2145 2146
		adev->ip_versions[DF_HWIP][0] = IP_VERSION(3, 6, 2);
		adev->ip_versions[NBIO_HWIP][0] = IP_VERSION(7, 4, 4);
		adev->ip_versions[UMC_HWIP][0] = IP_VERSION(6, 7, 0);
		adev->ip_versions[MP0_HWIP][0] = IP_VERSION(13, 0, 2);
		adev->ip_versions[MP1_HWIP][0] = IP_VERSION(13, 0, 2);
		adev->ip_versions[THM_HWIP][0] = IP_VERSION(13, 0, 2);
		adev->ip_versions[SMUIO_HWIP][0] = IP_VERSION(13, 0, 2);
		adev->ip_versions[GC_HWIP][0] = IP_VERSION(9, 4, 2);
		adev->ip_versions[UVD_HWIP][0] = IP_VERSION(2, 6, 0);
2147
		adev->ip_versions[UVD_HWIP][1] = IP_VERSION(2, 6, 0);
2148 2149 2150 2151 2152 2153 2154 2155
		adev->ip_versions[XGMI_HWIP][0] = IP_VERSION(6, 1, 0);
		break;
	default:
		r = amdgpu_discovery_reg_base_init(adev);
		if (r)
			return -EINVAL;

		amdgpu_discovery_harvest_ip(adev);
2156 2157 2158
		amdgpu_discovery_get_gfx_info(adev);
		amdgpu_discovery_get_mall_info(adev);
		amdgpu_discovery_get_vcn_info(adev);
2159
		break;
2160
	}
2161

2162
	switch (adev->ip_versions[GC_HWIP][0]) {
2163 2164 2165 2166 2167
	case IP_VERSION(9, 0, 1):
	case IP_VERSION(9, 2, 1):
	case IP_VERSION(9, 4, 0):
	case IP_VERSION(9, 4, 1):
	case IP_VERSION(9, 4, 2):
2168 2169 2170 2171 2172 2173
		adev->family = AMDGPU_FAMILY_AI;
		break;
	case IP_VERSION(9, 1, 0):
	case IP_VERSION(9, 2, 2):
	case IP_VERSION(9, 3, 0):
		adev->family = AMDGPU_FAMILY_RV;
2174
		break;
2175 2176
	case IP_VERSION(10, 1, 10):
	case IP_VERSION(10, 1, 1):
2177
	case IP_VERSION(10, 1, 2):
2178
	case IP_VERSION(10, 1, 3):
L
Lang Yu 已提交
2179
	case IP_VERSION(10, 1, 4):
2180 2181 2182 2183
	case IP_VERSION(10, 3, 0):
	case IP_VERSION(10, 3, 2):
	case IP_VERSION(10, 3, 4):
	case IP_VERSION(10, 3, 5):
2184 2185 2186 2187
		adev->family = AMDGPU_FAMILY_NV;
		break;
	case IP_VERSION(10, 3, 1):
		adev->family = AMDGPU_FAMILY_VGH;
2188
		adev->apu_flags |= AMD_APU_IS_VANGOGH;
2189
		break;
2190
	case IP_VERSION(10, 3, 3):
2191
		adev->family = AMDGPU_FAMILY_YC;
2192
		break;
Y
Yifan Zhang 已提交
2193 2194 2195
	case IP_VERSION(10, 3, 6):
		adev->family = AMDGPU_FAMILY_GC_10_3_6;
		break;
2196 2197 2198
	case IP_VERSION(10, 3, 7):
		adev->family = AMDGPU_FAMILY_GC_10_3_7;
		break;
2199
	case IP_VERSION(11, 0, 0):
2200
	case IP_VERSION(11, 0, 2):
2201
	case IP_VERSION(11, 0, 3):
2202 2203
		adev->family = AMDGPU_FAMILY_GC_11_0_0;
		break;
H
Huang Rui 已提交
2204 2205 2206
	case IP_VERSION(11, 0, 1):
		adev->family = AMDGPU_FAMILY_GC_11_0_1;
		break;
2207 2208 2209 2210
	default:
		return -EINVAL;
	}

2211 2212 2213 2214 2215
	switch (adev->ip_versions[GC_HWIP][0]) {
	case IP_VERSION(9, 1, 0):
	case IP_VERSION(9, 2, 2):
	case IP_VERSION(9, 3, 0):
	case IP_VERSION(10, 1, 3):
L
Lang Yu 已提交
2216
	case IP_VERSION(10, 1, 4):
2217 2218
	case IP_VERSION(10, 3, 1):
	case IP_VERSION(10, 3, 3):
Y
Yifan Zhang 已提交
2219
	case IP_VERSION(10, 3, 6):
2220
	case IP_VERSION(10, 3, 7):
2221
	case IP_VERSION(11, 0, 1):
2222 2223 2224 2225 2226 2227
		adev->flags |= AMD_IS_APU;
		break;
	default:
		break;
	}

2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
	if (adev->ip_versions[XGMI_HWIP][0] == IP_VERSION(4, 8, 0))
		adev->gmc.xgmi.supported = true;

	/* set NBIO version */
	switch (adev->ip_versions[NBIO_HWIP][0]) {
	case IP_VERSION(6, 1, 0):
	case IP_VERSION(6, 2, 0):
		adev->nbio.funcs = &nbio_v6_1_funcs;
		adev->nbio.hdp_flush_reg = &nbio_v6_1_hdp_flush_reg;
		break;
	case IP_VERSION(7, 0, 0):
	case IP_VERSION(7, 0, 1):
	case IP_VERSION(2, 5, 0):
		adev->nbio.funcs = &nbio_v7_0_funcs;
		adev->nbio.hdp_flush_reg = &nbio_v7_0_hdp_flush_reg;
		break;
	case IP_VERSION(7, 4, 0):
	case IP_VERSION(7, 4, 1):
2246 2247
	case IP_VERSION(7, 4, 4):
		adev->nbio.funcs = &nbio_v7_4_funcs;
2248
		adev->nbio.hdp_flush_reg = &nbio_v7_4_hdp_flush_reg;
2249
		break;
2250 2251
	case IP_VERSION(7, 2, 0):
	case IP_VERSION(7, 2, 1):
2252
	case IP_VERSION(7, 3, 0):
2253
	case IP_VERSION(7, 5, 0):
2254
	case IP_VERSION(7, 5, 1):
2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
		adev->nbio.funcs = &nbio_v7_2_funcs;
		adev->nbio.hdp_flush_reg = &nbio_v7_2_hdp_flush_reg;
		break;
	case IP_VERSION(2, 1, 1):
	case IP_VERSION(2, 3, 0):
	case IP_VERSION(2, 3, 1):
	case IP_VERSION(2, 3, 2):
	case IP_VERSION(3, 3, 0):
	case IP_VERSION(3, 3, 1):
	case IP_VERSION(3, 3, 2):
	case IP_VERSION(3, 3, 3):
		adev->nbio.funcs = &nbio_v2_3_funcs;
2267
		adev->nbio.hdp_flush_reg = &nbio_v2_3_hdp_flush_reg;
2268
		break;
2269
	case IP_VERSION(4, 3, 0):
2270
	case IP_VERSION(4, 3, 1):
2271 2272 2273 2274
		if (amdgpu_sriov_vf(adev))
			adev->nbio.funcs = &nbio_v4_3_sriov_funcs;
		else
			adev->nbio.funcs = &nbio_v4_3_funcs;
2275 2276
		adev->nbio.hdp_flush_reg = &nbio_v4_3_hdp_flush_reg;
		break;
2277 2278 2279 2280
	case IP_VERSION(7, 7, 0):
		adev->nbio.funcs = &nbio_v7_7_funcs;
		adev->nbio.hdp_flush_reg = &nbio_v7_7_hdp_flush_reg;
		break;
2281 2282 2283 2284 2285
	default:
		break;
	}

	switch (adev->ip_versions[HDP_HWIP][0]) {
2286 2287 2288 2289
	case IP_VERSION(4, 0, 0):
	case IP_VERSION(4, 0, 1):
	case IP_VERSION(4, 1, 0):
	case IP_VERSION(4, 1, 1):
2290
	case IP_VERSION(4, 1, 2):
2291
	case IP_VERSION(4, 2, 0):
2292
	case IP_VERSION(4, 2, 1):
2293
	case IP_VERSION(4, 4, 0):
2294
		adev->hdp.funcs = &hdp_v4_0_funcs;
2295
		break;
2296 2297 2298
	case IP_VERSION(5, 0, 0):
	case IP_VERSION(5, 0, 1):
	case IP_VERSION(5, 0, 2):
2299 2300
	case IP_VERSION(5, 0, 3):
	case IP_VERSION(5, 0, 4):
2301
	case IP_VERSION(5, 2, 0):
2302
		adev->hdp.funcs = &hdp_v5_0_funcs;
2303
		break;
2304 2305 2306
	case IP_VERSION(5, 2, 1):
		adev->hdp.funcs = &hdp_v5_2_funcs;
		break;
L
Likun Gao 已提交
2307
	case IP_VERSION(6, 0, 0):
2308
	case IP_VERSION(6, 0, 1):
L
Likun Gao 已提交
2309 2310
		adev->hdp.funcs = &hdp_v6_0_funcs;
		break;
2311
	default:
2312
		break;
2313 2314
	}

2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
	switch (adev->ip_versions[DF_HWIP][0]) {
	case IP_VERSION(3, 6, 0):
	case IP_VERSION(3, 6, 1):
	case IP_VERSION(3, 6, 2):
		adev->df.funcs = &df_v3_6_funcs;
		break;
	case IP_VERSION(2, 1, 0):
	case IP_VERSION(2, 1, 1):
	case IP_VERSION(2, 5, 0):
	case IP_VERSION(3, 5, 1):
	case IP_VERSION(3, 5, 2):
		adev->df.funcs = &df_v1_7_funcs;
		break;
	default:
		break;
2330 2331
	}

2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
	switch (adev->ip_versions[SMUIO_HWIP][0]) {
	case IP_VERSION(9, 0, 0):
	case IP_VERSION(9, 0, 1):
	case IP_VERSION(10, 0, 0):
	case IP_VERSION(10, 0, 1):
	case IP_VERSION(10, 0, 2):
		adev->smuio.funcs = &smuio_v9_0_funcs;
		break;
	case IP_VERSION(11, 0, 0):
	case IP_VERSION(11, 0, 2):
	case IP_VERSION(11, 0, 3):
	case IP_VERSION(11, 0, 4):
	case IP_VERSION(11, 0, 7):
	case IP_VERSION(11, 0, 8):
		adev->smuio.funcs = &smuio_v11_0_funcs;
		break;
	case IP_VERSION(11, 0, 6):
	case IP_VERSION(11, 0, 10):
	case IP_VERSION(11, 0, 11):
	case IP_VERSION(11, 5, 0):
	case IP_VERSION(13, 0, 1):
2353
	case IP_VERSION(13, 0, 9):
2354
	case IP_VERSION(13, 0, 10):
2355 2356 2357 2358 2359
		adev->smuio.funcs = &smuio_v11_0_6_funcs;
		break;
	case IP_VERSION(13, 0, 2):
		adev->smuio.funcs = &smuio_v13_0_funcs;
		break;
2360
	case IP_VERSION(13, 0, 6):
2361
	case IP_VERSION(13, 0, 8):
2362 2363
		adev->smuio.funcs = &smuio_v13_0_6_funcs;
		break;
2364 2365 2366 2367
	default:
		break;
	}

2368 2369
	switch (adev->ip_versions[LSDMA_HWIP][0]) {
	case IP_VERSION(6, 0, 0):
2370
	case IP_VERSION(6, 0, 1):
2371
	case IP_VERSION(6, 0, 2):
2372
	case IP_VERSION(6, 0, 3):
2373 2374 2375 2376 2377 2378
		adev->lsdma.funcs = &lsdma_v6_0_funcs;
		break;
	default:
		break;
	}

2379 2380 2381 2382 2383 2384 2385 2386
	r = amdgpu_discovery_set_common_ip_blocks(adev);
	if (r)
		return r;

	r = amdgpu_discovery_set_gmc_ip_blocks(adev);
	if (r)
		return r;

2387 2388
	/* For SR-IOV, PSP needs to be initialized before IH */
	if (amdgpu_sriov_vf(adev)) {
2389 2390 2391
		r = amdgpu_discovery_set_psp_ip_blocks(adev);
		if (r)
			return r;
2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404
		r = amdgpu_discovery_set_ih_ip_blocks(adev);
		if (r)
			return r;
	} else {
		r = amdgpu_discovery_set_ih_ip_blocks(adev);
		if (r)
			return r;

		if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)) {
			r = amdgpu_discovery_set_psp_ip_blocks(adev);
			if (r)
				return r;
		}
2405 2406 2407 2408 2409 2410
	}

	if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)) {
		r = amdgpu_discovery_set_smu_ip_blocks(adev);
		if (r)
			return r;
2411 2412
	}

2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
	r = amdgpu_discovery_set_display_ip_blocks(adev);
	if (r)
		return r;

	r = amdgpu_discovery_set_gc_ip_blocks(adev);
	if (r)
		return r;

	r = amdgpu_discovery_set_sdma_ip_blocks(adev);
	if (r)
		return r;

2425 2426 2427
	if ((adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT &&
	     !amdgpu_sriov_vf(adev)) ||
	    (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO && amdgpu_dpm == 1)) {
2428 2429 2430 2431 2432 2433 2434 2435 2436
		r = amdgpu_discovery_set_smu_ip_blocks(adev);
		if (r)
			return r;
	}

	r = amdgpu_discovery_set_mm_ip_blocks(adev);
	if (r)
		return r;

2437 2438 2439
	r = amdgpu_discovery_set_mes_ip_blocks(adev);
	if (r)
		return r;
2440 2441 2442 2443

	return 0;
}