kfd_topology.c 45.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 24 25 26 27 28 29
/*
 * Copyright 2014 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.
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/errno.h>
#include <linux/acpi.h>
#include <linux/hash.h>
#include <linux/cpufreq.h>
30
#include <linux/log2.h>
31 32
#include <linux/dmi.h>
#include <linux/atomic.h>
33 34 35 36

#include "kfd_priv.h"
#include "kfd_crat.h"
#include "kfd_topology.h"
37
#include "kfd_device_queue_manager.h"
38
#include "kfd_iommu.h"
39
#include "kfd_svm.h"
A
Amber Lin 已提交
40
#include "amdgpu_amdkfd.h"
41
#include "amdgpu_ras.h"
42

43 44
/* topology_device_list - Master list of all topology devices */
static struct list_head topology_device_list;
45
static struct kfd_system_properties sys_props;
46 47

static DECLARE_RWSEM(topology_lock);
48
static atomic_t topology_crat_proximity_domain;
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
						uint32_t proximity_domain)
{
	struct kfd_topology_device *top_dev;
	struct kfd_topology_device *device = NULL;

	down_read(&topology_lock);

	list_for_each_entry(top_dev, &topology_device_list, list)
		if (top_dev->proximity_domain == proximity_domain) {
			device = top_dev;
			break;
		}

	up_read(&topology_lock);

	return device;
}

69
struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
70
{
71 72
	struct kfd_topology_device *top_dev = NULL;
	struct kfd_topology_device *ret = NULL;
73 74 75 76 77

	down_read(&topology_lock);

	list_for_each_entry(top_dev, &topology_device_list, list)
		if (top_dev->gpu_id == gpu_id) {
78
			ret = top_dev;
79 80 81 82 83
			break;
		}

	up_read(&topology_lock);

84 85 86 87 88 89 90 91 92 93 94 95
	return ret;
}

struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
{
	struct kfd_topology_device *top_dev;

	top_dev = kfd_topology_device_by_id(gpu_id);
	if (!top_dev)
		return NULL;

	return top_dev->gpu;
96 97 98 99 100 101 102 103 104 105
}

struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
{
	struct kfd_topology_device *top_dev;
	struct kfd_dev *device = NULL;

	down_read(&topology_lock);

	list_for_each_entry(top_dev, &topology_device_list, list)
106
		if (top_dev->gpu && top_dev->gpu->pdev == pdev) {
107 108 109 110 111 112 113 114 115
			device = top_dev->gpu;
			break;
		}

	up_read(&topology_lock);

	return device;
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
struct kfd_dev *kfd_device_by_kgd(const struct kgd_dev *kgd)
{
	struct kfd_topology_device *top_dev;
	struct kfd_dev *device = NULL;

	down_read(&topology_lock);

	list_for_each_entry(top_dev, &topology_device_list, list)
		if (top_dev->gpu && top_dev->gpu->kgd == kgd) {
			device = top_dev->gpu;
			break;
		}

	up_read(&topology_lock);

	return device;
}

134
/* Called with write topology_lock acquired */
135 136 137 138 139
static void kfd_release_topology_device(struct kfd_topology_device *dev)
{
	struct kfd_mem_properties *mem;
	struct kfd_cache_properties *cache;
	struct kfd_iolink_properties *iolink;
140
	struct kfd_perf_properties *perf;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

	list_del(&dev->list);

	while (dev->mem_props.next != &dev->mem_props) {
		mem = container_of(dev->mem_props.next,
				struct kfd_mem_properties, list);
		list_del(&mem->list);
		kfree(mem);
	}

	while (dev->cache_props.next != &dev->cache_props) {
		cache = container_of(dev->cache_props.next,
				struct kfd_cache_properties, list);
		list_del(&cache->list);
		kfree(cache);
	}

	while (dev->io_link_props.next != &dev->io_link_props) {
		iolink = container_of(dev->io_link_props.next,
				struct kfd_iolink_properties, list);
		list_del(&iolink->list);
		kfree(iolink);
	}

165 166 167 168 169 170 171
	while (dev->perf_props.next != &dev->perf_props) {
		perf = container_of(dev->perf_props.next,
				struct kfd_perf_properties, list);
		list_del(&perf->list);
		kfree(perf);
	}

172 173 174
	kfree(dev);
}

175
void kfd_release_topology_device_list(struct list_head *device_list)
176 177 178
{
	struct kfd_topology_device *dev;

179 180 181
	while (!list_empty(device_list)) {
		dev = list_first_entry(device_list,
				       struct kfd_topology_device, list);
182
		kfd_release_topology_device(dev);
183
	}
184 185
}

186 187 188
static void kfd_release_live_view(void)
{
	kfd_release_topology_device_list(&topology_device_list);
189 190 191
	memset(&sys_props, 0, sizeof(sys_props));
}

192 193
struct kfd_topology_device *kfd_create_topology_device(
				struct list_head *device_list)
194 195 196 197
{
	struct kfd_topology_device *dev;

	dev = kfd_alloc_struct(dev);
198
	if (!dev) {
199
		pr_err("No memory to allocate a topology device");
200
		return NULL;
201 202 203 204 205
	}

	INIT_LIST_HEAD(&dev->mem_props);
	INIT_LIST_HEAD(&dev->cache_props);
	INIT_LIST_HEAD(&dev->io_link_props);
206
	INIT_LIST_HEAD(&dev->perf_props);
207

208
	list_add_tail(&dev->list, device_list);
209 210

	return dev;
211
}
212 213


214 215 216 217 218 219 220 221 222 223 224
#define sysfs_show_gen_prop(buffer, offs, fmt, ...)		\
		(offs += snprintf(buffer+offs, PAGE_SIZE-offs,	\
				  fmt, __VA_ARGS__))
#define sysfs_show_32bit_prop(buffer, offs, name, value) \
		sysfs_show_gen_prop(buffer, offs, "%s %u\n", name, value)
#define sysfs_show_64bit_prop(buffer, offs, name, value) \
		sysfs_show_gen_prop(buffer, offs, "%s %llu\n", name, value)
#define sysfs_show_32bit_val(buffer, offs, value) \
		sysfs_show_gen_prop(buffer, offs, "%u\n", value)
#define sysfs_show_str_val(buffer, offs, value) \
		sysfs_show_gen_prop(buffer, offs, "%s\n", value)
225 226 227 228

static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
		char *buffer)
{
229
	int offs = 0;
230 231 232 233 234

	/* Making sure that the buffer is an empty string */
	buffer[0] = 0;

	if (attr == &sys_props.attr_genid) {
235 236
		sysfs_show_32bit_val(buffer, offs,
				     sys_props.generation_count);
237
	} else if (attr == &sys_props.attr_props) {
238 239 240 241 242 243
		sysfs_show_64bit_prop(buffer, offs, "platform_oem",
				      sys_props.platform_oem);
		sysfs_show_64bit_prop(buffer, offs, "platform_id",
				      sys_props.platform_id);
		sysfs_show_64bit_prop(buffer, offs, "platform_rev",
				      sys_props.platform_rev);
244
	} else {
245
		offs = -EINVAL;
246 247
	}

248
	return offs;
249 250
}

