1d87f36a0SRajneesh Bhardwaj // SPDX-License-Identifier: GPL-2.0 OR MIT
25b5c4e40SEvgeny Pinchuk /*
3d87f36a0SRajneesh Bhardwaj  * Copyright 2014-2022 Advanced Micro Devices, Inc.
45b5c4e40SEvgeny Pinchuk  *
55b5c4e40SEvgeny Pinchuk  * Permission is hereby granted, free of charge, to any person obtaining a
65b5c4e40SEvgeny Pinchuk  * copy of this software and associated documentation files (the "Software"),
75b5c4e40SEvgeny Pinchuk  * to deal in the Software without restriction, including without limitation
85b5c4e40SEvgeny Pinchuk  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
95b5c4e40SEvgeny Pinchuk  * and/or sell copies of the Software, and to permit persons to whom the
105b5c4e40SEvgeny Pinchuk  * Software is furnished to do so, subject to the following conditions:
115b5c4e40SEvgeny Pinchuk  *
125b5c4e40SEvgeny Pinchuk  * The above copyright notice and this permission notice shall be included in
135b5c4e40SEvgeny Pinchuk  * all copies or substantial portions of the Software.
145b5c4e40SEvgeny Pinchuk  *
155b5c4e40SEvgeny Pinchuk  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
165b5c4e40SEvgeny Pinchuk  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
175b5c4e40SEvgeny Pinchuk  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
185b5c4e40SEvgeny Pinchuk  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
195b5c4e40SEvgeny Pinchuk  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
205b5c4e40SEvgeny Pinchuk  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
215b5c4e40SEvgeny Pinchuk  * OTHER DEALINGS IN THE SOFTWARE.
225b5c4e40SEvgeny Pinchuk  */
235b5c4e40SEvgeny Pinchuk 
245b5c4e40SEvgeny Pinchuk #include <linux/types.h>
255b5c4e40SEvgeny Pinchuk #include <linux/kernel.h>
265b5c4e40SEvgeny Pinchuk #include <linux/pci.h>
275b5c4e40SEvgeny Pinchuk #include <linux/errno.h>
285b5c4e40SEvgeny Pinchuk #include <linux/acpi.h>
295b5c4e40SEvgeny Pinchuk #include <linux/hash.h>
305b5c4e40SEvgeny Pinchuk #include <linux/cpufreq.h>
31f7c826adSAlexey Skidanov #include <linux/log2.h>
32520b8fb7SFelix Kuehling #include <linux/dmi.h>
33520b8fb7SFelix Kuehling #include <linux/atomic.h>
345b5c4e40SEvgeny Pinchuk 
355b5c4e40SEvgeny Pinchuk #include "kfd_priv.h"
365b5c4e40SEvgeny Pinchuk #include "kfd_crat.h"
375b5c4e40SEvgeny Pinchuk #include "kfd_topology.h"
38851a645eSFelix Kuehling #include "kfd_device_queue_manager.h"
395a75ea56SFelix Kuehling #include "kfd_svm.h"
40fc7f1d96SJonathan Kim #include "kfd_debug.h"
415b87245fSAmber Lin #include "amdgpu_amdkfd.h"
420dee45a2SEric Huang #include "amdgpu_ras.h"
430f28cca8SRamesh Errabolu #include "amdgpu.h"
445b5c4e40SEvgeny Pinchuk 
454f449311SHarish Kasiviswanathan /* topology_device_list - Master list of all topology devices */
464f449311SHarish Kasiviswanathan static struct list_head topology_device_list;
47520b8fb7SFelix Kuehling static struct kfd_system_properties sys_props;
485b5c4e40SEvgeny Pinchuk 
495b5c4e40SEvgeny Pinchuk static DECLARE_RWSEM(topology_lock);
5046d18d51SMukul Joshi static uint32_t topology_crat_proximity_domain;
515b5c4e40SEvgeny Pinchuk 
kfd_topology_device_by_proximity_domain_no_lock(uint32_t proximity_domain)5246d18d51SMukul Joshi struct kfd_topology_device *kfd_topology_device_by_proximity_domain_no_lock(
533a87177eSHarish Kasiviswanathan 						uint32_t proximity_domain)
543a87177eSHarish Kasiviswanathan {
553a87177eSHarish Kasiviswanathan 	struct kfd_topology_device *top_dev;
563a87177eSHarish Kasiviswanathan 	struct kfd_topology_device *device = NULL;
573a87177eSHarish Kasiviswanathan 
583a87177eSHarish Kasiviswanathan 	list_for_each_entry(top_dev, &topology_device_list, list)
593a87177eSHarish Kasiviswanathan 		if (top_dev->proximity_domain == proximity_domain) {
603a87177eSHarish Kasiviswanathan 			device = top_dev;
613a87177eSHarish Kasiviswanathan 			break;
623a87177eSHarish Kasiviswanathan 		}
633a87177eSHarish Kasiviswanathan 
6446d18d51SMukul Joshi 	return device;
6546d18d51SMukul Joshi }
6646d18d51SMukul Joshi 
kfd_topology_device_by_proximity_domain(uint32_t proximity_domain)6746d18d51SMukul Joshi struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
6846d18d51SMukul Joshi 						uint32_t proximity_domain)
6946d18d51SMukul Joshi {
7046d18d51SMukul Joshi 	struct kfd_topology_device *device = NULL;
7146d18d51SMukul Joshi 
7246d18d51SMukul Joshi 	down_read(&topology_lock);
7346d18d51SMukul Joshi 
7446d18d51SMukul Joshi 	device = kfd_topology_device_by_proximity_domain_no_lock(
7546d18d51SMukul Joshi 							proximity_domain);
763a87177eSHarish Kasiviswanathan 	up_read(&topology_lock);
773a87177eSHarish Kasiviswanathan 
783a87177eSHarish Kasiviswanathan 	return device;
793a87177eSHarish Kasiviswanathan }
803a87177eSHarish Kasiviswanathan 
kfd_topology_device_by_id(uint32_t gpu_id)8144d8cc6fSYong Zhao struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
825b5c4e40SEvgeny Pinchuk {
8344d8cc6fSYong Zhao 	struct kfd_topology_device *top_dev = NULL;
8444d8cc6fSYong Zhao 	struct kfd_topology_device *ret = NULL;
855b5c4e40SEvgeny Pinchuk 
865b5c4e40SEvgeny Pinchuk 	down_read(&topology_lock);
875b5c4e40SEvgeny Pinchuk 
885b5c4e40SEvgeny Pinchuk 	list_for_each_entry(top_dev, &topology_device_list, list)
895b5c4e40SEvgeny Pinchuk 		if (top_dev->gpu_id == gpu_id) {
9044d8cc6fSYong Zhao 			ret = top_dev;
915b5c4e40SEvgeny Pinchuk 			break;
925b5c4e40SEvgeny Pinchuk 		}
935b5c4e40SEvgeny Pinchuk 
945b5c4e40SEvgeny Pinchuk 	up_read(&topology_lock);
955b5c4e40SEvgeny Pinchuk 
9644d8cc6fSYong Zhao 	return ret;
9744d8cc6fSYong Zhao }
9844d8cc6fSYong Zhao 
kfd_device_by_id(uint32_t gpu_id)998dc1db31SMukul Joshi struct kfd_node *kfd_device_by_id(uint32_t gpu_id)
10044d8cc6fSYong Zhao {
10144d8cc6fSYong Zhao 	struct kfd_topology_device *top_dev;
10244d8cc6fSYong Zhao 
10344d8cc6fSYong Zhao 	top_dev = kfd_topology_device_by_id(gpu_id);
10444d8cc6fSYong Zhao 	if (!top_dev)
10544d8cc6fSYong Zhao 		return NULL;
10644d8cc6fSYong Zhao 
10744d8cc6fSYong Zhao 	return top_dev->gpu;
1085b5c4e40SEvgeny Pinchuk }
1095b5c4e40SEvgeny Pinchuk 
kfd_device_by_pci_dev(const struct pci_dev * pdev)1108dc1db31SMukul Joshi struct kfd_node *kfd_device_by_pci_dev(const struct pci_dev *pdev)
1115b5c4e40SEvgeny Pinchuk {
1125b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *top_dev;
1138dc1db31SMukul Joshi 	struct kfd_node *device = NULL;
1145b5c4e40SEvgeny Pinchuk 
1155b5c4e40SEvgeny Pinchuk 	down_read(&topology_lock);
1165b5c4e40SEvgeny Pinchuk 
1175b5c4e40SEvgeny Pinchuk 	list_for_each_entry(top_dev, &topology_device_list, list)
118d69a3b76SMukul Joshi 		if (top_dev->gpu && top_dev->gpu->adev->pdev == pdev) {
1195b5c4e40SEvgeny Pinchuk 			device = top_dev->gpu;
1205b5c4e40SEvgeny Pinchuk 			break;
1215b5c4e40SEvgeny Pinchuk 		}
1225b5c4e40SEvgeny Pinchuk 
1235b5c4e40SEvgeny Pinchuk 	up_read(&topology_lock);
1245b5c4e40SEvgeny Pinchuk 
1255b5c4e40SEvgeny Pinchuk 	return device;
1265b5c4e40SEvgeny Pinchuk }
1275b5c4e40SEvgeny Pinchuk 
1283a87177eSHarish Kasiviswanathan /* Called with write topology_lock acquired */
kfd_release_topology_device(struct kfd_topology_device * dev)1295b5c4e40SEvgeny Pinchuk static void kfd_release_topology_device(struct kfd_topology_device *dev)
1305b5c4e40SEvgeny Pinchuk {
1315b5c4e40SEvgeny Pinchuk 	struct kfd_mem_properties *mem;
1325b5c4e40SEvgeny Pinchuk 	struct kfd_cache_properties *cache;
1335b5c4e40SEvgeny Pinchuk 	struct kfd_iolink_properties *iolink;
1340f28cca8SRamesh Errabolu 	struct kfd_iolink_properties *p2plink;
135f4757347SAmber Lin 	struct kfd_perf_properties *perf;
1365b5c4e40SEvgeny Pinchuk 
1375b5c4e40SEvgeny Pinchuk 	list_del(&dev->list);
1385b5c4e40SEvgeny Pinchuk 
1395b5c4e40SEvgeny Pinchuk 	while (dev->mem_props.next != &dev->mem_props) {
1405b5c4e40SEvgeny Pinchuk 		mem = container_of(dev->mem_props.next,
1415b5c4e40SEvgeny Pinchuk 				struct kfd_mem_properties, list);
1425b5c4e40SEvgeny Pinchuk 		list_del(&mem->list);
1435b5c4e40SEvgeny Pinchuk 		kfree(mem);
1445b5c4e40SEvgeny Pinchuk 	}
1455b5c4e40SEvgeny Pinchuk 
1465b5c4e40SEvgeny Pinchuk 	while (dev->cache_props.next != &dev->cache_props) {
1475b5c4e40SEvgeny Pinchuk 		cache = container_of(dev->cache_props.next,
1485b5c4e40SEvgeny Pinchuk 				struct kfd_cache_properties, list);
1495b5c4e40SEvgeny Pinchuk 		list_del(&cache->list);
1505b5c4e40SEvgeny Pinchuk 		kfree(cache);
1515b5c4e40SEvgeny Pinchuk 	}
1525b5c4e40SEvgeny Pinchuk 
1535b5c4e40SEvgeny Pinchuk 	while (dev->io_link_props.next != &dev->io_link_props) {
1545b5c4e40SEvgeny Pinchuk 		iolink = container_of(dev->io_link_props.next,
1555b5c4e40SEvgeny Pinchuk 				struct kfd_iolink_properties, list);
1565b5c4e40SEvgeny Pinchuk 		list_del(&iolink->list);
1575b5c4e40SEvgeny Pinchuk 		kfree(iolink);
1585b5c4e40SEvgeny Pinchuk 	}
1595b5c4e40SEvgeny Pinchuk 
1600f28cca8SRamesh Errabolu 	while (dev->p2p_link_props.next != &dev->p2p_link_props) {
1610f28cca8SRamesh Errabolu 		p2plink = container_of(dev->p2p_link_props.next,
1620f28cca8SRamesh Errabolu 				struct kfd_iolink_properties, list);
1630f28cca8SRamesh Errabolu 		list_del(&p2plink->list);
1640f28cca8SRamesh Errabolu 		kfree(p2plink);
1650f28cca8SRamesh Errabolu 	}
1660f28cca8SRamesh Errabolu 
167f4757347SAmber Lin 	while (dev->perf_props.next != &dev->perf_props) {
168f4757347SAmber Lin 		perf = container_of(dev->perf_props.next,
169f4757347SAmber Lin 				struct kfd_perf_properties, list);
170f4757347SAmber Lin 		list_del(&perf->list);
171f4757347SAmber Lin 		kfree(perf);
172f4757347SAmber Lin 	}
173f4757347SAmber Lin 
1745b5c4e40SEvgeny Pinchuk 	kfree(dev);
1755b5c4e40SEvgeny Pinchuk }
1765b5c4e40SEvgeny Pinchuk 
kfd_release_topology_device_list(struct list_head * device_list)1774f449311SHarish Kasiviswanathan void kfd_release_topology_device_list(struct list_head *device_list)
1785b5c4e40SEvgeny Pinchuk {
1795b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
1805b5c4e40SEvgeny Pinchuk 
1814f449311SHarish Kasiviswanathan 	while (!list_empty(device_list)) {
1824f449311SHarish Kasiviswanathan 		dev = list_first_entry(device_list,
1835b5c4e40SEvgeny Pinchuk 				       struct kfd_topology_device, list);
1845b5c4e40SEvgeny Pinchuk 		kfd_release_topology_device(dev);
1855b5c4e40SEvgeny Pinchuk 	}
1864f449311SHarish Kasiviswanathan }
1875b5c4e40SEvgeny Pinchuk 
kfd_release_live_view(void)1884f449311SHarish Kasiviswanathan static void kfd_release_live_view(void)
1894f449311SHarish Kasiviswanathan {
1904f449311SHarish Kasiviswanathan 	kfd_release_topology_device_list(&topology_device_list);
1915b5c4e40SEvgeny Pinchuk 	memset(&sys_props, 0, sizeof(sys_props));
1925b5c4e40SEvgeny Pinchuk }
1935b5c4e40SEvgeny Pinchuk 
kfd_create_topology_device(struct list_head * device_list)1944f449311SHarish Kasiviswanathan struct kfd_topology_device *kfd_create_topology_device(
1954f449311SHarish Kasiviswanathan 				struct list_head *device_list)
1965b5c4e40SEvgeny Pinchuk {
1975b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
1985b5c4e40SEvgeny Pinchuk 
1995b5c4e40SEvgeny Pinchuk 	dev = kfd_alloc_struct(dev);
2004eacc26bSKent Russell 	if (!dev) {
2015b5c4e40SEvgeny Pinchuk 		pr_err("No memory to allocate a topology device");
20216b9201cSOded Gabbay 		return NULL;
2035b5c4e40SEvgeny Pinchuk 	}
2045b5c4e40SEvgeny Pinchuk 
2055b5c4e40SEvgeny Pinchuk 	INIT_LIST_HEAD(&dev->mem_props);
2065b5c4e40SEvgeny Pinchuk 	INIT_LIST_HEAD(&dev->cache_props);
2075b5c4e40SEvgeny Pinchuk 	INIT_LIST_HEAD(&dev->io_link_props);
2080f28cca8SRamesh Errabolu 	INIT_LIST_HEAD(&dev->p2p_link_props);
209f4757347SAmber Lin 	INIT_LIST_HEAD(&dev->perf_props);
2105b5c4e40SEvgeny Pinchuk 
2114f449311SHarish Kasiviswanathan 	list_add_tail(&dev->list, device_list);
2125b5c4e40SEvgeny Pinchuk 
2135b5c4e40SEvgeny Pinchuk 	return dev;
2145b5c4e40SEvgeny Pinchuk }
2155b5c4e40SEvgeny Pinchuk 
2165b5c4e40SEvgeny Pinchuk 
21783a13ef5SFelix Kuehling #define sysfs_show_gen_prop(buffer, offs, fmt, ...)		\
21883a13ef5SFelix Kuehling 		(offs += snprintf(buffer+offs, PAGE_SIZE-offs,	\
21983a13ef5SFelix Kuehling 				  fmt, __VA_ARGS__))
22083a13ef5SFelix Kuehling #define sysfs_show_32bit_prop(buffer, offs, name, value) \
22183a13ef5SFelix Kuehling 		sysfs_show_gen_prop(buffer, offs, "%s %u\n", name, value)
22283a13ef5SFelix Kuehling #define sysfs_show_64bit_prop(buffer, offs, name, value) \
22383a13ef5SFelix Kuehling 		sysfs_show_gen_prop(buffer, offs, "%s %llu\n", name, value)
22483a13ef5SFelix Kuehling #define sysfs_show_32bit_val(buffer, offs, value) \
22583a13ef5SFelix Kuehling 		sysfs_show_gen_prop(buffer, offs, "%u\n", value)
22683a13ef5SFelix Kuehling #define sysfs_show_str_val(buffer, offs, value) \
22783a13ef5SFelix Kuehling 		sysfs_show_gen_prop(buffer, offs, "%s\n", value)
2285b5c4e40SEvgeny Pinchuk 
sysprops_show(struct kobject * kobj,struct attribute * attr,char * buffer)2295b5c4e40SEvgeny Pinchuk static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
2305b5c4e40SEvgeny Pinchuk 		char *buffer)
2315b5c4e40SEvgeny Pinchuk {
23283a13ef5SFelix Kuehling 	int offs = 0;
2335b5c4e40SEvgeny Pinchuk 
2345b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
2355b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
2365b5c4e40SEvgeny Pinchuk 
2375b5c4e40SEvgeny Pinchuk 	if (attr == &sys_props.attr_genid) {
23883a13ef5SFelix Kuehling 		sysfs_show_32bit_val(buffer, offs,
23983a13ef5SFelix Kuehling 				     sys_props.generation_count);
2405b5c4e40SEvgeny Pinchuk 	} else if (attr == &sys_props.attr_props) {
24183a13ef5SFelix Kuehling 		sysfs_show_64bit_prop(buffer, offs, "platform_oem",
2425b5c4e40SEvgeny Pinchuk 				      sys_props.platform_oem);
24383a13ef5SFelix Kuehling 		sysfs_show_64bit_prop(buffer, offs, "platform_id",
2445b5c4e40SEvgeny Pinchuk 				      sys_props.platform_id);
24583a13ef5SFelix Kuehling 		sysfs_show_64bit_prop(buffer, offs, "platform_rev",
2465b5c4e40SEvgeny Pinchuk 				      sys_props.platform_rev);
2475b5c4e40SEvgeny Pinchuk 	} else {
24883a13ef5SFelix Kuehling 		offs = -EINVAL;
2495b5c4e40SEvgeny Pinchuk 	}
2505b5c4e40SEvgeny Pinchuk 
25183a13ef5SFelix Kuehling 	return offs;
2525b5c4e40SEvgeny Pinchuk }
2535b5c4e40SEvgeny Pinchuk 
kfd_topology_kobj_release(struct kobject * kobj)2545108d768SYong Zhao static void kfd_topology_kobj_release(struct kobject *kobj)
2555108d768SYong Zhao {
2565108d768SYong Zhao 	kfree(kobj);
2575108d768SYong Zhao }
2585108d768SYong Zhao 
2595b5c4e40SEvgeny Pinchuk static const struct sysfs_ops sysprops_ops = {
2605b5c4e40SEvgeny Pinchuk 	.show = sysprops_show,
2615b5c4e40SEvgeny Pinchuk };
2625b5c4e40SEvgeny Pinchuk 
2634fa01c63SThomas Weißschuh static const struct kobj_type sysprops_type = {
2645108d768SYong Zhao 	.release = kfd_topology_kobj_release,
2655b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &sysprops_ops,
2665b5c4e40SEvgeny Pinchuk };
2675b5c4e40SEvgeny Pinchuk 
iolink_show(struct kobject * kobj,struct attribute * attr,char * buffer)2685b5c4e40SEvgeny Pinchuk static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
2695b5c4e40SEvgeny Pinchuk 		char *buffer)
2705b5c4e40SEvgeny Pinchuk {
27183a13ef5SFelix Kuehling 	int offs = 0;
2725b5c4e40SEvgeny Pinchuk 	struct kfd_iolink_properties *iolink;
2735b5c4e40SEvgeny Pinchuk 
2745b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
2755b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
2765b5c4e40SEvgeny Pinchuk 
2775b5c4e40SEvgeny Pinchuk 	iolink = container_of(attr, struct kfd_iolink_properties, attr);
2786b855f7bSHarish Kasiviswanathan 	if (iolink->gpu && kfd_devcgroup_check_permission(iolink->gpu))
2796b855f7bSHarish Kasiviswanathan 		return -EPERM;
28083a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "type", iolink->iolink_type);
28183a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "version_major", iolink->ver_maj);
28283a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "version_minor", iolink->ver_min);
28383a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "node_from", iolink->node_from);
28483a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "node_to", iolink->node_to);
28583a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "weight", iolink->weight);
28683a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "min_latency", iolink->min_latency);
28783a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "max_latency", iolink->max_latency);
28883a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "min_bandwidth",
28983a13ef5SFelix Kuehling 			      iolink->min_bandwidth);
29083a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "max_bandwidth",
29183a13ef5SFelix Kuehling 			      iolink->max_bandwidth);
29283a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "recommended_transfer_size",
2935b5c4e40SEvgeny Pinchuk 			      iolink->rec_transfer_size);
29483a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "flags", iolink->flags);
2955b5c4e40SEvgeny Pinchuk 
29683a13ef5SFelix Kuehling 	return offs;
2975b5c4e40SEvgeny Pinchuk }
2985b5c4e40SEvgeny Pinchuk 
2995b5c4e40SEvgeny Pinchuk static const struct sysfs_ops iolink_ops = {
3005b5c4e40SEvgeny Pinchuk 	.show = iolink_show,
3015b5c4e40SEvgeny Pinchuk };
3025b5c4e40SEvgeny Pinchuk 
3034fa01c63SThomas Weißschuh static const struct kobj_type iolink_type = {
3045108d768SYong Zhao 	.release = kfd_topology_kobj_release,
3055b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &iolink_ops,
3065b5c4e40SEvgeny Pinchuk };
3075b5c4e40SEvgeny Pinchuk 
mem_show(struct kobject * kobj,struct attribute * attr,char * buffer)3085b5c4e40SEvgeny Pinchuk static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
3095b5c4e40SEvgeny Pinchuk 		char *buffer)
3105b5c4e40SEvgeny Pinchuk {
31183a13ef5SFelix Kuehling 	int offs = 0;
3125b5c4e40SEvgeny Pinchuk 	struct kfd_mem_properties *mem;
3135b5c4e40SEvgeny Pinchuk 
3145b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
3155b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
3165b5c4e40SEvgeny Pinchuk 
3175b5c4e40SEvgeny Pinchuk 	mem = container_of(attr, struct kfd_mem_properties, attr);
3186b855f7bSHarish Kasiviswanathan 	if (mem->gpu && kfd_devcgroup_check_permission(mem->gpu))
3196b855f7bSHarish Kasiviswanathan 		return -EPERM;
32083a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "heap_type", mem->heap_type);
32183a13ef5SFelix Kuehling 	sysfs_show_64bit_prop(buffer, offs, "size_in_bytes",
32283a13ef5SFelix Kuehling 			      mem->size_in_bytes);
32383a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "flags", mem->flags);
32483a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "width", mem->width);
32583a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "mem_clk_max",
32683a13ef5SFelix Kuehling 			      mem->mem_clk_max);
3275b5c4e40SEvgeny Pinchuk 
32883a13ef5SFelix Kuehling 	return offs;
3295b5c4e40SEvgeny Pinchuk }
3305b5c4e40SEvgeny Pinchuk 
3315b5c4e40SEvgeny Pinchuk static const struct sysfs_ops mem_ops = {
3325b5c4e40SEvgeny Pinchuk 	.show = mem_show,
3335b5c4e40SEvgeny Pinchuk };
3345b5c4e40SEvgeny Pinchuk 
3354fa01c63SThomas Weißschuh static const struct kobj_type mem_type = {
3365108d768SYong Zhao 	.release = kfd_topology_kobj_release,
3375b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &mem_ops,
3385b5c4e40SEvgeny Pinchuk };
3395b5c4e40SEvgeny Pinchuk 
kfd_cache_show(struct kobject * kobj,struct attribute * attr,char * buffer)3405b5c4e40SEvgeny Pinchuk static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
3415b5c4e40SEvgeny Pinchuk 		char *buffer)
3425b5c4e40SEvgeny Pinchuk {
34383a13ef5SFelix Kuehling 	int offs = 0;
344bc0c75a3SHarish Kasiviswanathan 	uint32_t i, j;
3455b5c4e40SEvgeny Pinchuk 	struct kfd_cache_properties *cache;
3465b5c4e40SEvgeny Pinchuk 
3475b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
3485b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
3495b5c4e40SEvgeny Pinchuk 	cache = container_of(attr, struct kfd_cache_properties, attr);
3506b855f7bSHarish Kasiviswanathan 	if (cache->gpu && kfd_devcgroup_check_permission(cache->gpu))
3516b855f7bSHarish Kasiviswanathan 		return -EPERM;
35283a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "processor_id_low",
3535b5c4e40SEvgeny Pinchuk 			cache->processor_id_low);
35483a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "level", cache->cache_level);
35583a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "size", cache->cache_size);
35683a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "cache_line_size",
35783a13ef5SFelix Kuehling 			      cache->cacheline_size);
35883a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "cache_lines_per_tag",
3595b5c4e40SEvgeny Pinchuk 			      cache->cachelines_per_tag);
36083a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "association", cache->cache_assoc);
36183a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "latency", cache->cache_latency);
36283a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "type", cache->cache_type);
363c0cc999fSMa Jun 
36483a13ef5SFelix Kuehling 	offs += snprintf(buffer+offs, PAGE_SIZE-offs, "sibling_map ");
365c0cc999fSMa Jun 	for (i = 0; i < cache->sibling_map_size; i++)
36683a13ef5SFelix Kuehling 		for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++)
367bc0c75a3SHarish Kasiviswanathan 			/* Check each bit */
36883a13ef5SFelix Kuehling 			offs += snprintf(buffer+offs, PAGE_SIZE-offs, "%d,",
36983a13ef5SFelix Kuehling 						(cache->sibling_map[i] >> j) & 1);
37083a13ef5SFelix Kuehling 
371bc0c75a3SHarish Kasiviswanathan 	/* Replace the last "," with end of line */
37283a13ef5SFelix Kuehling 	buffer[offs-1] = '\n';
37383a13ef5SFelix Kuehling 	return offs;
3745b5c4e40SEvgeny Pinchuk }
3755b5c4e40SEvgeny Pinchuk 
3765b5c4e40SEvgeny Pinchuk static const struct sysfs_ops cache_ops = {
3775b5c4e40SEvgeny Pinchuk 	.show = kfd_cache_show,
3785b5c4e40SEvgeny Pinchuk };
3795b5c4e40SEvgeny Pinchuk 
3804fa01c63SThomas Weißschuh static const struct kobj_type cache_type = {
3815108d768SYong Zhao 	.release = kfd_topology_kobj_release,
3825b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &cache_ops,
3835b5c4e40SEvgeny Pinchuk };
3845b5c4e40SEvgeny Pinchuk 
385f4757347SAmber Lin /****** Sysfs of Performance Counters ******/
386f4757347SAmber Lin 
387f4757347SAmber Lin struct kfd_perf_attr {
388f4757347SAmber Lin 	struct kobj_attribute attr;
389f4757347SAmber Lin 	uint32_t data;
390f4757347SAmber Lin };
391f4757347SAmber Lin 
perf_show(struct kobject * kobj,struct kobj_attribute * attrs,char * buf)392f4757347SAmber Lin static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
393f4757347SAmber Lin 			char *buf)
394f4757347SAmber Lin {
39583a13ef5SFelix Kuehling 	int offs = 0;
396f4757347SAmber Lin 	struct kfd_perf_attr *attr;
397f4757347SAmber Lin 
398f4757347SAmber Lin 	buf[0] = 0;
399f4757347SAmber Lin 	attr = container_of(attrs, struct kfd_perf_attr, attr);
400f4757347SAmber Lin 	if (!attr->data) /* invalid data for PMC */
401f4757347SAmber Lin 		return 0;
402f4757347SAmber Lin 	else
40383a13ef5SFelix Kuehling 		return sysfs_show_32bit_val(buf, offs, attr->data);
404f4757347SAmber Lin }
405f4757347SAmber Lin 
406f4757347SAmber Lin #define KFD_PERF_DESC(_name, _data)			\
407f4757347SAmber Lin {							\
408f4757347SAmber Lin 	.attr  = __ATTR(_name, 0444, perf_show, NULL),	\
409f4757347SAmber Lin 	.data = _data,					\
410f4757347SAmber Lin }
411f4757347SAmber Lin 
412f4757347SAmber Lin static struct kfd_perf_attr perf_attr_iommu[] = {
413f4757347SAmber Lin 	KFD_PERF_DESC(max_concurrent, 0),
414f4757347SAmber Lin 	KFD_PERF_DESC(num_counters, 0),
415f4757347SAmber Lin 	KFD_PERF_DESC(counter_ids, 0),
416f4757347SAmber Lin };
417f4757347SAmber Lin /****************************************/
418f4757347SAmber Lin 
node_show(struct kobject * kobj,struct attribute * attr,char * buffer)4195b5c4e40SEvgeny Pinchuk static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
4205b5c4e40SEvgeny Pinchuk 		char *buffer)
4215b5c4e40SEvgeny Pinchuk {
42283a13ef5SFelix Kuehling 	int offs = 0;
4235b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
424f7c826adSAlexey Skidanov 	uint32_t log_max_watch_addr;
4255b5c4e40SEvgeny Pinchuk 
4265b5c4e40SEvgeny Pinchuk 	/* Making sure that the buffer is an empty string */
4275b5c4e40SEvgeny Pinchuk 	buffer[0] = 0;
4285b5c4e40SEvgeny Pinchuk 
4295b5c4e40SEvgeny Pinchuk 	if (strcmp(attr->name, "gpu_id") == 0) {
4305b5c4e40SEvgeny Pinchuk 		dev = container_of(attr, struct kfd_topology_device,
4315b5c4e40SEvgeny Pinchuk 				attr_gpuid);
4326b855f7bSHarish Kasiviswanathan 		if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
4336b855f7bSHarish Kasiviswanathan 			return -EPERM;
43483a13ef5SFelix Kuehling 		return sysfs_show_32bit_val(buffer, offs, dev->gpu_id);
435f7c826adSAlexey Skidanov 	}
436f7c826adSAlexey Skidanov 
437f7c826adSAlexey Skidanov 	if (strcmp(attr->name, "name") == 0) {
4385b5c4e40SEvgeny Pinchuk 		dev = container_of(attr, struct kfd_topology_device,
4395b5c4e40SEvgeny Pinchuk 				attr_name);
440c181159aSYong Zhao 
4416b855f7bSHarish Kasiviswanathan 		if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
4426b855f7bSHarish Kasiviswanathan 			return -EPERM;
44383a13ef5SFelix Kuehling 		return sysfs_show_str_val(buffer, offs, dev->node_props.name);
444f7c826adSAlexey Skidanov 	}
445f7c826adSAlexey Skidanov 
4465b5c4e40SEvgeny Pinchuk 	dev = container_of(attr, struct kfd_topology_device,
4475b5c4e40SEvgeny Pinchuk 			attr_props);
4486b855f7bSHarish Kasiviswanathan 	if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
4496b855f7bSHarish Kasiviswanathan 		return -EPERM;
45083a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "cpu_cores_count",
4515b5c4e40SEvgeny Pinchuk 			      dev->node_props.cpu_cores_count);
45283a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "simd_count",
45397e3c6a8SMukul Joshi 			      dev->gpu ? dev->node_props.simd_count : 0);
45483a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "mem_banks_count",
4555b5c4e40SEvgeny Pinchuk 			      dev->node_props.mem_banks_count);
45683a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "caches_count",
4575b5c4e40SEvgeny Pinchuk 			      dev->node_props.caches_count);
45883a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "io_links_count",
4595b5c4e40SEvgeny Pinchuk 			      dev->node_props.io_links_count);
4600f28cca8SRamesh Errabolu 	sysfs_show_32bit_prop(buffer, offs, "p2p_links_count",
4610f28cca8SRamesh Errabolu 			      dev->node_props.p2p_links_count);
46283a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "cpu_core_id_base",
4635b5c4e40SEvgeny Pinchuk 			      dev->node_props.cpu_core_id_base);
46483a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "simd_id_base",
4655b5c4e40SEvgeny Pinchuk 			      dev->node_props.simd_id_base);
46683a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "max_waves_per_simd",
4675b5c4e40SEvgeny Pinchuk 			      dev->node_props.max_waves_per_simd);
46883a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "lds_size_in_kb",
4695b5c4e40SEvgeny Pinchuk 			      dev->node_props.lds_size_in_kb);
47083a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "gds_size_in_kb",
4715b5c4e40SEvgeny Pinchuk 			      dev->node_props.gds_size_in_kb);
47283a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "num_gws",
47329e76462SOak Zeng 			      dev->node_props.num_gws);
47483a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "wave_front_size",
4755b5c4e40SEvgeny Pinchuk 			      dev->node_props.wave_front_size);
47683a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "array_count",
477f38f147aSMukul Joshi 			      dev->gpu ? (dev->node_props.array_count *
478c4050ff1SLijo Lazar 					  NUM_XCC(dev->gpu->xcc_mask)) : 0);
47983a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "simd_arrays_per_engine",
4805b5c4e40SEvgeny Pinchuk 			      dev->node_props.simd_arrays_per_engine);
48183a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "cu_per_simd_array",
4825b5c4e40SEvgeny Pinchuk 			      dev->node_props.cu_per_simd_array);
48383a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "simd_per_cu",
4845b5c4e40SEvgeny Pinchuk 			      dev->node_props.simd_per_cu);
48583a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "max_slots_scratch_cu",
4865b5c4e40SEvgeny Pinchuk 			      dev->node_props.max_slots_scratch_cu);
4879d6fa9c7SGraham Sider 	sysfs_show_32bit_prop(buffer, offs, "gfx_target_version",
4889d6fa9c7SGraham Sider 			      dev->node_props.gfx_target_version);
48983a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "vendor_id",
4905b5c4e40SEvgeny Pinchuk 			      dev->node_props.vendor_id);
49183a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "device_id",
4925b5c4e40SEvgeny Pinchuk 			      dev->node_props.device_id);
49383a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "location_id",
4945b5c4e40SEvgeny Pinchuk 			      dev->node_props.location_id);
49583a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "domain",
4963e58e95aSOri Messinger 			      dev->node_props.domain);
49783a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "drm_render_minor",
4987c9b7171SOak Zeng 			      dev->node_props.drm_render_minor);
49983a13ef5SFelix Kuehling 	sysfs_show_64bit_prop(buffer, offs, "hive_id",
5000c1690e3SShaoyun Liu 			      dev->node_props.hive_id);
50183a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "num_sdma_engines",
50214568cf6SOak Zeng 			      dev->node_props.num_sdma_engines);
50383a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "num_sdma_xgmi_engines",
50414568cf6SOak Zeng 			      dev->node_props.num_sdma_xgmi_engines);
50583a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "num_sdma_queues_per_engine",
506bb71c74dSHuang Rui 			      dev->node_props.num_sdma_queues_per_engine);
50783a13ef5SFelix Kuehling 	sysfs_show_32bit_prop(buffer, offs, "num_cp_queues",
508f4feb9faSHuang Rui 			      dev->node_props.num_cp_queues);
5095b5c4e40SEvgeny Pinchuk 
5105b5c4e40SEvgeny Pinchuk 	if (dev->gpu) {
511f7c826adSAlexey Skidanov 		log_max_watch_addr =
5128dc1db31SMukul Joshi 			__ilog2_u32(dev->gpu->kfd->device_info.num_of_watch_points);
513f7c826adSAlexey Skidanov 
514f7c826adSAlexey Skidanov 		if (log_max_watch_addr) {
515f7c826adSAlexey Skidanov 			dev->node_props.capability |=
516f7c826adSAlexey Skidanov 					HSA_CAP_WATCH_POINTS_SUPPORTED;
517f7c826adSAlexey Skidanov 
518f7c826adSAlexey Skidanov 			dev->node_props.capability |=
519f7c826adSAlexey Skidanov 				((log_max_watch_addr <<
520f7c826adSAlexey Skidanov 					HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
521f7c826adSAlexey Skidanov 				HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
522f7c826adSAlexey Skidanov 		}
523f7c826adSAlexey Skidanov 
5247eb0502aSGraham Sider 		if (dev->gpu->adev->asic_type == CHIP_TONGA)
525413e85d5SBen Goz 			dev->node_props.capability |=
526413e85d5SBen Goz 					HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
527413e85d5SBen Goz 
52883a13ef5SFelix Kuehling 		sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_fcompute",
5293a87177eSHarish Kasiviswanathan 			dev->node_props.max_engine_clk_fcompute);
53042e08c78SOded Gabbay 
53183a13ef5SFelix Kuehling 		sysfs_show_64bit_prop(buffer, offs, "local_mem_size", 0ULL);
532f1386fbcSOded Gabbay 
53383a13ef5SFelix Kuehling 		sysfs_show_32bit_prop(buffer, offs, "fw_version",
5348dc1db31SMukul Joshi 				      dev->gpu->kfd->mec_fw_version);
53583a13ef5SFelix Kuehling 		sysfs_show_32bit_prop(buffer, offs, "capability",
536826f5de8SAlexey Skidanov 				      dev->node_props.capability);
537d230f1bfSJonathan Kim 		sysfs_show_64bit_prop(buffer, offs, "debug_prop",
538d230f1bfSJonathan Kim 				      dev->node_props.debug_prop);
53983a13ef5SFelix Kuehling 		sysfs_show_32bit_prop(buffer, offs, "sdma_fw_version",
5408dc1db31SMukul Joshi 				      dev->gpu->kfd->sdma_fw_version);
54111964258SKent Russell 		sysfs_show_64bit_prop(buffer, offs, "unique_id",
54202274fc0SGraham Sider 				      dev->gpu->adev->unique_id);
54374c5b85dSMukul Joshi 		sysfs_show_32bit_prop(buffer, offs, "num_xcc",
544c4050ff1SLijo Lazar 				      NUM_XCC(dev->gpu->xcc_mask));
5455b5c4e40SEvgeny Pinchuk 	}
5465b5c4e40SEvgeny Pinchuk 
54783a13ef5SFelix Kuehling 	return sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_ccompute",
5485b5c4e40SEvgeny Pinchuk 				     cpufreq_quick_get_max(0)/1000);
5495b5c4e40SEvgeny Pinchuk }
5505b5c4e40SEvgeny Pinchuk 
5515b5c4e40SEvgeny Pinchuk static const struct sysfs_ops node_ops = {
5525b5c4e40SEvgeny Pinchuk 	.show = node_show,
5535b5c4e40SEvgeny Pinchuk };
5545b5c4e40SEvgeny Pinchuk 
5554fa01c63SThomas Weißschuh static const struct kobj_type node_type = {
5565108d768SYong Zhao 	.release = kfd_topology_kobj_release,
5575b5c4e40SEvgeny Pinchuk 	.sysfs_ops = &node_ops,
5585b5c4e40SEvgeny Pinchuk };
5595b5c4e40SEvgeny Pinchuk 
kfd_remove_sysfs_file(struct kobject * kobj,struct attribute * attr)5605b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
5615b5c4e40SEvgeny Pinchuk {
5625b5c4e40SEvgeny Pinchuk 	sysfs_remove_file(kobj, attr);
5635b5c4e40SEvgeny Pinchuk 	kobject_del(kobj);
5645b5c4e40SEvgeny Pinchuk 	kobject_put(kobj);
5655b5c4e40SEvgeny Pinchuk }
5665b5c4e40SEvgeny Pinchuk 
kfd_remove_sysfs_node_entry(struct kfd_topology_device * dev)5675b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
5685b5c4e40SEvgeny Pinchuk {
5690f28cca8SRamesh Errabolu 	struct kfd_iolink_properties *p2plink;
5705b5c4e40SEvgeny Pinchuk 	struct kfd_iolink_properties *iolink;
5715b5c4e40SEvgeny Pinchuk 	struct kfd_cache_properties *cache;
5725b5c4e40SEvgeny Pinchuk 	struct kfd_mem_properties *mem;
573f4757347SAmber Lin 	struct kfd_perf_properties *perf;
5745b5c4e40SEvgeny Pinchuk 
5755b5c4e40SEvgeny Pinchuk 	if (dev->kobj_iolink) {
5765b5c4e40SEvgeny Pinchuk 		list_for_each_entry(iolink, &dev->io_link_props, list)
5775b5c4e40SEvgeny Pinchuk 			if (iolink->kobj) {
5785b5c4e40SEvgeny Pinchuk 				kfd_remove_sysfs_file(iolink->kobj,
5795b5c4e40SEvgeny Pinchuk 							&iolink->attr);
58016b9201cSOded Gabbay 				iolink->kobj = NULL;
5815b5c4e40SEvgeny Pinchuk 			}
5825b5c4e40SEvgeny Pinchuk 		kobject_del(dev->kobj_iolink);
5835b5c4e40SEvgeny Pinchuk 		kobject_put(dev->kobj_iolink);
58416b9201cSOded Gabbay 		dev->kobj_iolink = NULL;
5855b5c4e40SEvgeny Pinchuk 	}
5865b5c4e40SEvgeny Pinchuk 
5870f28cca8SRamesh Errabolu 	if (dev->kobj_p2plink) {
5880f28cca8SRamesh Errabolu 		list_for_each_entry(p2plink, &dev->p2p_link_props, list)
5890f28cca8SRamesh Errabolu 			if (p2plink->kobj) {
5900f28cca8SRamesh Errabolu 				kfd_remove_sysfs_file(p2plink->kobj,
5910f28cca8SRamesh Errabolu 							&p2plink->attr);
5920f28cca8SRamesh Errabolu 				p2plink->kobj = NULL;
5930f28cca8SRamesh Errabolu 			}
5940f28cca8SRamesh Errabolu 		kobject_del(dev->kobj_p2plink);
5950f28cca8SRamesh Errabolu 		kobject_put(dev->kobj_p2plink);
5960f28cca8SRamesh Errabolu 		dev->kobj_p2plink = NULL;
5970f28cca8SRamesh Errabolu 	}
5980f28cca8SRamesh Errabolu 
5995b5c4e40SEvgeny Pinchuk 	if (dev->kobj_cache) {
6005b5c4e40SEvgeny Pinchuk 		list_for_each_entry(cache, &dev->cache_props, list)
6015b5c4e40SEvgeny Pinchuk 			if (cache->kobj) {
6025b5c4e40SEvgeny Pinchuk 				kfd_remove_sysfs_file(cache->kobj,
6035b5c4e40SEvgeny Pinchuk 							&cache->attr);
60416b9201cSOded Gabbay 				cache->kobj = NULL;
6055b5c4e40SEvgeny Pinchuk 			}
6065b5c4e40SEvgeny Pinchuk 		kobject_del(dev->kobj_cache);
6075b5c4e40SEvgeny Pinchuk 		kobject_put(dev->kobj_cache);
60816b9201cSOded Gabbay 		dev->kobj_cache = NULL;
6095b5c4e40SEvgeny Pinchuk 	}
6105b5c4e40SEvgeny Pinchuk 
6115b5c4e40SEvgeny Pinchuk 	if (dev->kobj_mem) {
6125b5c4e40SEvgeny Pinchuk 		list_for_each_entry(mem, &dev->mem_props, list)
6135b5c4e40SEvgeny Pinchuk 			if (mem->kobj) {
6145b5c4e40SEvgeny Pinchuk 				kfd_remove_sysfs_file(mem->kobj, &mem->attr);
61516b9201cSOded Gabbay 				mem->kobj = NULL;
6165b5c4e40SEvgeny Pinchuk 			}
6175b5c4e40SEvgeny Pinchuk 		kobject_del(dev->kobj_mem);
6185b5c4e40SEvgeny Pinchuk 		kobject_put(dev->kobj_mem);
61916b9201cSOded Gabbay 		dev->kobj_mem = NULL;
6205b5c4e40SEvgeny Pinchuk 	}
6215b5c4e40SEvgeny Pinchuk 
622f4757347SAmber Lin 	if (dev->kobj_perf) {
623f4757347SAmber Lin 		list_for_each_entry(perf, &dev->perf_props, list) {
624f4757347SAmber Lin 			kfree(perf->attr_group);
625f4757347SAmber Lin 			perf->attr_group = NULL;
626f4757347SAmber Lin 		}
627f4757347SAmber Lin 		kobject_del(dev->kobj_perf);
628f4757347SAmber Lin 		kobject_put(dev->kobj_perf);
629f4757347SAmber Lin 		dev->kobj_perf = NULL;
630f4757347SAmber Lin 	}
631f4757347SAmber Lin 
6325b5c4e40SEvgeny Pinchuk 	if (dev->kobj_node) {
6335b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
6345b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(dev->kobj_node, &dev->attr_name);
6355b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(dev->kobj_node, &dev->attr_props);
6365b5c4e40SEvgeny Pinchuk 		kobject_del(dev->kobj_node);
6375b5c4e40SEvgeny Pinchuk 		kobject_put(dev->kobj_node);
63816b9201cSOded Gabbay 		dev->kobj_node = NULL;
6395b5c4e40SEvgeny Pinchuk 	}
6405b5c4e40SEvgeny Pinchuk }
6415b5c4e40SEvgeny Pinchuk 
kfd_build_sysfs_node_entry(struct kfd_topology_device * dev,uint32_t id)6425b5c4e40SEvgeny Pinchuk static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
6435b5c4e40SEvgeny Pinchuk 		uint32_t id)
6445b5c4e40SEvgeny Pinchuk {
6450f28cca8SRamesh Errabolu 	struct kfd_iolink_properties *p2plink;
6465b5c4e40SEvgeny Pinchuk 	struct kfd_iolink_properties *iolink;
6475b5c4e40SEvgeny Pinchuk 	struct kfd_cache_properties *cache;
6485b5c4e40SEvgeny Pinchuk 	struct kfd_mem_properties *mem;
649f4757347SAmber Lin 	struct kfd_perf_properties *perf;
6505b5c4e40SEvgeny Pinchuk 	int ret;
651f4757347SAmber Lin 	uint32_t i, num_attrs;
652f4757347SAmber Lin 	struct attribute **attrs;
6535b5c4e40SEvgeny Pinchuk 
65432fa8219SFelix Kuehling 	if (WARN_ON(dev->kobj_node))
65532fa8219SFelix Kuehling 		return -EEXIST;
65632fa8219SFelix Kuehling 
6575b5c4e40SEvgeny Pinchuk 	/*
6585b5c4e40SEvgeny Pinchuk 	 * Creating the sysfs folders
6595b5c4e40SEvgeny Pinchuk 	 */
6605b5c4e40SEvgeny Pinchuk 	dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
6615b5c4e40SEvgeny Pinchuk 	if (!dev->kobj_node)
6625b5c4e40SEvgeny Pinchuk 		return -ENOMEM;
6635b5c4e40SEvgeny Pinchuk 
6645b5c4e40SEvgeny Pinchuk 	ret = kobject_init_and_add(dev->kobj_node, &node_type,
6655b5c4e40SEvgeny Pinchuk 			sys_props.kobj_nodes, "%d", id);
66620eca012SQiushi Wu 	if (ret < 0) {
66720eca012SQiushi Wu 		kobject_put(dev->kobj_node);
6685b5c4e40SEvgeny Pinchuk 		return ret;
66920eca012SQiushi Wu 	}
6705b5c4e40SEvgeny Pinchuk 
6715b5c4e40SEvgeny Pinchuk 	dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
6725b5c4e40SEvgeny Pinchuk 	if (!dev->kobj_mem)
6735b5c4e40SEvgeny Pinchuk 		return -ENOMEM;
6745b5c4e40SEvgeny Pinchuk 
6755b5c4e40SEvgeny Pinchuk 	dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
6765b5c4e40SEvgeny Pinchuk 	if (!dev->kobj_cache)
6775b5c4e40SEvgeny Pinchuk 		return -ENOMEM;
6785b5c4e40SEvgeny Pinchuk 
6795b5c4e40SEvgeny Pinchuk 	dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
6805b5c4e40SEvgeny Pinchuk 	if (!dev->kobj_iolink)
6815b5c4e40SEvgeny Pinchuk 		return -ENOMEM;
6825b5c4e40SEvgeny Pinchuk 
6830f28cca8SRamesh Errabolu 	dev->kobj_p2plink = kobject_create_and_add("p2p_links", dev->kobj_node);
6840f28cca8SRamesh Errabolu 	if (!dev->kobj_p2plink)
6850f28cca8SRamesh Errabolu 		return -ENOMEM;
6860f28cca8SRamesh Errabolu 
687f4757347SAmber Lin 	dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
688f4757347SAmber Lin 	if (!dev->kobj_perf)
689f4757347SAmber Lin 		return -ENOMEM;
690f4757347SAmber Lin 
6915b5c4e40SEvgeny Pinchuk 	/*
6925b5c4e40SEvgeny Pinchuk 	 * Creating sysfs files for node properties
6935b5c4e40SEvgeny Pinchuk 	 */
6945b5c4e40SEvgeny Pinchuk 	dev->attr_gpuid.name = "gpu_id";
6955b5c4e40SEvgeny Pinchuk 	dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
6965b5c4e40SEvgeny Pinchuk 	sysfs_attr_init(&dev->attr_gpuid);
6975b5c4e40SEvgeny Pinchuk 	dev->attr_name.name = "name";
6985b5c4e40SEvgeny Pinchuk 	dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
6995b5c4e40SEvgeny Pinchuk 	sysfs_attr_init(&dev->attr_name);
7005b5c4e40SEvgeny Pinchuk 	dev->attr_props.name = "properties";
7015b5c4e40SEvgeny Pinchuk 	dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
7025b5c4e40SEvgeny Pinchuk 	sysfs_attr_init(&dev->attr_props);
7035b5c4e40SEvgeny Pinchuk 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
7045b5c4e40SEvgeny Pinchuk 	if (ret < 0)
7055b5c4e40SEvgeny Pinchuk 		return ret;
7065b5c4e40SEvgeny Pinchuk 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
7075b5c4e40SEvgeny Pinchuk 	if (ret < 0)
7085b5c4e40SEvgeny Pinchuk 		return ret;
7095b5c4e40SEvgeny Pinchuk 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
7105b5c4e40SEvgeny Pinchuk 	if (ret < 0)
7115b5c4e40SEvgeny Pinchuk 		return ret;
7125b5c4e40SEvgeny Pinchuk 
7135b5c4e40SEvgeny Pinchuk 	i = 0;
7145b5c4e40SEvgeny Pinchuk 	list_for_each_entry(mem, &dev->mem_props, list) {
7155b5c4e40SEvgeny Pinchuk 		mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
7165b5c4e40SEvgeny Pinchuk 		if (!mem->kobj)
7175b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
7185b5c4e40SEvgeny Pinchuk 		ret = kobject_init_and_add(mem->kobj, &mem_type,
7195b5c4e40SEvgeny Pinchuk 				dev->kobj_mem, "%d", i);
72020eca012SQiushi Wu 		if (ret < 0) {
72120eca012SQiushi Wu 			kobject_put(mem->kobj);
7225b5c4e40SEvgeny Pinchuk 			return ret;
72320eca012SQiushi Wu 		}
7245b5c4e40SEvgeny Pinchuk 
7255b5c4e40SEvgeny Pinchuk 		mem->attr.name = "properties";
7265b5c4e40SEvgeny Pinchuk 		mem->attr.mode = KFD_SYSFS_FILE_MODE;
7275b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&mem->attr);
7285b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(mem->kobj, &mem->attr);
7295b5c4e40SEvgeny Pinchuk 		if (ret < 0)
7305b5c4e40SEvgeny Pinchuk 			return ret;
7315b5c4e40SEvgeny Pinchuk 		i++;
7325b5c4e40SEvgeny Pinchuk 	}
7335b5c4e40SEvgeny Pinchuk 
7345b5c4e40SEvgeny Pinchuk 	i = 0;
7355b5c4e40SEvgeny Pinchuk 	list_for_each_entry(cache, &dev->cache_props, list) {
7365b5c4e40SEvgeny Pinchuk 		cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
7375b5c4e40SEvgeny Pinchuk 		if (!cache->kobj)
7385b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
7395b5c4e40SEvgeny Pinchuk 		ret = kobject_init_and_add(cache->kobj, &cache_type,
7405b5c4e40SEvgeny Pinchuk 				dev->kobj_cache, "%d", i);
74120eca012SQiushi Wu 		if (ret < 0) {
74220eca012SQiushi Wu 			kobject_put(cache->kobj);
7435b5c4e40SEvgeny Pinchuk 			return ret;
74420eca012SQiushi Wu 		}
7455b5c4e40SEvgeny Pinchuk 
7465b5c4e40SEvgeny Pinchuk 		cache->attr.name = "properties";
7475b5c4e40SEvgeny Pinchuk 		cache->attr.mode = KFD_SYSFS_FILE_MODE;
7485b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&cache->attr);
7495b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(cache->kobj, &cache->attr);
7505b5c4e40SEvgeny Pinchuk 		if (ret < 0)
7515b5c4e40SEvgeny Pinchuk 			return ret;
7525b5c4e40SEvgeny Pinchuk 		i++;
7535b5c4e40SEvgeny Pinchuk 	}
7545b5c4e40SEvgeny Pinchuk 
7555b5c4e40SEvgeny Pinchuk 	i = 0;
7565b5c4e40SEvgeny Pinchuk 	list_for_each_entry(iolink, &dev->io_link_props, list) {
7575b5c4e40SEvgeny Pinchuk 		iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
7585b5c4e40SEvgeny Pinchuk 		if (!iolink->kobj)
7595b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
7605b5c4e40SEvgeny Pinchuk 		ret = kobject_init_and_add(iolink->kobj, &iolink_type,
7615b5c4e40SEvgeny Pinchuk 				dev->kobj_iolink, "%d", i);
76220eca012SQiushi Wu 		if (ret < 0) {
76320eca012SQiushi Wu 			kobject_put(iolink->kobj);
7645b5c4e40SEvgeny Pinchuk 			return ret;
76520eca012SQiushi Wu 		}
7665b5c4e40SEvgeny Pinchuk 
7675b5c4e40SEvgeny Pinchuk 		iolink->attr.name = "properties";
7685b5c4e40SEvgeny Pinchuk 		iolink->attr.mode = KFD_SYSFS_FILE_MODE;
7695b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&iolink->attr);
7705b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(iolink->kobj, &iolink->attr);
7715b5c4e40SEvgeny Pinchuk 		if (ret < 0)
7725b5c4e40SEvgeny Pinchuk 			return ret;
7735b5c4e40SEvgeny Pinchuk 		i++;
7745b5c4e40SEvgeny Pinchuk 	}
7755b5c4e40SEvgeny Pinchuk 
7760f28cca8SRamesh Errabolu 	i = 0;
7770f28cca8SRamesh Errabolu 	list_for_each_entry(p2plink, &dev->p2p_link_props, list) {
7780f28cca8SRamesh Errabolu 		p2plink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
7790f28cca8SRamesh Errabolu 		if (!p2plink->kobj)
7800f28cca8SRamesh Errabolu 			return -ENOMEM;
7810f28cca8SRamesh Errabolu 		ret = kobject_init_and_add(p2plink->kobj, &iolink_type,
7820f28cca8SRamesh Errabolu 				dev->kobj_p2plink, "%d", i);
7830f28cca8SRamesh Errabolu 		if (ret < 0) {
7840f28cca8SRamesh Errabolu 			kobject_put(p2plink->kobj);
7850f28cca8SRamesh Errabolu 			return ret;
7860f28cca8SRamesh Errabolu 		}
7870f28cca8SRamesh Errabolu 
7880f28cca8SRamesh Errabolu 		p2plink->attr.name = "properties";
7890f28cca8SRamesh Errabolu 		p2plink->attr.mode = KFD_SYSFS_FILE_MODE;
7901f9d1ff1SMukul Joshi 		sysfs_attr_init(&p2plink->attr);
7910f28cca8SRamesh Errabolu 		ret = sysfs_create_file(p2plink->kobj, &p2plink->attr);
7920f28cca8SRamesh Errabolu 		if (ret < 0)
7930f28cca8SRamesh Errabolu 			return ret;
7940f28cca8SRamesh Errabolu 		i++;
7950f28cca8SRamesh Errabolu 	}
7960f28cca8SRamesh Errabolu 
797f4757347SAmber Lin 	/* All hardware blocks have the same number of attributes. */
7983f866f5fSGustavo A. R. Silva 	num_attrs = ARRAY_SIZE(perf_attr_iommu);
799f4757347SAmber Lin 	list_for_each_entry(perf, &dev->perf_props, list) {
800f4757347SAmber Lin 		perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
801f4757347SAmber Lin 			* num_attrs + sizeof(struct attribute_group),
802f4757347SAmber Lin 			GFP_KERNEL);
803f4757347SAmber Lin 		if (!perf->attr_group)
804f4757347SAmber Lin 			return -ENOMEM;
805f4757347SAmber Lin 
806f4757347SAmber Lin 		attrs = (struct attribute **)(perf->attr_group + 1);
807f4757347SAmber Lin 		if (!strcmp(perf->block_name, "iommu")) {
808f4757347SAmber Lin 		/* Information of IOMMU's num_counters and counter_ids is shown
809f4757347SAmber Lin 		 * under /sys/bus/event_source/devices/amd_iommu. We don't
810f4757347SAmber Lin 		 * duplicate here.
811f4757347SAmber Lin 		 */
812f4757347SAmber Lin 			perf_attr_iommu[0].data = perf->max_concurrent;
813f4757347SAmber Lin 			for (i = 0; i < num_attrs; i++)
814f4757347SAmber Lin 				attrs[i] = &perf_attr_iommu[i].attr.attr;
815f4757347SAmber Lin 		}
816f4757347SAmber Lin 		perf->attr_group->name = perf->block_name;
817f4757347SAmber Lin 		perf->attr_group->attrs = attrs;
818f4757347SAmber Lin 		ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
819f4757347SAmber Lin 		if (ret < 0)
820f4757347SAmber Lin 			return ret;
821f4757347SAmber Lin 	}
822f4757347SAmber Lin 
8235b5c4e40SEvgeny Pinchuk 	return 0;
8245b5c4e40SEvgeny Pinchuk }
8255b5c4e40SEvgeny Pinchuk 
8263a87177eSHarish Kasiviswanathan /* Called with write topology lock acquired */
kfd_build_sysfs_node_tree(void)8275b5c4e40SEvgeny Pinchuk static int kfd_build_sysfs_node_tree(void)
8285b5c4e40SEvgeny Pinchuk {
8295b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
8305b5c4e40SEvgeny Pinchuk 	int ret;
8315b5c4e40SEvgeny Pinchuk 	uint32_t i = 0;
8325b5c4e40SEvgeny Pinchuk 
8335b5c4e40SEvgeny Pinchuk 	list_for_each_entry(dev, &topology_device_list, list) {
8348dfead6cSBen Goz 		ret = kfd_build_sysfs_node_entry(dev, i);
8355b5c4e40SEvgeny Pinchuk 		if (ret < 0)
8365b5c4e40SEvgeny Pinchuk 			return ret;
8375b5c4e40SEvgeny Pinchuk 		i++;
8385b5c4e40SEvgeny Pinchuk 	}
8395b5c4e40SEvgeny Pinchuk 
8405b5c4e40SEvgeny Pinchuk 	return 0;
8415b5c4e40SEvgeny Pinchuk }
8425b5c4e40SEvgeny Pinchuk 
8433a87177eSHarish Kasiviswanathan /* Called with write topology lock acquired */
kfd_remove_sysfs_node_tree(void)8445b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_node_tree(void)
8455b5c4e40SEvgeny Pinchuk {
8465b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
8475b5c4e40SEvgeny Pinchuk 
8485b5c4e40SEvgeny Pinchuk 	list_for_each_entry(dev, &topology_device_list, list)
8495b5c4e40SEvgeny Pinchuk 		kfd_remove_sysfs_node_entry(dev);
8505b5c4e40SEvgeny Pinchuk }
8515b5c4e40SEvgeny Pinchuk 
kfd_topology_update_sysfs(void)8525b5c4e40SEvgeny Pinchuk static int kfd_topology_update_sysfs(void)
8535b5c4e40SEvgeny Pinchuk {
8545b5c4e40SEvgeny Pinchuk 	int ret;
8555b5c4e40SEvgeny Pinchuk 
8564eacc26bSKent Russell 	if (!sys_props.kobj_topology) {
8575b5c4e40SEvgeny Pinchuk 		sys_props.kobj_topology =
8585b5c4e40SEvgeny Pinchuk 				kfd_alloc_struct(sys_props.kobj_topology);
8595b5c4e40SEvgeny Pinchuk 		if (!sys_props.kobj_topology)
8605b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
8615b5c4e40SEvgeny Pinchuk 
8625b5c4e40SEvgeny Pinchuk 		ret = kobject_init_and_add(sys_props.kobj_topology,
8635b5c4e40SEvgeny Pinchuk 				&sysprops_type,  &kfd_device->kobj,
8645b5c4e40SEvgeny Pinchuk 				"topology");
86520eca012SQiushi Wu 		if (ret < 0) {
86620eca012SQiushi Wu 			kobject_put(sys_props.kobj_topology);
8675b5c4e40SEvgeny Pinchuk 			return ret;
86820eca012SQiushi Wu 		}
8695b5c4e40SEvgeny Pinchuk 
8705b5c4e40SEvgeny Pinchuk 		sys_props.kobj_nodes = kobject_create_and_add("nodes",
8715b5c4e40SEvgeny Pinchuk 				sys_props.kobj_topology);
8725b5c4e40SEvgeny Pinchuk 		if (!sys_props.kobj_nodes)
8735b5c4e40SEvgeny Pinchuk 			return -ENOMEM;
8745b5c4e40SEvgeny Pinchuk 
8755b5c4e40SEvgeny Pinchuk 		sys_props.attr_genid.name = "generation_id";
8765b5c4e40SEvgeny Pinchuk 		sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
8775b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&sys_props.attr_genid);
8785b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(sys_props.kobj_topology,
8795b5c4e40SEvgeny Pinchuk 				&sys_props.attr_genid);
8805b5c4e40SEvgeny Pinchuk 		if (ret < 0)
8815b5c4e40SEvgeny Pinchuk 			return ret;
8825b5c4e40SEvgeny Pinchuk 
8835b5c4e40SEvgeny Pinchuk 		sys_props.attr_props.name = "system_properties";
8845b5c4e40SEvgeny Pinchuk 		sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
8855b5c4e40SEvgeny Pinchuk 		sysfs_attr_init(&sys_props.attr_props);
8865b5c4e40SEvgeny Pinchuk 		ret = sysfs_create_file(sys_props.kobj_topology,
8875b5c4e40SEvgeny Pinchuk 				&sys_props.attr_props);
8885b5c4e40SEvgeny Pinchuk 		if (ret < 0)
8895b5c4e40SEvgeny Pinchuk 			return ret;
8905b5c4e40SEvgeny Pinchuk 	}
8915b5c4e40SEvgeny Pinchuk 
8925b5c4e40SEvgeny Pinchuk 	kfd_remove_sysfs_node_tree();
8935b5c4e40SEvgeny Pinchuk 
8945b5c4e40SEvgeny Pinchuk 	return kfd_build_sysfs_node_tree();
8955b5c4e40SEvgeny Pinchuk }
8965b5c4e40SEvgeny Pinchuk 
kfd_topology_release_sysfs(void)8975b5c4e40SEvgeny Pinchuk static void kfd_topology_release_sysfs(void)
8985b5c4e40SEvgeny Pinchuk {
8995b5c4e40SEvgeny Pinchuk 	kfd_remove_sysfs_node_tree();
9005b5c4e40SEvgeny Pinchuk 	if (sys_props.kobj_topology) {
9015b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(sys_props.kobj_topology,
9025b5c4e40SEvgeny Pinchuk 				&sys_props.attr_genid);
9035b5c4e40SEvgeny Pinchuk 		sysfs_remove_file(sys_props.kobj_topology,
9045b5c4e40SEvgeny Pinchuk 				&sys_props.attr_props);
9055b5c4e40SEvgeny Pinchuk 		if (sys_props.kobj_nodes) {
9065b5c4e40SEvgeny Pinchuk 			kobject_del(sys_props.kobj_nodes);
9075b5c4e40SEvgeny Pinchuk 			kobject_put(sys_props.kobj_nodes);
90816b9201cSOded Gabbay 			sys_props.kobj_nodes = NULL;
9095b5c4e40SEvgeny Pinchuk 		}
9105b5c4e40SEvgeny Pinchuk 		kobject_del(sys_props.kobj_topology);
9115b5c4e40SEvgeny Pinchuk 		kobject_put(sys_props.kobj_topology);
91216b9201cSOded Gabbay 		sys_props.kobj_topology = NULL;
9135b5c4e40SEvgeny Pinchuk 	}
9145b5c4e40SEvgeny Pinchuk }
9155b5c4e40SEvgeny Pinchuk 
9164f449311SHarish Kasiviswanathan /* Called with write topology_lock acquired */
kfd_topology_update_device_list(struct list_head * temp_list,struct list_head * master_list)9174f449311SHarish Kasiviswanathan static void kfd_topology_update_device_list(struct list_head *temp_list,
9184f449311SHarish Kasiviswanathan 					struct list_head *master_list)
9194f449311SHarish Kasiviswanathan {
9204f449311SHarish Kasiviswanathan 	while (!list_empty(temp_list)) {
9214f449311SHarish Kasiviswanathan 		list_move_tail(temp_list->next, master_list);
9224f449311SHarish Kasiviswanathan 		sys_props.num_devices++;
9234f449311SHarish Kasiviswanathan 	}
9244f449311SHarish Kasiviswanathan }
9254f449311SHarish Kasiviswanathan 
kfd_debug_print_topology(void)926520b8fb7SFelix Kuehling static void kfd_debug_print_topology(void)
927520b8fb7SFelix Kuehling {
928520b8fb7SFelix Kuehling 	struct kfd_topology_device *dev;
929520b8fb7SFelix Kuehling 
930520b8fb7SFelix Kuehling 	down_read(&topology_lock);
931520b8fb7SFelix Kuehling 
932520b8fb7SFelix Kuehling 	dev = list_last_entry(&topology_device_list,
933520b8fb7SFelix Kuehling 			struct kfd_topology_device, list);
934520b8fb7SFelix Kuehling 	if (dev) {
935520b8fb7SFelix Kuehling 		if (dev->node_props.cpu_cores_count &&
936520b8fb7SFelix Kuehling 				dev->node_props.simd_count) {
937520b8fb7SFelix Kuehling 			pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
938520b8fb7SFelix Kuehling 				dev->node_props.device_id,
939520b8fb7SFelix Kuehling 				dev->node_props.vendor_id);
940520b8fb7SFelix Kuehling 		} else if (dev->node_props.cpu_cores_count)
941520b8fb7SFelix Kuehling 			pr_info("Topology: Add CPU node\n");
942520b8fb7SFelix Kuehling 		else if (dev->node_props.simd_count)
943520b8fb7SFelix Kuehling 			pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
944520b8fb7SFelix Kuehling 				dev->node_props.device_id,
945520b8fb7SFelix Kuehling 				dev->node_props.vendor_id);
946520b8fb7SFelix Kuehling 	}
947520b8fb7SFelix Kuehling 	up_read(&topology_lock);
948520b8fb7SFelix Kuehling }
949520b8fb7SFelix Kuehling 
950520b8fb7SFelix Kuehling /* Helper function for intializing platform_xx members of
951520b8fb7SFelix Kuehling  * kfd_system_properties. Uses OEM info from the last CPU/APU node.
952520b8fb7SFelix Kuehling  */
kfd_update_system_properties(void)953520b8fb7SFelix Kuehling static void kfd_update_system_properties(void)
954520b8fb7SFelix Kuehling {
955520b8fb7SFelix Kuehling 	struct kfd_topology_device *dev;
956520b8fb7SFelix Kuehling 
957520b8fb7SFelix Kuehling 	down_read(&topology_lock);
958520b8fb7SFelix Kuehling 	dev = list_last_entry(&topology_device_list,
959520b8fb7SFelix Kuehling 			struct kfd_topology_device, list);
960520b8fb7SFelix Kuehling 	if (dev) {
961520b8fb7SFelix Kuehling 		sys_props.platform_id =
962520b8fb7SFelix Kuehling 			(*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
963520b8fb7SFelix Kuehling 		sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
964520b8fb7SFelix Kuehling 		sys_props.platform_rev = dev->oem_revision;
965520b8fb7SFelix Kuehling 	}
966520b8fb7SFelix Kuehling 	up_read(&topology_lock);
967520b8fb7SFelix Kuehling }
968520b8fb7SFelix Kuehling 
find_system_memory(const struct dmi_header * dm,void * private)969520b8fb7SFelix Kuehling static void find_system_memory(const struct dmi_header *dm,
970520b8fb7SFelix Kuehling 	void *private)
971520b8fb7SFelix Kuehling {
972520b8fb7SFelix Kuehling 	struct kfd_mem_properties *mem;
973520b8fb7SFelix Kuehling 	u16 mem_width, mem_clock;
974520b8fb7SFelix Kuehling 	struct kfd_topology_device *kdev =
975520b8fb7SFelix Kuehling 		(struct kfd_topology_device *)private;
976520b8fb7SFelix Kuehling 	const u8 *dmi_data = (const u8 *)(dm + 1);
977520b8fb7SFelix Kuehling 
978520b8fb7SFelix Kuehling 	if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
979520b8fb7SFelix Kuehling 		mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
980520b8fb7SFelix Kuehling 		mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
981520b8fb7SFelix Kuehling 		list_for_each_entry(mem, &kdev->mem_props, list) {
982520b8fb7SFelix Kuehling 			if (mem_width != 0xFFFF && mem_width != 0)
983520b8fb7SFelix Kuehling 				mem->width = mem_width;
984520b8fb7SFelix Kuehling 			if (mem_clock != 0)
985520b8fb7SFelix Kuehling 				mem->mem_clk_max = mem_clock;
986520b8fb7SFelix Kuehling 		}
987520b8fb7SFelix Kuehling 	}
988520b8fb7SFelix Kuehling }
989f4757347SAmber Lin 
990520b8fb7SFelix Kuehling /* kfd_add_non_crat_information - Add information that is not currently
991520b8fb7SFelix Kuehling  *	defined in CRAT but is necessary for KFD topology
992520b8fb7SFelix Kuehling  * @dev - topology device to which addition info is added
993520b8fb7SFelix Kuehling  */
kfd_add_non_crat_information(struct kfd_topology_device * kdev)994520b8fb7SFelix Kuehling static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
995520b8fb7SFelix Kuehling {
996520b8fb7SFelix Kuehling 	/* Check if CPU only node. */
997520b8fb7SFelix Kuehling 	if (!kdev->gpu) {
998520b8fb7SFelix Kuehling 		/* Add system memory information */
999520b8fb7SFelix Kuehling 		dmi_walk(find_system_memory, kdev);
1000520b8fb7SFelix Kuehling 	}
1001520b8fb7SFelix Kuehling 	/* TODO: For GPU node, rearrange code from kfd_topology_add_device */
1002520b8fb7SFelix Kuehling }
1003520b8fb7SFelix Kuehling 
kfd_topology_init(void)10045b5c4e40SEvgeny Pinchuk int kfd_topology_init(void)
10055b5c4e40SEvgeny Pinchuk {
100616b9201cSOded Gabbay 	void *crat_image = NULL;
10075b5c4e40SEvgeny Pinchuk 	size_t image_size = 0;
10085b5c4e40SEvgeny Pinchuk 	int ret;
10094f449311SHarish Kasiviswanathan 	struct list_head temp_topology_device_list;
1010520b8fb7SFelix Kuehling 	int cpu_only_node = 0;
1011520b8fb7SFelix Kuehling 	struct kfd_topology_device *kdev;
1012520b8fb7SFelix Kuehling 	int proximity_domain;
10135b5c4e40SEvgeny Pinchuk 
10144f449311SHarish Kasiviswanathan 	/* topology_device_list - Master list of all topology devices
10154f449311SHarish Kasiviswanathan 	 * temp_topology_device_list - temporary list created while parsing CRAT
10164f449311SHarish Kasiviswanathan 	 * or VCRAT. Once parsing is complete the contents of list is moved to
10174f449311SHarish Kasiviswanathan 	 * topology_device_list
10185b5c4e40SEvgeny Pinchuk 	 */
10194f449311SHarish Kasiviswanathan 
10204f449311SHarish Kasiviswanathan 	/* Initialize the head for the both the lists */
10215b5c4e40SEvgeny Pinchuk 	INIT_LIST_HEAD(&topology_device_list);
10224f449311SHarish Kasiviswanathan 	INIT_LIST_HEAD(&temp_topology_device_list);
10235b5c4e40SEvgeny Pinchuk 	init_rwsem(&topology_lock);
10245b5c4e40SEvgeny Pinchuk 
10255b5c4e40SEvgeny Pinchuk 	memset(&sys_props, 0, sizeof(sys_props));
10265b5c4e40SEvgeny Pinchuk 
1027520b8fb7SFelix Kuehling 	/* Proximity domains in ACPI CRAT tables start counting at
1028520b8fb7SFelix Kuehling 	 * 0. The same should be true for virtual CRAT tables created
1029520b8fb7SFelix Kuehling 	 * at this stage. GPUs added later in kfd_topology_add_device
1030520b8fb7SFelix Kuehling 	 * use a counter.
1031520b8fb7SFelix Kuehling 	 */
1032520b8fb7SFelix Kuehling 	proximity_domain = 0;
1033520b8fb7SFelix Kuehling 
1034520b8fb7SFelix Kuehling 	ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
1035520b8fb7SFelix Kuehling 					    COMPUTE_UNIT_CPU, NULL,
1036520b8fb7SFelix Kuehling 					    proximity_domain);
1037520b8fb7SFelix Kuehling 	cpu_only_node = 1;
1038520b8fb7SFelix Kuehling 	if (ret) {
1039520b8fb7SFelix Kuehling 		pr_err("Error creating VCRAT table for CPU\n");
1040520b8fb7SFelix Kuehling 		return ret;
1041520b8fb7SFelix Kuehling 	}
1042520b8fb7SFelix Kuehling 
1043520b8fb7SFelix Kuehling 	ret = kfd_parse_crat_table(crat_image,
1044520b8fb7SFelix Kuehling 				   &temp_topology_device_list,
1045520b8fb7SFelix Kuehling 				   proximity_domain);
1046520b8fb7SFelix Kuehling 	if (ret) {
1047520b8fb7SFelix Kuehling 		pr_err("Error parsing VCRAT table for CPU\n");
10488e05247dSHarish Kasiviswanathan 		goto err;
1049520b8fb7SFelix Kuehling 	}
10505b5c4e40SEvgeny Pinchuk 
1051f4757347SAmber Lin 	kdev = list_first_entry(&temp_topology_device_list,
1052f4757347SAmber Lin 				struct kfd_topology_device, list);
1053f4757347SAmber Lin 
10545b5c4e40SEvgeny Pinchuk 	down_write(&topology_lock);
10554f449311SHarish Kasiviswanathan 	kfd_topology_update_device_list(&temp_topology_device_list,
10564f449311SHarish Kasiviswanathan 					&topology_device_list);
105746d18d51SMukul Joshi 	topology_crat_proximity_domain = sys_props.num_devices-1;
10585b5c4e40SEvgeny Pinchuk 	ret = kfd_topology_update_sysfs();
10595b5c4e40SEvgeny Pinchuk 	up_write(&topology_lock);
10608e05247dSHarish Kasiviswanathan 
10614f449311SHarish Kasiviswanathan 	if (!ret) {
10624f449311SHarish Kasiviswanathan 		sys_props.generation_count++;
1063520b8fb7SFelix Kuehling 		kfd_update_system_properties();
1064520b8fb7SFelix Kuehling 		kfd_debug_print_topology();
10654f449311SHarish Kasiviswanathan 	} else
10668e05247dSHarish Kasiviswanathan 		pr_err("Failed to update topology in sysfs ret=%d\n", ret);
10675b5c4e40SEvgeny Pinchuk 
1068520b8fb7SFelix Kuehling 	/* For nodes with GPU, this information gets added
1069520b8fb7SFelix Kuehling 	 * when GPU is detected (kfd_topology_add_device).
1070520b8fb7SFelix Kuehling 	 */
1071520b8fb7SFelix Kuehling 	if (cpu_only_node) {
1072520b8fb7SFelix Kuehling 		/* Add additional information to CPU only node created above */
1073520b8fb7SFelix Kuehling 		down_write(&topology_lock);
1074520b8fb7SFelix Kuehling 		kdev = list_first_entry(&topology_device_list,
1075520b8fb7SFelix Kuehling 				struct kfd_topology_device, list);
1076520b8fb7SFelix Kuehling 		up_write(&topology_lock);
1077520b8fb7SFelix Kuehling 		kfd_add_non_crat_information(kdev);
1078520b8fb7SFelix Kuehling 	}
1079520b8fb7SFelix Kuehling 
10805b5c4e40SEvgeny Pinchuk err:
10818e05247dSHarish Kasiviswanathan 	kfd_destroy_crat_image(crat_image);
10825b5c4e40SEvgeny Pinchuk 	return ret;
10835b5c4e40SEvgeny Pinchuk }
10845b5c4e40SEvgeny Pinchuk 
kfd_topology_shutdown(void)10855b5c4e40SEvgeny Pinchuk void kfd_topology_shutdown(void)
10865b5c4e40SEvgeny Pinchuk {
10874f449311SHarish Kasiviswanathan 	down_write(&topology_lock);
10885b5c4e40SEvgeny Pinchuk 	kfd_topology_release_sysfs();
10895b5c4e40SEvgeny Pinchuk 	kfd_release_live_view();
10904f449311SHarish Kasiviswanathan 	up_write(&topology_lock);
10915b5c4e40SEvgeny Pinchuk }
10925b5c4e40SEvgeny Pinchuk 
kfd_generate_gpu_id(struct kfd_node * gpu)10938dc1db31SMukul Joshi static uint32_t kfd_generate_gpu_id(struct kfd_node *gpu)
10945b5c4e40SEvgeny Pinchuk {
10955b5c4e40SEvgeny Pinchuk 	uint32_t hashout;
109674c5b85dSMukul Joshi 	uint32_t buf[8];
1097585f0e6cSEdward O'Callaghan 	uint64_t local_mem_size;
10985b5c4e40SEvgeny Pinchuk 	int i;
10995b5c4e40SEvgeny Pinchuk 
11005b5c4e40SEvgeny Pinchuk 	if (!gpu)
11015b5c4e40SEvgeny Pinchuk 		return 0;
11025b5c4e40SEvgeny Pinchuk 
1103315e29ecSMukul Joshi 	local_mem_size = gpu->local_mem_info.local_mem_size_private +
1104315e29ecSMukul Joshi 			gpu->local_mem_info.local_mem_size_public;
1105d69a3b76SMukul Joshi 	buf[0] = gpu->adev->pdev->devfn;
1106d69a3b76SMukul Joshi 	buf[1] = gpu->adev->pdev->subsystem_vendor |
1107d69a3b76SMukul Joshi 		(gpu->adev->pdev->subsystem_device << 16);
1108d69a3b76SMukul Joshi 	buf[2] = pci_domain_nr(gpu->adev->pdev->bus);
1109d69a3b76SMukul Joshi 	buf[3] = gpu->adev->pdev->device;
1110d69a3b76SMukul Joshi 	buf[4] = gpu->adev->pdev->bus->number;
1111585f0e6cSEdward O'Callaghan 	buf[5] = lower_32_bits(local_mem_size);
1112585f0e6cSEdward O'Callaghan 	buf[6] = upper_32_bits(local_mem_size);
1113c4050ff1SLijo Lazar 	buf[7] = (ffs(gpu->xcc_mask) - 1) | (NUM_XCC(gpu->xcc_mask) << 16);
11145b5c4e40SEvgeny Pinchuk 
111574c5b85dSMukul Joshi 	for (i = 0, hashout = 0; i < 8; i++)
11165b5c4e40SEvgeny Pinchuk 		hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
11175b5c4e40SEvgeny Pinchuk 
11185b5c4e40SEvgeny Pinchuk 	return hashout;
11195b5c4e40SEvgeny Pinchuk }
11203a87177eSHarish Kasiviswanathan /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
11213a87177eSHarish Kasiviswanathan  *		the GPU device is not already present in the topology device
11223a87177eSHarish Kasiviswanathan  *		list then return NULL. This means a new topology device has to
11233a87177eSHarish Kasiviswanathan  *		be created for this GPU.
11243a87177eSHarish Kasiviswanathan  */
kfd_assign_gpu(struct kfd_node * gpu)11258dc1db31SMukul Joshi static struct kfd_topology_device *kfd_assign_gpu(struct kfd_node *gpu)
11265b5c4e40SEvgeny Pinchuk {
11275b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *dev;
112816b9201cSOded Gabbay 	struct kfd_topology_device *out_dev = NULL;
1129171bc67eSHarish Kasiviswanathan 	struct kfd_mem_properties *mem;
1130171bc67eSHarish Kasiviswanathan 	struct kfd_cache_properties *cache;
1131171bc67eSHarish Kasiviswanathan 	struct kfd_iolink_properties *iolink;
11320f28cca8SRamesh Errabolu 	struct kfd_iolink_properties *p2plink;
11335b5c4e40SEvgeny Pinchuk 
1134b8fe0524SFelix Kuehling 	list_for_each_entry(dev, &topology_device_list, list) {
1135b8fe0524SFelix Kuehling 		/* Discrete GPUs need their own topology device list
1136b8fe0524SFelix Kuehling 		 * entries. Don't assign them to CPU/APU nodes.
1137b8fe0524SFelix Kuehling 		 */
1138c99a2e7aSAlex Deucher 		if (dev->node_props.cpu_cores_count)
1139b8fe0524SFelix Kuehling 			continue;
1140b8fe0524SFelix Kuehling 
11414eacc26bSKent Russell 		if (!dev->gpu && (dev->node_props.simd_count > 0)) {
11425b5c4e40SEvgeny Pinchuk 			dev->gpu = gpu;
11435b5c4e40SEvgeny Pinchuk 			out_dev = dev;
1144171bc67eSHarish Kasiviswanathan 
1145171bc67eSHarish Kasiviswanathan 			list_for_each_entry(mem, &dev->mem_props, list)
1146171bc67eSHarish Kasiviswanathan 				mem->gpu = dev->gpu;
1147171bc67eSHarish Kasiviswanathan 			list_for_each_entry(cache, &dev->cache_props, list)
1148171bc67eSHarish Kasiviswanathan 				cache->gpu = dev->gpu;
1149171bc67eSHarish Kasiviswanathan 			list_for_each_entry(iolink, &dev->io_link_props, list)
1150171bc67eSHarish Kasiviswanathan 				iolink->gpu = dev->gpu;
11510f28cca8SRamesh Errabolu 			list_for_each_entry(p2plink, &dev->p2p_link_props, list)
11520f28cca8SRamesh Errabolu 				p2plink->gpu = dev->gpu;
11535b5c4e40SEvgeny Pinchuk 			break;
11545b5c4e40SEvgeny Pinchuk 		}
1155b8fe0524SFelix Kuehling 	}
11565b5c4e40SEvgeny Pinchuk 	return out_dev;
11575b5c4e40SEvgeny Pinchuk }
11585b5c4e40SEvgeny Pinchuk 
kfd_notify_gpu_change(uint32_t gpu_id,int arrival)11595b5c4e40SEvgeny Pinchuk static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
11605b5c4e40SEvgeny Pinchuk {
11615b5c4e40SEvgeny Pinchuk 	/*
11625b5c4e40SEvgeny Pinchuk 	 * TODO: Generate an event for thunk about the arrival/removal
11635b5c4e40SEvgeny Pinchuk 	 * of the GPU
11645b5c4e40SEvgeny Pinchuk 	 */
11655b5c4e40SEvgeny Pinchuk }
11665b5c4e40SEvgeny Pinchuk 
11673a87177eSHarish Kasiviswanathan /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
11683a87177eSHarish Kasiviswanathan  *		patch this after CRAT parsing.
11693a87177eSHarish Kasiviswanathan  */
kfd_fill_mem_clk_max_info(struct kfd_topology_device * dev)11703a87177eSHarish Kasiviswanathan static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
11713a87177eSHarish Kasiviswanathan {
11723a87177eSHarish Kasiviswanathan 	struct kfd_mem_properties *mem;
11733a87177eSHarish Kasiviswanathan 	struct kfd_local_mem_info local_mem_info;
11743a87177eSHarish Kasiviswanathan 
11753a87177eSHarish Kasiviswanathan 	if (!dev)
11763a87177eSHarish Kasiviswanathan 		return;
11773a87177eSHarish Kasiviswanathan 
11783a87177eSHarish Kasiviswanathan 	/* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
11793a87177eSHarish Kasiviswanathan 	 * single bank of VRAM local memory.
11803a87177eSHarish Kasiviswanathan 	 * for dGPUs - VCRAT reports only one bank of Local Memory
11813a87177eSHarish Kasiviswanathan 	 * for APUs - If CRAT from ACPI reports more than one bank, then
11823a87177eSHarish Kasiviswanathan 	 *	all the banks will report the same mem_clk_max information
11833a87177eSHarish Kasiviswanathan 	 */
1184315e29ecSMukul Joshi 	amdgpu_amdkfd_get_local_mem_info(dev->gpu->adev, &local_mem_info,
11859a3ce1a7SHawking Zhang 					 dev->gpu->xcp);
11863a87177eSHarish Kasiviswanathan 
11873a87177eSHarish Kasiviswanathan 	list_for_each_entry(mem, &dev->mem_props, list)
11883a87177eSHarish Kasiviswanathan 		mem->mem_clk_max = local_mem_info.mem_clk_max;
11893a87177eSHarish Kasiviswanathan }
11903a87177eSHarish Kasiviswanathan 
kfd_set_iolink_no_atomics(struct kfd_topology_device * dev,struct kfd_topology_device * target_gpu_dev,struct kfd_iolink_properties * link)1191bdd24657SJonathan Kim static void kfd_set_iolink_no_atomics(struct kfd_topology_device *dev,
1192bdd24657SJonathan Kim 					struct kfd_topology_device *target_gpu_dev,
1193bdd24657SJonathan Kim 					struct kfd_iolink_properties *link)
11943a87177eSHarish Kasiviswanathan {
1195bdd24657SJonathan Kim 	/* xgmi always supports atomics between links. */
1196bdd24657SJonathan Kim 	if (link->iolink_type == CRAT_IOLINK_TYPE_XGMI)
11973a87177eSHarish Kasiviswanathan 		return;
11983a87177eSHarish Kasiviswanathan 
1199bdd24657SJonathan Kim 	/* check pcie support to set cpu(dev) flags for target_gpu_dev link. */
1200bdd24657SJonathan Kim 	if (target_gpu_dev) {
1201bdd24657SJonathan Kim 		uint32_t cap;
1202bdd24657SJonathan Kim 
1203d69a3b76SMukul Joshi 		pcie_capability_read_dword(target_gpu_dev->gpu->adev->pdev,
1204d35f00d8SEric Huang 				PCI_EXP_DEVCAP2, &cap);
1205d35f00d8SEric Huang 
1206d35f00d8SEric Huang 		if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1207d35f00d8SEric Huang 			     PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1208bdd24657SJonathan Kim 			link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
12093a87177eSHarish Kasiviswanathan 				CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1210bdd24657SJonathan Kim 	/* set gpu (dev) flags. */
1211bdd24657SJonathan Kim 	} else {
12128dc1db31SMukul Joshi 		if (!dev->gpu->kfd->pci_atomic_requested ||
12137eb0502aSGraham Sider 				dev->gpu->adev->asic_type == CHIP_HAWAII)
1214bdd24657SJonathan Kim 			link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1215d35f00d8SEric Huang 				CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1216deb68983SJonathan Kim 	}
1217bdd24657SJonathan Kim }
1218bdd24657SJonathan Kim 
kfd_set_iolink_non_coherent(struct kfd_topology_device * to_dev,struct kfd_iolink_properties * outbound_link,struct kfd_iolink_properties * inbound_link)1219c9cfbf7fSEric Huang static void kfd_set_iolink_non_coherent(struct kfd_topology_device *to_dev,
1220c9cfbf7fSEric Huang 		struct kfd_iolink_properties *outbound_link,
1221c9cfbf7fSEric Huang 		struct kfd_iolink_properties *inbound_link)
1222c9cfbf7fSEric Huang {
1223c9cfbf7fSEric Huang 	/* CPU -> GPU with PCIe */
1224c9cfbf7fSEric Huang 	if (!to_dev->gpu &&
1225c9cfbf7fSEric Huang 	    inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
1226c9cfbf7fSEric Huang 		inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1227c9cfbf7fSEric Huang 
1228c9cfbf7fSEric Huang 	if (to_dev->gpu) {
1229c9cfbf7fSEric Huang 		/* GPU <-> GPU with PCIe and
1230c9cfbf7fSEric Huang 		 * Vega20 with XGMI
1231c9cfbf7fSEric Huang 		 */
1232c9cfbf7fSEric Huang 		if (inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS ||
1233c9cfbf7fSEric Huang 		    (inbound_link->iolink_type == CRAT_IOLINK_TYPE_XGMI &&
1234046e674bSGraham Sider 		    KFD_GC_VERSION(to_dev->gpu) == IP_VERSION(9, 4, 0))) {
1235c9cfbf7fSEric Huang 			outbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1236c9cfbf7fSEric Huang 			inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1237c9cfbf7fSEric Huang 		}
1238c9cfbf7fSEric Huang 	}
1239c9cfbf7fSEric Huang }
1240c9cfbf7fSEric Huang 
kfd_fill_iolink_non_crat_info(struct kfd_topology_device * dev)1241bdd24657SJonathan Kim static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
1242bdd24657SJonathan Kim {
1243bdd24657SJonathan Kim 	struct kfd_iolink_properties *link, *inbound_link;
1244bdd24657SJonathan Kim 	struct kfd_topology_device *peer_dev;
1245bdd24657SJonathan Kim 
1246bdd24657SJonathan Kim 	if (!dev || !dev->gpu)
1247bdd24657SJonathan Kim 		return;
1248d35f00d8SEric Huang 
1249d35f00d8SEric Huang 	/* GPU only creates direct links so apply flags setting to all */
1250d35f00d8SEric Huang 	list_for_each_entry(link, &dev->io_link_props, list) {
1251bdd24657SJonathan Kim 		link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1252bdd24657SJonathan Kim 		kfd_set_iolink_no_atomics(dev, NULL, link);
1253bdd24657SJonathan Kim 		peer_dev = kfd_topology_device_by_proximity_domain(
1254d35f00d8SEric Huang 				link->node_to);
1255bdd24657SJonathan Kim 
1256bdd24657SJonathan Kim 		if (!peer_dev)
1257bdd24657SJonathan Kim 			continue;
1258bdd24657SJonathan Kim 
1259a0af5dbdSJonathan Kim 		/* Include the CPU peer in GPU hive if connected over xGMI. */
1260b2ef2fdfSRajneesh Bhardwaj 		if (!peer_dev->gpu &&
12611698e200SJonathan Kim 		    link->iolink_type == CRAT_IOLINK_TYPE_XGMI) {
12621698e200SJonathan Kim 			/*
12631698e200SJonathan Kim 			 * If the GPU is not part of a GPU hive, use its pci
12641698e200SJonathan Kim 			 * device location as the hive ID to bind with the CPU.
12651698e200SJonathan Kim 			 */
12661698e200SJonathan Kim 			if (!dev->node_props.hive_id)
12671698e200SJonathan Kim 				dev->node_props.hive_id = pci_dev_id(dev->gpu->adev->pdev);
1268a0af5dbdSJonathan Kim 			peer_dev->node_props.hive_id = dev->node_props.hive_id;
12691698e200SJonathan Kim 		}
1270a0af5dbdSJonathan Kim 
1271bdd24657SJonathan Kim 		list_for_each_entry(inbound_link, &peer_dev->io_link_props,
1272bdd24657SJonathan Kim 									list) {
1273bdd24657SJonathan Kim 			if (inbound_link->node_to != link->node_from)
1274bdd24657SJonathan Kim 				continue;
1275bdd24657SJonathan Kim 
1276bdd24657SJonathan Kim 			inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1277bdd24657SJonathan Kim 			kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
1278c9cfbf7fSEric Huang 			kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
1279d35f00d8SEric Huang 		}
1280d35f00d8SEric Huang 	}
12810f28cca8SRamesh Errabolu 
12820f28cca8SRamesh Errabolu 	/* Create indirect links so apply flags setting to all */
12830f28cca8SRamesh Errabolu 	list_for_each_entry(link, &dev->p2p_link_props, list) {
12840f28cca8SRamesh Errabolu 		link->flags = CRAT_IOLINK_FLAGS_ENABLED;
12850f28cca8SRamesh Errabolu 		kfd_set_iolink_no_atomics(dev, NULL, link);
12860f28cca8SRamesh Errabolu 		peer_dev = kfd_topology_device_by_proximity_domain(
12870f28cca8SRamesh Errabolu 				link->node_to);
12880f28cca8SRamesh Errabolu 
12890f28cca8SRamesh Errabolu 		if (!peer_dev)
12900f28cca8SRamesh Errabolu 			continue;
12910f28cca8SRamesh Errabolu 
12920f28cca8SRamesh Errabolu 		list_for_each_entry(inbound_link, &peer_dev->p2p_link_props,
12930f28cca8SRamesh Errabolu 									list) {
12940f28cca8SRamesh Errabolu 			if (inbound_link->node_to != link->node_from)
12950f28cca8SRamesh Errabolu 				continue;
12960f28cca8SRamesh Errabolu 
12970f28cca8SRamesh Errabolu 			inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
12980f28cca8SRamesh Errabolu 			kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
12990f28cca8SRamesh Errabolu 			kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
13000f28cca8SRamesh Errabolu 		}
13010f28cca8SRamesh Errabolu 	}
13020f28cca8SRamesh Errabolu }
13030f28cca8SRamesh Errabolu 
kfd_build_p2p_node_entry(struct kfd_topology_device * dev,struct kfd_iolink_properties * p2plink)13040f28cca8SRamesh Errabolu static int kfd_build_p2p_node_entry(struct kfd_topology_device *dev,
13050f28cca8SRamesh Errabolu 				struct kfd_iolink_properties *p2plink)
13060f28cca8SRamesh Errabolu {
13070f28cca8SRamesh Errabolu 	int ret;
13080f28cca8SRamesh Errabolu 
13090f28cca8SRamesh Errabolu 	p2plink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
13100f28cca8SRamesh Errabolu 	if (!p2plink->kobj)
13110f28cca8SRamesh Errabolu 		return -ENOMEM;
13120f28cca8SRamesh Errabolu 
13130f28cca8SRamesh Errabolu 	ret = kobject_init_and_add(p2plink->kobj, &iolink_type,
13140f28cca8SRamesh Errabolu 			dev->kobj_p2plink, "%d", dev->node_props.p2p_links_count - 1);
13150f28cca8SRamesh Errabolu 	if (ret < 0) {
13160f28cca8SRamesh Errabolu 		kobject_put(p2plink->kobj);
13170f28cca8SRamesh Errabolu 		return ret;
13180f28cca8SRamesh Errabolu 	}
13190f28cca8SRamesh Errabolu 
13200f28cca8SRamesh Errabolu 	p2plink->attr.name = "properties";
13210f28cca8SRamesh Errabolu 	p2plink->attr.mode = KFD_SYSFS_FILE_MODE;
13220f28cca8SRamesh Errabolu 	sysfs_attr_init(&p2plink->attr);
13230f28cca8SRamesh Errabolu 	ret = sysfs_create_file(p2plink->kobj, &p2plink->attr);
13240f28cca8SRamesh Errabolu 	if (ret < 0)
13250f28cca8SRamesh Errabolu 		return ret;
13260f28cca8SRamesh Errabolu 
13270f28cca8SRamesh Errabolu 	return 0;
13280f28cca8SRamesh Errabolu }
13290f28cca8SRamesh Errabolu 
kfd_create_indirect_link_prop(struct kfd_topology_device * kdev,int gpu_node)13300f28cca8SRamesh Errabolu static int kfd_create_indirect_link_prop(struct kfd_topology_device *kdev, int gpu_node)
13310f28cca8SRamesh Errabolu {
13327d50b92dSDan Carpenter 	struct kfd_iolink_properties *gpu_link, *tmp_link, *cpu_link;
13330f28cca8SRamesh Errabolu 	struct kfd_iolink_properties *props = NULL, *props2 = NULL;
13340f28cca8SRamesh Errabolu 	struct kfd_topology_device *cpu_dev;
13350f28cca8SRamesh Errabolu 	int ret = 0;
13360f28cca8SRamesh Errabolu 	int i, num_cpu;
13370f28cca8SRamesh Errabolu 
13380f28cca8SRamesh Errabolu 	num_cpu = 0;
13390f28cca8SRamesh Errabolu 	list_for_each_entry(cpu_dev, &topology_device_list, list) {
13400f28cca8SRamesh Errabolu 		if (cpu_dev->gpu)
13410f28cca8SRamesh Errabolu 			break;
13420f28cca8SRamesh Errabolu 		num_cpu++;
13430f28cca8SRamesh Errabolu 	}
13440f28cca8SRamesh Errabolu 
13455024cce8SSrinivasan Shanmugam 	if (list_empty(&kdev->io_link_props))
13465024cce8SSrinivasan Shanmugam 		return -ENODATA;
13475024cce8SSrinivasan Shanmugam 
13480f28cca8SRamesh Errabolu 	gpu_link = list_first_entry(&kdev->io_link_props,
13490f28cca8SRamesh Errabolu 				    struct kfd_iolink_properties, list);
13500f28cca8SRamesh Errabolu 
13510f28cca8SRamesh Errabolu 	for (i = 0; i < num_cpu; i++) {
13520f28cca8SRamesh Errabolu 		/* CPU <--> GPU */
13530f28cca8SRamesh Errabolu 		if (gpu_link->node_to == i)
13540f28cca8SRamesh Errabolu 			continue;
13550f28cca8SRamesh Errabolu 
13560f28cca8SRamesh Errabolu 		/* find CPU <-->  CPU links */
13577d50b92dSDan Carpenter 		cpu_link = NULL;
13580f28cca8SRamesh Errabolu 		cpu_dev = kfd_topology_device_by_proximity_domain(i);
13590f28cca8SRamesh Errabolu 		if (cpu_dev) {
13607d50b92dSDan Carpenter 			list_for_each_entry(tmp_link,
13610f28cca8SRamesh Errabolu 					&cpu_dev->io_link_props, list) {
13627d50b92dSDan Carpenter 				if (tmp_link->node_to == gpu_link->node_to) {
13637d50b92dSDan Carpenter 					cpu_link = tmp_link;
13640f28cca8SRamesh Errabolu 					break;
13650f28cca8SRamesh Errabolu 				}
13660f28cca8SRamesh Errabolu 			}
13677d50b92dSDan Carpenter 		}
13680f28cca8SRamesh Errabolu 
13697d50b92dSDan Carpenter 		if (!cpu_link)
13700f28cca8SRamesh Errabolu 			return -ENOMEM;
13710f28cca8SRamesh Errabolu 
13720f28cca8SRamesh Errabolu 		/* CPU <--> CPU <--> GPU, GPU node*/
13730f28cca8SRamesh Errabolu 		props = kfd_alloc_struct(props);
13740f28cca8SRamesh Errabolu 		if (!props)
13750f28cca8SRamesh Errabolu 			return -ENOMEM;
13760f28cca8SRamesh Errabolu 
13770f28cca8SRamesh Errabolu 		memcpy(props, gpu_link, sizeof(struct kfd_iolink_properties));
13780f28cca8SRamesh Errabolu 		props->weight = gpu_link->weight + cpu_link->weight;
13790f28cca8SRamesh Errabolu 		props->min_latency = gpu_link->min_latency + cpu_link->min_latency;
13800f28cca8SRamesh Errabolu 		props->max_latency = gpu_link->max_latency + cpu_link->max_latency;
13810f28cca8SRamesh Errabolu 		props->min_bandwidth = min(gpu_link->min_bandwidth, cpu_link->min_bandwidth);
13820f28cca8SRamesh Errabolu 		props->max_bandwidth = min(gpu_link->max_bandwidth, cpu_link->max_bandwidth);
13830f28cca8SRamesh Errabolu 
13840f28cca8SRamesh Errabolu 		props->node_from = gpu_node;
13850f28cca8SRamesh Errabolu 		props->node_to = i;
13860f28cca8SRamesh Errabolu 		kdev->node_props.p2p_links_count++;
13870f28cca8SRamesh Errabolu 		list_add_tail(&props->list, &kdev->p2p_link_props);
13880f28cca8SRamesh Errabolu 		ret = kfd_build_p2p_node_entry(kdev, props);
13890f28cca8SRamesh Errabolu 		if (ret < 0)
13900f28cca8SRamesh Errabolu 			return ret;
13910f28cca8SRamesh Errabolu 
13920f28cca8SRamesh Errabolu 		/* for small Bar, no CPU --> GPU in-direct links */
13930f28cca8SRamesh Errabolu 		if (kfd_dev_is_large_bar(kdev->gpu)) {
13940f28cca8SRamesh Errabolu 			/* CPU <--> CPU <--> GPU, CPU node*/
13950f28cca8SRamesh Errabolu 			props2 = kfd_alloc_struct(props2);
13960f28cca8SRamesh Errabolu 			if (!props2)
13970f28cca8SRamesh Errabolu 				return -ENOMEM;
13980f28cca8SRamesh Errabolu 
13990f28cca8SRamesh Errabolu 			memcpy(props2, props, sizeof(struct kfd_iolink_properties));
14000f28cca8SRamesh Errabolu 			props2->node_from = i;
14010f28cca8SRamesh Errabolu 			props2->node_to = gpu_node;
14020f28cca8SRamesh Errabolu 			props2->kobj = NULL;
14030f28cca8SRamesh Errabolu 			cpu_dev->node_props.p2p_links_count++;
14040f28cca8SRamesh Errabolu 			list_add_tail(&props2->list, &cpu_dev->p2p_link_props);
14050f28cca8SRamesh Errabolu 			ret = kfd_build_p2p_node_entry(cpu_dev, props2);
14060f28cca8SRamesh Errabolu 			if (ret < 0)
14070f28cca8SRamesh Errabolu 				return ret;
14080f28cca8SRamesh Errabolu 		}
14090f28cca8SRamesh Errabolu 	}
14100f28cca8SRamesh Errabolu 	return ret;
14110f28cca8SRamesh Errabolu }
14120f28cca8SRamesh Errabolu 
14130f28cca8SRamesh Errabolu #if defined(CONFIG_HSA_AMD_P2P)
kfd_add_peer_prop(struct kfd_topology_device * kdev,struct kfd_topology_device * peer,int from,int to)14140f28cca8SRamesh Errabolu static int kfd_add_peer_prop(struct kfd_topology_device *kdev,
14150f28cca8SRamesh Errabolu 		struct kfd_topology_device *peer, int from, int to)
14160f28cca8SRamesh Errabolu {
14170f28cca8SRamesh Errabolu 	struct kfd_iolink_properties *props = NULL;
14180f28cca8SRamesh Errabolu 	struct kfd_iolink_properties *iolink1, *iolink2, *iolink3;
14190f28cca8SRamesh Errabolu 	struct kfd_topology_device *cpu_dev;
14200f28cca8SRamesh Errabolu 	int ret = 0;
14210f28cca8SRamesh Errabolu 
14220f28cca8SRamesh Errabolu 	if (!amdgpu_device_is_peer_accessible(
14230f28cca8SRamesh Errabolu 				kdev->gpu->adev,
14240f28cca8SRamesh Errabolu 				peer->gpu->adev))
14250f28cca8SRamesh Errabolu 		return ret;
14260f28cca8SRamesh Errabolu 
14275024cce8SSrinivasan Shanmugam 	if (list_empty(&kdev->io_link_props))
14285024cce8SSrinivasan Shanmugam 		return -ENODATA;
14295024cce8SSrinivasan Shanmugam 
14300f28cca8SRamesh Errabolu 	iolink1 = list_first_entry(&kdev->io_link_props,
14310f28cca8SRamesh Errabolu 				   struct kfd_iolink_properties, list);
14325024cce8SSrinivasan Shanmugam 
14335024cce8SSrinivasan Shanmugam 	if (list_empty(&peer->io_link_props))
14345024cce8SSrinivasan Shanmugam 		return -ENODATA;
14350f28cca8SRamesh Errabolu 
14360f28cca8SRamesh Errabolu 	iolink2 = list_first_entry(&peer->io_link_props,
14370f28cca8SRamesh Errabolu 				   struct kfd_iolink_properties, list);
14380f28cca8SRamesh Errabolu 
14390f28cca8SRamesh Errabolu 	props = kfd_alloc_struct(props);
14400f28cca8SRamesh Errabolu 	if (!props)
14410f28cca8SRamesh Errabolu 		return -ENOMEM;
14420f28cca8SRamesh Errabolu 
14430f28cca8SRamesh Errabolu 	memcpy(props, iolink1, sizeof(struct kfd_iolink_properties));
14440f28cca8SRamesh Errabolu 
14450f28cca8SRamesh Errabolu 	props->weight = iolink1->weight + iolink2->weight;
14460f28cca8SRamesh Errabolu 	props->min_latency = iolink1->min_latency + iolink2->min_latency;
14470f28cca8SRamesh Errabolu 	props->max_latency = iolink1->max_latency + iolink2->max_latency;
14480f28cca8SRamesh Errabolu 	props->min_bandwidth = min(iolink1->min_bandwidth, iolink2->min_bandwidth);
14490f28cca8SRamesh Errabolu 	props->max_bandwidth = min(iolink2->max_bandwidth, iolink2->max_bandwidth);
14500f28cca8SRamesh Errabolu 
14510f28cca8SRamesh Errabolu 	if (iolink1->node_to != iolink2->node_to) {
14520f28cca8SRamesh Errabolu 		/* CPU->CPU  link*/
14530f28cca8SRamesh Errabolu 		cpu_dev = kfd_topology_device_by_proximity_domain(iolink1->node_to);
14540f28cca8SRamesh Errabolu 		if (cpu_dev) {
1455*583e0a33SSrinivasan Shanmugam 			list_for_each_entry(iolink3, &cpu_dev->io_link_props, list) {
1456*583e0a33SSrinivasan Shanmugam 				if (iolink3->node_to != iolink2->node_to)
1457*583e0a33SSrinivasan Shanmugam 					continue;
14580f28cca8SRamesh Errabolu 
14590f28cca8SRamesh Errabolu 				props->weight += iolink3->weight;
14600f28cca8SRamesh Errabolu 				props->min_latency += iolink3->min_latency;
14610f28cca8SRamesh Errabolu 				props->max_latency += iolink3->max_latency;
14620f28cca8SRamesh Errabolu 				props->min_bandwidth = min(props->min_bandwidth,
14630f28cca8SRamesh Errabolu 							   iolink3->min_bandwidth);
14640f28cca8SRamesh Errabolu 				props->max_bandwidth = min(props->max_bandwidth,
14650f28cca8SRamesh Errabolu 							   iolink3->max_bandwidth);
1466*583e0a33SSrinivasan Shanmugam 				break;
1467*583e0a33SSrinivasan Shanmugam 			}
14680f28cca8SRamesh Errabolu 		} else {
14690f28cca8SRamesh Errabolu 			WARN(1, "CPU node not found");
14700f28cca8SRamesh Errabolu 		}
14710f28cca8SRamesh Errabolu 	}
14720f28cca8SRamesh Errabolu 
14730f28cca8SRamesh Errabolu 	props->node_from = from;
14740f28cca8SRamesh Errabolu 	props->node_to = to;
14750f28cca8SRamesh Errabolu 	peer->node_props.p2p_links_count++;
14760f28cca8SRamesh Errabolu 	list_add_tail(&props->list, &peer->p2p_link_props);
14770f28cca8SRamesh Errabolu 	ret = kfd_build_p2p_node_entry(peer, props);
14780f28cca8SRamesh Errabolu 
14790f28cca8SRamesh Errabolu 	return ret;
14800f28cca8SRamesh Errabolu }
14810f28cca8SRamesh Errabolu #endif
14820f28cca8SRamesh Errabolu 
kfd_dev_create_p2p_links(void)14830f28cca8SRamesh Errabolu static int kfd_dev_create_p2p_links(void)
14840f28cca8SRamesh Errabolu {
14850f28cca8SRamesh Errabolu 	struct kfd_topology_device *dev;
14860f28cca8SRamesh Errabolu 	struct kfd_topology_device *new_dev;
1487914da384SAlex Deucher #if defined(CONFIG_HSA_AMD_P2P)
1488914da384SAlex Deucher 	uint32_t i;
1489914da384SAlex Deucher #endif
1490914da384SAlex Deucher 	uint32_t k;
14910f28cca8SRamesh Errabolu 	int ret = 0;
14920f28cca8SRamesh Errabolu 
14930f28cca8SRamesh Errabolu 	k = 0;
14940f28cca8SRamesh Errabolu 	list_for_each_entry(dev, &topology_device_list, list)
14950f28cca8SRamesh Errabolu 		k++;
14960f28cca8SRamesh Errabolu 	if (k < 2)
14970f28cca8SRamesh Errabolu 		return 0;
14980f28cca8SRamesh Errabolu 
14990f28cca8SRamesh Errabolu 	new_dev = list_last_entry(&topology_device_list, struct kfd_topology_device, list);
15000f28cca8SRamesh Errabolu 	if (WARN_ON(!new_dev->gpu))
15010f28cca8SRamesh Errabolu 		return 0;
15020f28cca8SRamesh Errabolu 
15030f28cca8SRamesh Errabolu 	k--;
15040f28cca8SRamesh Errabolu 
15050f28cca8SRamesh Errabolu 	/* create in-direct links */
15060f28cca8SRamesh Errabolu 	ret = kfd_create_indirect_link_prop(new_dev, k);
15070f28cca8SRamesh Errabolu 	if (ret < 0)
15080f28cca8SRamesh Errabolu 		goto out;
15090f28cca8SRamesh Errabolu 
15100f28cca8SRamesh Errabolu 	/* create p2p links */
15110f28cca8SRamesh Errabolu #if defined(CONFIG_HSA_AMD_P2P)
1512914da384SAlex Deucher 	i = 0;
15130f28cca8SRamesh Errabolu 	list_for_each_entry(dev, &topology_device_list, list) {
15140f28cca8SRamesh Errabolu 		if (dev == new_dev)
15150f28cca8SRamesh Errabolu 			break;
15160f28cca8SRamesh Errabolu 		if (!dev->gpu || !dev->gpu->adev ||
15178dc1db31SMukul Joshi 		    (dev->gpu->kfd->hive_id &&
15188dc1db31SMukul Joshi 		     dev->gpu->kfd->hive_id == new_dev->gpu->kfd->hive_id))
15190f28cca8SRamesh Errabolu 			goto next;
15200f28cca8SRamesh Errabolu 
15210f28cca8SRamesh Errabolu 		/* check if node(s) is/are peer accessible in one direction or bi-direction */
15220f28cca8SRamesh Errabolu 		ret = kfd_add_peer_prop(new_dev, dev, i, k);
15230f28cca8SRamesh Errabolu 		if (ret < 0)
15240f28cca8SRamesh Errabolu 			goto out;
15250f28cca8SRamesh Errabolu 
15260f28cca8SRamesh Errabolu 		ret = kfd_add_peer_prop(dev, new_dev, k, i);
15270f28cca8SRamesh Errabolu 		if (ret < 0)
15280f28cca8SRamesh Errabolu 			goto out;
15290f28cca8SRamesh Errabolu next:
15300f28cca8SRamesh Errabolu 		i++;
15310f28cca8SRamesh Errabolu 	}
15320f28cca8SRamesh Errabolu #endif
15330f28cca8SRamesh Errabolu 
15340f28cca8SRamesh Errabolu out:
15350f28cca8SRamesh Errabolu 	return ret;
15363a87177eSHarish Kasiviswanathan }
15373a87177eSHarish Kasiviswanathan 
1538c0cc999fSMa Jun /* Helper function. See kfd_fill_gpu_cache_info for parameter description */
fill_in_l1_pcache(struct kfd_cache_properties ** props_ext,struct kfd_gpu_cache_info * pcache_info,struct kfd_cu_info * cu_info,int cu_bitmask,int cache_type,unsigned int cu_processor_id,int cu_block)1539c0cc999fSMa Jun static int fill_in_l1_pcache(struct kfd_cache_properties **props_ext,
1540c0cc999fSMa Jun 				struct kfd_gpu_cache_info *pcache_info,
1541c0cc999fSMa Jun 				struct kfd_cu_info *cu_info,
1542c0cc999fSMa Jun 				int cu_bitmask,
1543c0cc999fSMa Jun 				int cache_type, unsigned int cu_processor_id,
1544c0cc999fSMa Jun 				int cu_block)
1545c0cc999fSMa Jun {
1546c0cc999fSMa Jun 	unsigned int cu_sibling_map_mask;
1547c0cc999fSMa Jun 	int first_active_cu;
1548c0cc999fSMa Jun 	struct kfd_cache_properties *pcache = NULL;
1549c0cc999fSMa Jun 
1550c0cc999fSMa Jun 	cu_sibling_map_mask = cu_bitmask;
1551c0cc999fSMa Jun 	cu_sibling_map_mask >>= cu_block;
1552c0cc999fSMa Jun 	cu_sibling_map_mask &= ((1 << pcache_info[cache_type].num_cu_shared) - 1);
1553c0cc999fSMa Jun 	first_active_cu = ffs(cu_sibling_map_mask);
1554c0cc999fSMa Jun 
1555c0cc999fSMa Jun 	/* CU could be inactive. In case of shared cache find the first active
1556c0cc999fSMa Jun 	 * CU. and incase of non-shared cache check if the CU is inactive. If
1557c0cc999fSMa Jun 	 * inactive active skip it
1558c0cc999fSMa Jun 	 */
1559c0cc999fSMa Jun 	if (first_active_cu) {
1560c0cc999fSMa Jun 		pcache = kfd_alloc_struct(pcache);
1561c0cc999fSMa Jun 		if (!pcache)
1562c0cc999fSMa Jun 			return -ENOMEM;
1563c0cc999fSMa Jun 
1564c0cc999fSMa Jun 		memset(pcache, 0, sizeof(struct kfd_cache_properties));
1565c0cc999fSMa Jun 		pcache->processor_id_low = cu_processor_id + (first_active_cu - 1);
1566c0cc999fSMa Jun 		pcache->cache_level = pcache_info[cache_type].cache_level;
1567c0cc999fSMa Jun 		pcache->cache_size = pcache_info[cache_type].cache_size;
1568c0cc999fSMa Jun 
1569c0cc999fSMa Jun 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_DATA_CACHE)
1570c0cc999fSMa Jun 			pcache->cache_type |= HSA_CACHE_TYPE_DATA;
1571c0cc999fSMa Jun 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_INST_CACHE)
1572c0cc999fSMa Jun 			pcache->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
1573c0cc999fSMa Jun 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_CPU_CACHE)
1574c0cc999fSMa Jun 			pcache->cache_type |= HSA_CACHE_TYPE_CPU;
1575c0cc999fSMa Jun 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
1576c0cc999fSMa Jun 			pcache->cache_type |= HSA_CACHE_TYPE_HSACU;
1577c0cc999fSMa Jun 
1578c0cc999fSMa Jun 		/* Sibling map is w.r.t processor_id_low, so shift out
1579c0cc999fSMa Jun 		 * inactive CU
1580c0cc999fSMa Jun 		 */
1581c0cc999fSMa Jun 		cu_sibling_map_mask =
1582c0cc999fSMa Jun 			cu_sibling_map_mask >> (first_active_cu - 1);
1583c0cc999fSMa Jun 
1584c0cc999fSMa Jun 		pcache->sibling_map[0] = (uint8_t)(cu_sibling_map_mask & 0xFF);
1585c0cc999fSMa Jun 		pcache->sibling_map[1] =
1586c0cc999fSMa Jun 				(uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
1587c0cc999fSMa Jun 		pcache->sibling_map[2] =
1588c0cc999fSMa Jun 				(uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
1589c0cc999fSMa Jun 		pcache->sibling_map[3] =
1590c0cc999fSMa Jun 				(uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
1591c0cc999fSMa Jun 
1592c0cc999fSMa Jun 		pcache->sibling_map_size = 4;
1593c0cc999fSMa Jun 		*props_ext = pcache;
1594c0cc999fSMa Jun 
1595c0cc999fSMa Jun 		return 0;
1596c0cc999fSMa Jun 	}
1597c0cc999fSMa Jun 	return 1;
1598c0cc999fSMa Jun }
1599c0cc999fSMa Jun 
1600c0cc999fSMa Jun /* Helper function. See kfd_fill_gpu_cache_info for parameter description */
fill_in_l2_l3_pcache(struct kfd_cache_properties ** props_ext,struct kfd_gpu_cache_info * pcache_info,struct kfd_cu_info * cu_info,int cache_type,unsigned int cu_processor_id,struct kfd_node * knode)1601c0cc999fSMa Jun static int fill_in_l2_l3_pcache(struct kfd_cache_properties **props_ext,
1602c0cc999fSMa Jun 				struct kfd_gpu_cache_info *pcache_info,
1603c0cc999fSMa Jun 				struct kfd_cu_info *cu_info,
16040752e66eSMukul Joshi 				int cache_type, unsigned int cu_processor_id,
16050752e66eSMukul Joshi 				struct kfd_node *knode)
1606c0cc999fSMa Jun {
1607c0cc999fSMa Jun 	unsigned int cu_sibling_map_mask;
1608c0cc999fSMa Jun 	int first_active_cu;
16090752e66eSMukul Joshi 	int i, j, k, xcc, start, end;
1610c0cc999fSMa Jun 	struct kfd_cache_properties *pcache = NULL;
1611c0cc999fSMa Jun 
16120752e66eSMukul Joshi 	start = ffs(knode->xcc_mask) - 1;
16130752e66eSMukul Joshi 	end = start + NUM_XCC(knode->xcc_mask);
16140752e66eSMukul Joshi 	cu_sibling_map_mask = cu_info->cu_bitmap[start][0][0];
1615c0cc999fSMa Jun 	cu_sibling_map_mask &=
1616c0cc999fSMa Jun 		((1 << pcache_info[cache_type].num_cu_shared) - 1);
1617c0cc999fSMa Jun 	first_active_cu = ffs(cu_sibling_map_mask);
1618c0cc999fSMa Jun 
1619c0cc999fSMa Jun 	/* CU could be inactive. In case of shared cache find the first active
1620c0cc999fSMa Jun 	 * CU. and incase of non-shared cache check if the CU is inactive. If
1621c0cc999fSMa Jun 	 * inactive active skip it
1622c0cc999fSMa Jun 	 */
1623c0cc999fSMa Jun 	if (first_active_cu) {
1624c0cc999fSMa Jun 		pcache = kfd_alloc_struct(pcache);
1625c0cc999fSMa Jun 		if (!pcache)
1626c0cc999fSMa Jun 			return -ENOMEM;
1627c0cc999fSMa Jun 
1628c0cc999fSMa Jun 		memset(pcache, 0, sizeof(struct kfd_cache_properties));
1629c0cc999fSMa Jun 		pcache->processor_id_low = cu_processor_id
1630c0cc999fSMa Jun 					+ (first_active_cu - 1);
1631c0cc999fSMa Jun 		pcache->cache_level = pcache_info[cache_type].cache_level;
1632c0cc999fSMa Jun 		pcache->cache_size = pcache_info[cache_type].cache_size;
1633c0cc999fSMa Jun 
1634c0cc999fSMa Jun 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_DATA_CACHE)
1635c0cc999fSMa Jun 			pcache->cache_type |= HSA_CACHE_TYPE_DATA;
1636c0cc999fSMa Jun 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_INST_CACHE)
1637c0cc999fSMa Jun 			pcache->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
1638c0cc999fSMa Jun 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_CPU_CACHE)
1639c0cc999fSMa Jun 			pcache->cache_type |= HSA_CACHE_TYPE_CPU;
1640c0cc999fSMa Jun 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
1641c0cc999fSMa Jun 			pcache->cache_type |= HSA_CACHE_TYPE_HSACU;
1642c0cc999fSMa Jun 
1643c0cc999fSMa Jun 		/* Sibling map is w.r.t processor_id_low, so shift out
1644c0cc999fSMa Jun 		 * inactive CU
1645c0cc999fSMa Jun 		 */
1646c0cc999fSMa Jun 		cu_sibling_map_mask = cu_sibling_map_mask >> (first_active_cu - 1);
1647c0cc999fSMa Jun 		k = 0;
1648c0cc999fSMa Jun 
16490752e66eSMukul Joshi 		for (xcc = start; xcc < end; xcc++) {
1650c0cc999fSMa Jun 			for (i = 0; i < cu_info->num_shader_engines; i++) {
1651c0cc999fSMa Jun 				for (j = 0; j < cu_info->num_shader_arrays_per_engine; j++) {
1652c0cc999fSMa Jun 					pcache->sibling_map[k] = (uint8_t)(cu_sibling_map_mask & 0xFF);
1653c0cc999fSMa Jun 					pcache->sibling_map[k+1] = (uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
1654c0cc999fSMa Jun 					pcache->sibling_map[k+2] = (uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
1655c0cc999fSMa Jun 					pcache->sibling_map[k+3] = (uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
1656c0cc999fSMa Jun 					k += 4;
1657c0cc999fSMa Jun 
16580752e66eSMukul Joshi 					cu_sibling_map_mask = cu_info->cu_bitmap[xcc][i % 4][j + i / 4];
1659c0cc999fSMa Jun 					cu_sibling_map_mask &= ((1 << pcache_info[cache_type].num_cu_shared) - 1);
1660c0cc999fSMa Jun 				}
1661c0cc999fSMa Jun 			}
16620752e66eSMukul Joshi 		}
1663c0cc999fSMa Jun 		pcache->sibling_map_size = k;
1664c0cc999fSMa Jun 		*props_ext = pcache;
1665c0cc999fSMa Jun 		return 0;
1666c0cc999fSMa Jun 	}
1667c0cc999fSMa Jun 	return 1;
1668c0cc999fSMa Jun }
1669c0cc999fSMa Jun 
1670c0cc999fSMa Jun #define KFD_MAX_CACHE_TYPES 6
1671c0cc999fSMa Jun 
1672c0cc999fSMa Jun /* kfd_fill_cache_non_crat_info - Fill GPU cache info using kfd_gpu_cache_info
1673c0cc999fSMa Jun  * tables
1674c0cc999fSMa Jun  */
kfd_fill_cache_non_crat_info(struct kfd_topology_device * dev,struct kfd_node * kdev)16758dc1db31SMukul Joshi static void kfd_fill_cache_non_crat_info(struct kfd_topology_device *dev, struct kfd_node *kdev)
1676c0cc999fSMa Jun {
1677c0cc999fSMa Jun 	struct kfd_gpu_cache_info *pcache_info = NULL;
16780752e66eSMukul Joshi 	int i, j, k, xcc, start, end;
1679c0cc999fSMa Jun 	int ct = 0;
1680c0cc999fSMa Jun 	unsigned int cu_processor_id;
1681c0cc999fSMa Jun 	int ret;
1682c0cc999fSMa Jun 	unsigned int num_cu_shared;
1683c0cc999fSMa Jun 	struct kfd_cu_info cu_info;
1684c0cc999fSMa Jun 	struct kfd_cu_info *pcu_info;
1685c0cc999fSMa Jun 	int gpu_processor_id;
1686c0cc999fSMa Jun 	struct kfd_cache_properties *props_ext;
1687c0cc999fSMa Jun 	int num_of_entries = 0;
1688c0cc999fSMa Jun 	int num_of_cache_types = 0;
1689c0cc999fSMa Jun 	struct kfd_gpu_cache_info cache_info[KFD_MAX_CACHE_TYPES];
1690c0cc999fSMa Jun 
1691c0cc999fSMa Jun 	amdgpu_amdkfd_get_cu_info(kdev->adev, &cu_info);
1692c0cc999fSMa Jun 	pcu_info = &cu_info;
1693c0cc999fSMa Jun 
1694c0cc999fSMa Jun 	gpu_processor_id = dev->node_props.simd_id_base;
1695c0cc999fSMa Jun 
1696c0cc999fSMa Jun 	pcache_info = cache_info;
1697c0cc999fSMa Jun 	num_of_cache_types = kfd_get_gpu_cache_info(kdev, &pcache_info);
1698c0cc999fSMa Jun 	if (!num_of_cache_types) {
1699c0cc999fSMa Jun 		pr_warn("no cache info found\n");
1700c0cc999fSMa Jun 		return;
1701c0cc999fSMa Jun 	}
1702c0cc999fSMa Jun 
1703c0cc999fSMa Jun 	/* For each type of cache listed in the kfd_gpu_cache_info table,
1704c0cc999fSMa Jun 	 * go through all available Compute Units.
1705c0cc999fSMa Jun 	 * The [i,j,k] loop will
1706c0cc999fSMa Jun 	 *		if kfd_gpu_cache_info.num_cu_shared = 1
1707c0cc999fSMa Jun 	 *			will parse through all available CU
1708c0cc999fSMa Jun 	 *		If (kfd_gpu_cache_info.num_cu_shared != 1)
1709c0cc999fSMa Jun 	 *			then it will consider only one CU from
1710c0cc999fSMa Jun 	 *			the shared unit
1711c0cc999fSMa Jun 	 */
17120752e66eSMukul Joshi 	start = ffs(kdev->xcc_mask) - 1;
17130752e66eSMukul Joshi 	end = start + NUM_XCC(kdev->xcc_mask);
17140752e66eSMukul Joshi 
1715c0cc999fSMa Jun 	for (ct = 0; ct < num_of_cache_types; ct++) {
1716c0cc999fSMa Jun 		cu_processor_id = gpu_processor_id;
1717c0cc999fSMa Jun 		if (pcache_info[ct].cache_level == 1) {
17180752e66eSMukul Joshi 			for (xcc = start; xcc < end; xcc++) {
1719c0cc999fSMa Jun 				for (i = 0; i < pcu_info->num_shader_engines; i++) {
1720c0cc999fSMa Jun 					for (j = 0; j < pcu_info->num_shader_arrays_per_engine; j++) {
1721c0cc999fSMa Jun 						for (k = 0; k < pcu_info->num_cu_per_sh; k += pcache_info[ct].num_cu_shared) {
1722c0cc999fSMa Jun 
1723c0cc999fSMa Jun 							ret = fill_in_l1_pcache(&props_ext, pcache_info, pcu_info,
17240752e66eSMukul Joshi 										pcu_info->cu_bitmap[xcc][i % 4][j + i / 4], ct,
1725c0cc999fSMa Jun 										cu_processor_id, k);
1726c0cc999fSMa Jun 
1727c0cc999fSMa Jun 							if (ret < 0)
1728c0cc999fSMa Jun 								break;
1729c0cc999fSMa Jun 
1730c0cc999fSMa Jun 							if (!ret) {
1731c0cc999fSMa Jun 								num_of_entries++;
1732c0cc999fSMa Jun 								list_add_tail(&props_ext->list, &dev->cache_props);
1733c0cc999fSMa Jun 							}
1734c0cc999fSMa Jun 
1735c0cc999fSMa Jun 							/* Move to next CU block */
1736c0cc999fSMa Jun 							num_cu_shared = ((k + pcache_info[ct].num_cu_shared) <=
1737c0cc999fSMa Jun 								pcu_info->num_cu_per_sh) ?
1738c0cc999fSMa Jun 								pcache_info[ct].num_cu_shared :
1739c0cc999fSMa Jun 								(pcu_info->num_cu_per_sh - k);
1740c0cc999fSMa Jun 							cu_processor_id += num_cu_shared;
1741c0cc999fSMa Jun 						}
1742c0cc999fSMa Jun 					}
1743c0cc999fSMa Jun 				}
17440752e66eSMukul Joshi 			}
1745c0cc999fSMa Jun 		} else {
1746c0cc999fSMa Jun 			ret = fill_in_l2_l3_pcache(&props_ext, pcache_info,
17470752e66eSMukul Joshi 					pcu_info, ct, cu_processor_id, kdev);
1748c0cc999fSMa Jun 
1749c0cc999fSMa Jun 			if (ret < 0)
1750c0cc999fSMa Jun 				break;
1751c0cc999fSMa Jun 
1752c0cc999fSMa Jun 			if (!ret) {
1753c0cc999fSMa Jun 				num_of_entries++;
1754c0cc999fSMa Jun 				list_add_tail(&props_ext->list, &dev->cache_props);
1755c0cc999fSMa Jun 			}
1756c0cc999fSMa Jun 		}
1757c0cc999fSMa Jun 	}
1758c0cc999fSMa Jun 	dev->node_props.caches_count += num_of_entries;
1759c0cc999fSMa Jun 	pr_debug("Added [%d] GPU cache entries\n", num_of_entries);
1760c0cc999fSMa Jun }
1761c0cc999fSMa Jun 
kfd_topology_add_device_locked(struct kfd_node * gpu,uint32_t gpu_id,struct kfd_topology_device ** dev)17628dc1db31SMukul Joshi static int kfd_topology_add_device_locked(struct kfd_node *gpu, uint32_t gpu_id,
1763f701acb6SFelix Kuehling 					  struct kfd_topology_device **dev)
17645b5c4e40SEvgeny Pinchuk {
1765f701acb6SFelix Kuehling 	int proximity_domain = ++topology_crat_proximity_domain;
17664f449311SHarish Kasiviswanathan 	struct list_head temp_topology_device_list;
17673a87177eSHarish Kasiviswanathan 	void *crat_image = NULL;
17683a87177eSHarish Kasiviswanathan 	size_t image_size = 0;
1769f701acb6SFelix Kuehling 	int res;
177046d18d51SMukul Joshi 
17713a87177eSHarish Kasiviswanathan 	res = kfd_create_crat_image_virtual(&crat_image, &image_size,
17723a87177eSHarish Kasiviswanathan 					    COMPUTE_UNIT_GPU, gpu,
17733a87177eSHarish Kasiviswanathan 					    proximity_domain);
17743a87177eSHarish Kasiviswanathan 	if (res) {
17753a87177eSHarish Kasiviswanathan 		pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
17763a87177eSHarish Kasiviswanathan 		       gpu_id);
177746d18d51SMukul Joshi 		topology_crat_proximity_domain--;
1778f701acb6SFelix Kuehling 		goto err;
17793a87177eSHarish Kasiviswanathan 	}
1780c0cc999fSMa Jun 
1781f701acb6SFelix Kuehling 	INIT_LIST_HEAD(&temp_topology_device_list);
1782f701acb6SFelix Kuehling 
17833a87177eSHarish Kasiviswanathan 	res = kfd_parse_crat_table(crat_image,
17843a87177eSHarish Kasiviswanathan 				   &temp_topology_device_list,
17853a87177eSHarish Kasiviswanathan 				   proximity_domain);
17863a87177eSHarish Kasiviswanathan 	if (res) {
17873a87177eSHarish Kasiviswanathan 		pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
17883a87177eSHarish Kasiviswanathan 		       gpu_id);
178946d18d51SMukul Joshi 		topology_crat_proximity_domain--;
17905b5c4e40SEvgeny Pinchuk 		goto err;
17915b5c4e40SEvgeny Pinchuk 	}
17924f449311SHarish Kasiviswanathan 
17934f449311SHarish Kasiviswanathan 	kfd_topology_update_device_list(&temp_topology_device_list,
17944f449311SHarish Kasiviswanathan 					&topology_device_list);
17954f449311SHarish Kasiviswanathan 
1796f701acb6SFelix Kuehling 	*dev = kfd_assign_gpu(gpu);
1797f701acb6SFelix Kuehling 	if (WARN_ON(!*dev)) {
17983a87177eSHarish Kasiviswanathan 		res = -ENODEV;
17993a87177eSHarish Kasiviswanathan 		goto err;
18003a87177eSHarish Kasiviswanathan 	}
1801c0cc999fSMa Jun 
1802c0cc999fSMa Jun 	/* Fill the cache affinity information here for the GPUs
1803c0cc999fSMa Jun 	 * using VCRAT
1804c0cc999fSMa Jun 	 */
1805f701acb6SFelix Kuehling 	kfd_fill_cache_non_crat_info(*dev, gpu);
1806c0cc999fSMa Jun 
1807c0cc999fSMa Jun 	/* Update the SYSFS tree, since we added another topology
1808c0cc999fSMa Jun 	 * device
1809c0cc999fSMa Jun 	 */
1810c0cc999fSMa Jun 	res = kfd_topology_update_sysfs();
1811c0cc999fSMa Jun 	if (!res)
1812c0cc999fSMa Jun 		sys_props.generation_count++;
1813c0cc999fSMa Jun 	else
1814c0cc999fSMa Jun 		pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
1815c0cc999fSMa Jun 		       gpu_id, res);
1816f701acb6SFelix Kuehling 
1817f701acb6SFelix Kuehling err:
1818f701acb6SFelix Kuehling 	kfd_destroy_crat_image(crat_image);
1819f701acb6SFelix Kuehling 	return res;
18205b5c4e40SEvgeny Pinchuk }
1821f701acb6SFelix Kuehling 
kfd_topology_set_dbg_firmware_support(struct kfd_topology_device * dev)1822d230f1bfSJonathan Kim static void kfd_topology_set_dbg_firmware_support(struct kfd_topology_device *dev)
1823d230f1bfSJonathan Kim {
1824d230f1bfSJonathan Kim 	bool firmware_supported = true;
1825d230f1bfSJonathan Kim 
1826d230f1bfSJonathan Kim 	if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(11, 0, 0) &&
1827d230f1bfSJonathan Kim 			KFD_GC_VERSION(dev->gpu) < IP_VERSION(12, 0, 0)) {
182809d49e14SJonathan Kim 		uint32_t mes_api_rev = (dev->gpu->adev->mes.sched_version &
182909d49e14SJonathan Kim 						AMDGPU_MES_API_VERSION_MASK) >>
183009d49e14SJonathan Kim 						AMDGPU_MES_API_VERSION_SHIFT;
183109d49e14SJonathan Kim 		uint32_t mes_rev = dev->gpu->adev->mes.sched_version &
183209d49e14SJonathan Kim 						AMDGPU_MES_VERSION_MASK;
183309d49e14SJonathan Kim 
183409d49e14SJonathan Kim 		firmware_supported = (mes_api_rev >= 14) && (mes_rev >= 64);
1835d230f1bfSJonathan Kim 		goto out;
1836d230f1bfSJonathan Kim 	}
1837d230f1bfSJonathan Kim 
1838d230f1bfSJonathan Kim 	/*
1839d230f1bfSJonathan Kim 	 * Note: Any unlisted devices here are assumed to support exception handling.
1840d230f1bfSJonathan Kim 	 * Add additional checks here as needed.
1841d230f1bfSJonathan Kim 	 */
1842d230f1bfSJonathan Kim 	switch (KFD_GC_VERSION(dev->gpu)) {
1843d230f1bfSJonathan Kim 	case IP_VERSION(9, 0, 1):
1844d230f1bfSJonathan Kim 		firmware_supported = dev->gpu->kfd->mec_fw_version >= 459 + 32768;
1845d230f1bfSJonathan Kim 		break;
1846d230f1bfSJonathan Kim 	case IP_VERSION(9, 1, 0):
1847d230f1bfSJonathan Kim 	case IP_VERSION(9, 2, 1):
1848d230f1bfSJonathan Kim 	case IP_VERSION(9, 2, 2):
1849d230f1bfSJonathan Kim 	case IP_VERSION(9, 3, 0):
1850d230f1bfSJonathan Kim 	case IP_VERSION(9, 4, 0):
1851d230f1bfSJonathan Kim 		firmware_supported = dev->gpu->kfd->mec_fw_version >= 459;
1852d230f1bfSJonathan Kim 		break;
1853d230f1bfSJonathan Kim 	case IP_VERSION(9, 4, 1):
1854d230f1bfSJonathan Kim 		firmware_supported = dev->gpu->kfd->mec_fw_version >= 60;
1855d230f1bfSJonathan Kim 		break;
1856d230f1bfSJonathan Kim 	case IP_VERSION(9, 4, 2):
1857d230f1bfSJonathan Kim 		firmware_supported = dev->gpu->kfd->mec_fw_version >= 51;
1858d230f1bfSJonathan Kim 		break;
1859d230f1bfSJonathan Kim 	case IP_VERSION(10, 1, 10):
1860d230f1bfSJonathan Kim 	case IP_VERSION(10, 1, 2):
1861d230f1bfSJonathan Kim 	case IP_VERSION(10, 1, 1):
1862d230f1bfSJonathan Kim 		firmware_supported = dev->gpu->kfd->mec_fw_version >= 144;
1863d230f1bfSJonathan Kim 		break;
1864d230f1bfSJonathan Kim 	case IP_VERSION(10, 3, 0):
1865d230f1bfSJonathan Kim 	case IP_VERSION(10, 3, 2):
1866d230f1bfSJonathan Kim 	case IP_VERSION(10, 3, 1):
1867d230f1bfSJonathan Kim 	case IP_VERSION(10, 3, 4):
1868d230f1bfSJonathan Kim 	case IP_VERSION(10, 3, 5):
1869d230f1bfSJonathan Kim 		firmware_supported = dev->gpu->kfd->mec_fw_version >= 89;
1870d230f1bfSJonathan Kim 		break;
1871d230f1bfSJonathan Kim 	case IP_VERSION(10, 1, 3):
1872d230f1bfSJonathan Kim 	case IP_VERSION(10, 3, 3):
1873d230f1bfSJonathan Kim 		firmware_supported = false;
1874d230f1bfSJonathan Kim 		break;
1875d230f1bfSJonathan Kim 	default:
1876d230f1bfSJonathan Kim 		break;
1877d230f1bfSJonathan Kim 	}
1878d230f1bfSJonathan Kim 
1879d230f1bfSJonathan Kim out:
1880d230f1bfSJonathan Kim 	if (firmware_supported)
1881d230f1bfSJonathan Kim 		dev->node_props.capability |= HSA_CAP_TRAP_DEBUG_FIRMWARE_SUPPORTED;
1882d230f1bfSJonathan Kim }
1883d230f1bfSJonathan Kim 
kfd_topology_set_capabilities(struct kfd_topology_device * dev)1884d230f1bfSJonathan Kim static void kfd_topology_set_capabilities(struct kfd_topology_device *dev)
1885d230f1bfSJonathan Kim {
1886d230f1bfSJonathan Kim 	dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1887d230f1bfSJonathan Kim 				HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1888d230f1bfSJonathan Kim 				HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1889d230f1bfSJonathan Kim 
1890d230f1bfSJonathan Kim 	dev->node_props.capability |= HSA_CAP_TRAP_DEBUG_SUPPORT |
1891d230f1bfSJonathan Kim 			HSA_CAP_TRAP_DEBUG_WAVE_LAUNCH_TRAP_OVERRIDE_SUPPORTED |
1892d230f1bfSJonathan Kim 			HSA_CAP_TRAP_DEBUG_WAVE_LAUNCH_MODE_SUPPORTED;
1893d230f1bfSJonathan Kim 
1894fc7f1d96SJonathan Kim 	if (kfd_dbg_has_ttmps_always_setup(dev->gpu))
1895fc7f1d96SJonathan Kim 		dev->node_props.debug_prop |= HSA_DBG_DISPATCH_INFO_ALWAYS_VALID;
1896fc7f1d96SJonathan Kim 
1897d230f1bfSJonathan Kim 	if (KFD_GC_VERSION(dev->gpu) < IP_VERSION(10, 0, 0)) {
1898567db9e0SJonathan Kim 		if (KFD_GC_VERSION(dev->gpu) == IP_VERSION(9, 4, 3))
1899567db9e0SJonathan Kim 			dev->node_props.debug_prop |=
1900567db9e0SJonathan Kim 				HSA_DBG_WATCH_ADDR_MASK_LO_BIT_GFX9_4_3 |
1901567db9e0SJonathan Kim 				HSA_DBG_WATCH_ADDR_MASK_HI_BIT_GFX9_4_3;
1902567db9e0SJonathan Kim 		else
1903567db9e0SJonathan Kim 			dev->node_props.debug_prop |=
1904567db9e0SJonathan Kim 				HSA_DBG_WATCH_ADDR_MASK_LO_BIT_GFX9 |
1905d230f1bfSJonathan Kim 				HSA_DBG_WATCH_ADDR_MASK_HI_BIT;
1906d230f1bfSJonathan Kim 
19078e436326SJonathan Kim 		if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(9, 4, 2))
1908d230f1bfSJonathan Kim 			dev->node_props.capability |=
1909d230f1bfSJonathan Kim 				HSA_CAP_TRAP_DEBUG_PRECISE_MEMORY_OPERATIONS_SUPPORTED;
1910d230f1bfSJonathan Kim 	} else {
1911d230f1bfSJonathan Kim 		dev->node_props.debug_prop |= HSA_DBG_WATCH_ADDR_MASK_LO_BIT_GFX10 |
1912d230f1bfSJonathan Kim 					HSA_DBG_WATCH_ADDR_MASK_HI_BIT;
1913d230f1bfSJonathan Kim 
1914fc7f1d96SJonathan Kim 		if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(11, 0, 0))
1915d230f1bfSJonathan Kim 			dev->node_props.capability |=
1916d230f1bfSJonathan Kim 				HSA_CAP_TRAP_DEBUG_PRECISE_MEMORY_OPERATIONS_SUPPORTED;
1917d230f1bfSJonathan Kim 	}
1918d230f1bfSJonathan Kim 
1919d230f1bfSJonathan Kim 	kfd_topology_set_dbg_firmware_support(dev);
1920d230f1bfSJonathan Kim }
1921d230f1bfSJonathan Kim 
kfd_topology_add_device(struct kfd_node * gpu)19228dc1db31SMukul Joshi int kfd_topology_add_device(struct kfd_node *gpu)
1923f701acb6SFelix Kuehling {
1924f701acb6SFelix Kuehling 	uint32_t gpu_id;
1925f701acb6SFelix Kuehling 	struct kfd_topology_device *dev;
1926f701acb6SFelix Kuehling 	struct kfd_cu_info cu_info;
1927f701acb6SFelix Kuehling 	int res = 0;
1928f701acb6SFelix Kuehling 	int i;
1929f701acb6SFelix Kuehling 	const char *asic_name = amdgpu_asic_name[gpu->adev->asic_type];
1930f701acb6SFelix Kuehling 
1931f701acb6SFelix Kuehling 	gpu_id = kfd_generate_gpu_id(gpu);
1932400a39f1SJames Zhu 	if (gpu->xcp && !gpu->xcp->ddev) {
1933400a39f1SJames Zhu 		dev_warn(gpu->adev->dev,
1934400a39f1SJames Zhu 		"Won't add GPU (ID: 0x%x) to topology since it has no drm node assigned.",
1935400a39f1SJames Zhu 		gpu_id);
1936400a39f1SJames Zhu 		return 0;
1937400a39f1SJames Zhu 	} else {
1938f701acb6SFelix Kuehling 		pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1939400a39f1SJames Zhu 	}
1940f701acb6SFelix Kuehling 
1941f701acb6SFelix Kuehling 	/* Check to see if this gpu device exists in the topology_device_list.
1942f701acb6SFelix Kuehling 	 * If so, assign the gpu to that device,
1943f701acb6SFelix Kuehling 	 * else create a Virtual CRAT for this gpu device and then parse that
1944f701acb6SFelix Kuehling 	 * CRAT to create a new topology device. Once created assign the gpu to
1945f701acb6SFelix Kuehling 	 * that topology device
1946f701acb6SFelix Kuehling 	 */
1947f701acb6SFelix Kuehling 	down_write(&topology_lock);
1948f701acb6SFelix Kuehling 	dev = kfd_assign_gpu(gpu);
1949f701acb6SFelix Kuehling 	if (!dev)
1950f701acb6SFelix Kuehling 		res = kfd_topology_add_device_locked(gpu, gpu_id, &dev);
1951c0cc999fSMa Jun 	up_write(&topology_lock);
1952f701acb6SFelix Kuehling 	if (res)
1953f701acb6SFelix Kuehling 		return res;
19545b5c4e40SEvgeny Pinchuk 
19555b5c4e40SEvgeny Pinchuk 	dev->gpu_id = gpu_id;
19565b5c4e40SEvgeny Pinchuk 	gpu->id = gpu_id;
19573a87177eSHarish Kasiviswanathan 
19580f28cca8SRamesh Errabolu 	kfd_dev_create_p2p_links();
19590f28cca8SRamesh Errabolu 
19603a87177eSHarish Kasiviswanathan 	/* TODO: Move the following lines to function
19613a87177eSHarish Kasiviswanathan 	 *	kfd_add_non_crat_information
19623a87177eSHarish Kasiviswanathan 	 */
19633a87177eSHarish Kasiviswanathan 
19643a87177eSHarish Kasiviswanathan 	/* Fill-in additional information that is not available in CRAT but
19653a87177eSHarish Kasiviswanathan 	 * needed for the topology
19663a87177eSHarish Kasiviswanathan 	 */
19673a87177eSHarish Kasiviswanathan 
1968574c4183SGraham Sider 	amdgpu_amdkfd_get_cu_info(dev->gpu->adev, &cu_info);
1969c181159aSYong Zhao 
1970b7675b7bSGraham Sider 	for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1; i++) {
1971b7675b7bSGraham Sider 		dev->node_props.name[i] = __tolower(asic_name[i]);
1972b7675b7bSGraham Sider 		if (asic_name[i] == '\0')
1973b7675b7bSGraham Sider 			break;
1974b7675b7bSGraham Sider 	}
1975b7675b7bSGraham Sider 	dev->node_props.name[i] = '\0';
1976c181159aSYong Zhao 
19773a87177eSHarish Kasiviswanathan 	dev->node_props.simd_arrays_per_engine =
19783a87177eSHarish Kasiviswanathan 		cu_info.num_shader_arrays_per_engine;
19793a87177eSHarish Kasiviswanathan 
19808dc1db31SMukul Joshi 	dev->node_props.gfx_target_version =
19818dc1db31SMukul Joshi 				gpu->kfd->device_info.gfx_target_version;
1982d69a3b76SMukul Joshi 	dev->node_props.vendor_id = gpu->adev->pdev->vendor;
1983d69a3b76SMukul Joshi 	dev->node_props.device_id = gpu->adev->pdev->device;
1984c6d1ec41SJoseph Greathouse 	dev->node_props.capability |=
198502274fc0SGraham Sider 		((dev->gpu->adev->rev_id << HSA_CAP_ASIC_REVISION_SHIFT) &
1986c6d1ec41SJoseph Greathouse 			HSA_CAP_ASIC_REVISION_MASK);
198792085240SJonathan Kim 
1988d69a3b76SMukul Joshi 	dev->node_props.location_id = pci_dev_id(gpu->adev->pdev);
198992085240SJonathan Kim 	if (KFD_GC_VERSION(dev->gpu->kfd) == IP_VERSION(9, 4, 3))
199092085240SJonathan Kim 		dev->node_props.location_id |= dev->gpu->node_id;
199192085240SJonathan Kim 
1992d69a3b76SMukul Joshi 	dev->node_props.domain = pci_domain_nr(gpu->adev->pdev->bus);
19933a87177eSHarish Kasiviswanathan 	dev->node_props.max_engine_clk_fcompute =
1994574c4183SGraham Sider 		amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->adev);
19953a87177eSHarish Kasiviswanathan 	dev->node_props.max_engine_clk_ccompute =
19963a87177eSHarish Kasiviswanathan 		cpufreq_quick_get_max(0) / 1000;
1997a476c0c6SPhilip Yang 
1998a476c0c6SPhilip Yang 	if (gpu->xcp)
1999a476c0c6SPhilip Yang 		dev->node_props.drm_render_minor = gpu->xcp->ddev->render->index;
2000a476c0c6SPhilip Yang 	else
20017c9b7171SOak Zeng 		dev->node_props.drm_render_minor =
20028dc1db31SMukul Joshi 				gpu->kfd->shared_resources.drm_render_minor;
20035b5c4e40SEvgeny Pinchuk 
20048dc1db31SMukul Joshi 	dev->node_props.hive_id = gpu->kfd->hive_id;
2005ee2f17f4SAmber Lin 	dev->node_props.num_sdma_engines = kfd_get_num_sdma_engines(gpu);
200614568cf6SOak Zeng 	dev->node_props.num_sdma_xgmi_engines =
2007ee2f17f4SAmber Lin 					kfd_get_num_xgmi_sdma_engines(gpu);
2008bb71c74dSHuang Rui 	dev->node_props.num_sdma_queues_per_engine =
20098dc1db31SMukul Joshi 				gpu->kfd->device_info.num_sdma_queues_per_engine -
20108dc1db31SMukul Joshi 				gpu->kfd->device_info.num_reserved_sdma_queues_per_engine;
201129633d0eSJoseph Greathouse 	dev->node_props.num_gws = (dev->gpu->gws &&
201229e76462SOak Zeng 		dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
201302274fc0SGraham Sider 		dev->gpu->adev->gds.gws_size : 0;
2014e6945304SYong Zhao 	dev->node_props.num_cp_queues = get_cp_queues_num(dev->gpu->dqm);
20150c1690e3SShaoyun Liu 
20163a87177eSHarish Kasiviswanathan 	kfd_fill_mem_clk_max_info(dev);
20173a87177eSHarish Kasiviswanathan 	kfd_fill_iolink_non_crat_info(dev);
20183a87177eSHarish Kasiviswanathan 
20197eb0502aSGraham Sider 	switch (dev->gpu->adev->asic_type) {
20203a87177eSHarish Kasiviswanathan 	case CHIP_KAVERI:
20213a87177eSHarish Kasiviswanathan 	case CHIP_HAWAII:
20223a87177eSHarish Kasiviswanathan 	case CHIP_TONGA:
20233a87177eSHarish Kasiviswanathan 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
20243a87177eSHarish Kasiviswanathan 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
20253a87177eSHarish Kasiviswanathan 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
20263a87177eSHarish Kasiviswanathan 		break;
20273a87177eSHarish Kasiviswanathan 	case CHIP_CARRIZO:
20283a87177eSHarish Kasiviswanathan 	case CHIP_FIJI:
20293a87177eSHarish Kasiviswanathan 	case CHIP_POLARIS10:
20303a87177eSHarish Kasiviswanathan 	case CHIP_POLARIS11:
2031846a44d7SGang Ba 	case CHIP_POLARIS12:
2032ed81cd6eSKent Russell 	case CHIP_VEGAM:
203342aa8793SFelix Kuehling 		pr_debug("Adding doorbell packet type capability\n");
20343a87177eSHarish Kasiviswanathan 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
20353a87177eSHarish Kasiviswanathan 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
20363a87177eSHarish Kasiviswanathan 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
20373a87177eSHarish Kasiviswanathan 		break;
2038e4804a39SGraham Sider 	default:
2039d230f1bfSJonathan Kim 		if (KFD_GC_VERSION(dev->gpu) < IP_VERSION(9, 0, 1))
20403a87177eSHarish Kasiviswanathan 			WARN(1, "Unexpected ASIC family %u",
20417eb0502aSGraham Sider 			     dev->gpu->adev->asic_type);
2042d230f1bfSJonathan Kim 		else
2043d230f1bfSJonathan Kim 			kfd_topology_set_capabilities(dev);
20447639a8c4SBen Goz 	}
20457639a8c4SBen Goz 
20461ae99eabSOak Zeng 	/*
20471ae99eabSOak Zeng 	 * Overwrite ATS capability according to needs_iommu_device to fix
20481ae99eabSOak Zeng 	 * potential missing corresponding bit in CRAT of BIOS.
20491ae99eabSOak Zeng 	 */
20501ae99eabSOak Zeng 	dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;
20511ae99eabSOak Zeng 
20523a87177eSHarish Kasiviswanathan 	/* Fix errors in CZ CRAT.
20533a87177eSHarish Kasiviswanathan 	 * simd_count: Carrizo CRAT reports wrong simd_count, probably
20543a87177eSHarish Kasiviswanathan 	 *		because it doesn't consider masked out CUs
205570f372bfSPhilip Cox 	 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
20563a87177eSHarish Kasiviswanathan 	 */
20577eb0502aSGraham Sider 	if (dev->gpu->adev->asic_type == CHIP_CARRIZO) {
20583a87177eSHarish Kasiviswanathan 		dev->node_props.simd_count =
20593a87177eSHarish Kasiviswanathan 			cu_info.simd_per_cu * cu_info.cu_active_number;
206070f372bfSPhilip Cox 		dev->node_props.max_waves_per_simd = 10;
206170f372bfSPhilip Cox 	}
20623a87177eSHarish Kasiviswanathan 
20635436ab94SStanley.Yang 	/* kfd only concerns sram ecc on GFX and HBM ecc on UMC */
20640dee45a2SEric Huang 	dev->node_props.capability |=
206556c5977eSGraham Sider 		((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0) ?
20660dee45a2SEric Huang 		HSA_CAP_SRAM_EDCSUPPORTED : 0;
206756c5977eSGraham Sider 	dev->node_props.capability |=
206856c5977eSGraham Sider 		((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
20690dee45a2SEric Huang 		HSA_CAP_MEM_EDCSUPPORTED : 0;
20700dee45a2SEric Huang 
2071046e674bSGraham Sider 	if (KFD_GC_VERSION(dev->gpu) != IP_VERSION(9, 0, 1))
207256c5977eSGraham Sider 		dev->node_props.capability |= (dev->gpu->adev->ras_enabled != 0) ?
20730dee45a2SEric Huang 			HSA_CAP_RASEVENTNOTIFY : 0;
20740dee45a2SEric Huang 
2075610dab11SPhilip Yang 	if (KFD_IS_SVM_API_SUPPORTED(dev->gpu->adev))
20764c166eb9SPhilip Yang 		dev->node_props.capability |= HSA_CAP_SVMAPI_SUPPORTED;
20774c166eb9SPhilip Yang 
207803d400e7SAlex Sierra 	if (dev->gpu->adev->gmc.is_app_apu ||
207903d400e7SAlex Sierra 		dev->gpu->adev->gmc.xgmi.connected_to_cpu)
208003d400e7SAlex Sierra 		dev->node_props.capability |= HSA_CAP_FLAGS_COHERENTHOSTACCESS;
208103d400e7SAlex Sierra 
20823a87177eSHarish Kasiviswanathan 	kfd_debug_print_topology();
20833a87177eSHarish Kasiviswanathan 
20845b5c4e40SEvgeny Pinchuk 	kfd_notify_gpu_change(gpu_id, 1);
2085f701acb6SFelix Kuehling 
20867d4f8db4SDan Carpenter 	return 0;
20875b5c4e40SEvgeny Pinchuk }
20885b5c4e40SEvgeny Pinchuk 
208946d18d51SMukul Joshi /**
209046d18d51SMukul Joshi  * kfd_topology_update_io_links() - Update IO links after device removal.
209146d18d51SMukul Joshi  * @proximity_domain: Proximity domain value of the dev being removed.
209246d18d51SMukul Joshi  *
209346d18d51SMukul Joshi  * The topology list currently is arranged in increasing order of
209446d18d51SMukul Joshi  * proximity domain.
209546d18d51SMukul Joshi  *
209646d18d51SMukul Joshi  * Two things need to be done when a device is removed:
209746d18d51SMukul Joshi  * 1. All the IO links to this device need to be removed.
209846d18d51SMukul Joshi  * 2. All nodes after the current device node need to move
209946d18d51SMukul Joshi  *    up once this device node is removed from the topology
210046d18d51SMukul Joshi  *    list. As a result, the proximity domain values for
210146d18d51SMukul Joshi  *    all nodes after the node being deleted reduce by 1.
210246d18d51SMukul Joshi  *    This would also cause the proximity domain values for
210346d18d51SMukul Joshi  *    io links to be updated based on new proximity domain
210446d18d51SMukul Joshi  *    values.
210546d18d51SMukul Joshi  *
210646d18d51SMukul Joshi  * Context: The caller must hold write topology_lock.
210746d18d51SMukul Joshi  */
kfd_topology_update_io_links(int proximity_domain)210846d18d51SMukul Joshi static void kfd_topology_update_io_links(int proximity_domain)
210946d18d51SMukul Joshi {
211046d18d51SMukul Joshi 	struct kfd_topology_device *dev;
21110f28cca8SRamesh Errabolu 	struct kfd_iolink_properties *iolink, *p2plink, *tmp;
211246d18d51SMukul Joshi 
211346d18d51SMukul Joshi 	list_for_each_entry(dev, &topology_device_list, list) {
211446d18d51SMukul Joshi 		if (dev->proximity_domain > proximity_domain)
211546d18d51SMukul Joshi 			dev->proximity_domain--;
211646d18d51SMukul Joshi 
211746d18d51SMukul Joshi 		list_for_each_entry_safe(iolink, tmp, &dev->io_link_props, list) {
211846d18d51SMukul Joshi 			/*
211946d18d51SMukul Joshi 			 * If there is an io link to the dev being deleted
212046d18d51SMukul Joshi 			 * then remove that IO link also.
212146d18d51SMukul Joshi 			 */
212246d18d51SMukul Joshi 			if (iolink->node_to == proximity_domain) {
212346d18d51SMukul Joshi 				list_del(&iolink->list);
212446d18d51SMukul Joshi 				dev->node_props.io_links_count--;
212598447635SMukul Joshi 			} else {
212698447635SMukul Joshi 				if (iolink->node_from > proximity_domain)
212746d18d51SMukul Joshi 					iolink->node_from--;
212898447635SMukul Joshi 				if (iolink->node_to > proximity_domain)
212946d18d51SMukul Joshi 					iolink->node_to--;
213046d18d51SMukul Joshi 			}
213146d18d51SMukul Joshi 		}
21320f28cca8SRamesh Errabolu 
21330f28cca8SRamesh Errabolu 		list_for_each_entry_safe(p2plink, tmp, &dev->p2p_link_props, list) {
21340f28cca8SRamesh Errabolu 			/*
21350f28cca8SRamesh Errabolu 			 * If there is a p2p link to the dev being deleted
21360f28cca8SRamesh Errabolu 			 * then remove that p2p link also.
21370f28cca8SRamesh Errabolu 			 */
21380f28cca8SRamesh Errabolu 			if (p2plink->node_to == proximity_domain) {
21390f28cca8SRamesh Errabolu 				list_del(&p2plink->list);
21400f28cca8SRamesh Errabolu 				dev->node_props.p2p_links_count--;
21410f28cca8SRamesh Errabolu 			} else {
21420f28cca8SRamesh Errabolu 				if (p2plink->node_from > proximity_domain)
21430f28cca8SRamesh Errabolu 					p2plink->node_from--;
21440f28cca8SRamesh Errabolu 				if (p2plink->node_to > proximity_domain)
21450f28cca8SRamesh Errabolu 					p2plink->node_to--;
21460f28cca8SRamesh Errabolu 			}
21470f28cca8SRamesh Errabolu 		}
214846d18d51SMukul Joshi 	}
214946d18d51SMukul Joshi }
215046d18d51SMukul Joshi 
kfd_topology_remove_device(struct kfd_node * gpu)21518dc1db31SMukul Joshi int kfd_topology_remove_device(struct kfd_node *gpu)
21525b5c4e40SEvgeny Pinchuk {
21534f449311SHarish Kasiviswanathan 	struct kfd_topology_device *dev, *tmp;
21545b5c4e40SEvgeny Pinchuk 	uint32_t gpu_id;
21555b5c4e40SEvgeny Pinchuk 	int res = -ENODEV;
215646d18d51SMukul Joshi 	int i = 0;
21575b5c4e40SEvgeny Pinchuk 
21585b5c4e40SEvgeny Pinchuk 	down_write(&topology_lock);
21595b5c4e40SEvgeny Pinchuk 
216046d18d51SMukul Joshi 	list_for_each_entry_safe(dev, tmp, &topology_device_list, list) {
21615b5c4e40SEvgeny Pinchuk 		if (dev->gpu == gpu) {
21625b5c4e40SEvgeny Pinchuk 			gpu_id = dev->gpu_id;
21635b5c4e40SEvgeny Pinchuk 			kfd_remove_sysfs_node_entry(dev);
21645b5c4e40SEvgeny Pinchuk 			kfd_release_topology_device(dev);
21654f449311SHarish Kasiviswanathan 			sys_props.num_devices--;
216646d18d51SMukul Joshi 			kfd_topology_update_io_links(i);
216746d18d51SMukul Joshi 			topology_crat_proximity_domain = sys_props.num_devices-1;
216846d18d51SMukul Joshi 			sys_props.generation_count++;
21695b5c4e40SEvgeny Pinchuk 			res = 0;
21705b5c4e40SEvgeny Pinchuk 			if (kfd_topology_update_sysfs() < 0)
21715b5c4e40SEvgeny Pinchuk 				kfd_topology_release_sysfs();
21725b5c4e40SEvgeny Pinchuk 			break;
21735b5c4e40SEvgeny Pinchuk 		}
217446d18d51SMukul Joshi 		i++;
217546d18d51SMukul Joshi 	}
21765b5c4e40SEvgeny Pinchuk 
21775b5c4e40SEvgeny Pinchuk 	up_write(&topology_lock);
21785b5c4e40SEvgeny Pinchuk 
2179174de876SFelix Kuehling 	if (!res)
21805b5c4e40SEvgeny Pinchuk 		kfd_notify_gpu_change(gpu_id, 0);
21815b5c4e40SEvgeny Pinchuk 
21825b5c4e40SEvgeny Pinchuk 	return res;
21835b5c4e40SEvgeny Pinchuk }
21845b5c4e40SEvgeny Pinchuk 
21856d82eb0eSHarish Kasiviswanathan /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
21866d82eb0eSHarish Kasiviswanathan  *	topology. If GPU device is found @idx, then valid kfd_dev pointer is
21876d82eb0eSHarish Kasiviswanathan  *	returned through @kdev
21886d82eb0eSHarish Kasiviswanathan  * Return -	0: On success (@kdev will be NULL for non GPU nodes)
21896d82eb0eSHarish Kasiviswanathan  *		-1: If end of list
21905b5c4e40SEvgeny Pinchuk  */
kfd_topology_enum_kfd_devices(uint8_t idx,struct kfd_node ** kdev)21918dc1db31SMukul Joshi int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_node **kdev)
21925b5c4e40SEvgeny Pinchuk {
21935b5c4e40SEvgeny Pinchuk 
21945b5c4e40SEvgeny Pinchuk 	struct kfd_topology_device *top_dev;
21955b5c4e40SEvgeny Pinchuk 	uint8_t device_idx = 0;
21965b5c4e40SEvgeny Pinchuk 
21976d82eb0eSHarish Kasiviswanathan 	*kdev = NULL;
21985b5c4e40SEvgeny Pinchuk 	down_read(&topology_lock);
21995b5c4e40SEvgeny Pinchuk 
22005b5c4e40SEvgeny Pinchuk 	list_for_each_entry(top_dev, &topology_device_list, list) {
22015b5c4e40SEvgeny Pinchuk 		if (device_idx == idx) {
22026d82eb0eSHarish Kasiviswanathan 			*kdev = top_dev->gpu;
22036d82eb0eSHarish Kasiviswanathan 			up_read(&topology_lock);
22046d82eb0eSHarish Kasiviswanathan 			return 0;
22055b5c4e40SEvgeny Pinchuk 		}
22065b5c4e40SEvgeny Pinchuk 
22075b5c4e40SEvgeny Pinchuk 		device_idx++;
22085b5c4e40SEvgeny Pinchuk 	}
22095b5c4e40SEvgeny Pinchuk 
22105b5c4e40SEvgeny Pinchuk 	up_read(&topology_lock);
22115b5c4e40SEvgeny Pinchuk 
22126d82eb0eSHarish Kasiviswanathan 	return -1;
22135b5c4e40SEvgeny Pinchuk 
22145b5c4e40SEvgeny Pinchuk }
2215851a645eSFelix Kuehling 
kfd_cpumask_to_apic_id(const struct cpumask * cpumask)2216520b8fb7SFelix Kuehling static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
2217520b8fb7SFelix Kuehling {
2218520b8fb7SFelix Kuehling 	int first_cpu_of_numa_node;
2219520b8fb7SFelix Kuehling 
2220520b8fb7SFelix Kuehling 	if (!cpumask || cpumask == cpu_none_mask)
2221520b8fb7SFelix Kuehling 		return -1;
2222520b8fb7SFelix Kuehling 	first_cpu_of_numa_node = cpumask_first(cpumask);
2223520b8fb7SFelix Kuehling 	if (first_cpu_of_numa_node >= nr_cpu_ids)
2224520b8fb7SFelix Kuehling 		return -1;
2225df1dd4f4SFelix Kuehling #ifdef CONFIG_X86_64
2226df1dd4f4SFelix Kuehling 	return cpu_data(first_cpu_of_numa_node).apicid;
2227df1dd4f4SFelix Kuehling #else
2228df1dd4f4SFelix Kuehling 	return first_cpu_of_numa_node;
2229df1dd4f4SFelix Kuehling #endif
2230520b8fb7SFelix Kuehling }
2231520b8fb7SFelix Kuehling 
2232520b8fb7SFelix Kuehling /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
2233520b8fb7SFelix Kuehling  *	of the given NUMA node (numa_node_id)
2234520b8fb7SFelix Kuehling  * Return -1 on failure
2235520b8fb7SFelix Kuehling  */
kfd_numa_node_to_apic_id(int numa_node_id)2236520b8fb7SFelix Kuehling int kfd_numa_node_to_apic_id(int numa_node_id)
2237520b8fb7SFelix Kuehling {
2238520b8fb7SFelix Kuehling 	if (numa_node_id == -1) {
2239520b8fb7SFelix Kuehling 		pr_warn("Invalid NUMA Node. Use online CPU mask\n");
2240520b8fb7SFelix Kuehling 		return kfd_cpumask_to_apic_id(cpu_online_mask);
2241520b8fb7SFelix Kuehling 	}
2242520b8fb7SFelix Kuehling 	return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
2243520b8fb7SFelix Kuehling }
2244520b8fb7SFelix Kuehling 
2245851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS)
2246851a645eSFelix Kuehling 
kfd_debugfs_hqds_by_device(struct seq_file * m,void * data)2247851a645eSFelix Kuehling int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
2248851a645eSFelix Kuehling {
2249851a645eSFelix Kuehling 	struct kfd_topology_device *dev;
2250851a645eSFelix Kuehling 	unsigned int i = 0;
2251851a645eSFelix Kuehling 	int r = 0;
2252851a645eSFelix Kuehling 
2253851a645eSFelix Kuehling 	down_read(&topology_lock);
2254851a645eSFelix Kuehling 
2255851a645eSFelix Kuehling 	list_for_each_entry(dev, &topology_device_list, list) {
2256851a645eSFelix Kuehling 		if (!dev->gpu) {
2257851a645eSFelix Kuehling 			i++;
2258851a645eSFelix Kuehling 			continue;
2259851a645eSFelix Kuehling 		}
2260851a645eSFelix Kuehling 
2261851a645eSFelix Kuehling 		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
2262851a645eSFelix Kuehling 		r = dqm_debugfs_hqds(m, dev->gpu->dqm);
2263851a645eSFelix Kuehling 		if (r)
2264851a645eSFelix Kuehling 			break;
2265851a645eSFelix Kuehling 	}
2266851a645eSFelix Kuehling 
2267851a645eSFelix Kuehling 	up_read(&topology_lock);
2268851a645eSFelix Kuehling 
2269851a645eSFelix Kuehling 	return r;
2270851a645eSFelix Kuehling }
2271851a645eSFelix Kuehling 
kfd_debugfs_rls_by_device(struct seq_file * m,void * data)2272851a645eSFelix Kuehling int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
2273851a645eSFelix Kuehling {
2274851a645eSFelix Kuehling 	struct kfd_topology_device *dev;
2275851a645eSFelix Kuehling 	unsigned int i = 0;
2276851a645eSFelix Kuehling 	int r = 0;
2277851a645eSFelix Kuehling 
2278851a645eSFelix Kuehling 	down_read(&topology_lock);
2279851a645eSFelix Kuehling 
2280851a645eSFelix Kuehling 	list_for_each_entry(dev, &topology_device_list, list) {
2281851a645eSFelix Kuehling 		if (!dev->gpu) {
2282851a645eSFelix Kuehling 			i++;
2283851a645eSFelix Kuehling 			continue;
2284851a645eSFelix Kuehling 		}
2285851a645eSFelix Kuehling 
2286851a645eSFelix Kuehling 		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
22879af5379cSOak Zeng 		r = pm_debugfs_runlist(m, &dev->gpu->dqm->packet_mgr);
2288851a645eSFelix Kuehling 		if (r)
2289851a645eSFelix Kuehling 			break;
2290851a645eSFelix Kuehling 	}
2291851a645eSFelix Kuehling 
2292851a645eSFelix Kuehling 	up_read(&topology_lock);
2293851a645eSFelix Kuehling 
2294851a645eSFelix Kuehling 	return r;
2295851a645eSFelix Kuehling }
2296851a645eSFelix Kuehling 
2297851a645eSFelix Kuehling #endif
2298