drm/amdkfd: Fix sdma queue allocate race condition
[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);
29e76462
OZ
457 sysfs_show_32bit_prop(buffer, "num_gws",
458 dev->node_props.num_gws);
f7c826ad
AS
459 sysfs_show_32bit_prop(buffer, "wave_front_size",
460 dev->node_props.wave_front_size);
461 sysfs_show_32bit_prop(buffer, "array_count",
462 dev->node_props.array_count);
463 sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
464 dev->node_props.simd_arrays_per_engine);
465 sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
466 dev->node_props.cu_per_simd_array);
467 sysfs_show_32bit_prop(buffer, "simd_per_cu",
468 dev->node_props.simd_per_cu);
469 sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
470 dev->node_props.max_slots_scratch_cu);
471 sysfs_show_32bit_prop(buffer, "vendor_id",
472 dev->node_props.vendor_id);
473 sysfs_show_32bit_prop(buffer, "device_id",
474 dev->node_props.device_id);
475 sysfs_show_32bit_prop(buffer, "location_id",
476 dev->node_props.location_id);
7c9b7171
OZ
477 sysfs_show_32bit_prop(buffer, "drm_render_minor",
478 dev->node_props.drm_render_minor);
0c1690e3
SL
479 sysfs_show_64bit_prop(buffer, "hive_id",
480 dev->node_props.hive_id);
14568cf6
OZ
481 sysfs_show_32bit_prop(buffer, "num_sdma_engines",
482 dev->node_props.num_sdma_engines);
483 sysfs_show_32bit_prop(buffer, "num_sdma_xgmi_engines",
484 dev->node_props.num_sdma_xgmi_engines);
f7c826ad
AS
485
486 if (dev->gpu) {
487 log_max_watch_addr =
488 __ilog2_u32(dev->gpu->device_info->num_of_watch_points);
489
490 if (log_max_watch_addr) {
491 dev->node_props.capability |=
492 HSA_CAP_WATCH_POINTS_SUPPORTED;
493
494 dev->node_props.capability |=
495 ((log_max_watch_addr <<
496 HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
497 HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
5b5c4e40
EP
498 }
499
413e85d5
BG
500 if (dev->gpu->device_info->asic_family == CHIP_TONGA)
501 dev->node_props.capability |=
502 HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
503
f7c826ad 504 sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
3a87177e 505 dev->node_props.max_engine_clk_fcompute);
42e08c78 506
f7c826ad 507 sysfs_show_64bit_prop(buffer, "local_mem_size",
42e08c78 508 (unsigned long long int) 0);
f7c826ad
AS
509
510 sysfs_show_32bit_prop(buffer, "fw_version",
5ade6c9c 511 dev->gpu->mec_fw_version);
826f5de8
AS
512 sysfs_show_32bit_prop(buffer, "capability",
513 dev->node_props.capability);
5ade6c9c
FK
514 sysfs_show_32bit_prop(buffer, "sdma_fw_version",
515 dev->gpu->sdma_fw_version);
5b5c4e40
EP
516 }
517
f7c826ad
AS
518 return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
519 cpufreq_quick_get_max(0)/1000);
5b5c4e40
EP
520}
521
522static const struct sysfs_ops node_ops = {
523 .show = node_show,
524};
525
526static struct kobj_type node_type = {
5108d768 527 .release = kfd_topology_kobj_release,
5b5c4e40
EP
528 .sysfs_ops = &node_ops,
529};
530
531static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
532{
533 sysfs_remove_file(kobj, attr);
534 kobject_del(kobj);
535 kobject_put(kobj);
536}
537
538static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
539{
540 struct kfd_iolink_properties *iolink;
541 struct kfd_cache_properties *cache;
542 struct kfd_mem_properties *mem;
f4757347 543 struct kfd_perf_properties *perf;
5b5c4e40 544
5b5c4e40
EP
545 if (dev->kobj_iolink) {
546 list_for_each_entry(iolink, &dev->io_link_props, list)
547 if (iolink->kobj) {
548 kfd_remove_sysfs_file(iolink->kobj,
549 &iolink->attr);
16b9201c 550 iolink->kobj = NULL;
5b5c4e40
EP
551 }
552 kobject_del(dev->kobj_iolink);
553 kobject_put(dev->kobj_iolink);
16b9201c 554 dev->kobj_iolink = NULL;
5b5c4e40
EP
555 }
556
557 if (dev->kobj_cache) {
558 list_for_each_entry(cache, &dev->cache_props, list)
559 if (cache->kobj) {
560 kfd_remove_sysfs_file(cache->kobj,
561 &cache->attr);
16b9201c 562 cache->kobj = NULL;
5b5c4e40
EP
563 }
564 kobject_del(dev->kobj_cache);
565 kobject_put(dev->kobj_cache);
16b9201c 566 dev->kobj_cache = NULL;
5b5c4e40
EP
567 }
568
569 if (dev->kobj_mem) {
570 list_for_each_entry(mem, &dev->mem_props, list)
571 if (mem->kobj) {
572 kfd_remove_sysfs_file(mem->kobj, &mem->attr);
16b9201c 573 mem->kobj = NULL;
5b5c4e40
EP
574 }
575 kobject_del(dev->kobj_mem);
576 kobject_put(dev->kobj_mem);
16b9201c 577 dev->kobj_mem = NULL;
5b5c4e40
EP
578 }
579
f4757347
AL
580 if (dev->kobj_perf) {
581 list_for_each_entry(perf, &dev->perf_props, list) {
582 kfree(perf->attr_group);
583 perf->attr_group = NULL;
584 }
585 kobject_del(dev->kobj_perf);
586 kobject_put(dev->kobj_perf);
587 dev->kobj_perf = NULL;
588 }
589
5b5c4e40
EP
590 if (dev->kobj_node) {
591 sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
592 sysfs_remove_file(dev->kobj_node, &dev->attr_name);
593 sysfs_remove_file(dev->kobj_node, &dev->attr_props);
594 kobject_del(dev->kobj_node);
595 kobject_put(dev->kobj_node);
16b9201c 596 dev->kobj_node = NULL;
5b5c4e40
EP
597 }
598}
599
600static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
601 uint32_t id)
602{
603 struct kfd_iolink_properties *iolink;
604 struct kfd_cache_properties *cache;
605 struct kfd_mem_properties *mem;
f4757347 606 struct kfd_perf_properties *perf;
5b5c4e40 607 int ret;
f4757347
AL
608 uint32_t i, num_attrs;
609 struct attribute **attrs;
5b5c4e40 610
32fa8219
FK
611 if (WARN_ON(dev->kobj_node))
612 return -EEXIST;
613
5b5c4e40
EP
614 /*
615 * Creating the sysfs folders
616 */
5b5c4e40
EP
617 dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
618 if (!dev->kobj_node)
619 return -ENOMEM;
620
621 ret = kobject_init_and_add(dev->kobj_node, &node_type,
622 sys_props.kobj_nodes, "%d", id);
623 if (ret < 0)
624 return ret;
625
626 dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
627 if (!dev->kobj_mem)
628 return -ENOMEM;
629
630 dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
631 if (!dev->kobj_cache)
632 return -ENOMEM;
633
634 dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
635 if (!dev->kobj_iolink)
636 return -ENOMEM;
637
f4757347
AL
638 dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
639 if (!dev->kobj_perf)
640 return -ENOMEM;
641
5b5c4e40
EP
642 /*
643 * Creating sysfs files for node properties
644 */
645 dev->attr_gpuid.name = "gpu_id";
646 dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
647 sysfs_attr_init(&dev->attr_gpuid);
648 dev->attr_name.name = "name";
649 dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
650 sysfs_attr_init(&dev->attr_name);
651 dev->attr_props.name = "properties";
652 dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
653 sysfs_attr_init(&dev->attr_props);
654 ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
655 if (ret < 0)
656 return ret;
657 ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
658 if (ret < 0)
659 return ret;
660 ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
661 if (ret < 0)
662 return ret;
663
664 i = 0;
665 list_for_each_entry(mem, &dev->mem_props, list) {
666 mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
667 if (!mem->kobj)
668 return -ENOMEM;
669 ret = kobject_init_and_add(mem->kobj, &mem_type,
670 dev->kobj_mem, "%d", i);
671 if (ret < 0)
672 return ret;
673
674 mem->attr.name = "properties";
675 mem->attr.mode = KFD_SYSFS_FILE_MODE;
676 sysfs_attr_init(&mem->attr);
677 ret = sysfs_create_file(mem->kobj, &mem->attr);
678 if (ret < 0)
679 return ret;
680 i++;
681 }
682
683 i = 0;
684 list_for_each_entry(cache, &dev->cache_props, list) {
685 cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
686 if (!cache->kobj)
687 return -ENOMEM;
688 ret = kobject_init_and_add(cache->kobj, &cache_type,
689 dev->kobj_cache, "%d", i);
690 if (ret < 0)
691 return ret;
692
693 cache->attr.name = "properties";
694 cache->attr.mode = KFD_SYSFS_FILE_MODE;
695 sysfs_attr_init(&cache->attr);
696 ret = sysfs_create_file(cache->kobj, &cache->attr);
697 if (ret < 0)
698 return ret;
699 i++;
700 }
701
702 i = 0;
703 list_for_each_entry(iolink, &dev->io_link_props, list) {
704 iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
705 if (!iolink->kobj)
706 return -ENOMEM;
707 ret = kobject_init_and_add(iolink->kobj, &iolink_type,
708 dev->kobj_iolink, "%d", i);
709 if (ret < 0)
710 return ret;
711
712 iolink->attr.name = "properties";
713 iolink->attr.mode = KFD_SYSFS_FILE_MODE;
714 sysfs_attr_init(&iolink->attr);
715 ret = sysfs_create_file(iolink->kobj, &iolink->attr);
716 if (ret < 0)
717 return ret;
718 i++;
f4757347
AL
719 }
720
721 /* All hardware blocks have the same number of attributes. */
3f866f5f 722 num_attrs = ARRAY_SIZE(perf_attr_iommu);
f4757347
AL
723 list_for_each_entry(perf, &dev->perf_props, list) {
724 perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
725 * num_attrs + sizeof(struct attribute_group),
726 GFP_KERNEL);
727 if (!perf->attr_group)
728 return -ENOMEM;
729
730 attrs = (struct attribute **)(perf->attr_group + 1);
731 if (!strcmp(perf->block_name, "iommu")) {
732 /* Information of IOMMU's num_counters and counter_ids is shown
733 * under /sys/bus/event_source/devices/amd_iommu. We don't
734 * duplicate here.
735 */
736 perf_attr_iommu[0].data = perf->max_concurrent;
737 for (i = 0; i < num_attrs; i++)
738 attrs[i] = &perf_attr_iommu[i].attr.attr;
739 }
740 perf->attr_group->name = perf->block_name;
741 perf->attr_group->attrs = attrs;
742 ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
743 if (ret < 0)
744 return ret;
745 }
5b5c4e40
EP
746
747 return 0;
748}
749
3a87177e 750/* Called with write topology lock acquired */
5b5c4e40
EP
751static int kfd_build_sysfs_node_tree(void)
752{
753 struct kfd_topology_device *dev;
754 int ret;
755 uint32_t i = 0;
756
757 list_for_each_entry(dev, &topology_device_list, list) {
8dfead6c 758 ret = kfd_build_sysfs_node_entry(dev, i);
5b5c4e40
EP
759 if (ret < 0)
760 return ret;
761 i++;
762 }
763
764 return 0;
765}
766
3a87177e 767/* Called with write topology lock acquired */
5b5c4e40
EP
768static void kfd_remove_sysfs_node_tree(void)
769{
770 struct kfd_topology_device *dev;
771
772 list_for_each_entry(dev, &topology_device_list, list)
773 kfd_remove_sysfs_node_entry(dev);
774}
775
776static int kfd_topology_update_sysfs(void)
777{
778 int ret;
779
780 pr_info("Creating topology SYSFS entries\n");
4eacc26b 781 if (!sys_props.kobj_topology) {
5b5c4e40
EP
782 sys_props.kobj_topology =
783 kfd_alloc_struct(sys_props.kobj_topology);
784 if (!sys_props.kobj_topology)
785 return -ENOMEM;
786
787 ret = kobject_init_and_add(sys_props.kobj_topology,
788 &sysprops_type, &kfd_device->kobj,
789 "topology");
790 if (ret < 0)
791 return ret;
792
793 sys_props.kobj_nodes = kobject_create_and_add("nodes",
794 sys_props.kobj_topology);
795 if (!sys_props.kobj_nodes)
796 return -ENOMEM;
797
798 sys_props.attr_genid.name = "generation_id";
799 sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
800 sysfs_attr_init(&sys_props.attr_genid);
801 ret = sysfs_create_file(sys_props.kobj_topology,
802 &sys_props.attr_genid);
803 if (ret < 0)
804 return ret;
805
806 sys_props.attr_props.name = "system_properties";
807 sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
808 sysfs_attr_init(&sys_props.attr_props);
809 ret = sysfs_create_file(sys_props.kobj_topology,
810 &sys_props.attr_props);
811 if (ret < 0)
812 return ret;
813 }
814
815 kfd_remove_sysfs_node_tree();
816
817 return kfd_build_sysfs_node_tree();
818}
819
820static void kfd_topology_release_sysfs(void)
821{
822 kfd_remove_sysfs_node_tree();
823 if (sys_props.kobj_topology) {
824 sysfs_remove_file(sys_props.kobj_topology,
825 &sys_props.attr_genid);
826 sysfs_remove_file(sys_props.kobj_topology,
827 &sys_props.attr_props);
828 if (sys_props.kobj_nodes) {
829 kobject_del(sys_props.kobj_nodes);
830 kobject_put(sys_props.kobj_nodes);
16b9201c 831 sys_props.kobj_nodes = NULL;
5b5c4e40
EP
832 }
833 kobject_del(sys_props.kobj_topology);
834 kobject_put(sys_props.kobj_topology);
16b9201c 835 sys_props.kobj_topology = NULL;
5b5c4e40
EP
836 }
837}
838
4f449311
HK
839/* Called with write topology_lock acquired */
840static void kfd_topology_update_device_list(struct list_head *temp_list,
841 struct list_head *master_list)
842{
843 while (!list_empty(temp_list)) {
844 list_move_tail(temp_list->next, master_list);
845 sys_props.num_devices++;
846 }
847}
848
520b8fb7
FK
849static void kfd_debug_print_topology(void)
850{
851 struct kfd_topology_device *dev;
852
853 down_read(&topology_lock);
854
855 dev = list_last_entry(&topology_device_list,
856 struct kfd_topology_device, list);
857 if (dev) {
858 if (dev->node_props.cpu_cores_count &&
859 dev->node_props.simd_count) {
860 pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
861 dev->node_props.device_id,
862 dev->node_props.vendor_id);
863 } else if (dev->node_props.cpu_cores_count)
864 pr_info("Topology: Add CPU node\n");
865 else if (dev->node_props.simd_count)
866 pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
867 dev->node_props.device_id,
868 dev->node_props.vendor_id);
869 }
870 up_read(&topology_lock);
871}
872
873/* Helper function for intializing platform_xx members of
874 * kfd_system_properties. Uses OEM info from the last CPU/APU node.
875 */
876static void kfd_update_system_properties(void)
877{
878 struct kfd_topology_device *dev;
879
880 down_read(&topology_lock);
881 dev = list_last_entry(&topology_device_list,
882 struct kfd_topology_device, list);
883 if (dev) {
884 sys_props.platform_id =
885 (*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
886 sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
887 sys_props.platform_rev = dev->oem_revision;
888 }
889 up_read(&topology_lock);
890}
891
892static void find_system_memory(const struct dmi_header *dm,
893 void *private)
894{
895 struct kfd_mem_properties *mem;
896 u16 mem_width, mem_clock;
897 struct kfd_topology_device *kdev =
898 (struct kfd_topology_device *)private;
899 const u8 *dmi_data = (const u8 *)(dm + 1);
900
901 if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
902 mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
903 mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
904 list_for_each_entry(mem, &kdev->mem_props, list) {
905 if (mem_width != 0xFFFF && mem_width != 0)
906 mem->width = mem_width;
907 if (mem_clock != 0)
908 mem->mem_clk_max = mem_clock;
909 }
910 }
911}
f4757347
AL
912
913/*
914 * Performance counters information is not part of CRAT but we would like to
915 * put them in the sysfs under topology directory for Thunk to get the data.
916 * This function is called before updating the sysfs.
917 */
918static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
919{
64d1c3a4
FK
920 /* These are the only counters supported so far */
921 return kfd_iommu_add_perf_counters(kdev);
f4757347
AL
922}
923
520b8fb7
FK
924/* kfd_add_non_crat_information - Add information that is not currently
925 * defined in CRAT but is necessary for KFD topology
926 * @dev - topology device to which addition info is added
927 */
928static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
929{
930 /* Check if CPU only node. */
931 if (!kdev->gpu) {
932 /* Add system memory information */
933 dmi_walk(find_system_memory, kdev);
934 }
935 /* TODO: For GPU node, rearrange code from kfd_topology_add_device */
936}
937
b441093e
HK
938/* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
939 * Ignore CRAT for all other devices. AMD APU is identified if both CPU
940 * and GPU cores are present.
941 * @device_list - topology device list created by parsing ACPI CRAT table.
942 * @return - TRUE if invalid, FALSE is valid.
943 */
944static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
945{
946 struct kfd_topology_device *dev;
947
948 list_for_each_entry(dev, device_list, list) {
949 if (dev->node_props.cpu_cores_count &&
950 dev->node_props.simd_count)
951 return false;
952 }
953 pr_info("Ignoring ACPI CRAT on non-APU system\n");
954 return true;
955}
956
5b5c4e40
EP
957int kfd_topology_init(void)
958{
16b9201c 959 void *crat_image = NULL;
5b5c4e40
EP
960 size_t image_size = 0;
961 int ret;
4f449311 962 struct list_head temp_topology_device_list;
520b8fb7
FK
963 int cpu_only_node = 0;
964 struct kfd_topology_device *kdev;
965 int proximity_domain;
5b5c4e40 966
4f449311
HK
967 /* topology_device_list - Master list of all topology devices
968 * temp_topology_device_list - temporary list created while parsing CRAT
969 * or VCRAT. Once parsing is complete the contents of list is moved to
970 * topology_device_list
5b5c4e40 971 */
4f449311
HK
972
973 /* Initialize the head for the both the lists */
5b5c4e40 974 INIT_LIST_HEAD(&topology_device_list);
4f449311 975 INIT_LIST_HEAD(&temp_topology_device_list);
5b5c4e40 976 init_rwsem(&topology_lock);
5b5c4e40
EP
977
978 memset(&sys_props, 0, sizeof(sys_props));
979
520b8fb7
FK
980 /* Proximity domains in ACPI CRAT tables start counting at
981 * 0. The same should be true for virtual CRAT tables created
982 * at this stage. GPUs added later in kfd_topology_add_device
983 * use a counter.
984 */
985 proximity_domain = 0;
986
5b5c4e40 987 /*
520b8fb7 988 * Get the CRAT image from the ACPI. If ACPI doesn't have one
b441093e 989 * or if ACPI CRAT is invalid create a virtual CRAT.
520b8fb7
FK
990 * NOTE: The current implementation expects all AMD APUs to have
991 * CRAT. If no CRAT is available, it is assumed to be a CPU
5b5c4e40 992 */
8e05247d
HK
993 ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
994 if (!ret) {
4f449311 995 ret = kfd_parse_crat_table(crat_image,
520b8fb7
FK
996 &temp_topology_device_list,
997 proximity_domain);
b441093e
HK
998 if (ret ||
999 kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
520b8fb7
FK
1000 kfd_release_topology_device_list(
1001 &temp_topology_device_list);
1002 kfd_destroy_crat_image(crat_image);
1003 crat_image = NULL;
1004 }
1005 }
1006
1007 if (!crat_image) {
1008 ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
1009 COMPUTE_UNIT_CPU, NULL,
1010 proximity_domain);
1011 cpu_only_node = 1;
1012 if (ret) {
1013 pr_err("Error creating VCRAT table for CPU\n");
1014 return ret;
1015 }
1016
1017 ret = kfd_parse_crat_table(crat_image,
1018 &temp_topology_device_list,
1019 proximity_domain);
1020 if (ret) {
1021 pr_err("Error parsing VCRAT table for CPU\n");
5b5c4e40 1022 goto err;
520b8fb7 1023 }
5b5c4e40
EP
1024 }
1025
f4757347
AL
1026 kdev = list_first_entry(&temp_topology_device_list,
1027 struct kfd_topology_device, list);
1028 kfd_add_perf_to_topology(kdev);
1029
8e05247d 1030 down_write(&topology_lock);
4f449311
HK
1031 kfd_topology_update_device_list(&temp_topology_device_list,
1032 &topology_device_list);
520b8fb7 1033 atomic_set(&topology_crat_proximity_domain, sys_props.num_devices-1);
8e05247d
HK
1034 ret = kfd_topology_update_sysfs();
1035 up_write(&topology_lock);
1036
4f449311
HK
1037 if (!ret) {
1038 sys_props.generation_count++;
520b8fb7
FK
1039 kfd_update_system_properties();
1040 kfd_debug_print_topology();
8e05247d 1041 pr_info("Finished initializing topology\n");
4f449311 1042 } else
8e05247d
HK
1043 pr_err("Failed to update topology in sysfs ret=%d\n", ret);
1044
520b8fb7
FK
1045 /* For nodes with GPU, this information gets added
1046 * when GPU is detected (kfd_topology_add_device).
1047 */
1048 if (cpu_only_node) {
1049 /* Add additional information to CPU only node created above */
1050 down_write(&topology_lock);
1051 kdev = list_first_entry(&topology_device_list,
1052 struct kfd_topology_device, list);
1053 up_write(&topology_lock);
1054 kfd_add_non_crat_information(kdev);
1055 }
1056
5b5c4e40 1057err:
8e05247d 1058 kfd_destroy_crat_image(crat_image);
5b5c4e40
EP
1059 return ret;
1060}
1061
1062void kfd_topology_shutdown(void)
1063{
4f449311 1064 down_write(&topology_lock);
5b5c4e40
EP
1065 kfd_topology_release_sysfs();
1066 kfd_release_live_view();
4f449311 1067 up_write(&topology_lock);
5b5c4e40
EP
1068}
1069
5b5c4e40
EP
1070static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
1071{
1072 uint32_t hashout;
1073 uint32_t buf[7];
585f0e6c 1074 uint64_t local_mem_size;
5b5c4e40 1075 int i;
0504cccf 1076 struct kfd_local_mem_info local_mem_info;
5b5c4e40
EP
1077
1078 if (!gpu)
1079 return 0;
1080
7cd52c91 1081 amdgpu_amdkfd_get_local_mem_info(gpu->kgd, &local_mem_info);
0504cccf
HK
1082
1083 local_mem_size = local_mem_info.local_mem_size_private +
1084 local_mem_info.local_mem_size_public;
585f0e6c 1085
5b5c4e40 1086 buf[0] = gpu->pdev->devfn;
46096058
AL
1087 buf[1] = gpu->pdev->subsystem_vendor |
1088 (gpu->pdev->subsystem_device << 16);
1089 buf[2] = pci_domain_nr(gpu->pdev->bus);
5b5c4e40
EP
1090 buf[3] = gpu->pdev->device;
1091 buf[4] = gpu->pdev->bus->number;
585f0e6c
EC
1092 buf[5] = lower_32_bits(local_mem_size);
1093 buf[6] = upper_32_bits(local_mem_size);
5b5c4e40
EP
1094
1095 for (i = 0, hashout = 0; i < 7; i++)
1096 hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
1097
1098 return hashout;
1099}
3a87177e
HK
1100/* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
1101 * the GPU device is not already present in the topology device
1102 * list then return NULL. This means a new topology device has to
1103 * be created for this GPU.
3a87177e 1104 */
5b5c4e40
EP
1105static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
1106{
1107 struct kfd_topology_device *dev;
16b9201c 1108 struct kfd_topology_device *out_dev = NULL;
5b5c4e40 1109
3a87177e 1110 down_write(&topology_lock);
b8fe0524
FK
1111 list_for_each_entry(dev, &topology_device_list, list) {
1112 /* Discrete GPUs need their own topology device list
1113 * entries. Don't assign them to CPU/APU nodes.
1114 */
1115 if (!gpu->device_info->needs_iommu_device &&
1116 dev->node_props.cpu_cores_count)
1117 continue;
1118
4eacc26b 1119 if (!dev->gpu && (dev->node_props.simd_count > 0)) {
5b5c4e40
EP
1120 dev->gpu = gpu;
1121 out_dev = dev;
1122 break;
1123 }
b8fe0524 1124 }
3a87177e 1125 up_write(&topology_lock);
5b5c4e40
EP
1126 return out_dev;
1127}
1128
1129static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
1130{
1131 /*
1132 * TODO: Generate an event for thunk about the arrival/removal
1133 * of the GPU
1134 */
1135}
1136
3a87177e
HK
1137/* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
1138 * patch this after CRAT parsing.
1139 */
1140static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
1141{
1142 struct kfd_mem_properties *mem;
1143 struct kfd_local_mem_info local_mem_info;
1144
1145 if (!dev)
1146 return;
1147
1148 /* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
1149 * single bank of VRAM local memory.
1150 * for dGPUs - VCRAT reports only one bank of Local Memory
1151 * for APUs - If CRAT from ACPI reports more than one bank, then
1152 * all the banks will report the same mem_clk_max information
1153 */
7cd52c91 1154 amdgpu_amdkfd_get_local_mem_info(dev->gpu->kgd, &local_mem_info);
3a87177e
HK
1155
1156 list_for_each_entry(mem, &dev->mem_props, list)
1157 mem->mem_clk_max = local_mem_info.mem_clk_max;
1158}
1159
1160static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
1161{
d35f00d8
EH
1162 struct kfd_iolink_properties *link, *cpu_link;
1163 struct kfd_topology_device *cpu_dev;
1164 uint32_t cap;
1165 uint32_t cpu_flag = CRAT_IOLINK_FLAGS_ENABLED;
1166 uint32_t flag = CRAT_IOLINK_FLAGS_ENABLED;
3a87177e
HK
1167
1168 if (!dev || !dev->gpu)
1169 return;
1170
d35f00d8
EH
1171 pcie_capability_read_dword(dev->gpu->pdev,
1172 PCI_EXP_DEVCAP2, &cap);
1173
1174 if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1175 PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1176 cpu_flag |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1177 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1178
1179 if (!dev->gpu->pci_atomic_requested ||
1180 dev->gpu->device_info->asic_family == CHIP_HAWAII)
1181 flag |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1182 CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1183
1184 /* GPU only creates direct links so apply flags setting to all */
1185 list_for_each_entry(link, &dev->io_link_props, list) {
1186 link->flags = flag;
1187 cpu_dev = kfd_topology_device_by_proximity_domain(
1188 link->node_to);
1189 if (cpu_dev) {
1190 list_for_each_entry(cpu_link,
1191 &cpu_dev->io_link_props, list)
1192 if (cpu_link->node_to == link->node_from)
1193 cpu_link->flags = cpu_flag;
1194 }
1195 }
3a87177e
HK
1196}
1197
5b5c4e40
EP
1198int kfd_topology_add_device(struct kfd_dev *gpu)
1199{
1200 uint32_t gpu_id;
1201 struct kfd_topology_device *dev;
f7ce2fad 1202 struct kfd_cu_info cu_info;
4f449311
HK
1203 int res = 0;
1204 struct list_head temp_topology_device_list;
3a87177e
HK
1205 void *crat_image = NULL;
1206 size_t image_size = 0;
1207 int proximity_domain;
0dee45a2 1208 struct amdgpu_ras *ctx;
4f449311
HK
1209
1210 INIT_LIST_HEAD(&temp_topology_device_list);
5b5c4e40 1211
5b5c4e40
EP
1212 gpu_id = kfd_generate_gpu_id(gpu);
1213
79775b62 1214 pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
5b5c4e40 1215
3a87177e
HK
1216 proximity_domain = atomic_inc_return(&topology_crat_proximity_domain);
1217
1218 /* Check to see if this gpu device exists in the topology_device_list.
1219 * If so, assign the gpu to that device,
1220 * else create a Virtual CRAT for this gpu device and then parse that
1221 * CRAT to create a new topology device. Once created assign the gpu to
1222 * that topology device
5b5c4e40
EP
1223 */
1224 dev = kfd_assign_gpu(gpu);
1225 if (!dev) {
3a87177e
HK
1226 res = kfd_create_crat_image_virtual(&crat_image, &image_size,
1227 COMPUTE_UNIT_GPU, gpu,
1228 proximity_domain);
1229 if (res) {
1230 pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
1231 gpu_id);
1232 return res;
1233 }
1234 res = kfd_parse_crat_table(crat_image,
1235 &temp_topology_device_list,
1236 proximity_domain);
1237 if (res) {
1238 pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
1239 gpu_id);
5b5c4e40
EP
1240 goto err;
1241 }
4f449311 1242
4f449311
HK
1243 down_write(&topology_lock);
1244 kfd_topology_update_device_list(&temp_topology_device_list,
1245 &topology_device_list);
1246
8eabaf54
KR
1247 /* Update the SYSFS tree, since we added another topology
1248 * device
5b5c4e40 1249 */
3a87177e 1250 res = kfd_topology_update_sysfs();
4f449311
HK
1251 up_write(&topology_lock);
1252
3a87177e
HK
1253 if (!res)
1254 sys_props.generation_count++;
1255 else
1256 pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
1257 gpu_id, res);
1258 dev = kfd_assign_gpu(gpu);
1259 if (WARN_ON(!dev)) {
1260 res = -ENODEV;
1261 goto err;
1262 }
5b5c4e40
EP
1263 }
1264
1265 dev->gpu_id = gpu_id;
1266 gpu->id = gpu_id;
3a87177e
HK
1267
1268 /* TODO: Move the following lines to function
1269 * kfd_add_non_crat_information
1270 */
1271
1272 /* Fill-in additional information that is not available in CRAT but
1273 * needed for the topology
1274 */
1275
7cd52c91 1276 amdgpu_amdkfd_get_cu_info(dev->gpu->kgd, &cu_info);
3a87177e
HK
1277 dev->node_props.simd_arrays_per_engine =
1278 cu_info.num_shader_arrays_per_engine;
1279
5b5c4e40
EP
1280 dev->node_props.vendor_id = gpu->pdev->vendor;
1281 dev->node_props.device_id = gpu->pdev->device;
babe2ef3 1282 dev->node_props.location_id = pci_dev_id(gpu->pdev);
3a87177e 1283 dev->node_props.max_engine_clk_fcompute =
7cd52c91 1284 amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->kgd);
3a87177e
HK
1285 dev->node_props.max_engine_clk_ccompute =
1286 cpufreq_quick_get_max(0) / 1000;
7c9b7171
OZ
1287 dev->node_props.drm_render_minor =
1288 gpu->shared_resources.drm_render_minor;
3a87177e 1289
0c1690e3 1290 dev->node_props.hive_id = gpu->hive_id;
14568cf6
OZ
1291 dev->node_props.num_sdma_engines = gpu->device_info->num_sdma_engines;
1292 dev->node_props.num_sdma_xgmi_engines =
1293 gpu->device_info->num_xgmi_sdma_engines;
29e76462
OZ
1294 dev->node_props.num_gws = (hws_gws_support &&
1295 dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
1296 amdgpu_amdkfd_get_num_gws(dev->gpu->kgd) : 0;
0c1690e3 1297
3a87177e
HK
1298 kfd_fill_mem_clk_max_info(dev);
1299 kfd_fill_iolink_non_crat_info(dev);
1300
1301 switch (dev->gpu->device_info->asic_family) {
1302 case CHIP_KAVERI:
1303 case CHIP_HAWAII:
1304 case CHIP_TONGA:
1305 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
1306 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1307 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1308 break;
1309 case CHIP_CARRIZO:
1310 case CHIP_FIJI:
1311 case CHIP_POLARIS10:
1312 case CHIP_POLARIS11:
846a44d7 1313 case CHIP_POLARIS12:
ed81cd6e 1314 case CHIP_VEGAM:
42aa8793 1315 pr_debug("Adding doorbell packet type capability\n");
3a87177e
HK
1316 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
1317 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1318 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1319 break;
389056e5 1320 case CHIP_VEGA10:
846a44d7 1321 case CHIP_VEGA12:
22a3a294 1322 case CHIP_VEGA20:
389056e5
FK
1323 case CHIP_RAVEN:
1324 dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1325 HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1326 HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1327 break;
3a87177e
HK
1328 default:
1329 WARN(1, "Unexpected ASIC family %u",
1330 dev->gpu->device_info->asic_family);
7639a8c4
BG
1331 }
1332
3a87177e
HK
1333 /* Fix errors in CZ CRAT.
1334 * simd_count: Carrizo CRAT reports wrong simd_count, probably
1335 * because it doesn't consider masked out CUs
70f372bf
PC
1336 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
1337 * capability flag: Carrizo CRAT doesn't report IOMMU flags
3a87177e 1338 */
70f372bf 1339 if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
3a87177e
HK
1340 dev->node_props.simd_count =
1341 cu_info.simd_per_cu * cu_info.cu_active_number;
70f372bf
PC
1342 dev->node_props.max_waves_per_simd = 10;
1343 dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
1344 }
3a87177e 1345
0dee45a2
EH
1346 ctx = amdgpu_ras_get_context((struct amdgpu_device *)(dev->gpu->kgd));
1347 if (ctx) {
1348 /* kfd only concerns sram ecc on GFX/SDMA and HBM ecc on UMC */
1349 dev->node_props.capability |=
1350 (((ctx->features & BIT(AMDGPU_RAS_BLOCK__SDMA)) != 0) ||
1351 ((ctx->features & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0)) ?
1352 HSA_CAP_SRAM_EDCSUPPORTED : 0;
1353 dev->node_props.capability |= ((ctx->features & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
1354 HSA_CAP_MEM_EDCSUPPORTED : 0;
1355
1356 dev->node_props.capability |= (ctx->features != 0) ?
1357 HSA_CAP_RASEVENTNOTIFY : 0;
1358 }
1359
3a87177e
HK
1360 kfd_debug_print_topology();
1361
4f449311 1362 if (!res)
5b5c4e40 1363 kfd_notify_gpu_change(gpu_id, 1);
4f449311 1364err:
3a87177e 1365 kfd_destroy_crat_image(crat_image);
5b5c4e40
EP
1366 return res;
1367}
1368
1369int kfd_topology_remove_device(struct kfd_dev *gpu)
1370{
4f449311 1371 struct kfd_topology_device *dev, *tmp;
5b5c4e40
EP
1372 uint32_t gpu_id;
1373 int res = -ENODEV;
1374
5b5c4e40
EP
1375 down_write(&topology_lock);
1376
4f449311 1377 list_for_each_entry_safe(dev, tmp, &topology_device_list, list)
5b5c4e40
EP
1378 if (dev->gpu == gpu) {
1379 gpu_id = dev->gpu_id;
1380 kfd_remove_sysfs_node_entry(dev);
1381 kfd_release_topology_device(dev);
4f449311 1382 sys_props.num_devices--;
5b5c4e40
EP
1383 res = 0;
1384 if (kfd_topology_update_sysfs() < 0)
1385 kfd_topology_release_sysfs();
1386 break;
1387 }
1388
1389 up_write(&topology_lock);
1390
174de876 1391 if (!res)
5b5c4e40
EP
1392 kfd_notify_gpu_change(gpu_id, 0);
1393
1394 return res;
1395}
1396
6d82eb0e
HK
1397/* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
1398 * topology. If GPU device is found @idx, then valid kfd_dev pointer is
1399 * returned through @kdev
1400 * Return - 0: On success (@kdev will be NULL for non GPU nodes)
1401 * -1: If end of list
5b5c4e40 1402 */
6d82eb0e 1403int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
5b5c4e40
EP
1404{
1405
1406 struct kfd_topology_device *top_dev;
5b5c4e40
EP
1407 uint8_t device_idx = 0;
1408
6d82eb0e 1409 *kdev = NULL;
5b5c4e40
EP
1410 down_read(&topology_lock);
1411
1412 list_for_each_entry(top_dev, &topology_device_list, list) {
1413 if (device_idx == idx) {
6d82eb0e
HK
1414 *kdev = top_dev->gpu;
1415 up_read(&topology_lock);
1416 return 0;
5b5c4e40
EP
1417 }
1418
1419 device_idx++;
1420 }
1421
1422 up_read(&topology_lock);
1423
6d82eb0e 1424 return -1;
5b5c4e40
EP
1425
1426}
851a645e 1427
520b8fb7
FK
1428static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
1429{
520b8fb7
FK
1430 int first_cpu_of_numa_node;
1431
1432 if (!cpumask || cpumask == cpu_none_mask)
1433 return -1;
1434 first_cpu_of_numa_node = cpumask_first(cpumask);
1435 if (first_cpu_of_numa_node >= nr_cpu_ids)
1436 return -1;
df1dd4f4
FK
1437#ifdef CONFIG_X86_64
1438 return cpu_data(first_cpu_of_numa_node).apicid;
1439#else
1440 return first_cpu_of_numa_node;
1441#endif
520b8fb7
FK
1442}
1443
1444/* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
1445 * of the given NUMA node (numa_node_id)
1446 * Return -1 on failure
1447 */
1448int kfd_numa_node_to_apic_id(int numa_node_id)
1449{
1450 if (numa_node_id == -1) {
1451 pr_warn("Invalid NUMA Node. Use online CPU mask\n");
1452 return kfd_cpumask_to_apic_id(cpu_online_mask);
1453 }
1454 return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
1455}
1456
851a645e
FK
1457#if defined(CONFIG_DEBUG_FS)
1458
1459int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
1460{
1461 struct kfd_topology_device *dev;
1462 unsigned int i = 0;
1463 int r = 0;
1464
1465 down_read(&topology_lock);
1466
1467 list_for_each_entry(dev, &topology_device_list, list) {
1468 if (!dev->gpu) {
1469 i++;
1470 continue;
1471 }
1472
1473 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1474 r = dqm_debugfs_hqds(m, dev->gpu->dqm);
1475 if (r)
1476 break;
1477 }
1478
1479 up_read(&topology_lock);
1480
1481 return r;
1482}
1483
1484int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
1485{
1486 struct kfd_topology_device *dev;
1487 unsigned int i = 0;
1488 int r = 0;
1489
1490 down_read(&topology_lock);
1491
1492 list_for_each_entry(dev, &topology_device_list, list) {
1493 if (!dev->gpu) {
1494 i++;
1495 continue;
1496 }
1497
1498 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1499 r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets);
1500 if (r)
1501 break;
1502 }
1503
1504 up_read(&topology_lock);
1505
1506 return r;
1507}
1508
1509#endif