251 252 253 254 255
static void kfd_topology_kobj_release(struct kobject *kobj)
{
	kfree(kobj);
}

256 257 258 259 260
static const struct sysfs_ops sysprops_ops = {
	.show = sysprops_show,
};

static struct kobj_type sysprops_type = {
261
	.release = kfd_topology_kobj_release,
262 263 264 265 266 267
	.sysfs_ops = &sysprops_ops,
};

static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
		char *buffer)
{
268
	int offs = 0;
269 270 271 272 273 274
	struct kfd_iolink_properties *iolink;

	/* Making sure that the buffer is an empty string */
	buffer[0] = 0;

	iolink = container_of(attr, struct kfd_iolink_properties, attr);
275 276
	if (iolink->gpu && kfd_devcgroup_check_permission(iolink->gpu))
		return -EPERM;
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	sysfs_show_32bit_prop(buffer, offs, "type", iolink->iolink_type);
	sysfs_show_32bit_prop(buffer, offs, "version_major", iolink->ver_maj);
	sysfs_show_32bit_prop(buffer, offs, "version_minor", iolink->ver_min);
	sysfs_show_32bit_prop(buffer, offs, "node_from", iolink->node_from);
	sysfs_show_32bit_prop(buffer, offs, "node_to", iolink->node_to);
	sysfs_show_32bit_prop(buffer, offs, "weight", iolink->weight);
	sysfs_show_32bit_prop(buffer, offs, "min_latency", iolink->min_latency);
	sysfs_show_32bit_prop(buffer, offs, "max_latency", iolink->max_latency);
	sysfs_show_32bit_prop(buffer, offs, "min_bandwidth",
			      iolink->min_bandwidth);
	sysfs_show_32bit_prop(buffer, offs, "max_bandwidth",
			      iolink->max_bandwidth);
	sysfs_show_32bit_prop(buffer, offs, "recommended_transfer_size",
			      iolink->rec_transfer_size);
	sysfs_show_32bit_prop(buffer, offs, "flags", iolink->flags);

	return offs;
294 295 296 297 298 299 300
}

static const struct sysfs_ops iolink_ops = {
	.show = iolink_show,
};

static struct kobj_type iolink_type = {
301
	.release = kfd_topology_kobj_release,
302 303 304 305 306 307
	.sysfs_ops = &iolink_ops,
};

static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
		char *buffer)
{
308
	int offs = 0;
309 310 311 312 313 314
	struct kfd_mem_properties *mem;

	/* Making sure that the buffer is an empty string */
	buffer[0] = 0;

	mem = container_of(attr, struct kfd_mem_properties, attr);
315 316
	if (mem->gpu && kfd_devcgroup_check_permission(mem->gpu))
		return -EPERM;
317 318 319 320 321 322 323 324 325
	sysfs_show_32bit_prop(buffer, offs, "heap_type", mem->heap_type);
	sysfs_show_64bit_prop(buffer, offs, "size_in_bytes",
			      mem->size_in_bytes);
	sysfs_show_32bit_prop(buffer, offs, "flags", mem->flags);
	sysfs_show_32bit_prop(buffer, offs, "width", mem->width);
	sysfs_show_32bit_prop(buffer, offs, "mem_clk_max",
			      mem->mem_clk_max);

	return offs;
326 327 328 329 330 331 332
}

static const struct sysfs_ops mem_ops = {
	.show = mem_show,
};

static struct kobj_type mem_type = {
333
	.release = kfd_topology_kobj_release,
334 335 336 337 338 339
	.sysfs_ops = &mem_ops,
};

static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
		char *buffer)
{
340
	int offs = 0;
341
	uint32_t i, j;
342 343 344 345 346 347
	struct kfd_cache_properties *cache;

	/* Making sure that the buffer is an empty string */
	buffer[0] = 0;

	cache = container_of(attr, struct kfd_cache_properties, attr);
348 349
	if (cache->gpu && kfd_devcgroup_check_permission(cache->gpu))
		return -EPERM;
350
	sysfs_show_32bit_prop(buffer, offs, "processor_id_low",
351
			cache->processor_id_low);
352 353 354 355 356 357 358 359 360 361
	sysfs_show_32bit_prop(buffer, offs, "level", cache->cache_level);
	sysfs_show_32bit_prop(buffer, offs, "size", cache->cache_size);
	sysfs_show_32bit_prop(buffer, offs, "cache_line_size",
			      cache->cacheline_size);
	sysfs_show_32bit_prop(buffer, offs, "cache_lines_per_tag",
			      cache->cachelines_per_tag);
	sysfs_show_32bit_prop(buffer, offs, "association", cache->cache_assoc);
	sysfs_show_32bit_prop(buffer, offs, "latency", cache->cache_latency);
	sysfs_show_32bit_prop(buffer, offs, "type", cache->cache_type);
	offs += snprintf(buffer+offs, PAGE_SIZE-offs, "sibling_map ");
362
	for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++)
363
		for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++)
364
			/* Check each bit */
365 366 367
			offs += snprintf(buffer+offs, PAGE_SIZE-offs, "%d,",
					 (cache->sibling_map[i] >> j) & 1);

368
	/* Replace the last "," with end of line */
369 370
	buffer[offs-1] = '\n';
	return offs;
371 372 373 374 375 376 377
}

static const struct sysfs_ops cache_ops = {
	.show = kfd_cache_show,
};

static struct kobj_type cache_type = {
378
	.release = kfd_topology_kobj_release,
379 380 381
	.sysfs_ops = &cache_ops,
};

382 383 384 385 386 387 388 389 390 391
/****** Sysfs of Performance Counters ******/

struct kfd_perf_attr {
	struct kobj_attribute attr;
	uint32_t data;
};

static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
			char *buf)
{
392
	int offs = 0;
393 394 395 396 397 398 399
	struct kfd_perf_attr *attr;

	buf[0] = 0;
	attr = container_of(attrs, struct kfd_perf_attr, attr);
	if (!attr->data) /* invalid data for PMC */
		return 0;
	else
400
		return sysfs_show_32bit_val(buf, offs, attr->data);
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
}

#define KFD_PERF_DESC(_name, _data)			\
{							\
	.attr  = __ATTR(_name, 0444, perf_show, NULL),	\
	.data = _data,					\
}

static struct kfd_perf_attr perf_attr_iommu[] = {
	KFD_PERF_DESC(max_concurrent, 0),
	KFD_PERF_DESC(num_counters, 0),
	KFD_PERF_DESC(counter_ids, 0),
};
/****************************************/

