1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/pci.h>
24#include <linux/acpi.h>
25#include "kfd_crat.h"
26#include "kfd_priv.h"
27#include "kfd_topology.h"
28#include "kfd_iommu.h"
29#include "amdgpu_amdkfd.h"
30
31
32
33
34
35
36static uint32_t gpu_processor_id_low = 0x80001000;
37
38
39
40
41
42static inline unsigned int get_and_inc_gpu_processor_id(
43 unsigned int total_cu_count)
44{
45 int current_id = gpu_processor_id_low;
46
47 gpu_processor_id_low += total_cu_count;
48 return current_id;
49}
50
51
52struct kfd_gpu_cache_info {
53 uint32_t cache_size;
54 uint32_t cache_level;
55 uint32_t flags;
56
57
58
59 uint32_t num_cu_shared;
60};
61
62static struct kfd_gpu_cache_info kaveri_cache_info[] = {
63 {
64
65 .cache_size = 16,
66 .cache_level = 1,
67 .flags = (CRAT_CACHE_FLAGS_ENABLED |
68 CRAT_CACHE_FLAGS_DATA_CACHE |
69 CRAT_CACHE_FLAGS_SIMD_CACHE),
70 .num_cu_shared = 1,
71
72 },
73 {
74
75 .cache_size = 16,
76 .cache_level = 1,
77 .flags = (CRAT_CACHE_FLAGS_ENABLED |
78 CRAT_CACHE_FLAGS_INST_CACHE |
79 CRAT_CACHE_FLAGS_SIMD_CACHE),
80 .num_cu_shared = 2,
81 },
82 {
83
84 .cache_size = 8,
85 .cache_level = 1,
86 .flags = (CRAT_CACHE_FLAGS_ENABLED |
87 CRAT_CACHE_FLAGS_DATA_CACHE |
88 CRAT_CACHE_FLAGS_SIMD_CACHE),
89 .num_cu_shared = 2,
90 },
91
92
93};
94
95
96static struct kfd_gpu_cache_info carrizo_cache_info[] = {
97 {
98
99 .cache_size = 16,
100 .cache_level = 1,
101 .flags = (CRAT_CACHE_FLAGS_ENABLED |
102 CRAT_CACHE_FLAGS_DATA_CACHE |
103 CRAT_CACHE_FLAGS_SIMD_CACHE),
104 .num_cu_shared = 1,
105 },
106 {
107
108 .cache_size = 8,
109 .cache_level = 1,
110 .flags = (CRAT_CACHE_FLAGS_ENABLED |
111 CRAT_CACHE_FLAGS_INST_CACHE |
112 CRAT_CACHE_FLAGS_SIMD_CACHE),
113 .num_cu_shared = 4,
114 },
115 {
116
117 .cache_size = 4,
118 .cache_level = 1,
119 .flags = (CRAT_CACHE_FLAGS_ENABLED |
120 CRAT_CACHE_FLAGS_DATA_CACHE |
121 CRAT_CACHE_FLAGS_SIMD_CACHE),
122 .num_cu_shared = 4,
123 },
124
125
126};
127
128
129
130
131#define hawaii_cache_info kaveri_cache_info
132#define tonga_cache_info carrizo_cache_info
133#define fiji_cache_info carrizo_cache_info
134#define polaris10_cache_info carrizo_cache_info
135#define polaris11_cache_info carrizo_cache_info
136#define polaris12_cache_info carrizo_cache_info
137#define vegam_cache_info carrizo_cache_info
138
139#define vega10_cache_info carrizo_cache_info
140#define raven_cache_info carrizo_cache_info
141
142#define navi10_cache_info carrizo_cache_info
143
144static void kfd_populated_cu_info_cpu(struct kfd_topology_device *dev,
145 struct crat_subtype_computeunit *cu)
146{
147 dev->node_props.cpu_cores_count = cu->num_cpu_cores;
148 dev->node_props.cpu_core_id_base = cu->processor_id_low;
149 if (cu->hsa_capability & CRAT_CU_FLAGS_IOMMU_PRESENT)
150 dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
151
152 pr_debug("CU CPU: cores=%d id_base=%d\n", cu->num_cpu_cores,
153 cu->processor_id_low);
154}
155
156static void kfd_populated_cu_info_gpu(struct kfd_topology_device *dev,
157 struct crat_subtype_computeunit *cu)
158{
159 dev->node_props.simd_id_base = cu->processor_id_low;
160 dev->node_props.simd_count = cu->num_simd_cores;
161 dev->node_props.lds_size_in_kb = cu->lds_size_in_kb;
162 dev->node_props.max_waves_per_simd = cu->max_waves_simd;
163 dev->node_props.wave_front_size = cu->wave_front_size;
164 dev->node_props.array_count = cu->array_count;
165 dev->node_props.cu_per_simd_array = cu->num_cu_per_array;
166 dev->node_props.simd_per_cu = cu->num_simd_per_cu;
167 dev->node_props.max_slots_scratch_cu = cu->max_slots_scatch_cu;
168 if (cu->hsa_capability & CRAT_CU_FLAGS_HOT_PLUGGABLE)
169 dev->node_props.capability |= HSA_CAP_HOT_PLUGGABLE;
170 pr_debug("CU GPU: id_base=%d\n", cu->processor_id_low);
171}
172
173
174
175
176static int kfd_parse_subtype_cu(struct crat_subtype_computeunit *cu,
177 struct list_head *device_list)
178{
179 struct kfd_topology_device *dev;
180
181 pr_debug("Found CU entry in CRAT table with proximity_domain=%d caps=%x\n",
182 cu->proximity_domain, cu->hsa_capability);
183 list_for_each_entry(dev, device_list, list) {
184 if (cu->proximity_domain == dev->proximity_domain) {
185 if (cu->flags & CRAT_CU_FLAGS_CPU_PRESENT)
186 kfd_populated_cu_info_cpu(dev, cu);
187
188 if (cu->flags & CRAT_CU_FLAGS_GPU_PRESENT)
189 kfd_populated_cu_info_gpu(dev, cu);
190 break;
191 }
192 }
193
194 return 0;
195}
196
197static struct kfd_mem_properties *
198find_subtype_mem(uint32_t heap_type, uint32_t flags, uint32_t width,
199 struct kfd_topology_device *dev)
200{
201 struct kfd_mem_properties *props;
202
203 list_for_each_entry(props, &dev->mem_props, list) {
204 if (props->heap_type == heap_type
205 && props->flags == flags
206 && props->width == width)
207 return props;
208 }
209
210 return NULL;
211}
212
213
214
215static int kfd_parse_subtype_mem(struct crat_subtype_memory *mem,
216 struct list_head *device_list)
217{
218 struct kfd_mem_properties *props;
219 struct kfd_topology_device *dev;
220 uint32_t heap_type;
221 uint64_t size_in_bytes;
222 uint32_t flags = 0;
223 uint32_t width;
224
225 pr_debug("Found memory entry in CRAT table with proximity_domain=%d\n",
226 mem->proximity_domain);
227 list_for_each_entry(dev, device_list, list) {
228 if (mem->proximity_domain == dev->proximity_domain) {
229
230 if (dev->node_props.cpu_cores_count == 0) {
231
232 if (mem->visibility_type == 0)
233 heap_type =
234 HSA_MEM_HEAP_TYPE_FB_PRIVATE;
235
236 else
237 heap_type = mem->visibility_type;
238 } else
239 heap_type = HSA_MEM_HEAP_TYPE_SYSTEM;
240
241 if (mem->flags & CRAT_MEM_FLAGS_HOT_PLUGGABLE)
242 flags |= HSA_MEM_FLAGS_HOT_PLUGGABLE;
243 if (mem->flags & CRAT_MEM_FLAGS_NON_VOLATILE)
244 flags |= HSA_MEM_FLAGS_NON_VOLATILE;
245
246 size_in_bytes =
247 ((uint64_t)mem->length_high << 32) +
248 mem->length_low;
249 width = mem->width;
250
251
252
253
254
255
256 props = find_subtype_mem(heap_type, flags, width, dev);
257 if (props) {
258 props->size_in_bytes += size_in_bytes;
259 break;
260 }
261
262 props = kfd_alloc_struct(props);
263 if (!props)
264 return -ENOMEM;
265
266 props->heap_type = heap_type;
267 props->flags = flags;
268 props->size_in_bytes = size_in_bytes;
269 props->width = width;
270
271 dev->node_props.mem_banks_count++;
272 list_add_tail(&props->list, &dev->mem_props);
273
274 break;
275 }
276 }
277
278 return 0;
279}
280
281
282
283
284static int kfd_parse_subtype_cache(struct crat_subtype_cache *cache,
285 struct list_head *device_list)
286{
287 struct kfd_cache_properties *props;
288 struct kfd_topology_device *dev;
289 uint32_t id;
290 uint32_t total_num_of_cu;
291
292 id = cache->processor_id_low;
293
294 pr_debug("Found cache entry in CRAT table with processor_id=%d\n", id);
295 list_for_each_entry(dev, device_list, list) {
296 total_num_of_cu = (dev->node_props.array_count *
297 dev->node_props.cu_per_simd_array);
298
299
300
301
302
303
304
305
306
307 if ((id >= dev->node_props.cpu_core_id_base &&
308 id <= dev->node_props.cpu_core_id_base +
309 dev->node_props.cpu_cores_count) ||
310 (id >= dev->node_props.simd_id_base &&
311 id < dev->node_props.simd_id_base +
312 total_num_of_cu)) {
313 props = kfd_alloc_struct(props);
314 if (!props)
315 return -ENOMEM;
316
317 props->processor_id_low = id;
318 props->cache_level = cache->cache_level;
319 props->cache_size = cache->cache_size;
320 props->cacheline_size = cache->cache_line_size;
321 props->cachelines_per_tag = cache->lines_per_tag;
322 props->cache_assoc = cache->associativity;
323 props->cache_latency = cache->cache_latency;
324 memcpy(props->sibling_map, cache->sibling_map,
325 sizeof(props->sibling_map));
326
327 if (cache->flags & CRAT_CACHE_FLAGS_DATA_CACHE)
328 props->cache_type |= HSA_CACHE_TYPE_DATA;
329 if (cache->flags & CRAT_CACHE_FLAGS_INST_CACHE)
330 props->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
331 if (cache->flags & CRAT_CACHE_FLAGS_CPU_CACHE)
332 props->cache_type |= HSA_CACHE_TYPE_CPU;
333 if (cache->flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
334 props->cache_type |= HSA_CACHE_TYPE_HSACU;
335
336 dev->cache_count++;
337 dev->node_props.caches_count++;
338 list_add_tail(&props->list, &dev->cache_props);
339
340 break;
341 }
342 }
343
344 return 0;
345}
346
347
348
349
350static int kfd_parse_subtype_iolink(struct crat_subtype_iolink *iolink,
351 struct list_head *device_list)
352{
353 struct kfd_iolink_properties *props = NULL, *props2;
354 struct kfd_topology_device *dev, *to_dev;
355 uint32_t id_from;
356 uint32_t id_to;
357
358 id_from = iolink->proximity_domain_from;
359 id_to = iolink->proximity_domain_to;
360
361 pr_debug("Found IO link entry in CRAT table with id_from=%d, id_to %d\n",
362 id_from, id_to);
363 list_for_each_entry(dev, device_list, list) {
364 if (id_from == dev->proximity_domain) {
365 props = kfd_alloc_struct(props);
366 if (!props)
367 return -ENOMEM;
368
369 props->node_from = id_from;
370 props->node_to = id_to;
371 props->ver_maj = iolink->version_major;
372 props->ver_min = iolink->version_minor;
373 props->iolink_type = iolink->io_interface_type;
374
375 if (props->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
376 props->weight = 20;
377 else if (props->iolink_type == CRAT_IOLINK_TYPE_XGMI)
378 props->weight = 15 * iolink->num_hops_xgmi;
379 else
380 props->weight = node_distance(id_from, id_to);
381
382 props->min_latency = iolink->minimum_latency;
383 props->max_latency = iolink->maximum_latency;
384 props->min_bandwidth = iolink->minimum_bandwidth_mbs;
385 props->max_bandwidth = iolink->maximum_bandwidth_mbs;
386 props->rec_transfer_size =
387 iolink->recommended_transfer_size;
388
389 dev->io_link_count++;
390 dev->node_props.io_links_count++;
391 list_add_tail(&props->list, &dev->io_link_props);
392 break;
393 }
394 }
395
396
397
398
399
400
401
402
403
404 if (props && (iolink->flags & CRAT_IOLINK_FLAGS_BI_DIRECTIONAL)) {
405 to_dev = kfd_topology_device_by_proximity_domain(id_to);
406 if (!to_dev)
407 return -ENODEV;
408
409 props2 = kmemdup(props, sizeof(*props2), GFP_KERNEL);
410 props2->node_from = id_to;
411 props2->node_to = id_from;
412 props2->kobj = NULL;
413 to_dev->io_link_count++;
414 to_dev->node_props.io_links_count++;
415 list_add_tail(&props2->list, &to_dev->io_link_props);
416 }
417
418 return 0;
419}
420
421
422
423
424
425
426static int kfd_parse_subtype(struct crat_subtype_generic *sub_type_hdr,
427 struct list_head *device_list)
428{
429 struct crat_subtype_computeunit *cu;
430 struct crat_subtype_memory *mem;
431 struct crat_subtype_cache *cache;
432 struct crat_subtype_iolink *iolink;
433 int ret = 0;
434
435 switch (sub_type_hdr->type) {
436 case CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY:
437 cu = (struct crat_subtype_computeunit *)sub_type_hdr;
438 ret = kfd_parse_subtype_cu(cu, device_list);
439 break;
440 case CRAT_SUBTYPE_MEMORY_AFFINITY:
441 mem = (struct crat_subtype_memory *)sub_type_hdr;
442 ret = kfd_parse_subtype_mem(mem, device_list);
443 break;
444 case CRAT_SUBTYPE_CACHE_AFFINITY:
445 cache = (struct crat_subtype_cache *)sub_type_hdr;
446 ret = kfd_parse_subtype_cache(cache, device_list);
447 break;
448 case CRAT_SUBTYPE_TLB_AFFINITY:
449
450
451
452 pr_debug("Found TLB entry in CRAT table (not processing)\n");
453 break;
454 case CRAT_SUBTYPE_CCOMPUTE_AFFINITY:
455
456
457
458 pr_debug("Found CCOMPUTE entry in CRAT table (not processing)\n");
459 break;
460 case CRAT_SUBTYPE_IOLINK_AFFINITY:
461 iolink = (struct crat_subtype_iolink *)sub_type_hdr;
462 ret = kfd_parse_subtype_iolink(iolink, device_list);
463 break;
464 default:
465 pr_warn("Unknown subtype %d in CRAT\n",
466 sub_type_hdr->type);
467 }
468
469 return ret;
470}
471
472
473
474
475
476
477
478
479
480
481
482int kfd_parse_crat_table(void *crat_image, struct list_head *device_list,
483 uint32_t proximity_domain)
484{
485 struct kfd_topology_device *top_dev = NULL;
486 struct crat_subtype_generic *sub_type_hdr;
487 uint16_t node_id;
488 int ret = 0;
489 struct crat_header *crat_table = (struct crat_header *)crat_image;
490 uint16_t num_nodes;
491 uint32_t image_len;
492
493 if (!crat_image)
494 return -EINVAL;
495
496 if (!list_empty(device_list)) {
497 pr_warn("Error device list should be empty\n");
498 return -EINVAL;
499 }
500
501 num_nodes = crat_table->num_domains;
502 image_len = crat_table->length;
503
504 pr_info("Parsing CRAT table with %d nodes\n", num_nodes);
505
506 for (node_id = 0; node_id < num_nodes; node_id++) {
507 top_dev = kfd_create_topology_device(device_list);
508 if (!top_dev)
509 break;
510 top_dev->proximity_domain = proximity_domain++;
511 }
512
513 if (!top_dev) {
514 ret = -ENOMEM;
515 goto err;
516 }
517
518 memcpy(top_dev->oem_id, crat_table->oem_id, CRAT_OEMID_LENGTH);
519 memcpy(top_dev->oem_table_id, crat_table->oem_table_id,
520 CRAT_OEMTABLEID_LENGTH);
521 top_dev->oem_revision = crat_table->oem_revision;
522
523 sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
524 while ((char *)sub_type_hdr + sizeof(struct crat_subtype_generic) <
525 ((char *)crat_image) + image_len) {
526 if (sub_type_hdr->flags & CRAT_SUBTYPE_FLAGS_ENABLED) {
527 ret = kfd_parse_subtype(sub_type_hdr, device_list);
528 if (ret)
529 break;
530 }
531
532 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
533 sub_type_hdr->length);
534 }
535
536err:
537 if (ret)
538 kfd_release_topology_device_list(device_list);
539
540 return ret;
541}
542
543
544static int fill_in_pcache(struct crat_subtype_cache *pcache,
545 struct kfd_gpu_cache_info *pcache_info,
546 struct kfd_cu_info *cu_info,
547 int mem_available,
548 int cu_bitmask,
549 int cache_type, unsigned int cu_processor_id,
550 int cu_block)
551{
552 unsigned int cu_sibling_map_mask;
553 int first_active_cu;
554
555
556 if (sizeof(struct crat_subtype_cache) > mem_available)
557 return -ENOMEM;
558
559 cu_sibling_map_mask = cu_bitmask;
560 cu_sibling_map_mask >>= cu_block;
561 cu_sibling_map_mask &=
562 ((1 << pcache_info[cache_type].num_cu_shared) - 1);
563 first_active_cu = ffs(cu_sibling_map_mask);
564
565
566
567
568
569 if (first_active_cu) {
570 memset(pcache, 0, sizeof(struct crat_subtype_cache));
571 pcache->type = CRAT_SUBTYPE_CACHE_AFFINITY;
572 pcache->length = sizeof(struct crat_subtype_cache);
573 pcache->flags = pcache_info[cache_type].flags;
574 pcache->processor_id_low = cu_processor_id
575 + (first_active_cu - 1);
576 pcache->cache_level = pcache_info[cache_type].cache_level;
577 pcache->cache_size = pcache_info[cache_type].cache_size;
578
579
580
581
582 cu_sibling_map_mask =
583 cu_sibling_map_mask >> (first_active_cu - 1);
584
585 pcache->sibling_map[0] = (uint8_t)(cu_sibling_map_mask & 0xFF);
586 pcache->sibling_map[1] =
587 (uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
588 pcache->sibling_map[2] =
589 (uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
590 pcache->sibling_map[3] =
591 (uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
592 return 0;
593 }
594 return 1;
595}
596
597
598
599
600
601
602
603
604
605
606
607
608
609static int kfd_fill_gpu_cache_info(struct kfd_dev *kdev,
610 int gpu_processor_id,
611 int available_size,
612 struct kfd_cu_info *cu_info,
613 struct crat_subtype_cache *pcache,
614 int *size_filled,
615 int *num_of_entries)
616{
617 struct kfd_gpu_cache_info *pcache_info;
618 int num_of_cache_types = 0;
619 int i, j, k;
620 int ct = 0;
621 int mem_available = available_size;
622 unsigned int cu_processor_id;
623 int ret;
624
625 switch (kdev->device_info->asic_family) {
626 case CHIP_KAVERI:
627 pcache_info = kaveri_cache_info;
628 num_of_cache_types = ARRAY_SIZE(kaveri_cache_info);
629 break;
630 case CHIP_HAWAII:
631 pcache_info = hawaii_cache_info;
632 num_of_cache_types = ARRAY_SIZE(hawaii_cache_info);
633 break;
634 case CHIP_CARRIZO:
635 pcache_info = carrizo_cache_info;
636 num_of_cache_types = ARRAY_SIZE(carrizo_cache_info);
637 break;
638 case CHIP_TONGA:
639 pcache_info = tonga_cache_info;
640 num_of_cache_types = ARRAY_SIZE(tonga_cache_info);
641 break;
642 case CHIP_FIJI:
643 pcache_info = fiji_cache_info;
644 num_of_cache_types = ARRAY_SIZE(fiji_cache_info);
645 break;
646 case CHIP_POLARIS10:
647 pcache_info = polaris10_cache_info;
648 num_of_cache_types = ARRAY_SIZE(polaris10_cache_info);
649 break;
650 case CHIP_POLARIS11:
651 pcache_info = polaris11_cache_info;
652 num_of_cache_types = ARRAY_SIZE(polaris11_cache_info);
653 break;
654 case CHIP_POLARIS12:
655 pcache_info = polaris12_cache_info;
656 num_of_cache_types = ARRAY_SIZE(polaris12_cache_info);
657 break;
658 case CHIP_VEGAM:
659 pcache_info = vegam_cache_info;
660 num_of_cache_types = ARRAY_SIZE(vegam_cache_info);
661 break;
662 case CHIP_VEGA10:
663 case CHIP_VEGA12:
664 case CHIP_VEGA20:
665 pcache_info = vega10_cache_info;
666 num_of_cache_types = ARRAY_SIZE(vega10_cache_info);
667 break;
668 case CHIP_RAVEN:
669 pcache_info = raven_cache_info;
670 num_of_cache_types = ARRAY_SIZE(raven_cache_info);
671 break;
672 case CHIP_NAVI10:
673 pcache_info = navi10_cache_info;
674 num_of_cache_types = ARRAY_SIZE(navi10_cache_info);
675 break;
676 default:
677 return -EINVAL;
678 }
679
680 *size_filled = 0;
681 *num_of_entries = 0;
682
683
684
685
686
687
688
689
690
691
692
693 for (ct = 0; ct < num_of_cache_types; ct++) {
694 cu_processor_id = gpu_processor_id;
695 for (i = 0; i < cu_info->num_shader_engines; i++) {
696 for (j = 0; j < cu_info->num_shader_arrays_per_engine;
697 j++) {
698 for (k = 0; k < cu_info->num_cu_per_sh;
699 k += pcache_info[ct].num_cu_shared) {
700
701 ret = fill_in_pcache(pcache,
702 pcache_info,
703 cu_info,
704 mem_available,
705 cu_info->cu_bitmap[i][j],
706 ct,
707 cu_processor_id,
708 k);
709
710 if (ret < 0)
711 break;
712
713 if (!ret) {
714 pcache++;
715 (*num_of_entries)++;
716 mem_available -=
717 sizeof(*pcache);
718 (*size_filled) +=
719 sizeof(*pcache);
720 }
721
722
723 cu_processor_id +=
724 pcache_info[ct].num_cu_shared;
725 }
726 }
727 }
728 }
729
730 pr_debug("Added [%d] GPU cache entries\n", *num_of_entries);
731
732 return 0;
733}
734
735
736
737
738
739
740
741
742
743
744
745
746int kfd_create_crat_image_acpi(void **crat_image, size_t *size)
747{
748 struct acpi_table_header *crat_table;
749 acpi_status status;
750 void *pcrat_image;
751
752 if (!crat_image)
753 return -EINVAL;
754
755 *crat_image = NULL;
756
757
758 status = acpi_get_table(CRAT_SIGNATURE, 0, &crat_table);
759 if (status == AE_NOT_FOUND) {
760 pr_warn("CRAT table not found\n");
761 return -ENODATA;
762 } else if (ACPI_FAILURE(status)) {
763 const char *err = acpi_format_exception(status);
764
765 pr_err("CRAT table error: %s\n", err);
766 return -EINVAL;
767 }
768
769 if (ignore_crat) {
770 pr_info("CRAT table disabled by module option\n");
771 return -ENODATA;
772 }
773
774 pcrat_image = kmemdup(crat_table, crat_table->length, GFP_KERNEL);
775 if (!pcrat_image)
776 return -ENOMEM;
777
778 *crat_image = pcrat_image;
779 *size = crat_table->length;
780
781 return 0;
782}
783
784
785
786
787
788
789
790#define VCRAT_SIZE_FOR_CPU (2 * PAGE_SIZE)
791#define VCRAT_SIZE_FOR_GPU (3 * PAGE_SIZE)
792
793
794
795
796
797
798
799
800
801static int kfd_fill_cu_for_cpu(int numa_node_id, int *avail_size,
802 int proximity_domain,
803 struct crat_subtype_computeunit *sub_type_hdr)
804{
805 const struct cpumask *cpumask;
806
807 *avail_size -= sizeof(struct crat_subtype_computeunit);
808 if (*avail_size < 0)
809 return -ENOMEM;
810
811 memset(sub_type_hdr, 0, sizeof(struct crat_subtype_computeunit));
812
813
814 sub_type_hdr->type = CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY;
815 sub_type_hdr->length = sizeof(struct crat_subtype_computeunit);
816 sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
817
818 cpumask = cpumask_of_node(numa_node_id);
819
820
821 sub_type_hdr->flags |= CRAT_CU_FLAGS_CPU_PRESENT;
822 sub_type_hdr->proximity_domain = proximity_domain;
823 sub_type_hdr->processor_id_low = kfd_numa_node_to_apic_id(numa_node_id);
824 if (sub_type_hdr->processor_id_low == -1)
825 return -EINVAL;
826
827 sub_type_hdr->num_cpu_cores = cpumask_weight(cpumask);
828
829 return 0;
830}
831
832
833
834
835
836
837
838
839
840static int kfd_fill_mem_info_for_cpu(int numa_node_id, int *avail_size,
841 int proximity_domain,
842 struct crat_subtype_memory *sub_type_hdr)
843{
844 uint64_t mem_in_bytes = 0;
845 pg_data_t *pgdat;
846 int zone_type;
847
848 *avail_size -= sizeof(struct crat_subtype_memory);
849 if (*avail_size < 0)
850 return -ENOMEM;
851
852 memset(sub_type_hdr, 0, sizeof(struct crat_subtype_memory));
853
854
855 sub_type_hdr->type = CRAT_SUBTYPE_MEMORY_AFFINITY;
856 sub_type_hdr->length = sizeof(struct crat_subtype_memory);
857 sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
858
859
860
861
862
863
864
865 pgdat = NODE_DATA(numa_node_id);
866 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
867 mem_in_bytes += zone_managed_pages(&pgdat->node_zones[zone_type]);
868 mem_in_bytes <<= PAGE_SHIFT;
869
870 sub_type_hdr->length_low = lower_32_bits(mem_in_bytes);
871 sub_type_hdr->length_high = upper_32_bits(mem_in_bytes);
872 sub_type_hdr->proximity_domain = proximity_domain;
873
874 return 0;
875}
876
877#ifdef CONFIG_X86_64
878static int kfd_fill_iolink_info_for_cpu(int numa_node_id, int *avail_size,
879 uint32_t *num_entries,
880 struct crat_subtype_iolink *sub_type_hdr)
881{
882 int nid;
883 struct cpuinfo_x86 *c = &cpu_data(0);
884 uint8_t link_type;
885
886 if (c->x86_vendor == X86_VENDOR_AMD)
887 link_type = CRAT_IOLINK_TYPE_HYPERTRANSPORT;
888 else
889 link_type = CRAT_IOLINK_TYPE_QPI_1_1;
890
891 *num_entries = 0;
892
893
894 for_each_online_node(nid) {
895 if (nid == numa_node_id)
896 continue;
897
898 *avail_size -= sizeof(struct crat_subtype_iolink);
899 if (*avail_size < 0)
900 return -ENOMEM;
901
902 memset(sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
903
904
905 sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
906 sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
907 sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
908
909
910 sub_type_hdr->proximity_domain_from = numa_node_id;
911 sub_type_hdr->proximity_domain_to = nid;
912 sub_type_hdr->io_interface_type = link_type;
913
914 (*num_entries)++;
915 sub_type_hdr++;
916 }
917
918 return 0;
919}
920#endif
921
922
923
924
925
926
927
928static int kfd_create_vcrat_image_cpu(void *pcrat_image, size_t *size)
929{
930 struct crat_header *crat_table = (struct crat_header *)pcrat_image;
931 struct acpi_table_header *acpi_table;
932 acpi_status status;
933 struct crat_subtype_generic *sub_type_hdr;
934 int avail_size = *size;
935 int numa_node_id;
936#ifdef CONFIG_X86_64
937 uint32_t entries = 0;
938#endif
939 int ret = 0;
940
941 if (!pcrat_image || avail_size < VCRAT_SIZE_FOR_CPU)
942 return -EINVAL;
943
944
945
946
947 avail_size -= sizeof(struct crat_header);
948 if (avail_size < 0)
949 return -ENOMEM;
950
951 memset(crat_table, 0, sizeof(struct crat_header));
952 memcpy(&crat_table->signature, CRAT_SIGNATURE,
953 sizeof(crat_table->signature));
954 crat_table->length = sizeof(struct crat_header);
955
956 status = acpi_get_table("DSDT", 0, &acpi_table);
957 if (status != AE_OK)
958 pr_warn("DSDT table not found for OEM information\n");
959 else {
960 crat_table->oem_revision = acpi_table->revision;
961 memcpy(crat_table->oem_id, acpi_table->oem_id,
962 CRAT_OEMID_LENGTH);
963 memcpy(crat_table->oem_table_id, acpi_table->oem_table_id,
964 CRAT_OEMTABLEID_LENGTH);
965 }
966 crat_table->total_entries = 0;
967 crat_table->num_domains = 0;
968
969 sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
970
971 for_each_online_node(numa_node_id) {
972 if (kfd_numa_node_to_apic_id(numa_node_id) == -1)
973 continue;
974
975
976 ret = kfd_fill_cu_for_cpu(numa_node_id, &avail_size,
977 crat_table->num_domains,
978 (struct crat_subtype_computeunit *)sub_type_hdr);
979 if (ret < 0)
980 return ret;
981 crat_table->length += sub_type_hdr->length;
982 crat_table->total_entries++;
983
984 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
985 sub_type_hdr->length);
986
987
988 ret = kfd_fill_mem_info_for_cpu(numa_node_id, &avail_size,
989 crat_table->num_domains,
990 (struct crat_subtype_memory *)sub_type_hdr);
991 if (ret < 0)
992 return ret;
993 crat_table->length += sub_type_hdr->length;
994 crat_table->total_entries++;
995
996 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
997 sub_type_hdr->length);
998
999
1000#ifdef CONFIG_X86_64
1001 ret = kfd_fill_iolink_info_for_cpu(numa_node_id, &avail_size,
1002 &entries,
1003 (struct crat_subtype_iolink *)sub_type_hdr);
1004 if (ret < 0)
1005 return ret;
1006 crat_table->length += (sub_type_hdr->length * entries);
1007 crat_table->total_entries += entries;
1008
1009 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1010 sub_type_hdr->length * entries);
1011#else
1012 pr_info("IO link not available for non x86 platforms\n");
1013#endif
1014
1015 crat_table->num_domains++;
1016 }
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026 *size = crat_table->length;
1027 pr_info("Virtual CRAT table created for CPU\n");
1028
1029 return 0;
1030}
1031
1032static int kfd_fill_gpu_memory_affinity(int *avail_size,
1033 struct kfd_dev *kdev, uint8_t type, uint64_t size,
1034 struct crat_subtype_memory *sub_type_hdr,
1035 uint32_t proximity_domain,
1036 const struct kfd_local_mem_info *local_mem_info)
1037{
1038 *avail_size -= sizeof(struct crat_subtype_memory);
1039 if (*avail_size < 0)
1040 return -ENOMEM;
1041
1042 memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_memory));
1043 sub_type_hdr->type = CRAT_SUBTYPE_MEMORY_AFFINITY;
1044 sub_type_hdr->length = sizeof(struct crat_subtype_memory);
1045 sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED;
1046
1047 sub_type_hdr->proximity_domain = proximity_domain;
1048
1049 pr_debug("Fill gpu memory affinity - type 0x%x size 0x%llx\n",
1050 type, size);
1051
1052 sub_type_hdr->length_low = lower_32_bits(size);
1053 sub_type_hdr->length_high = upper_32_bits(size);
1054
1055 sub_type_hdr->width = local_mem_info->vram_width;
1056 sub_type_hdr->visibility_type = type;
1057
1058 return 0;
1059}
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070static int kfd_fill_gpu_direct_io_link_to_cpu(int *avail_size,
1071 struct kfd_dev *kdev,
1072 struct crat_subtype_iolink *sub_type_hdr,
1073 uint32_t proximity_domain)
1074{
1075 *avail_size -= sizeof(struct crat_subtype_iolink);
1076 if (*avail_size < 0)
1077 return -ENOMEM;
1078
1079 memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
1080
1081
1082 sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
1083 sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
1084 sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED;
1085 if (kfd_dev_is_large_bar(kdev))
1086 sub_type_hdr->flags |= CRAT_IOLINK_FLAGS_BI_DIRECTIONAL;
1087
1088
1089
1090
1091 sub_type_hdr->io_interface_type = CRAT_IOLINK_TYPE_PCIEXPRESS;
1092 sub_type_hdr->proximity_domain_from = proximity_domain;
1093#ifdef CONFIG_NUMA
1094 if (kdev->pdev->dev.numa_node == NUMA_NO_NODE)
1095 sub_type_hdr->proximity_domain_to = 0;
1096 else
1097 sub_type_hdr->proximity_domain_to = kdev->pdev->dev.numa_node;
1098#else
1099 sub_type_hdr->proximity_domain_to = 0;
1100#endif
1101 return 0;
1102}
1103
1104static int kfd_fill_gpu_xgmi_link_to_gpu(int *avail_size,
1105 struct kfd_dev *kdev,
1106 struct kfd_dev *peer_kdev,
1107 struct crat_subtype_iolink *sub_type_hdr,
1108 uint32_t proximity_domain_from,
1109 uint32_t proximity_domain_to)
1110{
1111 *avail_size -= sizeof(struct crat_subtype_iolink);
1112 if (*avail_size < 0)
1113 return -ENOMEM;
1114
1115 memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
1116
1117 sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
1118 sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
1119 sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED |
1120 CRAT_IOLINK_FLAGS_BI_DIRECTIONAL;
1121
1122 sub_type_hdr->io_interface_type = CRAT_IOLINK_TYPE_XGMI;
1123 sub_type_hdr->proximity_domain_from = proximity_domain_from;
1124 sub_type_hdr->proximity_domain_to = proximity_domain_to;
1125 sub_type_hdr->num_hops_xgmi =
1126 amdgpu_amdkfd_get_xgmi_hops_count(kdev->kgd, peer_kdev->kgd);
1127 return 0;
1128}
1129
1130
1131
1132
1133
1134
1135
1136static int kfd_create_vcrat_image_gpu(void *pcrat_image,
1137 size_t *size, struct kfd_dev *kdev,
1138 uint32_t proximity_domain)
1139{
1140 struct crat_header *crat_table = (struct crat_header *)pcrat_image;
1141 struct crat_subtype_generic *sub_type_hdr;
1142 struct kfd_local_mem_info local_mem_info;
1143 struct kfd_topology_device *peer_dev;
1144 struct crat_subtype_computeunit *cu;
1145 struct kfd_cu_info cu_info;
1146 int avail_size = *size;
1147 uint32_t total_num_of_cu;
1148 int num_of_cache_entries = 0;
1149 int cache_mem_filled = 0;
1150 uint32_t nid = 0;
1151 int ret = 0;
1152
1153 if (!pcrat_image || avail_size < VCRAT_SIZE_FOR_GPU)
1154 return -EINVAL;
1155
1156
1157
1158
1159 avail_size -= sizeof(struct crat_header);
1160 if (avail_size < 0)
1161 return -ENOMEM;
1162
1163 memset(crat_table, 0, sizeof(struct crat_header));
1164
1165 memcpy(&crat_table->signature, CRAT_SIGNATURE,
1166 sizeof(crat_table->signature));
1167
1168 crat_table->length = sizeof(struct crat_header);
1169 crat_table->num_domains = 1;
1170 crat_table->total_entries = 0;
1171
1172
1173
1174
1175 avail_size -= sizeof(struct crat_subtype_computeunit);
1176 if (avail_size < 0)
1177 return -ENOMEM;
1178
1179 sub_type_hdr = (struct crat_subtype_generic *)(crat_table + 1);
1180 memset(sub_type_hdr, 0, sizeof(struct crat_subtype_computeunit));
1181
1182 sub_type_hdr->type = CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY;
1183 sub_type_hdr->length = sizeof(struct crat_subtype_computeunit);
1184 sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
1185
1186
1187 cu = (struct crat_subtype_computeunit *)sub_type_hdr;
1188 cu->flags |= CRAT_CU_FLAGS_GPU_PRESENT;
1189 cu->proximity_domain = proximity_domain;
1190
1191 amdgpu_amdkfd_get_cu_info(kdev->kgd, &cu_info);
1192 cu->num_simd_per_cu = cu_info.simd_per_cu;
1193 cu->num_simd_cores = cu_info.simd_per_cu * cu_info.cu_active_number;
1194 cu->max_waves_simd = cu_info.max_waves_per_simd;
1195
1196 cu->wave_front_size = cu_info.wave_front_size;
1197 cu->array_count = cu_info.num_shader_arrays_per_engine *
1198 cu_info.num_shader_engines;
1199 total_num_of_cu = (cu->array_count * cu_info.num_cu_per_sh);
1200 cu->processor_id_low = get_and_inc_gpu_processor_id(total_num_of_cu);
1201 cu->num_cu_per_array = cu_info.num_cu_per_sh;
1202 cu->max_slots_scatch_cu = cu_info.max_scratch_slots_per_cu;
1203 cu->num_banks = cu_info.num_shader_engines;
1204 cu->lds_size_in_kb = cu_info.lds_size;
1205
1206 cu->hsa_capability = 0;
1207
1208
1209
1210
1211 if (!kfd_iommu_check_device(kdev))
1212 cu->hsa_capability |= CRAT_CU_FLAGS_IOMMU_PRESENT;
1213
1214 crat_table->length += sub_type_hdr->length;
1215 crat_table->total_entries++;
1216
1217
1218
1219
1220
1221
1222 amdgpu_amdkfd_get_local_mem_info(kdev->kgd, &local_mem_info);
1223 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1224 sub_type_hdr->length);
1225
1226 if (debug_largebar)
1227 local_mem_info.local_mem_size_private = 0;
1228
1229 if (local_mem_info.local_mem_size_private == 0)
1230 ret = kfd_fill_gpu_memory_affinity(&avail_size,
1231 kdev, HSA_MEM_HEAP_TYPE_FB_PUBLIC,
1232 local_mem_info.local_mem_size_public,
1233 (struct crat_subtype_memory *)sub_type_hdr,
1234 proximity_domain,
1235 &local_mem_info);
1236 else
1237 ret = kfd_fill_gpu_memory_affinity(&avail_size,
1238 kdev, HSA_MEM_HEAP_TYPE_FB_PRIVATE,
1239 local_mem_info.local_mem_size_public +
1240 local_mem_info.local_mem_size_private,
1241 (struct crat_subtype_memory *)sub_type_hdr,
1242 proximity_domain,
1243 &local_mem_info);
1244 if (ret < 0)
1245 return ret;
1246
1247 crat_table->length += sizeof(struct crat_subtype_memory);
1248 crat_table->total_entries++;
1249
1250
1251
1252
1253 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1254 sub_type_hdr->length);
1255 ret = kfd_fill_gpu_cache_info(kdev, cu->processor_id_low,
1256 avail_size,
1257 &cu_info,
1258 (struct crat_subtype_cache *)sub_type_hdr,
1259 &cache_mem_filled,
1260 &num_of_cache_entries);
1261
1262 if (ret < 0)
1263 return ret;
1264
1265 crat_table->length += cache_mem_filled;
1266 crat_table->total_entries += num_of_cache_entries;
1267 avail_size -= cache_mem_filled;
1268
1269
1270
1271
1272
1273 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1274 cache_mem_filled);
1275 ret = kfd_fill_gpu_direct_io_link_to_cpu(&avail_size, kdev,
1276 (struct crat_subtype_iolink *)sub_type_hdr, proximity_domain);
1277
1278 if (ret < 0)
1279 return ret;
1280
1281 crat_table->length += sub_type_hdr->length;
1282 crat_table->total_entries++;
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293 if (kdev->hive_id) {
1294 for (nid = 0; nid < proximity_domain; ++nid) {
1295 peer_dev = kfd_topology_device_by_proximity_domain(nid);
1296 if (!peer_dev->gpu)
1297 continue;
1298 if (peer_dev->gpu->hive_id != kdev->hive_id)
1299 continue;
1300 sub_type_hdr = (typeof(sub_type_hdr))(
1301 (char *)sub_type_hdr +
1302 sizeof(struct crat_subtype_iolink));
1303 ret = kfd_fill_gpu_xgmi_link_to_gpu(
1304 &avail_size, kdev, peer_dev->gpu,
1305 (struct crat_subtype_iolink *)sub_type_hdr,
1306 proximity_domain, nid);
1307 if (ret < 0)
1308 return ret;
1309 crat_table->length += sub_type_hdr->length;
1310 crat_table->total_entries++;
1311 }
1312 }
1313 *size = crat_table->length;
1314 pr_info("Virtual CRAT table created for GPU\n");
1315
1316 return ret;
1317}
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336int kfd_create_crat_image_virtual(void **crat_image, size_t *size,
1337 int flags, struct kfd_dev *kdev,
1338 uint32_t proximity_domain)
1339{
1340 void *pcrat_image = NULL;
1341 int ret = 0;
1342
1343 if (!crat_image)
1344 return -EINVAL;
1345
1346 *crat_image = NULL;
1347
1348
1349
1350
1351
1352
1353 switch (flags) {
1354 case COMPUTE_UNIT_CPU:
1355 pcrat_image = kmalloc(VCRAT_SIZE_FOR_CPU, GFP_KERNEL);
1356 if (!pcrat_image)
1357 return -ENOMEM;
1358 *size = VCRAT_SIZE_FOR_CPU;
1359 ret = kfd_create_vcrat_image_cpu(pcrat_image, size);
1360 break;
1361 case COMPUTE_UNIT_GPU:
1362 if (!kdev)
1363 return -EINVAL;
1364 pcrat_image = kmalloc(VCRAT_SIZE_FOR_GPU, GFP_KERNEL);
1365 if (!pcrat_image)
1366 return -ENOMEM;
1367 *size = VCRAT_SIZE_FOR_GPU;
1368 ret = kfd_create_vcrat_image_gpu(pcrat_image, size, kdev,
1369 proximity_domain);
1370 break;
1371 case (COMPUTE_UNIT_CPU | COMPUTE_UNIT_GPU):
1372
1373 ret = -EINVAL;
1374 pr_err("VCRAT not implemented for APU\n");
1375 break;
1376 default:
1377 ret = -EINVAL;
1378 }
1379
1380 if (!ret)
1381 *crat_image = pcrat_image;
1382 else
1383 kfree(pcrat_image);
1384
1385 return ret;
1386}
1387
1388
1389
1390
1391
1392
1393
1394void kfd_destroy_crat_image(void *crat_image)
1395{
1396 kfree(crat_image);
1397}
1398