15b5c4e40SEvgeny Pinchuk /*
25b5c4e40SEvgeny Pinchuk  * Copyright 2014 Advanced Micro Devices, Inc.
35b5c4e40SEvgeny Pinchuk  *
45b5c4e40SEvgeny Pinchuk  * Permission is hereby granted, free of charge, to any person obtaining a
55b5c4e40SEvgeny Pinchuk  * copy of this software and associated documentation files (the "Software"),
65b5c4e40SEvgeny Pinchuk  * to deal in the Software without restriction, including without limitation
75b5c4e40SEvgeny Pinchuk  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
85b5c4e40SEvgeny Pinchuk  * and/or sell copies of the Software, and to permit persons to whom the
95b5c4e40SEvgeny Pinchuk  * Software is furnished to do so, subject to the following conditions:
105b5c4e40SEvgeny Pinchuk  *
115b5c4e40SEvgeny Pinchuk  * The above copyright notice and this permission notice shall be included in
125b5c4e40SEvgeny Pinchuk  * all copies or substantial portions of the Software.
135b5c4e40SEvgeny Pinchuk  *
145b5c4e40SEvgeny Pinchuk  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
155b5c4e40SEvgeny Pinchuk  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
165b5c4e40SEvgeny Pinchuk  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
175b5c4e40SEvgeny Pinchuk  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
185b5c4e40SEvgeny Pinchuk  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
195b5c4e40SEvgeny Pinchuk  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
205b5c4e40SEvgeny Pinchuk  * OTHER DEALINGS IN THE SOFTWARE.
215b5c4e40SEvgeny Pinchuk  */
225b5c4e40SEvgeny Pinchuk 
235b5c4e40SEvgeny Pinchuk #include <linux/types.h>
245b5c4e40SEvgeny Pinchuk #include <linux/kernel.h>
255b5c4e40SEvgeny Pinchuk #include <linux/pci.h>
265b5c4e40SEvgeny Pinchuk #include <linux/errno.h>
275b5c4e40SEvgeny Pinchuk #include <linux/acpi.h>
285b5c4e40SEvgeny Pinchuk #include <linux/hash.h>
295b5c4e40SEvgeny Pinchuk #include <linux/cpufreq.h>
30f7c826adSAlexey Skidanov #include <linux/log2.h>
31520b8fb7SFelix Kuehling #include <linux/dmi.h>
32520b8fb7SFelix Kuehling #include <linux/atomic.h>
335b5c4e40SEvgeny Pinchuk 
345b5c4e40SEvgeny Pinchuk #include "kfd_priv.h"
355b5c4e40SEvgeny Pinchuk #include "kfd_crat.h"
365b5c4e40SEvgeny Pinchuk #include "kfd_topology.h"
37851a645eSFelix Kuehling #include "kfd_device_queue_manager.h"
3864d1c3a4SFelix Kuehling #include "kfd_iommu.h"
395b87245fSAmber Lin #include "amdgpu_amdkfd.h"
400dee45a2SEric Huang #include "amdgpu_ras.h"
415b5c4e40SEvgeny Pinchuk 
424f449311SHarish Kasiviswanathan /* topology_device_list - Master list of all topology devices */
434f449311SHarish Kasiviswanathan static struct list_head topology_device_list;
44520b8fb7SFelix Kuehling static struct kfd_system_properties sys_props;
455b5c4e40SEvgeny Pinchuk 
465b5c4e40SEvgeny Pinchuk static DECLARE_RWSEM(topology_lock);
47520b8fb7SFelix Kuehling static atomic_t topology_crat_proximity_domain;
485b5c4e40SEvgeny Pinchuk 
493a87177eSHarish Kasiviswanathan struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
503a87177eSHarish Kasiviswanathan 						uint32_t proximity_domain)
513a87177eSHarish Kasiviswanathan {
523a87177eSHarish Kasiviswanathan 	struct kfd_topology_device *top_dev;
533a87177eSHarish Kasiviswanathan 	struct kfd_topology_device *device = NULL;
543a87177eSHarish Kasiviswanathan 
553a87177eSHarish Kasiviswanathan 	down_read(&topology_lock);
563a87177eSHarish Kasiviswanathan 
573a87177eSHarish Kasiviswanathan 	list_for_each_entry(top_dev, &topology_device_list, list)
583a87177eSHarish Kasiviswanathan 		if (top_dev->proximity_domain == proximity_domain) {
593a87177eSHarish Kasiviswanathan 			device = top_dev;
603a87177eSHarish Kasiviswanathan 			break;
613a87177eSHarish Kasiviswanathan 		}
623a87177eSHarish Kasiviswanathan 
633a87177eSHarish Kasiviswanathan 	up_read(&topology_lock);
643a87177eSHarish Kasiviswanathan 
653a87177eSHarish Kasiviswanathan 	return device;
663a87177eSHarish Kasiviswanathan }
673a87177eSHarish Kasiviswanathan 
6844d8cc6fSYong Zhao struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
695b5c4e40SEvgeny Pinchuk {
7044d8cc6fSYong Zhao 	struct kfd_topology_device *top_dev = NULL;
7144d8cc6fSYong Zhao 	struct kfd_topology_device *ret = NULL;
725b5c4e40SEvgeny Pinchuk 
735b5c4e40SEvgeny Pinchuk 	down_read(&topology_lock);
745b5c4e40SEvgeny Pinchuk 
755b5c4e40SEvgeny Pinchuk 	list_for_each_entry(top_dev, &topology_device_list, list)
765b5c4e40SEvgeny Pinchuk 		if (top_dev->gpu_id == gpu_id) {
7744d8cc6fSYong Zhao 			ret = top_dev;
785b5c4e40SEvgeny Pinchuk 			break;
795b5c4e40SEvgeny Pinchuk 		}
805b5c4e40SEvgeny Pinchuk 
815b5c4e40SEvgeny Pinchuk 	up_read(&topology_lock);
825b5c4e40SEvgeny Pinchuk 
8344d8cc6fSYong Zhao 	return ret;
8444d8cc6fSYong Zhao }
8544d8cc6fSYong Zhao 
8644d8cc6fSYong Zhao struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
8744d8cc6fSYong Zhao {
8844d8cc6fSYong Zhao 	struct kfd_topology_device *top_dev;
8944d8cc6fSYong Zhao 
9044d8cc6fSYong Zhao 	top_dev = kfd_topology_device_by_id(gpu_id);
9144d8cc6fSYong Zhao 	if (!top_dev)
9244d8cc6fSYong Zhao 		return NULL;
9344d8cc6fSYong Zhao 
9444d8cc6fSYong Zhao 	return top_dev->gpu;
955b5c4e40SEvgeny Pinchuk }
965b5c4e40SEvgeny Pinchuk 
975b5c4e40SEvgeny Pinchuk struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
985b5c4e40SEvgeny Pinchuk {
995b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *top_dev;
1005b5c4e40SEvgeny Pinchuk 	struct kfd_dev *device = NULL;
1015b5c4e40SEvgeny Pinchuk 
1025b5c4e40SEvgeny Pinchuk 	down_read(&topology_lock);
1035b5c4e40SEvgeny Pinchuk 
1045b5c4e40SEvgeny Pinchuk 	list_for_each_entry(top_dev, &topology_device_list, list)
1053704d56eSFelix Kuehling 		if (top_dev->gpu && top_dev->gpu->pdev == pdev) {
1065b5c4e40SEvgeny Pinchuk 			device = top_dev->gpu;
1075b5c4e40SEvgeny Pinchuk 			break;
1085b5c4e40SEvgeny Pinchuk 		}
1095b5c4e40SEvgeny Pinchuk 
1105b5c4e40SEvgeny Pinchuk 	up_read(&topology_lock);
1115b5c4e40SEvgeny Pinchuk 
1125b5c4e40SEvgeny Pinchuk 	return device;
1135b5c4e40SEvgeny Pinchuk }
1145b5c4e40SEvgeny Pinchuk 
1151dde0ea9SFelix Kuehling struct kfd_dev *kfd_device_by_kgd(const struct kgd_dev *kgd)
1161dde0ea9SFelix Kuehling {
1171dde0ea9SFelix Kuehling 	struct kfd_topology_device *top_dev;
1181dde0ea9SFelix Kuehling 	struct kfd_dev *device = NULL;
1191dde0ea9SFelix Kuehling 
1201dde0ea9SFelix Kuehling 	down_read(&topology_lock);
1211dde0ea9SFelix Kuehling 
1221dde0ea9SFelix Kuehling 	list_for_each_entry(top_dev, &topology_device_list, list)
1231dde0ea9SFelix Kuehling 		if (top_dev->gpu && top_dev->gpu->kgd == kgd) {
1241dde0ea9SFelix Kuehling 			device = top_dev->gpu;
1251dde0ea9SFelix Kuehling 			break;
1261dde0ea9SFelix Kuehling 		}
1271dde0ea9SFelix Kuehling 
1281dde0ea9SFelix Kuehling 	up_read(&topology_lock);
1291dde0ea9SFelix Kuehling 
1301dde0ea9SFelix Kuehling 	return device;
1311dde0ea9SFelix Kuehling }
1321dde0ea9SFelix Kuehling 
1333a87177eSHarish Kasiviswanathan /* Called with write topology_lock acquired */
1345b5c4e40SEvgeny Pinchuk static void kfd_release_topology_device(struct kfd_topology_device *dev)
1355b5c4e40SEvgeny Pinchuk {
1365b5c4e40SEvgeny Pinchuk 	struct kfd_mem_properties *mem;
1375b5c4e40SEvgeny Pinchuk 	struct kfd_cache_properties *cache;
1385b5c4e40SEvgeny Pinchuk 	struct kfd_iolink_properties *iolink;
139f4757347SAmber Lin 	struct kfd_perf_properties *perf;
1405b5c4e40SEvgeny Pinchuk 
1415b5c4e40SEvgeny Pinchuk 	list_del(&dev->list);
1425b5c4e40SEvgeny Pinchuk 
1435b5c4e40SEvgeny Pinchuk 	while (dev->mem_props.next != &dev->mem_props) {
1445b5c4e40SEvgeny Pinchuk 		mem = container_of(dev->mem_props.next,
1455b5c4e40SEvgeny Pinchuk 				struct kfd_mem_properties, list);
1465b5c4e40SEvgeny Pinchuk 		list_del(&mem->list);
1475b5c4e40SEvgeny Pinchuk 		kfree(mem);
1485b5c4e40SEvgeny Pinchuk 	}
1495b5c4e40SEvgeny Pinchuk 
1505b5c4e40SEvgeny Pinchuk 	while (dev->cache_props.next != &dev->cache_props) {
1515b5c4e40SEvgeny Pinchuk 		cache = container_of(dev->cache_props.next,
1525b5c4e40SEvgeny Pinchuk 				struct kfd_cache_properties, list);
1535b5c4e40SEvgeny Pinchuk 		list_del(&cache->list);
1545b5c4e40SEvgeny Pinchuk 		kfree(cache);
1555b5c4e40SEvgeny Pinchuk 	}
1565b5c4e40SEvgeny Pinchuk 
1575b5c4e40SEvgeny Pinchuk 	while (dev->io_link_props.next != &dev->io_link_props) {
1585b5c4e40SEvgeny Pinchuk 		iolink = container_of(dev->io_link_props.next,
1595b5c4e40SEvgeny Pinchuk 				struct kfd_iolink_properties, list);
1605b5c4e40SEvgeny Pinchuk 		list_del(&iolink->list);
1615b5c4e40SEvgeny Pinchuk 		kfree(iolink);
1625b5c4e40SEvgeny Pinchuk 	}
1635b5c4e40SEvgeny Pinchuk 
164f4757347SAmber Lin 	while (dev->perf_props.next != &dev->perf_props) {
165f4757347SAmber Lin 		perf = container_of(dev->perf_props.next,
166f4757347SAmber Lin 				struct kfd_perf_properties, list);
167f4757347SAmber Lin 		list_del(&perf->list);
168f4757347SAmber Lin 		kfree(perf);
169f4757347SAmber Lin 	}
170f4757347SAmber Lin 
1715b5c4e40SEvgeny Pinchuk 	kfree(dev);
1725b5c4e40SEvgeny Pinchuk }
1735b5c4e40SEvgeny Pinchuk 
1744f449311SHarish Kasiviswanathan void kfd_release_topology_device_list(struct list_head *device_list)
1755b5c4e40SEvgeny Pinchuk {
1765b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
1775b5c4e40SEvgeny Pinchuk 
1784f449311SHarish Kasiviswanathan 	while (!list_empty(device_list)) {
1794f449311SHarish Kasiviswanathan 		dev = list_first_entry(device_list,
1805b5c4e40SEvgeny Pinchuk 				       struct kfd_topology_device, list);
1815b5c4e40SEvgeny Pinchuk 		kfd_release_topology_device(dev);
1825b5c4e40SEvgeny Pinchuk 	}
1834f449311SHarish Kasiviswanathan }
1845b5c4e40SEvgeny Pinchuk 
1854f449311SHarish Kasiviswanathan static void kfd_release_live_view(void)
1864f449311SHarish Kasiviswanathan {
1874f449311SHarish Kasiviswanathan 	kfd_release_topology_device_list(&topology_device_list);
1885b5c4e40SEvgeny Pinchuk 	memset(&sys_props, 0, sizeof(sys_props));
1895b5c4e40SEvgeny Pinchuk }
1905b5c4e40SEvgeny Pinchuk 
1914f449311SHarish Kasiviswanathan struct kfd_topology_device *kfd_create_topology_device(
1924f449311SHarish Kasiviswanathan 				struct list_head *device_list)
1935b5c4e40SEvgeny Pinchuk {
1945b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
1955b5c4e40SEvgeny Pinchuk 
1965b5c4e40SEvgeny Pinchuk 	dev = kfd_alloc_struct(dev);
1974eacc26bSKent Russell 	if (!dev) {
1985b5c4e40SEvgeny Pinchuk 		pr_err("No memory to allocate a topology device");
19916b9201cSOded Gabbay 		return NULL;
2005b5c4e40SEvgeny Pinchuk 	}
2015b5c4e40SEvgeny Pinchuk 
2025b5c4e40SEvgeny Pinchuk 	INIT_LIST_HEAD(&dev->mem_props);
2035b5c4e40SEvgeny Pinchuk 	INIT_LIST_HEAD(&dev->cache_props);
2045b5c4e40SEvgeny Pinchuk 	INIT_LIST_HEAD(&dev->io_link_props);
205f4757347SAmber Lin 	INIT_LIST_HEAD(&dev->perf_props);
2065b5c4e40SEvgeny Pinchuk 
2074f449311SHarish Kasiviswanathan 	list_add_tail(&dev->list, device_list);
2085b5c4e40SEvgeny Pinchuk 
2095b5c4e40SEvgeny Pinchuk 	return dev;
2105b5c4e40SEvgeny Pinchuk }
2115b5c4e40SEvgeny Pinchuk 
2125b5c4e40SEvgeny Pinchuk 
2135b5c4e40SEvgeny Pinchuk #define sysfs_show_gen_prop(buffer, fmt, ...) \
2145b5c4e40SEvgeny Pinchuk 		snprintf(buffer, PAGE_SIZE, "%s"fmt, buffer, __VA_ARGS__)
2155b5c4e40SEvgeny Pinchuk #define sysfs_show_32bit_prop(buffer, name, value) \
2165b5c4e40SEvgeny Pinchuk 		sysfs_show_gen_prop(buffer, "%s %u\n", name, value)
2175b5c4e40SEvgeny Pinchuk #define sysfs_show_64bit_prop(buffer, name, value) \
2185b5c4e40SEvgeny Pinchuk 		sysfs_show_gen_prop(buffer, "%s %llu\n", name, value)
2195b5c4e40SEvgeny Pinchuk #define sysfs_show_32bit_val(buffer, value) \
2205b5c4e40SEvgeny Pinchuk 		sysfs_show_gen_prop(buffer, "%u\n", value)
2215b5c4e40SEvgeny Pinchuk #define sysfs_show_str_val(buffer, value) \
2225b5c4e40SEvgeny Pinchuk 		sysfs_show_gen_prop(buffer, "%s\n", value)
2235b5c4e40SEvgeny Pinchuk 
2245b5c4e40SEvgeny Pinchuk static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
2255b5c4e40SEvgeny Pinchuk 		char *buffer)
2265b5c4e40SEvgeny Pinchuk {
2275b5c4e40SEvgeny Pinchuk 	ssize_t ret;
2285b5c4e40SEvgeny Pinchuk 
2295b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
2305b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
2315b5c4e40SEvgeny Pinchuk 
2325b5c4e40SEvgeny Pinchuk 	if (attr == &sys_props.attr_genid) {
2335b5c4e40SEvgeny Pinchuk 		ret = sysfs_show_32bit_val(buffer, sys_props.generation_count);
2345b5c4e40SEvgeny Pinchuk 	} else if (attr == &sys_props.attr_props) {
2355b5c4e40SEvgeny Pinchuk 		sysfs_show_64bit_prop(buffer, "platform_oem",
2365b5c4e40SEvgeny Pinchuk 				sys_props.platform_oem);
2375b5c4e40SEvgeny Pinchuk 		sysfs_show_64bit_prop(buffer, "platform_id",
2385b5c4e40SEvgeny Pinchuk 				sys_props.platform_id);
2395b5c4e40SEvgeny Pinchuk 		ret = sysfs_show_64bit_prop(buffer, "platform_rev",
2405b5c4e40SEvgeny Pinchuk 				sys_props.platform_rev);
2415b5c4e40SEvgeny Pinchuk 	} else {
2425b5c4e40SEvgeny Pinchuk 		ret = -EINVAL;
2435b5c4e40SEvgeny Pinchuk 	}
2445b5c4e40SEvgeny Pinchuk 
2455b5c4e40SEvgeny Pinchuk 	return ret;
2465b5c4e40SEvgeny Pinchuk }
2475b5c4e40SEvgeny Pinchuk 
2485108d768SYong Zhao static void kfd_topology_kobj_release(struct kobject *kobj)
2495108d768SYong Zhao {
2505108d768SYong Zhao 	kfree(kobj);
2515108d768SYong Zhao }
2525108d768SYong Zhao 
2535b5c4e40SEvgeny Pinchuk static const struct sysfs_ops sysprops_ops = {
2545b5c4e40SEvgeny Pinchuk 	.show = sysprops_show,
2555b5c4e40SEvgeny Pinchuk };
2565b5c4e40SEvgeny Pinchuk 
2575b5c4e40SEvgeny Pinchuk static struct kobj_type sysprops_type = {
2585108d768SYong Zhao 	.release = kfd_topology_kobj_release,
2595b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &sysprops_ops,
2605b5c4e40SEvgeny Pinchuk };
2615b5c4e40SEvgeny Pinchuk 
2625b5c4e40SEvgeny Pinchuk static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
2635b5c4e40SEvgeny Pinchuk 		char *buffer)
2645b5c4e40SEvgeny Pinchuk {
2655b5c4e40SEvgeny Pinchuk 	ssize_t ret;
2665b5c4e40SEvgeny Pinchuk 	struct kfd_iolink_properties *iolink;
2675b5c4e40SEvgeny Pinchuk 
2685b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
2695b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
2705b5c4e40SEvgeny Pinchuk 
2715b5c4e40SEvgeny Pinchuk 	iolink = container_of(attr, struct kfd_iolink_properties, attr);
2726b855f7bSHarish Kasiviswanathan 	if (iolink->gpu && kfd_devcgroup_check_permission(iolink->gpu))
2736b855f7bSHarish Kasiviswanathan 		return -EPERM;
2745b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
2755b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
2765b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
2775b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
2785b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
2795b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
2805b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
2815b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
2825b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
2835b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
2845b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
2855b5c4e40SEvgeny Pinchuk 			iolink->rec_transfer_size);
2865b5c4e40SEvgeny Pinchuk 	ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
2875b5c4e40SEvgeny Pinchuk 
2885b5c4e40SEvgeny Pinchuk 	return ret;
2895b5c4e40SEvgeny Pinchuk }
2905b5c4e40SEvgeny Pinchuk 
2915b5c4e40SEvgeny Pinchuk static const struct sysfs_ops iolink_ops = {
2925b5c4e40SEvgeny Pinchuk 	.show = iolink_show,
2935b5c4e40SEvgeny Pinchuk };
2945b5c4e40SEvgeny Pinchuk 
2955b5c4e40SEvgeny Pinchuk static struct kobj_type iolink_type = {
2965108d768SYong Zhao 	.release = kfd_topology_kobj_release,
2975b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &iolink_ops,
2985b5c4e40SEvgeny Pinchuk };
2995b5c4e40SEvgeny Pinchuk 
3005b5c4e40SEvgeny Pinchuk static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
3015b5c4e40SEvgeny Pinchuk 		char *buffer)
3025b5c4e40SEvgeny Pinchuk {
3035b5c4e40SEvgeny Pinchuk 	ssize_t ret;
3045b5c4e40SEvgeny Pinchuk 	struct kfd_mem_properties *mem;
3055b5c4e40SEvgeny Pinchuk 
3065b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
3075b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
3085b5c4e40SEvgeny Pinchuk 
3095b5c4e40SEvgeny Pinchuk 	mem = container_of(attr, struct kfd_mem_properties, attr);
3106b855f7bSHarish Kasiviswanathan 	if (mem->gpu && kfd_devcgroup_check_permission(mem->gpu))
3116b855f7bSHarish Kasiviswanathan 		return -EPERM;
3125b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
3135b5c4e40SEvgeny Pinchuk 	sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
3145b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "flags", mem->flags);
3155b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "width", mem->width);
3165b5c4e40SEvgeny Pinchuk 	ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
3175b5c4e40SEvgeny Pinchuk 
3185b5c4e40SEvgeny Pinchuk 	return ret;
3195b5c4e40SEvgeny Pinchuk }
3205b5c4e40SEvgeny Pinchuk 
3215b5c4e40SEvgeny Pinchuk static const struct sysfs_ops mem_ops = {
3225b5c4e40SEvgeny Pinchuk 	.show = mem_show,
3235b5c4e40SEvgeny Pinchuk };
3245b5c4e40SEvgeny Pinchuk 
3255b5c4e40SEvgeny Pinchuk static struct kobj_type mem_type = {
3265108d768SYong Zhao 	.release = kfd_topology_kobj_release,
3275b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &mem_ops,
3285b5c4e40SEvgeny Pinchuk };
3295b5c4e40SEvgeny Pinchuk 
3305b5c4e40SEvgeny Pinchuk static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
3315b5c4e40SEvgeny Pinchuk 		char *buffer)
3325b5c4e40SEvgeny Pinchuk {
3335b5c4e40SEvgeny Pinchuk 	ssize_t ret;
334bc0c75a3SHarish Kasiviswanathan 	uint32_t i, j;
3355b5c4e40SEvgeny Pinchuk 	struct kfd_cache_properties *cache;
3365b5c4e40SEvgeny Pinchuk 
3375b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
3385b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
3395b5c4e40SEvgeny Pinchuk 
3405b5c4e40SEvgeny Pinchuk 	cache = container_of(attr, struct kfd_cache_properties, attr);
3416b855f7bSHarish Kasiviswanathan 	if (cache->gpu && kfd_devcgroup_check_permission(cache->gpu))
3426b855f7bSHarish Kasiviswanathan 		return -EPERM;
3435b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "processor_id_low",
3445b5c4e40SEvgeny Pinchuk 			cache->processor_id_low);
3455b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
3465b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
3475b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
3485b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
3495b5c4e40SEvgeny Pinchuk 			cache->cachelines_per_tag);
3505b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
3515b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
3525b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
3535b5c4e40SEvgeny Pinchuk 	snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
354bc0c75a3SHarish Kasiviswanathan 	for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++)
355bc0c75a3SHarish Kasiviswanathan 		for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++) {
356bc0c75a3SHarish Kasiviswanathan 			/* Check each bit */
357bc0c75a3SHarish Kasiviswanathan 			if (cache->sibling_map[i] & (1 << j))
358bc0c75a3SHarish Kasiviswanathan 				ret = snprintf(buffer, PAGE_SIZE,
359bc0c75a3SHarish Kasiviswanathan 					 "%s%d%s", buffer, 1, ",");
360bc0c75a3SHarish Kasiviswanathan 			else
361bc0c75a3SHarish Kasiviswanathan 				ret = snprintf(buffer, PAGE_SIZE,
362bc0c75a3SHarish Kasiviswanathan 					 "%s%d%s", buffer, 0, ",");
363bc0c75a3SHarish Kasiviswanathan 		}
364bc0c75a3SHarish Kasiviswanathan 	/* Replace the last "," with end of line */
365bc0c75a3SHarish Kasiviswanathan 	*(buffer + strlen(buffer) - 1) = 0xA;
3665b5c4e40SEvgeny Pinchuk 	return ret;
3675b5c4e40SEvgeny Pinchuk }
3685b5c4e40SEvgeny Pinchuk 
3695b5c4e40SEvgeny Pinchuk static const struct sysfs_ops cache_ops = {
3705b5c4e40SEvgeny Pinchuk 	.show = kfd_cache_show,
3715b5c4e40SEvgeny Pinchuk };
3725b5c4e40SEvgeny Pinchuk 
3735b5c4e40SEvgeny Pinchuk static struct kobj_type cache_type = {
3745108d768SYong Zhao 	.release = kfd_topology_kobj_release,
3755b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &cache_ops,
3765b5c4e40SEvgeny Pinchuk };
3775b5c4e40SEvgeny Pinchuk 
378f4757347SAmber Lin /****** Sysfs of Performance Counters ******/
379f4757347SAmber Lin 
380f4757347SAmber Lin struct kfd_perf_attr {
381f4757347SAmber Lin 	struct kobj_attribute attr;
382f4757347SAmber Lin 	uint32_t data;
383f4757347SAmber Lin };
384f4757347SAmber Lin 
385f4757347SAmber Lin static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
386f4757347SAmber Lin 			char *buf)
387f4757347SAmber Lin {
388f4757347SAmber Lin 	struct kfd_perf_attr *attr;
389f4757347SAmber Lin 
390f4757347SAmber Lin 	buf[0] = 0;
391f4757347SAmber Lin 	attr = container_of(attrs, struct kfd_perf_attr, attr);
392f4757347SAmber Lin 	if (!attr->data) /* invalid data for PMC */
393f4757347SAmber Lin 		return 0;
394f4757347SAmber Lin 	else
395f4757347SAmber Lin 		return sysfs_show_32bit_val(buf, attr->data);
396f4757347SAmber Lin }
397f4757347SAmber Lin 
398f4757347SAmber Lin #define KFD_PERF_DESC(_name, _data)			\
399f4757347SAmber Lin {							\
400f4757347SAmber Lin 	.attr  = __ATTR(_name, 0444, perf_show, NULL),	\
401f4757347SAmber Lin 	.data = _data,					\
402f4757347SAmber Lin }
403f4757347SAmber Lin 
404f4757347SAmber Lin static struct kfd_perf_attr perf_attr_iommu[] = {
405f4757347SAmber Lin 	KFD_PERF_DESC(max_concurrent, 0),
406f4757347SAmber Lin 	KFD_PERF_DESC(num_counters, 0),
407f4757347SAmber Lin 	KFD_PERF_DESC(counter_ids, 0),
408f4757347SAmber Lin };
409f4757347SAmber Lin /****************************************/
410f4757347SAmber Lin 
4115b5c4e40SEvgeny Pinchuk static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
4125b5c4e40SEvgeny Pinchuk 		char *buffer)
4135b5c4e40SEvgeny Pinchuk {
4145b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
415f7c826adSAlexey Skidanov 	uint32_t log_max_watch_addr;
4165b5c4e40SEvgeny Pinchuk 
4175b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
4185b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
4195b5c4e40SEvgeny Pinchuk 
4205b5c4e40SEvgeny Pinchuk 	if (strcmp(attr->name, "gpu_id") == 0) {
4215b5c4e40SEvgeny Pinchuk 		dev = container_of(attr, struct kfd_topology_device,
4225b5c4e40SEvgeny Pinchuk 				attr_gpuid);
4236b855f7bSHarish Kasiviswanathan 		if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
4246b855f7bSHarish Kasiviswanathan 			return -EPERM;
425f7c826adSAlexey Skidanov 		return sysfs_show_32bit_val(buffer, dev->gpu_id);
426f7c826adSAlexey Skidanov 	}
427f7c826adSAlexey Skidanov 
428f7c826adSAlexey Skidanov 	if (strcmp(attr->name, "name") == 0) {
4295b5c4e40SEvgeny Pinchuk 		dev = container_of(attr, struct kfd_topology_device,
4305b5c4e40SEvgeny Pinchuk 				attr_name);
431c181159aSYong Zhao 
4326b855f7bSHarish Kasiviswanathan 		if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
4336b855f7bSHarish Kasiviswanathan 			return -EPERM;
434c181159aSYong Zhao 		return sysfs_show_str_val(buffer, dev->node_props.name);
435f7c826adSAlexey Skidanov 	}
436f7c826adSAlexey Skidanov 
4375b5c4e40SEvgeny Pinchuk 	dev = container_of(attr, struct kfd_topology_device,
4385b5c4e40SEvgeny Pinchuk 			attr_props);
4396b855f7bSHarish Kasiviswanathan 	if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
4406b855f7bSHarish Kasiviswanathan 		return -EPERM;
4415b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "cpu_cores_count",
4425b5c4e40SEvgeny Pinchuk 			dev->node_props.cpu_cores_count);
4435b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "simd_count",
4445b5c4e40SEvgeny Pinchuk 			dev->node_props.simd_count);
4455b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "mem_banks_count",
4465b5c4e40SEvgeny Pinchuk 			dev->node_props.mem_banks_count);
4475b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "caches_count",
4485b5c4e40SEvgeny Pinchuk 			dev->node_props.caches_count);
4495b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "io_links_count",
4505b5c4e40SEvgeny Pinchuk 			dev->node_props.io_links_count);
4515b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
4525b5c4e40SEvgeny Pinchuk 			dev->node_props.cpu_core_id_base);
4535b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "simd_id_base",
4545b5c4e40SEvgeny Pinchuk 			dev->node_props.simd_id_base);
4555b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
4565b5c4e40SEvgeny Pinchuk 			dev->node_props.max_waves_per_simd);
4575b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
4585b5c4e40SEvgeny Pinchuk 			dev->node_props.lds_size_in_kb);
4595b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
4605b5c4e40SEvgeny Pinchuk 			dev->node_props.gds_size_in_kb);
46129e76462SOak Zeng 	sysfs_show_32bit_prop(buffer, "num_gws",
46229e76462SOak Zeng 			dev->node_props.num_gws);
4635b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "wave_front_size",
4645b5c4e40SEvgeny Pinchuk 			dev->node_props.wave_front_size);
4655b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "array_count",
4665b5c4e40SEvgeny Pinchuk 			dev->node_props.array_count);
4675b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
4685b5c4e40SEvgeny Pinchuk 			dev->node_props.simd_arrays_per_engine);
4695b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
4705b5c4e40SEvgeny Pinchuk 			dev->node_props.cu_per_simd_array);
4715b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "simd_per_cu",
4725b5c4e40SEvgeny Pinchuk 			dev->node_props.simd_per_cu);
4735b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
4745b5c4e40SEvgeny Pinchuk 			dev->node_props.max_slots_scratch_cu);
4755b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "vendor_id",
4765b5c4e40SEvgeny Pinchuk 			dev->node_props.vendor_id);
4775b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "device_id",
4785b5c4e40SEvgeny Pinchuk 			dev->node_props.device_id);
4795b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "location_id",
4805b5c4e40SEvgeny Pinchuk 			dev->node_props.location_id);
4817c9b7171SOak Zeng 	sysfs_show_32bit_prop(buffer, "drm_render_minor",
4827c9b7171SOak Zeng 			dev->node_props.drm_render_minor);
4830c1690e3SShaoyun Liu 	sysfs_show_64bit_prop(buffer, "hive_id",
4840c1690e3SShaoyun Liu 			dev->node_props.hive_id);
48514568cf6SOak Zeng 	sysfs_show_32bit_prop(buffer, "num_sdma_engines",
48614568cf6SOak Zeng 			dev->node_props.num_sdma_engines);
48714568cf6SOak Zeng 	sysfs_show_32bit_prop(buffer, "num_sdma_xgmi_engines",
48814568cf6SOak Zeng 			dev->node_props.num_sdma_xgmi_engines);
489bb71c74dSHuang Rui 	sysfs_show_32bit_prop(buffer, "num_sdma_queues_per_engine",
490bb71c74dSHuang Rui 			dev->node_props.num_sdma_queues_per_engine);
491f4feb9faSHuang Rui 	sysfs_show_32bit_prop(buffer, "num_cp_queues",
492f4feb9faSHuang Rui 			dev->node_props.num_cp_queues);
4930c663695SDivya Shikre     sysfs_show_64bit_prop(buffer, "unique_id",
4940c663695SDivya Shikre 			dev->node_props.unique_id);
4955b5c4e40SEvgeny Pinchuk 
4965b5c4e40SEvgeny Pinchuk 	if (dev->gpu) {
497f7c826adSAlexey Skidanov 		log_max_watch_addr =
498f7c826adSAlexey Skidanov 			__ilog2_u32(dev->gpu->device_info->num_of_watch_points);
499f7c826adSAlexey Skidanov 
500f7c826adSAlexey Skidanov 		if (log_max_watch_addr) {
501f7c826adSAlexey Skidanov 			dev->node_props.capability |=
502f7c826adSAlexey Skidanov 					HSA_CAP_WATCH_POINTS_SUPPORTED;
503f7c826adSAlexey Skidanov 
504f7c826adSAlexey Skidanov 			dev->node_props.capability |=
505f7c826adSAlexey Skidanov 				((log_max_watch_addr <<
506f7c826adSAlexey Skidanov 					HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
507f7c826adSAlexey Skidanov 				HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
508f7c826adSAlexey Skidanov 		}
509f7c826adSAlexey Skidanov 
510413e85d5SBen Goz 		if (dev->gpu->device_info->asic_family == CHIP_TONGA)
511413e85d5SBen Goz 			dev->node_props.capability |=
512413e85d5SBen Goz 					HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
513413e85d5SBen Goz 
5145b5c4e40SEvgeny Pinchuk 		sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
5153a87177eSHarish Kasiviswanathan 			dev->node_props.max_engine_clk_fcompute);
51642e08c78SOded Gabbay 
5175b5c4e40SEvgeny Pinchuk 		sysfs_show_64bit_prop(buffer, "local_mem_size",
51842e08c78SOded Gabbay 				(unsigned long long int) 0);
519f1386fbcSOded Gabbay 
520f1386fbcSOded Gabbay 		sysfs_show_32bit_prop(buffer, "fw_version",
5215ade6c9cSFelix Kuehling 				dev->gpu->mec_fw_version);
522826f5de8SAlexey Skidanov 		sysfs_show_32bit_prop(buffer, "capability",
523826f5de8SAlexey Skidanov 				dev->node_props.capability);
5245ade6c9cSFelix Kuehling 		sysfs_show_32bit_prop(buffer, "sdma_fw_version",
5255ade6c9cSFelix Kuehling 				dev->gpu->sdma_fw_version);
5265b5c4e40SEvgeny Pinchuk 	}
5275b5c4e40SEvgeny Pinchuk 
528f7c826adSAlexey Skidanov 	return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
5295b5c4e40SEvgeny Pinchuk 					cpufreq_quick_get_max(0)/1000);
5305b5c4e40SEvgeny Pinchuk }
5315b5c4e40SEvgeny Pinchuk 
5325b5c4e40SEvgeny Pinchuk static const struct sysfs_ops node_ops = {
5335b5c4e40SEvgeny Pinchuk 	.show = node_show,
5345b5c4e40SEvgeny Pinchuk };
5355b5c4e40SEvgeny Pinchuk 
5365b5c4e40SEvgeny Pinchuk static struct kobj_type node_type = {
5375108d768SYong Zhao 	.release = kfd_topology_kobj_release,
5385b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &node_ops,
5395b5c4e40SEvgeny Pinchuk };
5405b5c4e40SEvgeny Pinchuk 
5415b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
5425b5c4e40SEvgeny Pinchuk {
5435b5c4e40SEvgeny Pinchuk 	sysfs_remove_file(kobj, attr);
5445b5c4e40SEvgeny Pinchuk 	kobject_del(kobj);
5455b5c4e40SEvgeny Pinchuk 	kobject_put(kobj);
5465b5c4e40SEvgeny Pinchuk }
5475b5c4e40SEvgeny Pinchuk 
5485b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
5495b5c4e40SEvgeny Pinchuk {
5505b5c4e40SEvgeny Pinchuk 	struct kfd_iolink_properties *iolink;
5515b5c4e40SEvgeny Pinchuk 	struct kfd_cache_properties *cache;
5525b5c4e40SEvgeny Pinchuk 	struct kfd_mem_properties *mem;
553f4757347SAmber Lin 	struct kfd_perf_properties *perf;
5545b5c4e40SEvgeny Pinchuk 
5555b5c4e40SEvgeny Pinchuk 	if (dev->kobj_iolink) {
5565b5c4e40SEvgeny Pinchuk 		list_for_each_entry(iolink, &dev->io_link_props, list)
5575b5c4e40SEvgeny Pinchuk 			if (iolink->kobj) {
5585b5c4e40SEvgeny Pinchuk 				kfd_remove_sysfs_file(iolink->kobj,
5595b5c4e40SEvgeny Pinchuk 							&iolink->attr);
56016b9201cSOded Gabbay 				iolink->kobj = NULL;
5615b5c4e40SEvgeny Pinchuk 			}
5625b5c4e40SEvgeny Pinchuk 		kobject_del(dev->kobj_iolink);
5635b5c4e40SEvgeny Pinchuk 		kobject_put(dev->kobj_iolink);
56416b9201cSOded Gabbay 		dev->kobj_iolink = NULL;
5655b5c4e40SEvgeny Pinchuk 	}
5665b5c4e40SEvgeny Pinchuk 
5675b5c4e40SEvgeny Pinchuk 	if (dev->kobj_cache) {
5685b5c4e40SEvgeny Pinchuk 		list_for_each_entry(cache, &dev->cache_props, list)
5695b5c4e40SEvgeny Pinchuk 			if (cache->kobj) {
5705b5c4e40SEvgeny Pinchuk 				kfd_remove_sysfs_file(cache->kobj,
5715b5c4e40SEvgeny Pinchuk 							&cache->attr);
57216b9201cSOded Gabbay 				cache->kobj = NULL;
5735b5c4e40SEvgeny Pinchuk 			}
5745b5c4e40SEvgeny Pinchuk 		kobject_del(dev->kobj_cache);
5755b5c4e40SEvgeny Pinchuk 		kobject_put(dev->kobj_cache);
57616b9201cSOded Gabbay 		dev->kobj_cache = NULL;
5775b5c4e40SEvgeny Pinchuk 	}
5785b5c4e40SEvgeny Pinchuk 
5795b5c4e40SEvgeny Pinchuk 	if (dev->kobj_mem) {
5805b5c4e40SEvgeny Pinchuk 		list_for_each_entry(mem, &dev->mem_props, list)
5815b5c4e40SEvgeny Pinchuk 			if (mem->kobj) {
5825b5c4e40SEvgeny Pinchuk 				kfd_remove_sysfs_file(mem->kobj, &mem->attr);
58316b9201cSOded Gabbay 				mem->kobj = NULL;
5845b5c4e40SEvgeny Pinchuk 			}
5855b5c4e40SEvgeny Pinchuk 		kobject_del(dev->kobj_mem);
5865b5c4e40SEvgeny Pinchuk 		kobject_put(dev->kobj_mem);
58716b9201cSOded Gabbay 		dev->kobj_mem = NULL;
5885b5c4e40SEvgeny Pinchuk 	}
5895b5c4e40SEvgeny Pinchuk 
590f4757347SAmber Lin 	if (dev->kobj_perf) {
591f4757347SAmber Lin 		list_for_each_entry(perf, &dev->perf_props, list) {
592f4757347SAmber Lin 			kfree(perf->attr_group);
593f4757347SAmber Lin 			perf->attr_group = NULL;
594f4757347SAmber Lin 		}
595f4757347SAmber Lin 		kobject_del(dev->kobj_perf);
596f4757347SAmber Lin 		kobject_put(dev->kobj_perf);
597f4757347SAmber Lin 		dev->kobj_perf = NULL;
598f4757347SAmber Lin 	}
599f4757347SAmber Lin 
6005b5c4e40SEvgeny Pinchuk 	if (dev->kobj_node) {
6015b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
6025b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(dev->kobj_node, &dev->attr_name);
6035b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(dev->kobj_node, &dev->attr_props);
6045b5c4e40SEvgeny Pinchuk 		kobject_del(dev->kobj_node);
6055b5c4e40SEvgeny Pinchuk 		kobject_put(dev->kobj_node);
60616b9201cSOded Gabbay 		dev->kobj_node = NULL;
6075b5c4e40SEvgeny Pinchuk 	}
6085b5c4e40SEvgeny Pinchuk }
6095b5c4e40SEvgeny Pinchuk 
6105b5c4e40SEvgeny Pinchuk static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
6115b5c4e40SEvgeny Pinchuk 		uint32_t id)
6125b5c4e40SEvgeny Pinchuk {
6135b5c4e40SEvgeny Pinchuk 	struct kfd_iolink_properties *iolink;
6145b5c4e40SEvgeny Pinchuk 	struct kfd_cache_properties *cache;
6155b5c4e40SEvgeny Pinchuk 	struct kfd_mem_properties *mem;
616f4757347SAmber Lin 	struct kfd_perf_properties *perf;
6175b5c4e40SEvgeny Pinchuk 	int ret;
618f4757347SAmber Lin 	uint32_t i, num_attrs;
619f4757347SAmber Lin 	struct attribute **attrs;
6205b5c4e40SEvgeny Pinchuk 
62132fa8219SFelix Kuehling 	if (WARN_ON(dev->kobj_node))
62232fa8219SFelix Kuehling 		return -EEXIST;
62332fa8219SFelix Kuehling 
6245b5c4e40SEvgeny Pinchuk 	/*
6255b5c4e40SEvgeny Pinchuk 	 * Creating the sysfs folders
6265b5c4e40SEvgeny Pinchuk 	 */
6275b5c4e40SEvgeny Pinchuk 	dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
6285b5c4e40SEvgeny Pinchuk 	if (!dev->kobj_node)
6295b5c4e40SEvgeny Pinchuk 		return -ENOMEM;
6305b5c4e40SEvgeny Pinchuk 
6315b5c4e40SEvgeny Pinchuk 	ret = kobject_init_and_add(dev->kobj_node, &node_type,
6325b5c4e40SEvgeny Pinchuk 			sys_props.kobj_nodes, "%d", id);
6335b5c4e40SEvgeny Pinchuk 	if (ret < 0)
6345b5c4e40SEvgeny Pinchuk 		return ret;
6355b5c4e40SEvgeny Pinchuk 
6365b5c4e40SEvgeny Pinchuk 	dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
6375b5c4e40SEvgeny Pinchuk 	if (!dev->kobj_mem)
6385b5c4e40SEvgeny Pinchuk 		return -ENOMEM;
6395b5c4e40SEvgeny Pinchuk 
6405b5c4e40SEvgeny Pinchuk 	dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
6415b5c4e40SEvgeny Pinchuk 	if (!dev->kobj_cache)
6425b5c4e40SEvgeny Pinchuk 		return -ENOMEM;
6435b5c4e40SEvgeny Pinchuk 
6445b5c4e40SEvgeny Pinchuk 	dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
6455b5c4e40SEvgeny Pinchuk 	if (!dev->kobj_iolink)
6465b5c4e40SEvgeny Pinchuk 		return -ENOMEM;
6475b5c4e40SEvgeny Pinchuk 
648f4757347SAmber Lin 	dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
649f4757347SAmber Lin 	if (!dev->kobj_perf)
650f4757347SAmber Lin 		return -ENOMEM;
651f4757347SAmber Lin 
6525b5c4e40SEvgeny Pinchuk 	/*
6535b5c4e40SEvgeny Pinchuk 	 * Creating sysfs files for node properties
6545b5c4e40SEvgeny Pinchuk 	 */
6555b5c4e40SEvgeny Pinchuk 	dev->attr_gpuid.name = "gpu_id";
6565b5c4e40SEvgeny Pinchuk 	dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
6575b5c4e40SEvgeny Pinchuk 	sysfs_attr_init(&dev->attr_gpuid);
6585b5c4e40SEvgeny Pinchuk 	dev->attr_name.name = "name";
6595b5c4e40SEvgeny Pinchuk 	dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
6605b5c4e40SEvgeny Pinchuk 	sysfs_attr_init(&dev->attr_name);
6615b5c4e40SEvgeny Pinchuk 	dev->attr_props.name = "properties";
6625b5c4e40SEvgeny Pinchuk 	dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
6635b5c4e40SEvgeny Pinchuk 	sysfs_attr_init(&dev->attr_props);
6645b5c4e40SEvgeny Pinchuk 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
6655b5c4e40SEvgeny Pinchuk 	if (ret < 0)
6665b5c4e40SEvgeny Pinchuk 		return ret;
6675b5c4e40SEvgeny Pinchuk 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
6685b5c4e40SEvgeny Pinchuk 	if (ret < 0)
6695b5c4e40SEvgeny Pinchuk 		return ret;
6705b5c4e40SEvgeny Pinchuk 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
6715b5c4e40SEvgeny Pinchuk 	if (ret < 0)
6725b5c4e40SEvgeny Pinchuk 		return ret;
6735b5c4e40SEvgeny Pinchuk 
6745b5c4e40SEvgeny Pinchuk 	i = 0;
6755b5c4e40SEvgeny Pinchuk 	list_for_each_entry(mem, &dev->mem_props, list) {
6765b5c4e40SEvgeny Pinchuk 		mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
6775b5c4e40SEvgeny Pinchuk 		if (!mem->kobj)
6785b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
6795b5c4e40SEvgeny Pinchuk 		ret = kobject_init_and_add(mem->kobj, &mem_type,
6805b5c4e40SEvgeny Pinchuk 				dev->kobj_mem, "%d", i);
6815b5c4e40SEvgeny Pinchuk 		if (ret < 0)
6825b5c4e40SEvgeny Pinchuk 			return ret;
6835b5c4e40SEvgeny Pinchuk 
6845b5c4e40SEvgeny Pinchuk 		mem->attr.name = "properties";
6855b5c4e40SEvgeny Pinchuk 		mem->attr.mode = KFD_SYSFS_FILE_MODE;
6865b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&mem->attr);
6875b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(mem->kobj, &mem->attr);
6885b5c4e40SEvgeny Pinchuk 		if (ret < 0)
6895b5c4e40SEvgeny Pinchuk 			return ret;
6905b5c4e40SEvgeny Pinchuk 		i++;
6915b5c4e40SEvgeny Pinchuk 	}
6925b5c4e40SEvgeny Pinchuk 
6935b5c4e40SEvgeny Pinchuk 	i = 0;
6945b5c4e40SEvgeny Pinchuk 	list_for_each_entry(cache, &dev->cache_props, list) {
6955b5c4e40SEvgeny Pinchuk 		cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
6965b5c4e40SEvgeny Pinchuk 		if (!cache->kobj)
6975b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
6985b5c4e40SEvgeny Pinchuk 		ret = kobject_init_and_add(cache->kobj, &cache_type,
6995b5c4e40SEvgeny Pinchuk 				dev->kobj_cache, "%d", i);
7005b5c4e40SEvgeny Pinchuk 		if (ret < 0)
7015b5c4e40SEvgeny Pinchuk 			return ret;
7025b5c4e40SEvgeny Pinchuk 
7035b5c4e40SEvgeny Pinchuk 		cache->attr.name = "properties";
7045b5c4e40SEvgeny Pinchuk 		cache->attr.mode = KFD_SYSFS_FILE_MODE;
7055b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&cache->attr);
7065b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(cache->kobj, &cache->attr);
7075b5c4e40SEvgeny Pinchuk 		if (ret < 0)
7085b5c4e40SEvgeny Pinchuk 			return ret;
7095b5c4e40SEvgeny Pinchuk 		i++;
7105b5c4e40SEvgeny Pinchuk 	}
7115b5c4e40SEvgeny Pinchuk 
7125b5c4e40SEvgeny Pinchuk 	i = 0;
7135b5c4e40SEvgeny Pinchuk 	list_for_each_entry(iolink, &dev->io_link_props, list) {
7145b5c4e40SEvgeny Pinchuk 		iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
7155b5c4e40SEvgeny Pinchuk 		if (!iolink->kobj)
7165b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
7175b5c4e40SEvgeny Pinchuk 		ret = kobject_init_and_add(iolink->kobj, &iolink_type,
7185b5c4e40SEvgeny Pinchuk 				dev->kobj_iolink, "%d", i);
7195b5c4e40SEvgeny Pinchuk 		if (ret < 0)
7205b5c4e40SEvgeny Pinchuk 			return ret;
7215b5c4e40SEvgeny Pinchuk 
7225b5c4e40SEvgeny Pinchuk 		iolink->attr.name = "properties";
7235b5c4e40SEvgeny Pinchuk 		iolink->attr.mode = KFD_SYSFS_FILE_MODE;
7245b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&iolink->attr);
7255b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(iolink->kobj, &iolink->attr);
7265b5c4e40SEvgeny Pinchuk 		if (ret < 0)
7275b5c4e40SEvgeny Pinchuk 			return ret;
7285b5c4e40SEvgeny Pinchuk 		i++;
7295b5c4e40SEvgeny Pinchuk 	}
7305b5c4e40SEvgeny Pinchuk 
731f4757347SAmber Lin 	/* All hardware blocks have the same number of attributes. */
7323f866f5fSGustavo A. R. Silva 	num_attrs = ARRAY_SIZE(perf_attr_iommu);
733f4757347SAmber Lin 	list_for_each_entry(perf, &dev->perf_props, list) {
734f4757347SAmber Lin 		perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
735f4757347SAmber Lin 			* num_attrs + sizeof(struct attribute_group),
736f4757347SAmber Lin 			GFP_KERNEL);
737f4757347SAmber Lin 		if (!perf->attr_group)
738f4757347SAmber Lin 			return -ENOMEM;
739f4757347SAmber Lin 
740f4757347SAmber Lin 		attrs = (struct attribute **)(perf->attr_group + 1);
741f4757347SAmber Lin 		if (!strcmp(perf->block_name, "iommu")) {
742f4757347SAmber Lin 		/* Information of IOMMU's num_counters and counter_ids is shown
743f4757347SAmber Lin 		 * under /sys/bus/event_source/devices/amd_iommu. We don't
744f4757347SAmber Lin 		 * duplicate here.
745f4757347SAmber Lin 		 */
746f4757347SAmber Lin 			perf_attr_iommu[0].data = perf->max_concurrent;
747f4757347SAmber Lin 			for (i = 0; i < num_attrs; i++)
748f4757347SAmber Lin 				attrs[i] = &perf_attr_iommu[i].attr.attr;
749f4757347SAmber Lin 		}
750f4757347SAmber Lin 		perf->attr_group->name = perf->block_name;
751f4757347SAmber Lin 		perf->attr_group->attrs = attrs;
752f4757347SAmber Lin 		ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
753f4757347SAmber Lin 		if (ret < 0)
754f4757347SAmber Lin 			return ret;
755f4757347SAmber Lin 	}
756f4757347SAmber Lin 
7575b5c4e40SEvgeny Pinchuk 	return 0;
7585b5c4e40SEvgeny Pinchuk }
7595b5c4e40SEvgeny Pinchuk 
7603a87177eSHarish Kasiviswanathan /* Called with write topology lock acquired */
7615b5c4e40SEvgeny Pinchuk static int kfd_build_sysfs_node_tree(void)
7625b5c4e40SEvgeny Pinchuk {
7635b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
7645b5c4e40SEvgeny Pinchuk 	int ret;
7655b5c4e40SEvgeny Pinchuk 	uint32_t i = 0;
7665b5c4e40SEvgeny Pinchuk 
7675b5c4e40SEvgeny Pinchuk 	list_for_each_entry(dev, &topology_device_list, list) {
7688dfead6cSBen Goz 		ret = kfd_build_sysfs_node_entry(dev, i);
7695b5c4e40SEvgeny Pinchuk 		if (ret < 0)
7705b5c4e40SEvgeny Pinchuk 			return ret;
7715b5c4e40SEvgeny Pinchuk 		i++;
7725b5c4e40SEvgeny Pinchuk 	}
7735b5c4e40SEvgeny Pinchuk 
7745b5c4e40SEvgeny Pinchuk 	return 0;
7755b5c4e40SEvgeny Pinchuk }
7765b5c4e40SEvgeny Pinchuk 
7773a87177eSHarish Kasiviswanathan /* Called with write topology lock acquired */
7785b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_node_tree(void)
7795b5c4e40SEvgeny Pinchuk {
7805b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
7815b5c4e40SEvgeny Pinchuk 
7825b5c4e40SEvgeny Pinchuk 	list_for_each_entry(dev, &topology_device_list, list)
7835b5c4e40SEvgeny Pinchuk 		kfd_remove_sysfs_node_entry(dev);
7845b5c4e40SEvgeny Pinchuk }
7855b5c4e40SEvgeny Pinchuk 
7865b5c4e40SEvgeny Pinchuk static int kfd_topology_update_sysfs(void)
7875b5c4e40SEvgeny Pinchuk {
7885b5c4e40SEvgeny Pinchuk 	int ret;
7895b5c4e40SEvgeny Pinchuk 
7905b5c4e40SEvgeny Pinchuk 	pr_info("Creating topology SYSFS entries\n");
7914eacc26bSKent Russell 	if (!sys_props.kobj_topology) {
7925b5c4e40SEvgeny Pinchuk 		sys_props.kobj_topology =
7935b5c4e40SEvgeny Pinchuk 				kfd_alloc_struct(sys_props.kobj_topology);
7945b5c4e40SEvgeny Pinchuk 		if (!sys_props.kobj_topology)
7955b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
7965b5c4e40SEvgeny Pinchuk 
7975b5c4e40SEvgeny Pinchuk 		ret = kobject_init_and_add(sys_props.kobj_topology,
7985b5c4e40SEvgeny Pinchuk 				&sysprops_type,  &kfd_device->kobj,
7995b5c4e40SEvgeny Pinchuk 				"topology");
8005b5c4e40SEvgeny Pinchuk 		if (ret < 0)
8015b5c4e40SEvgeny Pinchuk 			return ret;
8025b5c4e40SEvgeny Pinchuk 
8035b5c4e40SEvgeny Pinchuk 		sys_props.kobj_nodes = kobject_create_and_add("nodes",
8045b5c4e40SEvgeny Pinchuk 				sys_props.kobj_topology);
8055b5c4e40SEvgeny Pinchuk 		if (!sys_props.kobj_nodes)
8065b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
8075b5c4e40SEvgeny Pinchuk 
8085b5c4e40SEvgeny Pinchuk 		sys_props.attr_genid.name = "generation_id";
8095b5c4e40SEvgeny Pinchuk 		sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
8105b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&sys_props.attr_genid);
8115b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(sys_props.kobj_topology,
8125b5c4e40SEvgeny Pinchuk 				&sys_props.attr_genid);
8135b5c4e40SEvgeny Pinchuk 		if (ret < 0)
8145b5c4e40SEvgeny Pinchuk 			return ret;
8155b5c4e40SEvgeny Pinchuk 
8165b5c4e40SEvgeny Pinchuk 		sys_props.attr_props.name = "system_properties";
8175b5c4e40SEvgeny Pinchuk 		sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
8185b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&sys_props.attr_props);
8195b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(sys_props.kobj_topology,
8205b5c4e40SEvgeny Pinchuk 				&sys_props.attr_props);
8215b5c4e40SEvgeny Pinchuk 		if (ret < 0)
8225b5c4e40SEvgeny Pinchuk 			return ret;
8235b5c4e40SEvgeny Pinchuk 	}
8245b5c4e40SEvgeny Pinchuk 
8255b5c4e40SEvgeny Pinchuk 	kfd_remove_sysfs_node_tree();
8265b5c4e40SEvgeny Pinchuk 
8275b5c4e40SEvgeny Pinchuk 	return kfd_build_sysfs_node_tree();
8285b5c4e40SEvgeny Pinchuk }
8295b5c4e40SEvgeny Pinchuk 
8305b5c4e40SEvgeny Pinchuk static void kfd_topology_release_sysfs(void)
8315b5c4e40SEvgeny Pinchuk {
8325b5c4e40SEvgeny Pinchuk 	kfd_remove_sysfs_node_tree();
8335b5c4e40SEvgeny Pinchuk 	if (sys_props.kobj_topology) {
8345b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(sys_props.kobj_topology,
8355b5c4e40SEvgeny Pinchuk 				&sys_props.attr_genid);
8365b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(sys_props.kobj_topology,
8375b5c4e40SEvgeny Pinchuk 				&sys_props.attr_props);
8385b5c4e40SEvgeny Pinchuk 		if (sys_props.kobj_nodes) {
8395b5c4e40SEvgeny Pinchuk 			kobject_del(sys_props.kobj_nodes);
8405b5c4e40SEvgeny Pinchuk 			kobject_put(sys_props.kobj_nodes);
84116b9201cSOded Gabbay 			sys_props.kobj_nodes = NULL;
8425b5c4e40SEvgeny Pinchuk 		}
8435b5c4e40SEvgeny Pinchuk 		kobject_del(sys_props.kobj_topology);
8445b5c4e40SEvgeny Pinchuk 		kobject_put(sys_props.kobj_topology);
84516b9201cSOded Gabbay 		sys_props.kobj_topology = NULL;
8465b5c4e40SEvgeny Pinchuk 	}
8475b5c4e40SEvgeny Pinchuk }
8485b5c4e40SEvgeny Pinchuk 
8494f449311SHarish Kasiviswanathan /* Called with write topology_lock acquired */
8504f449311SHarish Kasiviswanathan static void kfd_topology_update_device_list(struct list_head *temp_list,
8514f449311SHarish Kasiviswanathan 					struct list_head *master_list)
8524f449311SHarish Kasiviswanathan {
8534f449311SHarish Kasiviswanathan 	while (!list_empty(temp_list)) {
8544f449311SHarish Kasiviswanathan 		list_move_tail(temp_list->next, master_list);
8554f449311SHarish Kasiviswanathan 		sys_props.num_devices++;
8564f449311SHarish Kasiviswanathan 	}
8574f449311SHarish Kasiviswanathan }
8584f449311SHarish Kasiviswanathan 
859520b8fb7SFelix Kuehling static void kfd_debug_print_topology(void)
860520b8fb7SFelix Kuehling {
861520b8fb7SFelix Kuehling 	struct kfd_topology_device *dev;
862520b8fb7SFelix Kuehling 
863520b8fb7SFelix Kuehling 	down_read(&topology_lock);
864520b8fb7SFelix Kuehling 
865520b8fb7SFelix Kuehling 	dev = list_last_entry(&topology_device_list,
866520b8fb7SFelix Kuehling 			struct kfd_topology_device, list);
867520b8fb7SFelix Kuehling 	if (dev) {
868520b8fb7SFelix Kuehling 		if (dev->node_props.cpu_cores_count &&
869520b8fb7SFelix Kuehling 				dev->node_props.simd_count) {
870520b8fb7SFelix Kuehling 			pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
871520b8fb7SFelix Kuehling 				dev->node_props.device_id,
872520b8fb7SFelix Kuehling 				dev->node_props.vendor_id);
873520b8fb7SFelix Kuehling 		} else if (dev->node_props.cpu_cores_count)
874520b8fb7SFelix Kuehling 			pr_info("Topology: Add CPU node\n");
875520b8fb7SFelix Kuehling 		else if (dev->node_props.simd_count)
876520b8fb7SFelix Kuehling 			pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
877520b8fb7SFelix Kuehling 				dev->node_props.device_id,
878520b8fb7SFelix Kuehling 				dev->node_props.vendor_id);
879520b8fb7SFelix Kuehling 	}
880520b8fb7SFelix Kuehling 	up_read(&topology_lock);
881520b8fb7SFelix Kuehling }
882520b8fb7SFelix Kuehling 
883520b8fb7SFelix Kuehling /* Helper function for intializing platform_xx members of
884520b8fb7SFelix Kuehling  * kfd_system_properties. Uses OEM info from the last CPU/APU node.
885520b8fb7SFelix Kuehling  */
886520b8fb7SFelix Kuehling static void kfd_update_system_properties(void)
887520b8fb7SFelix Kuehling {
888520b8fb7SFelix Kuehling 	struct kfd_topology_device *dev;
889520b8fb7SFelix Kuehling 
890520b8fb7SFelix Kuehling 	down_read(&topology_lock);
891520b8fb7SFelix Kuehling 	dev = list_last_entry(&topology_device_list,
892520b8fb7SFelix Kuehling 			struct kfd_topology_device, list);
893520b8fb7SFelix Kuehling 	if (dev) {
894520b8fb7SFelix Kuehling 		sys_props.platform_id =
895520b8fb7SFelix Kuehling 			(*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
896520b8fb7SFelix Kuehling 		sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
897520b8fb7SFelix Kuehling 		sys_props.platform_rev = dev->oem_revision;
898520b8fb7SFelix Kuehling 	}
899520b8fb7SFelix Kuehling 	up_read(&topology_lock);
900520b8fb7SFelix Kuehling }
901520b8fb7SFelix Kuehling 
902520b8fb7SFelix Kuehling static void find_system_memory(const struct dmi_header *dm,
903520b8fb7SFelix Kuehling 	void *private)
904520b8fb7SFelix Kuehling {
905520b8fb7SFelix Kuehling 	struct kfd_mem_properties *mem;
906520b8fb7SFelix Kuehling 	u16 mem_width, mem_clock;
907520b8fb7SFelix Kuehling 	struct kfd_topology_device *kdev =
908520b8fb7SFelix Kuehling 		(struct kfd_topology_device *)private;
909520b8fb7SFelix Kuehling 	const u8 *dmi_data = (const u8 *)(dm + 1);
910520b8fb7SFelix Kuehling 
911520b8fb7SFelix Kuehling 	if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
912520b8fb7SFelix Kuehling 		mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
913520b8fb7SFelix Kuehling 		mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
914520b8fb7SFelix Kuehling 		list_for_each_entry(mem, &kdev->mem_props, list) {
915520b8fb7SFelix Kuehling 			if (mem_width != 0xFFFF && mem_width != 0)
916520b8fb7SFelix Kuehling 				mem->width = mem_width;
917520b8fb7SFelix Kuehling 			if (mem_clock != 0)
918520b8fb7SFelix Kuehling 				mem->mem_clk_max = mem_clock;
919520b8fb7SFelix Kuehling 		}
920520b8fb7SFelix Kuehling 	}
921520b8fb7SFelix Kuehling }
922f4757347SAmber Lin 
923f4757347SAmber Lin /*
924f4757347SAmber Lin  * Performance counters information is not part of CRAT but we would like to
925f4757347SAmber Lin  * put them in the sysfs under topology directory for Thunk to get the data.
926f4757347SAmber Lin  * This function is called before updating the sysfs.
927f4757347SAmber Lin  */
928f4757347SAmber Lin static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
929f4757347SAmber Lin {
93064d1c3a4SFelix Kuehling 	/* These are the only counters supported so far */
93164d1c3a4SFelix Kuehling 	return kfd_iommu_add_perf_counters(kdev);
932f4757347SAmber Lin }
933f4757347SAmber Lin 
934520b8fb7SFelix Kuehling /* kfd_add_non_crat_information - Add information that is not currently
935520b8fb7SFelix Kuehling  *	defined in CRAT but is necessary for KFD topology
936520b8fb7SFelix Kuehling  * @dev - topology device to which addition info is added
937520b8fb7SFelix Kuehling  */
938520b8fb7SFelix Kuehling static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
939520b8fb7SFelix Kuehling {
940520b8fb7SFelix Kuehling 	/* Check if CPU only node. */
941520b8fb7SFelix Kuehling 	if (!kdev->gpu) {
942520b8fb7SFelix Kuehling 		/* Add system memory information */
943520b8fb7SFelix Kuehling 		dmi_walk(find_system_memory, kdev);
944520b8fb7SFelix Kuehling 	}
945520b8fb7SFelix Kuehling 	/* TODO: For GPU node, rearrange code from kfd_topology_add_device */
946520b8fb7SFelix Kuehling }
947520b8fb7SFelix Kuehling 
948b441093eSHarish Kasiviswanathan /* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
949b441093eSHarish Kasiviswanathan  *	Ignore CRAT for all other devices. AMD APU is identified if both CPU
950b441093eSHarish Kasiviswanathan  *	and GPU cores are present.
951b441093eSHarish Kasiviswanathan  * @device_list - topology device list created by parsing ACPI CRAT table.
952b441093eSHarish Kasiviswanathan  * @return - TRUE if invalid, FALSE is valid.
953b441093eSHarish Kasiviswanathan  */
954b441093eSHarish Kasiviswanathan static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
955b441093eSHarish Kasiviswanathan {
956b441093eSHarish Kasiviswanathan 	struct kfd_topology_device *dev;
957b441093eSHarish Kasiviswanathan 
958b441093eSHarish Kasiviswanathan 	list_for_each_entry(dev, device_list, list) {
959b441093eSHarish Kasiviswanathan 		if (dev->node_props.cpu_cores_count &&
960b441093eSHarish Kasiviswanathan 			dev->node_props.simd_count)
961b441093eSHarish Kasiviswanathan 			return false;
962b441093eSHarish Kasiviswanathan 	}
963b441093eSHarish Kasiviswanathan 	pr_info("Ignoring ACPI CRAT on non-APU system\n");
964b441093eSHarish Kasiviswanathan 	return true;
965b441093eSHarish Kasiviswanathan }
966b441093eSHarish Kasiviswanathan 
9675b5c4e40SEvgeny Pinchuk int kfd_topology_init(void)
9685b5c4e40SEvgeny Pinchuk {
96916b9201cSOded Gabbay 	void *crat_image = NULL;
9705b5c4e40SEvgeny Pinchuk 	size_t image_size = 0;
9715b5c4e40SEvgeny Pinchuk 	int ret;
9724f449311SHarish Kasiviswanathan 	struct list_head temp_topology_device_list;
973520b8fb7SFelix Kuehling 	int cpu_only_node = 0;
974520b8fb7SFelix Kuehling 	struct kfd_topology_device *kdev;
975520b8fb7SFelix Kuehling 	int proximity_domain;
9765b5c4e40SEvgeny Pinchuk 
9774f449311SHarish Kasiviswanathan 	/* topology_device_list - Master list of all topology devices
9784f449311SHarish Kasiviswanathan 	 * temp_topology_device_list - temporary list created while parsing CRAT
9794f449311SHarish Kasiviswanathan 	 * or VCRAT. Once parsing is complete the contents of list is moved to
9804f449311SHarish Kasiviswanathan 	 * topology_device_list
9815b5c4e40SEvgeny Pinchuk 	 */
9824f449311SHarish Kasiviswanathan 
9834f449311SHarish Kasiviswanathan 	/* Initialize the head for the both the lists */
9845b5c4e40SEvgeny Pinchuk 	INIT_LIST_HEAD(&topology_device_list);
9854f449311SHarish Kasiviswanathan 	INIT_LIST_HEAD(&temp_topology_device_list);
9865b5c4e40SEvgeny Pinchuk 	init_rwsem(&topology_lock);
9875b5c4e40SEvgeny Pinchuk 
9885b5c4e40SEvgeny Pinchuk 	memset(&sys_props, 0, sizeof(sys_props));
9895b5c4e40SEvgeny Pinchuk 
990520b8fb7SFelix Kuehling 	/* Proximity domains in ACPI CRAT tables start counting at
991520b8fb7SFelix Kuehling 	 * 0. The same should be true for virtual CRAT tables created
992520b8fb7SFelix Kuehling 	 * at this stage. GPUs added later in kfd_topology_add_device
993520b8fb7SFelix Kuehling 	 * use a counter.
994520b8fb7SFelix Kuehling 	 */
995520b8fb7SFelix Kuehling 	proximity_domain = 0;
996520b8fb7SFelix Kuehling 
9975b5c4e40SEvgeny Pinchuk 	/*
998520b8fb7SFelix Kuehling 	 * Get the CRAT image from the ACPI. If ACPI doesn't have one
999b441093eSHarish Kasiviswanathan 	 * or if ACPI CRAT is invalid create a virtual CRAT.
1000520b8fb7SFelix Kuehling 	 * NOTE: The current implementation expects all AMD APUs to have
1001520b8fb7SFelix Kuehling 	 *	CRAT. If no CRAT is available, it is assumed to be a CPU
10025b5c4e40SEvgeny Pinchuk 	 */
10038e05247dSHarish Kasiviswanathan 	ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
10048e05247dSHarish Kasiviswanathan 	if (!ret) {
10054f449311SHarish Kasiviswanathan 		ret = kfd_parse_crat_table(crat_image,
1006520b8fb7SFelix Kuehling 					   &temp_topology_device_list,
1007520b8fb7SFelix Kuehling 					   proximity_domain);
1008b441093eSHarish Kasiviswanathan 		if (ret ||
1009b441093eSHarish Kasiviswanathan 		    kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
1010520b8fb7SFelix Kuehling 			kfd_release_topology_device_list(
1011520b8fb7SFelix Kuehling 				&temp_topology_device_list);
1012520b8fb7SFelix Kuehling 			kfd_destroy_crat_image(crat_image);
1013520b8fb7SFelix Kuehling 			crat_image = NULL;
1014520b8fb7SFelix Kuehling 		}
1015520b8fb7SFelix Kuehling 	}
1016520b8fb7SFelix Kuehling 
1017520b8fb7SFelix Kuehling 	if (!crat_image) {
1018520b8fb7SFelix Kuehling 		ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
1019520b8fb7SFelix Kuehling 						    COMPUTE_UNIT_CPU, NULL,
1020520b8fb7SFelix Kuehling 						    proximity_domain);
1021520b8fb7SFelix Kuehling 		cpu_only_node = 1;
1022520b8fb7SFelix Kuehling 		if (ret) {
1023520b8fb7SFelix Kuehling 			pr_err("Error creating VCRAT table for CPU\n");
1024520b8fb7SFelix Kuehling 			return ret;
1025520b8fb7SFelix Kuehling 		}
1026520b8fb7SFelix Kuehling 
1027520b8fb7SFelix Kuehling 		ret = kfd_parse_crat_table(crat_image,
1028520b8fb7SFelix Kuehling 					   &temp_topology_device_list,
1029520b8fb7SFelix Kuehling 					   proximity_domain);
1030520b8fb7SFelix Kuehling 		if (ret) {
1031520b8fb7SFelix Kuehling 			pr_err("Error parsing VCRAT table for CPU\n");
10328e05247dSHarish Kasiviswanathan 			goto err;
1033520b8fb7SFelix Kuehling 		}
10345b5c4e40SEvgeny Pinchuk 	}
10355b5c4e40SEvgeny Pinchuk 
1036f4757347SAmber Lin 	kdev = list_first_entry(&temp_topology_device_list,
1037f4757347SAmber Lin 				struct kfd_topology_device, list);
1038f4757347SAmber Lin 	kfd_add_perf_to_topology(kdev);
1039f4757347SAmber Lin 
10405b5c4e40SEvgeny Pinchuk 	down_write(&topology_lock);
10414f449311SHarish Kasiviswanathan 	kfd_topology_update_device_list(&temp_topology_device_list,
10424f449311SHarish Kasiviswanathan 					&topology_device_list);
1043520b8fb7SFelix Kuehling 	atomic_set(&topology_crat_proximity_domain, sys_props.num_devices-1);
10445b5c4e40SEvgeny Pinchuk 	ret = kfd_topology_update_sysfs();
10455b5c4e40SEvgeny Pinchuk 	up_write(&topology_lock);
10468e05247dSHarish Kasiviswanathan 
10474f449311SHarish Kasiviswanathan 	if (!ret) {
10484f449311SHarish Kasiviswanathan 		sys_props.generation_count++;
1049520b8fb7SFelix Kuehling 		kfd_update_system_properties();
1050520b8fb7SFelix Kuehling 		kfd_debug_print_topology();
10518e05247dSHarish Kasiviswanathan 		pr_info("Finished initializing topology\n");
10524f449311SHarish Kasiviswanathan 	} else
10538e05247dSHarish Kasiviswanathan 		pr_err("Failed to update topology in sysfs ret=%d\n", ret);
10545b5c4e40SEvgeny Pinchuk 
1055520b8fb7SFelix Kuehling 	/* For nodes with GPU, this information gets added
1056520b8fb7SFelix Kuehling 	 * when GPU is detected (kfd_topology_add_device).
1057520b8fb7SFelix Kuehling 	 */
1058520b8fb7SFelix Kuehling 	if (cpu_only_node) {
1059520b8fb7SFelix Kuehling 		/* Add additional information to CPU only node created above */
1060520b8fb7SFelix Kuehling 		down_write(&topology_lock);
1061520b8fb7SFelix Kuehling 		kdev = list_first_entry(&topology_device_list,
1062520b8fb7SFelix Kuehling 				struct kfd_topology_device, list);
1063520b8fb7SFelix Kuehling 		up_write(&topology_lock);
1064520b8fb7SFelix Kuehling 		kfd_add_non_crat_information(kdev);
1065520b8fb7SFelix Kuehling 	}
1066520b8fb7SFelix Kuehling 
10675b5c4e40SEvgeny Pinchuk err:
10688e05247dSHarish Kasiviswanathan 	kfd_destroy_crat_image(crat_image);
10695b5c4e40SEvgeny Pinchuk 	return ret;
10705b5c4e40SEvgeny Pinchuk }
10715b5c4e40SEvgeny Pinchuk 
10725b5c4e40SEvgeny Pinchuk void kfd_topology_shutdown(void)
10735b5c4e40SEvgeny Pinchuk {
10744f449311SHarish Kasiviswanathan 	down_write(&topology_lock);
10755b5c4e40SEvgeny Pinchuk 	kfd_topology_release_sysfs();
10765b5c4e40SEvgeny Pinchuk 	kfd_release_live_view();
10774f449311SHarish Kasiviswanathan 	up_write(&topology_lock);
10785b5c4e40SEvgeny Pinchuk }
10795b5c4e40SEvgeny Pinchuk 
10805b5c4e40SEvgeny Pinchuk static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
10815b5c4e40SEvgeny Pinchuk {
10825b5c4e40SEvgeny Pinchuk 	uint32_t hashout;
10835b5c4e40SEvgeny Pinchuk 	uint32_t buf[7];
1084585f0e6cSEdward O'Callaghan 	uint64_t local_mem_size;
10855b5c4e40SEvgeny Pinchuk 	int i;
10860504cccfSHarish Kasiviswanathan 	struct kfd_local_mem_info local_mem_info;
10875b5c4e40SEvgeny Pinchuk 
10885b5c4e40SEvgeny Pinchuk 	if (!gpu)
10895b5c4e40SEvgeny Pinchuk 		return 0;
10905b5c4e40SEvgeny Pinchuk 
10917cd52c91SAmber Lin 	amdgpu_amdkfd_get_local_mem_info(gpu->kgd, &local_mem_info);
10920504cccfSHarish Kasiviswanathan 
10930504cccfSHarish Kasiviswanathan 	local_mem_size = local_mem_info.local_mem_size_private +
10940504cccfSHarish Kasiviswanathan 			local_mem_info.local_mem_size_public;
1095585f0e6cSEdward O'Callaghan 
10965b5c4e40SEvgeny Pinchuk 	buf[0] = gpu->pdev->devfn;
109746096058SAmber Lin 	buf[1] = gpu->pdev->subsystem_vendor |
109846096058SAmber Lin 		(gpu->pdev->subsystem_device << 16);
109946096058SAmber Lin 	buf[2] = pci_domain_nr(gpu->pdev->bus);
11005b5c4e40SEvgeny Pinchuk 	buf[3] = gpu->pdev->device;
11015b5c4e40SEvgeny Pinchuk 	buf[4] = gpu->pdev->bus->number;
1102585f0e6cSEdward O'Callaghan 	buf[5] = lower_32_bits(local_mem_size);
1103585f0e6cSEdward O'Callaghan 	buf[6] = upper_32_bits(local_mem_size);
11045b5c4e40SEvgeny Pinchuk 
11055b5c4e40SEvgeny Pinchuk 	for (i = 0, hashout = 0; i < 7; i++)
11065b5c4e40SEvgeny Pinchuk 		hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
11075b5c4e40SEvgeny Pinchuk 
11085b5c4e40SEvgeny Pinchuk 	return hashout;
11095b5c4e40SEvgeny Pinchuk }
11103a87177eSHarish Kasiviswanathan /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
11113a87177eSHarish Kasiviswanathan  *		the GPU device is not already present in the topology device
11123a87177eSHarish Kasiviswanathan  *		list then return NULL. This means a new topology device has to
11133a87177eSHarish Kasiviswanathan  *		be created for this GPU.
11143a87177eSHarish Kasiviswanathan  */
11155b5c4e40SEvgeny Pinchuk static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
11165b5c4e40SEvgeny Pinchuk {
11175b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
111816b9201cSOded Gabbay 	struct kfd_topology_device *out_dev = NULL;
1119171bc67eSHarish Kasiviswanathan 	struct kfd_mem_properties *mem;
1120171bc67eSHarish Kasiviswanathan 	struct kfd_cache_properties *cache;
1121171bc67eSHarish Kasiviswanathan 	struct kfd_iolink_properties *iolink;
11225b5c4e40SEvgeny Pinchuk 
11233a87177eSHarish Kasiviswanathan 	down_write(&topology_lock);
1124b8fe0524SFelix Kuehling 	list_for_each_entry(dev, &topology_device_list, list) {
1125b8fe0524SFelix Kuehling 		/* Discrete GPUs need their own topology device list
1126b8fe0524SFelix Kuehling 		 * entries. Don't assign them to CPU/APU nodes.
1127b8fe0524SFelix Kuehling 		 */
1128b8fe0524SFelix Kuehling 		if (!gpu->device_info->needs_iommu_device &&
1129b8fe0524SFelix Kuehling 		    dev->node_props.cpu_cores_count)
1130b8fe0524SFelix Kuehling 			continue;
1131b8fe0524SFelix Kuehling 
11324eacc26bSKent Russell 		if (!dev->gpu && (dev->node_props.simd_count > 0)) {
11335b5c4e40SEvgeny Pinchuk 			dev->gpu = gpu;
11345b5c4e40SEvgeny Pinchuk 			out_dev = dev;
1135171bc67eSHarish Kasiviswanathan 
1136171bc67eSHarish Kasiviswanathan 			list_for_each_entry(mem, &dev->mem_props, list)
1137171bc67eSHarish Kasiviswanathan 				mem->gpu = dev->gpu;
1138171bc67eSHarish Kasiviswanathan 			list_for_each_entry(cache, &dev->cache_props, list)
1139171bc67eSHarish Kasiviswanathan 				cache->gpu = dev->gpu;
1140171bc67eSHarish Kasiviswanathan 			list_for_each_entry(iolink, &dev->io_link_props, list)
1141171bc67eSHarish Kasiviswanathan 				iolink->gpu = dev->gpu;
11425b5c4e40SEvgeny Pinchuk 			break;
11435b5c4e40SEvgeny Pinchuk 		}
1144b8fe0524SFelix Kuehling 	}
11453a87177eSHarish Kasiviswanathan 	up_write(&topology_lock);
11465b5c4e40SEvgeny Pinchuk 	return out_dev;
11475b5c4e40SEvgeny Pinchuk }
11485b5c4e40SEvgeny Pinchuk 
11495b5c4e40SEvgeny Pinchuk static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
11505b5c4e40SEvgeny Pinchuk {
11515b5c4e40SEvgeny Pinchuk 	/*
11525b5c4e40SEvgeny Pinchuk 	 * TODO: Generate an event for thunk about the arrival/removal
11535b5c4e40SEvgeny Pinchuk 	 * of the GPU
11545b5c4e40SEvgeny Pinchuk 	 */
11555b5c4e40SEvgeny Pinchuk }
11565b5c4e40SEvgeny Pinchuk 
11573a87177eSHarish Kasiviswanathan /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
11583a87177eSHarish Kasiviswanathan  *		patch this after CRAT parsing.
11593a87177eSHarish Kasiviswanathan  */
11603a87177eSHarish Kasiviswanathan static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
11613a87177eSHarish Kasiviswanathan {
11623a87177eSHarish Kasiviswanathan 	struct kfd_mem_properties *mem;
11633a87177eSHarish Kasiviswanathan 	struct kfd_local_mem_info local_mem_info;
11643a87177eSHarish Kasiviswanathan 
11653a87177eSHarish Kasiviswanathan 	if (!dev)
11663a87177eSHarish Kasiviswanathan 		return;
11673a87177eSHarish Kasiviswanathan 
11683a87177eSHarish Kasiviswanathan 	/* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
11693a87177eSHarish Kasiviswanathan 	 * single bank of VRAM local memory.
11703a87177eSHarish Kasiviswanathan 	 * for dGPUs - VCRAT reports only one bank of Local Memory
11713a87177eSHarish Kasiviswanathan 	 * for APUs - If CRAT from ACPI reports more than one bank, then
11723a87177eSHarish Kasiviswanathan 	 *	all the banks will report the same mem_clk_max information
11733a87177eSHarish Kasiviswanathan 	 */
11747cd52c91SAmber Lin 	amdgpu_amdkfd_get_local_mem_info(dev->gpu->kgd, &local_mem_info);
11753a87177eSHarish Kasiviswanathan 
11763a87177eSHarish Kasiviswanathan 	list_for_each_entry(mem, &dev->mem_props, list)
11773a87177eSHarish Kasiviswanathan 		mem->mem_clk_max = local_mem_info.mem_clk_max;
11783a87177eSHarish Kasiviswanathan }
11793a87177eSHarish Kasiviswanathan 
11803a87177eSHarish Kasiviswanathan static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
11813a87177eSHarish Kasiviswanathan {
1182d35f00d8SEric Huang 	struct kfd_iolink_properties *link, *cpu_link;
1183d35f00d8SEric Huang 	struct kfd_topology_device *cpu_dev;
1184d35f00d8SEric Huang 	uint32_t cap;
1185d35f00d8SEric Huang 	uint32_t cpu_flag = CRAT_IOLINK_FLAGS_ENABLED;
1186d35f00d8SEric Huang 	uint32_t flag = CRAT_IOLINK_FLAGS_ENABLED;
11873a87177eSHarish Kasiviswanathan 
11883a87177eSHarish Kasiviswanathan 	if (!dev || !dev->gpu)
11893a87177eSHarish Kasiviswanathan 		return;
11903a87177eSHarish Kasiviswanathan 
1191d35f00d8SEric Huang 	pcie_capability_read_dword(dev->gpu->pdev,
1192d35f00d8SEric Huang 			PCI_EXP_DEVCAP2, &cap);
1193d35f00d8SEric Huang 
1194d35f00d8SEric Huang 	if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1195d35f00d8SEric Huang 		     PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1196d35f00d8SEric Huang 		cpu_flag |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
11973a87177eSHarish Kasiviswanathan 			CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1198d35f00d8SEric Huang 
1199d35f00d8SEric Huang 	if (!dev->gpu->pci_atomic_requested ||
1200d35f00d8SEric Huang 	    dev->gpu->device_info->asic_family == CHIP_HAWAII)
1201d35f00d8SEric Huang 		flag |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1202d35f00d8SEric Huang 			CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1203d35f00d8SEric Huang 
1204d35f00d8SEric Huang 	/* GPU only creates direct links so apply flags setting to all */
1205d35f00d8SEric Huang 	list_for_each_entry(link, &dev->io_link_props, list) {
1206d35f00d8SEric Huang 		link->flags = flag;
1207d35f00d8SEric Huang 		cpu_dev = kfd_topology_device_by_proximity_domain(
1208d35f00d8SEric Huang 				link->node_to);
1209d35f00d8SEric Huang 		if (cpu_dev) {
1210d35f00d8SEric Huang 			list_for_each_entry(cpu_link,
1211d35f00d8SEric Huang 					    &cpu_dev->io_link_props, list)
1212d35f00d8SEric Huang 				if (cpu_link->node_to == link->node_from)
1213d35f00d8SEric Huang 					cpu_link->flags = cpu_flag;
1214d35f00d8SEric Huang 		}
1215d35f00d8SEric Huang 	}
12163a87177eSHarish Kasiviswanathan }
12173a87177eSHarish Kasiviswanathan 
12185b5c4e40SEvgeny Pinchuk int kfd_topology_add_device(struct kfd_dev *gpu)
12195b5c4e40SEvgeny Pinchuk {
12205b5c4e40SEvgeny Pinchuk 	uint32_t gpu_id;
12215b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
1222f7ce2fadSFlora Cui 	struct kfd_cu_info cu_info;
12234f449311SHarish Kasiviswanathan 	int res = 0;
12244f449311SHarish Kasiviswanathan 	struct list_head temp_topology_device_list;
12253a87177eSHarish Kasiviswanathan 	void *crat_image = NULL;
12263a87177eSHarish Kasiviswanathan 	size_t image_size = 0;
12273a87177eSHarish Kasiviswanathan 	int proximity_domain;
12280dee45a2SEric Huang 	struct amdgpu_ras *ctx;
12294f449311SHarish Kasiviswanathan 
12304f449311SHarish Kasiviswanathan 	INIT_LIST_HEAD(&temp_topology_device_list);
12315b5c4e40SEvgeny Pinchuk 
12325b5c4e40SEvgeny Pinchuk 	gpu_id = kfd_generate_gpu_id(gpu);
12335b5c4e40SEvgeny Pinchuk 
123479775b62SKent Russell 	pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
12355b5c4e40SEvgeny Pinchuk 
12363a87177eSHarish Kasiviswanathan 	proximity_domain = atomic_inc_return(&topology_crat_proximity_domain);
12373a87177eSHarish Kasiviswanathan 
12383a87177eSHarish Kasiviswanathan 	/* Check to see if this gpu device exists in the topology_device_list.
12393a87177eSHarish Kasiviswanathan 	 * If so, assign the gpu to that device,
12403a87177eSHarish Kasiviswanathan 	 * else create a Virtual CRAT for this gpu device and then parse that
12413a87177eSHarish Kasiviswanathan 	 * CRAT to create a new topology device. Once created assign the gpu to
12423a87177eSHarish Kasiviswanathan 	 * that topology device
12435b5c4e40SEvgeny Pinchuk 	 */
12445b5c4e40SEvgeny Pinchuk 	dev = kfd_assign_gpu(gpu);
12455b5c4e40SEvgeny Pinchuk 	if (!dev) {
12463a87177eSHarish Kasiviswanathan 		res = kfd_create_crat_image_virtual(&crat_image, &image_size,
12473a87177eSHarish Kasiviswanathan 						    COMPUTE_UNIT_GPU, gpu,
12483a87177eSHarish Kasiviswanathan 						    proximity_domain);
12493a87177eSHarish Kasiviswanathan 		if (res) {
12503a87177eSHarish Kasiviswanathan 			pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
12513a87177eSHarish Kasiviswanathan 			       gpu_id);
12523a87177eSHarish Kasiviswanathan 			return res;
12533a87177eSHarish Kasiviswanathan 		}
12543a87177eSHarish Kasiviswanathan 		res = kfd_parse_crat_table(crat_image,
12553a87177eSHarish Kasiviswanathan 					   &temp_topology_device_list,
12563a87177eSHarish Kasiviswanathan 					   proximity_domain);
12573a87177eSHarish Kasiviswanathan 		if (res) {
12583a87177eSHarish Kasiviswanathan 			pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
12593a87177eSHarish Kasiviswanathan 			       gpu_id);
12605b5c4e40SEvgeny Pinchuk 			goto err;
12615b5c4e40SEvgeny Pinchuk 		}
12624f449311SHarish Kasiviswanathan 
12634f449311SHarish Kasiviswanathan 		down_write(&topology_lock);
12644f449311SHarish Kasiviswanathan 		kfd_topology_update_device_list(&temp_topology_device_list,
12654f449311SHarish Kasiviswanathan 			&topology_device_list);
12664f449311SHarish Kasiviswanathan 
12678eabaf54SKent Russell 		/* Update the SYSFS tree, since we added another topology
12688eabaf54SKent Russell 		 * device
12695b5c4e40SEvgeny Pinchuk 		 */
12703a87177eSHarish Kasiviswanathan 		res = kfd_topology_update_sysfs();
12714f449311SHarish Kasiviswanathan 		up_write(&topology_lock);
12724f449311SHarish Kasiviswanathan 
12733a87177eSHarish Kasiviswanathan 		if (!res)
12743a87177eSHarish Kasiviswanathan 			sys_props.generation_count++;
12753a87177eSHarish Kasiviswanathan 		else
12763a87177eSHarish Kasiviswanathan 			pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
12773a87177eSHarish Kasiviswanathan 						gpu_id, res);
12783a87177eSHarish Kasiviswanathan 		dev = kfd_assign_gpu(gpu);
12793a87177eSHarish Kasiviswanathan 		if (WARN_ON(!dev)) {
12803a87177eSHarish Kasiviswanathan 			res = -ENODEV;
12813a87177eSHarish Kasiviswanathan 			goto err;
12823a87177eSHarish Kasiviswanathan 		}
12835b5c4e40SEvgeny Pinchuk 	}
12845b5c4e40SEvgeny Pinchuk 
12855b5c4e40SEvgeny Pinchuk 	dev->gpu_id = gpu_id;
12865b5c4e40SEvgeny Pinchuk 	gpu->id = gpu_id;
12873a87177eSHarish Kasiviswanathan 
12883a87177eSHarish Kasiviswanathan 	/* TODO: Move the following lines to function
12893a87177eSHarish Kasiviswanathan 	 *	kfd_add_non_crat_information
12903a87177eSHarish Kasiviswanathan 	 */
12913a87177eSHarish Kasiviswanathan 
12923a87177eSHarish Kasiviswanathan 	/* Fill-in additional information that is not available in CRAT but
12933a87177eSHarish Kasiviswanathan 	 * needed for the topology
12943a87177eSHarish Kasiviswanathan 	 */
12953a87177eSHarish Kasiviswanathan 
12967cd52c91SAmber Lin 	amdgpu_amdkfd_get_cu_info(dev->gpu->kgd, &cu_info);
1297c181159aSYong Zhao 
1298c181159aSYong Zhao 	strncpy(dev->node_props.name, gpu->device_info->asic_name,
1299c181159aSYong Zhao 			KFD_TOPOLOGY_PUBLIC_NAME_SIZE);
1300c181159aSYong Zhao 
13013a87177eSHarish Kasiviswanathan 	dev->node_props.simd_arrays_per_engine =
13023a87177eSHarish Kasiviswanathan 		cu_info.num_shader_arrays_per_engine;
13033a87177eSHarish Kasiviswanathan 
13045b5c4e40SEvgeny Pinchuk 	dev->node_props.vendor_id = gpu->pdev->vendor;
13055b5c4e40SEvgeny Pinchuk 	dev->node_props.device_id = gpu->pdev->device;
1306babe2ef3SHeiner Kallweit 	dev->node_props.location_id = pci_dev_id(gpu->pdev);
13073a87177eSHarish Kasiviswanathan 	dev->node_props.max_engine_clk_fcompute =
13087cd52c91SAmber Lin 		amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->kgd);
13093a87177eSHarish Kasiviswanathan 	dev->node_props.max_engine_clk_ccompute =
13103a87177eSHarish Kasiviswanathan 		cpufreq_quick_get_max(0) / 1000;
13117c9b7171SOak Zeng 	dev->node_props.drm_render_minor =
13127c9b7171SOak Zeng 		gpu->shared_resources.drm_render_minor;
13135b5c4e40SEvgeny Pinchuk 
13140c1690e3SShaoyun Liu 	dev->node_props.hive_id = gpu->hive_id;
131514568cf6SOak Zeng 	dev->node_props.num_sdma_engines = gpu->device_info->num_sdma_engines;
131614568cf6SOak Zeng 	dev->node_props.num_sdma_xgmi_engines =
131714568cf6SOak Zeng 				gpu->device_info->num_xgmi_sdma_engines;
1318bb71c74dSHuang Rui 	dev->node_props.num_sdma_queues_per_engine =
1319bb71c74dSHuang Rui 				gpu->device_info->num_sdma_queues_per_engine;
132029e76462SOak Zeng 	dev->node_props.num_gws = (hws_gws_support &&
132129e76462SOak Zeng 		dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
132229e76462SOak Zeng 		amdgpu_amdkfd_get_num_gws(dev->gpu->kgd) : 0;
1323e6945304SYong Zhao 	dev->node_props.num_cp_queues = get_cp_queues_num(dev->gpu->dqm);
13240c663695SDivya Shikre 	dev->node_props.unique_id = gpu->unique_id;
13250c1690e3SShaoyun Liu 
13263a87177eSHarish Kasiviswanathan 	kfd_fill_mem_clk_max_info(dev);
13273a87177eSHarish Kasiviswanathan 	kfd_fill_iolink_non_crat_info(dev);
13283a87177eSHarish Kasiviswanathan 
13293a87177eSHarish Kasiviswanathan 	switch (dev->gpu->device_info->asic_family) {
13303a87177eSHarish Kasiviswanathan 	case CHIP_KAVERI:
13313a87177eSHarish Kasiviswanathan 	case CHIP_HAWAII:
13323a87177eSHarish Kasiviswanathan 	case CHIP_TONGA:
13333a87177eSHarish Kasiviswanathan 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
13343a87177eSHarish Kasiviswanathan 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
13353a87177eSHarish Kasiviswanathan 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
13363a87177eSHarish Kasiviswanathan 		break;
13373a87177eSHarish Kasiviswanathan 	case CHIP_CARRIZO:
13383a87177eSHarish Kasiviswanathan 	case CHIP_FIJI:
13393a87177eSHarish Kasiviswanathan 	case CHIP_POLARIS10:
13403a87177eSHarish Kasiviswanathan 	case CHIP_POLARIS11:
1341846a44d7SGang Ba 	case CHIP_POLARIS12:
1342ed81cd6eSKent Russell 	case CHIP_VEGAM:
134342aa8793SFelix Kuehling 		pr_debug("Adding doorbell packet type capability\n");
13443a87177eSHarish Kasiviswanathan 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
13453a87177eSHarish Kasiviswanathan 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
13463a87177eSHarish Kasiviswanathan 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
13473a87177eSHarish Kasiviswanathan 		break;
1348389056e5SFelix Kuehling 	case CHIP_VEGA10:
1349846a44d7SGang Ba 	case CHIP_VEGA12:
135022a3a294SShaoyun Liu 	case CHIP_VEGA20:
1351389056e5SFelix Kuehling 	case CHIP_RAVEN:
1352f5d843d4SHuang Rui 	case CHIP_RENOIR:
135349adcf8aSYong Zhao 	case CHIP_ARCTURUS:
135414328aa5SPhilip Cox 	case CHIP_NAVI10:
13550e94b564Sshaoyunl 	case CHIP_NAVI12:
13568099ae40SYong Zhao 	case CHIP_NAVI14:
1357389056e5SFelix Kuehling 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1358389056e5SFelix Kuehling 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1359389056e5SFelix Kuehling 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1360389056e5SFelix Kuehling 		break;
13613a87177eSHarish Kasiviswanathan 	default:
13623a87177eSHarish Kasiviswanathan 		WARN(1, "Unexpected ASIC family %u",
13633a87177eSHarish Kasiviswanathan 		     dev->gpu->device_info->asic_family);
13647639a8c4SBen Goz 	}
13657639a8c4SBen Goz 
13661ae99eabSOak Zeng 	/*
13671ae99eabSOak Zeng 	* Overwrite ATS capability according to needs_iommu_device to fix
13681ae99eabSOak Zeng 	* potential missing corresponding bit in CRAT of BIOS.
13691ae99eabSOak Zeng 	*/
13701ae99eabSOak Zeng 	if (dev->gpu->device_info->needs_iommu_device)
13711ae99eabSOak Zeng 		dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
13721ae99eabSOak Zeng 	else
13731ae99eabSOak Zeng 		dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;
13741ae99eabSOak Zeng 
13753a87177eSHarish Kasiviswanathan 	/* Fix errors in CZ CRAT.
13763a87177eSHarish Kasiviswanathan 	 * simd_count: Carrizo CRAT reports wrong simd_count, probably
13773a87177eSHarish Kasiviswanathan 	 *		because it doesn't consider masked out CUs
137870f372bfSPhilip Cox 	 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
13793a87177eSHarish Kasiviswanathan 	 */
138070f372bfSPhilip Cox 	if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
13813a87177eSHarish Kasiviswanathan 		dev->node_props.simd_count =
13823a87177eSHarish Kasiviswanathan 			cu_info.simd_per_cu * cu_info.cu_active_number;
138370f372bfSPhilip Cox 		dev->node_props.max_waves_per_simd = 10;
138470f372bfSPhilip Cox 	}
13853a87177eSHarish Kasiviswanathan 
13860dee45a2SEric Huang 	ctx = amdgpu_ras_get_context((struct amdgpu_device *)(dev->gpu->kgd));
13870dee45a2SEric Huang 	if (ctx) {
13880dee45a2SEric Huang 		/* kfd only concerns sram ecc on GFX/SDMA and HBM ecc on UMC */
13890dee45a2SEric Huang 		dev->node_props.capability |=
13900dee45a2SEric Huang 			(((ctx->features & BIT(AMDGPU_RAS_BLOCK__SDMA)) != 0) ||
13910dee45a2SEric Huang 			 ((ctx->features & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0)) ?
13920dee45a2SEric Huang 			HSA_CAP_SRAM_EDCSUPPORTED : 0;
13930dee45a2SEric Huang 		dev->node_props.capability |= ((ctx->features & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
13940dee45a2SEric Huang 			HSA_CAP_MEM_EDCSUPPORTED : 0;
13950dee45a2SEric Huang 
13960dee45a2SEric Huang 		dev->node_props.capability |= (ctx->features != 0) ?
13970dee45a2SEric Huang 			HSA_CAP_RASEVENTNOTIFY : 0;
13980dee45a2SEric Huang 	}
13990dee45a2SEric Huang 
14003a87177eSHarish Kasiviswanathan 	kfd_debug_print_topology();
14013a87177eSHarish Kasiviswanathan 
14024f449311SHarish Kasiviswanathan 	if (!res)
14035b5c4e40SEvgeny Pinchuk 		kfd_notify_gpu_change(gpu_id, 1);
14044f449311SHarish Kasiviswanathan err:
14053a87177eSHarish Kasiviswanathan 	kfd_destroy_crat_image(crat_image);
14065b5c4e40SEvgeny Pinchuk 	return res;
14075b5c4e40SEvgeny Pinchuk }
14085b5c4e40SEvgeny Pinchuk 
14095b5c4e40SEvgeny Pinchuk int kfd_topology_remove_device(struct kfd_dev *gpu)
14105b5c4e40SEvgeny Pinchuk {
14114f449311SHarish Kasiviswanathan 	struct kfd_topology_device *dev, *tmp;
14125b5c4e40SEvgeny Pinchuk 	uint32_t gpu_id;
14135b5c4e40SEvgeny Pinchuk 	int res = -ENODEV;
14145b5c4e40SEvgeny Pinchuk 
14155b5c4e40SEvgeny Pinchuk 	down_write(&topology_lock);
14165b5c4e40SEvgeny Pinchuk 
14174f449311SHarish Kasiviswanathan 	list_for_each_entry_safe(dev, tmp, &topology_device_list, list)
14185b5c4e40SEvgeny Pinchuk 		if (dev->gpu == gpu) {
14195b5c4e40SEvgeny Pinchuk 			gpu_id = dev->gpu_id;
14205b5c4e40SEvgeny Pinchuk 			kfd_remove_sysfs_node_entry(dev);
14215b5c4e40SEvgeny Pinchuk 			kfd_release_topology_device(dev);
14224f449311SHarish Kasiviswanathan 			sys_props.num_devices--;
14235b5c4e40SEvgeny Pinchuk 			res = 0;
14245b5c4e40SEvgeny Pinchuk 			if (kfd_topology_update_sysfs() < 0)
14255b5c4e40SEvgeny Pinchuk 				kfd_topology_release_sysfs();
14265b5c4e40SEvgeny Pinchuk 			break;
14275b5c4e40SEvgeny Pinchuk 		}
14285b5c4e40SEvgeny Pinchuk 
14295b5c4e40SEvgeny Pinchuk 	up_write(&topology_lock);
14305b5c4e40SEvgeny Pinchuk 
1431174de876SFelix Kuehling 	if (!res)
14325b5c4e40SEvgeny Pinchuk 		kfd_notify_gpu_change(gpu_id, 0);
14335b5c4e40SEvgeny Pinchuk 
14345b5c4e40SEvgeny Pinchuk 	return res;
14355b5c4e40SEvgeny Pinchuk }
14365b5c4e40SEvgeny Pinchuk 
14376d82eb0eSHarish Kasiviswanathan /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
14386d82eb0eSHarish Kasiviswanathan  *	topology. If GPU device is found @idx, then valid kfd_dev pointer is
14396d82eb0eSHarish Kasiviswanathan  *	returned through @kdev
14406d82eb0eSHarish Kasiviswanathan  * Return -	0: On success (@kdev will be NULL for non GPU nodes)
14416d82eb0eSHarish Kasiviswanathan  *		-1: If end of list
14425b5c4e40SEvgeny Pinchuk  */
14436d82eb0eSHarish Kasiviswanathan int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
14445b5c4e40SEvgeny Pinchuk {
14455b5c4e40SEvgeny Pinchuk 
14465b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *top_dev;
14475b5c4e40SEvgeny Pinchuk 	uint8_t device_idx = 0;
14485b5c4e40SEvgeny Pinchuk 
14496d82eb0eSHarish Kasiviswanathan 	*kdev = NULL;
14505b5c4e40SEvgeny Pinchuk 	down_read(&topology_lock);
14515b5c4e40SEvgeny Pinchuk 
14525b5c4e40SEvgeny Pinchuk 	list_for_each_entry(top_dev, &topology_device_list, list) {
14535b5c4e40SEvgeny Pinchuk 		if (device_idx == idx) {
14546d82eb0eSHarish Kasiviswanathan 			*kdev = top_dev->gpu;
14556d82eb0eSHarish Kasiviswanathan 			up_read(&topology_lock);
14566d82eb0eSHarish Kasiviswanathan 			return 0;
14575b5c4e40SEvgeny Pinchuk 		}
14585b5c4e40SEvgeny Pinchuk 
14595b5c4e40SEvgeny Pinchuk 		device_idx++;
14605b5c4e40SEvgeny Pinchuk 	}
14615b5c4e40SEvgeny Pinchuk 
14625b5c4e40SEvgeny Pinchuk 	up_read(&topology_lock);
14635b5c4e40SEvgeny Pinchuk 
14646d82eb0eSHarish Kasiviswanathan 	return -1;
14655b5c4e40SEvgeny Pinchuk 
14665b5c4e40SEvgeny Pinchuk }
1467851a645eSFelix Kuehling 
1468520b8fb7SFelix Kuehling static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
1469520b8fb7SFelix Kuehling {
1470520b8fb7SFelix Kuehling 	int first_cpu_of_numa_node;
1471520b8fb7SFelix Kuehling 
1472520b8fb7SFelix Kuehling 	if (!cpumask || cpumask == cpu_none_mask)
1473520b8fb7SFelix Kuehling 		return -1;
1474520b8fb7SFelix Kuehling 	first_cpu_of_numa_node = cpumask_first(cpumask);
1475520b8fb7SFelix Kuehling 	if (first_cpu_of_numa_node >= nr_cpu_ids)
1476520b8fb7SFelix Kuehling 		return -1;
1477df1dd4f4SFelix Kuehling #ifdef CONFIG_X86_64
1478df1dd4f4SFelix Kuehling 	return cpu_data(first_cpu_of_numa_node).apicid;
1479df1dd4f4SFelix Kuehling #else
1480df1dd4f4SFelix Kuehling 	return first_cpu_of_numa_node;
1481df1dd4f4SFelix Kuehling #endif
1482520b8fb7SFelix Kuehling }
1483520b8fb7SFelix Kuehling 
1484520b8fb7SFelix Kuehling /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
1485520b8fb7SFelix Kuehling  *	of the given NUMA node (numa_node_id)
1486520b8fb7SFelix Kuehling  * Return -1 on failure
1487520b8fb7SFelix Kuehling  */
1488520b8fb7SFelix Kuehling int kfd_numa_node_to_apic_id(int numa_node_id)
1489520b8fb7SFelix Kuehling {
1490520b8fb7SFelix Kuehling 	if (numa_node_id == -1) {
1491520b8fb7SFelix Kuehling 		pr_warn("Invalid NUMA Node. Use online CPU mask\n");
1492520b8fb7SFelix Kuehling 		return kfd_cpumask_to_apic_id(cpu_online_mask);
1493520b8fb7SFelix Kuehling 	}
1494520b8fb7SFelix Kuehling 	return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
1495520b8fb7SFelix Kuehling }
1496520b8fb7SFelix Kuehling 
1497851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS)
1498851a645eSFelix Kuehling 
1499851a645eSFelix Kuehling int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
1500851a645eSFelix Kuehling {
1501851a645eSFelix Kuehling 	struct kfd_topology_device *dev;
1502851a645eSFelix Kuehling 	unsigned int i = 0;
1503851a645eSFelix Kuehling 	int r = 0;
1504851a645eSFelix Kuehling 
1505851a645eSFelix Kuehling 	down_read(&topology_lock);
1506851a645eSFelix Kuehling 
1507851a645eSFelix Kuehling 	list_for_each_entry(dev, &topology_device_list, list) {
1508851a645eSFelix Kuehling 		if (!dev->gpu) {
1509851a645eSFelix Kuehling 			i++;
1510851a645eSFelix Kuehling 			continue;
1511851a645eSFelix Kuehling 		}
1512851a645eSFelix Kuehling 
1513851a645eSFelix Kuehling 		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1514851a645eSFelix Kuehling 		r = dqm_debugfs_hqds(m, dev->gpu->dqm);
1515851a645eSFelix Kuehling 		if (r)
1516851a645eSFelix Kuehling 			break;
1517851a645eSFelix Kuehling 	}
1518851a645eSFelix Kuehling 
1519851a645eSFelix Kuehling 	up_read(&topology_lock);
1520851a645eSFelix Kuehling 
1521851a645eSFelix Kuehling 	return r;
1522851a645eSFelix Kuehling }
1523851a645eSFelix Kuehling 
1524851a645eSFelix Kuehling int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
1525851a645eSFelix Kuehling {
1526851a645eSFelix Kuehling 	struct kfd_topology_device *dev;
1527851a645eSFelix Kuehling 	unsigned int i = 0;
1528851a645eSFelix Kuehling 	int r = 0;
1529851a645eSFelix Kuehling 
1530851a645eSFelix Kuehling 	down_read(&topology_lock);
1531851a645eSFelix Kuehling 
1532851a645eSFelix Kuehling 	list_for_each_entry(dev, &topology_device_list, list) {
1533851a645eSFelix Kuehling 		if (!dev->gpu) {
1534851a645eSFelix Kuehling 			i++;
1535851a645eSFelix Kuehling 			continue;
1536851a645eSFelix Kuehling 		}
1537851a645eSFelix Kuehling 
1538851a645eSFelix Kuehling 		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1539851a645eSFelix Kuehling 		r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets);
1540851a645eSFelix Kuehling 		if (r)
1541851a645eSFelix Kuehling 			break;
1542851a645eSFelix Kuehling 	}
1543851a645eSFelix Kuehling 
1544851a645eSFelix Kuehling 	up_read(&topology_lock);
1545851a645eSFelix Kuehling 
1546851a645eSFelix Kuehling 	return r;
1547851a645eSFelix Kuehling }
1548851a645eSFelix Kuehling 
1549851a645eSFelix Kuehling #endif
1550