1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2014-2022 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/pci.h>
27 #include <linux/errno.h>
28 #include <linux/acpi.h>
29 #include <linux/hash.h>
30 #include <linux/cpufreq.h>
31 #include <linux/log2.h>
32 #include <linux/dmi.h>
33 #include <linux/atomic.h>
37 #include "kfd_topology.h"
38 #include "kfd_device_queue_manager.h"
39 #include "kfd_iommu.h"
41 #include "amdgpu_amdkfd.h"
42 #include "amdgpu_ras.h"
45 /* topology_device_list - Master list of all topology devices */
46 static struct list_head topology_device_list;
47 static struct kfd_system_properties sys_props;
49 static DECLARE_RWSEM(topology_lock);
50 static uint32_t topology_crat_proximity_domain;
52 struct kfd_topology_device *kfd_topology_device_by_proximity_domain_no_lock(
53 uint32_t proximity_domain)
55 struct kfd_topology_device *top_dev;
56 struct kfd_topology_device *device = NULL;
58 list_for_each_entry(top_dev, &topology_device_list, list)
59 if (top_dev->proximity_domain == proximity_domain) {
67 struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
68 uint32_t proximity_domain)
70 struct kfd_topology_device *device = NULL;
72 down_read(&topology_lock);
74 device = kfd_topology_device_by_proximity_domain_no_lock(
76 up_read(&topology_lock);
81 struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
83 struct kfd_topology_device *top_dev = NULL;
84 struct kfd_topology_device *ret = NULL;
86 down_read(&topology_lock);
88 list_for_each_entry(top_dev, &topology_device_list, list)
89 if (top_dev->gpu_id == gpu_id) {
94 up_read(&topology_lock);
99 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
101 struct kfd_topology_device *top_dev;
103 top_dev = kfd_topology_device_by_id(gpu_id);
110 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
112 struct kfd_topology_device *top_dev;
113 struct kfd_dev *device = NULL;
115 down_read(&topology_lock);
117 list_for_each_entry(top_dev, &topology_device_list, list)
118 if (top_dev->gpu && top_dev->gpu->pdev == pdev) {
119 device = top_dev->gpu;
123 up_read(&topology_lock);
128 struct kfd_dev *kfd_device_by_adev(const struct amdgpu_device *adev)
130 struct kfd_topology_device *top_dev;
131 struct kfd_dev *device = NULL;
133 down_read(&topology_lock);
135 list_for_each_entry(top_dev, &topology_device_list, list)
136 if (top_dev->gpu && top_dev->gpu->adev == adev) {
137 device = top_dev->gpu;
141 up_read(&topology_lock);
146 /* Called with write topology_lock acquired */
147 static void kfd_release_topology_device(struct kfd_topology_device *dev)
149 struct kfd_mem_properties *mem;
150 struct kfd_cache_properties *cache;
151 struct kfd_iolink_properties *iolink;
152 struct kfd_iolink_properties *p2plink;
153 struct kfd_perf_properties *perf;
155 list_del(&dev->list);
157 while (dev->mem_props.next != &dev->mem_props) {
158 mem = container_of(dev->mem_props.next,
159 struct kfd_mem_properties, list);
160 list_del(&mem->list);
164 while (dev->cache_props.next != &dev->cache_props) {
165 cache = container_of(dev->cache_props.next,
166 struct kfd_cache_properties, list);
167 list_del(&cache->list);
171 while (dev->io_link_props.next != &dev->io_link_props) {
172 iolink = container_of(dev->io_link_props.next,
173 struct kfd_iolink_properties, list);
174 list_del(&iolink->list);
178 while (dev->p2p_link_props.next != &dev->p2p_link_props) {
179 p2plink = container_of(dev->p2p_link_props.next,
180 struct kfd_iolink_properties, list);
181 list_del(&p2plink->list);
185 while (dev->perf_props.next != &dev->perf_props) {
186 perf = container_of(dev->perf_props.next,
187 struct kfd_perf_properties, list);
188 list_del(&perf->list);
195 void kfd_release_topology_device_list(struct list_head *device_list)
197 struct kfd_topology_device *dev;
199 while (!list_empty(device_list)) {
200 dev = list_first_entry(device_list,
201 struct kfd_topology_device, list);
202 kfd_release_topology_device(dev);
206 static void kfd_release_live_view(void)
208 kfd_release_topology_device_list(&topology_device_list);
209 memset(&sys_props, 0, sizeof(sys_props));
212 struct kfd_topology_device *kfd_create_topology_device(
213 struct list_head *device_list)
215 struct kfd_topology_device *dev;
217 dev = kfd_alloc_struct(dev);
219 pr_err("No memory to allocate a topology device");
223 INIT_LIST_HEAD(&dev->mem_props);
224 INIT_LIST_HEAD(&dev->cache_props);
225 INIT_LIST_HEAD(&dev->io_link_props);
226 INIT_LIST_HEAD(&dev->p2p_link_props);
227 INIT_LIST_HEAD(&dev->perf_props);
229 list_add_tail(&dev->list, device_list);
235 #define sysfs_show_gen_prop(buffer, offs, fmt, ...) \
236 (offs += snprintf(buffer+offs, PAGE_SIZE-offs, \
238 #define sysfs_show_32bit_prop(buffer, offs, name, value) \
239 sysfs_show_gen_prop(buffer, offs, "%s %u\n", name, value)
240 #define sysfs_show_64bit_prop(buffer, offs, name, value) \
241 sysfs_show_gen_prop(buffer, offs, "%s %llu\n", name, value)
242 #define sysfs_show_32bit_val(buffer, offs, value) \
243 sysfs_show_gen_prop(buffer, offs, "%u\n", value)
244 #define sysfs_show_str_val(buffer, offs, value) \
245 sysfs_show_gen_prop(buffer, offs, "%s\n", value)
247 static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
252 /* Making sure that the buffer is an empty string */
255 if (attr == &sys_props.attr_genid) {
256 sysfs_show_32bit_val(buffer, offs,
257 sys_props.generation_count);
258 } else if (attr == &sys_props.attr_props) {
259 sysfs_show_64bit_prop(buffer, offs, "platform_oem",
260 sys_props.platform_oem);
261 sysfs_show_64bit_prop(buffer, offs, "platform_id",
262 sys_props.platform_id);
263 sysfs_show_64bit_prop(buffer, offs, "platform_rev",
264 sys_props.platform_rev);
272 static void kfd_topology_kobj_release(struct kobject *kobj)
277 static const struct sysfs_ops sysprops_ops = {
278 .show = sysprops_show,
281 static struct kobj_type sysprops_type = {
282 .release = kfd_topology_kobj_release,
283 .sysfs_ops = &sysprops_ops,
286 static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
290 struct kfd_iolink_properties *iolink;
292 /* Making sure that the buffer is an empty string */
295 iolink = container_of(attr, struct kfd_iolink_properties, attr);
296 if (iolink->gpu && kfd_devcgroup_check_permission(iolink->gpu))
298 sysfs_show_32bit_prop(buffer, offs, "type", iolink->iolink_type);
299 sysfs_show_32bit_prop(buffer, offs, "version_major", iolink->ver_maj);
300 sysfs_show_32bit_prop(buffer, offs, "version_minor", iolink->ver_min);
301 sysfs_show_32bit_prop(buffer, offs, "node_from", iolink->node_from);
302 sysfs_show_32bit_prop(buffer, offs, "node_to", iolink->node_to);
303 sysfs_show_32bit_prop(buffer, offs, "weight", iolink->weight);
304 sysfs_show_32bit_prop(buffer, offs, "min_latency", iolink->min_latency);
305 sysfs_show_32bit_prop(buffer, offs, "max_latency", iolink->max_latency);
306 sysfs_show_32bit_prop(buffer, offs, "min_bandwidth",
307 iolink->min_bandwidth);
308 sysfs_show_32bit_prop(buffer, offs, "max_bandwidth",
309 iolink->max_bandwidth);
310 sysfs_show_32bit_prop(buffer, offs, "recommended_transfer_size",
311 iolink->rec_transfer_size);
312 sysfs_show_32bit_prop(buffer, offs, "flags", iolink->flags);
317 static const struct sysfs_ops iolink_ops = {
321 static struct kobj_type iolink_type = {
322 .release = kfd_topology_kobj_release,
323 .sysfs_ops = &iolink_ops,
326 static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
330 struct kfd_mem_properties *mem;
332 /* Making sure that the buffer is an empty string */
335 mem = container_of(attr, struct kfd_mem_properties, attr);
336 if (mem->gpu && kfd_devcgroup_check_permission(mem->gpu))
338 sysfs_show_32bit_prop(buffer, offs, "heap_type", mem->heap_type);
339 sysfs_show_64bit_prop(buffer, offs, "size_in_bytes",
341 sysfs_show_32bit_prop(buffer, offs, "flags", mem->flags);
342 sysfs_show_32bit_prop(buffer, offs, "width", mem->width);
343 sysfs_show_32bit_prop(buffer, offs, "mem_clk_max",
349 static const struct sysfs_ops mem_ops = {
353 static struct kobj_type mem_type = {
354 .release = kfd_topology_kobj_release,
355 .sysfs_ops = &mem_ops,
358 static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
363 struct kfd_cache_properties *cache;
365 /* Making sure that the buffer is an empty string */
368 cache = container_of(attr, struct kfd_cache_properties, attr);
369 if (cache->gpu && kfd_devcgroup_check_permission(cache->gpu))
371 sysfs_show_32bit_prop(buffer, offs, "processor_id_low",
372 cache->processor_id_low);
373 sysfs_show_32bit_prop(buffer, offs, "level", cache->cache_level);
374 sysfs_show_32bit_prop(buffer, offs, "size", cache->cache_size);
375 sysfs_show_32bit_prop(buffer, offs, "cache_line_size",
376 cache->cacheline_size);
377 sysfs_show_32bit_prop(buffer, offs, "cache_lines_per_tag",
378 cache->cachelines_per_tag);
379 sysfs_show_32bit_prop(buffer, offs, "association", cache->cache_assoc);
380 sysfs_show_32bit_prop(buffer, offs, "latency", cache->cache_latency);
381 sysfs_show_32bit_prop(buffer, offs, "type", cache->cache_type);
382 offs += snprintf(buffer+offs, PAGE_SIZE-offs, "sibling_map ");
383 for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++)
384 for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++)
386 offs += snprintf(buffer+offs, PAGE_SIZE-offs, "%d,",
387 (cache->sibling_map[i] >> j) & 1);
389 /* Replace the last "," with end of line */
390 buffer[offs-1] = '\n';
394 static const struct sysfs_ops cache_ops = {
395 .show = kfd_cache_show,
398 static struct kobj_type cache_type = {
399 .release = kfd_topology_kobj_release,
400 .sysfs_ops = &cache_ops,
403 /****** Sysfs of Performance Counters ******/
405 struct kfd_perf_attr {
406 struct kobj_attribute attr;
410 static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
414 struct kfd_perf_attr *attr;
417 attr = container_of(attrs, struct kfd_perf_attr, attr);
418 if (!attr->data) /* invalid data for PMC */
421 return sysfs_show_32bit_val(buf, offs, attr->data);
424 #define KFD_PERF_DESC(_name, _data) \
426 .attr = __ATTR(_name, 0444, perf_show, NULL), \
430 static struct kfd_perf_attr perf_attr_iommu[] = {
431 KFD_PERF_DESC(max_concurrent, 0),
432 KFD_PERF_DESC(num_counters, 0),
433 KFD_PERF_DESC(counter_ids, 0),
435 /****************************************/
437 static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
441 struct kfd_topology_device *dev;
442 uint32_t log_max_watch_addr;
444 /* Making sure that the buffer is an empty string */
447 if (strcmp(attr->name, "gpu_id") == 0) {
448 dev = container_of(attr, struct kfd_topology_device,
450 if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
452 return sysfs_show_32bit_val(buffer, offs, dev->gpu_id);
455 if (strcmp(attr->name, "name") == 0) {
456 dev = container_of(attr, struct kfd_topology_device,
459 if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
461 return sysfs_show_str_val(buffer, offs, dev->node_props.name);
464 dev = container_of(attr, struct kfd_topology_device,
466 if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
468 sysfs_show_32bit_prop(buffer, offs, "cpu_cores_count",
469 dev->node_props.cpu_cores_count);
470 sysfs_show_32bit_prop(buffer, offs, "simd_count",
471 dev->gpu ? dev->node_props.simd_count : 0);
472 sysfs_show_32bit_prop(buffer, offs, "mem_banks_count",
473 dev->node_props.mem_banks_count);
474 sysfs_show_32bit_prop(buffer, offs, "caches_count",
475 dev->node_props.caches_count);
476 sysfs_show_32bit_prop(buffer, offs, "io_links_count",
477 dev->node_props.io_links_count);
478 sysfs_show_32bit_prop(buffer, offs, "p2p_links_count",
479 dev->node_props.p2p_links_count);
480 sysfs_show_32bit_prop(buffer, offs, "cpu_core_id_base",
481 dev->node_props.cpu_core_id_base);
482 sysfs_show_32bit_prop(buffer, offs, "simd_id_base",
483 dev->node_props.simd_id_base);
484 sysfs_show_32bit_prop(buffer, offs, "max_waves_per_simd",
485 dev->node_props.max_waves_per_simd);
486 sysfs_show_32bit_prop(buffer, offs, "lds_size_in_kb",
487 dev->node_props.lds_size_in_kb);
488 sysfs_show_32bit_prop(buffer, offs, "gds_size_in_kb",
489 dev->node_props.gds_size_in_kb);
490 sysfs_show_32bit_prop(buffer, offs, "num_gws",
491 dev->node_props.num_gws);
492 sysfs_show_32bit_prop(buffer, offs, "wave_front_size",
493 dev->node_props.wave_front_size);
494 sysfs_show_32bit_prop(buffer, offs, "array_count",
495 dev->node_props.array_count);
496 sysfs_show_32bit_prop(buffer, offs, "simd_arrays_per_engine",
497 dev->node_props.simd_arrays_per_engine);
498 sysfs_show_32bit_prop(buffer, offs, "cu_per_simd_array",
499 dev->node_props.cu_per_simd_array);
500 sysfs_show_32bit_prop(buffer, offs, "simd_per_cu",
501 dev->node_props.simd_per_cu);
502 sysfs_show_32bit_prop(buffer, offs, "max_slots_scratch_cu",
503 dev->node_props.max_slots_scratch_cu);
504 sysfs_show_32bit_prop(buffer, offs, "gfx_target_version",
505 dev->node_props.gfx_target_version);
506 sysfs_show_32bit_prop(buffer, offs, "vendor_id",
507 dev->node_props.vendor_id);
508 sysfs_show_32bit_prop(buffer, offs, "device_id",
509 dev->node_props.device_id);
510 sysfs_show_32bit_prop(buffer, offs, "location_id",
511 dev->node_props.location_id);
512 sysfs_show_32bit_prop(buffer, offs, "domain",
513 dev->node_props.domain);
514 sysfs_show_32bit_prop(buffer, offs, "drm_render_minor",
515 dev->node_props.drm_render_minor);
516 sysfs_show_64bit_prop(buffer, offs, "hive_id",
517 dev->node_props.hive_id);
518 sysfs_show_32bit_prop(buffer, offs, "num_sdma_engines",
519 dev->node_props.num_sdma_engines);
520 sysfs_show_32bit_prop(buffer, offs, "num_sdma_xgmi_engines",
521 dev->node_props.num_sdma_xgmi_engines);
522 sysfs_show_32bit_prop(buffer, offs, "num_sdma_queues_per_engine",
523 dev->node_props.num_sdma_queues_per_engine);
524 sysfs_show_32bit_prop(buffer, offs, "num_cp_queues",
525 dev->node_props.num_cp_queues);
529 __ilog2_u32(dev->gpu->device_info.num_of_watch_points);
531 if (log_max_watch_addr) {
532 dev->node_props.capability |=
533 HSA_CAP_WATCH_POINTS_SUPPORTED;
535 dev->node_props.capability |=
536 ((log_max_watch_addr <<
537 HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
538 HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
541 if (dev->gpu->adev->asic_type == CHIP_TONGA)
542 dev->node_props.capability |=
543 HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
545 sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_fcompute",
546 dev->node_props.max_engine_clk_fcompute);
548 sysfs_show_64bit_prop(buffer, offs, "local_mem_size", 0ULL);
550 sysfs_show_32bit_prop(buffer, offs, "fw_version",
551 dev->gpu->mec_fw_version);
552 sysfs_show_32bit_prop(buffer, offs, "capability",
553 dev->node_props.capability);
554 sysfs_show_32bit_prop(buffer, offs, "sdma_fw_version",
555 dev->gpu->sdma_fw_version);
556 sysfs_show_64bit_prop(buffer, offs, "unique_id",
557 dev->gpu->adev->unique_id);
561 return sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_ccompute",
562 cpufreq_quick_get_max(0)/1000);
565 static const struct sysfs_ops node_ops = {
569 static struct kobj_type node_type = {
570 .release = kfd_topology_kobj_release,
571 .sysfs_ops = &node_ops,
574 static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
576 sysfs_remove_file(kobj, attr);
581 static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
583 struct kfd_iolink_properties *p2plink;
584 struct kfd_iolink_properties *iolink;
585 struct kfd_cache_properties *cache;
586 struct kfd_mem_properties *mem;
587 struct kfd_perf_properties *perf;
589 if (dev->kobj_iolink) {
590 list_for_each_entry(iolink, &dev->io_link_props, list)
592 kfd_remove_sysfs_file(iolink->kobj,
596 kobject_del(dev->kobj_iolink);
597 kobject_put(dev->kobj_iolink);
598 dev->kobj_iolink = NULL;
601 if (dev->kobj_p2plink) {
602 list_for_each_entry(p2plink, &dev->p2p_link_props, list)
604 kfd_remove_sysfs_file(p2plink->kobj,
606 p2plink->kobj = NULL;
608 kobject_del(dev->kobj_p2plink);
609 kobject_put(dev->kobj_p2plink);
610 dev->kobj_p2plink = NULL;
613 if (dev->kobj_cache) {
614 list_for_each_entry(cache, &dev->cache_props, list)
616 kfd_remove_sysfs_file(cache->kobj,
620 kobject_del(dev->kobj_cache);
621 kobject_put(dev->kobj_cache);
622 dev->kobj_cache = NULL;
626 list_for_each_entry(mem, &dev->mem_props, list)
628 kfd_remove_sysfs_file(mem->kobj, &mem->attr);
631 kobject_del(dev->kobj_mem);
632 kobject_put(dev->kobj_mem);
633 dev->kobj_mem = NULL;
636 if (dev->kobj_perf) {
637 list_for_each_entry(perf, &dev->perf_props, list) {
638 kfree(perf->attr_group);
639 perf->attr_group = NULL;
641 kobject_del(dev->kobj_perf);
642 kobject_put(dev->kobj_perf);
643 dev->kobj_perf = NULL;
646 if (dev->kobj_node) {
647 sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
648 sysfs_remove_file(dev->kobj_node, &dev->attr_name);
649 sysfs_remove_file(dev->kobj_node, &dev->attr_props);
650 kobject_del(dev->kobj_node);
651 kobject_put(dev->kobj_node);
652 dev->kobj_node = NULL;
656 static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
659 struct kfd_iolink_properties *p2plink;
660 struct kfd_iolink_properties *iolink;
661 struct kfd_cache_properties *cache;
662 struct kfd_mem_properties *mem;
663 struct kfd_perf_properties *perf;
665 uint32_t i, num_attrs;
666 struct attribute **attrs;
668 if (WARN_ON(dev->kobj_node))
672 * Creating the sysfs folders
674 dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
678 ret = kobject_init_and_add(dev->kobj_node, &node_type,
679 sys_props.kobj_nodes, "%d", id);
681 kobject_put(dev->kobj_node);
685 dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
689 dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
690 if (!dev->kobj_cache)
693 dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
694 if (!dev->kobj_iolink)
697 dev->kobj_p2plink = kobject_create_and_add("p2p_links", dev->kobj_node);
698 if (!dev->kobj_p2plink)
701 dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
706 * Creating sysfs files for node properties
708 dev->attr_gpuid.name = "gpu_id";
709 dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
710 sysfs_attr_init(&dev->attr_gpuid);
711 dev->attr_name.name = "name";
712 dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
713 sysfs_attr_init(&dev->attr_name);
714 dev->attr_props.name = "properties";
715 dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
716 sysfs_attr_init(&dev->attr_props);
717 ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
720 ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
723 ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
728 list_for_each_entry(mem, &dev->mem_props, list) {
729 mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
732 ret = kobject_init_and_add(mem->kobj, &mem_type,
733 dev->kobj_mem, "%d", i);
735 kobject_put(mem->kobj);
739 mem->attr.name = "properties";
740 mem->attr.mode = KFD_SYSFS_FILE_MODE;
741 sysfs_attr_init(&mem->attr);
742 ret = sysfs_create_file(mem->kobj, &mem->attr);
749 list_for_each_entry(cache, &dev->cache_props, list) {
750 cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
753 ret = kobject_init_and_add(cache->kobj, &cache_type,
754 dev->kobj_cache, "%d", i);
756 kobject_put(cache->kobj);
760 cache->attr.name = "properties";
761 cache->attr.mode = KFD_SYSFS_FILE_MODE;
762 sysfs_attr_init(&cache->attr);
763 ret = sysfs_create_file(cache->kobj, &cache->attr);
770 list_for_each_entry(iolink, &dev->io_link_props, list) {
771 iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
774 ret = kobject_init_and_add(iolink->kobj, &iolink_type,
775 dev->kobj_iolink, "%d", i);
777 kobject_put(iolink->kobj);
781 iolink->attr.name = "properties";
782 iolink->attr.mode = KFD_SYSFS_FILE_MODE;
783 sysfs_attr_init(&iolink->attr);
784 ret = sysfs_create_file(iolink->kobj, &iolink->attr);
791 list_for_each_entry(p2plink, &dev->p2p_link_props, list) {
792 p2plink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
795 ret = kobject_init_and_add(p2plink->kobj, &iolink_type,
796 dev->kobj_p2plink, "%d", i);
798 kobject_put(p2plink->kobj);
802 p2plink->attr.name = "properties";
803 p2plink->attr.mode = KFD_SYSFS_FILE_MODE;
804 sysfs_attr_init(&iolink->attr);
805 ret = sysfs_create_file(p2plink->kobj, &p2plink->attr);
811 /* All hardware blocks have the same number of attributes. */
812 num_attrs = ARRAY_SIZE(perf_attr_iommu);
813 list_for_each_entry(perf, &dev->perf_props, list) {
814 perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
815 * num_attrs + sizeof(struct attribute_group),
817 if (!perf->attr_group)
820 attrs = (struct attribute **)(perf->attr_group + 1);
821 if (!strcmp(perf->block_name, "iommu")) {
822 /* Information of IOMMU's num_counters and counter_ids is shown
823 * under /sys/bus/event_source/devices/amd_iommu. We don't
826 perf_attr_iommu[0].data = perf->max_concurrent;
827 for (i = 0; i < num_attrs; i++)
828 attrs[i] = &perf_attr_iommu[i].attr.attr;
830 perf->attr_group->name = perf->block_name;
831 perf->attr_group->attrs = attrs;
832 ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
840 /* Called with write topology lock acquired */
841 static int kfd_build_sysfs_node_tree(void)
843 struct kfd_topology_device *dev;
847 list_for_each_entry(dev, &topology_device_list, list) {
848 ret = kfd_build_sysfs_node_entry(dev, i);
857 /* Called with write topology lock acquired */
858 static void kfd_remove_sysfs_node_tree(void)
860 struct kfd_topology_device *dev;
862 list_for_each_entry(dev, &topology_device_list, list)
863 kfd_remove_sysfs_node_entry(dev);
866 static int kfd_topology_update_sysfs(void)
870 if (!sys_props.kobj_topology) {
871 sys_props.kobj_topology =
872 kfd_alloc_struct(sys_props.kobj_topology);
873 if (!sys_props.kobj_topology)
876 ret = kobject_init_and_add(sys_props.kobj_topology,
877 &sysprops_type, &kfd_device->kobj,
880 kobject_put(sys_props.kobj_topology);
884 sys_props.kobj_nodes = kobject_create_and_add("nodes",
885 sys_props.kobj_topology);
886 if (!sys_props.kobj_nodes)
889 sys_props.attr_genid.name = "generation_id";
890 sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
891 sysfs_attr_init(&sys_props.attr_genid);
892 ret = sysfs_create_file(sys_props.kobj_topology,
893 &sys_props.attr_genid);
897 sys_props.attr_props.name = "system_properties";
898 sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
899 sysfs_attr_init(&sys_props.attr_props);
900 ret = sysfs_create_file(sys_props.kobj_topology,
901 &sys_props.attr_props);
906 kfd_remove_sysfs_node_tree();
908 return kfd_build_sysfs_node_tree();
911 static void kfd_topology_release_sysfs(void)
913 kfd_remove_sysfs_node_tree();
914 if (sys_props.kobj_topology) {
915 sysfs_remove_file(sys_props.kobj_topology,
916 &sys_props.attr_genid);
917 sysfs_remove_file(sys_props.kobj_topology,
918 &sys_props.attr_props);
919 if (sys_props.kobj_nodes) {
920 kobject_del(sys_props.kobj_nodes);
921 kobject_put(sys_props.kobj_nodes);
922 sys_props.kobj_nodes = NULL;
924 kobject_del(sys_props.kobj_topology);
925 kobject_put(sys_props.kobj_topology);
926 sys_props.kobj_topology = NULL;
930 /* Called with write topology_lock acquired */
931 static void kfd_topology_update_device_list(struct list_head *temp_list,
932 struct list_head *master_list)
934 while (!list_empty(temp_list)) {
935 list_move_tail(temp_list->next, master_list);
936 sys_props.num_devices++;
940 static void kfd_debug_print_topology(void)
942 struct kfd_topology_device *dev;
944 down_read(&topology_lock);
946 dev = list_last_entry(&topology_device_list,
947 struct kfd_topology_device, list);
949 if (dev->node_props.cpu_cores_count &&
950 dev->node_props.simd_count) {
951 pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
952 dev->node_props.device_id,
953 dev->node_props.vendor_id);
954 } else if (dev->node_props.cpu_cores_count)
955 pr_info("Topology: Add CPU node\n");
956 else if (dev->node_props.simd_count)
957 pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
958 dev->node_props.device_id,
959 dev->node_props.vendor_id);
961 up_read(&topology_lock);
964 /* Helper function for intializing platform_xx members of
965 * kfd_system_properties. Uses OEM info from the last CPU/APU node.
967 static void kfd_update_system_properties(void)
969 struct kfd_topology_device *dev;
971 down_read(&topology_lock);
972 dev = list_last_entry(&topology_device_list,
973 struct kfd_topology_device, list);
975 sys_props.platform_id =
976 (*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
977 sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
978 sys_props.platform_rev = dev->oem_revision;
980 up_read(&topology_lock);
983 static void find_system_memory(const struct dmi_header *dm,
986 struct kfd_mem_properties *mem;
987 u16 mem_width, mem_clock;
988 struct kfd_topology_device *kdev =
989 (struct kfd_topology_device *)private;
990 const u8 *dmi_data = (const u8 *)(dm + 1);
992 if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
993 mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
994 mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
995 list_for_each_entry(mem, &kdev->mem_props, list) {
996 if (mem_width != 0xFFFF && mem_width != 0)
997 mem->width = mem_width;
999 mem->mem_clk_max = mem_clock;
1005 * Performance counters information is not part of CRAT but we would like to
1006 * put them in the sysfs under topology directory for Thunk to get the data.
1007 * This function is called before updating the sysfs.
1009 static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
1011 /* These are the only counters supported so far */
1012 return kfd_iommu_add_perf_counters(kdev);
1015 /* kfd_add_non_crat_information - Add information that is not currently
1016 * defined in CRAT but is necessary for KFD topology
1017 * @dev - topology device to which addition info is added
1019 static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
1021 /* Check if CPU only node. */
1023 /* Add system memory information */
1024 dmi_walk(find_system_memory, kdev);
1026 /* TODO: For GPU node, rearrange code from kfd_topology_add_device */
1029 /* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
1030 * Ignore CRAT for all other devices. AMD APU is identified if both CPU
1031 * and GPU cores are present.
1032 * @device_list - topology device list created by parsing ACPI CRAT table.
1033 * @return - TRUE if invalid, FALSE is valid.
1035 static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
1037 struct kfd_topology_device *dev;
1039 list_for_each_entry(dev, device_list, list) {
1040 if (dev->node_props.cpu_cores_count &&
1041 dev->node_props.simd_count)
1044 pr_info("Ignoring ACPI CRAT on non-APU system\n");
1048 int kfd_topology_init(void)
1050 void *crat_image = NULL;
1051 size_t image_size = 0;
1053 struct list_head temp_topology_device_list;
1054 int cpu_only_node = 0;
1055 struct kfd_topology_device *kdev;
1056 int proximity_domain;
1058 /* topology_device_list - Master list of all topology devices
1059 * temp_topology_device_list - temporary list created while parsing CRAT
1060 * or VCRAT. Once parsing is complete the contents of list is moved to
1061 * topology_device_list
1064 /* Initialize the head for the both the lists */
1065 INIT_LIST_HEAD(&topology_device_list);
1066 INIT_LIST_HEAD(&temp_topology_device_list);
1067 init_rwsem(&topology_lock);
1069 memset(&sys_props, 0, sizeof(sys_props));
1071 /* Proximity domains in ACPI CRAT tables start counting at
1072 * 0. The same should be true for virtual CRAT tables created
1073 * at this stage. GPUs added later in kfd_topology_add_device
1076 proximity_domain = 0;
1079 * Get the CRAT image from the ACPI. If ACPI doesn't have one
1080 * or if ACPI CRAT is invalid create a virtual CRAT.
1081 * NOTE: The current implementation expects all AMD APUs to have
1082 * CRAT. If no CRAT is available, it is assumed to be a CPU
1084 ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
1086 ret = kfd_parse_crat_table(crat_image,
1087 &temp_topology_device_list,
1090 kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
1091 kfd_release_topology_device_list(
1092 &temp_topology_device_list);
1093 kfd_destroy_crat_image(crat_image);
1099 ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
1100 COMPUTE_UNIT_CPU, NULL,
1104 pr_err("Error creating VCRAT table for CPU\n");
1108 ret = kfd_parse_crat_table(crat_image,
1109 &temp_topology_device_list,
1112 pr_err("Error parsing VCRAT table for CPU\n");
1117 kdev = list_first_entry(&temp_topology_device_list,
1118 struct kfd_topology_device, list);
1119 kfd_add_perf_to_topology(kdev);
1121 down_write(&topology_lock);
1122 kfd_topology_update_device_list(&temp_topology_device_list,
1123 &topology_device_list);
1124 topology_crat_proximity_domain = sys_props.num_devices-1;
1125 ret = kfd_topology_update_sysfs();
1126 up_write(&topology_lock);
1129 sys_props.generation_count++;
1130 kfd_update_system_properties();
1131 kfd_debug_print_topology();
1133 pr_err("Failed to update topology in sysfs ret=%d\n", ret);
1135 /* For nodes with GPU, this information gets added
1136 * when GPU is detected (kfd_topology_add_device).
1138 if (cpu_only_node) {
1139 /* Add additional information to CPU only node created above */
1140 down_write(&topology_lock);
1141 kdev = list_first_entry(&topology_device_list,
1142 struct kfd_topology_device, list);
1143 up_write(&topology_lock);
1144 kfd_add_non_crat_information(kdev);
1148 kfd_destroy_crat_image(crat_image);
1152 void kfd_topology_shutdown(void)
1154 down_write(&topology_lock);
1155 kfd_topology_release_sysfs();
1156 kfd_release_live_view();
1157 up_write(&topology_lock);
1160 static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
1164 uint64_t local_mem_size;
1170 local_mem_size = gpu->local_mem_info.local_mem_size_private +
1171 gpu->local_mem_info.local_mem_size_public;
1173 buf[0] = gpu->pdev->devfn;
1174 buf[1] = gpu->pdev->subsystem_vendor |
1175 (gpu->pdev->subsystem_device << 16);
1176 buf[2] = pci_domain_nr(gpu->pdev->bus);
1177 buf[3] = gpu->pdev->device;
1178 buf[4] = gpu->pdev->bus->number;
1179 buf[5] = lower_32_bits(local_mem_size);
1180 buf[6] = upper_32_bits(local_mem_size);
1182 for (i = 0, hashout = 0; i < 7; i++)
1183 hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
1187 /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
1188 * the GPU device is not already present in the topology device
1189 * list then return NULL. This means a new topology device has to
1190 * be created for this GPU.
1192 static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
1194 struct kfd_topology_device *dev;
1195 struct kfd_topology_device *out_dev = NULL;
1196 struct kfd_mem_properties *mem;
1197 struct kfd_cache_properties *cache;
1198 struct kfd_iolink_properties *iolink;
1199 struct kfd_iolink_properties *p2plink;
1201 down_write(&topology_lock);
1202 list_for_each_entry(dev, &topology_device_list, list) {
1203 /* Discrete GPUs need their own topology device list
1204 * entries. Don't assign them to CPU/APU nodes.
1206 if (!gpu->use_iommu_v2 &&
1207 dev->node_props.cpu_cores_count)
1210 if (!dev->gpu && (dev->node_props.simd_count > 0)) {
1214 list_for_each_entry(mem, &dev->mem_props, list)
1215 mem->gpu = dev->gpu;
1216 list_for_each_entry(cache, &dev->cache_props, list)
1217 cache->gpu = dev->gpu;
1218 list_for_each_entry(iolink, &dev->io_link_props, list)
1219 iolink->gpu = dev->gpu;
1220 list_for_each_entry(p2plink, &dev->p2p_link_props, list)
1221 p2plink->gpu = dev->gpu;
1225 up_write(&topology_lock);
1229 static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
1232 * TODO: Generate an event for thunk about the arrival/removal
1237 /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
1238 * patch this after CRAT parsing.
1240 static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
1242 struct kfd_mem_properties *mem;
1243 struct kfd_local_mem_info local_mem_info;
1248 /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
1249 * single bank of VRAM local memory.
1250 * for dGPUs - VCRAT reports only one bank of Local Memory
1251 * for APUs - If CRAT from ACPI reports more than one bank, then
1252 * all the banks will report the same mem_clk_max information
1254 amdgpu_amdkfd_get_local_mem_info(dev->gpu->adev, &local_mem_info);
1256 list_for_each_entry(mem, &dev->mem_props, list)
1257 mem->mem_clk_max = local_mem_info.mem_clk_max;
1260 static void kfd_set_iolink_no_atomics(struct kfd_topology_device *dev,
1261 struct kfd_topology_device *target_gpu_dev,
1262 struct kfd_iolink_properties *link)
1264 /* xgmi always supports atomics between links. */
1265 if (link->iolink_type == CRAT_IOLINK_TYPE_XGMI)
1268 /* check pcie support to set cpu(dev) flags for target_gpu_dev link. */
1269 if (target_gpu_dev) {
1272 pcie_capability_read_dword(target_gpu_dev->gpu->pdev,
1273 PCI_EXP_DEVCAP2, &cap);
1275 if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1276 PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1277 link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1278 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1279 /* set gpu (dev) flags. */
1281 if (!dev->gpu->pci_atomic_requested ||
1282 dev->gpu->adev->asic_type == CHIP_HAWAII)
1283 link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1284 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1288 static void kfd_set_iolink_non_coherent(struct kfd_topology_device *to_dev,
1289 struct kfd_iolink_properties *outbound_link,
1290 struct kfd_iolink_properties *inbound_link)
1292 /* CPU -> GPU with PCIe */
1294 inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
1295 inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1298 /* GPU <-> GPU with PCIe and
1301 if (inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS ||
1302 (inbound_link->iolink_type == CRAT_IOLINK_TYPE_XGMI &&
1303 KFD_GC_VERSION(to_dev->gpu) == IP_VERSION(9, 4, 0))) {
1304 outbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1305 inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1310 static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
1312 struct kfd_iolink_properties *link, *inbound_link;
1313 struct kfd_topology_device *peer_dev;
1315 if (!dev || !dev->gpu)
1318 /* GPU only creates direct links so apply flags setting to all */
1319 list_for_each_entry(link, &dev->io_link_props, list) {
1320 link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1321 kfd_set_iolink_no_atomics(dev, NULL, link);
1322 peer_dev = kfd_topology_device_by_proximity_domain(
1328 /* Include the CPU peer in GPU hive if connected over xGMI. */
1329 if (!peer_dev->gpu && !peer_dev->node_props.hive_id &&
1330 dev->node_props.hive_id &&
1331 dev->gpu->adev->gmc.xgmi.connected_to_cpu)
1332 peer_dev->node_props.hive_id = dev->node_props.hive_id;
1334 list_for_each_entry(inbound_link, &peer_dev->io_link_props,
1336 if (inbound_link->node_to != link->node_from)
1339 inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1340 kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
1341 kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
1345 /* Create indirect links so apply flags setting to all */
1346 list_for_each_entry(link, &dev->p2p_link_props, list) {
1347 link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1348 kfd_set_iolink_no_atomics(dev, NULL, link);
1349 peer_dev = kfd_topology_device_by_proximity_domain(
1355 list_for_each_entry(inbound_link, &peer_dev->p2p_link_props,
1357 if (inbound_link->node_to != link->node_from)
1360 inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1361 kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
1362 kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
1367 static int kfd_build_p2p_node_entry(struct kfd_topology_device *dev,
1368 struct kfd_iolink_properties *p2plink)
1372 p2plink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
1376 ret = kobject_init_and_add(p2plink->kobj, &iolink_type,
1377 dev->kobj_p2plink, "%d", dev->node_props.p2p_links_count - 1);
1379 kobject_put(p2plink->kobj);
1383 p2plink->attr.name = "properties";
1384 p2plink->attr.mode = KFD_SYSFS_FILE_MODE;
1385 sysfs_attr_init(&p2plink->attr);
1386 ret = sysfs_create_file(p2plink->kobj, &p2plink->attr);
1393 static int kfd_create_indirect_link_prop(struct kfd_topology_device *kdev, int gpu_node)
1395 struct kfd_iolink_properties *props = NULL, *props2 = NULL;
1396 struct kfd_iolink_properties *gpu_link, *cpu_link;
1397 struct kfd_topology_device *cpu_dev;
1402 list_for_each_entry(cpu_dev, &topology_device_list, list) {
1408 gpu_link = list_first_entry(&kdev->io_link_props,
1409 struct kfd_iolink_properties, list);
1413 for (i = 0; i < num_cpu; i++) {
1415 if (gpu_link->node_to == i)
1418 /* find CPU <--> CPU links */
1419 cpu_dev = kfd_topology_device_by_proximity_domain(i);
1421 list_for_each_entry(cpu_link,
1422 &cpu_dev->io_link_props, list) {
1423 if (cpu_link->node_to == gpu_link->node_to)
1428 if (cpu_link->node_to != gpu_link->node_to)
1431 /* CPU <--> CPU <--> GPU, GPU node*/
1432 props = kfd_alloc_struct(props);
1436 memcpy(props, gpu_link, sizeof(struct kfd_iolink_properties));
1437 props->weight = gpu_link->weight + cpu_link->weight;
1438 props->min_latency = gpu_link->min_latency + cpu_link->min_latency;
1439 props->max_latency = gpu_link->max_latency + cpu_link->max_latency;
1440 props->min_bandwidth = min(gpu_link->min_bandwidth, cpu_link->min_bandwidth);
1441 props->max_bandwidth = min(gpu_link->max_bandwidth, cpu_link->max_bandwidth);
1443 props->node_from = gpu_node;
1445 kdev->node_props.p2p_links_count++;
1446 list_add_tail(&props->list, &kdev->p2p_link_props);
1447 ret = kfd_build_p2p_node_entry(kdev, props);
1451 /* for small Bar, no CPU --> GPU in-direct links */
1452 if (kfd_dev_is_large_bar(kdev->gpu)) {
1453 /* CPU <--> CPU <--> GPU, CPU node*/
1454 props2 = kfd_alloc_struct(props2);
1458 memcpy(props2, props, sizeof(struct kfd_iolink_properties));
1459 props2->node_from = i;
1460 props2->node_to = gpu_node;
1461 props2->kobj = NULL;
1462 cpu_dev->node_props.p2p_links_count++;
1463 list_add_tail(&props2->list, &cpu_dev->p2p_link_props);
1464 ret = kfd_build_p2p_node_entry(cpu_dev, props2);
1472 #if defined(CONFIG_HSA_AMD_P2P)
1473 static int kfd_add_peer_prop(struct kfd_topology_device *kdev,
1474 struct kfd_topology_device *peer, int from, int to)
1476 struct kfd_iolink_properties *props = NULL;
1477 struct kfd_iolink_properties *iolink1, *iolink2, *iolink3;
1478 struct kfd_topology_device *cpu_dev;
1481 if (!amdgpu_device_is_peer_accessible(
1486 iolink1 = list_first_entry(&kdev->io_link_props,
1487 struct kfd_iolink_properties, list);
1491 iolink2 = list_first_entry(&peer->io_link_props,
1492 struct kfd_iolink_properties, list);
1496 props = kfd_alloc_struct(props);
1500 memcpy(props, iolink1, sizeof(struct kfd_iolink_properties));
1502 props->weight = iolink1->weight + iolink2->weight;
1503 props->min_latency = iolink1->min_latency + iolink2->min_latency;
1504 props->max_latency = iolink1->max_latency + iolink2->max_latency;
1505 props->min_bandwidth = min(iolink1->min_bandwidth, iolink2->min_bandwidth);
1506 props->max_bandwidth = min(iolink2->max_bandwidth, iolink2->max_bandwidth);
1508 if (iolink1->node_to != iolink2->node_to) {
1510 cpu_dev = kfd_topology_device_by_proximity_domain(iolink1->node_to);
1512 list_for_each_entry(iolink3, &cpu_dev->io_link_props, list)
1513 if (iolink3->node_to == iolink2->node_to)
1516 props->weight += iolink3->weight;
1517 props->min_latency += iolink3->min_latency;
1518 props->max_latency += iolink3->max_latency;
1519 props->min_bandwidth = min(props->min_bandwidth,
1520 iolink3->min_bandwidth);
1521 props->max_bandwidth = min(props->max_bandwidth,
1522 iolink3->max_bandwidth);
1524 WARN(1, "CPU node not found");
1528 props->node_from = from;
1529 props->node_to = to;
1530 peer->node_props.p2p_links_count++;
1531 list_add_tail(&props->list, &peer->p2p_link_props);
1532 ret = kfd_build_p2p_node_entry(peer, props);
1538 static int kfd_dev_create_p2p_links(void)
1540 struct kfd_topology_device *dev;
1541 struct kfd_topology_device *new_dev;
1542 #if defined(CONFIG_HSA_AMD_P2P)
1549 list_for_each_entry(dev, &topology_device_list, list)
1554 new_dev = list_last_entry(&topology_device_list, struct kfd_topology_device, list);
1555 if (WARN_ON(!new_dev->gpu))
1560 /* create in-direct links */
1561 ret = kfd_create_indirect_link_prop(new_dev, k);
1565 /* create p2p links */
1566 #if defined(CONFIG_HSA_AMD_P2P)
1568 list_for_each_entry(dev, &topology_device_list, list) {
1571 if (!dev->gpu || !dev->gpu->adev ||
1572 (dev->gpu->hive_id &&
1573 dev->gpu->hive_id == new_dev->gpu->hive_id))
1576 /* check if node(s) is/are peer accessible in one direction or bi-direction */
1577 ret = kfd_add_peer_prop(new_dev, dev, i, k);
1581 ret = kfd_add_peer_prop(dev, new_dev, k, i);
1593 int kfd_topology_add_device(struct kfd_dev *gpu)
1596 struct kfd_topology_device *dev;
1597 struct kfd_cu_info cu_info;
1599 struct list_head temp_topology_device_list;
1600 void *crat_image = NULL;
1601 size_t image_size = 0;
1602 int proximity_domain;
1604 const char *asic_name = amdgpu_asic_name[gpu->adev->asic_type];
1606 INIT_LIST_HEAD(&temp_topology_device_list);
1608 gpu_id = kfd_generate_gpu_id(gpu);
1609 pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1611 /* Check to see if this gpu device exists in the topology_device_list.
1612 * If so, assign the gpu to that device,
1613 * else create a Virtual CRAT for this gpu device and then parse that
1614 * CRAT to create a new topology device. Once created assign the gpu to
1615 * that topology device
1617 dev = kfd_assign_gpu(gpu);
1619 down_write(&topology_lock);
1620 proximity_domain = ++topology_crat_proximity_domain;
1622 res = kfd_create_crat_image_virtual(&crat_image, &image_size,
1623 COMPUTE_UNIT_GPU, gpu,
1626 pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
1628 topology_crat_proximity_domain--;
1631 res = kfd_parse_crat_table(crat_image,
1632 &temp_topology_device_list,
1635 pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
1637 topology_crat_proximity_domain--;
1641 kfd_topology_update_device_list(&temp_topology_device_list,
1642 &topology_device_list);
1644 /* Update the SYSFS tree, since we added another topology
1647 res = kfd_topology_update_sysfs();
1648 up_write(&topology_lock);
1651 sys_props.generation_count++;
1653 pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
1655 dev = kfd_assign_gpu(gpu);
1656 if (WARN_ON(!dev)) {
1662 dev->gpu_id = gpu_id;
1665 kfd_dev_create_p2p_links();
1667 /* TODO: Move the following lines to function
1668 * kfd_add_non_crat_information
1671 /* Fill-in additional information that is not available in CRAT but
1672 * needed for the topology
1675 amdgpu_amdkfd_get_cu_info(dev->gpu->adev, &cu_info);
1677 for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1; i++) {
1678 dev->node_props.name[i] = __tolower(asic_name[i]);
1679 if (asic_name[i] == '\0')
1682 dev->node_props.name[i] = '\0';
1684 dev->node_props.simd_arrays_per_engine =
1685 cu_info.num_shader_arrays_per_engine;
1687 dev->node_props.gfx_target_version = gpu->device_info.gfx_target_version;
1688 dev->node_props.vendor_id = gpu->pdev->vendor;
1689 dev->node_props.device_id = gpu->pdev->device;
1690 dev->node_props.capability |=
1691 ((dev->gpu->adev->rev_id << HSA_CAP_ASIC_REVISION_SHIFT) &
1692 HSA_CAP_ASIC_REVISION_MASK);
1693 dev->node_props.location_id = pci_dev_id(gpu->pdev);
1694 dev->node_props.domain = pci_domain_nr(gpu->pdev->bus);
1695 dev->node_props.max_engine_clk_fcompute =
1696 amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->adev);
1697 dev->node_props.max_engine_clk_ccompute =
1698 cpufreq_quick_get_max(0) / 1000;
1699 dev->node_props.drm_render_minor =
1700 gpu->shared_resources.drm_render_minor;
1702 dev->node_props.hive_id = gpu->hive_id;
1703 dev->node_props.num_sdma_engines = kfd_get_num_sdma_engines(gpu);
1704 dev->node_props.num_sdma_xgmi_engines =
1705 kfd_get_num_xgmi_sdma_engines(gpu);
1706 dev->node_props.num_sdma_queues_per_engine =
1707 gpu->device_info.num_sdma_queues_per_engine -
1708 gpu->device_info.num_reserved_sdma_queues_per_engine;
1709 dev->node_props.num_gws = (dev->gpu->gws &&
1710 dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
1711 dev->gpu->adev->gds.gws_size : 0;
1712 dev->node_props.num_cp_queues = get_cp_queues_num(dev->gpu->dqm);
1714 kfd_fill_mem_clk_max_info(dev);
1715 kfd_fill_iolink_non_crat_info(dev);
1717 switch (dev->gpu->adev->asic_type) {
1721 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
1722 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1723 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1727 case CHIP_POLARIS10:
1728 case CHIP_POLARIS11:
1729 case CHIP_POLARIS12:
1731 pr_debug("Adding doorbell packet type capability\n");
1732 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
1733 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1734 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1737 if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(9, 0, 1))
1738 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1739 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1740 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1742 WARN(1, "Unexpected ASIC family %u",
1743 dev->gpu->adev->asic_type);
1747 * Overwrite ATS capability according to needs_iommu_device to fix
1748 * potential missing corresponding bit in CRAT of BIOS.
1750 if (dev->gpu->use_iommu_v2)
1751 dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
1753 dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;
1755 /* Fix errors in CZ CRAT.
1756 * simd_count: Carrizo CRAT reports wrong simd_count, probably
1757 * because it doesn't consider masked out CUs
1758 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
1760 if (dev->gpu->adev->asic_type == CHIP_CARRIZO) {
1761 dev->node_props.simd_count =
1762 cu_info.simd_per_cu * cu_info.cu_active_number;
1763 dev->node_props.max_waves_per_simd = 10;
1766 /* kfd only concerns sram ecc on GFX and HBM ecc on UMC */
1767 dev->node_props.capability |=
1768 ((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0) ?
1769 HSA_CAP_SRAM_EDCSUPPORTED : 0;
1770 dev->node_props.capability |=
1771 ((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
1772 HSA_CAP_MEM_EDCSUPPORTED : 0;
1774 if (KFD_GC_VERSION(dev->gpu) != IP_VERSION(9, 0, 1))
1775 dev->node_props.capability |= (dev->gpu->adev->ras_enabled != 0) ?
1776 HSA_CAP_RASEVENTNOTIFY : 0;
1778 if (KFD_IS_SVM_API_SUPPORTED(dev->gpu->adev->kfd.dev))
1779 dev->node_props.capability |= HSA_CAP_SVMAPI_SUPPORTED;
1781 kfd_debug_print_topology();
1784 kfd_notify_gpu_change(gpu_id, 1);
1786 kfd_destroy_crat_image(crat_image);
1791 * kfd_topology_update_io_links() - Update IO links after device removal.
1792 * @proximity_domain: Proximity domain value of the dev being removed.
1794 * The topology list currently is arranged in increasing order of
1797 * Two things need to be done when a device is removed:
1798 * 1. All the IO links to this device need to be removed.
1799 * 2. All nodes after the current device node need to move
1800 * up once this device node is removed from the topology
1801 * list. As a result, the proximity domain values for
1802 * all nodes after the node being deleted reduce by 1.
1803 * This would also cause the proximity domain values for
1804 * io links to be updated based on new proximity domain
1807 * Context: The caller must hold write topology_lock.
1809 static void kfd_topology_update_io_links(int proximity_domain)
1811 struct kfd_topology_device *dev;
1812 struct kfd_iolink_properties *iolink, *p2plink, *tmp;
1814 list_for_each_entry(dev, &topology_device_list, list) {
1815 if (dev->proximity_domain > proximity_domain)
1816 dev->proximity_domain--;
1818 list_for_each_entry_safe(iolink, tmp, &dev->io_link_props, list) {
1820 * If there is an io link to the dev being deleted
1821 * then remove that IO link also.
1823 if (iolink->node_to == proximity_domain) {
1824 list_del(&iolink->list);
1825 dev->node_props.io_links_count--;
1827 if (iolink->node_from > proximity_domain)
1828 iolink->node_from--;
1829 if (iolink->node_to > proximity_domain)
1834 list_for_each_entry_safe(p2plink, tmp, &dev->p2p_link_props, list) {
1836 * If there is a p2p link to the dev being deleted
1837 * then remove that p2p link also.
1839 if (p2plink->node_to == proximity_domain) {
1840 list_del(&p2plink->list);
1841 dev->node_props.p2p_links_count--;
1843 if (p2plink->node_from > proximity_domain)
1844 p2plink->node_from--;
1845 if (p2plink->node_to > proximity_domain)
1852 int kfd_topology_remove_device(struct kfd_dev *gpu)
1854 struct kfd_topology_device *dev, *tmp;
1859 down_write(&topology_lock);
1861 list_for_each_entry_safe(dev, tmp, &topology_device_list, list) {
1862 if (dev->gpu == gpu) {
1863 gpu_id = dev->gpu_id;
1864 kfd_remove_sysfs_node_entry(dev);
1865 kfd_release_topology_device(dev);
1866 sys_props.num_devices--;
1867 kfd_topology_update_io_links(i);
1868 topology_crat_proximity_domain = sys_props.num_devices-1;
1869 sys_props.generation_count++;
1871 if (kfd_topology_update_sysfs() < 0)
1872 kfd_topology_release_sysfs();
1878 up_write(&topology_lock);
1881 kfd_notify_gpu_change(gpu_id, 0);
1886 /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
1887 * topology. If GPU device is found @idx, then valid kfd_dev pointer is
1888 * returned through @kdev
1889 * Return - 0: On success (@kdev will be NULL for non GPU nodes)
1890 * -1: If end of list
1892 int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
1895 struct kfd_topology_device *top_dev;
1896 uint8_t device_idx = 0;
1899 down_read(&topology_lock);
1901 list_for_each_entry(top_dev, &topology_device_list, list) {
1902 if (device_idx == idx) {
1903 *kdev = top_dev->gpu;
1904 up_read(&topology_lock);
1911 up_read(&topology_lock);
1917 static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
1919 int first_cpu_of_numa_node;
1921 if (!cpumask || cpumask == cpu_none_mask)
1923 first_cpu_of_numa_node = cpumask_first(cpumask);
1924 if (first_cpu_of_numa_node >= nr_cpu_ids)
1926 #ifdef CONFIG_X86_64
1927 return cpu_data(first_cpu_of_numa_node).apicid;
1929 return first_cpu_of_numa_node;
1933 /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
1934 * of the given NUMA node (numa_node_id)
1935 * Return -1 on failure
1937 int kfd_numa_node_to_apic_id(int numa_node_id)
1939 if (numa_node_id == -1) {
1940 pr_warn("Invalid NUMA Node. Use online CPU mask\n");
1941 return kfd_cpumask_to_apic_id(cpu_online_mask);
1943 return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
1946 void kfd_double_confirm_iommu_support(struct kfd_dev *gpu)
1948 struct kfd_topology_device *dev;
1950 gpu->use_iommu_v2 = false;
1952 if (!gpu->device_info.needs_iommu_device)
1955 down_read(&topology_lock);
1957 /* Only use IOMMUv2 if there is an APU topology node with no GPU
1958 * assigned yet. This GPU will be assigned to it.
1960 list_for_each_entry(dev, &topology_device_list, list)
1961 if (dev->node_props.cpu_cores_count &&
1962 dev->node_props.simd_count &&
1964 gpu->use_iommu_v2 = true;
1966 up_read(&topology_lock);
1969 #if defined(CONFIG_DEBUG_FS)
1971 int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
1973 struct kfd_topology_device *dev;
1977 down_read(&topology_lock);
1979 list_for_each_entry(dev, &topology_device_list, list) {
1985 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1986 r = dqm_debugfs_hqds(m, dev->gpu->dqm);
1991 up_read(&topology_lock);
1996 int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
1998 struct kfd_topology_device *dev;
2002 down_read(&topology_lock);
2004 list_for_each_entry(dev, &topology_device_list, list) {
2010 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
2011 r = pm_debugfs_runlist(m, &dev->gpu->dqm->packet_mgr);
2016 up_read(&topology_lock);