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