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"
39 /* topology_device_list - Master list of all topology devices */
40 static struct list_head topology_device_list;
41 static struct kfd_system_properties sys_props;
43 static DECLARE_RWSEM(topology_lock);
44 static atomic_t topology_crat_proximity_domain;
46 struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
47 uint32_t proximity_domain)
49 struct kfd_topology_device *top_dev;
50 struct kfd_topology_device *device = NULL;
52 down_read(&topology_lock);
54 list_for_each_entry(top_dev, &topology_device_list, list)
55 if (top_dev->proximity_domain == proximity_domain) {
60 up_read(&topology_lock);
65 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
67 struct kfd_topology_device *top_dev;
68 struct kfd_dev *device = NULL;
70 down_read(&topology_lock);
72 list_for_each_entry(top_dev, &topology_device_list, list)
73 if (top_dev->gpu_id == gpu_id) {
74 device = top_dev->gpu;
78 up_read(&topology_lock);
83 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
85 struct kfd_topology_device *top_dev;
86 struct kfd_dev *device = NULL;
88 down_read(&topology_lock);
90 list_for_each_entry(top_dev, &topology_device_list, list)
91 if (top_dev->gpu->pdev == pdev) {
92 device = top_dev->gpu;
96 up_read(&topology_lock);
101 /* Called with write topology_lock acquired */
102 static void kfd_release_topology_device(struct kfd_topology_device *dev)
104 struct kfd_mem_properties *mem;
105 struct kfd_cache_properties *cache;
106 struct kfd_iolink_properties *iolink;
107 struct kfd_perf_properties *perf;
109 list_del(&dev->list);
111 while (dev->mem_props.next != &dev->mem_props) {
112 mem = container_of(dev->mem_props.next,
113 struct kfd_mem_properties, list);
114 list_del(&mem->list);
118 while (dev->cache_props.next != &dev->cache_props) {
119 cache = container_of(dev->cache_props.next,
120 struct kfd_cache_properties, list);
121 list_del(&cache->list);
125 while (dev->io_link_props.next != &dev->io_link_props) {
126 iolink = container_of(dev->io_link_props.next,
127 struct kfd_iolink_properties, list);
128 list_del(&iolink->list);
132 while (dev->perf_props.next != &dev->perf_props) {
133 perf = container_of(dev->perf_props.next,
134 struct kfd_perf_properties, list);
135 list_del(&perf->list);
142 void kfd_release_topology_device_list(struct list_head *device_list)
144 struct kfd_topology_device *dev;
146 while (!list_empty(device_list)) {
147 dev = list_first_entry(device_list,
148 struct kfd_topology_device, list);
149 kfd_release_topology_device(dev);
153 static void kfd_release_live_view(void)
155 kfd_release_topology_device_list(&topology_device_list);
156 memset(&sys_props, 0, sizeof(sys_props));
159 struct kfd_topology_device *kfd_create_topology_device(
160 struct list_head *device_list)
162 struct kfd_topology_device *dev;
164 dev = kfd_alloc_struct(dev);
166 pr_err("No memory to allocate a topology device");
170 INIT_LIST_HEAD(&dev->mem_props);
171 INIT_LIST_HEAD(&dev->cache_props);
172 INIT_LIST_HEAD(&dev->io_link_props);
173 INIT_LIST_HEAD(&dev->perf_props);
175 list_add_tail(&dev->list, device_list);
181 #define sysfs_show_gen_prop(buffer, fmt, ...) \
182 snprintf(buffer, PAGE_SIZE, "%s"fmt, buffer, __VA_ARGS__)
183 #define sysfs_show_32bit_prop(buffer, name, value) \
184 sysfs_show_gen_prop(buffer, "%s %u\n", name, value)
185 #define sysfs_show_64bit_prop(buffer, name, value) \
186 sysfs_show_gen_prop(buffer, "%s %llu\n", name, value)
187 #define sysfs_show_32bit_val(buffer, value) \
188 sysfs_show_gen_prop(buffer, "%u\n", value)
189 #define sysfs_show_str_val(buffer, value) \
190 sysfs_show_gen_prop(buffer, "%s\n", value)
192 static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
197 /* Making sure that the buffer is an empty string */
200 if (attr == &sys_props.attr_genid) {
201 ret = sysfs_show_32bit_val(buffer, sys_props.generation_count);
202 } else if (attr == &sys_props.attr_props) {
203 sysfs_show_64bit_prop(buffer, "platform_oem",
204 sys_props.platform_oem);
205 sysfs_show_64bit_prop(buffer, "platform_id",
206 sys_props.platform_id);
207 ret = sysfs_show_64bit_prop(buffer, "platform_rev",
208 sys_props.platform_rev);
216 static void kfd_topology_kobj_release(struct kobject *kobj)
221 static const struct sysfs_ops sysprops_ops = {
222 .show = sysprops_show,
225 static struct kobj_type sysprops_type = {
226 .release = kfd_topology_kobj_release,
227 .sysfs_ops = &sysprops_ops,
230 static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
234 struct kfd_iolink_properties *iolink;
236 /* Making sure that the buffer is an empty string */
239 iolink = container_of(attr, struct kfd_iolink_properties, attr);
240 sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
241 sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
242 sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
243 sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
244 sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
245 sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
246 sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
247 sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
248 sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
249 sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
250 sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
251 iolink->rec_transfer_size);
252 ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
257 static const struct sysfs_ops iolink_ops = {
261 static struct kobj_type iolink_type = {
262 .release = kfd_topology_kobj_release,
263 .sysfs_ops = &iolink_ops,
266 static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
270 struct kfd_mem_properties *mem;
272 /* Making sure that the buffer is an empty string */
275 mem = container_of(attr, struct kfd_mem_properties, attr);
276 sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
277 sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
278 sysfs_show_32bit_prop(buffer, "flags", mem->flags);
279 sysfs_show_32bit_prop(buffer, "width", mem->width);
280 ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
285 static const struct sysfs_ops mem_ops = {
289 static struct kobj_type mem_type = {
290 .release = kfd_topology_kobj_release,
291 .sysfs_ops = &mem_ops,
294 static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
299 struct kfd_cache_properties *cache;
301 /* Making sure that the buffer is an empty string */
304 cache = container_of(attr, struct kfd_cache_properties, attr);
305 sysfs_show_32bit_prop(buffer, "processor_id_low",
306 cache->processor_id_low);
307 sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
308 sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
309 sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
310 sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
311 cache->cachelines_per_tag);
312 sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
313 sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
314 sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
315 snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
316 for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++)
317 for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++) {
319 if (cache->sibling_map[i] & (1 << j))
320 ret = snprintf(buffer, PAGE_SIZE,
321 "%s%d%s", buffer, 1, ",");
323 ret = snprintf(buffer, PAGE_SIZE,
324 "%s%d%s", buffer, 0, ",");
326 /* Replace the last "," with end of line */
327 *(buffer + strlen(buffer) - 1) = 0xA;
331 static const struct sysfs_ops cache_ops = {
332 .show = kfd_cache_show,
335 static struct kobj_type cache_type = {
336 .release = kfd_topology_kobj_release,
337 .sysfs_ops = &cache_ops,
340 /****** Sysfs of Performance Counters ******/
342 struct kfd_perf_attr {
343 struct kobj_attribute attr;
347 static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
350 struct kfd_perf_attr *attr;
353 attr = container_of(attrs, struct kfd_perf_attr, attr);
354 if (!attr->data) /* invalid data for PMC */
357 return sysfs_show_32bit_val(buf, attr->data);
360 #define KFD_PERF_DESC(_name, _data) \
362 .attr = __ATTR(_name, 0444, perf_show, NULL), \
366 static struct kfd_perf_attr perf_attr_iommu[] = {
367 KFD_PERF_DESC(max_concurrent, 0),
368 KFD_PERF_DESC(num_counters, 0),
369 KFD_PERF_DESC(counter_ids, 0),
371 /****************************************/
373 static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
376 struct kfd_topology_device *dev;
377 char public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE];
379 uint32_t log_max_watch_addr;
381 /* Making sure that the buffer is an empty string */
384 if (strcmp(attr->name, "gpu_id") == 0) {
385 dev = container_of(attr, struct kfd_topology_device,
387 return sysfs_show_32bit_val(buffer, dev->gpu_id);
390 if (strcmp(attr->name, "name") == 0) {
391 dev = container_of(attr, struct kfd_topology_device,
393 for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE; i++) {
395 (char)dev->node_props.marketing_name[i];
396 if (dev->node_props.marketing_name[i] == 0)
399 public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1] = 0x0;
400 return sysfs_show_str_val(buffer, public_name);
403 dev = container_of(attr, struct kfd_topology_device,
405 sysfs_show_32bit_prop(buffer, "cpu_cores_count",
406 dev->node_props.cpu_cores_count);
407 sysfs_show_32bit_prop(buffer, "simd_count",
408 dev->node_props.simd_count);
409 sysfs_show_32bit_prop(buffer, "mem_banks_count",
410 dev->node_props.mem_banks_count);
411 sysfs_show_32bit_prop(buffer, "caches_count",
412 dev->node_props.caches_count);
413 sysfs_show_32bit_prop(buffer, "io_links_count",
414 dev->node_props.io_links_count);
415 sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
416 dev->node_props.cpu_core_id_base);
417 sysfs_show_32bit_prop(buffer, "simd_id_base",
418 dev->node_props.simd_id_base);
419 sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
420 dev->node_props.max_waves_per_simd);
421 sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
422 dev->node_props.lds_size_in_kb);
423 sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
424 dev->node_props.gds_size_in_kb);
425 sysfs_show_32bit_prop(buffer, "wave_front_size",
426 dev->node_props.wave_front_size);
427 sysfs_show_32bit_prop(buffer, "array_count",
428 dev->node_props.array_count);
429 sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
430 dev->node_props.simd_arrays_per_engine);
431 sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
432 dev->node_props.cu_per_simd_array);
433 sysfs_show_32bit_prop(buffer, "simd_per_cu",
434 dev->node_props.simd_per_cu);
435 sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
436 dev->node_props.max_slots_scratch_cu);
437 sysfs_show_32bit_prop(buffer, "vendor_id",
438 dev->node_props.vendor_id);
439 sysfs_show_32bit_prop(buffer, "device_id",
440 dev->node_props.device_id);
441 sysfs_show_32bit_prop(buffer, "location_id",
442 dev->node_props.location_id);
446 __ilog2_u32(dev->gpu->device_info->num_of_watch_points);
448 if (log_max_watch_addr) {
449 dev->node_props.capability |=
450 HSA_CAP_WATCH_POINTS_SUPPORTED;
452 dev->node_props.capability |=
453 ((log_max_watch_addr <<
454 HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
455 HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
458 if (dev->gpu->device_info->asic_family == CHIP_TONGA)
459 dev->node_props.capability |=
460 HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
462 sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
463 dev->node_props.max_engine_clk_fcompute);
465 sysfs_show_64bit_prop(buffer, "local_mem_size",
466 (unsigned long long int) 0);
468 sysfs_show_32bit_prop(buffer, "fw_version",
469 dev->gpu->kfd2kgd->get_fw_version(
472 sysfs_show_32bit_prop(buffer, "capability",
473 dev->node_props.capability);
476 return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
477 cpufreq_quick_get_max(0)/1000);
480 static const struct sysfs_ops node_ops = {
484 static struct kobj_type node_type = {
485 .release = kfd_topology_kobj_release,
486 .sysfs_ops = &node_ops,
489 static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
491 sysfs_remove_file(kobj, attr);
496 static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
498 struct kfd_iolink_properties *iolink;
499 struct kfd_cache_properties *cache;
500 struct kfd_mem_properties *mem;
501 struct kfd_perf_properties *perf;
503 if (dev->kobj_iolink) {
504 list_for_each_entry(iolink, &dev->io_link_props, list)
506 kfd_remove_sysfs_file(iolink->kobj,
510 kobject_del(dev->kobj_iolink);
511 kobject_put(dev->kobj_iolink);
512 dev->kobj_iolink = NULL;
515 if (dev->kobj_cache) {
516 list_for_each_entry(cache, &dev->cache_props, list)
518 kfd_remove_sysfs_file(cache->kobj,
522 kobject_del(dev->kobj_cache);
523 kobject_put(dev->kobj_cache);
524 dev->kobj_cache = NULL;
528 list_for_each_entry(mem, &dev->mem_props, list)
530 kfd_remove_sysfs_file(mem->kobj, &mem->attr);
533 kobject_del(dev->kobj_mem);
534 kobject_put(dev->kobj_mem);
535 dev->kobj_mem = NULL;
538 if (dev->kobj_perf) {
539 list_for_each_entry(perf, &dev->perf_props, list) {
540 kfree(perf->attr_group);
541 perf->attr_group = NULL;
543 kobject_del(dev->kobj_perf);
544 kobject_put(dev->kobj_perf);
545 dev->kobj_perf = NULL;
548 if (dev->kobj_node) {
549 sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
550 sysfs_remove_file(dev->kobj_node, &dev->attr_name);
551 sysfs_remove_file(dev->kobj_node, &dev->attr_props);
552 kobject_del(dev->kobj_node);
553 kobject_put(dev->kobj_node);
554 dev->kobj_node = NULL;
558 static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
561 struct kfd_iolink_properties *iolink;
562 struct kfd_cache_properties *cache;
563 struct kfd_mem_properties *mem;
564 struct kfd_perf_properties *perf;
566 uint32_t i, num_attrs;
567 struct attribute **attrs;
569 if (WARN_ON(dev->kobj_node))
573 * Creating the sysfs folders
575 dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
579 ret = kobject_init_and_add(dev->kobj_node, &node_type,
580 sys_props.kobj_nodes, "%d", id);
584 dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
588 dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
589 if (!dev->kobj_cache)
592 dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
593 if (!dev->kobj_iolink)
596 dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
601 * Creating sysfs files for node properties
603 dev->attr_gpuid.name = "gpu_id";
604 dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
605 sysfs_attr_init(&dev->attr_gpuid);
606 dev->attr_name.name = "name";
607 dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
608 sysfs_attr_init(&dev->attr_name);
609 dev->attr_props.name = "properties";
610 dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
611 sysfs_attr_init(&dev->attr_props);
612 ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
615 ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
618 ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
623 list_for_each_entry(mem, &dev->mem_props, list) {
624 mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
627 ret = kobject_init_and_add(mem->kobj, &mem_type,
628 dev->kobj_mem, "%d", i);
632 mem->attr.name = "properties";
633 mem->attr.mode = KFD_SYSFS_FILE_MODE;
634 sysfs_attr_init(&mem->attr);
635 ret = sysfs_create_file(mem->kobj, &mem->attr);
642 list_for_each_entry(cache, &dev->cache_props, list) {
643 cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
646 ret = kobject_init_and_add(cache->kobj, &cache_type,
647 dev->kobj_cache, "%d", i);
651 cache->attr.name = "properties";
652 cache->attr.mode = KFD_SYSFS_FILE_MODE;
653 sysfs_attr_init(&cache->attr);
654 ret = sysfs_create_file(cache->kobj, &cache->attr);
661 list_for_each_entry(iolink, &dev->io_link_props, list) {
662 iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
665 ret = kobject_init_and_add(iolink->kobj, &iolink_type,
666 dev->kobj_iolink, "%d", i);
670 iolink->attr.name = "properties";
671 iolink->attr.mode = KFD_SYSFS_FILE_MODE;
672 sysfs_attr_init(&iolink->attr);
673 ret = sysfs_create_file(iolink->kobj, &iolink->attr);
679 /* All hardware blocks have the same number of attributes. */
680 num_attrs = sizeof(perf_attr_iommu)/sizeof(struct kfd_perf_attr);
681 list_for_each_entry(perf, &dev->perf_props, list) {
682 perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
683 * num_attrs + sizeof(struct attribute_group),
685 if (!perf->attr_group)
688 attrs = (struct attribute **)(perf->attr_group + 1);
689 if (!strcmp(perf->block_name, "iommu")) {
690 /* Information of IOMMU's num_counters and counter_ids is shown
691 * under /sys/bus/event_source/devices/amd_iommu. We don't
694 perf_attr_iommu[0].data = perf->max_concurrent;
695 for (i = 0; i < num_attrs; i++)
696 attrs[i] = &perf_attr_iommu[i].attr.attr;
698 perf->attr_group->name = perf->block_name;
699 perf->attr_group->attrs = attrs;
700 ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
708 /* Called with write topology lock acquired */
709 static int kfd_build_sysfs_node_tree(void)
711 struct kfd_topology_device *dev;
715 list_for_each_entry(dev, &topology_device_list, list) {
716 ret = kfd_build_sysfs_node_entry(dev, i);
725 /* Called with write topology lock acquired */
726 static void kfd_remove_sysfs_node_tree(void)
728 struct kfd_topology_device *dev;
730 list_for_each_entry(dev, &topology_device_list, list)
731 kfd_remove_sysfs_node_entry(dev);
734 static int kfd_topology_update_sysfs(void)
738 pr_info("Creating topology SYSFS entries\n");
739 if (!sys_props.kobj_topology) {
740 sys_props.kobj_topology =
741 kfd_alloc_struct(sys_props.kobj_topology);
742 if (!sys_props.kobj_topology)
745 ret = kobject_init_and_add(sys_props.kobj_topology,
746 &sysprops_type, &kfd_device->kobj,
751 sys_props.kobj_nodes = kobject_create_and_add("nodes",
752 sys_props.kobj_topology);
753 if (!sys_props.kobj_nodes)
756 sys_props.attr_genid.name = "generation_id";
757 sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
758 sysfs_attr_init(&sys_props.attr_genid);
759 ret = sysfs_create_file(sys_props.kobj_topology,
760 &sys_props.attr_genid);
764 sys_props.attr_props.name = "system_properties";
765 sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
766 sysfs_attr_init(&sys_props.attr_props);
767 ret = sysfs_create_file(sys_props.kobj_topology,
768 &sys_props.attr_props);
773 kfd_remove_sysfs_node_tree();
775 return kfd_build_sysfs_node_tree();
778 static void kfd_topology_release_sysfs(void)
780 kfd_remove_sysfs_node_tree();
781 if (sys_props.kobj_topology) {
782 sysfs_remove_file(sys_props.kobj_topology,
783 &sys_props.attr_genid);
784 sysfs_remove_file(sys_props.kobj_topology,
785 &sys_props.attr_props);
786 if (sys_props.kobj_nodes) {
787 kobject_del(sys_props.kobj_nodes);
788 kobject_put(sys_props.kobj_nodes);
789 sys_props.kobj_nodes = NULL;
791 kobject_del(sys_props.kobj_topology);
792 kobject_put(sys_props.kobj_topology);
793 sys_props.kobj_topology = NULL;
797 /* Called with write topology_lock acquired */
798 static void kfd_topology_update_device_list(struct list_head *temp_list,
799 struct list_head *master_list)
801 while (!list_empty(temp_list)) {
802 list_move_tail(temp_list->next, master_list);
803 sys_props.num_devices++;
807 static void kfd_debug_print_topology(void)
809 struct kfd_topology_device *dev;
811 down_read(&topology_lock);
813 dev = list_last_entry(&topology_device_list,
814 struct kfd_topology_device, list);
816 if (dev->node_props.cpu_cores_count &&
817 dev->node_props.simd_count) {
818 pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
819 dev->node_props.device_id,
820 dev->node_props.vendor_id);
821 } else if (dev->node_props.cpu_cores_count)
822 pr_info("Topology: Add CPU node\n");
823 else if (dev->node_props.simd_count)
824 pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
825 dev->node_props.device_id,
826 dev->node_props.vendor_id);
828 up_read(&topology_lock);
831 /* Helper function for intializing platform_xx members of
832 * kfd_system_properties. Uses OEM info from the last CPU/APU node.
834 static void kfd_update_system_properties(void)
836 struct kfd_topology_device *dev;
838 down_read(&topology_lock);
839 dev = list_last_entry(&topology_device_list,
840 struct kfd_topology_device, list);
842 sys_props.platform_id =
843 (*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
844 sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
845 sys_props.platform_rev = dev->oem_revision;
847 up_read(&topology_lock);
850 static void find_system_memory(const struct dmi_header *dm,
853 struct kfd_mem_properties *mem;
854 u16 mem_width, mem_clock;
855 struct kfd_topology_device *kdev =
856 (struct kfd_topology_device *)private;
857 const u8 *dmi_data = (const u8 *)(dm + 1);
859 if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
860 mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
861 mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
862 list_for_each_entry(mem, &kdev->mem_props, list) {
863 if (mem_width != 0xFFFF && mem_width != 0)
864 mem->width = mem_width;
866 mem->mem_clk_max = mem_clock;
872 * Performance counters information is not part of CRAT but we would like to
873 * put them in the sysfs under topology directory for Thunk to get the data.
874 * This function is called before updating the sysfs.
876 static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
878 struct kfd_perf_properties *props;
880 if (amd_iommu_pc_supported()) {
881 props = kfd_alloc_struct(props);
884 strcpy(props->block_name, "iommu");
885 props->max_concurrent = amd_iommu_pc_get_max_banks(0) *
886 amd_iommu_pc_get_max_counters(0); /* assume one iommu */
887 list_add_tail(&props->list, &kdev->perf_props);
893 /* kfd_add_non_crat_information - Add information that is not currently
894 * defined in CRAT but is necessary for KFD topology
895 * @dev - topology device to which addition info is added
897 static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
899 /* Check if CPU only node. */
901 /* Add system memory information */
902 dmi_walk(find_system_memory, kdev);
904 /* TODO: For GPU node, rearrange code from kfd_topology_add_device */
907 /* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
908 * Ignore CRAT for all other devices. AMD APU is identified if both CPU
909 * and GPU cores are present.
910 * @device_list - topology device list created by parsing ACPI CRAT table.
911 * @return - TRUE if invalid, FALSE is valid.
913 static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
915 struct kfd_topology_device *dev;
917 list_for_each_entry(dev, device_list, list) {
918 if (dev->node_props.cpu_cores_count &&
919 dev->node_props.simd_count)
922 pr_info("Ignoring ACPI CRAT on non-APU system\n");
926 int kfd_topology_init(void)
928 void *crat_image = NULL;
929 size_t image_size = 0;
931 struct list_head temp_topology_device_list;
932 int cpu_only_node = 0;
933 struct kfd_topology_device *kdev;
934 int proximity_domain;
936 /* topology_device_list - Master list of all topology devices
937 * temp_topology_device_list - temporary list created while parsing CRAT
938 * or VCRAT. Once parsing is complete the contents of list is moved to
939 * topology_device_list
942 /* Initialize the head for the both the lists */
943 INIT_LIST_HEAD(&topology_device_list);
944 INIT_LIST_HEAD(&temp_topology_device_list);
945 init_rwsem(&topology_lock);
947 memset(&sys_props, 0, sizeof(sys_props));
949 /* Proximity domains in ACPI CRAT tables start counting at
950 * 0. The same should be true for virtual CRAT tables created
951 * at this stage. GPUs added later in kfd_topology_add_device
954 proximity_domain = 0;
957 * Get the CRAT image from the ACPI. If ACPI doesn't have one
958 * or if ACPI CRAT is invalid create a virtual CRAT.
959 * NOTE: The current implementation expects all AMD APUs to have
960 * CRAT. If no CRAT is available, it is assumed to be a CPU
962 ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
964 ret = kfd_parse_crat_table(crat_image,
965 &temp_topology_device_list,
968 kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
969 kfd_release_topology_device_list(
970 &temp_topology_device_list);
971 kfd_destroy_crat_image(crat_image);
977 ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
978 COMPUTE_UNIT_CPU, NULL,
982 pr_err("Error creating VCRAT table for CPU\n");
986 ret = kfd_parse_crat_table(crat_image,
987 &temp_topology_device_list,
990 pr_err("Error parsing VCRAT table for CPU\n");
995 kdev = list_first_entry(&temp_topology_device_list,
996 struct kfd_topology_device, list);
997 kfd_add_perf_to_topology(kdev);
999 down_write(&topology_lock);
1000 kfd_topology_update_device_list(&temp_topology_device_list,
1001 &topology_device_list);
1002 atomic_set(&topology_crat_proximity_domain, sys_props.num_devices-1);
1003 ret = kfd_topology_update_sysfs();
1004 up_write(&topology_lock);
1007 sys_props.generation_count++;
1008 kfd_update_system_properties();
1009 kfd_debug_print_topology();
1010 pr_info("Finished initializing topology\n");
1012 pr_err("Failed to update topology in sysfs ret=%d\n", ret);
1014 /* For nodes with GPU, this information gets added
1015 * when GPU is detected (kfd_topology_add_device).
1017 if (cpu_only_node) {
1018 /* Add additional information to CPU only node created above */
1019 down_write(&topology_lock);
1020 kdev = list_first_entry(&topology_device_list,
1021 struct kfd_topology_device, list);
1022 up_write(&topology_lock);
1023 kfd_add_non_crat_information(kdev);
1027 kfd_destroy_crat_image(crat_image);
1031 void kfd_topology_shutdown(void)
1033 down_write(&topology_lock);
1034 kfd_topology_release_sysfs();
1035 kfd_release_live_view();
1036 up_write(&topology_lock);
1039 static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
1043 uint64_t local_mem_size;
1045 struct kfd_local_mem_info local_mem_info;
1050 gpu->kfd2kgd->get_local_mem_info(gpu->kgd, &local_mem_info);
1052 local_mem_size = local_mem_info.local_mem_size_private +
1053 local_mem_info.local_mem_size_public;
1055 buf[0] = gpu->pdev->devfn;
1056 buf[1] = gpu->pdev->subsystem_vendor;
1057 buf[2] = gpu->pdev->subsystem_device;
1058 buf[3] = gpu->pdev->device;
1059 buf[4] = gpu->pdev->bus->number;
1060 buf[5] = lower_32_bits(local_mem_size);
1061 buf[6] = upper_32_bits(local_mem_size);
1063 for (i = 0, hashout = 0; i < 7; i++)
1064 hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
1068 /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
1069 * the GPU device is not already present in the topology device
1070 * list then return NULL. This means a new topology device has to
1071 * be created for this GPU.
1072 * TODO: Rather than assiging @gpu to first topology device withtout
1073 * gpu attached, it will better to have more stringent check.
1075 static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
1077 struct kfd_topology_device *dev;
1078 struct kfd_topology_device *out_dev = NULL;
1080 down_write(&topology_lock);
1081 list_for_each_entry(dev, &topology_device_list, list)
1082 if (!dev->gpu && (dev->node_props.simd_count > 0)) {
1087 up_write(&topology_lock);
1091 static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
1094 * TODO: Generate an event for thunk about the arrival/removal
1099 /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
1100 * patch this after CRAT parsing.
1102 static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
1104 struct kfd_mem_properties *mem;
1105 struct kfd_local_mem_info local_mem_info;
1110 /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
1111 * single bank of VRAM local memory.
1112 * for dGPUs - VCRAT reports only one bank of Local Memory
1113 * for APUs - If CRAT from ACPI reports more than one bank, then
1114 * all the banks will report the same mem_clk_max information
1116 dev->gpu->kfd2kgd->get_local_mem_info(dev->gpu->kgd,
1119 list_for_each_entry(mem, &dev->mem_props, list)
1120 mem->mem_clk_max = local_mem_info.mem_clk_max;
1123 static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
1125 struct kfd_iolink_properties *link;
1127 if (!dev || !dev->gpu)
1130 /* GPU only creates direck links so apply flags setting to all */
1131 if (dev->gpu->device_info->asic_family == CHIP_HAWAII)
1132 list_for_each_entry(link, &dev->io_link_props, list)
1133 link->flags = CRAT_IOLINK_FLAGS_ENABLED |
1134 CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1135 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1138 int kfd_topology_add_device(struct kfd_dev *gpu)
1141 struct kfd_topology_device *dev;
1142 struct kfd_cu_info cu_info;
1144 struct list_head temp_topology_device_list;
1145 void *crat_image = NULL;
1146 size_t image_size = 0;
1147 int proximity_domain;
1149 INIT_LIST_HEAD(&temp_topology_device_list);
1151 gpu_id = kfd_generate_gpu_id(gpu);
1153 pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1155 proximity_domain = atomic_inc_return(&topology_crat_proximity_domain);
1157 /* Check to see if this gpu device exists in the topology_device_list.
1158 * If so, assign the gpu to that device,
1159 * else create a Virtual CRAT for this gpu device and then parse that
1160 * CRAT to create a new topology device. Once created assign the gpu to
1161 * that topology device
1163 dev = kfd_assign_gpu(gpu);
1165 res = kfd_create_crat_image_virtual(&crat_image, &image_size,
1166 COMPUTE_UNIT_GPU, gpu,
1169 pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
1173 res = kfd_parse_crat_table(crat_image,
1174 &temp_topology_device_list,
1177 pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
1182 down_write(&topology_lock);
1183 kfd_topology_update_device_list(&temp_topology_device_list,
1184 &topology_device_list);
1186 /* Update the SYSFS tree, since we added another topology
1189 res = kfd_topology_update_sysfs();
1190 up_write(&topology_lock);
1193 sys_props.generation_count++;
1195 pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
1197 dev = kfd_assign_gpu(gpu);
1198 if (WARN_ON(!dev)) {
1204 dev->gpu_id = gpu_id;
1207 /* TODO: Move the following lines to function
1208 * kfd_add_non_crat_information
1211 /* Fill-in additional information that is not available in CRAT but
1212 * needed for the topology
1215 dev->gpu->kfd2kgd->get_cu_info(dev->gpu->kgd, &cu_info);
1216 dev->node_props.simd_arrays_per_engine =
1217 cu_info.num_shader_arrays_per_engine;
1219 dev->node_props.vendor_id = gpu->pdev->vendor;
1220 dev->node_props.device_id = gpu->pdev->device;
1221 dev->node_props.location_id = PCI_DEVID(gpu->pdev->bus->number,
1223 dev->node_props.max_engine_clk_fcompute =
1224 dev->gpu->kfd2kgd->get_max_engine_clock_in_mhz(dev->gpu->kgd);
1225 dev->node_props.max_engine_clk_ccompute =
1226 cpufreq_quick_get_max(0) / 1000;
1228 kfd_fill_mem_clk_max_info(dev);
1229 kfd_fill_iolink_non_crat_info(dev);
1231 switch (dev->gpu->device_info->asic_family) {
1235 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
1236 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1237 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1241 case CHIP_POLARIS10:
1242 case CHIP_POLARIS11:
1243 pr_debug("Adding doorbell packet type capability\n");
1244 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
1245 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1246 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1249 WARN(1, "Unexpected ASIC family %u",
1250 dev->gpu->device_info->asic_family);
1253 /* Fix errors in CZ CRAT.
1254 * simd_count: Carrizo CRAT reports wrong simd_count, probably
1255 * because it doesn't consider masked out CUs
1256 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
1257 * capability flag: Carrizo CRAT doesn't report IOMMU flags
1259 if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
1260 dev->node_props.simd_count =
1261 cu_info.simd_per_cu * cu_info.cu_active_number;
1262 dev->node_props.max_waves_per_simd = 10;
1263 dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
1266 kfd_debug_print_topology();
1269 kfd_notify_gpu_change(gpu_id, 1);
1271 kfd_destroy_crat_image(crat_image);
1275 int kfd_topology_remove_device(struct kfd_dev *gpu)
1277 struct kfd_topology_device *dev, *tmp;
1281 down_write(&topology_lock);
1283 list_for_each_entry_safe(dev, tmp, &topology_device_list, list)
1284 if (dev->gpu == gpu) {
1285 gpu_id = dev->gpu_id;
1286 kfd_remove_sysfs_node_entry(dev);
1287 kfd_release_topology_device(dev);
1288 sys_props.num_devices--;
1290 if (kfd_topology_update_sysfs() < 0)
1291 kfd_topology_release_sysfs();
1295 up_write(&topology_lock);
1298 kfd_notify_gpu_change(gpu_id, 0);
1303 /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
1304 * topology. If GPU device is found @idx, then valid kfd_dev pointer is
1305 * returned through @kdev
1306 * Return - 0: On success (@kdev will be NULL for non GPU nodes)
1307 * -1: If end of list
1309 int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
1312 struct kfd_topology_device *top_dev;
1313 uint8_t device_idx = 0;
1316 down_read(&topology_lock);
1318 list_for_each_entry(top_dev, &topology_device_list, list) {
1319 if (device_idx == idx) {
1320 *kdev = top_dev->gpu;
1321 up_read(&topology_lock);
1328 up_read(&topology_lock);
1334 static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
1336 const struct cpuinfo_x86 *cpuinfo;
1337 int first_cpu_of_numa_node;
1339 if (!cpumask || cpumask == cpu_none_mask)
1341 first_cpu_of_numa_node = cpumask_first(cpumask);
1342 if (first_cpu_of_numa_node >= nr_cpu_ids)
1344 cpuinfo = &cpu_data(first_cpu_of_numa_node);
1346 return cpuinfo->apicid;
1349 /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
1350 * of the given NUMA node (numa_node_id)
1351 * Return -1 on failure
1353 int kfd_numa_node_to_apic_id(int numa_node_id)
1355 if (numa_node_id == -1) {
1356 pr_warn("Invalid NUMA Node. Use online CPU mask\n");
1357 return kfd_cpumask_to_apic_id(cpu_online_mask);
1359 return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
1362 #if defined(CONFIG_DEBUG_FS)
1364 int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
1366 struct kfd_topology_device *dev;
1370 down_read(&topology_lock);
1372 list_for_each_entry(dev, &topology_device_list, list) {
1378 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1379 r = dqm_debugfs_hqds(m, dev->gpu->dqm);
1384 up_read(&topology_lock);
1389 int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
1391 struct kfd_topology_device *dev;
1395 down_read(&topology_lock);
1397 list_for_each_entry(dev, &topology_device_list, list) {
1403 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1404 r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets);
1409 up_read(&topology_lock);