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