drm/amdkfd: Add VegaM support
[linux-block.git] / drivers / gpu / drm / amd / amdkfd / kfd_topology.c
CommitLineData
5b5c4e40
EP
1/*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 */
22
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>
f7c826ad 30#include <linux/log2.h>
520b8fb7
FK
31#include <linux/dmi.h>
32#include <linux/atomic.h>
5b5c4e40
EP
33
34#include "kfd_priv.h"
35#include "kfd_crat.h"
36#include "kfd_topology.h"
851a645e 37#include "kfd_device_queue_manager.h"
64d1c3a4 38#include "kfd_iommu.h"
5b87245f 39#include "amdgpu_amdkfd.h"
0dee45a2 40#include "amdgpu_ras.h"
5b5c4e40 41
4f449311
HK
42/* topology_device_list - Master list of all topology devices */
43static struct list_head topology_device_list;
520b8fb7 44static struct kfd_system_properties sys_props;
5b5c4e40
EP
45
46static DECLARE_RWSEM(topology_lock);
520b8fb7 47static atomic_t topology_crat_proximity_domain;
5b5c4e40 48
3a87177e
HK
49struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
50 uint32_t proximity_domain)
51{
52 struct kfd_topology_device *top_dev;
53 struct kfd_topology_device *device = NULL;
54
55 down_read(&topology_lock);
56
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;
61 }
62
63 up_read(&topology_lock);
64
65 return device;
66}
67
44d8cc6f 68struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
5b5c4e40 69{
44d8cc6f
YZ
70 struct kfd_topology_device *top_dev = NULL;
71 struct kfd_topology_device *ret = NULL;
5b5c4e40
EP
72
73 down_read(&topology_lock);
74
75 list_for_each_entry(top_dev, &topology_device_list, list)
76 if (top_dev->gpu_id == gpu_id) {
44d8cc6f 77 ret = top_dev;
5b5c4e40
EP
78 break;
79 }
80
81 up_read(&topology_lock);
82
44d8cc6f
YZ
83 return ret;
84}
85
86struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
87{
88 struct kfd_topology_device *top_dev;
89
90 top_dev = kfd_topology_device_by_id(gpu_id);
91 if (!top_dev)
92 return NULL;
93
94 return top_dev->gpu;
5b5c4e40
EP
95}
96
97struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
98{
99 struct kfd_topology_device *top_dev;
100 struct kfd_dev *device = NULL;
101
102 down_read(&topology_lock);
103
104 list_for_each_entry(top_dev, &topology_device_list, list)
3704d56e 105 if (top_dev->gpu && top_dev->gpu->pdev == pdev) {
5b5c4e40
EP
106 device = top_dev->gpu;
107 break;
108 }
109
110 up_read(&topology_lock);
111
112 return device;
113}
114
1dde0ea9
FK
115struct kfd_dev *kfd_device_by_kgd(const struct kgd_dev *kgd)
116{
117 struct kfd_topology_device *top_dev;
118 struct kfd_dev *device = NULL;
119
120 down_read(&topology_lock);
121
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;
126 }
127
128 up_read(&topology_lock);
129
130 return device;
131}
132
3a87177e 133/* Called with write topology_lock acquired */
5b5c4e40
EP
134static void kfd_release_topology_device(struct kfd_topology_device *dev)
135{
136 struct kfd_mem_properties *mem;
137 struct kfd_cache_properties *cache;
138 struct kfd_iolink_properties *iolink;
f4757347 139 struct kfd_perf_properties *perf;
5b5c4e40 140
5b5c4e40
EP
141 list_del(&dev->list);
142
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);
148 }
149
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);
155 }
156
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);
162 }
163
f4757347
AL
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);
169 }
170
5b5c4e40 171 kfree(dev);
5b5c4e40
EP
172}
173
4f449311 174void kfd_release_topology_device_list(struct list_head *device_list)
5b5c4e40
EP
175{
176 struct kfd_topology_device *dev;
177
4f449311
HK
178 while (!list_empty(device_list)) {
179 dev = list_first_entry(device_list,
180 struct kfd_topology_device, list);
5b5c4e40 181 kfd_release_topology_device(dev);
4f449311 182 }
5b5c4e40
EP
183}
184
4f449311
HK
185static void kfd_release_live_view(void)
186{
187 kfd_release_topology_device_list(&topology_device_list);
5b5c4e40
EP
188 memset(&sys_props, 0, sizeof(sys_props));
189}
190
4f449311
HK
191struct kfd_topology_device *kfd_create_topology_device(
192 struct list_head *device_list)
5b5c4e40
EP
193{
194 struct kfd_topology_device *dev;
195
196 dev = kfd_alloc_struct(dev);
4eacc26b 197 if (!dev) {
5b5c4e40 198 pr_err("No memory to allocate a topology device");
16b9201c 199 return NULL;
5b5c4e40
EP
200 }
201
202 INIT_LIST_HEAD(&dev->mem_props);
203 INIT_LIST_HEAD(&dev->cache_props);
204 INIT_LIST_HEAD(&dev->io_link_props);
f4757347 205 INIT_LIST_HEAD(&dev->perf_props);
5b5c4e40 206
4f449311 207 list_add_tail(&dev->list, device_list);
5b5c4e40
EP
208
209 return dev;
16b9201c 210}
5b5c4e40 211
5b5c4e40
EP
212
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)
223
224static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
225 char *buffer)
226{
227 ssize_t ret;
228
229 /* Making sure that the buffer is an empty string */
230 buffer[0] = 0;
231
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;
243 }
244
245 return ret;
246}
247
5108d768
YZ
248static void kfd_topology_kobj_release(struct kobject *kobj)
249{
250 kfree(kobj);
251}
252
5b5c4e40
EP
253static const struct sysfs_ops sysprops_ops = {
254 .show = sysprops_show,
255};
256
257static struct kobj_type sysprops_type = {
5108d768 258 .release = kfd_topology_kobj_release,
5b5c4e40
EP
259 .sysfs_ops = &sysprops_ops,
260};
261
262static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
263 char *buffer)
264{
265 ssize_t ret;
266 struct kfd_iolink_properties *iolink;
267
268 /* Making sure that the buffer is an empty string */
269 buffer[0] = 0;
270
271 iolink = container_of(attr, struct kfd_iolink_properties, attr);
272 sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
273 sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
274 sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
275 sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
276 sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
277 sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
278 sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
279 sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
280 sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
281 sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
282 sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
283 iolink->rec_transfer_size);
284 ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
285
286 return ret;
287}
288
289static const struct sysfs_ops iolink_ops = {
290 .show = iolink_show,
291};
292
293static struct kobj_type iolink_type = {
5108d768 294 .release = kfd_topology_kobj_release,
5b5c4e40
EP
295 .sysfs_ops = &iolink_ops,
296};
297
298static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
299 char *buffer)
300{
301 ssize_t ret;
302 struct kfd_mem_properties *mem;
303
304 /* Making sure that the buffer is an empty string */
305 buffer[0] = 0;
306
307 mem = container_of(attr, struct kfd_mem_properties, attr);
308 sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
309 sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
310 sysfs_show_32bit_prop(buffer, "flags", mem->flags);
311 sysfs_show_32bit_prop(buffer, "width", mem->width);
312 ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
313
314 return ret;
315}
316
317static const struct sysfs_ops mem_ops = {
318 .show = mem_show,
319};
320
321static struct kobj_type mem_type = {
5108d768 322 .release = kfd_topology_kobj_release,
5b5c4e40
EP
323 .sysfs_ops = &mem_ops,
324};
325
326static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
327 char *buffer)
328{
329 ssize_t ret;
bc0c75a3 330 uint32_t i, j;
5b5c4e40
EP
331 struct kfd_cache_properties *cache;
332
333 /* Making sure that the buffer is an empty string */
334 buffer[0] = 0;
335
336 cache = container_of(attr, struct kfd_cache_properties, attr);
337 sysfs_show_32bit_prop(buffer, "processor_id_low",
338 cache->processor_id_low);
339 sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
340 sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
341 sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
342 sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
343 cache->cachelines_per_tag);
344 sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
345 sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
346 sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
347 snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
bc0c75a3
HK
348 for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++)
349 for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++) {
350 /* Check each bit */
351 if (cache->sibling_map[i] & (1 << j))
352 ret = snprintf(buffer, PAGE_SIZE,
353 "%s%d%s", buffer, 1, ",");
354 else
355 ret = snprintf(buffer, PAGE_SIZE,
356 "%s%d%s", buffer, 0, ",");
357 }
358 /* Replace the last "," with end of line */
359 *(buffer + strlen(buffer) - 1) = 0xA;
5b5c4e40
EP
360 return ret;
361}
362
363static const struct sysfs_ops cache_ops = {
364 .show = kfd_cache_show,
365};
366
367static struct kobj_type cache_type = {
5108d768 368 .release = kfd_topology_kobj_release,
5b5c4e40
EP
369 .sysfs_ops = &cache_ops,
370};
371
f4757347
AL
372/****** Sysfs of Performance Counters ******/
373
374struct kfd_perf_attr {
375 struct kobj_attribute attr;
376 uint32_t data;
377};
378
379static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
380 char *buf)
381{
382 struct kfd_perf_attr *attr;
383
384 buf[0] = 0;
385 attr = container_of(attrs, struct kfd_perf_attr, attr);
386 if (!attr->data) /* invalid data for PMC */
387 return 0;
388 else
389 return sysfs_show_32bit_val(buf, attr->data);
390}
391
392#define KFD_PERF_DESC(_name, _data) \
393{ \
394 .attr = __ATTR(_name, 0444, perf_show, NULL), \
395 .data = _data, \
396}
397
398static struct kfd_perf_attr perf_attr_iommu[] = {
399 KFD_PERF_DESC(max_concurrent, 0),
400 KFD_PERF_DESC(num_counters, 0),
401 KFD_PERF_DESC(counter_ids, 0),
402};
403/****************************************/
404
5b5c4e40
EP
405static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
406 char *buffer)
407{
5b5c4e40
EP
408 struct kfd_topology_device *dev;
409 char public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE];
410 uint32_t i;
f7c826ad 411 uint32_t log_max_watch_addr;
5b5c4e40
EP
412
413 /* Making sure that the buffer is an empty string */
414 buffer[0] = 0;
415
416 if (strcmp(attr->name, "gpu_id") == 0) {
417 dev = container_of(attr, struct kfd_topology_device,
418 attr_gpuid);
f7c826ad
AS
419 return sysfs_show_32bit_val(buffer, dev->gpu_id);
420 }
421
422 if (strcmp(attr->name, "name") == 0) {
5b5c4e40
EP
423 dev = container_of(attr, struct kfd_topology_device,
424 attr_name);
425 for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE; i++) {
426 public_name[i] =
427 (char)dev->node_props.marketing_name[i];
428 if (dev->node_props.marketing_name[i] == 0)
429 break;
430 }
431 public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1] = 0x0;
f7c826ad
AS
432 return sysfs_show_str_val(buffer, public_name);
433 }
5b5c4e40 434
f7c826ad
AS
435 dev = container_of(attr, struct kfd_topology_device,
436 attr_props);
437 sysfs_show_32bit_prop(buffer, "cpu_cores_count",
438 dev->node_props.cpu_cores_count);
439 sysfs_show_32bit_prop(buffer, "simd_count",
440 dev->node_props.simd_count);
175b9263
FK
441 sysfs_show_32bit_prop(buffer, "mem_banks_count",
442 dev->node_props.mem_banks_count);
f7c826ad
AS
443 sysfs_show_32bit_prop(buffer, "caches_count",
444 dev->node_props.caches_count);
445 sysfs_show_32bit_prop(buffer, "io_links_count",
446 dev->node_props.io_links_count);
447 sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
448 dev->node_props.cpu_core_id_base);
449 sysfs_show_32bit_prop(buffer, "simd_id_base",
450 dev->node_props.simd_id_base);
f7c826ad
AS
451 sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
452 dev->node_props.max_waves_per_simd);
453 sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
454 dev->node_props.lds_size_in_kb);
455 sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
456 dev->node_props.gds_size_in_kb);
457 sysfs_show_32bit_prop(buffer, "wave_front_size",
458 dev->node_props.wave_front_size);
459 sysfs_show_32bit_prop(buffer, "array_count",
460 dev->node_props.array_count);
461 sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
462 dev->node_props.simd_arrays_per_engine);
463 sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
464 dev->node_props.cu_per_simd_array);
465 sysfs_show_32bit_prop(buffer, "simd_per_cu",
466 dev->node_props.simd_per_cu);
467 sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
468 dev->node_props.max_slots_scratch_cu);
469 sysfs_show_32bit_prop(buffer, "vendor_id",
470 dev->node_props.vendor_id);
471 sysfs_show_32bit_prop(buffer, "device_id",
472 dev->node_props.device_id);
473 sysfs_show_32bit_prop(buffer, "location_id",
474 dev->node_props.location_id);
7c9b7171
OZ
475 sysfs_show_32bit_prop(buffer, "drm_render_minor",
476 dev->node_props.drm_render_minor);
0c1690e3
SL
477 sysfs_show_64bit_prop(buffer, "hive_id",
478 dev->node_props.hive_id);
14568cf6
OZ
479 sysfs_show_32bit_prop(buffer, "num_sdma_engines",
480 dev->node_props.num_sdma_engines);
481 sysfs_show_32bit_prop(buffer, "num_sdma_xgmi_engines",
482 dev->node_props.num_sdma_xgmi_engines);
f7c826ad
AS
483
484 if (dev->gpu) {
485 log_max_watch_addr =
486 __ilog2_u32(dev->gpu->device_info->num_of_watch_points);
487
488 if (log_max_watch_addr) {
489 dev->node_props.capability |=
490 HSA_CAP_WATCH_POINTS_SUPPORTED;
491
492 dev->node_props.capability |=
493 ((log_max_watch_addr <<
494 HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
495 HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
5b5c4e40
EP
496 }
497
413e85d5
BG
498 if (dev->gpu->device_info->asic_family == CHIP_TONGA)
499 dev->node_props.capability |=
500 HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
501
f7c826ad 502 sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
3a87177e 503 dev->node_props.max_engine_clk_fcompute);
42e08c78 504
f7c826ad 505 sysfs_show_64bit_prop(buffer, "local_mem_size",
42e08c78 506 (unsigned long long int) 0);
f7c826ad
AS
507
508 sysfs_show_32bit_prop(buffer, "fw_version",
5ade6c9c 509 dev->gpu->mec_fw_version);
826f5de8
AS
510 sysfs_show_32bit_prop(buffer, "capability",
511 dev->node_props.capability);
5ade6c9c
FK
512 sysfs_show_32bit_prop(buffer, "sdma_fw_version",
513 dev->gpu->sdma_fw_version);
5b5c4e40
EP
514 }
515
f7c826ad
AS
516 return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
517 cpufreq_quick_get_max(0)/1000);
5b5c4e40
EP
518}
519
520static const struct sysfs_ops node_ops = {
521 .show = node_show,
522};
523
524static struct kobj_type node_type = {
5108d768 525 .release = kfd_topology_kobj_release,
5b5c4e40
EP
526 .sysfs_ops = &node_ops,
527};
528
529static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
530{
531 sysfs_remove_file(kobj, attr);
532 kobject_del(kobj);
533 kobject_put(kobj);
534}
535
536static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
537{
538 struct kfd_iolink_properties *iolink;
539 struct kfd_cache_properties *cache;
540 struct kfd_mem_properties *mem;
f4757347 541 struct kfd_perf_properties *perf;
5b5c4e40 542
5b5c4e40
EP
543 if (dev->kobj_iolink) {
544 list_for_each_entry(iolink, &dev->io_link_props, list)
545 if (iolink->kobj) {
546 kfd_remove_sysfs_file(iolink->kobj,
547 &iolink->attr);
16b9201c 548 iolink->kobj = NULL;
5b5c4e40
EP
549 }
550 kobject_del(dev->kobj_iolink);
551 kobject_put(dev->kobj_iolink);
16b9201c 552 dev->kobj_iolink = NULL;
5b5c4e40
EP
553 }
554
555 if (dev->kobj_cache) {
556 list_for_each_entry(cache, &dev->cache_props, list)
557 if (cache->kobj) {
558 kfd_remove_sysfs_file(cache->kobj,
559 &cache->attr);
16b9201c 560 cache->kobj = NULL;
5b5c4e40
EP
561 }
562 kobject_del(dev->kobj_cache);
563 kobject_put(dev->kobj_cache);
16b9201c 564 dev->kobj_cache = NULL;
5b5c4e40
EP
565 }
566
567 if (dev->kobj_mem) {
568 list_for_each_entry(mem, &dev->mem_props, list)
569 if (mem->kobj) {
570 kfd_remove_sysfs_file(mem->kobj, &mem->attr);
16b9201c 571 mem->kobj = NULL;
5b5c4e40
EP
572 }
573 kobject_del(dev->kobj_mem);
574 kobject_put(dev->kobj_mem);
16b9201c 575 dev->kobj_mem = NULL;
5b5c4e40
EP
576 }
577
f4757347
AL
578 if (dev->kobj_perf) {
579 list_for_each_entry(perf, &dev->perf_props, list) {
580 kfree(perf->attr_group);
581 perf->attr_group = NULL;
582 }
583 kobject_del(dev->kobj_perf);
584 kobject_put(dev->kobj_perf);
585 dev->kobj_perf = NULL;
586 }
587
5b5c4e40
EP
588 if (dev->kobj_node) {
589 sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
590 sysfs_remove_file(dev->kobj_node, &dev->attr_name);
591 sysfs_remove_file(dev->kobj_node, &dev->attr_props);
592 kobject_del(dev->kobj_node);
593 kobject_put(dev->kobj_node);
16b9201c 594 dev->kobj_node = NULL;
5b5c4e40
EP
595 }
596}
597
598static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
599 uint32_t id)
600{
601 struct kfd_iolink_properties *iolink;
602 struct kfd_cache_properties *cache;
603 struct kfd_mem_properties *mem;
f4757347 604 struct kfd_perf_properties *perf;
5b5c4e40 605 int ret;
f4757347
AL
606 uint32_t i, num_attrs;
607 struct attribute **attrs;
5b5c4e40 608
32fa8219
FK
609 if (WARN_ON(dev->kobj_node))
610 return -EEXIST;
611
5b5c4e40
EP
612 /*
613 * Creating the sysfs folders
614 */
5b5c4e40
EP
615 dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
616 if (!dev->kobj_node)
617 return -ENOMEM;
618
619 ret = kobject_init_and_add(dev->kobj_node, &node_type,
620 sys_props.kobj_nodes, "%d", id);
621 if (ret < 0)
622 return ret;
623
624 dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
625 if (!dev->kobj_mem)
626 return -ENOMEM;
627
628 dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
629 if (!dev->kobj_cache)
630 return -ENOMEM;
631
632 dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
633 if (!dev->kobj_iolink)
634 return -ENOMEM;
635
f4757347
AL
636 dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
637 if (!dev->kobj_perf)
638 return -ENOMEM;
639
5b5c4e40
EP
640 /*
641 * Creating sysfs files for node properties
642 */
643 dev->attr_gpuid.name = "gpu_id";
644 dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
645 sysfs_attr_init(&dev->attr_gpuid);
646 dev->attr_name.name = "name";
647 dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
648 sysfs_attr_init(&dev->attr_name);
649 dev->attr_props.name = "properties";
650 dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
651 sysfs_attr_init(&dev->attr_props);
652 ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
653 if (ret < 0)
654 return ret;
655 ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
656 if (ret < 0)
657 return ret;
658 ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
659 if (ret < 0)
660 return ret;
661
662 i = 0;
663 list_for_each_entry(mem, &dev->mem_props, list) {
664 mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
665 if (!mem->kobj)
666 return -ENOMEM;
667 ret = kobject_init_and_add(mem->kobj, &mem_type,
668 dev->kobj_mem, "%d", i);
669 if (ret < 0)
670 return ret;
671
672 mem->attr.name = "properties";
673 mem->attr.mode = KFD_SYSFS_FILE_MODE;
674 sysfs_attr_init(&mem->attr);
675 ret = sysfs_create_file(mem->kobj, &mem->attr);
676 if (ret < 0)
677 return ret;
678 i++;
679 }
680
681 i = 0;
682 list_for_each_entry(cache, &dev->cache_props, list) {
683 cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
684 if (!cache->kobj)
685 return -ENOMEM;
686 ret = kobject_init_and_add(cache->kobj, &cache_type,
687 dev->kobj_cache, "%d", i);
688 if (ret < 0)
689 return ret;
690
691 cache->attr.name = "properties";
692 cache->attr.mode = KFD_SYSFS_FILE_MODE;
693 sysfs_attr_init(&cache->attr);
694 ret = sysfs_create_file(cache->kobj, &cache->attr);
695 if (ret < 0)
696 return ret;
697 i++;
698 }
699
700 i = 0;
701 list_for_each_entry(iolink, &dev->io_link_props, list) {
702 iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
703 if (!iolink->kobj)
704 return -ENOMEM;
705 ret = kobject_init_and_add(iolink->kobj, &iolink_type,
706 dev->kobj_iolink, "%d", i);
707 if (ret < 0)
708 return ret;
709
710 iolink->attr.name = "properties";
711 iolink->attr.mode = KFD_SYSFS_FILE_MODE;
712 sysfs_attr_init(&iolink->attr);
713 ret = sysfs_create_file(iolink->kobj, &iolink->attr);
714 if (ret < 0)
715 return ret;
716 i++;
f4757347
AL
717 }
718
719 /* All hardware blocks have the same number of attributes. */
3f866f5f 720 num_attrs = ARRAY_SIZE(perf_attr_iommu);
f4757347
AL
721 list_for_each_entry(perf, &dev->perf_props, list) {
722 perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
723 * num_attrs + sizeof(struct attribute_group),
724 GFP_KERNEL);
725 if (!perf->attr_group)
726 return -ENOMEM;
727
728 attrs = (struct attribute **)(perf->attr_group + 1);
729 if (!strcmp(perf->block_name, "iommu")) {
730 /* Information of IOMMU's num_counters and counter_ids is shown
731 * under /sys/bus/event_source/devices/amd_iommu. We don't
732 * duplicate here.
733 */
734 perf_attr_iommu[0].data = perf->max_concurrent;
735 for (i = 0; i < num_attrs; i++)
736 attrs[i] = &perf_attr_iommu[i].attr.attr;
737 }
738 perf->attr_group->name = perf->block_name;
739 perf->attr_group->attrs = attrs;
740 ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
741 if (ret < 0)
742 return ret;
743 }
5b5c4e40
EP
744
745 return 0;
746}
747
3a87177e 748/* Called with write topology lock acquired */
5b5c4e40
EP
749static int kfd_build_sysfs_node_tree(void)
750{
751 struct kfd_topology_device *dev;
752 int ret;
753 uint32_t i = 0;
754
755 list_for_each_entry(dev, &topology_device_list, list) {
8dfead6c 756 ret = kfd_build_sysfs_node_entry(dev, i);
5b5c4e40
EP
757 if (ret < 0)
758 return ret;
759 i++;
760 }
761
762 return 0;
763}
764
3a87177e 765/* Called with write topology lock acquired */
5b5c4e40
EP
766static void kfd_remove_sysfs_node_tree(void)
767{
768 struct kfd_topology_device *dev;
769
770 list_for_each_entry(dev, &topology_device_list, list)
771 kfd_remove_sysfs_node_entry(dev);
772}
773
774static int kfd_topology_update_sysfs(void)
775{
776 int ret;
777
778 pr_info("Creating topology SYSFS entries\n");
4eacc26b 779 if (!sys_props.kobj_topology) {
5b5c4e40
EP
780 sys_props.kobj_topology =
781 kfd_alloc_struct(sys_props.kobj_topology);
782 if (!sys_props.kobj_topology)
783 return -ENOMEM;
784
785 ret = kobject_init_and_add(sys_props.kobj_topology,
786 &sysprops_type, &kfd_device->kobj,
787 "topology");
788 if (ret < 0)
789 return ret;
790
791 sys_props.kobj_nodes = kobject_create_and_add("nodes",
792 sys_props.kobj_topology);
793 if (!sys_props.kobj_nodes)
794 return -ENOMEM;
795
796 sys_props.attr_genid.name = "generation_id";
797 sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
798 sysfs_attr_init(&sys_props.attr_genid);
799 ret = sysfs_create_file(sys_props.kobj_topology,
800 &sys_props.attr_genid);
801 if (ret < 0)
802 return ret;
803
804 sys_props.attr_props.name = "system_properties";
805 sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
806 sysfs_attr_init(&sys_props.attr_props);
807 ret = sysfs_create_file(sys_props.kobj_topology,
808 &sys_props.attr_props);
809 if (ret < 0)
810 return ret;
811 }
812
813 kfd_remove_sysfs_node_tree();
814
815 return kfd_build_sysfs_node_tree();
816}
817
818static void kfd_topology_release_sysfs(void)
819{
820 kfd_remove_sysfs_node_tree();
821 if (sys_props.kobj_topology) {
822 sysfs_remove_file(sys_props.kobj_topology,
823 &sys_props.attr_genid);
824 sysfs_remove_file(sys_props.kobj_topology,
825 &sys_props.attr_props);
826 if (sys_props.kobj_nodes) {
827 kobject_del(sys_props.kobj_nodes);
828 kobject_put(sys_props.kobj_nodes);
16b9201c 829 sys_props.kobj_nodes = NULL;
5b5c4e40
EP
830 }
831 kobject_del(sys_props.kobj_topology);
832 kobject_put(sys_props.kobj_topology);
16b9201c 833 sys_props.kobj_topology = NULL;
5b5c4e40
EP
834 }
835}
836
4f449311
HK
837/* Called with write topology_lock acquired */
838static void kfd_topology_update_device_list(struct list_head *temp_list,
839 struct list_head *master_list)
840{
841 while (!list_empty(temp_list)) {
842 list_move_tail(temp_list->next, master_list);
843 sys_props.num_devices++;
844 }
845}
846
520b8fb7
FK
847static void kfd_debug_print_topology(void)
848{
849 struct kfd_topology_device *dev;
850
851 down_read(&topology_lock);
852
853 dev = list_last_entry(&topology_device_list,
854 struct kfd_topology_device, list);
855 if (dev) {
856 if (dev->node_props.cpu_cores_count &&
857 dev->node_props.simd_count) {
858 pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
859 dev->node_props.device_id,
860 dev->node_props.vendor_id);
861 } else if (dev->node_props.cpu_cores_count)
862 pr_info("Topology: Add CPU node\n");
863 else if (dev->node_props.simd_count)
864 pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
865 dev->node_props.device_id,
866 dev->node_props.vendor_id);
867 }
868 up_read(&topology_lock);
869}
870
871/* Helper function for intializing platform_xx members of
872 * kfd_system_properties. Uses OEM info from the last CPU/APU node.
873 */
874static void kfd_update_system_properties(void)
875{
876 struct kfd_topology_device *dev;
877
878 down_read(&topology_lock);
879 dev = list_last_entry(&topology_device_list,
880 struct kfd_topology_device, list);
881 if (dev) {
882 sys_props.platform_id =
883 (*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
884 sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
885 sys_props.platform_rev = dev->oem_revision;
886 }
887 up_read(&topology_lock);
888}
889
890static void find_system_memory(const struct dmi_header *dm,
891 void *private)
892{
893 struct kfd_mem_properties *mem;
894 u16 mem_width, mem_clock;
895 struct kfd_topology_device *kdev =
896 (struct kfd_topology_device *)private;
897 const u8 *dmi_data = (const u8 *)(dm + 1);
898
899 if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
900 mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
901 mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
902 list_for_each_entry(mem, &kdev->mem_props, list) {
903 if (mem_width != 0xFFFF && mem_width != 0)
904 mem->width = mem_width;
905 if (mem_clock != 0)
906 mem->mem_clk_max = mem_clock;
907 }
908 }
909}
f4757347
AL
910
911/*
912 * Performance counters information is not part of CRAT but we would like to
913 * put them in the sysfs under topology directory for Thunk to get the data.
914 * This function is called before updating the sysfs.
915 */
916static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
917{
64d1c3a4
FK
918 /* These are the only counters supported so far */
919 return kfd_iommu_add_perf_counters(kdev);
f4757347
AL
920}
921
520b8fb7
FK
922/* kfd_add_non_crat_information - Add information that is not currently
923 * defined in CRAT but is necessary for KFD topology
924 * @dev - topology device to which addition info is added
925 */
926static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
927{
928 /* Check if CPU only node. */
929 if (!kdev->gpu) {
930 /* Add system memory information */
931 dmi_walk(find_system_memory, kdev);
932 }
933 /* TODO: For GPU node, rearrange code from kfd_topology_add_device */
934}
935
b441093e
HK
936/* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
937 * Ignore CRAT for all other devices. AMD APU is identified if both CPU
938 * and GPU cores are present.
939 * @device_list - topology device list created by parsing ACPI CRAT table.
940 * @return - TRUE if invalid, FALSE is valid.
941 */
942static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
943{
944 struct kfd_topology_device *dev;
945
946 list_for_each_entry(dev, device_list, list) {
947 if (dev->node_props.cpu_cores_count &&
948 dev->node_props.simd_count)
949 return false;
950 }
951 pr_info("Ignoring ACPI CRAT on non-APU system\n");
952 return true;
953}
954
5b5c4e40
EP
955int kfd_topology_init(void)
956{
16b9201c 957 void *crat_image = NULL;
5b5c4e40
EP
958 size_t image_size = 0;
959 int ret;
4f449311 960 struct list_head temp_topology_device_list;
520b8fb7
FK
961 int cpu_only_node = 0;
962 struct kfd_topology_device *kdev;
963 int proximity_domain;
5b5c4e40 964
4f449311
HK
965 /* topology_device_list - Master list of all topology devices
966 * temp_topology_device_list - temporary list created while parsing CRAT
967 * or VCRAT. Once parsing is complete the contents of list is moved to
968 * topology_device_list
5b5c4e40 969 */
4f449311
HK
970
971 /* Initialize the head for the both the lists */
5b5c4e40 972 INIT_LIST_HEAD(&topology_device_list);
4f449311 973 INIT_LIST_HEAD(&temp_topology_device_list);
5b5c4e40 974 init_rwsem(&topology_lock);
5b5c4e40
EP
975
976 memset(&sys_props, 0, sizeof(sys_props));
977
520b8fb7
FK
978 /* Proximity domains in ACPI CRAT tables start counting at
979 * 0. The same should be true for virtual CRAT tables created
980 * at this stage. GPUs added later in kfd_topology_add_device
981 * use a counter.
982 */
983 proximity_domain = 0;
984
5b5c4e40 985 /*
520b8fb7 986 * Get the CRAT image from the ACPI. If ACPI doesn't have one
b441093e 987 * or if ACPI CRAT is invalid create a virtual CRAT.
520b8fb7
FK
988 * NOTE: The current implementation expects all AMD APUs to have
989 * CRAT. If no CRAT is available, it is assumed to be a CPU
5b5c4e40 990 */
8e05247d
HK
991 ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
992 if (!ret) {
4f449311 993 ret = kfd_parse_crat_table(crat_image,
520b8fb7
FK
994 &temp_topology_device_list,
995 proximity_domain);
b441093e
HK
996 if (ret ||
997 kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
520b8fb7
FK
998 kfd_release_topology_device_list(
999 &temp_topology_device_list);
1000 kfd_destroy_crat_image(crat_image);
1001 crat_image = NULL;
1002 }
1003 }
1004
1005 if (!crat_image) {
1006 ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
1007 COMPUTE_UNIT_CPU, NULL,
1008 proximity_domain);
1009 cpu_only_node = 1;
1010 if (ret) {
1011 pr_err("Error creating VCRAT table for CPU\n");
1012 return ret;
1013 }
1014
1015 ret = kfd_parse_crat_table(crat_image,
1016 &temp_topology_device_list,
1017 proximity_domain);
1018 if (ret) {
1019 pr_err("Error parsing VCRAT table for CPU\n");
5b5c4e40 1020 goto err;
520b8fb7 1021 }
5b5c4e40
EP
1022 }
1023
f4757347
AL
1024 kdev = list_first_entry(&temp_topology_device_list,
1025 struct kfd_topology_device, list);
1026 kfd_add_perf_to_topology(kdev);
1027
8e05247d 1028 down_write(&topology_lock);
4f449311
HK
1029 kfd_topology_update_device_list(&temp_topology_device_list,
1030 &topology_device_list);
520b8fb7 1031 atomic_set(&topology_crat_proximity_domain, sys_props.num_devices-1);
8e05247d
HK
1032 ret = kfd_topology_update_sysfs();
1033 up_write(&topology_lock);
1034
4f449311
HK
1035 if (!ret) {
1036 sys_props.generation_count++;
520b8fb7
FK
1037 kfd_update_system_properties();
1038 kfd_debug_print_topology();
8e05247d 1039 pr_info("Finished initializing topology\n");
4f449311 1040 } else
8e05247d
HK
1041 pr_err("Failed to update topology in sysfs ret=%d\n", ret);
1042
520b8fb7
FK
1043 /* For nodes with GPU, this information gets added
1044 * when GPU is detected (kfd_topology_add_device).
1045 */
1046 if (cpu_only_node) {
1047 /* Add additional information to CPU only node created above */
1048 down_write(&topology_lock);
1049 kdev = list_first_entry(&topology_device_list,
1050 struct kfd_topology_device, list);
1051 up_write(&topology_lock);
1052 kfd_add_non_crat_information(kdev);
1053 }
1054
5b5c4e40 1055err:
8e05247d 1056 kfd_destroy_crat_image(crat_image);
5b5c4e40
EP
1057 return ret;
1058}
1059
1060void kfd_topology_shutdown(void)
1061{
4f449311 1062 down_write(&topology_lock);
5b5c4e40
EP
1063 kfd_topology_release_sysfs();
1064 kfd_release_live_view();
4f449311 1065 up_write(&topology_lock);
5b5c4e40
EP
1066}
1067
5b5c4e40
EP
1068static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
1069{
1070 uint32_t hashout;
1071 uint32_t buf[7];
585f0e6c 1072 uint64_t local_mem_size;
5b5c4e40 1073 int i;
0504cccf 1074 struct kfd_local_mem_info local_mem_info;
5b5c4e40
EP
1075
1076 if (!gpu)
1077 return 0;
1078
7cd52c91 1079 amdgpu_amdkfd_get_local_mem_info(gpu->kgd, &local_mem_info);
0504cccf
HK
1080
1081 local_mem_size = local_mem_info.local_mem_size_private +
1082 local_mem_info.local_mem_size_public;
585f0e6c 1083
5b5c4e40
EP
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;
585f0e6c
EC
1089 buf[5] = lower_32_bits(local_mem_size);
1090 buf[6] = upper_32_bits(local_mem_size);
5b5c4e40
EP
1091
1092 for (i = 0, hashout = 0; i < 7; i++)
1093 hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
1094
1095 return hashout;
1096}
3a87177e
HK
1097/* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
1098 * the GPU device is not already present in the topology device
1099 * list then return NULL. This means a new topology device has to
1100 * be created for this GPU.
3a87177e 1101 */
5b5c4e40
EP
1102static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
1103{
1104 struct kfd_topology_device *dev;
16b9201c 1105 struct kfd_topology_device *out_dev = NULL;
5b5c4e40 1106
3a87177e 1107 down_write(&topology_lock);
b8fe0524
FK
1108 list_for_each_entry(dev, &topology_device_list, list) {
1109 /* Discrete GPUs need their own topology device list
1110 * entries. Don't assign them to CPU/APU nodes.
1111 */
1112 if (!gpu->device_info->needs_iommu_device &&
1113 dev->node_props.cpu_cores_count)
1114 continue;
1115
4eacc26b 1116 if (!dev->gpu && (dev->node_props.simd_count > 0)) {
5b5c4e40
EP
1117 dev->gpu = gpu;
1118 out_dev = dev;
1119 break;
1120 }
b8fe0524 1121 }
3a87177e 1122 up_write(&topology_lock);
5b5c4e40
EP
1123 return out_dev;
1124}
1125
1126static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
1127{
1128 /*
1129 * TODO: Generate an event for thunk about the arrival/removal
1130 * of the GPU
1131 */
1132}
1133
3a87177e
HK
1134/* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
1135 * patch this after CRAT parsing.
1136 */
1137static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
1138{
1139 struct kfd_mem_properties *mem;
1140 struct kfd_local_mem_info local_mem_info;
1141
1142 if (!dev)
1143 return;
1144
1145 /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
1146 * single bank of VRAM local memory.
1147 * for dGPUs - VCRAT reports only one bank of Local Memory
1148 * for APUs - If CRAT from ACPI reports more than one bank, then
1149 * all the banks will report the same mem_clk_max information
1150 */
7cd52c91 1151 amdgpu_amdkfd_get_local_mem_info(dev->gpu->kgd, &local_mem_info);
3a87177e
HK
1152
1153 list_for_each_entry(mem, &dev->mem_props, list)
1154 mem->mem_clk_max = local_mem_info.mem_clk_max;
1155}
1156
1157static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
1158{
d35f00d8
EH
1159 struct kfd_iolink_properties *link, *cpu_link;
1160 struct kfd_topology_device *cpu_dev;
1161 uint32_t cap;
1162 uint32_t cpu_flag = CRAT_IOLINK_FLAGS_ENABLED;
1163 uint32_t flag = CRAT_IOLINK_FLAGS_ENABLED;
3a87177e
HK
1164
1165 if (!dev || !dev->gpu)
1166 return;
1167
d35f00d8
EH
1168 pcie_capability_read_dword(dev->gpu->pdev,
1169 PCI_EXP_DEVCAP2, &cap);
1170
1171 if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1172 PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1173 cpu_flag |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1174 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1175
1176 if (!dev->gpu->pci_atomic_requested ||
1177 dev->gpu->device_info->asic_family == CHIP_HAWAII)
1178 flag |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1179 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1180
1181 /* GPU only creates direct links so apply flags setting to all */
1182 list_for_each_entry(link, &dev->io_link_props, list) {
1183 link->flags = flag;
1184 cpu_dev = kfd_topology_device_by_proximity_domain(
1185 link->node_to);
1186 if (cpu_dev) {
1187 list_for_each_entry(cpu_link,
1188 &cpu_dev->io_link_props, list)
1189 if (cpu_link->node_to == link->node_from)
1190 cpu_link->flags = cpu_flag;
1191 }
1192 }
3a87177e
HK
1193}
1194
5b5c4e40
EP
1195int kfd_topology_add_device(struct kfd_dev *gpu)
1196{
1197 uint32_t gpu_id;
1198 struct kfd_topology_device *dev;
f7ce2fad 1199 struct kfd_cu_info cu_info;
4f449311
HK
1200 int res = 0;
1201 struct list_head temp_topology_device_list;
3a87177e
HK
1202 void *crat_image = NULL;
1203 size_t image_size = 0;
1204 int proximity_domain;
0dee45a2 1205 struct amdgpu_ras *ctx;
4f449311
HK
1206
1207 INIT_LIST_HEAD(&temp_topology_device_list);
5b5c4e40 1208
5b5c4e40
EP
1209 gpu_id = kfd_generate_gpu_id(gpu);
1210
79775b62 1211 pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
5b5c4e40 1212
3a87177e
HK
1213 proximity_domain = atomic_inc_return(&topology_crat_proximity_domain);
1214
1215 /* Check to see if this gpu device exists in the topology_device_list.
1216 * If so, assign the gpu to that device,
1217 * else create a Virtual CRAT for this gpu device and then parse that
1218 * CRAT to create a new topology device. Once created assign the gpu to
1219 * that topology device
5b5c4e40
EP
1220 */
1221 dev = kfd_assign_gpu(gpu);
1222 if (!dev) {
3a87177e
HK
1223 res = kfd_create_crat_image_virtual(&crat_image, &image_size,
1224 COMPUTE_UNIT_GPU, gpu,
1225 proximity_domain);
1226 if (res) {
1227 pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
1228 gpu_id);
1229 return res;
1230 }
1231 res = kfd_parse_crat_table(crat_image,
1232 &temp_topology_device_list,
1233 proximity_domain);
1234 if (res) {
1235 pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
1236 gpu_id);
5b5c4e40
EP
1237 goto err;
1238 }
4f449311 1239
4f449311
HK
1240 down_write(&topology_lock);
1241 kfd_topology_update_device_list(&temp_topology_device_list,
1242 &topology_device_list);
1243
8eabaf54
KR
1244 /* Update the SYSFS tree, since we added another topology
1245 * device
5b5c4e40 1246 */
3a87177e 1247 res = kfd_topology_update_sysfs();
4f449311
HK
1248 up_write(&topology_lock);
1249
3a87177e
HK
1250 if (!res)
1251 sys_props.generation_count++;
1252 else
1253 pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
1254 gpu_id, res);
1255 dev = kfd_assign_gpu(gpu);
1256 if (WARN_ON(!dev)) {
1257 res = -ENODEV;
1258 goto err;
1259 }
5b5c4e40
EP
1260 }
1261
1262 dev->gpu_id = gpu_id;
1263 gpu->id = gpu_id;
3a87177e
HK
1264
1265 /* TODO: Move the following lines to function
1266 * kfd_add_non_crat_information
1267 */
1268
1269 /* Fill-in additional information that is not available in CRAT but
1270 * needed for the topology
1271 */
1272
7cd52c91 1273 amdgpu_amdkfd_get_cu_info(dev->gpu->kgd, &cu_info);
3a87177e
HK
1274 dev->node_props.simd_arrays_per_engine =
1275 cu_info.num_shader_arrays_per_engine;
1276
5b5c4e40
EP
1277 dev->node_props.vendor_id = gpu->pdev->vendor;
1278 dev->node_props.device_id = gpu->pdev->device;
babe2ef3 1279 dev->node_props.location_id = pci_dev_id(gpu->pdev);
3a87177e 1280 dev->node_props.max_engine_clk_fcompute =
7cd52c91 1281 amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->kgd);
3a87177e
HK
1282 dev->node_props.max_engine_clk_ccompute =
1283 cpufreq_quick_get_max(0) / 1000;
7c9b7171
OZ
1284 dev->node_props.drm_render_minor =
1285 gpu->shared_resources.drm_render_minor;
3a87177e 1286
0c1690e3 1287 dev->node_props.hive_id = gpu->hive_id;
14568cf6
OZ
1288 dev->node_props.num_sdma_engines = gpu->device_info->num_sdma_engines;
1289 dev->node_props.num_sdma_xgmi_engines =
1290 gpu->device_info->num_xgmi_sdma_engines;
0c1690e3 1291
3a87177e
HK
1292 kfd_fill_mem_clk_max_info(dev);
1293 kfd_fill_iolink_non_crat_info(dev);
1294
1295 switch (dev->gpu->device_info->asic_family) {
1296 case CHIP_KAVERI:
1297 case CHIP_HAWAII:
1298 case CHIP_TONGA:
1299 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
1300 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1301 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1302 break;
1303 case CHIP_CARRIZO:
1304 case CHIP_FIJI:
1305 case CHIP_POLARIS10:
1306 case CHIP_POLARIS11:
846a44d7 1307 case CHIP_POLARIS12:
ed81cd6e 1308 case CHIP_VEGAM:
42aa8793 1309 pr_debug("Adding doorbell packet type capability\n");
3a87177e
HK
1310 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
1311 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1312 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1313 break;
389056e5 1314 case CHIP_VEGA10:
846a44d7 1315 case CHIP_VEGA12:
22a3a294 1316 case CHIP_VEGA20:
389056e5
FK
1317 case CHIP_RAVEN:
1318 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1319 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1320 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1321 break;
3a87177e
HK
1322 default:
1323 WARN(1, "Unexpected ASIC family %u",
1324 dev->gpu->device_info->asic_family);
7639a8c4
BG
1325 }
1326
3a87177e
HK
1327 /* Fix errors in CZ CRAT.
1328 * simd_count: Carrizo CRAT reports wrong simd_count, probably
1329 * because it doesn't consider masked out CUs
70f372bf
PC
1330 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
1331 * capability flag: Carrizo CRAT doesn't report IOMMU flags
3a87177e 1332 */
70f372bf 1333 if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
3a87177e
HK
1334 dev->node_props.simd_count =
1335 cu_info.simd_per_cu * cu_info.cu_active_number;
70f372bf
PC
1336 dev->node_props.max_waves_per_simd = 10;
1337 dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
1338 }
3a87177e 1339
0dee45a2
EH
1340 ctx = amdgpu_ras_get_context((struct amdgpu_device *)(dev->gpu->kgd));
1341 if (ctx) {
1342 /* kfd only concerns sram ecc on GFX/SDMA and HBM ecc on UMC */
1343 dev->node_props.capability |=
1344 (((ctx->features & BIT(AMDGPU_RAS_BLOCK__SDMA)) != 0) ||
1345 ((ctx->features & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0)) ?
1346 HSA_CAP_SRAM_EDCSUPPORTED : 0;
1347 dev->node_props.capability |= ((ctx->features & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
1348 HSA_CAP_MEM_EDCSUPPORTED : 0;
1349
1350 dev->node_props.capability |= (ctx->features != 0) ?
1351 HSA_CAP_RASEVENTNOTIFY : 0;
1352 }
1353
3a87177e
HK
1354 kfd_debug_print_topology();
1355
4f449311 1356 if (!res)
5b5c4e40 1357 kfd_notify_gpu_change(gpu_id, 1);
4f449311 1358err:
3a87177e 1359 kfd_destroy_crat_image(crat_image);
5b5c4e40
EP
1360 return res;
1361}
1362
1363int kfd_topology_remove_device(struct kfd_dev *gpu)
1364{
4f449311 1365 struct kfd_topology_device *dev, *tmp;
5b5c4e40
EP
1366 uint32_t gpu_id;
1367 int res = -ENODEV;
1368
5b5c4e40
EP
1369 down_write(&topology_lock);
1370
4f449311 1371 list_for_each_entry_safe(dev, tmp, &topology_device_list, list)
5b5c4e40
EP
1372 if (dev->gpu == gpu) {
1373 gpu_id = dev->gpu_id;
1374 kfd_remove_sysfs_node_entry(dev);
1375 kfd_release_topology_device(dev);
4f449311 1376 sys_props.num_devices--;
5b5c4e40
EP
1377 res = 0;
1378 if (kfd_topology_update_sysfs() < 0)
1379 kfd_topology_release_sysfs();
1380 break;
1381 }
1382
1383 up_write(&topology_lock);
1384
174de876 1385 if (!res)
5b5c4e40
EP
1386 kfd_notify_gpu_change(gpu_id, 0);
1387
1388 return res;
1389}
1390
6d82eb0e
HK
1391/* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
1392 * topology. If GPU device is found @idx, then valid kfd_dev pointer is
1393 * returned through @kdev
1394 * Return - 0: On success (@kdev will be NULL for non GPU nodes)
1395 * -1: If end of list
5b5c4e40 1396 */
6d82eb0e 1397int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
5b5c4e40
EP
1398{
1399
1400 struct kfd_topology_device *top_dev;
5b5c4e40
EP
1401 uint8_t device_idx = 0;
1402
6d82eb0e 1403 *kdev = NULL;
5b5c4e40
EP
1404 down_read(&topology_lock);
1405
1406 list_for_each_entry(top_dev, &topology_device_list, list) {
1407 if (device_idx == idx) {
6d82eb0e
HK
1408 *kdev = top_dev->gpu;
1409 up_read(&topology_lock);
1410 return 0;
5b5c4e40
EP
1411 }
1412
1413 device_idx++;
1414 }
1415
1416 up_read(&topology_lock);
1417
6d82eb0e 1418 return -1;
5b5c4e40
EP
1419
1420}
851a645e 1421
520b8fb7
FK
1422static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
1423{
520b8fb7
FK
1424 int first_cpu_of_numa_node;
1425
1426 if (!cpumask || cpumask == cpu_none_mask)
1427 return -1;
1428 first_cpu_of_numa_node = cpumask_first(cpumask);
1429 if (first_cpu_of_numa_node >= nr_cpu_ids)
1430 return -1;
df1dd4f4
FK
1431#ifdef CONFIG_X86_64
1432 return cpu_data(first_cpu_of_numa_node).apicid;
1433#else
1434 return first_cpu_of_numa_node;
1435#endif
520b8fb7
FK
1436}
1437
1438/* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
1439 * of the given NUMA node (numa_node_id)
1440 * Return -1 on failure
1441 */
1442int kfd_numa_node_to_apic_id(int numa_node_id)
1443{
1444 if (numa_node_id == -1) {
1445 pr_warn("Invalid NUMA Node. Use online CPU mask\n");
1446 return kfd_cpumask_to_apic_id(cpu_online_mask);
1447 }
1448 return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
1449}
1450
851a645e
FK
1451#if defined(CONFIG_DEBUG_FS)
1452
1453int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
1454{
1455 struct kfd_topology_device *dev;
1456 unsigned int i = 0;
1457 int r = 0;
1458
1459 down_read(&topology_lock);
1460
1461 list_for_each_entry(dev, &topology_device_list, list) {
1462 if (!dev->gpu) {
1463 i++;
1464 continue;
1465 }
1466
1467 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1468 r = dqm_debugfs_hqds(m, dev->gpu->dqm);
1469 if (r)
1470 break;
1471 }
1472
1473 up_read(&topology_lock);
1474
1475 return r;
1476}
1477
1478int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
1479{
1480 struct kfd_topology_device *dev;
1481 unsigned int i = 0;
1482 int r = 0;
1483
1484 down_read(&topology_lock);
1485
1486 list_for_each_entry(dev, &topology_device_list, list) {
1487 if (!dev->gpu) {
1488 i++;
1489 continue;
1490 }
1491
1492 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1493 r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets);
1494 if (r)
1495 break;
1496 }
1497
1498 up_read(&topology_lock);
1499
1500 return r;
1501}
1502
1503#endif