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>
33 #include "kfd_topology.h"
35 static struct list_head topology_device_list;
36 static int topology_crat_parsed;
37 static struct kfd_system_properties sys_props;
39 static DECLARE_RWSEM(topology_lock);
41 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
43 struct kfd_topology_device *top_dev;
44 struct kfd_dev *device = NULL;
46 down_read(&topology_lock);
48 list_for_each_entry(top_dev, &topology_device_list, list)
49 if (top_dev->gpu_id == gpu_id) {
50 device = top_dev->gpu;
54 up_read(&topology_lock);
59 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
61 struct kfd_topology_device *top_dev;
62 struct kfd_dev *device = NULL;
64 down_read(&topology_lock);
66 list_for_each_entry(top_dev, &topology_device_list, list)
67 if (top_dev->gpu->pdev == pdev) {
68 device = top_dev->gpu;
72 up_read(&topology_lock);
77 static int kfd_topology_get_crat_acpi(void *crat_image, size_t *size)
79 struct acpi_table_header *crat_table;
86 * Fetch the CRAT table from ACPI
88 status = acpi_get_table(CRAT_SIGNATURE, 0, &crat_table);
89 if (status == AE_NOT_FOUND) {
90 pr_warn("CRAT table not found\n");
92 } else if (ACPI_FAILURE(status)) {
93 const char *err = acpi_format_exception(status);
95 pr_err("CRAT table error: %s\n", err);
99 if (*size >= crat_table->length && crat_image != NULL)
100 memcpy(crat_image, crat_table, crat_table->length);
102 *size = crat_table->length;
107 static void kfd_populated_cu_info_cpu(struct kfd_topology_device *dev,
108 struct crat_subtype_computeunit *cu)
113 dev->node_props.cpu_cores_count = cu->num_cpu_cores;
114 dev->node_props.cpu_core_id_base = cu->processor_id_low;
115 if (cu->hsa_capability & CRAT_CU_FLAGS_IOMMU_PRESENT)
116 dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
118 pr_info("CU CPU: cores=%d id_base=%d\n", cu->num_cpu_cores,
119 cu->processor_id_low);
122 static void kfd_populated_cu_info_gpu(struct kfd_topology_device *dev,
123 struct crat_subtype_computeunit *cu)
128 dev->node_props.simd_id_base = cu->processor_id_low;
129 dev->node_props.simd_count = cu->num_simd_cores;
130 dev->node_props.lds_size_in_kb = cu->lds_size_in_kb;
131 dev->node_props.max_waves_per_simd = cu->max_waves_simd;
132 dev->node_props.wave_front_size = cu->wave_front_size;
133 dev->node_props.mem_banks_count = cu->num_banks;
134 dev->node_props.array_count = cu->num_arrays;
135 dev->node_props.cu_per_simd_array = cu->num_cu_per_array;
136 dev->node_props.simd_per_cu = cu->num_simd_per_cu;
137 dev->node_props.max_slots_scratch_cu = cu->max_slots_scatch_cu;
138 if (cu->hsa_capability & CRAT_CU_FLAGS_HOT_PLUGGABLE)
139 dev->node_props.capability |= HSA_CAP_HOT_PLUGGABLE;
140 pr_info("CU GPU: simds=%d id_base=%d\n", cu->num_simd_cores,
141 cu->processor_id_low);
144 /* kfd_parse_subtype_cu is called when the topology mutex is already acquired */
145 static int kfd_parse_subtype_cu(struct crat_subtype_computeunit *cu)
147 struct kfd_topology_device *dev;
152 pr_info("Found CU entry in CRAT table with proximity_domain=%d caps=%x\n",
153 cu->proximity_domain, cu->hsa_capability);
154 list_for_each_entry(dev, &topology_device_list, list) {
155 if (cu->proximity_domain == i) {
156 if (cu->flags & CRAT_CU_FLAGS_CPU_PRESENT)
157 kfd_populated_cu_info_cpu(dev, cu);
159 if (cu->flags & CRAT_CU_FLAGS_GPU_PRESENT)
160 kfd_populated_cu_info_gpu(dev, cu);
170 * kfd_parse_subtype_mem is called when the topology mutex is
173 static int kfd_parse_subtype_mem(struct crat_subtype_memory *mem)
175 struct kfd_mem_properties *props;
176 struct kfd_topology_device *dev;
181 pr_info("Found memory entry in CRAT table with proximity_domain=%d\n",
182 mem->promixity_domain);
183 list_for_each_entry(dev, &topology_device_list, list) {
184 if (mem->promixity_domain == i) {
185 props = kfd_alloc_struct(props);
189 if (dev->node_props.cpu_cores_count == 0)
190 props->heap_type = HSA_MEM_HEAP_TYPE_FB_PRIVATE;
192 props->heap_type = HSA_MEM_HEAP_TYPE_SYSTEM;
194 if (mem->flags & CRAT_MEM_FLAGS_HOT_PLUGGABLE)
195 props->flags |= HSA_MEM_FLAGS_HOT_PLUGGABLE;
196 if (mem->flags & CRAT_MEM_FLAGS_NON_VOLATILE)
197 props->flags |= HSA_MEM_FLAGS_NON_VOLATILE;
199 props->size_in_bytes =
200 ((uint64_t)mem->length_high << 32) +
202 props->width = mem->width;
204 dev->mem_bank_count++;
205 list_add_tail(&props->list, &dev->mem_props);
216 * kfd_parse_subtype_cache is called when the topology mutex
217 * is already acquired
219 static int kfd_parse_subtype_cache(struct crat_subtype_cache *cache)
221 struct kfd_cache_properties *props;
222 struct kfd_topology_device *dev;
227 id = cache->processor_id_low;
229 pr_info("Found cache entry in CRAT table with processor_id=%d\n", id);
230 list_for_each_entry(dev, &topology_device_list, list)
231 if (id == dev->node_props.cpu_core_id_base ||
232 id == dev->node_props.simd_id_base) {
233 props = kfd_alloc_struct(props);
237 props->processor_id_low = id;
238 props->cache_level = cache->cache_level;
239 props->cache_size = cache->cache_size;
240 props->cacheline_size = cache->cache_line_size;
241 props->cachelines_per_tag = cache->lines_per_tag;
242 props->cache_assoc = cache->associativity;
243 props->cache_latency = cache->cache_latency;
245 if (cache->flags & CRAT_CACHE_FLAGS_DATA_CACHE)
246 props->cache_type |= HSA_CACHE_TYPE_DATA;
247 if (cache->flags & CRAT_CACHE_FLAGS_INST_CACHE)
248 props->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
249 if (cache->flags & CRAT_CACHE_FLAGS_CPU_CACHE)
250 props->cache_type |= HSA_CACHE_TYPE_CPU;
251 if (cache->flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
252 props->cache_type |= HSA_CACHE_TYPE_HSACU;
255 dev->node_props.caches_count++;
256 list_add_tail(&props->list, &dev->cache_props);
265 * kfd_parse_subtype_iolink is called when the topology mutex
266 * is already acquired
268 static int kfd_parse_subtype_iolink(struct crat_subtype_iolink *iolink)
270 struct kfd_iolink_properties *props;
271 struct kfd_topology_device *dev;
278 id_from = iolink->proximity_domain_from;
279 id_to = iolink->proximity_domain_to;
281 pr_info("Found IO link entry in CRAT table with id_from=%d\n", id_from);
282 list_for_each_entry(dev, &topology_device_list, list) {
284 props = kfd_alloc_struct(props);
288 props->node_from = id_from;
289 props->node_to = id_to;
290 props->ver_maj = iolink->version_major;
291 props->ver_min = iolink->version_minor;
294 * weight factor (derived from CDIR), currently always 1
298 props->min_latency = iolink->minimum_latency;
299 props->max_latency = iolink->maximum_latency;
300 props->min_bandwidth = iolink->minimum_bandwidth_mbs;
301 props->max_bandwidth = iolink->maximum_bandwidth_mbs;
302 props->rec_transfer_size =
303 iolink->recommended_transfer_size;
305 dev->io_link_count++;
306 dev->node_props.io_links_count++;
307 list_add_tail(&props->list, &dev->io_link_props);
317 static int kfd_parse_subtype(struct crat_subtype_generic *sub_type_hdr)
319 struct crat_subtype_computeunit *cu;
320 struct crat_subtype_memory *mem;
321 struct crat_subtype_cache *cache;
322 struct crat_subtype_iolink *iolink;
325 BUG_ON(!sub_type_hdr);
327 switch (sub_type_hdr->type) {
328 case CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY:
329 cu = (struct crat_subtype_computeunit *)sub_type_hdr;
330 ret = kfd_parse_subtype_cu(cu);
332 case CRAT_SUBTYPE_MEMORY_AFFINITY:
333 mem = (struct crat_subtype_memory *)sub_type_hdr;
334 ret = kfd_parse_subtype_mem(mem);
336 case CRAT_SUBTYPE_CACHE_AFFINITY:
337 cache = (struct crat_subtype_cache *)sub_type_hdr;
338 ret = kfd_parse_subtype_cache(cache);
340 case CRAT_SUBTYPE_TLB_AFFINITY:
342 * For now, nothing to do here
344 pr_info("Found TLB entry in CRAT table (not processing)\n");
346 case CRAT_SUBTYPE_CCOMPUTE_AFFINITY:
348 * For now, nothing to do here
350 pr_info("Found CCOMPUTE entry in CRAT table (not processing)\n");
352 case CRAT_SUBTYPE_IOLINK_AFFINITY:
353 iolink = (struct crat_subtype_iolink *)sub_type_hdr;
354 ret = kfd_parse_subtype_iolink(iolink);
357 pr_warn("Unknown subtype (%d) in CRAT\n",
364 static void kfd_release_topology_device(struct kfd_topology_device *dev)
366 struct kfd_mem_properties *mem;
367 struct kfd_cache_properties *cache;
368 struct kfd_iolink_properties *iolink;
372 list_del(&dev->list);
374 while (dev->mem_props.next != &dev->mem_props) {
375 mem = container_of(dev->mem_props.next,
376 struct kfd_mem_properties, list);
377 list_del(&mem->list);
381 while (dev->cache_props.next != &dev->cache_props) {
382 cache = container_of(dev->cache_props.next,
383 struct kfd_cache_properties, list);
384 list_del(&cache->list);
388 while (dev->io_link_props.next != &dev->io_link_props) {
389 iolink = container_of(dev->io_link_props.next,
390 struct kfd_iolink_properties, list);
391 list_del(&iolink->list);
397 sys_props.num_devices--;
400 static void kfd_release_live_view(void)
402 struct kfd_topology_device *dev;
404 while (topology_device_list.next != &topology_device_list) {
405 dev = container_of(topology_device_list.next,
406 struct kfd_topology_device, list);
407 kfd_release_topology_device(dev);
410 memset(&sys_props, 0, sizeof(sys_props));
413 static struct kfd_topology_device *kfd_create_topology_device(void)
415 struct kfd_topology_device *dev;
417 dev = kfd_alloc_struct(dev);
419 pr_err("No memory to allocate a topology device");
423 INIT_LIST_HEAD(&dev->mem_props);
424 INIT_LIST_HEAD(&dev->cache_props);
425 INIT_LIST_HEAD(&dev->io_link_props);
427 list_add_tail(&dev->list, &topology_device_list);
428 sys_props.num_devices++;
433 static int kfd_parse_crat_table(void *crat_image)
435 struct kfd_topology_device *top_dev;
436 struct crat_subtype_generic *sub_type_hdr;
439 struct crat_header *crat_table = (struct crat_header *)crat_image;
446 num_nodes = crat_table->num_domains;
447 image_len = crat_table->length;
449 pr_info("Parsing CRAT table with %d nodes\n", num_nodes);
451 for (node_id = 0; node_id < num_nodes; node_id++) {
452 top_dev = kfd_create_topology_device();
454 kfd_release_live_view();
459 sys_props.platform_id =
460 (*((uint64_t *)crat_table->oem_id)) & CRAT_OEMID_64BIT_MASK;
461 sys_props.platform_oem = *((uint64_t *)crat_table->oem_table_id);
462 sys_props.platform_rev = crat_table->revision;
464 sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
465 while ((char *)sub_type_hdr + sizeof(struct crat_subtype_generic) <
466 ((char *)crat_image) + image_len) {
467 if (sub_type_hdr->flags & CRAT_SUBTYPE_FLAGS_ENABLED) {
468 ret = kfd_parse_subtype(sub_type_hdr);
470 kfd_release_live_view();
475 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
476 sub_type_hdr->length);
479 sys_props.generation_count++;
480 topology_crat_parsed = 1;
486 #define sysfs_show_gen_prop(buffer, fmt, ...) \
487 snprintf(buffer, PAGE_SIZE, "%s"fmt, buffer, __VA_ARGS__)
488 #define sysfs_show_32bit_prop(buffer, name, value) \
489 sysfs_show_gen_prop(buffer, "%s %u\n", name, value)
490 #define sysfs_show_64bit_prop(buffer, name, value) \
491 sysfs_show_gen_prop(buffer, "%s %llu\n", name, value)
492 #define sysfs_show_32bit_val(buffer, value) \
493 sysfs_show_gen_prop(buffer, "%u\n", value)
494 #define sysfs_show_str_val(buffer, value) \
495 sysfs_show_gen_prop(buffer, "%s\n", value)
497 static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
502 /* Making sure that the buffer is an empty string */
505 if (attr == &sys_props.attr_genid) {
506 ret = sysfs_show_32bit_val(buffer, sys_props.generation_count);
507 } else if (attr == &sys_props.attr_props) {
508 sysfs_show_64bit_prop(buffer, "platform_oem",
509 sys_props.platform_oem);
510 sysfs_show_64bit_prop(buffer, "platform_id",
511 sys_props.platform_id);
512 ret = sysfs_show_64bit_prop(buffer, "platform_rev",
513 sys_props.platform_rev);
521 static const struct sysfs_ops sysprops_ops = {
522 .show = sysprops_show,
525 static struct kobj_type sysprops_type = {
526 .sysfs_ops = &sysprops_ops,
529 static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
533 struct kfd_iolink_properties *iolink;
535 /* Making sure that the buffer is an empty string */
538 iolink = container_of(attr, struct kfd_iolink_properties, attr);
539 sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
540 sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
541 sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
542 sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
543 sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
544 sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
545 sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
546 sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
547 sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
548 sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
549 sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
550 iolink->rec_transfer_size);
551 ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
556 static const struct sysfs_ops iolink_ops = {
560 static struct kobj_type iolink_type = {
561 .sysfs_ops = &iolink_ops,
564 static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
568 struct kfd_mem_properties *mem;
570 /* Making sure that the buffer is an empty string */
573 mem = container_of(attr, struct kfd_mem_properties, attr);
574 sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
575 sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
576 sysfs_show_32bit_prop(buffer, "flags", mem->flags);
577 sysfs_show_32bit_prop(buffer, "width", mem->width);
578 ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
583 static const struct sysfs_ops mem_ops = {
587 static struct kobj_type mem_type = {
588 .sysfs_ops = &mem_ops,
591 static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
596 struct kfd_cache_properties *cache;
598 /* Making sure that the buffer is an empty string */
601 cache = container_of(attr, struct kfd_cache_properties, attr);
602 sysfs_show_32bit_prop(buffer, "processor_id_low",
603 cache->processor_id_low);
604 sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
605 sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
606 sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
607 sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
608 cache->cachelines_per_tag);
609 sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
610 sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
611 sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
612 snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
613 for (i = 0; i < KFD_TOPOLOGY_CPU_SIBLINGS; i++)
614 ret = snprintf(buffer, PAGE_SIZE, "%s%d%s",
615 buffer, cache->sibling_map[i],
616 (i == KFD_TOPOLOGY_CPU_SIBLINGS-1) ?
622 static const struct sysfs_ops cache_ops = {
623 .show = kfd_cache_show,
626 static struct kobj_type cache_type = {
627 .sysfs_ops = &cache_ops,
630 static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
634 struct kfd_topology_device *dev;
635 char public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE];
638 /* Making sure that the buffer is an empty string */
641 if (strcmp(attr->name, "gpu_id") == 0) {
642 dev = container_of(attr, struct kfd_topology_device,
644 ret = sysfs_show_32bit_val(buffer, dev->gpu_id);
645 } else if (strcmp(attr->name, "name") == 0) {
646 dev = container_of(attr, struct kfd_topology_device,
648 for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE; i++) {
650 (char)dev->node_props.marketing_name[i];
651 if (dev->node_props.marketing_name[i] == 0)
654 public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1] = 0x0;
655 ret = sysfs_show_str_val(buffer, public_name);
657 dev = container_of(attr, struct kfd_topology_device,
659 sysfs_show_32bit_prop(buffer, "cpu_cores_count",
660 dev->node_props.cpu_cores_count);
661 sysfs_show_32bit_prop(buffer, "simd_count",
662 dev->node_props.simd_count);
664 if (dev->mem_bank_count < dev->node_props.mem_banks_count) {
665 pr_warn("kfd: mem_banks_count truncated from %d to %d\n",
666 dev->node_props.mem_banks_count,
667 dev->mem_bank_count);
668 sysfs_show_32bit_prop(buffer, "mem_banks_count",
669 dev->mem_bank_count);
671 sysfs_show_32bit_prop(buffer, "mem_banks_count",
672 dev->node_props.mem_banks_count);
675 sysfs_show_32bit_prop(buffer, "caches_count",
676 dev->node_props.caches_count);
677 sysfs_show_32bit_prop(buffer, "io_links_count",
678 dev->node_props.io_links_count);
679 sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
680 dev->node_props.cpu_core_id_base);
681 sysfs_show_32bit_prop(buffer, "simd_id_base",
682 dev->node_props.simd_id_base);
683 sysfs_show_32bit_prop(buffer, "capability",
684 dev->node_props.capability);
685 sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
686 dev->node_props.max_waves_per_simd);
687 sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
688 dev->node_props.lds_size_in_kb);
689 sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
690 dev->node_props.gds_size_in_kb);
691 sysfs_show_32bit_prop(buffer, "wave_front_size",
692 dev->node_props.wave_front_size);
693 sysfs_show_32bit_prop(buffer, "array_count",
694 dev->node_props.array_count);
695 sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
696 dev->node_props.simd_arrays_per_engine);
697 sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
698 dev->node_props.cu_per_simd_array);
699 sysfs_show_32bit_prop(buffer, "simd_per_cu",
700 dev->node_props.simd_per_cu);
701 sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
702 dev->node_props.max_slots_scratch_cu);
703 sysfs_show_32bit_prop(buffer, "vendor_id",
704 dev->node_props.vendor_id);
705 sysfs_show_32bit_prop(buffer, "device_id",
706 dev->node_props.device_id);
707 sysfs_show_32bit_prop(buffer, "location_id",
708 dev->node_props.location_id);
711 sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
712 kfd2kgd->get_max_engine_clock_in_mhz(
714 sysfs_show_64bit_prop(buffer, "local_mem_size",
715 kfd2kgd->get_vmem_size(dev->gpu->kgd));
717 sysfs_show_32bit_prop(buffer, "fw_version",
718 kfd2kgd->get_fw_version(
724 ret = sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
725 cpufreq_quick_get_max(0)/1000);
731 static const struct sysfs_ops node_ops = {
735 static struct kobj_type node_type = {
736 .sysfs_ops = &node_ops,
739 static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
741 sysfs_remove_file(kobj, attr);
746 static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
748 struct kfd_iolink_properties *iolink;
749 struct kfd_cache_properties *cache;
750 struct kfd_mem_properties *mem;
754 if (dev->kobj_iolink) {
755 list_for_each_entry(iolink, &dev->io_link_props, list)
757 kfd_remove_sysfs_file(iolink->kobj,
761 kobject_del(dev->kobj_iolink);
762 kobject_put(dev->kobj_iolink);
763 dev->kobj_iolink = NULL;
766 if (dev->kobj_cache) {
767 list_for_each_entry(cache, &dev->cache_props, list)
769 kfd_remove_sysfs_file(cache->kobj,
773 kobject_del(dev->kobj_cache);
774 kobject_put(dev->kobj_cache);
775 dev->kobj_cache = NULL;
779 list_for_each_entry(mem, &dev->mem_props, list)
781 kfd_remove_sysfs_file(mem->kobj, &mem->attr);
784 kobject_del(dev->kobj_mem);
785 kobject_put(dev->kobj_mem);
786 dev->kobj_mem = NULL;
789 if (dev->kobj_node) {
790 sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
791 sysfs_remove_file(dev->kobj_node, &dev->attr_name);
792 sysfs_remove_file(dev->kobj_node, &dev->attr_props);
793 kobject_del(dev->kobj_node);
794 kobject_put(dev->kobj_node);
795 dev->kobj_node = NULL;
799 static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
802 struct kfd_iolink_properties *iolink;
803 struct kfd_cache_properties *cache;
804 struct kfd_mem_properties *mem;
811 * Creating the sysfs folders
813 BUG_ON(dev->kobj_node);
814 dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
818 ret = kobject_init_and_add(dev->kobj_node, &node_type,
819 sys_props.kobj_nodes, "%d", id);
823 dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
827 dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
828 if (!dev->kobj_cache)
831 dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
832 if (!dev->kobj_iolink)
836 * Creating sysfs files for node properties
838 dev->attr_gpuid.name = "gpu_id";
839 dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
840 sysfs_attr_init(&dev->attr_gpuid);
841 dev->attr_name.name = "name";
842 dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
843 sysfs_attr_init(&dev->attr_name);
844 dev->attr_props.name = "properties";
845 dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
846 sysfs_attr_init(&dev->attr_props);
847 ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
850 ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
853 ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
858 list_for_each_entry(mem, &dev->mem_props, list) {
859 mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
862 ret = kobject_init_and_add(mem->kobj, &mem_type,
863 dev->kobj_mem, "%d", i);
867 mem->attr.name = "properties";
868 mem->attr.mode = KFD_SYSFS_FILE_MODE;
869 sysfs_attr_init(&mem->attr);
870 ret = sysfs_create_file(mem->kobj, &mem->attr);
877 list_for_each_entry(cache, &dev->cache_props, list) {
878 cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
881 ret = kobject_init_and_add(cache->kobj, &cache_type,
882 dev->kobj_cache, "%d", i);
886 cache->attr.name = "properties";
887 cache->attr.mode = KFD_SYSFS_FILE_MODE;
888 sysfs_attr_init(&cache->attr);
889 ret = sysfs_create_file(cache->kobj, &cache->attr);
896 list_for_each_entry(iolink, &dev->io_link_props, list) {
897 iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
900 ret = kobject_init_and_add(iolink->kobj, &iolink_type,
901 dev->kobj_iolink, "%d", i);
905 iolink->attr.name = "properties";
906 iolink->attr.mode = KFD_SYSFS_FILE_MODE;
907 sysfs_attr_init(&iolink->attr);
908 ret = sysfs_create_file(iolink->kobj, &iolink->attr);
917 static int kfd_build_sysfs_node_tree(void)
919 struct kfd_topology_device *dev;
923 list_for_each_entry(dev, &topology_device_list, list) {
924 ret = kfd_build_sysfs_node_entry(dev, 0);
933 static void kfd_remove_sysfs_node_tree(void)
935 struct kfd_topology_device *dev;
937 list_for_each_entry(dev, &topology_device_list, list)
938 kfd_remove_sysfs_node_entry(dev);
941 static int kfd_topology_update_sysfs(void)
945 pr_info("Creating topology SYSFS entries\n");
946 if (sys_props.kobj_topology == NULL) {
947 sys_props.kobj_topology =
948 kfd_alloc_struct(sys_props.kobj_topology);
949 if (!sys_props.kobj_topology)
952 ret = kobject_init_and_add(sys_props.kobj_topology,
953 &sysprops_type, &kfd_device->kobj,
958 sys_props.kobj_nodes = kobject_create_and_add("nodes",
959 sys_props.kobj_topology);
960 if (!sys_props.kobj_nodes)
963 sys_props.attr_genid.name = "generation_id";
964 sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
965 sysfs_attr_init(&sys_props.attr_genid);
966 ret = sysfs_create_file(sys_props.kobj_topology,
967 &sys_props.attr_genid);
971 sys_props.attr_props.name = "system_properties";
972 sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
973 sysfs_attr_init(&sys_props.attr_props);
974 ret = sysfs_create_file(sys_props.kobj_topology,
975 &sys_props.attr_props);
980 kfd_remove_sysfs_node_tree();
982 return kfd_build_sysfs_node_tree();
985 static void kfd_topology_release_sysfs(void)
987 kfd_remove_sysfs_node_tree();
988 if (sys_props.kobj_topology) {
989 sysfs_remove_file(sys_props.kobj_topology,
990 &sys_props.attr_genid);
991 sysfs_remove_file(sys_props.kobj_topology,
992 &sys_props.attr_props);
993 if (sys_props.kobj_nodes) {
994 kobject_del(sys_props.kobj_nodes);
995 kobject_put(sys_props.kobj_nodes);
996 sys_props.kobj_nodes = NULL;
998 kobject_del(sys_props.kobj_topology);
999 kobject_put(sys_props.kobj_topology);
1000 sys_props.kobj_topology = NULL;
1004 int kfd_topology_init(void)
1006 void *crat_image = NULL;
1007 size_t image_size = 0;
1011 * Initialize the head for the topology device list
1013 INIT_LIST_HEAD(&topology_device_list);
1014 init_rwsem(&topology_lock);
1015 topology_crat_parsed = 0;
1017 memset(&sys_props, 0, sizeof(sys_props));
1020 * Get the CRAT image from the ACPI
1022 ret = kfd_topology_get_crat_acpi(crat_image, &image_size);
1023 if (ret == 0 && image_size > 0) {
1024 pr_info("Found CRAT image with size=%zd\n", image_size);
1025 crat_image = kmalloc(image_size, GFP_KERNEL);
1028 pr_err("No memory for allocating CRAT image\n");
1031 ret = kfd_topology_get_crat_acpi(crat_image, &image_size);
1034 down_write(&topology_lock);
1035 ret = kfd_parse_crat_table(crat_image);
1037 ret = kfd_topology_update_sysfs();
1038 up_write(&topology_lock);
1040 pr_err("Couldn't get CRAT table size from ACPI\n");
1043 } else if (ret == -ENODATA) {
1046 pr_err("Couldn't get CRAT table size from ACPI\n");
1050 pr_info("Finished initializing topology ret=%d\n", ret);
1054 void kfd_topology_shutdown(void)
1056 kfd_topology_release_sysfs();
1057 kfd_release_live_view();
1060 static void kfd_debug_print_topology(void)
1062 struct kfd_topology_device *dev;
1065 pr_info("DEBUG PRINT OF TOPOLOGY:");
1066 list_for_each_entry(dev, &topology_device_list, list) {
1067 pr_info("Node: %d\n", i);
1068 pr_info("\tGPU assigned: %s\n", (dev->gpu ? "yes" : "no"));
1069 pr_info("\tCPU count: %d\n", dev->node_props.cpu_cores_count);
1070 pr_info("\tSIMD count: %d", dev->node_props.simd_count);
1075 static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
1084 buf[0] = gpu->pdev->devfn;
1085 buf[1] = gpu->pdev->subsystem_vendor;
1086 buf[2] = gpu->pdev->subsystem_device;
1087 buf[3] = gpu->pdev->device;
1088 buf[4] = gpu->pdev->bus->number;
1089 buf[5] = (uint32_t)(kfd2kgd->get_vmem_size(gpu->kgd) & 0xffffffff);
1090 buf[6] = (uint32_t)(kfd2kgd->get_vmem_size(gpu->kgd) >> 32);
1092 for (i = 0, hashout = 0; i < 7; i++)
1093 hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
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;
1105 list_for_each_entry(dev, &topology_device_list, list)
1106 if (dev->gpu == NULL && dev->node_props.simd_count > 0) {
1115 static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
1118 * TODO: Generate an event for thunk about the arrival/removal
1123 int kfd_topology_add_device(struct kfd_dev *gpu)
1126 struct kfd_topology_device *dev;
1131 gpu_id = kfd_generate_gpu_id(gpu);
1133 pr_debug("kfd: Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1135 down_write(&topology_lock);
1137 * Try to assign the GPU to existing topology device (generated from
1140 dev = kfd_assign_gpu(gpu);
1142 pr_info("GPU was not found in the current topology. Extending.\n");
1143 kfd_debug_print_topology();
1144 dev = kfd_create_topology_device();
1152 * TODO: Make a call to retrieve topology information from the
1157 * Update the SYSFS tree, since we added another topology device
1159 if (kfd_topology_update_sysfs() < 0)
1160 kfd_topology_release_sysfs();
1164 dev->gpu_id = gpu_id;
1166 dev->node_props.vendor_id = gpu->pdev->vendor;
1167 dev->node_props.device_id = gpu->pdev->device;
1168 dev->node_props.location_id = (gpu->pdev->bus->number << 24) +
1169 (gpu->pdev->devfn & 0xffffff);
1171 * TODO: Retrieve max engine clock values from KGD
1177 up_write(&topology_lock);
1180 kfd_notify_gpu_change(gpu_id, 1);
1185 int kfd_topology_remove_device(struct kfd_dev *gpu)
1187 struct kfd_topology_device *dev;
1193 down_write(&topology_lock);
1195 list_for_each_entry(dev, &topology_device_list, list)
1196 if (dev->gpu == gpu) {
1197 gpu_id = dev->gpu_id;
1198 kfd_remove_sysfs_node_entry(dev);
1199 kfd_release_topology_device(dev);
1201 if (kfd_topology_update_sysfs() < 0)
1202 kfd_topology_release_sysfs();
1206 up_write(&topology_lock);
1209 kfd_notify_gpu_change(gpu_id, 0);
1215 * When idx is out of bounds, the function will return NULL
1217 struct kfd_dev *kfd_topology_enum_kfd_devices(uint8_t idx)
1220 struct kfd_topology_device *top_dev;
1221 struct kfd_dev *device = NULL;
1222 uint8_t device_idx = 0;
1224 down_read(&topology_lock);
1226 list_for_each_entry(top_dev, &topology_device_list, list) {
1227 if (device_idx == idx) {
1228 device = top_dev->gpu;
1235 up_read(&topology_lock);