1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2015-2022 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <linux/pci.h>
25 #include <linux/acpi.h>
28 #include "kfd_topology.h"
29 #include "kfd_iommu.h"
31 #include "amdgpu_amdkfd.h"
33 /* GPU Processor ID base for dGPUs for which VCRAT needs to be created.
34 * GPU processor ID are expressed with Bit[31]=1.
35 * The base is set to 0x8000_0000 + 0x1000 to avoid collision with GPU IDs
38 static uint32_t gpu_processor_id_low = 0x80001000;
40 /* Return the next available gpu_processor_id and increment it for next GPU
41 * @total_cu_count - Total CUs present in the GPU including ones
44 static inline unsigned int get_and_inc_gpu_processor_id(
45 unsigned int total_cu_count)
47 int current_id = gpu_processor_id_low;
49 gpu_processor_id_low += total_cu_count;
53 /* Static table to describe GPU Cache information */
54 struct kfd_gpu_cache_info {
58 /* Indicates how many Compute Units share this cache
59 * within a SA. Value = 1 indicates the cache is not shared
61 uint32_t num_cu_shared;
64 static struct kfd_gpu_cache_info kaveri_cache_info[] = {
66 /* TCP L1 Cache per CU */
69 .flags = (CRAT_CACHE_FLAGS_ENABLED |
70 CRAT_CACHE_FLAGS_DATA_CACHE |
71 CRAT_CACHE_FLAGS_SIMD_CACHE),
75 /* Scalar L1 Instruction Cache (in SQC module) per bank */
78 .flags = (CRAT_CACHE_FLAGS_ENABLED |
79 CRAT_CACHE_FLAGS_INST_CACHE |
80 CRAT_CACHE_FLAGS_SIMD_CACHE),
84 /* Scalar L1 Data Cache (in SQC module) per bank */
87 .flags = (CRAT_CACHE_FLAGS_ENABLED |
88 CRAT_CACHE_FLAGS_DATA_CACHE |
89 CRAT_CACHE_FLAGS_SIMD_CACHE),
93 /* TODO: Add L2 Cache information */
97 static struct kfd_gpu_cache_info carrizo_cache_info[] = {
99 /* TCP L1 Cache per CU */
102 .flags = (CRAT_CACHE_FLAGS_ENABLED |
103 CRAT_CACHE_FLAGS_DATA_CACHE |
104 CRAT_CACHE_FLAGS_SIMD_CACHE),
108 /* Scalar L1 Instruction Cache (in SQC module) per bank */
111 .flags = (CRAT_CACHE_FLAGS_ENABLED |
112 CRAT_CACHE_FLAGS_INST_CACHE |
113 CRAT_CACHE_FLAGS_SIMD_CACHE),
117 /* Scalar L1 Data Cache (in SQC module) per bank. */
120 .flags = (CRAT_CACHE_FLAGS_ENABLED |
121 CRAT_CACHE_FLAGS_DATA_CACHE |
122 CRAT_CACHE_FLAGS_SIMD_CACHE),
126 /* TODO: Add L2 Cache information */
129 #define hawaii_cache_info kaveri_cache_info
130 #define tonga_cache_info carrizo_cache_info
131 #define fiji_cache_info carrizo_cache_info
132 #define polaris10_cache_info carrizo_cache_info
133 #define polaris11_cache_info carrizo_cache_info
134 #define polaris12_cache_info carrizo_cache_info
135 #define vegam_cache_info carrizo_cache_info
137 /* NOTE: L1 cache information has been updated and L2/L3
138 * cache information has been added for Vega10 and
139 * newer ASICs. The unit for cache_size is KiB.
140 * In future, check & update cache details
141 * for every new ASIC is required.
144 static struct kfd_gpu_cache_info vega10_cache_info[] = {
146 /* TCP L1 Cache per CU */
149 .flags = (CRAT_CACHE_FLAGS_ENABLED |
150 CRAT_CACHE_FLAGS_DATA_CACHE |
151 CRAT_CACHE_FLAGS_SIMD_CACHE),
155 /* Scalar L1 Instruction Cache per SQC */
158 .flags = (CRAT_CACHE_FLAGS_ENABLED |
159 CRAT_CACHE_FLAGS_INST_CACHE |
160 CRAT_CACHE_FLAGS_SIMD_CACHE),
164 /* Scalar L1 Data Cache per SQC */
167 .flags = (CRAT_CACHE_FLAGS_ENABLED |
168 CRAT_CACHE_FLAGS_DATA_CACHE |
169 CRAT_CACHE_FLAGS_SIMD_CACHE),
173 /* L2 Data Cache per GPU (Total Tex Cache) */
176 .flags = (CRAT_CACHE_FLAGS_ENABLED |
177 CRAT_CACHE_FLAGS_DATA_CACHE |
178 CRAT_CACHE_FLAGS_SIMD_CACHE),
183 static struct kfd_gpu_cache_info raven_cache_info[] = {
185 /* TCP L1 Cache per CU */
188 .flags = (CRAT_CACHE_FLAGS_ENABLED |
189 CRAT_CACHE_FLAGS_DATA_CACHE |
190 CRAT_CACHE_FLAGS_SIMD_CACHE),
194 /* Scalar L1 Instruction Cache per SQC */
197 .flags = (CRAT_CACHE_FLAGS_ENABLED |
198 CRAT_CACHE_FLAGS_INST_CACHE |
199 CRAT_CACHE_FLAGS_SIMD_CACHE),
203 /* Scalar L1 Data Cache per SQC */
206 .flags = (CRAT_CACHE_FLAGS_ENABLED |
207 CRAT_CACHE_FLAGS_DATA_CACHE |
208 CRAT_CACHE_FLAGS_SIMD_CACHE),
212 /* L2 Data Cache per GPU (Total Tex Cache) */
215 .flags = (CRAT_CACHE_FLAGS_ENABLED |
216 CRAT_CACHE_FLAGS_DATA_CACHE |
217 CRAT_CACHE_FLAGS_SIMD_CACHE),
222 static struct kfd_gpu_cache_info renoir_cache_info[] = {
224 /* TCP L1 Cache per CU */
227 .flags = (CRAT_CACHE_FLAGS_ENABLED |
228 CRAT_CACHE_FLAGS_DATA_CACHE |
229 CRAT_CACHE_FLAGS_SIMD_CACHE),
233 /* Scalar L1 Instruction Cache per SQC */
236 .flags = (CRAT_CACHE_FLAGS_ENABLED |
237 CRAT_CACHE_FLAGS_INST_CACHE |
238 CRAT_CACHE_FLAGS_SIMD_CACHE),
242 /* Scalar L1 Data Cache per SQC */
245 .flags = (CRAT_CACHE_FLAGS_ENABLED |
246 CRAT_CACHE_FLAGS_DATA_CACHE |
247 CRAT_CACHE_FLAGS_SIMD_CACHE),
251 /* L2 Data Cache per GPU (Total Tex Cache) */
254 .flags = (CRAT_CACHE_FLAGS_ENABLED |
255 CRAT_CACHE_FLAGS_DATA_CACHE |
256 CRAT_CACHE_FLAGS_SIMD_CACHE),
261 static struct kfd_gpu_cache_info vega12_cache_info[] = {
263 /* TCP L1 Cache per CU */
266 .flags = (CRAT_CACHE_FLAGS_ENABLED |
267 CRAT_CACHE_FLAGS_DATA_CACHE |
268 CRAT_CACHE_FLAGS_SIMD_CACHE),
272 /* Scalar L1 Instruction Cache per SQC */
275 .flags = (CRAT_CACHE_FLAGS_ENABLED |
276 CRAT_CACHE_FLAGS_INST_CACHE |
277 CRAT_CACHE_FLAGS_SIMD_CACHE),
281 /* Scalar L1 Data Cache per SQC */
284 .flags = (CRAT_CACHE_FLAGS_ENABLED |
285 CRAT_CACHE_FLAGS_DATA_CACHE |
286 CRAT_CACHE_FLAGS_SIMD_CACHE),
290 /* L2 Data Cache per GPU (Total Tex Cache) */
293 .flags = (CRAT_CACHE_FLAGS_ENABLED |
294 CRAT_CACHE_FLAGS_DATA_CACHE |
295 CRAT_CACHE_FLAGS_SIMD_CACHE),
300 static struct kfd_gpu_cache_info vega20_cache_info[] = {
302 /* TCP L1 Cache per CU */
305 .flags = (CRAT_CACHE_FLAGS_ENABLED |
306 CRAT_CACHE_FLAGS_DATA_CACHE |
307 CRAT_CACHE_FLAGS_SIMD_CACHE),
311 /* Scalar L1 Instruction Cache per SQC */
314 .flags = (CRAT_CACHE_FLAGS_ENABLED |
315 CRAT_CACHE_FLAGS_INST_CACHE |
316 CRAT_CACHE_FLAGS_SIMD_CACHE),
320 /* Scalar L1 Data Cache per SQC */
323 .flags = (CRAT_CACHE_FLAGS_ENABLED |
324 CRAT_CACHE_FLAGS_DATA_CACHE |
325 CRAT_CACHE_FLAGS_SIMD_CACHE),
329 /* L2 Data Cache per GPU (Total Tex Cache) */
332 .flags = (CRAT_CACHE_FLAGS_ENABLED |
333 CRAT_CACHE_FLAGS_DATA_CACHE |
334 CRAT_CACHE_FLAGS_SIMD_CACHE),
339 static struct kfd_gpu_cache_info aldebaran_cache_info[] = {
341 /* TCP L1 Cache per CU */
344 .flags = (CRAT_CACHE_FLAGS_ENABLED |
345 CRAT_CACHE_FLAGS_DATA_CACHE |
346 CRAT_CACHE_FLAGS_SIMD_CACHE),
350 /* Scalar L1 Instruction Cache per SQC */
353 .flags = (CRAT_CACHE_FLAGS_ENABLED |
354 CRAT_CACHE_FLAGS_INST_CACHE |
355 CRAT_CACHE_FLAGS_SIMD_CACHE),
359 /* Scalar L1 Data Cache per SQC */
362 .flags = (CRAT_CACHE_FLAGS_ENABLED |
363 CRAT_CACHE_FLAGS_DATA_CACHE |
364 CRAT_CACHE_FLAGS_SIMD_CACHE),
368 /* L2 Data Cache per GPU (Total Tex Cache) */
371 .flags = (CRAT_CACHE_FLAGS_ENABLED |
372 CRAT_CACHE_FLAGS_DATA_CACHE |
373 CRAT_CACHE_FLAGS_SIMD_CACHE),
378 static struct kfd_gpu_cache_info navi10_cache_info[] = {
380 /* TCP L1 Cache per CU */
383 .flags = (CRAT_CACHE_FLAGS_ENABLED |
384 CRAT_CACHE_FLAGS_DATA_CACHE |
385 CRAT_CACHE_FLAGS_SIMD_CACHE),
389 /* Scalar L1 Instruction Cache per SQC */
392 .flags = (CRAT_CACHE_FLAGS_ENABLED |
393 CRAT_CACHE_FLAGS_INST_CACHE |
394 CRAT_CACHE_FLAGS_SIMD_CACHE),
398 /* Scalar L1 Data Cache per SQC */
401 .flags = (CRAT_CACHE_FLAGS_ENABLED |
402 CRAT_CACHE_FLAGS_DATA_CACHE |
403 CRAT_CACHE_FLAGS_SIMD_CACHE),
407 /* GL1 Data Cache per SA */
410 .flags = (CRAT_CACHE_FLAGS_ENABLED |
411 CRAT_CACHE_FLAGS_DATA_CACHE |
412 CRAT_CACHE_FLAGS_SIMD_CACHE),
416 /* L2 Data Cache per GPU (Total Tex Cache) */
419 .flags = (CRAT_CACHE_FLAGS_ENABLED |
420 CRAT_CACHE_FLAGS_DATA_CACHE |
421 CRAT_CACHE_FLAGS_SIMD_CACHE),
426 static struct kfd_gpu_cache_info vangogh_cache_info[] = {
428 /* TCP L1 Cache per CU */
431 .flags = (CRAT_CACHE_FLAGS_ENABLED |
432 CRAT_CACHE_FLAGS_DATA_CACHE |
433 CRAT_CACHE_FLAGS_SIMD_CACHE),
437 /* Scalar L1 Instruction Cache per SQC */
440 .flags = (CRAT_CACHE_FLAGS_ENABLED |
441 CRAT_CACHE_FLAGS_INST_CACHE |
442 CRAT_CACHE_FLAGS_SIMD_CACHE),
446 /* Scalar L1 Data Cache per SQC */
449 .flags = (CRAT_CACHE_FLAGS_ENABLED |
450 CRAT_CACHE_FLAGS_DATA_CACHE |
451 CRAT_CACHE_FLAGS_SIMD_CACHE),
455 /* GL1 Data Cache per SA */
458 .flags = (CRAT_CACHE_FLAGS_ENABLED |
459 CRAT_CACHE_FLAGS_DATA_CACHE |
460 CRAT_CACHE_FLAGS_SIMD_CACHE),
464 /* L2 Data Cache per GPU (Total Tex Cache) */
467 .flags = (CRAT_CACHE_FLAGS_ENABLED |
468 CRAT_CACHE_FLAGS_DATA_CACHE |
469 CRAT_CACHE_FLAGS_SIMD_CACHE),
474 static struct kfd_gpu_cache_info navi14_cache_info[] = {
476 /* TCP L1 Cache per CU */
479 .flags = (CRAT_CACHE_FLAGS_ENABLED |
480 CRAT_CACHE_FLAGS_DATA_CACHE |
481 CRAT_CACHE_FLAGS_SIMD_CACHE),
485 /* Scalar L1 Instruction Cache per SQC */
488 .flags = (CRAT_CACHE_FLAGS_ENABLED |
489 CRAT_CACHE_FLAGS_INST_CACHE |
490 CRAT_CACHE_FLAGS_SIMD_CACHE),
494 /* Scalar L1 Data Cache per SQC */
497 .flags = (CRAT_CACHE_FLAGS_ENABLED |
498 CRAT_CACHE_FLAGS_DATA_CACHE |
499 CRAT_CACHE_FLAGS_SIMD_CACHE),
503 /* GL1 Data Cache per SA */
506 .flags = (CRAT_CACHE_FLAGS_ENABLED |
507 CRAT_CACHE_FLAGS_DATA_CACHE |
508 CRAT_CACHE_FLAGS_SIMD_CACHE),
512 /* L2 Data Cache per GPU (Total Tex Cache) */
515 .flags = (CRAT_CACHE_FLAGS_ENABLED |
516 CRAT_CACHE_FLAGS_DATA_CACHE |
517 CRAT_CACHE_FLAGS_SIMD_CACHE),
522 static struct kfd_gpu_cache_info sienna_cichlid_cache_info[] = {
524 /* TCP L1 Cache per CU */
527 .flags = (CRAT_CACHE_FLAGS_ENABLED |
528 CRAT_CACHE_FLAGS_DATA_CACHE |
529 CRAT_CACHE_FLAGS_SIMD_CACHE),
533 /* Scalar L1 Instruction Cache per SQC */
536 .flags = (CRAT_CACHE_FLAGS_ENABLED |
537 CRAT_CACHE_FLAGS_INST_CACHE |
538 CRAT_CACHE_FLAGS_SIMD_CACHE),
542 /* Scalar L1 Data Cache per SQC */
545 .flags = (CRAT_CACHE_FLAGS_ENABLED |
546 CRAT_CACHE_FLAGS_DATA_CACHE |
547 CRAT_CACHE_FLAGS_SIMD_CACHE),
551 /* GL1 Data Cache per SA */
554 .flags = (CRAT_CACHE_FLAGS_ENABLED |
555 CRAT_CACHE_FLAGS_DATA_CACHE |
556 CRAT_CACHE_FLAGS_SIMD_CACHE),
560 /* L2 Data Cache per GPU (Total Tex Cache) */
563 .flags = (CRAT_CACHE_FLAGS_ENABLED |
564 CRAT_CACHE_FLAGS_DATA_CACHE |
565 CRAT_CACHE_FLAGS_SIMD_CACHE),
569 /* L3 Data Cache per GPU */
570 .cache_size = 128*1024,
572 .flags = (CRAT_CACHE_FLAGS_ENABLED |
573 CRAT_CACHE_FLAGS_DATA_CACHE |
574 CRAT_CACHE_FLAGS_SIMD_CACHE),
579 static struct kfd_gpu_cache_info navy_flounder_cache_info[] = {
581 /* TCP L1 Cache per CU */
584 .flags = (CRAT_CACHE_FLAGS_ENABLED |
585 CRAT_CACHE_FLAGS_DATA_CACHE |
586 CRAT_CACHE_FLAGS_SIMD_CACHE),
590 /* Scalar L1 Instruction Cache per SQC */
593 .flags = (CRAT_CACHE_FLAGS_ENABLED |
594 CRAT_CACHE_FLAGS_INST_CACHE |
595 CRAT_CACHE_FLAGS_SIMD_CACHE),
599 /* Scalar L1 Data Cache per SQC */
602 .flags = (CRAT_CACHE_FLAGS_ENABLED |
603 CRAT_CACHE_FLAGS_DATA_CACHE |
604 CRAT_CACHE_FLAGS_SIMD_CACHE),
608 /* GL1 Data Cache per SA */
611 .flags = (CRAT_CACHE_FLAGS_ENABLED |
612 CRAT_CACHE_FLAGS_DATA_CACHE |
613 CRAT_CACHE_FLAGS_SIMD_CACHE),
617 /* L2 Data Cache per GPU (Total Tex Cache) */
620 .flags = (CRAT_CACHE_FLAGS_ENABLED |
621 CRAT_CACHE_FLAGS_DATA_CACHE |
622 CRAT_CACHE_FLAGS_SIMD_CACHE),
626 /* L3 Data Cache per GPU */
627 .cache_size = 96*1024,
629 .flags = (CRAT_CACHE_FLAGS_ENABLED |
630 CRAT_CACHE_FLAGS_DATA_CACHE |
631 CRAT_CACHE_FLAGS_SIMD_CACHE),
636 static struct kfd_gpu_cache_info dimgrey_cavefish_cache_info[] = {
638 /* TCP L1 Cache per CU */
641 .flags = (CRAT_CACHE_FLAGS_ENABLED |
642 CRAT_CACHE_FLAGS_DATA_CACHE |
643 CRAT_CACHE_FLAGS_SIMD_CACHE),
647 /* Scalar L1 Instruction Cache per SQC */
650 .flags = (CRAT_CACHE_FLAGS_ENABLED |
651 CRAT_CACHE_FLAGS_INST_CACHE |
652 CRAT_CACHE_FLAGS_SIMD_CACHE),
656 /* Scalar L1 Data Cache per SQC */
659 .flags = (CRAT_CACHE_FLAGS_ENABLED |
660 CRAT_CACHE_FLAGS_DATA_CACHE |
661 CRAT_CACHE_FLAGS_SIMD_CACHE),
665 /* GL1 Data Cache per SA */
668 .flags = (CRAT_CACHE_FLAGS_ENABLED |
669 CRAT_CACHE_FLAGS_DATA_CACHE |
670 CRAT_CACHE_FLAGS_SIMD_CACHE),
674 /* L2 Data Cache per GPU (Total Tex Cache) */
677 .flags = (CRAT_CACHE_FLAGS_ENABLED |
678 CRAT_CACHE_FLAGS_DATA_CACHE |
679 CRAT_CACHE_FLAGS_SIMD_CACHE),
683 /* L3 Data Cache per GPU */
684 .cache_size = 32*1024,
686 .flags = (CRAT_CACHE_FLAGS_ENABLED |
687 CRAT_CACHE_FLAGS_DATA_CACHE |
688 CRAT_CACHE_FLAGS_SIMD_CACHE),
693 static struct kfd_gpu_cache_info beige_goby_cache_info[] = {
695 /* TCP L1 Cache per CU */
698 .flags = (CRAT_CACHE_FLAGS_ENABLED |
699 CRAT_CACHE_FLAGS_DATA_CACHE |
700 CRAT_CACHE_FLAGS_SIMD_CACHE),
704 /* Scalar L1 Instruction Cache per SQC */
707 .flags = (CRAT_CACHE_FLAGS_ENABLED |
708 CRAT_CACHE_FLAGS_INST_CACHE |
709 CRAT_CACHE_FLAGS_SIMD_CACHE),
713 /* Scalar L1 Data Cache per SQC */
716 .flags = (CRAT_CACHE_FLAGS_ENABLED |
717 CRAT_CACHE_FLAGS_DATA_CACHE |
718 CRAT_CACHE_FLAGS_SIMD_CACHE),
722 /* GL1 Data Cache per SA */
725 .flags = (CRAT_CACHE_FLAGS_ENABLED |
726 CRAT_CACHE_FLAGS_DATA_CACHE |
727 CRAT_CACHE_FLAGS_SIMD_CACHE),
731 /* L2 Data Cache per GPU (Total Tex Cache) */
734 .flags = (CRAT_CACHE_FLAGS_ENABLED |
735 CRAT_CACHE_FLAGS_DATA_CACHE |
736 CRAT_CACHE_FLAGS_SIMD_CACHE),
740 /* L3 Data Cache per GPU */
741 .cache_size = 16*1024,
743 .flags = (CRAT_CACHE_FLAGS_ENABLED |
744 CRAT_CACHE_FLAGS_DATA_CACHE |
745 CRAT_CACHE_FLAGS_SIMD_CACHE),
750 static struct kfd_gpu_cache_info yellow_carp_cache_info[] = {
752 /* TCP L1 Cache per CU */
755 .flags = (CRAT_CACHE_FLAGS_ENABLED |
756 CRAT_CACHE_FLAGS_DATA_CACHE |
757 CRAT_CACHE_FLAGS_SIMD_CACHE),
761 /* Scalar L1 Instruction Cache per SQC */
764 .flags = (CRAT_CACHE_FLAGS_ENABLED |
765 CRAT_CACHE_FLAGS_INST_CACHE |
766 CRAT_CACHE_FLAGS_SIMD_CACHE),
770 /* Scalar L1 Data Cache per SQC */
773 .flags = (CRAT_CACHE_FLAGS_ENABLED |
774 CRAT_CACHE_FLAGS_DATA_CACHE |
775 CRAT_CACHE_FLAGS_SIMD_CACHE),
779 /* GL1 Data Cache per SA */
782 .flags = (CRAT_CACHE_FLAGS_ENABLED |
783 CRAT_CACHE_FLAGS_DATA_CACHE |
784 CRAT_CACHE_FLAGS_SIMD_CACHE),
788 /* L2 Data Cache per GPU (Total Tex Cache) */
791 .flags = (CRAT_CACHE_FLAGS_ENABLED |
792 CRAT_CACHE_FLAGS_DATA_CACHE |
793 CRAT_CACHE_FLAGS_SIMD_CACHE),
798 static void kfd_populated_cu_info_cpu(struct kfd_topology_device *dev,
799 struct crat_subtype_computeunit *cu)
801 dev->node_props.cpu_cores_count = cu->num_cpu_cores;
802 dev->node_props.cpu_core_id_base = cu->processor_id_low;
803 if (cu->hsa_capability & CRAT_CU_FLAGS_IOMMU_PRESENT)
804 dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
806 pr_debug("CU CPU: cores=%d id_base=%d\n", cu->num_cpu_cores,
807 cu->processor_id_low);
810 static void kfd_populated_cu_info_gpu(struct kfd_topology_device *dev,
811 struct crat_subtype_computeunit *cu)
813 dev->node_props.simd_id_base = cu->processor_id_low;
814 dev->node_props.simd_count = cu->num_simd_cores;
815 dev->node_props.lds_size_in_kb = cu->lds_size_in_kb;
816 dev->node_props.max_waves_per_simd = cu->max_waves_simd;
817 dev->node_props.wave_front_size = cu->wave_front_size;
818 dev->node_props.array_count = cu->array_count;
819 dev->node_props.cu_per_simd_array = cu->num_cu_per_array;
820 dev->node_props.simd_per_cu = cu->num_simd_per_cu;
821 dev->node_props.max_slots_scratch_cu = cu->max_slots_scatch_cu;
822 if (cu->hsa_capability & CRAT_CU_FLAGS_HOT_PLUGGABLE)
823 dev->node_props.capability |= HSA_CAP_HOT_PLUGGABLE;
824 pr_debug("CU GPU: id_base=%d\n", cu->processor_id_low);
827 /* kfd_parse_subtype_cu - parse compute unit subtypes and attach it to correct
828 * topology device present in the device_list
830 static int kfd_parse_subtype_cu(struct crat_subtype_computeunit *cu,
831 struct list_head *device_list)
833 struct kfd_topology_device *dev;
835 pr_debug("Found CU entry in CRAT table with proximity_domain=%d caps=%x\n",
836 cu->proximity_domain, cu->hsa_capability);
837 list_for_each_entry(dev, device_list, list) {
838 if (cu->proximity_domain == dev->proximity_domain) {
839 if (cu->flags & CRAT_CU_FLAGS_CPU_PRESENT)
840 kfd_populated_cu_info_cpu(dev, cu);
842 if (cu->flags & CRAT_CU_FLAGS_GPU_PRESENT)
843 kfd_populated_cu_info_gpu(dev, cu);
851 static struct kfd_mem_properties *
852 find_subtype_mem(uint32_t heap_type, uint32_t flags, uint32_t width,
853 struct kfd_topology_device *dev)
855 struct kfd_mem_properties *props;
857 list_for_each_entry(props, &dev->mem_props, list) {
858 if (props->heap_type == heap_type
859 && props->flags == flags
860 && props->width == width)
866 /* kfd_parse_subtype_mem - parse memory subtypes and attach it to correct
867 * topology device present in the device_list
869 static int kfd_parse_subtype_mem(struct crat_subtype_memory *mem,
870 struct list_head *device_list)
872 struct kfd_mem_properties *props;
873 struct kfd_topology_device *dev;
875 uint64_t size_in_bytes;
879 pr_debug("Found memory entry in CRAT table with proximity_domain=%d\n",
880 mem->proximity_domain);
881 list_for_each_entry(dev, device_list, list) {
882 if (mem->proximity_domain == dev->proximity_domain) {
883 /* We're on GPU node */
884 if (dev->node_props.cpu_cores_count == 0) {
886 if (mem->visibility_type == 0)
888 HSA_MEM_HEAP_TYPE_FB_PRIVATE;
891 heap_type = mem->visibility_type;
893 heap_type = HSA_MEM_HEAP_TYPE_SYSTEM;
895 if (mem->flags & CRAT_MEM_FLAGS_HOT_PLUGGABLE)
896 flags |= HSA_MEM_FLAGS_HOT_PLUGGABLE;
897 if (mem->flags & CRAT_MEM_FLAGS_NON_VOLATILE)
898 flags |= HSA_MEM_FLAGS_NON_VOLATILE;
901 ((uint64_t)mem->length_high << 32) +
905 /* Multiple banks of the same type are aggregated into
906 * one. User mode doesn't care about multiple physical
907 * memory segments. It's managed as a single virtual
908 * heap for user mode.
910 props = find_subtype_mem(heap_type, flags, width, dev);
912 props->size_in_bytes += size_in_bytes;
916 props = kfd_alloc_struct(props);
920 props->heap_type = heap_type;
921 props->flags = flags;
922 props->size_in_bytes = size_in_bytes;
923 props->width = width;
925 dev->node_props.mem_banks_count++;
926 list_add_tail(&props->list, &dev->mem_props);
935 /* kfd_parse_subtype_cache - parse cache subtypes and attach it to correct
936 * topology device present in the device_list
938 static int kfd_parse_subtype_cache(struct crat_subtype_cache *cache,
939 struct list_head *device_list)
941 struct kfd_cache_properties *props;
942 struct kfd_topology_device *dev;
944 uint32_t total_num_of_cu;
946 id = cache->processor_id_low;
948 pr_debug("Found cache entry in CRAT table with processor_id=%d\n", id);
949 list_for_each_entry(dev, device_list, list) {
950 total_num_of_cu = (dev->node_props.array_count *
951 dev->node_props.cu_per_simd_array);
953 /* Cache infomration in CRAT doesn't have proximity_domain
954 * information as it is associated with a CPU core or GPU
955 * Compute Unit. So map the cache using CPU core Id or SIMD
957 * TODO: This works because currently we can safely assume that
958 * Compute Units are parsed before caches are parsed. In
959 * future, remove this dependency
961 if ((id >= dev->node_props.cpu_core_id_base &&
962 id <= dev->node_props.cpu_core_id_base +
963 dev->node_props.cpu_cores_count) ||
964 (id >= dev->node_props.simd_id_base &&
965 id < dev->node_props.simd_id_base +
967 props = kfd_alloc_struct(props);
971 props->processor_id_low = id;
972 props->cache_level = cache->cache_level;
973 props->cache_size = cache->cache_size;
974 props->cacheline_size = cache->cache_line_size;
975 props->cachelines_per_tag = cache->lines_per_tag;
976 props->cache_assoc = cache->associativity;
977 props->cache_latency = cache->cache_latency;
978 memcpy(props->sibling_map, cache->sibling_map,
979 sizeof(props->sibling_map));
981 if (cache->flags & CRAT_CACHE_FLAGS_DATA_CACHE)
982 props->cache_type |= HSA_CACHE_TYPE_DATA;
983 if (cache->flags & CRAT_CACHE_FLAGS_INST_CACHE)
984 props->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
985 if (cache->flags & CRAT_CACHE_FLAGS_CPU_CACHE)
986 props->cache_type |= HSA_CACHE_TYPE_CPU;
987 if (cache->flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
988 props->cache_type |= HSA_CACHE_TYPE_HSACU;
991 dev->node_props.caches_count++;
992 list_add_tail(&props->list, &dev->cache_props);
1001 /* kfd_parse_subtype_iolink - parse iolink subtypes and attach it to correct
1002 * topology device present in the device_list
1004 static int kfd_parse_subtype_iolink(struct crat_subtype_iolink *iolink,
1005 struct list_head *device_list)
1007 struct kfd_iolink_properties *props = NULL, *props2;
1008 struct kfd_topology_device *dev, *to_dev;
1012 id_from = iolink->proximity_domain_from;
1013 id_to = iolink->proximity_domain_to;
1015 pr_debug("Found IO link entry in CRAT table with id_from=%d, id_to %d\n",
1017 list_for_each_entry(dev, device_list, list) {
1018 if (id_from == dev->proximity_domain) {
1019 props = kfd_alloc_struct(props);
1023 props->node_from = id_from;
1024 props->node_to = id_to;
1025 props->ver_maj = iolink->version_major;
1026 props->ver_min = iolink->version_minor;
1027 props->iolink_type = iolink->io_interface_type;
1029 if (props->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
1031 else if (props->iolink_type == CRAT_IOLINK_TYPE_XGMI)
1032 props->weight = 15 * iolink->num_hops_xgmi;
1034 props->weight = node_distance(id_from, id_to);
1036 props->min_latency = iolink->minimum_latency;
1037 props->max_latency = iolink->maximum_latency;
1038 props->min_bandwidth = iolink->minimum_bandwidth_mbs;
1039 props->max_bandwidth = iolink->maximum_bandwidth_mbs;
1040 props->rec_transfer_size =
1041 iolink->recommended_transfer_size;
1043 dev->io_link_count++;
1044 dev->node_props.io_links_count++;
1045 list_add_tail(&props->list, &dev->io_link_props);
1050 /* CPU topology is created before GPUs are detected, so CPU->GPU
1051 * links are not built at that time. If a PCIe type is discovered, it
1052 * means a GPU is detected and we are adding GPU->CPU to the topology.
1053 * At this time, also add the corresponded CPU->GPU link if GPU
1055 * For xGMI, we only added the link with one direction in the crat
1056 * table, add corresponded reversed direction link now.
1058 if (props && (iolink->flags & CRAT_IOLINK_FLAGS_BI_DIRECTIONAL)) {
1059 to_dev = kfd_topology_device_by_proximity_domain_no_lock(id_to);
1062 /* same everything but the other direction */
1063 props2 = kmemdup(props, sizeof(*props2), GFP_KERNEL);
1067 props2->node_from = id_to;
1068 props2->node_to = id_from;
1069 props2->kobj = NULL;
1070 to_dev->io_link_count++;
1071 to_dev->node_props.io_links_count++;
1072 list_add_tail(&props2->list, &to_dev->io_link_props);
1078 /* kfd_parse_subtype - parse subtypes and attach it to correct topology device
1079 * present in the device_list
1080 * @sub_type_hdr - subtype section of crat_image
1081 * @device_list - list of topology devices present in this crat_image
1083 static int kfd_parse_subtype(struct crat_subtype_generic *sub_type_hdr,
1084 struct list_head *device_list)
1086 struct crat_subtype_computeunit *cu;
1087 struct crat_subtype_memory *mem;
1088 struct crat_subtype_cache *cache;
1089 struct crat_subtype_iolink *iolink;
1092 switch (sub_type_hdr->type) {
1093 case CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY:
1094 cu = (struct crat_subtype_computeunit *)sub_type_hdr;
1095 ret = kfd_parse_subtype_cu(cu, device_list);
1097 case CRAT_SUBTYPE_MEMORY_AFFINITY:
1098 mem = (struct crat_subtype_memory *)sub_type_hdr;
1099 ret = kfd_parse_subtype_mem(mem, device_list);
1101 case CRAT_SUBTYPE_CACHE_AFFINITY:
1102 cache = (struct crat_subtype_cache *)sub_type_hdr;
1103 ret = kfd_parse_subtype_cache(cache, device_list);
1105 case CRAT_SUBTYPE_TLB_AFFINITY:
1107 * For now, nothing to do here
1109 pr_debug("Found TLB entry in CRAT table (not processing)\n");
1111 case CRAT_SUBTYPE_CCOMPUTE_AFFINITY:
1113 * For now, nothing to do here
1115 pr_debug("Found CCOMPUTE entry in CRAT table (not processing)\n");
1117 case CRAT_SUBTYPE_IOLINK_AFFINITY:
1118 iolink = (struct crat_subtype_iolink *)sub_type_hdr;
1119 ret = kfd_parse_subtype_iolink(iolink, device_list);
1122 pr_warn("Unknown subtype %d in CRAT\n",
1123 sub_type_hdr->type);
1129 /* kfd_parse_crat_table - parse CRAT table. For each node present in CRAT
1130 * create a kfd_topology_device and add in to device_list. Also parse
1131 * CRAT subtypes and attach it to appropriate kfd_topology_device
1132 * @crat_image - input image containing CRAT
1133 * @device_list - [OUT] list of kfd_topology_device generated after
1134 * parsing crat_image
1135 * @proximity_domain - Proximity domain of the first device in the table
1137 * Return - 0 if successful else -ve value
1139 int kfd_parse_crat_table(void *crat_image, struct list_head *device_list,
1140 uint32_t proximity_domain)
1142 struct kfd_topology_device *top_dev = NULL;
1143 struct crat_subtype_generic *sub_type_hdr;
1146 struct crat_header *crat_table = (struct crat_header *)crat_image;
1153 if (!list_empty(device_list)) {
1154 pr_warn("Error device list should be empty\n");
1158 num_nodes = crat_table->num_domains;
1159 image_len = crat_table->length;
1161 pr_debug("Parsing CRAT table with %d nodes\n", num_nodes);
1163 for (node_id = 0; node_id < num_nodes; node_id++) {
1164 top_dev = kfd_create_topology_device(device_list);
1167 top_dev->proximity_domain = proximity_domain++;
1175 memcpy(top_dev->oem_id, crat_table->oem_id, CRAT_OEMID_LENGTH);
1176 memcpy(top_dev->oem_table_id, crat_table->oem_table_id,
1177 CRAT_OEMTABLEID_LENGTH);
1178 top_dev->oem_revision = crat_table->oem_revision;
1180 sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
1181 while ((char *)sub_type_hdr + sizeof(struct crat_subtype_generic) <
1182 ((char *)crat_image) + image_len) {
1183 if (sub_type_hdr->flags & CRAT_SUBTYPE_FLAGS_ENABLED) {
1184 ret = kfd_parse_subtype(sub_type_hdr, device_list);
1189 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1190 sub_type_hdr->length);
1195 kfd_release_topology_device_list(device_list);
1200 /* Helper function. See kfd_fill_gpu_cache_info for parameter description */
1201 static int fill_in_l1_pcache(struct crat_subtype_cache *pcache,
1202 struct kfd_gpu_cache_info *pcache_info,
1203 struct kfd_cu_info *cu_info,
1206 int cache_type, unsigned int cu_processor_id,
1209 unsigned int cu_sibling_map_mask;
1210 int first_active_cu;
1212 /* First check if enough memory is available */
1213 if (sizeof(struct crat_subtype_cache) > mem_available)
1216 cu_sibling_map_mask = cu_bitmask;
1217 cu_sibling_map_mask >>= cu_block;
1218 cu_sibling_map_mask &=
1219 ((1 << pcache_info[cache_type].num_cu_shared) - 1);
1220 first_active_cu = ffs(cu_sibling_map_mask);
1222 /* CU could be inactive. In case of shared cache find the first active
1223 * CU. and incase of non-shared cache check if the CU is inactive. If
1224 * inactive active skip it
1226 if (first_active_cu) {
1227 memset(pcache, 0, sizeof(struct crat_subtype_cache));
1228 pcache->type = CRAT_SUBTYPE_CACHE_AFFINITY;
1229 pcache->length = sizeof(struct crat_subtype_cache);
1230 pcache->flags = pcache_info[cache_type].flags;
1231 pcache->processor_id_low = cu_processor_id
1232 + (first_active_cu - 1);
1233 pcache->cache_level = pcache_info[cache_type].cache_level;
1234 pcache->cache_size = pcache_info[cache_type].cache_size;
1236 /* Sibling map is w.r.t processor_id_low, so shift out
1239 cu_sibling_map_mask =
1240 cu_sibling_map_mask >> (first_active_cu - 1);
1242 pcache->sibling_map[0] = (uint8_t)(cu_sibling_map_mask & 0xFF);
1243 pcache->sibling_map[1] =
1244 (uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
1245 pcache->sibling_map[2] =
1246 (uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
1247 pcache->sibling_map[3] =
1248 (uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
1254 /* Helper function. See kfd_fill_gpu_cache_info for parameter description */
1255 static int fill_in_l2_l3_pcache(struct crat_subtype_cache *pcache,
1256 struct kfd_gpu_cache_info *pcache_info,
1257 struct kfd_cu_info *cu_info,
1259 int cache_type, unsigned int cu_processor_id)
1261 unsigned int cu_sibling_map_mask;
1262 int first_active_cu;
1265 /* First check if enough memory is available */
1266 if (sizeof(struct crat_subtype_cache) > mem_available)
1269 cu_sibling_map_mask = cu_info->cu_bitmap[0][0];
1270 cu_sibling_map_mask &=
1271 ((1 << pcache_info[cache_type].num_cu_shared) - 1);
1272 first_active_cu = ffs(cu_sibling_map_mask);
1274 /* CU could be inactive. In case of shared cache find the first active
1275 * CU. and incase of non-shared cache check if the CU is inactive. If
1276 * inactive active skip it
1278 if (first_active_cu) {
1279 memset(pcache, 0, sizeof(struct crat_subtype_cache));
1280 pcache->type = CRAT_SUBTYPE_CACHE_AFFINITY;
1281 pcache->length = sizeof(struct crat_subtype_cache);
1282 pcache->flags = pcache_info[cache_type].flags;
1283 pcache->processor_id_low = cu_processor_id
1284 + (first_active_cu - 1);
1285 pcache->cache_level = pcache_info[cache_type].cache_level;
1286 pcache->cache_size = pcache_info[cache_type].cache_size;
1288 /* Sibling map is w.r.t processor_id_low, so shift out
1291 cu_sibling_map_mask =
1292 cu_sibling_map_mask >> (first_active_cu - 1);
1294 for (i = 0; i < cu_info->num_shader_engines; i++) {
1295 for (j = 0; j < cu_info->num_shader_arrays_per_engine;
1297 pcache->sibling_map[k] =
1298 (uint8_t)(cu_sibling_map_mask & 0xFF);
1299 pcache->sibling_map[k+1] =
1300 (uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
1301 pcache->sibling_map[k+2] =
1302 (uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
1303 pcache->sibling_map[k+3] =
1304 (uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
1306 cu_sibling_map_mask =
1307 cu_info->cu_bitmap[i % 4][j + i / 4];
1308 cu_sibling_map_mask &= (
1309 (1 << pcache_info[cache_type].num_cu_shared)
1318 #define KFD_MAX_CACHE_TYPES 6
1320 static int kfd_fill_gpu_cache_info_from_gfx_config(struct kfd_dev *kdev,
1321 struct kfd_gpu_cache_info *pcache_info)
1323 struct amdgpu_device *adev = kdev->adev;
1326 /* TCP L1 Cache per CU */
1327 if (adev->gfx.config.gc_tcp_l1_size) {
1328 pcache_info[i].cache_size = adev->gfx.config.gc_tcp_l1_size;
1329 pcache_info[i].cache_level = 1;
1330 pcache_info[i].flags = (CRAT_CACHE_FLAGS_ENABLED |
1331 CRAT_CACHE_FLAGS_DATA_CACHE |
1332 CRAT_CACHE_FLAGS_SIMD_CACHE);
1333 pcache_info[0].num_cu_shared = adev->gfx.config.gc_num_tcp_per_wpg / 2;
1336 /* Scalar L1 Instruction Cache per SQC */
1337 if (adev->gfx.config.gc_l1_instruction_cache_size_per_sqc) {
1338 pcache_info[i].cache_size =
1339 adev->gfx.config.gc_l1_instruction_cache_size_per_sqc;
1340 pcache_info[i].cache_level = 1;
1341 pcache_info[i].flags = (CRAT_CACHE_FLAGS_ENABLED |
1342 CRAT_CACHE_FLAGS_INST_CACHE |
1343 CRAT_CACHE_FLAGS_SIMD_CACHE);
1344 pcache_info[i].num_cu_shared = adev->gfx.config.gc_num_sqc_per_wgp * 2;
1347 /* Scalar L1 Data Cache per SQC */
1348 if (adev->gfx.config.gc_l1_data_cache_size_per_sqc) {
1349 pcache_info[i].cache_size = adev->gfx.config.gc_l1_data_cache_size_per_sqc;
1350 pcache_info[i].cache_level = 1;
1351 pcache_info[i].flags = (CRAT_CACHE_FLAGS_ENABLED |
1352 CRAT_CACHE_FLAGS_DATA_CACHE |
1353 CRAT_CACHE_FLAGS_SIMD_CACHE);
1354 pcache_info[i].num_cu_shared = adev->gfx.config.gc_num_sqc_per_wgp * 2;
1357 /* GL1 Data Cache per SA */
1358 if (adev->gfx.config.gc_gl1c_per_sa &&
1359 adev->gfx.config.gc_gl1c_size_per_instance) {
1360 pcache_info[i].cache_size = adev->gfx.config.gc_gl1c_per_sa *
1361 adev->gfx.config.gc_gl1c_size_per_instance;
1362 pcache_info[i].cache_level = 1;
1363 pcache_info[i].flags = (CRAT_CACHE_FLAGS_ENABLED |
1364 CRAT_CACHE_FLAGS_DATA_CACHE |
1365 CRAT_CACHE_FLAGS_SIMD_CACHE);
1366 pcache_info[i].num_cu_shared = adev->gfx.config.max_cu_per_sh;
1369 /* L2 Data Cache per GPU (Total Tex Cache) */
1370 if (adev->gfx.config.gc_gl2c_per_gpu) {
1371 pcache_info[i].cache_size = adev->gfx.config.gc_gl2c_per_gpu;
1372 pcache_info[i].cache_level = 2;
1373 pcache_info[i].flags = (CRAT_CACHE_FLAGS_ENABLED |
1374 CRAT_CACHE_FLAGS_DATA_CACHE |
1375 CRAT_CACHE_FLAGS_SIMD_CACHE);
1376 pcache_info[i].num_cu_shared = adev->gfx.config.max_cu_per_sh;
1379 /* L3 Data Cache per GPU */
1380 if (adev->gmc.mall_size) {
1381 pcache_info[i].cache_size = adev->gmc.mall_size / 1024;
1382 pcache_info[i].cache_level = 3;
1383 pcache_info[i].flags = (CRAT_CACHE_FLAGS_ENABLED |
1384 CRAT_CACHE_FLAGS_DATA_CACHE |
1385 CRAT_CACHE_FLAGS_SIMD_CACHE);
1386 pcache_info[i].num_cu_shared = adev->gfx.config.max_cu_per_sh;
1392 /* kfd_fill_gpu_cache_info - Fill GPU cache info using kfd_gpu_cache_info
1395 * @kdev - [IN] GPU device
1396 * @gpu_processor_id - [IN] GPU processor ID to which these caches
1398 * @available_size - [IN] Amount of memory available in pcache
1399 * @cu_info - [IN] Compute Unit info obtained from KGD
1400 * @pcache - [OUT] memory into which cache data is to be filled in.
1401 * @size_filled - [OUT] amount of data used up in pcache.
1402 * @num_of_entries - [OUT] number of caches added
1404 static int kfd_fill_gpu_cache_info(struct kfd_dev *kdev,
1405 int gpu_processor_id,
1407 struct kfd_cu_info *cu_info,
1408 struct crat_subtype_cache *pcache,
1410 int *num_of_entries)
1412 struct kfd_gpu_cache_info *pcache_info;
1413 struct kfd_gpu_cache_info cache_info[KFD_MAX_CACHE_TYPES];
1414 int num_of_cache_types = 0;
1417 int mem_available = available_size;
1418 unsigned int cu_processor_id;
1420 unsigned int num_cu_shared;
1422 switch (kdev->adev->asic_type) {
1424 pcache_info = kaveri_cache_info;
1425 num_of_cache_types = ARRAY_SIZE(kaveri_cache_info);
1428 pcache_info = hawaii_cache_info;
1429 num_of_cache_types = ARRAY_SIZE(hawaii_cache_info);
1432 pcache_info = carrizo_cache_info;
1433 num_of_cache_types = ARRAY_SIZE(carrizo_cache_info);
1436 pcache_info = tonga_cache_info;
1437 num_of_cache_types = ARRAY_SIZE(tonga_cache_info);
1440 pcache_info = fiji_cache_info;
1441 num_of_cache_types = ARRAY_SIZE(fiji_cache_info);
1443 case CHIP_POLARIS10:
1444 pcache_info = polaris10_cache_info;
1445 num_of_cache_types = ARRAY_SIZE(polaris10_cache_info);
1447 case CHIP_POLARIS11:
1448 pcache_info = polaris11_cache_info;
1449 num_of_cache_types = ARRAY_SIZE(polaris11_cache_info);
1451 case CHIP_POLARIS12:
1452 pcache_info = polaris12_cache_info;
1453 num_of_cache_types = ARRAY_SIZE(polaris12_cache_info);
1456 pcache_info = vegam_cache_info;
1457 num_of_cache_types = ARRAY_SIZE(vegam_cache_info);
1460 switch (KFD_GC_VERSION(kdev)) {
1461 case IP_VERSION(9, 0, 1):
1462 pcache_info = vega10_cache_info;
1463 num_of_cache_types = ARRAY_SIZE(vega10_cache_info);
1465 case IP_VERSION(9, 2, 1):
1466 pcache_info = vega12_cache_info;
1467 num_of_cache_types = ARRAY_SIZE(vega12_cache_info);
1469 case IP_VERSION(9, 4, 0):
1470 case IP_VERSION(9, 4, 1):
1471 pcache_info = vega20_cache_info;
1472 num_of_cache_types = ARRAY_SIZE(vega20_cache_info);
1474 case IP_VERSION(9, 4, 2):
1475 pcache_info = aldebaran_cache_info;
1476 num_of_cache_types = ARRAY_SIZE(aldebaran_cache_info);
1478 case IP_VERSION(9, 1, 0):
1479 case IP_VERSION(9, 2, 2):
1480 pcache_info = raven_cache_info;
1481 num_of_cache_types = ARRAY_SIZE(raven_cache_info);
1483 case IP_VERSION(9, 3, 0):
1484 pcache_info = renoir_cache_info;
1485 num_of_cache_types = ARRAY_SIZE(renoir_cache_info);
1487 case IP_VERSION(10, 1, 10):
1488 case IP_VERSION(10, 1, 2):
1489 case IP_VERSION(10, 1, 3):
1490 case IP_VERSION(10, 1, 4):
1491 pcache_info = navi10_cache_info;
1492 num_of_cache_types = ARRAY_SIZE(navi10_cache_info);
1494 case IP_VERSION(10, 1, 1):
1495 pcache_info = navi14_cache_info;
1496 num_of_cache_types = ARRAY_SIZE(navi14_cache_info);
1498 case IP_VERSION(10, 3, 0):
1499 pcache_info = sienna_cichlid_cache_info;
1500 num_of_cache_types = ARRAY_SIZE(sienna_cichlid_cache_info);
1502 case IP_VERSION(10, 3, 2):
1503 pcache_info = navy_flounder_cache_info;
1504 num_of_cache_types = ARRAY_SIZE(navy_flounder_cache_info);
1506 case IP_VERSION(10, 3, 4):
1507 pcache_info = dimgrey_cavefish_cache_info;
1508 num_of_cache_types = ARRAY_SIZE(dimgrey_cavefish_cache_info);
1510 case IP_VERSION(10, 3, 1):
1511 pcache_info = vangogh_cache_info;
1512 num_of_cache_types = ARRAY_SIZE(vangogh_cache_info);
1514 case IP_VERSION(10, 3, 5):
1515 pcache_info = beige_goby_cache_info;
1516 num_of_cache_types = ARRAY_SIZE(beige_goby_cache_info);
1518 case IP_VERSION(10, 3, 3):
1519 case IP_VERSION(10, 3, 6): /* TODO: Double check these on production silicon */
1520 case IP_VERSION(10, 3, 7): /* TODO: Double check these on production silicon */
1521 pcache_info = yellow_carp_cache_info;
1522 num_of_cache_types = ARRAY_SIZE(yellow_carp_cache_info);
1524 case IP_VERSION(11, 0, 0):
1525 case IP_VERSION(11, 0, 1):
1526 case IP_VERSION(11, 0, 2):
1527 pcache_info = cache_info;
1528 num_of_cache_types =
1529 kfd_fill_gpu_cache_info_from_gfx_config(kdev, pcache_info);
1537 *num_of_entries = 0;
1539 /* For each type of cache listed in the kfd_gpu_cache_info table,
1540 * go through all available Compute Units.
1541 * The [i,j,k] loop will
1542 * if kfd_gpu_cache_info.num_cu_shared = 1
1543 * will parse through all available CU
1544 * If (kfd_gpu_cache_info.num_cu_shared != 1)
1545 * then it will consider only one CU from
1549 for (ct = 0; ct < num_of_cache_types; ct++) {
1550 cu_processor_id = gpu_processor_id;
1551 if (pcache_info[ct].cache_level == 1) {
1552 for (i = 0; i < cu_info->num_shader_engines; i++) {
1553 for (j = 0; j < cu_info->num_shader_arrays_per_engine; j++) {
1554 for (k = 0; k < cu_info->num_cu_per_sh;
1555 k += pcache_info[ct].num_cu_shared) {
1556 ret = fill_in_l1_pcache(pcache,
1560 cu_info->cu_bitmap[i % 4][j + i / 4],
1570 (*num_of_entries)++;
1571 mem_available -= sizeof(*pcache);
1572 (*size_filled) += sizeof(*pcache);
1575 /* Move to next CU block */
1576 num_cu_shared = ((k + pcache_info[ct].num_cu_shared) <=
1577 cu_info->num_cu_per_sh) ?
1578 pcache_info[ct].num_cu_shared :
1579 (cu_info->num_cu_per_sh - k);
1580 cu_processor_id += num_cu_shared;
1585 ret = fill_in_l2_l3_pcache(pcache,
1597 (*num_of_entries)++;
1598 mem_available -= sizeof(*pcache);
1599 (*size_filled) += sizeof(*pcache);
1604 pr_debug("Added [%d] GPU cache entries\n", *num_of_entries);
1609 static bool kfd_ignore_crat(void)
1616 #ifndef KFD_SUPPORT_IOMMU_V2
1626 * kfd_create_crat_image_acpi - Allocates memory for CRAT image and
1627 * copies CRAT from ACPI (if available).
1628 * NOTE: Call kfd_destroy_crat_image to free CRAT image memory
1630 * @crat_image: CRAT read from ACPI. If no CRAT in ACPI then
1631 * crat_image will be NULL
1632 * @size: [OUT] size of crat_image
1634 * Return 0 if successful else return error code
1636 int kfd_create_crat_image_acpi(void **crat_image, size_t *size)
1638 struct acpi_table_header *crat_table;
1648 if (kfd_ignore_crat()) {
1649 pr_info("CRAT table disabled by module option\n");
1653 /* Fetch the CRAT table from ACPI */
1654 status = acpi_get_table(CRAT_SIGNATURE, 0, &crat_table);
1655 if (status == AE_NOT_FOUND) {
1656 pr_info("CRAT table not found\n");
1658 } else if (ACPI_FAILURE(status)) {
1659 const char *err = acpi_format_exception(status);
1661 pr_err("CRAT table error: %s\n", err);
1665 pcrat_image = kvmalloc(crat_table->length, GFP_KERNEL);
1671 memcpy(pcrat_image, crat_table, crat_table->length);
1672 *crat_image = pcrat_image;
1673 *size = crat_table->length;
1675 acpi_put_table(crat_table);
1679 /* Memory required to create Virtual CRAT.
1680 * Since there is no easy way to predict the amount of memory required, the
1681 * following amount is allocated for GPU Virtual CRAT. This is
1682 * expected to cover all known conditions. But to be safe additional check
1683 * is put in the code to ensure we don't overwrite.
1685 #define VCRAT_SIZE_FOR_GPU (4 * PAGE_SIZE)
1687 /* kfd_fill_cu_for_cpu - Fill in Compute info for the given CPU NUMA node
1689 * @numa_node_id: CPU NUMA node id
1690 * @avail_size: Available size in the memory
1691 * @sub_type_hdr: Memory into which compute info will be filled in
1693 * Return 0 if successful else return -ve value
1695 static int kfd_fill_cu_for_cpu(int numa_node_id, int *avail_size,
1696 int proximity_domain,
1697 struct crat_subtype_computeunit *sub_type_hdr)
1699 const struct cpumask *cpumask;
1701 *avail_size -= sizeof(struct crat_subtype_computeunit);
1702 if (*avail_size < 0)
1705 memset(sub_type_hdr, 0, sizeof(struct crat_subtype_computeunit));
1707 /* Fill in subtype header data */
1708 sub_type_hdr->type = CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY;
1709 sub_type_hdr->length = sizeof(struct crat_subtype_computeunit);
1710 sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
1712 cpumask = cpumask_of_node(numa_node_id);
1714 /* Fill in CU data */
1715 sub_type_hdr->flags |= CRAT_CU_FLAGS_CPU_PRESENT;
1716 sub_type_hdr->proximity_domain = proximity_domain;
1717 sub_type_hdr->processor_id_low = kfd_numa_node_to_apic_id(numa_node_id);
1718 if (sub_type_hdr->processor_id_low == -1)
1721 sub_type_hdr->num_cpu_cores = cpumask_weight(cpumask);
1726 /* kfd_fill_mem_info_for_cpu - Fill in Memory info for the given CPU NUMA node
1728 * @numa_node_id: CPU NUMA node id
1729 * @avail_size: Available size in the memory
1730 * @sub_type_hdr: Memory into which compute info will be filled in
1732 * Return 0 if successful else return -ve value
1734 static int kfd_fill_mem_info_for_cpu(int numa_node_id, int *avail_size,
1735 int proximity_domain,
1736 struct crat_subtype_memory *sub_type_hdr)
1738 uint64_t mem_in_bytes = 0;
1742 *avail_size -= sizeof(struct crat_subtype_memory);
1743 if (*avail_size < 0)
1746 memset(sub_type_hdr, 0, sizeof(struct crat_subtype_memory));
1748 /* Fill in subtype header data */
1749 sub_type_hdr->type = CRAT_SUBTYPE_MEMORY_AFFINITY;
1750 sub_type_hdr->length = sizeof(struct crat_subtype_memory);
1751 sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
1753 /* Fill in Memory Subunit data */
1755 /* Unlike si_meminfo, si_meminfo_node is not exported. So
1756 * the following lines are duplicated from si_meminfo_node
1759 pgdat = NODE_DATA(numa_node_id);
1760 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
1761 mem_in_bytes += zone_managed_pages(&pgdat->node_zones[zone_type]);
1762 mem_in_bytes <<= PAGE_SHIFT;
1764 sub_type_hdr->length_low = lower_32_bits(mem_in_bytes);
1765 sub_type_hdr->length_high = upper_32_bits(mem_in_bytes);
1766 sub_type_hdr->proximity_domain = proximity_domain;
1771 #ifdef CONFIG_X86_64
1772 static int kfd_fill_iolink_info_for_cpu(int numa_node_id, int *avail_size,
1773 uint32_t *num_entries,
1774 struct crat_subtype_iolink *sub_type_hdr)
1777 struct cpuinfo_x86 *c = &cpu_data(0);
1780 if (c->x86_vendor == X86_VENDOR_AMD)
1781 link_type = CRAT_IOLINK_TYPE_HYPERTRANSPORT;
1783 link_type = CRAT_IOLINK_TYPE_QPI_1_1;
1787 /* Create IO links from this node to other CPU nodes */
1788 for_each_online_node(nid) {
1789 if (nid == numa_node_id) /* node itself */
1792 *avail_size -= sizeof(struct crat_subtype_iolink);
1793 if (*avail_size < 0)
1796 memset(sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
1798 /* Fill in subtype header data */
1799 sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
1800 sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
1801 sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
1803 /* Fill in IO link data */
1804 sub_type_hdr->proximity_domain_from = numa_node_id;
1805 sub_type_hdr->proximity_domain_to = nid;
1806 sub_type_hdr->io_interface_type = link_type;
1816 /* kfd_create_vcrat_image_cpu - Create Virtual CRAT for CPU
1818 * @pcrat_image: Fill in VCRAT for CPU
1819 * @size: [IN] allocated size of crat_image.
1820 * [OUT] actual size of data filled in crat_image
1822 static int kfd_create_vcrat_image_cpu(void *pcrat_image, size_t *size)
1824 struct crat_header *crat_table = (struct crat_header *)pcrat_image;
1825 struct acpi_table_header *acpi_table;
1827 struct crat_subtype_generic *sub_type_hdr;
1828 int avail_size = *size;
1830 #ifdef CONFIG_X86_64
1831 uint32_t entries = 0;
1838 /* Fill in CRAT Header.
1839 * Modify length and total_entries as subunits are added.
1841 avail_size -= sizeof(struct crat_header);
1845 memset(crat_table, 0, sizeof(struct crat_header));
1846 memcpy(&crat_table->signature, CRAT_SIGNATURE,
1847 sizeof(crat_table->signature));
1848 crat_table->length = sizeof(struct crat_header);
1850 status = acpi_get_table("DSDT", 0, &acpi_table);
1851 if (status != AE_OK)
1852 pr_warn("DSDT table not found for OEM information\n");
1854 crat_table->oem_revision = acpi_table->revision;
1855 memcpy(crat_table->oem_id, acpi_table->oem_id,
1857 memcpy(crat_table->oem_table_id, acpi_table->oem_table_id,
1858 CRAT_OEMTABLEID_LENGTH);
1859 acpi_put_table(acpi_table);
1861 crat_table->total_entries = 0;
1862 crat_table->num_domains = 0;
1864 sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
1866 for_each_online_node(numa_node_id) {
1867 if (kfd_numa_node_to_apic_id(numa_node_id) == -1)
1870 /* Fill in Subtype: Compute Unit */
1871 ret = kfd_fill_cu_for_cpu(numa_node_id, &avail_size,
1872 crat_table->num_domains,
1873 (struct crat_subtype_computeunit *)sub_type_hdr);
1876 crat_table->length += sub_type_hdr->length;
1877 crat_table->total_entries++;
1879 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1880 sub_type_hdr->length);
1882 /* Fill in Subtype: Memory */
1883 ret = kfd_fill_mem_info_for_cpu(numa_node_id, &avail_size,
1884 crat_table->num_domains,
1885 (struct crat_subtype_memory *)sub_type_hdr);
1888 crat_table->length += sub_type_hdr->length;
1889 crat_table->total_entries++;
1891 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1892 sub_type_hdr->length);
1894 /* Fill in Subtype: IO Link */
1895 #ifdef CONFIG_X86_64
1896 ret = kfd_fill_iolink_info_for_cpu(numa_node_id, &avail_size,
1898 (struct crat_subtype_iolink *)sub_type_hdr);
1903 crat_table->length += (sub_type_hdr->length * entries);
1904 crat_table->total_entries += entries;
1906 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1907 sub_type_hdr->length * entries);
1910 pr_info("IO link not available for non x86 platforms\n");
1913 crat_table->num_domains++;
1916 /* TODO: Add cache Subtype for CPU.
1917 * Currently, CPU cache information is available in function
1918 * detect_cache_attributes(cpu) defined in the file
1919 * ./arch/x86/kernel/cpu/intel_cacheinfo.c. This function is not
1920 * exported and to get the same information the code needs to be
1924 *size = crat_table->length;
1925 pr_info("Virtual CRAT table created for CPU\n");
1930 static int kfd_fill_gpu_memory_affinity(int *avail_size,
1931 struct kfd_dev *kdev, uint8_t type, uint64_t size,
1932 struct crat_subtype_memory *sub_type_hdr,
1933 uint32_t proximity_domain,
1934 const struct kfd_local_mem_info *local_mem_info)
1936 *avail_size -= sizeof(struct crat_subtype_memory);
1937 if (*avail_size < 0)
1940 memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_memory));
1941 sub_type_hdr->type = CRAT_SUBTYPE_MEMORY_AFFINITY;
1942 sub_type_hdr->length = sizeof(struct crat_subtype_memory);
1943 sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED;
1945 sub_type_hdr->proximity_domain = proximity_domain;
1947 pr_debug("Fill gpu memory affinity - type 0x%x size 0x%llx\n",
1950 sub_type_hdr->length_low = lower_32_bits(size);
1951 sub_type_hdr->length_high = upper_32_bits(size);
1953 sub_type_hdr->width = local_mem_info->vram_width;
1954 sub_type_hdr->visibility_type = type;
1959 #ifdef CONFIG_ACPI_NUMA
1960 static void kfd_find_numa_node_in_srat(struct kfd_dev *kdev)
1962 struct acpi_table_header *table_header = NULL;
1963 struct acpi_subtable_header *sub_header = NULL;
1964 unsigned long table_end, subtable_len;
1965 u32 pci_id = pci_domain_nr(kdev->pdev->bus) << 16 |
1966 pci_dev_id(kdev->pdev);
1969 struct acpi_srat_cpu_affinity *cpu;
1970 struct acpi_srat_generic_affinity *gpu;
1971 int pxm = 0, max_pxm = 0;
1972 int numa_node = NUMA_NO_NODE;
1975 /* Fetch the SRAT table from ACPI */
1976 status = acpi_get_table(ACPI_SIG_SRAT, 0, &table_header);
1977 if (status == AE_NOT_FOUND) {
1978 pr_warn("SRAT table not found\n");
1980 } else if (ACPI_FAILURE(status)) {
1981 const char *err = acpi_format_exception(status);
1982 pr_err("SRAT table error: %s\n", err);
1986 table_end = (unsigned long)table_header + table_header->length;
1988 /* Parse all entries looking for a match. */
1989 sub_header = (struct acpi_subtable_header *)
1990 ((unsigned long)table_header +
1991 sizeof(struct acpi_table_srat));
1992 subtable_len = sub_header->length;
1994 while (((unsigned long)sub_header) + subtable_len < table_end) {
1996 * If length is 0, break from this loop to avoid
1999 if (subtable_len == 0) {
2000 pr_err("SRAT invalid zero length\n");
2004 switch (sub_header->type) {
2005 case ACPI_SRAT_TYPE_CPU_AFFINITY:
2006 cpu = (struct acpi_srat_cpu_affinity *)sub_header;
2007 pxm = *((u32 *)cpu->proximity_domain_hi) << 8 |
2008 cpu->proximity_domain_lo;
2012 case ACPI_SRAT_TYPE_GENERIC_AFFINITY:
2013 gpu = (struct acpi_srat_generic_affinity *)sub_header;
2014 bdf = *((u16 *)(&gpu->device_handle[0])) << 16 |
2015 *((u16 *)(&gpu->device_handle[2]));
2016 if (bdf == pci_id) {
2018 numa_node = pxm_to_node(gpu->proximity_domain);
2028 sub_header = (struct acpi_subtable_header *)
2029 ((unsigned long)sub_header + subtable_len);
2030 subtable_len = sub_header->length;
2033 acpi_put_table(table_header);
2035 /* Workaround bad cpu-gpu binding case */
2036 if (found && (numa_node < 0 ||
2037 numa_node > pxm_to_node(max_pxm)))
2040 if (numa_node != NUMA_NO_NODE)
2041 set_dev_node(&kdev->pdev->dev, numa_node);
2045 /* kfd_fill_gpu_direct_io_link - Fill in direct io link from GPU
2047 * @avail_size: Available size in the memory
2048 * @kdev - [IN] GPU device
2049 * @sub_type_hdr: Memory into which io link info will be filled in
2050 * @proximity_domain - proximity domain of the GPU node
2052 * Return 0 if successful else return -ve value
2054 static int kfd_fill_gpu_direct_io_link_to_cpu(int *avail_size,
2055 struct kfd_dev *kdev,
2056 struct crat_subtype_iolink *sub_type_hdr,
2057 uint32_t proximity_domain)
2059 *avail_size -= sizeof(struct crat_subtype_iolink);
2060 if (*avail_size < 0)
2063 memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
2065 /* Fill in subtype header data */
2066 sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
2067 sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
2068 sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED;
2069 if (kfd_dev_is_large_bar(kdev))
2070 sub_type_hdr->flags |= CRAT_IOLINK_FLAGS_BI_DIRECTIONAL;
2072 /* Fill in IOLINK subtype.
2073 * TODO: Fill-in other fields of iolink subtype
2075 if (kdev->adev->gmc.xgmi.connected_to_cpu) {
2077 * with host gpu xgmi link, host can access gpu memory whether
2078 * or not pcie bar type is large, so always create bidirectional
2081 sub_type_hdr->flags |= CRAT_IOLINK_FLAGS_BI_DIRECTIONAL;
2082 sub_type_hdr->io_interface_type = CRAT_IOLINK_TYPE_XGMI;
2083 sub_type_hdr->num_hops_xgmi = 1;
2084 if (KFD_GC_VERSION(kdev) == IP_VERSION(9, 4, 2)) {
2085 sub_type_hdr->minimum_bandwidth_mbs =
2086 amdgpu_amdkfd_get_xgmi_bandwidth_mbytes(
2087 kdev->adev, NULL, true);
2088 sub_type_hdr->maximum_bandwidth_mbs =
2089 sub_type_hdr->minimum_bandwidth_mbs;
2092 sub_type_hdr->io_interface_type = CRAT_IOLINK_TYPE_PCIEXPRESS;
2093 sub_type_hdr->minimum_bandwidth_mbs =
2094 amdgpu_amdkfd_get_pcie_bandwidth_mbytes(kdev->adev, true);
2095 sub_type_hdr->maximum_bandwidth_mbs =
2096 amdgpu_amdkfd_get_pcie_bandwidth_mbytes(kdev->adev, false);
2099 sub_type_hdr->proximity_domain_from = proximity_domain;
2101 #ifdef CONFIG_ACPI_NUMA
2102 if (kdev->pdev->dev.numa_node == NUMA_NO_NODE)
2103 kfd_find_numa_node_in_srat(kdev);
2106 if (kdev->pdev->dev.numa_node == NUMA_NO_NODE)
2107 sub_type_hdr->proximity_domain_to = 0;
2109 sub_type_hdr->proximity_domain_to = kdev->pdev->dev.numa_node;
2111 sub_type_hdr->proximity_domain_to = 0;
2116 static int kfd_fill_gpu_xgmi_link_to_gpu(int *avail_size,
2117 struct kfd_dev *kdev,
2118 struct kfd_dev *peer_kdev,
2119 struct crat_subtype_iolink *sub_type_hdr,
2120 uint32_t proximity_domain_from,
2121 uint32_t proximity_domain_to)
2123 *avail_size -= sizeof(struct crat_subtype_iolink);
2124 if (*avail_size < 0)
2127 memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
2129 sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
2130 sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
2131 sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED |
2132 CRAT_IOLINK_FLAGS_BI_DIRECTIONAL;
2134 sub_type_hdr->io_interface_type = CRAT_IOLINK_TYPE_XGMI;
2135 sub_type_hdr->proximity_domain_from = proximity_domain_from;
2136 sub_type_hdr->proximity_domain_to = proximity_domain_to;
2137 sub_type_hdr->num_hops_xgmi =
2138 amdgpu_amdkfd_get_xgmi_hops_count(kdev->adev, peer_kdev->adev);
2139 sub_type_hdr->maximum_bandwidth_mbs =
2140 amdgpu_amdkfd_get_xgmi_bandwidth_mbytes(kdev->adev, peer_kdev->adev, false);
2141 sub_type_hdr->minimum_bandwidth_mbs = sub_type_hdr->maximum_bandwidth_mbs ?
2142 amdgpu_amdkfd_get_xgmi_bandwidth_mbytes(kdev->adev, NULL, true) : 0;
2147 /* kfd_create_vcrat_image_gpu - Create Virtual CRAT for CPU
2149 * @pcrat_image: Fill in VCRAT for GPU
2150 * @size: [IN] allocated size of crat_image.
2151 * [OUT] actual size of data filled in crat_image
2153 static int kfd_create_vcrat_image_gpu(void *pcrat_image,
2154 size_t *size, struct kfd_dev *kdev,
2155 uint32_t proximity_domain)
2157 struct crat_header *crat_table = (struct crat_header *)pcrat_image;
2158 struct crat_subtype_generic *sub_type_hdr;
2159 struct kfd_local_mem_info local_mem_info;
2160 struct kfd_topology_device *peer_dev;
2161 struct crat_subtype_computeunit *cu;
2162 struct kfd_cu_info cu_info;
2163 int avail_size = *size;
2164 uint32_t total_num_of_cu;
2165 int num_of_cache_entries = 0;
2166 int cache_mem_filled = 0;
2170 if (!pcrat_image || avail_size < VCRAT_SIZE_FOR_GPU)
2173 /* Fill the CRAT Header.
2174 * Modify length and total_entries as subunits are added.
2176 avail_size -= sizeof(struct crat_header);
2180 memset(crat_table, 0, sizeof(struct crat_header));
2182 memcpy(&crat_table->signature, CRAT_SIGNATURE,
2183 sizeof(crat_table->signature));
2184 /* Change length as we add more subtypes*/
2185 crat_table->length = sizeof(struct crat_header);
2186 crat_table->num_domains = 1;
2187 crat_table->total_entries = 0;
2189 /* Fill in Subtype: Compute Unit
2190 * First fill in the sub type header and then sub type data
2192 avail_size -= sizeof(struct crat_subtype_computeunit);
2196 sub_type_hdr = (struct crat_subtype_generic *)(crat_table + 1);
2197 memset(sub_type_hdr, 0, sizeof(struct crat_subtype_computeunit));
2199 sub_type_hdr->type = CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY;
2200 sub_type_hdr->length = sizeof(struct crat_subtype_computeunit);
2201 sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
2203 /* Fill CU subtype data */
2204 cu = (struct crat_subtype_computeunit *)sub_type_hdr;
2205 cu->flags |= CRAT_CU_FLAGS_GPU_PRESENT;
2206 cu->proximity_domain = proximity_domain;
2208 amdgpu_amdkfd_get_cu_info(kdev->adev, &cu_info);
2209 cu->num_simd_per_cu = cu_info.simd_per_cu;
2210 cu->num_simd_cores = cu_info.simd_per_cu * cu_info.cu_active_number;
2211 cu->max_waves_simd = cu_info.max_waves_per_simd;
2213 cu->wave_front_size = cu_info.wave_front_size;
2214 cu->array_count = cu_info.num_shader_arrays_per_engine *
2215 cu_info.num_shader_engines;
2216 total_num_of_cu = (cu->array_count * cu_info.num_cu_per_sh);
2217 cu->processor_id_low = get_and_inc_gpu_processor_id(total_num_of_cu);
2218 cu->num_cu_per_array = cu_info.num_cu_per_sh;
2219 cu->max_slots_scatch_cu = cu_info.max_scratch_slots_per_cu;
2220 cu->num_banks = cu_info.num_shader_engines;
2221 cu->lds_size_in_kb = cu_info.lds_size;
2223 cu->hsa_capability = 0;
2225 /* Check if this node supports IOMMU. During parsing this flag will
2226 * translate to HSA_CAP_ATS_PRESENT
2228 if (!kfd_iommu_check_device(kdev))
2229 cu->hsa_capability |= CRAT_CU_FLAGS_IOMMU_PRESENT;
2231 crat_table->length += sub_type_hdr->length;
2232 crat_table->total_entries++;
2234 /* Fill in Subtype: Memory. Only on systems with large BAR (no
2235 * private FB), report memory as public. On other systems
2236 * report the total FB size (public+private) as a single
2239 local_mem_info = kdev->local_mem_info;
2240 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
2241 sub_type_hdr->length);
2244 local_mem_info.local_mem_size_private = 0;
2246 if (local_mem_info.local_mem_size_private == 0)
2247 ret = kfd_fill_gpu_memory_affinity(&avail_size,
2248 kdev, HSA_MEM_HEAP_TYPE_FB_PUBLIC,
2249 local_mem_info.local_mem_size_public,
2250 (struct crat_subtype_memory *)sub_type_hdr,
2254 ret = kfd_fill_gpu_memory_affinity(&avail_size,
2255 kdev, HSA_MEM_HEAP_TYPE_FB_PRIVATE,
2256 local_mem_info.local_mem_size_public +
2257 local_mem_info.local_mem_size_private,
2258 (struct crat_subtype_memory *)sub_type_hdr,
2264 crat_table->length += sizeof(struct crat_subtype_memory);
2265 crat_table->total_entries++;
2267 /* TODO: Fill in cache information. This information is NOT readily
2270 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
2271 sub_type_hdr->length);
2272 ret = kfd_fill_gpu_cache_info(kdev, cu->processor_id_low,
2275 (struct crat_subtype_cache *)sub_type_hdr,
2277 &num_of_cache_entries);
2282 crat_table->length += cache_mem_filled;
2283 crat_table->total_entries += num_of_cache_entries;
2284 avail_size -= cache_mem_filled;
2286 /* Fill in Subtype: IO_LINKS
2287 * Only direct links are added here which is Link from GPU to
2288 * to its NUMA node. Indirect links are added by userspace.
2290 sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
2292 ret = kfd_fill_gpu_direct_io_link_to_cpu(&avail_size, kdev,
2293 (struct crat_subtype_iolink *)sub_type_hdr, proximity_domain);
2298 crat_table->length += sub_type_hdr->length;
2299 crat_table->total_entries++;
2302 /* Fill in Subtype: IO_LINKS
2303 * Direct links from GPU to other GPUs through xGMI.
2304 * We will loop GPUs that already be processed (with lower value
2305 * of proximity_domain), add the link for the GPUs with same
2306 * hive id (from this GPU to other GPU) . The reversed iolink
2307 * (from other GPU to this GPU) will be added
2308 * in kfd_parse_subtype_iolink.
2310 if (kdev->hive_id) {
2311 for (nid = 0; nid < proximity_domain; ++nid) {
2312 peer_dev = kfd_topology_device_by_proximity_domain_no_lock(nid);
2315 if (peer_dev->gpu->hive_id != kdev->hive_id)
2317 sub_type_hdr = (typeof(sub_type_hdr))(
2318 (char *)sub_type_hdr +
2319 sizeof(struct crat_subtype_iolink));
2320 ret = kfd_fill_gpu_xgmi_link_to_gpu(
2321 &avail_size, kdev, peer_dev->gpu,
2322 (struct crat_subtype_iolink *)sub_type_hdr,
2323 proximity_domain, nid);
2326 crat_table->length += sub_type_hdr->length;
2327 crat_table->total_entries++;
2330 *size = crat_table->length;
2331 pr_info("Virtual CRAT table created for GPU\n");
2336 /* kfd_create_crat_image_virtual - Allocates memory for CRAT image and
2337 * creates a Virtual CRAT (VCRAT) image
2339 * NOTE: Call kfd_destroy_crat_image to free CRAT image memory
2341 * @crat_image: VCRAT image created because ACPI does not have a
2342 * CRAT for this device
2343 * @size: [OUT] size of virtual crat_image
2344 * @flags: COMPUTE_UNIT_CPU - Create VCRAT for CPU device
2345 * COMPUTE_UNIT_GPU - Create VCRAT for GPU
2346 * (COMPUTE_UNIT_CPU | COMPUTE_UNIT_GPU) - Create VCRAT for APU
2347 * -- this option is not currently implemented.
2348 * The assumption is that all AMD APUs will have CRAT
2349 * @kdev: Valid kfd_device required if flags contain COMPUTE_UNIT_GPU
2351 * Return 0 if successful else return -ve value
2353 int kfd_create_crat_image_virtual(void **crat_image, size_t *size,
2354 int flags, struct kfd_dev *kdev,
2355 uint32_t proximity_domain)
2357 void *pcrat_image = NULL;
2358 int ret = 0, num_nodes;
2366 /* Allocate the CPU Virtual CRAT size based on the number of online
2367 * nodes. Allocate VCRAT_SIZE_FOR_GPU for GPU virtual CRAT image.
2368 * This should cover all the current conditions. A check is put not
2369 * to overwrite beyond allocated size for GPUs
2372 case COMPUTE_UNIT_CPU:
2373 num_nodes = num_online_nodes();
2374 dyn_size = sizeof(struct crat_header) +
2375 num_nodes * (sizeof(struct crat_subtype_computeunit) +
2376 sizeof(struct crat_subtype_memory) +
2377 (num_nodes - 1) * sizeof(struct crat_subtype_iolink));
2378 pcrat_image = kvmalloc(dyn_size, GFP_KERNEL);
2382 pr_debug("CRAT size is %ld", dyn_size);
2383 ret = kfd_create_vcrat_image_cpu(pcrat_image, size);
2385 case COMPUTE_UNIT_GPU:
2388 pcrat_image = kvmalloc(VCRAT_SIZE_FOR_GPU, GFP_KERNEL);
2391 *size = VCRAT_SIZE_FOR_GPU;
2392 ret = kfd_create_vcrat_image_gpu(pcrat_image, size, kdev,
2395 case (COMPUTE_UNIT_CPU | COMPUTE_UNIT_GPU):
2398 pr_err("VCRAT not implemented for APU\n");
2405 *crat_image = pcrat_image;
2407 kvfree(pcrat_image);
2413 /* kfd_destroy_crat_image
2415 * @crat_image: [IN] - crat_image from kfd_create_crat_image_xxx(..)
2418 void kfd_destroy_crat_image(void *crat_image)