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"
395b5c4e40SEvgeny Pinchuk 
404f449311SHarish Kasiviswanathan /* topology_device_list - Master list of all topology devices */
414f449311SHarish Kasiviswanathan static struct list_head topology_device_list;
42520b8fb7SFelix Kuehling static struct kfd_system_properties sys_props;
435b5c4e40SEvgeny Pinchuk 
445b5c4e40SEvgeny Pinchuk static DECLARE_RWSEM(topology_lock);
45520b8fb7SFelix Kuehling static atomic_t topology_crat_proximity_domain;
465b5c4e40SEvgeny Pinchuk 
473a87177eSHarish Kasiviswanathan struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
483a87177eSHarish Kasiviswanathan 						uint32_t proximity_domain)
493a87177eSHarish Kasiviswanathan {
503a87177eSHarish Kasiviswanathan 	struct kfd_topology_device *top_dev;
513a87177eSHarish Kasiviswanathan 	struct kfd_topology_device *device = NULL;
523a87177eSHarish Kasiviswanathan 
533a87177eSHarish Kasiviswanathan 	down_read(&topology_lock);
543a87177eSHarish Kasiviswanathan 
553a87177eSHarish Kasiviswanathan 	list_for_each_entry(top_dev, &topology_device_list, list)
563a87177eSHarish Kasiviswanathan 		if (top_dev->proximity_domain == proximity_domain) {
573a87177eSHarish Kasiviswanathan 			device = top_dev;
583a87177eSHarish Kasiviswanathan 			break;
593a87177eSHarish Kasiviswanathan 		}
603a87177eSHarish Kasiviswanathan 
613a87177eSHarish Kasiviswanathan 	up_read(&topology_lock);
623a87177eSHarish Kasiviswanathan 
633a87177eSHarish Kasiviswanathan 	return device;
643a87177eSHarish Kasiviswanathan }
653a87177eSHarish Kasiviswanathan 
665b5c4e40SEvgeny Pinchuk struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
675b5c4e40SEvgeny Pinchuk {
685b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *top_dev;
695b5c4e40SEvgeny Pinchuk 	struct kfd_dev *device = NULL;
705b5c4e40SEvgeny Pinchuk 
715b5c4e40SEvgeny Pinchuk 	down_read(&topology_lock);
725b5c4e40SEvgeny Pinchuk 
735b5c4e40SEvgeny Pinchuk 	list_for_each_entry(top_dev, &topology_device_list, list)
745b5c4e40SEvgeny Pinchuk 		if (top_dev->gpu_id == gpu_id) {
755b5c4e40SEvgeny Pinchuk 			device = top_dev->gpu;
765b5c4e40SEvgeny Pinchuk 			break;
775b5c4e40SEvgeny Pinchuk 		}
785b5c4e40SEvgeny Pinchuk 
795b5c4e40SEvgeny Pinchuk 	up_read(&topology_lock);
805b5c4e40SEvgeny Pinchuk 
815b5c4e40SEvgeny Pinchuk 	return device;
825b5c4e40SEvgeny Pinchuk }
835b5c4e40SEvgeny Pinchuk 
845b5c4e40SEvgeny Pinchuk struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
855b5c4e40SEvgeny Pinchuk {
865b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *top_dev;
875b5c4e40SEvgeny Pinchuk 	struct kfd_dev *device = NULL;
885b5c4e40SEvgeny Pinchuk 
895b5c4e40SEvgeny Pinchuk 	down_read(&topology_lock);
905b5c4e40SEvgeny Pinchuk 
915b5c4e40SEvgeny Pinchuk 	list_for_each_entry(top_dev, &topology_device_list, list)
925b5c4e40SEvgeny Pinchuk 		if (top_dev->gpu->pdev == pdev) {
935b5c4e40SEvgeny Pinchuk 			device = top_dev->gpu;
945b5c4e40SEvgeny Pinchuk 			break;
955b5c4e40SEvgeny Pinchuk 		}
965b5c4e40SEvgeny Pinchuk 
975b5c4e40SEvgeny Pinchuk 	up_read(&topology_lock);
985b5c4e40SEvgeny Pinchuk 
995b5c4e40SEvgeny Pinchuk 	return device;
1005b5c4e40SEvgeny Pinchuk }
1015b5c4e40SEvgeny Pinchuk 
1023a87177eSHarish Kasiviswanathan /* Called with write topology_lock acquired */
1035b5c4e40SEvgeny Pinchuk static void kfd_release_topology_device(struct kfd_topology_device *dev)
1045b5c4e40SEvgeny Pinchuk {
1055b5c4e40SEvgeny Pinchuk 	struct kfd_mem_properties *mem;
1065b5c4e40SEvgeny Pinchuk 	struct kfd_cache_properties *cache;
1075b5c4e40SEvgeny Pinchuk 	struct kfd_iolink_properties *iolink;
108f4757347SAmber Lin 	struct kfd_perf_properties *perf;
1095b5c4e40SEvgeny Pinchuk 
1105b5c4e40SEvgeny Pinchuk 	list_del(&dev->list);
1115b5c4e40SEvgeny Pinchuk 
1125b5c4e40SEvgeny Pinchuk 	while (dev->mem_props.next != &dev->mem_props) {
1135b5c4e40SEvgeny Pinchuk 		mem = container_of(dev->mem_props.next,
1145b5c4e40SEvgeny Pinchuk 				struct kfd_mem_properties, list);
1155b5c4e40SEvgeny Pinchuk 		list_del(&mem->list);
1165b5c4e40SEvgeny Pinchuk 		kfree(mem);
1175b5c4e40SEvgeny Pinchuk 	}
1185b5c4e40SEvgeny Pinchuk 
1195b5c4e40SEvgeny Pinchuk 	while (dev->cache_props.next != &dev->cache_props) {
1205b5c4e40SEvgeny Pinchuk 		cache = container_of(dev->cache_props.next,
1215b5c4e40SEvgeny Pinchuk 				struct kfd_cache_properties, list);
1225b5c4e40SEvgeny Pinchuk 		list_del(&cache->list);
1235b5c4e40SEvgeny Pinchuk 		kfree(cache);
1245b5c4e40SEvgeny Pinchuk 	}
1255b5c4e40SEvgeny Pinchuk 
1265b5c4e40SEvgeny Pinchuk 	while (dev->io_link_props.next != &dev->io_link_props) {
1275b5c4e40SEvgeny Pinchuk 		iolink = container_of(dev->io_link_props.next,
1285b5c4e40SEvgeny Pinchuk 				struct kfd_iolink_properties, list);
1295b5c4e40SEvgeny Pinchuk 		list_del(&iolink->list);
1305b5c4e40SEvgeny Pinchuk 		kfree(iolink);
1315b5c4e40SEvgeny Pinchuk 	}
1325b5c4e40SEvgeny Pinchuk 
133f4757347SAmber Lin 	while (dev->perf_props.next != &dev->perf_props) {
134f4757347SAmber Lin 		perf = container_of(dev->perf_props.next,
135f4757347SAmber Lin 				struct kfd_perf_properties, list);
136f4757347SAmber Lin 		list_del(&perf->list);
137f4757347SAmber Lin 		kfree(perf);
138f4757347SAmber Lin 	}
139f4757347SAmber Lin 
1405b5c4e40SEvgeny Pinchuk 	kfree(dev);
1415b5c4e40SEvgeny Pinchuk }
1425b5c4e40SEvgeny Pinchuk 
1434f449311SHarish Kasiviswanathan void kfd_release_topology_device_list(struct list_head *device_list)
1445b5c4e40SEvgeny Pinchuk {
1455b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
1465b5c4e40SEvgeny Pinchuk 
1474f449311SHarish Kasiviswanathan 	while (!list_empty(device_list)) {
1484f449311SHarish Kasiviswanathan 		dev = list_first_entry(device_list,
1495b5c4e40SEvgeny Pinchuk 				       struct kfd_topology_device, list);
1505b5c4e40SEvgeny Pinchuk 		kfd_release_topology_device(dev);
1515b5c4e40SEvgeny Pinchuk 	}
1524f449311SHarish Kasiviswanathan }
1535b5c4e40SEvgeny Pinchuk 
1544f449311SHarish Kasiviswanathan static void kfd_release_live_view(void)
1554f449311SHarish Kasiviswanathan {
1564f449311SHarish Kasiviswanathan 	kfd_release_topology_device_list(&topology_device_list);
1575b5c4e40SEvgeny Pinchuk 	memset(&sys_props, 0, sizeof(sys_props));
1585b5c4e40SEvgeny Pinchuk }
1595b5c4e40SEvgeny Pinchuk 
1604f449311SHarish Kasiviswanathan struct kfd_topology_device *kfd_create_topology_device(
1614f449311SHarish Kasiviswanathan 				struct list_head *device_list)
1625b5c4e40SEvgeny Pinchuk {
1635b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
1645b5c4e40SEvgeny Pinchuk 
1655b5c4e40SEvgeny Pinchuk 	dev = kfd_alloc_struct(dev);
1664eacc26bSKent Russell 	if (!dev) {
1675b5c4e40SEvgeny Pinchuk 		pr_err("No memory to allocate a topology device");
16816b9201cSOded Gabbay 		return NULL;
1695b5c4e40SEvgeny Pinchuk 	}
1705b5c4e40SEvgeny Pinchuk 
1715b5c4e40SEvgeny Pinchuk 	INIT_LIST_HEAD(&dev->mem_props);
1725b5c4e40SEvgeny Pinchuk 	INIT_LIST_HEAD(&dev->cache_props);
1735b5c4e40SEvgeny Pinchuk 	INIT_LIST_HEAD(&dev->io_link_props);
174f4757347SAmber Lin 	INIT_LIST_HEAD(&dev->perf_props);
1755b5c4e40SEvgeny Pinchuk 
1764f449311SHarish Kasiviswanathan 	list_add_tail(&dev->list, device_list);
1775b5c4e40SEvgeny Pinchuk 
1785b5c4e40SEvgeny Pinchuk 	return dev;
1795b5c4e40SEvgeny Pinchuk }
1805b5c4e40SEvgeny Pinchuk 
1815b5c4e40SEvgeny Pinchuk 
1825b5c4e40SEvgeny Pinchuk #define sysfs_show_gen_prop(buffer, fmt, ...) \
1835b5c4e40SEvgeny Pinchuk 		snprintf(buffer, PAGE_SIZE, "%s"fmt, buffer, __VA_ARGS__)
1845b5c4e40SEvgeny Pinchuk #define sysfs_show_32bit_prop(buffer, name, value) \
1855b5c4e40SEvgeny Pinchuk 		sysfs_show_gen_prop(buffer, "%s %u\n", name, value)
1865b5c4e40SEvgeny Pinchuk #define sysfs_show_64bit_prop(buffer, name, value) \
1875b5c4e40SEvgeny Pinchuk 		sysfs_show_gen_prop(buffer, "%s %llu\n", name, value)
1885b5c4e40SEvgeny Pinchuk #define sysfs_show_32bit_val(buffer, value) \
1895b5c4e40SEvgeny Pinchuk 		sysfs_show_gen_prop(buffer, "%u\n", value)
1905b5c4e40SEvgeny Pinchuk #define sysfs_show_str_val(buffer, value) \
1915b5c4e40SEvgeny Pinchuk 		sysfs_show_gen_prop(buffer, "%s\n", value)
1925b5c4e40SEvgeny Pinchuk 
1935b5c4e40SEvgeny Pinchuk static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
1945b5c4e40SEvgeny Pinchuk 		char *buffer)
1955b5c4e40SEvgeny Pinchuk {
1965b5c4e40SEvgeny Pinchuk 	ssize_t ret;
1975b5c4e40SEvgeny Pinchuk 
1985b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
1995b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
2005b5c4e40SEvgeny Pinchuk 
2015b5c4e40SEvgeny Pinchuk 	if (attr == &sys_props.attr_genid) {
2025b5c4e40SEvgeny Pinchuk 		ret = sysfs_show_32bit_val(buffer, sys_props.generation_count);
2035b5c4e40SEvgeny Pinchuk 	} else if (attr == &sys_props.attr_props) {
2045b5c4e40SEvgeny Pinchuk 		sysfs_show_64bit_prop(buffer, "platform_oem",
2055b5c4e40SEvgeny Pinchuk 				sys_props.platform_oem);
2065b5c4e40SEvgeny Pinchuk 		sysfs_show_64bit_prop(buffer, "platform_id",
2075b5c4e40SEvgeny Pinchuk 				sys_props.platform_id);
2085b5c4e40SEvgeny Pinchuk 		ret = sysfs_show_64bit_prop(buffer, "platform_rev",
2095b5c4e40SEvgeny Pinchuk 				sys_props.platform_rev);
2105b5c4e40SEvgeny Pinchuk 	} else {
2115b5c4e40SEvgeny Pinchuk 		ret = -EINVAL;
2125b5c4e40SEvgeny Pinchuk 	}
2135b5c4e40SEvgeny Pinchuk 
2145b5c4e40SEvgeny Pinchuk 	return ret;
2155b5c4e40SEvgeny Pinchuk }
2165b5c4e40SEvgeny Pinchuk 
2175108d768SYong Zhao static void kfd_topology_kobj_release(struct kobject *kobj)
2185108d768SYong Zhao {
2195108d768SYong Zhao 	kfree(kobj);
2205108d768SYong Zhao }
2215108d768SYong Zhao 
2225b5c4e40SEvgeny Pinchuk static const struct sysfs_ops sysprops_ops = {
2235b5c4e40SEvgeny Pinchuk 	.show = sysprops_show,
2245b5c4e40SEvgeny Pinchuk };
2255b5c4e40SEvgeny Pinchuk 
2265b5c4e40SEvgeny Pinchuk static struct kobj_type sysprops_type = {
2275108d768SYong Zhao 	.release = kfd_topology_kobj_release,
2285b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &sysprops_ops,
2295b5c4e40SEvgeny Pinchuk };
2305b5c4e40SEvgeny Pinchuk 
2315b5c4e40SEvgeny Pinchuk static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
2325b5c4e40SEvgeny Pinchuk 		char *buffer)
2335b5c4e40SEvgeny Pinchuk {
2345b5c4e40SEvgeny Pinchuk 	ssize_t ret;
2355b5c4e40SEvgeny Pinchuk 	struct kfd_iolink_properties *iolink;
2365b5c4e40SEvgeny Pinchuk 
2375b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
2385b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
2395b5c4e40SEvgeny Pinchuk 
2405b5c4e40SEvgeny Pinchuk 	iolink = container_of(attr, struct kfd_iolink_properties, attr);
2415b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
2425b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
2435b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
2445b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
2455b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
2465b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
2475b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
2485b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
2495b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
2505b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
2515b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
2525b5c4e40SEvgeny Pinchuk 			iolink->rec_transfer_size);
2535b5c4e40SEvgeny Pinchuk 	ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
2545b5c4e40SEvgeny Pinchuk 
2555b5c4e40SEvgeny Pinchuk 	return ret;
2565b5c4e40SEvgeny Pinchuk }
2575b5c4e40SEvgeny Pinchuk 
2585b5c4e40SEvgeny Pinchuk static const struct sysfs_ops iolink_ops = {
2595b5c4e40SEvgeny Pinchuk 	.show = iolink_show,
2605b5c4e40SEvgeny Pinchuk };
2615b5c4e40SEvgeny Pinchuk 
2625b5c4e40SEvgeny Pinchuk static struct kobj_type iolink_type = {
2635108d768SYong Zhao 	.release = kfd_topology_kobj_release,
2645b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &iolink_ops,
2655b5c4e40SEvgeny Pinchuk };
2665b5c4e40SEvgeny Pinchuk 
2675b5c4e40SEvgeny Pinchuk static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
2685b5c4e40SEvgeny Pinchuk 		char *buffer)
2695b5c4e40SEvgeny Pinchuk {
2705b5c4e40SEvgeny Pinchuk 	ssize_t ret;
2715b5c4e40SEvgeny Pinchuk 	struct kfd_mem_properties *mem;
2725b5c4e40SEvgeny Pinchuk 
2735b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
2745b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
2755b5c4e40SEvgeny Pinchuk 
2765b5c4e40SEvgeny Pinchuk 	mem = container_of(attr, struct kfd_mem_properties, attr);
2775b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
2785b5c4e40SEvgeny Pinchuk 	sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
2795b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "flags", mem->flags);
2805b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "width", mem->width);
2815b5c4e40SEvgeny Pinchuk 	ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
2825b5c4e40SEvgeny Pinchuk 
2835b5c4e40SEvgeny Pinchuk 	return ret;
2845b5c4e40SEvgeny Pinchuk }
2855b5c4e40SEvgeny Pinchuk 
2865b5c4e40SEvgeny Pinchuk static const struct sysfs_ops mem_ops = {
2875b5c4e40SEvgeny Pinchuk 	.show = mem_show,
2885b5c4e40SEvgeny Pinchuk };
2895b5c4e40SEvgeny Pinchuk 
2905b5c4e40SEvgeny Pinchuk static struct kobj_type mem_type = {
2915108d768SYong Zhao 	.release = kfd_topology_kobj_release,
2925b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &mem_ops,
2935b5c4e40SEvgeny Pinchuk };
2945b5c4e40SEvgeny Pinchuk 
2955b5c4e40SEvgeny Pinchuk static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
2965b5c4e40SEvgeny Pinchuk 		char *buffer)
2975b5c4e40SEvgeny Pinchuk {
2985b5c4e40SEvgeny Pinchuk 	ssize_t ret;
299bc0c75a3SHarish Kasiviswanathan 	uint32_t i, j;
3005b5c4e40SEvgeny Pinchuk 	struct kfd_cache_properties *cache;
3015b5c4e40SEvgeny Pinchuk 
3025b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
3035b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
3045b5c4e40SEvgeny Pinchuk 
3055b5c4e40SEvgeny Pinchuk 	cache = container_of(attr, struct kfd_cache_properties, attr);
3065b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "processor_id_low",
3075b5c4e40SEvgeny Pinchuk 			cache->processor_id_low);
3085b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
3095b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
3105b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
3115b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
3125b5c4e40SEvgeny Pinchuk 			cache->cachelines_per_tag);
3135b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
3145b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
3155b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
3165b5c4e40SEvgeny Pinchuk 	snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
317bc0c75a3SHarish Kasiviswanathan 	for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++)
318bc0c75a3SHarish Kasiviswanathan 		for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++) {
319bc0c75a3SHarish Kasiviswanathan 			/* Check each bit */
320bc0c75a3SHarish Kasiviswanathan 			if (cache->sibling_map[i] & (1 << j))
321bc0c75a3SHarish Kasiviswanathan 				ret = snprintf(buffer, PAGE_SIZE,
322bc0c75a3SHarish Kasiviswanathan 					 "%s%d%s", buffer, 1, ",");
323bc0c75a3SHarish Kasiviswanathan 			else
324bc0c75a3SHarish Kasiviswanathan 				ret = snprintf(buffer, PAGE_SIZE,
325bc0c75a3SHarish Kasiviswanathan 					 "%s%d%s", buffer, 0, ",");
326bc0c75a3SHarish Kasiviswanathan 		}
327bc0c75a3SHarish Kasiviswanathan 	/* Replace the last "," with end of line */
328bc0c75a3SHarish Kasiviswanathan 	*(buffer + strlen(buffer) - 1) = 0xA;
3295b5c4e40SEvgeny Pinchuk 	return ret;
3305b5c4e40SEvgeny Pinchuk }
3315b5c4e40SEvgeny Pinchuk 
3325b5c4e40SEvgeny Pinchuk static const struct sysfs_ops cache_ops = {
3335b5c4e40SEvgeny Pinchuk 	.show = kfd_cache_show,
3345b5c4e40SEvgeny Pinchuk };
3355b5c4e40SEvgeny Pinchuk 
3365b5c4e40SEvgeny Pinchuk static struct kobj_type cache_type = {
3375108d768SYong Zhao 	.release = kfd_topology_kobj_release,
3385b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &cache_ops,
3395b5c4e40SEvgeny Pinchuk };
3405b5c4e40SEvgeny Pinchuk 
341f4757347SAmber Lin /****** Sysfs of Performance Counters ******/
342f4757347SAmber Lin 
343f4757347SAmber Lin struct kfd_perf_attr {
344f4757347SAmber Lin 	struct kobj_attribute attr;
345f4757347SAmber Lin 	uint32_t data;
346f4757347SAmber Lin };
347f4757347SAmber Lin 
348f4757347SAmber Lin static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
349f4757347SAmber Lin 			char *buf)
350f4757347SAmber Lin {
351f4757347SAmber Lin 	struct kfd_perf_attr *attr;
352f4757347SAmber Lin 
353f4757347SAmber Lin 	buf[0] = 0;
354f4757347SAmber Lin 	attr = container_of(attrs, struct kfd_perf_attr, attr);
355f4757347SAmber Lin 	if (!attr->data) /* invalid data for PMC */
356f4757347SAmber Lin 		return 0;
357f4757347SAmber Lin 	else
358f4757347SAmber Lin 		return sysfs_show_32bit_val(buf, attr->data);
359f4757347SAmber Lin }
360f4757347SAmber Lin 
361f4757347SAmber Lin #define KFD_PERF_DESC(_name, _data)			\
362f4757347SAmber Lin {							\
363f4757347SAmber Lin 	.attr  = __ATTR(_name, 0444, perf_show, NULL),	\
364f4757347SAmber Lin 	.data = _data,					\
365f4757347SAmber Lin }
366f4757347SAmber Lin 
367f4757347SAmber Lin static struct kfd_perf_attr perf_attr_iommu[] = {
368f4757347SAmber Lin 	KFD_PERF_DESC(max_concurrent, 0),
369f4757347SAmber Lin 	KFD_PERF_DESC(num_counters, 0),
370f4757347SAmber Lin 	KFD_PERF_DESC(counter_ids, 0),
371f4757347SAmber Lin };
372f4757347SAmber Lin /****************************************/
373f4757347SAmber Lin 
3745b5c4e40SEvgeny Pinchuk static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
3755b5c4e40SEvgeny Pinchuk 		char *buffer)
3765b5c4e40SEvgeny Pinchuk {
3775b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
3785b5c4e40SEvgeny Pinchuk 	char public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE];
3795b5c4e40SEvgeny Pinchuk 	uint32_t i;
380f7c826adSAlexey Skidanov 	uint32_t log_max_watch_addr;
3815b5c4e40SEvgeny Pinchuk 
3825b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
3835b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
3845b5c4e40SEvgeny Pinchuk 
3855b5c4e40SEvgeny Pinchuk 	if (strcmp(attr->name, "gpu_id") == 0) {
3865b5c4e40SEvgeny Pinchuk 		dev = container_of(attr, struct kfd_topology_device,
3875b5c4e40SEvgeny Pinchuk 				attr_gpuid);
388f7c826adSAlexey Skidanov 		return sysfs_show_32bit_val(buffer, dev->gpu_id);
389f7c826adSAlexey Skidanov 	}
390f7c826adSAlexey Skidanov 
391f7c826adSAlexey Skidanov 	if (strcmp(attr->name, "name") == 0) {
3925b5c4e40SEvgeny Pinchuk 		dev = container_of(attr, struct kfd_topology_device,
3935b5c4e40SEvgeny Pinchuk 				attr_name);
3945b5c4e40SEvgeny Pinchuk 		for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE; i++) {
3955b5c4e40SEvgeny Pinchuk 			public_name[i] =
3965b5c4e40SEvgeny Pinchuk 					(char)dev->node_props.marketing_name[i];
3975b5c4e40SEvgeny Pinchuk 			if (dev->node_props.marketing_name[i] == 0)
3985b5c4e40SEvgeny Pinchuk 				break;
3995b5c4e40SEvgeny Pinchuk 		}
4005b5c4e40SEvgeny Pinchuk 		public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1] = 0x0;
401f7c826adSAlexey Skidanov 		return sysfs_show_str_val(buffer, public_name);
402f7c826adSAlexey Skidanov 	}
403f7c826adSAlexey Skidanov 
4045b5c4e40SEvgeny Pinchuk 	dev = container_of(attr, struct kfd_topology_device,
4055b5c4e40SEvgeny Pinchuk 			attr_props);
4065b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "cpu_cores_count",
4075b5c4e40SEvgeny Pinchuk 			dev->node_props.cpu_cores_count);
4085b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "simd_count",
4095b5c4e40SEvgeny Pinchuk 			dev->node_props.simd_count);
4105b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "mem_banks_count",
4115b5c4e40SEvgeny Pinchuk 			dev->node_props.mem_banks_count);
4125b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "caches_count",
4135b5c4e40SEvgeny Pinchuk 			dev->node_props.caches_count);
4145b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "io_links_count",
4155b5c4e40SEvgeny Pinchuk 			dev->node_props.io_links_count);
4165b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
4175b5c4e40SEvgeny Pinchuk 			dev->node_props.cpu_core_id_base);
4185b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "simd_id_base",
4195b5c4e40SEvgeny Pinchuk 			dev->node_props.simd_id_base);
4205b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
4215b5c4e40SEvgeny Pinchuk 			dev->node_props.max_waves_per_simd);
4225b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
4235b5c4e40SEvgeny Pinchuk 			dev->node_props.lds_size_in_kb);
4245b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
4255b5c4e40SEvgeny Pinchuk 			dev->node_props.gds_size_in_kb);
4265b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "wave_front_size",
4275b5c4e40SEvgeny Pinchuk 			dev->node_props.wave_front_size);
4285b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "array_count",
4295b5c4e40SEvgeny Pinchuk 			dev->node_props.array_count);
4305b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
4315b5c4e40SEvgeny Pinchuk 			dev->node_props.simd_arrays_per_engine);
4325b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
4335b5c4e40SEvgeny Pinchuk 			dev->node_props.cu_per_simd_array);
4345b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "simd_per_cu",
4355b5c4e40SEvgeny Pinchuk 			dev->node_props.simd_per_cu);
4365b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
4375b5c4e40SEvgeny Pinchuk 			dev->node_props.max_slots_scratch_cu);
4385b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "vendor_id",
4395b5c4e40SEvgeny Pinchuk 			dev->node_props.vendor_id);
4405b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "device_id",
4415b5c4e40SEvgeny Pinchuk 			dev->node_props.device_id);
4425b5c4e40SEvgeny Pinchuk 	sysfs_show_32bit_prop(buffer, "location_id",
4435b5c4e40SEvgeny Pinchuk 			dev->node_props.location_id);
4447c9b7171SOak Zeng 	sysfs_show_32bit_prop(buffer, "drm_render_minor",
4457c9b7171SOak Zeng 			dev->node_props.drm_render_minor);
4465b5c4e40SEvgeny Pinchuk 
4475b5c4e40SEvgeny Pinchuk 	if (dev->gpu) {
448f7c826adSAlexey Skidanov 		log_max_watch_addr =
449f7c826adSAlexey Skidanov 			__ilog2_u32(dev->gpu->device_info->num_of_watch_points);
450f7c826adSAlexey Skidanov 
451f7c826adSAlexey Skidanov 		if (log_max_watch_addr) {
452f7c826adSAlexey Skidanov 			dev->node_props.capability |=
453f7c826adSAlexey Skidanov 					HSA_CAP_WATCH_POINTS_SUPPORTED;
454f7c826adSAlexey Skidanov 
455f7c826adSAlexey Skidanov 			dev->node_props.capability |=
456f7c826adSAlexey Skidanov 				((log_max_watch_addr <<
457f7c826adSAlexey Skidanov 					HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
458f7c826adSAlexey Skidanov 				HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
459f7c826adSAlexey Skidanov 		}
460f7c826adSAlexey Skidanov 
461413e85d5SBen Goz 		if (dev->gpu->device_info->asic_family == CHIP_TONGA)
462413e85d5SBen Goz 			dev->node_props.capability |=
463413e85d5SBen Goz 					HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
464413e85d5SBen Goz 
4655b5c4e40SEvgeny Pinchuk 		sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
4663a87177eSHarish Kasiviswanathan 			dev->node_props.max_engine_clk_fcompute);
46742e08c78SOded Gabbay 
4685b5c4e40SEvgeny Pinchuk 		sysfs_show_64bit_prop(buffer, "local_mem_size",
46942e08c78SOded Gabbay 				(unsigned long long int) 0);
470f1386fbcSOded Gabbay 
471f1386fbcSOded Gabbay 		sysfs_show_32bit_prop(buffer, "fw_version",
472cea405b1SXihan Zhang 			dev->gpu->kfd2kgd->get_fw_version(
473f1386fbcSOded Gabbay 						dev->gpu->kgd,
474f1386fbcSOded Gabbay 						KGD_ENGINE_MEC1));
475826f5de8SAlexey Skidanov 		sysfs_show_32bit_prop(buffer, "capability",
476826f5de8SAlexey Skidanov 				dev->node_props.capability);
4775b5c4e40SEvgeny Pinchuk 	}
4785b5c4e40SEvgeny Pinchuk 
479f7c826adSAlexey Skidanov 	return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
4805b5c4e40SEvgeny Pinchuk 					cpufreq_quick_get_max(0)/1000);
4815b5c4e40SEvgeny Pinchuk }
4825b5c4e40SEvgeny Pinchuk 
4835b5c4e40SEvgeny Pinchuk static const struct sysfs_ops node_ops = {
4845b5c4e40SEvgeny Pinchuk 	.show = node_show,
4855b5c4e40SEvgeny Pinchuk };
4865b5c4e40SEvgeny Pinchuk 
4875b5c4e40SEvgeny Pinchuk static struct kobj_type node_type = {
4885108d768SYong Zhao 	.release = kfd_topology_kobj_release,
4895b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &node_ops,
4905b5c4e40SEvgeny Pinchuk };
4915b5c4e40SEvgeny Pinchuk 
4925b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
4935b5c4e40SEvgeny Pinchuk {
4945b5c4e40SEvgeny Pinchuk 	sysfs_remove_file(kobj, attr);
4955b5c4e40SEvgeny Pinchuk 	kobject_del(kobj);
4965b5c4e40SEvgeny Pinchuk 	kobject_put(kobj);
4975b5c4e40SEvgeny Pinchuk }
4985b5c4e40SEvgeny Pinchuk 
4995b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
5005b5c4e40SEvgeny Pinchuk {
5015b5c4e40SEvgeny Pinchuk 	struct kfd_iolink_properties *iolink;
5025b5c4e40SEvgeny Pinchuk 	struct kfd_cache_properties *cache;
5035b5c4e40SEvgeny Pinchuk 	struct kfd_mem_properties *mem;
504f4757347SAmber Lin 	struct kfd_perf_properties *perf;
5055b5c4e40SEvgeny Pinchuk 
5065b5c4e40SEvgeny Pinchuk 	if (dev->kobj_iolink) {
5075b5c4e40SEvgeny Pinchuk 		list_for_each_entry(iolink, &dev->io_link_props, list)
5085b5c4e40SEvgeny Pinchuk 			if (iolink->kobj) {
5095b5c4e40SEvgeny Pinchuk 				kfd_remove_sysfs_file(iolink->kobj,
5105b5c4e40SEvgeny Pinchuk 							&iolink->attr);
51116b9201cSOded Gabbay 				iolink->kobj = NULL;
5125b5c4e40SEvgeny Pinchuk 			}
5135b5c4e40SEvgeny Pinchuk 		kobject_del(dev->kobj_iolink);
5145b5c4e40SEvgeny Pinchuk 		kobject_put(dev->kobj_iolink);
51516b9201cSOded Gabbay 		dev->kobj_iolink = NULL;
5165b5c4e40SEvgeny Pinchuk 	}
5175b5c4e40SEvgeny Pinchuk 
5185b5c4e40SEvgeny Pinchuk 	if (dev->kobj_cache) {
5195b5c4e40SEvgeny Pinchuk 		list_for_each_entry(cache, &dev->cache_props, list)
5205b5c4e40SEvgeny Pinchuk 			if (cache->kobj) {
5215b5c4e40SEvgeny Pinchuk 				kfd_remove_sysfs_file(cache->kobj,
5225b5c4e40SEvgeny Pinchuk 							&cache->attr);
52316b9201cSOded Gabbay 				cache->kobj = NULL;
5245b5c4e40SEvgeny Pinchuk 			}
5255b5c4e40SEvgeny Pinchuk 		kobject_del(dev->kobj_cache);
5265b5c4e40SEvgeny Pinchuk 		kobject_put(dev->kobj_cache);
52716b9201cSOded Gabbay 		dev->kobj_cache = NULL;
5285b5c4e40SEvgeny Pinchuk 	}
5295b5c4e40SEvgeny Pinchuk 
5305b5c4e40SEvgeny Pinchuk 	if (dev->kobj_mem) {
5315b5c4e40SEvgeny Pinchuk 		list_for_each_entry(mem, &dev->mem_props, list)
5325b5c4e40SEvgeny Pinchuk 			if (mem->kobj) {
5335b5c4e40SEvgeny Pinchuk 				kfd_remove_sysfs_file(mem->kobj, &mem->attr);
53416b9201cSOded Gabbay 				mem->kobj = NULL;
5355b5c4e40SEvgeny Pinchuk 			}
5365b5c4e40SEvgeny Pinchuk 		kobject_del(dev->kobj_mem);
5375b5c4e40SEvgeny Pinchuk 		kobject_put(dev->kobj_mem);
53816b9201cSOded Gabbay 		dev->kobj_mem = NULL;
5395b5c4e40SEvgeny Pinchuk 	}
5405b5c4e40SEvgeny Pinchuk 
541f4757347SAmber Lin 	if (dev->kobj_perf) {
542f4757347SAmber Lin 		list_for_each_entry(perf, &dev->perf_props, list) {
543f4757347SAmber Lin 			kfree(perf->attr_group);
544f4757347SAmber Lin 			perf->attr_group = NULL;
545f4757347SAmber Lin 		}
546f4757347SAmber Lin 		kobject_del(dev->kobj_perf);
547f4757347SAmber Lin 		kobject_put(dev->kobj_perf);
548f4757347SAmber Lin 		dev->kobj_perf = NULL;
549f4757347SAmber Lin 	}
550f4757347SAmber Lin 
5515b5c4e40SEvgeny Pinchuk 	if (dev->kobj_node) {
5525b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
5535b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(dev->kobj_node, &dev->attr_name);
5545b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(dev->kobj_node, &dev->attr_props);
5555b5c4e40SEvgeny Pinchuk 		kobject_del(dev->kobj_node);
5565b5c4e40SEvgeny Pinchuk 		kobject_put(dev->kobj_node);
55716b9201cSOded Gabbay 		dev->kobj_node = NULL;
5585b5c4e40SEvgeny Pinchuk 	}
5595b5c4e40SEvgeny Pinchuk }
5605b5c4e40SEvgeny Pinchuk 
5615b5c4e40SEvgeny Pinchuk static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
5625b5c4e40SEvgeny Pinchuk 		uint32_t id)
5635b5c4e40SEvgeny Pinchuk {
5645b5c4e40SEvgeny Pinchuk 	struct kfd_iolink_properties *iolink;
5655b5c4e40SEvgeny Pinchuk 	struct kfd_cache_properties *cache;
5665b5c4e40SEvgeny Pinchuk 	struct kfd_mem_properties *mem;
567f4757347SAmber Lin 	struct kfd_perf_properties *perf;
5685b5c4e40SEvgeny Pinchuk 	int ret;
569f4757347SAmber Lin 	uint32_t i, num_attrs;
570f4757347SAmber Lin 	struct attribute **attrs;
5715b5c4e40SEvgeny Pinchuk 
57232fa8219SFelix Kuehling 	if (WARN_ON(dev->kobj_node))
57332fa8219SFelix Kuehling 		return -EEXIST;
57432fa8219SFelix Kuehling 
5755b5c4e40SEvgeny Pinchuk 	/*
5765b5c4e40SEvgeny Pinchuk 	 * Creating the sysfs folders
5775b5c4e40SEvgeny Pinchuk 	 */
5785b5c4e40SEvgeny Pinchuk 	dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
5795b5c4e40SEvgeny Pinchuk 	if (!dev->kobj_node)
5805b5c4e40SEvgeny Pinchuk 		return -ENOMEM;
5815b5c4e40SEvgeny Pinchuk 
5825b5c4e40SEvgeny Pinchuk 	ret = kobject_init_and_add(dev->kobj_node, &node_type,
5835b5c4e40SEvgeny Pinchuk 			sys_props.kobj_nodes, "%d", id);
5845b5c4e40SEvgeny Pinchuk 	if (ret < 0)
5855b5c4e40SEvgeny Pinchuk 		return ret;
5865b5c4e40SEvgeny Pinchuk 
5875b5c4e40SEvgeny Pinchuk 	dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
5885b5c4e40SEvgeny Pinchuk 	if (!dev->kobj_mem)
5895b5c4e40SEvgeny Pinchuk 		return -ENOMEM;
5905b5c4e40SEvgeny Pinchuk 
5915b5c4e40SEvgeny Pinchuk 	dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
5925b5c4e40SEvgeny Pinchuk 	if (!dev->kobj_cache)
5935b5c4e40SEvgeny Pinchuk 		return -ENOMEM;
5945b5c4e40SEvgeny Pinchuk 
5955b5c4e40SEvgeny Pinchuk 	dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
5965b5c4e40SEvgeny Pinchuk 	if (!dev->kobj_iolink)
5975b5c4e40SEvgeny Pinchuk 		return -ENOMEM;
5985b5c4e40SEvgeny Pinchuk 
599f4757347SAmber Lin 	dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
600f4757347SAmber Lin 	if (!dev->kobj_perf)
601f4757347SAmber Lin 		return -ENOMEM;
602f4757347SAmber Lin 
6035b5c4e40SEvgeny Pinchuk 	/*
6045b5c4e40SEvgeny Pinchuk 	 * Creating sysfs files for node properties
6055b5c4e40SEvgeny Pinchuk 	 */
6065b5c4e40SEvgeny Pinchuk 	dev->attr_gpuid.name = "gpu_id";
6075b5c4e40SEvgeny Pinchuk 	dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
6085b5c4e40SEvgeny Pinchuk 	sysfs_attr_init(&dev->attr_gpuid);
6095b5c4e40SEvgeny Pinchuk 	dev->attr_name.name = "name";
6105b5c4e40SEvgeny Pinchuk 	dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
6115b5c4e40SEvgeny Pinchuk 	sysfs_attr_init(&dev->attr_name);
6125b5c4e40SEvgeny Pinchuk 	dev->attr_props.name = "properties";
6135b5c4e40SEvgeny Pinchuk 	dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
6145b5c4e40SEvgeny Pinchuk 	sysfs_attr_init(&dev->attr_props);
6155b5c4e40SEvgeny Pinchuk 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
6165b5c4e40SEvgeny Pinchuk 	if (ret < 0)
6175b5c4e40SEvgeny Pinchuk 		return ret;
6185b5c4e40SEvgeny Pinchuk 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
6195b5c4e40SEvgeny Pinchuk 	if (ret < 0)
6205b5c4e40SEvgeny Pinchuk 		return ret;
6215b5c4e40SEvgeny Pinchuk 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
6225b5c4e40SEvgeny Pinchuk 	if (ret < 0)
6235b5c4e40SEvgeny Pinchuk 		return ret;
6245b5c4e40SEvgeny Pinchuk 
6255b5c4e40SEvgeny Pinchuk 	i = 0;
6265b5c4e40SEvgeny Pinchuk 	list_for_each_entry(mem, &dev->mem_props, list) {
6275b5c4e40SEvgeny Pinchuk 		mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
6285b5c4e40SEvgeny Pinchuk 		if (!mem->kobj)
6295b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
6305b5c4e40SEvgeny Pinchuk 		ret = kobject_init_and_add(mem->kobj, &mem_type,
6315b5c4e40SEvgeny Pinchuk 				dev->kobj_mem, "%d", i);
6325b5c4e40SEvgeny Pinchuk 		if (ret < 0)
6335b5c4e40SEvgeny Pinchuk 			return ret;
6345b5c4e40SEvgeny Pinchuk 
6355b5c4e40SEvgeny Pinchuk 		mem->attr.name = "properties";
6365b5c4e40SEvgeny Pinchuk 		mem->attr.mode = KFD_SYSFS_FILE_MODE;
6375b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&mem->attr);
6385b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(mem->kobj, &mem->attr);
6395b5c4e40SEvgeny Pinchuk 		if (ret < 0)
6405b5c4e40SEvgeny Pinchuk 			return ret;
6415b5c4e40SEvgeny Pinchuk 		i++;
6425b5c4e40SEvgeny Pinchuk 	}
6435b5c4e40SEvgeny Pinchuk 
6445b5c4e40SEvgeny Pinchuk 	i = 0;
6455b5c4e40SEvgeny Pinchuk 	list_for_each_entry(cache, &dev->cache_props, list) {
6465b5c4e40SEvgeny Pinchuk 		cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
6475b5c4e40SEvgeny Pinchuk 		if (!cache->kobj)
6485b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
6495b5c4e40SEvgeny Pinchuk 		ret = kobject_init_and_add(cache->kobj, &cache_type,
6505b5c4e40SEvgeny Pinchuk 				dev->kobj_cache, "%d", i);
6515b5c4e40SEvgeny Pinchuk 		if (ret < 0)
6525b5c4e40SEvgeny Pinchuk 			return ret;
6535b5c4e40SEvgeny Pinchuk 
6545b5c4e40SEvgeny Pinchuk 		cache->attr.name = "properties";
6555b5c4e40SEvgeny Pinchuk 		cache->attr.mode = KFD_SYSFS_FILE_MODE;
6565b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&cache->attr);
6575b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(cache->kobj, &cache->attr);
6585b5c4e40SEvgeny Pinchuk 		if (ret < 0)
6595b5c4e40SEvgeny Pinchuk 			return ret;
6605b5c4e40SEvgeny Pinchuk 		i++;
6615b5c4e40SEvgeny Pinchuk 	}
6625b5c4e40SEvgeny Pinchuk 
6635b5c4e40SEvgeny Pinchuk 	i = 0;
6645b5c4e40SEvgeny Pinchuk 	list_for_each_entry(iolink, &dev->io_link_props, list) {
6655b5c4e40SEvgeny Pinchuk 		iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
6665b5c4e40SEvgeny Pinchuk 		if (!iolink->kobj)
6675b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
6685b5c4e40SEvgeny Pinchuk 		ret = kobject_init_and_add(iolink->kobj, &iolink_type,
6695b5c4e40SEvgeny Pinchuk 				dev->kobj_iolink, "%d", i);
6705b5c4e40SEvgeny Pinchuk 		if (ret < 0)
6715b5c4e40SEvgeny Pinchuk 			return ret;
6725b5c4e40SEvgeny Pinchuk 
6735b5c4e40SEvgeny Pinchuk 		iolink->attr.name = "properties";
6745b5c4e40SEvgeny Pinchuk 		iolink->attr.mode = KFD_SYSFS_FILE_MODE;
6755b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&iolink->attr);
6765b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(iolink->kobj, &iolink->attr);
6775b5c4e40SEvgeny Pinchuk 		if (ret < 0)
6785b5c4e40SEvgeny Pinchuk 			return ret;
6795b5c4e40SEvgeny Pinchuk 		i++;
6805b5c4e40SEvgeny Pinchuk 	}
6815b5c4e40SEvgeny Pinchuk 
682f4757347SAmber Lin 	/* All hardware blocks have the same number of attributes. */
6833f866f5fSGustavo A. R. Silva 	num_attrs = ARRAY_SIZE(perf_attr_iommu);
684f4757347SAmber Lin 	list_for_each_entry(perf, &dev->perf_props, list) {
685f4757347SAmber Lin 		perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
686f4757347SAmber Lin 			* num_attrs + sizeof(struct attribute_group),
687f4757347SAmber Lin 			GFP_KERNEL);
688f4757347SAmber Lin 		if (!perf->attr_group)
689f4757347SAmber Lin 			return -ENOMEM;
690f4757347SAmber Lin 
691f4757347SAmber Lin 		attrs = (struct attribute **)(perf->attr_group + 1);
692f4757347SAmber Lin 		if (!strcmp(perf->block_name, "iommu")) {
693f4757347SAmber Lin 		/* Information of IOMMU's num_counters and counter_ids is shown
694f4757347SAmber Lin 		 * under /sys/bus/event_source/devices/amd_iommu. We don't
695f4757347SAmber Lin 		 * duplicate here.
696f4757347SAmber Lin 		 */
697f4757347SAmber Lin 			perf_attr_iommu[0].data = perf->max_concurrent;
698f4757347SAmber Lin 			for (i = 0; i < num_attrs; i++)
699f4757347SAmber Lin 				attrs[i] = &perf_attr_iommu[i].attr.attr;
700f4757347SAmber Lin 		}
701f4757347SAmber Lin 		perf->attr_group->name = perf->block_name;
702f4757347SAmber Lin 		perf->attr_group->attrs = attrs;
703f4757347SAmber Lin 		ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
704f4757347SAmber Lin 		if (ret < 0)
705f4757347SAmber Lin 			return ret;
706f4757347SAmber Lin 	}
707f4757347SAmber Lin 
7085b5c4e40SEvgeny Pinchuk 	return 0;
7095b5c4e40SEvgeny Pinchuk }
7105b5c4e40SEvgeny Pinchuk 
7113a87177eSHarish Kasiviswanathan /* Called with write topology lock acquired */
7125b5c4e40SEvgeny Pinchuk static int kfd_build_sysfs_node_tree(void)
7135b5c4e40SEvgeny Pinchuk {
7145b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
7155b5c4e40SEvgeny Pinchuk 	int ret;
7165b5c4e40SEvgeny Pinchuk 	uint32_t i = 0;
7175b5c4e40SEvgeny Pinchuk 
7185b5c4e40SEvgeny Pinchuk 	list_for_each_entry(dev, &topology_device_list, list) {
7198dfead6cSBen Goz 		ret = kfd_build_sysfs_node_entry(dev, i);
7205b5c4e40SEvgeny Pinchuk 		if (ret < 0)
7215b5c4e40SEvgeny Pinchuk 			return ret;
7225b5c4e40SEvgeny Pinchuk 		i++;
7235b5c4e40SEvgeny Pinchuk 	}
7245b5c4e40SEvgeny Pinchuk 
7255b5c4e40SEvgeny Pinchuk 	return 0;
7265b5c4e40SEvgeny Pinchuk }
7275b5c4e40SEvgeny Pinchuk 
7283a87177eSHarish Kasiviswanathan /* Called with write topology lock acquired */
7295b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_node_tree(void)
7305b5c4e40SEvgeny Pinchuk {
7315b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
7325b5c4e40SEvgeny Pinchuk 
7335b5c4e40SEvgeny Pinchuk 	list_for_each_entry(dev, &topology_device_list, list)
7345b5c4e40SEvgeny Pinchuk 		kfd_remove_sysfs_node_entry(dev);
7355b5c4e40SEvgeny Pinchuk }
7365b5c4e40SEvgeny Pinchuk 
7375b5c4e40SEvgeny Pinchuk static int kfd_topology_update_sysfs(void)
7385b5c4e40SEvgeny Pinchuk {
7395b5c4e40SEvgeny Pinchuk 	int ret;
7405b5c4e40SEvgeny Pinchuk 
7415b5c4e40SEvgeny Pinchuk 	pr_info("Creating topology SYSFS entries\n");
7424eacc26bSKent Russell 	if (!sys_props.kobj_topology) {
7435b5c4e40SEvgeny Pinchuk 		sys_props.kobj_topology =
7445b5c4e40SEvgeny Pinchuk 				kfd_alloc_struct(sys_props.kobj_topology);
7455b5c4e40SEvgeny Pinchuk 		if (!sys_props.kobj_topology)
7465b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
7475b5c4e40SEvgeny Pinchuk 
7485b5c4e40SEvgeny Pinchuk 		ret = kobject_init_and_add(sys_props.kobj_topology,
7495b5c4e40SEvgeny Pinchuk 				&sysprops_type,  &kfd_device->kobj,
7505b5c4e40SEvgeny Pinchuk 				"topology");
7515b5c4e40SEvgeny Pinchuk 		if (ret < 0)
7525b5c4e40SEvgeny Pinchuk 			return ret;
7535b5c4e40SEvgeny Pinchuk 
7545b5c4e40SEvgeny Pinchuk 		sys_props.kobj_nodes = kobject_create_and_add("nodes",
7555b5c4e40SEvgeny Pinchuk 				sys_props.kobj_topology);
7565b5c4e40SEvgeny Pinchuk 		if (!sys_props.kobj_nodes)
7575b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
7585b5c4e40SEvgeny Pinchuk 
7595b5c4e40SEvgeny Pinchuk 		sys_props.attr_genid.name = "generation_id";
7605b5c4e40SEvgeny Pinchuk 		sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
7615b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&sys_props.attr_genid);
7625b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(sys_props.kobj_topology,
7635b5c4e40SEvgeny Pinchuk 				&sys_props.attr_genid);
7645b5c4e40SEvgeny Pinchuk 		if (ret < 0)
7655b5c4e40SEvgeny Pinchuk 			return ret;
7665b5c4e40SEvgeny Pinchuk 
7675b5c4e40SEvgeny Pinchuk 		sys_props.attr_props.name = "system_properties";
7685b5c4e40SEvgeny Pinchuk 		sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
7695b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&sys_props.attr_props);
7705b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(sys_props.kobj_topology,
7715b5c4e40SEvgeny Pinchuk 				&sys_props.attr_props);
7725b5c4e40SEvgeny Pinchuk 		if (ret < 0)
7735b5c4e40SEvgeny Pinchuk 			return ret;
7745b5c4e40SEvgeny Pinchuk 	}
7755b5c4e40SEvgeny Pinchuk 
7765b5c4e40SEvgeny Pinchuk 	kfd_remove_sysfs_node_tree();
7775b5c4e40SEvgeny Pinchuk 
7785b5c4e40SEvgeny Pinchuk 	return kfd_build_sysfs_node_tree();
7795b5c4e40SEvgeny Pinchuk }
7805b5c4e40SEvgeny Pinchuk 
7815b5c4e40SEvgeny Pinchuk static void kfd_topology_release_sysfs(void)
7825b5c4e40SEvgeny Pinchuk {
7835b5c4e40SEvgeny Pinchuk 	kfd_remove_sysfs_node_tree();
7845b5c4e40SEvgeny Pinchuk 	if (sys_props.kobj_topology) {
7855b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(sys_props.kobj_topology,
7865b5c4e40SEvgeny Pinchuk 				&sys_props.attr_genid);
7875b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(sys_props.kobj_topology,
7885b5c4e40SEvgeny Pinchuk 				&sys_props.attr_props);
7895b5c4e40SEvgeny Pinchuk 		if (sys_props.kobj_nodes) {
7905b5c4e40SEvgeny Pinchuk 			kobject_del(sys_props.kobj_nodes);
7915b5c4e40SEvgeny Pinchuk 			kobject_put(sys_props.kobj_nodes);
79216b9201cSOded Gabbay 			sys_props.kobj_nodes = NULL;
7935b5c4e40SEvgeny Pinchuk 		}
7945b5c4e40SEvgeny Pinchuk 		kobject_del(sys_props.kobj_topology);
7955b5c4e40SEvgeny Pinchuk 		kobject_put(sys_props.kobj_topology);
79616b9201cSOded Gabbay 		sys_props.kobj_topology = NULL;
7975b5c4e40SEvgeny Pinchuk 	}
7985b5c4e40SEvgeny Pinchuk }
7995b5c4e40SEvgeny Pinchuk 
8004f449311SHarish Kasiviswanathan /* Called with write topology_lock acquired */
8014f449311SHarish Kasiviswanathan static void kfd_topology_update_device_list(struct list_head *temp_list,
8024f449311SHarish Kasiviswanathan 					struct list_head *master_list)
8034f449311SHarish Kasiviswanathan {
8044f449311SHarish Kasiviswanathan 	while (!list_empty(temp_list)) {
8054f449311SHarish Kasiviswanathan 		list_move_tail(temp_list->next, master_list);
8064f449311SHarish Kasiviswanathan 		sys_props.num_devices++;
8074f449311SHarish Kasiviswanathan 	}
8084f449311SHarish Kasiviswanathan }
8094f449311SHarish Kasiviswanathan 
810520b8fb7SFelix Kuehling static void kfd_debug_print_topology(void)
811520b8fb7SFelix Kuehling {
812520b8fb7SFelix Kuehling 	struct kfd_topology_device *dev;
813520b8fb7SFelix Kuehling 
814520b8fb7SFelix Kuehling 	down_read(&topology_lock);
815520b8fb7SFelix Kuehling 
816520b8fb7SFelix Kuehling 	dev = list_last_entry(&topology_device_list,
817520b8fb7SFelix Kuehling 			struct kfd_topology_device, list);
818520b8fb7SFelix Kuehling 	if (dev) {
819520b8fb7SFelix Kuehling 		if (dev->node_props.cpu_cores_count &&
820520b8fb7SFelix Kuehling 				dev->node_props.simd_count) {
821520b8fb7SFelix Kuehling 			pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
822520b8fb7SFelix Kuehling 				dev->node_props.device_id,
823520b8fb7SFelix Kuehling 				dev->node_props.vendor_id);
824520b8fb7SFelix Kuehling 		} else if (dev->node_props.cpu_cores_count)
825520b8fb7SFelix Kuehling 			pr_info("Topology: Add CPU node\n");
826520b8fb7SFelix Kuehling 		else if (dev->node_props.simd_count)
827520b8fb7SFelix Kuehling 			pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
828520b8fb7SFelix Kuehling 				dev->node_props.device_id,
829520b8fb7SFelix Kuehling 				dev->node_props.vendor_id);
830520b8fb7SFelix Kuehling 	}
831520b8fb7SFelix Kuehling 	up_read(&topology_lock);
832520b8fb7SFelix Kuehling }
833520b8fb7SFelix Kuehling 
834520b8fb7SFelix Kuehling /* Helper function for intializing platform_xx members of
835520b8fb7SFelix Kuehling  * kfd_system_properties. Uses OEM info from the last CPU/APU node.
836520b8fb7SFelix Kuehling  */
837520b8fb7SFelix Kuehling static void kfd_update_system_properties(void)
838520b8fb7SFelix Kuehling {
839520b8fb7SFelix Kuehling 	struct kfd_topology_device *dev;
840520b8fb7SFelix Kuehling 
841520b8fb7SFelix Kuehling 	down_read(&topology_lock);
842520b8fb7SFelix Kuehling 	dev = list_last_entry(&topology_device_list,
843520b8fb7SFelix Kuehling 			struct kfd_topology_device, list);
844520b8fb7SFelix Kuehling 	if (dev) {
845520b8fb7SFelix Kuehling 		sys_props.platform_id =
846520b8fb7SFelix Kuehling 			(*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
847520b8fb7SFelix Kuehling 		sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
848520b8fb7SFelix Kuehling 		sys_props.platform_rev = dev->oem_revision;
849520b8fb7SFelix Kuehling 	}
850520b8fb7SFelix Kuehling 	up_read(&topology_lock);
851520b8fb7SFelix Kuehling }
852520b8fb7SFelix Kuehling 
853520b8fb7SFelix Kuehling static void find_system_memory(const struct dmi_header *dm,
854520b8fb7SFelix Kuehling 	void *private)
855520b8fb7SFelix Kuehling {
856520b8fb7SFelix Kuehling 	struct kfd_mem_properties *mem;
857520b8fb7SFelix Kuehling 	u16 mem_width, mem_clock;
858520b8fb7SFelix Kuehling 	struct kfd_topology_device *kdev =
859520b8fb7SFelix Kuehling 		(struct kfd_topology_device *)private;
860520b8fb7SFelix Kuehling 	const u8 *dmi_data = (const u8 *)(dm + 1);
861520b8fb7SFelix Kuehling 
862520b8fb7SFelix Kuehling 	if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
863520b8fb7SFelix Kuehling 		mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
864520b8fb7SFelix Kuehling 		mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
865520b8fb7SFelix Kuehling 		list_for_each_entry(mem, &kdev->mem_props, list) {
866520b8fb7SFelix Kuehling 			if (mem_width != 0xFFFF && mem_width != 0)
867520b8fb7SFelix Kuehling 				mem->width = mem_width;
868520b8fb7SFelix Kuehling 			if (mem_clock != 0)
869520b8fb7SFelix Kuehling 				mem->mem_clk_max = mem_clock;
870520b8fb7SFelix Kuehling 		}
871520b8fb7SFelix Kuehling 	}
872520b8fb7SFelix Kuehling }
873f4757347SAmber Lin 
874f4757347SAmber Lin /*
875f4757347SAmber Lin  * Performance counters information is not part of CRAT but we would like to
876f4757347SAmber Lin  * put them in the sysfs under topology directory for Thunk to get the data.
877f4757347SAmber Lin  * This function is called before updating the sysfs.
878f4757347SAmber Lin  */
879f4757347SAmber Lin static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
880f4757347SAmber Lin {
88164d1c3a4SFelix Kuehling 	/* These are the only counters supported so far */
88264d1c3a4SFelix Kuehling 	return kfd_iommu_add_perf_counters(kdev);
883f4757347SAmber Lin }
884f4757347SAmber Lin 
885520b8fb7SFelix Kuehling /* kfd_add_non_crat_information - Add information that is not currently
886520b8fb7SFelix Kuehling  *	defined in CRAT but is necessary for KFD topology
887520b8fb7SFelix Kuehling  * @dev - topology device to which addition info is added
888520b8fb7SFelix Kuehling  */
889520b8fb7SFelix Kuehling static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
890520b8fb7SFelix Kuehling {
891520b8fb7SFelix Kuehling 	/* Check if CPU only node. */
892520b8fb7SFelix Kuehling 	if (!kdev->gpu) {
893520b8fb7SFelix Kuehling 		/* Add system memory information */
894520b8fb7SFelix Kuehling 		dmi_walk(find_system_memory, kdev);
895520b8fb7SFelix Kuehling 	}
896520b8fb7SFelix Kuehling 	/* TODO: For GPU node, rearrange code from kfd_topology_add_device */
897520b8fb7SFelix Kuehling }
898520b8fb7SFelix Kuehling 
899b441093eSHarish Kasiviswanathan /* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
900b441093eSHarish Kasiviswanathan  *	Ignore CRAT for all other devices. AMD APU is identified if both CPU
901b441093eSHarish Kasiviswanathan  *	and GPU cores are present.
902b441093eSHarish Kasiviswanathan  * @device_list - topology device list created by parsing ACPI CRAT table.
903b441093eSHarish Kasiviswanathan  * @return - TRUE if invalid, FALSE is valid.
904b441093eSHarish Kasiviswanathan  */
905b441093eSHarish Kasiviswanathan static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
906b441093eSHarish Kasiviswanathan {
907b441093eSHarish Kasiviswanathan 	struct kfd_topology_device *dev;
908b441093eSHarish Kasiviswanathan 
909b441093eSHarish Kasiviswanathan 	list_for_each_entry(dev, device_list, list) {
910b441093eSHarish Kasiviswanathan 		if (dev->node_props.cpu_cores_count &&
911b441093eSHarish Kasiviswanathan 			dev->node_props.simd_count)
912b441093eSHarish Kasiviswanathan 			return false;
913b441093eSHarish Kasiviswanathan 	}
914b441093eSHarish Kasiviswanathan 	pr_info("Ignoring ACPI CRAT on non-APU system\n");
915b441093eSHarish Kasiviswanathan 	return true;
916b441093eSHarish Kasiviswanathan }
917b441093eSHarish Kasiviswanathan 
9185b5c4e40SEvgeny Pinchuk int kfd_topology_init(void)
9195b5c4e40SEvgeny Pinchuk {
92016b9201cSOded Gabbay 	void *crat_image = NULL;
9215b5c4e40SEvgeny Pinchuk 	size_t image_size = 0;
9225b5c4e40SEvgeny Pinchuk 	int ret;
9234f449311SHarish Kasiviswanathan 	struct list_head temp_topology_device_list;
924520b8fb7SFelix Kuehling 	int cpu_only_node = 0;
925520b8fb7SFelix Kuehling 	struct kfd_topology_device *kdev;
926520b8fb7SFelix Kuehling 	int proximity_domain;
9275b5c4e40SEvgeny Pinchuk 
9284f449311SHarish Kasiviswanathan 	/* topology_device_list - Master list of all topology devices
9294f449311SHarish Kasiviswanathan 	 * temp_topology_device_list - temporary list created while parsing CRAT
9304f449311SHarish Kasiviswanathan 	 * or VCRAT. Once parsing is complete the contents of list is moved to
9314f449311SHarish Kasiviswanathan 	 * topology_device_list
9325b5c4e40SEvgeny Pinchuk 	 */
9334f449311SHarish Kasiviswanathan 
9344f449311SHarish Kasiviswanathan 	/* Initialize the head for the both the lists */
9355b5c4e40SEvgeny Pinchuk 	INIT_LIST_HEAD(&topology_device_list);
9364f449311SHarish Kasiviswanathan 	INIT_LIST_HEAD(&temp_topology_device_list);
9375b5c4e40SEvgeny Pinchuk 	init_rwsem(&topology_lock);
9385b5c4e40SEvgeny Pinchuk 
9395b5c4e40SEvgeny Pinchuk 	memset(&sys_props, 0, sizeof(sys_props));
9405b5c4e40SEvgeny Pinchuk 
941520b8fb7SFelix Kuehling 	/* Proximity domains in ACPI CRAT tables start counting at
942520b8fb7SFelix Kuehling 	 * 0. The same should be true for virtual CRAT tables created
943520b8fb7SFelix Kuehling 	 * at this stage. GPUs added later in kfd_topology_add_device
944520b8fb7SFelix Kuehling 	 * use a counter.
945520b8fb7SFelix Kuehling 	 */
946520b8fb7SFelix Kuehling 	proximity_domain = 0;
947520b8fb7SFelix Kuehling 
9485b5c4e40SEvgeny Pinchuk 	/*
949520b8fb7SFelix Kuehling 	 * Get the CRAT image from the ACPI. If ACPI doesn't have one
950b441093eSHarish Kasiviswanathan 	 * or if ACPI CRAT is invalid create a virtual CRAT.
951520b8fb7SFelix Kuehling 	 * NOTE: The current implementation expects all AMD APUs to have
952520b8fb7SFelix Kuehling 	 *	CRAT. If no CRAT is available, it is assumed to be a CPU
9535b5c4e40SEvgeny Pinchuk 	 */
9548e05247dSHarish Kasiviswanathan 	ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
9558e05247dSHarish Kasiviswanathan 	if (!ret) {
9564f449311SHarish Kasiviswanathan 		ret = kfd_parse_crat_table(crat_image,
957520b8fb7SFelix Kuehling 					   &temp_topology_device_list,
958520b8fb7SFelix Kuehling 					   proximity_domain);
959b441093eSHarish Kasiviswanathan 		if (ret ||
960b441093eSHarish Kasiviswanathan 		    kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
961520b8fb7SFelix Kuehling 			kfd_release_topology_device_list(
962520b8fb7SFelix Kuehling 				&temp_topology_device_list);
963520b8fb7SFelix Kuehling 			kfd_destroy_crat_image(crat_image);
964520b8fb7SFelix Kuehling 			crat_image = NULL;
965520b8fb7SFelix Kuehling 		}
966520b8fb7SFelix Kuehling 	}
967520b8fb7SFelix Kuehling 
968520b8fb7SFelix Kuehling 	if (!crat_image) {
969520b8fb7SFelix Kuehling 		ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
970520b8fb7SFelix Kuehling 						    COMPUTE_UNIT_CPU, NULL,
971520b8fb7SFelix Kuehling 						    proximity_domain);
972520b8fb7SFelix Kuehling 		cpu_only_node = 1;
973520b8fb7SFelix Kuehling 		if (ret) {
974520b8fb7SFelix Kuehling 			pr_err("Error creating VCRAT table for CPU\n");
975520b8fb7SFelix Kuehling 			return ret;
976520b8fb7SFelix Kuehling 		}
977520b8fb7SFelix Kuehling 
978520b8fb7SFelix Kuehling 		ret = kfd_parse_crat_table(crat_image,
979520b8fb7SFelix Kuehling 					   &temp_topology_device_list,
980520b8fb7SFelix Kuehling 					   proximity_domain);
981520b8fb7SFelix Kuehling 		if (ret) {
982520b8fb7SFelix Kuehling 			pr_err("Error parsing VCRAT table for CPU\n");
9838e05247dSHarish Kasiviswanathan 			goto err;
984520b8fb7SFelix Kuehling 		}
9855b5c4e40SEvgeny Pinchuk 	}
9865b5c4e40SEvgeny Pinchuk 
987f4757347SAmber Lin 	kdev = list_first_entry(&temp_topology_device_list,
988f4757347SAmber Lin 				struct kfd_topology_device, list);
989f4757347SAmber Lin 	kfd_add_perf_to_topology(kdev);
990f4757347SAmber Lin 
9915b5c4e40SEvgeny Pinchuk 	down_write(&topology_lock);
9924f449311SHarish Kasiviswanathan 	kfd_topology_update_device_list(&temp_topology_device_list,
9934f449311SHarish Kasiviswanathan 					&topology_device_list);
994520b8fb7SFelix Kuehling 	atomic_set(&topology_crat_proximity_domain, sys_props.num_devices-1);
9955b5c4e40SEvgeny Pinchuk 	ret = kfd_topology_update_sysfs();
9965b5c4e40SEvgeny Pinchuk 	up_write(&topology_lock);
9978e05247dSHarish Kasiviswanathan 
9984f449311SHarish Kasiviswanathan 	if (!ret) {
9994f449311SHarish Kasiviswanathan 		sys_props.generation_count++;
1000520b8fb7SFelix Kuehling 		kfd_update_system_properties();
1001520b8fb7SFelix Kuehling 		kfd_debug_print_topology();
10028e05247dSHarish Kasiviswanathan 		pr_info("Finished initializing topology\n");
10034f449311SHarish Kasiviswanathan 	} else
10048e05247dSHarish Kasiviswanathan 		pr_err("Failed to update topology in sysfs ret=%d\n", ret);
10055b5c4e40SEvgeny Pinchuk 
1006520b8fb7SFelix Kuehling 	/* For nodes with GPU, this information gets added
1007520b8fb7SFelix Kuehling 	 * when GPU is detected (kfd_topology_add_device).
1008520b8fb7SFelix Kuehling 	 */
1009520b8fb7SFelix Kuehling 	if (cpu_only_node) {
1010520b8fb7SFelix Kuehling 		/* Add additional information to CPU only node created above */
1011520b8fb7SFelix Kuehling 		down_write(&topology_lock);
1012520b8fb7SFelix Kuehling 		kdev = list_first_entry(&topology_device_list,
1013520b8fb7SFelix Kuehling 				struct kfd_topology_device, list);
1014520b8fb7SFelix Kuehling 		up_write(&topology_lock);
1015520b8fb7SFelix Kuehling 		kfd_add_non_crat_information(kdev);
1016520b8fb7SFelix Kuehling 	}
1017520b8fb7SFelix Kuehling 
10185b5c4e40SEvgeny Pinchuk err:
10198e05247dSHarish Kasiviswanathan 	kfd_destroy_crat_image(crat_image);
10205b5c4e40SEvgeny Pinchuk 	return ret;
10215b5c4e40SEvgeny Pinchuk }
10225b5c4e40SEvgeny Pinchuk 
10235b5c4e40SEvgeny Pinchuk void kfd_topology_shutdown(void)
10245b5c4e40SEvgeny Pinchuk {
10254f449311SHarish Kasiviswanathan 	down_write(&topology_lock);
10265b5c4e40SEvgeny Pinchuk 	kfd_topology_release_sysfs();
10275b5c4e40SEvgeny Pinchuk 	kfd_release_live_view();
10284f449311SHarish Kasiviswanathan 	up_write(&topology_lock);
10295b5c4e40SEvgeny Pinchuk }
10305b5c4e40SEvgeny Pinchuk 
10315b5c4e40SEvgeny Pinchuk static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
10325b5c4e40SEvgeny Pinchuk {
10335b5c4e40SEvgeny Pinchuk 	uint32_t hashout;
10345b5c4e40SEvgeny Pinchuk 	uint32_t buf[7];
1035585f0e6cSEdward O'Callaghan 	uint64_t local_mem_size;
10365b5c4e40SEvgeny Pinchuk 	int i;
10370504cccfSHarish Kasiviswanathan 	struct kfd_local_mem_info local_mem_info;
10385b5c4e40SEvgeny Pinchuk 
10395b5c4e40SEvgeny Pinchuk 	if (!gpu)
10405b5c4e40SEvgeny Pinchuk 		return 0;
10415b5c4e40SEvgeny Pinchuk 
10420504cccfSHarish Kasiviswanathan 	gpu->kfd2kgd->get_local_mem_info(gpu->kgd, &local_mem_info);
10430504cccfSHarish Kasiviswanathan 
10440504cccfSHarish Kasiviswanathan 	local_mem_size = local_mem_info.local_mem_size_private +
10450504cccfSHarish Kasiviswanathan 			local_mem_info.local_mem_size_public;
1046585f0e6cSEdward O'Callaghan 
10475b5c4e40SEvgeny Pinchuk 	buf[0] = gpu->pdev->devfn;
10485b5c4e40SEvgeny Pinchuk 	buf[1] = gpu->pdev->subsystem_vendor;
10495b5c4e40SEvgeny Pinchuk 	buf[2] = gpu->pdev->subsystem_device;
10505b5c4e40SEvgeny Pinchuk 	buf[3] = gpu->pdev->device;
10515b5c4e40SEvgeny Pinchuk 	buf[4] = gpu->pdev->bus->number;
1052585f0e6cSEdward O'Callaghan 	buf[5] = lower_32_bits(local_mem_size);
1053585f0e6cSEdward O'Callaghan 	buf[6] = upper_32_bits(local_mem_size);
10545b5c4e40SEvgeny Pinchuk 
10555b5c4e40SEvgeny Pinchuk 	for (i = 0, hashout = 0; i < 7; i++)
10565b5c4e40SEvgeny Pinchuk 		hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
10575b5c4e40SEvgeny Pinchuk 
10585b5c4e40SEvgeny Pinchuk 	return hashout;
10595b5c4e40SEvgeny Pinchuk }
10603a87177eSHarish Kasiviswanathan /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
10613a87177eSHarish Kasiviswanathan  *		the GPU device is not already present in the topology device
10623a87177eSHarish Kasiviswanathan  *		list then return NULL. This means a new topology device has to
10633a87177eSHarish Kasiviswanathan  *		be created for this GPU.
10643a87177eSHarish Kasiviswanathan  * TODO: Rather than assiging @gpu to first topology device withtout
10653a87177eSHarish Kasiviswanathan  *		gpu attached, it will better to have more stringent check.
10663a87177eSHarish Kasiviswanathan  */
10675b5c4e40SEvgeny Pinchuk static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
10685b5c4e40SEvgeny Pinchuk {
10695b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
107016b9201cSOded Gabbay 	struct kfd_topology_device *out_dev = NULL;
10715b5c4e40SEvgeny Pinchuk 
10723a87177eSHarish Kasiviswanathan 	down_write(&topology_lock);
10735b5c4e40SEvgeny Pinchuk 	list_for_each_entry(dev, &topology_device_list, list)
10744eacc26bSKent Russell 		if (!dev->gpu && (dev->node_props.simd_count > 0)) {
10755b5c4e40SEvgeny Pinchuk 			dev->gpu = gpu;
10765b5c4e40SEvgeny Pinchuk 			out_dev = dev;
10775b5c4e40SEvgeny Pinchuk 			break;
10785b5c4e40SEvgeny Pinchuk 		}
10793a87177eSHarish Kasiviswanathan 	up_write(&topology_lock);
10805b5c4e40SEvgeny Pinchuk 	return out_dev;
10815b5c4e40SEvgeny Pinchuk }
10825b5c4e40SEvgeny Pinchuk 
10835b5c4e40SEvgeny Pinchuk static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
10845b5c4e40SEvgeny Pinchuk {
10855b5c4e40SEvgeny Pinchuk 	/*
10865b5c4e40SEvgeny Pinchuk 	 * TODO: Generate an event for thunk about the arrival/removal
10875b5c4e40SEvgeny Pinchuk 	 * of the GPU
10885b5c4e40SEvgeny Pinchuk 	 */
10895b5c4e40SEvgeny Pinchuk }
10905b5c4e40SEvgeny Pinchuk 
10913a87177eSHarish Kasiviswanathan /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
10923a87177eSHarish Kasiviswanathan  *		patch this after CRAT parsing.
10933a87177eSHarish Kasiviswanathan  */
10943a87177eSHarish Kasiviswanathan static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
10953a87177eSHarish Kasiviswanathan {
10963a87177eSHarish Kasiviswanathan 	struct kfd_mem_properties *mem;
10973a87177eSHarish Kasiviswanathan 	struct kfd_local_mem_info local_mem_info;
10983a87177eSHarish Kasiviswanathan 
10993a87177eSHarish Kasiviswanathan 	if (!dev)
11003a87177eSHarish Kasiviswanathan 		return;
11013a87177eSHarish Kasiviswanathan 
11023a87177eSHarish Kasiviswanathan 	/* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
11033a87177eSHarish Kasiviswanathan 	 * single bank of VRAM local memory.
11043a87177eSHarish Kasiviswanathan 	 * for dGPUs - VCRAT reports only one bank of Local Memory
11053a87177eSHarish Kasiviswanathan 	 * for APUs - If CRAT from ACPI reports more than one bank, then
11063a87177eSHarish Kasiviswanathan 	 *	all the banks will report the same mem_clk_max information
11073a87177eSHarish Kasiviswanathan 	 */
11083a87177eSHarish Kasiviswanathan 	dev->gpu->kfd2kgd->get_local_mem_info(dev->gpu->kgd,
11093a87177eSHarish Kasiviswanathan 		&local_mem_info);
11103a87177eSHarish Kasiviswanathan 
11113a87177eSHarish Kasiviswanathan 	list_for_each_entry(mem, &dev->mem_props, list)
11123a87177eSHarish Kasiviswanathan 		mem->mem_clk_max = local_mem_info.mem_clk_max;
11133a87177eSHarish Kasiviswanathan }
11143a87177eSHarish Kasiviswanathan 
11153a87177eSHarish Kasiviswanathan static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
11163a87177eSHarish Kasiviswanathan {
11173a87177eSHarish Kasiviswanathan 	struct kfd_iolink_properties *link;
11183a87177eSHarish Kasiviswanathan 
11193a87177eSHarish Kasiviswanathan 	if (!dev || !dev->gpu)
11203a87177eSHarish Kasiviswanathan 		return;
11213a87177eSHarish Kasiviswanathan 
11223a87177eSHarish Kasiviswanathan 	/* GPU only creates direck links so apply flags setting to all */
11233a87177eSHarish Kasiviswanathan 	if (dev->gpu->device_info->asic_family == CHIP_HAWAII)
11243a87177eSHarish Kasiviswanathan 		list_for_each_entry(link, &dev->io_link_props, list)
11253a87177eSHarish Kasiviswanathan 			link->flags = CRAT_IOLINK_FLAGS_ENABLED |
11263a87177eSHarish Kasiviswanathan 				CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
11273a87177eSHarish Kasiviswanathan 				CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
11283a87177eSHarish Kasiviswanathan }
11293a87177eSHarish Kasiviswanathan 
11305b5c4e40SEvgeny Pinchuk int kfd_topology_add_device(struct kfd_dev *gpu)
11315b5c4e40SEvgeny Pinchuk {
11325b5c4e40SEvgeny Pinchuk 	uint32_t gpu_id;
11335b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
1134f7ce2fadSFlora Cui 	struct kfd_cu_info cu_info;
11354f449311SHarish Kasiviswanathan 	int res = 0;
11364f449311SHarish Kasiviswanathan 	struct list_head temp_topology_device_list;
11373a87177eSHarish Kasiviswanathan 	void *crat_image = NULL;
11383a87177eSHarish Kasiviswanathan 	size_t image_size = 0;
11393a87177eSHarish Kasiviswanathan 	int proximity_domain;
11404f449311SHarish Kasiviswanathan 
11414f449311SHarish Kasiviswanathan 	INIT_LIST_HEAD(&temp_topology_device_list);
11425b5c4e40SEvgeny Pinchuk 
11435b5c4e40SEvgeny Pinchuk 	gpu_id = kfd_generate_gpu_id(gpu);
11445b5c4e40SEvgeny Pinchuk 
114579775b62SKent Russell 	pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
11465b5c4e40SEvgeny Pinchuk 
11473a87177eSHarish Kasiviswanathan 	proximity_domain = atomic_inc_return(&topology_crat_proximity_domain);
11483a87177eSHarish Kasiviswanathan 
11493a87177eSHarish Kasiviswanathan 	/* Check to see if this gpu device exists in the topology_device_list.
11503a87177eSHarish Kasiviswanathan 	 * If so, assign the gpu to that device,
11513a87177eSHarish Kasiviswanathan 	 * else create a Virtual CRAT for this gpu device and then parse that
11523a87177eSHarish Kasiviswanathan 	 * CRAT to create a new topology device. Once created assign the gpu to
11533a87177eSHarish Kasiviswanathan 	 * that topology device
11545b5c4e40SEvgeny Pinchuk 	 */
11555b5c4e40SEvgeny Pinchuk 	dev = kfd_assign_gpu(gpu);
11565b5c4e40SEvgeny Pinchuk 	if (!dev) {
11573a87177eSHarish Kasiviswanathan 		res = kfd_create_crat_image_virtual(&crat_image, &image_size,
11583a87177eSHarish Kasiviswanathan 						    COMPUTE_UNIT_GPU, gpu,
11593a87177eSHarish Kasiviswanathan 						    proximity_domain);
11603a87177eSHarish Kasiviswanathan 		if (res) {
11613a87177eSHarish Kasiviswanathan 			pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
11623a87177eSHarish Kasiviswanathan 			       gpu_id);
11633a87177eSHarish Kasiviswanathan 			return res;
11643a87177eSHarish Kasiviswanathan 		}
11653a87177eSHarish Kasiviswanathan 		res = kfd_parse_crat_table(crat_image,
11663a87177eSHarish Kasiviswanathan 					   &temp_topology_device_list,
11673a87177eSHarish Kasiviswanathan 					   proximity_domain);
11683a87177eSHarish Kasiviswanathan 		if (res) {
11693a87177eSHarish Kasiviswanathan 			pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
11703a87177eSHarish Kasiviswanathan 			       gpu_id);
11715b5c4e40SEvgeny Pinchuk 			goto err;
11725b5c4e40SEvgeny Pinchuk 		}
11734f449311SHarish Kasiviswanathan 
11744f449311SHarish Kasiviswanathan 		down_write(&topology_lock);
11754f449311SHarish Kasiviswanathan 		kfd_topology_update_device_list(&temp_topology_device_list,
11764f449311SHarish Kasiviswanathan 			&topology_device_list);
11774f449311SHarish Kasiviswanathan 
11788eabaf54SKent Russell 		/* Update the SYSFS tree, since we added another topology
11798eabaf54SKent Russell 		 * device
11805b5c4e40SEvgeny Pinchuk 		 */
11813a87177eSHarish Kasiviswanathan 		res = kfd_topology_update_sysfs();
11824f449311SHarish Kasiviswanathan 		up_write(&topology_lock);
11834f449311SHarish Kasiviswanathan 
11843a87177eSHarish Kasiviswanathan 		if (!res)
11853a87177eSHarish Kasiviswanathan 			sys_props.generation_count++;
11863a87177eSHarish Kasiviswanathan 		else
11873a87177eSHarish Kasiviswanathan 			pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
11883a87177eSHarish Kasiviswanathan 						gpu_id, res);
11893a87177eSHarish Kasiviswanathan 		dev = kfd_assign_gpu(gpu);
11903a87177eSHarish Kasiviswanathan 		if (WARN_ON(!dev)) {
11913a87177eSHarish Kasiviswanathan 			res = -ENODEV;
11923a87177eSHarish Kasiviswanathan 			goto err;
11933a87177eSHarish Kasiviswanathan 		}
11945b5c4e40SEvgeny Pinchuk 	}
11955b5c4e40SEvgeny Pinchuk 
11965b5c4e40SEvgeny Pinchuk 	dev->gpu_id = gpu_id;
11975b5c4e40SEvgeny Pinchuk 	gpu->id = gpu_id;
11983a87177eSHarish Kasiviswanathan 
11993a87177eSHarish Kasiviswanathan 	/* TODO: Move the following lines to function
12003a87177eSHarish Kasiviswanathan 	 *	kfd_add_non_crat_information
12013a87177eSHarish Kasiviswanathan 	 */
12023a87177eSHarish Kasiviswanathan 
12033a87177eSHarish Kasiviswanathan 	/* Fill-in additional information that is not available in CRAT but
12043a87177eSHarish Kasiviswanathan 	 * needed for the topology
12053a87177eSHarish Kasiviswanathan 	 */
12063a87177eSHarish Kasiviswanathan 
1207f7ce2fadSFlora Cui 	dev->gpu->kfd2kgd->get_cu_info(dev->gpu->kgd, &cu_info);
12083a87177eSHarish Kasiviswanathan 	dev->node_props.simd_arrays_per_engine =
12093a87177eSHarish Kasiviswanathan 		cu_info.num_shader_arrays_per_engine;
12103a87177eSHarish Kasiviswanathan 
12115b5c4e40SEvgeny Pinchuk 	dev->node_props.vendor_id = gpu->pdev->vendor;
12125b5c4e40SEvgeny Pinchuk 	dev->node_props.device_id = gpu->pdev->device;
1213d63f0ba2SHarish Kasiviswanathan 	dev->node_props.location_id = PCI_DEVID(gpu->pdev->bus->number,
1214d63f0ba2SHarish Kasiviswanathan 		gpu->pdev->devfn);
12153a87177eSHarish Kasiviswanathan 	dev->node_props.max_engine_clk_fcompute =
12163a87177eSHarish Kasiviswanathan 		dev->gpu->kfd2kgd->get_max_engine_clock_in_mhz(dev->gpu->kgd);
12173a87177eSHarish Kasiviswanathan 	dev->node_props.max_engine_clk_ccompute =
12183a87177eSHarish Kasiviswanathan 		cpufreq_quick_get_max(0) / 1000;
12197c9b7171SOak Zeng 	dev->node_props.drm_render_minor =
12207c9b7171SOak Zeng 		gpu->shared_resources.drm_render_minor;
12215b5c4e40SEvgeny Pinchuk 
12223a87177eSHarish Kasiviswanathan 	kfd_fill_mem_clk_max_info(dev);
12233a87177eSHarish Kasiviswanathan 	kfd_fill_iolink_non_crat_info(dev);
12243a87177eSHarish Kasiviswanathan 
12253a87177eSHarish Kasiviswanathan 	switch (dev->gpu->device_info->asic_family) {
12263a87177eSHarish Kasiviswanathan 	case CHIP_KAVERI:
12273a87177eSHarish Kasiviswanathan 	case CHIP_HAWAII:
12283a87177eSHarish Kasiviswanathan 	case CHIP_TONGA:
12293a87177eSHarish Kasiviswanathan 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
12303a87177eSHarish Kasiviswanathan 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
12313a87177eSHarish Kasiviswanathan 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
12323a87177eSHarish Kasiviswanathan 		break;
12333a87177eSHarish Kasiviswanathan 	case CHIP_CARRIZO:
12343a87177eSHarish Kasiviswanathan 	case CHIP_FIJI:
12353a87177eSHarish Kasiviswanathan 	case CHIP_POLARIS10:
12363a87177eSHarish Kasiviswanathan 	case CHIP_POLARIS11:
123742aa8793SFelix Kuehling 		pr_debug("Adding doorbell packet type capability\n");
12383a87177eSHarish Kasiviswanathan 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
12393a87177eSHarish Kasiviswanathan 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
12403a87177eSHarish Kasiviswanathan 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
12413a87177eSHarish Kasiviswanathan 		break;
1242389056e5SFelix Kuehling 	case CHIP_VEGA10:
1243389056e5SFelix Kuehling 	case CHIP_RAVEN:
1244389056e5SFelix Kuehling 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1245389056e5SFelix Kuehling 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1246389056e5SFelix Kuehling 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1247389056e5SFelix Kuehling 		break;
12483a87177eSHarish Kasiviswanathan 	default:
12493a87177eSHarish Kasiviswanathan 		WARN(1, "Unexpected ASIC family %u",
12503a87177eSHarish Kasiviswanathan 		     dev->gpu->device_info->asic_family);
12517639a8c4SBen Goz 	}
12527639a8c4SBen Goz 
12533a87177eSHarish Kasiviswanathan 	/* Fix errors in CZ CRAT.
12543a87177eSHarish Kasiviswanathan 	 * simd_count: Carrizo CRAT reports wrong simd_count, probably
12553a87177eSHarish Kasiviswanathan 	 *		because it doesn't consider masked out CUs
125670f372bfSPhilip Cox 	 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
125770f372bfSPhilip Cox 	 * capability flag: Carrizo CRAT doesn't report IOMMU flags
12583a87177eSHarish Kasiviswanathan 	 */
125970f372bfSPhilip Cox 	if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
12603a87177eSHarish Kasiviswanathan 		dev->node_props.simd_count =
12613a87177eSHarish Kasiviswanathan 			cu_info.simd_per_cu * cu_info.cu_active_number;
126270f372bfSPhilip Cox 		dev->node_props.max_waves_per_simd = 10;
126370f372bfSPhilip Cox 		dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
126470f372bfSPhilip Cox 	}
12653a87177eSHarish Kasiviswanathan 
12663a87177eSHarish Kasiviswanathan 	kfd_debug_print_topology();
12673a87177eSHarish Kasiviswanathan 
12684f449311SHarish Kasiviswanathan 	if (!res)
12695b5c4e40SEvgeny Pinchuk 		kfd_notify_gpu_change(gpu_id, 1);
12704f449311SHarish Kasiviswanathan err:
12713a87177eSHarish Kasiviswanathan 	kfd_destroy_crat_image(crat_image);
12725b5c4e40SEvgeny Pinchuk 	return res;
12735b5c4e40SEvgeny Pinchuk }
12745b5c4e40SEvgeny Pinchuk 
12755b5c4e40SEvgeny Pinchuk int kfd_topology_remove_device(struct kfd_dev *gpu)
12765b5c4e40SEvgeny Pinchuk {
12774f449311SHarish Kasiviswanathan 	struct kfd_topology_device *dev, *tmp;
12785b5c4e40SEvgeny Pinchuk 	uint32_t gpu_id;
12795b5c4e40SEvgeny Pinchuk 	int res = -ENODEV;
12805b5c4e40SEvgeny Pinchuk 
12815b5c4e40SEvgeny Pinchuk 	down_write(&topology_lock);
12825b5c4e40SEvgeny Pinchuk 
12834f449311SHarish Kasiviswanathan 	list_for_each_entry_safe(dev, tmp, &topology_device_list, list)
12845b5c4e40SEvgeny Pinchuk 		if (dev->gpu == gpu) {
12855b5c4e40SEvgeny Pinchuk 			gpu_id = dev->gpu_id;
12865b5c4e40SEvgeny Pinchuk 			kfd_remove_sysfs_node_entry(dev);
12875b5c4e40SEvgeny Pinchuk 			kfd_release_topology_device(dev);
12884f449311SHarish Kasiviswanathan 			sys_props.num_devices--;
12895b5c4e40SEvgeny Pinchuk 			res = 0;
12905b5c4e40SEvgeny Pinchuk 			if (kfd_topology_update_sysfs() < 0)
12915b5c4e40SEvgeny Pinchuk 				kfd_topology_release_sysfs();
12925b5c4e40SEvgeny Pinchuk 			break;
12935b5c4e40SEvgeny Pinchuk 		}
12945b5c4e40SEvgeny Pinchuk 
12955b5c4e40SEvgeny Pinchuk 	up_write(&topology_lock);
12965b5c4e40SEvgeny Pinchuk 
1297174de876SFelix Kuehling 	if (!res)
12985b5c4e40SEvgeny Pinchuk 		kfd_notify_gpu_change(gpu_id, 0);
12995b5c4e40SEvgeny Pinchuk 
13005b5c4e40SEvgeny Pinchuk 	return res;
13015b5c4e40SEvgeny Pinchuk }
13025b5c4e40SEvgeny Pinchuk 
13036d82eb0eSHarish Kasiviswanathan /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
13046d82eb0eSHarish Kasiviswanathan  *	topology. If GPU device is found @idx, then valid kfd_dev pointer is
13056d82eb0eSHarish Kasiviswanathan  *	returned through @kdev
13066d82eb0eSHarish Kasiviswanathan  * Return -	0: On success (@kdev will be NULL for non GPU nodes)
13076d82eb0eSHarish Kasiviswanathan  *		-1: If end of list
13085b5c4e40SEvgeny Pinchuk  */
13096d82eb0eSHarish Kasiviswanathan int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
13105b5c4e40SEvgeny Pinchuk {
13115b5c4e40SEvgeny Pinchuk 
13125b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *top_dev;
13135b5c4e40SEvgeny Pinchuk 	uint8_t device_idx = 0;
13145b5c4e40SEvgeny Pinchuk 
13156d82eb0eSHarish Kasiviswanathan 	*kdev = NULL;
13165b5c4e40SEvgeny Pinchuk 	down_read(&topology_lock);
13175b5c4e40SEvgeny Pinchuk 
13185b5c4e40SEvgeny Pinchuk 	list_for_each_entry(top_dev, &topology_device_list, list) {
13195b5c4e40SEvgeny Pinchuk 		if (device_idx == idx) {
13206d82eb0eSHarish Kasiviswanathan 			*kdev = top_dev->gpu;
13216d82eb0eSHarish Kasiviswanathan 			up_read(&topology_lock);
13226d82eb0eSHarish Kasiviswanathan 			return 0;
13235b5c4e40SEvgeny Pinchuk 		}
13245b5c4e40SEvgeny Pinchuk 
13255b5c4e40SEvgeny Pinchuk 		device_idx++;
13265b5c4e40SEvgeny Pinchuk 	}
13275b5c4e40SEvgeny Pinchuk 
13285b5c4e40SEvgeny Pinchuk 	up_read(&topology_lock);
13295b5c4e40SEvgeny Pinchuk 
13306d82eb0eSHarish Kasiviswanathan 	return -1;
13315b5c4e40SEvgeny Pinchuk 
13325b5c4e40SEvgeny Pinchuk }
1333851a645eSFelix Kuehling 
1334520b8fb7SFelix Kuehling static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
1335520b8fb7SFelix Kuehling {
1336520b8fb7SFelix Kuehling 	const struct cpuinfo_x86 *cpuinfo;
1337520b8fb7SFelix Kuehling 	int first_cpu_of_numa_node;
1338520b8fb7SFelix Kuehling 
1339520b8fb7SFelix Kuehling 	if (!cpumask || cpumask == cpu_none_mask)
1340520b8fb7SFelix Kuehling 		return -1;
1341520b8fb7SFelix Kuehling 	first_cpu_of_numa_node = cpumask_first(cpumask);
1342520b8fb7SFelix Kuehling 	if (first_cpu_of_numa_node >= nr_cpu_ids)
1343520b8fb7SFelix Kuehling 		return -1;
1344520b8fb7SFelix Kuehling 	cpuinfo = &cpu_data(first_cpu_of_numa_node);
1345520b8fb7SFelix Kuehling 
1346520b8fb7SFelix Kuehling 	return cpuinfo->apicid;
1347520b8fb7SFelix Kuehling }
1348520b8fb7SFelix Kuehling 
1349520b8fb7SFelix Kuehling /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
1350520b8fb7SFelix Kuehling  *	of the given NUMA node (numa_node_id)
1351520b8fb7SFelix Kuehling  * Return -1 on failure
1352520b8fb7SFelix Kuehling  */
1353520b8fb7SFelix Kuehling int kfd_numa_node_to_apic_id(int numa_node_id)
1354520b8fb7SFelix Kuehling {
1355520b8fb7SFelix Kuehling 	if (numa_node_id == -1) {
1356520b8fb7SFelix Kuehling 		pr_warn("Invalid NUMA Node. Use online CPU mask\n");
1357520b8fb7SFelix Kuehling 		return kfd_cpumask_to_apic_id(cpu_online_mask);
1358520b8fb7SFelix Kuehling 	}
1359520b8fb7SFelix Kuehling 	return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
1360520b8fb7SFelix Kuehling }
1361520b8fb7SFelix Kuehling 
1362851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS)
1363851a645eSFelix Kuehling 
1364851a645eSFelix Kuehling int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
1365851a645eSFelix Kuehling {
1366851a645eSFelix Kuehling 	struct kfd_topology_device *dev;
1367851a645eSFelix Kuehling 	unsigned int i = 0;
1368851a645eSFelix Kuehling 	int r = 0;
1369851a645eSFelix Kuehling 
1370851a645eSFelix Kuehling 	down_read(&topology_lock);
1371851a645eSFelix Kuehling 
1372851a645eSFelix Kuehling 	list_for_each_entry(dev, &topology_device_list, list) {
1373851a645eSFelix Kuehling 		if (!dev->gpu) {
1374851a645eSFelix Kuehling 			i++;
1375851a645eSFelix Kuehling 			continue;
1376851a645eSFelix Kuehling 		}
1377851a645eSFelix Kuehling 
1378851a645eSFelix Kuehling 		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1379851a645eSFelix Kuehling 		r = dqm_debugfs_hqds(m, dev->gpu->dqm);
1380851a645eSFelix Kuehling 		if (r)
1381851a645eSFelix Kuehling 			break;
1382851a645eSFelix Kuehling 	}
1383851a645eSFelix Kuehling 
1384851a645eSFelix Kuehling 	up_read(&topology_lock);
1385851a645eSFelix Kuehling 
1386851a645eSFelix Kuehling 	return r;
1387851a645eSFelix Kuehling }
1388851a645eSFelix Kuehling 
1389851a645eSFelix Kuehling int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
1390851a645eSFelix Kuehling {
1391851a645eSFelix Kuehling 	struct kfd_topology_device *dev;
1392851a645eSFelix Kuehling 	unsigned int i = 0;
1393851a645eSFelix Kuehling 	int r = 0;
1394851a645eSFelix Kuehling 
1395851a645eSFelix Kuehling 	down_read(&topology_lock);
1396851a645eSFelix Kuehling 
1397851a645eSFelix Kuehling 	list_for_each_entry(dev, &topology_device_list, list) {
1398851a645eSFelix Kuehling 		if (!dev->gpu) {
1399851a645eSFelix Kuehling 			i++;
1400851a645eSFelix Kuehling 			continue;
1401851a645eSFelix Kuehling 		}
1402851a645eSFelix Kuehling 
1403851a645eSFelix Kuehling 		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1404851a645eSFelix Kuehling 		r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets);
1405851a645eSFelix Kuehling 		if (r)
1406851a645eSFelix Kuehling 			break;
1407851a645eSFelix Kuehling 	}
1408851a645eSFelix Kuehling 
1409851a645eSFelix Kuehling 	up_read(&topology_lock);
1410851a645eSFelix Kuehling 
1411851a645eSFelix Kuehling 	return r;
1412851a645eSFelix Kuehling }
1413851a645eSFelix Kuehling 
1414851a645eSFelix Kuehling #endif
1415