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"
44 /* topology_device_list - Master list of all topology devices */
45 static struct list_head topology_device_list;
46 static struct kfd_system_properties sys_props;
48 static DECLARE_RWSEM(topology_lock);
49 static uint32_t topology_crat_proximity_domain;
51 struct kfd_topology_device *kfd_topology_device_by_proximity_domain_no_lock(
52 uint32_t proximity_domain)
54 struct kfd_topology_device *top_dev;
55 struct kfd_topology_device *device = NULL;
57 list_for_each_entry(top_dev, &topology_device_list, list)
58 if (top_dev->proximity_domain == proximity_domain) {
66 struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
67 uint32_t proximity_domain)
69 struct kfd_topology_device *device = NULL;
71 down_read(&topology_lock);
73 device = kfd_topology_device_by_proximity_domain_no_lock(
75 up_read(&topology_lock);
80 struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
82 struct kfd_topology_device *top_dev = NULL;
83 struct kfd_topology_device *ret = NULL;
85 down_read(&topology_lock);
87 list_for_each_entry(top_dev, &topology_device_list, list)
88 if (top_dev->gpu_id == gpu_id) {
93 up_read(&topology_lock);
98 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
100 struct kfd_topology_device *top_dev;
102 top_dev = kfd_topology_device_by_id(gpu_id);
109 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
111 struct kfd_topology_device *top_dev;
112 struct kfd_dev *device = NULL;
114 down_read(&topology_lock);
116 list_for_each_entry(top_dev, &topology_device_list, list)
117 if (top_dev->gpu && top_dev->gpu->pdev == pdev) {
118 device = top_dev->gpu;
122 up_read(&topology_lock);
127 struct kfd_dev *kfd_device_by_adev(const struct amdgpu_device *adev)
129 struct kfd_topology_device *top_dev;
130 struct kfd_dev *device = NULL;
132 down_read(&topology_lock);
134 list_for_each_entry(top_dev, &topology_device_list, list)
135 if (top_dev->gpu && top_dev->gpu->adev == adev) {
136 device = top_dev->gpu;
140 up_read(&topology_lock);
145 /* Called with write topology_lock acquired */
146 static void kfd_release_topology_device(struct kfd_topology_device *dev)
148 struct kfd_mem_properties *mem;
149 struct kfd_cache_properties *cache;
150 struct kfd_iolink_properties *iolink;
151 struct kfd_perf_properties *perf;
153 list_del(&dev->list);
155 while (dev->mem_props.next != &dev->mem_props) {
156 mem = container_of(dev->mem_props.next,
157 struct kfd_mem_properties, list);
158 list_del(&mem->list);
162 while (dev->cache_props.next != &dev->cache_props) {
163 cache = container_of(dev->cache_props.next,
164 struct kfd_cache_properties, list);
165 list_del(&cache->list);
169 while (dev->io_link_props.next != &dev->io_link_props) {
170 iolink = container_of(dev->io_link_props.next,
171 struct kfd_iolink_properties, list);
172 list_del(&iolink->list);
176 while (dev->perf_props.next != &dev->perf_props) {
177 perf = container_of(dev->perf_props.next,
178 struct kfd_perf_properties, list);
179 list_del(&perf->list);
186 void kfd_release_topology_device_list(struct list_head *device_list)
188 struct kfd_topology_device *dev;
190 while (!list_empty(device_list)) {
191 dev = list_first_entry(device_list,
192 struct kfd_topology_device, list);
193 kfd_release_topology_device(dev);
197 static void kfd_release_live_view(void)
199 kfd_release_topology_device_list(&topology_device_list);
200 memset(&sys_props, 0, sizeof(sys_props));
203 struct kfd_topology_device *kfd_create_topology_device(
204 struct list_head *device_list)
206 struct kfd_topology_device *dev;
208 dev = kfd_alloc_struct(dev);
210 pr_err("No memory to allocate a topology device");
214 INIT_LIST_HEAD(&dev->mem_props);
215 INIT_LIST_HEAD(&dev->cache_props);
216 INIT_LIST_HEAD(&dev->io_link_props);
217 INIT_LIST_HEAD(&dev->perf_props);
219 list_add_tail(&dev->list, device_list);
225 #define sysfs_show_gen_prop(buffer, offs, fmt, ...) \
226 (offs += snprintf(buffer+offs, PAGE_SIZE-offs, \
228 #define sysfs_show_32bit_prop(buffer, offs, name, value) \
229 sysfs_show_gen_prop(buffer, offs, "%s %u\n", name, value)
230 #define sysfs_show_64bit_prop(buffer, offs, name, value) \
231 sysfs_show_gen_prop(buffer, offs, "%s %llu\n", name, value)
232 #define sysfs_show_32bit_val(buffer, offs, value) \
233 sysfs_show_gen_prop(buffer, offs, "%u\n", value)
234 #define sysfs_show_str_val(buffer, offs, value) \
235 sysfs_show_gen_prop(buffer, offs, "%s\n", value)
237 static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
242 /* Making sure that the buffer is an empty string */
245 if (attr == &sys_props.attr_genid) {
246 sysfs_show_32bit_val(buffer, offs,
247 sys_props.generation_count);
248 } else if (attr == &sys_props.attr_props) {
249 sysfs_show_64bit_prop(buffer, offs, "platform_oem",
250 sys_props.platform_oem);
251 sysfs_show_64bit_prop(buffer, offs, "platform_id",
252 sys_props.platform_id);
253 sysfs_show_64bit_prop(buffer, offs, "platform_rev",
254 sys_props.platform_rev);
262 static void kfd_topology_kobj_release(struct kobject *kobj)
267 static const struct sysfs_ops sysprops_ops = {
268 .show = sysprops_show,
271 static struct kobj_type sysprops_type = {
272 .release = kfd_topology_kobj_release,
273 .sysfs_ops = &sysprops_ops,
276 static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
280 struct kfd_iolink_properties *iolink;
282 /* Making sure that the buffer is an empty string */
285 iolink = container_of(attr, struct kfd_iolink_properties, attr);
286 if (iolink->gpu && kfd_devcgroup_check_permission(iolink->gpu))
288 sysfs_show_32bit_prop(buffer, offs, "type", iolink->iolink_type);
289 sysfs_show_32bit_prop(buffer, offs, "version_major", iolink->ver_maj);
290 sysfs_show_32bit_prop(buffer, offs, "version_minor", iolink->ver_min);
291 sysfs_show_32bit_prop(buffer, offs, "node_from", iolink->node_from);
292 sysfs_show_32bit_prop(buffer, offs, "node_to", iolink->node_to);
293 sysfs_show_32bit_prop(buffer, offs, "weight", iolink->weight);
294 sysfs_show_32bit_prop(buffer, offs, "min_latency", iolink->min_latency);
295 sysfs_show_32bit_prop(buffer, offs, "max_latency", iolink->max_latency);
296 sysfs_show_32bit_prop(buffer, offs, "min_bandwidth",
297 iolink->min_bandwidth);
298 sysfs_show_32bit_prop(buffer, offs, "max_bandwidth",
299 iolink->max_bandwidth);
300 sysfs_show_32bit_prop(buffer, offs, "recommended_transfer_size",
301 iolink->rec_transfer_size);
302 sysfs_show_32bit_prop(buffer, offs, "flags", iolink->flags);
307 static const struct sysfs_ops iolink_ops = {
311 static struct kobj_type iolink_type = {
312 .release = kfd_topology_kobj_release,
313 .sysfs_ops = &iolink_ops,
316 static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
320 struct kfd_mem_properties *mem;
322 /* Making sure that the buffer is an empty string */
325 mem = container_of(attr, struct kfd_mem_properties, attr);
326 if (mem->gpu && kfd_devcgroup_check_permission(mem->gpu))
328 sysfs_show_32bit_prop(buffer, offs, "heap_type", mem->heap_type);
329 sysfs_show_64bit_prop(buffer, offs, "size_in_bytes",
331 sysfs_show_32bit_prop(buffer, offs, "flags", mem->flags);
332 sysfs_show_32bit_prop(buffer, offs, "width", mem->width);
333 sysfs_show_32bit_prop(buffer, offs, "mem_clk_max",
339 static const struct sysfs_ops mem_ops = {
343 static struct kobj_type mem_type = {
344 .release = kfd_topology_kobj_release,
345 .sysfs_ops = &mem_ops,
348 static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
353 struct kfd_cache_properties *cache;
355 /* Making sure that the buffer is an empty string */
358 cache = container_of(attr, struct kfd_cache_properties, attr);
359 if (cache->gpu && kfd_devcgroup_check_permission(cache->gpu))
361 sysfs_show_32bit_prop(buffer, offs, "processor_id_low",
362 cache->processor_id_low);
363 sysfs_show_32bit_prop(buffer, offs, "level", cache->cache_level);
364 sysfs_show_32bit_prop(buffer, offs, "size", cache->cache_size);
365 sysfs_show_32bit_prop(buffer, offs, "cache_line_size",
366 cache->cacheline_size);
367 sysfs_show_32bit_prop(buffer, offs, "cache_lines_per_tag",
368 cache->cachelines_per_tag);
369 sysfs_show_32bit_prop(buffer, offs, "association", cache->cache_assoc);
370 sysfs_show_32bit_prop(buffer, offs, "latency", cache->cache_latency);
371 sysfs_show_32bit_prop(buffer, offs, "type", cache->cache_type);
372 offs += snprintf(buffer+offs, PAGE_SIZE-offs, "sibling_map ");
373 for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++)
374 for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++)
376 offs += snprintf(buffer+offs, PAGE_SIZE-offs, "%d,",
377 (cache->sibling_map[i] >> j) & 1);
379 /* Replace the last "," with end of line */
380 buffer[offs-1] = '\n';
384 static const struct sysfs_ops cache_ops = {
385 .show = kfd_cache_show,
388 static struct kobj_type cache_type = {
389 .release = kfd_topology_kobj_release,
390 .sysfs_ops = &cache_ops,
393 /****** Sysfs of Performance Counters ******/
395 struct kfd_perf_attr {
396 struct kobj_attribute attr;
400 static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
404 struct kfd_perf_attr *attr;
407 attr = container_of(attrs, struct kfd_perf_attr, attr);
408 if (!attr->data) /* invalid data for PMC */
411 return sysfs_show_32bit_val(buf, offs, attr->data);
414 #define KFD_PERF_DESC(_name, _data) \
416 .attr = __ATTR(_name, 0444, perf_show, NULL), \
420 static struct kfd_perf_attr perf_attr_iommu[] = {
421 KFD_PERF_DESC(max_concurrent, 0),
422 KFD_PERF_DESC(num_counters, 0),
423 KFD_PERF_DESC(counter_ids, 0),
425 /****************************************/
427 static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
431 struct kfd_topology_device *dev;
432 uint32_t log_max_watch_addr;
434 /* Making sure that the buffer is an empty string */
437 if (strcmp(attr->name, "gpu_id") == 0) {
438 dev = container_of(attr, struct kfd_topology_device,
440 if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
442 return sysfs_show_32bit_val(buffer, offs, dev->gpu_id);
445 if (strcmp(attr->name, "name") == 0) {
446 dev = container_of(attr, struct kfd_topology_device,
449 if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
451 return sysfs_show_str_val(buffer, offs, dev->node_props.name);
454 dev = container_of(attr, struct kfd_topology_device,
456 if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
458 sysfs_show_32bit_prop(buffer, offs, "cpu_cores_count",
459 dev->node_props.cpu_cores_count);
460 sysfs_show_32bit_prop(buffer, offs, "simd_count",
461 dev->gpu ? dev->node_props.simd_count : 0);
462 sysfs_show_32bit_prop(buffer, offs, "mem_banks_count",
463 dev->node_props.mem_banks_count);
464 sysfs_show_32bit_prop(buffer, offs, "caches_count",
465 dev->node_props.caches_count);
466 sysfs_show_32bit_prop(buffer, offs, "io_links_count",
467 dev->node_props.io_links_count);
468 sysfs_show_32bit_prop(buffer, offs, "cpu_core_id_base",
469 dev->node_props.cpu_core_id_base);
470 sysfs_show_32bit_prop(buffer, offs, "simd_id_base",
471 dev->node_props.simd_id_base);
472 sysfs_show_32bit_prop(buffer, offs, "max_waves_per_simd",
473 dev->node_props.max_waves_per_simd);
474 sysfs_show_32bit_prop(buffer, offs, "lds_size_in_kb",
475 dev->node_props.lds_size_in_kb);
476 sysfs_show_32bit_prop(buffer, offs, "gds_size_in_kb",
477 dev->node_props.gds_size_in_kb);
478 sysfs_show_32bit_prop(buffer, offs, "num_gws",
479 dev->node_props.num_gws);
480 sysfs_show_32bit_prop(buffer, offs, "wave_front_size",
481 dev->node_props.wave_front_size);
482 sysfs_show_32bit_prop(buffer, offs, "array_count",
483 dev->node_props.array_count);
484 sysfs_show_32bit_prop(buffer, offs, "simd_arrays_per_engine",
485 dev->node_props.simd_arrays_per_engine);
486 sysfs_show_32bit_prop(buffer, offs, "cu_per_simd_array",
487 dev->node_props.cu_per_simd_array);
488 sysfs_show_32bit_prop(buffer, offs, "simd_per_cu",
489 dev->node_props.simd_per_cu);
490 sysfs_show_32bit_prop(buffer, offs, "max_slots_scratch_cu",
491 dev->node_props.max_slots_scratch_cu);
492 sysfs_show_32bit_prop(buffer, offs, "gfx_target_version",
493 dev->node_props.gfx_target_version);
494 sysfs_show_32bit_prop(buffer, offs, "vendor_id",
495 dev->node_props.vendor_id);
496 sysfs_show_32bit_prop(buffer, offs, "device_id",
497 dev->node_props.device_id);
498 sysfs_show_32bit_prop(buffer, offs, "location_id",
499 dev->node_props.location_id);
500 sysfs_show_32bit_prop(buffer, offs, "domain",
501 dev->node_props.domain);
502 sysfs_show_32bit_prop(buffer, offs, "drm_render_minor",
503 dev->node_props.drm_render_minor);
504 sysfs_show_64bit_prop(buffer, offs, "hive_id",
505 dev->node_props.hive_id);
506 sysfs_show_32bit_prop(buffer, offs, "num_sdma_engines",
507 dev->node_props.num_sdma_engines);
508 sysfs_show_32bit_prop(buffer, offs, "num_sdma_xgmi_engines",
509 dev->node_props.num_sdma_xgmi_engines);
510 sysfs_show_32bit_prop(buffer, offs, "num_sdma_queues_per_engine",
511 dev->node_props.num_sdma_queues_per_engine);
512 sysfs_show_32bit_prop(buffer, offs, "num_cp_queues",
513 dev->node_props.num_cp_queues);
517 __ilog2_u32(dev->gpu->device_info.num_of_watch_points);
519 if (log_max_watch_addr) {
520 dev->node_props.capability |=
521 HSA_CAP_WATCH_POINTS_SUPPORTED;
523 dev->node_props.capability |=
524 ((log_max_watch_addr <<
525 HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
526 HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
529 if (dev->gpu->adev->asic_type == CHIP_TONGA)
530 dev->node_props.capability |=
531 HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
533 sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_fcompute",
534 dev->node_props.max_engine_clk_fcompute);
536 sysfs_show_64bit_prop(buffer, offs, "local_mem_size", 0ULL);
538 sysfs_show_32bit_prop(buffer, offs, "fw_version",
539 dev->gpu->mec_fw_version);
540 sysfs_show_32bit_prop(buffer, offs, "capability",
541 dev->node_props.capability);
542 sysfs_show_32bit_prop(buffer, offs, "sdma_fw_version",
543 dev->gpu->sdma_fw_version);
544 sysfs_show_64bit_prop(buffer, offs, "unique_id",
545 dev->gpu->adev->unique_id);
549 return sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_ccompute",
550 cpufreq_quick_get_max(0)/1000);
553 static const struct sysfs_ops node_ops = {
557 static struct kobj_type node_type = {
558 .release = kfd_topology_kobj_release,
559 .sysfs_ops = &node_ops,
562 static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
564 sysfs_remove_file(kobj, attr);
569 static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
571 struct kfd_iolink_properties *iolink;
572 struct kfd_cache_properties *cache;
573 struct kfd_mem_properties *mem;
574 struct kfd_perf_properties *perf;
576 if (dev->kobj_iolink) {
577 list_for_each_entry(iolink, &dev->io_link_props, list)
579 kfd_remove_sysfs_file(iolink->kobj,
583 kobject_del(dev->kobj_iolink);
584 kobject_put(dev->kobj_iolink);
585 dev->kobj_iolink = NULL;
588 if (dev->kobj_cache) {
589 list_for_each_entry(cache, &dev->cache_props, list)
591 kfd_remove_sysfs_file(cache->kobj,
595 kobject_del(dev->kobj_cache);
596 kobject_put(dev->kobj_cache);
597 dev->kobj_cache = NULL;
601 list_for_each_entry(mem, &dev->mem_props, list)
603 kfd_remove_sysfs_file(mem->kobj, &mem->attr);
606 kobject_del(dev->kobj_mem);
607 kobject_put(dev->kobj_mem);
608 dev->kobj_mem = NULL;
611 if (dev->kobj_perf) {
612 list_for_each_entry(perf, &dev->perf_props, list) {
613 kfree(perf->attr_group);
614 perf->attr_group = NULL;
616 kobject_del(dev->kobj_perf);
617 kobject_put(dev->kobj_perf);
618 dev->kobj_perf = NULL;
621 if (dev->kobj_node) {
622 sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
623 sysfs_remove_file(dev->kobj_node, &dev->attr_name);
624 sysfs_remove_file(dev->kobj_node, &dev->attr_props);
625 kobject_del(dev->kobj_node);
626 kobject_put(dev->kobj_node);
627 dev->kobj_node = NULL;
631 static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
634 struct kfd_iolink_properties *iolink;
635 struct kfd_cache_properties *cache;
636 struct kfd_mem_properties *mem;
637 struct kfd_perf_properties *perf;
639 uint32_t i, num_attrs;
640 struct attribute **attrs;
642 if (WARN_ON(dev->kobj_node))
646 * Creating the sysfs folders
648 dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
652 ret = kobject_init_and_add(dev->kobj_node, &node_type,
653 sys_props.kobj_nodes, "%d", id);
655 kobject_put(dev->kobj_node);
659 dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
663 dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
664 if (!dev->kobj_cache)
667 dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
668 if (!dev->kobj_iolink)
671 dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
676 * Creating sysfs files for node properties
678 dev->attr_gpuid.name = "gpu_id";
679 dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
680 sysfs_attr_init(&dev->attr_gpuid);
681 dev->attr_name.name = "name";
682 dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
683 sysfs_attr_init(&dev->attr_name);
684 dev->attr_props.name = "properties";
685 dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
686 sysfs_attr_init(&dev->attr_props);
687 ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
690 ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
693 ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
698 list_for_each_entry(mem, &dev->mem_props, list) {
699 mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
702 ret = kobject_init_and_add(mem->kobj, &mem_type,
703 dev->kobj_mem, "%d", i);
705 kobject_put(mem->kobj);
709 mem->attr.name = "properties";
710 mem->attr.mode = KFD_SYSFS_FILE_MODE;
711 sysfs_attr_init(&mem->attr);
712 ret = sysfs_create_file(mem->kobj, &mem->attr);
719 list_for_each_entry(cache, &dev->cache_props, list) {
720 cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
723 ret = kobject_init_and_add(cache->kobj, &cache_type,
724 dev->kobj_cache, "%d", i);
726 kobject_put(cache->kobj);
730 cache->attr.name = "properties";
731 cache->attr.mode = KFD_SYSFS_FILE_MODE;
732 sysfs_attr_init(&cache->attr);
733 ret = sysfs_create_file(cache->kobj, &cache->attr);
740 list_for_each_entry(iolink, &dev->io_link_props, list) {
741 iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
744 ret = kobject_init_and_add(iolink->kobj, &iolink_type,
745 dev->kobj_iolink, "%d", i);
747 kobject_put(iolink->kobj);
751 iolink->attr.name = "properties";
752 iolink->attr.mode = KFD_SYSFS_FILE_MODE;
753 sysfs_attr_init(&iolink->attr);
754 ret = sysfs_create_file(iolink->kobj, &iolink->attr);
760 /* All hardware blocks have the same number of attributes. */
761 num_attrs = ARRAY_SIZE(perf_attr_iommu);
762 list_for_each_entry(perf, &dev->perf_props, list) {
763 perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
764 * num_attrs + sizeof(struct attribute_group),
766 if (!perf->attr_group)
769 attrs = (struct attribute **)(perf->attr_group + 1);
770 if (!strcmp(perf->block_name, "iommu")) {
771 /* Information of IOMMU's num_counters and counter_ids is shown
772 * under /sys/bus/event_source/devices/amd_iommu. We don't
775 perf_attr_iommu[0].data = perf->max_concurrent;
776 for (i = 0; i < num_attrs; i++)
777 attrs[i] = &perf_attr_iommu[i].attr.attr;
779 perf->attr_group->name = perf->block_name;
780 perf->attr_group->attrs = attrs;
781 ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
789 /* Called with write topology lock acquired */
790 static int kfd_build_sysfs_node_tree(void)
792 struct kfd_topology_device *dev;
796 list_for_each_entry(dev, &topology_device_list, list) {
797 ret = kfd_build_sysfs_node_entry(dev, i);
806 /* Called with write topology lock acquired */
807 static void kfd_remove_sysfs_node_tree(void)
809 struct kfd_topology_device *dev;
811 list_for_each_entry(dev, &topology_device_list, list)
812 kfd_remove_sysfs_node_entry(dev);
815 static int kfd_topology_update_sysfs(void)
819 if (!sys_props.kobj_topology) {
820 sys_props.kobj_topology =
821 kfd_alloc_struct(sys_props.kobj_topology);
822 if (!sys_props.kobj_topology)
825 ret = kobject_init_and_add(sys_props.kobj_topology,
826 &sysprops_type, &kfd_device->kobj,
829 kobject_put(sys_props.kobj_topology);
833 sys_props.kobj_nodes = kobject_create_and_add("nodes",
834 sys_props.kobj_topology);
835 if (!sys_props.kobj_nodes)
838 sys_props.attr_genid.name = "generation_id";
839 sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
840 sysfs_attr_init(&sys_props.attr_genid);
841 ret = sysfs_create_file(sys_props.kobj_topology,
842 &sys_props.attr_genid);
846 sys_props.attr_props.name = "system_properties";
847 sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
848 sysfs_attr_init(&sys_props.attr_props);
849 ret = sysfs_create_file(sys_props.kobj_topology,
850 &sys_props.attr_props);
855 kfd_remove_sysfs_node_tree();
857 return kfd_build_sysfs_node_tree();
860 static void kfd_topology_release_sysfs(void)
862 kfd_remove_sysfs_node_tree();
863 if (sys_props.kobj_topology) {
864 sysfs_remove_file(sys_props.kobj_topology,
865 &sys_props.attr_genid);
866 sysfs_remove_file(sys_props.kobj_topology,
867 &sys_props.attr_props);
868 if (sys_props.kobj_nodes) {
869 kobject_del(sys_props.kobj_nodes);
870 kobject_put(sys_props.kobj_nodes);
871 sys_props.kobj_nodes = NULL;
873 kobject_del(sys_props.kobj_topology);
874 kobject_put(sys_props.kobj_topology);
875 sys_props.kobj_topology = NULL;
879 /* Called with write topology_lock acquired */
880 static void kfd_topology_update_device_list(struct list_head *temp_list,
881 struct list_head *master_list)
883 while (!list_empty(temp_list)) {
884 list_move_tail(temp_list->next, master_list);
885 sys_props.num_devices++;
889 static void kfd_debug_print_topology(void)
891 struct kfd_topology_device *dev;
893 down_read(&topology_lock);
895 dev = list_last_entry(&topology_device_list,
896 struct kfd_topology_device, list);
898 if (dev->node_props.cpu_cores_count &&
899 dev->node_props.simd_count) {
900 pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
901 dev->node_props.device_id,
902 dev->node_props.vendor_id);
903 } else if (dev->node_props.cpu_cores_count)
904 pr_info("Topology: Add CPU node\n");
905 else if (dev->node_props.simd_count)
906 pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
907 dev->node_props.device_id,
908 dev->node_props.vendor_id);
910 up_read(&topology_lock);
913 /* Helper function for intializing platform_xx members of
914 * kfd_system_properties. Uses OEM info from the last CPU/APU node.
916 static void kfd_update_system_properties(void)
918 struct kfd_topology_device *dev;
920 down_read(&topology_lock);
921 dev = list_last_entry(&topology_device_list,
922 struct kfd_topology_device, list);
924 sys_props.platform_id =
925 (*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
926 sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
927 sys_props.platform_rev = dev->oem_revision;
929 up_read(&topology_lock);
932 static void find_system_memory(const struct dmi_header *dm,
935 struct kfd_mem_properties *mem;
936 u16 mem_width, mem_clock;
937 struct kfd_topology_device *kdev =
938 (struct kfd_topology_device *)private;
939 const u8 *dmi_data = (const u8 *)(dm + 1);
941 if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
942 mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
943 mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
944 list_for_each_entry(mem, &kdev->mem_props, list) {
945 if (mem_width != 0xFFFF && mem_width != 0)
946 mem->width = mem_width;
948 mem->mem_clk_max = mem_clock;
954 * Performance counters information is not part of CRAT but we would like to
955 * put them in the sysfs under topology directory for Thunk to get the data.
956 * This function is called before updating the sysfs.
958 static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
960 /* These are the only counters supported so far */
961 return kfd_iommu_add_perf_counters(kdev);
964 /* kfd_add_non_crat_information - Add information that is not currently
965 * defined in CRAT but is necessary for KFD topology
966 * @dev - topology device to which addition info is added
968 static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
970 /* Check if CPU only node. */
972 /* Add system memory information */
973 dmi_walk(find_system_memory, kdev);
975 /* TODO: For GPU node, rearrange code from kfd_topology_add_device */
978 /* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
979 * Ignore CRAT for all other devices. AMD APU is identified if both CPU
980 * and GPU cores are present.
981 * @device_list - topology device list created by parsing ACPI CRAT table.
982 * @return - TRUE if invalid, FALSE is valid.
984 static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
986 struct kfd_topology_device *dev;
988 list_for_each_entry(dev, device_list, list) {
989 if (dev->node_props.cpu_cores_count &&
990 dev->node_props.simd_count)
993 pr_info("Ignoring ACPI CRAT on non-APU system\n");
997 int kfd_topology_init(void)
999 void *crat_image = NULL;
1000 size_t image_size = 0;
1002 struct list_head temp_topology_device_list;
1003 int cpu_only_node = 0;
1004 struct kfd_topology_device *kdev;
1005 int proximity_domain;
1007 /* topology_device_list - Master list of all topology devices
1008 * temp_topology_device_list - temporary list created while parsing CRAT
1009 * or VCRAT. Once parsing is complete the contents of list is moved to
1010 * topology_device_list
1013 /* Initialize the head for the both the lists */
1014 INIT_LIST_HEAD(&topology_device_list);
1015 INIT_LIST_HEAD(&temp_topology_device_list);
1016 init_rwsem(&topology_lock);
1018 memset(&sys_props, 0, sizeof(sys_props));
1020 /* Proximity domains in ACPI CRAT tables start counting at
1021 * 0. The same should be true for virtual CRAT tables created
1022 * at this stage. GPUs added later in kfd_topology_add_device
1025 proximity_domain = 0;
1028 * Get the CRAT image from the ACPI. If ACPI doesn't have one
1029 * or if ACPI CRAT is invalid create a virtual CRAT.
1030 * NOTE: The current implementation expects all AMD APUs to have
1031 * CRAT. If no CRAT is available, it is assumed to be a CPU
1033 ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
1035 ret = kfd_parse_crat_table(crat_image,
1036 &temp_topology_device_list,
1039 kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
1040 kfd_release_topology_device_list(
1041 &temp_topology_device_list);
1042 kfd_destroy_crat_image(crat_image);
1048 ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
1049 COMPUTE_UNIT_CPU, NULL,
1053 pr_err("Error creating VCRAT table for CPU\n");
1057 ret = kfd_parse_crat_table(crat_image,
1058 &temp_topology_device_list,
1061 pr_err("Error parsing VCRAT table for CPU\n");
1066 kdev = list_first_entry(&temp_topology_device_list,
1067 struct kfd_topology_device, list);
1068 kfd_add_perf_to_topology(kdev);
1070 down_write(&topology_lock);
1071 kfd_topology_update_device_list(&temp_topology_device_list,
1072 &topology_device_list);
1073 topology_crat_proximity_domain = sys_props.num_devices-1;
1074 ret = kfd_topology_update_sysfs();
1075 up_write(&topology_lock);
1078 sys_props.generation_count++;
1079 kfd_update_system_properties();
1080 kfd_debug_print_topology();
1082 pr_err("Failed to update topology in sysfs ret=%d\n", ret);
1084 /* For nodes with GPU, this information gets added
1085 * when GPU is detected (kfd_topology_add_device).
1087 if (cpu_only_node) {
1088 /* Add additional information to CPU only node created above */
1089 down_write(&topology_lock);
1090 kdev = list_first_entry(&topology_device_list,
1091 struct kfd_topology_device, list);
1092 up_write(&topology_lock);
1093 kfd_add_non_crat_information(kdev);
1097 kfd_destroy_crat_image(crat_image);
1101 void kfd_topology_shutdown(void)
1103 down_write(&topology_lock);
1104 kfd_topology_release_sysfs();
1105 kfd_release_live_view();
1106 up_write(&topology_lock);
1109 static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
1113 uint64_t local_mem_size;
1115 struct kfd_local_mem_info local_mem_info;
1120 amdgpu_amdkfd_get_local_mem_info(gpu->adev, &local_mem_info);
1122 local_mem_size = local_mem_info.local_mem_size_private +
1123 local_mem_info.local_mem_size_public;
1125 buf[0] = gpu->pdev->devfn;
1126 buf[1] = gpu->pdev->subsystem_vendor |
1127 (gpu->pdev->subsystem_device << 16);
1128 buf[2] = pci_domain_nr(gpu->pdev->bus);
1129 buf[3] = gpu->pdev->device;
1130 buf[4] = gpu->pdev->bus->number;
1131 buf[5] = lower_32_bits(local_mem_size);
1132 buf[6] = upper_32_bits(local_mem_size);
1134 for (i = 0, hashout = 0; i < 7; i++)
1135 hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
1139 /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
1140 * the GPU device is not already present in the topology device
1141 * list then return NULL. This means a new topology device has to
1142 * be created for this GPU.
1144 static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
1146 struct kfd_topology_device *dev;
1147 struct kfd_topology_device *out_dev = NULL;
1148 struct kfd_mem_properties *mem;
1149 struct kfd_cache_properties *cache;
1150 struct kfd_iolink_properties *iolink;
1152 down_write(&topology_lock);
1153 list_for_each_entry(dev, &topology_device_list, list) {
1154 /* Discrete GPUs need their own topology device list
1155 * entries. Don't assign them to CPU/APU nodes.
1157 if (!gpu->use_iommu_v2 &&
1158 dev->node_props.cpu_cores_count)
1161 if (!dev->gpu && (dev->node_props.simd_count > 0)) {
1165 list_for_each_entry(mem, &dev->mem_props, list)
1166 mem->gpu = dev->gpu;
1167 list_for_each_entry(cache, &dev->cache_props, list)
1168 cache->gpu = dev->gpu;
1169 list_for_each_entry(iolink, &dev->io_link_props, list)
1170 iolink->gpu = dev->gpu;
1174 up_write(&topology_lock);
1178 static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
1181 * TODO: Generate an event for thunk about the arrival/removal
1186 /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
1187 * patch this after CRAT parsing.
1189 static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
1191 struct kfd_mem_properties *mem;
1192 struct kfd_local_mem_info local_mem_info;
1197 /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
1198 * single bank of VRAM local memory.
1199 * for dGPUs - VCRAT reports only one bank of Local Memory
1200 * for APUs - If CRAT from ACPI reports more than one bank, then
1201 * all the banks will report the same mem_clk_max information
1203 amdgpu_amdkfd_get_local_mem_info(dev->gpu->adev, &local_mem_info);
1205 list_for_each_entry(mem, &dev->mem_props, list)
1206 mem->mem_clk_max = local_mem_info.mem_clk_max;
1209 static void kfd_set_iolink_no_atomics(struct kfd_topology_device *dev,
1210 struct kfd_topology_device *target_gpu_dev,
1211 struct kfd_iolink_properties *link)
1213 /* xgmi always supports atomics between links. */
1214 if (link->iolink_type == CRAT_IOLINK_TYPE_XGMI)
1217 /* check pcie support to set cpu(dev) flags for target_gpu_dev link. */
1218 if (target_gpu_dev) {
1221 pcie_capability_read_dword(target_gpu_dev->gpu->pdev,
1222 PCI_EXP_DEVCAP2, &cap);
1224 if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1225 PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1226 link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1227 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1228 /* set gpu (dev) flags. */
1230 if (!dev->gpu->pci_atomic_requested ||
1231 dev->gpu->adev->asic_type == CHIP_HAWAII)
1232 link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1233 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1237 static void kfd_set_iolink_non_coherent(struct kfd_topology_device *to_dev,
1238 struct kfd_iolink_properties *outbound_link,
1239 struct kfd_iolink_properties *inbound_link)
1241 /* CPU -> GPU with PCIe */
1243 inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
1244 inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1247 /* GPU <-> GPU with PCIe and
1250 if (inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS ||
1251 (inbound_link->iolink_type == CRAT_IOLINK_TYPE_XGMI &&
1252 KFD_GC_VERSION(to_dev->gpu) == IP_VERSION(9, 4, 0))) {
1253 outbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1254 inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1259 static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
1261 struct kfd_iolink_properties *link, *inbound_link;
1262 struct kfd_topology_device *peer_dev;
1264 if (!dev || !dev->gpu)
1267 /* GPU only creates direct links so apply flags setting to all */
1268 list_for_each_entry(link, &dev->io_link_props, list) {
1269 link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1270 kfd_set_iolink_no_atomics(dev, NULL, link);
1271 peer_dev = kfd_topology_device_by_proximity_domain(
1277 list_for_each_entry(inbound_link, &peer_dev->io_link_props,
1279 if (inbound_link->node_to != link->node_from)
1282 inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1283 kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
1284 kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
1289 int kfd_topology_add_device(struct kfd_dev *gpu)
1292 struct kfd_topology_device *dev;
1293 struct kfd_cu_info cu_info;
1295 struct list_head temp_topology_device_list;
1296 void *crat_image = NULL;
1297 size_t image_size = 0;
1298 int proximity_domain;
1300 const char *asic_name = amdgpu_asic_name[gpu->adev->asic_type];
1302 INIT_LIST_HEAD(&temp_topology_device_list);
1304 gpu_id = kfd_generate_gpu_id(gpu);
1306 pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1308 /* Include the CPU in xGMI hive if xGMI connected by assigning it the hive ID. */
1309 if (gpu->hive_id && gpu->adev->gmc.xgmi.connected_to_cpu) {
1310 struct kfd_topology_device *top_dev;
1312 down_read(&topology_lock);
1314 list_for_each_entry(top_dev, &topology_device_list, list) {
1318 top_dev->node_props.hive_id = gpu->hive_id;
1321 up_read(&topology_lock);
1324 /* Check to see if this gpu device exists in the topology_device_list.
1325 * If so, assign the gpu to that device,
1326 * else create a Virtual CRAT for this gpu device and then parse that
1327 * CRAT to create a new topology device. Once created assign the gpu to
1328 * that topology device
1330 dev = kfd_assign_gpu(gpu);
1332 down_write(&topology_lock);
1333 proximity_domain = ++topology_crat_proximity_domain;
1335 res = kfd_create_crat_image_virtual(&crat_image, &image_size,
1336 COMPUTE_UNIT_GPU, gpu,
1339 pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
1341 topology_crat_proximity_domain--;
1344 res = kfd_parse_crat_table(crat_image,
1345 &temp_topology_device_list,
1348 pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
1350 topology_crat_proximity_domain--;
1354 kfd_topology_update_device_list(&temp_topology_device_list,
1355 &topology_device_list);
1357 /* Update the SYSFS tree, since we added another topology
1360 res = kfd_topology_update_sysfs();
1361 up_write(&topology_lock);
1364 sys_props.generation_count++;
1366 pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
1368 dev = kfd_assign_gpu(gpu);
1369 if (WARN_ON(!dev)) {
1375 dev->gpu_id = gpu_id;
1378 /* TODO: Move the following lines to function
1379 * kfd_add_non_crat_information
1382 /* Fill-in additional information that is not available in CRAT but
1383 * needed for the topology
1386 amdgpu_amdkfd_get_cu_info(dev->gpu->adev, &cu_info);
1388 for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1; i++) {
1389 dev->node_props.name[i] = __tolower(asic_name[i]);
1390 if (asic_name[i] == '\0')
1393 dev->node_props.name[i] = '\0';
1395 dev->node_props.simd_arrays_per_engine =
1396 cu_info.num_shader_arrays_per_engine;
1398 dev->node_props.gfx_target_version = gpu->device_info.gfx_target_version;
1399 dev->node_props.vendor_id = gpu->pdev->vendor;
1400 dev->node_props.device_id = gpu->pdev->device;
1401 dev->node_props.capability |=
1402 ((dev->gpu->adev->rev_id << HSA_CAP_ASIC_REVISION_SHIFT) &
1403 HSA_CAP_ASIC_REVISION_MASK);
1404 dev->node_props.location_id = pci_dev_id(gpu->pdev);
1405 dev->node_props.domain = pci_domain_nr(gpu->pdev->bus);
1406 dev->node_props.max_engine_clk_fcompute =
1407 amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->adev);
1408 dev->node_props.max_engine_clk_ccompute =
1409 cpufreq_quick_get_max(0) / 1000;
1410 dev->node_props.drm_render_minor =
1411 gpu->shared_resources.drm_render_minor;
1413 dev->node_props.hive_id = gpu->hive_id;
1414 dev->node_props.num_sdma_engines = kfd_get_num_sdma_engines(gpu);
1415 dev->node_props.num_sdma_xgmi_engines =
1416 kfd_get_num_xgmi_sdma_engines(gpu);
1417 dev->node_props.num_sdma_queues_per_engine =
1418 gpu->device_info.num_sdma_queues_per_engine;
1419 dev->node_props.num_gws = (dev->gpu->gws &&
1420 dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
1421 dev->gpu->adev->gds.gws_size : 0;
1422 dev->node_props.num_cp_queues = get_cp_queues_num(dev->gpu->dqm);
1424 kfd_fill_mem_clk_max_info(dev);
1425 kfd_fill_iolink_non_crat_info(dev);
1427 switch (dev->gpu->adev->asic_type) {
1431 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
1432 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1433 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1437 case CHIP_POLARIS10:
1438 case CHIP_POLARIS11:
1439 case CHIP_POLARIS12:
1441 pr_debug("Adding doorbell packet type capability\n");
1442 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
1443 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1444 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1447 if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(9, 0, 1))
1448 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1449 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1450 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1452 WARN(1, "Unexpected ASIC family %u",
1453 dev->gpu->adev->asic_type);
1457 * Overwrite ATS capability according to needs_iommu_device to fix
1458 * potential missing corresponding bit in CRAT of BIOS.
1460 if (dev->gpu->use_iommu_v2)
1461 dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
1463 dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;
1465 /* Fix errors in CZ CRAT.
1466 * simd_count: Carrizo CRAT reports wrong simd_count, probably
1467 * because it doesn't consider masked out CUs
1468 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
1470 if (dev->gpu->adev->asic_type == CHIP_CARRIZO) {
1471 dev->node_props.simd_count =
1472 cu_info.simd_per_cu * cu_info.cu_active_number;
1473 dev->node_props.max_waves_per_simd = 10;
1476 /* kfd only concerns sram ecc on GFX and HBM ecc on UMC */
1477 dev->node_props.capability |=
1478 ((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0) ?
1479 HSA_CAP_SRAM_EDCSUPPORTED : 0;
1480 dev->node_props.capability |=
1481 ((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
1482 HSA_CAP_MEM_EDCSUPPORTED : 0;
1484 if (KFD_GC_VERSION(dev->gpu) != IP_VERSION(9, 0, 1))
1485 dev->node_props.capability |= (dev->gpu->adev->ras_enabled != 0) ?
1486 HSA_CAP_RASEVENTNOTIFY : 0;
1488 if (KFD_IS_SVM_API_SUPPORTED(dev->gpu->adev->kfd.dev))
1489 dev->node_props.capability |= HSA_CAP_SVMAPI_SUPPORTED;
1491 kfd_debug_print_topology();
1494 kfd_notify_gpu_change(gpu_id, 1);
1496 kfd_destroy_crat_image(crat_image);
1501 * kfd_topology_update_io_links() - Update IO links after device removal.
1502 * @proximity_domain: Proximity domain value of the dev being removed.
1504 * The topology list currently is arranged in increasing order of
1507 * Two things need to be done when a device is removed:
1508 * 1. All the IO links to this device need to be removed.
1509 * 2. All nodes after the current device node need to move
1510 * up once this device node is removed from the topology
1511 * list. As a result, the proximity domain values for
1512 * all nodes after the node being deleted reduce by 1.
1513 * This would also cause the proximity domain values for
1514 * io links to be updated based on new proximity domain
1517 * Context: The caller must hold write topology_lock.
1519 static void kfd_topology_update_io_links(int proximity_domain)
1521 struct kfd_topology_device *dev;
1522 struct kfd_iolink_properties *iolink, *tmp;
1524 list_for_each_entry(dev, &topology_device_list, list) {
1525 if (dev->proximity_domain > proximity_domain)
1526 dev->proximity_domain--;
1528 list_for_each_entry_safe(iolink, tmp, &dev->io_link_props, list) {
1530 * If there is an io link to the dev being deleted
1531 * then remove that IO link also.
1533 if (iolink->node_to == proximity_domain) {
1534 list_del(&iolink->list);
1535 dev->io_link_count--;
1536 dev->node_props.io_links_count--;
1537 } else if (iolink->node_from > proximity_domain) {
1538 iolink->node_from--;
1539 } else if (iolink->node_to > proximity_domain) {
1547 int kfd_topology_remove_device(struct kfd_dev *gpu)
1549 struct kfd_topology_device *dev, *tmp;
1554 down_write(&topology_lock);
1556 list_for_each_entry_safe(dev, tmp, &topology_device_list, list) {
1557 if (dev->gpu == gpu) {
1558 gpu_id = dev->gpu_id;
1559 kfd_remove_sysfs_node_entry(dev);
1560 kfd_release_topology_device(dev);
1561 sys_props.num_devices--;
1562 kfd_topology_update_io_links(i);
1563 topology_crat_proximity_domain = sys_props.num_devices-1;
1564 sys_props.generation_count++;
1566 if (kfd_topology_update_sysfs() < 0)
1567 kfd_topology_release_sysfs();
1573 up_write(&topology_lock);
1576 kfd_notify_gpu_change(gpu_id, 0);
1581 /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
1582 * topology. If GPU device is found @idx, then valid kfd_dev pointer is
1583 * returned through @kdev
1584 * Return - 0: On success (@kdev will be NULL for non GPU nodes)
1585 * -1: If end of list
1587 int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
1590 struct kfd_topology_device *top_dev;
1591 uint8_t device_idx = 0;
1594 down_read(&topology_lock);
1596 list_for_each_entry(top_dev, &topology_device_list, list) {
1597 if (device_idx == idx) {
1598 *kdev = top_dev->gpu;
1599 up_read(&topology_lock);
1606 up_read(&topology_lock);
1612 static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
1614 int first_cpu_of_numa_node;
1616 if (!cpumask || cpumask == cpu_none_mask)
1618 first_cpu_of_numa_node = cpumask_first(cpumask);
1619 if (first_cpu_of_numa_node >= nr_cpu_ids)
1621 #ifdef CONFIG_X86_64
1622 return cpu_data(first_cpu_of_numa_node).apicid;
1624 return first_cpu_of_numa_node;
1628 /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
1629 * of the given NUMA node (numa_node_id)
1630 * Return -1 on failure
1632 int kfd_numa_node_to_apic_id(int numa_node_id)
1634 if (numa_node_id == -1) {
1635 pr_warn("Invalid NUMA Node. Use online CPU mask\n");
1636 return kfd_cpumask_to_apic_id(cpu_online_mask);
1638 return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
1641 void kfd_double_confirm_iommu_support(struct kfd_dev *gpu)
1643 struct kfd_topology_device *dev;
1645 gpu->use_iommu_v2 = false;
1647 if (!gpu->device_info.needs_iommu_device)
1650 down_read(&topology_lock);
1652 /* Only use IOMMUv2 if there is an APU topology node with no GPU
1653 * assigned yet. This GPU will be assigned to it.
1655 list_for_each_entry(dev, &topology_device_list, list)
1656 if (dev->node_props.cpu_cores_count &&
1657 dev->node_props.simd_count &&
1659 gpu->use_iommu_v2 = true;
1661 up_read(&topology_lock);
1664 #if defined(CONFIG_DEBUG_FS)
1666 int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
1668 struct kfd_topology_device *dev;
1672 down_read(&topology_lock);
1674 list_for_each_entry(dev, &topology_device_list, list) {
1680 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1681 r = dqm_debugfs_hqds(m, dev->gpu->dqm);
1686 up_read(&topology_lock);
1691 int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
1693 struct kfd_topology_device *dev;
1697 down_read(&topology_lock);
1699 list_for_each_entry(dev, &topology_device_list, list) {
1705 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1706 r = pm_debugfs_runlist(m, &dev->gpu->dqm->packet_mgr);
1711 up_read(&topology_lock);