416 417 418
static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
		char *buffer)
{
419
	int offs = 0;
420
	struct kfd_topology_device *dev;
421
	uint32_t log_max_watch_addr;
422 423 424 425 426 427 428

	/* Making sure that the buffer is an empty string */
	buffer[0] = 0;

	if (strcmp(attr->name, "gpu_id") == 0) {
		dev = container_of(attr, struct kfd_topology_device,
				attr_gpuid);
429 430
		if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
			return -EPERM;
431
		return sysfs_show_32bit_val(buffer, offs, dev->gpu_id);
432 433 434
	}

	if (strcmp(attr->name, "name") == 0) {
435 436
		dev = container_of(attr, struct kfd_topology_device,
				attr_name);
437

438 439
		if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
			return -EPERM;
440
		return sysfs_show_str_val(buffer, offs, dev->node_props.name);
441
	}
442

443 444
	dev = container_of(attr, struct kfd_topology_device,
			attr_props);
445 446
	if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
		return -EPERM;
447 448 449
	sysfs_show_32bit_prop(buffer, offs, "cpu_cores_count",
			      dev->node_props.cpu_cores_count);
	sysfs_show_32bit_prop(buffer, offs, "simd_count",
450
			      dev->gpu ? dev->node_props.simd_count : 0);
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
	sysfs_show_32bit_prop(buffer, offs, "mem_banks_count",
			      dev->node_props.mem_banks_count);
	sysfs_show_32bit_prop(buffer, offs, "caches_count",
			      dev->node_props.caches_count);
	sysfs_show_32bit_prop(buffer, offs, "io_links_count",
			      dev->node_props.io_links_count);
	sysfs_show_32bit_prop(buffer, offs, "cpu_core_id_base",
			      dev->node_props.cpu_core_id_base);
	sysfs_show_32bit_prop(buffer, offs, "simd_id_base",
			      dev->node_props.simd_id_base);
	sysfs_show_32bit_prop(buffer, offs, "max_waves_per_simd",
			      dev->node_props.max_waves_per_simd);
	sysfs_show_32bit_prop(buffer, offs, "lds_size_in_kb",
			      dev->node_props.lds_size_in_kb);
	sysfs_show_32bit_prop(buffer, offs, "gds_size_in_kb",
			      dev->node_props.gds_size_in_kb);
	sysfs_show_32bit_prop(buffer, offs, "num_gws",
			      dev->node_props.num_gws);
	sysfs_show_32bit_prop(buffer, offs, "wave_front_size",
			      dev->node_props.wave_front_size);
	sysfs_show_32bit_prop(buffer, offs, "array_count",
			      dev->node_props.array_count);
	sysfs_show_32bit_prop(buffer, offs, "simd_arrays_per_engine",
			      dev->node_props.simd_arrays_per_engine);
	sysfs_show_32bit_prop(buffer, offs, "cu_per_simd_array",
			      dev->node_props.cu_per_simd_array);
	sysfs_show_32bit_prop(buffer, offs, "simd_per_cu",
			      dev->node_props.simd_per_cu);
	sysfs_show_32bit_prop(buffer, offs, "max_slots_scratch_cu",
			      dev->node_props.max_slots_scratch_cu);
481 482
	sysfs_show_32bit_prop(buffer, offs, "gfx_target_version",
			      dev->node_props.gfx_target_version);
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
	sysfs_show_32bit_prop(buffer, offs, "vendor_id",
			      dev->node_props.vendor_id);
	sysfs_show_32bit_prop(buffer, offs, "device_id",
			      dev->node_props.device_id);
	sysfs_show_32bit_prop(buffer, offs, "location_id",
			      dev->node_props.location_id);
	sysfs_show_32bit_prop(buffer, offs, "domain",
			      dev->node_props.domain);
	sysfs_show_32bit_prop(buffer, offs, "drm_render_minor",
			      dev->node_props.drm_render_minor);
	sysfs_show_64bit_prop(buffer, offs, "hive_id",
			      dev->node_props.hive_id);
	sysfs_show_32bit_prop(buffer, offs, "num_sdma_engines",
			      dev->node_props.num_sdma_engines);
	sysfs_show_32bit_prop(buffer, offs, "num_sdma_xgmi_engines",
			      dev->node_props.num_sdma_xgmi_engines);
	sysfs_show_32bit_prop(buffer, offs, "num_sdma_queues_per_engine",
			      dev->node_props.num_sdma_queues_per_engine);
	sysfs_show_32bit_prop(buffer, offs, "num_cp_queues",
			      dev->node_props.num_cp_queues);
503 504 505 506 507 508 509 510 511 512 513 514 515

	if (dev->gpu) {
		log_max_watch_addr =
			__ilog2_u32(dev->gpu->device_info->num_of_watch_points);

		if (log_max_watch_addr) {
			dev->node_props.capability |=
					HSA_CAP_WATCH_POINTS_SUPPORTED;

			dev->node_props.capability |=
				((log_max_watch_addr <<
					HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
				HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
516 517
		}

518 519 520 521
		if (dev->gpu->device_info->asic_family == CHIP_TONGA)
			dev->node_props.capability |=
					HSA_CAP_AQL_QUEUE_DOUBLE_MAP;

522
		sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_fcompute",
523
			dev->node_props.max_engine_clk_fcompute);
524

525
		sysfs_show_64bit_prop(buffer, offs, "local_mem_size", 0ULL);
526

527 528 529 530 531 532
		sysfs_show_32bit_prop(buffer, offs, "fw_version",
				      dev->gpu->mec_fw_version);
		sysfs_show_32bit_prop(buffer, offs, "capability",
				      dev->node_props.capability);
		sysfs_show_32bit_prop(buffer, offs, "sdma_fw_version",
				      dev->gpu->sdma_fw_version);
533 534 535
		sysfs_show_64bit_prop(buffer, offs, "unique_id",
				      amdgpu_amdkfd_get_unique_id(dev->gpu->kgd));

536 537
	}

538 539
	return sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_ccompute",
				     cpufreq_quick_get_max(0)/1000);
540 541 542 543 544 545 546
}

static const struct sysfs_ops node_ops = {
	.show = node_show,
};

static struct kobj_type node_type = {
547
	.release = kfd_topology_kobj_release,
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
	.sysfs_ops = &node_ops,
};

static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
{
	sysfs_remove_file(kobj, attr);
	kobject_del(kobj);
	kobject_put(kobj);
}

static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
{
	struct kfd_iolink_properties *iolink;
	struct kfd_cache_properties *cache;
	struct kfd_mem_properties *mem;
563
	struct kfd_perf_properties *perf;
564 565 566 567 568 569

	if (dev->kobj_iolink) {
		list_for_each_entry(iolink, &dev->io_link_props, list)
			if (iolink->kobj) {
				kfd_remove_sysfs_file(iolink->kobj,
							&iolink->attr);
570
				iolink->kobj = NULL;
571 572 573
			}
		kobject_del(dev->kobj_iolink);
		kobject_put(dev->kobj_iolink);
574
		dev->kobj_iolink = NULL;
575 576 577 578 579 580 581
	}

	if (dev->kobj_cache) {
		list_for_each_entry(cache, &dev->cache_props, list)
			if (cache->kobj) {
				kfd_remove_sysfs_file(cache->kobj,
							&cache->attr);
582
				cache->kobj = NULL;
583 584 585
			}
		kobject_del(dev->kobj_cache);
		kobject_put(dev->kobj_cache);
586
		dev->kobj_cache = NULL;
587 588 589 590 591 592
	}

	if (dev->kobj_mem) {
		list_for_each_entry(mem, &dev->mem_props, list)
			if (mem->kobj) {
				kfd_remove_sysfs_file(mem->kobj, &mem->attr);
593
				mem->kobj = NULL;
594 595 596
			}
		kobject_del(dev->kobj_mem);
		kobject_put(dev->kobj_mem);
597
		dev->kobj_mem = NULL;
598 599
	}

600 601 602 603 604 605 606 607 608 609
	if (dev->kobj_perf) {
		list_for_each_entry(perf, &dev->perf_props, list) {
			kfree(perf->attr_group);
			perf->attr_group = NULL;
		}
		kobject_del(dev->kobj_perf);
		kobject_put(dev->kobj_perf);
		dev->kobj_perf = NULL;
	}

610 611 612 613 614 615
	if (dev->kobj_node) {
		sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
		sysfs_remove_file(dev->kobj_node, &dev->attr_name);
		sysfs_remove_file(dev->kobj_node, &dev->attr_props);
		kobject_del(dev->kobj_node);
		kobject_put(dev->kobj_node);
616
		dev->kobj_node = NULL;
617 618 619 620 621 622 623 624 625
	}
}

