15b5c4e40SEvgeny Pinchuk /* 25b5c4e40SEvgeny Pinchuk * Copyright 2014 Advanced Micro Devices, Inc. 35b5c4e40SEvgeny Pinchuk * 45b5c4e40SEvgeny Pinchuk * Permission is hereby granted, free of charge, to any person obtaining a 55b5c4e40SEvgeny Pinchuk * copy of this software and associated documentation files (the "Software"), 65b5c4e40SEvgeny Pinchuk * to deal in the Software without restriction, including without limitation 75b5c4e40SEvgeny Pinchuk * the rights to use, copy, modify, merge, publish, distribute, sublicense, 85b5c4e40SEvgeny Pinchuk * and/or sell copies of the Software, and to permit persons to whom the 95b5c4e40SEvgeny Pinchuk * Software is furnished to do so, subject to the following conditions: 105b5c4e40SEvgeny Pinchuk * 115b5c4e40SEvgeny Pinchuk * The above copyright notice and this permission notice shall be included in 125b5c4e40SEvgeny Pinchuk * all copies or substantial portions of the Software. 135b5c4e40SEvgeny Pinchuk * 145b5c4e40SEvgeny Pinchuk * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 155b5c4e40SEvgeny Pinchuk * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 165b5c4e40SEvgeny Pinchuk * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 175b5c4e40SEvgeny Pinchuk * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 185b5c4e40SEvgeny Pinchuk * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 195b5c4e40SEvgeny Pinchuk * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 205b5c4e40SEvgeny Pinchuk * OTHER DEALINGS IN THE SOFTWARE. 215b5c4e40SEvgeny Pinchuk */ 225b5c4e40SEvgeny Pinchuk 235b5c4e40SEvgeny Pinchuk #include <linux/types.h> 245b5c4e40SEvgeny Pinchuk #include <linux/kernel.h> 255b5c4e40SEvgeny Pinchuk #include <linux/pci.h> 265b5c4e40SEvgeny Pinchuk #include <linux/errno.h> 275b5c4e40SEvgeny Pinchuk #include <linux/acpi.h> 285b5c4e40SEvgeny Pinchuk #include <linux/hash.h> 295b5c4e40SEvgeny Pinchuk #include <linux/cpufreq.h> 30f7c826adSAlexey Skidanov #include <linux/log2.h> 31520b8fb7SFelix Kuehling #include <linux/dmi.h> 32520b8fb7SFelix Kuehling #include <linux/atomic.h> 335b5c4e40SEvgeny Pinchuk 345b5c4e40SEvgeny Pinchuk #include "kfd_priv.h" 355b5c4e40SEvgeny Pinchuk #include "kfd_crat.h" 365b5c4e40SEvgeny Pinchuk #include "kfd_topology.h" 37851a645eSFelix Kuehling #include "kfd_device_queue_manager.h" 3864d1c3a4SFelix Kuehling #include "kfd_iommu.h" 395b87245fSAmber Lin #include "amdgpu_amdkfd.h" 400dee45a2SEric Huang #include "amdgpu_ras.h" 415b5c4e40SEvgeny Pinchuk 424f449311SHarish Kasiviswanathan /* topology_device_list - Master list of all topology devices */ 434f449311SHarish Kasiviswanathan static struct list_head topology_device_list; 44520b8fb7SFelix Kuehling static struct kfd_system_properties sys_props; 455b5c4e40SEvgeny Pinchuk 465b5c4e40SEvgeny Pinchuk static DECLARE_RWSEM(topology_lock); 47520b8fb7SFelix Kuehling static atomic_t topology_crat_proximity_domain; 485b5c4e40SEvgeny Pinchuk 493a87177eSHarish Kasiviswanathan struct kfd_topology_device *kfd_topology_device_by_proximity_domain( 503a87177eSHarish Kasiviswanathan uint32_t proximity_domain) 513a87177eSHarish Kasiviswanathan { 523a87177eSHarish Kasiviswanathan struct kfd_topology_device *top_dev; 533a87177eSHarish Kasiviswanathan struct kfd_topology_device *device = NULL; 543a87177eSHarish Kasiviswanathan 553a87177eSHarish Kasiviswanathan down_read(&topology_lock); 563a87177eSHarish Kasiviswanathan 573a87177eSHarish Kasiviswanathan list_for_each_entry(top_dev, &topology_device_list, list) 583a87177eSHarish Kasiviswanathan if (top_dev->proximity_domain == proximity_domain) { 593a87177eSHarish Kasiviswanathan device = top_dev; 603a87177eSHarish Kasiviswanathan break; 613a87177eSHarish Kasiviswanathan } 623a87177eSHarish Kasiviswanathan 633a87177eSHarish Kasiviswanathan up_read(&topology_lock); 643a87177eSHarish Kasiviswanathan 653a87177eSHarish Kasiviswanathan return device; 663a87177eSHarish Kasiviswanathan } 673a87177eSHarish Kasiviswanathan 6844d8cc6fSYong Zhao struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id) 695b5c4e40SEvgeny Pinchuk { 7044d8cc6fSYong Zhao struct kfd_topology_device *top_dev = NULL; 7144d8cc6fSYong Zhao struct kfd_topology_device *ret = NULL; 725b5c4e40SEvgeny Pinchuk 735b5c4e40SEvgeny Pinchuk down_read(&topology_lock); 745b5c4e40SEvgeny Pinchuk 755b5c4e40SEvgeny Pinchuk list_for_each_entry(top_dev, &topology_device_list, list) 765b5c4e40SEvgeny Pinchuk if (top_dev->gpu_id == gpu_id) { 7744d8cc6fSYong Zhao ret = top_dev; 785b5c4e40SEvgeny Pinchuk break; 795b5c4e40SEvgeny Pinchuk } 805b5c4e40SEvgeny Pinchuk 815b5c4e40SEvgeny Pinchuk up_read(&topology_lock); 825b5c4e40SEvgeny Pinchuk 8344d8cc6fSYong Zhao return ret; 8444d8cc6fSYong Zhao } 8544d8cc6fSYong Zhao 8644d8cc6fSYong Zhao struct kfd_dev *kfd_device_by_id(uint32_t gpu_id) 8744d8cc6fSYong Zhao { 8844d8cc6fSYong Zhao struct kfd_topology_device *top_dev; 8944d8cc6fSYong Zhao 9044d8cc6fSYong Zhao top_dev = kfd_topology_device_by_id(gpu_id); 9144d8cc6fSYong Zhao if (!top_dev) 9244d8cc6fSYong Zhao return NULL; 9344d8cc6fSYong Zhao 9444d8cc6fSYong Zhao return top_dev->gpu; 955b5c4e40SEvgeny Pinchuk } 965b5c4e40SEvgeny Pinchuk 975b5c4e40SEvgeny Pinchuk struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev) 985b5c4e40SEvgeny Pinchuk { 995b5c4e40SEvgeny Pinchuk struct kfd_topology_device *top_dev; 1005b5c4e40SEvgeny Pinchuk struct kfd_dev *device = NULL; 1015b5c4e40SEvgeny Pinchuk 1025b5c4e40SEvgeny Pinchuk down_read(&topology_lock); 1035b5c4e40SEvgeny Pinchuk 1045b5c4e40SEvgeny Pinchuk list_for_each_entry(top_dev, &topology_device_list, list) 1053704d56eSFelix Kuehling if (top_dev->gpu && top_dev->gpu->pdev == pdev) { 1065b5c4e40SEvgeny Pinchuk device = top_dev->gpu; 1075b5c4e40SEvgeny Pinchuk break; 1085b5c4e40SEvgeny Pinchuk } 1095b5c4e40SEvgeny Pinchuk 1105b5c4e40SEvgeny Pinchuk up_read(&topology_lock); 1115b5c4e40SEvgeny Pinchuk 1125b5c4e40SEvgeny Pinchuk return device; 1135b5c4e40SEvgeny Pinchuk } 1145b5c4e40SEvgeny Pinchuk 1151dde0ea9SFelix Kuehling struct kfd_dev *kfd_device_by_kgd(const struct kgd_dev *kgd) 1161dde0ea9SFelix Kuehling { 1171dde0ea9SFelix Kuehling struct kfd_topology_device *top_dev; 1181dde0ea9SFelix Kuehling struct kfd_dev *device = NULL; 1191dde0ea9SFelix Kuehling 1201dde0ea9SFelix Kuehling down_read(&topology_lock); 1211dde0ea9SFelix Kuehling 1221dde0ea9SFelix Kuehling list_for_each_entry(top_dev, &topology_device_list, list) 1231dde0ea9SFelix Kuehling if (top_dev->gpu && top_dev->gpu->kgd == kgd) { 1241dde0ea9SFelix Kuehling device = top_dev->gpu; 1251dde0ea9SFelix Kuehling break; 1261dde0ea9SFelix Kuehling } 1271dde0ea9SFelix Kuehling 1281dde0ea9SFelix Kuehling up_read(&topology_lock); 1291dde0ea9SFelix Kuehling 1301dde0ea9SFelix Kuehling return device; 1311dde0ea9SFelix Kuehling } 1321dde0ea9SFelix Kuehling 1333a87177eSHarish Kasiviswanathan /* Called with write topology_lock acquired */ 1345b5c4e40SEvgeny Pinchuk static void kfd_release_topology_device(struct kfd_topology_device *dev) 1355b5c4e40SEvgeny Pinchuk { 1365b5c4e40SEvgeny Pinchuk struct kfd_mem_properties *mem; 1375b5c4e40SEvgeny Pinchuk struct kfd_cache_properties *cache; 1385b5c4e40SEvgeny Pinchuk struct kfd_iolink_properties *iolink; 139f4757347SAmber Lin struct kfd_perf_properties *perf; 1405b5c4e40SEvgeny Pinchuk 1415b5c4e40SEvgeny Pinchuk list_del(&dev->list); 1425b5c4e40SEvgeny Pinchuk 1435b5c4e40SEvgeny Pinchuk while (dev->mem_props.next != &dev->mem_props) { 1445b5c4e40SEvgeny Pinchuk mem = container_of(dev->mem_props.next, 1455b5c4e40SEvgeny Pinchuk struct kfd_mem_properties, list); 1465b5c4e40SEvgeny Pinchuk list_del(&mem->list); 1475b5c4e40SEvgeny Pinchuk kfree(mem); 1485b5c4e40SEvgeny Pinchuk } 1495b5c4e40SEvgeny Pinchuk 1505b5c4e40SEvgeny Pinchuk while (dev->cache_props.next != &dev->cache_props) { 1515b5c4e40SEvgeny Pinchuk cache = container_of(dev->cache_props.next, 1525b5c4e40SEvgeny Pinchuk struct kfd_cache_properties, list); 1535b5c4e40SEvgeny Pinchuk list_del(&cache->list); 1545b5c4e40SEvgeny Pinchuk kfree(cache); 1555b5c4e40SEvgeny Pinchuk } 1565b5c4e40SEvgeny Pinchuk 1575b5c4e40SEvgeny Pinchuk while (dev->io_link_props.next != &dev->io_link_props) { 1585b5c4e40SEvgeny Pinchuk iolink = container_of(dev->io_link_props.next, 1595b5c4e40SEvgeny Pinchuk struct kfd_iolink_properties, list); 1605b5c4e40SEvgeny Pinchuk list_del(&iolink->list); 1615b5c4e40SEvgeny Pinchuk kfree(iolink); 1625b5c4e40SEvgeny Pinchuk } 1635b5c4e40SEvgeny Pinchuk 164f4757347SAmber Lin while (dev->perf_props.next != &dev->perf_props) { 165f4757347SAmber Lin perf = container_of(dev->perf_props.next, 166f4757347SAmber Lin struct kfd_perf_properties, list); 167f4757347SAmber Lin list_del(&perf->list); 168f4757347SAmber Lin kfree(perf); 169f4757347SAmber Lin } 170f4757347SAmber Lin 1715b5c4e40SEvgeny Pinchuk kfree(dev); 1725b5c4e40SEvgeny Pinchuk } 1735b5c4e40SEvgeny Pinchuk 1744f449311SHarish Kasiviswanathan void kfd_release_topology_device_list(struct list_head *device_list) 1755b5c4e40SEvgeny Pinchuk { 1765b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev; 1775b5c4e40SEvgeny Pinchuk 1784f449311SHarish Kasiviswanathan while (!list_empty(device_list)) { 1794f449311SHarish Kasiviswanathan dev = list_first_entry(device_list, 1805b5c4e40SEvgeny Pinchuk struct kfd_topology_device, list); 1815b5c4e40SEvgeny Pinchuk kfd_release_topology_device(dev); 1825b5c4e40SEvgeny Pinchuk } 1834f449311SHarish Kasiviswanathan } 1845b5c4e40SEvgeny Pinchuk 1854f449311SHarish Kasiviswanathan static void kfd_release_live_view(void) 1864f449311SHarish Kasiviswanathan { 1874f449311SHarish Kasiviswanathan kfd_release_topology_device_list(&topology_device_list); 1885b5c4e40SEvgeny Pinchuk memset(&sys_props, 0, sizeof(sys_props)); 1895b5c4e40SEvgeny Pinchuk } 1905b5c4e40SEvgeny Pinchuk 1914f449311SHarish Kasiviswanathan struct kfd_topology_device *kfd_create_topology_device( 1924f449311SHarish Kasiviswanathan struct list_head *device_list) 1935b5c4e40SEvgeny Pinchuk { 1945b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev; 1955b5c4e40SEvgeny Pinchuk 1965b5c4e40SEvgeny Pinchuk dev = kfd_alloc_struct(dev); 1974eacc26bSKent Russell if (!dev) { 1985b5c4e40SEvgeny Pinchuk pr_err("No memory to allocate a topology device"); 19916b9201cSOded Gabbay return NULL; 2005b5c4e40SEvgeny Pinchuk } 2015b5c4e40SEvgeny Pinchuk 2025b5c4e40SEvgeny Pinchuk INIT_LIST_HEAD(&dev->mem_props); 2035b5c4e40SEvgeny Pinchuk INIT_LIST_HEAD(&dev->cache_props); 2045b5c4e40SEvgeny Pinchuk INIT_LIST_HEAD(&dev->io_link_props); 205f4757347SAmber Lin INIT_LIST_HEAD(&dev->perf_props); 2065b5c4e40SEvgeny Pinchuk 2074f449311SHarish Kasiviswanathan list_add_tail(&dev->list, device_list); 2085b5c4e40SEvgeny Pinchuk 2095b5c4e40SEvgeny Pinchuk return dev; 2105b5c4e40SEvgeny Pinchuk } 2115b5c4e40SEvgeny Pinchuk 2125b5c4e40SEvgeny Pinchuk 21383a13ef5SFelix Kuehling #define sysfs_show_gen_prop(buffer, offs, fmt, ...) \ 21483a13ef5SFelix Kuehling (offs += snprintf(buffer+offs, PAGE_SIZE-offs, \ 21583a13ef5SFelix Kuehling fmt, __VA_ARGS__)) 21683a13ef5SFelix Kuehling #define sysfs_show_32bit_prop(buffer, offs, name, value) \ 21783a13ef5SFelix Kuehling sysfs_show_gen_prop(buffer, offs, "%s %u\n", name, value) 21883a13ef5SFelix Kuehling #define sysfs_show_64bit_prop(buffer, offs, name, value) \ 21983a13ef5SFelix Kuehling sysfs_show_gen_prop(buffer, offs, "%s %llu\n", name, value) 22083a13ef5SFelix Kuehling #define sysfs_show_32bit_val(buffer, offs, value) \ 22183a13ef5SFelix Kuehling sysfs_show_gen_prop(buffer, offs, "%u\n", value) 22283a13ef5SFelix Kuehling #define sysfs_show_str_val(buffer, offs, value) \ 22383a13ef5SFelix Kuehling sysfs_show_gen_prop(buffer, offs, "%s\n", value) 2245b5c4e40SEvgeny Pinchuk 2255b5c4e40SEvgeny Pinchuk static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr, 2265b5c4e40SEvgeny Pinchuk char *buffer) 2275b5c4e40SEvgeny Pinchuk { 22883a13ef5SFelix Kuehling int offs = 0; 2295b5c4e40SEvgeny Pinchuk 2305b5c4e40SEvgeny Pinchuk /* Making sure that the buffer is an empty string */ 2315b5c4e40SEvgeny Pinchuk buffer[0] = 0; 2325b5c4e40SEvgeny Pinchuk 2335b5c4e40SEvgeny Pinchuk if (attr == &sys_props.attr_genid) { 23483a13ef5SFelix Kuehling sysfs_show_32bit_val(buffer, offs, 23583a13ef5SFelix Kuehling sys_props.generation_count); 2365b5c4e40SEvgeny Pinchuk } else if (attr == &sys_props.attr_props) { 23783a13ef5SFelix Kuehling sysfs_show_64bit_prop(buffer, offs, "platform_oem", 2385b5c4e40SEvgeny Pinchuk sys_props.platform_oem); 23983a13ef5SFelix Kuehling sysfs_show_64bit_prop(buffer, offs, "platform_id", 2405b5c4e40SEvgeny Pinchuk sys_props.platform_id); 24183a13ef5SFelix Kuehling sysfs_show_64bit_prop(buffer, offs, "platform_rev", 2425b5c4e40SEvgeny Pinchuk sys_props.platform_rev); 2435b5c4e40SEvgeny Pinchuk } else { 24483a13ef5SFelix Kuehling offs = -EINVAL; 2455b5c4e40SEvgeny Pinchuk } 2465b5c4e40SEvgeny Pinchuk 24783a13ef5SFelix Kuehling return offs; 2485b5c4e40SEvgeny Pinchuk } 2495b5c4e40SEvgeny Pinchuk 2505108d768SYong Zhao static void kfd_topology_kobj_release(struct kobject *kobj) 2515108d768SYong Zhao { 2525108d768SYong Zhao kfree(kobj); 2535108d768SYong Zhao } 2545108d768SYong Zhao 2555b5c4e40SEvgeny Pinchuk static const struct sysfs_ops sysprops_ops = { 2565b5c4e40SEvgeny Pinchuk .show = sysprops_show, 2575b5c4e40SEvgeny Pinchuk }; 2585b5c4e40SEvgeny Pinchuk 2595b5c4e40SEvgeny Pinchuk static struct kobj_type sysprops_type = { 2605108d768SYong Zhao .release = kfd_topology_kobj_release, 2615b5c4e40SEvgeny Pinchuk .sysfs_ops = &sysprops_ops, 2625b5c4e40SEvgeny Pinchuk }; 2635b5c4e40SEvgeny Pinchuk 2645b5c4e40SEvgeny Pinchuk static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr, 2655b5c4e40SEvgeny Pinchuk char *buffer) 2665b5c4e40SEvgeny Pinchuk { 26783a13ef5SFelix Kuehling int offs = 0; 2685b5c4e40SEvgeny Pinchuk struct kfd_iolink_properties *iolink; 2695b5c4e40SEvgeny Pinchuk 2705b5c4e40SEvgeny Pinchuk /* Making sure that the buffer is an empty string */ 2715b5c4e40SEvgeny Pinchuk buffer[0] = 0; 2725b5c4e40SEvgeny Pinchuk 2735b5c4e40SEvgeny Pinchuk iolink = container_of(attr, struct kfd_iolink_properties, attr); 2746b855f7bSHarish Kasiviswanathan if (iolink->gpu && kfd_devcgroup_check_permission(iolink->gpu)) 2756b855f7bSHarish Kasiviswanathan return -EPERM; 27683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "type", iolink->iolink_type); 27783a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "version_major", iolink->ver_maj); 27883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "version_minor", iolink->ver_min); 27983a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "node_from", iolink->node_from); 28083a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "node_to", iolink->node_to); 28183a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "weight", iolink->weight); 28283a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "min_latency", iolink->min_latency); 28383a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "max_latency", iolink->max_latency); 28483a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "min_bandwidth", 28583a13ef5SFelix Kuehling iolink->min_bandwidth); 28683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "max_bandwidth", 28783a13ef5SFelix Kuehling iolink->max_bandwidth); 28883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "recommended_transfer_size", 2895b5c4e40SEvgeny Pinchuk iolink->rec_transfer_size); 29083a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "flags", iolink->flags); 2915b5c4e40SEvgeny Pinchuk 29283a13ef5SFelix Kuehling return offs; 2935b5c4e40SEvgeny Pinchuk } 2945b5c4e40SEvgeny Pinchuk 2955b5c4e40SEvgeny Pinchuk static const struct sysfs_ops iolink_ops = { 2965b5c4e40SEvgeny Pinchuk .show = iolink_show, 2975b5c4e40SEvgeny Pinchuk }; 2985b5c4e40SEvgeny Pinchuk 2995b5c4e40SEvgeny Pinchuk static struct kobj_type iolink_type = { 3005108d768SYong Zhao .release = kfd_topology_kobj_release, 3015b5c4e40SEvgeny Pinchuk .sysfs_ops = &iolink_ops, 3025b5c4e40SEvgeny Pinchuk }; 3035b5c4e40SEvgeny Pinchuk 3045b5c4e40SEvgeny Pinchuk static ssize_t mem_show(struct kobject *kobj, struct attribute *attr, 3055b5c4e40SEvgeny Pinchuk char *buffer) 3065b5c4e40SEvgeny Pinchuk { 30783a13ef5SFelix Kuehling int offs = 0; 3085b5c4e40SEvgeny Pinchuk struct kfd_mem_properties *mem; 3095b5c4e40SEvgeny Pinchuk 3105b5c4e40SEvgeny Pinchuk /* Making sure that the buffer is an empty string */ 3115b5c4e40SEvgeny Pinchuk buffer[0] = 0; 3125b5c4e40SEvgeny Pinchuk 3135b5c4e40SEvgeny Pinchuk mem = container_of(attr, struct kfd_mem_properties, attr); 3146b855f7bSHarish Kasiviswanathan if (mem->gpu && kfd_devcgroup_check_permission(mem->gpu)) 3156b855f7bSHarish Kasiviswanathan return -EPERM; 31683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "heap_type", mem->heap_type); 31783a13ef5SFelix Kuehling sysfs_show_64bit_prop(buffer, offs, "size_in_bytes", 31883a13ef5SFelix Kuehling mem->size_in_bytes); 31983a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "flags", mem->flags); 32083a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "width", mem->width); 32183a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "mem_clk_max", 32283a13ef5SFelix Kuehling mem->mem_clk_max); 3235b5c4e40SEvgeny Pinchuk 32483a13ef5SFelix Kuehling return offs; 3255b5c4e40SEvgeny Pinchuk } 3265b5c4e40SEvgeny Pinchuk 3275b5c4e40SEvgeny Pinchuk static const struct sysfs_ops mem_ops = { 3285b5c4e40SEvgeny Pinchuk .show = mem_show, 3295b5c4e40SEvgeny Pinchuk }; 3305b5c4e40SEvgeny Pinchuk 3315b5c4e40SEvgeny Pinchuk static struct kobj_type mem_type = { 3325108d768SYong Zhao .release = kfd_topology_kobj_release, 3335b5c4e40SEvgeny Pinchuk .sysfs_ops = &mem_ops, 3345b5c4e40SEvgeny Pinchuk }; 3355b5c4e40SEvgeny Pinchuk 3365b5c4e40SEvgeny Pinchuk static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr, 3375b5c4e40SEvgeny Pinchuk char *buffer) 3385b5c4e40SEvgeny Pinchuk { 33983a13ef5SFelix Kuehling int offs = 0; 340bc0c75a3SHarish Kasiviswanathan uint32_t i, j; 3415b5c4e40SEvgeny Pinchuk struct kfd_cache_properties *cache; 3425b5c4e40SEvgeny Pinchuk 3435b5c4e40SEvgeny Pinchuk /* Making sure that the buffer is an empty string */ 3445b5c4e40SEvgeny Pinchuk buffer[0] = 0; 3455b5c4e40SEvgeny Pinchuk 3465b5c4e40SEvgeny Pinchuk cache = container_of(attr, struct kfd_cache_properties, attr); 3476b855f7bSHarish Kasiviswanathan if (cache->gpu && kfd_devcgroup_check_permission(cache->gpu)) 3486b855f7bSHarish Kasiviswanathan return -EPERM; 34983a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "processor_id_low", 3505b5c4e40SEvgeny Pinchuk cache->processor_id_low); 35183a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "level", cache->cache_level); 35283a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "size", cache->cache_size); 35383a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "cache_line_size", 35483a13ef5SFelix Kuehling cache->cacheline_size); 35583a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "cache_lines_per_tag", 3565b5c4e40SEvgeny Pinchuk cache->cachelines_per_tag); 35783a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "association", cache->cache_assoc); 35883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "latency", cache->cache_latency); 35983a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "type", cache->cache_type); 36083a13ef5SFelix Kuehling offs += snprintf(buffer+offs, PAGE_SIZE-offs, "sibling_map "); 361bc0c75a3SHarish Kasiviswanathan for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++) 36283a13ef5SFelix Kuehling for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++) 363bc0c75a3SHarish Kasiviswanathan /* Check each bit */ 36483a13ef5SFelix Kuehling offs += snprintf(buffer+offs, PAGE_SIZE-offs, "%d,", 36583a13ef5SFelix Kuehling (cache->sibling_map[i] >> j) & 1); 36683a13ef5SFelix Kuehling 367bc0c75a3SHarish Kasiviswanathan /* Replace the last "," with end of line */ 36883a13ef5SFelix Kuehling buffer[offs-1] = '\n'; 36983a13ef5SFelix Kuehling return offs; 3705b5c4e40SEvgeny Pinchuk } 3715b5c4e40SEvgeny Pinchuk 3725b5c4e40SEvgeny Pinchuk static const struct sysfs_ops cache_ops = { 3735b5c4e40SEvgeny Pinchuk .show = kfd_cache_show, 3745b5c4e40SEvgeny Pinchuk }; 3755b5c4e40SEvgeny Pinchuk 3765b5c4e40SEvgeny Pinchuk static struct kobj_type cache_type = { 3775108d768SYong Zhao .release = kfd_topology_kobj_release, 3785b5c4e40SEvgeny Pinchuk .sysfs_ops = &cache_ops, 3795b5c4e40SEvgeny Pinchuk }; 3805b5c4e40SEvgeny Pinchuk 381f4757347SAmber Lin /****** Sysfs of Performance Counters ******/ 382f4757347SAmber Lin 383f4757347SAmber Lin struct kfd_perf_attr { 384f4757347SAmber Lin struct kobj_attribute attr; 385f4757347SAmber Lin uint32_t data; 386f4757347SAmber Lin }; 387f4757347SAmber Lin 388f4757347SAmber Lin static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs, 389f4757347SAmber Lin char *buf) 390f4757347SAmber Lin { 39183a13ef5SFelix Kuehling int offs = 0; 392f4757347SAmber Lin struct kfd_perf_attr *attr; 393f4757347SAmber Lin 394f4757347SAmber Lin buf[0] = 0; 395f4757347SAmber Lin attr = container_of(attrs, struct kfd_perf_attr, attr); 396f4757347SAmber Lin if (!attr->data) /* invalid data for PMC */ 397f4757347SAmber Lin return 0; 398f4757347SAmber Lin else 39983a13ef5SFelix Kuehling return sysfs_show_32bit_val(buf, offs, attr->data); 400f4757347SAmber Lin } 401f4757347SAmber Lin 402f4757347SAmber Lin #define KFD_PERF_DESC(_name, _data) \ 403f4757347SAmber Lin { \ 404f4757347SAmber Lin .attr = __ATTR(_name, 0444, perf_show, NULL), \ 405f4757347SAmber Lin .data = _data, \ 406f4757347SAmber Lin } 407f4757347SAmber Lin 408f4757347SAmber Lin static struct kfd_perf_attr perf_attr_iommu[] = { 409f4757347SAmber Lin KFD_PERF_DESC(max_concurrent, 0), 410f4757347SAmber Lin KFD_PERF_DESC(num_counters, 0), 411f4757347SAmber Lin KFD_PERF_DESC(counter_ids, 0), 412f4757347SAmber Lin }; 413f4757347SAmber Lin /****************************************/ 414f4757347SAmber Lin 4155b5c4e40SEvgeny Pinchuk static ssize_t node_show(struct kobject *kobj, struct attribute *attr, 4165b5c4e40SEvgeny Pinchuk char *buffer) 4175b5c4e40SEvgeny Pinchuk { 41883a13ef5SFelix Kuehling int offs = 0; 4195b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev; 420f7c826adSAlexey Skidanov uint32_t log_max_watch_addr; 4215b5c4e40SEvgeny Pinchuk 4225b5c4e40SEvgeny Pinchuk /* Making sure that the buffer is an empty string */ 4235b5c4e40SEvgeny Pinchuk buffer[0] = 0; 4245b5c4e40SEvgeny Pinchuk 4255b5c4e40SEvgeny Pinchuk if (strcmp(attr->name, "gpu_id") == 0) { 4265b5c4e40SEvgeny Pinchuk dev = container_of(attr, struct kfd_topology_device, 4275b5c4e40SEvgeny Pinchuk attr_gpuid); 4286b855f7bSHarish Kasiviswanathan if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu)) 4296b855f7bSHarish Kasiviswanathan return -EPERM; 43083a13ef5SFelix Kuehling return sysfs_show_32bit_val(buffer, offs, dev->gpu_id); 431f7c826adSAlexey Skidanov } 432f7c826adSAlexey Skidanov 433f7c826adSAlexey Skidanov if (strcmp(attr->name, "name") == 0) { 4345b5c4e40SEvgeny Pinchuk dev = container_of(attr, struct kfd_topology_device, 4355b5c4e40SEvgeny Pinchuk attr_name); 436c181159aSYong Zhao 4376b855f7bSHarish Kasiviswanathan if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu)) 4386b855f7bSHarish Kasiviswanathan return -EPERM; 43983a13ef5SFelix Kuehling return sysfs_show_str_val(buffer, offs, dev->node_props.name); 440f7c826adSAlexey Skidanov } 441f7c826adSAlexey Skidanov 4425b5c4e40SEvgeny Pinchuk dev = container_of(attr, struct kfd_topology_device, 4435b5c4e40SEvgeny Pinchuk attr_props); 4446b855f7bSHarish Kasiviswanathan if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu)) 4456b855f7bSHarish Kasiviswanathan return -EPERM; 44683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "cpu_cores_count", 4475b5c4e40SEvgeny Pinchuk dev->node_props.cpu_cores_count); 44883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "simd_count", 4496127896fSHuang Rui dev->gpu ? dev->node_props.simd_count : 0); 45083a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "mem_banks_count", 4515b5c4e40SEvgeny Pinchuk dev->node_props.mem_banks_count); 45283a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "caches_count", 4535b5c4e40SEvgeny Pinchuk dev->node_props.caches_count); 45483a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "io_links_count", 4555b5c4e40SEvgeny Pinchuk dev->node_props.io_links_count); 45683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "cpu_core_id_base", 4575b5c4e40SEvgeny Pinchuk dev->node_props.cpu_core_id_base); 45883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "simd_id_base", 4595b5c4e40SEvgeny Pinchuk dev->node_props.simd_id_base); 46083a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "max_waves_per_simd", 4615b5c4e40SEvgeny Pinchuk dev->node_props.max_waves_per_simd); 46283a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "lds_size_in_kb", 4635b5c4e40SEvgeny Pinchuk dev->node_props.lds_size_in_kb); 46483a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "gds_size_in_kb", 4655b5c4e40SEvgeny Pinchuk dev->node_props.gds_size_in_kb); 46683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "num_gws", 46729e76462SOak Zeng dev->node_props.num_gws); 46883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "wave_front_size", 4695b5c4e40SEvgeny Pinchuk dev->node_props.wave_front_size); 47083a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "array_count", 4715b5c4e40SEvgeny Pinchuk dev->node_props.array_count); 47283a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "simd_arrays_per_engine", 4735b5c4e40SEvgeny Pinchuk dev->node_props.simd_arrays_per_engine); 47483a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "cu_per_simd_array", 4755b5c4e40SEvgeny Pinchuk dev->node_props.cu_per_simd_array); 47683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "simd_per_cu", 4775b5c4e40SEvgeny Pinchuk dev->node_props.simd_per_cu); 47883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "max_slots_scratch_cu", 4795b5c4e40SEvgeny Pinchuk dev->node_props.max_slots_scratch_cu); 48083a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "vendor_id", 4815b5c4e40SEvgeny Pinchuk dev->node_props.vendor_id); 48283a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "device_id", 4835b5c4e40SEvgeny Pinchuk dev->node_props.device_id); 48483a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "location_id", 4855b5c4e40SEvgeny Pinchuk dev->node_props.location_id); 48683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "domain", 4873e58e95aSOri Messinger dev->node_props.domain); 48883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "drm_render_minor", 4897c9b7171SOak Zeng dev->node_props.drm_render_minor); 49083a13ef5SFelix Kuehling sysfs_show_64bit_prop(buffer, offs, "hive_id", 4910c1690e3SShaoyun Liu dev->node_props.hive_id); 49283a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "num_sdma_engines", 49314568cf6SOak Zeng dev->node_props.num_sdma_engines); 49483a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "num_sdma_xgmi_engines", 49514568cf6SOak Zeng dev->node_props.num_sdma_xgmi_engines); 49683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "num_sdma_queues_per_engine", 497bb71c74dSHuang Rui dev->node_props.num_sdma_queues_per_engine); 49883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "num_cp_queues", 499f4feb9faSHuang Rui dev->node_props.num_cp_queues); 5005b5c4e40SEvgeny Pinchuk 5015b5c4e40SEvgeny Pinchuk if (dev->gpu) { 502f7c826adSAlexey Skidanov log_max_watch_addr = 503f7c826adSAlexey Skidanov __ilog2_u32(dev->gpu->device_info->num_of_watch_points); 504f7c826adSAlexey Skidanov 505f7c826adSAlexey Skidanov if (log_max_watch_addr) { 506f7c826adSAlexey Skidanov dev->node_props.capability |= 507f7c826adSAlexey Skidanov HSA_CAP_WATCH_POINTS_SUPPORTED; 508f7c826adSAlexey Skidanov 509f7c826adSAlexey Skidanov dev->node_props.capability |= 510f7c826adSAlexey Skidanov ((log_max_watch_addr << 511f7c826adSAlexey Skidanov HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) & 512f7c826adSAlexey Skidanov HSA_CAP_WATCH_POINTS_TOTALBITS_MASK); 513f7c826adSAlexey Skidanov } 514f7c826adSAlexey Skidanov 515413e85d5SBen Goz if (dev->gpu->device_info->asic_family == CHIP_TONGA) 516413e85d5SBen Goz dev->node_props.capability |= 517413e85d5SBen Goz HSA_CAP_AQL_QUEUE_DOUBLE_MAP; 518413e85d5SBen Goz 51983a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_fcompute", 5203a87177eSHarish Kasiviswanathan dev->node_props.max_engine_clk_fcompute); 52142e08c78SOded Gabbay 52283a13ef5SFelix Kuehling sysfs_show_64bit_prop(buffer, offs, "local_mem_size", 0ULL); 523f1386fbcSOded Gabbay 52483a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "fw_version", 5255ade6c9cSFelix Kuehling dev->gpu->mec_fw_version); 52683a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "capability", 527826f5de8SAlexey Skidanov dev->node_props.capability); 52883a13ef5SFelix Kuehling sysfs_show_32bit_prop(buffer, offs, "sdma_fw_version", 5295ade6c9cSFelix Kuehling dev->gpu->sdma_fw_version); 53011964258SKent Russell sysfs_show_64bit_prop(buffer, offs, "unique_id", 53111964258SKent Russell amdgpu_amdkfd_get_unique_id(dev->gpu->kgd)); 53211964258SKent Russell 5335b5c4e40SEvgeny Pinchuk } 5345b5c4e40SEvgeny Pinchuk 53583a13ef5SFelix Kuehling return sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_ccompute", 5365b5c4e40SEvgeny Pinchuk cpufreq_quick_get_max(0)/1000); 5375b5c4e40SEvgeny Pinchuk } 5385b5c4e40SEvgeny Pinchuk 5395b5c4e40SEvgeny Pinchuk static const struct sysfs_ops node_ops = { 5405b5c4e40SEvgeny Pinchuk .show = node_show, 5415b5c4e40SEvgeny Pinchuk }; 5425b5c4e40SEvgeny Pinchuk 5435b5c4e40SEvgeny Pinchuk static struct kobj_type node_type = { 5445108d768SYong Zhao .release = kfd_topology_kobj_release, 5455b5c4e40SEvgeny Pinchuk .sysfs_ops = &node_ops, 5465b5c4e40SEvgeny Pinchuk }; 5475b5c4e40SEvgeny Pinchuk 5485b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr) 5495b5c4e40SEvgeny Pinchuk { 5505b5c4e40SEvgeny Pinchuk sysfs_remove_file(kobj, attr); 5515b5c4e40SEvgeny Pinchuk kobject_del(kobj); 5525b5c4e40SEvgeny Pinchuk kobject_put(kobj); 5535b5c4e40SEvgeny Pinchuk } 5545b5c4e40SEvgeny Pinchuk 5555b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev) 5565b5c4e40SEvgeny Pinchuk { 5575b5c4e40SEvgeny Pinchuk struct kfd_iolink_properties *iolink; 5585b5c4e40SEvgeny Pinchuk struct kfd_cache_properties *cache; 5595b5c4e40SEvgeny Pinchuk struct kfd_mem_properties *mem; 560f4757347SAmber Lin struct kfd_perf_properties *perf; 5615b5c4e40SEvgeny Pinchuk 5625b5c4e40SEvgeny Pinchuk if (dev->kobj_iolink) { 5635b5c4e40SEvgeny Pinchuk list_for_each_entry(iolink, &dev->io_link_props, list) 5645b5c4e40SEvgeny Pinchuk if (iolink->kobj) { 5655b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_file(iolink->kobj, 5665b5c4e40SEvgeny Pinchuk &iolink->attr); 56716b9201cSOded Gabbay iolink->kobj = NULL; 5685b5c4e40SEvgeny Pinchuk } 5695b5c4e40SEvgeny Pinchuk kobject_del(dev->kobj_iolink); 5705b5c4e40SEvgeny Pinchuk kobject_put(dev->kobj_iolink); 57116b9201cSOded Gabbay dev->kobj_iolink = NULL; 5725b5c4e40SEvgeny Pinchuk } 5735b5c4e40SEvgeny Pinchuk 5745b5c4e40SEvgeny Pinchuk if (dev->kobj_cache) { 5755b5c4e40SEvgeny Pinchuk list_for_each_entry(cache, &dev->cache_props, list) 5765b5c4e40SEvgeny Pinchuk if (cache->kobj) { 5775b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_file(cache->kobj, 5785b5c4e40SEvgeny Pinchuk &cache->attr); 57916b9201cSOded Gabbay cache->kobj = NULL; 5805b5c4e40SEvgeny Pinchuk } 5815b5c4e40SEvgeny Pinchuk kobject_del(dev->kobj_cache); 5825b5c4e40SEvgeny Pinchuk kobject_put(dev->kobj_cache); 58316b9201cSOded Gabbay dev->kobj_cache = NULL; 5845b5c4e40SEvgeny Pinchuk } 5855b5c4e40SEvgeny Pinchuk 5865b5c4e40SEvgeny Pinchuk if (dev->kobj_mem) { 5875b5c4e40SEvgeny Pinchuk list_for_each_entry(mem, &dev->mem_props, list) 5885b5c4e40SEvgeny Pinchuk if (mem->kobj) { 5895b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_file(mem->kobj, &mem->attr); 59016b9201cSOded Gabbay mem->kobj = NULL; 5915b5c4e40SEvgeny Pinchuk } 5925b5c4e40SEvgeny Pinchuk kobject_del(dev->kobj_mem); 5935b5c4e40SEvgeny Pinchuk kobject_put(dev->kobj_mem); 59416b9201cSOded Gabbay dev->kobj_mem = NULL; 5955b5c4e40SEvgeny Pinchuk } 5965b5c4e40SEvgeny Pinchuk 597f4757347SAmber Lin if (dev->kobj_perf) { 598f4757347SAmber Lin list_for_each_entry(perf, &dev->perf_props, list) { 599f4757347SAmber Lin kfree(perf->attr_group); 600f4757347SAmber Lin perf->attr_group = NULL; 601f4757347SAmber Lin } 602f4757347SAmber Lin kobject_del(dev->kobj_perf); 603f4757347SAmber Lin kobject_put(dev->kobj_perf); 604f4757347SAmber Lin dev->kobj_perf = NULL; 605f4757347SAmber Lin } 606f4757347SAmber Lin 6075b5c4e40SEvgeny Pinchuk if (dev->kobj_node) { 6085b5c4e40SEvgeny Pinchuk sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid); 6095b5c4e40SEvgeny Pinchuk sysfs_remove_file(dev->kobj_node, &dev->attr_name); 6105b5c4e40SEvgeny Pinchuk sysfs_remove_file(dev->kobj_node, &dev->attr_props); 6115b5c4e40SEvgeny Pinchuk kobject_del(dev->kobj_node); 6125b5c4e40SEvgeny Pinchuk kobject_put(dev->kobj_node); 61316b9201cSOded Gabbay dev->kobj_node = NULL; 6145b5c4e40SEvgeny Pinchuk } 6155b5c4e40SEvgeny Pinchuk } 6165b5c4e40SEvgeny Pinchuk 6175b5c4e40SEvgeny Pinchuk static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev, 6185b5c4e40SEvgeny Pinchuk uint32_t id) 6195b5c4e40SEvgeny Pinchuk { 6205b5c4e40SEvgeny Pinchuk struct kfd_iolink_properties *iolink; 6215b5c4e40SEvgeny Pinchuk struct kfd_cache_properties *cache; 6225b5c4e40SEvgeny Pinchuk struct kfd_mem_properties *mem; 623f4757347SAmber Lin struct kfd_perf_properties *perf; 6245b5c4e40SEvgeny Pinchuk int ret; 625f4757347SAmber Lin uint32_t i, num_attrs; 626f4757347SAmber Lin struct attribute **attrs; 6275b5c4e40SEvgeny Pinchuk 62832fa8219SFelix Kuehling if (WARN_ON(dev->kobj_node)) 62932fa8219SFelix Kuehling return -EEXIST; 63032fa8219SFelix Kuehling 6315b5c4e40SEvgeny Pinchuk /* 6325b5c4e40SEvgeny Pinchuk * Creating the sysfs folders 6335b5c4e40SEvgeny Pinchuk */ 6345b5c4e40SEvgeny Pinchuk dev->kobj_node = kfd_alloc_struct(dev->kobj_node); 6355b5c4e40SEvgeny Pinchuk if (!dev->kobj_node) 6365b5c4e40SEvgeny Pinchuk return -ENOMEM; 6375b5c4e40SEvgeny Pinchuk 6385b5c4e40SEvgeny Pinchuk ret = kobject_init_and_add(dev->kobj_node, &node_type, 6395b5c4e40SEvgeny Pinchuk sys_props.kobj_nodes, "%d", id); 64020eca012SQiushi Wu if (ret < 0) { 64120eca012SQiushi Wu kobject_put(dev->kobj_node); 6425b5c4e40SEvgeny Pinchuk return ret; 64320eca012SQiushi Wu } 6445b5c4e40SEvgeny Pinchuk 6455b5c4e40SEvgeny Pinchuk dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node); 6465b5c4e40SEvgeny Pinchuk if (!dev->kobj_mem) 6475b5c4e40SEvgeny Pinchuk return -ENOMEM; 6485b5c4e40SEvgeny Pinchuk 6495b5c4e40SEvgeny Pinchuk dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node); 6505b5c4e40SEvgeny Pinchuk if (!dev->kobj_cache) 6515b5c4e40SEvgeny Pinchuk return -ENOMEM; 6525b5c4e40SEvgeny Pinchuk 6535b5c4e40SEvgeny Pinchuk dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node); 6545b5c4e40SEvgeny Pinchuk if (!dev->kobj_iolink) 6555b5c4e40SEvgeny Pinchuk return -ENOMEM; 6565b5c4e40SEvgeny Pinchuk 657f4757347SAmber Lin dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node); 658f4757347SAmber Lin if (!dev->kobj_perf) 659f4757347SAmber Lin return -ENOMEM; 660f4757347SAmber Lin 6615b5c4e40SEvgeny Pinchuk /* 6625b5c4e40SEvgeny Pinchuk * Creating sysfs files for node properties 6635b5c4e40SEvgeny Pinchuk */ 6645b5c4e40SEvgeny Pinchuk dev->attr_gpuid.name = "gpu_id"; 6655b5c4e40SEvgeny Pinchuk dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE; 6665b5c4e40SEvgeny Pinchuk sysfs_attr_init(&dev->attr_gpuid); 6675b5c4e40SEvgeny Pinchuk dev->attr_name.name = "name"; 6685b5c4e40SEvgeny Pinchuk dev->attr_name.mode = KFD_SYSFS_FILE_MODE; 6695b5c4e40SEvgeny Pinchuk sysfs_attr_init(&dev->attr_name); 6705b5c4e40SEvgeny Pinchuk dev->attr_props.name = "properties"; 6715b5c4e40SEvgeny Pinchuk dev->attr_props.mode = KFD_SYSFS_FILE_MODE; 6725b5c4e40SEvgeny Pinchuk sysfs_attr_init(&dev->attr_props); 6735b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid); 6745b5c4e40SEvgeny Pinchuk if (ret < 0) 6755b5c4e40SEvgeny Pinchuk return ret; 6765b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(dev->kobj_node, &dev->attr_name); 6775b5c4e40SEvgeny Pinchuk if (ret < 0) 6785b5c4e40SEvgeny Pinchuk return ret; 6795b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(dev->kobj_node, &dev->attr_props); 6805b5c4e40SEvgeny Pinchuk if (ret < 0) 6815b5c4e40SEvgeny Pinchuk return ret; 6825b5c4e40SEvgeny Pinchuk 6835b5c4e40SEvgeny Pinchuk i = 0; 6845b5c4e40SEvgeny Pinchuk list_for_each_entry(mem, &dev->mem_props, list) { 6855b5c4e40SEvgeny Pinchuk mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL); 6865b5c4e40SEvgeny Pinchuk if (!mem->kobj) 6875b5c4e40SEvgeny Pinchuk return -ENOMEM; 6885b5c4e40SEvgeny Pinchuk ret = kobject_init_and_add(mem->kobj, &mem_type, 6895b5c4e40SEvgeny Pinchuk dev->kobj_mem, "%d", i); 69020eca012SQiushi Wu if (ret < 0) { 69120eca012SQiushi Wu kobject_put(mem->kobj); 6925b5c4e40SEvgeny Pinchuk return ret; 69320eca012SQiushi Wu } 6945b5c4e40SEvgeny Pinchuk 6955b5c4e40SEvgeny Pinchuk mem->attr.name = "properties"; 6965b5c4e40SEvgeny Pinchuk mem->attr.mode = KFD_SYSFS_FILE_MODE; 6975b5c4e40SEvgeny Pinchuk sysfs_attr_init(&mem->attr); 6985b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(mem->kobj, &mem->attr); 6995b5c4e40SEvgeny Pinchuk if (ret < 0) 7005b5c4e40SEvgeny Pinchuk return ret; 7015b5c4e40SEvgeny Pinchuk i++; 7025b5c4e40SEvgeny Pinchuk } 7035b5c4e40SEvgeny Pinchuk 7045b5c4e40SEvgeny Pinchuk i = 0; 7055b5c4e40SEvgeny Pinchuk list_for_each_entry(cache, &dev->cache_props, list) { 7065b5c4e40SEvgeny Pinchuk cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL); 7075b5c4e40SEvgeny Pinchuk if (!cache->kobj) 7085b5c4e40SEvgeny Pinchuk return -ENOMEM; 7095b5c4e40SEvgeny Pinchuk ret = kobject_init_and_add(cache->kobj, &cache_type, 7105b5c4e40SEvgeny Pinchuk dev->kobj_cache, "%d", i); 71120eca012SQiushi Wu if (ret < 0) { 71220eca012SQiushi Wu kobject_put(cache->kobj); 7135b5c4e40SEvgeny Pinchuk return ret; 71420eca012SQiushi Wu } 7155b5c4e40SEvgeny Pinchuk 7165b5c4e40SEvgeny Pinchuk cache->attr.name = "properties"; 7175b5c4e40SEvgeny Pinchuk cache->attr.mode = KFD_SYSFS_FILE_MODE; 7185b5c4e40SEvgeny Pinchuk sysfs_attr_init(&cache->attr); 7195b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(cache->kobj, &cache->attr); 7205b5c4e40SEvgeny Pinchuk if (ret < 0) 7215b5c4e40SEvgeny Pinchuk return ret; 7225b5c4e40SEvgeny Pinchuk i++; 7235b5c4e40SEvgeny Pinchuk } 7245b5c4e40SEvgeny Pinchuk 7255b5c4e40SEvgeny Pinchuk i = 0; 7265b5c4e40SEvgeny Pinchuk list_for_each_entry(iolink, &dev->io_link_props, list) { 7275b5c4e40SEvgeny Pinchuk iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL); 7285b5c4e40SEvgeny Pinchuk if (!iolink->kobj) 7295b5c4e40SEvgeny Pinchuk return -ENOMEM; 7305b5c4e40SEvgeny Pinchuk ret = kobject_init_and_add(iolink->kobj, &iolink_type, 7315b5c4e40SEvgeny Pinchuk dev->kobj_iolink, "%d", i); 73220eca012SQiushi Wu if (ret < 0) { 73320eca012SQiushi Wu kobject_put(iolink->kobj); 7345b5c4e40SEvgeny Pinchuk return ret; 73520eca012SQiushi Wu } 7365b5c4e40SEvgeny Pinchuk 7375b5c4e40SEvgeny Pinchuk iolink->attr.name = "properties"; 7385b5c4e40SEvgeny Pinchuk iolink->attr.mode = KFD_SYSFS_FILE_MODE; 7395b5c4e40SEvgeny Pinchuk sysfs_attr_init(&iolink->attr); 7405b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(iolink->kobj, &iolink->attr); 7415b5c4e40SEvgeny Pinchuk if (ret < 0) 7425b5c4e40SEvgeny Pinchuk return ret; 7435b5c4e40SEvgeny Pinchuk i++; 7445b5c4e40SEvgeny Pinchuk } 7455b5c4e40SEvgeny Pinchuk 746f4757347SAmber Lin /* All hardware blocks have the same number of attributes. */ 7473f866f5fSGustavo A. R. Silva num_attrs = ARRAY_SIZE(perf_attr_iommu); 748f4757347SAmber Lin list_for_each_entry(perf, &dev->perf_props, list) { 749f4757347SAmber Lin perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr) 750f4757347SAmber Lin * num_attrs + sizeof(struct attribute_group), 751f4757347SAmber Lin GFP_KERNEL); 752f4757347SAmber Lin if (!perf->attr_group) 753f4757347SAmber Lin return -ENOMEM; 754f4757347SAmber Lin 755f4757347SAmber Lin attrs = (struct attribute **)(perf->attr_group + 1); 756f4757347SAmber Lin if (!strcmp(perf->block_name, "iommu")) { 757f4757347SAmber Lin /* Information of IOMMU's num_counters and counter_ids is shown 758f4757347SAmber Lin * under /sys/bus/event_source/devices/amd_iommu. We don't 759f4757347SAmber Lin * duplicate here. 760f4757347SAmber Lin */ 761f4757347SAmber Lin perf_attr_iommu[0].data = perf->max_concurrent; 762f4757347SAmber Lin for (i = 0; i < num_attrs; i++) 763f4757347SAmber Lin attrs[i] = &perf_attr_iommu[i].attr.attr; 764f4757347SAmber Lin } 765f4757347SAmber Lin perf->attr_group->name = perf->block_name; 766f4757347SAmber Lin perf->attr_group->attrs = attrs; 767f4757347SAmber Lin ret = sysfs_create_group(dev->kobj_perf, perf->attr_group); 768f4757347SAmber Lin if (ret < 0) 769f4757347SAmber Lin return ret; 770f4757347SAmber Lin } 771f4757347SAmber Lin 7725b5c4e40SEvgeny Pinchuk return 0; 7735b5c4e40SEvgeny Pinchuk } 7745b5c4e40SEvgeny Pinchuk 7753a87177eSHarish Kasiviswanathan /* Called with write topology lock acquired */ 7765b5c4e40SEvgeny Pinchuk static int kfd_build_sysfs_node_tree(void) 7775b5c4e40SEvgeny Pinchuk { 7785b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev; 7795b5c4e40SEvgeny Pinchuk int ret; 7805b5c4e40SEvgeny Pinchuk uint32_t i = 0; 7815b5c4e40SEvgeny Pinchuk 7825b5c4e40SEvgeny Pinchuk list_for_each_entry(dev, &topology_device_list, list) { 7838dfead6cSBen Goz ret = kfd_build_sysfs_node_entry(dev, i); 7845b5c4e40SEvgeny Pinchuk if (ret < 0) 7855b5c4e40SEvgeny Pinchuk return ret; 7865b5c4e40SEvgeny Pinchuk i++; 7875b5c4e40SEvgeny Pinchuk } 7885b5c4e40SEvgeny Pinchuk 7895b5c4e40SEvgeny Pinchuk return 0; 7905b5c4e40SEvgeny Pinchuk } 7915b5c4e40SEvgeny Pinchuk 7923a87177eSHarish Kasiviswanathan /* Called with write topology lock acquired */ 7935b5c4e40SEvgeny Pinchuk static void kfd_remove_sysfs_node_tree(void) 7945b5c4e40SEvgeny Pinchuk { 7955b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev; 7965b5c4e40SEvgeny Pinchuk 7975b5c4e40SEvgeny Pinchuk list_for_each_entry(dev, &topology_device_list, list) 7985b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_node_entry(dev); 7995b5c4e40SEvgeny Pinchuk } 8005b5c4e40SEvgeny Pinchuk 8015b5c4e40SEvgeny Pinchuk static int kfd_topology_update_sysfs(void) 8025b5c4e40SEvgeny Pinchuk { 8035b5c4e40SEvgeny Pinchuk int ret; 8045b5c4e40SEvgeny Pinchuk 8054eacc26bSKent Russell if (!sys_props.kobj_topology) { 8065b5c4e40SEvgeny Pinchuk sys_props.kobj_topology = 8075b5c4e40SEvgeny Pinchuk kfd_alloc_struct(sys_props.kobj_topology); 8085b5c4e40SEvgeny Pinchuk if (!sys_props.kobj_topology) 8095b5c4e40SEvgeny Pinchuk return -ENOMEM; 8105b5c4e40SEvgeny Pinchuk 8115b5c4e40SEvgeny Pinchuk ret = kobject_init_and_add(sys_props.kobj_topology, 8125b5c4e40SEvgeny Pinchuk &sysprops_type, &kfd_device->kobj, 8135b5c4e40SEvgeny Pinchuk "topology"); 81420eca012SQiushi Wu if (ret < 0) { 81520eca012SQiushi Wu kobject_put(sys_props.kobj_topology); 8165b5c4e40SEvgeny Pinchuk return ret; 81720eca012SQiushi Wu } 8185b5c4e40SEvgeny Pinchuk 8195b5c4e40SEvgeny Pinchuk sys_props.kobj_nodes = kobject_create_and_add("nodes", 8205b5c4e40SEvgeny Pinchuk sys_props.kobj_topology); 8215b5c4e40SEvgeny Pinchuk if (!sys_props.kobj_nodes) 8225b5c4e40SEvgeny Pinchuk return -ENOMEM; 8235b5c4e40SEvgeny Pinchuk 8245b5c4e40SEvgeny Pinchuk sys_props.attr_genid.name = "generation_id"; 8255b5c4e40SEvgeny Pinchuk sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE; 8265b5c4e40SEvgeny Pinchuk sysfs_attr_init(&sys_props.attr_genid); 8275b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(sys_props.kobj_topology, 8285b5c4e40SEvgeny Pinchuk &sys_props.attr_genid); 8295b5c4e40SEvgeny Pinchuk if (ret < 0) 8305b5c4e40SEvgeny Pinchuk return ret; 8315b5c4e40SEvgeny Pinchuk 8325b5c4e40SEvgeny Pinchuk sys_props.attr_props.name = "system_properties"; 8335b5c4e40SEvgeny Pinchuk sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE; 8345b5c4e40SEvgeny Pinchuk sysfs_attr_init(&sys_props.attr_props); 8355b5c4e40SEvgeny Pinchuk ret = sysfs_create_file(sys_props.kobj_topology, 8365b5c4e40SEvgeny Pinchuk &sys_props.attr_props); 8375b5c4e40SEvgeny Pinchuk if (ret < 0) 8385b5c4e40SEvgeny Pinchuk return ret; 8395b5c4e40SEvgeny Pinchuk } 8405b5c4e40SEvgeny Pinchuk 8415b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_node_tree(); 8425b5c4e40SEvgeny Pinchuk 8435b5c4e40SEvgeny Pinchuk return kfd_build_sysfs_node_tree(); 8445b5c4e40SEvgeny Pinchuk } 8455b5c4e40SEvgeny Pinchuk 8465b5c4e40SEvgeny Pinchuk static void kfd_topology_release_sysfs(void) 8475b5c4e40SEvgeny Pinchuk { 8485b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_node_tree(); 8495b5c4e40SEvgeny Pinchuk if (sys_props.kobj_topology) { 8505b5c4e40SEvgeny Pinchuk sysfs_remove_file(sys_props.kobj_topology, 8515b5c4e40SEvgeny Pinchuk &sys_props.attr_genid); 8525b5c4e40SEvgeny Pinchuk sysfs_remove_file(sys_props.kobj_topology, 8535b5c4e40SEvgeny Pinchuk &sys_props.attr_props); 8545b5c4e40SEvgeny Pinchuk if (sys_props.kobj_nodes) { 8555b5c4e40SEvgeny Pinchuk kobject_del(sys_props.kobj_nodes); 8565b5c4e40SEvgeny Pinchuk kobject_put(sys_props.kobj_nodes); 85716b9201cSOded Gabbay sys_props.kobj_nodes = NULL; 8585b5c4e40SEvgeny Pinchuk } 8595b5c4e40SEvgeny Pinchuk kobject_del(sys_props.kobj_topology); 8605b5c4e40SEvgeny Pinchuk kobject_put(sys_props.kobj_topology); 86116b9201cSOded Gabbay sys_props.kobj_topology = NULL; 8625b5c4e40SEvgeny Pinchuk } 8635b5c4e40SEvgeny Pinchuk } 8645b5c4e40SEvgeny Pinchuk 8654f449311SHarish Kasiviswanathan /* Called with write topology_lock acquired */ 8664f449311SHarish Kasiviswanathan static void kfd_topology_update_device_list(struct list_head *temp_list, 8674f449311SHarish Kasiviswanathan struct list_head *master_list) 8684f449311SHarish Kasiviswanathan { 8694f449311SHarish Kasiviswanathan while (!list_empty(temp_list)) { 8704f449311SHarish Kasiviswanathan list_move_tail(temp_list->next, master_list); 8714f449311SHarish Kasiviswanathan sys_props.num_devices++; 8724f449311SHarish Kasiviswanathan } 8734f449311SHarish Kasiviswanathan } 8744f449311SHarish Kasiviswanathan 875520b8fb7SFelix Kuehling static void kfd_debug_print_topology(void) 876520b8fb7SFelix Kuehling { 877520b8fb7SFelix Kuehling struct kfd_topology_device *dev; 878520b8fb7SFelix Kuehling 879520b8fb7SFelix Kuehling down_read(&topology_lock); 880520b8fb7SFelix Kuehling 881520b8fb7SFelix Kuehling dev = list_last_entry(&topology_device_list, 882520b8fb7SFelix Kuehling struct kfd_topology_device, list); 883520b8fb7SFelix Kuehling if (dev) { 884520b8fb7SFelix Kuehling if (dev->node_props.cpu_cores_count && 885520b8fb7SFelix Kuehling dev->node_props.simd_count) { 886520b8fb7SFelix Kuehling pr_info("Topology: Add APU node [0x%0x:0x%0x]\n", 887520b8fb7SFelix Kuehling dev->node_props.device_id, 888520b8fb7SFelix Kuehling dev->node_props.vendor_id); 889520b8fb7SFelix Kuehling } else if (dev->node_props.cpu_cores_count) 890520b8fb7SFelix Kuehling pr_info("Topology: Add CPU node\n"); 891520b8fb7SFelix Kuehling else if (dev->node_props.simd_count) 892520b8fb7SFelix Kuehling pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n", 893520b8fb7SFelix Kuehling dev->node_props.device_id, 894520b8fb7SFelix Kuehling dev->node_props.vendor_id); 895520b8fb7SFelix Kuehling } 896520b8fb7SFelix Kuehling up_read(&topology_lock); 897520b8fb7SFelix Kuehling } 898520b8fb7SFelix Kuehling 899520b8fb7SFelix Kuehling /* Helper function for intializing platform_xx members of 900520b8fb7SFelix Kuehling * kfd_system_properties. Uses OEM info from the last CPU/APU node. 901520b8fb7SFelix Kuehling */ 902520b8fb7SFelix Kuehling static void kfd_update_system_properties(void) 903520b8fb7SFelix Kuehling { 904520b8fb7SFelix Kuehling struct kfd_topology_device *dev; 905520b8fb7SFelix Kuehling 906520b8fb7SFelix Kuehling down_read(&topology_lock); 907520b8fb7SFelix Kuehling dev = list_last_entry(&topology_device_list, 908520b8fb7SFelix Kuehling struct kfd_topology_device, list); 909520b8fb7SFelix Kuehling if (dev) { 910520b8fb7SFelix Kuehling sys_props.platform_id = 911520b8fb7SFelix Kuehling (*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK; 912520b8fb7SFelix Kuehling sys_props.platform_oem = *((uint64_t *)dev->oem_table_id); 913520b8fb7SFelix Kuehling sys_props.platform_rev = dev->oem_revision; 914520b8fb7SFelix Kuehling } 915520b8fb7SFelix Kuehling up_read(&topology_lock); 916520b8fb7SFelix Kuehling } 917520b8fb7SFelix Kuehling 918520b8fb7SFelix Kuehling static void find_system_memory(const struct dmi_header *dm, 919520b8fb7SFelix Kuehling void *private) 920520b8fb7SFelix Kuehling { 921520b8fb7SFelix Kuehling struct kfd_mem_properties *mem; 922520b8fb7SFelix Kuehling u16 mem_width, mem_clock; 923520b8fb7SFelix Kuehling struct kfd_topology_device *kdev = 924520b8fb7SFelix Kuehling (struct kfd_topology_device *)private; 925520b8fb7SFelix Kuehling const u8 *dmi_data = (const u8 *)(dm + 1); 926520b8fb7SFelix Kuehling 927520b8fb7SFelix Kuehling if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) { 928520b8fb7SFelix Kuehling mem_width = (u16)(*(const u16 *)(dmi_data + 0x6)); 929520b8fb7SFelix Kuehling mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11)); 930520b8fb7SFelix Kuehling list_for_each_entry(mem, &kdev->mem_props, list) { 931520b8fb7SFelix Kuehling if (mem_width != 0xFFFF && mem_width != 0) 932520b8fb7SFelix Kuehling mem->width = mem_width; 933520b8fb7SFelix Kuehling if (mem_clock != 0) 934520b8fb7SFelix Kuehling mem->mem_clk_max = mem_clock; 935520b8fb7SFelix Kuehling } 936520b8fb7SFelix Kuehling } 937520b8fb7SFelix Kuehling } 938f4757347SAmber Lin 939f4757347SAmber Lin /* 940f4757347SAmber Lin * Performance counters information is not part of CRAT but we would like to 941f4757347SAmber Lin * put them in the sysfs under topology directory for Thunk to get the data. 942f4757347SAmber Lin * This function is called before updating the sysfs. 943f4757347SAmber Lin */ 944f4757347SAmber Lin static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev) 945f4757347SAmber Lin { 94664d1c3a4SFelix Kuehling /* These are the only counters supported so far */ 94764d1c3a4SFelix Kuehling return kfd_iommu_add_perf_counters(kdev); 948f4757347SAmber Lin } 949f4757347SAmber Lin 950520b8fb7SFelix Kuehling /* kfd_add_non_crat_information - Add information that is not currently 951520b8fb7SFelix Kuehling * defined in CRAT but is necessary for KFD topology 952520b8fb7SFelix Kuehling * @dev - topology device to which addition info is added 953520b8fb7SFelix Kuehling */ 954520b8fb7SFelix Kuehling static void kfd_add_non_crat_information(struct kfd_topology_device *kdev) 955520b8fb7SFelix Kuehling { 956520b8fb7SFelix Kuehling /* Check if CPU only node. */ 957520b8fb7SFelix Kuehling if (!kdev->gpu) { 958520b8fb7SFelix Kuehling /* Add system memory information */ 959520b8fb7SFelix Kuehling dmi_walk(find_system_memory, kdev); 960520b8fb7SFelix Kuehling } 961520b8fb7SFelix Kuehling /* TODO: For GPU node, rearrange code from kfd_topology_add_device */ 962520b8fb7SFelix Kuehling } 963520b8fb7SFelix Kuehling 964b441093eSHarish Kasiviswanathan /* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices. 965b441093eSHarish Kasiviswanathan * Ignore CRAT for all other devices. AMD APU is identified if both CPU 966b441093eSHarish Kasiviswanathan * and GPU cores are present. 967b441093eSHarish Kasiviswanathan * @device_list - topology device list created by parsing ACPI CRAT table. 968b441093eSHarish Kasiviswanathan * @return - TRUE if invalid, FALSE is valid. 969b441093eSHarish Kasiviswanathan */ 970b441093eSHarish Kasiviswanathan static bool kfd_is_acpi_crat_invalid(struct list_head *device_list) 971b441093eSHarish Kasiviswanathan { 972b441093eSHarish Kasiviswanathan struct kfd_topology_device *dev; 973b441093eSHarish Kasiviswanathan 974b441093eSHarish Kasiviswanathan list_for_each_entry(dev, device_list, list) { 975b441093eSHarish Kasiviswanathan if (dev->node_props.cpu_cores_count && 976b441093eSHarish Kasiviswanathan dev->node_props.simd_count) 977b441093eSHarish Kasiviswanathan return false; 978b441093eSHarish Kasiviswanathan } 979b441093eSHarish Kasiviswanathan pr_info("Ignoring ACPI CRAT on non-APU system\n"); 980b441093eSHarish Kasiviswanathan return true; 981b441093eSHarish Kasiviswanathan } 982b441093eSHarish Kasiviswanathan 9835b5c4e40SEvgeny Pinchuk int kfd_topology_init(void) 9845b5c4e40SEvgeny Pinchuk { 98516b9201cSOded Gabbay void *crat_image = NULL; 9865b5c4e40SEvgeny Pinchuk size_t image_size = 0; 9875b5c4e40SEvgeny Pinchuk int ret; 9884f449311SHarish Kasiviswanathan struct list_head temp_topology_device_list; 989520b8fb7SFelix Kuehling int cpu_only_node = 0; 990520b8fb7SFelix Kuehling struct kfd_topology_device *kdev; 991520b8fb7SFelix Kuehling int proximity_domain; 9925b5c4e40SEvgeny Pinchuk 9934f449311SHarish Kasiviswanathan /* topology_device_list - Master list of all topology devices 9944f449311SHarish Kasiviswanathan * temp_topology_device_list - temporary list created while parsing CRAT 9954f449311SHarish Kasiviswanathan * or VCRAT. Once parsing is complete the contents of list is moved to 9964f449311SHarish Kasiviswanathan * topology_device_list 9975b5c4e40SEvgeny Pinchuk */ 9984f449311SHarish Kasiviswanathan 9994f449311SHarish Kasiviswanathan /* Initialize the head for the both the lists */ 10005b5c4e40SEvgeny Pinchuk INIT_LIST_HEAD(&topology_device_list); 10014f449311SHarish Kasiviswanathan INIT_LIST_HEAD(&temp_topology_device_list); 10025b5c4e40SEvgeny Pinchuk init_rwsem(&topology_lock); 10035b5c4e40SEvgeny Pinchuk 10045b5c4e40SEvgeny Pinchuk memset(&sys_props, 0, sizeof(sys_props)); 10055b5c4e40SEvgeny Pinchuk 1006520b8fb7SFelix Kuehling /* Proximity domains in ACPI CRAT tables start counting at 1007520b8fb7SFelix Kuehling * 0. The same should be true for virtual CRAT tables created 1008520b8fb7SFelix Kuehling * at this stage. GPUs added later in kfd_topology_add_device 1009520b8fb7SFelix Kuehling * use a counter. 1010520b8fb7SFelix Kuehling */ 1011520b8fb7SFelix Kuehling proximity_domain = 0; 1012520b8fb7SFelix Kuehling 10135b5c4e40SEvgeny Pinchuk /* 1014520b8fb7SFelix Kuehling * Get the CRAT image from the ACPI. If ACPI doesn't have one 1015b441093eSHarish Kasiviswanathan * or if ACPI CRAT is invalid create a virtual CRAT. 1016520b8fb7SFelix Kuehling * NOTE: The current implementation expects all AMD APUs to have 1017520b8fb7SFelix Kuehling * CRAT. If no CRAT is available, it is assumed to be a CPU 10185b5c4e40SEvgeny Pinchuk */ 10198e05247dSHarish Kasiviswanathan ret = kfd_create_crat_image_acpi(&crat_image, &image_size); 10208e05247dSHarish Kasiviswanathan if (!ret) { 10214f449311SHarish Kasiviswanathan ret = kfd_parse_crat_table(crat_image, 1022520b8fb7SFelix Kuehling &temp_topology_device_list, 1023520b8fb7SFelix Kuehling proximity_domain); 1024b441093eSHarish Kasiviswanathan if (ret || 1025b441093eSHarish Kasiviswanathan kfd_is_acpi_crat_invalid(&temp_topology_device_list)) { 1026520b8fb7SFelix Kuehling kfd_release_topology_device_list( 1027520b8fb7SFelix Kuehling &temp_topology_device_list); 1028520b8fb7SFelix Kuehling kfd_destroy_crat_image(crat_image); 1029520b8fb7SFelix Kuehling crat_image = NULL; 1030520b8fb7SFelix Kuehling } 1031520b8fb7SFelix Kuehling } 1032520b8fb7SFelix Kuehling 1033520b8fb7SFelix Kuehling if (!crat_image) { 1034520b8fb7SFelix Kuehling ret = kfd_create_crat_image_virtual(&crat_image, &image_size, 1035520b8fb7SFelix Kuehling COMPUTE_UNIT_CPU, NULL, 1036520b8fb7SFelix Kuehling proximity_domain); 1037520b8fb7SFelix Kuehling cpu_only_node = 1; 1038520b8fb7SFelix Kuehling if (ret) { 1039520b8fb7SFelix Kuehling pr_err("Error creating VCRAT table for CPU\n"); 1040520b8fb7SFelix Kuehling return ret; 1041520b8fb7SFelix Kuehling } 1042520b8fb7SFelix Kuehling 1043520b8fb7SFelix Kuehling ret = kfd_parse_crat_table(crat_image, 1044520b8fb7SFelix Kuehling &temp_topology_device_list, 1045520b8fb7SFelix Kuehling proximity_domain); 1046520b8fb7SFelix Kuehling if (ret) { 1047520b8fb7SFelix Kuehling pr_err("Error parsing VCRAT table for CPU\n"); 10488e05247dSHarish Kasiviswanathan goto err; 1049520b8fb7SFelix Kuehling } 10505b5c4e40SEvgeny Pinchuk } 10515b5c4e40SEvgeny Pinchuk 1052f4757347SAmber Lin kdev = list_first_entry(&temp_topology_device_list, 1053f4757347SAmber Lin struct kfd_topology_device, list); 1054f4757347SAmber Lin kfd_add_perf_to_topology(kdev); 1055f4757347SAmber Lin 10565b5c4e40SEvgeny Pinchuk down_write(&topology_lock); 10574f449311SHarish Kasiviswanathan kfd_topology_update_device_list(&temp_topology_device_list, 10584f449311SHarish Kasiviswanathan &topology_device_list); 1059520b8fb7SFelix Kuehling atomic_set(&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 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 10955b5c4e40SEvgeny Pinchuk static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu) 10965b5c4e40SEvgeny Pinchuk { 10975b5c4e40SEvgeny Pinchuk uint32_t hashout; 10985b5c4e40SEvgeny Pinchuk uint32_t buf[7]; 1099585f0e6cSEdward O'Callaghan uint64_t local_mem_size; 11005b5c4e40SEvgeny Pinchuk int i; 11010504cccfSHarish Kasiviswanathan struct kfd_local_mem_info local_mem_info; 11025b5c4e40SEvgeny Pinchuk 11035b5c4e40SEvgeny Pinchuk if (!gpu) 11045b5c4e40SEvgeny Pinchuk return 0; 11055b5c4e40SEvgeny Pinchuk 11067cd52c91SAmber Lin amdgpu_amdkfd_get_local_mem_info(gpu->kgd, &local_mem_info); 11070504cccfSHarish Kasiviswanathan 11080504cccfSHarish Kasiviswanathan local_mem_size = local_mem_info.local_mem_size_private + 11090504cccfSHarish Kasiviswanathan local_mem_info.local_mem_size_public; 1110585f0e6cSEdward O'Callaghan 11115b5c4e40SEvgeny Pinchuk buf[0] = gpu->pdev->devfn; 111246096058SAmber Lin buf[1] = gpu->pdev->subsystem_vendor | 111346096058SAmber Lin (gpu->pdev->subsystem_device << 16); 111446096058SAmber Lin buf[2] = pci_domain_nr(gpu->pdev->bus); 11155b5c4e40SEvgeny Pinchuk buf[3] = gpu->pdev->device; 11165b5c4e40SEvgeny Pinchuk buf[4] = gpu->pdev->bus->number; 1117585f0e6cSEdward O'Callaghan buf[5] = lower_32_bits(local_mem_size); 1118585f0e6cSEdward O'Callaghan buf[6] = upper_32_bits(local_mem_size); 11195b5c4e40SEvgeny Pinchuk 11205b5c4e40SEvgeny Pinchuk for (i = 0, hashout = 0; i < 7; i++) 11215b5c4e40SEvgeny Pinchuk hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH); 11225b5c4e40SEvgeny Pinchuk 11235b5c4e40SEvgeny Pinchuk return hashout; 11245b5c4e40SEvgeny Pinchuk } 11253a87177eSHarish Kasiviswanathan /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If 11263a87177eSHarish Kasiviswanathan * the GPU device is not already present in the topology device 11273a87177eSHarish Kasiviswanathan * list then return NULL. This means a new topology device has to 11283a87177eSHarish Kasiviswanathan * be created for this GPU. 11293a87177eSHarish Kasiviswanathan */ 11305b5c4e40SEvgeny Pinchuk static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu) 11315b5c4e40SEvgeny Pinchuk { 11325b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev; 113316b9201cSOded Gabbay struct kfd_topology_device *out_dev = NULL; 1134171bc67eSHarish Kasiviswanathan struct kfd_mem_properties *mem; 1135171bc67eSHarish Kasiviswanathan struct kfd_cache_properties *cache; 1136171bc67eSHarish Kasiviswanathan struct kfd_iolink_properties *iolink; 11375b5c4e40SEvgeny Pinchuk 11383a87177eSHarish Kasiviswanathan down_write(&topology_lock); 1139b8fe0524SFelix Kuehling list_for_each_entry(dev, &topology_device_list, list) { 1140b8fe0524SFelix Kuehling /* Discrete GPUs need their own topology device list 1141b8fe0524SFelix Kuehling * entries. Don't assign them to CPU/APU nodes. 1142b8fe0524SFelix Kuehling */ 11436127896fSHuang Rui if (!gpu->use_iommu_v2 && 1144b8fe0524SFelix Kuehling dev->node_props.cpu_cores_count) 1145b8fe0524SFelix Kuehling continue; 1146b8fe0524SFelix Kuehling 11474eacc26bSKent Russell if (!dev->gpu && (dev->node_props.simd_count > 0)) { 11485b5c4e40SEvgeny Pinchuk dev->gpu = gpu; 11495b5c4e40SEvgeny Pinchuk out_dev = dev; 1150171bc67eSHarish Kasiviswanathan 1151171bc67eSHarish Kasiviswanathan list_for_each_entry(mem, &dev->mem_props, list) 1152171bc67eSHarish Kasiviswanathan mem->gpu = dev->gpu; 1153171bc67eSHarish Kasiviswanathan list_for_each_entry(cache, &dev->cache_props, list) 1154171bc67eSHarish Kasiviswanathan cache->gpu = dev->gpu; 1155171bc67eSHarish Kasiviswanathan list_for_each_entry(iolink, &dev->io_link_props, list) 1156171bc67eSHarish Kasiviswanathan iolink->gpu = dev->gpu; 11575b5c4e40SEvgeny Pinchuk break; 11585b5c4e40SEvgeny Pinchuk } 1159b8fe0524SFelix Kuehling } 11603a87177eSHarish Kasiviswanathan up_write(&topology_lock); 11615b5c4e40SEvgeny Pinchuk return out_dev; 11625b5c4e40SEvgeny Pinchuk } 11635b5c4e40SEvgeny Pinchuk 11645b5c4e40SEvgeny Pinchuk static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival) 11655b5c4e40SEvgeny Pinchuk { 11665b5c4e40SEvgeny Pinchuk /* 11675b5c4e40SEvgeny Pinchuk * TODO: Generate an event for thunk about the arrival/removal 11685b5c4e40SEvgeny Pinchuk * of the GPU 11695b5c4e40SEvgeny Pinchuk */ 11705b5c4e40SEvgeny Pinchuk } 11715b5c4e40SEvgeny Pinchuk 11723a87177eSHarish Kasiviswanathan /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info, 11733a87177eSHarish Kasiviswanathan * patch this after CRAT parsing. 11743a87177eSHarish Kasiviswanathan */ 11753a87177eSHarish Kasiviswanathan static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev) 11763a87177eSHarish Kasiviswanathan { 11773a87177eSHarish Kasiviswanathan struct kfd_mem_properties *mem; 11783a87177eSHarish Kasiviswanathan struct kfd_local_mem_info local_mem_info; 11793a87177eSHarish Kasiviswanathan 11803a87177eSHarish Kasiviswanathan if (!dev) 11813a87177eSHarish Kasiviswanathan return; 11823a87177eSHarish Kasiviswanathan 11833a87177eSHarish Kasiviswanathan /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with 11843a87177eSHarish Kasiviswanathan * single bank of VRAM local memory. 11853a87177eSHarish Kasiviswanathan * for dGPUs - VCRAT reports only one bank of Local Memory 11863a87177eSHarish Kasiviswanathan * for APUs - If CRAT from ACPI reports more than one bank, then 11873a87177eSHarish Kasiviswanathan * all the banks will report the same mem_clk_max information 11883a87177eSHarish Kasiviswanathan */ 11897cd52c91SAmber Lin amdgpu_amdkfd_get_local_mem_info(dev->gpu->kgd, &local_mem_info); 11903a87177eSHarish Kasiviswanathan 11913a87177eSHarish Kasiviswanathan list_for_each_entry(mem, &dev->mem_props, list) 11923a87177eSHarish Kasiviswanathan mem->mem_clk_max = local_mem_info.mem_clk_max; 11933a87177eSHarish Kasiviswanathan } 11943a87177eSHarish Kasiviswanathan 1195bdd24657SJonathan Kim static void kfd_set_iolink_no_atomics(struct kfd_topology_device *dev, 1196bdd24657SJonathan Kim struct kfd_topology_device *target_gpu_dev, 1197bdd24657SJonathan Kim struct kfd_iolink_properties *link) 11983a87177eSHarish Kasiviswanathan { 1199bdd24657SJonathan Kim /* xgmi always supports atomics between links. */ 1200bdd24657SJonathan Kim if (link->iolink_type == CRAT_IOLINK_TYPE_XGMI) 12013a87177eSHarish Kasiviswanathan return; 12023a87177eSHarish Kasiviswanathan 1203bdd24657SJonathan Kim /* check pcie support to set cpu(dev) flags for target_gpu_dev link. */ 1204bdd24657SJonathan Kim if (target_gpu_dev) { 1205bdd24657SJonathan Kim uint32_t cap; 1206bdd24657SJonathan Kim 1207bdd24657SJonathan Kim pcie_capability_read_dword(target_gpu_dev->gpu->pdev, 1208d35f00d8SEric Huang PCI_EXP_DEVCAP2, &cap); 1209d35f00d8SEric Huang 1210d35f00d8SEric Huang if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | 1211d35f00d8SEric Huang PCI_EXP_DEVCAP2_ATOMIC_COMP64))) 1212bdd24657SJonathan Kim link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT | 12133a87177eSHarish Kasiviswanathan CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT; 1214bdd24657SJonathan Kim /* set gpu (dev) flags. */ 1215bdd24657SJonathan Kim } else { 1216d35f00d8SEric Huang if (!dev->gpu->pci_atomic_requested || 1217deb68983SJonathan Kim dev->gpu->device_info->asic_family == 1218deb68983SJonathan Kim CHIP_HAWAII) 1219bdd24657SJonathan Kim link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT | 1220d35f00d8SEric Huang CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT; 1221deb68983SJonathan Kim } 1222bdd24657SJonathan Kim } 1223bdd24657SJonathan Kim 1224bdd24657SJonathan Kim static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev) 1225bdd24657SJonathan Kim { 1226bdd24657SJonathan Kim struct kfd_iolink_properties *link, *inbound_link; 1227bdd24657SJonathan Kim struct kfd_topology_device *peer_dev; 1228bdd24657SJonathan Kim 1229bdd24657SJonathan Kim if (!dev || !dev->gpu) 1230bdd24657SJonathan Kim return; 1231d35f00d8SEric Huang 1232d35f00d8SEric Huang /* GPU only creates direct links so apply flags setting to all */ 1233d35f00d8SEric Huang list_for_each_entry(link, &dev->io_link_props, list) { 1234bdd24657SJonathan Kim link->flags = CRAT_IOLINK_FLAGS_ENABLED; 1235bdd24657SJonathan Kim kfd_set_iolink_no_atomics(dev, NULL, link); 1236bdd24657SJonathan Kim peer_dev = kfd_topology_device_by_proximity_domain( 1237d35f00d8SEric Huang link->node_to); 1238bdd24657SJonathan Kim 1239bdd24657SJonathan Kim if (!peer_dev) 1240bdd24657SJonathan Kim continue; 1241bdd24657SJonathan Kim 1242bdd24657SJonathan Kim list_for_each_entry(inbound_link, &peer_dev->io_link_props, 1243bdd24657SJonathan Kim list) { 1244bdd24657SJonathan Kim if (inbound_link->node_to != link->node_from) 1245bdd24657SJonathan Kim continue; 1246bdd24657SJonathan Kim 1247bdd24657SJonathan Kim inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED; 1248bdd24657SJonathan Kim kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link); 1249d35f00d8SEric Huang } 1250d35f00d8SEric Huang } 12513a87177eSHarish Kasiviswanathan } 12523a87177eSHarish Kasiviswanathan 12535b5c4e40SEvgeny Pinchuk int kfd_topology_add_device(struct kfd_dev *gpu) 12545b5c4e40SEvgeny Pinchuk { 12555b5c4e40SEvgeny Pinchuk uint32_t gpu_id; 12565b5c4e40SEvgeny Pinchuk struct kfd_topology_device *dev; 1257f7ce2fadSFlora Cui struct kfd_cu_info cu_info; 12584f449311SHarish Kasiviswanathan int res = 0; 12594f449311SHarish Kasiviswanathan struct list_head temp_topology_device_list; 12603a87177eSHarish Kasiviswanathan void *crat_image = NULL; 12613a87177eSHarish Kasiviswanathan size_t image_size = 0; 12623a87177eSHarish Kasiviswanathan int proximity_domain; 12635436ab94SStanley.Yang struct amdgpu_device *adev; 12644f449311SHarish Kasiviswanathan 12654f449311SHarish Kasiviswanathan INIT_LIST_HEAD(&temp_topology_device_list); 12665b5c4e40SEvgeny Pinchuk 12675b5c4e40SEvgeny Pinchuk gpu_id = kfd_generate_gpu_id(gpu); 12685b5c4e40SEvgeny Pinchuk 126979775b62SKent Russell pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id); 12705b5c4e40SEvgeny Pinchuk 12713a87177eSHarish Kasiviswanathan proximity_domain = atomic_inc_return(&topology_crat_proximity_domain); 12723a87177eSHarish Kasiviswanathan 12733a87177eSHarish Kasiviswanathan /* Check to see if this gpu device exists in the topology_device_list. 12743a87177eSHarish Kasiviswanathan * If so, assign the gpu to that device, 12753a87177eSHarish Kasiviswanathan * else create a Virtual CRAT for this gpu device and then parse that 12763a87177eSHarish Kasiviswanathan * CRAT to create a new topology device. Once created assign the gpu to 12773a87177eSHarish Kasiviswanathan * that topology device 12785b5c4e40SEvgeny Pinchuk */ 12795b5c4e40SEvgeny Pinchuk dev = kfd_assign_gpu(gpu); 12805b5c4e40SEvgeny Pinchuk if (!dev) { 12813a87177eSHarish Kasiviswanathan res = kfd_create_crat_image_virtual(&crat_image, &image_size, 12823a87177eSHarish Kasiviswanathan COMPUTE_UNIT_GPU, gpu, 12833a87177eSHarish Kasiviswanathan proximity_domain); 12843a87177eSHarish Kasiviswanathan if (res) { 12853a87177eSHarish Kasiviswanathan pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n", 12863a87177eSHarish Kasiviswanathan gpu_id); 12873a87177eSHarish Kasiviswanathan return res; 12883a87177eSHarish Kasiviswanathan } 12893a87177eSHarish Kasiviswanathan res = kfd_parse_crat_table(crat_image, 12903a87177eSHarish Kasiviswanathan &temp_topology_device_list, 12913a87177eSHarish Kasiviswanathan proximity_domain); 12923a87177eSHarish Kasiviswanathan if (res) { 12933a87177eSHarish Kasiviswanathan pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n", 12943a87177eSHarish Kasiviswanathan gpu_id); 12955b5c4e40SEvgeny Pinchuk goto err; 12965b5c4e40SEvgeny Pinchuk } 12974f449311SHarish Kasiviswanathan 12984f449311SHarish Kasiviswanathan down_write(&topology_lock); 12994f449311SHarish Kasiviswanathan kfd_topology_update_device_list(&temp_topology_device_list, 13004f449311SHarish Kasiviswanathan &topology_device_list); 13014f449311SHarish Kasiviswanathan 13028eabaf54SKent Russell /* Update the SYSFS tree, since we added another topology 13038eabaf54SKent Russell * device 13045b5c4e40SEvgeny Pinchuk */ 13053a87177eSHarish Kasiviswanathan res = kfd_topology_update_sysfs(); 13064f449311SHarish Kasiviswanathan up_write(&topology_lock); 13074f449311SHarish Kasiviswanathan 13083a87177eSHarish Kasiviswanathan if (!res) 13093a87177eSHarish Kasiviswanathan sys_props.generation_count++; 13103a87177eSHarish Kasiviswanathan else 13113a87177eSHarish Kasiviswanathan pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n", 13123a87177eSHarish Kasiviswanathan gpu_id, res); 13133a87177eSHarish Kasiviswanathan dev = kfd_assign_gpu(gpu); 13143a87177eSHarish Kasiviswanathan if (WARN_ON(!dev)) { 13153a87177eSHarish Kasiviswanathan res = -ENODEV; 13163a87177eSHarish Kasiviswanathan goto err; 13173a87177eSHarish Kasiviswanathan } 13185b5c4e40SEvgeny Pinchuk } 13195b5c4e40SEvgeny Pinchuk 13205b5c4e40SEvgeny Pinchuk dev->gpu_id = gpu_id; 13215b5c4e40SEvgeny Pinchuk gpu->id = gpu_id; 13223a87177eSHarish Kasiviswanathan 13233a87177eSHarish Kasiviswanathan /* TODO: Move the following lines to function 13243a87177eSHarish Kasiviswanathan * kfd_add_non_crat_information 13253a87177eSHarish Kasiviswanathan */ 13263a87177eSHarish Kasiviswanathan 13273a87177eSHarish Kasiviswanathan /* Fill-in additional information that is not available in CRAT but 13283a87177eSHarish Kasiviswanathan * needed for the topology 13293a87177eSHarish Kasiviswanathan */ 13303a87177eSHarish Kasiviswanathan 13317cd52c91SAmber Lin amdgpu_amdkfd_get_cu_info(dev->gpu->kgd, &cu_info); 1332c181159aSYong Zhao 1333c181159aSYong Zhao strncpy(dev->node_props.name, gpu->device_info->asic_name, 1334c181159aSYong Zhao KFD_TOPOLOGY_PUBLIC_NAME_SIZE); 1335c181159aSYong Zhao 13363a87177eSHarish Kasiviswanathan dev->node_props.simd_arrays_per_engine = 13373a87177eSHarish Kasiviswanathan cu_info.num_shader_arrays_per_engine; 13383a87177eSHarish Kasiviswanathan 13395b5c4e40SEvgeny Pinchuk dev->node_props.vendor_id = gpu->pdev->vendor; 13405b5c4e40SEvgeny Pinchuk dev->node_props.device_id = gpu->pdev->device; 1341c6d1ec41SJoseph Greathouse dev->node_props.capability |= 1342c6d1ec41SJoseph Greathouse ((amdgpu_amdkfd_get_asic_rev_id(dev->gpu->kgd) << 1343c6d1ec41SJoseph Greathouse HSA_CAP_ASIC_REVISION_SHIFT) & 1344c6d1ec41SJoseph Greathouse HSA_CAP_ASIC_REVISION_MASK); 1345babe2ef3SHeiner Kallweit dev->node_props.location_id = pci_dev_id(gpu->pdev); 13463e58e95aSOri Messinger dev->node_props.domain = pci_domain_nr(gpu->pdev->bus); 13473a87177eSHarish Kasiviswanathan dev->node_props.max_engine_clk_fcompute = 13487cd52c91SAmber Lin amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->kgd); 13493a87177eSHarish Kasiviswanathan dev->node_props.max_engine_clk_ccompute = 13503a87177eSHarish Kasiviswanathan cpufreq_quick_get_max(0) / 1000; 13517c9b7171SOak Zeng dev->node_props.drm_render_minor = 13527c9b7171SOak Zeng gpu->shared_resources.drm_render_minor; 13535b5c4e40SEvgeny Pinchuk 13540c1690e3SShaoyun Liu dev->node_props.hive_id = gpu->hive_id; 135514568cf6SOak Zeng dev->node_props.num_sdma_engines = gpu->device_info->num_sdma_engines; 135614568cf6SOak Zeng dev->node_props.num_sdma_xgmi_engines = 135714568cf6SOak Zeng gpu->device_info->num_xgmi_sdma_engines; 1358bb71c74dSHuang Rui dev->node_props.num_sdma_queues_per_engine = 1359bb71c74dSHuang Rui gpu->device_info->num_sdma_queues_per_engine; 136029633d0eSJoseph Greathouse dev->node_props.num_gws = (dev->gpu->gws && 136129e76462SOak Zeng dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ? 136229e76462SOak Zeng amdgpu_amdkfd_get_num_gws(dev->gpu->kgd) : 0; 1363e6945304SYong Zhao dev->node_props.num_cp_queues = get_cp_queues_num(dev->gpu->dqm); 13640c1690e3SShaoyun Liu 13653a87177eSHarish Kasiviswanathan kfd_fill_mem_clk_max_info(dev); 13663a87177eSHarish Kasiviswanathan kfd_fill_iolink_non_crat_info(dev); 13673a87177eSHarish Kasiviswanathan 13683a87177eSHarish Kasiviswanathan switch (dev->gpu->device_info->asic_family) { 13693a87177eSHarish Kasiviswanathan case CHIP_KAVERI: 13703a87177eSHarish Kasiviswanathan case CHIP_HAWAII: 13713a87177eSHarish Kasiviswanathan case CHIP_TONGA: 13723a87177eSHarish Kasiviswanathan dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 << 13733a87177eSHarish Kasiviswanathan HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) & 13743a87177eSHarish Kasiviswanathan HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK); 13753a87177eSHarish Kasiviswanathan break; 13763a87177eSHarish Kasiviswanathan case CHIP_CARRIZO: 13773a87177eSHarish Kasiviswanathan case CHIP_FIJI: 13783a87177eSHarish Kasiviswanathan case CHIP_POLARIS10: 13793a87177eSHarish Kasiviswanathan case CHIP_POLARIS11: 1380846a44d7SGang Ba case CHIP_POLARIS12: 1381ed81cd6eSKent Russell case CHIP_VEGAM: 138242aa8793SFelix Kuehling pr_debug("Adding doorbell packet type capability\n"); 13833a87177eSHarish Kasiviswanathan dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 << 13843a87177eSHarish Kasiviswanathan HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) & 13853a87177eSHarish Kasiviswanathan HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK); 13863a87177eSHarish Kasiviswanathan break; 1387389056e5SFelix Kuehling case CHIP_VEGA10: 1388846a44d7SGang Ba case CHIP_VEGA12: 138922a3a294SShaoyun Liu case CHIP_VEGA20: 1390389056e5SFelix Kuehling case CHIP_RAVEN: 1391f5d843d4SHuang Rui case CHIP_RENOIR: 139249adcf8aSYong Zhao case CHIP_ARCTURUS: 139336e22d59SYong Zhao case CHIP_ALDEBARAN: 139414328aa5SPhilip Cox case CHIP_NAVI10: 13950e94b564Sshaoyunl case CHIP_NAVI12: 13968099ae40SYong Zhao case CHIP_NAVI14: 13973a2f0c81SYong Zhao case CHIP_SIENNA_CICHLID: 1398de89b2e4SChengming Gui case CHIP_NAVY_FLOUNDER: 13993a5e715dSHuang Rui case CHIP_VANGOGH: 1400eb5a34d4SChengming Gui case CHIP_DIMGREY_CAVEFISH: 1401*5cf607ccSChengming Gui case CHIP_BEIGE_GOBY: 1402389056e5SFelix Kuehling dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 << 1403389056e5SFelix Kuehling HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) & 1404389056e5SFelix Kuehling HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK); 1405389056e5SFelix Kuehling break; 14063a87177eSHarish Kasiviswanathan default: 14073a87177eSHarish Kasiviswanathan WARN(1, "Unexpected ASIC family %u", 14083a87177eSHarish Kasiviswanathan dev->gpu->device_info->asic_family); 14097639a8c4SBen Goz } 14107639a8c4SBen Goz 14111ae99eabSOak Zeng /* 14121ae99eabSOak Zeng * Overwrite ATS capability according to needs_iommu_device to fix 14131ae99eabSOak Zeng * potential missing corresponding bit in CRAT of BIOS. 14141ae99eabSOak Zeng */ 14156127896fSHuang Rui if (dev->gpu->use_iommu_v2) 14161ae99eabSOak Zeng dev->node_props.capability |= HSA_CAP_ATS_PRESENT; 14171ae99eabSOak Zeng else 14181ae99eabSOak Zeng dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT; 14191ae99eabSOak Zeng 14203a87177eSHarish Kasiviswanathan /* Fix errors in CZ CRAT. 14213a87177eSHarish Kasiviswanathan * simd_count: Carrizo CRAT reports wrong simd_count, probably 14223a87177eSHarish Kasiviswanathan * because it doesn't consider masked out CUs 142370f372bfSPhilip Cox * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd 14243a87177eSHarish Kasiviswanathan */ 142570f372bfSPhilip Cox if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) { 14263a87177eSHarish Kasiviswanathan dev->node_props.simd_count = 14273a87177eSHarish Kasiviswanathan cu_info.simd_per_cu * cu_info.cu_active_number; 142870f372bfSPhilip Cox dev->node_props.max_waves_per_simd = 10; 142970f372bfSPhilip Cox } 14303a87177eSHarish Kasiviswanathan 14315436ab94SStanley.Yang adev = (struct amdgpu_device *)(dev->gpu->kgd); 14325436ab94SStanley.Yang /* kfd only concerns sram ecc on GFX and HBM ecc on UMC */ 14330dee45a2SEric Huang dev->node_props.capability |= 14348ab0d6f0SLuben Tuikov ((adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0) ? 14350dee45a2SEric Huang HSA_CAP_SRAM_EDCSUPPORTED : 0; 14368ab0d6f0SLuben Tuikov dev->node_props.capability |= ((adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ? 14370dee45a2SEric Huang HSA_CAP_MEM_EDCSUPPORTED : 0; 14380dee45a2SEric Huang 14395436ab94SStanley.Yang if (adev->asic_type != CHIP_VEGA10) 14408ab0d6f0SLuben Tuikov dev->node_props.capability |= (adev->ras_enabled != 0) ? 14410dee45a2SEric Huang HSA_CAP_RASEVENTNOTIFY : 0; 14420dee45a2SEric Huang 14434c166eb9SPhilip Yang /* SVM API and HMM page migration work together, device memory type 14444c166eb9SPhilip Yang * is initialized to not 0 when page migration register device memory. 14454c166eb9SPhilip Yang */ 14464c166eb9SPhilip Yang if (adev->kfd.dev->pgmap.type != 0) 14474c166eb9SPhilip Yang dev->node_props.capability |= HSA_CAP_SVMAPI_SUPPORTED; 14484c166eb9SPhilip Yang 14493a87177eSHarish Kasiviswanathan kfd_debug_print_topology(); 14503a87177eSHarish Kasiviswanathan 14514f449311SHarish Kasiviswanathan if (!res) 14525b5c4e40SEvgeny Pinchuk kfd_notify_gpu_change(gpu_id, 1); 14534f449311SHarish Kasiviswanathan err: 14543a87177eSHarish Kasiviswanathan kfd_destroy_crat_image(crat_image); 14555b5c4e40SEvgeny Pinchuk return res; 14565b5c4e40SEvgeny Pinchuk } 14575b5c4e40SEvgeny Pinchuk 14585b5c4e40SEvgeny Pinchuk int kfd_topology_remove_device(struct kfd_dev *gpu) 14595b5c4e40SEvgeny Pinchuk { 14604f449311SHarish Kasiviswanathan struct kfd_topology_device *dev, *tmp; 14615b5c4e40SEvgeny Pinchuk uint32_t gpu_id; 14625b5c4e40SEvgeny Pinchuk int res = -ENODEV; 14635b5c4e40SEvgeny Pinchuk 14645b5c4e40SEvgeny Pinchuk down_write(&topology_lock); 14655b5c4e40SEvgeny Pinchuk 14664f449311SHarish Kasiviswanathan list_for_each_entry_safe(dev, tmp, &topology_device_list, list) 14675b5c4e40SEvgeny Pinchuk if (dev->gpu == gpu) { 14685b5c4e40SEvgeny Pinchuk gpu_id = dev->gpu_id; 14695b5c4e40SEvgeny Pinchuk kfd_remove_sysfs_node_entry(dev); 14705b5c4e40SEvgeny Pinchuk kfd_release_topology_device(dev); 14714f449311SHarish Kasiviswanathan sys_props.num_devices--; 14725b5c4e40SEvgeny Pinchuk res = 0; 14735b5c4e40SEvgeny Pinchuk if (kfd_topology_update_sysfs() < 0) 14745b5c4e40SEvgeny Pinchuk kfd_topology_release_sysfs(); 14755b5c4e40SEvgeny Pinchuk break; 14765b5c4e40SEvgeny Pinchuk } 14775b5c4e40SEvgeny Pinchuk 14785b5c4e40SEvgeny Pinchuk up_write(&topology_lock); 14795b5c4e40SEvgeny Pinchuk 1480174de876SFelix Kuehling if (!res) 14815b5c4e40SEvgeny Pinchuk kfd_notify_gpu_change(gpu_id, 0); 14825b5c4e40SEvgeny Pinchuk 14835b5c4e40SEvgeny Pinchuk return res; 14845b5c4e40SEvgeny Pinchuk } 14855b5c4e40SEvgeny Pinchuk 14866d82eb0eSHarish Kasiviswanathan /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD 14876d82eb0eSHarish Kasiviswanathan * topology. If GPU device is found @idx, then valid kfd_dev pointer is 14886d82eb0eSHarish Kasiviswanathan * returned through @kdev 14896d82eb0eSHarish Kasiviswanathan * Return - 0: On success (@kdev will be NULL for non GPU nodes) 14906d82eb0eSHarish Kasiviswanathan * -1: If end of list 14915b5c4e40SEvgeny Pinchuk */ 14926d82eb0eSHarish Kasiviswanathan int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev) 14935b5c4e40SEvgeny Pinchuk { 14945b5c4e40SEvgeny Pinchuk 14955b5c4e40SEvgeny Pinchuk struct kfd_topology_device *top_dev; 14965b5c4e40SEvgeny Pinchuk uint8_t device_idx = 0; 14975b5c4e40SEvgeny Pinchuk 14986d82eb0eSHarish Kasiviswanathan *kdev = NULL; 14995b5c4e40SEvgeny Pinchuk down_read(&topology_lock); 15005b5c4e40SEvgeny Pinchuk 15015b5c4e40SEvgeny Pinchuk list_for_each_entry(top_dev, &topology_device_list, list) { 15025b5c4e40SEvgeny Pinchuk if (device_idx == idx) { 15036d82eb0eSHarish Kasiviswanathan *kdev = top_dev->gpu; 15046d82eb0eSHarish Kasiviswanathan up_read(&topology_lock); 15056d82eb0eSHarish Kasiviswanathan return 0; 15065b5c4e40SEvgeny Pinchuk } 15075b5c4e40SEvgeny Pinchuk 15085b5c4e40SEvgeny Pinchuk device_idx++; 15095b5c4e40SEvgeny Pinchuk } 15105b5c4e40SEvgeny Pinchuk 15115b5c4e40SEvgeny Pinchuk up_read(&topology_lock); 15125b5c4e40SEvgeny Pinchuk 15136d82eb0eSHarish Kasiviswanathan return -1; 15145b5c4e40SEvgeny Pinchuk 15155b5c4e40SEvgeny Pinchuk } 1516851a645eSFelix Kuehling 1517520b8fb7SFelix Kuehling static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask) 1518520b8fb7SFelix Kuehling { 1519520b8fb7SFelix Kuehling int first_cpu_of_numa_node; 1520520b8fb7SFelix Kuehling 1521520b8fb7SFelix Kuehling if (!cpumask || cpumask == cpu_none_mask) 1522520b8fb7SFelix Kuehling return -1; 1523520b8fb7SFelix Kuehling first_cpu_of_numa_node = cpumask_first(cpumask); 1524520b8fb7SFelix Kuehling if (first_cpu_of_numa_node >= nr_cpu_ids) 1525520b8fb7SFelix Kuehling return -1; 1526df1dd4f4SFelix Kuehling #ifdef CONFIG_X86_64 1527df1dd4f4SFelix Kuehling return cpu_data(first_cpu_of_numa_node).apicid; 1528df1dd4f4SFelix Kuehling #else 1529df1dd4f4SFelix Kuehling return first_cpu_of_numa_node; 1530df1dd4f4SFelix Kuehling #endif 1531520b8fb7SFelix Kuehling } 1532520b8fb7SFelix Kuehling 1533520b8fb7SFelix Kuehling /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor 1534520b8fb7SFelix Kuehling * of the given NUMA node (numa_node_id) 1535520b8fb7SFelix Kuehling * Return -1 on failure 1536520b8fb7SFelix Kuehling */ 1537520b8fb7SFelix Kuehling int kfd_numa_node_to_apic_id(int numa_node_id) 1538520b8fb7SFelix Kuehling { 1539520b8fb7SFelix Kuehling if (numa_node_id == -1) { 1540520b8fb7SFelix Kuehling pr_warn("Invalid NUMA Node. Use online CPU mask\n"); 1541520b8fb7SFelix Kuehling return kfd_cpumask_to_apic_id(cpu_online_mask); 1542520b8fb7SFelix Kuehling } 1543520b8fb7SFelix Kuehling return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id)); 1544520b8fb7SFelix Kuehling } 1545520b8fb7SFelix Kuehling 15466127896fSHuang Rui void kfd_double_confirm_iommu_support(struct kfd_dev *gpu) 15476127896fSHuang Rui { 15486127896fSHuang Rui struct kfd_topology_device *dev; 15496127896fSHuang Rui 15506127896fSHuang Rui gpu->use_iommu_v2 = false; 15516127896fSHuang Rui 15526127896fSHuang Rui if (!gpu->device_info->needs_iommu_device) 15536127896fSHuang Rui return; 15546127896fSHuang Rui 15556127896fSHuang Rui down_read(&topology_lock); 15566127896fSHuang Rui 15576127896fSHuang Rui /* Only use IOMMUv2 if there is an APU topology node with no GPU 15586127896fSHuang Rui * assigned yet. This GPU will be assigned to it. 15596127896fSHuang Rui */ 15606127896fSHuang Rui list_for_each_entry(dev, &topology_device_list, list) 15616127896fSHuang Rui if (dev->node_props.cpu_cores_count && 15626127896fSHuang Rui dev->node_props.simd_count && 15636127896fSHuang Rui !dev->gpu) 15646127896fSHuang Rui gpu->use_iommu_v2 = true; 15656127896fSHuang Rui 15666127896fSHuang Rui up_read(&topology_lock); 15676127896fSHuang Rui } 15686127896fSHuang Rui 1569851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS) 1570851a645eSFelix Kuehling 1571851a645eSFelix Kuehling int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data) 1572851a645eSFelix Kuehling { 1573851a645eSFelix Kuehling struct kfd_topology_device *dev; 1574851a645eSFelix Kuehling unsigned int i = 0; 1575851a645eSFelix Kuehling int r = 0; 1576851a645eSFelix Kuehling 1577851a645eSFelix Kuehling down_read(&topology_lock); 1578851a645eSFelix Kuehling 1579851a645eSFelix Kuehling list_for_each_entry(dev, &topology_device_list, list) { 1580851a645eSFelix Kuehling if (!dev->gpu) { 1581851a645eSFelix Kuehling i++; 1582851a645eSFelix Kuehling continue; 1583851a645eSFelix Kuehling } 1584851a645eSFelix Kuehling 1585851a645eSFelix Kuehling seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id); 1586851a645eSFelix Kuehling r = dqm_debugfs_hqds(m, dev->gpu->dqm); 1587851a645eSFelix Kuehling if (r) 1588851a645eSFelix Kuehling break; 1589851a645eSFelix Kuehling } 1590851a645eSFelix Kuehling 1591851a645eSFelix Kuehling up_read(&topology_lock); 1592851a645eSFelix Kuehling 1593851a645eSFelix Kuehling return r; 1594851a645eSFelix Kuehling } 1595851a645eSFelix Kuehling 1596851a645eSFelix Kuehling int kfd_debugfs_rls_by_device(struct seq_file *m, void *data) 1597851a645eSFelix Kuehling { 1598851a645eSFelix Kuehling struct kfd_topology_device *dev; 1599851a645eSFelix Kuehling unsigned int i = 0; 1600851a645eSFelix Kuehling int r = 0; 1601851a645eSFelix Kuehling 1602851a645eSFelix Kuehling down_read(&topology_lock); 1603851a645eSFelix Kuehling 1604851a645eSFelix Kuehling list_for_each_entry(dev, &topology_device_list, list) { 1605851a645eSFelix Kuehling if (!dev->gpu) { 1606851a645eSFelix Kuehling i++; 1607851a645eSFelix Kuehling continue; 1608851a645eSFelix Kuehling } 1609851a645eSFelix Kuehling 1610851a645eSFelix Kuehling seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id); 1611851a645eSFelix Kuehling r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets); 1612851a645eSFelix Kuehling if (r) 1613851a645eSFelix Kuehling break; 1614851a645eSFelix Kuehling } 1615851a645eSFelix Kuehling 1616851a645eSFelix Kuehling up_read(&topology_lock); 1617851a645eSFelix Kuehling 1618851a645eSFelix Kuehling return r; 1619851a645eSFelix Kuehling } 1620851a645eSFelix Kuehling 1621851a645eSFelix Kuehling #endif 1622