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>
34 #include "kfd_topology.h"
35 #include "kfd_device_queue_manager.h"
37 static struct list_head topology_device_list;
38 static int topology_crat_parsed;
39 static struct kfd_system_properties sys_props;
41 static DECLARE_RWSEM(topology_lock);
43 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
45 struct kfd_topology_device *top_dev;
46 struct kfd_dev *device = NULL;
48 down_read(&topology_lock);
50 list_for_each_entry(top_dev, &topology_device_list, list)
51 if (top_dev->gpu_id == gpu_id) {
52 device = top_dev->gpu;
56 up_read(&topology_lock);
61 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
63 struct kfd_topology_device *top_dev;
64 struct kfd_dev *device = NULL;
66 down_read(&topology_lock);
68 list_for_each_entry(top_dev, &topology_device_list, list)
69 if (top_dev->gpu->pdev == pdev) {
70 device = top_dev->gpu;
74 up_read(&topology_lock);
79 static int kfd_topology_get_crat_acpi(void *crat_image, size_t *size)
81 struct acpi_table_header *crat_table;
88 * Fetch the CRAT table from ACPI
90 status = acpi_get_table(CRAT_SIGNATURE, 0, &crat_table);
91 if (status == AE_NOT_FOUND) {
92 pr_warn("CRAT table not found\n");
94 } else if (ACPI_FAILURE(status)) {
95 const char *err = acpi_format_exception(status);
97 pr_err("CRAT table error: %s\n", err);
101 if (*size >= crat_table->length && crat_image != NULL)
102 memcpy(crat_image, crat_table, crat_table->length);
104 *size = crat_table->length;
109 static void kfd_populated_cu_info_cpu(struct kfd_topology_device *dev,
110 struct crat_subtype_computeunit *cu)
112 dev->node_props.cpu_cores_count = cu->num_cpu_cores;
113 dev->node_props.cpu_core_id_base = cu->processor_id_low;
114 if (cu->hsa_capability & CRAT_CU_FLAGS_IOMMU_PRESENT)
115 dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
117 pr_info("CU CPU: cores=%d id_base=%d\n", cu->num_cpu_cores,
118 cu->processor_id_low);
121 static void kfd_populated_cu_info_gpu(struct kfd_topology_device *dev,
122 struct crat_subtype_computeunit *cu)
124 dev->node_props.simd_id_base = cu->processor_id_low;
125 dev->node_props.simd_count = cu->num_simd_cores;
126 dev->node_props.lds_size_in_kb = cu->lds_size_in_kb;
127 dev->node_props.max_waves_per_simd = cu->max_waves_simd;
128 dev->node_props.wave_front_size = cu->wave_front_size;
129 dev->node_props.mem_banks_count = cu->num_banks;
130 dev->node_props.array_count = cu->num_arrays;
131 dev->node_props.cu_per_simd_array = cu->num_cu_per_array;
132 dev->node_props.simd_per_cu = cu->num_simd_per_cu;
133 dev->node_props.max_slots_scratch_cu = cu->max_slots_scatch_cu;
134 if (cu->hsa_capability & CRAT_CU_FLAGS_HOT_PLUGGABLE)
135 dev->node_props.capability |= HSA_CAP_HOT_PLUGGABLE;
136 pr_info("CU GPU: simds=%d id_base=%d\n", cu->num_simd_cores,
137 cu->processor_id_low);
140 /* kfd_parse_subtype_cu is called when the topology mutex is already acquired */
141 static int kfd_parse_subtype_cu(struct crat_subtype_computeunit *cu)
143 struct kfd_topology_device *dev;
146 pr_info("Found CU entry in CRAT table with proximity_domain=%d caps=%x\n",
147 cu->proximity_domain, cu->hsa_capability);
148 list_for_each_entry(dev, &topology_device_list, list) {
149 if (cu->proximity_domain == i) {
150 if (cu->flags & CRAT_CU_FLAGS_CPU_PRESENT)
151 kfd_populated_cu_info_cpu(dev, cu);
153 if (cu->flags & CRAT_CU_FLAGS_GPU_PRESENT)
154 kfd_populated_cu_info_gpu(dev, cu);
164 * kfd_parse_subtype_mem is called when the topology mutex is
167 static int kfd_parse_subtype_mem(struct crat_subtype_memory *mem)
169 struct kfd_mem_properties *props;
170 struct kfd_topology_device *dev;
173 pr_info("Found memory entry in CRAT table with proximity_domain=%d\n",
174 mem->promixity_domain);
175 list_for_each_entry(dev, &topology_device_list, list) {
176 if (mem->promixity_domain == i) {
177 props = kfd_alloc_struct(props);
181 if (dev->node_props.cpu_cores_count == 0)
182 props->heap_type = HSA_MEM_HEAP_TYPE_FB_PRIVATE;
184 props->heap_type = HSA_MEM_HEAP_TYPE_SYSTEM;
186 if (mem->flags & CRAT_MEM_FLAGS_HOT_PLUGGABLE)
187 props->flags |= HSA_MEM_FLAGS_HOT_PLUGGABLE;
188 if (mem->flags & CRAT_MEM_FLAGS_NON_VOLATILE)
189 props->flags |= HSA_MEM_FLAGS_NON_VOLATILE;
191 props->size_in_bytes =
192 ((uint64_t)mem->length_high << 32) +
194 props->width = mem->width;
196 dev->mem_bank_count++;
197 list_add_tail(&props->list, &dev->mem_props);
208 * kfd_parse_subtype_cache is called when the topology mutex
209 * is already acquired
211 static int kfd_parse_subtype_cache(struct crat_subtype_cache *cache)
213 struct kfd_cache_properties *props;
214 struct kfd_topology_device *dev;
217 id = cache->processor_id_low;
219 pr_info("Found cache entry in CRAT table with processor_id=%d\n", id);
220 list_for_each_entry(dev, &topology_device_list, list)
221 if (id == dev->node_props.cpu_core_id_base ||
222 id == dev->node_props.simd_id_base) {
223 props = kfd_alloc_struct(props);
227 props->processor_id_low = id;
228 props->cache_level = cache->cache_level;
229 props->cache_size = cache->cache_size;
230 props->cacheline_size = cache->cache_line_size;
231 props->cachelines_per_tag = cache->lines_per_tag;
232 props->cache_assoc = cache->associativity;
233 props->cache_latency = cache->cache_latency;
235 if (cache->flags & CRAT_CACHE_FLAGS_DATA_CACHE)
236 props->cache_type |= HSA_CACHE_TYPE_DATA;
237 if (cache->flags & CRAT_CACHE_FLAGS_INST_CACHE)
238 props->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
239 if (cache->flags & CRAT_CACHE_FLAGS_CPU_CACHE)
240 props->cache_type |= HSA_CACHE_TYPE_CPU;
241 if (cache->flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
242 props->cache_type |= HSA_CACHE_TYPE_HSACU;
245 dev->node_props.caches_count++;
246 list_add_tail(&props->list, &dev->cache_props);
255 * kfd_parse_subtype_iolink is called when the topology mutex
256 * is already acquired
258 static int kfd_parse_subtype_iolink(struct crat_subtype_iolink *iolink)
260 struct kfd_iolink_properties *props;
261 struct kfd_topology_device *dev;
266 id_from = iolink->proximity_domain_from;
267 id_to = iolink->proximity_domain_to;
269 pr_info("Found IO link entry in CRAT table with id_from=%d\n", id_from);
270 list_for_each_entry(dev, &topology_device_list, list) {
272 props = kfd_alloc_struct(props);
276 props->node_from = id_from;
277 props->node_to = id_to;
278 props->ver_maj = iolink->version_major;
279 props->ver_min = iolink->version_minor;
282 * weight factor (derived from CDIR), currently always 1
286 props->min_latency = iolink->minimum_latency;
287 props->max_latency = iolink->maximum_latency;
288 props->min_bandwidth = iolink->minimum_bandwidth_mbs;
289 props->max_bandwidth = iolink->maximum_bandwidth_mbs;
290 props->rec_transfer_size =
291 iolink->recommended_transfer_size;
293 dev->io_link_count++;
294 dev->node_props.io_links_count++;
295 list_add_tail(&props->list, &dev->io_link_props);
305 static int kfd_parse_subtype(struct crat_subtype_generic *sub_type_hdr)
307 struct crat_subtype_computeunit *cu;
308 struct crat_subtype_memory *mem;
309 struct crat_subtype_cache *cache;
310 struct crat_subtype_iolink *iolink;
313 switch (sub_type_hdr->type) {
314 case CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY:
315 cu = (struct crat_subtype_computeunit *)sub_type_hdr;
316 ret = kfd_parse_subtype_cu(cu);
318 case CRAT_SUBTYPE_MEMORY_AFFINITY:
319 mem = (struct crat_subtype_memory *)sub_type_hdr;
320 ret = kfd_parse_subtype_mem(mem);
322 case CRAT_SUBTYPE_CACHE_AFFINITY:
323 cache = (struct crat_subtype_cache *)sub_type_hdr;
324 ret = kfd_parse_subtype_cache(cache);
326 case CRAT_SUBTYPE_TLB_AFFINITY:
328 * For now, nothing to do here
330 pr_info("Found TLB entry in CRAT table (not processing)\n");
332 case CRAT_SUBTYPE_CCOMPUTE_AFFINITY:
334 * For now, nothing to do here
336 pr_info("Found CCOMPUTE entry in CRAT table (not processing)\n");
338 case CRAT_SUBTYPE_IOLINK_AFFINITY:
339 iolink = (struct crat_subtype_iolink *)sub_type_hdr;
340 ret = kfd_parse_subtype_iolink(iolink);
343 pr_warn("Unknown subtype (%d) in CRAT\n",
350 static void kfd_release_topology_device(struct kfd_topology_device *dev)
352 struct kfd_mem_properties *mem;
353 struct kfd_cache_properties *cache;
354 struct kfd_iolink_properties *iolink;
356 list_del(&dev->list);
358 while (dev->mem_props.next != &dev->mem_props) {
359 mem = container_of(dev->mem_props.next,
360 struct kfd_mem_properties, list);
361 list_del(&mem->list);
365 while (dev->cache_props.next != &dev->cache_props) {
366 cache = container_of(dev->cache_props.next,
367 struct kfd_cache_properties, list);
368 list_del(&cache->list);
372 while (dev->io_link_props.next != &dev->io_link_props) {
373 iolink = container_of(dev->io_link_props.next,
374 struct kfd_iolink_properties, list);
375 list_del(&iolink->list);
381 sys_props.num_devices--;
384 static void kfd_release_live_view(void)
386 struct kfd_topology_device *dev;
388 while (topology_device_list.next != &topology_device_list) {
389 dev = container_of(topology_device_list.next,
390 struct kfd_topology_device, list);
391 kfd_release_topology_device(dev);
394 memset(&sys_props, 0, sizeof(sys_props));
397 static struct kfd_topology_device *kfd_create_topology_device(void)
399 struct kfd_topology_device *dev;
401 dev = kfd_alloc_struct(dev);
403 pr_err("No memory to allocate a topology device");
407 INIT_LIST_HEAD(&dev->mem_props);
408 INIT_LIST_HEAD(&dev->cache_props);
409 INIT_LIST_HEAD(&dev->io_link_props);
411 list_add_tail(&dev->list, &topology_device_list);
412 sys_props.num_devices++;
417 static int kfd_parse_crat_table(void *crat_image)
419 struct kfd_topology_device *top_dev;
420 struct crat_subtype_generic *sub_type_hdr;
423 struct crat_header *crat_table = (struct crat_header *)crat_image;
430 num_nodes = crat_table->num_domains;
431 image_len = crat_table->length;
433 pr_info("Parsing CRAT table with %d nodes\n", num_nodes);
435 for (node_id = 0; node_id < num_nodes; node_id++) {
436 top_dev = kfd_create_topology_device();
438 kfd_release_live_view();
443 sys_props.platform_id =
444 (*((uint64_t *)crat_table->oem_id)) & CRAT_OEMID_64BIT_MASK;
445 sys_props.platform_oem = *((uint64_t *)crat_table->oem_table_id);
446 sys_props.platform_rev = crat_table->revision;
448 sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
449 while ((char *)sub_type_hdr + sizeof(struct crat_subtype_generic) <
450 ((char *)crat_image) + image_len) {
451 if (sub_type_hdr->flags & CRAT_SUBTYPE_FLAGS_ENABLED) {
452 ret = kfd_parse_subtype(sub_type_hdr);
454 kfd_release_live_view();
459 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
460 sub_type_hdr->length);
463 sys_props.generation_count++;
464 topology_crat_parsed = 1;
470 #define sysfs_show_gen_prop(buffer, fmt, ...) \
471 snprintf(buffer, PAGE_SIZE, "%s"fmt, buffer, __VA_ARGS__)
472 #define sysfs_show_32bit_prop(buffer, name, value) \
473 sysfs_show_gen_prop(buffer, "%s %u\n", name, value)
474 #define sysfs_show_64bit_prop(buffer, name, value) \
475 sysfs_show_gen_prop(buffer, "%s %llu\n", name, value)
476 #define sysfs_show_32bit_val(buffer, value) \
477 sysfs_show_gen_prop(buffer, "%u\n", value)
478 #define sysfs_show_str_val(buffer, value) \
479 sysfs_show_gen_prop(buffer, "%s\n", value)
481 static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
486 /* Making sure that the buffer is an empty string */
489 if (attr == &sys_props.attr_genid) {
490 ret = sysfs_show_32bit_val(buffer, sys_props.generation_count);
491 } else if (attr == &sys_props.attr_props) {
492 sysfs_show_64bit_prop(buffer, "platform_oem",
493 sys_props.platform_oem);
494 sysfs_show_64bit_prop(buffer, "platform_id",
495 sys_props.platform_id);
496 ret = sysfs_show_64bit_prop(buffer, "platform_rev",
497 sys_props.platform_rev);
505 static const struct sysfs_ops sysprops_ops = {
506 .show = sysprops_show,
509 static struct kobj_type sysprops_type = {
510 .sysfs_ops = &sysprops_ops,
513 static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
517 struct kfd_iolink_properties *iolink;
519 /* Making sure that the buffer is an empty string */
522 iolink = container_of(attr, struct kfd_iolink_properties, attr);
523 sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
524 sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
525 sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
526 sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
527 sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
528 sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
529 sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
530 sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
531 sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
532 sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
533 sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
534 iolink->rec_transfer_size);
535 ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
540 static const struct sysfs_ops iolink_ops = {
544 static struct kobj_type iolink_type = {
545 .sysfs_ops = &iolink_ops,
548 static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
552 struct kfd_mem_properties *mem;
554 /* Making sure that the buffer is an empty string */
557 mem = container_of(attr, struct kfd_mem_properties, attr);
558 sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
559 sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
560 sysfs_show_32bit_prop(buffer, "flags", mem->flags);
561 sysfs_show_32bit_prop(buffer, "width", mem->width);
562 ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
567 static const struct sysfs_ops mem_ops = {
571 static struct kobj_type mem_type = {
572 .sysfs_ops = &mem_ops,
575 static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
580 struct kfd_cache_properties *cache;
582 /* Making sure that the buffer is an empty string */
585 cache = container_of(attr, struct kfd_cache_properties, attr);
586 sysfs_show_32bit_prop(buffer, "processor_id_low",
587 cache->processor_id_low);
588 sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
589 sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
590 sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
591 sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
592 cache->cachelines_per_tag);
593 sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
594 sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
595 sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
596 snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
597 for (i = 0; i < KFD_TOPOLOGY_CPU_SIBLINGS; i++)
598 ret = snprintf(buffer, PAGE_SIZE, "%s%d%s",
599 buffer, cache->sibling_map[i],
600 (i == KFD_TOPOLOGY_CPU_SIBLINGS-1) ?
606 static const struct sysfs_ops cache_ops = {
607 .show = kfd_cache_show,
610 static struct kobj_type cache_type = {
611 .sysfs_ops = &cache_ops,
614 static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
617 struct kfd_topology_device *dev;
618 char public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE];
620 uint32_t log_max_watch_addr;
622 /* Making sure that the buffer is an empty string */
625 if (strcmp(attr->name, "gpu_id") == 0) {
626 dev = container_of(attr, struct kfd_topology_device,
628 return sysfs_show_32bit_val(buffer, dev->gpu_id);
631 if (strcmp(attr->name, "name") == 0) {
632 dev = container_of(attr, struct kfd_topology_device,
634 for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE; i++) {
636 (char)dev->node_props.marketing_name[i];
637 if (dev->node_props.marketing_name[i] == 0)
640 public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1] = 0x0;
641 return sysfs_show_str_val(buffer, public_name);
644 dev = container_of(attr, struct kfd_topology_device,
646 sysfs_show_32bit_prop(buffer, "cpu_cores_count",
647 dev->node_props.cpu_cores_count);
648 sysfs_show_32bit_prop(buffer, "simd_count",
649 dev->node_props.simd_count);
651 if (dev->mem_bank_count < dev->node_props.mem_banks_count) {
652 pr_info_once("mem_banks_count truncated from %d to %d\n",
653 dev->node_props.mem_banks_count,
654 dev->mem_bank_count);
655 sysfs_show_32bit_prop(buffer, "mem_banks_count",
656 dev->mem_bank_count);
658 sysfs_show_32bit_prop(buffer, "mem_banks_count",
659 dev->node_props.mem_banks_count);
662 sysfs_show_32bit_prop(buffer, "caches_count",
663 dev->node_props.caches_count);
664 sysfs_show_32bit_prop(buffer, "io_links_count",
665 dev->node_props.io_links_count);
666 sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
667 dev->node_props.cpu_core_id_base);
668 sysfs_show_32bit_prop(buffer, "simd_id_base",
669 dev->node_props.simd_id_base);
670 sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
671 dev->node_props.max_waves_per_simd);
672 sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
673 dev->node_props.lds_size_in_kb);
674 sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
675 dev->node_props.gds_size_in_kb);
676 sysfs_show_32bit_prop(buffer, "wave_front_size",
677 dev->node_props.wave_front_size);
678 sysfs_show_32bit_prop(buffer, "array_count",
679 dev->node_props.array_count);
680 sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
681 dev->node_props.simd_arrays_per_engine);
682 sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
683 dev->node_props.cu_per_simd_array);
684 sysfs_show_32bit_prop(buffer, "simd_per_cu",
685 dev->node_props.simd_per_cu);
686 sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
687 dev->node_props.max_slots_scratch_cu);
688 sysfs_show_32bit_prop(buffer, "vendor_id",
689 dev->node_props.vendor_id);
690 sysfs_show_32bit_prop(buffer, "device_id",
691 dev->node_props.device_id);
692 sysfs_show_32bit_prop(buffer, "location_id",
693 dev->node_props.location_id);
697 __ilog2_u32(dev->gpu->device_info->num_of_watch_points);
699 if (log_max_watch_addr) {
700 dev->node_props.capability |=
701 HSA_CAP_WATCH_POINTS_SUPPORTED;
703 dev->node_props.capability |=
704 ((log_max_watch_addr <<
705 HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
706 HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
709 sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
710 dev->gpu->kfd2kgd->get_max_engine_clock_in_mhz(
713 sysfs_show_64bit_prop(buffer, "local_mem_size",
714 (unsigned long long int) 0);
716 sysfs_show_32bit_prop(buffer, "fw_version",
717 dev->gpu->kfd2kgd->get_fw_version(
720 sysfs_show_32bit_prop(buffer, "capability",
721 dev->node_props.capability);
724 return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
725 cpufreq_quick_get_max(0)/1000);
728 static const struct sysfs_ops node_ops = {
732 static struct kobj_type node_type = {
733 .sysfs_ops = &node_ops,
736 static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
738 sysfs_remove_file(kobj, attr);
743 static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
745 struct kfd_iolink_properties *iolink;
746 struct kfd_cache_properties *cache;
747 struct kfd_mem_properties *mem;
749 if (dev->kobj_iolink) {
750 list_for_each_entry(iolink, &dev->io_link_props, list)
752 kfd_remove_sysfs_file(iolink->kobj,
756 kobject_del(dev->kobj_iolink);
757 kobject_put(dev->kobj_iolink);
758 dev->kobj_iolink = NULL;
761 if (dev->kobj_cache) {
762 list_for_each_entry(cache, &dev->cache_props, list)
764 kfd_remove_sysfs_file(cache->kobj,
768 kobject_del(dev->kobj_cache);
769 kobject_put(dev->kobj_cache);
770 dev->kobj_cache = NULL;
774 list_for_each_entry(mem, &dev->mem_props, list)
776 kfd_remove_sysfs_file(mem->kobj, &mem->attr);
779 kobject_del(dev->kobj_mem);
780 kobject_put(dev->kobj_mem);
781 dev->kobj_mem = NULL;
784 if (dev->kobj_node) {
785 sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
786 sysfs_remove_file(dev->kobj_node, &dev->attr_name);
787 sysfs_remove_file(dev->kobj_node, &dev->attr_props);
788 kobject_del(dev->kobj_node);
789 kobject_put(dev->kobj_node);
790 dev->kobj_node = NULL;
794 static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
797 struct kfd_iolink_properties *iolink;
798 struct kfd_cache_properties *cache;
799 struct kfd_mem_properties *mem;
803 if (WARN_ON(dev->kobj_node))
807 * Creating the sysfs folders
809 dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
813 ret = kobject_init_and_add(dev->kobj_node, &node_type,
814 sys_props.kobj_nodes, "%d", id);
818 dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
822 dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
823 if (!dev->kobj_cache)
826 dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
827 if (!dev->kobj_iolink)
831 * Creating sysfs files for node properties
833 dev->attr_gpuid.name = "gpu_id";
834 dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
835 sysfs_attr_init(&dev->attr_gpuid);
836 dev->attr_name.name = "name";
837 dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
838 sysfs_attr_init(&dev->attr_name);
839 dev->attr_props.name = "properties";
840 dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
841 sysfs_attr_init(&dev->attr_props);
842 ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
845 ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
848 ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
853 list_for_each_entry(mem, &dev->mem_props, list) {
854 mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
857 ret = kobject_init_and_add(mem->kobj, &mem_type,
858 dev->kobj_mem, "%d", i);
862 mem->attr.name = "properties";
863 mem->attr.mode = KFD_SYSFS_FILE_MODE;
864 sysfs_attr_init(&mem->attr);
865 ret = sysfs_create_file(mem->kobj, &mem->attr);
872 list_for_each_entry(cache, &dev->cache_props, list) {
873 cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
876 ret = kobject_init_and_add(cache->kobj, &cache_type,
877 dev->kobj_cache, "%d", i);
881 cache->attr.name = "properties";
882 cache->attr.mode = KFD_SYSFS_FILE_MODE;
883 sysfs_attr_init(&cache->attr);
884 ret = sysfs_create_file(cache->kobj, &cache->attr);
891 list_for_each_entry(iolink, &dev->io_link_props, list) {
892 iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
895 ret = kobject_init_and_add(iolink->kobj, &iolink_type,
896 dev->kobj_iolink, "%d", i);
900 iolink->attr.name = "properties";
901 iolink->attr.mode = KFD_SYSFS_FILE_MODE;
902 sysfs_attr_init(&iolink->attr);
903 ret = sysfs_create_file(iolink->kobj, &iolink->attr);
912 static int kfd_build_sysfs_node_tree(void)
914 struct kfd_topology_device *dev;
918 list_for_each_entry(dev, &topology_device_list, list) {
919 ret = kfd_build_sysfs_node_entry(dev, i);
928 static void kfd_remove_sysfs_node_tree(void)
930 struct kfd_topology_device *dev;
932 list_for_each_entry(dev, &topology_device_list, list)
933 kfd_remove_sysfs_node_entry(dev);
936 static int kfd_topology_update_sysfs(void)
940 pr_info("Creating topology SYSFS entries\n");
941 if (!sys_props.kobj_topology) {
942 sys_props.kobj_topology =
943 kfd_alloc_struct(sys_props.kobj_topology);
944 if (!sys_props.kobj_topology)
947 ret = kobject_init_and_add(sys_props.kobj_topology,
948 &sysprops_type, &kfd_device->kobj,
953 sys_props.kobj_nodes = kobject_create_and_add("nodes",
954 sys_props.kobj_topology);
955 if (!sys_props.kobj_nodes)
958 sys_props.attr_genid.name = "generation_id";
959 sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
960 sysfs_attr_init(&sys_props.attr_genid);
961 ret = sysfs_create_file(sys_props.kobj_topology,
962 &sys_props.attr_genid);
966 sys_props.attr_props.name = "system_properties";
967 sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
968 sysfs_attr_init(&sys_props.attr_props);
969 ret = sysfs_create_file(sys_props.kobj_topology,
970 &sys_props.attr_props);
975 kfd_remove_sysfs_node_tree();
977 return kfd_build_sysfs_node_tree();
980 static void kfd_topology_release_sysfs(void)
982 kfd_remove_sysfs_node_tree();
983 if (sys_props.kobj_topology) {
984 sysfs_remove_file(sys_props.kobj_topology,
985 &sys_props.attr_genid);
986 sysfs_remove_file(sys_props.kobj_topology,
987 &sys_props.attr_props);
988 if (sys_props.kobj_nodes) {
989 kobject_del(sys_props.kobj_nodes);
990 kobject_put(sys_props.kobj_nodes);
991 sys_props.kobj_nodes = NULL;
993 kobject_del(sys_props.kobj_topology);
994 kobject_put(sys_props.kobj_topology);
995 sys_props.kobj_topology = NULL;
999 int kfd_topology_init(void)
1001 void *crat_image = NULL;
1002 size_t image_size = 0;
1006 * Initialize the head for the topology device list
1008 INIT_LIST_HEAD(&topology_device_list);
1009 init_rwsem(&topology_lock);
1010 topology_crat_parsed = 0;
1012 memset(&sys_props, 0, sizeof(sys_props));
1015 * Get the CRAT image from the ACPI
1017 ret = kfd_topology_get_crat_acpi(crat_image, &image_size);
1018 if (ret == 0 && image_size > 0) {
1019 pr_info("Found CRAT image with size=%zd\n", image_size);
1020 crat_image = kmalloc(image_size, GFP_KERNEL);
1023 pr_err("No memory for allocating CRAT image\n");
1026 ret = kfd_topology_get_crat_acpi(crat_image, &image_size);
1029 down_write(&topology_lock);
1030 ret = kfd_parse_crat_table(crat_image);
1032 ret = kfd_topology_update_sysfs();
1033 up_write(&topology_lock);
1035 pr_err("Couldn't get CRAT table size from ACPI\n");
1038 } else if (ret == -ENODATA) {
1041 pr_err("Couldn't get CRAT table size from ACPI\n");
1045 pr_info("Finished initializing topology ret=%d\n", ret);
1049 void kfd_topology_shutdown(void)
1051 kfd_topology_release_sysfs();
1052 kfd_release_live_view();
1055 static void kfd_debug_print_topology(void)
1057 struct kfd_topology_device *dev;
1060 pr_info("DEBUG PRINT OF TOPOLOGY:");
1061 list_for_each_entry(dev, &topology_device_list, list) {
1062 pr_info("Node: %d\n", i);
1063 pr_info("\tGPU assigned: %s\n", (dev->gpu ? "yes" : "no"));
1064 pr_info("\tCPU count: %d\n", dev->node_props.cpu_cores_count);
1065 pr_info("\tSIMD count: %d", dev->node_props.simd_count);
1070 static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
1074 uint64_t local_mem_size;
1080 local_mem_size = gpu->kfd2kgd->get_vmem_size(gpu->kgd);
1082 buf[0] = gpu->pdev->devfn;
1083 buf[1] = gpu->pdev->subsystem_vendor;
1084 buf[2] = gpu->pdev->subsystem_device;
1085 buf[3] = gpu->pdev->device;
1086 buf[4] = gpu->pdev->bus->number;
1087 buf[5] = lower_32_bits(local_mem_size);
1088 buf[6] = upper_32_bits(local_mem_size);
1090 for (i = 0, hashout = 0; i < 7; i++)
1091 hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
1096 static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
1098 struct kfd_topology_device *dev;
1099 struct kfd_topology_device *out_dev = NULL;
1101 list_for_each_entry(dev, &topology_device_list, list)
1102 if (!dev->gpu && (dev->node_props.simd_count > 0)) {
1111 static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
1114 * TODO: Generate an event for thunk about the arrival/removal
1119 int kfd_topology_add_device(struct kfd_dev *gpu)
1122 struct kfd_topology_device *dev;
1125 gpu_id = kfd_generate_gpu_id(gpu);
1127 pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1129 down_write(&topology_lock);
1131 * Try to assign the GPU to existing topology device (generated from
1134 dev = kfd_assign_gpu(gpu);
1136 pr_info("GPU was not found in the current topology. Extending.\n");
1137 kfd_debug_print_topology();
1138 dev = kfd_create_topology_device();
1146 * TODO: Make a call to retrieve topology information from the
1150 /* Update the SYSFS tree, since we added another topology
1153 if (kfd_topology_update_sysfs() < 0)
1154 kfd_topology_release_sysfs();
1158 dev->gpu_id = gpu_id;
1160 dev->node_props.vendor_id = gpu->pdev->vendor;
1161 dev->node_props.device_id = gpu->pdev->device;
1162 dev->node_props.location_id = (gpu->pdev->bus->number << 24) +
1163 (gpu->pdev->devfn & 0xffffff);
1165 * TODO: Retrieve max engine clock values from KGD
1168 if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
1169 dev->node_props.capability |= HSA_CAP_DOORBELL_PACKET_TYPE;
1170 pr_info("Adding doorbell packet type capability\n");
1176 up_write(&topology_lock);
1179 kfd_notify_gpu_change(gpu_id, 1);
1184 int kfd_topology_remove_device(struct kfd_dev *gpu)
1186 struct kfd_topology_device *dev;
1190 down_write(&topology_lock);
1192 list_for_each_entry(dev, &topology_device_list, list)
1193 if (dev->gpu == gpu) {
1194 gpu_id = dev->gpu_id;
1195 kfd_remove_sysfs_node_entry(dev);
1196 kfd_release_topology_device(dev);
1198 if (kfd_topology_update_sysfs() < 0)
1199 kfd_topology_release_sysfs();
1203 up_write(&topology_lock);
1206 kfd_notify_gpu_change(gpu_id, 0);
1212 * When idx is out of bounds, the function will return NULL
1214 struct kfd_dev *kfd_topology_enum_kfd_devices(uint8_t idx)
1217 struct kfd_topology_device *top_dev;
1218 struct kfd_dev *device = NULL;
1219 uint8_t device_idx = 0;
1221 down_read(&topology_lock);
1223 list_for_each_entry(top_dev, &topology_device_list, list) {
1224 if (device_idx == idx) {
1225 device = top_dev->gpu;
1232 up_read(&topology_lock);
1238 #if defined(CONFIG_DEBUG_FS)
1240 int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
1242 struct kfd_topology_device *dev;
1246 down_read(&topology_lock);
1248 list_for_each_entry(dev, &topology_device_list, list) {
1254 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1255 r = dqm_debugfs_hqds(m, dev->gpu->dqm);
1260 up_read(&topology_lock);
1265 int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
1267 struct kfd_topology_device *dev;
1271 down_read(&topology_lock);
1273 list_for_each_entry(dev, &topology_device_list, list) {
1279 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1280 r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets);
1285 up_read(&topology_lock);