static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
		uint32_t id)
{
	struct kfd_iolink_properties *iolink;
	struct kfd_cache_properties *cache;
	struct kfd_mem_properties *mem;
626
	struct kfd_perf_properties *perf;
627
	int ret;
628 629
	uint32_t i, num_attrs;
	struct attribute **attrs;
630

631 632 633
	if (WARN_ON(dev->kobj_node))
		return -EEXIST;

634 635 636 637 638 639 640 641 642
	/*
	 * Creating the sysfs folders
	 */
	dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
	if (!dev->kobj_node)
		return -ENOMEM;

	ret = kobject_init_and_add(dev->kobj_node, &node_type,
			sys_props.kobj_nodes, "%d", id);
643 644
	if (ret < 0) {
		kobject_put(dev->kobj_node);
645
		return ret;
646
	}
647 648 649 650 651 652 653 654 655 656 657 658 659

	dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
	if (!dev->kobj_mem)
		return -ENOMEM;

	dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
	if (!dev->kobj_cache)
		return -ENOMEM;

	dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
	if (!dev->kobj_iolink)
		return -ENOMEM;

660 661 662 663
	dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
	if (!dev->kobj_perf)
		return -ENOMEM;

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
	/*
	 * Creating sysfs files for node properties
	 */
	dev->attr_gpuid.name = "gpu_id";
	dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
	sysfs_attr_init(&dev->attr_gpuid);
	dev->attr_name.name = "name";
	dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
	sysfs_attr_init(&dev->attr_name);
	dev->attr_props.name = "properties";
	dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
	sysfs_attr_init(&dev->attr_props);
	ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
	if (ret < 0)
		return ret;
	ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
	if (ret < 0)
		return ret;
	ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
	if (ret < 0)
		return ret;

	i = 0;
	list_for_each_entry(mem, &dev->mem_props, list) {
		mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
		if (!mem->kobj)
			return -ENOMEM;
		ret = kobject_init_and_add(mem->kobj, &mem_type,
				dev->kobj_mem, "%d", i);
693 694
		if (ret < 0) {
			kobject_put(mem->kobj);
695
			return ret;
696
		}
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713

		mem->attr.name = "properties";
		mem->attr.mode = KFD_SYSFS_FILE_MODE;
		sysfs_attr_init(&mem->attr);
		ret = sysfs_create_file(mem->kobj, &mem->attr);
		if (ret < 0)
			return ret;
		i++;
	}

	i = 0;
	list_for_each_entry(cache, &dev->cache_props, list) {
		cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
		if (!cache->kobj)
			return -ENOMEM;
		ret = kobject_init_and_add(cache->kobj, &cache_type,
				dev->kobj_cache, "%d", i);
714 715
		if (ret < 0) {
			kobject_put(cache->kobj);
716
			return ret;
717
		}
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

		cache->attr.name = "properties";
		cache->attr.mode = KFD_SYSFS_FILE_MODE;
		sysfs_attr_init(&cache->attr);
		ret = sysfs_create_file(cache->kobj, &cache->attr);
		if (ret < 0)
			return ret;
		i++;
	}

	i = 0;
	list_for_each_entry(iolink, &dev->io_link_props, list) {
		iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
		if (!iolink->kobj)
			return -ENOMEM;
		ret = kobject_init_and_add(iolink->kobj, &iolink_type,
				dev->kobj_iolink, "%d", i);
735 736
		if (ret < 0) {
			kobject_put(iolink->kobj);
737
			return ret;
738
		}
739 740 741 742 743 744 745 746

		iolink->attr.name = "properties";
		iolink->attr.mode = KFD_SYSFS_FILE_MODE;
		sysfs_attr_init(&iolink->attr);
		ret = sysfs_create_file(iolink->kobj, &iolink->attr);
		if (ret < 0)
			return ret;
		i++;
747 748 749
	}

	/* All hardware blocks have the same number of attributes. */
750
	num_attrs = ARRAY_SIZE(perf_attr_iommu);
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
	list_for_each_entry(perf, &dev->perf_props, list) {
		perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
			* num_attrs + sizeof(struct attribute_group),
			GFP_KERNEL);
		if (!perf->attr_group)
			return -ENOMEM;

		attrs = (struct attribute **)(perf->attr_group + 1);
		if (!strcmp(perf->block_name, "iommu")) {
		/* Information of IOMMU's num_counters and counter_ids is shown
		 * under /sys/bus/event_source/devices/amd_iommu. We don't
		 * duplicate here.
		 */
			perf_attr_iommu[0].data = perf->max_concurrent;
			for (i = 0; i < num_attrs; i++)
				attrs[i] = &perf_attr_iommu[i].attr.attr;
		}
		perf->attr_group->name = perf->block_name;
		perf->attr_group->attrs = attrs;
		ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
		if (ret < 0)
			return ret;
	}
774 775 776 777

	return 0;
}

778
/* Called with write topology lock acquired */
779 780 781 782 783 784 785
static int kfd_build_sysfs_node_tree(void)
{
	struct kfd_topology_device *dev;
	int ret;
	uint32_t i = 0;

	list_for_each_entry(dev, &topology_device_list, list) {
786
		ret = kfd_build_sysfs_node_entry(dev, i);
787 788 789 790 791 792 793 794
		if (ret < 0)
			return ret;
		i++;
	}

	return 0;
}

795
/* Called with write topology lock acquired */
796 797 798 799 800 801 802 803 804 805 806 807
static void kfd_remove_sysfs_node_tree(void)
{
	struct kfd_topology_device *dev;

	list_for_each_entry(dev, &topology_device_list, list)
		kfd_remove_sysfs_node_entry(dev);
}

