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>
343ed181b8SHarish Kasiviswanathan #include <linux/crc16.h>
355b5c4e40SEvgeny Pinchuk
365b5c4e40SEvgeny Pinchuk #include "kfd_priv.h"
375b5c4e40SEvgeny Pinchuk #include "kfd_crat.h"
385b5c4e40SEvgeny Pinchuk #include "kfd_topology.h"
39851a645eSFelix Kuehling #include "kfd_device_queue_manager.h"
405a75ea56SFelix Kuehling #include "kfd_svm.h"
41fc7f1d96SJonathan Kim #include "kfd_debug.h"
425b87245fSAmber Lin #include "amdgpu_amdkfd.h"
430dee45a2SEric Huang #include "amdgpu_ras.h"
440f28cca8SRamesh Errabolu #include "amdgpu.h"
455b5c4e40SEvgeny Pinchuk
464f449311SHarish Kasiviswanathan /* topology_device_list - Master list of all topology devices */
474f449311SHarish Kasiviswanathan static struct list_head topology_device_list;
48520b8fb7SFelix Kuehling static struct kfd_system_properties sys_props;
495b5c4e40SEvgeny Pinchuk
505b5c4e40SEvgeny Pinchuk static DECLARE_RWSEM(topology_lock);
5146d18d51SMukul Joshi static uint32_t topology_crat_proximity_domain;
525b5c4e40SEvgeny Pinchuk
kfd_topology_device_by_proximity_domain_no_lock(uint32_t proximity_domain)5346d18d51SMukul Joshi struct kfd_topology_device *kfd_topology_device_by_proximity_domain_no_lock(
543a87177eSHarish Kasiviswanathan uint32_t proximity_domain)
553a87177eSHarish Kasiviswanathan {
563a87177eSHarish Kasiviswanathan struct kfd_topology_device *top_dev;
573a87177eSHarish Kasiviswanathan struct kfd_topology_device *device = NULL;
583a87177eSHarish Kasiviswanathan
593a87177eSHarish Kasiviswanathan list_for_each_entry(top_dev, &topology_device_list, list)
603a87177eSHarish Kasiviswanathan if (top_dev->proximity_domain == proximity_domain) {
613a87177eSHarish Kasiviswanathan device = top_dev;
623a87177eSHarish Kasiviswanathan break;
633a87177eSHarish Kasiviswanathan }
643a87177eSHarish Kasiviswanathan
6546d18d51SMukul Joshi return device;
6646d18d51SMukul Joshi }
6746d18d51SMukul Joshi
kfd_topology_device_by_proximity_domain(uint32_t proximity_domain)6846d18d51SMukul Joshi struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
6946d18d51SMukul Joshi uint32_t proximity_domain)
7046d18d51SMukul Joshi {
7146d18d51SMukul Joshi struct kfd_topology_device *device = NULL;
7246d18d51SMukul Joshi
7346d18d51SMukul Joshi down_read(&topology_lock);
7446d18d51SMukul Joshi
7546d18d51SMukul Joshi device = kfd_topology_device_by_proximity_domain_no_lock(
7646d18d51SMukul Joshi proximity_domain);
773a87177eSHarish Kasiviswanathan up_read(&topology_lock);
783a87177eSHarish Kasiviswanathan
793a87177eSHarish Kasiviswanathan return device;
803a87177eSHarish Kasiviswanathan }
813a87177eSHarish Kasiviswanathan
kfd_topology_device_by_id(uint32_t gpu_id)8244d8cc6fSYong Zhao struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
835b5c4e40SEvgeny Pinchuk {
8444d8cc6fSYong Zhao struct kfd_topology_device *top_dev = NULL;
8544d8cc6fSYong Zhao struct kfd_topology_device *ret = NULL;
865b5c4e40SEvgeny Pinchuk
875b5c4e40SEvgeny Pinchuk down_read(&topology_lock);
885b5c4e40SEvgeny Pinchuk
895b5c4e40SEvgeny Pinchuk list_for_each_entry(top_dev, &topology_device_list, list)
905b5c4e40SEvgeny Pinchuk if (top_dev->gpu_id == gpu_id) {
9144d8cc6fSYong Zhao ret = top_dev;
925b5c4e40SEvgeny Pinchuk break;
935b5c4e40SEvgeny Pinchuk }
945b5c4e40SEvgeny Pinchuk
955b5c4e40SEvgeny Pinchuk up_read(&topology_lock);
965b5c4e40SEvgeny Pinchuk
9744d8cc6fSYong Zhao return ret;
9844d8cc6fSYong Zhao }
9944d8cc6fSYong Zhao
kfd_device_by_id(uint32_t gpu_id)1008dc1db31SMukul Joshi struct kfd_node *kfd_device_by_id(uint32_t gpu_id)
10144d8cc6fSYong Zhao {
10244d8cc6fSYong Zhao struct kfd_topology_device *top_dev;
10344d8cc6fSYong Zhao
10444d8cc6fSYong Zhao top_dev = kfd_topology_device_by_id(gpu_id);
10544d8cc6fSYong Zhao if (!top_dev)
10644d8cc6fSYong Zhao return NULL;
10744d8cc6fSYong Zhao
10844d8cc6fSYong Zhao return top_dev->gpu;
1095b5c4e40SEvgeny Pinchuk }
1105b5c4e40SEvgeny Pinchuk
kfd_device_by_pci_dev(const struct pci_dev * pdev)1118dc1db31SMukul Joshi struct kfd_node *kfd_device_by_pci_dev(const struct pci_dev *pdev)
1125b5c4e40SEvgeny Pinchuk {
1135b5c4e40SEvgeny Pinchuk struct kfd_topology_device *top_dev;
1148dc1db31SMukul Joshi struct kfd_node *device = NULL;
1155b5c4e40SEvgeny Pinchuk
1165b5c4e40SEvgeny Pinchuk down_read(&topology_lock);
1175b5c4e40SEvgeny Pinchuk
1185b5c4e40SEvgeny Pinchuk list_for_each_entry(top_dev, &topology_device_list, list)
119d69a3b76SMukul Joshi if (top_dev->gpu && top_dev->gpu->adev->pdev == pdev) {
1205b5c4e40SEvgeny Pinchuk device = top_dev->gpu;
1215b5c4e40SEvgeny Pinchuk break;
1225b5c4e40SEvgeny Pinchuk }
1235b5c4e40SEvgeny Pinchuk
1245b5c4e40SEvgeny Pinchuk up_read(&topology_lock);
1255b5c4e40SEvgeny Pinchuk
1265b5c4e40SEvgeny Pinchuk return device;
1275b5c4e40SEvgeny Pinchuk }
1285b5c4e40SEvgeny Pinchuk
1293a87177eSHarish Kasiviswanathan /* Called with write topology_lock acquired */
kfd_release_topology_device(struct kfd_topology_device * dev)1305b5c4e40SEvgeny Pinchuk static void kfd_release_topology_device(struct kfd_topology_device *dev)
1315b5c4e40SEvgeny Pinchuk {
1325b5c4e40SEvgeny Pinchuk struct kfd_mem_properties *mem;
1335b5c4e40SEvgeny Pinchuk struct kfd_cache_properties *cache;
1345b5c4e40SEvgeny Pinchuk struct kfd_iolink_properties *iolink;
1350f28cca8SRamesh Errabolu struct kfd_iolink_properties *p2plink;
136f4757347SAmber Lin struct kfd_perf_properties *perf;
1375b5c4e40SEvgeny Pinchuk
1385b5c4e40SEvgeny Pinchuk list_del(&dev->list);
1395b5c4e40SEvgeny Pinchuk
1405b5c4e40SEvgeny Pinchuk while (dev->mem_props.next != &dev->mem_props) {
1415b5c4e40SEvgeny Pinchuk mem = container_of(dev->mem_props.next,
1425b5c4e40SEvgeny Pinchuk struct kfd_mem_properties, list);
1435b5c4e40SEvgeny Pinchuk list_del(&mem->list);
1445b5c4e40SEvgeny Pinchuk kfree(mem);
1455b5c4e40SEvgeny Pinchuk }
1465b5c4e40SEvgeny Pinchuk
1475b5c4e40SEvgeny Pinchuk while (dev->cache_props.next != &dev->cache_props) {
1485b5c4e40SEvgeny Pinchuk cache = container_of(dev->cache_props.next,
1495b5c4e40SEvgeny Pinchuk struct kfd_cache_properties, list);
1505b5c4e40SEvgeny Pinchuk list_del(&cache->list);
1515b5c4e40SEvgeny Pinchuk kfree(cache);
1525b5c4e40SEvgeny Pinchuk }
1535b5c4e40SEvgeny Pinchuk
1545b5c4e40SEvgeny Pinchuk while (dev->io_link_props.next != &dev->io_link_props) {
1555b5c4e40SEvgeny Pinchuk iolink = container_of(dev->io_link_props.next,
1565b5c4e40SEvgeny Pinchuk struct kfd_iolink_properties, list);
1575b5c4e40SEvgeny Pinchuk list_del(&iolink->list);
1585b5c4e40SEvgeny Pinchuk kfree(iolink);
1595b5c4e40SEvgeny Pinchuk }
1605b5c4e40SEvgeny Pinchuk
1610f28cca8SRamesh Errabolu while (dev->p2p_link_props.next != &dev->p2p_link_props) {
1620f28cca8SRamesh Errabolu p2plink = container_of(dev->p2p_link_props.next,
1630f28cca8SRamesh Errabolu struct kfd_iolink_properties, list);
1640f28cca8SRamesh Errabolu list_del(&p2plink->list);
1650f28cca8SRamesh Errabolu kfree(p2plink);
1660f28cca8SRamesh Errabolu }
1670f28cca8SRamesh Errabolu
168f4757347SAmber Lin while (dev->perf_props.next != &dev->perf_props) {
169f4757347SAmber Lin perf = container_of(dev->perf_props.next,
170f4757347SAmber Lin struct kfd_perf_properties, list);
171f4757347SAmber Lin list_del(&perf->list);
172f4757347SAmber Lin kfree(perf);
173f4757347SAmber Lin }
174f4757347SAmber Lin
1755b5c4e40SEvgeny Pinchuk kfree(dev);
1765b5c4e40SEvgeny Pinchuk }
1775b5c4e40SEvgeny Pinchuk
kfd_release_topology_device_list(struct list_head * device_list)1784f449311SHarish Kasiviswanathan void kfd_release_topology_device_list(struct list_head *device_list)
1795b5c4e40SEvgeny Pinchuk {
1805b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev;
1815b5c4e40SEvgeny Pinchuk
1824f449311SHarish Kasiviswanathan while (!list_empty(device_list)) {
1834f449311SHarish Kasiviswanathan dev = list_first_entry(device_list,
1845b5c4e40SEvgeny Pinchuk struct kfd_topology_device, list);
1855b5c4e40SEvgeny Pinchuk kfd_release_topology_device(dev);
1865b5c4e40SEvgeny Pinchuk }
1874f449311SHarish Kasiviswanathan }
1885b5c4e40SEvgeny Pinchuk
kfd_release_live_view(void)1894f449311SHarish Kasiviswanathan static void kfd_release_live_view(void)
1904f449311SHarish Kasiviswanathan {
1914f449311SHarish Kasiviswanathan kfd_release_topology_device_list(&topology_device_list);
1925b5c4e40SEvgeny Pinchuk memset(&sys_props, 0, sizeof(sys_props));
1935b5c4e40SEvgeny Pinchuk }
1945b5c4e40SEvgeny Pinchuk
kfd_create_topology_device(struct list_head * device_list)1954f449311SHarish Kasiviswanathan struct kfd_topology_device *kfd_create_topology_device(
1964f449311SHarish Kasiviswanathan struct list_head *device_list)
1975b5c4e40SEvgeny Pinchuk {
1985b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev;
1995b5c4e40SEvgeny Pinchuk
2005b5c4e40SEvgeny Pinchuk dev = kfd_alloc_struct(dev);
2014eacc26bSKent Russell if (!dev) {
2025b5c4e40SEvgeny Pinchuk pr_err("No memory to allocate a topology device");
20316b9201cSOded Gabbay return NULL;
2045b5c4e40SEvgeny Pinchuk }
2055b5c4e40SEvgeny Pinchuk
2065b5c4e40SEvgeny Pinchuk INIT_LIST_HEAD(&dev->mem_props);
2075b5c4e40SEvgeny Pinchuk INIT_LIST_HEAD(&dev->cache_props);
2085b5c4e40SEvgeny Pinchuk INIT_LIST_HEAD(&dev->io_link_props);
2090f28cca8SRamesh Errabolu INIT_LIST_HEAD(&dev->p2p_link_props);
210f4757347SAmber Lin INIT_LIST_HEAD(&dev->perf_props);
2115b5c4e40SEvgeny Pinchuk
2124f449311SHarish Kasiviswanathan list_add_tail(&dev->list, device_list);
2135b5c4e40SEvgeny Pinchuk
2145b5c4e40SEvgeny Pinchuk return dev;
2155b5c4e40SEvgeny Pinchuk }
2165b5c4e40SEvgeny Pinchuk
2175b5c4e40SEvgeny Pinchuk
21883a13ef5SFelix Kuehling #define sysfs_show_gen_prop(buffer, offs, fmt, ...) \
21983a13ef5SFelix Kuehling (offs += snprintf(buffer+offs, PAGE_SIZE-offs, \
22083a13ef5SFelix Kuehling fmt, __VA_ARGS__))
22183a13ef5SFelix Kuehling #define sysfs_show_32bit_prop(buffer, offs, name, value) \
22283a13ef5SFelix Kuehling sysfs_show_gen_prop(buffer, offs, "%s %u\n", name, value)
22383a13ef5SFelix Kuehling #define sysfs_show_64bit_prop(buffer, offs, name, value) \
22483a13ef5SFelix Kuehling sysfs_show_gen_prop(buffer, offs, "%s %llu\n", name, value)
22583a13ef5SFelix Kuehling #define sysfs_show_32bit_val(buffer, offs, value) \
22683a13ef5SFelix Kuehling sysfs_show_gen_prop(buffer, offs, "%u\n", value)
22783a13ef5SFelix Kuehling #define sysfs_show_str_val(buffer, offs, value) \
22883a13ef5SFelix Kuehling sysfs_show_gen_prop(buffer, offs, "%s\n", value)
2295b5c4e40SEvgeny Pinchuk
sysprops_show(struct kobject * kobj,struct attribute * attr,char * buffer)2305b5c4e40SEvgeny Pinchuk static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
2315b5c4e40SEvgeny Pinchuk char *buffer)
2325b5c4e40SEvgeny Pinchuk {
23383a13ef5SFelix Kuehling int offs = 0;
2345b5c4e40SEvgeny Pinchuk
2355b5c4e40SEvgeny Pinchuk /* Making sure that the buffer is an empty string */
2365b5c4e40SEvgeny Pinchuk buffer[0] = 0;
2375b5c4e40SEvgeny Pinchuk
2385b5c4e40SEvgeny Pinchuk if (attr == &sys_props.attr_genid) {
23983a13ef5SFelix Kuehling sysfs_show_32bit_val(buffer, offs,
24083a13ef5SFelix Kuehling sys_props.generation_count);
2415b5c4e40SEvgeny Pinchuk } else if (attr == &sys_props.attr_props) {
24283a13ef5SFelix Kuehling sysfs_show_64bit_prop(buffer, offs, "platform_oem",
2435b5c4e40SEvgeny Pinchuk sys_props.platform_oem);
24483a13ef5SFelix Kuehling sysfs_show_64bit_prop(buffer, offs, "platform_id",
2455b5c4e40SEvgeny Pinchuk sys_props.platform_id);
24683a13ef5SFelix Kuehling sysfs_show_64bit_prop(buffer, offs, "platform_rev",
2475b5c4e40SEvgeny Pinchuk sys_props.platform_rev);
2485b5c4e40SEvgeny Pinchuk } else {
24983a13ef5SFelix Kuehling offs = -EINVAL;
2505b5c4e40SEvgeny Pinchuk }
2515b5c4e40SEvgeny Pinchuk
25283a13ef5SFelix Kuehling return offs;
2535b5c4e40SEvgeny Pinchuk }
2545b5c4e40SEvgeny Pinchuk
kfd_topology_kobj_release(struct kobject * kobj)2555108d768SYong Zhao static void kfd_topology_kobj_release(struct kobject *kobj)
2565108d768SYong Zhao {
2575108d768SYong Zhao kfree(kobj);
2585108d768SYong Zhao }
2595108d768SYong Zhao
2605b5c4e40SEvgeny Pinchuk static const struct sysfs_ops sysprops_ops = {
2615b5c4e40SEvgeny Pinchuk .show = sysprops_show,
2625b5c4e40SEvgeny Pinchuk };
2635b5c4e40SEvgeny Pinchuk
2644fa01c63SThomas Weißschuh static const struct kobj_type sysprops_type = {
2655108d768SYong Zhao .release = kfd_topology_kobj_release,
2665b5c4e40SEvgeny Pinchuk .sysfs_ops = &sysprops_ops,
2675b5c4e40SEvgeny Pinchuk };
2685b5c4e40SEvgeny Pinchuk
iolink_show(struct kobject * kobj,struct attribute * attr,char * buffer)2695b5c4e40SEvgeny Pinchuk static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
2705b5c4e40SEvgeny Pinchuk char *buffer)
2715b5c4e40SEvgeny Pinchuk {
27283a13ef5SFelix Kuehling int offs = 0;
2735b5c4e40SEvgeny Pinchuk struct kfd_iolink_properties *iolink;
2745b5c4e40SEvgeny Pinchuk
2755b5c4e40SEvgeny Pinchuk /* Making sure that the buffer is an empty string */
2765b5c4e40SEvgeny Pinchuk buffer[0] = 0;
2775b5c4e40SEvgeny Pinchuk
2785b5c4e40SEvgeny Pinchuk iolink = container_of(attr, struct kfd_iolink_properties, attr);
2796b855f7bSHarish Kasiviswanathan if (iolink->gpu && kfd_devcgroup_check_permission(iolink->gpu))
2806b855f7bSHarish Kasiviswanathan return -EPERM;
28183a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "type", iolink->iolink_type);
28283a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "version_major", iolink->ver_maj);
28383a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "version_minor", iolink->ver_min);
28483a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "node_from", iolink->node_from);
28583a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "node_to", iolink->node_to);
28683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "weight", iolink->weight);
28783a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "min_latency", iolink->min_latency);
28883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "max_latency", iolink->max_latency);
28983a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "min_bandwidth",
29083a13ef5SFelix Kuehling iolink->min_bandwidth);
29183a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "max_bandwidth",
29283a13ef5SFelix Kuehling iolink->max_bandwidth);
29383a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "recommended_transfer_size",
2945b5c4e40SEvgeny Pinchuk iolink->rec_transfer_size);
295e06b71b2SJonathan Kim sysfs_show_32bit_prop(buffer, offs, "recommended_sdma_engine_id_mask",
296e06b71b2SJonathan Kim iolink->rec_sdma_eng_id_mask);
29783a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "flags", iolink->flags);
2985b5c4e40SEvgeny Pinchuk
29983a13ef5SFelix Kuehling return offs;
3005b5c4e40SEvgeny Pinchuk }
3015b5c4e40SEvgeny Pinchuk
3025b5c4e40SEvgeny Pinchuk static const struct sysfs_ops iolink_ops = {
3035b5c4e40SEvgeny Pinchuk .show = iolink_show,
3045b5c4e40SEvgeny Pinchuk };
3055b5c4e40SEvgeny Pinchuk
3064fa01c63SThomas Weißschuh static const struct kobj_type iolink_type = {
3075108d768SYong Zhao .release = kfd_topology_kobj_release,
3085b5c4e40SEvgeny Pinchuk .sysfs_ops = &iolink_ops,
3095b5c4e40SEvgeny Pinchuk };
3105b5c4e40SEvgeny Pinchuk
mem_show(struct kobject * kobj,struct attribute * attr,char * buffer)3115b5c4e40SEvgeny Pinchuk static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
3125b5c4e40SEvgeny Pinchuk char *buffer)
3135b5c4e40SEvgeny Pinchuk {
31483a13ef5SFelix Kuehling int offs = 0;
3155b5c4e40SEvgeny Pinchuk struct kfd_mem_properties *mem;
3165b5c4e40SEvgeny Pinchuk
3175b5c4e40SEvgeny Pinchuk /* Making sure that the buffer is an empty string */
3185b5c4e40SEvgeny Pinchuk buffer[0] = 0;
3195b5c4e40SEvgeny Pinchuk
3205b5c4e40SEvgeny Pinchuk mem = container_of(attr, struct kfd_mem_properties, attr);
3216b855f7bSHarish Kasiviswanathan if (mem->gpu && kfd_devcgroup_check_permission(mem->gpu))
3226b855f7bSHarish Kasiviswanathan return -EPERM;
32383a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "heap_type", mem->heap_type);
32483a13ef5SFelix Kuehling sysfs_show_64bit_prop(buffer, offs, "size_in_bytes",
32583a13ef5SFelix Kuehling mem->size_in_bytes);
32683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "flags", mem->flags);
32783a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "width", mem->width);
32883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "mem_clk_max",
32983a13ef5SFelix Kuehling mem->mem_clk_max);
3305b5c4e40SEvgeny Pinchuk
33183a13ef5SFelix Kuehling return offs;
3325b5c4e40SEvgeny Pinchuk }
3335b5c4e40SEvgeny Pinchuk
3345b5c4e40SEvgeny Pinchuk static const struct sysfs_ops mem_ops = {
3355b5c4e40SEvgeny Pinchuk .show = mem_show,
3365b5c4e40SEvgeny Pinchuk };
3375b5c4e40SEvgeny Pinchuk
3384fa01c63SThomas Weißschuh static const struct kobj_type mem_type = {
3395108d768SYong Zhao .release = kfd_topology_kobj_release,
3405b5c4e40SEvgeny Pinchuk .sysfs_ops = &mem_ops,
3415b5c4e40SEvgeny Pinchuk };
3425b5c4e40SEvgeny Pinchuk
kfd_cache_show(struct kobject * kobj,struct attribute * attr,char * buffer)3435b5c4e40SEvgeny Pinchuk static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
3445b5c4e40SEvgeny Pinchuk char *buffer)
3455b5c4e40SEvgeny Pinchuk {
34683a13ef5SFelix Kuehling int offs = 0;
347bc0c75a3SHarish Kasiviswanathan uint32_t i, j;
3485b5c4e40SEvgeny Pinchuk struct kfd_cache_properties *cache;
3495b5c4e40SEvgeny Pinchuk
3505b5c4e40SEvgeny Pinchuk /* Making sure that the buffer is an empty string */
3515b5c4e40SEvgeny Pinchuk buffer[0] = 0;
3525b5c4e40SEvgeny Pinchuk cache = container_of(attr, struct kfd_cache_properties, attr);
3536b855f7bSHarish Kasiviswanathan if (cache->gpu && kfd_devcgroup_check_permission(cache->gpu))
3546b855f7bSHarish Kasiviswanathan return -EPERM;
35583a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "processor_id_low",
3565b5c4e40SEvgeny Pinchuk cache->processor_id_low);
35783a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "level", cache->cache_level);
35883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "size", cache->cache_size);
35983a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "cache_line_size",
36083a13ef5SFelix Kuehling cache->cacheline_size);
36183a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "cache_lines_per_tag",
3625b5c4e40SEvgeny Pinchuk cache->cachelines_per_tag);
36383a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "association", cache->cache_assoc);
36483a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "latency", cache->cache_latency);
36583a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "type", cache->cache_type);
366c0cc999fSMa Jun
36783a13ef5SFelix Kuehling offs += snprintf(buffer+offs, PAGE_SIZE-offs, "sibling_map ");
368c0cc999fSMa Jun for (i = 0; i < cache->sibling_map_size; i++)
36983a13ef5SFelix Kuehling for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++)
370bc0c75a3SHarish Kasiviswanathan /* Check each bit */
37183a13ef5SFelix Kuehling offs += snprintf(buffer+offs, PAGE_SIZE-offs, "%d,",
37283a13ef5SFelix Kuehling (cache->sibling_map[i] >> j) & 1);
37383a13ef5SFelix Kuehling
374bc0c75a3SHarish Kasiviswanathan /* Replace the last "," with end of line */
37583a13ef5SFelix Kuehling buffer[offs-1] = '\n';
37683a13ef5SFelix Kuehling return offs;
3775b5c4e40SEvgeny Pinchuk }
3785b5c4e40SEvgeny Pinchuk
3795b5c4e40SEvgeny Pinchuk static const struct sysfs_ops cache_ops = {
3805b5c4e40SEvgeny Pinchuk .show = kfd_cache_show,
3815b5c4e40SEvgeny Pinchuk };
3825b5c4e40SEvgeny Pinchuk
3834fa01c63SThomas Weißschuh static const struct kobj_type cache_type = {
3845108d768SYong Zhao .release = kfd_topology_kobj_release,
3855b5c4e40SEvgeny Pinchuk .sysfs_ops = &cache_ops,
3865b5c4e40SEvgeny Pinchuk };
3875b5c4e40SEvgeny Pinchuk
388f4757347SAmber Lin /****** Sysfs of Performance Counters ******/
389f4757347SAmber Lin
390f4757347SAmber Lin struct kfd_perf_attr {
391f4757347SAmber Lin struct kobj_attribute attr;
392f4757347SAmber Lin uint32_t data;
393f4757347SAmber Lin };
394f4757347SAmber Lin
perf_show(struct kobject * kobj,struct kobj_attribute * attrs,char * buf)395f4757347SAmber Lin static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
396f4757347SAmber Lin char *buf)
397f4757347SAmber Lin {
39883a13ef5SFelix Kuehling int offs = 0;
399f4757347SAmber Lin struct kfd_perf_attr *attr;
400f4757347SAmber Lin
401f4757347SAmber Lin buf[0] = 0;
402f4757347SAmber Lin attr = container_of(attrs, struct kfd_perf_attr, attr);
403f4757347SAmber Lin if (!attr->data) /* invalid data for PMC */
404f4757347SAmber Lin return 0;
405f4757347SAmber Lin else
40683a13ef5SFelix Kuehling return sysfs_show_32bit_val(buf, offs, attr->data);
407f4757347SAmber Lin }
408f4757347SAmber Lin
409f4757347SAmber Lin #define KFD_PERF_DESC(_name, _data) \
410f4757347SAmber Lin { \
411f4757347SAmber Lin .attr = __ATTR(_name, 0444, perf_show, NULL), \
412f4757347SAmber Lin .data = _data, \
413f4757347SAmber Lin }
414f4757347SAmber Lin
415f4757347SAmber Lin static struct kfd_perf_attr perf_attr_iommu[] = {
416f4757347SAmber Lin KFD_PERF_DESC(max_concurrent, 0),
417f4757347SAmber Lin KFD_PERF_DESC(num_counters, 0),
418f4757347SAmber Lin KFD_PERF_DESC(counter_ids, 0),
419f4757347SAmber Lin };
420f4757347SAmber Lin /****************************************/
421f4757347SAmber Lin
node_show(struct kobject * kobj,struct attribute * attr,char * buffer)4225b5c4e40SEvgeny Pinchuk static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
4235b5c4e40SEvgeny Pinchuk char *buffer)
4245b5c4e40SEvgeny Pinchuk {
42583a13ef5SFelix Kuehling int offs = 0;
4265b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev;
427f7c826adSAlexey Skidanov uint32_t log_max_watch_addr;
4285b5c4e40SEvgeny Pinchuk
4295b5c4e40SEvgeny Pinchuk /* Making sure that the buffer is an empty string */
4305b5c4e40SEvgeny Pinchuk buffer[0] = 0;
4315b5c4e40SEvgeny Pinchuk
4325b5c4e40SEvgeny Pinchuk if (strcmp(attr->name, "gpu_id") == 0) {
4335b5c4e40SEvgeny Pinchuk dev = container_of(attr, struct kfd_topology_device,
4345b5c4e40SEvgeny Pinchuk attr_gpuid);
4356b855f7bSHarish Kasiviswanathan if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
4366b855f7bSHarish Kasiviswanathan return -EPERM;
43783a13ef5SFelix Kuehling return sysfs_show_32bit_val(buffer, offs, dev->gpu_id);
438f7c826adSAlexey Skidanov }
439f7c826adSAlexey Skidanov
440f7c826adSAlexey Skidanov if (strcmp(attr->name, "name") == 0) {
4415b5c4e40SEvgeny Pinchuk dev = container_of(attr, struct kfd_topology_device,
4425b5c4e40SEvgeny Pinchuk attr_name);
443c181159aSYong Zhao
4446b855f7bSHarish Kasiviswanathan if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
4456b855f7bSHarish Kasiviswanathan return -EPERM;
44683a13ef5SFelix Kuehling return sysfs_show_str_val(buffer, offs, dev->node_props.name);
447f7c826adSAlexey Skidanov }
448f7c826adSAlexey Skidanov
4495b5c4e40SEvgeny Pinchuk dev = container_of(attr, struct kfd_topology_device,
4505b5c4e40SEvgeny Pinchuk attr_props);
4516b855f7bSHarish Kasiviswanathan if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
4526b855f7bSHarish Kasiviswanathan return -EPERM;
45383a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "cpu_cores_count",
4545b5c4e40SEvgeny Pinchuk dev->node_props.cpu_cores_count);
45583a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "simd_count",
45697e3c6a8SMukul Joshi dev->gpu ? dev->node_props.simd_count : 0);
45783a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "mem_banks_count",
4585b5c4e40SEvgeny Pinchuk dev->node_props.mem_banks_count);
45983a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "caches_count",
4605b5c4e40SEvgeny Pinchuk dev->node_props.caches_count);
46183a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "io_links_count",
4625b5c4e40SEvgeny Pinchuk dev->node_props.io_links_count);
4630f28cca8SRamesh Errabolu sysfs_show_32bit_prop(buffer, offs, "p2p_links_count",
4640f28cca8SRamesh Errabolu dev->node_props.p2p_links_count);
46583a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "cpu_core_id_base",
4665b5c4e40SEvgeny Pinchuk dev->node_props.cpu_core_id_base);
46783a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "simd_id_base",
4685b5c4e40SEvgeny Pinchuk dev->node_props.simd_id_base);
46983a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "max_waves_per_simd",
4705b5c4e40SEvgeny Pinchuk dev->node_props.max_waves_per_simd);
47183a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "lds_size_in_kb",
4725b5c4e40SEvgeny Pinchuk dev->node_props.lds_size_in_kb);
47383a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "gds_size_in_kb",
4745b5c4e40SEvgeny Pinchuk dev->node_props.gds_size_in_kb);
47583a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "num_gws",
47629e76462SOak Zeng dev->node_props.num_gws);
47783a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "wave_front_size",
4785b5c4e40SEvgeny Pinchuk dev->node_props.wave_front_size);
47983a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "array_count",
480f38f147aSMukul Joshi dev->gpu ? (dev->node_props.array_count *
481c4050ff1SLijo Lazar NUM_XCC(dev->gpu->xcc_mask)) : 0);
48283a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "simd_arrays_per_engine",
4835b5c4e40SEvgeny Pinchuk dev->node_props.simd_arrays_per_engine);
48483a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "cu_per_simd_array",
4855b5c4e40SEvgeny Pinchuk dev->node_props.cu_per_simd_array);
48683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "simd_per_cu",
4875b5c4e40SEvgeny Pinchuk dev->node_props.simd_per_cu);
48883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "max_slots_scratch_cu",
4895b5c4e40SEvgeny Pinchuk dev->node_props.max_slots_scratch_cu);
4909d6fa9c7SGraham Sider sysfs_show_32bit_prop(buffer, offs, "gfx_target_version",
4919d6fa9c7SGraham Sider dev->node_props.gfx_target_version);
49283a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "vendor_id",
4935b5c4e40SEvgeny Pinchuk dev->node_props.vendor_id);
49483a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "device_id",
4955b5c4e40SEvgeny Pinchuk dev->node_props.device_id);
49683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "location_id",
4975b5c4e40SEvgeny Pinchuk dev->node_props.location_id);
49883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "domain",
4993e58e95aSOri Messinger dev->node_props.domain);
50083a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "drm_render_minor",
5017c9b7171SOak Zeng dev->node_props.drm_render_minor);
50283a13ef5SFelix Kuehling sysfs_show_64bit_prop(buffer, offs, "hive_id",
5030c1690e3SShaoyun Liu dev->node_props.hive_id);
50483a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "num_sdma_engines",
50514568cf6SOak Zeng dev->node_props.num_sdma_engines);
50683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "num_sdma_xgmi_engines",
50714568cf6SOak Zeng dev->node_props.num_sdma_xgmi_engines);
50883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "num_sdma_queues_per_engine",
509bb71c74dSHuang Rui dev->node_props.num_sdma_queues_per_engine);
51083a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "num_cp_queues",
511f4feb9faSHuang Rui dev->node_props.num_cp_queues);
5125b5c4e40SEvgeny Pinchuk
5135b5c4e40SEvgeny Pinchuk if (dev->gpu) {
514f7c826adSAlexey Skidanov log_max_watch_addr =
5158dc1db31SMukul Joshi __ilog2_u32(dev->gpu->kfd->device_info.num_of_watch_points);
516f7c826adSAlexey Skidanov
517f7c826adSAlexey Skidanov if (log_max_watch_addr) {
518f7c826adSAlexey Skidanov dev->node_props.capability |=
519f7c826adSAlexey Skidanov HSA_CAP_WATCH_POINTS_SUPPORTED;
520f7c826adSAlexey Skidanov
521f7c826adSAlexey Skidanov dev->node_props.capability |=
522f7c826adSAlexey Skidanov ((log_max_watch_addr <<
523f7c826adSAlexey Skidanov HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
524f7c826adSAlexey Skidanov HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
525f7c826adSAlexey Skidanov }
526f7c826adSAlexey Skidanov
5277eb0502aSGraham Sider if (dev->gpu->adev->asic_type == CHIP_TONGA)
528413e85d5SBen Goz dev->node_props.capability |=
529413e85d5SBen Goz HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
530413e85d5SBen Goz
53183a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_fcompute",
5323a87177eSHarish Kasiviswanathan dev->node_props.max_engine_clk_fcompute);
53342e08c78SOded Gabbay
53483a13ef5SFelix Kuehling sysfs_show_64bit_prop(buffer, offs, "local_mem_size", 0ULL);
535f1386fbcSOded Gabbay
53683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "fw_version",
5378dc1db31SMukul Joshi dev->gpu->kfd->mec_fw_version);
53883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "capability",
539826f5de8SAlexey Skidanov dev->node_props.capability);
540d230f1bfSJonathan Kim sysfs_show_64bit_prop(buffer, offs, "debug_prop",
541d230f1bfSJonathan Kim dev->node_props.debug_prop);
54283a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "sdma_fw_version",
5438dc1db31SMukul Joshi dev->gpu->kfd->sdma_fw_version);
54411964258SKent Russell sysfs_show_64bit_prop(buffer, offs, "unique_id",
54502274fc0SGraham Sider dev->gpu->adev->unique_id);
54674c5b85dSMukul Joshi sysfs_show_32bit_prop(buffer, offs, "num_xcc",
547c4050ff1SLijo Lazar NUM_XCC(dev->gpu->xcc_mask));
5485b5c4e40SEvgeny Pinchuk }
5495b5c4e40SEvgeny Pinchuk
55083a13ef5SFelix Kuehling return sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_ccompute",
5515b5c4e40SEvgeny Pinchuk cpufreq_quick_get_max(0)/1000);
5525b5c4e40SEvgeny Pinchuk }
5535b5c4e40SEvgeny Pinchuk
5545b5c4e40SEvgeny Pinchuk static const struct sysfs_ops node_ops = {
5555b5c4e40SEvgeny Pinchuk .show = node_show,
5565b5c4e40SEvgeny Pinchuk };
5575b5c4e40SEvgeny Pinchuk
5584fa01c63SThomas Weißschuh static const struct kobj_type node_type = {
5595108d768SYong Zhao .release = kfd_topology_kobj_release,
5605b5c4e40SEvgeny Pinchuk .sysfs_ops = &node_ops,
5615b5c4e40SEvgeny Pinchuk };
5625b5c4e40SEvgeny Pinchuk
kfd_remove_sysfs_file(struct kobject * kobj,struct attribute * attr)5635b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
5645b5c4e40SEvgeny Pinchuk {
5655b5c4e40SEvgeny Pinchuk sysfs_remove_file(kobj, attr);
5665b5c4e40SEvgeny Pinchuk kobject_del(kobj);
5675b5c4e40SEvgeny Pinchuk kobject_put(kobj);
5685b5c4e40SEvgeny Pinchuk }
5695b5c4e40SEvgeny Pinchuk
kfd_remove_sysfs_node_entry(struct kfd_topology_device * dev)5705b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
5715b5c4e40SEvgeny Pinchuk {
5720f28cca8SRamesh Errabolu struct kfd_iolink_properties *p2plink;
5735b5c4e40SEvgeny Pinchuk struct kfd_iolink_properties *iolink;
5745b5c4e40SEvgeny Pinchuk struct kfd_cache_properties *cache;
5755b5c4e40SEvgeny Pinchuk struct kfd_mem_properties *mem;
576f4757347SAmber Lin struct kfd_perf_properties *perf;
5775b5c4e40SEvgeny Pinchuk
5785b5c4e40SEvgeny Pinchuk if (dev->kobj_iolink) {
5795b5c4e40SEvgeny Pinchuk list_for_each_entry(iolink, &dev->io_link_props, list)
5805b5c4e40SEvgeny Pinchuk if (iolink->kobj) {
5815b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_file(iolink->kobj,
5825b5c4e40SEvgeny Pinchuk &iolink->attr);
58316b9201cSOded Gabbay iolink->kobj = NULL;
5845b5c4e40SEvgeny Pinchuk }
5855b5c4e40SEvgeny Pinchuk kobject_del(dev->kobj_iolink);
5865b5c4e40SEvgeny Pinchuk kobject_put(dev->kobj_iolink);
58716b9201cSOded Gabbay dev->kobj_iolink = NULL;
5885b5c4e40SEvgeny Pinchuk }
5895b5c4e40SEvgeny Pinchuk
5900f28cca8SRamesh Errabolu if (dev->kobj_p2plink) {
5910f28cca8SRamesh Errabolu list_for_each_entry(p2plink, &dev->p2p_link_props, list)
5920f28cca8SRamesh Errabolu if (p2plink->kobj) {
5930f28cca8SRamesh Errabolu kfd_remove_sysfs_file(p2plink->kobj,
5940f28cca8SRamesh Errabolu &p2plink->attr);
5950f28cca8SRamesh Errabolu p2plink->kobj = NULL;
5960f28cca8SRamesh Errabolu }
5970f28cca8SRamesh Errabolu kobject_del(dev->kobj_p2plink);
5980f28cca8SRamesh Errabolu kobject_put(dev->kobj_p2plink);
5990f28cca8SRamesh Errabolu dev->kobj_p2plink = NULL;
6000f28cca8SRamesh Errabolu }
6010f28cca8SRamesh Errabolu
6025b5c4e40SEvgeny Pinchuk if (dev->kobj_cache) {
6035b5c4e40SEvgeny Pinchuk list_for_each_entry(cache, &dev->cache_props, list)
6045b5c4e40SEvgeny Pinchuk if (cache->kobj) {
6055b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_file(cache->kobj,
6065b5c4e40SEvgeny Pinchuk &cache->attr);
60716b9201cSOded Gabbay cache->kobj = NULL;
6085b5c4e40SEvgeny Pinchuk }
6095b5c4e40SEvgeny Pinchuk kobject_del(dev->kobj_cache);
6105b5c4e40SEvgeny Pinchuk kobject_put(dev->kobj_cache);
61116b9201cSOded Gabbay dev->kobj_cache = NULL;
6125b5c4e40SEvgeny Pinchuk }
6135b5c4e40SEvgeny Pinchuk
6145b5c4e40SEvgeny Pinchuk if (dev->kobj_mem) {
6155b5c4e40SEvgeny Pinchuk list_for_each_entry(mem, &dev->mem_props, list)
6165b5c4e40SEvgeny Pinchuk if (mem->kobj) {
6175b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_file(mem->kobj, &mem->attr);
61816b9201cSOded Gabbay mem->kobj = NULL;
6195b5c4e40SEvgeny Pinchuk }
6205b5c4e40SEvgeny Pinchuk kobject_del(dev->kobj_mem);
6215b5c4e40SEvgeny Pinchuk kobject_put(dev->kobj_mem);
62216b9201cSOded Gabbay dev->kobj_mem = NULL;
6235b5c4e40SEvgeny Pinchuk }
6245b5c4e40SEvgeny Pinchuk
625f4757347SAmber Lin if (dev->kobj_perf) {
626f4757347SAmber Lin list_for_each_entry(perf, &dev->perf_props, list) {
627f4757347SAmber Lin kfree(perf->attr_group);
628f4757347SAmber Lin perf->attr_group = NULL;
629f4757347SAmber Lin }
630f4757347SAmber Lin kobject_del(dev->kobj_perf);
631f4757347SAmber Lin kobject_put(dev->kobj_perf);
632f4757347SAmber Lin dev->kobj_perf = NULL;
633f4757347SAmber Lin }
634f4757347SAmber Lin
6355b5c4e40SEvgeny Pinchuk if (dev->kobj_node) {
6365b5c4e40SEvgeny Pinchuk sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
6375b5c4e40SEvgeny Pinchuk sysfs_remove_file(dev->kobj_node, &dev->attr_name);
6385b5c4e40SEvgeny Pinchuk sysfs_remove_file(dev->kobj_node, &dev->attr_props);
6395b5c4e40SEvgeny Pinchuk kobject_del(dev->kobj_node);
6405b5c4e40SEvgeny Pinchuk kobject_put(dev->kobj_node);
64116b9201cSOded Gabbay dev->kobj_node = NULL;
6425b5c4e40SEvgeny Pinchuk }
6435b5c4e40SEvgeny Pinchuk }
6445b5c4e40SEvgeny Pinchuk
kfd_build_sysfs_node_entry(struct kfd_topology_device * dev,uint32_t id)6455b5c4e40SEvgeny Pinchuk static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
6465b5c4e40SEvgeny Pinchuk uint32_t id)
6475b5c4e40SEvgeny Pinchuk {
6480f28cca8SRamesh Errabolu struct kfd_iolink_properties *p2plink;
6495b5c4e40SEvgeny Pinchuk struct kfd_iolink_properties *iolink;
6505b5c4e40SEvgeny Pinchuk struct kfd_cache_properties *cache;
6515b5c4e40SEvgeny Pinchuk struct kfd_mem_properties *mem;
652f4757347SAmber Lin struct kfd_perf_properties *perf;
6535b5c4e40SEvgeny Pinchuk int ret;
654f4757347SAmber Lin uint32_t i, num_attrs;
655f4757347SAmber Lin struct attribute **attrs;
6565b5c4e40SEvgeny Pinchuk
65732fa8219SFelix Kuehling if (WARN_ON(dev->kobj_node))
65832fa8219SFelix Kuehling return -EEXIST;
65932fa8219SFelix Kuehling
6605b5c4e40SEvgeny Pinchuk /*
6615b5c4e40SEvgeny Pinchuk * Creating the sysfs folders
6625b5c4e40SEvgeny Pinchuk */
6635b5c4e40SEvgeny Pinchuk dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
6645b5c4e40SEvgeny Pinchuk if (!dev->kobj_node)
6655b5c4e40SEvgeny Pinchuk return -ENOMEM;
6665b5c4e40SEvgeny Pinchuk
6675b5c4e40SEvgeny Pinchuk ret = kobject_init_and_add(dev->kobj_node, &node_type,
6685b5c4e40SEvgeny Pinchuk sys_props.kobj_nodes, "%d", id);
66920eca012SQiushi Wu if (ret < 0) {
67020eca012SQiushi Wu kobject_put(dev->kobj_node);
6715b5c4e40SEvgeny Pinchuk return ret;
67220eca012SQiushi Wu }
6735b5c4e40SEvgeny Pinchuk
6745b5c4e40SEvgeny Pinchuk dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
6755b5c4e40SEvgeny Pinchuk if (!dev->kobj_mem)
6765b5c4e40SEvgeny Pinchuk return -ENOMEM;
6775b5c4e40SEvgeny Pinchuk
6785b5c4e40SEvgeny Pinchuk dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
6795b5c4e40SEvgeny Pinchuk if (!dev->kobj_cache)
6805b5c4e40SEvgeny Pinchuk return -ENOMEM;
6815b5c4e40SEvgeny Pinchuk
6825b5c4e40SEvgeny Pinchuk dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
6835b5c4e40SEvgeny Pinchuk if (!dev->kobj_iolink)
6845b5c4e40SEvgeny Pinchuk return -ENOMEM;
6855b5c4e40SEvgeny Pinchuk
6860f28cca8SRamesh Errabolu dev->kobj_p2plink = kobject_create_and_add("p2p_links", dev->kobj_node);
6870f28cca8SRamesh Errabolu if (!dev->kobj_p2plink)
6880f28cca8SRamesh Errabolu return -ENOMEM;
6890f28cca8SRamesh Errabolu
690f4757347SAmber Lin dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
691f4757347SAmber Lin if (!dev->kobj_perf)
692f4757347SAmber Lin return -ENOMEM;
693f4757347SAmber Lin
6945b5c4e40SEvgeny Pinchuk /*
6955b5c4e40SEvgeny Pinchuk * Creating sysfs files for node properties
6965b5c4e40SEvgeny Pinchuk */
6975b5c4e40SEvgeny Pinchuk dev->attr_gpuid.name = "gpu_id";
6985b5c4e40SEvgeny Pinchuk dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
6995b5c4e40SEvgeny Pinchuk sysfs_attr_init(&dev->attr_gpuid);
7005b5c4e40SEvgeny Pinchuk dev->attr_name.name = "name";
7015b5c4e40SEvgeny Pinchuk dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
7025b5c4e40SEvgeny Pinchuk sysfs_attr_init(&dev->attr_name);
7035b5c4e40SEvgeny Pinchuk dev->attr_props.name = "properties";
7045b5c4e40SEvgeny Pinchuk dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
7055b5c4e40SEvgeny Pinchuk sysfs_attr_init(&dev->attr_props);
7065b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
7075b5c4e40SEvgeny Pinchuk if (ret < 0)
7085b5c4e40SEvgeny Pinchuk return ret;
7095b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
7105b5c4e40SEvgeny Pinchuk if (ret < 0)
7115b5c4e40SEvgeny Pinchuk return ret;
7125b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
7135b5c4e40SEvgeny Pinchuk if (ret < 0)
7145b5c4e40SEvgeny Pinchuk return ret;
7155b5c4e40SEvgeny Pinchuk
7165b5c4e40SEvgeny Pinchuk i = 0;
7175b5c4e40SEvgeny Pinchuk list_for_each_entry(mem, &dev->mem_props, list) {
7185b5c4e40SEvgeny Pinchuk mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
7195b5c4e40SEvgeny Pinchuk if (!mem->kobj)
7205b5c4e40SEvgeny Pinchuk return -ENOMEM;
7215b5c4e40SEvgeny Pinchuk ret = kobject_init_and_add(mem->kobj, &mem_type,
7225b5c4e40SEvgeny Pinchuk dev->kobj_mem, "%d", i);
72320eca012SQiushi Wu if (ret < 0) {
72420eca012SQiushi Wu kobject_put(mem->kobj);
7255b5c4e40SEvgeny Pinchuk return ret;
72620eca012SQiushi Wu }
7275b5c4e40SEvgeny Pinchuk
7285b5c4e40SEvgeny Pinchuk mem->attr.name = "properties";
7295b5c4e40SEvgeny Pinchuk mem->attr.mode = KFD_SYSFS_FILE_MODE;
7305b5c4e40SEvgeny Pinchuk sysfs_attr_init(&mem->attr);
7315b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(mem->kobj, &mem->attr);
7325b5c4e40SEvgeny Pinchuk if (ret < 0)
7335b5c4e40SEvgeny Pinchuk return ret;
7345b5c4e40SEvgeny Pinchuk i++;
7355b5c4e40SEvgeny Pinchuk }
7365b5c4e40SEvgeny Pinchuk
7375b5c4e40SEvgeny Pinchuk i = 0;
7385b5c4e40SEvgeny Pinchuk list_for_each_entry(cache, &dev->cache_props, list) {
7395b5c4e40SEvgeny Pinchuk cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
7405b5c4e40SEvgeny Pinchuk if (!cache->kobj)
7415b5c4e40SEvgeny Pinchuk return -ENOMEM;
7425b5c4e40SEvgeny Pinchuk ret = kobject_init_and_add(cache->kobj, &cache_type,
7435b5c4e40SEvgeny Pinchuk dev->kobj_cache, "%d", i);
74420eca012SQiushi Wu if (ret < 0) {
74520eca012SQiushi Wu kobject_put(cache->kobj);
7465b5c4e40SEvgeny Pinchuk return ret;
74720eca012SQiushi Wu }
7485b5c4e40SEvgeny Pinchuk
7495b5c4e40SEvgeny Pinchuk cache->attr.name = "properties";
7505b5c4e40SEvgeny Pinchuk cache->attr.mode = KFD_SYSFS_FILE_MODE;
7515b5c4e40SEvgeny Pinchuk sysfs_attr_init(&cache->attr);
7525b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(cache->kobj, &cache->attr);
7535b5c4e40SEvgeny Pinchuk if (ret < 0)
7545b5c4e40SEvgeny Pinchuk return ret;
7555b5c4e40SEvgeny Pinchuk i++;
7565b5c4e40SEvgeny Pinchuk }
7575b5c4e40SEvgeny Pinchuk
7585b5c4e40SEvgeny Pinchuk i = 0;
7595b5c4e40SEvgeny Pinchuk list_for_each_entry(iolink, &dev->io_link_props, list) {
7605b5c4e40SEvgeny Pinchuk iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
7615b5c4e40SEvgeny Pinchuk if (!iolink->kobj)
7625b5c4e40SEvgeny Pinchuk return -ENOMEM;
7635b5c4e40SEvgeny Pinchuk ret = kobject_init_and_add(iolink->kobj, &iolink_type,
7645b5c4e40SEvgeny Pinchuk dev->kobj_iolink, "%d", i);
76520eca012SQiushi Wu if (ret < 0) {
76620eca012SQiushi Wu kobject_put(iolink->kobj);
7675b5c4e40SEvgeny Pinchuk return ret;
76820eca012SQiushi Wu }
7695b5c4e40SEvgeny Pinchuk
7705b5c4e40SEvgeny Pinchuk iolink->attr.name = "properties";
7715b5c4e40SEvgeny Pinchuk iolink->attr.mode = KFD_SYSFS_FILE_MODE;
7725b5c4e40SEvgeny Pinchuk sysfs_attr_init(&iolink->attr);
7735b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(iolink->kobj, &iolink->attr);
7745b5c4e40SEvgeny Pinchuk if (ret < 0)
7755b5c4e40SEvgeny Pinchuk return ret;
7765b5c4e40SEvgeny Pinchuk i++;
7775b5c4e40SEvgeny Pinchuk }
7785b5c4e40SEvgeny Pinchuk
7790f28cca8SRamesh Errabolu i = 0;
7800f28cca8SRamesh Errabolu list_for_each_entry(p2plink, &dev->p2p_link_props, list) {
7810f28cca8SRamesh Errabolu p2plink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
7820f28cca8SRamesh Errabolu if (!p2plink->kobj)
7830f28cca8SRamesh Errabolu return -ENOMEM;
7840f28cca8SRamesh Errabolu ret = kobject_init_and_add(p2plink->kobj, &iolink_type,
7850f28cca8SRamesh Errabolu dev->kobj_p2plink, "%d", i);
7860f28cca8SRamesh Errabolu if (ret < 0) {
7870f28cca8SRamesh Errabolu kobject_put(p2plink->kobj);
7880f28cca8SRamesh Errabolu return ret;
7890f28cca8SRamesh Errabolu }
7900f28cca8SRamesh Errabolu
7910f28cca8SRamesh Errabolu p2plink->attr.name = "properties";
7920f28cca8SRamesh Errabolu p2plink->attr.mode = KFD_SYSFS_FILE_MODE;
7931f9d1ff1SMukul Joshi sysfs_attr_init(&p2plink->attr);
7940f28cca8SRamesh Errabolu ret = sysfs_create_file(p2plink->kobj, &p2plink->attr);
7950f28cca8SRamesh Errabolu if (ret < 0)
7960f28cca8SRamesh Errabolu return ret;
7970f28cca8SRamesh Errabolu i++;
7980f28cca8SRamesh Errabolu }
7990f28cca8SRamesh Errabolu
800f4757347SAmber Lin /* All hardware blocks have the same number of attributes. */
8013f866f5fSGustavo A. R. Silva num_attrs = ARRAY_SIZE(perf_attr_iommu);
802f4757347SAmber Lin list_for_each_entry(perf, &dev->perf_props, list) {
803f4757347SAmber Lin perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
804f4757347SAmber Lin * num_attrs + sizeof(struct attribute_group),
805f4757347SAmber Lin GFP_KERNEL);
806f4757347SAmber Lin if (!perf->attr_group)
807f4757347SAmber Lin return -ENOMEM;
808f4757347SAmber Lin
809f4757347SAmber Lin attrs = (struct attribute **)(perf->attr_group + 1);
810f4757347SAmber Lin if (!strcmp(perf->block_name, "iommu")) {
811f4757347SAmber Lin /* Information of IOMMU's num_counters and counter_ids is shown
812f4757347SAmber Lin * under /sys/bus/event_source/devices/amd_iommu. We don't
813f4757347SAmber Lin * duplicate here.
814f4757347SAmber Lin */
815f4757347SAmber Lin perf_attr_iommu[0].data = perf->max_concurrent;
816f4757347SAmber Lin for (i = 0; i < num_attrs; i++)
817f4757347SAmber Lin attrs[i] = &perf_attr_iommu[i].attr.attr;
818f4757347SAmber Lin }
819f4757347SAmber Lin perf->attr_group->name = perf->block_name;
820f4757347SAmber Lin perf->attr_group->attrs = attrs;
821f4757347SAmber Lin ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
822f4757347SAmber Lin if (ret < 0)
823f4757347SAmber Lin return ret;
824f4757347SAmber Lin }
825f4757347SAmber Lin
8265b5c4e40SEvgeny Pinchuk return 0;
8275b5c4e40SEvgeny Pinchuk }
8285b5c4e40SEvgeny Pinchuk
8293a87177eSHarish Kasiviswanathan /* Called with write topology lock acquired */
kfd_build_sysfs_node_tree(void)8305b5c4e40SEvgeny Pinchuk static int kfd_build_sysfs_node_tree(void)
8315b5c4e40SEvgeny Pinchuk {
8325b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev;
8335b5c4e40SEvgeny Pinchuk int ret;
8345b5c4e40SEvgeny Pinchuk uint32_t i = 0;
8355b5c4e40SEvgeny Pinchuk
8365b5c4e40SEvgeny Pinchuk list_for_each_entry(dev, &topology_device_list, list) {
8378dfead6cSBen Goz ret = kfd_build_sysfs_node_entry(dev, i);
8385b5c4e40SEvgeny Pinchuk if (ret < 0)
8395b5c4e40SEvgeny Pinchuk return ret;
8405b5c4e40SEvgeny Pinchuk i++;
8415b5c4e40SEvgeny Pinchuk }
8425b5c4e40SEvgeny Pinchuk
8435b5c4e40SEvgeny Pinchuk return 0;
8445b5c4e40SEvgeny Pinchuk }
8455b5c4e40SEvgeny Pinchuk
8463a87177eSHarish Kasiviswanathan /* Called with write topology lock acquired */
kfd_remove_sysfs_node_tree(void)8475b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_node_tree(void)
8485b5c4e40SEvgeny Pinchuk {
8495b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev;
8505b5c4e40SEvgeny Pinchuk
8515b5c4e40SEvgeny Pinchuk list_for_each_entry(dev, &topology_device_list, list)
8525b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_node_entry(dev);
8535b5c4e40SEvgeny Pinchuk }
8545b5c4e40SEvgeny Pinchuk
kfd_topology_update_sysfs(void)8555b5c4e40SEvgeny Pinchuk static int kfd_topology_update_sysfs(void)
8565b5c4e40SEvgeny Pinchuk {
8575b5c4e40SEvgeny Pinchuk int ret;
8585b5c4e40SEvgeny Pinchuk
8594eacc26bSKent Russell if (!sys_props.kobj_topology) {
8605b5c4e40SEvgeny Pinchuk sys_props.kobj_topology =
8615b5c4e40SEvgeny Pinchuk kfd_alloc_struct(sys_props.kobj_topology);
8625b5c4e40SEvgeny Pinchuk if (!sys_props.kobj_topology)
8635b5c4e40SEvgeny Pinchuk return -ENOMEM;
8645b5c4e40SEvgeny Pinchuk
8655b5c4e40SEvgeny Pinchuk ret = kobject_init_and_add(sys_props.kobj_topology,
8665b5c4e40SEvgeny Pinchuk &sysprops_type, &kfd_device->kobj,
8675b5c4e40SEvgeny Pinchuk "topology");
86820eca012SQiushi Wu if (ret < 0) {
86920eca012SQiushi Wu kobject_put(sys_props.kobj_topology);
8705b5c4e40SEvgeny Pinchuk return ret;
87120eca012SQiushi Wu }
8725b5c4e40SEvgeny Pinchuk
8735b5c4e40SEvgeny Pinchuk sys_props.kobj_nodes = kobject_create_and_add("nodes",
8745b5c4e40SEvgeny Pinchuk sys_props.kobj_topology);
8755b5c4e40SEvgeny Pinchuk if (!sys_props.kobj_nodes)
8765b5c4e40SEvgeny Pinchuk return -ENOMEM;
8775b5c4e40SEvgeny Pinchuk
8785b5c4e40SEvgeny Pinchuk sys_props.attr_genid.name = "generation_id";
8795b5c4e40SEvgeny Pinchuk sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
8805b5c4e40SEvgeny Pinchuk sysfs_attr_init(&sys_props.attr_genid);
8815b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(sys_props.kobj_topology,
8825b5c4e40SEvgeny Pinchuk &sys_props.attr_genid);
8835b5c4e40SEvgeny Pinchuk if (ret < 0)
8845b5c4e40SEvgeny Pinchuk return ret;
8855b5c4e40SEvgeny Pinchuk
8865b5c4e40SEvgeny Pinchuk sys_props.attr_props.name = "system_properties";
8875b5c4e40SEvgeny Pinchuk sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
8885b5c4e40SEvgeny Pinchuk sysfs_attr_init(&sys_props.attr_props);
8895b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(sys_props.kobj_topology,
8905b5c4e40SEvgeny Pinchuk &sys_props.attr_props);
8915b5c4e40SEvgeny Pinchuk if (ret < 0)
8925b5c4e40SEvgeny Pinchuk return ret;
8935b5c4e40SEvgeny Pinchuk }
8945b5c4e40SEvgeny Pinchuk
8955b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_node_tree();
8965b5c4e40SEvgeny Pinchuk
8975b5c4e40SEvgeny Pinchuk return kfd_build_sysfs_node_tree();
8985b5c4e40SEvgeny Pinchuk }
8995b5c4e40SEvgeny Pinchuk
kfd_topology_release_sysfs(void)9005b5c4e40SEvgeny Pinchuk static void kfd_topology_release_sysfs(void)
9015b5c4e40SEvgeny Pinchuk {
9025b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_node_tree();
9035b5c4e40SEvgeny Pinchuk if (sys_props.kobj_topology) {
9045b5c4e40SEvgeny Pinchuk sysfs_remove_file(sys_props.kobj_topology,
9055b5c4e40SEvgeny Pinchuk &sys_props.attr_genid);
9065b5c4e40SEvgeny Pinchuk sysfs_remove_file(sys_props.kobj_topology,
9075b5c4e40SEvgeny Pinchuk &sys_props.attr_props);
9085b5c4e40SEvgeny Pinchuk if (sys_props.kobj_nodes) {
9095b5c4e40SEvgeny Pinchuk kobject_del(sys_props.kobj_nodes);
9105b5c4e40SEvgeny Pinchuk kobject_put(sys_props.kobj_nodes);
91116b9201cSOded Gabbay sys_props.kobj_nodes = NULL;
9125b5c4e40SEvgeny Pinchuk }
9135b5c4e40SEvgeny Pinchuk kobject_del(sys_props.kobj_topology);
9145b5c4e40SEvgeny Pinchuk kobject_put(sys_props.kobj_topology);
91516b9201cSOded Gabbay sys_props.kobj_topology = NULL;
9165b5c4e40SEvgeny Pinchuk }
9175b5c4e40SEvgeny Pinchuk }
9185b5c4e40SEvgeny Pinchuk
9194f449311SHarish Kasiviswanathan /* Called with write topology_lock acquired */
kfd_topology_update_device_list(struct list_head * temp_list,struct list_head * master_list)9204f449311SHarish Kasiviswanathan static void kfd_topology_update_device_list(struct list_head *temp_list,
9214f449311SHarish Kasiviswanathan struct list_head *master_list)
9224f449311SHarish Kasiviswanathan {
9234f449311SHarish Kasiviswanathan while (!list_empty(temp_list)) {
9244f449311SHarish Kasiviswanathan list_move_tail(temp_list->next, master_list);
9254f449311SHarish Kasiviswanathan sys_props.num_devices++;
9264f449311SHarish Kasiviswanathan }
9274f449311SHarish Kasiviswanathan }
9284f449311SHarish Kasiviswanathan
kfd_debug_print_topology(void)929520b8fb7SFelix Kuehling static void kfd_debug_print_topology(void)
930520b8fb7SFelix Kuehling {
931520b8fb7SFelix Kuehling struct kfd_topology_device *dev;
932520b8fb7SFelix Kuehling
933520b8fb7SFelix Kuehling down_read(&topology_lock);
934520b8fb7SFelix Kuehling
935520b8fb7SFelix Kuehling dev = list_last_entry(&topology_device_list,
936520b8fb7SFelix Kuehling struct kfd_topology_device, list);
937520b8fb7SFelix Kuehling if (dev) {
938520b8fb7SFelix Kuehling if (dev->node_props.cpu_cores_count &&
939520b8fb7SFelix Kuehling dev->node_props.simd_count) {
940520b8fb7SFelix Kuehling pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
941520b8fb7SFelix Kuehling dev->node_props.device_id,
942520b8fb7SFelix Kuehling dev->node_props.vendor_id);
943520b8fb7SFelix Kuehling } else if (dev->node_props.cpu_cores_count)
944520b8fb7SFelix Kuehling pr_info("Topology: Add CPU node\n");
945520b8fb7SFelix Kuehling else if (dev->node_props.simd_count)
946520b8fb7SFelix Kuehling pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
947520b8fb7SFelix Kuehling dev->node_props.device_id,
948520b8fb7SFelix Kuehling dev->node_props.vendor_id);
949520b8fb7SFelix Kuehling }
950520b8fb7SFelix Kuehling up_read(&topology_lock);
951520b8fb7SFelix Kuehling }
952520b8fb7SFelix Kuehling
953520b8fb7SFelix Kuehling /* Helper function for intializing platform_xx members of
954520b8fb7SFelix Kuehling * kfd_system_properties. Uses OEM info from the last CPU/APU node.
955520b8fb7SFelix Kuehling */
kfd_update_system_properties(void)956520b8fb7SFelix Kuehling static void kfd_update_system_properties(void)
957520b8fb7SFelix Kuehling {
958520b8fb7SFelix Kuehling struct kfd_topology_device *dev;
959520b8fb7SFelix Kuehling
960520b8fb7SFelix Kuehling down_read(&topology_lock);
961520b8fb7SFelix Kuehling dev = list_last_entry(&topology_device_list,
962520b8fb7SFelix Kuehling struct kfd_topology_device, list);
963520b8fb7SFelix Kuehling if (dev) {
96410f624efSMichael Chen sys_props.platform_id = dev->oem_id64;
965520b8fb7SFelix Kuehling sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
966520b8fb7SFelix Kuehling sys_props.platform_rev = dev->oem_revision;
967520b8fb7SFelix Kuehling }
968520b8fb7SFelix Kuehling up_read(&topology_lock);
969520b8fb7SFelix Kuehling }
970520b8fb7SFelix Kuehling
find_system_memory(const struct dmi_header * dm,void * private)971520b8fb7SFelix Kuehling static void find_system_memory(const struct dmi_header *dm,
972520b8fb7SFelix Kuehling void *private)
973520b8fb7SFelix Kuehling {
974520b8fb7SFelix Kuehling struct kfd_mem_properties *mem;
975520b8fb7SFelix Kuehling u16 mem_width, mem_clock;
976520b8fb7SFelix Kuehling struct kfd_topology_device *kdev =
977520b8fb7SFelix Kuehling (struct kfd_topology_device *)private;
978520b8fb7SFelix Kuehling const u8 *dmi_data = (const u8 *)(dm + 1);
979520b8fb7SFelix Kuehling
980520b8fb7SFelix Kuehling if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
981520b8fb7SFelix Kuehling mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
982520b8fb7SFelix Kuehling mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
983520b8fb7SFelix Kuehling list_for_each_entry(mem, &kdev->mem_props, list) {
984520b8fb7SFelix Kuehling if (mem_width != 0xFFFF && mem_width != 0)
985520b8fb7SFelix Kuehling mem->width = mem_width;
986520b8fb7SFelix Kuehling if (mem_clock != 0)
987520b8fb7SFelix Kuehling mem->mem_clk_max = mem_clock;
988520b8fb7SFelix Kuehling }
989520b8fb7SFelix Kuehling }
990520b8fb7SFelix Kuehling }
991f4757347SAmber Lin
992520b8fb7SFelix Kuehling /* kfd_add_non_crat_information - Add information that is not currently
993520b8fb7SFelix Kuehling * defined in CRAT but is necessary for KFD topology
994520b8fb7SFelix Kuehling * @dev - topology device to which addition info is added
995520b8fb7SFelix Kuehling */
kfd_add_non_crat_information(struct kfd_topology_device * kdev)996520b8fb7SFelix Kuehling static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
997520b8fb7SFelix Kuehling {
998520b8fb7SFelix Kuehling /* Check if CPU only node. */
999520b8fb7SFelix Kuehling if (!kdev->gpu) {
1000520b8fb7SFelix Kuehling /* Add system memory information */
1001520b8fb7SFelix Kuehling dmi_walk(find_system_memory, kdev);
1002520b8fb7SFelix Kuehling }
1003520b8fb7SFelix Kuehling /* TODO: For GPU node, rearrange code from kfd_topology_add_device */
1004520b8fb7SFelix Kuehling }
1005520b8fb7SFelix Kuehling
kfd_topology_init(void)10065b5c4e40SEvgeny Pinchuk int kfd_topology_init(void)
10075b5c4e40SEvgeny Pinchuk {
100816b9201cSOded Gabbay void *crat_image = NULL;
10095b5c4e40SEvgeny Pinchuk size_t image_size = 0;
10105b5c4e40SEvgeny Pinchuk int ret;
10114f449311SHarish Kasiviswanathan struct list_head temp_topology_device_list;
1012520b8fb7SFelix Kuehling int cpu_only_node = 0;
1013520b8fb7SFelix Kuehling struct kfd_topology_device *kdev;
1014520b8fb7SFelix Kuehling int proximity_domain;
10155b5c4e40SEvgeny Pinchuk
10164f449311SHarish Kasiviswanathan /* topology_device_list - Master list of all topology devices
10174f449311SHarish Kasiviswanathan * temp_topology_device_list - temporary list created while parsing CRAT
10184f449311SHarish Kasiviswanathan * or VCRAT. Once parsing is complete the contents of list is moved to
10194f449311SHarish Kasiviswanathan * topology_device_list
10205b5c4e40SEvgeny Pinchuk */
10214f449311SHarish Kasiviswanathan
10224f449311SHarish Kasiviswanathan /* Initialize the head for the both the lists */
10235b5c4e40SEvgeny Pinchuk INIT_LIST_HEAD(&topology_device_list);
10244f449311SHarish Kasiviswanathan INIT_LIST_HEAD(&temp_topology_device_list);
10255b5c4e40SEvgeny Pinchuk init_rwsem(&topology_lock);
10265b5c4e40SEvgeny Pinchuk
10275b5c4e40SEvgeny Pinchuk memset(&sys_props, 0, sizeof(sys_props));
10285b5c4e40SEvgeny Pinchuk
1029520b8fb7SFelix Kuehling /* Proximity domains in ACPI CRAT tables start counting at
1030520b8fb7SFelix Kuehling * 0. The same should be true for virtual CRAT tables created
1031520b8fb7SFelix Kuehling * at this stage. GPUs added later in kfd_topology_add_device
1032520b8fb7SFelix Kuehling * use a counter.
1033520b8fb7SFelix Kuehling */
1034520b8fb7SFelix Kuehling proximity_domain = 0;
1035520b8fb7SFelix Kuehling
1036520b8fb7SFelix Kuehling ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
1037520b8fb7SFelix Kuehling COMPUTE_UNIT_CPU, NULL,
1038520b8fb7SFelix Kuehling proximity_domain);
1039520b8fb7SFelix Kuehling cpu_only_node = 1;
1040520b8fb7SFelix Kuehling if (ret) {
1041520b8fb7SFelix Kuehling pr_err("Error creating VCRAT table for CPU\n");
1042520b8fb7SFelix Kuehling return ret;
1043520b8fb7SFelix Kuehling }
1044520b8fb7SFelix Kuehling
1045520b8fb7SFelix Kuehling ret = kfd_parse_crat_table(crat_image,
1046520b8fb7SFelix Kuehling &temp_topology_device_list,
1047520b8fb7SFelix Kuehling proximity_domain);
1048520b8fb7SFelix Kuehling if (ret) {
1049520b8fb7SFelix Kuehling pr_err("Error parsing VCRAT table for CPU\n");
10508e05247dSHarish Kasiviswanathan goto err;
1051520b8fb7SFelix Kuehling }
10525b5c4e40SEvgeny Pinchuk
1053f4757347SAmber Lin kdev = list_first_entry(&temp_topology_device_list,
1054f4757347SAmber Lin struct kfd_topology_device, list);
1055f4757347SAmber Lin
10565b5c4e40SEvgeny Pinchuk down_write(&topology_lock);
10574f449311SHarish Kasiviswanathan kfd_topology_update_device_list(&temp_topology_device_list,
10584f449311SHarish Kasiviswanathan &topology_device_list);
105946d18d51SMukul Joshi topology_crat_proximity_domain = sys_props.num_devices-1;
10605b5c4e40SEvgeny Pinchuk ret = kfd_topology_update_sysfs();
10615b5c4e40SEvgeny Pinchuk up_write(&topology_lock);
10628e05247dSHarish Kasiviswanathan
10634f449311SHarish Kasiviswanathan if (!ret) {
10644f449311SHarish Kasiviswanathan sys_props.generation_count++;
1065520b8fb7SFelix Kuehling kfd_update_system_properties();
1066520b8fb7SFelix Kuehling kfd_debug_print_topology();
10674f449311SHarish Kasiviswanathan } else
10688e05247dSHarish Kasiviswanathan pr_err("Failed to update topology in sysfs ret=%d\n", ret);
10695b5c4e40SEvgeny Pinchuk
1070520b8fb7SFelix Kuehling /* For nodes with GPU, this information gets added
1071520b8fb7SFelix Kuehling * when GPU is detected (kfd_topology_add_device).
1072520b8fb7SFelix Kuehling */
1073520b8fb7SFelix Kuehling if (cpu_only_node) {
1074520b8fb7SFelix Kuehling /* Add additional information to CPU only node created above */
1075520b8fb7SFelix Kuehling down_write(&topology_lock);
1076520b8fb7SFelix Kuehling kdev = list_first_entry(&topology_device_list,
1077520b8fb7SFelix Kuehling struct kfd_topology_device, list);
1078520b8fb7SFelix Kuehling up_write(&topology_lock);
1079520b8fb7SFelix Kuehling kfd_add_non_crat_information(kdev);
1080520b8fb7SFelix Kuehling }
1081520b8fb7SFelix Kuehling
10825b5c4e40SEvgeny Pinchuk err:
10838e05247dSHarish Kasiviswanathan kfd_destroy_crat_image(crat_image);
10845b5c4e40SEvgeny Pinchuk return ret;
10855b5c4e40SEvgeny Pinchuk }
10865b5c4e40SEvgeny Pinchuk
kfd_topology_shutdown(void)10875b5c4e40SEvgeny Pinchuk void kfd_topology_shutdown(void)
10885b5c4e40SEvgeny Pinchuk {
10894f449311SHarish Kasiviswanathan down_write(&topology_lock);
10905b5c4e40SEvgeny Pinchuk kfd_topology_release_sysfs();
10915b5c4e40SEvgeny Pinchuk kfd_release_live_view();
10924f449311SHarish Kasiviswanathan up_write(&topology_lock);
10935b5c4e40SEvgeny Pinchuk }
10945b5c4e40SEvgeny Pinchuk
kfd_generate_gpu_id(struct kfd_node * gpu)10958dc1db31SMukul Joshi static uint32_t kfd_generate_gpu_id(struct kfd_node *gpu)
10965b5c4e40SEvgeny Pinchuk {
10973ed181b8SHarish Kasiviswanathan uint32_t gpu_id;
109874c5b85dSMukul Joshi uint32_t buf[8];
1099585f0e6cSEdward O'Callaghan uint64_t local_mem_size;
11003ed181b8SHarish Kasiviswanathan struct kfd_topology_device *dev;
11013ed181b8SHarish Kasiviswanathan bool is_unique;
11023ed181b8SHarish Kasiviswanathan uint8_t *crc_buf;
11035b5c4e40SEvgeny Pinchuk
11045b5c4e40SEvgeny Pinchuk if (!gpu)
11055b5c4e40SEvgeny Pinchuk return 0;
11065b5c4e40SEvgeny Pinchuk
11073ed181b8SHarish Kasiviswanathan crc_buf = (uint8_t *)&buf;
1108315e29ecSMukul Joshi local_mem_size = gpu->local_mem_info.local_mem_size_private +
1109315e29ecSMukul Joshi gpu->local_mem_info.local_mem_size_public;
1110d69a3b76SMukul Joshi buf[0] = gpu->adev->pdev->devfn;
1111d69a3b76SMukul Joshi buf[1] = gpu->adev->pdev->subsystem_vendor |
1112d69a3b76SMukul Joshi (gpu->adev->pdev->subsystem_device << 16);
1113d69a3b76SMukul Joshi buf[2] = pci_domain_nr(gpu->adev->pdev->bus);
1114d69a3b76SMukul Joshi buf[3] = gpu->adev->pdev->device;
1115d69a3b76SMukul Joshi buf[4] = gpu->adev->pdev->bus->number;
1116585f0e6cSEdward O'Callaghan buf[5] = lower_32_bits(local_mem_size);
1117585f0e6cSEdward O'Callaghan buf[6] = upper_32_bits(local_mem_size);
1118c4050ff1SLijo Lazar buf[7] = (ffs(gpu->xcc_mask) - 1) | (NUM_XCC(gpu->xcc_mask) << 16);
11195b5c4e40SEvgeny Pinchuk
11203ed181b8SHarish Kasiviswanathan gpu_id = crc16(0, crc_buf, sizeof(buf)) &
11213ed181b8SHarish Kasiviswanathan ((1 << KFD_GPU_ID_HASH_WIDTH) - 1);
11225b5c4e40SEvgeny Pinchuk
11233ed181b8SHarish Kasiviswanathan /* There is a very small possibility when generating a
11243ed181b8SHarish Kasiviswanathan * 16 (KFD_GPU_ID_HASH_WIDTH) bit value from 8 word buffer
11253ed181b8SHarish Kasiviswanathan * that the value could be 0 or non-unique. So, check if
11263ed181b8SHarish Kasiviswanathan * it is unique and non-zero. If not unique increment till
11273ed181b8SHarish Kasiviswanathan * unique one is found. In case of overflow, restart from 1
11283ed181b8SHarish Kasiviswanathan */
11293ed181b8SHarish Kasiviswanathan
11303ed181b8SHarish Kasiviswanathan down_read(&topology_lock);
11313ed181b8SHarish Kasiviswanathan do {
11323ed181b8SHarish Kasiviswanathan is_unique = true;
11333ed181b8SHarish Kasiviswanathan if (!gpu_id)
11343ed181b8SHarish Kasiviswanathan gpu_id = 1;
11353ed181b8SHarish Kasiviswanathan list_for_each_entry(dev, &topology_device_list, list) {
11363ed181b8SHarish Kasiviswanathan if (dev->gpu && dev->gpu_id == gpu_id) {
11373ed181b8SHarish Kasiviswanathan is_unique = false;
11383ed181b8SHarish Kasiviswanathan break;
11393ed181b8SHarish Kasiviswanathan }
11403ed181b8SHarish Kasiviswanathan }
11413ed181b8SHarish Kasiviswanathan if (unlikely(!is_unique))
11423ed181b8SHarish Kasiviswanathan gpu_id = (gpu_id + 1) &
11433ed181b8SHarish Kasiviswanathan ((1 << KFD_GPU_ID_HASH_WIDTH) - 1);
11443ed181b8SHarish Kasiviswanathan } while (!is_unique);
11453ed181b8SHarish Kasiviswanathan up_read(&topology_lock);
11463ed181b8SHarish Kasiviswanathan
11473ed181b8SHarish Kasiviswanathan return gpu_id;
11485b5c4e40SEvgeny Pinchuk }
11493a87177eSHarish Kasiviswanathan /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
11503a87177eSHarish Kasiviswanathan * the GPU device is not already present in the topology device
11513a87177eSHarish Kasiviswanathan * list then return NULL. This means a new topology device has to
11523a87177eSHarish Kasiviswanathan * be created for this GPU.
11533a87177eSHarish Kasiviswanathan */
kfd_assign_gpu(struct kfd_node * gpu)11548dc1db31SMukul Joshi static struct kfd_topology_device *kfd_assign_gpu(struct kfd_node *gpu)
11555b5c4e40SEvgeny Pinchuk {
11565b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev;
115716b9201cSOded Gabbay struct kfd_topology_device *out_dev = NULL;
1158171bc67eSHarish Kasiviswanathan struct kfd_mem_properties *mem;
1159171bc67eSHarish Kasiviswanathan struct kfd_cache_properties *cache;
1160171bc67eSHarish Kasiviswanathan struct kfd_iolink_properties *iolink;
11610f28cca8SRamesh Errabolu struct kfd_iolink_properties *p2plink;
11625b5c4e40SEvgeny Pinchuk
1163b8fe0524SFelix Kuehling list_for_each_entry(dev, &topology_device_list, list) {
1164b8fe0524SFelix Kuehling /* Discrete GPUs need their own topology device list
1165b8fe0524SFelix Kuehling * entries. Don't assign them to CPU/APU nodes.
1166b8fe0524SFelix Kuehling */
1167c99a2e7aSAlex Deucher if (dev->node_props.cpu_cores_count)
1168b8fe0524SFelix Kuehling continue;
1169b8fe0524SFelix Kuehling
11704eacc26bSKent Russell if (!dev->gpu && (dev->node_props.simd_count > 0)) {
11715b5c4e40SEvgeny Pinchuk dev->gpu = gpu;
11725b5c4e40SEvgeny Pinchuk out_dev = dev;
1173171bc67eSHarish Kasiviswanathan
1174171bc67eSHarish Kasiviswanathan list_for_each_entry(mem, &dev->mem_props, list)
1175171bc67eSHarish Kasiviswanathan mem->gpu = dev->gpu;
1176171bc67eSHarish Kasiviswanathan list_for_each_entry(cache, &dev->cache_props, list)
1177171bc67eSHarish Kasiviswanathan cache->gpu = dev->gpu;
1178171bc67eSHarish Kasiviswanathan list_for_each_entry(iolink, &dev->io_link_props, list)
1179171bc67eSHarish Kasiviswanathan iolink->gpu = dev->gpu;
11800f28cca8SRamesh Errabolu list_for_each_entry(p2plink, &dev->p2p_link_props, list)
11810f28cca8SRamesh Errabolu p2plink->gpu = dev->gpu;
11825b5c4e40SEvgeny Pinchuk break;
11835b5c4e40SEvgeny Pinchuk }
1184b8fe0524SFelix Kuehling }
11855b5c4e40SEvgeny Pinchuk return out_dev;
11865b5c4e40SEvgeny Pinchuk }
11875b5c4e40SEvgeny Pinchuk
kfd_notify_gpu_change(uint32_t gpu_id,int arrival)11885b5c4e40SEvgeny Pinchuk static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
11895b5c4e40SEvgeny Pinchuk {
11905b5c4e40SEvgeny Pinchuk /*
11915b5c4e40SEvgeny Pinchuk * TODO: Generate an event for thunk about the arrival/removal
11925b5c4e40SEvgeny Pinchuk * of the GPU
11935b5c4e40SEvgeny Pinchuk */
11945b5c4e40SEvgeny Pinchuk }
11955b5c4e40SEvgeny Pinchuk
11963a87177eSHarish Kasiviswanathan /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
11973a87177eSHarish Kasiviswanathan * patch this after CRAT parsing.
11983a87177eSHarish Kasiviswanathan */
kfd_fill_mem_clk_max_info(struct kfd_topology_device * dev)11993a87177eSHarish Kasiviswanathan static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
12003a87177eSHarish Kasiviswanathan {
12013a87177eSHarish Kasiviswanathan struct kfd_mem_properties *mem;
12023a87177eSHarish Kasiviswanathan struct kfd_local_mem_info local_mem_info;
12033a87177eSHarish Kasiviswanathan
12043a87177eSHarish Kasiviswanathan if (!dev)
12053a87177eSHarish Kasiviswanathan return;
12063a87177eSHarish Kasiviswanathan
12073a87177eSHarish Kasiviswanathan /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
12083a87177eSHarish Kasiviswanathan * single bank of VRAM local memory.
12093a87177eSHarish Kasiviswanathan * for dGPUs - VCRAT reports only one bank of Local Memory
12103a87177eSHarish Kasiviswanathan * for APUs - If CRAT from ACPI reports more than one bank, then
12113a87177eSHarish Kasiviswanathan * all the banks will report the same mem_clk_max information
12123a87177eSHarish Kasiviswanathan */
1213315e29ecSMukul Joshi amdgpu_amdkfd_get_local_mem_info(dev->gpu->adev, &local_mem_info,
12149a3ce1a7SHawking Zhang dev->gpu->xcp);
12153a87177eSHarish Kasiviswanathan
12163a87177eSHarish Kasiviswanathan list_for_each_entry(mem, &dev->mem_props, list)
12173a87177eSHarish Kasiviswanathan mem->mem_clk_max = local_mem_info.mem_clk_max;
12183a87177eSHarish Kasiviswanathan }
12193a87177eSHarish Kasiviswanathan
kfd_set_iolink_no_atomics(struct kfd_topology_device * dev,struct kfd_topology_device * target_gpu_dev,struct kfd_iolink_properties * link)1220bdd24657SJonathan Kim static void kfd_set_iolink_no_atomics(struct kfd_topology_device *dev,
1221bdd24657SJonathan Kim struct kfd_topology_device *target_gpu_dev,
1222bdd24657SJonathan Kim struct kfd_iolink_properties *link)
12233a87177eSHarish Kasiviswanathan {
1224bdd24657SJonathan Kim /* xgmi always supports atomics between links. */
1225bdd24657SJonathan Kim if (link->iolink_type == CRAT_IOLINK_TYPE_XGMI)
12263a87177eSHarish Kasiviswanathan return;
12273a87177eSHarish Kasiviswanathan
1228bdd24657SJonathan Kim /* check pcie support to set cpu(dev) flags for target_gpu_dev link. */
1229bdd24657SJonathan Kim if (target_gpu_dev) {
1230bdd24657SJonathan Kim uint32_t cap;
1231bdd24657SJonathan Kim
1232d69a3b76SMukul Joshi pcie_capability_read_dword(target_gpu_dev->gpu->adev->pdev,
1233d35f00d8SEric Huang PCI_EXP_DEVCAP2, &cap);
1234d35f00d8SEric Huang
1235d35f00d8SEric Huang if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1236d35f00d8SEric Huang PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1237bdd24657SJonathan Kim link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
12383a87177eSHarish Kasiviswanathan CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1239bdd24657SJonathan Kim /* set gpu (dev) flags. */
1240bdd24657SJonathan Kim } else {
12418dc1db31SMukul Joshi if (!dev->gpu->kfd->pci_atomic_requested ||
12427eb0502aSGraham Sider dev->gpu->adev->asic_type == CHIP_HAWAII)
1243bdd24657SJonathan Kim link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1244d35f00d8SEric Huang CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1245deb68983SJonathan Kim }
1246bdd24657SJonathan Kim }
1247bdd24657SJonathan Kim
kfd_set_iolink_non_coherent(struct kfd_topology_device * to_dev,struct kfd_iolink_properties * outbound_link,struct kfd_iolink_properties * inbound_link)1248c9cfbf7fSEric Huang static void kfd_set_iolink_non_coherent(struct kfd_topology_device *to_dev,
1249c9cfbf7fSEric Huang struct kfd_iolink_properties *outbound_link,
1250c9cfbf7fSEric Huang struct kfd_iolink_properties *inbound_link)
1251c9cfbf7fSEric Huang {
1252c9cfbf7fSEric Huang /* CPU -> GPU with PCIe */
1253c9cfbf7fSEric Huang if (!to_dev->gpu &&
1254c9cfbf7fSEric Huang inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
1255c9cfbf7fSEric Huang inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1256c9cfbf7fSEric Huang
1257c9cfbf7fSEric Huang if (to_dev->gpu) {
1258c9cfbf7fSEric Huang /* GPU <-> GPU with PCIe and
1259c9cfbf7fSEric Huang * Vega20 with XGMI
1260c9cfbf7fSEric Huang */
1261c9cfbf7fSEric Huang if (inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS ||
1262c9cfbf7fSEric Huang (inbound_link->iolink_type == CRAT_IOLINK_TYPE_XGMI &&
1263046e674bSGraham Sider KFD_GC_VERSION(to_dev->gpu) == IP_VERSION(9, 4, 0))) {
1264c9cfbf7fSEric Huang outbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1265c9cfbf7fSEric Huang inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1266c9cfbf7fSEric Huang }
1267c9cfbf7fSEric Huang }
1268c9cfbf7fSEric Huang }
1269c9cfbf7fSEric Huang
1270e06b71b2SJonathan Kim #define REC_SDMA_NUM_GPU 8
1271e06b71b2SJonathan Kim static const int rec_sdma_eng_map[REC_SDMA_NUM_GPU][REC_SDMA_NUM_GPU] = {
1272e06b71b2SJonathan Kim { -1, 14, 12, 2, 4, 8, 10, 6 },
1273e06b71b2SJonathan Kim { 14, -1, 2, 10, 8, 4, 6, 12 },
1274e06b71b2SJonathan Kim { 10, 2, -1, 12, 14, 6, 4, 8 },
1275e06b71b2SJonathan Kim { 2, 12, 10, -1, 6, 14, 8, 4 },
1276e06b71b2SJonathan Kim { 4, 8, 14, 6, -1, 10, 12, 2 },
1277e06b71b2SJonathan Kim { 8, 4, 6, 14, 12, -1, 2, 10 },
1278e06b71b2SJonathan Kim { 10, 6, 4, 8, 12, 2, -1, 14 },
1279e06b71b2SJonathan Kim { 6, 12, 8, 4, 2, 10, 14, -1 }};
1280e06b71b2SJonathan Kim
kfd_set_recommended_sdma_engines(struct kfd_topology_device * to_dev,struct kfd_iolink_properties * outbound_link,struct kfd_iolink_properties * inbound_link)1281e06b71b2SJonathan Kim static void kfd_set_recommended_sdma_engines(struct kfd_topology_device *to_dev,
1282e06b71b2SJonathan Kim struct kfd_iolink_properties *outbound_link,
1283e06b71b2SJonathan Kim struct kfd_iolink_properties *inbound_link)
1284e06b71b2SJonathan Kim {
1285e06b71b2SJonathan Kim struct kfd_node *gpu = outbound_link->gpu;
1286e06b71b2SJonathan Kim struct amdgpu_device *adev = gpu->adev;
1287e06b71b2SJonathan Kim int num_xgmi_nodes = adev->gmc.xgmi.num_physical_nodes;
1288e06b71b2SJonathan Kim bool support_rec_eng = !amdgpu_sriov_vf(adev) && to_dev->gpu &&
128970f83e77SJonathan Kim adev->aid_mask && num_xgmi_nodes && gpu->kfd->num_nodes == 1 &&
129070f83e77SJonathan Kim kfd_get_num_xgmi_sdma_engines(gpu) >= 14 &&
1291e06b71b2SJonathan Kim (!(adev->flags & AMD_IS_APU) && num_xgmi_nodes == 8);
1292e06b71b2SJonathan Kim
1293e06b71b2SJonathan Kim if (support_rec_eng) {
1294e06b71b2SJonathan Kim int src_socket_id = adev->gmc.xgmi.physical_node_id;
1295e06b71b2SJonathan Kim int dst_socket_id = to_dev->gpu->adev->gmc.xgmi.physical_node_id;
1296e06b71b2SJonathan Kim
1297e06b71b2SJonathan Kim outbound_link->rec_sdma_eng_id_mask =
1298e06b71b2SJonathan Kim 1 << rec_sdma_eng_map[src_socket_id][dst_socket_id];
1299e06b71b2SJonathan Kim inbound_link->rec_sdma_eng_id_mask =
1300e06b71b2SJonathan Kim 1 << rec_sdma_eng_map[dst_socket_id][src_socket_id];
1301e06b71b2SJonathan Kim } else {
1302e06b71b2SJonathan Kim int num_sdma_eng = kfd_get_num_sdma_engines(gpu);
1303e06b71b2SJonathan Kim int i, eng_offset = 0;
1304e06b71b2SJonathan Kim
1305e06b71b2SJonathan Kim if (outbound_link->iolink_type == CRAT_IOLINK_TYPE_XGMI &&
1306e06b71b2SJonathan Kim kfd_get_num_xgmi_sdma_engines(gpu) && to_dev->gpu) {
1307e06b71b2SJonathan Kim eng_offset = num_sdma_eng;
1308e06b71b2SJonathan Kim num_sdma_eng = kfd_get_num_xgmi_sdma_engines(gpu);
1309e06b71b2SJonathan Kim }
1310e06b71b2SJonathan Kim
1311e06b71b2SJonathan Kim for (i = 0; i < num_sdma_eng; i++) {
1312e06b71b2SJonathan Kim outbound_link->rec_sdma_eng_id_mask |= (1 << (i + eng_offset));
1313e06b71b2SJonathan Kim inbound_link->rec_sdma_eng_id_mask |= (1 << (i + eng_offset));
1314e06b71b2SJonathan Kim }
1315e06b71b2SJonathan Kim }
1316e06b71b2SJonathan Kim }
1317e06b71b2SJonathan Kim
kfd_fill_iolink_non_crat_info(struct kfd_topology_device * dev)1318bdd24657SJonathan Kim static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
1319bdd24657SJonathan Kim {
1320bdd24657SJonathan Kim struct kfd_iolink_properties *link, *inbound_link;
1321bdd24657SJonathan Kim struct kfd_topology_device *peer_dev;
1322bdd24657SJonathan Kim
1323bdd24657SJonathan Kim if (!dev || !dev->gpu)
1324bdd24657SJonathan Kim return;
1325d35f00d8SEric Huang
1326d35f00d8SEric Huang /* GPU only creates direct links so apply flags setting to all */
1327d35f00d8SEric Huang list_for_each_entry(link, &dev->io_link_props, list) {
1328bdd24657SJonathan Kim link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1329bdd24657SJonathan Kim kfd_set_iolink_no_atomics(dev, NULL, link);
1330bdd24657SJonathan Kim peer_dev = kfd_topology_device_by_proximity_domain(
1331d35f00d8SEric Huang link->node_to);
1332bdd24657SJonathan Kim
1333bdd24657SJonathan Kim if (!peer_dev)
1334bdd24657SJonathan Kim continue;
1335bdd24657SJonathan Kim
1336a0af5dbdSJonathan Kim /* Include the CPU peer in GPU hive if connected over xGMI. */
1337b2ef2fdfSRajneesh Bhardwaj if (!peer_dev->gpu &&
13381698e200SJonathan Kim link->iolink_type == CRAT_IOLINK_TYPE_XGMI) {
13391698e200SJonathan Kim /*
13401698e200SJonathan Kim * If the GPU is not part of a GPU hive, use its pci
13411698e200SJonathan Kim * device location as the hive ID to bind with the CPU.
13421698e200SJonathan Kim */
13431698e200SJonathan Kim if (!dev->node_props.hive_id)
13441698e200SJonathan Kim dev->node_props.hive_id = pci_dev_id(dev->gpu->adev->pdev);
1345a0af5dbdSJonathan Kim peer_dev->node_props.hive_id = dev->node_props.hive_id;
13461698e200SJonathan Kim }
1347a0af5dbdSJonathan Kim
1348bdd24657SJonathan Kim list_for_each_entry(inbound_link, &peer_dev->io_link_props,
1349bdd24657SJonathan Kim list) {
1350bdd24657SJonathan Kim if (inbound_link->node_to != link->node_from)
1351bdd24657SJonathan Kim continue;
1352bdd24657SJonathan Kim
1353bdd24657SJonathan Kim inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1354bdd24657SJonathan Kim kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
1355c9cfbf7fSEric Huang kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
1356e06b71b2SJonathan Kim kfd_set_recommended_sdma_engines(peer_dev, link, inbound_link);
1357d35f00d8SEric Huang }
1358d35f00d8SEric Huang }
13590f28cca8SRamesh Errabolu
13600f28cca8SRamesh Errabolu /* Create indirect links so apply flags setting to all */
13610f28cca8SRamesh Errabolu list_for_each_entry(link, &dev->p2p_link_props, list) {
13620f28cca8SRamesh Errabolu link->flags = CRAT_IOLINK_FLAGS_ENABLED;
13630f28cca8SRamesh Errabolu kfd_set_iolink_no_atomics(dev, NULL, link);
13640f28cca8SRamesh Errabolu peer_dev = kfd_topology_device_by_proximity_domain(
13650f28cca8SRamesh Errabolu link->node_to);
13660f28cca8SRamesh Errabolu
13670f28cca8SRamesh Errabolu if (!peer_dev)
13680f28cca8SRamesh Errabolu continue;
13690f28cca8SRamesh Errabolu
13700f28cca8SRamesh Errabolu list_for_each_entry(inbound_link, &peer_dev->p2p_link_props,
13710f28cca8SRamesh Errabolu list) {
13720f28cca8SRamesh Errabolu if (inbound_link->node_to != link->node_from)
13730f28cca8SRamesh Errabolu continue;
13740f28cca8SRamesh Errabolu
13750f28cca8SRamesh Errabolu inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
13760f28cca8SRamesh Errabolu kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
13770f28cca8SRamesh Errabolu kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
13780f28cca8SRamesh Errabolu }
13790f28cca8SRamesh Errabolu }
13800f28cca8SRamesh Errabolu }
13810f28cca8SRamesh Errabolu
kfd_build_p2p_node_entry(struct kfd_topology_device * dev,struct kfd_iolink_properties * p2plink)13820f28cca8SRamesh Errabolu static int kfd_build_p2p_node_entry(struct kfd_topology_device *dev,
13830f28cca8SRamesh Errabolu struct kfd_iolink_properties *p2plink)
13840f28cca8SRamesh Errabolu {
13850f28cca8SRamesh Errabolu int ret;
13860f28cca8SRamesh Errabolu
13870f28cca8SRamesh Errabolu p2plink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
13880f28cca8SRamesh Errabolu if (!p2plink->kobj)
13890f28cca8SRamesh Errabolu return -ENOMEM;
13900f28cca8SRamesh Errabolu
13910f28cca8SRamesh Errabolu ret = kobject_init_and_add(p2plink->kobj, &iolink_type,
13920f28cca8SRamesh Errabolu dev->kobj_p2plink, "%d", dev->node_props.p2p_links_count - 1);
13930f28cca8SRamesh Errabolu if (ret < 0) {
13940f28cca8SRamesh Errabolu kobject_put(p2plink->kobj);
13950f28cca8SRamesh Errabolu return ret;
13960f28cca8SRamesh Errabolu }
13970f28cca8SRamesh Errabolu
13980f28cca8SRamesh Errabolu p2plink->attr.name = "properties";
13990f28cca8SRamesh Errabolu p2plink->attr.mode = KFD_SYSFS_FILE_MODE;
14000f28cca8SRamesh Errabolu sysfs_attr_init(&p2plink->attr);
14010f28cca8SRamesh Errabolu ret = sysfs_create_file(p2plink->kobj, &p2plink->attr);
14020f28cca8SRamesh Errabolu if (ret < 0)
14030f28cca8SRamesh Errabolu return ret;
14040f28cca8SRamesh Errabolu
14050f28cca8SRamesh Errabolu return 0;
14060f28cca8SRamesh Errabolu }
14070f28cca8SRamesh Errabolu
kfd_create_indirect_link_prop(struct kfd_topology_device * kdev,int gpu_node)14080f28cca8SRamesh Errabolu static int kfd_create_indirect_link_prop(struct kfd_topology_device *kdev, int gpu_node)
14090f28cca8SRamesh Errabolu {
14107d50b92dSDan Carpenter struct kfd_iolink_properties *gpu_link, *tmp_link, *cpu_link;
14110f28cca8SRamesh Errabolu struct kfd_iolink_properties *props = NULL, *props2 = NULL;
14120f28cca8SRamesh Errabolu struct kfd_topology_device *cpu_dev;
14130f28cca8SRamesh Errabolu int ret = 0;
14140f28cca8SRamesh Errabolu int i, num_cpu;
14150f28cca8SRamesh Errabolu
14160f28cca8SRamesh Errabolu num_cpu = 0;
14170f28cca8SRamesh Errabolu list_for_each_entry(cpu_dev, &topology_device_list, list) {
14180f28cca8SRamesh Errabolu if (cpu_dev->gpu)
14190f28cca8SRamesh Errabolu break;
14200f28cca8SRamesh Errabolu num_cpu++;
14210f28cca8SRamesh Errabolu }
14220f28cca8SRamesh Errabolu
1423499839ecSSrinivasan Shanmugam if (list_empty(&kdev->io_link_props))
1424499839ecSSrinivasan Shanmugam return -ENODATA;
1425499839ecSSrinivasan Shanmugam
14260f28cca8SRamesh Errabolu gpu_link = list_first_entry(&kdev->io_link_props,
14270f28cca8SRamesh Errabolu struct kfd_iolink_properties, list);
14280f28cca8SRamesh Errabolu
14290f28cca8SRamesh Errabolu for (i = 0; i < num_cpu; i++) {
14300f28cca8SRamesh Errabolu /* CPU <--> GPU */
14310f28cca8SRamesh Errabolu if (gpu_link->node_to == i)
14320f28cca8SRamesh Errabolu continue;
14330f28cca8SRamesh Errabolu
14340f28cca8SRamesh Errabolu /* find CPU <--> CPU links */
14357d50b92dSDan Carpenter cpu_link = NULL;
14360f28cca8SRamesh Errabolu cpu_dev = kfd_topology_device_by_proximity_domain(i);
14370f28cca8SRamesh Errabolu if (cpu_dev) {
14387d50b92dSDan Carpenter list_for_each_entry(tmp_link,
14390f28cca8SRamesh Errabolu &cpu_dev->io_link_props, list) {
14407d50b92dSDan Carpenter if (tmp_link->node_to == gpu_link->node_to) {
14417d50b92dSDan Carpenter cpu_link = tmp_link;
14420f28cca8SRamesh Errabolu break;
14430f28cca8SRamesh Errabolu }
14440f28cca8SRamesh Errabolu }
14457d50b92dSDan Carpenter }
14460f28cca8SRamesh Errabolu
14477d50b92dSDan Carpenter if (!cpu_link)
14480f28cca8SRamesh Errabolu return -ENOMEM;
14490f28cca8SRamesh Errabolu
14500f28cca8SRamesh Errabolu /* CPU <--> CPU <--> GPU, GPU node*/
14510f28cca8SRamesh Errabolu props = kfd_alloc_struct(props);
14520f28cca8SRamesh Errabolu if (!props)
14530f28cca8SRamesh Errabolu return -ENOMEM;
14540f28cca8SRamesh Errabolu
14550f28cca8SRamesh Errabolu memcpy(props, gpu_link, sizeof(struct kfd_iolink_properties));
14560f28cca8SRamesh Errabolu props->weight = gpu_link->weight + cpu_link->weight;
14570f28cca8SRamesh Errabolu props->min_latency = gpu_link->min_latency + cpu_link->min_latency;
14580f28cca8SRamesh Errabolu props->max_latency = gpu_link->max_latency + cpu_link->max_latency;
14590f28cca8SRamesh Errabolu props->min_bandwidth = min(gpu_link->min_bandwidth, cpu_link->min_bandwidth);
14600f28cca8SRamesh Errabolu props->max_bandwidth = min(gpu_link->max_bandwidth, cpu_link->max_bandwidth);
14610f28cca8SRamesh Errabolu
14620f28cca8SRamesh Errabolu props->node_from = gpu_node;
14630f28cca8SRamesh Errabolu props->node_to = i;
14640f28cca8SRamesh Errabolu kdev->node_props.p2p_links_count++;
14650f28cca8SRamesh Errabolu list_add_tail(&props->list, &kdev->p2p_link_props);
14660f28cca8SRamesh Errabolu ret = kfd_build_p2p_node_entry(kdev, props);
14670f28cca8SRamesh Errabolu if (ret < 0)
14680f28cca8SRamesh Errabolu return ret;
14690f28cca8SRamesh Errabolu
14700f28cca8SRamesh Errabolu /* for small Bar, no CPU --> GPU in-direct links */
14710f28cca8SRamesh Errabolu if (kfd_dev_is_large_bar(kdev->gpu)) {
14720f28cca8SRamesh Errabolu /* CPU <--> CPU <--> GPU, CPU node*/
14730f28cca8SRamesh Errabolu props2 = kfd_alloc_struct(props2);
14740f28cca8SRamesh Errabolu if (!props2)
14750f28cca8SRamesh Errabolu return -ENOMEM;
14760f28cca8SRamesh Errabolu
14770f28cca8SRamesh Errabolu memcpy(props2, props, sizeof(struct kfd_iolink_properties));
14780f28cca8SRamesh Errabolu props2->node_from = i;
14790f28cca8SRamesh Errabolu props2->node_to = gpu_node;
14800f28cca8SRamesh Errabolu props2->kobj = NULL;
14810f28cca8SRamesh Errabolu cpu_dev->node_props.p2p_links_count++;
14820f28cca8SRamesh Errabolu list_add_tail(&props2->list, &cpu_dev->p2p_link_props);
14830f28cca8SRamesh Errabolu ret = kfd_build_p2p_node_entry(cpu_dev, props2);
14840f28cca8SRamesh Errabolu if (ret < 0)
14850f28cca8SRamesh Errabolu return ret;
14860f28cca8SRamesh Errabolu }
14870f28cca8SRamesh Errabolu }
14880f28cca8SRamesh Errabolu return ret;
14890f28cca8SRamesh Errabolu }
14900f28cca8SRamesh Errabolu
14910f28cca8SRamesh 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)14920f28cca8SRamesh Errabolu static int kfd_add_peer_prop(struct kfd_topology_device *kdev,
14930f28cca8SRamesh Errabolu struct kfd_topology_device *peer, int from, int to)
14940f28cca8SRamesh Errabolu {
14950f28cca8SRamesh Errabolu struct kfd_iolink_properties *props = NULL;
14960f28cca8SRamesh Errabolu struct kfd_iolink_properties *iolink1, *iolink2, *iolink3;
14970f28cca8SRamesh Errabolu struct kfd_topology_device *cpu_dev;
14980f28cca8SRamesh Errabolu int ret = 0;
14990f28cca8SRamesh Errabolu
15000f28cca8SRamesh Errabolu if (!amdgpu_device_is_peer_accessible(
15010f28cca8SRamesh Errabolu kdev->gpu->adev,
15020f28cca8SRamesh Errabolu peer->gpu->adev))
15030f28cca8SRamesh Errabolu return ret;
15040f28cca8SRamesh Errabolu
1505499839ecSSrinivasan Shanmugam if (list_empty(&kdev->io_link_props))
1506499839ecSSrinivasan Shanmugam return -ENODATA;
1507499839ecSSrinivasan Shanmugam
15080f28cca8SRamesh Errabolu iolink1 = list_first_entry(&kdev->io_link_props,
15090f28cca8SRamesh Errabolu struct kfd_iolink_properties, list);
1510499839ecSSrinivasan Shanmugam
1511499839ecSSrinivasan Shanmugam if (list_empty(&peer->io_link_props))
1512499839ecSSrinivasan Shanmugam return -ENODATA;
15130f28cca8SRamesh Errabolu
15140f28cca8SRamesh Errabolu iolink2 = list_first_entry(&peer->io_link_props,
15150f28cca8SRamesh Errabolu struct kfd_iolink_properties, list);
15160f28cca8SRamesh Errabolu
15170f28cca8SRamesh Errabolu props = kfd_alloc_struct(props);
15180f28cca8SRamesh Errabolu if (!props)
15190f28cca8SRamesh Errabolu return -ENOMEM;
15200f28cca8SRamesh Errabolu
15210f28cca8SRamesh Errabolu memcpy(props, iolink1, sizeof(struct kfd_iolink_properties));
15220f28cca8SRamesh Errabolu
15230f28cca8SRamesh Errabolu props->weight = iolink1->weight + iolink2->weight;
15240f28cca8SRamesh Errabolu props->min_latency = iolink1->min_latency + iolink2->min_latency;
15250f28cca8SRamesh Errabolu props->max_latency = iolink1->max_latency + iolink2->max_latency;
15260f28cca8SRamesh Errabolu props->min_bandwidth = min(iolink1->min_bandwidth, iolink2->min_bandwidth);
15270f28cca8SRamesh Errabolu props->max_bandwidth = min(iolink2->max_bandwidth, iolink2->max_bandwidth);
15280f28cca8SRamesh Errabolu
15290f28cca8SRamesh Errabolu if (iolink1->node_to != iolink2->node_to) {
15300f28cca8SRamesh Errabolu /* CPU->CPU link*/
15310f28cca8SRamesh Errabolu cpu_dev = kfd_topology_device_by_proximity_domain(iolink1->node_to);
15320f28cca8SRamesh Errabolu if (cpu_dev) {
1533b1a428b4SSrinivasan Shanmugam list_for_each_entry(iolink3, &cpu_dev->io_link_props, list) {
1534b1a428b4SSrinivasan Shanmugam if (iolink3->node_to != iolink2->node_to)
1535b1a428b4SSrinivasan Shanmugam continue;
15360f28cca8SRamesh Errabolu
15370f28cca8SRamesh Errabolu props->weight += iolink3->weight;
15380f28cca8SRamesh Errabolu props->min_latency += iolink3->min_latency;
15390f28cca8SRamesh Errabolu props->max_latency += iolink3->max_latency;
15400f28cca8SRamesh Errabolu props->min_bandwidth = min(props->min_bandwidth,
15410f28cca8SRamesh Errabolu iolink3->min_bandwidth);
15420f28cca8SRamesh Errabolu props->max_bandwidth = min(props->max_bandwidth,
15430f28cca8SRamesh Errabolu iolink3->max_bandwidth);
1544b1a428b4SSrinivasan Shanmugam break;
1545b1a428b4SSrinivasan Shanmugam }
15460f28cca8SRamesh Errabolu } else {
15470f28cca8SRamesh Errabolu WARN(1, "CPU node not found");
15480f28cca8SRamesh Errabolu }
15490f28cca8SRamesh Errabolu }
15500f28cca8SRamesh Errabolu
15510f28cca8SRamesh Errabolu props->node_from = from;
15520f28cca8SRamesh Errabolu props->node_to = to;
15530f28cca8SRamesh Errabolu peer->node_props.p2p_links_count++;
15540f28cca8SRamesh Errabolu list_add_tail(&props->list, &peer->p2p_link_props);
15550f28cca8SRamesh Errabolu ret = kfd_build_p2p_node_entry(peer, props);
15560f28cca8SRamesh Errabolu
15570f28cca8SRamesh Errabolu return ret;
15580f28cca8SRamesh Errabolu }
15590f28cca8SRamesh Errabolu #endif
15600f28cca8SRamesh Errabolu
kfd_dev_create_p2p_links(void)15610f28cca8SRamesh Errabolu static int kfd_dev_create_p2p_links(void)
15620f28cca8SRamesh Errabolu {
15630f28cca8SRamesh Errabolu struct kfd_topology_device *dev;
15640f28cca8SRamesh Errabolu struct kfd_topology_device *new_dev;
1565914da384SAlex Deucher #if defined(CONFIG_HSA_AMD_P2P)
1566914da384SAlex Deucher uint32_t i;
1567914da384SAlex Deucher #endif
1568914da384SAlex Deucher uint32_t k;
15690f28cca8SRamesh Errabolu int ret = 0;
15700f28cca8SRamesh Errabolu
15710f28cca8SRamesh Errabolu k = 0;
15720f28cca8SRamesh Errabolu list_for_each_entry(dev, &topology_device_list, list)
15730f28cca8SRamesh Errabolu k++;
15740f28cca8SRamesh Errabolu if (k < 2)
15750f28cca8SRamesh Errabolu return 0;
15760f28cca8SRamesh Errabolu
15770f28cca8SRamesh Errabolu new_dev = list_last_entry(&topology_device_list, struct kfd_topology_device, list);
15780f28cca8SRamesh Errabolu if (WARN_ON(!new_dev->gpu))
15790f28cca8SRamesh Errabolu return 0;
15800f28cca8SRamesh Errabolu
15810f28cca8SRamesh Errabolu k--;
15820f28cca8SRamesh Errabolu
15830f28cca8SRamesh Errabolu /* create in-direct links */
15840f28cca8SRamesh Errabolu ret = kfd_create_indirect_link_prop(new_dev, k);
15850f28cca8SRamesh Errabolu if (ret < 0)
15860f28cca8SRamesh Errabolu goto out;
15870f28cca8SRamesh Errabolu
15880f28cca8SRamesh Errabolu /* create p2p links */
15890f28cca8SRamesh Errabolu #if defined(CONFIG_HSA_AMD_P2P)
1590914da384SAlex Deucher i = 0;
15910f28cca8SRamesh Errabolu list_for_each_entry(dev, &topology_device_list, list) {
15920f28cca8SRamesh Errabolu if (dev == new_dev)
15930f28cca8SRamesh Errabolu break;
15940f28cca8SRamesh Errabolu if (!dev->gpu || !dev->gpu->adev ||
15958dc1db31SMukul Joshi (dev->gpu->kfd->hive_id &&
15968dc1db31SMukul Joshi dev->gpu->kfd->hive_id == new_dev->gpu->kfd->hive_id))
15970f28cca8SRamesh Errabolu goto next;
15980f28cca8SRamesh Errabolu
15990f28cca8SRamesh Errabolu /* check if node(s) is/are peer accessible in one direction or bi-direction */
16000f28cca8SRamesh Errabolu ret = kfd_add_peer_prop(new_dev, dev, i, k);
16010f28cca8SRamesh Errabolu if (ret < 0)
16020f28cca8SRamesh Errabolu goto out;
16030f28cca8SRamesh Errabolu
16040f28cca8SRamesh Errabolu ret = kfd_add_peer_prop(dev, new_dev, k, i);
16050f28cca8SRamesh Errabolu if (ret < 0)
16060f28cca8SRamesh Errabolu goto out;
16070f28cca8SRamesh Errabolu next:
16080f28cca8SRamesh Errabolu i++;
16090f28cca8SRamesh Errabolu }
16100f28cca8SRamesh Errabolu #endif
16110f28cca8SRamesh Errabolu
16120f28cca8SRamesh Errabolu out:
16130f28cca8SRamesh Errabolu return ret;
16143a87177eSHarish Kasiviswanathan }
16153a87177eSHarish Kasiviswanathan
1616c0cc999fSMa 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)1617c0cc999fSMa Jun static int fill_in_l1_pcache(struct kfd_cache_properties **props_ext,
1618c0cc999fSMa Jun struct kfd_gpu_cache_info *pcache_info,
1619c0cc999fSMa Jun int cu_bitmask,
1620c0cc999fSMa Jun int cache_type, unsigned int cu_processor_id,
1621c0cc999fSMa Jun int cu_block)
1622c0cc999fSMa Jun {
1623c0cc999fSMa Jun unsigned int cu_sibling_map_mask;
1624c0cc999fSMa Jun int first_active_cu;
1625c0cc999fSMa Jun struct kfd_cache_properties *pcache = NULL;
1626c0cc999fSMa Jun
1627c0cc999fSMa Jun cu_sibling_map_mask = cu_bitmask;
1628c0cc999fSMa Jun cu_sibling_map_mask >>= cu_block;
1629c0cc999fSMa Jun cu_sibling_map_mask &= ((1 << pcache_info[cache_type].num_cu_shared) - 1);
1630c0cc999fSMa Jun first_active_cu = ffs(cu_sibling_map_mask);
1631c0cc999fSMa Jun
1632c0cc999fSMa Jun /* CU could be inactive. In case of shared cache find the first active
1633c0cc999fSMa Jun * CU. and incase of non-shared cache check if the CU is inactive. If
1634c0cc999fSMa Jun * inactive active skip it
1635c0cc999fSMa Jun */
1636c0cc999fSMa Jun if (first_active_cu) {
1637c0cc999fSMa Jun pcache = kfd_alloc_struct(pcache);
1638c0cc999fSMa Jun if (!pcache)
1639c0cc999fSMa Jun return -ENOMEM;
1640c0cc999fSMa Jun
1641c0cc999fSMa Jun memset(pcache, 0, sizeof(struct kfd_cache_properties));
1642c0cc999fSMa Jun pcache->processor_id_low = cu_processor_id + (first_active_cu - 1);
1643c0cc999fSMa Jun pcache->cache_level = pcache_info[cache_type].cache_level;
1644c0cc999fSMa Jun pcache->cache_size = pcache_info[cache_type].cache_size;
16455a2df8ecSJoseph Greathouse pcache->cacheline_size = pcache_info[cache_type].cache_line_size;
1646c0cc999fSMa Jun
1647c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_DATA_CACHE)
1648c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_DATA;
1649c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_INST_CACHE)
1650c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
1651c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_CPU_CACHE)
1652c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_CPU;
1653c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
1654c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_HSACU;
1655c0cc999fSMa Jun
1656c0cc999fSMa Jun /* Sibling map is w.r.t processor_id_low, so shift out
1657c0cc999fSMa Jun * inactive CU
1658c0cc999fSMa Jun */
1659c0cc999fSMa Jun cu_sibling_map_mask =
1660c0cc999fSMa Jun cu_sibling_map_mask >> (first_active_cu - 1);
1661c0cc999fSMa Jun
1662c0cc999fSMa Jun pcache->sibling_map[0] = (uint8_t)(cu_sibling_map_mask & 0xFF);
1663c0cc999fSMa Jun pcache->sibling_map[1] =
1664c0cc999fSMa Jun (uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
1665c0cc999fSMa Jun pcache->sibling_map[2] =
1666c0cc999fSMa Jun (uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
1667c0cc999fSMa Jun pcache->sibling_map[3] =
1668c0cc999fSMa Jun (uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
1669c0cc999fSMa Jun
1670c0cc999fSMa Jun pcache->sibling_map_size = 4;
1671c0cc999fSMa Jun *props_ext = pcache;
1672c0cc999fSMa Jun
1673c0cc999fSMa Jun return 0;
1674c0cc999fSMa Jun }
1675c0cc999fSMa Jun return 1;
1676c0cc999fSMa Jun }
1677c0cc999fSMa Jun
1678c0cc999fSMa 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)1679c0cc999fSMa Jun static int fill_in_l2_l3_pcache(struct kfd_cache_properties **props_ext,
1680c0cc999fSMa Jun struct kfd_gpu_cache_info *pcache_info,
16810021d70aSAlex Deucher struct amdgpu_cu_info *cu_info,
16820021d70aSAlex Deucher struct amdgpu_gfx_config *gfx_info,
16830752e66eSMukul Joshi int cache_type, unsigned int cu_processor_id,
16840752e66eSMukul Joshi struct kfd_node *knode)
1685c0cc999fSMa Jun {
1686c0cc999fSMa Jun unsigned int cu_sibling_map_mask;
1687c0cc999fSMa Jun int first_active_cu;
16880752e66eSMukul Joshi int i, j, k, xcc, start, end;
1689be457b22SMukul Joshi int num_xcc = NUM_XCC(knode->xcc_mask);
1690c0cc999fSMa Jun struct kfd_cache_properties *pcache = NULL;
1691be457b22SMukul Joshi enum amdgpu_memory_partition mode;
1692be457b22SMukul Joshi struct amdgpu_device *adev = knode->adev;
1693c0cc999fSMa Jun
16940752e66eSMukul Joshi start = ffs(knode->xcc_mask) - 1;
1695be457b22SMukul Joshi end = start + num_xcc;
16960021d70aSAlex Deucher cu_sibling_map_mask = cu_info->bitmap[start][0][0];
1697c0cc999fSMa Jun cu_sibling_map_mask &=
1698c0cc999fSMa Jun ((1 << pcache_info[cache_type].num_cu_shared) - 1);
1699c0cc999fSMa Jun first_active_cu = ffs(cu_sibling_map_mask);
1700c0cc999fSMa Jun
1701c0cc999fSMa Jun /* CU could be inactive. In case of shared cache find the first active
1702c0cc999fSMa Jun * CU. and incase of non-shared cache check if the CU is inactive. If
1703c0cc999fSMa Jun * inactive active skip it
1704c0cc999fSMa Jun */
1705c0cc999fSMa Jun if (first_active_cu) {
1706c0cc999fSMa Jun pcache = kfd_alloc_struct(pcache);
1707c0cc999fSMa Jun if (!pcache)
1708c0cc999fSMa Jun return -ENOMEM;
1709c0cc999fSMa Jun
1710c0cc999fSMa Jun memset(pcache, 0, sizeof(struct kfd_cache_properties));
1711c0cc999fSMa Jun pcache->processor_id_low = cu_processor_id
1712c0cc999fSMa Jun + (first_active_cu - 1);
1713c0cc999fSMa Jun pcache->cache_level = pcache_info[cache_type].cache_level;
17145a2df8ecSJoseph Greathouse pcache->cacheline_size = pcache_info[cache_type].cache_line_size;
1715be457b22SMukul Joshi
17165f571c61SHawking Zhang if (KFD_GC_VERSION(knode) == IP_VERSION(9, 4, 3) ||
17175f571c61SHawking Zhang KFD_GC_VERSION(knode) == IP_VERSION(9, 4, 4))
1718be457b22SMukul Joshi mode = adev->gmc.gmc_funcs->query_mem_partition_mode(adev);
1719be457b22SMukul Joshi else
1720be457b22SMukul Joshi mode = UNKNOWN_MEMORY_PARTITION_MODE;
1721be457b22SMukul Joshi
1722c0cc999fSMa Jun pcache->cache_size = pcache_info[cache_type].cache_size;
172317278169SKent Russell /* Partition mode only affects L3 cache size */
172417278169SKent Russell if (mode && pcache->cache_level == 3)
172517278169SKent Russell pcache->cache_size /= mode;
1726c0cc999fSMa Jun
1727c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_DATA_CACHE)
1728c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_DATA;
1729c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_INST_CACHE)
1730c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
1731c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_CPU_CACHE)
1732c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_CPU;
1733c0cc999fSMa Jun if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
1734c0cc999fSMa Jun pcache->cache_type |= HSA_CACHE_TYPE_HSACU;
1735c0cc999fSMa Jun
1736c0cc999fSMa Jun /* Sibling map is w.r.t processor_id_low, so shift out
1737c0cc999fSMa Jun * inactive CU
1738c0cc999fSMa Jun */
1739c0cc999fSMa Jun cu_sibling_map_mask = cu_sibling_map_mask >> (first_active_cu - 1);
1740c0cc999fSMa Jun k = 0;
1741c0cc999fSMa Jun
17420752e66eSMukul Joshi for (xcc = start; xcc < end; xcc++) {
17430021d70aSAlex Deucher for (i = 0; i < gfx_info->max_shader_engines; i++) {
17440021d70aSAlex Deucher for (j = 0; j < gfx_info->max_sh_per_se; j++) {
1745c0cc999fSMa Jun pcache->sibling_map[k] = (uint8_t)(cu_sibling_map_mask & 0xFF);
1746c0cc999fSMa Jun pcache->sibling_map[k+1] = (uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
1747c0cc999fSMa Jun pcache->sibling_map[k+2] = (uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
1748c0cc999fSMa Jun pcache->sibling_map[k+3] = (uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
1749c0cc999fSMa Jun k += 4;
1750c0cc999fSMa Jun
17510021d70aSAlex Deucher cu_sibling_map_mask = cu_info->bitmap[xcc][i % 4][j + i / 4];
1752c0cc999fSMa Jun cu_sibling_map_mask &= ((1 << pcache_info[cache_type].num_cu_shared) - 1);
1753c0cc999fSMa Jun }
1754c0cc999fSMa Jun }
17550752e66eSMukul Joshi }
1756c0cc999fSMa Jun pcache->sibling_map_size = k;
1757c0cc999fSMa Jun *props_ext = pcache;
1758c0cc999fSMa Jun return 0;
1759c0cc999fSMa Jun }
1760c0cc999fSMa Jun return 1;
1761c0cc999fSMa Jun }
1762c0cc999fSMa Jun
1763c0cc999fSMa Jun #define KFD_MAX_CACHE_TYPES 6
1764c0cc999fSMa Jun
1765c0cc999fSMa Jun /* kfd_fill_cache_non_crat_info - Fill GPU cache info using kfd_gpu_cache_info
1766c0cc999fSMa Jun * tables
1767c0cc999fSMa Jun */
kfd_fill_cache_non_crat_info(struct kfd_topology_device * dev,struct kfd_node * kdev)17688dc1db31SMukul Joshi static void kfd_fill_cache_non_crat_info(struct kfd_topology_device *dev, struct kfd_node *kdev)
1769c0cc999fSMa Jun {
1770c0cc999fSMa Jun struct kfd_gpu_cache_info *pcache_info = NULL;
17710752e66eSMukul Joshi int i, j, k, xcc, start, end;
1772c0cc999fSMa Jun int ct = 0;
1773c0cc999fSMa Jun unsigned int cu_processor_id;
1774c0cc999fSMa Jun int ret;
1775c0cc999fSMa Jun unsigned int num_cu_shared;
17760021d70aSAlex Deucher struct amdgpu_cu_info *cu_info = &kdev->adev->gfx.cu_info;
17770021d70aSAlex Deucher struct amdgpu_gfx_config *gfx_info = &kdev->adev->gfx.config;
1778c0cc999fSMa Jun int gpu_processor_id;
1779c0cc999fSMa Jun struct kfd_cache_properties *props_ext;
1780c0cc999fSMa Jun int num_of_entries = 0;
1781c0cc999fSMa Jun int num_of_cache_types = 0;
1782c0cc999fSMa Jun struct kfd_gpu_cache_info cache_info[KFD_MAX_CACHE_TYPES];
1783c0cc999fSMa Jun
1784c0cc999fSMa Jun
1785c0cc999fSMa Jun gpu_processor_id = dev->node_props.simd_id_base;
1786c0cc999fSMa Jun
17875a2df8ecSJoseph Greathouse memset(cache_info, 0, sizeof(cache_info));
1788c0cc999fSMa Jun pcache_info = cache_info;
1789c0cc999fSMa Jun num_of_cache_types = kfd_get_gpu_cache_info(kdev, &pcache_info);
1790c0cc999fSMa Jun if (!num_of_cache_types) {
1791c0cc999fSMa Jun pr_warn("no cache info found\n");
1792c0cc999fSMa Jun return;
1793c0cc999fSMa Jun }
1794c0cc999fSMa Jun
1795c0cc999fSMa Jun /* For each type of cache listed in the kfd_gpu_cache_info table,
1796c0cc999fSMa Jun * go through all available Compute Units.
1797c0cc999fSMa Jun * The [i,j,k] loop will
1798c0cc999fSMa Jun * if kfd_gpu_cache_info.num_cu_shared = 1
1799c0cc999fSMa Jun * will parse through all available CU
1800c0cc999fSMa Jun * If (kfd_gpu_cache_info.num_cu_shared != 1)
1801c0cc999fSMa Jun * then it will consider only one CU from
1802c0cc999fSMa Jun * the shared unit
1803c0cc999fSMa Jun */
18040752e66eSMukul Joshi start = ffs(kdev->xcc_mask) - 1;
18050752e66eSMukul Joshi end = start + NUM_XCC(kdev->xcc_mask);
18060752e66eSMukul Joshi
1807c0cc999fSMa Jun for (ct = 0; ct < num_of_cache_types; ct++) {
1808c0cc999fSMa Jun cu_processor_id = gpu_processor_id;
1809c0cc999fSMa Jun if (pcache_info[ct].cache_level == 1) {
18100752e66eSMukul Joshi for (xcc = start; xcc < end; xcc++) {
18110021d70aSAlex Deucher for (i = 0; i < gfx_info->max_shader_engines; i++) {
18120021d70aSAlex Deucher for (j = 0; j < gfx_info->max_sh_per_se; j++) {
18130021d70aSAlex Deucher for (k = 0; k < gfx_info->max_cu_per_sh; k += pcache_info[ct].num_cu_shared) {
1814c0cc999fSMa Jun
18150021d70aSAlex Deucher ret = fill_in_l1_pcache(&props_ext, pcache_info,
18160021d70aSAlex Deucher cu_info->bitmap[xcc][i % 4][j + i / 4], ct,
1817c0cc999fSMa Jun cu_processor_id, k);
1818c0cc999fSMa Jun
1819c0cc999fSMa Jun if (ret < 0)
1820c0cc999fSMa Jun break;
1821c0cc999fSMa Jun
1822c0cc999fSMa Jun if (!ret) {
1823c0cc999fSMa Jun num_of_entries++;
1824c0cc999fSMa Jun list_add_tail(&props_ext->list, &dev->cache_props);
1825c0cc999fSMa Jun }
1826c0cc999fSMa Jun
1827c0cc999fSMa Jun /* Move to next CU block */
1828c0cc999fSMa Jun num_cu_shared = ((k + pcache_info[ct].num_cu_shared) <=
18290021d70aSAlex Deucher gfx_info->max_cu_per_sh) ?
1830c0cc999fSMa Jun pcache_info[ct].num_cu_shared :
18310021d70aSAlex Deucher (gfx_info->max_cu_per_sh - k);
1832c0cc999fSMa Jun cu_processor_id += num_cu_shared;
1833c0cc999fSMa Jun }
1834c0cc999fSMa Jun }
1835c0cc999fSMa Jun }
18360752e66eSMukul Joshi }
1837c0cc999fSMa Jun } else {
1838c0cc999fSMa Jun ret = fill_in_l2_l3_pcache(&props_ext, pcache_info,
18390021d70aSAlex Deucher cu_info, gfx_info, ct, cu_processor_id, kdev);
1840c0cc999fSMa Jun
1841c0cc999fSMa Jun if (ret < 0)
1842c0cc999fSMa Jun break;
1843c0cc999fSMa Jun
1844c0cc999fSMa Jun if (!ret) {
1845c0cc999fSMa Jun num_of_entries++;
1846c0cc999fSMa Jun list_add_tail(&props_ext->list, &dev->cache_props);
1847c0cc999fSMa Jun }
1848c0cc999fSMa Jun }
1849c0cc999fSMa Jun }
1850c0cc999fSMa Jun dev->node_props.caches_count += num_of_entries;
1851c0cc999fSMa Jun pr_debug("Added [%d] GPU cache entries\n", num_of_entries);
1852c0cc999fSMa Jun }
1853c0cc999fSMa Jun
kfd_topology_add_device_locked(struct kfd_node * gpu,struct kfd_topology_device ** dev)18548e8c68f4SHarish Kasiviswanathan static int kfd_topology_add_device_locked(struct kfd_node *gpu,
1855f701acb6SFelix Kuehling struct kfd_topology_device **dev)
18565b5c4e40SEvgeny Pinchuk {
1857f701acb6SFelix Kuehling int proximity_domain = ++topology_crat_proximity_domain;
18584f449311SHarish Kasiviswanathan struct list_head temp_topology_device_list;
18593a87177eSHarish Kasiviswanathan void *crat_image = NULL;
18603a87177eSHarish Kasiviswanathan size_t image_size = 0;
1861f701acb6SFelix Kuehling int res;
186246d18d51SMukul Joshi
18633a87177eSHarish Kasiviswanathan res = kfd_create_crat_image_virtual(&crat_image, &image_size,
18643a87177eSHarish Kasiviswanathan COMPUTE_UNIT_GPU, gpu,
18653a87177eSHarish Kasiviswanathan proximity_domain);
18663a87177eSHarish Kasiviswanathan if (res) {
18678e8c68f4SHarish Kasiviswanathan dev_err(gpu->adev->dev, "Error creating VCRAT\n");
186846d18d51SMukul Joshi topology_crat_proximity_domain--;
1869f701acb6SFelix Kuehling goto err;
18703a87177eSHarish Kasiviswanathan }
1871c0cc999fSMa Jun
1872f701acb6SFelix Kuehling INIT_LIST_HEAD(&temp_topology_device_list);
1873f701acb6SFelix Kuehling
18743a87177eSHarish Kasiviswanathan res = kfd_parse_crat_table(crat_image,
18753a87177eSHarish Kasiviswanathan &temp_topology_device_list,
18763a87177eSHarish Kasiviswanathan proximity_domain);
18773a87177eSHarish Kasiviswanathan if (res) {
18788e8c68f4SHarish Kasiviswanathan dev_err(gpu->adev->dev, "Error parsing VCRAT\n");
187946d18d51SMukul Joshi topology_crat_proximity_domain--;
18805b5c4e40SEvgeny Pinchuk goto err;
18815b5c4e40SEvgeny Pinchuk }
18824f449311SHarish Kasiviswanathan
18834f449311SHarish Kasiviswanathan kfd_topology_update_device_list(&temp_topology_device_list,
18844f449311SHarish Kasiviswanathan &topology_device_list);
18854f449311SHarish Kasiviswanathan
1886f701acb6SFelix Kuehling *dev = kfd_assign_gpu(gpu);
1887f701acb6SFelix Kuehling if (WARN_ON(!*dev)) {
18883a87177eSHarish Kasiviswanathan res = -ENODEV;
18893a87177eSHarish Kasiviswanathan goto err;
18903a87177eSHarish Kasiviswanathan }
1891c0cc999fSMa Jun
1892c0cc999fSMa Jun /* Fill the cache affinity information here for the GPUs
1893c0cc999fSMa Jun * using VCRAT
1894c0cc999fSMa Jun */
1895f701acb6SFelix Kuehling kfd_fill_cache_non_crat_info(*dev, gpu);
1896c0cc999fSMa Jun
1897c0cc999fSMa Jun /* Update the SYSFS tree, since we added another topology
1898c0cc999fSMa Jun * device
1899c0cc999fSMa Jun */
1900c0cc999fSMa Jun res = kfd_topology_update_sysfs();
1901c0cc999fSMa Jun if (!res)
1902c0cc999fSMa Jun sys_props.generation_count++;
1903c0cc999fSMa Jun else
19048e8c68f4SHarish Kasiviswanathan dev_err(gpu->adev->dev, "Failed to update GPU to sysfs topology. res=%d\n",
19058e8c68f4SHarish Kasiviswanathan res);
1906f701acb6SFelix Kuehling
1907f701acb6SFelix Kuehling err:
1908f701acb6SFelix Kuehling kfd_destroy_crat_image(crat_image);
1909f701acb6SFelix Kuehling return res;
19105b5c4e40SEvgeny Pinchuk }
1911f701acb6SFelix Kuehling
kfd_topology_set_dbg_firmware_support(struct kfd_topology_device * dev)1912d230f1bfSJonathan Kim static void kfd_topology_set_dbg_firmware_support(struct kfd_topology_device *dev)
1913d230f1bfSJonathan Kim {
1914d230f1bfSJonathan Kim bool firmware_supported = true;
1915d230f1bfSJonathan Kim
1916d230f1bfSJonathan Kim if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(11, 0, 0) &&
1917d230f1bfSJonathan Kim KFD_GC_VERSION(dev->gpu) < IP_VERSION(12, 0, 0)) {
191809d49e14SJonathan Kim uint32_t mes_api_rev = (dev->gpu->adev->mes.sched_version &
191909d49e14SJonathan Kim AMDGPU_MES_API_VERSION_MASK) >>
192009d49e14SJonathan Kim AMDGPU_MES_API_VERSION_SHIFT;
192109d49e14SJonathan Kim uint32_t mes_rev = dev->gpu->adev->mes.sched_version &
192209d49e14SJonathan Kim AMDGPU_MES_VERSION_MASK;
192309d49e14SJonathan Kim
192409d49e14SJonathan Kim firmware_supported = (mes_api_rev >= 14) && (mes_rev >= 64);
1925d230f1bfSJonathan Kim goto out;
1926d230f1bfSJonathan Kim }
1927d230f1bfSJonathan Kim
1928d230f1bfSJonathan Kim /*
1929d230f1bfSJonathan Kim * Note: Any unlisted devices here are assumed to support exception handling.
1930d230f1bfSJonathan Kim * Add additional checks here as needed.
1931d230f1bfSJonathan Kim */
1932d230f1bfSJonathan Kim switch (KFD_GC_VERSION(dev->gpu)) {
1933d230f1bfSJonathan Kim case IP_VERSION(9, 0, 1):
1934d230f1bfSJonathan Kim firmware_supported = dev->gpu->kfd->mec_fw_version >= 459 + 32768;
1935d230f1bfSJonathan Kim break;
1936d230f1bfSJonathan Kim case IP_VERSION(9, 1, 0):
1937d230f1bfSJonathan Kim case IP_VERSION(9, 2, 1):
1938d230f1bfSJonathan Kim case IP_VERSION(9, 2, 2):
1939d230f1bfSJonathan Kim case IP_VERSION(9, 3, 0):
1940d230f1bfSJonathan Kim case IP_VERSION(9, 4, 0):
1941d230f1bfSJonathan Kim firmware_supported = dev->gpu->kfd->mec_fw_version >= 459;
1942d230f1bfSJonathan Kim break;
1943d230f1bfSJonathan Kim case IP_VERSION(9, 4, 1):
1944d230f1bfSJonathan Kim firmware_supported = dev->gpu->kfd->mec_fw_version >= 60;
1945d230f1bfSJonathan Kim break;
1946d230f1bfSJonathan Kim case IP_VERSION(9, 4, 2):
1947d230f1bfSJonathan Kim firmware_supported = dev->gpu->kfd->mec_fw_version >= 51;
1948d230f1bfSJonathan Kim break;
1949d230f1bfSJonathan Kim case IP_VERSION(10, 1, 10):
1950d230f1bfSJonathan Kim case IP_VERSION(10, 1, 2):
1951d230f1bfSJonathan Kim case IP_VERSION(10, 1, 1):
1952d230f1bfSJonathan Kim firmware_supported = dev->gpu->kfd->mec_fw_version >= 144;
1953d230f1bfSJonathan Kim break;
1954d230f1bfSJonathan Kim case IP_VERSION(10, 3, 0):
1955d230f1bfSJonathan Kim case IP_VERSION(10, 3, 2):
1956d230f1bfSJonathan Kim case IP_VERSION(10, 3, 1):
1957d230f1bfSJonathan Kim case IP_VERSION(10, 3, 4):
1958d230f1bfSJonathan Kim case IP_VERSION(10, 3, 5):
1959d230f1bfSJonathan Kim firmware_supported = dev->gpu->kfd->mec_fw_version >= 89;
1960d230f1bfSJonathan Kim break;
1961d230f1bfSJonathan Kim case IP_VERSION(10, 1, 3):
1962d230f1bfSJonathan Kim case IP_VERSION(10, 3, 3):
1963d230f1bfSJonathan Kim firmware_supported = false;
1964d230f1bfSJonathan Kim break;
1965d230f1bfSJonathan Kim default:
1966d230f1bfSJonathan Kim break;
1967d230f1bfSJonathan Kim }
1968d230f1bfSJonathan Kim
1969d230f1bfSJonathan Kim out:
1970d230f1bfSJonathan Kim if (firmware_supported)
1971d230f1bfSJonathan Kim dev->node_props.capability |= HSA_CAP_TRAP_DEBUG_FIRMWARE_SUPPORTED;
1972d230f1bfSJonathan Kim }
1973d230f1bfSJonathan Kim
kfd_topology_set_capabilities(struct kfd_topology_device * dev)1974d230f1bfSJonathan Kim static void kfd_topology_set_capabilities(struct kfd_topology_device *dev)
1975d230f1bfSJonathan Kim {
1976d230f1bfSJonathan Kim dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1977d230f1bfSJonathan Kim HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1978d230f1bfSJonathan Kim HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1979d230f1bfSJonathan Kim
1980d230f1bfSJonathan Kim dev->node_props.capability |= HSA_CAP_TRAP_DEBUG_SUPPORT |
1981d230f1bfSJonathan Kim HSA_CAP_TRAP_DEBUG_WAVE_LAUNCH_TRAP_OVERRIDE_SUPPORTED |
1982d230f1bfSJonathan Kim HSA_CAP_TRAP_DEBUG_WAVE_LAUNCH_MODE_SUPPORTED;
1983d230f1bfSJonathan Kim
1984fc7f1d96SJonathan Kim if (kfd_dbg_has_ttmps_always_setup(dev->gpu))
1985fc7f1d96SJonathan Kim dev->node_props.debug_prop |= HSA_DBG_DISPATCH_INFO_ALWAYS_VALID;
1986fc7f1d96SJonathan Kim
1987d230f1bfSJonathan Kim if (KFD_GC_VERSION(dev->gpu) < IP_VERSION(10, 0, 0)) {
19885f571c61SHawking Zhang if (KFD_GC_VERSION(dev->gpu) == IP_VERSION(9, 4, 3) ||
19895f571c61SHawking Zhang KFD_GC_VERSION(dev->gpu) == IP_VERSION(9, 4, 4))
1990567db9e0SJonathan Kim dev->node_props.debug_prop |=
1991567db9e0SJonathan Kim HSA_DBG_WATCH_ADDR_MASK_LO_BIT_GFX9_4_3 |
1992567db9e0SJonathan Kim HSA_DBG_WATCH_ADDR_MASK_HI_BIT_GFX9_4_3;
1993567db9e0SJonathan Kim else
1994567db9e0SJonathan Kim dev->node_props.debug_prop |=
1995567db9e0SJonathan Kim HSA_DBG_WATCH_ADDR_MASK_LO_BIT_GFX9 |
1996d230f1bfSJonathan Kim HSA_DBG_WATCH_ADDR_MASK_HI_BIT;
1997d230f1bfSJonathan Kim
19988e436326SJonathan Kim if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(9, 4, 2))
1999d230f1bfSJonathan Kim dev->node_props.capability |=
2000d230f1bfSJonathan Kim HSA_CAP_TRAP_DEBUG_PRECISE_MEMORY_OPERATIONS_SUPPORTED;
2001d230f1bfSJonathan Kim } else {
2002d230f1bfSJonathan Kim dev->node_props.debug_prop |= HSA_DBG_WATCH_ADDR_MASK_LO_BIT_GFX10 |
2003d230f1bfSJonathan Kim HSA_DBG_WATCH_ADDR_MASK_HI_BIT;
2004d230f1bfSJonathan Kim
2005fc7f1d96SJonathan Kim if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(11, 0, 0))
2006d230f1bfSJonathan Kim dev->node_props.capability |=
2007d230f1bfSJonathan Kim HSA_CAP_TRAP_DEBUG_PRECISE_MEMORY_OPERATIONS_SUPPORTED;
20089243240bSJonathan Kim
20099243240bSJonathan Kim if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(12, 0, 0))
20109243240bSJonathan Kim dev->node_props.capability |=
20119243240bSJonathan Kim HSA_CAP_TRAP_DEBUG_PRECISE_ALU_OPERATIONS_SUPPORTED;
2012d230f1bfSJonathan Kim }
2013d230f1bfSJonathan Kim
2014d230f1bfSJonathan Kim kfd_topology_set_dbg_firmware_support(dev);
2015d230f1bfSJonathan Kim }
2016d230f1bfSJonathan Kim
kfd_topology_add_device(struct kfd_node * gpu)20178dc1db31SMukul Joshi int kfd_topology_add_device(struct kfd_node *gpu)
2018f701acb6SFelix Kuehling {
2019f701acb6SFelix Kuehling uint32_t gpu_id;
2020f701acb6SFelix Kuehling struct kfd_topology_device *dev;
2021f701acb6SFelix Kuehling int res = 0;
2022f701acb6SFelix Kuehling int i;
2023f701acb6SFelix Kuehling const char *asic_name = amdgpu_asic_name[gpu->adev->asic_type];
20240021d70aSAlex Deucher struct amdgpu_gfx_config *gfx_info = &gpu->adev->gfx.config;
20250021d70aSAlex Deucher struct amdgpu_cu_info *cu_info = &gpu->adev->gfx.cu_info;
2026f701acb6SFelix Kuehling
2027400a39f1SJames Zhu if (gpu->xcp && !gpu->xcp->ddev) {
2028400a39f1SJames Zhu dev_warn(gpu->adev->dev,
20298e8c68f4SHarish Kasiviswanathan "Won't add GPU to topology since it has no drm node assigned.");
2030400a39f1SJames Zhu return 0;
2031400a39f1SJames Zhu } else {
20328e8c68f4SHarish Kasiviswanathan dev_dbg(gpu->adev->dev, "Adding new GPU to topology\n");
2033400a39f1SJames Zhu }
2034f701acb6SFelix Kuehling
2035f701acb6SFelix Kuehling /* Check to see if this gpu device exists in the topology_device_list.
2036f701acb6SFelix Kuehling * If so, assign the gpu to that device,
2037f701acb6SFelix Kuehling * else create a Virtual CRAT for this gpu device and then parse that
2038f701acb6SFelix Kuehling * CRAT to create a new topology device. Once created assign the gpu to
2039f701acb6SFelix Kuehling * that topology device
2040f701acb6SFelix Kuehling */
2041f701acb6SFelix Kuehling down_write(&topology_lock);
2042f701acb6SFelix Kuehling dev = kfd_assign_gpu(gpu);
2043f701acb6SFelix Kuehling if (!dev)
20448e8c68f4SHarish Kasiviswanathan res = kfd_topology_add_device_locked(gpu, &dev);
2045c0cc999fSMa Jun up_write(&topology_lock);
2046f701acb6SFelix Kuehling if (res)
2047f701acb6SFelix Kuehling return res;
20485b5c4e40SEvgeny Pinchuk
20493ed181b8SHarish Kasiviswanathan gpu_id = kfd_generate_gpu_id(gpu);
20505b5c4e40SEvgeny Pinchuk dev->gpu_id = gpu_id;
20515b5c4e40SEvgeny Pinchuk gpu->id = gpu_id;
20523a87177eSHarish Kasiviswanathan
20530f28cca8SRamesh Errabolu kfd_dev_create_p2p_links();
20540f28cca8SRamesh Errabolu
20553a87177eSHarish Kasiviswanathan /* TODO: Move the following lines to function
20563a87177eSHarish Kasiviswanathan * kfd_add_non_crat_information
20573a87177eSHarish Kasiviswanathan */
20583a87177eSHarish Kasiviswanathan
20593a87177eSHarish Kasiviswanathan /* Fill-in additional information that is not available in CRAT but
20603a87177eSHarish Kasiviswanathan * needed for the topology
20613a87177eSHarish Kasiviswanathan */
2062b7675b7bSGraham Sider for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1; i++) {
2063b7675b7bSGraham Sider dev->node_props.name[i] = __tolower(asic_name[i]);
2064b7675b7bSGraham Sider if (asic_name[i] == '\0')
2065b7675b7bSGraham Sider break;
2066b7675b7bSGraham Sider }
2067b7675b7bSGraham Sider dev->node_props.name[i] = '\0';
2068c181159aSYong Zhao
20693a87177eSHarish Kasiviswanathan dev->node_props.simd_arrays_per_engine =
20700021d70aSAlex Deucher gfx_info->max_sh_per_se;
20713a87177eSHarish Kasiviswanathan
20728dc1db31SMukul Joshi dev->node_props.gfx_target_version =
20738dc1db31SMukul Joshi gpu->kfd->device_info.gfx_target_version;
2074d69a3b76SMukul Joshi dev->node_props.vendor_id = gpu->adev->pdev->vendor;
2075d69a3b76SMukul Joshi dev->node_props.device_id = gpu->adev->pdev->device;
2076c6d1ec41SJoseph Greathouse dev->node_props.capability |=
207702274fc0SGraham Sider ((dev->gpu->adev->rev_id << HSA_CAP_ASIC_REVISION_SHIFT) &
2078c6d1ec41SJoseph Greathouse HSA_CAP_ASIC_REVISION_MASK);
207992085240SJonathan Kim
2080d69a3b76SMukul Joshi dev->node_props.location_id = pci_dev_id(gpu->adev->pdev);
2081*42b3a6f1SLijo Lazar if (gpu->kfd->num_nodes > 1)
2082421226e5SLijo Lazar dev->node_props.location_id |= dev->gpu->node_id;
208392085240SJonathan Kim
2084d69a3b76SMukul Joshi dev->node_props.domain = pci_domain_nr(gpu->adev->pdev->bus);
20853a87177eSHarish Kasiviswanathan dev->node_props.max_engine_clk_fcompute =
2086574c4183SGraham Sider amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->adev);
20873a87177eSHarish Kasiviswanathan dev->node_props.max_engine_clk_ccompute =
20883a87177eSHarish Kasiviswanathan cpufreq_quick_get_max(0) / 1000;
2089a476c0c6SPhilip Yang
2090a476c0c6SPhilip Yang if (gpu->xcp)
2091a476c0c6SPhilip Yang dev->node_props.drm_render_minor = gpu->xcp->ddev->render->index;
2092a476c0c6SPhilip Yang else
20937c9b7171SOak Zeng dev->node_props.drm_render_minor =
20948dc1db31SMukul Joshi gpu->kfd->shared_resources.drm_render_minor;
20955b5c4e40SEvgeny Pinchuk
20968dc1db31SMukul Joshi dev->node_props.hive_id = gpu->kfd->hive_id;
2097ee2f17f4SAmber Lin dev->node_props.num_sdma_engines = kfd_get_num_sdma_engines(gpu);
209814568cf6SOak Zeng dev->node_props.num_sdma_xgmi_engines =
2099ee2f17f4SAmber Lin kfd_get_num_xgmi_sdma_engines(gpu);
2100bb71c74dSHuang Rui dev->node_props.num_sdma_queues_per_engine =
21018dc1db31SMukul Joshi gpu->kfd->device_info.num_sdma_queues_per_engine -
21028dc1db31SMukul Joshi gpu->kfd->device_info.num_reserved_sdma_queues_per_engine;
210329633d0eSJoseph Greathouse dev->node_props.num_gws = (dev->gpu->gws &&
210429e76462SOak Zeng dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
210502274fc0SGraham Sider dev->gpu->adev->gds.gws_size : 0;
2106e6945304SYong Zhao dev->node_props.num_cp_queues = get_cp_queues_num(dev->gpu->dqm);
21070c1690e3SShaoyun Liu
21083a87177eSHarish Kasiviswanathan kfd_fill_mem_clk_max_info(dev);
21093a87177eSHarish Kasiviswanathan kfd_fill_iolink_non_crat_info(dev);
21103a87177eSHarish Kasiviswanathan
21117eb0502aSGraham Sider switch (dev->gpu->adev->asic_type) {
21123a87177eSHarish Kasiviswanathan case CHIP_KAVERI:
21133a87177eSHarish Kasiviswanathan case CHIP_HAWAII:
21143a87177eSHarish Kasiviswanathan case CHIP_TONGA:
21153a87177eSHarish Kasiviswanathan dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
21163a87177eSHarish Kasiviswanathan HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
21173a87177eSHarish Kasiviswanathan HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
21183a87177eSHarish Kasiviswanathan break;
21193a87177eSHarish Kasiviswanathan case CHIP_CARRIZO:
21203a87177eSHarish Kasiviswanathan case CHIP_FIJI:
21213a87177eSHarish Kasiviswanathan case CHIP_POLARIS10:
21223a87177eSHarish Kasiviswanathan case CHIP_POLARIS11:
2123846a44d7SGang Ba case CHIP_POLARIS12:
2124ed81cd6eSKent Russell case CHIP_VEGAM:
212542aa8793SFelix Kuehling pr_debug("Adding doorbell packet type capability\n");
21263a87177eSHarish Kasiviswanathan dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
21273a87177eSHarish Kasiviswanathan HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
21283a87177eSHarish Kasiviswanathan HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
21293a87177eSHarish Kasiviswanathan break;
2130e4804a39SGraham Sider default:
2131d230f1bfSJonathan Kim if (KFD_GC_VERSION(dev->gpu) < IP_VERSION(9, 0, 1))
21323a87177eSHarish Kasiviswanathan WARN(1, "Unexpected ASIC family %u",
21337eb0502aSGraham Sider dev->gpu->adev->asic_type);
2134d230f1bfSJonathan Kim else
2135d230f1bfSJonathan Kim kfd_topology_set_capabilities(dev);
21367639a8c4SBen Goz }
21377639a8c4SBen Goz
21381ae99eabSOak Zeng /*
21391ae99eabSOak Zeng * Overwrite ATS capability according to needs_iommu_device to fix
21401ae99eabSOak Zeng * potential missing corresponding bit in CRAT of BIOS.
21411ae99eabSOak Zeng */
21421ae99eabSOak Zeng dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;
21431ae99eabSOak Zeng
21443a87177eSHarish Kasiviswanathan /* Fix errors in CZ CRAT.
21453a87177eSHarish Kasiviswanathan * simd_count: Carrizo CRAT reports wrong simd_count, probably
21463a87177eSHarish Kasiviswanathan * because it doesn't consider masked out CUs
214770f372bfSPhilip Cox * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
21483a87177eSHarish Kasiviswanathan */
21497eb0502aSGraham Sider if (dev->gpu->adev->asic_type == CHIP_CARRIZO) {
21503a87177eSHarish Kasiviswanathan dev->node_props.simd_count =
21510021d70aSAlex Deucher cu_info->simd_per_cu * cu_info->number;
215270f372bfSPhilip Cox dev->node_props.max_waves_per_simd = 10;
215370f372bfSPhilip Cox }
21543a87177eSHarish Kasiviswanathan
21555436ab94SStanley.Yang /* kfd only concerns sram ecc on GFX and HBM ecc on UMC */
21560dee45a2SEric Huang dev->node_props.capability |=
215756c5977eSGraham Sider ((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0) ?
21580dee45a2SEric Huang HSA_CAP_SRAM_EDCSUPPORTED : 0;
215956c5977eSGraham Sider dev->node_props.capability |=
216056c5977eSGraham Sider ((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
21610dee45a2SEric Huang HSA_CAP_MEM_EDCSUPPORTED : 0;
21620dee45a2SEric Huang
2163046e674bSGraham Sider if (KFD_GC_VERSION(dev->gpu) != IP_VERSION(9, 0, 1))
216456c5977eSGraham Sider dev->node_props.capability |= (dev->gpu->adev->ras_enabled != 0) ?
21650dee45a2SEric Huang HSA_CAP_RASEVENTNOTIFY : 0;
21660dee45a2SEric Huang
2167610dab11SPhilip Yang if (KFD_IS_SVM_API_SUPPORTED(dev->gpu->adev))
21684c166eb9SPhilip Yang dev->node_props.capability |= HSA_CAP_SVMAPI_SUPPORTED;
21694c166eb9SPhilip Yang
217003d400e7SAlex Sierra if (dev->gpu->adev->gmc.is_app_apu ||
217103d400e7SAlex Sierra dev->gpu->adev->gmc.xgmi.connected_to_cpu)
217203d400e7SAlex Sierra dev->node_props.capability |= HSA_CAP_FLAGS_COHERENTHOSTACCESS;
217303d400e7SAlex Sierra
2174517fff22SPhilip Yang kfd_queue_ctx_save_restore_size(dev);
2175517fff22SPhilip Yang
21763a87177eSHarish Kasiviswanathan kfd_debug_print_topology();
21773a87177eSHarish Kasiviswanathan
21785b5c4e40SEvgeny Pinchuk kfd_notify_gpu_change(gpu_id, 1);
2179f701acb6SFelix Kuehling
21807d4f8db4SDan Carpenter return 0;
21815b5c4e40SEvgeny Pinchuk }
21825b5c4e40SEvgeny Pinchuk
218346d18d51SMukul Joshi /**
218446d18d51SMukul Joshi * kfd_topology_update_io_links() - Update IO links after device removal.
218546d18d51SMukul Joshi * @proximity_domain: Proximity domain value of the dev being removed.
218646d18d51SMukul Joshi *
218746d18d51SMukul Joshi * The topology list currently is arranged in increasing order of
218846d18d51SMukul Joshi * proximity domain.
218946d18d51SMukul Joshi *
219046d18d51SMukul Joshi * Two things need to be done when a device is removed:
219146d18d51SMukul Joshi * 1. All the IO links to this device need to be removed.
219246d18d51SMukul Joshi * 2. All nodes after the current device node need to move
219346d18d51SMukul Joshi * up once this device node is removed from the topology
219446d18d51SMukul Joshi * list. As a result, the proximity domain values for
219546d18d51SMukul Joshi * all nodes after the node being deleted reduce by 1.
219646d18d51SMukul Joshi * This would also cause the proximity domain values for
219746d18d51SMukul Joshi * io links to be updated based on new proximity domain
219846d18d51SMukul Joshi * values.
219946d18d51SMukul Joshi *
220046d18d51SMukul Joshi * Context: The caller must hold write topology_lock.
220146d18d51SMukul Joshi */
kfd_topology_update_io_links(int proximity_domain)220246d18d51SMukul Joshi static void kfd_topology_update_io_links(int proximity_domain)
220346d18d51SMukul Joshi {
220446d18d51SMukul Joshi struct kfd_topology_device *dev;
22050f28cca8SRamesh Errabolu struct kfd_iolink_properties *iolink, *p2plink, *tmp;
220646d18d51SMukul Joshi
220746d18d51SMukul Joshi list_for_each_entry(dev, &topology_device_list, list) {
220846d18d51SMukul Joshi if (dev->proximity_domain > proximity_domain)
220946d18d51SMukul Joshi dev->proximity_domain--;
221046d18d51SMukul Joshi
221146d18d51SMukul Joshi list_for_each_entry_safe(iolink, tmp, &dev->io_link_props, list) {
221246d18d51SMukul Joshi /*
221346d18d51SMukul Joshi * If there is an io link to the dev being deleted
221446d18d51SMukul Joshi * then remove that IO link also.
221546d18d51SMukul Joshi */
221646d18d51SMukul Joshi if (iolink->node_to == proximity_domain) {
221746d18d51SMukul Joshi list_del(&iolink->list);
221846d18d51SMukul Joshi dev->node_props.io_links_count--;
221998447635SMukul Joshi } else {
222098447635SMukul Joshi if (iolink->node_from > proximity_domain)
222146d18d51SMukul Joshi iolink->node_from--;
222298447635SMukul Joshi if (iolink->node_to > proximity_domain)
222346d18d51SMukul Joshi iolink->node_to--;
222446d18d51SMukul Joshi }
222546d18d51SMukul Joshi }
22260f28cca8SRamesh Errabolu
22270f28cca8SRamesh Errabolu list_for_each_entry_safe(p2plink, tmp, &dev->p2p_link_props, list) {
22280f28cca8SRamesh Errabolu /*
22290f28cca8SRamesh Errabolu * If there is a p2p link to the dev being deleted
22300f28cca8SRamesh Errabolu * then remove that p2p link also.
22310f28cca8SRamesh Errabolu */
22320f28cca8SRamesh Errabolu if (p2plink->node_to == proximity_domain) {
22330f28cca8SRamesh Errabolu list_del(&p2plink->list);
22340f28cca8SRamesh Errabolu dev->node_props.p2p_links_count--;
22350f28cca8SRamesh Errabolu } else {
22360f28cca8SRamesh Errabolu if (p2plink->node_from > proximity_domain)
22370f28cca8SRamesh Errabolu p2plink->node_from--;
22380f28cca8SRamesh Errabolu if (p2plink->node_to > proximity_domain)
22390f28cca8SRamesh Errabolu p2plink->node_to--;
22400f28cca8SRamesh Errabolu }
22410f28cca8SRamesh Errabolu }
224246d18d51SMukul Joshi }
224346d18d51SMukul Joshi }
224446d18d51SMukul Joshi
kfd_topology_remove_device(struct kfd_node * gpu)22458dc1db31SMukul Joshi int kfd_topology_remove_device(struct kfd_node *gpu)
22465b5c4e40SEvgeny Pinchuk {
22474f449311SHarish Kasiviswanathan struct kfd_topology_device *dev, *tmp;
22485b5c4e40SEvgeny Pinchuk uint32_t gpu_id;
22495b5c4e40SEvgeny Pinchuk int res = -ENODEV;
225046d18d51SMukul Joshi int i = 0;
22515b5c4e40SEvgeny Pinchuk
22525b5c4e40SEvgeny Pinchuk down_write(&topology_lock);
22535b5c4e40SEvgeny Pinchuk
225446d18d51SMukul Joshi list_for_each_entry_safe(dev, tmp, &topology_device_list, list) {
22555b5c4e40SEvgeny Pinchuk if (dev->gpu == gpu) {
22565b5c4e40SEvgeny Pinchuk gpu_id = dev->gpu_id;
22575b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_node_entry(dev);
22585b5c4e40SEvgeny Pinchuk kfd_release_topology_device(dev);
22594f449311SHarish Kasiviswanathan sys_props.num_devices--;
226046d18d51SMukul Joshi kfd_topology_update_io_links(i);
226146d18d51SMukul Joshi topology_crat_proximity_domain = sys_props.num_devices-1;
226246d18d51SMukul Joshi sys_props.generation_count++;
22635b5c4e40SEvgeny Pinchuk res = 0;
22645b5c4e40SEvgeny Pinchuk if (kfd_topology_update_sysfs() < 0)
22655b5c4e40SEvgeny Pinchuk kfd_topology_release_sysfs();
22665b5c4e40SEvgeny Pinchuk break;
22675b5c4e40SEvgeny Pinchuk }
226846d18d51SMukul Joshi i++;
226946d18d51SMukul Joshi }
22705b5c4e40SEvgeny Pinchuk
22715b5c4e40SEvgeny Pinchuk up_write(&topology_lock);
22725b5c4e40SEvgeny Pinchuk
2273174de876SFelix Kuehling if (!res)
22745b5c4e40SEvgeny Pinchuk kfd_notify_gpu_change(gpu_id, 0);
22755b5c4e40SEvgeny Pinchuk
22765b5c4e40SEvgeny Pinchuk return res;
22775b5c4e40SEvgeny Pinchuk }
22785b5c4e40SEvgeny Pinchuk
22796d82eb0eSHarish Kasiviswanathan /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
22806d82eb0eSHarish Kasiviswanathan * topology. If GPU device is found @idx, then valid kfd_dev pointer is
22816d82eb0eSHarish Kasiviswanathan * returned through @kdev
22826d82eb0eSHarish Kasiviswanathan * Return - 0: On success (@kdev will be NULL for non GPU nodes)
22836d82eb0eSHarish Kasiviswanathan * -1: If end of list
22845b5c4e40SEvgeny Pinchuk */
kfd_topology_enum_kfd_devices(uint8_t idx,struct kfd_node ** kdev)22858dc1db31SMukul Joshi int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_node **kdev)
22865b5c4e40SEvgeny Pinchuk {
22875b5c4e40SEvgeny Pinchuk
22885b5c4e40SEvgeny Pinchuk struct kfd_topology_device *top_dev;
22895b5c4e40SEvgeny Pinchuk uint8_t device_idx = 0;
22905b5c4e40SEvgeny Pinchuk
22916d82eb0eSHarish Kasiviswanathan *kdev = NULL;
22925b5c4e40SEvgeny Pinchuk down_read(&topology_lock);
22935b5c4e40SEvgeny Pinchuk
22945b5c4e40SEvgeny Pinchuk list_for_each_entry(top_dev, &topology_device_list, list) {
22955b5c4e40SEvgeny Pinchuk if (device_idx == idx) {
22966d82eb0eSHarish Kasiviswanathan *kdev = top_dev->gpu;
22976d82eb0eSHarish Kasiviswanathan up_read(&topology_lock);
22986d82eb0eSHarish Kasiviswanathan return 0;
22995b5c4e40SEvgeny Pinchuk }
23005b5c4e40SEvgeny Pinchuk
23015b5c4e40SEvgeny Pinchuk device_idx++;
23025b5c4e40SEvgeny Pinchuk }
23035b5c4e40SEvgeny Pinchuk
23045b5c4e40SEvgeny Pinchuk up_read(&topology_lock);
23055b5c4e40SEvgeny Pinchuk
23066d82eb0eSHarish Kasiviswanathan return -1;
23075b5c4e40SEvgeny Pinchuk
23085b5c4e40SEvgeny Pinchuk }
2309851a645eSFelix Kuehling
kfd_cpumask_to_apic_id(const struct cpumask * cpumask)2310520b8fb7SFelix Kuehling static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
2311520b8fb7SFelix Kuehling {
2312520b8fb7SFelix Kuehling int first_cpu_of_numa_node;
2313520b8fb7SFelix Kuehling
2314520b8fb7SFelix Kuehling if (!cpumask || cpumask == cpu_none_mask)
2315520b8fb7SFelix Kuehling return -1;
2316520b8fb7SFelix Kuehling first_cpu_of_numa_node = cpumask_first(cpumask);
2317520b8fb7SFelix Kuehling if (first_cpu_of_numa_node >= nr_cpu_ids)
2318520b8fb7SFelix Kuehling return -1;
2319df1dd4f4SFelix Kuehling #ifdef CONFIG_X86_64
2320b9655e70SThomas Gleixner return cpu_data(first_cpu_of_numa_node).topo.apicid;
2321df1dd4f4SFelix Kuehling #else
2322df1dd4f4SFelix Kuehling return first_cpu_of_numa_node;
2323df1dd4f4SFelix Kuehling #endif
2324520b8fb7SFelix Kuehling }
2325520b8fb7SFelix Kuehling
2326520b8fb7SFelix Kuehling /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
2327520b8fb7SFelix Kuehling * of the given NUMA node (numa_node_id)
2328520b8fb7SFelix Kuehling * Return -1 on failure
2329520b8fb7SFelix Kuehling */
kfd_numa_node_to_apic_id(int numa_node_id)2330520b8fb7SFelix Kuehling int kfd_numa_node_to_apic_id(int numa_node_id)
2331520b8fb7SFelix Kuehling {
2332520b8fb7SFelix Kuehling if (numa_node_id == -1) {
2333520b8fb7SFelix Kuehling pr_warn("Invalid NUMA Node. Use online CPU mask\n");
2334520b8fb7SFelix Kuehling return kfd_cpumask_to_apic_id(cpu_online_mask);
2335520b8fb7SFelix Kuehling }
2336520b8fb7SFelix Kuehling return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
2337520b8fb7SFelix Kuehling }
2338520b8fb7SFelix Kuehling
2339851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS)
2340851a645eSFelix Kuehling
kfd_debugfs_hqds_by_device(struct seq_file * m,void * data)2341851a645eSFelix Kuehling int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
2342851a645eSFelix Kuehling {
2343851a645eSFelix Kuehling struct kfd_topology_device *dev;
2344851a645eSFelix Kuehling unsigned int i = 0;
2345851a645eSFelix Kuehling int r = 0;
2346851a645eSFelix Kuehling
2347851a645eSFelix Kuehling down_read(&topology_lock);
2348851a645eSFelix Kuehling
2349851a645eSFelix Kuehling list_for_each_entry(dev, &topology_device_list, list) {
2350851a645eSFelix Kuehling if (!dev->gpu) {
2351851a645eSFelix Kuehling i++;
2352851a645eSFelix Kuehling continue;
2353851a645eSFelix Kuehling }
2354851a645eSFelix Kuehling
2355851a645eSFelix Kuehling seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
2356851a645eSFelix Kuehling r = dqm_debugfs_hqds(m, dev->gpu->dqm);
2357851a645eSFelix Kuehling if (r)
2358851a645eSFelix Kuehling break;
2359851a645eSFelix Kuehling }
2360851a645eSFelix Kuehling
2361851a645eSFelix Kuehling up_read(&topology_lock);
2362851a645eSFelix Kuehling
2363851a645eSFelix Kuehling return r;
2364851a645eSFelix Kuehling }
2365851a645eSFelix Kuehling
kfd_debugfs_rls_by_device(struct seq_file * m,void * data)2366851a645eSFelix Kuehling int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
2367851a645eSFelix Kuehling {
2368851a645eSFelix Kuehling struct kfd_topology_device *dev;
2369851a645eSFelix Kuehling unsigned int i = 0;
2370851a645eSFelix Kuehling int r = 0;
2371851a645eSFelix Kuehling
2372851a645eSFelix Kuehling down_read(&topology_lock);
2373851a645eSFelix Kuehling
2374851a645eSFelix Kuehling list_for_each_entry(dev, &topology_device_list, list) {
2375851a645eSFelix Kuehling if (!dev->gpu) {
2376851a645eSFelix Kuehling i++;
2377851a645eSFelix Kuehling continue;
2378851a645eSFelix Kuehling }
2379851a645eSFelix Kuehling
2380851a645eSFelix Kuehling seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
23819af5379cSOak Zeng r = pm_debugfs_runlist(m, &dev->gpu->dqm->packet_mgr);
2382851a645eSFelix Kuehling if (r)
2383851a645eSFelix Kuehling break;
2384851a645eSFelix Kuehling }
2385851a645eSFelix Kuehling
2386851a645eSFelix Kuehling up_read(&topology_lock);
2387851a645eSFelix Kuehling
2388851a645eSFelix Kuehling return r;
2389851a645eSFelix Kuehling }
2390851a645eSFelix Kuehling
2391851a645eSFelix Kuehling #endif
2392