cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

kfd_topology.c (48308B)


      1// SPDX-License-Identifier: GPL-2.0 OR MIT
      2/*
      3 * Copyright 2014-2022 Advanced Micro Devices, Inc.
      4 *
      5 * Permission is hereby granted, free of charge, to any person obtaining a
      6 * copy of this software and associated documentation files (the "Software"),
      7 * to deal in the Software without restriction, including without limitation
      8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      9 * and/or sell copies of the Software, and to permit persons to whom the
     10 * Software is furnished to do so, subject to the following conditions:
     11 *
     12 * The above copyright notice and this permission notice shall be included in
     13 * all copies or substantial portions of the Software.
     14 *
     15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     21 * OTHER DEALINGS IN THE SOFTWARE.
     22 */
     23
     24#include <linux/types.h>
     25#include <linux/kernel.h>
     26#include <linux/pci.h>
     27#include <linux/errno.h>
     28#include <linux/acpi.h>
     29#include <linux/hash.h>
     30#include <linux/cpufreq.h>
     31#include <linux/log2.h>
     32#include <linux/dmi.h>
     33#include <linux/atomic.h>
     34
     35#include "kfd_priv.h"
     36#include "kfd_crat.h"
     37#include "kfd_topology.h"
     38#include "kfd_device_queue_manager.h"
     39#include "kfd_iommu.h"
     40#include "kfd_svm.h"
     41#include "amdgpu_amdkfd.h"
     42#include "amdgpu_ras.h"
     43
     44/* topology_device_list - Master list of all topology devices */
     45static struct list_head topology_device_list;
     46static struct kfd_system_properties sys_props;
     47
     48static DECLARE_RWSEM(topology_lock);
     49static uint32_t topology_crat_proximity_domain;
     50
     51struct kfd_topology_device *kfd_topology_device_by_proximity_domain_no_lock(
     52						uint32_t proximity_domain)
     53{
     54	struct kfd_topology_device *top_dev;
     55	struct kfd_topology_device *device = NULL;
     56
     57	list_for_each_entry(top_dev, &topology_device_list, list)
     58		if (top_dev->proximity_domain == proximity_domain) {
     59			device = top_dev;
     60			break;
     61		}
     62
     63	return device;
     64}
     65
     66struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
     67						uint32_t proximity_domain)
     68{
     69	struct kfd_topology_device *device = NULL;
     70
     71	down_read(&topology_lock);
     72
     73	device = kfd_topology_device_by_proximity_domain_no_lock(
     74							proximity_domain);
     75	up_read(&topology_lock);
     76
     77	return device;
     78}
     79
     80struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
     81{
     82	struct kfd_topology_device *top_dev = NULL;
     83	struct kfd_topology_device *ret = NULL;
     84
     85	down_read(&topology_lock);
     86
     87	list_for_each_entry(top_dev, &topology_device_list, list)
     88		if (top_dev->gpu_id == gpu_id) {
     89			ret = top_dev;
     90			break;
     91		}
     92
     93	up_read(&topology_lock);
     94
     95	return ret;
     96}
     97
     98struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
     99{
    100	struct kfd_topology_device *top_dev;
    101
    102	top_dev = kfd_topology_device_by_id(gpu_id);
    103	if (!top_dev)
    104		return NULL;
    105
    106	return top_dev->gpu;
    107}
    108
    109struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
    110{
    111	struct kfd_topology_device *top_dev;
    112	struct kfd_dev *device = NULL;
    113
    114	down_read(&topology_lock);
    115
    116	list_for_each_entry(top_dev, &topology_device_list, list)
    117		if (top_dev->gpu && top_dev->gpu->pdev == pdev) {
    118			device = top_dev->gpu;
    119			break;
    120		}
    121
    122	up_read(&topology_lock);
    123
    124	return device;
    125}
    126
    127struct kfd_dev *kfd_device_by_adev(const struct amdgpu_device *adev)
    128{
    129	struct kfd_topology_device *top_dev;
    130	struct kfd_dev *device = NULL;
    131
    132	down_read(&topology_lock);
    133
    134	list_for_each_entry(top_dev, &topology_device_list, list)
    135		if (top_dev->gpu && top_dev->gpu->adev == adev) {
    136			device = top_dev->gpu;
    137			break;
    138		}
    139
    140	up_read(&topology_lock);
    141
    142	return device;
    143}
    144
    145/* Called with write topology_lock acquired */
    146static void kfd_release_topology_device(struct kfd_topology_device *dev)
    147{
    148	struct kfd_mem_properties *mem;
    149	struct kfd_cache_properties *cache;
    150	struct kfd_iolink_properties *iolink;
    151	struct kfd_perf_properties *perf;
    152
    153	list_del(&dev->list);
    154
    155	while (dev->mem_props.next != &dev->mem_props) {
    156		mem = container_of(dev->mem_props.next,
    157				struct kfd_mem_properties, list);
    158		list_del(&mem->list);
    159		kfree(mem);
    160	}
    161
    162	while (dev->cache_props.next != &dev->cache_props) {
    163		cache = container_of(dev->cache_props.next,
    164				struct kfd_cache_properties, list);
    165		list_del(&cache->list);
    166		kfree(cache);
    167	}
    168
    169	while (dev->io_link_props.next != &dev->io_link_props) {
    170		iolink = container_of(dev->io_link_props.next,
    171				struct kfd_iolink_properties, list);
    172		list_del(&iolink->list);
    173		kfree(iolink);
    174	}
    175
    176	while (dev->perf_props.next != &dev->perf_props) {
    177		perf = container_of(dev->perf_props.next,
    178				struct kfd_perf_properties, list);
    179		list_del(&perf->list);
    180		kfree(perf);
    181	}
    182
    183	kfree(dev);
    184}
    185
    186void kfd_release_topology_device_list(struct list_head *device_list)
    187{
    188	struct kfd_topology_device *dev;
    189
    190	while (!list_empty(device_list)) {
    191		dev = list_first_entry(device_list,
    192				       struct kfd_topology_device, list);
    193		kfd_release_topology_device(dev);
    194	}
    195}
    196
    197static void kfd_release_live_view(void)
    198{
    199	kfd_release_topology_device_list(&topology_device_list);
    200	memset(&sys_props, 0, sizeof(sys_props));
    201}
    202
    203struct kfd_topology_device *kfd_create_topology_device(
    204				struct list_head *device_list)
    205{
    206	struct kfd_topology_device *dev;
    207
    208	dev = kfd_alloc_struct(dev);
    209	if (!dev) {
    210		pr_err("No memory to allocate a topology device");
    211		return NULL;
    212	}
    213
    214	INIT_LIST_HEAD(&dev->mem_props);
    215	INIT_LIST_HEAD(&dev->cache_props);
    216	INIT_LIST_HEAD(&dev->io_link_props);
    217	INIT_LIST_HEAD(&dev->perf_props);
    218
    219	list_add_tail(&dev->list, device_list);
    220
    221	return dev;
    222}
    223
    224
    225#define sysfs_show_gen_prop(buffer, offs, fmt, ...)		\
    226		(offs += snprintf(buffer+offs, PAGE_SIZE-offs,	\
    227				  fmt, __VA_ARGS__))
    228#define sysfs_show_32bit_prop(buffer, offs, name, value) \
    229		sysfs_show_gen_prop(buffer, offs, "%s %u\n", name, value)
    230#define sysfs_show_64bit_prop(buffer, offs, name, value) \
    231		sysfs_show_gen_prop(buffer, offs, "%s %llu\n", name, value)
    232#define sysfs_show_32bit_val(buffer, offs, value) \
    233		sysfs_show_gen_prop(buffer, offs, "%u\n", value)
    234#define sysfs_show_str_val(buffer, offs, value) \
    235		sysfs_show_gen_prop(buffer, offs, "%s\n", value)
    236
    237static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
    238		char *buffer)
    239{
    240	int offs = 0;
    241
    242	/* Making sure that the buffer is an empty string */
    243	buffer[0] = 0;
    244
    245	if (attr == &sys_props.attr_genid) {
    246		sysfs_show_32bit_val(buffer, offs,
    247				     sys_props.generation_count);
    248	} else if (attr == &sys_props.attr_props) {
    249		sysfs_show_64bit_prop(buffer, offs, "platform_oem",
    250				      sys_props.platform_oem);
    251		sysfs_show_64bit_prop(buffer, offs, "platform_id",
    252				      sys_props.platform_id);
    253		sysfs_show_64bit_prop(buffer, offs, "platform_rev",
    254				      sys_props.platform_rev);
    255	} else {
    256		offs = -EINVAL;
    257	}
    258
    259	return offs;
    260}
    261
    262static void kfd_topology_kobj_release(struct kobject *kobj)
    263{
    264	kfree(kobj);
    265}
    266
    267static const struct sysfs_ops sysprops_ops = {
    268	.show = sysprops_show,
    269};
    270
    271static struct kobj_type sysprops_type = {
    272	.release = kfd_topology_kobj_release,
    273	.sysfs_ops = &sysprops_ops,
    274};
    275
    276static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
    277		char *buffer)
    278{
    279	int offs = 0;
    280	struct kfd_iolink_properties *iolink;
    281
    282	/* Making sure that the buffer is an empty string */
    283	buffer[0] = 0;
    284
    285	iolink = container_of(attr, struct kfd_iolink_properties, attr);
    286	if (iolink->gpu && kfd_devcgroup_check_permission(iolink->gpu))
    287		return -EPERM;
    288	sysfs_show_32bit_prop(buffer, offs, "type", iolink->iolink_type);
    289	sysfs_show_32bit_prop(buffer, offs, "version_major", iolink->ver_maj);
    290	sysfs_show_32bit_prop(buffer, offs, "version_minor", iolink->ver_min);
    291	sysfs_show_32bit_prop(buffer, offs, "node_from", iolink->node_from);
    292	sysfs_show_32bit_prop(buffer, offs, "node_to", iolink->node_to);
    293	sysfs_show_32bit_prop(buffer, offs, "weight", iolink->weight);
    294	sysfs_show_32bit_prop(buffer, offs, "min_latency", iolink->min_latency);
    295	sysfs_show_32bit_prop(buffer, offs, "max_latency", iolink->max_latency);
    296	sysfs_show_32bit_prop(buffer, offs, "min_bandwidth",
    297			      iolink->min_bandwidth);
    298	sysfs_show_32bit_prop(buffer, offs, "max_bandwidth",
    299			      iolink->max_bandwidth);
    300	sysfs_show_32bit_prop(buffer, offs, "recommended_transfer_size",
    301			      iolink->rec_transfer_size);
    302	sysfs_show_32bit_prop(buffer, offs, "flags", iolink->flags);
    303
    304	return offs;
    305}
    306
    307static const struct sysfs_ops iolink_ops = {
    308	.show = iolink_show,
    309};
    310
    311static struct kobj_type iolink_type = {
    312	.release = kfd_topology_kobj_release,
    313	.sysfs_ops = &iolink_ops,
    314};
    315
    316static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
    317		char *buffer)
    318{
    319	int offs = 0;
    320	struct kfd_mem_properties *mem;
    321
    322	/* Making sure that the buffer is an empty string */
    323	buffer[0] = 0;
    324
    325	mem = container_of(attr, struct kfd_mem_properties, attr);
    326	if (mem->gpu && kfd_devcgroup_check_permission(mem->gpu))
    327		return -EPERM;
    328	sysfs_show_32bit_prop(buffer, offs, "heap_type", mem->heap_type);
    329	sysfs_show_64bit_prop(buffer, offs, "size_in_bytes",
    330			      mem->size_in_bytes);
    331	sysfs_show_32bit_prop(buffer, offs, "flags", mem->flags);
    332	sysfs_show_32bit_prop(buffer, offs, "width", mem->width);
    333	sysfs_show_32bit_prop(buffer, offs, "mem_clk_max",
    334			      mem->mem_clk_max);
    335
    336	return offs;
    337}
    338
    339static const struct sysfs_ops mem_ops = {
    340	.show = mem_show,
    341};
    342
    343static struct kobj_type mem_type = {
    344	.release = kfd_topology_kobj_release,
    345	.sysfs_ops = &mem_ops,
    346};
    347
    348static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
    349		char *buffer)
    350{
    351	int offs = 0;
    352	uint32_t i, j;
    353	struct kfd_cache_properties *cache;
    354
    355	/* Making sure that the buffer is an empty string */
    356	buffer[0] = 0;
    357
    358	cache = container_of(attr, struct kfd_cache_properties, attr);
    359	if (cache->gpu && kfd_devcgroup_check_permission(cache->gpu))
    360		return -EPERM;
    361	sysfs_show_32bit_prop(buffer, offs, "processor_id_low",
    362			cache->processor_id_low);
    363	sysfs_show_32bit_prop(buffer, offs, "level", cache->cache_level);
    364	sysfs_show_32bit_prop(buffer, offs, "size", cache->cache_size);
    365	sysfs_show_32bit_prop(buffer, offs, "cache_line_size",
    366			      cache->cacheline_size);
    367	sysfs_show_32bit_prop(buffer, offs, "cache_lines_per_tag",
    368			      cache->cachelines_per_tag);
    369	sysfs_show_32bit_prop(buffer, offs, "association", cache->cache_assoc);
    370	sysfs_show_32bit_prop(buffer, offs, "latency", cache->cache_latency);
    371	sysfs_show_32bit_prop(buffer, offs, "type", cache->cache_type);
    372	offs += snprintf(buffer+offs, PAGE_SIZE-offs, "sibling_map ");
    373	for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++)
    374		for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++)
    375			/* Check each bit */
    376			offs += snprintf(buffer+offs, PAGE_SIZE-offs, "%d,",
    377					 (cache->sibling_map[i] >> j) & 1);
    378
    379	/* Replace the last "," with end of line */
    380	buffer[offs-1] = '\n';
    381	return offs;
    382}
    383
    384static const struct sysfs_ops cache_ops = {
    385	.show = kfd_cache_show,
    386};
    387
    388static struct kobj_type cache_type = {
    389	.release = kfd_topology_kobj_release,
    390	.sysfs_ops = &cache_ops,
    391};
    392
    393/****** Sysfs of Performance Counters ******/
    394
    395struct kfd_perf_attr {
    396	struct kobj_attribute attr;
    397	uint32_t data;
    398};
    399
    400static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
    401			char *buf)
    402{
    403	int offs = 0;
    404	struct kfd_perf_attr *attr;
    405
    406	buf[0] = 0;
    407	attr = container_of(attrs, struct kfd_perf_attr, attr);
    408	if (!attr->data) /* invalid data for PMC */
    409		return 0;
    410	else
    411		return sysfs_show_32bit_val(buf, offs, attr->data);
    412}
    413
    414#define KFD_PERF_DESC(_name, _data)			\
    415{							\
    416	.attr  = __ATTR(_name, 0444, perf_show, NULL),	\
    417	.data = _data,					\
    418}
    419
    420static struct kfd_perf_attr perf_attr_iommu[] = {
    421	KFD_PERF_DESC(max_concurrent, 0),
    422	KFD_PERF_DESC(num_counters, 0),
    423	KFD_PERF_DESC(counter_ids, 0),
    424};
    425/****************************************/
    426
    427static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
    428		char *buffer)
    429{
    430	int offs = 0;
    431	struct kfd_topology_device *dev;
    432	uint32_t log_max_watch_addr;
    433
    434	/* Making sure that the buffer is an empty string */
    435	buffer[0] = 0;
    436
    437	if (strcmp(attr->name, "gpu_id") == 0) {
    438		dev = container_of(attr, struct kfd_topology_device,
    439				attr_gpuid);
    440		if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
    441			return -EPERM;
    442		return sysfs_show_32bit_val(buffer, offs, dev->gpu_id);
    443	}
    444
    445	if (strcmp(attr->name, "name") == 0) {
    446		dev = container_of(attr, struct kfd_topology_device,
    447				attr_name);
    448
    449		if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
    450			return -EPERM;
    451		return sysfs_show_str_val(buffer, offs, dev->node_props.name);
    452	}
    453
    454	dev = container_of(attr, struct kfd_topology_device,
    455			attr_props);
    456	if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
    457		return -EPERM;
    458	sysfs_show_32bit_prop(buffer, offs, "cpu_cores_count",
    459			      dev->node_props.cpu_cores_count);
    460	sysfs_show_32bit_prop(buffer, offs, "simd_count",
    461			      dev->gpu ? dev->node_props.simd_count : 0);
    462	sysfs_show_32bit_prop(buffer, offs, "mem_banks_count",
    463			      dev->node_props.mem_banks_count);
    464	sysfs_show_32bit_prop(buffer, offs, "caches_count",
    465			      dev->node_props.caches_count);
    466	sysfs_show_32bit_prop(buffer, offs, "io_links_count",
    467			      dev->node_props.io_links_count);
    468	sysfs_show_32bit_prop(buffer, offs, "cpu_core_id_base",
    469			      dev->node_props.cpu_core_id_base);
    470	sysfs_show_32bit_prop(buffer, offs, "simd_id_base",
    471			      dev->node_props.simd_id_base);
    472	sysfs_show_32bit_prop(buffer, offs, "max_waves_per_simd",
    473			      dev->node_props.max_waves_per_simd);
    474	sysfs_show_32bit_prop(buffer, offs, "lds_size_in_kb",
    475			      dev->node_props.lds_size_in_kb);
    476	sysfs_show_32bit_prop(buffer, offs, "gds_size_in_kb",
    477			      dev->node_props.gds_size_in_kb);
    478	sysfs_show_32bit_prop(buffer, offs, "num_gws",
    479			      dev->node_props.num_gws);
    480	sysfs_show_32bit_prop(buffer, offs, "wave_front_size",
    481			      dev->node_props.wave_front_size);
    482	sysfs_show_32bit_prop(buffer, offs, "array_count",
    483			      dev->node_props.array_count);
    484	sysfs_show_32bit_prop(buffer, offs, "simd_arrays_per_engine",
    485			      dev->node_props.simd_arrays_per_engine);
    486	sysfs_show_32bit_prop(buffer, offs, "cu_per_simd_array",
    487			      dev->node_props.cu_per_simd_array);
    488	sysfs_show_32bit_prop(buffer, offs, "simd_per_cu",
    489			      dev->node_props.simd_per_cu);
    490	sysfs_show_32bit_prop(buffer, offs, "max_slots_scratch_cu",
    491			      dev->node_props.max_slots_scratch_cu);
    492	sysfs_show_32bit_prop(buffer, offs, "gfx_target_version",
    493			      dev->node_props.gfx_target_version);
    494	sysfs_show_32bit_prop(buffer, offs, "vendor_id",
    495			      dev->node_props.vendor_id);
    496	sysfs_show_32bit_prop(buffer, offs, "device_id",
    497			      dev->node_props.device_id);
    498	sysfs_show_32bit_prop(buffer, offs, "location_id",
    499			      dev->node_props.location_id);
    500	sysfs_show_32bit_prop(buffer, offs, "domain",
    501			      dev->node_props.domain);
    502	sysfs_show_32bit_prop(buffer, offs, "drm_render_minor",
    503			      dev->node_props.drm_render_minor);
    504	sysfs_show_64bit_prop(buffer, offs, "hive_id",
    505			      dev->node_props.hive_id);
    506	sysfs_show_32bit_prop(buffer, offs, "num_sdma_engines",
    507			      dev->node_props.num_sdma_engines);
    508	sysfs_show_32bit_prop(buffer, offs, "num_sdma_xgmi_engines",
    509			      dev->node_props.num_sdma_xgmi_engines);
    510	sysfs_show_32bit_prop(buffer, offs, "num_sdma_queues_per_engine",
    511			      dev->node_props.num_sdma_queues_per_engine);
    512	sysfs_show_32bit_prop(buffer, offs, "num_cp_queues",
    513			      dev->node_props.num_cp_queues);
    514
    515	if (dev->gpu) {
    516		log_max_watch_addr =
    517			__ilog2_u32(dev->gpu->device_info.num_of_watch_points);
    518
    519		if (log_max_watch_addr) {
    520			dev->node_props.capability |=
    521					HSA_CAP_WATCH_POINTS_SUPPORTED;
    522
    523			dev->node_props.capability |=
    524				((log_max_watch_addr <<
    525					HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
    526				HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
    527		}
    528
    529		if (dev->gpu->adev->asic_type == CHIP_TONGA)
    530			dev->node_props.capability |=
    531					HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
    532
    533		sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_fcompute",
    534			dev->node_props.max_engine_clk_fcompute);
    535
    536		sysfs_show_64bit_prop(buffer, offs, "local_mem_size", 0ULL);
    537
    538		sysfs_show_32bit_prop(buffer, offs, "fw_version",
    539				      dev->gpu->mec_fw_version);
    540		sysfs_show_32bit_prop(buffer, offs, "capability",
    541				      dev->node_props.capability);
    542		sysfs_show_32bit_prop(buffer, offs, "sdma_fw_version",
    543				      dev->gpu->sdma_fw_version);
    544		sysfs_show_64bit_prop(buffer, offs, "unique_id",
    545				      dev->gpu->adev->unique_id);
    546
    547	}
    548
    549	return sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_ccompute",
    550				     cpufreq_quick_get_max(0)/1000);
    551}
    552
    553static const struct sysfs_ops node_ops = {
    554	.show = node_show,
    555};
    556
    557static struct kobj_type node_type = {
    558	.release = kfd_topology_kobj_release,
    559	.sysfs_ops = &node_ops,
    560};
    561
    562static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
    563{
    564	sysfs_remove_file(kobj, attr);
    565	kobject_del(kobj);
    566	kobject_put(kobj);
    567}
    568
    569static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
    570{
    571	struct kfd_iolink_properties *iolink;
    572	struct kfd_cache_properties *cache;
    573	struct kfd_mem_properties *mem;
    574	struct kfd_perf_properties *perf;
    575
    576	if (dev->kobj_iolink) {
    577		list_for_each_entry(iolink, &dev->io_link_props, list)
    578			if (iolink->kobj) {
    579				kfd_remove_sysfs_file(iolink->kobj,
    580							&iolink->attr);
    581				iolink->kobj = NULL;
    582			}
    583		kobject_del(dev->kobj_iolink);
    584		kobject_put(dev->kobj_iolink);
    585		dev->kobj_iolink = NULL;
    586	}
    587
    588	if (dev->kobj_cache) {
    589		list_for_each_entry(cache, &dev->cache_props, list)
    590			if (cache->kobj) {
    591				kfd_remove_sysfs_file(cache->kobj,
    592							&cache->attr);
    593				cache->kobj = NULL;
    594			}
    595		kobject_del(dev->kobj_cache);
    596		kobject_put(dev->kobj_cache);
    597		dev->kobj_cache = NULL;
    598	}
    599
    600	if (dev->kobj_mem) {
    601		list_for_each_entry(mem, &dev->mem_props, list)
    602			if (mem->kobj) {
    603				kfd_remove_sysfs_file(mem->kobj, &mem->attr);
    604				mem->kobj = NULL;
    605			}
    606		kobject_del(dev->kobj_mem);
    607		kobject_put(dev->kobj_mem);
    608		dev->kobj_mem = NULL;
    609	}
    610
    611	if (dev->kobj_perf) {
    612		list_for_each_entry(perf, &dev->perf_props, list) {
    613			kfree(perf->attr_group);
    614			perf->attr_group = NULL;
    615		}
    616		kobject_del(dev->kobj_perf);
    617		kobject_put(dev->kobj_perf);
    618		dev->kobj_perf = NULL;
    619	}
    620
    621	if (dev->kobj_node) {
    622		sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
    623		sysfs_remove_file(dev->kobj_node, &dev->attr_name);
    624		sysfs_remove_file(dev->kobj_node, &dev->attr_props);
    625		kobject_del(dev->kobj_node);
    626		kobject_put(dev->kobj_node);
    627		dev->kobj_node = NULL;
    628	}
    629}
    630
    631static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
    632		uint32_t id)
    633{
    634	struct kfd_iolink_properties *iolink;
    635	struct kfd_cache_properties *cache;
    636	struct kfd_mem_properties *mem;
    637	struct kfd_perf_properties *perf;
    638	int ret;
    639	uint32_t i, num_attrs;
    640	struct attribute **attrs;
    641
    642	if (WARN_ON(dev->kobj_node))
    643		return -EEXIST;
    644
    645	/*
    646	 * Creating the sysfs folders
    647	 */
    648	dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
    649	if (!dev->kobj_node)
    650		return -ENOMEM;
    651
    652	ret = kobject_init_and_add(dev->kobj_node, &node_type,
    653			sys_props.kobj_nodes, "%d", id);
    654	if (ret < 0) {
    655		kobject_put(dev->kobj_node);
    656		return ret;
    657	}
    658
    659	dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
    660	if (!dev->kobj_mem)
    661		return -ENOMEM;
    662
    663	dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
    664	if (!dev->kobj_cache)
    665		return -ENOMEM;
    666
    667	dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
    668	if (!dev->kobj_iolink)
    669		return -ENOMEM;
    670
    671	dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
    672	if (!dev->kobj_perf)
    673		return -ENOMEM;
    674
    675	/*
    676	 * Creating sysfs files for node properties
    677	 */
    678	dev->attr_gpuid.name = "gpu_id";
    679	dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
    680	sysfs_attr_init(&dev->attr_gpuid);
    681	dev->attr_name.name = "name";
    682	dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
    683	sysfs_attr_init(&dev->attr_name);
    684	dev->attr_props.name = "properties";
    685	dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
    686	sysfs_attr_init(&dev->attr_props);
    687	ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
    688	if (ret < 0)
    689		return ret;
    690	ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
    691	if (ret < 0)
    692		return ret;
    693	ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
    694	if (ret < 0)
    695		return ret;
    696
    697	i = 0;
    698	list_for_each_entry(mem, &dev->mem_props, list) {
    699		mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    700		if (!mem->kobj)
    701			return -ENOMEM;
    702		ret = kobject_init_and_add(mem->kobj, &mem_type,
    703				dev->kobj_mem, "%d", i);
    704		if (ret < 0) {
    705			kobject_put(mem->kobj);
    706			return ret;
    707		}
    708
    709		mem->attr.name = "properties";
    710		mem->attr.mode = KFD_SYSFS_FILE_MODE;
    711		sysfs_attr_init(&mem->attr);
    712		ret = sysfs_create_file(mem->kobj, &mem->attr);
    713		if (ret < 0)
    714			return ret;
    715		i++;
    716	}
    717
    718	i = 0;
    719	list_for_each_entry(cache, &dev->cache_props, list) {
    720		cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    721		if (!cache->kobj)
    722			return -ENOMEM;
    723		ret = kobject_init_and_add(cache->kobj, &cache_type,
    724				dev->kobj_cache, "%d", i);
    725		if (ret < 0) {
    726			kobject_put(cache->kobj);
    727			return ret;
    728		}
    729
    730		cache->attr.name = "properties";
    731		cache->attr.mode = KFD_SYSFS_FILE_MODE;
    732		sysfs_attr_init(&cache->attr);
    733		ret = sysfs_create_file(cache->kobj, &cache->attr);
    734		if (ret < 0)
    735			return ret;
    736		i++;
    737	}
    738
    739	i = 0;
    740	list_for_each_entry(iolink, &dev->io_link_props, list) {
    741		iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    742		if (!iolink->kobj)
    743			return -ENOMEM;
    744		ret = kobject_init_and_add(iolink->kobj, &iolink_type,
    745				dev->kobj_iolink, "%d", i);
    746		if (ret < 0) {
    747			kobject_put(iolink->kobj);
    748			return ret;
    749		}
    750
    751		iolink->attr.name = "properties";
    752		iolink->attr.mode = KFD_SYSFS_FILE_MODE;
    753		sysfs_attr_init(&iolink->attr);
    754		ret = sysfs_create_file(iolink->kobj, &iolink->attr);
    755		if (ret < 0)
    756			return ret;
    757		i++;
    758	}
    759
    760	/* All hardware blocks have the same number of attributes. */
    761	num_attrs = ARRAY_SIZE(perf_attr_iommu);
    762	list_for_each_entry(perf, &dev->perf_props, list) {
    763		perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
    764			* num_attrs + sizeof(struct attribute_group),
    765			GFP_KERNEL);
    766		if (!perf->attr_group)
    767			return -ENOMEM;
    768
    769		attrs = (struct attribute **)(perf->attr_group + 1);
    770		if (!strcmp(perf->block_name, "iommu")) {
    771		/* Information of IOMMU's num_counters and counter_ids is shown
    772		 * under /sys/bus/event_source/devices/amd_iommu. We don't
    773		 * duplicate here.
    774		 */
    775			perf_attr_iommu[0].data = perf->max_concurrent;
    776			for (i = 0; i < num_attrs; i++)
    777				attrs[i] = &perf_attr_iommu[i].attr.attr;
    778		}
    779		perf->attr_group->name = perf->block_name;
    780		perf->attr_group->attrs = attrs;
    781		ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
    782		if (ret < 0)
    783			return ret;
    784	}
    785
    786	return 0;
    787}
    788
    789/* Called with write topology lock acquired */
    790static int kfd_build_sysfs_node_tree(void)
    791{
    792	struct kfd_topology_device *dev;
    793	int ret;
    794	uint32_t i = 0;
    795
    796	list_for_each_entry(dev, &topology_device_list, list) {
    797		ret = kfd_build_sysfs_node_entry(dev, i);
    798		if (ret < 0)
    799			return ret;
    800		i++;
    801	}
    802
    803	return 0;
    804}
    805
    806/* Called with write topology lock acquired */
    807static void kfd_remove_sysfs_node_tree(void)
    808{
    809	struct kfd_topology_device *dev;
    810
    811	list_for_each_entry(dev, &topology_device_list, list)
    812		kfd_remove_sysfs_node_entry(dev);
    813}
    814
    815static int kfd_topology_update_sysfs(void)
    816{
    817	int ret;
    818
    819	if (!sys_props.kobj_topology) {
    820		sys_props.kobj_topology =
    821				kfd_alloc_struct(sys_props.kobj_topology);
    822		if (!sys_props.kobj_topology)
    823			return -ENOMEM;
    824
    825		ret = kobject_init_and_add(sys_props.kobj_topology,
    826				&sysprops_type,  &kfd_device->kobj,
    827				"topology");
    828		if (ret < 0) {
    829			kobject_put(sys_props.kobj_topology);
    830			return ret;
    831		}
    832
    833		sys_props.kobj_nodes = kobject_create_and_add("nodes",
    834				sys_props.kobj_topology);
    835		if (!sys_props.kobj_nodes)
    836			return -ENOMEM;
    837
    838		sys_props.attr_genid.name = "generation_id";
    839		sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
    840		sysfs_attr_init(&sys_props.attr_genid);
    841		ret = sysfs_create_file(sys_props.kobj_topology,
    842				&sys_props.attr_genid);
    843		if (ret < 0)
    844			return ret;
    845
    846		sys_props.attr_props.name = "system_properties";
    847		sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
    848		sysfs_attr_init(&sys_props.attr_props);
    849		ret = sysfs_create_file(sys_props.kobj_topology,
    850				&sys_props.attr_props);
    851		if (ret < 0)
    852			return ret;
    853	}
    854
    855	kfd_remove_sysfs_node_tree();
    856
    857	return kfd_build_sysfs_node_tree();
    858}
    859
    860static void kfd_topology_release_sysfs(void)
    861{
    862	kfd_remove_sysfs_node_tree();
    863	if (sys_props.kobj_topology) {
    864		sysfs_remove_file(sys_props.kobj_topology,
    865				&sys_props.attr_genid);
    866		sysfs_remove_file(sys_props.kobj_topology,
    867				&sys_props.attr_props);
    868		if (sys_props.kobj_nodes) {
    869			kobject_del(sys_props.kobj_nodes);
    870			kobject_put(sys_props.kobj_nodes);
    871			sys_props.kobj_nodes = NULL;
    872		}
    873		kobject_del(sys_props.kobj_topology);
    874		kobject_put(sys_props.kobj_topology);
    875		sys_props.kobj_topology = NULL;
    876	}
    877}
    878
    879/* Called with write topology_lock acquired */
    880static void kfd_topology_update_device_list(struct list_head *temp_list,
    881					struct list_head *master_list)
    882{
    883	while (!list_empty(temp_list)) {
    884		list_move_tail(temp_list->next, master_list);
    885		sys_props.num_devices++;
    886	}
    887}
    888
    889static void kfd_debug_print_topology(void)
    890{
    891	struct kfd_topology_device *dev;
    892
    893	down_read(&topology_lock);
    894
    895	dev = list_last_entry(&topology_device_list,
    896			struct kfd_topology_device, list);
    897	if (dev) {
    898		if (dev->node_props.cpu_cores_count &&
    899				dev->node_props.simd_count) {
    900			pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
    901				dev->node_props.device_id,
    902				dev->node_props.vendor_id);
    903		} else if (dev->node_props.cpu_cores_count)
    904			pr_info("Topology: Add CPU node\n");
    905		else if (dev->node_props.simd_count)
    906			pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
    907				dev->node_props.device_id,
    908				dev->node_props.vendor_id);
    909	}
    910	up_read(&topology_lock);
    911}
    912
    913/* Helper function for intializing platform_xx members of
    914 * kfd_system_properties. Uses OEM info from the last CPU/APU node.
    915 */
    916static void kfd_update_system_properties(void)
    917{
    918	struct kfd_topology_device *dev;
    919
    920	down_read(&topology_lock);
    921	dev = list_last_entry(&topology_device_list,
    922			struct kfd_topology_device, list);
    923	if (dev) {
    924		sys_props.platform_id =
    925			(*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
    926		sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
    927		sys_props.platform_rev = dev->oem_revision;
    928	}
    929	up_read(&topology_lock);
    930}
    931
    932static void find_system_memory(const struct dmi_header *dm,
    933	void *private)
    934{
    935	struct kfd_mem_properties *mem;
    936	u16 mem_width, mem_clock;
    937	struct kfd_topology_device *kdev =
    938		(struct kfd_topology_device *)private;
    939	const u8 *dmi_data = (const u8 *)(dm + 1);
    940
    941	if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
    942		mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
    943		mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
    944		list_for_each_entry(mem, &kdev->mem_props, list) {
    945			if (mem_width != 0xFFFF && mem_width != 0)
    946				mem->width = mem_width;
    947			if (mem_clock != 0)
    948				mem->mem_clk_max = mem_clock;
    949		}
    950	}
    951}
    952
    953/*
    954 * Performance counters information is not part of CRAT but we would like to
    955 * put them in the sysfs under topology directory for Thunk to get the data.
    956 * This function is called before updating the sysfs.
    957 */
    958static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
    959{
    960	/* These are the only counters supported so far */
    961	return kfd_iommu_add_perf_counters(kdev);
    962}
    963
    964/* kfd_add_non_crat_information - Add information that is not currently
    965 *	defined in CRAT but is necessary for KFD topology
    966 * @dev - topology device to which addition info is added
    967 */
    968static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
    969{
    970	/* Check if CPU only node. */
    971	if (!kdev->gpu) {
    972		/* Add system memory information */
    973		dmi_walk(find_system_memory, kdev);
    974	}
    975	/* TODO: For GPU node, rearrange code from kfd_topology_add_device */
    976}
    977
    978/* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
    979 *	Ignore CRAT for all other devices. AMD APU is identified if both CPU
    980 *	and GPU cores are present.
    981 * @device_list - topology device list created by parsing ACPI CRAT table.
    982 * @return - TRUE if invalid, FALSE is valid.
    983 */
    984static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
    985{
    986	struct kfd_topology_device *dev;
    987
    988	list_for_each_entry(dev, device_list, list) {
    989		if (dev->node_props.cpu_cores_count &&
    990			dev->node_props.simd_count)
    991			return false;
    992	}
    993	pr_info("Ignoring ACPI CRAT on non-APU system\n");
    994	return true;
    995}
    996
    997int kfd_topology_init(void)
    998{
    999	void *crat_image = NULL;
   1000	size_t image_size = 0;
   1001	int ret;
   1002	struct list_head temp_topology_device_list;
   1003	int cpu_only_node = 0;
   1004	struct kfd_topology_device *kdev;
   1005	int proximity_domain;
   1006
   1007	/* topology_device_list - Master list of all topology devices
   1008	 * temp_topology_device_list - temporary list created while parsing CRAT
   1009	 * or VCRAT. Once parsing is complete the contents of list is moved to
   1010	 * topology_device_list
   1011	 */
   1012
   1013	/* Initialize the head for the both the lists */
   1014	INIT_LIST_HEAD(&topology_device_list);
   1015	INIT_LIST_HEAD(&temp_topology_device_list);
   1016	init_rwsem(&topology_lock);
   1017
   1018	memset(&sys_props, 0, sizeof(sys_props));
   1019
   1020	/* Proximity domains in ACPI CRAT tables start counting at
   1021	 * 0. The same should be true for virtual CRAT tables created
   1022	 * at this stage. GPUs added later in kfd_topology_add_device
   1023	 * use a counter.
   1024	 */
   1025	proximity_domain = 0;
   1026
   1027	/*
   1028	 * Get the CRAT image from the ACPI. If ACPI doesn't have one
   1029	 * or if ACPI CRAT is invalid create a virtual CRAT.
   1030	 * NOTE: The current implementation expects all AMD APUs to have
   1031	 *	CRAT. If no CRAT is available, it is assumed to be a CPU
   1032	 */
   1033	ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
   1034	if (!ret) {
   1035		ret = kfd_parse_crat_table(crat_image,
   1036					   &temp_topology_device_list,
   1037					   proximity_domain);
   1038		if (ret ||
   1039		    kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
   1040			kfd_release_topology_device_list(
   1041				&temp_topology_device_list);
   1042			kfd_destroy_crat_image(crat_image);
   1043			crat_image = NULL;
   1044		}
   1045	}
   1046
   1047	if (!crat_image) {
   1048		ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
   1049						    COMPUTE_UNIT_CPU, NULL,
   1050						    proximity_domain);
   1051		cpu_only_node = 1;
   1052		if (ret) {
   1053			pr_err("Error creating VCRAT table for CPU\n");
   1054			return ret;
   1055		}
   1056
   1057		ret = kfd_parse_crat_table(crat_image,
   1058					   &temp_topology_device_list,
   1059					   proximity_domain);
   1060		if (ret) {
   1061			pr_err("Error parsing VCRAT table for CPU\n");
   1062			goto err;
   1063		}
   1064	}
   1065
   1066	kdev = list_first_entry(&temp_topology_device_list,
   1067				struct kfd_topology_device, list);
   1068	kfd_add_perf_to_topology(kdev);
   1069
   1070	down_write(&topology_lock);
   1071	kfd_topology_update_device_list(&temp_topology_device_list,
   1072					&topology_device_list);
   1073	topology_crat_proximity_domain = sys_props.num_devices-1;
   1074	ret = kfd_topology_update_sysfs();
   1075	up_write(&topology_lock);
   1076
   1077	if (!ret) {
   1078		sys_props.generation_count++;
   1079		kfd_update_system_properties();
   1080		kfd_debug_print_topology();
   1081	} else
   1082		pr_err("Failed to update topology in sysfs ret=%d\n", ret);
   1083
   1084	/* For nodes with GPU, this information gets added
   1085	 * when GPU is detected (kfd_topology_add_device).
   1086	 */
   1087	if (cpu_only_node) {
   1088		/* Add additional information to CPU only node created above */
   1089		down_write(&topology_lock);
   1090		kdev = list_first_entry(&topology_device_list,
   1091				struct kfd_topology_device, list);
   1092		up_write(&topology_lock);
   1093		kfd_add_non_crat_information(kdev);
   1094	}
   1095
   1096err:
   1097	kfd_destroy_crat_image(crat_image);
   1098	return ret;
   1099}
   1100
   1101void kfd_topology_shutdown(void)
   1102{
   1103	down_write(&topology_lock);
   1104	kfd_topology_release_sysfs();
   1105	kfd_release_live_view();
   1106	up_write(&topology_lock);
   1107}
   1108
   1109static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
   1110{
   1111	uint32_t hashout;
   1112	uint32_t buf[7];
   1113	uint64_t local_mem_size;
   1114	int i;
   1115
   1116	if (!gpu)
   1117		return 0;
   1118
   1119	local_mem_size = gpu->local_mem_info.local_mem_size_private +
   1120			gpu->local_mem_info.local_mem_size_public;
   1121
   1122	buf[0] = gpu->pdev->devfn;
   1123	buf[1] = gpu->pdev->subsystem_vendor |
   1124		(gpu->pdev->subsystem_device << 16);
   1125	buf[2] = pci_domain_nr(gpu->pdev->bus);
   1126	buf[3] = gpu->pdev->device;
   1127	buf[4] = gpu->pdev->bus->number;
   1128	buf[5] = lower_32_bits(local_mem_size);
   1129	buf[6] = upper_32_bits(local_mem_size);
   1130
   1131	for (i = 0, hashout = 0; i < 7; i++)
   1132		hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
   1133
   1134	return hashout;
   1135}
   1136/* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
   1137 *		the GPU device is not already present in the topology device
   1138 *		list then return NULL. This means a new topology device has to
   1139 *		be created for this GPU.
   1140 */
   1141static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
   1142{
   1143	struct kfd_topology_device *dev;
   1144	struct kfd_topology_device *out_dev = NULL;
   1145	struct kfd_mem_properties *mem;
   1146	struct kfd_cache_properties *cache;
   1147	struct kfd_iolink_properties *iolink;
   1148
   1149	down_write(&topology_lock);
   1150	list_for_each_entry(dev, &topology_device_list, list) {
   1151		/* Discrete GPUs need their own topology device list
   1152		 * entries. Don't assign them to CPU/APU nodes.
   1153		 */
   1154		if (!gpu->use_iommu_v2 &&
   1155		    dev->node_props.cpu_cores_count)
   1156			continue;
   1157
   1158		if (!dev->gpu && (dev->node_props.simd_count > 0)) {
   1159			dev->gpu = gpu;
   1160			out_dev = dev;
   1161
   1162			list_for_each_entry(mem, &dev->mem_props, list)
   1163				mem->gpu = dev->gpu;
   1164			list_for_each_entry(cache, &dev->cache_props, list)
   1165				cache->gpu = dev->gpu;
   1166			list_for_each_entry(iolink, &dev->io_link_props, list)
   1167				iolink->gpu = dev->gpu;
   1168			break;
   1169		}
   1170	}
   1171	up_write(&topology_lock);
   1172	return out_dev;
   1173}
   1174
   1175static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
   1176{
   1177	/*
   1178	 * TODO: Generate an event for thunk about the arrival/removal
   1179	 * of the GPU
   1180	 */
   1181}
   1182
   1183/* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
   1184 *		patch this after CRAT parsing.
   1185 */
   1186static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
   1187{
   1188	struct kfd_mem_properties *mem;
   1189	struct kfd_local_mem_info local_mem_info;
   1190
   1191	if (!dev)
   1192		return;
   1193
   1194	/* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
   1195	 * single bank of VRAM local memory.
   1196	 * for dGPUs - VCRAT reports only one bank of Local Memory
   1197	 * for APUs - If CRAT from ACPI reports more than one bank, then
   1198	 *	all the banks will report the same mem_clk_max information
   1199	 */
   1200	amdgpu_amdkfd_get_local_mem_info(dev->gpu->adev, &local_mem_info);
   1201
   1202	list_for_each_entry(mem, &dev->mem_props, list)
   1203		mem->mem_clk_max = local_mem_info.mem_clk_max;
   1204}
   1205
   1206static void kfd_set_iolink_no_atomics(struct kfd_topology_device *dev,
   1207					struct kfd_topology_device *target_gpu_dev,
   1208					struct kfd_iolink_properties *link)
   1209{
   1210	/* xgmi always supports atomics between links. */
   1211	if (link->iolink_type == CRAT_IOLINK_TYPE_XGMI)
   1212		return;
   1213
   1214	/* check pcie support to set cpu(dev) flags for target_gpu_dev link. */
   1215	if (target_gpu_dev) {
   1216		uint32_t cap;
   1217
   1218		pcie_capability_read_dword(target_gpu_dev->gpu->pdev,
   1219				PCI_EXP_DEVCAP2, &cap);
   1220
   1221		if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
   1222			     PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
   1223			link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
   1224				CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
   1225	/* set gpu (dev) flags. */
   1226	} else {
   1227		if (!dev->gpu->pci_atomic_requested ||
   1228				dev->gpu->adev->asic_type == CHIP_HAWAII)
   1229			link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
   1230				CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
   1231	}
   1232}
   1233
   1234static void kfd_set_iolink_non_coherent(struct kfd_topology_device *to_dev,
   1235		struct kfd_iolink_properties *outbound_link,
   1236		struct kfd_iolink_properties *inbound_link)
   1237{
   1238	/* CPU -> GPU with PCIe */
   1239	if (!to_dev->gpu &&
   1240	    inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
   1241		inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
   1242
   1243	if (to_dev->gpu) {
   1244		/* GPU <-> GPU with PCIe and
   1245		 * Vega20 with XGMI
   1246		 */
   1247		if (inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS ||
   1248		    (inbound_link->iolink_type == CRAT_IOLINK_TYPE_XGMI &&
   1249		    KFD_GC_VERSION(to_dev->gpu) == IP_VERSION(9, 4, 0))) {
   1250			outbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
   1251			inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
   1252		}
   1253	}
   1254}
   1255
   1256static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
   1257{
   1258	struct kfd_iolink_properties *link, *inbound_link;
   1259	struct kfd_topology_device *peer_dev;
   1260
   1261	if (!dev || !dev->gpu)
   1262		return;
   1263
   1264	/* GPU only creates direct links so apply flags setting to all */
   1265	list_for_each_entry(link, &dev->io_link_props, list) {
   1266		link->flags = CRAT_IOLINK_FLAGS_ENABLED;
   1267		kfd_set_iolink_no_atomics(dev, NULL, link);
   1268		peer_dev = kfd_topology_device_by_proximity_domain(
   1269				link->node_to);
   1270
   1271		if (!peer_dev)
   1272			continue;
   1273
   1274		/* Include the CPU peer in GPU hive if connected over xGMI. */
   1275		if (!peer_dev->gpu && !peer_dev->node_props.hive_id &&
   1276				dev->node_props.hive_id &&
   1277				dev->gpu->adev->gmc.xgmi.connected_to_cpu)
   1278			peer_dev->node_props.hive_id = dev->node_props.hive_id;
   1279
   1280		list_for_each_entry(inbound_link, &peer_dev->io_link_props,
   1281									list) {
   1282			if (inbound_link->node_to != link->node_from)
   1283				continue;
   1284
   1285			inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
   1286			kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
   1287			kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
   1288		}
   1289	}
   1290}
   1291
   1292int kfd_topology_add_device(struct kfd_dev *gpu)
   1293{
   1294	uint32_t gpu_id;
   1295	struct kfd_topology_device *dev;
   1296	struct kfd_cu_info cu_info;
   1297	int res = 0;
   1298	struct list_head temp_topology_device_list;
   1299	void *crat_image = NULL;
   1300	size_t image_size = 0;
   1301	int proximity_domain;
   1302	int i;
   1303	const char *asic_name = amdgpu_asic_name[gpu->adev->asic_type];
   1304
   1305	INIT_LIST_HEAD(&temp_topology_device_list);
   1306
   1307	gpu_id = kfd_generate_gpu_id(gpu);
   1308
   1309	pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
   1310
   1311	/* Check to see if this gpu device exists in the topology_device_list.
   1312	 * If so, assign the gpu to that device,
   1313	 * else create a Virtual CRAT for this gpu device and then parse that
   1314	 * CRAT to create a new topology device. Once created assign the gpu to
   1315	 * that topology device
   1316	 */
   1317	dev = kfd_assign_gpu(gpu);
   1318	if (!dev) {
   1319		down_write(&topology_lock);
   1320		proximity_domain = ++topology_crat_proximity_domain;
   1321
   1322		res = kfd_create_crat_image_virtual(&crat_image, &image_size,
   1323						    COMPUTE_UNIT_GPU, gpu,
   1324						    proximity_domain);
   1325		if (res) {
   1326			pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
   1327			       gpu_id);
   1328			topology_crat_proximity_domain--;
   1329			return res;
   1330		}
   1331		res = kfd_parse_crat_table(crat_image,
   1332					   &temp_topology_device_list,
   1333					   proximity_domain);
   1334		if (res) {
   1335			pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
   1336			       gpu_id);
   1337			topology_crat_proximity_domain--;
   1338			goto err;
   1339		}
   1340
   1341		kfd_topology_update_device_list(&temp_topology_device_list,
   1342			&topology_device_list);
   1343
   1344		/* Update the SYSFS tree, since we added another topology
   1345		 * device
   1346		 */
   1347		res = kfd_topology_update_sysfs();
   1348		up_write(&topology_lock);
   1349
   1350		if (!res)
   1351			sys_props.generation_count++;
   1352		else
   1353			pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
   1354						gpu_id, res);
   1355		dev = kfd_assign_gpu(gpu);
   1356		if (WARN_ON(!dev)) {
   1357			res = -ENODEV;
   1358			goto err;
   1359		}
   1360	}
   1361
   1362	dev->gpu_id = gpu_id;
   1363	gpu->id = gpu_id;
   1364
   1365	/* TODO: Move the following lines to function
   1366	 *	kfd_add_non_crat_information
   1367	 */
   1368
   1369	/* Fill-in additional information that is not available in CRAT but
   1370	 * needed for the topology
   1371	 */
   1372
   1373	amdgpu_amdkfd_get_cu_info(dev->gpu->adev, &cu_info);
   1374
   1375	for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1; i++) {
   1376		dev->node_props.name[i] = __tolower(asic_name[i]);
   1377		if (asic_name[i] == '\0')
   1378			break;
   1379	}
   1380	dev->node_props.name[i] = '\0';
   1381
   1382	dev->node_props.simd_arrays_per_engine =
   1383		cu_info.num_shader_arrays_per_engine;
   1384
   1385	dev->node_props.gfx_target_version = gpu->device_info.gfx_target_version;
   1386	dev->node_props.vendor_id = gpu->pdev->vendor;
   1387	dev->node_props.device_id = gpu->pdev->device;
   1388	dev->node_props.capability |=
   1389		((dev->gpu->adev->rev_id << HSA_CAP_ASIC_REVISION_SHIFT) &
   1390			HSA_CAP_ASIC_REVISION_MASK);
   1391	dev->node_props.location_id = pci_dev_id(gpu->pdev);
   1392	dev->node_props.domain = pci_domain_nr(gpu->pdev->bus);
   1393	dev->node_props.max_engine_clk_fcompute =
   1394		amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->adev);
   1395	dev->node_props.max_engine_clk_ccompute =
   1396		cpufreq_quick_get_max(0) / 1000;
   1397	dev->node_props.drm_render_minor =
   1398		gpu->shared_resources.drm_render_minor;
   1399
   1400	dev->node_props.hive_id = gpu->hive_id;
   1401	dev->node_props.num_sdma_engines = kfd_get_num_sdma_engines(gpu);
   1402	dev->node_props.num_sdma_xgmi_engines =
   1403					kfd_get_num_xgmi_sdma_engines(gpu);
   1404	dev->node_props.num_sdma_queues_per_engine =
   1405				gpu->device_info.num_sdma_queues_per_engine -
   1406				gpu->device_info.num_reserved_sdma_queues_per_engine;
   1407	dev->node_props.num_gws = (dev->gpu->gws &&
   1408		dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
   1409		dev->gpu->adev->gds.gws_size : 0;
   1410	dev->node_props.num_cp_queues = get_cp_queues_num(dev->gpu->dqm);
   1411
   1412	kfd_fill_mem_clk_max_info(dev);
   1413	kfd_fill_iolink_non_crat_info(dev);
   1414
   1415	switch (dev->gpu->adev->asic_type) {
   1416	case CHIP_KAVERI:
   1417	case CHIP_HAWAII:
   1418	case CHIP_TONGA:
   1419		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
   1420			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
   1421			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
   1422		break;
   1423	case CHIP_CARRIZO:
   1424	case CHIP_FIJI:
   1425	case CHIP_POLARIS10:
   1426	case CHIP_POLARIS11:
   1427	case CHIP_POLARIS12:
   1428	case CHIP_VEGAM:
   1429		pr_debug("Adding doorbell packet type capability\n");
   1430		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
   1431			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
   1432			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
   1433		break;
   1434	default:
   1435		if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(9, 0, 1))
   1436			dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
   1437				HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
   1438				HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
   1439		else
   1440			WARN(1, "Unexpected ASIC family %u",
   1441			     dev->gpu->adev->asic_type);
   1442	}
   1443
   1444	/*
   1445	 * Overwrite ATS capability according to needs_iommu_device to fix
   1446	 * potential missing corresponding bit in CRAT of BIOS.
   1447	 */
   1448	if (dev->gpu->use_iommu_v2)
   1449		dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
   1450	else
   1451		dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;
   1452
   1453	/* Fix errors in CZ CRAT.
   1454	 * simd_count: Carrizo CRAT reports wrong simd_count, probably
   1455	 *		because it doesn't consider masked out CUs
   1456	 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
   1457	 */
   1458	if (dev->gpu->adev->asic_type == CHIP_CARRIZO) {
   1459		dev->node_props.simd_count =
   1460			cu_info.simd_per_cu * cu_info.cu_active_number;
   1461		dev->node_props.max_waves_per_simd = 10;
   1462	}
   1463
   1464	/* kfd only concerns sram ecc on GFX and HBM ecc on UMC */
   1465	dev->node_props.capability |=
   1466		((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0) ?
   1467		HSA_CAP_SRAM_EDCSUPPORTED : 0;
   1468	dev->node_props.capability |=
   1469		((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
   1470		HSA_CAP_MEM_EDCSUPPORTED : 0;
   1471
   1472	if (KFD_GC_VERSION(dev->gpu) != IP_VERSION(9, 0, 1))
   1473		dev->node_props.capability |= (dev->gpu->adev->ras_enabled != 0) ?
   1474			HSA_CAP_RASEVENTNOTIFY : 0;
   1475
   1476	if (KFD_IS_SVM_API_SUPPORTED(dev->gpu->adev->kfd.dev))
   1477		dev->node_props.capability |= HSA_CAP_SVMAPI_SUPPORTED;
   1478
   1479	kfd_debug_print_topology();
   1480
   1481	if (!res)
   1482		kfd_notify_gpu_change(gpu_id, 1);
   1483err:
   1484	kfd_destroy_crat_image(crat_image);
   1485	return res;
   1486}
   1487
   1488/**
   1489 * kfd_topology_update_io_links() - Update IO links after device removal.
   1490 * @proximity_domain: Proximity domain value of the dev being removed.
   1491 *
   1492 * The topology list currently is arranged in increasing order of
   1493 * proximity domain.
   1494 *
   1495 * Two things need to be done when a device is removed:
   1496 * 1. All the IO links to this device need to be removed.
   1497 * 2. All nodes after the current device node need to move
   1498 *    up once this device node is removed from the topology
   1499 *    list. As a result, the proximity domain values for
   1500 *    all nodes after the node being deleted reduce by 1.
   1501 *    This would also cause the proximity domain values for
   1502 *    io links to be updated based on new proximity domain
   1503 *    values.
   1504 *
   1505 * Context: The caller must hold write topology_lock.
   1506 */
   1507static void kfd_topology_update_io_links(int proximity_domain)
   1508{
   1509	struct kfd_topology_device *dev;
   1510	struct kfd_iolink_properties *iolink, *tmp;
   1511
   1512	list_for_each_entry(dev, &topology_device_list, list) {
   1513		if (dev->proximity_domain > proximity_domain)
   1514			dev->proximity_domain--;
   1515
   1516		list_for_each_entry_safe(iolink, tmp, &dev->io_link_props, list) {
   1517			/*
   1518			 * If there is an io link to the dev being deleted
   1519			 * then remove that IO link also.
   1520			 */
   1521			if (iolink->node_to == proximity_domain) {
   1522				list_del(&iolink->list);
   1523				dev->io_link_count--;
   1524				dev->node_props.io_links_count--;
   1525			} else {
   1526				if (iolink->node_from > proximity_domain)
   1527					iolink->node_from--;
   1528				if (iolink->node_to > proximity_domain)
   1529					iolink->node_to--;
   1530			}
   1531		}
   1532	}
   1533}
   1534
   1535int kfd_topology_remove_device(struct kfd_dev *gpu)
   1536{
   1537	struct kfd_topology_device *dev, *tmp;
   1538	uint32_t gpu_id;
   1539	int res = -ENODEV;
   1540	int i = 0;
   1541
   1542	down_write(&topology_lock);
   1543
   1544	list_for_each_entry_safe(dev, tmp, &topology_device_list, list) {
   1545		if (dev->gpu == gpu) {
   1546			gpu_id = dev->gpu_id;
   1547			kfd_remove_sysfs_node_entry(dev);
   1548			kfd_release_topology_device(dev);
   1549			sys_props.num_devices--;
   1550			kfd_topology_update_io_links(i);
   1551			topology_crat_proximity_domain = sys_props.num_devices-1;
   1552			sys_props.generation_count++;
   1553			res = 0;
   1554			if (kfd_topology_update_sysfs() < 0)
   1555				kfd_topology_release_sysfs();
   1556			break;
   1557		}
   1558		i++;
   1559	}
   1560
   1561	up_write(&topology_lock);
   1562
   1563	if (!res)
   1564		kfd_notify_gpu_change(gpu_id, 0);
   1565
   1566	return res;
   1567}
   1568
   1569/* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
   1570 *	topology. If GPU device is found @idx, then valid kfd_dev pointer is
   1571 *	returned through @kdev
   1572 * Return -	0: On success (@kdev will be NULL for non GPU nodes)
   1573 *		-1: If end of list
   1574 */
   1575int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
   1576{
   1577
   1578	struct kfd_topology_device *top_dev;
   1579	uint8_t device_idx = 0;
   1580
   1581	*kdev = NULL;
   1582	down_read(&topology_lock);
   1583
   1584	list_for_each_entry(top_dev, &topology_device_list, list) {
   1585		if (device_idx == idx) {
   1586			*kdev = top_dev->gpu;
   1587			up_read(&topology_lock);
   1588			return 0;
   1589		}
   1590
   1591		device_idx++;
   1592	}
   1593
   1594	up_read(&topology_lock);
   1595
   1596	return -1;
   1597
   1598}
   1599
   1600static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
   1601{
   1602	int first_cpu_of_numa_node;
   1603
   1604	if (!cpumask || cpumask == cpu_none_mask)
   1605		return -1;
   1606	first_cpu_of_numa_node = cpumask_first(cpumask);
   1607	if (first_cpu_of_numa_node >= nr_cpu_ids)
   1608		return -1;
   1609#ifdef CONFIG_X86_64
   1610	return cpu_data(first_cpu_of_numa_node).apicid;
   1611#else
   1612	return first_cpu_of_numa_node;
   1613#endif
   1614}
   1615
   1616/* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
   1617 *	of the given NUMA node (numa_node_id)
   1618 * Return -1 on failure
   1619 */
   1620int kfd_numa_node_to_apic_id(int numa_node_id)
   1621{
   1622	if (numa_node_id == -1) {
   1623		pr_warn("Invalid NUMA Node. Use online CPU mask\n");
   1624		return kfd_cpumask_to_apic_id(cpu_online_mask);
   1625	}
   1626	return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
   1627}
   1628
   1629void kfd_double_confirm_iommu_support(struct kfd_dev *gpu)
   1630{
   1631	struct kfd_topology_device *dev;
   1632
   1633	gpu->use_iommu_v2 = false;
   1634
   1635	if (!gpu->device_info.needs_iommu_device)
   1636		return;
   1637
   1638	down_read(&topology_lock);
   1639
   1640	/* Only use IOMMUv2 if there is an APU topology node with no GPU
   1641	 * assigned yet. This GPU will be assigned to it.
   1642	 */
   1643	list_for_each_entry(dev, &topology_device_list, list)
   1644		if (dev->node_props.cpu_cores_count &&
   1645		    dev->node_props.simd_count &&
   1646		    !dev->gpu)
   1647			gpu->use_iommu_v2 = true;
   1648
   1649	up_read(&topology_lock);
   1650}
   1651
   1652#if defined(CONFIG_DEBUG_FS)
   1653
   1654int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
   1655{
   1656	struct kfd_topology_device *dev;
   1657	unsigned int i = 0;
   1658	int r = 0;
   1659
   1660	down_read(&topology_lock);
   1661
   1662	list_for_each_entry(dev, &topology_device_list, list) {
   1663		if (!dev->gpu) {
   1664			i++;
   1665			continue;
   1666		}
   1667
   1668		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
   1669		r = dqm_debugfs_hqds(m, dev->gpu->dqm);
   1670		if (r)
   1671			break;
   1672	}
   1673
   1674	up_read(&topology_lock);
   1675
   1676	return r;
   1677}
   1678
   1679int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
   1680{
   1681	struct kfd_topology_device *dev;
   1682	unsigned int i = 0;
   1683	int r = 0;
   1684
   1685	down_read(&topology_lock);
   1686
   1687	list_for_each_entry(dev, &topology_device_list, list) {
   1688		if (!dev->gpu) {
   1689			i++;
   1690			continue;
   1691		}
   1692
   1693		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
   1694		r = pm_debugfs_runlist(m, &dev->gpu->dqm->packet_mgr);
   1695		if (r)
   1696			break;
   1697	}
   1698
   1699	up_read(&topology_lock);
   1700
   1701	return r;
   1702}
   1703
   1704#endif