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