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