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