9d03a56fd9c75b5e31a5e7f94dcbbc0deedb27b6
[linux-2.6-microblaze.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
32 #include "kfd_priv.h"
33 #include "kfd_crat.h"
34 #include "kfd_topology.h"
35 #include "kfd_device_queue_manager.h"
36
37 static struct list_head topology_device_list;
38 static int topology_crat_parsed;
39 static struct kfd_system_properties sys_props;
40
41 static DECLARE_RWSEM(topology_lock);
42
43 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
44 {
45         struct kfd_topology_device *top_dev;
46         struct kfd_dev *device = NULL;
47
48         down_read(&topology_lock);
49
50         list_for_each_entry(top_dev, &topology_device_list, list)
51                 if (top_dev->gpu_id == gpu_id) {
52                         device = top_dev->gpu;
53                         break;
54                 }
55
56         up_read(&topology_lock);
57
58         return device;
59 }
60
61 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
62 {
63         struct kfd_topology_device *top_dev;
64         struct kfd_dev *device = NULL;
65
66         down_read(&topology_lock);
67
68         list_for_each_entry(top_dev, &topology_device_list, list)
69                 if (top_dev->gpu->pdev == pdev) {
70                         device = top_dev->gpu;
71                         break;
72                 }
73
74         up_read(&topology_lock);
75
76         return device;
77 }
78
79 static int kfd_topology_get_crat_acpi(void *crat_image, size_t *size)
80 {
81         struct acpi_table_header *crat_table;
82         acpi_status status;
83
84         if (!size)
85                 return -EINVAL;
86
87         /*
88          * Fetch the CRAT table from ACPI
89          */
90         status = acpi_get_table(CRAT_SIGNATURE, 0, &crat_table);
91         if (status == AE_NOT_FOUND) {
92                 pr_warn("CRAT table not found\n");
93                 return -ENODATA;
94         } else if (ACPI_FAILURE(status)) {
95                 const char *err = acpi_format_exception(status);
96
97                 pr_err("CRAT table error: %s\n", err);
98                 return -EINVAL;
99         }
100
101         if (*size >= crat_table->length && crat_image != NULL)
102                 memcpy(crat_image, crat_table, crat_table->length);
103
104         *size = crat_table->length;
105
106         return 0;
107 }
108
109 static void kfd_populated_cu_info_cpu(struct kfd_topology_device *dev,
110                 struct crat_subtype_computeunit *cu)
111 {
112         dev->node_props.cpu_cores_count = cu->num_cpu_cores;
113         dev->node_props.cpu_core_id_base = cu->processor_id_low;
114         if (cu->hsa_capability & CRAT_CU_FLAGS_IOMMU_PRESENT)
115                 dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
116
117         pr_info("CU CPU: cores=%d id_base=%d\n", cu->num_cpu_cores,
118                         cu->processor_id_low);
119 }
120
121 static void kfd_populated_cu_info_gpu(struct kfd_topology_device *dev,
122                 struct crat_subtype_computeunit *cu)
123 {
124         dev->node_props.simd_id_base = cu->processor_id_low;
125         dev->node_props.simd_count = cu->num_simd_cores;
126         dev->node_props.lds_size_in_kb = cu->lds_size_in_kb;
127         dev->node_props.max_waves_per_simd = cu->max_waves_simd;
128         dev->node_props.wave_front_size = cu->wave_front_size;
129         dev->node_props.mem_banks_count = cu->num_banks;
130         dev->node_props.array_count = cu->num_arrays;
131         dev->node_props.cu_per_simd_array = cu->num_cu_per_array;
132         dev->node_props.simd_per_cu = cu->num_simd_per_cu;
133         dev->node_props.max_slots_scratch_cu = cu->max_slots_scatch_cu;
134         if (cu->hsa_capability & CRAT_CU_FLAGS_HOT_PLUGGABLE)
135                 dev->node_props.capability |= HSA_CAP_HOT_PLUGGABLE;
136         pr_info("CU GPU: simds=%d id_base=%d\n", cu->num_simd_cores,
137                                 cu->processor_id_low);
138 }
139
140 /* kfd_parse_subtype_cu is called when the topology mutex is already acquired */
141 static int kfd_parse_subtype_cu(struct crat_subtype_computeunit *cu)
142 {
143         struct kfd_topology_device *dev;
144         int i = 0;
145
146         pr_info("Found CU entry in CRAT table with proximity_domain=%d caps=%x\n",
147                         cu->proximity_domain, cu->hsa_capability);
148         list_for_each_entry(dev, &topology_device_list, list) {
149                 if (cu->proximity_domain == i) {
150                         if (cu->flags & CRAT_CU_FLAGS_CPU_PRESENT)
151                                 kfd_populated_cu_info_cpu(dev, cu);
152
153                         if (cu->flags & CRAT_CU_FLAGS_GPU_PRESENT)
154                                 kfd_populated_cu_info_gpu(dev, cu);
155                         break;
156                 }
157                 i++;
158         }
159
160         return 0;
161 }
162
163 /*
164  * kfd_parse_subtype_mem is called when the topology mutex is
165  * already acquired
166  */
167 static int kfd_parse_subtype_mem(struct crat_subtype_memory *mem)
168 {
169         struct kfd_mem_properties *props;
170         struct kfd_topology_device *dev;
171         int i = 0;
172
173         pr_info("Found memory entry in CRAT table with proximity_domain=%d\n",
174                         mem->promixity_domain);
175         list_for_each_entry(dev, &topology_device_list, list) {
176                 if (mem->promixity_domain == i) {
177                         props = kfd_alloc_struct(props);
178                         if (props == NULL)
179                                 return -ENOMEM;
180
181                         if (dev->node_props.cpu_cores_count == 0)
182                                 props->heap_type = HSA_MEM_HEAP_TYPE_FB_PRIVATE;
183                         else
184                                 props->heap_type = HSA_MEM_HEAP_TYPE_SYSTEM;
185
186                         if (mem->flags & CRAT_MEM_FLAGS_HOT_PLUGGABLE)
187                                 props->flags |= HSA_MEM_FLAGS_HOT_PLUGGABLE;
188                         if (mem->flags & CRAT_MEM_FLAGS_NON_VOLATILE)
189                                 props->flags |= HSA_MEM_FLAGS_NON_VOLATILE;
190
191                         props->size_in_bytes =
192                                 ((uint64_t)mem->length_high << 32) +
193                                                         mem->length_low;
194                         props->width = mem->width;
195
196                         dev->mem_bank_count++;
197                         list_add_tail(&props->list, &dev->mem_props);
198
199                         break;
200                 }
201                 i++;
202         }
203
204         return 0;
205 }
206
207 /*
208  * kfd_parse_subtype_cache is called when the topology mutex
209  * is already acquired
210  */
211 static int kfd_parse_subtype_cache(struct crat_subtype_cache *cache)
212 {
213         struct kfd_cache_properties *props;
214         struct kfd_topology_device *dev;
215         uint32_t id;
216
217         id = cache->processor_id_low;
218
219         pr_info("Found cache entry in CRAT table with processor_id=%d\n", id);
220         list_for_each_entry(dev, &topology_device_list, list)
221                 if (id == dev->node_props.cpu_core_id_base ||
222                     id == dev->node_props.simd_id_base) {
223                         props = kfd_alloc_struct(props);
224                         if (props == NULL)
225                                 return -ENOMEM;
226
227                         props->processor_id_low = id;
228                         props->cache_level = cache->cache_level;
229                         props->cache_size = cache->cache_size;
230                         props->cacheline_size = cache->cache_line_size;
231                         props->cachelines_per_tag = cache->lines_per_tag;
232                         props->cache_assoc = cache->associativity;
233                         props->cache_latency = cache->cache_latency;
234
235                         if (cache->flags & CRAT_CACHE_FLAGS_DATA_CACHE)
236                                 props->cache_type |= HSA_CACHE_TYPE_DATA;
237                         if (cache->flags & CRAT_CACHE_FLAGS_INST_CACHE)
238                                 props->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
239                         if (cache->flags & CRAT_CACHE_FLAGS_CPU_CACHE)
240                                 props->cache_type |= HSA_CACHE_TYPE_CPU;
241                         if (cache->flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
242                                 props->cache_type |= HSA_CACHE_TYPE_HSACU;
243
244                         dev->cache_count++;
245                         dev->node_props.caches_count++;
246                         list_add_tail(&props->list, &dev->cache_props);
247
248                         break;
249                 }
250
251         return 0;
252 }
253
254 /*
255  * kfd_parse_subtype_iolink is called when the topology mutex
256  * is already acquired
257  */
258 static int kfd_parse_subtype_iolink(struct crat_subtype_iolink *iolink)
259 {
260         struct kfd_iolink_properties *props;
261         struct kfd_topology_device *dev;
262         uint32_t i = 0;
263         uint32_t id_from;
264         uint32_t id_to;
265
266         id_from = iolink->proximity_domain_from;
267         id_to = iolink->proximity_domain_to;
268
269         pr_info("Found IO link entry in CRAT table with id_from=%d\n", id_from);
270         list_for_each_entry(dev, &topology_device_list, list) {
271                 if (id_from == i) {
272                         props = kfd_alloc_struct(props);
273                         if (props == NULL)
274                                 return -ENOMEM;
275
276                         props->node_from = id_from;
277                         props->node_to = id_to;
278                         props->ver_maj = iolink->version_major;
279                         props->ver_min = iolink->version_minor;
280
281                         /*
282                          * weight factor (derived from CDIR), currently always 1
283                          */
284                         props->weight = 1;
285
286                         props->min_latency = iolink->minimum_latency;
287                         props->max_latency = iolink->maximum_latency;
288                         props->min_bandwidth = iolink->minimum_bandwidth_mbs;
289                         props->max_bandwidth = iolink->maximum_bandwidth_mbs;
290                         props->rec_transfer_size =
291                                         iolink->recommended_transfer_size;
292
293                         dev->io_link_count++;
294                         dev->node_props.io_links_count++;
295                         list_add_tail(&props->list, &dev->io_link_props);
296
297                         break;
298                 }
299                 i++;
300         }
301
302         return 0;
303 }
304
305 static int kfd_parse_subtype(struct crat_subtype_generic *sub_type_hdr)
306 {
307         struct crat_subtype_computeunit *cu;
308         struct crat_subtype_memory *mem;
309         struct crat_subtype_cache *cache;
310         struct crat_subtype_iolink *iolink;
311         int ret = 0;
312
313         switch (sub_type_hdr->type) {
314         case CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY:
315                 cu = (struct crat_subtype_computeunit *)sub_type_hdr;
316                 ret = kfd_parse_subtype_cu(cu);
317                 break;
318         case CRAT_SUBTYPE_MEMORY_AFFINITY:
319                 mem = (struct crat_subtype_memory *)sub_type_hdr;
320                 ret = kfd_parse_subtype_mem(mem);
321                 break;
322         case CRAT_SUBTYPE_CACHE_AFFINITY:
323                 cache = (struct crat_subtype_cache *)sub_type_hdr;
324                 ret = kfd_parse_subtype_cache(cache);
325                 break;
326         case CRAT_SUBTYPE_TLB_AFFINITY:
327                 /*
328                  * For now, nothing to do here
329                  */
330                 pr_info("Found TLB entry in CRAT table (not processing)\n");
331                 break;
332         case CRAT_SUBTYPE_CCOMPUTE_AFFINITY:
333                 /*
334                  * For now, nothing to do here
335                  */
336                 pr_info("Found CCOMPUTE entry in CRAT table (not processing)\n");
337                 break;
338         case CRAT_SUBTYPE_IOLINK_AFFINITY:
339                 iolink = (struct crat_subtype_iolink *)sub_type_hdr;
340                 ret = kfd_parse_subtype_iolink(iolink);
341                 break;
342         default:
343                 pr_warn("Unknown subtype (%d) in CRAT\n",
344                                 sub_type_hdr->type);
345         }
346
347         return ret;
348 }
349
350 static void kfd_release_topology_device(struct kfd_topology_device *dev)
351 {
352         struct kfd_mem_properties *mem;
353         struct kfd_cache_properties *cache;
354         struct kfd_iolink_properties *iolink;
355
356         list_del(&dev->list);
357
358         while (dev->mem_props.next != &dev->mem_props) {
359                 mem = container_of(dev->mem_props.next,
360                                 struct kfd_mem_properties, list);
361                 list_del(&mem->list);
362                 kfree(mem);
363         }
364
365         while (dev->cache_props.next != &dev->cache_props) {
366                 cache = container_of(dev->cache_props.next,
367                                 struct kfd_cache_properties, list);
368                 list_del(&cache->list);
369                 kfree(cache);
370         }
371
372         while (dev->io_link_props.next != &dev->io_link_props) {
373                 iolink = container_of(dev->io_link_props.next,
374                                 struct kfd_iolink_properties, list);
375                 list_del(&iolink->list);
376                 kfree(iolink);
377         }
378
379         kfree(dev);
380
381         sys_props.num_devices--;
382 }
383
384 static void kfd_release_live_view(void)
385 {
386         struct kfd_topology_device *dev;
387
388         while (topology_device_list.next != &topology_device_list) {
389                 dev = container_of(topology_device_list.next,
390                                  struct kfd_topology_device, list);
391                 kfd_release_topology_device(dev);
392 }
393
394         memset(&sys_props, 0, sizeof(sys_props));
395 }
396
397 static struct kfd_topology_device *kfd_create_topology_device(void)
398 {
399         struct kfd_topology_device *dev;
400
401         dev = kfd_alloc_struct(dev);
402         if (!dev) {
403                 pr_err("No memory to allocate a topology device");
404                 return NULL;
405         }
406
407         INIT_LIST_HEAD(&dev->mem_props);
408         INIT_LIST_HEAD(&dev->cache_props);
409         INIT_LIST_HEAD(&dev->io_link_props);
410
411         list_add_tail(&dev->list, &topology_device_list);
412         sys_props.num_devices++;
413
414         return dev;
415 }
416
417 static int kfd_parse_crat_table(void *crat_image)
418 {
419         struct kfd_topology_device *top_dev;
420         struct crat_subtype_generic *sub_type_hdr;
421         uint16_t node_id;
422         int ret;
423         struct crat_header *crat_table = (struct crat_header *)crat_image;
424         uint16_t num_nodes;
425         uint32_t image_len;
426
427         if (!crat_image)
428                 return -EINVAL;
429
430         num_nodes = crat_table->num_domains;
431         image_len = crat_table->length;
432
433         pr_info("Parsing CRAT table with %d nodes\n", num_nodes);
434
435         for (node_id = 0; node_id < num_nodes; node_id++) {
436                 top_dev = kfd_create_topology_device();
437                 if (!top_dev) {
438                         kfd_release_live_view();
439                         return -ENOMEM;
440                 }
441         }
442
443         sys_props.platform_id =
444                 (*((uint64_t *)crat_table->oem_id)) & CRAT_OEMID_64BIT_MASK;
445         sys_props.platform_oem = *((uint64_t *)crat_table->oem_table_id);
446         sys_props.platform_rev = crat_table->revision;
447
448         sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
449         while ((char *)sub_type_hdr + sizeof(struct crat_subtype_generic) <
450                         ((char *)crat_image) + image_len) {
451                 if (sub_type_hdr->flags & CRAT_SUBTYPE_FLAGS_ENABLED) {
452                         ret = kfd_parse_subtype(sub_type_hdr);
453                         if (ret != 0) {
454                                 kfd_release_live_view();
455                                 return ret;
456                         }
457                 }
458
459                 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
460                                 sub_type_hdr->length);
461         }
462
463         sys_props.generation_count++;
464         topology_crat_parsed = 1;
465
466         return 0;
467 }
468
469
470 #define sysfs_show_gen_prop(buffer, fmt, ...) \
471                 snprintf(buffer, PAGE_SIZE, "%s"fmt, buffer, __VA_ARGS__)
472 #define sysfs_show_32bit_prop(buffer, name, value) \
473                 sysfs_show_gen_prop(buffer, "%s %u\n", name, value)
474 #define sysfs_show_64bit_prop(buffer, name, value) \
475                 sysfs_show_gen_prop(buffer, "%s %llu\n", name, value)
476 #define sysfs_show_32bit_val(buffer, value) \
477                 sysfs_show_gen_prop(buffer, "%u\n", value)
478 #define sysfs_show_str_val(buffer, value) \
479                 sysfs_show_gen_prop(buffer, "%s\n", value)
480
481 static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
482                 char *buffer)
483 {
484         ssize_t ret;
485
486         /* Making sure that the buffer is an empty string */
487         buffer[0] = 0;
488
489         if (attr == &sys_props.attr_genid) {
490                 ret = sysfs_show_32bit_val(buffer, sys_props.generation_count);
491         } else if (attr == &sys_props.attr_props) {
492                 sysfs_show_64bit_prop(buffer, "platform_oem",
493                                 sys_props.platform_oem);
494                 sysfs_show_64bit_prop(buffer, "platform_id",
495                                 sys_props.platform_id);
496                 ret = sysfs_show_64bit_prop(buffer, "platform_rev",
497                                 sys_props.platform_rev);
498         } else {
499                 ret = -EINVAL;
500         }
501
502         return ret;
503 }
504
505 static const struct sysfs_ops sysprops_ops = {
506         .show = sysprops_show,
507 };
508
509 static struct kobj_type sysprops_type = {
510         .sysfs_ops = &sysprops_ops,
511 };
512
513 static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
514                 char *buffer)
515 {
516         ssize_t ret;
517         struct kfd_iolink_properties *iolink;
518
519         /* Making sure that the buffer is an empty string */
520         buffer[0] = 0;
521
522         iolink = container_of(attr, struct kfd_iolink_properties, attr);
523         sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
524         sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
525         sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
526         sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
527         sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
528         sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
529         sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
530         sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
531         sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
532         sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
533         sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
534                         iolink->rec_transfer_size);
535         ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
536
537         return ret;
538 }
539
540 static const struct sysfs_ops iolink_ops = {
541         .show = iolink_show,
542 };
543
544 static struct kobj_type iolink_type = {
545         .sysfs_ops = &iolink_ops,
546 };
547
548 static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
549                 char *buffer)
550 {
551         ssize_t ret;
552         struct kfd_mem_properties *mem;
553
554         /* Making sure that the buffer is an empty string */
555         buffer[0] = 0;
556
557         mem = container_of(attr, struct kfd_mem_properties, attr);
558         sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
559         sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
560         sysfs_show_32bit_prop(buffer, "flags", mem->flags);
561         sysfs_show_32bit_prop(buffer, "width", mem->width);
562         ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
563
564         return ret;
565 }
566
567 static const struct sysfs_ops mem_ops = {
568         .show = mem_show,
569 };
570
571 static struct kobj_type mem_type = {
572         .sysfs_ops = &mem_ops,
573 };
574
575 static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
576                 char *buffer)
577 {
578         ssize_t ret;
579         uint32_t i;
580         struct kfd_cache_properties *cache;
581
582         /* Making sure that the buffer is an empty string */
583         buffer[0] = 0;
584
585         cache = container_of(attr, struct kfd_cache_properties, attr);
586         sysfs_show_32bit_prop(buffer, "processor_id_low",
587                         cache->processor_id_low);
588         sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
589         sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
590         sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
591         sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
592                         cache->cachelines_per_tag);
593         sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
594         sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
595         sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
596         snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
597         for (i = 0; i < KFD_TOPOLOGY_CPU_SIBLINGS; i++)
598                 ret = snprintf(buffer, PAGE_SIZE, "%s%d%s",
599                                 buffer, cache->sibling_map[i],
600                                 (i == KFD_TOPOLOGY_CPU_SIBLINGS-1) ?
601                                                 "\n" : ",");
602
603         return ret;
604 }
605
606 static const struct sysfs_ops cache_ops = {
607         .show = kfd_cache_show,
608 };
609
610 static struct kobj_type cache_type = {
611         .sysfs_ops = &cache_ops,
612 };
613
614 static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
615                 char *buffer)
616 {
617         struct kfd_topology_device *dev;
618         char public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE];
619         uint32_t i;
620         uint32_t log_max_watch_addr;
621
622         /* Making sure that the buffer is an empty string */
623         buffer[0] = 0;
624
625         if (strcmp(attr->name, "gpu_id") == 0) {
626                 dev = container_of(attr, struct kfd_topology_device,
627                                 attr_gpuid);
628                 return sysfs_show_32bit_val(buffer, dev->gpu_id);
629         }
630
631         if (strcmp(attr->name, "name") == 0) {
632                 dev = container_of(attr, struct kfd_topology_device,
633                                 attr_name);
634                 for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE; i++) {
635                         public_name[i] =
636                                         (char)dev->node_props.marketing_name[i];
637                         if (dev->node_props.marketing_name[i] == 0)
638                                 break;
639                 }
640                 public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1] = 0x0;
641                 return sysfs_show_str_val(buffer, public_name);
642         }
643
644         dev = container_of(attr, struct kfd_topology_device,
645                         attr_props);
646         sysfs_show_32bit_prop(buffer, "cpu_cores_count",
647                         dev->node_props.cpu_cores_count);
648         sysfs_show_32bit_prop(buffer, "simd_count",
649                         dev->node_props.simd_count);
650
651         if (dev->mem_bank_count < dev->node_props.mem_banks_count) {
652                 pr_info_once("mem_banks_count truncated from %d to %d\n",
653                                 dev->node_props.mem_banks_count,
654                                 dev->mem_bank_count);
655                 sysfs_show_32bit_prop(buffer, "mem_banks_count",
656                                 dev->mem_bank_count);
657         } else {
658                 sysfs_show_32bit_prop(buffer, "mem_banks_count",
659                                 dev->node_props.mem_banks_count);
660         }
661
662         sysfs_show_32bit_prop(buffer, "caches_count",
663                         dev->node_props.caches_count);
664         sysfs_show_32bit_prop(buffer, "io_links_count",
665                         dev->node_props.io_links_count);
666         sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
667                         dev->node_props.cpu_core_id_base);
668         sysfs_show_32bit_prop(buffer, "simd_id_base",
669                         dev->node_props.simd_id_base);
670         sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
671                         dev->node_props.max_waves_per_simd);
672         sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
673                         dev->node_props.lds_size_in_kb);
674         sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
675                         dev->node_props.gds_size_in_kb);
676         sysfs_show_32bit_prop(buffer, "wave_front_size",
677                         dev->node_props.wave_front_size);
678         sysfs_show_32bit_prop(buffer, "array_count",
679                         dev->node_props.array_count);
680         sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
681                         dev->node_props.simd_arrays_per_engine);
682         sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
683                         dev->node_props.cu_per_simd_array);
684         sysfs_show_32bit_prop(buffer, "simd_per_cu",
685                         dev->node_props.simd_per_cu);
686         sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
687                         dev->node_props.max_slots_scratch_cu);
688         sysfs_show_32bit_prop(buffer, "vendor_id",
689                         dev->node_props.vendor_id);
690         sysfs_show_32bit_prop(buffer, "device_id",
691                         dev->node_props.device_id);
692         sysfs_show_32bit_prop(buffer, "location_id",
693                         dev->node_props.location_id);
694
695         if (dev->gpu) {
696                 log_max_watch_addr =
697                         __ilog2_u32(dev->gpu->device_info->num_of_watch_points);
698
699                 if (log_max_watch_addr) {
700                         dev->node_props.capability |=
701                                         HSA_CAP_WATCH_POINTS_SUPPORTED;
702
703                         dev->node_props.capability |=
704                                 ((log_max_watch_addr <<
705                                         HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
706                                 HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
707                 }
708
709                 sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
710                         dev->gpu->kfd2kgd->get_max_engine_clock_in_mhz(
711                                         dev->gpu->kgd));
712
713                 sysfs_show_64bit_prop(buffer, "local_mem_size",
714                                 (unsigned long long int) 0);
715
716                 sysfs_show_32bit_prop(buffer, "fw_version",
717                         dev->gpu->kfd2kgd->get_fw_version(
718                                                 dev->gpu->kgd,
719                                                 KGD_ENGINE_MEC1));
720                 sysfs_show_32bit_prop(buffer, "capability",
721                                 dev->node_props.capability);
722         }
723
724         return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
725                                         cpufreq_quick_get_max(0)/1000);
726 }
727
728 static const struct sysfs_ops node_ops = {
729         .show = node_show,
730 };
731
732 static struct kobj_type node_type = {
733         .sysfs_ops = &node_ops,
734 };
735
736 static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
737 {
738         sysfs_remove_file(kobj, attr);
739         kobject_del(kobj);
740         kobject_put(kobj);
741 }
742
743 static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
744 {
745         struct kfd_iolink_properties *iolink;
746         struct kfd_cache_properties *cache;
747         struct kfd_mem_properties *mem;
748
749         if (dev->kobj_iolink) {
750                 list_for_each_entry(iolink, &dev->io_link_props, list)
751                         if (iolink->kobj) {
752                                 kfd_remove_sysfs_file(iolink->kobj,
753                                                         &iolink->attr);
754                                 iolink->kobj = NULL;
755                         }
756                 kobject_del(dev->kobj_iolink);
757                 kobject_put(dev->kobj_iolink);
758                 dev->kobj_iolink = NULL;
759         }
760
761         if (dev->kobj_cache) {
762                 list_for_each_entry(cache, &dev->cache_props, list)
763                         if (cache->kobj) {
764                                 kfd_remove_sysfs_file(cache->kobj,
765                                                         &cache->attr);
766                                 cache->kobj = NULL;
767                         }
768                 kobject_del(dev->kobj_cache);
769                 kobject_put(dev->kobj_cache);
770                 dev->kobj_cache = NULL;
771         }
772
773         if (dev->kobj_mem) {
774                 list_for_each_entry(mem, &dev->mem_props, list)
775                         if (mem->kobj) {
776                                 kfd_remove_sysfs_file(mem->kobj, &mem->attr);
777                                 mem->kobj = NULL;
778                         }
779                 kobject_del(dev->kobj_mem);
780                 kobject_put(dev->kobj_mem);
781                 dev->kobj_mem = NULL;
782         }
783
784         if (dev->kobj_node) {
785                 sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
786                 sysfs_remove_file(dev->kobj_node, &dev->attr_name);
787                 sysfs_remove_file(dev->kobj_node, &dev->attr_props);
788                 kobject_del(dev->kobj_node);
789                 kobject_put(dev->kobj_node);
790                 dev->kobj_node = NULL;
791         }
792 }
793
794 static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
795                 uint32_t id)
796 {
797         struct kfd_iolink_properties *iolink;
798         struct kfd_cache_properties *cache;
799         struct kfd_mem_properties *mem;
800         int ret;
801         uint32_t i;
802
803         if (WARN_ON(dev->kobj_node))
804                 return -EEXIST;
805
806         /*
807          * Creating the sysfs folders
808          */
809         dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
810         if (!dev->kobj_node)
811                 return -ENOMEM;
812
813         ret = kobject_init_and_add(dev->kobj_node, &node_type,
814                         sys_props.kobj_nodes, "%d", id);
815         if (ret < 0)
816                 return ret;
817
818         dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
819         if (!dev->kobj_mem)
820                 return -ENOMEM;
821
822         dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
823         if (!dev->kobj_cache)
824                 return -ENOMEM;
825
826         dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
827         if (!dev->kobj_iolink)
828                 return -ENOMEM;
829
830         /*
831          * Creating sysfs files for node properties
832          */
833         dev->attr_gpuid.name = "gpu_id";
834         dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
835         sysfs_attr_init(&dev->attr_gpuid);
836         dev->attr_name.name = "name";
837         dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
838         sysfs_attr_init(&dev->attr_name);
839         dev->attr_props.name = "properties";
840         dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
841         sysfs_attr_init(&dev->attr_props);
842         ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
843         if (ret < 0)
844                 return ret;
845         ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
846         if (ret < 0)
847                 return ret;
848         ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
849         if (ret < 0)
850                 return ret;
851
852         i = 0;
853         list_for_each_entry(mem, &dev->mem_props, list) {
854                 mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
855                 if (!mem->kobj)
856                         return -ENOMEM;
857                 ret = kobject_init_and_add(mem->kobj, &mem_type,
858                                 dev->kobj_mem, "%d", i);
859                 if (ret < 0)
860                         return ret;
861
862                 mem->attr.name = "properties";
863                 mem->attr.mode = KFD_SYSFS_FILE_MODE;
864                 sysfs_attr_init(&mem->attr);
865                 ret = sysfs_create_file(mem->kobj, &mem->attr);
866                 if (ret < 0)
867                         return ret;
868                 i++;
869         }
870
871         i = 0;
872         list_for_each_entry(cache, &dev->cache_props, list) {
873                 cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
874                 if (!cache->kobj)
875                         return -ENOMEM;
876                 ret = kobject_init_and_add(cache->kobj, &cache_type,
877                                 dev->kobj_cache, "%d", i);
878                 if (ret < 0)
879                         return ret;
880
881                 cache->attr.name = "properties";
882                 cache->attr.mode = KFD_SYSFS_FILE_MODE;
883                 sysfs_attr_init(&cache->attr);
884                 ret = sysfs_create_file(cache->kobj, &cache->attr);
885                 if (ret < 0)
886                         return ret;
887                 i++;
888         }
889
890         i = 0;
891         list_for_each_entry(iolink, &dev->io_link_props, list) {
892                 iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
893                 if (!iolink->kobj)
894                         return -ENOMEM;
895                 ret = kobject_init_and_add(iolink->kobj, &iolink_type,
896                                 dev->kobj_iolink, "%d", i);
897                 if (ret < 0)
898                         return ret;
899
900                 iolink->attr.name = "properties";
901                 iolink->attr.mode = KFD_SYSFS_FILE_MODE;
902                 sysfs_attr_init(&iolink->attr);
903                 ret = sysfs_create_file(iolink->kobj, &iolink->attr);
904                 if (ret < 0)
905                         return ret;
906                 i++;
907 }
908
909         return 0;
910 }
911
912 static int kfd_build_sysfs_node_tree(void)
913 {
914         struct kfd_topology_device *dev;
915         int ret;
916         uint32_t i = 0;
917
918         list_for_each_entry(dev, &topology_device_list, list) {
919                 ret = kfd_build_sysfs_node_entry(dev, i);
920                 if (ret < 0)
921                         return ret;
922                 i++;
923         }
924
925         return 0;
926 }
927
928 static void kfd_remove_sysfs_node_tree(void)
929 {
930         struct kfd_topology_device *dev;
931
932         list_for_each_entry(dev, &topology_device_list, list)
933                 kfd_remove_sysfs_node_entry(dev);
934 }
935
936 static int kfd_topology_update_sysfs(void)
937 {
938         int ret;
939
940         pr_info("Creating topology SYSFS entries\n");
941         if (!sys_props.kobj_topology) {
942                 sys_props.kobj_topology =
943                                 kfd_alloc_struct(sys_props.kobj_topology);
944                 if (!sys_props.kobj_topology)
945                         return -ENOMEM;
946
947                 ret = kobject_init_and_add(sys_props.kobj_topology,
948                                 &sysprops_type,  &kfd_device->kobj,
949                                 "topology");
950                 if (ret < 0)
951                         return ret;
952
953                 sys_props.kobj_nodes = kobject_create_and_add("nodes",
954                                 sys_props.kobj_topology);
955                 if (!sys_props.kobj_nodes)
956                         return -ENOMEM;
957
958                 sys_props.attr_genid.name = "generation_id";
959                 sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
960                 sysfs_attr_init(&sys_props.attr_genid);
961                 ret = sysfs_create_file(sys_props.kobj_topology,
962                                 &sys_props.attr_genid);
963                 if (ret < 0)
964                         return ret;
965
966                 sys_props.attr_props.name = "system_properties";
967                 sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
968                 sysfs_attr_init(&sys_props.attr_props);
969                 ret = sysfs_create_file(sys_props.kobj_topology,
970                                 &sys_props.attr_props);
971                 if (ret < 0)
972                         return ret;
973         }
974
975         kfd_remove_sysfs_node_tree();
976
977         return kfd_build_sysfs_node_tree();
978 }
979
980 static void kfd_topology_release_sysfs(void)
981 {
982         kfd_remove_sysfs_node_tree();
983         if (sys_props.kobj_topology) {
984                 sysfs_remove_file(sys_props.kobj_topology,
985                                 &sys_props.attr_genid);
986                 sysfs_remove_file(sys_props.kobj_topology,
987                                 &sys_props.attr_props);
988                 if (sys_props.kobj_nodes) {
989                         kobject_del(sys_props.kobj_nodes);
990                         kobject_put(sys_props.kobj_nodes);
991                         sys_props.kobj_nodes = NULL;
992                 }
993                 kobject_del(sys_props.kobj_topology);
994                 kobject_put(sys_props.kobj_topology);
995                 sys_props.kobj_topology = NULL;
996         }
997 }
998
999 int kfd_topology_init(void)
1000 {
1001         void *crat_image = NULL;
1002         size_t image_size = 0;
1003         int ret;
1004
1005         /*
1006          * Initialize the head for the topology device list
1007          */
1008         INIT_LIST_HEAD(&topology_device_list);
1009         init_rwsem(&topology_lock);
1010         topology_crat_parsed = 0;
1011
1012         memset(&sys_props, 0, sizeof(sys_props));
1013
1014         /*
1015          * Get the CRAT image from the ACPI
1016          */
1017         ret = kfd_topology_get_crat_acpi(crat_image, &image_size);
1018         if (ret == 0 && image_size > 0) {
1019                 pr_info("Found CRAT image with size=%zd\n", image_size);
1020                 crat_image = kmalloc(image_size, GFP_KERNEL);
1021                 if (!crat_image) {
1022                         ret = -ENOMEM;
1023                         pr_err("No memory for allocating CRAT image\n");
1024                         goto err;
1025                 }
1026                 ret = kfd_topology_get_crat_acpi(crat_image, &image_size);
1027
1028                 if (ret == 0) {
1029                         down_write(&topology_lock);
1030                         ret = kfd_parse_crat_table(crat_image);
1031                         if (ret == 0)
1032                                 ret = kfd_topology_update_sysfs();
1033                         up_write(&topology_lock);
1034                 } else {
1035                         pr_err("Couldn't get CRAT table size from ACPI\n");
1036                 }
1037                 kfree(crat_image);
1038         } else if (ret == -ENODATA) {
1039                 ret = 0;
1040         } else {
1041                 pr_err("Couldn't get CRAT table size from ACPI\n");
1042         }
1043
1044 err:
1045         pr_info("Finished initializing topology ret=%d\n", ret);
1046         return ret;
1047 }
1048
1049 void kfd_topology_shutdown(void)
1050 {
1051         kfd_topology_release_sysfs();
1052         kfd_release_live_view();
1053 }
1054
1055 static void kfd_debug_print_topology(void)
1056 {
1057         struct kfd_topology_device *dev;
1058         uint32_t i = 0;
1059
1060         pr_info("DEBUG PRINT OF TOPOLOGY:");
1061         list_for_each_entry(dev, &topology_device_list, list) {
1062                 pr_info("Node: %d\n", i);
1063                 pr_info("\tGPU assigned: %s\n", (dev->gpu ? "yes" : "no"));
1064                 pr_info("\tCPU count: %d\n", dev->node_props.cpu_cores_count);
1065                 pr_info("\tSIMD count: %d", dev->node_props.simd_count);
1066                 i++;
1067         }
1068 }
1069
1070 static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
1071 {
1072         uint32_t hashout;
1073         uint32_t buf[7];
1074         uint64_t local_mem_size;
1075         int i;
1076
1077         if (!gpu)
1078                 return 0;
1079
1080         local_mem_size = gpu->kfd2kgd->get_vmem_size(gpu->kgd);
1081
1082         buf[0] = gpu->pdev->devfn;
1083         buf[1] = gpu->pdev->subsystem_vendor;
1084         buf[2] = gpu->pdev->subsystem_device;
1085         buf[3] = gpu->pdev->device;
1086         buf[4] = gpu->pdev->bus->number;
1087         buf[5] = lower_32_bits(local_mem_size);
1088         buf[6] = upper_32_bits(local_mem_size);
1089
1090         for (i = 0, hashout = 0; i < 7; i++)
1091                 hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
1092
1093         return hashout;
1094 }
1095
1096 static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
1097 {
1098         struct kfd_topology_device *dev;
1099         struct kfd_topology_device *out_dev = NULL;
1100
1101         list_for_each_entry(dev, &topology_device_list, list)
1102                 if (!dev->gpu && (dev->node_props.simd_count > 0)) {
1103                         dev->gpu = gpu;
1104                         out_dev = dev;
1105                         break;
1106                 }
1107
1108         return out_dev;
1109 }
1110
1111 static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
1112 {
1113         /*
1114          * TODO: Generate an event for thunk about the arrival/removal
1115          * of the GPU
1116          */
1117 }
1118
1119 int kfd_topology_add_device(struct kfd_dev *gpu)
1120 {
1121         uint32_t gpu_id;
1122         struct kfd_topology_device *dev;
1123         int res;
1124
1125         gpu_id = kfd_generate_gpu_id(gpu);
1126
1127         pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1128
1129         down_write(&topology_lock);
1130         /*
1131          * Try to assign the GPU to existing topology device (generated from
1132          * CRAT table
1133          */
1134         dev = kfd_assign_gpu(gpu);
1135         if (!dev) {
1136                 pr_info("GPU was not found in the current topology. Extending.\n");
1137                 kfd_debug_print_topology();
1138                 dev = kfd_create_topology_device();
1139                 if (!dev) {
1140                         res = -ENOMEM;
1141                         goto err;
1142                 }
1143                 dev->gpu = gpu;
1144
1145                 /*
1146                  * TODO: Make a call to retrieve topology information from the
1147                  * GPU vBIOS
1148                  */
1149
1150                 /* Update the SYSFS tree, since we added another topology
1151                  * device
1152                  */
1153                 if (kfd_topology_update_sysfs() < 0)
1154                         kfd_topology_release_sysfs();
1155
1156         }
1157
1158         dev->gpu_id = gpu_id;
1159         gpu->id = gpu_id;
1160         dev->node_props.vendor_id = gpu->pdev->vendor;
1161         dev->node_props.device_id = gpu->pdev->device;
1162         dev->node_props.location_id = (gpu->pdev->bus->number << 24) +
1163                         (gpu->pdev->devfn & 0xffffff);
1164         /*
1165          * TODO: Retrieve max engine clock values from KGD
1166          */
1167
1168         if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
1169                 dev->node_props.capability |= HSA_CAP_DOORBELL_PACKET_TYPE;
1170                 pr_info("Adding doorbell packet type capability\n");
1171         }
1172
1173         res = 0;
1174
1175 err:
1176         up_write(&topology_lock);
1177
1178         if (res == 0)
1179                 kfd_notify_gpu_change(gpu_id, 1);
1180
1181         return res;
1182 }
1183
1184 int kfd_topology_remove_device(struct kfd_dev *gpu)
1185 {
1186         struct kfd_topology_device *dev;
1187         uint32_t gpu_id;
1188         int res = -ENODEV;
1189
1190         down_write(&topology_lock);
1191
1192         list_for_each_entry(dev, &topology_device_list, list)
1193                 if (dev->gpu == gpu) {
1194                         gpu_id = dev->gpu_id;
1195                         kfd_remove_sysfs_node_entry(dev);
1196                         kfd_release_topology_device(dev);
1197                         res = 0;
1198                         if (kfd_topology_update_sysfs() < 0)
1199                                 kfd_topology_release_sysfs();
1200                         break;
1201                 }
1202
1203         up_write(&topology_lock);
1204
1205         if (res == 0)
1206                 kfd_notify_gpu_change(gpu_id, 0);
1207
1208         return res;
1209 }
1210
1211 /*
1212  * When idx is out of bounds, the function will return NULL
1213  */
1214 struct kfd_dev *kfd_topology_enum_kfd_devices(uint8_t idx)
1215 {
1216
1217         struct kfd_topology_device *top_dev;
1218         struct kfd_dev *device = NULL;
1219         uint8_t device_idx = 0;
1220
1221         down_read(&topology_lock);
1222
1223         list_for_each_entry(top_dev, &topology_device_list, list) {
1224                 if (device_idx == idx) {
1225                         device = top_dev->gpu;
1226                         break;
1227                 }
1228
1229                 device_idx++;
1230         }
1231
1232         up_read(&topology_lock);
1233
1234         return device;
1235
1236 }
1237
1238 #if defined(CONFIG_DEBUG_FS)
1239
1240 int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
1241 {
1242         struct kfd_topology_device *dev;
1243         unsigned int i = 0;
1244         int r = 0;
1245
1246         down_read(&topology_lock);
1247
1248         list_for_each_entry(dev, &topology_device_list, list) {
1249                 if (!dev->gpu) {
1250                         i++;
1251                         continue;
1252                 }
1253
1254                 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1255                 r = dqm_debugfs_hqds(m, dev->gpu->dqm);
1256                 if (r)
1257                         break;
1258         }
1259
1260         up_read(&topology_lock);
1261
1262         return r;
1263 }
1264
1265 int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
1266 {
1267         struct kfd_topology_device *dev;
1268         unsigned int i = 0;
1269         int r = 0;
1270
1271         down_read(&topology_lock);
1272
1273         list_for_each_entry(dev, &topology_device_list, list) {
1274                 if (!dev->gpu) {
1275                         i++;
1276                         continue;
1277                 }
1278
1279                 seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1280                 r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets);
1281                 if (r)
1282                         break;
1283         }
1284
1285         up_read(&topology_lock);
1286
1287         return r;
1288 }
1289
1290 #endif