treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / amd / amdkfd / kfd_topology.c
blob203c823d65f158bc6a211895273b6f33263feb49
1 /*
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>
34 #include "kfd_priv.h"
35 #include "kfd_crat.h"
36 #include "kfd_topology.h"
37 #include "kfd_device_queue_manager.h"
38 #include "kfd_iommu.h"
39 #include "amdgpu_amdkfd.h"
40 #include "amdgpu_ras.h"
42 /* topology_device_list - Master list of all topology devices */
43 static struct list_head topology_device_list;
44 static struct kfd_system_properties sys_props;
46 static DECLARE_RWSEM(topology_lock);
47 static atomic_t topology_crat_proximity_domain;
49 struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
50 uint32_t proximity_domain)
52 struct kfd_topology_device *top_dev;
53 struct kfd_topology_device *device = NULL;
55 down_read(&topology_lock);
57 list_for_each_entry(top_dev, &topology_device_list, list)
58 if (top_dev->proximity_domain == proximity_domain) {
59 device = top_dev;
60 break;
63 up_read(&topology_lock);
65 return device;
68 struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
70 struct kfd_topology_device *top_dev = NULL;
71 struct kfd_topology_device *ret = NULL;
73 down_read(&topology_lock);
75 list_for_each_entry(top_dev, &topology_device_list, list)
76 if (top_dev->gpu_id == gpu_id) {
77 ret = top_dev;
78 break;
81 up_read(&topology_lock);
83 return ret;
86 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
88 struct kfd_topology_device *top_dev;
90 top_dev = kfd_topology_device_by_id(gpu_id);
91 if (!top_dev)
92 return NULL;
94 return top_dev->gpu;
97 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
99 struct kfd_topology_device *top_dev;
100 struct kfd_dev *device = NULL;
102 down_read(&topology_lock);
104 list_for_each_entry(top_dev, &topology_device_list, list)
105 if (top_dev->gpu && top_dev->gpu->pdev == pdev) {
106 device = top_dev->gpu;
107 break;
110 up_read(&topology_lock);
112 return device;
115 struct kfd_dev *kfd_device_by_kgd(const struct kgd_dev *kgd)
117 struct kfd_topology_device *top_dev;
118 struct kfd_dev *device = NULL;
120 down_read(&topology_lock);
122 list_for_each_entry(top_dev, &topology_device_list, list)
123 if (top_dev->gpu && top_dev->gpu->kgd == kgd) {
124 device = top_dev->gpu;
125 break;
128 up_read(&topology_lock);
130 return device;
133 /* Called with write topology_lock acquired */
134 static void kfd_release_topology_device(struct kfd_topology_device *dev)
136 struct kfd_mem_properties *mem;
137 struct kfd_cache_properties *cache;
138 struct kfd_iolink_properties *iolink;
139 struct kfd_perf_properties *perf;
141 list_del(&dev->list);
143 while (dev->mem_props.next != &dev->mem_props) {
144 mem = container_of(dev->mem_props.next,
145 struct kfd_mem_properties, list);
146 list_del(&mem->list);
147 kfree(mem);
150 while (dev->cache_props.next != &dev->cache_props) {
151 cache = container_of(dev->cache_props.next,
152 struct kfd_cache_properties, list);
153 list_del(&cache->list);
154 kfree(cache);
157 while (dev->io_link_props.next != &dev->io_link_props) {
158 iolink = container_of(dev->io_link_props.next,
159 struct kfd_iolink_properties, list);
160 list_del(&iolink->list);
161 kfree(iolink);
164 while (dev->perf_props.next != &dev->perf_props) {
165 perf = container_of(dev->perf_props.next,
166 struct kfd_perf_properties, list);
167 list_del(&perf->list);
168 kfree(perf);
171 kfree(dev);
174 void kfd_release_topology_device_list(struct list_head *device_list)
176 struct kfd_topology_device *dev;
178 while (!list_empty(device_list)) {
179 dev = list_first_entry(device_list,
180 struct kfd_topology_device, list);
181 kfd_release_topology_device(dev);
185 static void kfd_release_live_view(void)
187 kfd_release_topology_device_list(&topology_device_list);
188 memset(&sys_props, 0, sizeof(sys_props));
191 struct kfd_topology_device *kfd_create_topology_device(
192 struct list_head *device_list)
194 struct kfd_topology_device *dev;
196 dev = kfd_alloc_struct(dev);
197 if (!dev) {
198 pr_err("No memory to allocate a topology device");
199 return NULL;
202 INIT_LIST_HEAD(&dev->mem_props);
203 INIT_LIST_HEAD(&dev->cache_props);
204 INIT_LIST_HEAD(&dev->io_link_props);
205 INIT_LIST_HEAD(&dev->perf_props);
207 list_add_tail(&dev->list, device_list);
209 return dev;
213 #define sysfs_show_gen_prop(buffer, fmt, ...) \
214 snprintf(buffer, PAGE_SIZE, "%s"fmt, buffer, __VA_ARGS__)
215 #define sysfs_show_32bit_prop(buffer, name, value) \
216 sysfs_show_gen_prop(buffer, "%s %u\n", name, value)
217 #define sysfs_show_64bit_prop(buffer, name, value) \
218 sysfs_show_gen_prop(buffer, "%s %llu\n", name, value)
219 #define sysfs_show_32bit_val(buffer, value) \
220 sysfs_show_gen_prop(buffer, "%u\n", value)
221 #define sysfs_show_str_val(buffer, value) \
222 sysfs_show_gen_prop(buffer, "%s\n", value)
224 static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
225 char *buffer)
227 ssize_t ret;
229 /* Making sure that the buffer is an empty string */
230 buffer[0] = 0;
232 if (attr == &sys_props.attr_genid) {
233 ret = sysfs_show_32bit_val(buffer, sys_props.generation_count);
234 } else if (attr == &sys_props.attr_props) {
235 sysfs_show_64bit_prop(buffer, "platform_oem",
236 sys_props.platform_oem);
237 sysfs_show_64bit_prop(buffer, "platform_id",
238 sys_props.platform_id);
239 ret = sysfs_show_64bit_prop(buffer, "platform_rev",
240 sys_props.platform_rev);
241 } else {
242 ret = -EINVAL;
245 return ret;
248 static void kfd_topology_kobj_release(struct kobject *kobj)
250 kfree(kobj);
253 static const struct sysfs_ops sysprops_ops = {
254 .show = sysprops_show,
257 static struct kobj_type sysprops_type = {
258 .release = kfd_topology_kobj_release,
259 .sysfs_ops = &sysprops_ops,
262 static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
263 char *buffer)
265 ssize_t ret;
266 struct kfd_iolink_properties *iolink;
268 /* Making sure that the buffer is an empty string */
269 buffer[0] = 0;
271 iolink = container_of(attr, struct kfd_iolink_properties, attr);
272 if (iolink->gpu && kfd_devcgroup_check_permission(iolink->gpu))
273 return -EPERM;
274 sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
275 sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
276 sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
277 sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
278 sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
279 sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
280 sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
281 sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
282 sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
283 sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
284 sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
285 iolink->rec_transfer_size);
286 ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
288 return ret;
291 static const struct sysfs_ops iolink_ops = {
292 .show = iolink_show,
295 static struct kobj_type iolink_type = {
296 .release = kfd_topology_kobj_release,
297 .sysfs_ops = &iolink_ops,
300 static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
301 char *buffer)
303 ssize_t ret;
304 struct kfd_mem_properties *mem;
306 /* Making sure that the buffer is an empty string */
307 buffer[0] = 0;
309 mem = container_of(attr, struct kfd_mem_properties, attr);
310 if (mem->gpu && kfd_devcgroup_check_permission(mem->gpu))
311 return -EPERM;
312 sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
313 sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
314 sysfs_show_32bit_prop(buffer, "flags", mem->flags);
315 sysfs_show_32bit_prop(buffer, "width", mem->width);
316 ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
318 return ret;
321 static const struct sysfs_ops mem_ops = {
322 .show = mem_show,
325 static struct kobj_type mem_type = {
326 .release = kfd_topology_kobj_release,
327 .sysfs_ops = &mem_ops,
330 static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
331 char *buffer)
333 ssize_t ret;
334 uint32_t i, j;
335 struct kfd_cache_properties *cache;
337 /* Making sure that the buffer is an empty string */
338 buffer[0] = 0;
340 cache = container_of(attr, struct kfd_cache_properties, attr);
341 if (cache->gpu && kfd_devcgroup_check_permission(cache->gpu))
342 return -EPERM;
343 sysfs_show_32bit_prop(buffer, "processor_id_low",
344 cache->processor_id_low);
345 sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
346 sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
347 sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
348 sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
349 cache->cachelines_per_tag);
350 sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
351 sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
352 sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
353 snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
354 for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++)
355 for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++) {
356 /* Check each bit */
357 if (cache->sibling_map[i] & (1 << j))
358 ret = snprintf(buffer, PAGE_SIZE,
359 "%s%d%s", buffer, 1, ",");
360 else
361 ret = snprintf(buffer, PAGE_SIZE,
362 "%s%d%s", buffer, 0, ",");
364 /* Replace the last "," with end of line */
365 *(buffer + strlen(buffer) - 1) = 0xA;
366 return ret;
369 static const struct sysfs_ops cache_ops = {
370 .show = kfd_cache_show,
373 static struct kobj_type cache_type = {
374 .release = kfd_topology_kobj_release,
375 .sysfs_ops = &cache_ops,
378 /****** Sysfs of Performance Counters ******/
380 struct kfd_perf_attr {
381 struct kobj_attribute attr;
382 uint32_t data;
385 static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
386 char *buf)
388 struct kfd_perf_attr *attr;
390 buf[0] = 0;
391 attr = container_of(attrs, struct kfd_perf_attr, attr);
392 if (!attr->data) /* invalid data for PMC */
393 return 0;
394 else
395 return sysfs_show_32bit_val(buf, attr->data);
398 #define KFD_PERF_DESC(_name, _data) \
400 .attr = __ATTR(_name, 0444, perf_show, NULL), \
401 .data = _data, \
404 static struct kfd_perf_attr perf_attr_iommu[] = {
405 KFD_PERF_DESC(max_concurrent, 0),
406 KFD_PERF_DESC(num_counters, 0),
407 KFD_PERF_DESC(counter_ids, 0),
409 /****************************************/
411 static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
412 char *buffer)
414 struct kfd_topology_device *dev;
415 uint32_t log_max_watch_addr;
417 /* Making sure that the buffer is an empty string */
418 buffer[0] = 0;
420 if (strcmp(attr->name, "gpu_id") == 0) {
421 dev = container_of(attr, struct kfd_topology_device,
422 attr_gpuid);
423 if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
424 return -EPERM;
425 return sysfs_show_32bit_val(buffer, dev->gpu_id);
428 if (strcmp(attr->name, "name") == 0) {
429 dev = container_of(attr, struct kfd_topology_device,
430 attr_name);
432 if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
433 return -EPERM;
434 return sysfs_show_str_val(buffer, dev->node_props.name);
437 dev = container_of(attr, struct kfd_topology_device,
438 attr_props);
439 if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
440 return -EPERM;
441 sysfs_show_32bit_prop(buffer, "cpu_cores_count",
442 dev->node_props.cpu_cores_count);
443 sysfs_show_32bit_prop(buffer, "simd_count",
444 dev->node_props.simd_count);
445 sysfs_show_32bit_prop(buffer, "mem_banks_count",
446 dev->node_props.mem_banks_count);
447 sysfs_show_32bit_prop(buffer, "caches_count",
448 dev->node_props.caches_count);
449 sysfs_show_32bit_prop(buffer, "io_links_count",
450 dev->node_props.io_links_count);
451 sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
452 dev->node_props.cpu_core_id_base);
453 sysfs_show_32bit_prop(buffer, "simd_id_base",
454 dev->node_props.simd_id_base);
455 sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
456 dev->node_props.max_waves_per_simd);
457 sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
458 dev->node_props.lds_size_in_kb);
459 sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
460 dev->node_props.gds_size_in_kb);
461 sysfs_show_32bit_prop(buffer, "num_gws",
462 dev->node_props.num_gws);
463 sysfs_show_32bit_prop(buffer, "wave_front_size",
464 dev->node_props.wave_front_size);
465 sysfs_show_32bit_prop(buffer, "array_count",
466 dev->node_props.array_count);
467 sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
468 dev->node_props.simd_arrays_per_engine);
469 sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
470 dev->node_props.cu_per_simd_array);
471 sysfs_show_32bit_prop(buffer, "simd_per_cu",
472 dev->node_props.simd_per_cu);
473 sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
474 dev->node_props.max_slots_scratch_cu);
475 sysfs_show_32bit_prop(buffer, "vendor_id",
476 dev->node_props.vendor_id);
477 sysfs_show_32bit_prop(buffer, "device_id",
478 dev->node_props.device_id);
479 sysfs_show_32bit_prop(buffer, "location_id",
480 dev->node_props.location_id);
481 sysfs_show_32bit_prop(buffer, "drm_render_minor",
482 dev->node_props.drm_render_minor);
483 sysfs_show_64bit_prop(buffer, "hive_id",
484 dev->node_props.hive_id);
485 sysfs_show_32bit_prop(buffer, "num_sdma_engines",
486 dev->node_props.num_sdma_engines);
487 sysfs_show_32bit_prop(buffer, "num_sdma_xgmi_engines",
488 dev->node_props.num_sdma_xgmi_engines);
489 sysfs_show_32bit_prop(buffer, "num_sdma_queues_per_engine",
490 dev->node_props.num_sdma_queues_per_engine);
491 sysfs_show_32bit_prop(buffer, "num_cp_queues",
492 dev->node_props.num_cp_queues);
494 if (dev->gpu) {
495 log_max_watch_addr =
496 __ilog2_u32(dev->gpu->device_info->num_of_watch_points);
498 if (log_max_watch_addr) {
499 dev->node_props.capability |=
500 HSA_CAP_WATCH_POINTS_SUPPORTED;
502 dev->node_props.capability |=
503 ((log_max_watch_addr <<
504 HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
505 HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
508 if (dev->gpu->device_info->asic_family == CHIP_TONGA)
509 dev->node_props.capability |=
510 HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
512 sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
513 dev->node_props.max_engine_clk_fcompute);
515 sysfs_show_64bit_prop(buffer, "local_mem_size",
516 (unsigned long long int) 0);
518 sysfs_show_32bit_prop(buffer, "fw_version",
519 dev->gpu->mec_fw_version);
520 sysfs_show_32bit_prop(buffer, "capability",
521 dev->node_props.capability);
522 sysfs_show_32bit_prop(buffer, "sdma_fw_version",
523 dev->gpu->sdma_fw_version);
526 return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
527 cpufreq_quick_get_max(0)/1000);
530 static const struct sysfs_ops node_ops = {
531 .show = node_show,
534 static struct kobj_type node_type = {
535 .release = kfd_topology_kobj_release,
536 .sysfs_ops = &node_ops,
539 static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
541 sysfs_remove_file(kobj, attr);
542 kobject_del(kobj);
543 kobject_put(kobj);
546 static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
548 struct kfd_iolink_properties *iolink;
549 struct kfd_cache_properties *cache;
550 struct kfd_mem_properties *mem;
551 struct kfd_perf_properties *perf;
553 if (dev->kobj_iolink) {
554 list_for_each_entry(iolink, &dev->io_link_props, list)
555 if (iolink->kobj) {
556 kfd_remove_sysfs_file(iolink->kobj,
557 &iolink->attr);
558 iolink->kobj = NULL;
560 kobject_del(dev->kobj_iolink);
561 kobject_put(dev->kobj_iolink);
562 dev->kobj_iolink = NULL;
565 if (dev->kobj_cache) {
566 list_for_each_entry(cache, &dev->cache_props, list)
567 if (cache->kobj) {
568 kfd_remove_sysfs_file(cache->kobj,
569 &cache->attr);
570 cache->kobj = NULL;
572 kobject_del(dev->kobj_cache);
573 kobject_put(dev->kobj_cache);
574 dev->kobj_cache = NULL;
577 if (dev->kobj_mem) {
578 list_for_each_entry(mem, &dev->mem_props, list)
579 if (mem->kobj) {
580 kfd_remove_sysfs_file(mem->kobj, &mem->attr);
581 mem->kobj = NULL;
583 kobject_del(dev->kobj_mem);
584 kobject_put(dev->kobj_mem);
585 dev->kobj_mem = NULL;
588 if (dev->kobj_perf) {
589 list_for_each_entry(perf, &dev->perf_props, list) {
590 kfree(perf->attr_group);
591 perf->attr_group = NULL;
593 kobject_del(dev->kobj_perf);
594 kobject_put(dev->kobj_perf);
595 dev->kobj_perf = NULL;
598 if (dev->kobj_node) {
599 sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
600 sysfs_remove_file(dev->kobj_node, &dev->attr_name);
601 sysfs_remove_file(dev->kobj_node, &dev->attr_props);
602 kobject_del(dev->kobj_node);
603 kobject_put(dev->kobj_node);
604 dev->kobj_node = NULL;
608 static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
609 uint32_t id)
611 struct kfd_iolink_properties *iolink;
612 struct kfd_cache_properties *cache;
613 struct kfd_mem_properties *mem;
614 struct kfd_perf_properties *perf;
615 int ret;
616 uint32_t i, num_attrs;
617 struct attribute **attrs;
619 if (WARN_ON(dev->kobj_node))
620 return -EEXIST;
623 * Creating the sysfs folders
625 dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
626 if (!dev->kobj_node)
627 return -ENOMEM;
629 ret = kobject_init_and_add(dev->kobj_node, &node_type,
630 sys_props.kobj_nodes, "%d", id);
631 if (ret < 0)
632 return ret;
634 dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
635 if (!dev->kobj_mem)
636 return -ENOMEM;
638 dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
639 if (!dev->kobj_cache)
640 return -ENOMEM;
642 dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
643 if (!dev->kobj_iolink)
644 return -ENOMEM;
646 dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
647 if (!dev->kobj_perf)
648 return -ENOMEM;
651 * Creating sysfs files for node properties
653 dev->attr_gpuid.name = "gpu_id";
654 dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
655 sysfs_attr_init(&dev->attr_gpuid);
656 dev->attr_name.name = "name";
657 dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
658 sysfs_attr_init(&dev->attr_name);
659 dev->attr_props.name = "properties";
660 dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
661 sysfs_attr_init(&dev->attr_props);
662 ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
663 if (ret < 0)
664 return ret;
665 ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
666 if (ret < 0)
667 return ret;
668 ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
669 if (ret < 0)
670 return ret;
672 i = 0;
673 list_for_each_entry(mem, &dev->mem_props, list) {
674 mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
675 if (!mem->kobj)
676 return -ENOMEM;
677 ret = kobject_init_and_add(mem->kobj, &mem_type,
678 dev->kobj_mem, "%d", i);
679 if (ret < 0)
680 return ret;
682 mem->attr.name = "properties";
683 mem->attr.mode = KFD_SYSFS_FILE_MODE;
684 sysfs_attr_init(&mem->attr);
685 ret = sysfs_create_file(mem->kobj, &mem->attr);
686 if (ret < 0)
687 return ret;
688 i++;
691 i = 0;
692 list_for_each_entry(cache, &dev->cache_props, list) {
693 cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
694 if (!cache->kobj)
695 return -ENOMEM;
696 ret = kobject_init_and_add(cache->kobj, &cache_type,
697 dev->kobj_cache, "%d", i);
698 if (ret < 0)
699 return ret;
701 cache->attr.name = "properties";
702 cache->attr.mode = KFD_SYSFS_FILE_MODE;
703 sysfs_attr_init(&cache->attr);
704 ret = sysfs_create_file(cache->kobj, &cache->attr);
705 if (ret < 0)
706 return ret;
707 i++;
710 i = 0;
711 list_for_each_entry(iolink, &dev->io_link_props, list) {
712 iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
713 if (!iolink->kobj)
714 return -ENOMEM;
715 ret = kobject_init_and_add(iolink->kobj, &iolink_type,
716 dev->kobj_iolink, "%d", i);
717 if (ret < 0)
718 return ret;
720 iolink->attr.name = "properties";
721 iolink->attr.mode = KFD_SYSFS_FILE_MODE;
722 sysfs_attr_init(&iolink->attr);
723 ret = sysfs_create_file(iolink->kobj, &iolink->attr);
724 if (ret < 0)
725 return ret;
726 i++;
729 /* All hardware blocks have the same number of attributes. */
730 num_attrs = ARRAY_SIZE(perf_attr_iommu);
731 list_for_each_entry(perf, &dev->perf_props, list) {
732 perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
733 * num_attrs + sizeof(struct attribute_group),
734 GFP_KERNEL);
735 if (!perf->attr_group)
736 return -ENOMEM;
738 attrs = (struct attribute **)(perf->attr_group + 1);
739 if (!strcmp(perf->block_name, "iommu")) {
740 /* Information of IOMMU's num_counters and counter_ids is shown
741 * under /sys/bus/event_source/devices/amd_iommu. We don't
742 * duplicate here.
744 perf_attr_iommu[0].data = perf->max_concurrent;
745 for (i = 0; i < num_attrs; i++)
746 attrs[i] = &perf_attr_iommu[i].attr.attr;
748 perf->attr_group->name = perf->block_name;
749 perf->attr_group->attrs = attrs;
750 ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
751 if (ret < 0)
752 return ret;
755 return 0;
758 /* Called with write topology lock acquired */
759 static int kfd_build_sysfs_node_tree(void)
761 struct kfd_topology_device *dev;
762 int ret;
763 uint32_t i = 0;
765 list_for_each_entry(dev, &topology_device_list, list) {
766 ret = kfd_build_sysfs_node_entry(dev, i);
767 if (ret < 0)
768 return ret;
769 i++;
772 return 0;
775 /* Called with write topology lock acquired */
776 static void kfd_remove_sysfs_node_tree(void)
778 struct kfd_topology_device *dev;
780 list_for_each_entry(dev, &topology_device_list, list)
781 kfd_remove_sysfs_node_entry(dev);
784 static int kfd_topology_update_sysfs(void)
786 int ret;
788 pr_info("Creating topology SYSFS entries\n");
789 if (!sys_props.kobj_topology) {
790 sys_props.kobj_topology =
791 kfd_alloc_struct(sys_props.kobj_topology);
792 if (!sys_props.kobj_topology)
793 return -ENOMEM;
795 ret = kobject_init_and_add(sys_props.kobj_topology,
796 &sysprops_type, &kfd_device->kobj,
797 "topology");
798 if (ret < 0)
799 return ret;
801 sys_props.kobj_nodes = kobject_create_and_add("nodes",
802 sys_props.kobj_topology);
803 if (!sys_props.kobj_nodes)
804 return -ENOMEM;
806 sys_props.attr_genid.name = "generation_id";
807 sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
808 sysfs_attr_init(&sys_props.attr_genid);
809 ret = sysfs_create_file(sys_props.kobj_topology,
810 &sys_props.attr_genid);
811 if (ret < 0)
812 return ret;
814 sys_props.attr_props.name = "system_properties";
815 sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
816 sysfs_attr_init(&sys_props.attr_props);
817 ret = sysfs_create_file(sys_props.kobj_topology,
818 &sys_props.attr_props);
819 if (ret < 0)
820 return ret;
823 kfd_remove_sysfs_node_tree();
825 return kfd_build_sysfs_node_tree();
828 static void kfd_topology_release_sysfs(void)
830 kfd_remove_sysfs_node_tree();
831 if (sys_props.kobj_topology) {
832 sysfs_remove_file(sys_props.kobj_topology,
833 &sys_props.attr_genid);
834 sysfs_remove_file(sys_props.kobj_topology,
835 &sys_props.attr_props);
836 if (sys_props.kobj_nodes) {
837 kobject_del(sys_props.kobj_nodes);
838 kobject_put(sys_props.kobj_nodes);
839 sys_props.kobj_nodes = NULL;
841 kobject_del(sys_props.kobj_topology);
842 kobject_put(sys_props.kobj_topology);
843 sys_props.kobj_topology = NULL;
847 /* Called with write topology_lock acquired */
848 static void kfd_topology_update_device_list(struct list_head *temp_list,
849 struct list_head *master_list)
851 while (!list_empty(temp_list)) {
852 list_move_tail(temp_list->next, master_list);
853 sys_props.num_devices++;
857 static void kfd_debug_print_topology(void)
859 struct kfd_topology_device *dev;
861 down_read(&topology_lock);
863 dev = list_last_entry(&topology_device_list,
864 struct kfd_topology_device, list);
865 if (dev) {
866 if (dev->node_props.cpu_cores_count &&
867 dev->node_props.simd_count) {
868 pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
869 dev->node_props.device_id,
870 dev->node_props.vendor_id);
871 } else if (dev->node_props.cpu_cores_count)
872 pr_info("Topology: Add CPU node\n");
873 else if (dev->node_props.simd_count)
874 pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
875 dev->node_props.device_id,
876 dev->node_props.vendor_id);
878 up_read(&topology_lock);
881 /* Helper function for intializing platform_xx members of
882 * kfd_system_properties. Uses OEM info from the last CPU/APU node.
884 static void kfd_update_system_properties(void)
886 struct kfd_topology_device *dev;
888 down_read(&topology_lock);
889 dev = list_last_entry(&topology_device_list,
890 struct kfd_topology_device, list);
891 if (dev) {
892 sys_props.platform_id =
893 (*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
894 sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
895 sys_props.platform_rev = dev->oem_revision;
897 up_read(&topology_lock);
900 static void find_system_memory(const struct dmi_header *dm,
901 void *private)
903 struct kfd_mem_properties *mem;
904 u16 mem_width, mem_clock;
905 struct kfd_topology_device *kdev =
906 (struct kfd_topology_device *)private;
907 const u8 *dmi_data = (const u8 *)(dm + 1);
909 if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
910 mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
911 mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
912 list_for_each_entry(mem, &kdev->mem_props, list) {
913 if (mem_width != 0xFFFF && mem_width != 0)
914 mem->width = mem_width;
915 if (mem_clock != 0)
916 mem->mem_clk_max = mem_clock;
922 * Performance counters information is not part of CRAT but we would like to
923 * put them in the sysfs under topology directory for Thunk to get the data.
924 * This function is called before updating the sysfs.
926 static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
928 /* These are the only counters supported so far */
929 return kfd_iommu_add_perf_counters(kdev);
932 /* kfd_add_non_crat_information - Add information that is not currently
933 * defined in CRAT but is necessary for KFD topology
934 * @dev - topology device to which addition info is added
936 static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
938 /* Check if CPU only node. */
939 if (!kdev->gpu) {
940 /* Add system memory information */
941 dmi_walk(find_system_memory, kdev);
943 /* TODO: For GPU node, rearrange code from kfd_topology_add_device */
946 /* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
947 * Ignore CRAT for all other devices. AMD APU is identified if both CPU
948 * and GPU cores are present.
949 * @device_list - topology device list created by parsing ACPI CRAT table.
950 * @return - TRUE if invalid, FALSE is valid.
952 static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
954 struct kfd_topology_device *dev;
956 list_for_each_entry(dev, device_list, list) {
957 if (dev->node_props.cpu_cores_count &&
958 dev->node_props.simd_count)
959 return false;
961 pr_info("Ignoring ACPI CRAT on non-APU system\n");
962 return true;
965 int kfd_topology_init(void)
967 void *crat_image = NULL;
968 size_t image_size = 0;
969 int ret;
970 struct list_head temp_topology_device_list;
971 int cpu_only_node = 0;
972 struct kfd_topology_device *kdev;
973 int proximity_domain;
975 /* topology_device_list - Master list of all topology devices
976 * temp_topology_device_list - temporary list created while parsing CRAT
977 * or VCRAT. Once parsing is complete the contents of list is moved to
978 * topology_device_list
981 /* Initialize the head for the both the lists */
982 INIT_LIST_HEAD(&topology_device_list);
983 INIT_LIST_HEAD(&temp_topology_device_list);
984 init_rwsem(&topology_lock);
986 memset(&sys_props, 0, sizeof(sys_props));
988 /* Proximity domains in ACPI CRAT tables start counting at
989 * 0. The same should be true for virtual CRAT tables created
990 * at this stage. GPUs added later in kfd_topology_add_device
991 * use a counter.
993 proximity_domain = 0;
996 * Get the CRAT image from the ACPI. If ACPI doesn't have one
997 * or if ACPI CRAT is invalid create a virtual CRAT.
998 * NOTE: The current implementation expects all AMD APUs to have
999 * CRAT. If no CRAT is available, it is assumed to be a CPU
1001 ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
1002 if (!ret) {
1003 ret = kfd_parse_crat_table(crat_image,
1004 &temp_topology_device_list,
1005 proximity_domain);
1006 if (ret ||
1007 kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
1008 kfd_release_topology_device_list(
1009 &temp_topology_device_list);
1010 kfd_destroy_crat_image(crat_image);
1011 crat_image = NULL;
1015 if (!crat_image) {
1016 ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
1017 COMPUTE_UNIT_CPU, NULL,
1018 proximity_domain);
1019 cpu_only_node = 1;
1020 if (ret) {
1021 pr_err("Error creating VCRAT table for CPU\n");
1022 return ret;
1025 ret = kfd_parse_crat_table(crat_image,
1026 &temp_topology_device_list,
1027 proximity_domain);
1028 if (ret) {
1029 pr_err("Error parsing VCRAT table for CPU\n");
1030 goto err;
1034 kdev = list_first_entry(&temp_topology_device_list,
1035 struct kfd_topology_device, list);
1036 kfd_add_perf_to_topology(kdev);
1038 down_write(&topology_lock);
1039 kfd_topology_update_device_list(&temp_topology_device_list,
1040 &topology_device_list);
1041 atomic_set(&topology_crat_proximity_domain, sys_props.num_devices-1);
1042 ret = kfd_topology_update_sysfs();
1043 up_write(&topology_lock);
1045 if (!ret) {
1046 sys_props.generation_count++;
1047 kfd_update_system_properties();
1048 kfd_debug_print_topology();
1049 pr_info("Finished initializing topology\n");
1050 } else
1051 pr_err("Failed to update topology in sysfs ret=%d\n", ret);
1053 /* For nodes with GPU, this information gets added
1054 * when GPU is detected (kfd_topology_add_device).
1056 if (cpu_only_node) {
1057 /* Add additional information to CPU only node created above */
1058 down_write(&topology_lock);
1059 kdev = list_first_entry(&topology_device_list,
1060 struct kfd_topology_device, list);
1061 up_write(&topology_lock);
1062 kfd_add_non_crat_information(kdev);
1065 err:
1066 kfd_destroy_crat_image(crat_image);
1067 return ret;
1070 void kfd_topology_shutdown(void)
1072 down_write(&topology_lock);
1073 kfd_topology_release_sysfs();
1074 kfd_release_live_view();
1075 up_write(&topology_lock);
1078 static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
1080 uint32_t hashout;
1081 uint32_t buf[7];
1082 uint64_t local_mem_size;
1083 int i;
1084 struct kfd_local_mem_info local_mem_info;
1086 if (!gpu)
1087 return 0;
1089 amdgpu_amdkfd_get_local_mem_info(gpu->kgd, &local_mem_info);
1091 local_mem_size = local_mem_info.local_mem_size_private +
1092 local_mem_info.local_mem_size_public;
1094 buf[0] = gpu->pdev->devfn;
1095 buf[1] = gpu->pdev->subsystem_vendor |
1096 (gpu->pdev->subsystem_device << 16);
1097 buf[2] = pci_domain_nr(gpu->pdev->bus);
1098 buf[3] = gpu->pdev->device;
1099 buf[4] = gpu->pdev->bus->number;
1100 buf[5] = lower_32_bits(local_mem_size);
1101 buf[6] = upper_32_bits(local_mem_size);
1103 for (i = 0, hashout = 0; i < 7; i++)
1104 hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
1106 return hashout;
1108 /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
1109 * the GPU device is not already present in the topology device
1110 * list then return NULL. This means a new topology device has to
1111 * be created for this GPU.
1113 static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
1115 struct kfd_topology_device *dev;
1116 struct kfd_topology_device *out_dev = NULL;
1117 struct kfd_mem_properties *mem;
1118 struct kfd_cache_properties *cache;
1119 struct kfd_iolink_properties *iolink;
1121 down_write(&topology_lock);
1122 list_for_each_entry(dev, &topology_device_list, list) {
1123 /* Discrete GPUs need their own topology device list
1124 * entries. Don't assign them to CPU/APU nodes.
1126 if (!gpu->device_info->needs_iommu_device &&
1127 dev->node_props.cpu_cores_count)
1128 continue;
1130 if (!dev->gpu && (dev->node_props.simd_count > 0)) {
1131 dev->gpu = gpu;
1132 out_dev = dev;
1134 list_for_each_entry(mem, &dev->mem_props, list)
1135 mem->gpu = dev->gpu;
1136 list_for_each_entry(cache, &dev->cache_props, list)
1137 cache->gpu = dev->gpu;
1138 list_for_each_entry(iolink, &dev->io_link_props, list)
1139 iolink->gpu = dev->gpu;
1140 break;
1143 up_write(&topology_lock);
1144 return out_dev;
1147 static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
1150 * TODO: Generate an event for thunk about the arrival/removal
1151 * of the GPU
1155 /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
1156 * patch this after CRAT parsing.
1158 static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
1160 struct kfd_mem_properties *mem;
1161 struct kfd_local_mem_info local_mem_info;
1163 if (!dev)
1164 return;
1166 /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
1167 * single bank of VRAM local memory.
1168 * for dGPUs - VCRAT reports only one bank of Local Memory
1169 * for APUs - If CRAT from ACPI reports more than one bank, then
1170 * all the banks will report the same mem_clk_max information
1172 amdgpu_amdkfd_get_local_mem_info(dev->gpu->kgd, &local_mem_info);
1174 list_for_each_entry(mem, &dev->mem_props, list)
1175 mem->mem_clk_max = local_mem_info.mem_clk_max;
1178 static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
1180 struct kfd_iolink_properties *link, *cpu_link;
1181 struct kfd_topology_device *cpu_dev;
1182 uint32_t cap;
1183 uint32_t cpu_flag = CRAT_IOLINK_FLAGS_ENABLED;
1184 uint32_t flag = CRAT_IOLINK_FLAGS_ENABLED;
1186 if (!dev || !dev->gpu)
1187 return;
1189 pcie_capability_read_dword(dev->gpu->pdev,
1190 PCI_EXP_DEVCAP2, &cap);
1192 if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1193 PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1194 cpu_flag |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1195 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1197 if (!dev->gpu->pci_atomic_requested ||
1198 dev->gpu->device_info->asic_family == CHIP_HAWAII)
1199 flag |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1200 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1202 /* GPU only creates direct links so apply flags setting to all */
1203 list_for_each_entry(link, &dev->io_link_props, list) {
1204 link->flags = flag;
1205 cpu_dev = kfd_topology_device_by_proximity_domain(
1206 link->node_to);
1207 if (cpu_dev) {
1208 list_for_each_entry(cpu_link,
1209 &cpu_dev->io_link_props, list)
1210 if (cpu_link->node_to == link->node_from)
1211 cpu_link->flags = cpu_flag;
1216 int kfd_topology_add_device(struct kfd_dev *gpu)
1218 uint32_t gpu_id;
1219 struct kfd_topology_device *dev;
1220 struct kfd_cu_info cu_info;
1221 int res = 0;
1222 struct list_head temp_topology_device_list;
1223 void *crat_image = NULL;
1224 size_t image_size = 0;
1225 int proximity_domain;
1226 struct amdgpu_ras *ctx;
1228 INIT_LIST_HEAD(&temp_topology_device_list);
1230 gpu_id = kfd_generate_gpu_id(gpu);
1232 pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1234 proximity_domain = atomic_inc_return(&topology_crat_proximity_domain);
1236 /* Check to see if this gpu device exists in the topology_device_list.
1237 * If so, assign the gpu to that device,
1238 * else create a Virtual CRAT for this gpu device and then parse that
1239 * CRAT to create a new topology device. Once created assign the gpu to
1240 * that topology device
1242 dev = kfd_assign_gpu(gpu);
1243 if (!dev) {
1244 res = kfd_create_crat_image_virtual(&crat_image, &image_size,
1245 COMPUTE_UNIT_GPU, gpu,
1246 proximity_domain);
1247 if (res) {
1248 pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
1249 gpu_id);
1250 return res;
1252 res = kfd_parse_crat_table(crat_image,
1253 &temp_topology_device_list,
1254 proximity_domain);
1255 if (res) {
1256 pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
1257 gpu_id);
1258 goto err;
1261 down_write(&topology_lock);
1262 kfd_topology_update_device_list(&temp_topology_device_list,
1263 &topology_device_list);
1265 /* Update the SYSFS tree, since we added another topology
1266 * device
1268 res = kfd_topology_update_sysfs();
1269 up_write(&topology_lock);
1271 if (!res)
1272 sys_props.generation_count++;
1273 else
1274 pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
1275 gpu_id, res);
1276 dev = kfd_assign_gpu(gpu);
1277 if (WARN_ON(!dev)) {
1278 res = -ENODEV;
1279 goto err;
1283 dev->gpu_id = gpu_id;
1284 gpu->id = gpu_id;
1286 /* TODO: Move the following lines to function
1287 * kfd_add_non_crat_information
1290 /* Fill-in additional information that is not available in CRAT but
1291 * needed for the topology
1294 amdgpu_amdkfd_get_cu_info(dev->gpu->kgd, &cu_info);
1296 strncpy(dev->node_props.name, gpu->device_info->asic_name,
1297 KFD_TOPOLOGY_PUBLIC_NAME_SIZE);
1299 dev->node_props.simd_arrays_per_engine =
1300 cu_info.num_shader_arrays_per_engine;
1302 dev->node_props.vendor_id = gpu->pdev->vendor;
1303 dev->node_props.device_id = gpu->pdev->device;
1304 dev->node_props.location_id = pci_dev_id(gpu->pdev);
1305 dev->node_props.max_engine_clk_fcompute =
1306 amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->kgd);
1307 dev->node_props.max_engine_clk_ccompute =
1308 cpufreq_quick_get_max(0) / 1000;
1309 dev->node_props.drm_render_minor =
1310 gpu->shared_resources.drm_render_minor;
1312 dev->node_props.hive_id = gpu->hive_id;
1313 dev->node_props.num_sdma_engines = gpu->device_info->num_sdma_engines;
1314 dev->node_props.num_sdma_xgmi_engines =
1315 gpu->device_info->num_xgmi_sdma_engines;
1316 dev->node_props.num_sdma_queues_per_engine =
1317 gpu->device_info->num_sdma_queues_per_engine;
1318 dev->node_props.num_gws = (hws_gws_support &&
1319 dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
1320 amdgpu_amdkfd_get_num_gws(dev->gpu->kgd) : 0;
1321 dev->node_props.num_cp_queues = get_queues_num(dev->gpu->dqm);
1323 kfd_fill_mem_clk_max_info(dev);
1324 kfd_fill_iolink_non_crat_info(dev);
1326 switch (dev->gpu->device_info->asic_family) {
1327 case CHIP_KAVERI:
1328 case CHIP_HAWAII:
1329 case CHIP_TONGA:
1330 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
1331 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1332 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1333 break;
1334 case CHIP_CARRIZO:
1335 case CHIP_FIJI:
1336 case CHIP_POLARIS10:
1337 case CHIP_POLARIS11:
1338 case CHIP_POLARIS12:
1339 case CHIP_VEGAM:
1340 pr_debug("Adding doorbell packet type capability\n");
1341 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
1342 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1343 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1344 break;
1345 case CHIP_VEGA10:
1346 case CHIP_VEGA12:
1347 case CHIP_VEGA20:
1348 case CHIP_RAVEN:
1349 case CHIP_RENOIR:
1350 case CHIP_ARCTURUS:
1351 case CHIP_NAVI10:
1352 case CHIP_NAVI12:
1353 case CHIP_NAVI14:
1354 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1355 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1356 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1357 break;
1358 default:
1359 WARN(1, "Unexpected ASIC family %u",
1360 dev->gpu->device_info->asic_family);
1364 * Overwrite ATS capability according to needs_iommu_device to fix
1365 * potential missing corresponding bit in CRAT of BIOS.
1367 if (dev->gpu->device_info->needs_iommu_device)
1368 dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
1369 else
1370 dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;
1372 /* Fix errors in CZ CRAT.
1373 * simd_count: Carrizo CRAT reports wrong simd_count, probably
1374 * because it doesn't consider masked out CUs
1375 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
1377 if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
1378 dev->node_props.simd_count =
1379 cu_info.simd_per_cu * cu_info.cu_active_number;
1380 dev->node_props.max_waves_per_simd = 10;
1383 ctx = amdgpu_ras_get_context((struct amdgpu_device *)(dev->gpu->kgd));
1384 if (ctx) {
1385 /* kfd only concerns sram ecc on GFX/SDMA and HBM ecc on UMC */
1386 dev->node_props.capability |=
1387 (((ctx->features & BIT(AMDGPU_RAS_BLOCK__SDMA)) != 0) ||
1388 ((ctx->features & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0)) ?
1389 HSA_CAP_SRAM_EDCSUPPORTED : 0;
1390 dev->node_props.capability |= ((ctx->features & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
1391 HSA_CAP_MEM_EDCSUPPORTED : 0;
1393 dev->node_props.capability |= (ctx->features != 0) ?
1394 HSA_CAP_RASEVENTNOTIFY : 0;
1397 kfd_debug_print_topology();
1399 if (!res)
1400 kfd_notify_gpu_change(gpu_id, 1);
1401 err:
1402 kfd_destroy_crat_image(crat_image);
1403 return res;
1406 int kfd_topology_remove_device(struct kfd_dev *gpu)
1408 struct kfd_topology_device *dev, *tmp;
1409 uint32_t gpu_id;
1410 int res = -ENODEV;
1412 down_write(&topology_lock);
1414 list_for_each_entry_safe(dev, tmp, &topology_device_list, list)
1415 if (dev->gpu == gpu) {
1416 gpu_id = dev->gpu_id;
1417 kfd_remove_sysfs_node_entry(dev);
1418 kfd_release_topology_device(dev);
1419 sys_props.num_devices--;
1420 res = 0;
1421 if (kfd_topology_update_sysfs() < 0)
1422 kfd_topology_release_sysfs();
1423 break;
1426 up_write(&topology_lock);
1428 if (!res)
1429 kfd_notify_gpu_change(gpu_id, 0);
1431 return res;
1434 /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
1435 * topology. If GPU device is found @idx, then valid kfd_dev pointer is
1436 * returned through @kdev
1437 * Return - 0: On success (@kdev will be NULL for non GPU nodes)
1438 * -1: If end of list
1440 int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
1443 struct kfd_topology_device *top_dev;
1444 uint8_t device_idx = 0;
1446 *kdev = NULL;
1447 down_read(&topology_lock);
1449 list_for_each_entry(top_dev, &topology_device_list, list) {
1450 if (device_idx == idx) {
1451 *kdev = top_dev->gpu;
1452 up_read(&topology_lock);
1453 return 0;
1456 device_idx++;
1459 up_read(&topology_lock);
1461 return -1;
1465 static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
1467 int first_cpu_of_numa_node;
1469 if (!cpumask || cpumask == cpu_none_mask)
1470 return -1;
1471 first_cpu_of_numa_node = cpumask_first(cpumask);
1472 if (first_cpu_of_numa_node >= nr_cpu_ids)
1473 return -1;
1474 #ifdef CONFIG_X86_64
1475 return cpu_data(first_cpu_of_numa_node).apicid;
1476 #else
1477 return first_cpu_of_numa_node;
1478 #endif
1481 /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
1482 * of the given NUMA node (numa_node_id)
1483 * Return -1 on failure
1485 int kfd_numa_node_to_apic_id(int numa_node_id)
1487 if (numa_node_id == -1) {
1488 pr_warn("Invalid NUMA Node. Use online CPU mask\n");
1489 return kfd_cpumask_to_apic_id(cpu_online_mask);
1491 return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
1494 #if defined(CONFIG_DEBUG_FS)
1496 int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
1498 struct kfd_topology_device *dev;
1499 unsigned int i = 0;
1500 int r = 0;
1502 down_read(&topology_lock);
1504 list_for_each_entry(dev, &topology_device_list, list) {
1505 if (!dev->gpu) {
1506 i++;
1507 continue;
1510 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1511 r = dqm_debugfs_hqds(m, dev->gpu->dqm);
1512 if (r)
1513 break;
1516 up_read(&topology_lock);
1518 return r;
1521 int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
1523 struct kfd_topology_device *dev;
1524 unsigned int i = 0;
1525 int r = 0;
1527 down_read(&topology_lock);
1529 list_for_each_entry(dev, &topology_device_list, list) {
1530 if (!dev->gpu) {
1531 i++;
1532 continue;
1535 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1536 r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets);
1537 if (r)
1538 break;
1541 up_read(&topology_lock);
1543 return r;
1546 #endif