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) {
961f163ba83SMichael Chen sys_props.platform_id = dev->oem_id64;
962520b8fb7SFelix Kuehling sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
963520b8fb7SFelix Kuehling sys_props.platform_rev = dev->oem_revision;
964520b8fb7SFelix Kuehling }
965520b8fb7SFelix Kuehling up_read(&topology_lock);
966520b8fb7SFelix Kuehling }
967520b8fb7SFelix Kuehling
find_system_memory(const struct dmi_header * dm,void * private)968520b8fb7SFelix Kuehling static void find_system_memory(const struct dmi_header *dm,
969520b8fb7SFelix Kuehling void *private)
970520b8fb7SFelix Kuehling {
971520b8fb7SFelix Kuehling struct kfd_mem_properties *mem;
972520b8fb7SFelix Kuehling u16 mem_width, mem_clock;
973520b8fb7SFelix Kuehling struct kfd_topology_device *kdev =
974520b8fb7SFelix Kuehling (struct kfd_topology_device *)private;
975520b8fb7SFelix Kuehling const u8 *dmi_data = (const u8 *)(dm + 1);
976520b8fb7SFelix Kuehling
977520b8fb7SFelix Kuehling if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
978520b8fb7SFelix Kuehling mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
979520b8fb7SFelix Kuehling mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
980520b8fb7SFelix Kuehling list_for_each_entry(mem, &kdev->mem_props, list) {
981520b8fb7SFelix Kuehling if (mem_width != 0xFFFF && mem_width != 0)
982520b8fb7SFelix Kuehling mem->width = mem_width;
983520b8fb7SFelix Kuehling if (mem_clock != 0)
984520b8fb7SFelix Kuehling mem->mem_clk_max = mem_clock;
985520b8fb7SFelix Kuehling }
986520b8fb7SFelix Kuehling }
987520b8fb7SFelix Kuehling }
988f4757347SAmber Lin
989520b8fb7SFelix Kuehling /* kfd_add_non_crat_information - Add information that is not currently
990520b8fb7SFelix Kuehling * defined in CRAT but is necessary for KFD topology
991520b8fb7SFelix Kuehling * @dev - topology device to which addition info is added
992520b8fb7SFelix Kuehling */
kfd_add_non_crat_information(struct kfd_topology_device * kdev)993520b8fb7SFelix Kuehling static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
994520b8fb7SFelix Kuehling {
995520b8fb7SFelix Kuehling /* Check if CPU only node. */
996520b8fb7SFelix Kuehling if (!kdev->gpu) {
997520b8fb7SFelix Kuehling /* Add system memory information */
998520b8fb7SFelix Kuehling dmi_walk(find_system_memory, kdev);
999520b8fb7SFelix Kuehling }
1000520b8fb7SFelix Kuehling /* TODO: For GPU node, rearrange code from kfd_topology_add_device */
1001520b8fb7SFelix Kuehling }
1002520b8fb7SFelix Kuehling
kfd_topology_init(void)10035b5c4e40SEvgeny Pinchuk int kfd_topology_init(void)
10045b5c4e40SEvgeny Pinchuk {
100516b9201cSOded Gabbay void *crat_image = NULL;
10065b5c4e40SEvgeny Pinchuk size_t image_size = 0;
10075b5c4e40SEvgeny Pinchuk int ret;
10084f449311SHarish Kasiviswanathan struct list_head temp_topology_device_list;
1009520b8fb7SFelix Kuehling int cpu_only_node = 0;
1010520b8fb7SFelix Kuehling struct kfd_topology_device *kdev;
1011520b8fb7SFelix Kuehling int proximity_domain;
10125b5c4e40SEvgeny Pinchuk
10134f449311SHarish Kasiviswanathan /* topology_device_list - Master list of all topology devices
10144f449311SHarish Kasiviswanathan * temp_topology_device_list - temporary list created while parsing CRAT
10154f449311SHarish Kasiviswanathan * or VCRAT. Once parsing is complete the contents of list is moved to
10164f449311SHarish Kasiviswanathan * topology_device_list
10175b5c4e40SEvgeny Pinchuk */
10184f449311SHarish Kasiviswanathan
10194f449311SHarish Kasiviswanathan /* Initialize the head for the both the lists */
10205b5c4e40SEvgeny Pinchuk INIT_LIST_HEAD(&topology_device_list);
10214f449311SHarish Kasiviswanathan INIT_LIST_HEAD(&temp_topology_device_list);
10225b5c4e40SEvgeny Pinchuk init_rwsem(&topology_lock);
10235b5c4e40SEvgeny Pinchuk
10245b5c4e40SEvgeny Pinchuk memset(&sys_props, 0, sizeof(sys_props));
10255b5c4e40SEvgeny Pinchuk
1026520b8fb7SFelix Kuehling /* Proximity domains in ACPI CRAT tables start counting at
1027520b8fb7SFelix Kuehling * 0. The same should be true for virtual CRAT tables created
1028520b8fb7SFelix Kuehling * at this stage. GPUs added later in kfd_topology_add_device
1029520b8fb7SFelix Kuehling * use a counter.
1030520b8fb7SFelix Kuehling */
1031520b8fb7SFelix Kuehling proximity_domain = 0;
1032520b8fb7SFelix Kuehling
1033520b8fb7SFelix Kuehling ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
1034520b8fb7SFelix Kuehling COMPUTE_UNIT_CPU, NULL,
1035520b8fb7SFelix Kuehling proximity_domain);
1036520b8fb7SFelix Kuehling cpu_only_node = 1;
1037520b8fb7SFelix Kuehling if (ret) {
1038520b8fb7SFelix Kuehling pr_err("Error creating VCRAT table for CPU\n");
1039520b8fb7SFelix Kuehling return ret;
1040520b8fb7SFelix Kuehling }
1041520b8fb7SFelix Kuehling
1042520b8fb7SFelix Kuehling ret = kfd_parse_crat_table(crat_image,
1043520b8fb7SFelix Kuehling &temp_topology_device_list,
1044520b8fb7SFelix Kuehling proximity_domain);
1045520b8fb7SFelix Kuehling if (ret) {
1046520b8fb7SFelix Kuehling pr_err("Error parsing VCRAT table for CPU\n");
10478e05247dSHarish Kasiviswanathan goto err;
1048520b8fb7SFelix Kuehling }
10495b5c4e40SEvgeny Pinchuk
1050f4757347SAmber Lin kdev = list_first_entry(&temp_topology_device_list,
1051f4757347SAmber Lin struct kfd_topology_device, list);
1052f4757347SAmber Lin
10535b5c4e40SEvgeny Pinchuk down_write(&topology_lock);
10544f449311SHarish Kasiviswanathan kfd_topology_update_device_list(&temp_topology_device_list,
10554f449311SHarish Kasiviswanathan &topology_device_list);
105646d18d51SMukul Joshi topology_crat_proximity_domain = sys_props.num_devices-1;
10575b5c4e40SEvgeny Pinchuk ret = kfd_topology_update_sysfs();
10585b5c4e40SEvgeny Pinchuk up_write(&topology_lock);
10598e05247dSHarish Kasiviswanathan
10604f449311SHarish Kasiviswanathan if (!ret) {
10614f449311SHarish Kasiviswanathan sys_props.generation_count++;
1062520b8fb7SFelix Kuehling kfd_update_system_properties();
1063520b8fb7SFelix Kuehling kfd_debug_print_topology();
10644f449311SHarish Kasiviswanathan } else
10658e05247dSHarish Kasiviswanathan pr_err("Failed to update topology in sysfs ret=%d\n", ret);
10665b5c4e40SEvgeny Pinchuk
1067520b8fb7SFelix Kuehling /* For nodes with GPU, this information gets added
1068520b8fb7SFelix Kuehling * when GPU is detected (kfd_topology_add_device).
1069520b8fb7SFelix Kuehling */
1070520b8fb7SFelix Kuehling if (cpu_only_node) {
1071520b8fb7SFelix Kuehling /* Add additional information to CPU only node created above */
1072520b8fb7SFelix Kuehling down_write(&topology_lock);
1073520b8fb7SFelix Kuehling kdev = list_first_entry(&topology_device_list,
1074520b8fb7SFelix Kuehling struct kfd_topology_device, list);
1075520b8fb7SFelix Kuehling up_write(&topology_lock);
1076520b8fb7SFelix Kuehling kfd_add_non_crat_information(kdev);
1077520b8fb7SFelix Kuehling }
1078520b8fb7SFelix Kuehling
10795b5c4e40SEvgeny Pinchuk err:
10808e05247dSHarish Kasiviswanathan kfd_destroy_crat_image(crat_image);
10815b5c4e40SEvgeny Pinchuk return ret;
10825b5c4e40SEvgeny Pinchuk }
10835b5c4e40SEvgeny Pinchuk
kfd_topology_shutdown(void)10845b5c4e40SEvgeny Pinchuk void kfd_topology_shutdown(void)
10855b5c4e40SEvgeny Pinchuk {
10864f449311SHarish Kasiviswanathan down_write(&topology_lock);
10875b5c4e40SEvgeny Pinchuk kfd_topology_release_sysfs();
10885b5c4e40SEvgeny Pinchuk kfd_release_live_view();
10894f449311SHarish Kasiviswanathan up_write(&topology_lock);
10905b5c4e40SEvgeny Pinchuk }
10915b5c4e40SEvgeny Pinchuk
kfd_generate_gpu_id(struct kfd_node * gpu)10928dc1db31SMukul Joshi static uint32_t kfd_generate_gpu_id(struct kfd_node *gpu)
10935b5c4e40SEvgeny Pinchuk {
10945b5c4e40SEvgeny Pinchuk uint32_t hashout;
109574c5b85dSMukul Joshi uint32_t buf[8];
1096585f0e6cSEdward O'Callaghan uint64_t local_mem_size;
10975b5c4e40SEvgeny Pinchuk int i;
10985b5c4e40SEvgeny Pinchuk
10995b5c4e40SEvgeny Pinchuk if (!gpu)
11005b5c4e40SEvgeny Pinchuk return 0;
11015b5c4e40SEvgeny Pinchuk
1102315e29ecSMukul Joshi local_mem_size = gpu->local_mem_info.local_mem_size_private +
1103315e29ecSMukul Joshi gpu->local_mem_info.local_mem_size_public;
1104d69a3b76SMukul Joshi buf[0] = gpu->adev->pdev->devfn;
1105d69a3b76SMukul Joshi buf[1] = gpu->adev->pdev->subsystem_vendor |
1106d69a3b76SMukul Joshi (gpu->adev->pdev->subsystem_device << 16);
1107d69a3b76SMukul Joshi buf[2] = pci_domain_nr(gpu->adev->pdev->bus);
1108d69a3b76SMukul Joshi buf[3] = gpu->adev->pdev->device;
1109d69a3b76SMukul Joshi buf[4] = gpu->adev->pdev->bus->number;
1110585f0e6cSEdward O'Callaghan buf[5] = lower_32_bits(local_mem_size);
1111585f0e6cSEdward O'Callaghan buf[6] = upper_32_bits(local_mem_size);
1112c4050ff1SLijo Lazar buf[7] = (ffs(gpu->xcc_mask) - 1) | (NUM_XCC(gpu->xcc_mask) << 16);
11135b5c4e40SEvgeny Pinchuk
111474c5b85dSMukul Joshi for (i = 0, hashout = 0; i < 8; i++)
11155b5c4e40SEvgeny Pinchuk hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
11165b5c4e40SEvgeny Pinchuk
11175b5c4e40SEvgeny Pinchuk return hashout;
11185b5c4e40SEvgeny Pinchuk }
11193a87177eSHarish Kasiviswanathan /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
11203a87177eSHarish Kasiviswanathan * the GPU device is not already present in the topology device
11213a87177eSHarish Kasiviswanathan * list then return NULL. This means a new topology device has to
11223a87177eSHarish Kasiviswanathan * be created for this GPU.
11233a87177eSHarish Kasiviswanathan */
kfd_assign_gpu(struct kfd_node * gpu)11248dc1db31SMukul Joshi static struct kfd_topology_device *kfd_assign_gpu(struct kfd_node *gpu)
11255b5c4e40SEvgeny Pinchuk {
11265b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev;
112716b9201cSOded Gabbay struct kfd_topology_device *out_dev = NULL;
1128171bc67eSHarish Kasiviswanathan struct kfd_mem_properties *mem;
1129171bc67eSHarish Kasiviswanathan struct kfd_cache_properties *cache;
1130171bc67eSHarish Kasiviswanathan struct kfd_iolink_properties *iolink;
11310f28cca8SRamesh Errabolu struct kfd_iolink_properties *p2plink;
11325b5c4e40SEvgeny Pinchuk
1133b8fe0524SFelix Kuehling list_for_each_entry(dev, &topology_device_list, list) {
1134b8fe0524SFelix Kuehling /* Discrete GPUs need their own topology device list
1135b8fe0524SFelix Kuehling * entries. Don't assign them to CPU/APU nodes.
1136b8fe0524SFelix Kuehling */
1137c99a2e7aSAlex Deucher if (dev->node_props.cpu_cores_count)
1138b8fe0524SFelix Kuehling continue;
1139b8fe0524SFelix Kuehling
11404eacc26bSKent Russell if (!dev->gpu && (dev->node_props.simd_count > 0)) {
11415b5c4e40SEvgeny Pinchuk dev->gpu = gpu;
11425b5c4e40SEvgeny Pinchuk out_dev = dev;
1143171bc67eSHarish Kasiviswanathan
1144171bc67eSHarish Kasiviswanathan list_for_each_entry(mem, &dev->mem_props, list)
1145171bc67eSHarish Kasiviswanathan mem->gpu = dev->gpu;
1146171bc67eSHarish Kasiviswanathan list_for_each_entry(cache, &dev->cache_props, list)
1147171bc67eSHarish Kasiviswanathan cache->gpu = dev->gpu;
1148171bc67eSHarish Kasiviswanathan list_for_each_entry(iolink, &dev->io_link_props, list)
1149171bc67eSHarish Kasiviswanathan iolink->gpu = dev->gpu;
11500f28cca8SRamesh Errabolu list_for_each_entry(p2plink, &dev->p2p_link_props, list)
11510f28cca8SRamesh Errabolu p2plink->gpu = dev->gpu;
11525b5c4e40SEvgeny Pinchuk break;
11535b5c4e40SEvgeny Pinchuk }
1154b8fe0524SFelix Kuehling }
11555b5c4e40SEvgeny Pinchuk return out_dev;
11565b5c4e40SEvgeny Pinchuk }
11575b5c4e40SEvgeny Pinchuk
kfd_notify_gpu_change(uint32_t gpu_id,int arrival)11585b5c4e40SEvgeny Pinchuk static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
11595b5c4e40SEvgeny Pinchuk {
11605b5c4e40SEvgeny Pinchuk /*
11615b5c4e40SEvgeny Pinchuk * TODO: Generate an event for thunk about the arrival/removal
11625b5c4e40SEvgeny Pinchuk * of the GPU
11635b5c4e40SEvgeny Pinchuk */
11645b5c4e40SEvgeny Pinchuk }
11655b5c4e40SEvgeny Pinchuk
11663a87177eSHarish Kasiviswanathan /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
11673a87177eSHarish Kasiviswanathan * patch this after CRAT parsing.
11683a87177eSHarish Kasiviswanathan */
kfd_fill_mem_clk_max_info(struct kfd_topology_device * dev)11693a87177eSHarish Kasiviswanathan static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
11703a87177eSHarish Kasiviswanathan {
11713a87177eSHarish Kasiviswanathan struct kfd_mem_properties *mem;
11723a87177eSHarish Kasiviswanathan struct kfd_local_mem_info local_mem_info;
11733a87177eSHarish Kasiviswanathan
11743a87177eSHarish Kasiviswanathan if (!dev)
11753a87177eSHarish Kasiviswanathan return;
11763a87177eSHarish Kasiviswanathan
11773a87177eSHarish Kasiviswanathan /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
11783a87177eSHarish Kasiviswanathan * single bank of VRAM local memory.
11793a87177eSHarish Kasiviswanathan * for dGPUs - VCRAT reports only one bank of Local Memory
11803a87177eSHarish Kasiviswanathan * for APUs - If CRAT from ACPI reports more than one bank, then
11813a87177eSHarish Kasiviswanathan * all the banks will report the same mem_clk_max information
11823a87177eSHarish Kasiviswanathan */
1183315e29ecSMukul Joshi amdgpu_amdkfd_get_local_mem_info(dev->gpu->adev, &local_mem_info,
11849a3ce1a7SHawking Zhang dev->gpu->xcp);
11853a87177eSHarish Kasiviswanathan
11863a87177eSHarish Kasiviswanathan list_for_each_entry(mem, &dev->mem_props, list)
11873a87177eSHarish Kasiviswanathan mem->mem_clk_max = local_mem_info.mem_clk_max;
11883a87177eSHarish Kasiviswanathan }
11893a87177eSHarish Kasiviswanathan
kfd_set_iolink_no_atomics(struct kfd_topology_device * dev,struct kfd_topology_device * target_gpu_dev,struct kfd_iolink_properties * link)1190bdd24657SJonathan Kim static void kfd_set_iolink_no_atomics(struct kfd_topology_device *dev,
1191bdd24657SJonathan Kim struct kfd_topology_device *target_gpu_dev,
1192bdd24657SJonathan Kim struct kfd_iolink_properties *link)
11933a87177eSHarish Kasiviswanathan {
1194bdd24657SJonathan Kim /* xgmi always supports atomics between links. */
1195bdd24657SJonathan Kim if (link->iolink_type == CRAT_IOLINK_TYPE_XGMI)
11963a87177eSHarish Kasiviswanathan return;
11973a87177eSHarish Kasiviswanathan
1198bdd24657SJonathan Kim /* check pcie support to set cpu(dev) flags for target_gpu_dev link. */
1199bdd24657SJonathan Kim if (target_gpu_dev) {
1200bdd24657SJonathan Kim uint32_t cap;
1201bdd24657SJonathan Kim
1202d69a3b76SMukul Joshi pcie_capability_read_dword(target_gpu_dev->gpu->adev->pdev,
1203d35f00d8SEric Huang PCI_EXP_DEVCAP2, &cap);
1204d35f00d8SEric Huang
1205d35f00d8SEric Huang if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1206d35f00d8SEric Huang PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1207bdd24657SJonathan Kim link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
12083a87177eSHarish Kasiviswanathan CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1209bdd24657SJonathan Kim /* set gpu (dev) flags. */
1210bdd24657SJonathan Kim } else {
12118dc1db31SMukul Joshi if (!dev->gpu->kfd->pci_atomic_requested ||
12127eb0502aSGraham Sider dev->gpu->adev->asic_type == CHIP_HAWAII)
1213bdd24657SJonathan Kim link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1214d35f00d8SEric Huang CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1215deb68983SJonathan Kim }
1216bdd24657SJonathan Kim }
1217bdd24657SJonathan Kim
kfd_set_iolink_non_coherent(struct kfd_topology_device * to_dev,struct kfd_iolink_properties * outbound_link,struct kfd_iolink_properties * inbound_link)1218c9cfbf7fSEric Huang static void kfd_set_iolink_non_coherent(struct kfd_topology_device *to_dev,
1219c9cfbf7fSEric Huang struct kfd_iolink_properties *outbound_link,
1220c9cfbf7fSEric Huang struct kfd_iolink_properties *inbound_link)
1221c9cfbf7fSEric Huang {
1222c9cfbf7fSEric Huang /* CPU -> GPU with PCIe */
1223c9cfbf7fSEric Huang if (!to_dev->gpu &&
1224c9cfbf7fSEric Huang inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
1225c9cfbf7fSEric Huang inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1226c9cfbf7fSEric Huang
1227c9cfbf7fSEric Huang if (to_dev->gpu) {
1228c9cfbf7fSEric Huang /* GPU <-> GPU with PCIe and
1229c9cfbf7fSEric Huang * Vega20 with XGMI
1230c9cfbf7fSEric Huang */
1231c9cfbf7fSEric Huang if (inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS ||
1232c9cfbf7fSEric Huang (inbound_link->iolink_type == CRAT_IOLINK_TYPE_XGMI &&
1233046e674bSGraham Sider KFD_GC_VERSION(to_dev->gpu) == IP_VERSION(9, 4, 0))) {
1234c9cfbf7fSEric Huang outbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1235c9cfbf7fSEric Huang inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1236c9cfbf7fSEric Huang }
1237c9cfbf7fSEric Huang }
1238c9cfbf7fSEric Huang }
1239c9cfbf7fSEric Huang
kfd_fill_iolink_non_crat_info(struct kfd_topology_device * dev)1240bdd24657SJonathan Kim static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
1241bdd24657SJonathan Kim {
1242bdd24657SJonathan Kim struct kfd_iolink_properties *link, *inbound_link;
1243bdd24657SJonathan Kim struct kfd_topology_device *peer_dev;
1244bdd24657SJonathan Kim
1245bdd24657SJonathan Kim if (!dev || !dev->gpu)
1246bdd24657SJonathan Kim return;
1247d35f00d8SEric Huang
1248d35f00d8SEric Huang /* GPU only creates direct links so apply flags setting to all */
1249d35f00d8SEric Huang list_for_each_entry(link, &dev->io_link_props, list) {
1250bdd24657SJonathan Kim link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1251bdd24657SJonathan Kim kfd_set_iolink_no_atomics(dev, NULL, link);
1252bdd24657SJonathan Kim peer_dev = kfd_topology_device_by_proximity_domain(
1253d35f00d8SEric Huang link->node_to);
1254bdd24657SJonathan Kim
1255bdd24657SJonathan Kim if (!peer_dev)
1256bdd24657SJonathan Kim continue;
1257bdd24657SJonathan Kim
1258a0af5dbdSJonathan Kim /* Include the CPU peer in GPU hive if connected over xGMI. */
1259b2ef2fdfSRajneesh Bhardwaj if (!peer_dev->gpu &&
12601698e200SJonathan Kim link->iolink_type == CRAT_IOLINK_TYPE_XGMI) {
12611698e200SJonathan Kim /*
12621698e200SJonathan Kim * If the GPU is not part of a GPU hive, use its pci
12631698e200SJonathan Kim * device location as the hive ID to bind with the CPU.
12641698e200SJonathan Kim */
12651698e200SJonathan Kim if (!dev->node_props.hive_id)
12661698e200SJonathan Kim dev->node_props.hive_id = pci_dev_id(dev->gpu->adev->pdev);
1267a0af5dbdSJonathan Kim peer_dev->node_props.hive_id = dev->node_props.hive_id;
12681698e200SJonathan Kim }
1269a0af5dbdSJonathan Kim
1270bdd24657SJonathan Kim list_for_each_entry(inbound_link, &peer_dev->io_link_props,
1271bdd24657SJonathan Kim list) {
1272bdd24657SJonathan Kim if (inbound_link->node_to != link->node_from)
1273bdd24657SJonathan Kim continue;
1274bdd24657SJonathan Kim
1275bdd24657SJonathan Kim inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1276bdd24657SJonathan Kim kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
1277c9cfbf7fSEric Huang kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
1278d35f00d8SEric Huang }
1279d35f00d8SEric Huang }
12800f28cca8SRamesh Errabolu
12810f28cca8SRamesh Errabolu /* Create indirect links so apply flags setting to all */
12820f28cca8SRamesh Errabolu list_for_each_entry(link, &dev->p2p_link_props, list) {
12830f28cca8SRamesh Errabolu link->flags = CRAT_IOLINK_FLAGS_ENABLED;
12840f28cca8SRamesh Errabolu kfd_set_iolink_no_atomics(dev, NULL, link);
12850f28cca8SRamesh Errabolu peer_dev = kfd_topology_device_by_proximity_domain(
12860f28cca8SRamesh Errabolu link->node_to);
12870f28cca8SRamesh Errabolu
12880f28cca8SRamesh Errabolu if (!peer_dev)
12890f28cca8SRamesh Errabolu continue;
12900f28cca8SRamesh Errabolu
12910f28cca8SRamesh Errabolu list_for_each_entry(inbound_link, &peer_dev->p2p_link_props,
12920f28cca8SRamesh Errabolu list) {
12930f28cca8SRamesh Errabolu if (inbound_link->node_to != link->node_from)
12940f28cca8SRamesh Errabolu continue;
12950f28cca8SRamesh Errabolu
12960f28cca8SRamesh Errabolu inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
12970f28cca8SRamesh Errabolu kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
12980f28cca8SRamesh Errabolu kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
12990f28cca8SRamesh Errabolu }
13000f28cca8SRamesh Errabolu }
13010f28cca8SRamesh Errabolu }
13020f28cca8SRamesh Errabolu
kfd_build_p2p_node_entry(struct kfd_topology_device * dev,struct kfd_iolink_properties * p2plink)13030f28cca8SRamesh Errabolu static int kfd_build_p2p_node_entry(struct kfd_topology_device *dev,
13040f28cca8SRamesh Errabolu struct kfd_iolink_properties *p2plink)
13050f28cca8SRamesh Errabolu {
13060f28cca8SRamesh Errabolu int ret;
13070f28cca8SRamesh Errabolu
13080f28cca8SRamesh Errabolu p2plink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
13090f28cca8SRamesh Errabolu if (!p2plink->kobj)
13100f28cca8SRamesh Errabolu return -ENOMEM;
13110f28cca8SRamesh Errabolu
13120f28cca8SRamesh Errabolu ret = kobject_init_and_add(p2plink->kobj, &iolink_type,
13130f28cca8SRamesh Errabolu dev->kobj_p2plink, "%d", dev->node_props.p2p_links_count - 1);
13140f28cca8SRamesh Errabolu if (ret < 0) {
13150f28cca8SRamesh Errabolu kobject_put(p2plink->kobj);
13160f28cca8SRamesh Errabolu return ret;
13170f28cca8SRamesh Errabolu }
13180f28cca8SRamesh Errabolu
13190f28cca8SRamesh Errabolu p2plink->attr.name = "properties";
13200f28cca8SRamesh Errabolu p2plink->attr.mode = KFD_SYSFS_FILE_MODE;
13210f28cca8SRamesh Errabolu sysfs_attr_init(&p2plink->attr);
13220f28cca8SRamesh Errabolu ret = sysfs_create_file(p2plink->kobj, &p2plink->attr);
13230f28cca8SRamesh Errabolu if (ret < 0)
13240f28cca8SRamesh Errabolu return ret;
13250f28cca8SRamesh Errabolu
13260f28cca8SRamesh Errabolu return 0;
13270f28cca8SRamesh Errabolu }
13280f28cca8SRamesh Errabolu
kfd_create_indirect_link_prop(struct kfd_topology_device * kdev,int gpu_node)13290f28cca8SRamesh Errabolu static int kfd_create_indirect_link_prop(struct kfd_topology_device *kdev, int gpu_node)
13300f28cca8SRamesh Errabolu {
13317d50b92dSDan Carpenter struct kfd_iolink_properties *gpu_link, *tmp_link, *cpu_link;
13320f28cca8SRamesh Errabolu struct kfd_iolink_properties *props = NULL, *props2 = NULL;
13330f28cca8SRamesh Errabolu struct kfd_topology_device *cpu_dev;
13340f28cca8SRamesh Errabolu int ret = 0;
13350f28cca8SRamesh Errabolu int i, num_cpu;
13360f28cca8SRamesh Errabolu
13370f28cca8SRamesh Errabolu num_cpu = 0;
13380f28cca8SRamesh Errabolu list_for_each_entry(cpu_dev, &topology_device_list, list) {
13390f28cca8SRamesh Errabolu if (cpu_dev->gpu)
13400f28cca8SRamesh Errabolu break;
13410f28cca8SRamesh Errabolu num_cpu++;
13420f28cca8SRamesh Errabolu }
13430f28cca8SRamesh Errabolu
13445024cce8SSrinivasan Shanmugam if (list_empty(&kdev->io_link_props))
13455024cce8SSrinivasan Shanmugam return -ENODATA;
13465024cce8SSrinivasan Shanmugam
13470f28cca8SRamesh Errabolu gpu_link = list_first_entry(&kdev->io_link_props,
13480f28cca8SRamesh Errabolu struct kfd_iolink_properties, list);
13490f28cca8SRamesh Errabolu
13500f28cca8SRamesh Errabolu for (i = 0; i < num_cpu; i++) {
13510f28cca8SRamesh Errabolu /* CPU <--> GPU */
13520f28cca8SRamesh Errabolu if (gpu_link->node_to == i)
13530f28cca8SRamesh Errabolu continue;
13540f28cca8SRamesh Errabolu
13550f28cca8SRamesh Errabolu /* find CPU <--> CPU links */
13567d50b92dSDan Carpenter cpu_link = NULL;
13570f28cca8SRamesh Errabolu cpu_dev = kfd_topology_device_by_proximity_domain(i);
13580f28cca8SRamesh Errabolu if (cpu_dev) {
13597d50b92dSDan Carpenter list_for_each_entry(tmp_link,
13600f28cca8SRamesh Errabolu &cpu_dev->io_link_props, list) {
13617d50b92dSDan Carpenter if (tmp_link->node_to == gpu_link->node_to) {
13627d50b92dSDan Carpenter cpu_link = tmp_link;
13630f28cca8SRamesh Errabolu break;
13640f28cca8SRamesh Errabolu }
13650f28cca8SRamesh Errabolu }
13667d50b92dSDan Carpenter }
13670f28cca8SRamesh Errabolu
13687d50b92dSDan Carpenter if (!cpu_link)
13690f28cca8SRamesh Errabolu return -ENOMEM;
13700f28cca8SRamesh Errabolu
13710f28cca8SRamesh Errabolu /* CPU <--> CPU <--> GPU, GPU node*/
13720f28cca8SRamesh Errabolu props = kfd_alloc_struct(props);
13730f28cca8SRamesh Errabolu if (!props)
13740f28cca8SRamesh Errabolu return -ENOMEM;
13750f28cca8SRamesh Errabolu
13760f28cca8SRamesh Errabolu memcpy(props, gpu_link, sizeof(struct kfd_iolink_properties));
13770f28cca8SRamesh Errabolu props->weight = gpu_link->weight + cpu_link->weight;
13780f28cca8SRamesh Errabolu props->min_latency = gpu_link->min_latency + cpu_link->min_latency;
13790f28cca8SRamesh Errabolu props->max_latency = gpu_link->max_latency + cpu_link->max_latency;
13800f28cca8SRamesh Errabolu props->min_bandwidth = min(gpu_link->min_bandwidth, cpu_link->min_bandwidth);
13810f28cca8SRamesh Errabolu props->max_bandwidth = min(gpu_link->max_bandwidth, cpu_link->max_bandwidth);
13820f28cca8SRamesh Errabolu
13830f28cca8SRamesh Errabolu props->node_from = gpu_node;
13840f28cca8SRamesh Errabolu props->node_to = i;
13850f28cca8SRamesh Errabolu kdev->node_props.p2p_links_count++;
13860f28cca8SRamesh Errabolu list_add_tail(&props->list, &kdev->p2p_link_props);
13870f28cca8SRamesh Errabolu ret = kfd_build_p2p_node_entry(kdev, props);
13880f28cca8SRamesh Errabolu if (ret < 0)
13890f28cca8SRamesh Errabolu return ret;
13900f28cca8SRamesh Errabolu
13910f28cca8SRamesh Errabolu /* for small Bar, no CPU --> GPU in-direct links */
13920f28cca8SRamesh Errabolu if (kfd_dev_is_large_bar(kdev->gpu)) {
13930f28cca8SRamesh Errabolu /* CPU <--> CPU <--> GPU, CPU node*/
13940f28cca8SRamesh Errabolu props2 = kfd_alloc_struct(props2);
13950f28cca8SRamesh Errabolu if (!props2)
13960f28cca8SRamesh Errabolu return -ENOMEM;
13970f28cca8SRamesh Errabolu
13980f28cca8SRamesh Errabolu memcpy(props2, props, sizeof(struct kfd_iolink_properties));
13990f28cca8SRamesh Errabolu props2->node_from = i;
14000f28cca8SRamesh Errabolu props2->node_to = gpu_node;
14010f28cca8SRamesh Errabolu props2->kobj = NULL;
14020f28cca8SRamesh Errabolu cpu_dev->node_props.p2p_links_count++;
14030f28cca8SRamesh Errabolu list_add_tail(&props2->list, &cpu_dev->p2p_link_props);
14040f28cca8SRamesh Errabolu ret = kfd_build_p2p_node_entry(cpu_dev, props2);
14050f28cca8SRamesh Errabolu if (ret < 0)
14060f28cca8SRamesh Errabolu return ret;
14070f28cca8SRamesh Errabolu }
14080f28cca8SRamesh Errabolu }
14090f28cca8SRamesh Errabolu return ret;
14100f28cca8SRamesh Errabolu }
14110f28cca8SRamesh Errabolu
14120f28cca8SRamesh 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)14130f28cca8SRamesh Errabolu static int kfd_add_peer_prop(struct kfd_topology_device *kdev,
14140f28cca8SRamesh Errabolu struct kfd_topology_device *peer, int from, int to)
14150f28cca8SRamesh Errabolu {
14160f28cca8SRamesh Errabolu struct kfd_iolink_properties *props = NULL;
14170f28cca8SRamesh Errabolu struct kfd_iolink_properties *iolink1, *iolink2, *iolink3;
14180f28cca8SRamesh Errabolu struct kfd_topology_device *cpu_dev;
14190f28cca8SRamesh Errabolu int ret = 0;
14200f28cca8SRamesh Errabolu
14210f28cca8SRamesh Errabolu if (!amdgpu_device_is_peer_accessible(
14220f28cca8SRamesh Errabolu kdev->gpu->adev,
14230f28cca8SRamesh Errabolu peer->gpu->adev))
14240f28cca8SRamesh Errabolu return ret;
14250f28cca8SRamesh Errabolu
14265024cce8SSrinivasan Shanmugam if (list_empty(&kdev->io_link_props))
14275024cce8SSrinivasan Shanmugam return -ENODATA;
14285024cce8SSrinivasan Shanmugam
14290f28cca8SRamesh Errabolu iolink1 = list_first_entry(&kdev->io_link_props,
14300f28cca8SRamesh Errabolu struct kfd_iolink_properties, list);
14315024cce8SSrinivasan Shanmugam
14325024cce8SSrinivasan Shanmugam if (list_empty(&peer->io_link_props))
14335024cce8SSrinivasan Shanmugam return -ENODATA;
14340f28cca8SRamesh Errabolu
14350f28cca8SRamesh Errabolu iolink2 = list_first_entry(&peer->io_link_props,
14360f28cca8SRamesh Errabolu struct kfd_iolink_properties, list);
14370f28cca8SRamesh Errabolu
14380f28cca8SRamesh Errabolu props = kfd_alloc_struct(props);
14390f28cca8SRamesh Errabolu if (!props)
14400f28cca8SRamesh Errabolu return -ENOMEM;
14410f28cca8SRamesh Errabolu
14420f28cca8SRamesh Errabolu memcpy(props, iolink1, sizeof(struct kfd_iolink_properties));
14430f28cca8SRamesh Errabolu
14440f28cca8SRamesh Errabolu props->weight = iolink1->weight + iolink2->weight;
14450f28cca8SRamesh Errabolu props->min_latency = iolink1->min_latency + iolink2->min_latency;
14460f28cca8SRamesh Errabolu props->max_latency = iolink1->max_latency + iolink2->max_latency;
14470f28cca8SRamesh Errabolu props->min_bandwidth = min(iolink1->min_bandwidth, iolink2->min_bandwidth);
14480f28cca8SRamesh Errabolu props->max_bandwidth = min(iolink2->max_bandwidth, iolink2->max_bandwidth);
14490f28cca8SRamesh Errabolu
14500f28cca8SRamesh Errabolu if (iolink1->node_to != iolink2->node_to) {
14510f28cca8SRamesh Errabolu /* CPU->CPU link*/
14520f28cca8SRamesh Errabolu cpu_dev = kfd_topology_device_by_proximity_domain(iolink1->node_to);
14530f28cca8SRamesh Errabolu if (cpu_dev) {
1454583e0a33SSrinivasan Shanmugam list_for_each_entry(iolink3, &cpu_dev->io_link_props, list) {
1455583e0a33SSrinivasan Shanmugam if (iolink3->node_to != iolink2->node_to)
1456583e0a33SSrinivasan Shanmugam continue;
14570f28cca8SRamesh Errabolu
14580f28cca8SRamesh Errabolu props->weight += iolink3->weight;
14590f28cca8SRamesh Errabolu props->min_latency += iolink3->min_latency;
14600f28cca8SRamesh Errabolu props->max_latency += iolink3->max_latency;
14610f28cca8SRamesh Errabolu props->min_bandwidth = min(props->min_bandwidth,
14620f28cca8SRamesh Errabolu iolink3->min_bandwidth);
14630f28cca8SRamesh Errabolu props->max_bandwidth = min(props->max_bandwidth,
14640f28cca8SRamesh Errabolu iolink3->max_bandwidth);
1465583e0a33SSrinivasan Shanmugam break;
1466583e0a33SSrinivasan Shanmugam }
14670f28cca8SRamesh Errabolu } else {
14680f28cca8SRamesh Errabolu WARN(1, "CPU node not found");
14690f28cca8SRamesh Errabolu }
14700f28cca8SRamesh Errabolu }
14710f28cca8SRamesh Errabolu
14720f28cca8SRamesh Errabolu props->node_from = from;
14730f28cca8SRamesh Errabolu props->node_to = to;
14740f28cca8SRamesh Errabolu peer->node_props.p2p_links_count++;
14750f28cca8SRamesh Errabolu list_add_tail(&props->list, &peer->p2p_link_props);
14760f28cca8SRamesh Errabolu ret = kfd_build_p2p_node_entry(peer, props);
14770f28cca8SRamesh Errabolu
14780f28cca8SRamesh Errabolu return ret;
14790f28cca8SRamesh Errabolu }
14800f28cca8SRamesh Errabolu #endif
14810f28cca8SRamesh Errabolu
kfd_dev_create_p2p_links(void)14820f28cca8SRamesh Errabolu static int kfd_dev_create_p2p_links(void)
14830f28cca8SRamesh Errabolu {
14840f28cca8SRamesh Errabolu struct kfd_topology_device *dev;
14850f28cca8SRamesh Errabolu struct kfd_topology_device *new_dev;
1486914da384SAlex Deucher #if defined(CONFIG_HSA_AMD_P2P)
1487914da384SAlex Deucher uint32_t i;
1488914da384SAlex Deucher #endif
1489914da384SAlex Deucher uint32_t k;
14900f28cca8SRamesh Errabolu int ret = 0;
14910f28cca8SRamesh Errabolu
14920f28cca8SRamesh Errabolu k = 0;
14930f28cca8SRamesh Errabolu list_for_each_entry(dev, &topology_device_list, list)
14940f28cca8SRamesh Errabolu k++;
14950f28cca8SRamesh Errabolu if (k < 2)
14960f28cca8SRamesh Errabolu return 0;
14970f28cca8SRamesh Errabolu
14980f28cca8SRamesh Errabolu new_dev = list_last_entry(&topology_device_list, struct kfd_topology_device, list);
14990f28cca8SRamesh Errabolu if (WARN_ON(!new_dev->gpu))
15000f28cca8SRamesh Errabolu return 0;
15010f28cca8SRamesh Errabolu
15020f28cca8SRamesh Errabolu k--;
15030f28cca8SRamesh Errabolu
15040f28cca8SRamesh Errabolu /* create in-direct links */
15050f28cca8SRamesh Errabolu ret = kfd_create_indirect_link_prop(new_dev, k);
15060f28cca8SRamesh Errabolu if (ret < 0)
15070f28cca8SRamesh Errabolu goto out;
15080f28cca8SRamesh Errabolu
15090f28cca8SRamesh Errabolu /* create p2p links */
15100f28cca8SRamesh Errabolu #if defined(CONFIG_HSA_AMD_P2P)
1511914da384SAlex Deucher i = 0;
15120f28cca8SRamesh Errabolu list_for_each_entry(dev, &topology_device_list, list) {
15130f28cca8SRamesh Errabolu if (dev == new_dev)
15140f28cca8SRamesh Errabolu break;
15150f28cca8SRamesh Errabolu if (!dev->gpu || !dev->gpu->adev ||
15168dc1db31SMukul Joshi (dev->gpu->kfd->hive_id &&
15178dc1db31SMukul Joshi dev->gpu->kfd->hive_id == new_dev->gpu->kfd->hive_id))
15180f28cca8SRamesh Errabolu goto next;
15190f28cca8SRamesh Errabolu
15200f28cca8SRamesh Errabolu /* check if node(s) is/are peer accessible in one direction or bi-direction */
15210f28cca8SRamesh Errabolu ret = kfd_add_peer_prop(new_dev, dev, i, k);
15220f28cca8SRamesh Errabolu if (ret < 0)
15230f28cca8SRamesh Errabolu goto out;
15240f28cca8SRamesh Errabolu
15250f28cca8SRamesh Errabolu ret = kfd_add_peer_prop(dev, new_dev, k, i);
15260f28cca8SRamesh Errabolu if (ret < 0)
15270f28cca8SRamesh Errabolu goto out;
15280f28cca8SRamesh Errabolu next:
15290f28cca8SRamesh Errabolu i++;
15300f28cca8SRamesh Errabolu }
15310f28cca8SRamesh Errabolu #endif
15320f28cca8SRamesh Errabolu
15330f28cca8SRamesh Errabolu out:
15340f28cca8SRamesh Errabolu return ret;
15353a87177eSHarish Kasiviswanathan }
15363a87177eSHarish Kasiviswanathan
1537c0cc999fSMa 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,int cu_bitmask,int cache_type,unsigned int cu_processor_id,int cu_block)1538c0cc999fSMa Jun static int fill_in_l1_pcache(struct kfd_cache_properties **props_ext,
1539c0cc999fSMa Jun struct kfd_gpu_cache_info *pcache_info,
1540c0cc999fSMa Jun int cu_bitmask,
1541c0cc999fSMa Jun int cache_type, unsigned int cu_processor_id,
1542c0cc999fSMa Jun int cu_block)
1543c0cc999fSMa Jun {
1544c0cc999fSMa Jun unsigned int cu_sibling_map_mask;
1545c0cc999fSMa Jun int first_active_cu;
1546c0cc999fSMa Jun struct kfd_cache_properties *pcache = NULL;
1547c0cc999fSMa Jun
1548c0cc999fSMa Jun cu_sibling_map_mask = cu_bitmask;
1549c0cc999fSMa Jun cu_sibling_map_mask >>= cu_block;
1550c0cc999fSMa Jun cu_sibling_map_mask &= ((1 << pcache_info[cache_type].num_cu_shared) - 1);
1551c0cc999fSMa Jun first_active_cu = ffs(cu_sibling_map_mask);
1552c0cc999fSMa Jun
1553c0cc999fSMa Jun /* CU could be inactive. In case of shared cache find the first active
1554c0cc999fSMa Jun * CU. and incase of non-shared cache check if the CU is inactive. If
1555c0cc999fSMa Jun * inactive active skip it
1556c0cc999fSMa Jun */
1557c0cc999fSMa Jun if (first_active_cu) {
1558c0cc999fSMa Jun pcache = kfd_alloc_struct(pcache);
1559c0cc999fSMa Jun if (!pcache)
1560c0cc999fSMa Jun return -ENOMEM;
1561c0cc999fSMa Jun
1562c0cc999fSMa Jun memset(pcache, 0, sizeof(struct kfd_cache_properties));
1563c0cc999fSMa Jun pcache->processor_id_low = cu_processor_id + (first_active_cu - 1);
1564c0cc999fSMa Jun pcache->cache_level = pcache_info[cache_type].cache_level;
1565c0cc999fSMa Jun pcache->cache_size = pcache_info[cache_type].cache_size;
1566c0cc999fSMa Jun
1567c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_DATA_CACHE)
1568c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_DATA;
1569c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_INST_CACHE)
1570c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
1571c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_CPU_CACHE)
1572c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_CPU;
1573c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
1574c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_HSACU;
1575c0cc999fSMa Jun
1576c0cc999fSMa Jun /* Sibling map is w.r.t processor_id_low, so shift out
1577c0cc999fSMa Jun * inactive CU
1578c0cc999fSMa Jun */
1579c0cc999fSMa Jun cu_sibling_map_mask =
1580c0cc999fSMa Jun cu_sibling_map_mask >> (first_active_cu - 1);
1581c0cc999fSMa Jun
1582c0cc999fSMa Jun pcache->sibling_map[0] = (uint8_t)(cu_sibling_map_mask & 0xFF);
1583c0cc999fSMa Jun pcache->sibling_map[1] =
1584c0cc999fSMa Jun (uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
1585c0cc999fSMa Jun pcache->sibling_map[2] =
1586c0cc999fSMa Jun (uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
1587c0cc999fSMa Jun pcache->sibling_map[3] =
1588c0cc999fSMa Jun (uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
1589c0cc999fSMa Jun
1590c0cc999fSMa Jun pcache->sibling_map_size = 4;
1591c0cc999fSMa Jun *props_ext = pcache;
1592c0cc999fSMa Jun
1593c0cc999fSMa Jun return 0;
1594c0cc999fSMa Jun }
1595c0cc999fSMa Jun return 1;
1596c0cc999fSMa Jun }
1597c0cc999fSMa Jun
1598c0cc999fSMa 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 amdgpu_cu_info * cu_info,struct amdgpu_gfx_config * gfx_info,int cache_type,unsigned int cu_processor_id,struct kfd_node * knode)1599c0cc999fSMa Jun static int fill_in_l2_l3_pcache(struct kfd_cache_properties **props_ext,
1600c0cc999fSMa Jun struct kfd_gpu_cache_info *pcache_info,
1601*9f7042ffSAlex Deucher struct amdgpu_cu_info *cu_info,
1602*9f7042ffSAlex Deucher struct amdgpu_gfx_config *gfx_info,
16030752e66eSMukul Joshi int cache_type, unsigned int cu_processor_id,
16040752e66eSMukul Joshi struct kfd_node *knode)
1605c0cc999fSMa Jun {
1606c0cc999fSMa Jun unsigned int cu_sibling_map_mask;
1607c0cc999fSMa Jun int first_active_cu;
16080752e66eSMukul Joshi int i, j, k, xcc, start, end;
1609c0cc999fSMa Jun struct kfd_cache_properties *pcache = NULL;
1610c0cc999fSMa Jun
16110752e66eSMukul Joshi start = ffs(knode->xcc_mask) - 1;
16120752e66eSMukul Joshi end = start + NUM_XCC(knode->xcc_mask);
1613*9f7042ffSAlex Deucher cu_sibling_map_mask = cu_info->bitmap[start][0][0];
1614c0cc999fSMa Jun cu_sibling_map_mask &=
1615c0cc999fSMa Jun ((1 << pcache_info[cache_type].num_cu_shared) - 1);
1616c0cc999fSMa Jun first_active_cu = ffs(cu_sibling_map_mask);
1617c0cc999fSMa Jun
1618c0cc999fSMa Jun /* CU could be inactive. In case of shared cache find the first active
1619c0cc999fSMa Jun * CU. and incase of non-shared cache check if the CU is inactive. If
1620c0cc999fSMa Jun * inactive active skip it
1621c0cc999fSMa Jun */
1622c0cc999fSMa Jun if (first_active_cu) {
1623c0cc999fSMa Jun pcache = kfd_alloc_struct(pcache);
1624c0cc999fSMa Jun if (!pcache)
1625c0cc999fSMa Jun return -ENOMEM;
1626c0cc999fSMa Jun
1627c0cc999fSMa Jun memset(pcache, 0, sizeof(struct kfd_cache_properties));
1628c0cc999fSMa Jun pcache->processor_id_low = cu_processor_id
1629c0cc999fSMa Jun + (first_active_cu - 1);
1630c0cc999fSMa Jun pcache->cache_level = pcache_info[cache_type].cache_level;
1631c0cc999fSMa Jun pcache->cache_size = pcache_info[cache_type].cache_size;
1632c0cc999fSMa Jun
1633c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_DATA_CACHE)
1634c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_DATA;
1635c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_INST_CACHE)
1636c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
1637c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_CPU_CACHE)
1638c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_CPU;
1639c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
1640c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_HSACU;
1641c0cc999fSMa Jun
1642c0cc999fSMa Jun /* Sibling map is w.r.t processor_id_low, so shift out
1643c0cc999fSMa Jun * inactive CU
1644c0cc999fSMa Jun */
1645c0cc999fSMa Jun cu_sibling_map_mask = cu_sibling_map_mask >> (first_active_cu - 1);
1646c0cc999fSMa Jun k = 0;
1647c0cc999fSMa Jun
16480752e66eSMukul Joshi for (xcc = start; xcc < end; xcc++) {
1649*9f7042ffSAlex Deucher for (i = 0; i < gfx_info->max_shader_engines; i++) {
1650*9f7042ffSAlex Deucher for (j = 0; j < gfx_info->max_sh_per_se; j++) {
1651c0cc999fSMa Jun pcache->sibling_map[k] = (uint8_t)(cu_sibling_map_mask & 0xFF);
1652c0cc999fSMa Jun pcache->sibling_map[k+1] = (uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
1653c0cc999fSMa Jun pcache->sibling_map[k+2] = (uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
1654c0cc999fSMa Jun pcache->sibling_map[k+3] = (uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
1655c0cc999fSMa Jun k += 4;
1656c0cc999fSMa Jun
1657*9f7042ffSAlex Deucher cu_sibling_map_mask = cu_info->bitmap[xcc][i % 4][j + i / 4];
1658c0cc999fSMa Jun cu_sibling_map_mask &= ((1 << pcache_info[cache_type].num_cu_shared) - 1);
1659c0cc999fSMa Jun }
1660c0cc999fSMa Jun }
16610752e66eSMukul Joshi }
1662c0cc999fSMa Jun pcache->sibling_map_size = k;
1663c0cc999fSMa Jun *props_ext = pcache;
1664c0cc999fSMa Jun return 0;
1665c0cc999fSMa Jun }
1666c0cc999fSMa Jun return 1;
1667c0cc999fSMa Jun }
1668c0cc999fSMa Jun
1669c0cc999fSMa Jun #define KFD_MAX_CACHE_TYPES 6
1670c0cc999fSMa Jun
1671c0cc999fSMa Jun /* kfd_fill_cache_non_crat_info - Fill GPU cache info using kfd_gpu_cache_info
1672c0cc999fSMa Jun * tables
1673c0cc999fSMa Jun */
kfd_fill_cache_non_crat_info(struct kfd_topology_device * dev,struct kfd_node * kdev)16748dc1db31SMukul Joshi static void kfd_fill_cache_non_crat_info(struct kfd_topology_device *dev, struct kfd_node *kdev)
1675c0cc999fSMa Jun {
1676c0cc999fSMa Jun struct kfd_gpu_cache_info *pcache_info = NULL;
16770752e66eSMukul Joshi int i, j, k, xcc, start, end;
1678c0cc999fSMa Jun int ct = 0;
1679c0cc999fSMa Jun unsigned int cu_processor_id;
1680c0cc999fSMa Jun int ret;
1681c0cc999fSMa Jun unsigned int num_cu_shared;
1682*9f7042ffSAlex Deucher struct amdgpu_cu_info *cu_info = &kdev->adev->gfx.cu_info;
1683*9f7042ffSAlex Deucher struct amdgpu_gfx_config *gfx_info = &kdev->adev->gfx.config;
1684c0cc999fSMa Jun int gpu_processor_id;
1685c0cc999fSMa Jun struct kfd_cache_properties *props_ext;
1686c0cc999fSMa Jun int num_of_entries = 0;
1687c0cc999fSMa Jun int num_of_cache_types = 0;
1688c0cc999fSMa Jun struct kfd_gpu_cache_info cache_info[KFD_MAX_CACHE_TYPES];
1689c0cc999fSMa Jun
1690c0cc999fSMa Jun
1691c0cc999fSMa Jun gpu_processor_id = dev->node_props.simd_id_base;
1692c0cc999fSMa Jun
1693c0cc999fSMa Jun pcache_info = cache_info;
1694c0cc999fSMa Jun num_of_cache_types = kfd_get_gpu_cache_info(kdev, &pcache_info);
1695c0cc999fSMa Jun if (!num_of_cache_types) {
1696c0cc999fSMa Jun pr_warn("no cache info found\n");
1697c0cc999fSMa Jun return;
1698c0cc999fSMa Jun }
1699c0cc999fSMa Jun
1700c0cc999fSMa Jun /* For each type of cache listed in the kfd_gpu_cache_info table,
1701c0cc999fSMa Jun * go through all available Compute Units.
1702c0cc999fSMa Jun * The [i,j,k] loop will
1703c0cc999fSMa Jun * if kfd_gpu_cache_info.num_cu_shared = 1
1704c0cc999fSMa Jun * will parse through all available CU
1705c0cc999fSMa Jun * If (kfd_gpu_cache_info.num_cu_shared != 1)
1706c0cc999fSMa Jun * then it will consider only one CU from
1707c0cc999fSMa Jun * the shared unit
1708c0cc999fSMa Jun */
17090752e66eSMukul Joshi start = ffs(kdev->xcc_mask) - 1;
17100752e66eSMukul Joshi end = start + NUM_XCC(kdev->xcc_mask);
17110752e66eSMukul Joshi
1712c0cc999fSMa Jun for (ct = 0; ct < num_of_cache_types; ct++) {
1713c0cc999fSMa Jun cu_processor_id = gpu_processor_id;
1714c0cc999fSMa Jun if (pcache_info[ct].cache_level == 1) {
17150752e66eSMukul Joshi for (xcc = start; xcc < end; xcc++) {
1716*9f7042ffSAlex Deucher for (i = 0; i < gfx_info->max_shader_engines; i++) {
1717*9f7042ffSAlex Deucher for (j = 0; j < gfx_info->max_sh_per_se; j++) {
1718*9f7042ffSAlex Deucher for (k = 0; k < gfx_info->max_cu_per_sh; k += pcache_info[ct].num_cu_shared) {
1719c0cc999fSMa Jun
1720*9f7042ffSAlex Deucher ret = fill_in_l1_pcache(&props_ext, pcache_info,
1721*9f7042ffSAlex Deucher cu_info->bitmap[xcc][i % 4][j + i / 4], ct,
1722c0cc999fSMa Jun cu_processor_id, k);
1723c0cc999fSMa Jun
1724c0cc999fSMa Jun if (ret < 0)
1725c0cc999fSMa Jun break;
1726c0cc999fSMa Jun
1727c0cc999fSMa Jun if (!ret) {
1728c0cc999fSMa Jun num_of_entries++;
1729c0cc999fSMa Jun list_add_tail(&props_ext->list, &dev->cache_props);
1730c0cc999fSMa Jun }
1731c0cc999fSMa Jun
1732c0cc999fSMa Jun /* Move to next CU block */
1733c0cc999fSMa Jun num_cu_shared = ((k + pcache_info[ct].num_cu_shared) <=
1734*9f7042ffSAlex Deucher gfx_info->max_cu_per_sh) ?
1735c0cc999fSMa Jun pcache_info[ct].num_cu_shared :
1736*9f7042ffSAlex Deucher (gfx_info->max_cu_per_sh - k);
1737c0cc999fSMa Jun cu_processor_id += num_cu_shared;
1738c0cc999fSMa Jun }
1739c0cc999fSMa Jun }
1740c0cc999fSMa Jun }
17410752e66eSMukul Joshi }
1742c0cc999fSMa Jun } else {
1743c0cc999fSMa Jun ret = fill_in_l2_l3_pcache(&props_ext, pcache_info,
1744*9f7042ffSAlex Deucher cu_info, gfx_info, ct, cu_processor_id, kdev);
1745c0cc999fSMa Jun
1746c0cc999fSMa Jun if (ret < 0)
1747c0cc999fSMa Jun break;
1748c0cc999fSMa Jun
1749c0cc999fSMa Jun if (!ret) {
1750c0cc999fSMa Jun num_of_entries++;
1751c0cc999fSMa Jun list_add_tail(&props_ext->list, &dev->cache_props);
1752c0cc999fSMa Jun }
1753c0cc999fSMa Jun }
1754c0cc999fSMa Jun }
1755c0cc999fSMa Jun dev->node_props.caches_count += num_of_entries;
1756c0cc999fSMa Jun pr_debug("Added [%d] GPU cache entries\n", num_of_entries);
1757c0cc999fSMa Jun }
1758c0cc999fSMa Jun
kfd_topology_add_device_locked(struct kfd_node * gpu,uint32_t gpu_id,struct kfd_topology_device ** dev)17598dc1db31SMukul Joshi static int kfd_topology_add_device_locked(struct kfd_node *gpu, uint32_t gpu_id,
1760f701acb6SFelix Kuehling struct kfd_topology_device **dev)
17615b5c4e40SEvgeny Pinchuk {
1762f701acb6SFelix Kuehling int proximity_domain = ++topology_crat_proximity_domain;
17634f449311SHarish Kasiviswanathan struct list_head temp_topology_device_list;
17643a87177eSHarish Kasiviswanathan void *crat_image = NULL;
17653a87177eSHarish Kasiviswanathan size_t image_size = 0;
1766f701acb6SFelix Kuehling int res;
176746d18d51SMukul Joshi
17683a87177eSHarish Kasiviswanathan res = kfd_create_crat_image_virtual(&crat_image, &image_size,
17693a87177eSHarish Kasiviswanathan COMPUTE_UNIT_GPU, gpu,
17703a87177eSHarish Kasiviswanathan proximity_domain);
17713a87177eSHarish Kasiviswanathan if (res) {
17723a87177eSHarish Kasiviswanathan pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
17733a87177eSHarish Kasiviswanathan gpu_id);
177446d18d51SMukul Joshi topology_crat_proximity_domain--;
1775f701acb6SFelix Kuehling goto err;
17763a87177eSHarish Kasiviswanathan }
1777c0cc999fSMa Jun
1778f701acb6SFelix Kuehling INIT_LIST_HEAD(&temp_topology_device_list);
1779f701acb6SFelix Kuehling
17803a87177eSHarish Kasiviswanathan res = kfd_parse_crat_table(crat_image,
17813a87177eSHarish Kasiviswanathan &temp_topology_device_list,
17823a87177eSHarish Kasiviswanathan proximity_domain);
17833a87177eSHarish Kasiviswanathan if (res) {
17843a87177eSHarish Kasiviswanathan pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
17853a87177eSHarish Kasiviswanathan gpu_id);
178646d18d51SMukul Joshi topology_crat_proximity_domain--;
17875b5c4e40SEvgeny Pinchuk goto err;
17885b5c4e40SEvgeny Pinchuk }
17894f449311SHarish Kasiviswanathan
17904f449311SHarish Kasiviswanathan kfd_topology_update_device_list(&temp_topology_device_list,
17914f449311SHarish Kasiviswanathan &topology_device_list);
17924f449311SHarish Kasiviswanathan
1793f701acb6SFelix Kuehling *dev = kfd_assign_gpu(gpu);
1794f701acb6SFelix Kuehling if (WARN_ON(!*dev)) {
17953a87177eSHarish Kasiviswanathan res = -ENODEV;
17963a87177eSHarish Kasiviswanathan goto err;
17973a87177eSHarish Kasiviswanathan }
1798c0cc999fSMa Jun
1799c0cc999fSMa Jun /* Fill the cache affinity information here for the GPUs
1800c0cc999fSMa Jun * using VCRAT
1801c0cc999fSMa Jun */
1802f701acb6SFelix Kuehling kfd_fill_cache_non_crat_info(*dev, gpu);
1803c0cc999fSMa Jun
1804c0cc999fSMa Jun /* Update the SYSFS tree, since we added another topology
1805c0cc999fSMa Jun * device
1806c0cc999fSMa Jun */
1807c0cc999fSMa Jun res = kfd_topology_update_sysfs();
1808c0cc999fSMa Jun if (!res)
1809c0cc999fSMa Jun sys_props.generation_count++;
1810c0cc999fSMa Jun else
1811c0cc999fSMa Jun pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
1812c0cc999fSMa Jun gpu_id, res);
1813f701acb6SFelix Kuehling
1814f701acb6SFelix Kuehling err:
1815f701acb6SFelix Kuehling kfd_destroy_crat_image(crat_image);
1816f701acb6SFelix Kuehling return res;
18175b5c4e40SEvgeny Pinchuk }
1818f701acb6SFelix Kuehling
kfd_topology_set_dbg_firmware_support(struct kfd_topology_device * dev)1819d230f1bfSJonathan Kim static void kfd_topology_set_dbg_firmware_support(struct kfd_topology_device *dev)
1820d230f1bfSJonathan Kim {
1821d230f1bfSJonathan Kim bool firmware_supported = true;
1822d230f1bfSJonathan Kim
1823d230f1bfSJonathan Kim if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(11, 0, 0) &&
1824d230f1bfSJonathan Kim KFD_GC_VERSION(dev->gpu) < IP_VERSION(12, 0, 0)) {
182509d49e14SJonathan Kim uint32_t mes_api_rev = (dev->gpu->adev->mes.sched_version &
182609d49e14SJonathan Kim AMDGPU_MES_API_VERSION_MASK) >>
182709d49e14SJonathan Kim AMDGPU_MES_API_VERSION_SHIFT;
182809d49e14SJonathan Kim uint32_t mes_rev = dev->gpu->adev->mes.sched_version &
182909d49e14SJonathan Kim AMDGPU_MES_VERSION_MASK;
183009d49e14SJonathan Kim
183109d49e14SJonathan Kim firmware_supported = (mes_api_rev >= 14) && (mes_rev >= 64);
1832d230f1bfSJonathan Kim goto out;
1833d230f1bfSJonathan Kim }
1834d230f1bfSJonathan Kim
1835d230f1bfSJonathan Kim /*
1836d230f1bfSJonathan Kim * Note: Any unlisted devices here are assumed to support exception handling.
1837d230f1bfSJonathan Kim * Add additional checks here as needed.
1838d230f1bfSJonathan Kim */
1839d230f1bfSJonathan Kim switch (KFD_GC_VERSION(dev->gpu)) {
1840d230f1bfSJonathan Kim case IP_VERSION(9, 0, 1):
1841d230f1bfSJonathan Kim firmware_supported = dev->gpu->kfd->mec_fw_version >= 459 + 32768;
1842d230f1bfSJonathan Kim break;
1843d230f1bfSJonathan Kim case IP_VERSION(9, 1, 0):
1844d230f1bfSJonathan Kim case IP_VERSION(9, 2, 1):
1845d230f1bfSJonathan Kim case IP_VERSION(9, 2, 2):
1846d230f1bfSJonathan Kim case IP_VERSION(9, 3, 0):
1847d230f1bfSJonathan Kim case IP_VERSION(9, 4, 0):
1848d230f1bfSJonathan Kim firmware_supported = dev->gpu->kfd->mec_fw_version >= 459;
1849d230f1bfSJonathan Kim break;
1850d230f1bfSJonathan Kim case IP_VERSION(9, 4, 1):
1851d230f1bfSJonathan Kim firmware_supported = dev->gpu->kfd->mec_fw_version >= 60;
1852d230f1bfSJonathan Kim break;
1853d230f1bfSJonathan Kim case IP_VERSION(9, 4, 2):
1854d230f1bfSJonathan Kim firmware_supported = dev->gpu->kfd->mec_fw_version >= 51;
1855d230f1bfSJonathan Kim break;
1856d230f1bfSJonathan Kim case IP_VERSION(10, 1, 10):
1857d230f1bfSJonathan Kim case IP_VERSION(10, 1, 2):
1858d230f1bfSJonathan Kim case IP_VERSION(10, 1, 1):
1859d230f1bfSJonathan Kim firmware_supported = dev->gpu->kfd->mec_fw_version >= 144;
1860d230f1bfSJonathan Kim break;
1861d230f1bfSJonathan Kim case IP_VERSION(10, 3, 0):
1862d230f1bfSJonathan Kim case IP_VERSION(10, 3, 2):
1863d230f1bfSJonathan Kim case IP_VERSION(10, 3, 1):
1864d230f1bfSJonathan Kim case IP_VERSION(10, 3, 4):
1865d230f1bfSJonathan Kim case IP_VERSION(10, 3, 5):
1866d230f1bfSJonathan Kim firmware_supported = dev->gpu->kfd->mec_fw_version >= 89;
1867d230f1bfSJonathan Kim break;
1868d230f1bfSJonathan Kim case IP_VERSION(10, 1, 3):
1869d230f1bfSJonathan Kim case IP_VERSION(10, 3, 3):
1870d230f1bfSJonathan Kim firmware_supported = false;
1871d230f1bfSJonathan Kim break;
1872d230f1bfSJonathan Kim default:
1873d230f1bfSJonathan Kim break;
1874d230f1bfSJonathan Kim }
1875d230f1bfSJonathan Kim
1876d230f1bfSJonathan Kim out:
1877d230f1bfSJonathan Kim if (firmware_supported)
1878d230f1bfSJonathan Kim dev->node_props.capability |= HSA_CAP_TRAP_DEBUG_FIRMWARE_SUPPORTED;
1879d230f1bfSJonathan Kim }
1880d230f1bfSJonathan Kim
kfd_topology_set_capabilities(struct kfd_topology_device * dev)1881d230f1bfSJonathan Kim static void kfd_topology_set_capabilities(struct kfd_topology_device *dev)
1882d230f1bfSJonathan Kim {
1883d230f1bfSJonathan Kim dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1884d230f1bfSJonathan Kim HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1885d230f1bfSJonathan Kim HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1886d230f1bfSJonathan Kim
1887d230f1bfSJonathan Kim dev->node_props.capability |= HSA_CAP_TRAP_DEBUG_SUPPORT |
1888d230f1bfSJonathan Kim HSA_CAP_TRAP_DEBUG_WAVE_LAUNCH_TRAP_OVERRIDE_SUPPORTED |
1889d230f1bfSJonathan Kim HSA_CAP_TRAP_DEBUG_WAVE_LAUNCH_MODE_SUPPORTED;
1890d230f1bfSJonathan Kim
1891fc7f1d96SJonathan Kim if (kfd_dbg_has_ttmps_always_setup(dev->gpu))
1892fc7f1d96SJonathan Kim dev->node_props.debug_prop |= HSA_DBG_DISPATCH_INFO_ALWAYS_VALID;
1893fc7f1d96SJonathan Kim
1894d230f1bfSJonathan Kim if (KFD_GC_VERSION(dev->gpu) < IP_VERSION(10, 0, 0)) {
1895567db9e0SJonathan Kim if (KFD_GC_VERSION(dev->gpu) == IP_VERSION(9, 4, 3))
1896567db9e0SJonathan Kim dev->node_props.debug_prop |=
1897567db9e0SJonathan Kim HSA_DBG_WATCH_ADDR_MASK_LO_BIT_GFX9_4_3 |
1898567db9e0SJonathan Kim HSA_DBG_WATCH_ADDR_MASK_HI_BIT_GFX9_4_3;
1899567db9e0SJonathan Kim else
1900567db9e0SJonathan Kim dev->node_props.debug_prop |=
1901567db9e0SJonathan Kim HSA_DBG_WATCH_ADDR_MASK_LO_BIT_GFX9 |
1902d230f1bfSJonathan Kim HSA_DBG_WATCH_ADDR_MASK_HI_BIT;
1903d230f1bfSJonathan Kim
19048e436326SJonathan Kim if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(9, 4, 2))
1905d230f1bfSJonathan Kim dev->node_props.capability |=
1906d230f1bfSJonathan Kim HSA_CAP_TRAP_DEBUG_PRECISE_MEMORY_OPERATIONS_SUPPORTED;
1907d230f1bfSJonathan Kim } else {
1908d230f1bfSJonathan Kim dev->node_props.debug_prop |= HSA_DBG_WATCH_ADDR_MASK_LO_BIT_GFX10 |
1909d230f1bfSJonathan Kim HSA_DBG_WATCH_ADDR_MASK_HI_BIT;
1910d230f1bfSJonathan Kim
1911fc7f1d96SJonathan Kim if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(11, 0, 0))
1912d230f1bfSJonathan Kim dev->node_props.capability |=
1913d230f1bfSJonathan Kim HSA_CAP_TRAP_DEBUG_PRECISE_MEMORY_OPERATIONS_SUPPORTED;
1914d230f1bfSJonathan Kim }
1915d230f1bfSJonathan Kim
1916d230f1bfSJonathan Kim kfd_topology_set_dbg_firmware_support(dev);
1917d230f1bfSJonathan Kim }
1918d230f1bfSJonathan Kim
kfd_topology_add_device(struct kfd_node * gpu)19198dc1db31SMukul Joshi int kfd_topology_add_device(struct kfd_node *gpu)
1920f701acb6SFelix Kuehling {
1921f701acb6SFelix Kuehling uint32_t gpu_id;
1922f701acb6SFelix Kuehling struct kfd_topology_device *dev;
1923f701acb6SFelix Kuehling int res = 0;
1924f701acb6SFelix Kuehling int i;
1925f701acb6SFelix Kuehling const char *asic_name = amdgpu_asic_name[gpu->adev->asic_type];
1926*9f7042ffSAlex Deucher struct amdgpu_gfx_config *gfx_info = &gpu->adev->gfx.config;
1927*9f7042ffSAlex Deucher struct amdgpu_cu_info *cu_info = &gpu->adev->gfx.cu_info;
1928f701acb6SFelix Kuehling
1929f701acb6SFelix Kuehling gpu_id = kfd_generate_gpu_id(gpu);
1930400a39f1SJames Zhu if (gpu->xcp && !gpu->xcp->ddev) {
1931400a39f1SJames Zhu dev_warn(gpu->adev->dev,
1932400a39f1SJames Zhu "Won't add GPU (ID: 0x%x) to topology since it has no drm node assigned.",
1933400a39f1SJames Zhu gpu_id);
1934400a39f1SJames Zhu return 0;
1935400a39f1SJames Zhu } else {
1936f701acb6SFelix Kuehling pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1937400a39f1SJames Zhu }
1938f701acb6SFelix Kuehling
1939f701acb6SFelix Kuehling /* Check to see if this gpu device exists in the topology_device_list.
1940f701acb6SFelix Kuehling * If so, assign the gpu to that device,
1941f701acb6SFelix Kuehling * else create a Virtual CRAT for this gpu device and then parse that
1942f701acb6SFelix Kuehling * CRAT to create a new topology device. Once created assign the gpu to
1943f701acb6SFelix Kuehling * that topology device
1944f701acb6SFelix Kuehling */
1945f701acb6SFelix Kuehling down_write(&topology_lock);
1946f701acb6SFelix Kuehling dev = kfd_assign_gpu(gpu);
1947f701acb6SFelix Kuehling if (!dev)
1948f701acb6SFelix Kuehling res = kfd_topology_add_device_locked(gpu, gpu_id, &dev);
1949c0cc999fSMa Jun up_write(&topology_lock);
1950f701acb6SFelix Kuehling if (res)
1951f701acb6SFelix Kuehling return res;
19525b5c4e40SEvgeny Pinchuk
19535b5c4e40SEvgeny Pinchuk dev->gpu_id = gpu_id;
19545b5c4e40SEvgeny Pinchuk gpu->id = gpu_id;
19553a87177eSHarish Kasiviswanathan
19560f28cca8SRamesh Errabolu kfd_dev_create_p2p_links();
19570f28cca8SRamesh Errabolu
19583a87177eSHarish Kasiviswanathan /* TODO: Move the following lines to function
19593a87177eSHarish Kasiviswanathan * kfd_add_non_crat_information
19603a87177eSHarish Kasiviswanathan */
19613a87177eSHarish Kasiviswanathan
19623a87177eSHarish Kasiviswanathan /* Fill-in additional information that is not available in CRAT but
19633a87177eSHarish Kasiviswanathan * needed for the topology
19643a87177eSHarish Kasiviswanathan */
1965b7675b7bSGraham Sider for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1; i++) {
1966b7675b7bSGraham Sider dev->node_props.name[i] = __tolower(asic_name[i]);
1967b7675b7bSGraham Sider if (asic_name[i] == '\0')
1968b7675b7bSGraham Sider break;
1969b7675b7bSGraham Sider }
1970b7675b7bSGraham Sider dev->node_props.name[i] = '\0';
1971c181159aSYong Zhao
19723a87177eSHarish Kasiviswanathan dev->node_props.simd_arrays_per_engine =
1973*9f7042ffSAlex Deucher gfx_info->max_sh_per_se;
19743a87177eSHarish Kasiviswanathan
19758dc1db31SMukul Joshi dev->node_props.gfx_target_version =
19768dc1db31SMukul Joshi gpu->kfd->device_info.gfx_target_version;
1977d69a3b76SMukul Joshi dev->node_props.vendor_id = gpu->adev->pdev->vendor;
1978d69a3b76SMukul Joshi dev->node_props.device_id = gpu->adev->pdev->device;
1979c6d1ec41SJoseph Greathouse dev->node_props.capability |=
198002274fc0SGraham Sider ((dev->gpu->adev->rev_id << HSA_CAP_ASIC_REVISION_SHIFT) &
1981c6d1ec41SJoseph Greathouse HSA_CAP_ASIC_REVISION_MASK);
198292085240SJonathan Kim
1983d69a3b76SMukul Joshi dev->node_props.location_id = pci_dev_id(gpu->adev->pdev);
198492085240SJonathan Kim if (KFD_GC_VERSION(dev->gpu->kfd) == IP_VERSION(9, 4, 3))
198592085240SJonathan Kim dev->node_props.location_id |= dev->gpu->node_id;
198692085240SJonathan Kim
1987d69a3b76SMukul Joshi dev->node_props.domain = pci_domain_nr(gpu->adev->pdev->bus);
19883a87177eSHarish Kasiviswanathan dev->node_props.max_engine_clk_fcompute =
1989574c4183SGraham Sider amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->adev);
19903a87177eSHarish Kasiviswanathan dev->node_props.max_engine_clk_ccompute =
19913a87177eSHarish Kasiviswanathan cpufreq_quick_get_max(0) / 1000;
1992a476c0c6SPhilip Yang
1993a476c0c6SPhilip Yang if (gpu->xcp)
1994a476c0c6SPhilip Yang dev->node_props.drm_render_minor = gpu->xcp->ddev->render->index;
1995a476c0c6SPhilip Yang else
19967c9b7171SOak Zeng dev->node_props.drm_render_minor =
19978dc1db31SMukul Joshi gpu->kfd->shared_resources.drm_render_minor;
19985b5c4e40SEvgeny Pinchuk
19998dc1db31SMukul Joshi dev->node_props.hive_id = gpu->kfd->hive_id;
2000ee2f17f4SAmber Lin dev->node_props.num_sdma_engines = kfd_get_num_sdma_engines(gpu);
200114568cf6SOak Zeng dev->node_props.num_sdma_xgmi_engines =
2002ee2f17f4SAmber Lin kfd_get_num_xgmi_sdma_engines(gpu);
2003bb71c74dSHuang Rui dev->node_props.num_sdma_queues_per_engine =
20048dc1db31SMukul Joshi gpu->kfd->device_info.num_sdma_queues_per_engine -
20058dc1db31SMukul Joshi gpu->kfd->device_info.num_reserved_sdma_queues_per_engine;
200629633d0eSJoseph Greathouse dev->node_props.num_gws = (dev->gpu->gws &&
200729e76462SOak Zeng dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
200802274fc0SGraham Sider dev->gpu->adev->gds.gws_size : 0;
2009e6945304SYong Zhao dev->node_props.num_cp_queues = get_cp_queues_num(dev->gpu->dqm);
20100c1690e3SShaoyun Liu
20113a87177eSHarish Kasiviswanathan kfd_fill_mem_clk_max_info(dev);
20123a87177eSHarish Kasiviswanathan kfd_fill_iolink_non_crat_info(dev);
20133a87177eSHarish Kasiviswanathan
20147eb0502aSGraham Sider switch (dev->gpu->adev->asic_type) {
20153a87177eSHarish Kasiviswanathan case CHIP_KAVERI:
20163a87177eSHarish Kasiviswanathan case CHIP_HAWAII:
20173a87177eSHarish Kasiviswanathan case CHIP_TONGA:
20183a87177eSHarish Kasiviswanathan dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
20193a87177eSHarish Kasiviswanathan HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
20203a87177eSHarish Kasiviswanathan HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
20213a87177eSHarish Kasiviswanathan break;
20223a87177eSHarish Kasiviswanathan case CHIP_CARRIZO:
20233a87177eSHarish Kasiviswanathan case CHIP_FIJI:
20243a87177eSHarish Kasiviswanathan case CHIP_POLARIS10:
20253a87177eSHarish Kasiviswanathan case CHIP_POLARIS11:
2026846a44d7SGang Ba case CHIP_POLARIS12:
2027ed81cd6eSKent Russell case CHIP_VEGAM:
202842aa8793SFelix Kuehling pr_debug("Adding doorbell packet type capability\n");
20293a87177eSHarish Kasiviswanathan dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
20303a87177eSHarish Kasiviswanathan HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
20313a87177eSHarish Kasiviswanathan HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
20323a87177eSHarish Kasiviswanathan break;
2033e4804a39SGraham Sider default:
2034d230f1bfSJonathan Kim if (KFD_GC_VERSION(dev->gpu) < IP_VERSION(9, 0, 1))
20353a87177eSHarish Kasiviswanathan WARN(1, "Unexpected ASIC family %u",
20367eb0502aSGraham Sider dev->gpu->adev->asic_type);
2037d230f1bfSJonathan Kim else
2038d230f1bfSJonathan Kim kfd_topology_set_capabilities(dev);
20397639a8c4SBen Goz }
20407639a8c4SBen Goz
20411ae99eabSOak Zeng /*
20421ae99eabSOak Zeng * Overwrite ATS capability according to needs_iommu_device to fix
20431ae99eabSOak Zeng * potential missing corresponding bit in CRAT of BIOS.
20441ae99eabSOak Zeng */
20451ae99eabSOak Zeng dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;
20461ae99eabSOak Zeng
20473a87177eSHarish Kasiviswanathan /* Fix errors in CZ CRAT.
20483a87177eSHarish Kasiviswanathan * simd_count: Carrizo CRAT reports wrong simd_count, probably
20493a87177eSHarish Kasiviswanathan * because it doesn't consider masked out CUs
205070f372bfSPhilip Cox * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
20513a87177eSHarish Kasiviswanathan */
20527eb0502aSGraham Sider if (dev->gpu->adev->asic_type == CHIP_CARRIZO) {
20533a87177eSHarish Kasiviswanathan dev->node_props.simd_count =
2054*9f7042ffSAlex Deucher cu_info->simd_per_cu * cu_info->number;
205570f372bfSPhilip Cox dev->node_props.max_waves_per_simd = 10;
205670f372bfSPhilip Cox }
20573a87177eSHarish Kasiviswanathan
20585436ab94SStanley.Yang /* kfd only concerns sram ecc on GFX and HBM ecc on UMC */
20590dee45a2SEric Huang dev->node_props.capability |=
206056c5977eSGraham Sider ((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0) ?
20610dee45a2SEric Huang HSA_CAP_SRAM_EDCSUPPORTED : 0;
206256c5977eSGraham Sider dev->node_props.capability |=
206356c5977eSGraham Sider ((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
20640dee45a2SEric Huang HSA_CAP_MEM_EDCSUPPORTED : 0;
20650dee45a2SEric Huang
2066046e674bSGraham Sider if (KFD_GC_VERSION(dev->gpu) != IP_VERSION(9, 0, 1))
206756c5977eSGraham Sider dev->node_props.capability |= (dev->gpu->adev->ras_enabled != 0) ?
20680dee45a2SEric Huang HSA_CAP_RASEVENTNOTIFY : 0;
20690dee45a2SEric Huang
2070610dab11SPhilip Yang if (KFD_IS_SVM_API_SUPPORTED(dev->gpu->adev))
20714c166eb9SPhilip Yang dev->node_props.capability |= HSA_CAP_SVMAPI_SUPPORTED;
20724c166eb9SPhilip Yang
207303d400e7SAlex Sierra if (dev->gpu->adev->gmc.is_app_apu ||
207403d400e7SAlex Sierra dev->gpu->adev->gmc.xgmi.connected_to_cpu)
207503d400e7SAlex Sierra dev->node_props.capability |= HSA_CAP_FLAGS_COHERENTHOSTACCESS;
207603d400e7SAlex Sierra
20773a87177eSHarish Kasiviswanathan kfd_debug_print_topology();
20783a87177eSHarish Kasiviswanathan
20795b5c4e40SEvgeny Pinchuk kfd_notify_gpu_change(gpu_id, 1);
2080f701acb6SFelix Kuehling
20817d4f8db4SDan Carpenter return 0;
20825b5c4e40SEvgeny Pinchuk }
20835b5c4e40SEvgeny Pinchuk
208446d18d51SMukul Joshi /**
208546d18d51SMukul Joshi * kfd_topology_update_io_links() - Update IO links after device removal.
208646d18d51SMukul Joshi * @proximity_domain: Proximity domain value of the dev being removed.
208746d18d51SMukul Joshi *
208846d18d51SMukul Joshi * The topology list currently is arranged in increasing order of
208946d18d51SMukul Joshi * proximity domain.
209046d18d51SMukul Joshi *
209146d18d51SMukul Joshi * Two things need to be done when a device is removed:
209246d18d51SMukul Joshi * 1. All the IO links to this device need to be removed.
209346d18d51SMukul Joshi * 2. All nodes after the current device node need to move
209446d18d51SMukul Joshi * up once this device node is removed from the topology
209546d18d51SMukul Joshi * list. As a result, the proximity domain values for
209646d18d51SMukul Joshi * all nodes after the node being deleted reduce by 1.
209746d18d51SMukul Joshi * This would also cause the proximity domain values for
209846d18d51SMukul Joshi * io links to be updated based on new proximity domain
209946d18d51SMukul Joshi * values.
210046d18d51SMukul Joshi *
210146d18d51SMukul Joshi * Context: The caller must hold write topology_lock.
210246d18d51SMukul Joshi */
kfd_topology_update_io_links(int proximity_domain)210346d18d51SMukul Joshi static void kfd_topology_update_io_links(int proximity_domain)
210446d18d51SMukul Joshi {
210546d18d51SMukul Joshi struct kfd_topology_device *dev;
21060f28cca8SRamesh Errabolu struct kfd_iolink_properties *iolink, *p2plink, *tmp;
210746d18d51SMukul Joshi
210846d18d51SMukul Joshi list_for_each_entry(dev, &topology_device_list, list) {
210946d18d51SMukul Joshi if (dev->proximity_domain > proximity_domain)
211046d18d51SMukul Joshi dev->proximity_domain--;
211146d18d51SMukul Joshi
211246d18d51SMukul Joshi list_for_each_entry_safe(iolink, tmp, &dev->io_link_props, list) {
211346d18d51SMukul Joshi /*
211446d18d51SMukul Joshi * If there is an io link to the dev being deleted
211546d18d51SMukul Joshi * then remove that IO link also.
211646d18d51SMukul Joshi */
211746d18d51SMukul Joshi if (iolink->node_to == proximity_domain) {
211846d18d51SMukul Joshi list_del(&iolink->list);
211946d18d51SMukul Joshi dev->node_props.io_links_count--;
212098447635SMukul Joshi } else {
212198447635SMukul Joshi if (iolink->node_from > proximity_domain)
212246d18d51SMukul Joshi iolink->node_from--;
212398447635SMukul Joshi if (iolink->node_to > proximity_domain)
212446d18d51SMukul Joshi iolink->node_to--;
212546d18d51SMukul Joshi }
212646d18d51SMukul Joshi }
21270f28cca8SRamesh Errabolu
21280f28cca8SRamesh Errabolu list_for_each_entry_safe(p2plink, tmp, &dev->p2p_link_props, list) {
21290f28cca8SRamesh Errabolu /*
21300f28cca8SRamesh Errabolu * If there is a p2p link to the dev being deleted
21310f28cca8SRamesh Errabolu * then remove that p2p link also.
21320f28cca8SRamesh Errabolu */
21330f28cca8SRamesh Errabolu if (p2plink->node_to == proximity_domain) {
21340f28cca8SRamesh Errabolu list_del(&p2plink->list);
21350f28cca8SRamesh Errabolu dev->node_props.p2p_links_count--;
21360f28cca8SRamesh Errabolu } else {
21370f28cca8SRamesh Errabolu if (p2plink->node_from > proximity_domain)
21380f28cca8SRamesh Errabolu p2plink->node_from--;
21390f28cca8SRamesh Errabolu if (p2plink->node_to > proximity_domain)
21400f28cca8SRamesh Errabolu p2plink->node_to--;
21410f28cca8SRamesh Errabolu }
21420f28cca8SRamesh Errabolu }
214346d18d51SMukul Joshi }
214446d18d51SMukul Joshi }
214546d18d51SMukul Joshi
kfd_topology_remove_device(struct kfd_node * gpu)21468dc1db31SMukul Joshi int kfd_topology_remove_device(struct kfd_node *gpu)
21475b5c4e40SEvgeny Pinchuk {
21484f449311SHarish Kasiviswanathan struct kfd_topology_device *dev, *tmp;
21495b5c4e40SEvgeny Pinchuk uint32_t gpu_id;
21505b5c4e40SEvgeny Pinchuk int res = -ENODEV;
215146d18d51SMukul Joshi int i = 0;
21525b5c4e40SEvgeny Pinchuk
21535b5c4e40SEvgeny Pinchuk down_write(&topology_lock);
21545b5c4e40SEvgeny Pinchuk
215546d18d51SMukul Joshi list_for_each_entry_safe(dev, tmp, &topology_device_list, list) {
21565b5c4e40SEvgeny Pinchuk if (dev->gpu == gpu) {
21575b5c4e40SEvgeny Pinchuk gpu_id = dev->gpu_id;
21585b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_node_entry(dev);
21595b5c4e40SEvgeny Pinchuk kfd_release_topology_device(dev);
21604f449311SHarish Kasiviswanathan sys_props.num_devices--;
216146d18d51SMukul Joshi kfd_topology_update_io_links(i);
216246d18d51SMukul Joshi topology_crat_proximity_domain = sys_props.num_devices-1;
216346d18d51SMukul Joshi sys_props.generation_count++;
21645b5c4e40SEvgeny Pinchuk res = 0;
21655b5c4e40SEvgeny Pinchuk if (kfd_topology_update_sysfs() < 0)
21665b5c4e40SEvgeny Pinchuk kfd_topology_release_sysfs();
21675b5c4e40SEvgeny Pinchuk break;
21685b5c4e40SEvgeny Pinchuk }
216946d18d51SMukul Joshi i++;
217046d18d51SMukul Joshi }
21715b5c4e40SEvgeny Pinchuk
21725b5c4e40SEvgeny Pinchuk up_write(&topology_lock);
21735b5c4e40SEvgeny Pinchuk
2174174de876SFelix Kuehling if (!res)
21755b5c4e40SEvgeny Pinchuk kfd_notify_gpu_change(gpu_id, 0);
21765b5c4e40SEvgeny Pinchuk
21775b5c4e40SEvgeny Pinchuk return res;
21785b5c4e40SEvgeny Pinchuk }
21795b5c4e40SEvgeny Pinchuk
21806d82eb0eSHarish Kasiviswanathan /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
21816d82eb0eSHarish Kasiviswanathan * topology. If GPU device is found @idx, then valid kfd_dev pointer is
21826d82eb0eSHarish Kasiviswanathan * returned through @kdev
21836d82eb0eSHarish Kasiviswanathan * Return - 0: On success (@kdev will be NULL for non GPU nodes)
21846d82eb0eSHarish Kasiviswanathan * -1: If end of list
21855b5c4e40SEvgeny Pinchuk */
kfd_topology_enum_kfd_devices(uint8_t idx,struct kfd_node ** kdev)21868dc1db31SMukul Joshi int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_node **kdev)
21875b5c4e40SEvgeny Pinchuk {
21885b5c4e40SEvgeny Pinchuk
21895b5c4e40SEvgeny Pinchuk struct kfd_topology_device *top_dev;
21905b5c4e40SEvgeny Pinchuk uint8_t device_idx = 0;
21915b5c4e40SEvgeny Pinchuk
21926d82eb0eSHarish Kasiviswanathan *kdev = NULL;
21935b5c4e40SEvgeny Pinchuk down_read(&topology_lock);
21945b5c4e40SEvgeny Pinchuk
21955b5c4e40SEvgeny Pinchuk list_for_each_entry(top_dev, &topology_device_list, list) {
21965b5c4e40SEvgeny Pinchuk if (device_idx == idx) {
21976d82eb0eSHarish Kasiviswanathan *kdev = top_dev->gpu;
21986d82eb0eSHarish Kasiviswanathan up_read(&topology_lock);
21996d82eb0eSHarish Kasiviswanathan return 0;
22005b5c4e40SEvgeny Pinchuk }
22015b5c4e40SEvgeny Pinchuk
22025b5c4e40SEvgeny Pinchuk device_idx++;
22035b5c4e40SEvgeny Pinchuk }
22045b5c4e40SEvgeny Pinchuk
22055b5c4e40SEvgeny Pinchuk up_read(&topology_lock);
22065b5c4e40SEvgeny Pinchuk
22076d82eb0eSHarish Kasiviswanathan return -1;
22085b5c4e40SEvgeny Pinchuk
22095b5c4e40SEvgeny Pinchuk }
2210851a645eSFelix Kuehling
kfd_cpumask_to_apic_id(const struct cpumask * cpumask)2211520b8fb7SFelix Kuehling static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
2212520b8fb7SFelix Kuehling {
2213520b8fb7SFelix Kuehling int first_cpu_of_numa_node;
2214520b8fb7SFelix Kuehling
2215520b8fb7SFelix Kuehling if (!cpumask || cpumask == cpu_none_mask)
2216520b8fb7SFelix Kuehling return -1;
2217520b8fb7SFelix Kuehling first_cpu_of_numa_node = cpumask_first(cpumask);
2218520b8fb7SFelix Kuehling if (first_cpu_of_numa_node >= nr_cpu_ids)
2219520b8fb7SFelix Kuehling return -1;
2220df1dd4f4SFelix Kuehling #ifdef CONFIG_X86_64
2221df1dd4f4SFelix Kuehling return cpu_data(first_cpu_of_numa_node).apicid;
2222df1dd4f4SFelix Kuehling #else
2223df1dd4f4SFelix Kuehling return first_cpu_of_numa_node;
2224df1dd4f4SFelix Kuehling #endif
2225520b8fb7SFelix Kuehling }
2226520b8fb7SFelix Kuehling
2227520b8fb7SFelix Kuehling /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
2228520b8fb7SFelix Kuehling * of the given NUMA node (numa_node_id)
2229520b8fb7SFelix Kuehling * Return -1 on failure
2230520b8fb7SFelix Kuehling */
kfd_numa_node_to_apic_id(int numa_node_id)2231520b8fb7SFelix Kuehling int kfd_numa_node_to_apic_id(int numa_node_id)
2232520b8fb7SFelix Kuehling {
2233520b8fb7SFelix Kuehling if (numa_node_id == -1) {
2234520b8fb7SFelix Kuehling pr_warn("Invalid NUMA Node. Use online CPU mask\n");
2235520b8fb7SFelix Kuehling return kfd_cpumask_to_apic_id(cpu_online_mask);
2236520b8fb7SFelix Kuehling }
2237520b8fb7SFelix Kuehling return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
2238520b8fb7SFelix Kuehling }
2239520b8fb7SFelix Kuehling
2240851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS)
2241851a645eSFelix Kuehling
kfd_debugfs_hqds_by_device(struct seq_file * m,void * data)2242851a645eSFelix Kuehling int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
2243851a645eSFelix Kuehling {
2244851a645eSFelix Kuehling struct kfd_topology_device *dev;
2245851a645eSFelix Kuehling unsigned int i = 0;
2246851a645eSFelix Kuehling int r = 0;
2247851a645eSFelix Kuehling
2248851a645eSFelix Kuehling down_read(&topology_lock);
2249851a645eSFelix Kuehling
2250851a645eSFelix Kuehling list_for_each_entry(dev, &topology_device_list, list) {
2251851a645eSFelix Kuehling if (!dev->gpu) {
2252851a645eSFelix Kuehling i++;
2253851a645eSFelix Kuehling continue;
2254851a645eSFelix Kuehling }
2255851a645eSFelix Kuehling
2256851a645eSFelix Kuehling seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
2257851a645eSFelix Kuehling r = dqm_debugfs_hqds(m, dev->gpu->dqm);
2258851a645eSFelix Kuehling if (r)
2259851a645eSFelix Kuehling break;
2260851a645eSFelix Kuehling }
2261851a645eSFelix Kuehling
2262851a645eSFelix Kuehling up_read(&topology_lock);
2263851a645eSFelix Kuehling
2264851a645eSFelix Kuehling return r;
2265851a645eSFelix Kuehling }
2266851a645eSFelix Kuehling
kfd_debugfs_rls_by_device(struct seq_file * m,void * data)2267851a645eSFelix Kuehling int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
2268851a645eSFelix Kuehling {
2269851a645eSFelix Kuehling struct kfd_topology_device *dev;
2270851a645eSFelix Kuehling unsigned int i = 0;
2271851a645eSFelix Kuehling int r = 0;
2272851a645eSFelix Kuehling
2273851a645eSFelix Kuehling down_read(&topology_lock);
2274851a645eSFelix Kuehling
2275851a645eSFelix Kuehling list_for_each_entry(dev, &topology_device_list, list) {
2276851a645eSFelix Kuehling if (!dev->gpu) {
2277851a645eSFelix Kuehling i++;
2278851a645eSFelix Kuehling continue;
2279851a645eSFelix Kuehling }
2280851a645eSFelix Kuehling
2281851a645eSFelix Kuehling seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
22829af5379cSOak Zeng r = pm_debugfs_runlist(m, &dev->gpu->dqm->packet_mgr);
2283851a645eSFelix Kuehling if (r)
2284851a645eSFelix Kuehling break;
2285851a645eSFelix Kuehling }
2286851a645eSFelix Kuehling
2287851a645eSFelix Kuehling up_read(&topology_lock);
2288851a645eSFelix Kuehling
2289851a645eSFelix Kuehling return r;
2290851a645eSFelix Kuehling }
2291851a645eSFelix Kuehling
2292851a645eSFelix Kuehling #endif
2293