static int kfd_topology_update_sysfs(void)
{
	int ret;

808
	if (!sys_props.kobj_topology) {
809 810 811 812 813 814 815 816
		sys_props.kobj_topology =
				kfd_alloc_struct(sys_props.kobj_topology);
		if (!sys_props.kobj_topology)
			return -ENOMEM;

		ret = kobject_init_and_add(sys_props.kobj_topology,
				&sysprops_type,  &kfd_device->kobj,
				"topology");
817 818
		if (ret < 0) {
			kobject_put(sys_props.kobj_topology);
819
			return ret;
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 854 855 856 857 858 859

		sys_props.kobj_nodes = kobject_create_and_add("nodes",
				sys_props.kobj_topology);
		if (!sys_props.kobj_nodes)
			return -ENOMEM;

		sys_props.attr_genid.name = "generation_id";
		sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
		sysfs_attr_init(&sys_props.attr_genid);
		ret = sysfs_create_file(sys_props.kobj_topology,
				&sys_props.attr_genid);
		if (ret < 0)
			return ret;

		sys_props.attr_props.name = "system_properties";
		sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
		sysfs_attr_init(&sys_props.attr_props);
		ret = sysfs_create_file(sys_props.kobj_topology,
				&sys_props.attr_props);
		if (ret < 0)
			return ret;
	}

	kfd_remove_sysfs_node_tree();

	return kfd_build_sysfs_node_tree();
}

static void kfd_topology_release_sysfs(void)
{
	kfd_remove_sysfs_node_tree();
	if (sys_props.kobj_topology) {
		sysfs_remove_file(sys_props.kobj_topology,
				&sys_props.attr_genid);
		sysfs_remove_file(sys_props.kobj_topology,
				&sys_props.attr_props);
		if (sys_props.kobj_nodes) {
			kobject_del(sys_props.kobj_nodes);
			kobject_put(sys_props.kobj_nodes);
860
			sys_props.kobj_nodes = NULL;
861 862 863
		}
		kobject_del(sys_props.kobj_topology);
		kobject_put(sys_props.kobj_topology);
864
		sys_props.kobj_topology = NULL;
865 866 867
	}
}

868 869 870 871 872 873 874 875 876 877
/* Called with write topology_lock acquired */
static void kfd_topology_update_device_list(struct list_head *temp_list,
					struct list_head *master_list)
{
	while (!list_empty(temp_list)) {
		list_move_tail(temp_list->next, master_list);
		sys_props.num_devices++;
	}
}

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 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
static void kfd_debug_print_topology(void)
{
	struct kfd_topology_device *dev;

	down_read(&topology_lock);

	dev = list_last_entry(&topology_device_list,
			struct kfd_topology_device, list);
	if (dev) {
		if (dev->node_props.cpu_cores_count &&
				dev->node_props.simd_count) {
			pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
				dev->node_props.device_id,
				dev->node_props.vendor_id);
		} else if (dev->node_props.cpu_cores_count)
			pr_info("Topology: Add CPU node\n");
		else if (dev->node_props.simd_count)
			pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
				dev->node_props.device_id,
				dev->node_props.vendor_id);
	}
	up_read(&topology_lock);
}

/* Helper function for intializing platform_xx members of
 * kfd_system_properties. Uses OEM info from the last CPU/APU node.
 */
static void kfd_update_system_properties(void)
{
	struct kfd_topology_device *dev;

	down_read(&topology_lock);
	dev = list_last_entry(&topology_device_list,
			struct kfd_topology_device, list);
	if (dev) {
		sys_props.platform_id =
			(*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
		sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
		sys_props.platform_rev = dev->oem_revision;
	}
	up_read(&topology_lock);
}

static void find_system_memory(const struct dmi_header *dm,
	void *private)
{
	struct kfd_mem_properties *mem;
	u16 mem_width, mem_clock;
	struct kfd_topology_device *kdev =
		(struct kfd_topology_device *)private;
	const u8 *dmi_data = (const u8 *)(dm + 1);

	if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
		mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
		mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
		list_for_each_entry(mem, &kdev->mem_props, list) {
			if (mem_width != 0xFFFF && mem_width != 0)
				mem->width = mem_width;
			if (mem_clock != 0)
				mem->mem_clk_max = mem_clock;
		}
	}
}
941 942 943 944 945 946 947 948

/*
 * Performance counters information is not part of CRAT but we would like to
 * put them in the sysfs under topology directory for Thunk to get the data.
 * This function is called before updating the sysfs.
 */
static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
{
949 950
	/* These are the only counters supported so far */
	return kfd_iommu_add_perf_counters(kdev);
951 952
}

953 954 955 956 957 958 959 960 961 962 963 964 965 966
/* kfd_add_non_crat_information - Add information that is not currently
 *	defined in CRAT but is necessary for KFD topology
 * @dev - topology device to which addition info is added
 */
static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
{
	/* Check if CPU only node. */
	if (!kdev->gpu) {
		/* Add system memory information */
		dmi_walk(find_system_memory, kdev);
	}
	/* TODO: For GPU node, rearrange code from kfd_topology_add_device */
}

967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
/* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
 *	Ignore CRAT for all other devices. AMD APU is identified if both CPU
 *	and GPU cores are present.
 * @device_list - topology device list created by parsing ACPI CRAT table.
 * @return - TRUE if invalid, FALSE is valid.
 */
static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
{
	struct kfd_topology_device *dev;

	list_for_each_entry(dev, device_list, list) {
		if (dev->node_props.cpu_cores_count &&
			dev->node_props.simd_count)
			return false;
	}
	pr_info("Ignoring ACPI CRAT on non-APU system\n");
	return true;
}

986 987
int kfd_topology_init(void)
{
988
	void *crat_image = NULL;
989 990
	size_t image_size = 0;
	int ret;
991
	struct list_head temp_topology_device_list;
992 993 994
	int cpu_only_node = 0;
	struct kfd_topology_device *kdev;
	int proximity_domain;
995

996 997 998 999
	/* topology_device_list - Master list of all topology devices
	 * temp_topology_device_list - temporary list created while parsing CRAT
	 * or VCRAT. Once parsing is complete the contents of list is moved to
	 * topology_device_list
1000
	 */
1001 1002

	/* Initialize the head for the both the lists */
1003
	INIT_LIST_HEAD(&topology_device_list);
1004
	INIT_LIST_HEAD(&temp_topology_device_list);
1005 1006 1007 1008
	init_rwsem(&topology_lock);

	memset(&sys_props, 0, sizeof(sys_props));

1009 1010 1011 1012 1013 1014 1015
	/* Proximity domains in ACPI CRAT tables start counting at
	 * 0. The same should be true for virtual CRAT tables created
	 * at this stage. GPUs added later in kfd_topology_add_device
	 * use a counter.
	 */
	proximity_domain = 0;

1016
	/*
1017
	 * Get the CRAT image from the ACPI. If ACPI doesn't have one
1018
	 * or if ACPI CRAT is invalid create a virtual CRAT.
1019 1020
	 * NOTE: The current implementation expects all AMD APUs to have
	 *	CRAT. If no CRAT is available, it is assumed to be a CPU
1021
	 */
1022 1023
	ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
	if (!ret) {
1024
		ret = kfd_parse_crat_table(crat_image,
1025 1026
					   &temp_topology_device_list,
					   proximity_domain);
1027 1028
		if (ret ||
		    kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
			kfd_release_topology_device_list(
				&temp_topology_device_list);
			kfd_destroy_crat_image(crat_image);
			crat_image = NULL;
		}
	}

	if (!crat_image) {
		ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
						    COMPUTE_UNIT_CPU, NULL,
						    proximity_domain);
		cpu_only_node = 1;
		if (ret) {
			pr_err("Error creating VCRAT table for CPU\n");
			return ret;
		}

		ret = kfd_parse_crat_table(crat_image,
					   &temp_topology_device_list,
					   proximity_domain);
		if (ret) {
			pr_err("Error parsing VCRAT table for CPU\n");
1051
			goto err;
1052
		}
1053 1054
	}

