2 * Copyright 2014 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/pci.h>
26 #include <linux/errno.h>
27 #include <linux/acpi.h>
28 #include <linux/hash.h>
29 #include <linux/cpufreq.h>
30 #include <linux/log2.h>
31 #include <linux/dmi.h>
32 #include <linux/atomic.h>
36 #include "kfd_topology.h"
37 #include "kfd_device_queue_manager.h"
38 #include "kfd_iommu.h"
39 #include "amdgpu_amdkfd.h"
40 #include "amdgpu_ras.h"
42 /* topology_device_list - Master list of all topology devices */
43 static struct list_head topology_device_list;
44 static struct kfd_system_properties sys_props;
46 static DECLARE_RWSEM(topology_lock);
47 static atomic_t topology_crat_proximity_domain;
49 struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
50 uint32_t proximity_domain)
52 struct kfd_topology_device *top_dev;
53 struct kfd_topology_device *device = NULL;
55 down_read(&topology_lock);
57 list_for_each_entry(top_dev, &topology_device_list, list)
58 if (top_dev->proximity_domain == proximity_domain) {
63 up_read(&topology_lock);
68 struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
70 struct kfd_topology_device *top_dev = NULL;
71 struct kfd_topology_device *ret = NULL;
73 down_read(&topology_lock);
75 list_for_each_entry(top_dev, &topology_device_list, list)
76 if (top_dev->gpu_id == gpu_id) {
81 up_read(&topology_lock);
86 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
88 struct kfd_topology_device *top_dev;
90 top_dev = kfd_topology_device_by_id(gpu_id);
97 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
99 struct kfd_topology_device *top_dev;
100 struct kfd_dev *device = NULL;
102 down_read(&topology_lock);
104 list_for_each_entry(top_dev, &topology_device_list, list)
105 if (top_dev->gpu && top_dev->gpu->pdev == pdev) {
106 device = top_dev->gpu;
110 up_read(&topology_lock);
115 struct kfd_dev *kfd_device_by_kgd(const struct kgd_dev *kgd)
117 struct kfd_topology_device *top_dev;
118 struct kfd_dev *device = NULL;
120 down_read(&topology_lock);
122 list_for_each_entry(top_dev, &topology_device_list, list)
123 if (top_dev->gpu && top_dev->gpu->kgd == kgd) {
124 device = top_dev->gpu;
128 up_read(&topology_lock);
133 /* Called with write topology_lock acquired */
134 static void kfd_release_topology_device(struct kfd_topology_device *dev)
136 struct kfd_mem_properties *mem;
137 struct kfd_cache_properties *cache;
138 struct kfd_iolink_properties *iolink;
139 struct kfd_perf_properties *perf;
141 list_del(&dev->list);
143 while (dev->mem_props.next != &dev->mem_props) {
144 mem = container_of(dev->mem_props.next,
145 struct kfd_mem_properties, list);
146 list_del(&mem->list);
150 while (dev->cache_props.next != &dev->cache_props) {
151 cache = container_of(dev->cache_props.next,
152 struct kfd_cache_properties, list);
153 list_del(&cache->list);
157 while (dev->io_link_props.next != &dev->io_link_props) {
158 iolink = container_of(dev->io_link_props.next,
159 struct kfd_iolink_properties, list);
160 list_del(&iolink->list);
164 while (dev->perf_props.next != &dev->perf_props) {
165 perf = container_of(dev->perf_props.next,
166 struct kfd_perf_properties, list);
167 list_del(&perf->list);
174 void kfd_release_topology_device_list(struct list_head *device_list)
176 struct kfd_topology_device *dev;
178 while (!list_empty(device_list)) {
179 dev = list_first_entry(device_list,
180 struct kfd_topology_device, list);
181 kfd_release_topology_device(dev);
185 static void kfd_release_live_view(void)
187 kfd_release_topology_device_list(&topology_device_list);
188 memset(&sys_props, 0, sizeof(sys_props));
191 struct kfd_topology_device *kfd_create_topology_device(
192 struct list_head *device_list)
194 struct kfd_topology_device *dev;
196 dev = kfd_alloc_struct(dev);
198 pr_err("No memory to allocate a topology device");
202 INIT_LIST_HEAD(&dev->mem_props);
203 INIT_LIST_HEAD(&dev->cache_props);
204 INIT_LIST_HEAD(&dev->io_link_props);
205 INIT_LIST_HEAD(&dev->perf_props);
207 list_add_tail(&dev->list, device_list);
213 #define sysfs_show_gen_prop(buffer, fmt, ...) \
214 snprintf(buffer, PAGE_SIZE, "%s"fmt, buffer, __VA_ARGS__)
215 #define sysfs_show_32bit_prop(buffer, name, value) \
216 sysfs_show_gen_prop(buffer, "%s %u\n", name, value)
217 #define sysfs_show_64bit_prop(buffer, name, value) \
218 sysfs_show_gen_prop(buffer, "%s %llu\n", name, value)
219 #define sysfs_show_32bit_val(buffer, value) \
220 sysfs_show_gen_prop(buffer, "%u\n", value)
221 #define sysfs_show_str_val(buffer, value) \
222 sysfs_show_gen_prop(buffer, "%s\n", value)
224 static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
229 /* Making sure that the buffer is an empty string */
232 if (attr == &sys_props.attr_genid) {
233 ret = sysfs_show_32bit_val(buffer, sys_props.generation_count);
234 } else if (attr == &sys_props.attr_props) {
235 sysfs_show_64bit_prop(buffer, "platform_oem",
236 sys_props.platform_oem);
237 sysfs_show_64bit_prop(buffer, "platform_id",
238 sys_props.platform_id);
239 ret = sysfs_show_64bit_prop(buffer, "platform_rev",
240 sys_props.platform_rev);
248 static void kfd_topology_kobj_release(struct kobject *kobj)
253 static const struct sysfs_ops sysprops_ops = {
254 .show = sysprops_show,
257 static struct kobj_type sysprops_type = {
258 .release = kfd_topology_kobj_release,
259 .sysfs_ops = &sysprops_ops,
262 static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
266 struct kfd_iolink_properties *iolink;
268 /* Making sure that the buffer is an empty string */
271 iolink = container_of(attr, struct kfd_iolink_properties, attr);
272 sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
273 sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
274 sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
275 sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
276 sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
277 sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
278 sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
279 sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
280 sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
281 sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
282 sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
283 iolink->rec_transfer_size);
284 ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
289 static const struct sysfs_ops iolink_ops = {
293 static struct kobj_type iolink_type = {
294 .release = kfd_topology_kobj_release,
295 .sysfs_ops = &iolink_ops,
298 static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
302 struct kfd_mem_properties *mem;
304 /* Making sure that the buffer is an empty string */
307 mem = container_of(attr, struct kfd_mem_properties, attr);
308 sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
309 sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
310 sysfs_show_32bit_prop(buffer, "flags", mem->flags);
311 sysfs_show_32bit_prop(buffer, "width", mem->width);
312 ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
317 static const struct sysfs_ops mem_ops = {
321 static struct kobj_type mem_type = {
322 .release = kfd_topology_kobj_release,
323 .sysfs_ops = &mem_ops,
326 static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
331 struct kfd_cache_properties *cache;
333 /* Making sure that the buffer is an empty string */
336 cache = container_of(attr, struct kfd_cache_properties, attr);
337 sysfs_show_32bit_prop(buffer, "processor_id_low",
338 cache->processor_id_low);
339 sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
340 sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
341 sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
342 sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
343 cache->cachelines_per_tag);
344 sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
345 sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
346 sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
347 snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
348 for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++)
349 for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++) {
351 if (cache->sibling_map[i] & (1 << j))
352 ret = snprintf(buffer, PAGE_SIZE,
353 "%s%d%s", buffer, 1, ",");
355 ret = snprintf(buffer, PAGE_SIZE,
356 "%s%d%s", buffer, 0, ",");
358 /* Replace the last "," with end of line */
359 *(buffer + strlen(buffer) - 1) = 0xA;
363 static const struct sysfs_ops cache_ops = {
364 .show = kfd_cache_show,
367 static struct kobj_type cache_type = {
368 .release = kfd_topology_kobj_release,
369 .sysfs_ops = &cache_ops,
372 /****** Sysfs of Performance Counters ******/
374 struct kfd_perf_attr {
375 struct kobj_attribute attr;
379 static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
382 struct kfd_perf_attr *attr;
385 attr = container_of(attrs, struct kfd_perf_attr, attr);
386 if (!attr->data) /* invalid data for PMC */
389 return sysfs_show_32bit_val(buf, attr->data);
392 #define KFD_PERF_DESC(_name, _data) \
394 .attr = __ATTR(_name, 0444, perf_show, NULL), \
398 static struct kfd_perf_attr perf_attr_iommu[] = {
399 KFD_PERF_DESC(max_concurrent, 0),
400 KFD_PERF_DESC(num_counters, 0),
401 KFD_PERF_DESC(counter_ids, 0),
403 /****************************************/
405 static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
408 struct kfd_topology_device *dev;
409 char public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE];
411 uint32_t log_max_watch_addr;
413 /* Making sure that the buffer is an empty string */
416 if (strcmp(attr->name, "gpu_id") == 0) {
417 dev = container_of(attr, struct kfd_topology_device,
419 return sysfs_show_32bit_val(buffer, dev->gpu_id);
422 if (strcmp(attr->name, "name") == 0) {
423 dev = container_of(attr, struct kfd_topology_device,
425 for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE; i++) {
427 (char)dev->node_props.marketing_name[i];
428 if (dev->node_props.marketing_name[i] == 0)
431 public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1] = 0x0;
432 return sysfs_show_str_val(buffer, public_name);
435 dev = container_of(attr, struct kfd_topology_device,
437 sysfs_show_32bit_prop(buffer, "cpu_cores_count",
438 dev->node_props.cpu_cores_count);
439 sysfs_show_32bit_prop(buffer, "simd_count",
440 dev->node_props.simd_count);
441 sysfs_show_32bit_prop(buffer, "mem_banks_count",
442 dev->node_props.mem_banks_count);
443 sysfs_show_32bit_prop(buffer, "caches_count",
444 dev->node_props.caches_count);
445 sysfs_show_32bit_prop(buffer, "io_links_count",
446 dev->node_props.io_links_count);
447 sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
448 dev->node_props.cpu_core_id_base);
449 sysfs_show_32bit_prop(buffer, "simd_id_base",
450 dev->node_props.simd_id_base);
451 sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
452 dev->node_props.max_waves_per_simd);
453 sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
454 dev->node_props.lds_size_in_kb);
455 sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
456 dev->node_props.gds_size_in_kb);
457 sysfs_show_32bit_prop(buffer, "wave_front_size",
458 dev->node_props.wave_front_size);
459 sysfs_show_32bit_prop(buffer, "array_count",
460 dev->node_props.array_count);
461 sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
462 dev->node_props.simd_arrays_per_engine);
463 sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
464 dev->node_props.cu_per_simd_array);
465 sysfs_show_32bit_prop(buffer, "simd_per_cu",
466 dev->node_props.simd_per_cu);
467 sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
468 dev->node_props.max_slots_scratch_cu);
469 sysfs_show_32bit_prop(buffer, "vendor_id",
470 dev->node_props.vendor_id);
471 sysfs_show_32bit_prop(buffer, "device_id",
472 dev->node_props.device_id);
473 sysfs_show_32bit_prop(buffer, "location_id",
474 dev->node_props.location_id);
475 sysfs_show_32bit_prop(buffer, "drm_render_minor",
476 dev->node_props.drm_render_minor);
477 sysfs_show_64bit_prop(buffer, "hive_id",
478 dev->node_props.hive_id);
482 __ilog2_u32(dev->gpu->device_info->num_of_watch_points);
484 if (log_max_watch_addr) {
485 dev->node_props.capability |=
486 HSA_CAP_WATCH_POINTS_SUPPORTED;
488 dev->node_props.capability |=
489 ((log_max_watch_addr <<
490 HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
491 HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
494 if (dev->gpu->device_info->asic_family == CHIP_TONGA)
495 dev->node_props.capability |=
496 HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
498 sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
499 dev->node_props.max_engine_clk_fcompute);
501 sysfs_show_64bit_prop(buffer, "local_mem_size",
502 (unsigned long long int) 0);
504 sysfs_show_32bit_prop(buffer, "fw_version",
505 dev->gpu->mec_fw_version);
506 sysfs_show_32bit_prop(buffer, "capability",
507 dev->node_props.capability);
508 sysfs_show_32bit_prop(buffer, "sdma_fw_version",
509 dev->gpu->sdma_fw_version);
512 return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
513 cpufreq_quick_get_max(0)/1000);
516 static const struct sysfs_ops node_ops = {
520 static struct kobj_type node_type = {
521 .release = kfd_topology_kobj_release,
522 .sysfs_ops = &node_ops,
525 static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
527 sysfs_remove_file(kobj, attr);
532 static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
534 struct kfd_iolink_properties *iolink;
535 struct kfd_cache_properties *cache;
536 struct kfd_mem_properties *mem;
537 struct kfd_perf_properties *perf;
539 if (dev->kobj_iolink) {
540 list_for_each_entry(iolink, &dev->io_link_props, list)
542 kfd_remove_sysfs_file(iolink->kobj,
546 kobject_del(dev->kobj_iolink);
547 kobject_put(dev->kobj_iolink);
548 dev->kobj_iolink = NULL;
551 if (dev->kobj_cache) {
552 list_for_each_entry(cache, &dev->cache_props, list)
554 kfd_remove_sysfs_file(cache->kobj,
558 kobject_del(dev->kobj_cache);
559 kobject_put(dev->kobj_cache);
560 dev->kobj_cache = NULL;
564 list_for_each_entry(mem, &dev->mem_props, list)
566 kfd_remove_sysfs_file(mem->kobj, &mem->attr);
569 kobject_del(dev->kobj_mem);
570 kobject_put(dev->kobj_mem);
571 dev->kobj_mem = NULL;
574 if (dev->kobj_perf) {
575 list_for_each_entry(perf, &dev->perf_props, list) {
576 kfree(perf->attr_group);
577 perf->attr_group = NULL;
579 kobject_del(dev->kobj_perf);
580 kobject_put(dev->kobj_perf);
581 dev->kobj_perf = NULL;
584 if (dev->kobj_node) {
585 sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
586 sysfs_remove_file(dev->kobj_node, &dev->attr_name);
587 sysfs_remove_file(dev->kobj_node, &dev->attr_props);
588 kobject_del(dev->kobj_node);
589 kobject_put(dev->kobj_node);
590 dev->kobj_node = NULL;
594 static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
597 struct kfd_iolink_properties *iolink;
598 struct kfd_cache_properties *cache;
599 struct kfd_mem_properties *mem;
600 struct kfd_perf_properties *perf;
602 uint32_t i, num_attrs;
603 struct attribute **attrs;
605 if (WARN_ON(dev->kobj_node))
609 * Creating the sysfs folders
611 dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
615 ret = kobject_init_and_add(dev->kobj_node, &node_type,
616 sys_props.kobj_nodes, "%d", id);
620 dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
624 dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
625 if (!dev->kobj_cache)
628 dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
629 if (!dev->kobj_iolink)
632 dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
637 * Creating sysfs files for node properties
639 dev->attr_gpuid.name = "gpu_id";
640 dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
641 sysfs_attr_init(&dev->attr_gpuid);
642 dev->attr_name.name = "name";
643 dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
644 sysfs_attr_init(&dev->attr_name);
645 dev->attr_props.name = "properties";
646 dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
647 sysfs_attr_init(&dev->attr_props);
648 ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
651 ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
654 ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
659 list_for_each_entry(mem, &dev->mem_props, list) {
660 mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
663 ret = kobject_init_and_add(mem->kobj, &mem_type,
664 dev->kobj_mem, "%d", i);
668 mem->attr.name = "properties";
669 mem->attr.mode = KFD_SYSFS_FILE_MODE;
670 sysfs_attr_init(&mem->attr);
671 ret = sysfs_create_file(mem->kobj, &mem->attr);
678 list_for_each_entry(cache, &dev->cache_props, list) {
679 cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
682 ret = kobject_init_and_add(cache->kobj, &cache_type,
683 dev->kobj_cache, "%d", i);
687 cache->attr.name = "properties";
688 cache->attr.mode = KFD_SYSFS_FILE_MODE;
689 sysfs_attr_init(&cache->attr);
690 ret = sysfs_create_file(cache->kobj, &cache->attr);
697 list_for_each_entry(iolink, &dev->io_link_props, list) {
698 iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
701 ret = kobject_init_and_add(iolink->kobj, &iolink_type,
702 dev->kobj_iolink, "%d", i);
706 iolink->attr.name = "properties";
707 iolink->attr.mode = KFD_SYSFS_FILE_MODE;
708 sysfs_attr_init(&iolink->attr);
709 ret = sysfs_create_file(iolink->kobj, &iolink->attr);
715 /* All hardware blocks have the same number of attributes. */
716 num_attrs = ARRAY_SIZE(perf_attr_iommu);
717 list_for_each_entry(perf, &dev->perf_props, list) {
718 perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
719 * num_attrs + sizeof(struct attribute_group),
721 if (!perf->attr_group)
724 attrs = (struct attribute **)(perf->attr_group + 1);
725 if (!strcmp(perf->block_name, "iommu")) {
726 /* Information of IOMMU's num_counters and counter_ids is shown
727 * under /sys/bus/event_source/devices/amd_iommu. We don't
730 perf_attr_iommu[0].data = perf->max_concurrent;
731 for (i = 0; i < num_attrs; i++)
732 attrs[i] = &perf_attr_iommu[i].attr.attr;
734 perf->attr_group->name = perf->block_name;
735 perf->attr_group->attrs = attrs;
736 ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
744 /* Called with write topology lock acquired */
745 static int kfd_build_sysfs_node_tree(void)
747 struct kfd_topology_device *dev;
751 list_for_each_entry(dev, &topology_device_list, list) {
752 ret = kfd_build_sysfs_node_entry(dev, i);
761 /* Called with write topology lock acquired */
762 static void kfd_remove_sysfs_node_tree(void)
764 struct kfd_topology_device *dev;
766 list_for_each_entry(dev, &topology_device_list, list)
767 kfd_remove_sysfs_node_entry(dev);
770 static int kfd_topology_update_sysfs(void)
774 pr_info("Creating topology SYSFS entries\n");
775 if (!sys_props.kobj_topology) {
776 sys_props.kobj_topology =
777 kfd_alloc_struct(sys_props.kobj_topology);
778 if (!sys_props.kobj_topology)
781 ret = kobject_init_and_add(sys_props.kobj_topology,
782 &sysprops_type, &kfd_device->kobj,
787 sys_props.kobj_nodes = kobject_create_and_add("nodes",
788 sys_props.kobj_topology);
789 if (!sys_props.kobj_nodes)
792 sys_props.attr_genid.name = "generation_id";
793 sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
794 sysfs_attr_init(&sys_props.attr_genid);
795 ret = sysfs_create_file(sys_props.kobj_topology,
796 &sys_props.attr_genid);
800 sys_props.attr_props.name = "system_properties";
801 sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
802 sysfs_attr_init(&sys_props.attr_props);
803 ret = sysfs_create_file(sys_props.kobj_topology,
804 &sys_props.attr_props);
809 kfd_remove_sysfs_node_tree();
811 return kfd_build_sysfs_node_tree();
814 static void kfd_topology_release_sysfs(void)
816 kfd_remove_sysfs_node_tree();
817 if (sys_props.kobj_topology) {
818 sysfs_remove_file(sys_props.kobj_topology,
819 &sys_props.attr_genid);
820 sysfs_remove_file(sys_props.kobj_topology,
821 &sys_props.attr_props);
822 if (sys_props.kobj_nodes) {
823 kobject_del(sys_props.kobj_nodes);
824 kobject_put(sys_props.kobj_nodes);
825 sys_props.kobj_nodes = NULL;
827 kobject_del(sys_props.kobj_topology);
828 kobject_put(sys_props.kobj_topology);
829 sys_props.kobj_topology = NULL;
833 /* Called with write topology_lock acquired */
834 static void kfd_topology_update_device_list(struct list_head *temp_list,
835 struct list_head *master_list)
837 while (!list_empty(temp_list)) {
838 list_move_tail(temp_list->next, master_list);
839 sys_props.num_devices++;
843 static void kfd_debug_print_topology(void)
845 struct kfd_topology_device *dev;
847 down_read(&topology_lock);
849 dev = list_last_entry(&topology_device_list,
850 struct kfd_topology_device, list);
852 if (dev->node_props.cpu_cores_count &&
853 dev->node_props.simd_count) {
854 pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
855 dev->node_props.device_id,
856 dev->node_props.vendor_id);
857 } else if (dev->node_props.cpu_cores_count)
858 pr_info("Topology: Add CPU node\n");
859 else if (dev->node_props.simd_count)
860 pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
861 dev->node_props.device_id,
862 dev->node_props.vendor_id);
864 up_read(&topology_lock);
867 /* Helper function for intializing platform_xx members of
868 * kfd_system_properties. Uses OEM info from the last CPU/APU node.
870 static void kfd_update_system_properties(void)
872 struct kfd_topology_device *dev;
874 down_read(&topology_lock);
875 dev = list_last_entry(&topology_device_list,
876 struct kfd_topology_device, list);
878 sys_props.platform_id =
879 (*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
880 sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
881 sys_props.platform_rev = dev->oem_revision;
883 up_read(&topology_lock);
886 static void find_system_memory(const struct dmi_header *dm,
889 struct kfd_mem_properties *mem;
890 u16 mem_width, mem_clock;
891 struct kfd_topology_device *kdev =
892 (struct kfd_topology_device *)private;
893 const u8 *dmi_data = (const u8 *)(dm + 1);
895 if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
896 mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
897 mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
898 list_for_each_entry(mem, &kdev->mem_props, list) {
899 if (mem_width != 0xFFFF && mem_width != 0)
900 mem->width = mem_width;
902 mem->mem_clk_max = mem_clock;
908 * Performance counters information is not part of CRAT but we would like to
909 * put them in the sysfs under topology directory for Thunk to get the data.
910 * This function is called before updating the sysfs.
912 static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
914 /* These are the only counters supported so far */
915 return kfd_iommu_add_perf_counters(kdev);
918 /* kfd_add_non_crat_information - Add information that is not currently
919 * defined in CRAT but is necessary for KFD topology
920 * @dev - topology device to which addition info is added
922 static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
924 /* Check if CPU only node. */
926 /* Add system memory information */
927 dmi_walk(find_system_memory, kdev);
929 /* TODO: For GPU node, rearrange code from kfd_topology_add_device */
932 /* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
933 * Ignore CRAT for all other devices. AMD APU is identified if both CPU
934 * and GPU cores are present.
935 * @device_list - topology device list created by parsing ACPI CRAT table.
936 * @return - TRUE if invalid, FALSE is valid.
938 static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
940 struct kfd_topology_device *dev;
942 list_for_each_entry(dev, device_list, list) {
943 if (dev->node_props.cpu_cores_count &&
944 dev->node_props.simd_count)
947 pr_info("Ignoring ACPI CRAT on non-APU system\n");
951 int kfd_topology_init(void)
953 void *crat_image = NULL;
954 size_t image_size = 0;
956 struct list_head temp_topology_device_list;
957 int cpu_only_node = 0;
958 struct kfd_topology_device *kdev;
959 int proximity_domain;
961 /* topology_device_list - Master list of all topology devices
962 * temp_topology_device_list - temporary list created while parsing CRAT
963 * or VCRAT. Once parsing is complete the contents of list is moved to
964 * topology_device_list
967 /* Initialize the head for the both the lists */
968 INIT_LIST_HEAD(&topology_device_list);
969 INIT_LIST_HEAD(&temp_topology_device_list);
970 init_rwsem(&topology_lock);
972 memset(&sys_props, 0, sizeof(sys_props));
974 /* Proximity domains in ACPI CRAT tables start counting at
975 * 0. The same should be true for virtual CRAT tables created
976 * at this stage. GPUs added later in kfd_topology_add_device
979 proximity_domain = 0;
982 * Get the CRAT image from the ACPI. If ACPI doesn't have one
983 * or if ACPI CRAT is invalid create a virtual CRAT.
984 * NOTE: The current implementation expects all AMD APUs to have
985 * CRAT. If no CRAT is available, it is assumed to be a CPU
987 ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
989 ret = kfd_parse_crat_table(crat_image,
990 &temp_topology_device_list,
993 kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
994 kfd_release_topology_device_list(
995 &temp_topology_device_list);
996 kfd_destroy_crat_image(crat_image);
1002 ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
1003 COMPUTE_UNIT_CPU, NULL,
1007 pr_err("Error creating VCRAT table for CPU\n");
1011 ret = kfd_parse_crat_table(crat_image,
1012 &temp_topology_device_list,
1015 pr_err("Error parsing VCRAT table for CPU\n");
1020 kdev = list_first_entry(&temp_topology_device_list,
1021 struct kfd_topology_device, list);
1022 kfd_add_perf_to_topology(kdev);
1024 down_write(&topology_lock);
1025 kfd_topology_update_device_list(&temp_topology_device_list,
1026 &topology_device_list);
1027 atomic_set(&topology_crat_proximity_domain, sys_props.num_devices-1);
1028 ret = kfd_topology_update_sysfs();
1029 up_write(&topology_lock);
1032 sys_props.generation_count++;
1033 kfd_update_system_properties();
1034 kfd_debug_print_topology();
1035 pr_info("Finished initializing topology\n");
1037 pr_err("Failed to update topology in sysfs ret=%d\n", ret);
1039 /* For nodes with GPU, this information gets added
1040 * when GPU is detected (kfd_topology_add_device).
1042 if (cpu_only_node) {
1043 /* Add additional information to CPU only node created above */
1044 down_write(&topology_lock);
1045 kdev = list_first_entry(&topology_device_list,
1046 struct kfd_topology_device, list);
1047 up_write(&topology_lock);
1048 kfd_add_non_crat_information(kdev);
1052 kfd_destroy_crat_image(crat_image);
1056 void kfd_topology_shutdown(void)
1058 down_write(&topology_lock);
1059 kfd_topology_release_sysfs();
1060 kfd_release_live_view();
1061 up_write(&topology_lock);
1064 static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
1068 uint64_t local_mem_size;
1070 struct kfd_local_mem_info local_mem_info;
1075 amdgpu_amdkfd_get_local_mem_info(gpu->kgd, &local_mem_info);
1077 local_mem_size = local_mem_info.local_mem_size_private +
1078 local_mem_info.local_mem_size_public;
1080 buf[0] = gpu->pdev->devfn;
1081 buf[1] = gpu->pdev->subsystem_vendor;
1082 buf[2] = gpu->pdev->subsystem_device;
1083 buf[3] = gpu->pdev->device;
1084 buf[4] = gpu->pdev->bus->number;
1085 buf[5] = lower_32_bits(local_mem_size);
1086 buf[6] = upper_32_bits(local_mem_size);
1088 for (i = 0, hashout = 0; i < 7; i++)
1089 hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
1093 /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
1094 * the GPU device is not already present in the topology device
1095 * list then return NULL. This means a new topology device has to
1096 * be created for this GPU.
1098 static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
1100 struct kfd_topology_device *dev;
1101 struct kfd_topology_device *out_dev = NULL;
1103 down_write(&topology_lock);
1104 list_for_each_entry(dev, &topology_device_list, list) {
1105 /* Discrete GPUs need their own topology device list
1106 * entries. Don't assign them to CPU/APU nodes.
1108 if (!gpu->device_info->needs_iommu_device &&
1109 dev->node_props.cpu_cores_count)
1112 if (!dev->gpu && (dev->node_props.simd_count > 0)) {
1118 up_write(&topology_lock);
1122 static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
1125 * TODO: Generate an event for thunk about the arrival/removal
1130 /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
1131 * patch this after CRAT parsing.
1133 static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
1135 struct kfd_mem_properties *mem;
1136 struct kfd_local_mem_info local_mem_info;
1141 /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
1142 * single bank of VRAM local memory.
1143 * for dGPUs - VCRAT reports only one bank of Local Memory
1144 * for APUs - If CRAT from ACPI reports more than one bank, then
1145 * all the banks will report the same mem_clk_max information
1147 amdgpu_amdkfd_get_local_mem_info(dev->gpu->kgd, &local_mem_info);
1149 list_for_each_entry(mem, &dev->mem_props, list)
1150 mem->mem_clk_max = local_mem_info.mem_clk_max;
1153 static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
1155 struct kfd_iolink_properties *link, *cpu_link;
1156 struct kfd_topology_device *cpu_dev;
1158 uint32_t cpu_flag = CRAT_IOLINK_FLAGS_ENABLED;
1159 uint32_t flag = CRAT_IOLINK_FLAGS_ENABLED;
1161 if (!dev || !dev->gpu)
1164 pcie_capability_read_dword(dev->gpu->pdev,
1165 PCI_EXP_DEVCAP2, &cap);
1167 if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1168 PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1169 cpu_flag |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1170 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1172 if (!dev->gpu->pci_atomic_requested ||
1173 dev->gpu->device_info->asic_family == CHIP_HAWAII)
1174 flag |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1175 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1177 /* GPU only creates direct links so apply flags setting to all */
1178 list_for_each_entry(link, &dev->io_link_props, list) {
1180 cpu_dev = kfd_topology_device_by_proximity_domain(
1183 list_for_each_entry(cpu_link,
1184 &cpu_dev->io_link_props, list)
1185 if (cpu_link->node_to == link->node_from)
1186 cpu_link->flags = cpu_flag;
1191 int kfd_topology_add_device(struct kfd_dev *gpu)
1194 struct kfd_topology_device *dev;
1195 struct kfd_cu_info cu_info;
1197 struct list_head temp_topology_device_list;
1198 void *crat_image = NULL;
1199 size_t image_size = 0;
1200 int proximity_domain;
1201 struct amdgpu_ras *ctx;
1203 INIT_LIST_HEAD(&temp_topology_device_list);
1205 gpu_id = kfd_generate_gpu_id(gpu);
1207 pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1209 proximity_domain = atomic_inc_return(&topology_crat_proximity_domain);
1211 /* Check to see if this gpu device exists in the topology_device_list.
1212 * If so, assign the gpu to that device,
1213 * else create a Virtual CRAT for this gpu device and then parse that
1214 * CRAT to create a new topology device. Once created assign the gpu to
1215 * that topology device
1217 dev = kfd_assign_gpu(gpu);
1219 res = kfd_create_crat_image_virtual(&crat_image, &image_size,
1220 COMPUTE_UNIT_GPU, gpu,
1223 pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
1227 res = kfd_parse_crat_table(crat_image,
1228 &temp_topology_device_list,
1231 pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
1236 down_write(&topology_lock);
1237 kfd_topology_update_device_list(&temp_topology_device_list,
1238 &topology_device_list);
1240 /* Update the SYSFS tree, since we added another topology
1243 res = kfd_topology_update_sysfs();
1244 up_write(&topology_lock);
1247 sys_props.generation_count++;
1249 pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
1251 dev = kfd_assign_gpu(gpu);
1252 if (WARN_ON(!dev)) {
1258 dev->gpu_id = gpu_id;
1261 /* TODO: Move the following lines to function
1262 * kfd_add_non_crat_information
1265 /* Fill-in additional information that is not available in CRAT but
1266 * needed for the topology
1269 amdgpu_amdkfd_get_cu_info(dev->gpu->kgd, &cu_info);
1270 dev->node_props.simd_arrays_per_engine =
1271 cu_info.num_shader_arrays_per_engine;
1273 dev->node_props.vendor_id = gpu->pdev->vendor;
1274 dev->node_props.device_id = gpu->pdev->device;
1275 dev->node_props.location_id = pci_dev_id(gpu->pdev);
1276 dev->node_props.max_engine_clk_fcompute =
1277 amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->kgd);
1278 dev->node_props.max_engine_clk_ccompute =
1279 cpufreq_quick_get_max(0) / 1000;
1280 dev->node_props.drm_render_minor =
1281 gpu->shared_resources.drm_render_minor;
1283 dev->node_props.hive_id = gpu->hive_id;
1285 kfd_fill_mem_clk_max_info(dev);
1286 kfd_fill_iolink_non_crat_info(dev);
1288 switch (dev->gpu->device_info->asic_family) {
1292 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
1293 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1294 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1298 case CHIP_POLARIS10:
1299 case CHIP_POLARIS11:
1300 case CHIP_POLARIS12:
1301 pr_debug("Adding doorbell packet type capability\n");
1302 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
1303 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1304 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1310 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1311 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1312 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1315 WARN(1, "Unexpected ASIC family %u",
1316 dev->gpu->device_info->asic_family);
1319 /* Fix errors in CZ CRAT.
1320 * simd_count: Carrizo CRAT reports wrong simd_count, probably
1321 * because it doesn't consider masked out CUs
1322 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
1323 * capability flag: Carrizo CRAT doesn't report IOMMU flags
1325 if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
1326 dev->node_props.simd_count =
1327 cu_info.simd_per_cu * cu_info.cu_active_number;
1328 dev->node_props.max_waves_per_simd = 10;
1329 dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
1332 ctx = amdgpu_ras_get_context((struct amdgpu_device *)(dev->gpu->kgd));
1334 /* kfd only concerns sram ecc on GFX/SDMA and HBM ecc on UMC */
1335 dev->node_props.capability |=
1336 (((ctx->features & BIT(AMDGPU_RAS_BLOCK__SDMA)) != 0) ||
1337 ((ctx->features & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0)) ?
1338 HSA_CAP_SRAM_EDCSUPPORTED : 0;
1339 dev->node_props.capability |= ((ctx->features & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
1340 HSA_CAP_MEM_EDCSUPPORTED : 0;
1342 dev->node_props.capability |= (ctx->features != 0) ?
1343 HSA_CAP_RASEVENTNOTIFY : 0;
1346 kfd_debug_print_topology();
1349 kfd_notify_gpu_change(gpu_id, 1);
1351 kfd_destroy_crat_image(crat_image);
1355 int kfd_topology_remove_device(struct kfd_dev *gpu)
1357 struct kfd_topology_device *dev, *tmp;
1361 down_write(&topology_lock);
1363 list_for_each_entry_safe(dev, tmp, &topology_device_list, list)
1364 if (dev->gpu == gpu) {
1365 gpu_id = dev->gpu_id;
1366 kfd_remove_sysfs_node_entry(dev);
1367 kfd_release_topology_device(dev);
1368 sys_props.num_devices--;
1370 if (kfd_topology_update_sysfs() < 0)
1371 kfd_topology_release_sysfs();
1375 up_write(&topology_lock);
1378 kfd_notify_gpu_change(gpu_id, 0);
1383 /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
1384 * topology. If GPU device is found @idx, then valid kfd_dev pointer is
1385 * returned through @kdev
1386 * Return - 0: On success (@kdev will be NULL for non GPU nodes)
1387 * -1: If end of list
1389 int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
1392 struct kfd_topology_device *top_dev;
1393 uint8_t device_idx = 0;
1396 down_read(&topology_lock);
1398 list_for_each_entry(top_dev, &topology_device_list, list) {
1399 if (device_idx == idx) {
1400 *kdev = top_dev->gpu;
1401 up_read(&topology_lock);
1408 up_read(&topology_lock);
1414 static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
1416 int first_cpu_of_numa_node;
1418 if (!cpumask || cpumask == cpu_none_mask)
1420 first_cpu_of_numa_node = cpumask_first(cpumask);
1421 if (first_cpu_of_numa_node >= nr_cpu_ids)
1423 #ifdef CONFIG_X86_64
1424 return cpu_data(first_cpu_of_numa_node).apicid;
1426 return first_cpu_of_numa_node;
1430 /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
1431 * of the given NUMA node (numa_node_id)
1432 * Return -1 on failure
1434 int kfd_numa_node_to_apic_id(int numa_node_id)
1436 if (numa_node_id == -1) {
1437 pr_warn("Invalid NUMA Node. Use online CPU mask\n");
1438 return kfd_cpumask_to_apic_id(cpu_online_mask);
1440 return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
1443 #if defined(CONFIG_DEBUG_FS)
1445 int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
1447 struct kfd_topology_device *dev;
1451 down_read(&topology_lock);
1453 list_for_each_entry(dev, &topology_device_list, list) {
1459 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1460 r = dqm_debugfs_hqds(m, dev->gpu->dqm);
1465 up_read(&topology_lock);
1470 int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
1472 struct kfd_topology_device *dev;
1476 down_read(&topology_lock);
1478 list_for_each_entry(dev, &topology_device_list, list) {
1484 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1485 r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets);
1490 up_read(&topology_lock);