xref: /openbmc/linux/drivers/gpu/drm/amd/amdkfd/kfd_crat.c (revision 4f727ecefefbd180de10e25b3e74c03dce3f1e75)
1 /*
2  * Copyright 2015-2017 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include <linux/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 /* GPU Processor ID base for dGPUs for which VCRAT needs to be created.
32  * GPU processor ID are expressed with Bit[31]=1.
33  * The base is set to 0x8000_0000 + 0x1000 to avoid collision with GPU IDs
34  * used in the CRAT.
35  */
36 static uint32_t gpu_processor_id_low = 0x80001000;
37 
38 /* Return the next available gpu_processor_id and increment it for next GPU
39  *	@total_cu_count - Total CUs present in the GPU including ones
40  *			  masked off
41  */
42 static 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 /* Static table to describe GPU Cache information */
52 struct kfd_gpu_cache_info {
53 	uint32_t	cache_size;
54 	uint32_t	cache_level;
55 	uint32_t	flags;
56 	/* Indicates how many Compute Units share this cache
57 	 * Value = 1 indicates the cache is not shared
58 	 */
59 	uint32_t	num_cu_shared;
60 };
61 
62 static struct kfd_gpu_cache_info kaveri_cache_info[] = {
63 	{
64 		/* TCP L1 Cache per CU */
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 		/* Scalar L1 Instruction Cache (in SQC module) per bank */
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 		/* Scalar L1 Data Cache (in SQC module) per bank */
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 	/* TODO: Add L2 Cache information */
93 };
94 
95 
96 static struct kfd_gpu_cache_info carrizo_cache_info[] = {
97 	{
98 		/* TCP L1 Cache per CU */
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 		/* Scalar L1 Instruction Cache (in SQC module) per bank */
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 		/* Scalar L1 Data Cache (in SQC module) per bank. */
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 	/* TODO: Add L2 Cache information */
126 };
127 
128 /* NOTE: In future if more information is added to struct kfd_gpu_cache_info
129  * the following ASICs may need a separate table.
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 /* TODO - check & update Vega10 cache details */
139 #define vega10_cache_info carrizo_cache_info
140 #define raven_cache_info carrizo_cache_info
141 /* TODO - check & update Navi10 cache details */
142 #define navi10_cache_info carrizo_cache_info
143 
144 static 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 
156 static 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 /* kfd_parse_subtype_cu - parse compute unit subtypes and attach it to correct
174  * topology device present in the device_list
175  */
176 static 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 
197 static struct kfd_mem_properties *
198 find_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 /* kfd_parse_subtype_mem - parse memory subtypes and attach it to correct
213  * topology device present in the device_list
214  */
215 static 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 			/* We're on GPU node */
230 			if (dev->node_props.cpu_cores_count == 0) {
231 				/* APU */
232 				if (mem->visibility_type == 0)
233 					heap_type =
234 						HSA_MEM_HEAP_TYPE_FB_PRIVATE;
235 				/* dGPU */
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 			/* Multiple banks of the same type are aggregated into
252 			 * one. User mode doesn't care about multiple physical
253 			 * memory segments. It's managed as a single virtual
254 			 * heap for user mode.
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 /* kfd_parse_subtype_cache - parse cache subtypes and attach it to correct
282  * topology device present in the device_list
283  */
284 static 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 		/* Cache infomration in CRAT doesn't have proximity_domain
300 		 * information as it is associated with a CPU core or GPU
301 		 * Compute Unit. So map the cache using CPU core Id or SIMD
302 		 * (GPU) ID.
303 		 * TODO: This works because currently we can safely assume that
304 		 *  Compute Units are parsed before caches are parsed. In
305 		 *  future, remove this dependency
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 /* kfd_parse_subtype_iolink - parse iolink subtypes and attach it to correct
348  * topology device present in the device_list
349  */
350 static 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 	/* CPU topology is created before GPUs are detected, so CPU->GPU
397 	 * links are not built at that time. If a PCIe type is discovered, it
398 	 * means a GPU is detected and we are adding GPU->CPU to the topology.
399 	 * At this time, also add the corresponded CPU->GPU link if GPU
400 	 * is large bar.
401 	 * For xGMI, we only added the link with one direction in the crat
402 	 * table, add corresponded reversed direction link now.
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 		/* same everything but the other direction */
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 /* kfd_parse_subtype - parse subtypes and attach it to correct topology device
422  * present in the device_list
423  *	@sub_type_hdr - subtype section of crat_image
424  *	@device_list - list of topology devices present in this crat_image
425  */
426 static 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 		 * For now, nothing to do here
451 		 */
452 		pr_debug("Found TLB entry in CRAT table (not processing)\n");
453 		break;
454 	case CRAT_SUBTYPE_CCOMPUTE_AFFINITY:
455 		/*
456 		 * For now, nothing to do here
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 /* kfd_parse_crat_table - parse CRAT table. For each node present in CRAT
473  * create a kfd_topology_device and add in to device_list. Also parse
474  * CRAT subtypes and attach it to appropriate kfd_topology_device
475  *	@crat_image - input image containing CRAT
476  *	@device_list - [OUT] list of kfd_topology_device generated after
477  *		       parsing crat_image
478  *	@proximity_domain - Proximity domain of the first device in the table
479  *
480  *	Return - 0 if successful else -ve value
481  */
482 int 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 
536 err:
537 	if (ret)
538 		kfd_release_topology_device_list(device_list);
539 
540 	return ret;
541 }
542 
543 /* Helper function. See kfd_fill_gpu_cache_info for parameter description */
544 static 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 	/* First check if enough memory is available */
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 	/* CU could be inactive. In case of shared cache find the first active
566 	 * CU. and incase of non-shared cache check if the CU is inactive. If
567 	 * inactive active skip it
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 		/* Sibling map is w.r.t processor_id_low, so shift out
580 		 * inactive CU
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 /* kfd_fill_gpu_cache_info - Fill GPU cache info using kfd_gpu_cache_info
598  * tables
599  *
600  *	@kdev - [IN] GPU device
601  *	@gpu_processor_id - [IN] GPU processor ID to which these caches
602  *			    associate
603  *	@available_size - [IN] Amount of memory available in pcache
604  *	@cu_info - [IN] Compute Unit info obtained from KGD
605  *	@pcache - [OUT] memory into which cache data is to be filled in.
606  *	@size_filled - [OUT] amount of data used up in pcache.
607  *	@num_of_entries - [OUT] number of caches added
608  */
609 static 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 	case CHIP_NAVI10:
672 		pcache_info = navi10_cache_info;
673 		num_of_cache_types = ARRAY_SIZE(navi10_cache_info);
674 		break;
675 	default:
676 		return -EINVAL;
677 	}
678 
679 	*size_filled = 0;
680 	*num_of_entries = 0;
681 
682 	/* For each type of cache listed in the kfd_gpu_cache_info table,
683 	 * go through all available Compute Units.
684 	 * The [i,j,k] loop will
685 	 *		if kfd_gpu_cache_info.num_cu_shared = 1
686 	 *			will parse through all available CU
687 	 *		If (kfd_gpu_cache_info.num_cu_shared != 1)
688 	 *			then it will consider only one CU from
689 	 *			the shared unit
690 	 */
691 
692 	for (ct = 0; ct < num_of_cache_types; ct++) {
693 		cu_processor_id = gpu_processor_id;
694 		for (i = 0; i < cu_info->num_shader_engines; i++) {
695 			for (j = 0; j < cu_info->num_shader_arrays_per_engine;
696 				j++) {
697 				for (k = 0; k < cu_info->num_cu_per_sh;
698 					k += pcache_info[ct].num_cu_shared) {
699 
700 					ret = fill_in_pcache(pcache,
701 						pcache_info,
702 						cu_info,
703 						mem_available,
704 						cu_info->cu_bitmap[i][j],
705 						ct,
706 						cu_processor_id,
707 						k);
708 
709 					if (ret < 0)
710 						break;
711 
712 					if (!ret) {
713 						pcache++;
714 						(*num_of_entries)++;
715 						mem_available -=
716 							sizeof(*pcache);
717 						(*size_filled) +=
718 							sizeof(*pcache);
719 					}
720 
721 					/* Move to next CU block */
722 					cu_processor_id +=
723 						pcache_info[ct].num_cu_shared;
724 				}
725 			}
726 		}
727 	}
728 
729 	pr_debug("Added [%d] GPU cache entries\n", *num_of_entries);
730 
731 	return 0;
732 }
733 
734 /*
735  * kfd_create_crat_image_acpi - Allocates memory for CRAT image and
736  * copies CRAT from ACPI (if available).
737  * NOTE: Call kfd_destroy_crat_image to free CRAT image memory
738  *
739  *	@crat_image: CRAT read from ACPI. If no CRAT in ACPI then
740  *		     crat_image will be NULL
741  *	@size: [OUT] size of crat_image
742  *
743  *	Return 0 if successful else return error code
744  */
745 int kfd_create_crat_image_acpi(void **crat_image, size_t *size)
746 {
747 	struct acpi_table_header *crat_table;
748 	acpi_status status;
749 	void *pcrat_image;
750 
751 	if (!crat_image)
752 		return -EINVAL;
753 
754 	*crat_image = NULL;
755 
756 	/* Fetch the CRAT table from ACPI */
757 	status = acpi_get_table(CRAT_SIGNATURE, 0, &crat_table);
758 	if (status == AE_NOT_FOUND) {
759 		pr_warn("CRAT table not found\n");
760 		return -ENODATA;
761 	} else if (ACPI_FAILURE(status)) {
762 		const char *err = acpi_format_exception(status);
763 
764 		pr_err("CRAT table error: %s\n", err);
765 		return -EINVAL;
766 	}
767 
768 	if (ignore_crat) {
769 		pr_info("CRAT table disabled by module option\n");
770 		return -ENODATA;
771 	}
772 
773 	pcrat_image = kmemdup(crat_table, crat_table->length, GFP_KERNEL);
774 	if (!pcrat_image)
775 		return -ENOMEM;
776 
777 	*crat_image = pcrat_image;
778 	*size = crat_table->length;
779 
780 	return 0;
781 }
782 
783 /* Memory required to create Virtual CRAT.
784  * Since there is no easy way to predict the amount of memory required, the
785  * following amount are allocated for CPU and GPU Virtual CRAT. This is
786  * expected to cover all known conditions. But to be safe additional check
787  * is put in the code to ensure we don't overwrite.
788  */
789 #define VCRAT_SIZE_FOR_CPU	(2 * PAGE_SIZE)
790 #define VCRAT_SIZE_FOR_GPU	(3 * PAGE_SIZE)
791 
792 /* kfd_fill_cu_for_cpu - Fill in Compute info for the given CPU NUMA node
793  *
794  *	@numa_node_id: CPU NUMA node id
795  *	@avail_size: Available size in the memory
796  *	@sub_type_hdr: Memory into which compute info will be filled in
797  *
798  *	Return 0 if successful else return -ve value
799  */
800 static int kfd_fill_cu_for_cpu(int numa_node_id, int *avail_size,
801 				int proximity_domain,
802 				struct crat_subtype_computeunit *sub_type_hdr)
803 {
804 	const struct cpumask *cpumask;
805 
806 	*avail_size -= sizeof(struct crat_subtype_computeunit);
807 	if (*avail_size < 0)
808 		return -ENOMEM;
809 
810 	memset(sub_type_hdr, 0, sizeof(struct crat_subtype_computeunit));
811 
812 	/* Fill in subtype header data */
813 	sub_type_hdr->type = CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY;
814 	sub_type_hdr->length = sizeof(struct crat_subtype_computeunit);
815 	sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
816 
817 	cpumask = cpumask_of_node(numa_node_id);
818 
819 	/* Fill in CU data */
820 	sub_type_hdr->flags |= CRAT_CU_FLAGS_CPU_PRESENT;
821 	sub_type_hdr->proximity_domain = proximity_domain;
822 	sub_type_hdr->processor_id_low = kfd_numa_node_to_apic_id(numa_node_id);
823 	if (sub_type_hdr->processor_id_low == -1)
824 		return -EINVAL;
825 
826 	sub_type_hdr->num_cpu_cores = cpumask_weight(cpumask);
827 
828 	return 0;
829 }
830 
831 /* kfd_fill_mem_info_for_cpu - Fill in Memory info for the given CPU NUMA node
832  *
833  *	@numa_node_id: CPU NUMA node id
834  *	@avail_size: Available size in the memory
835  *	@sub_type_hdr: Memory into which compute info will be filled in
836  *
837  *	Return 0 if successful else return -ve value
838  */
839 static int kfd_fill_mem_info_for_cpu(int numa_node_id, int *avail_size,
840 			int proximity_domain,
841 			struct crat_subtype_memory *sub_type_hdr)
842 {
843 	uint64_t mem_in_bytes = 0;
844 	pg_data_t *pgdat;
845 	int zone_type;
846 
847 	*avail_size -= sizeof(struct crat_subtype_memory);
848 	if (*avail_size < 0)
849 		return -ENOMEM;
850 
851 	memset(sub_type_hdr, 0, sizeof(struct crat_subtype_memory));
852 
853 	/* Fill in subtype header data */
854 	sub_type_hdr->type = CRAT_SUBTYPE_MEMORY_AFFINITY;
855 	sub_type_hdr->length = sizeof(struct crat_subtype_memory);
856 	sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
857 
858 	/* Fill in Memory Subunit data */
859 
860 	/* Unlike si_meminfo, si_meminfo_node is not exported. So
861 	 * the following lines are duplicated from si_meminfo_node
862 	 * function
863 	 */
864 	pgdat = NODE_DATA(numa_node_id);
865 	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
866 		mem_in_bytes += zone_managed_pages(&pgdat->node_zones[zone_type]);
867 	mem_in_bytes <<= PAGE_SHIFT;
868 
869 	sub_type_hdr->length_low = lower_32_bits(mem_in_bytes);
870 	sub_type_hdr->length_high = upper_32_bits(mem_in_bytes);
871 	sub_type_hdr->proximity_domain = proximity_domain;
872 
873 	return 0;
874 }
875 
876 #ifdef CONFIG_X86_64
877 static int kfd_fill_iolink_info_for_cpu(int numa_node_id, int *avail_size,
878 				uint32_t *num_entries,
879 				struct crat_subtype_iolink *sub_type_hdr)
880 {
881 	int nid;
882 	struct cpuinfo_x86 *c = &cpu_data(0);
883 	uint8_t link_type;
884 
885 	if (c->x86_vendor == X86_VENDOR_AMD)
886 		link_type = CRAT_IOLINK_TYPE_HYPERTRANSPORT;
887 	else
888 		link_type = CRAT_IOLINK_TYPE_QPI_1_1;
889 
890 	*num_entries = 0;
891 
892 	/* Create IO links from this node to other CPU nodes */
893 	for_each_online_node(nid) {
894 		if (nid == numa_node_id) /* node itself */
895 			continue;
896 
897 		*avail_size -= sizeof(struct crat_subtype_iolink);
898 		if (*avail_size < 0)
899 			return -ENOMEM;
900 
901 		memset(sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
902 
903 		/* Fill in subtype header data */
904 		sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
905 		sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
906 		sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
907 
908 		/* Fill in IO link data */
909 		sub_type_hdr->proximity_domain_from = numa_node_id;
910 		sub_type_hdr->proximity_domain_to = nid;
911 		sub_type_hdr->io_interface_type = link_type;
912 
913 		(*num_entries)++;
914 		sub_type_hdr++;
915 	}
916 
917 	return 0;
918 }
919 #endif
920 
921 /* kfd_create_vcrat_image_cpu - Create Virtual CRAT for CPU
922  *
923  *	@pcrat_image: Fill in VCRAT for CPU
924  *	@size:	[IN] allocated size of crat_image.
925  *		[OUT] actual size of data filled in crat_image
926  */
927 static int kfd_create_vcrat_image_cpu(void *pcrat_image, size_t *size)
928 {
929 	struct crat_header *crat_table = (struct crat_header *)pcrat_image;
930 	struct acpi_table_header *acpi_table;
931 	acpi_status status;
932 	struct crat_subtype_generic *sub_type_hdr;
933 	int avail_size = *size;
934 	int numa_node_id;
935 #ifdef CONFIG_X86_64
936 	uint32_t entries = 0;
937 #endif
938 	int ret = 0;
939 
940 	if (!pcrat_image || avail_size < VCRAT_SIZE_FOR_CPU)
941 		return -EINVAL;
942 
943 	/* Fill in CRAT Header.
944 	 * Modify length and total_entries as subunits are added.
945 	 */
946 	avail_size -= sizeof(struct crat_header);
947 	if (avail_size < 0)
948 		return -ENOMEM;
949 
950 	memset(crat_table, 0, sizeof(struct crat_header));
951 	memcpy(&crat_table->signature, CRAT_SIGNATURE,
952 			sizeof(crat_table->signature));
953 	crat_table->length = sizeof(struct crat_header);
954 
955 	status = acpi_get_table("DSDT", 0, &acpi_table);
956 	if (status != AE_OK)
957 		pr_warn("DSDT table not found for OEM information\n");
958 	else {
959 		crat_table->oem_revision = acpi_table->revision;
960 		memcpy(crat_table->oem_id, acpi_table->oem_id,
961 				CRAT_OEMID_LENGTH);
962 		memcpy(crat_table->oem_table_id, acpi_table->oem_table_id,
963 				CRAT_OEMTABLEID_LENGTH);
964 	}
965 	crat_table->total_entries = 0;
966 	crat_table->num_domains = 0;
967 
968 	sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
969 
970 	for_each_online_node(numa_node_id) {
971 		if (kfd_numa_node_to_apic_id(numa_node_id) == -1)
972 			continue;
973 
974 		/* Fill in Subtype: Compute Unit */
975 		ret = kfd_fill_cu_for_cpu(numa_node_id, &avail_size,
976 			crat_table->num_domains,
977 			(struct crat_subtype_computeunit *)sub_type_hdr);
978 		if (ret < 0)
979 			return ret;
980 		crat_table->length += sub_type_hdr->length;
981 		crat_table->total_entries++;
982 
983 		sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
984 			sub_type_hdr->length);
985 
986 		/* Fill in Subtype: Memory */
987 		ret = kfd_fill_mem_info_for_cpu(numa_node_id, &avail_size,
988 			crat_table->num_domains,
989 			(struct crat_subtype_memory *)sub_type_hdr);
990 		if (ret < 0)
991 			return ret;
992 		crat_table->length += sub_type_hdr->length;
993 		crat_table->total_entries++;
994 
995 		sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
996 			sub_type_hdr->length);
997 
998 		/* Fill in Subtype: IO Link */
999 #ifdef CONFIG_X86_64
1000 		ret = kfd_fill_iolink_info_for_cpu(numa_node_id, &avail_size,
1001 				&entries,
1002 				(struct crat_subtype_iolink *)sub_type_hdr);
1003 		if (ret < 0)
1004 			return ret;
1005 		crat_table->length += (sub_type_hdr->length * entries);
1006 		crat_table->total_entries += entries;
1007 
1008 		sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1009 				sub_type_hdr->length * entries);
1010 #else
1011 		pr_info("IO link not available for non x86 platforms\n");
1012 #endif
1013 
1014 		crat_table->num_domains++;
1015 	}
1016 
1017 	/* TODO: Add cache Subtype for CPU.
1018 	 * Currently, CPU cache information is available in function
1019 	 * detect_cache_attributes(cpu) defined in the file
1020 	 * ./arch/x86/kernel/cpu/intel_cacheinfo.c. This function is not
1021 	 * exported and to get the same information the code needs to be
1022 	 * duplicated.
1023 	 */
1024 
1025 	*size = crat_table->length;
1026 	pr_info("Virtual CRAT table created for CPU\n");
1027 
1028 	return 0;
1029 }
1030 
1031 static int kfd_fill_gpu_memory_affinity(int *avail_size,
1032 		struct kfd_dev *kdev, uint8_t type, uint64_t size,
1033 		struct crat_subtype_memory *sub_type_hdr,
1034 		uint32_t proximity_domain,
1035 		const struct kfd_local_mem_info *local_mem_info)
1036 {
1037 	*avail_size -= sizeof(struct crat_subtype_memory);
1038 	if (*avail_size < 0)
1039 		return -ENOMEM;
1040 
1041 	memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_memory));
1042 	sub_type_hdr->type = CRAT_SUBTYPE_MEMORY_AFFINITY;
1043 	sub_type_hdr->length = sizeof(struct crat_subtype_memory);
1044 	sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED;
1045 
1046 	sub_type_hdr->proximity_domain = proximity_domain;
1047 
1048 	pr_debug("Fill gpu memory affinity - type 0x%x size 0x%llx\n",
1049 			type, size);
1050 
1051 	sub_type_hdr->length_low = lower_32_bits(size);
1052 	sub_type_hdr->length_high = upper_32_bits(size);
1053 
1054 	sub_type_hdr->width = local_mem_info->vram_width;
1055 	sub_type_hdr->visibility_type = type;
1056 
1057 	return 0;
1058 }
1059 
1060 /* kfd_fill_gpu_direct_io_link - Fill in direct io link from GPU
1061  * to its NUMA node
1062  *	@avail_size: Available size in the memory
1063  *	@kdev - [IN] GPU device
1064  *	@sub_type_hdr: Memory into which io link info will be filled in
1065  *	@proximity_domain - proximity domain of the GPU node
1066  *
1067  *	Return 0 if successful else return -ve value
1068  */
1069 static int kfd_fill_gpu_direct_io_link_to_cpu(int *avail_size,
1070 			struct kfd_dev *kdev,
1071 			struct crat_subtype_iolink *sub_type_hdr,
1072 			uint32_t proximity_domain)
1073 {
1074 	*avail_size -= sizeof(struct crat_subtype_iolink);
1075 	if (*avail_size < 0)
1076 		return -ENOMEM;
1077 
1078 	memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
1079 
1080 	/* Fill in subtype header data */
1081 	sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
1082 	sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
1083 	sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED;
1084 	if (kfd_dev_is_large_bar(kdev))
1085 		sub_type_hdr->flags |= CRAT_IOLINK_FLAGS_BI_DIRECTIONAL;
1086 
1087 	/* Fill in IOLINK subtype.
1088 	 * TODO: Fill-in other fields of iolink subtype
1089 	 */
1090 	sub_type_hdr->io_interface_type = CRAT_IOLINK_TYPE_PCIEXPRESS;
1091 	sub_type_hdr->proximity_domain_from = proximity_domain;
1092 #ifdef CONFIG_NUMA
1093 	if (kdev->pdev->dev.numa_node == NUMA_NO_NODE)
1094 		sub_type_hdr->proximity_domain_to = 0;
1095 	else
1096 		sub_type_hdr->proximity_domain_to = kdev->pdev->dev.numa_node;
1097 #else
1098 	sub_type_hdr->proximity_domain_to = 0;
1099 #endif
1100 	return 0;
1101 }
1102 
1103 static int kfd_fill_gpu_xgmi_link_to_gpu(int *avail_size,
1104 			struct kfd_dev *kdev,
1105 			struct kfd_dev *peer_kdev,
1106 			struct crat_subtype_iolink *sub_type_hdr,
1107 			uint32_t proximity_domain_from,
1108 			uint32_t proximity_domain_to)
1109 {
1110 	*avail_size -= sizeof(struct crat_subtype_iolink);
1111 	if (*avail_size < 0)
1112 		return -ENOMEM;
1113 
1114 	memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
1115 
1116 	sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
1117 	sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
1118 	sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED |
1119 			       CRAT_IOLINK_FLAGS_BI_DIRECTIONAL;
1120 
1121 	sub_type_hdr->io_interface_type = CRAT_IOLINK_TYPE_XGMI;
1122 	sub_type_hdr->proximity_domain_from = proximity_domain_from;
1123 	sub_type_hdr->proximity_domain_to = proximity_domain_to;
1124 	sub_type_hdr->num_hops_xgmi =
1125 		amdgpu_amdkfd_get_xgmi_hops_count(kdev->kgd, peer_kdev->kgd);
1126 	return 0;
1127 }
1128 
1129 /* kfd_create_vcrat_image_gpu - Create Virtual CRAT for CPU
1130  *
1131  *	@pcrat_image: Fill in VCRAT for GPU
1132  *	@size:	[IN] allocated size of crat_image.
1133  *		[OUT] actual size of data filled in crat_image
1134  */
1135 static int kfd_create_vcrat_image_gpu(void *pcrat_image,
1136 				      size_t *size, struct kfd_dev *kdev,
1137 				      uint32_t proximity_domain)
1138 {
1139 	struct crat_header *crat_table = (struct crat_header *)pcrat_image;
1140 	struct crat_subtype_generic *sub_type_hdr;
1141 	struct kfd_local_mem_info local_mem_info;
1142 	struct kfd_topology_device *peer_dev;
1143 	struct crat_subtype_computeunit *cu;
1144 	struct kfd_cu_info cu_info;
1145 	int avail_size = *size;
1146 	uint32_t total_num_of_cu;
1147 	int num_of_cache_entries = 0;
1148 	int cache_mem_filled = 0;
1149 	uint32_t nid = 0;
1150 	int ret = 0;
1151 
1152 	if (!pcrat_image || avail_size < VCRAT_SIZE_FOR_GPU)
1153 		return -EINVAL;
1154 
1155 	/* Fill the CRAT Header.
1156 	 * Modify length and total_entries as subunits are added.
1157 	 */
1158 	avail_size -= sizeof(struct crat_header);
1159 	if (avail_size < 0)
1160 		return -ENOMEM;
1161 
1162 	memset(crat_table, 0, sizeof(struct crat_header));
1163 
1164 	memcpy(&crat_table->signature, CRAT_SIGNATURE,
1165 			sizeof(crat_table->signature));
1166 	/* Change length as we add more subtypes*/
1167 	crat_table->length = sizeof(struct crat_header);
1168 	crat_table->num_domains = 1;
1169 	crat_table->total_entries = 0;
1170 
1171 	/* Fill in Subtype: Compute Unit
1172 	 * First fill in the sub type header and then sub type data
1173 	 */
1174 	avail_size -= sizeof(struct crat_subtype_computeunit);
1175 	if (avail_size < 0)
1176 		return -ENOMEM;
1177 
1178 	sub_type_hdr = (struct crat_subtype_generic *)(crat_table + 1);
1179 	memset(sub_type_hdr, 0, sizeof(struct crat_subtype_computeunit));
1180 
1181 	sub_type_hdr->type = CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY;
1182 	sub_type_hdr->length = sizeof(struct crat_subtype_computeunit);
1183 	sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
1184 
1185 	/* Fill CU subtype data */
1186 	cu = (struct crat_subtype_computeunit *)sub_type_hdr;
1187 	cu->flags |= CRAT_CU_FLAGS_GPU_PRESENT;
1188 	cu->proximity_domain = proximity_domain;
1189 
1190 	amdgpu_amdkfd_get_cu_info(kdev->kgd, &cu_info);
1191 	cu->num_simd_per_cu = cu_info.simd_per_cu;
1192 	cu->num_simd_cores = cu_info.simd_per_cu * cu_info.cu_active_number;
1193 	cu->max_waves_simd = cu_info.max_waves_per_simd;
1194 
1195 	cu->wave_front_size = cu_info.wave_front_size;
1196 	cu->array_count = cu_info.num_shader_arrays_per_engine *
1197 		cu_info.num_shader_engines;
1198 	total_num_of_cu = (cu->array_count * cu_info.num_cu_per_sh);
1199 	cu->processor_id_low = get_and_inc_gpu_processor_id(total_num_of_cu);
1200 	cu->num_cu_per_array = cu_info.num_cu_per_sh;
1201 	cu->max_slots_scatch_cu = cu_info.max_scratch_slots_per_cu;
1202 	cu->num_banks = cu_info.num_shader_engines;
1203 	cu->lds_size_in_kb = cu_info.lds_size;
1204 
1205 	cu->hsa_capability = 0;
1206 
1207 	/* Check if this node supports IOMMU. During parsing this flag will
1208 	 * translate to HSA_CAP_ATS_PRESENT
1209 	 */
1210 	if (!kfd_iommu_check_device(kdev))
1211 		cu->hsa_capability |= CRAT_CU_FLAGS_IOMMU_PRESENT;
1212 
1213 	crat_table->length += sub_type_hdr->length;
1214 	crat_table->total_entries++;
1215 
1216 	/* Fill in Subtype: Memory. Only on systems with large BAR (no
1217 	 * private FB), report memory as public. On other systems
1218 	 * report the total FB size (public+private) as a single
1219 	 * private heap.
1220 	 */
1221 	amdgpu_amdkfd_get_local_mem_info(kdev->kgd, &local_mem_info);
1222 	sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1223 			sub_type_hdr->length);
1224 
1225 	if (debug_largebar)
1226 		local_mem_info.local_mem_size_private = 0;
1227 
1228 	if (local_mem_info.local_mem_size_private == 0)
1229 		ret = kfd_fill_gpu_memory_affinity(&avail_size,
1230 				kdev, HSA_MEM_HEAP_TYPE_FB_PUBLIC,
1231 				local_mem_info.local_mem_size_public,
1232 				(struct crat_subtype_memory *)sub_type_hdr,
1233 				proximity_domain,
1234 				&local_mem_info);
1235 	else
1236 		ret = kfd_fill_gpu_memory_affinity(&avail_size,
1237 				kdev, HSA_MEM_HEAP_TYPE_FB_PRIVATE,
1238 				local_mem_info.local_mem_size_public +
1239 				local_mem_info.local_mem_size_private,
1240 				(struct crat_subtype_memory *)sub_type_hdr,
1241 				proximity_domain,
1242 				&local_mem_info);
1243 	if (ret < 0)
1244 		return ret;
1245 
1246 	crat_table->length += sizeof(struct crat_subtype_memory);
1247 	crat_table->total_entries++;
1248 
1249 	/* TODO: Fill in cache information. This information is NOT readily
1250 	 * available in KGD
1251 	 */
1252 	sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1253 		sub_type_hdr->length);
1254 	ret = kfd_fill_gpu_cache_info(kdev, cu->processor_id_low,
1255 				avail_size,
1256 				&cu_info,
1257 				(struct crat_subtype_cache *)sub_type_hdr,
1258 				&cache_mem_filled,
1259 				&num_of_cache_entries);
1260 
1261 	if (ret < 0)
1262 		return ret;
1263 
1264 	crat_table->length += cache_mem_filled;
1265 	crat_table->total_entries += num_of_cache_entries;
1266 	avail_size -= cache_mem_filled;
1267 
1268 	/* Fill in Subtype: IO_LINKS
1269 	 *  Only direct links are added here which is Link from GPU to
1270 	 *  to its NUMA node. Indirect links are added by userspace.
1271 	 */
1272 	sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1273 		cache_mem_filled);
1274 	ret = kfd_fill_gpu_direct_io_link_to_cpu(&avail_size, kdev,
1275 		(struct crat_subtype_iolink *)sub_type_hdr, proximity_domain);
1276 
1277 	if (ret < 0)
1278 		return ret;
1279 
1280 	crat_table->length += sub_type_hdr->length;
1281 	crat_table->total_entries++;
1282 
1283 
1284 	/* Fill in Subtype: IO_LINKS
1285 	 * Direct links from GPU to other GPUs through xGMI.
1286 	 * We will loop GPUs that already be processed (with lower value
1287 	 * of proximity_domain), add the link for the GPUs with same
1288 	 * hive id (from this GPU to other GPU) . The reversed iolink
1289 	 * (from other GPU to this GPU) will be added
1290 	 * in kfd_parse_subtype_iolink.
1291 	 */
1292 	if (kdev->hive_id) {
1293 		for (nid = 0; nid < proximity_domain; ++nid) {
1294 			peer_dev = kfd_topology_device_by_proximity_domain(nid);
1295 			if (!peer_dev->gpu)
1296 				continue;
1297 			if (peer_dev->gpu->hive_id != kdev->hive_id)
1298 				continue;
1299 			sub_type_hdr = (typeof(sub_type_hdr))(
1300 				(char *)sub_type_hdr +
1301 				sizeof(struct crat_subtype_iolink));
1302 			ret = kfd_fill_gpu_xgmi_link_to_gpu(
1303 				&avail_size, kdev, peer_dev->gpu,
1304 				(struct crat_subtype_iolink *)sub_type_hdr,
1305 				proximity_domain, nid);
1306 			if (ret < 0)
1307 				return ret;
1308 			crat_table->length += sub_type_hdr->length;
1309 			crat_table->total_entries++;
1310 		}
1311 	}
1312 	*size = crat_table->length;
1313 	pr_info("Virtual CRAT table created for GPU\n");
1314 
1315 	return ret;
1316 }
1317 
1318 /* kfd_create_crat_image_virtual - Allocates memory for CRAT image and
1319  *		creates a Virtual CRAT (VCRAT) image
1320  *
1321  * NOTE: Call kfd_destroy_crat_image to free CRAT image memory
1322  *
1323  *	@crat_image: VCRAT image created because ACPI does not have a
1324  *		     CRAT for this device
1325  *	@size: [OUT] size of virtual crat_image
1326  *	@flags:	COMPUTE_UNIT_CPU - Create VCRAT for CPU device
1327  *		COMPUTE_UNIT_GPU - Create VCRAT for GPU
1328  *		(COMPUTE_UNIT_CPU | COMPUTE_UNIT_GPU) - Create VCRAT for APU
1329  *			-- this option is not currently implemented.
1330  *			The assumption is that all AMD APUs will have CRAT
1331  *	@kdev: Valid kfd_device required if flags contain COMPUTE_UNIT_GPU
1332  *
1333  *	Return 0 if successful else return -ve value
1334  */
1335 int kfd_create_crat_image_virtual(void **crat_image, size_t *size,
1336 				  int flags, struct kfd_dev *kdev,
1337 				  uint32_t proximity_domain)
1338 {
1339 	void *pcrat_image = NULL;
1340 	int ret = 0;
1341 
1342 	if (!crat_image)
1343 		return -EINVAL;
1344 
1345 	*crat_image = NULL;
1346 
1347 	/* Allocate one VCRAT_SIZE_FOR_CPU for CPU virtual CRAT image and
1348 	 * VCRAT_SIZE_FOR_GPU for GPU virtual CRAT image. This should cover
1349 	 * all the current conditions. A check is put not to overwrite beyond
1350 	 * allocated size
1351 	 */
1352 	switch (flags) {
1353 	case COMPUTE_UNIT_CPU:
1354 		pcrat_image = kmalloc(VCRAT_SIZE_FOR_CPU, GFP_KERNEL);
1355 		if (!pcrat_image)
1356 			return -ENOMEM;
1357 		*size = VCRAT_SIZE_FOR_CPU;
1358 		ret = kfd_create_vcrat_image_cpu(pcrat_image, size);
1359 		break;
1360 	case COMPUTE_UNIT_GPU:
1361 		if (!kdev)
1362 			return -EINVAL;
1363 		pcrat_image = kmalloc(VCRAT_SIZE_FOR_GPU, GFP_KERNEL);
1364 		if (!pcrat_image)
1365 			return -ENOMEM;
1366 		*size = VCRAT_SIZE_FOR_GPU;
1367 		ret = kfd_create_vcrat_image_gpu(pcrat_image, size, kdev,
1368 						 proximity_domain);
1369 		break;
1370 	case (COMPUTE_UNIT_CPU | COMPUTE_UNIT_GPU):
1371 		/* TODO: */
1372 		ret = -EINVAL;
1373 		pr_err("VCRAT not implemented for APU\n");
1374 		break;
1375 	default:
1376 		ret = -EINVAL;
1377 	}
1378 
1379 	if (!ret)
1380 		*crat_image = pcrat_image;
1381 	else
1382 		kfree(pcrat_image);
1383 
1384 	return ret;
1385 }
1386 
1387 
1388 /* kfd_destroy_crat_image
1389  *
1390  *	@crat_image: [IN] - crat_image from kfd_create_crat_image_xxx(..)
1391  *
1392  */
1393 void kfd_destroy_crat_image(void *crat_image)
1394 {
1395 	kfree(crat_image);
1396 }
1397