1055 1056 1057 1058
	kdev = list_first_entry(&temp_topology_device_list,
				struct kfd_topology_device, list);
	kfd_add_perf_to_topology(kdev);

1059
	down_write(&topology_lock);
1060 1061
	kfd_topology_update_device_list(&temp_topology_device_list,
					&topology_device_list);
1062
	atomic_set(&topology_crat_proximity_domain, sys_props.num_devices-1);
1063 1064 1065
	ret = kfd_topology_update_sysfs();
	up_write(&topology_lock);

1066 1067
	if (!ret) {
		sys_props.generation_count++;
1068 1069
		kfd_update_system_properties();
		kfd_debug_print_topology();
1070
	} else
1071 1072
		pr_err("Failed to update topology in sysfs ret=%d\n", ret);

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
	/* For nodes with GPU, this information gets added
	 * when GPU is detected (kfd_topology_add_device).
	 */
	if (cpu_only_node) {
		/* Add additional information to CPU only node created above */
		down_write(&topology_lock);
		kdev = list_first_entry(&topology_device_list,
				struct kfd_topology_device, list);
		up_write(&topology_lock);
		kfd_add_non_crat_information(kdev);
	}

1085
err:
1086
	kfd_destroy_crat_image(crat_image);
1087 1088 1089 1090 1091
	return ret;
}

void kfd_topology_shutdown(void)
{
1092
	down_write(&topology_lock);
1093 1094
	kfd_topology_release_sysfs();
	kfd_release_live_view();
1095
	up_write(&topology_lock);
1096 1097 1098 1099 1100 1101
}

static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
{
	uint32_t hashout;
	uint32_t buf[7];
1102
	uint64_t local_mem_size;
1103
	int i;
1104
	struct kfd_local_mem_info local_mem_info;
1105 1106 1107 1108

	if (!gpu)
		return 0;

1109
	amdgpu_amdkfd_get_local_mem_info(gpu->kgd, &local_mem_info);
1110 1111 1112

	local_mem_size = local_mem_info.local_mem_size_private +
			local_mem_info.local_mem_size_public;
1113

1114
	buf[0] = gpu->pdev->devfn;
1115 1116 1117
	buf[1] = gpu->pdev->subsystem_vendor |
		(gpu->pdev->subsystem_device << 16);
	buf[2] = pci_domain_nr(gpu->pdev->bus);
1118 1119
	buf[3] = gpu->pdev->device;
	buf[4] = gpu->pdev->bus->number;
1120 1121
	buf[5] = lower_32_bits(local_mem_size);
	buf[6] = upper_32_bits(local_mem_size);
1122 1123 1124 1125 1126 1127

	for (i = 0, hashout = 0; i < 7; i++)
		hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);

	return hashout;
}
1128 1129 1130 1131 1132
/* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
 *		the GPU device is not already present in the topology device
 *		list then return NULL. This means a new topology device has to
 *		be created for this GPU.
 */
1133 1134 1135
static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
{
	struct kfd_topology_device *dev;
1136
	struct kfd_topology_device *out_dev = NULL;
1137 1138 1139
	struct kfd_mem_properties *mem;
	struct kfd_cache_properties *cache;
	struct kfd_iolink_properties *iolink;
1140

1141
	down_write(&topology_lock);
1142 1143 1144 1145
	list_for_each_entry(dev, &topology_device_list, list) {
		/* Discrete GPUs need their own topology device list
		 * entries. Don't assign them to CPU/APU nodes.
		 */
1146
		if (!gpu->use_iommu_v2 &&
1147 1148 1149
		    dev->node_props.cpu_cores_count)
			continue;

1150
		if (!dev->gpu && (dev->node_props.simd_count > 0)) {
1151 1152
			dev->gpu = gpu;
			out_dev = dev;
1153 1154 1155 1156 1157 1158 1159

			list_for_each_entry(mem, &dev->mem_props, list)
				mem->gpu = dev->gpu;
			list_for_each_entry(cache, &dev->cache_props, list)
				cache->gpu = dev->gpu;
			list_for_each_entry(iolink, &dev->io_link_props, list)
				iolink->gpu = dev->gpu;
1160 1161
			break;
		}
1162
	}
1163
	up_write(&topology_lock);
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
	return out_dev;
}

static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
{
	/*
	 * TODO: Generate an event for thunk about the arrival/removal
	 * of the GPU
	 */
}

1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
/* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
 *		patch this after CRAT parsing.
 */
static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
{
	struct kfd_mem_properties *mem;
	struct kfd_local_mem_info local_mem_info;

	if (!dev)
		return;

	/* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
	 * single bank of VRAM local memory.
	 * for dGPUs - VCRAT reports only one bank of Local Memory
	 * for APUs - If CRAT from ACPI reports more than one bank, then
	 *	all the banks will report the same mem_clk_max information
	 */
1192
	amdgpu_amdkfd_get_local_mem_info(dev->gpu->kgd, &local_mem_info);
1193 1194 1195 1196 1197

	list_for_each_entry(mem, &dev->mem_props, list)
		mem->mem_clk_max = local_mem_info.mem_clk_max;
}

1198 1199 1200
static void kfd_set_iolink_no_atomics(struct kfd_topology_device *dev,
					struct kfd_topology_device *target_gpu_dev,
					struct kfd_iolink_properties *link)
1201
{
1202 1203
	/* xgmi always supports atomics between links. */
	if (link->iolink_type == CRAT_IOLINK_TYPE_XGMI)
1204 1205
		return;

1206 1207 1208 1209 1210
	/* check pcie support to set cpu(dev) flags for target_gpu_dev link. */
	if (target_gpu_dev) {
		uint32_t cap;

		pcie_capability_read_dword(target_gpu_dev->gpu->pdev,
1211
				PCI_EXP_DEVCAP2, &cap);
1212

1213 1214
		if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
			     PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1215
			link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1216
				CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1217 1218
	/* set gpu (dev) flags. */
	} else {
1219 1220 1221
		if (!dev->gpu->pci_atomic_requested ||
				dev->gpu->device_info->asic_family ==
							CHIP_HAWAII)
1222
			link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1223 1224
				CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
	}
1225 1226
}

1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
static void kfd_set_iolink_non_coherent(struct kfd_topology_device *to_dev,
		struct kfd_iolink_properties *outbound_link,
		struct kfd_iolink_properties *inbound_link)
{
	/* CPU -> GPU with PCIe */
	if (!to_dev->gpu &&
	    inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
		inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;

	if (to_dev->gpu) {
		/* GPU <-> GPU with PCIe and
		 * Vega20 with XGMI
		 */
		if (inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS ||
		    (inbound_link->iolink_type == CRAT_IOLINK_TYPE_XGMI &&
		    to_dev->gpu->device_info->asic_family == CHIP_VEGA20)) {
			outbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
			inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
		}
	}
}

1249 1250 1251 1252 1253 1254 1255
static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
{
	struct kfd_iolink_properties *link, *inbound_link;
	struct kfd_topology_device *peer_dev;

	if (!dev || !dev->gpu)
		return;
1256 1257 1258

	/* GPU only creates direct links so apply flags setting to all */
	list_for_each_entry(link, &dev->io_link_props, list) {
1259 1260 1261
		link->flags = CRAT_IOLINK_FLAGS_ENABLED;
		kfd_set_iolink_no_atomics(dev, NULL, link);
		peer_dev = kfd_topology_device_by_proximity_domain(
1262
				link->node_to);
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

		if (!peer_dev)
			continue;

		list_for_each_entry(inbound_link, &peer_dev->io_link_props,
									list) {
			if (inbound_link->node_to != link->node_from)
				continue;

			inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
			kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
1274
			kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
1275 1276
		}
	}
1277 1278
}

1279 1280 1281 1282
int kfd_topology_add_device(struct kfd_dev *gpu)
{
	uint32_t gpu_id;
	struct kfd_topology_device *dev;
1283
	struct kfd_cu_info cu_info;
1284 1285
	int res = 0;
	struct list_head temp_topology_device_list;
1286 1287 1288
	void *crat_image = NULL;
	size_t image_size = 0;
	int proximity_domain;
1289
	struct amdgpu_device *adev;
1290 1291

	INIT_LIST_HEAD(&temp_topology_device_list);
1292 1293 1294

	gpu_id = kfd_generate_gpu_id(gpu);

1295
	pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1296

1297 1298
	proximity_domain = atomic_inc_return(&topology_crat_proximity_domain);

1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
	adev = (struct amdgpu_device *)(gpu->kgd);

	/* Include the CPU in xGMI hive if xGMI connected by assigning it the hive ID. */
	if (gpu->hive_id && adev->gmc.xgmi.connected_to_cpu) {
		struct kfd_topology_device *top_dev;

		down_read(&topology_lock);

		list_for_each_entry(top_dev, &topology_device_list, list) {
			if (top_dev->gpu)
				break;

			top_dev->node_props.hive_id = gpu->hive_id;
		}

		up_read(&topology_lock);
	}

1317 1318 1319 1320 1321
	/* Check to see if this gpu device exists in the topology_device_list.
	 * If so, assign the gpu to that device,
	 * else create a Virtual CRAT for this gpu device and then parse that
	 * CRAT to create a new topology device. Once created assign the gpu to
	 * that topology device
1322 1323 1324
	 */
	dev = kfd_assign_gpu(gpu);
	if (!dev) {
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
		res = kfd_create_crat_image_virtual(&crat_image, &image_size,
						    COMPUTE_UNIT_GPU, gpu,
						    proximity_domain);
		if (res) {
			pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
			       gpu_id);
			return res;
		}
		res = kfd_parse_crat_table(crat_image,
					   &temp_topology_device_list,
					   proximity_domain);
		if (res) {
			pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
			       gpu_id);
1339 1340
			goto err;
		}
1341 1342 1343 1344 1345

		down_write(&topology_lock);
		kfd_topology_update_device_list(&temp_topology_device_list,
			&topology_device_list);

1346 1347
		/* Update the SYSFS tree, since we added another topology
		 * device
1348
		 */
1349
		res = kfd_topology_update_sysfs();
1350 1351
		up_write(&topology_lock);

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
		if (!res)
			sys_props.generation_count++;
		else
			pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
						gpu_id, res);
		dev = kfd_assign_gpu(gpu);
		if (WARN_ON(!dev)) {
			res = -ENODEV;
			goto err;
		}
1362 1363 1364 1365
	}

	dev->gpu_id = gpu_id;
	gpu->id = gpu_id;
1366 1367 1368 1369 1370 1371 1372 1373 1374

	/* TODO: Move the following lines to function
	 *	kfd_add_non_crat_information
	 */

	/* Fill-in additional information that is not available in CRAT but
	 * needed for the topology
	 */

1375
	amdgpu_amdkfd_get_cu_info(dev->gpu->kgd, &cu_info);
1376 1377 1378 1379

	strncpy(dev->node_props.name, gpu->device_info->asic_name,
			KFD_TOPOLOGY_PUBLIC_NAME_SIZE);

1380 1381 1382
	dev->node_props.simd_arrays_per_engine =
		cu_info.num_shader_arrays_per_engine;

1383
	dev->node_props.gfx_target_version = gpu->device_info->gfx_target_version;
1384 1385
	dev->node_props.vendor_id = gpu->pdev->vendor;
	dev->node_props.device_id = gpu->pdev->device;
1386 1387 1388 1389
	dev->node_props.capability |=
		((amdgpu_amdkfd_get_asic_rev_id(dev->gpu->kgd) <<
			HSA_CAP_ASIC_REVISION_SHIFT) &
			HSA_CAP_ASIC_REVISION_MASK);
1390
	dev->node_props.location_id = pci_dev_id(gpu->pdev);
1391
	dev->node_props.domain = pci_domain_nr(gpu->pdev->bus);
1392
	dev->node_props.max_engine_clk_fcompute =
1393
		amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->kgd);
1394 1395
	dev->node_props.max_engine_clk_ccompute =
		cpufreq_quick_get_max(0) / 1000;
1396 1397
	dev->node_props.drm_render_minor =
		gpu->shared_resources.drm_render_minor;
1398

1399
	dev->node_props.hive_id = gpu->hive_id;
1400 1401 1402
	dev->node_props.num_sdma_engines = gpu->device_info->num_sdma_engines;
	dev->node_props.num_sdma_xgmi_engines =
				gpu->device_info->num_xgmi_sdma_engines;
1403 1404
	dev->node_props.num_sdma_queues_per_engine =
				gpu->device_info->num_sdma_queues_per_engine;
1405
	dev->node_props.num_gws = (dev->gpu->gws &&
1406 1407
		dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
		amdgpu_amdkfd_get_num_gws(dev->gpu->kgd) : 0;
1408
	dev->node_props.num_cp_queues = get_cp_queues_num(dev->gpu->dqm);
1409

1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
	kfd_fill_mem_clk_max_info(dev);
	kfd_fill_iolink_non_crat_info(dev);

	switch (dev->gpu->device_info->asic_family) {
	case CHIP_KAVERI:
	case CHIP_HAWAII:
	case CHIP_TONGA:
		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
		break;
	case CHIP_CARRIZO:
	case CHIP_FIJI:
	case CHIP_POLARIS10:
	case CHIP_POLARIS11:
1425
	case CHIP_POLARIS12:
K
Kent Russell 已提交
1426
	case CHIP_VEGAM:
1427
		pr_debug("Adding doorbell packet type capability\n");
1428 1429 1430 1431
		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
		break;
1432
	case CHIP_VEGA10:
1433
	case CHIP_VEGA12:
1434
	case CHIP_VEGA20:
1435
	case CHIP_RAVEN:
H
Huang Rui 已提交
1436
	case CHIP_RENOIR:
Y
Yong Zhao 已提交
1437
	case CHIP_ARCTURUS:
1438
	case CHIP_ALDEBARAN:
1439
	case CHIP_NAVI10:
1440
	case CHIP_NAVI12:
Y
Yong Zhao 已提交
1441
	case CHIP_NAVI14:
1442
	case CHIP_SIENNA_CICHLID:
1443
	case CHIP_NAVY_FLOUNDER:
H
Huang Rui 已提交
1444
	case CHIP_VANGOGH:
1445
	case CHIP_DIMGREY_CAVEFISH:
1446
	case CHIP_BEIGE_GOBY:
1447
	case CHIP_YELLOW_CARP:
T
Tao Zhou 已提交
1448
	case CHIP_CYAN_SKILLFISH:
1449 1450 1451 1452
		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
		break;
1453 1454 1455
	default:
		WARN(1, "Unexpected ASIC family %u",
		     dev->gpu->device_info->asic_family);
1456 1457
	}

1458 1459 1460 1461
	/*
	* Overwrite ATS capability according to needs_iommu_device to fix
	* potential missing corresponding bit in CRAT of BIOS.
	*/
1462
	if (dev->gpu->use_iommu_v2)
1463 1464 1465 1466
		dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
	else
		dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;

1467 1468 1469
	/* Fix errors in CZ CRAT.
	 * simd_count: Carrizo CRAT reports wrong simd_count, probably
	 *		because it doesn't consider masked out CUs
1470
	 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
1471
	 */
1472
	if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
1473 1474
		dev->node_props.simd_count =
			cu_info.simd_per_cu * cu_info.cu_active_number;
1475 1476
		dev->node_props.max_waves_per_simd = 10;
	}
1477

1478 1479
	/* kfd only concerns sram ecc on GFX and HBM ecc on UMC */
	dev->node_props.capability |=
1480
		((adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0) ?
1481
		HSA_CAP_SRAM_EDCSUPPORTED : 0;
1482
	dev->node_props.capability |= ((adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
1483 1484 1485
		HSA_CAP_MEM_EDCSUPPORTED : 0;

	if (adev->asic_type != CHIP_VEGA10)
1486
		dev->node_props.capability |= (adev->ras_enabled != 0) ?
1487 1488
			HSA_CAP_RASEVENTNOTIFY : 0;

1489
	if (KFD_IS_SVM_API_SUPPORTED(adev->kfd.dev))
1490 1491
		dev->node_props.capability |= HSA_CAP_SVMAPI_SUPPORTED;

1492 1493
	kfd_debug_print_topology();

1494
	if (!res)
1495
		kfd_notify_gpu_change(gpu_id, 1);
1496
err:
1497
	kfd_destroy_crat_image(crat_image);
1498 1499 1500 1501 1502
	return res;
}

int kfd_topology_remove_device(struct kfd_dev *gpu)
{
1503
	struct kfd_topology_device *dev, *tmp;
1504 1505 1506 1507 1508
	uint32_t gpu_id;
	int res = -ENODEV;

	down_write(&topology_lock);

1509
	list_for_each_entry_safe(dev, tmp, &topology_device_list, list)
1510 1511 1512 1513
		if (dev->gpu == gpu) {
			gpu_id = dev->gpu_id;
			kfd_remove_sysfs_node_entry(dev);
			kfd_release_topology_device(dev);
1514
			sys_props.num_devices--;
1515 1516 1517 1518 1519 1520 1521 1522
			res = 0;
			if (kfd_topology_update_sysfs() < 0)
				kfd_topology_release_sysfs();
			break;
		}

	up_write(&topology_lock);

1523
	if (!res)
1524 1525 1526 1527 1528
		kfd_notify_gpu_change(gpu_id, 0);

	return res;
}

1529 1530 1531 1532 1533
/* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
 *	topology. If GPU device is found @idx, then valid kfd_dev pointer is
 *	returned through @kdev
 * Return -	0: On success (@kdev will be NULL for non GPU nodes)
 *		-1: If end of list
1534
 */
1535
int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
1536 1537 1538 1539 1540
{

	struct kfd_topology_device *top_dev;
	uint8_t device_idx = 0;

1541
	*kdev = NULL;
1542 1543 1544 1545
	down_read(&topology_lock);

	list_for_each_entry(top_dev, &topology_device_list, list) {
		if (device_idx == idx) {
1546 1547 1548
			*kdev = top_dev->gpu;
			up_read(&topology_lock);
			return 0;
1549 1550 1551 1552 1553 1554 1555
		}

		device_idx++;
	}

	up_read(&topology_lock);

1556
	return -1;
1557 1558

}
1559

1560 1561 1562 1563 1564 1565 1566 1567 1568
static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
{
	int first_cpu_of_numa_node;

	if (!cpumask || cpumask == cpu_none_mask)
		return -1;
	first_cpu_of_numa_node = cpumask_first(cpumask);
	if (first_cpu_of_numa_node >= nr_cpu_ids)
		return -1;
1569 1570 1571 1572 1573
#ifdef CONFIG_X86_64
	return cpu_data(first_cpu_of_numa_node).apicid;
#else
	return first_cpu_of_numa_node;
#endif
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
}

/* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
 *	of the given NUMA node (numa_node_id)
 * Return -1 on failure
 */
int kfd_numa_node_to_apic_id(int numa_node_id)
{
	if (numa_node_id == -1) {
		pr_warn("Invalid NUMA Node. Use online CPU mask\n");
		return kfd_cpumask_to_apic_id(cpu_online_mask);
	}
	return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
}

1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
void kfd_double_confirm_iommu_support(struct kfd_dev *gpu)
{
	struct kfd_topology_device *dev;

	gpu->use_iommu_v2 = false;

	if (!gpu->device_info->needs_iommu_device)
		return;

	down_read(&topology_lock);

	/* Only use IOMMUv2 if there is an APU topology node with no GPU
	 * assigned yet. This GPU will be assigned to it.
	 */
	list_for_each_entry(dev, &topology_device_list, list)
		if (dev->node_props.cpu_cores_count &&
		    dev->node_props.simd_count &&
		    !dev->gpu)
			gpu->use_iommu_v2 = true;

	up_read(&topology_lock);
}

1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
#if defined(CONFIG_DEBUG_FS)

int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
{
	struct kfd_topology_device *dev;
	unsigned int i = 0;
	int r = 0;

	down_read(&topology_lock);

	list_for_each_entry(dev, &topology_device_list, list) {
		if (!dev->gpu) {
			i++;
			continue;
		}

		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
		r = dqm_debugfs_hqds(m, dev->gpu->dqm);
		if (r)
			break;
	}

	up_read(&topology_lock);

	return r;
}

int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
{
	struct kfd_topology_device *dev;
	unsigned int i = 0;
	int r = 0;

	down_read(&topology_lock);

	list_for_each_entry(dev, &topology_device_list, list) {
		if (!dev->gpu) {
			i++;
			continue;
		}

		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1654
		r = pm_debugfs_runlist(m, &dev->gpu->dqm->packet_mgr);
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
		if (r)
			break;
	}

	up_read(&topology_lock);

	return r;
}

#endif