xref: /openbmc/linux/drivers/gpu/drm/amd/amdkfd/kfd_crat.c (revision 828ff2ad)
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 /* TODO - check & update Vega10 cache details */
137 #define vega10_cache_info carrizo_cache_info
138 #define raven_cache_info carrizo_cache_info
139 
140 static void kfd_populated_cu_info_cpu(struct kfd_topology_device *dev,
141 		struct crat_subtype_computeunit *cu)
142 {
143 	dev->node_props.cpu_cores_count = cu->num_cpu_cores;
144 	dev->node_props.cpu_core_id_base = cu->processor_id_low;
145 	if (cu->hsa_capability & CRAT_CU_FLAGS_IOMMU_PRESENT)
146 		dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
147 
148 	pr_debug("CU CPU: cores=%d id_base=%d\n", cu->num_cpu_cores,
149 			cu->processor_id_low);
150 }
151 
152 static void kfd_populated_cu_info_gpu(struct kfd_topology_device *dev,
153 		struct crat_subtype_computeunit *cu)
154 {
155 	dev->node_props.simd_id_base = cu->processor_id_low;
156 	dev->node_props.simd_count = cu->num_simd_cores;
157 	dev->node_props.lds_size_in_kb = cu->lds_size_in_kb;
158 	dev->node_props.max_waves_per_simd = cu->max_waves_simd;
159 	dev->node_props.wave_front_size = cu->wave_front_size;
160 	dev->node_props.array_count = cu->array_count;
161 	dev->node_props.cu_per_simd_array = cu->num_cu_per_array;
162 	dev->node_props.simd_per_cu = cu->num_simd_per_cu;
163 	dev->node_props.max_slots_scratch_cu = cu->max_slots_scatch_cu;
164 	if (cu->hsa_capability & CRAT_CU_FLAGS_HOT_PLUGGABLE)
165 		dev->node_props.capability |= HSA_CAP_HOT_PLUGGABLE;
166 	pr_debug("CU GPU: id_base=%d\n", cu->processor_id_low);
167 }
168 
169 /* kfd_parse_subtype_cu - parse compute unit subtypes and attach it to correct
170  * topology device present in the device_list
171  */
172 static int kfd_parse_subtype_cu(struct crat_subtype_computeunit *cu,
173 				struct list_head *device_list)
174 {
175 	struct kfd_topology_device *dev;
176 
177 	pr_debug("Found CU entry in CRAT table with proximity_domain=%d caps=%x\n",
178 			cu->proximity_domain, cu->hsa_capability);
179 	list_for_each_entry(dev, device_list, list) {
180 		if (cu->proximity_domain == dev->proximity_domain) {
181 			if (cu->flags & CRAT_CU_FLAGS_CPU_PRESENT)
182 				kfd_populated_cu_info_cpu(dev, cu);
183 
184 			if (cu->flags & CRAT_CU_FLAGS_GPU_PRESENT)
185 				kfd_populated_cu_info_gpu(dev, cu);
186 			break;
187 		}
188 	}
189 
190 	return 0;
191 }
192 
193 static struct kfd_mem_properties *
194 find_subtype_mem(uint32_t heap_type, uint32_t flags, uint32_t width,
195 		struct kfd_topology_device *dev)
196 {
197 	struct kfd_mem_properties *props;
198 
199 	list_for_each_entry(props, &dev->mem_props, list) {
200 		if (props->heap_type == heap_type
201 				&& props->flags == flags
202 				&& props->width == width)
203 			return props;
204 	}
205 
206 	return NULL;
207 }
208 /* kfd_parse_subtype_mem - parse memory subtypes and attach it to correct
209  * topology device present in the device_list
210  */
211 static int kfd_parse_subtype_mem(struct crat_subtype_memory *mem,
212 				struct list_head *device_list)
213 {
214 	struct kfd_mem_properties *props;
215 	struct kfd_topology_device *dev;
216 	uint32_t heap_type;
217 	uint64_t size_in_bytes;
218 	uint32_t flags = 0;
219 	uint32_t width;
220 
221 	pr_debug("Found memory entry in CRAT table with proximity_domain=%d\n",
222 			mem->proximity_domain);
223 	list_for_each_entry(dev, device_list, list) {
224 		if (mem->proximity_domain == dev->proximity_domain) {
225 			/* We're on GPU node */
226 			if (dev->node_props.cpu_cores_count == 0) {
227 				/* APU */
228 				if (mem->visibility_type == 0)
229 					heap_type =
230 						HSA_MEM_HEAP_TYPE_FB_PRIVATE;
231 				/* dGPU */
232 				else
233 					heap_type = mem->visibility_type;
234 			} else
235 				heap_type = HSA_MEM_HEAP_TYPE_SYSTEM;
236 
237 			if (mem->flags & CRAT_MEM_FLAGS_HOT_PLUGGABLE)
238 				flags |= HSA_MEM_FLAGS_HOT_PLUGGABLE;
239 			if (mem->flags & CRAT_MEM_FLAGS_NON_VOLATILE)
240 				flags |= HSA_MEM_FLAGS_NON_VOLATILE;
241 
242 			size_in_bytes =
243 				((uint64_t)mem->length_high << 32) +
244 							mem->length_low;
245 			width = mem->width;
246 
247 			/* Multiple banks of the same type are aggregated into
248 			 * one. User mode doesn't care about multiple physical
249 			 * memory segments. It's managed as a single virtual
250 			 * heap for user mode.
251 			 */
252 			props = find_subtype_mem(heap_type, flags, width, dev);
253 			if (props) {
254 				props->size_in_bytes += size_in_bytes;
255 				break;
256 			}
257 
258 			props = kfd_alloc_struct(props);
259 			if (!props)
260 				return -ENOMEM;
261 
262 			props->heap_type = heap_type;
263 			props->flags = flags;
264 			props->size_in_bytes = size_in_bytes;
265 			props->width = width;
266 
267 			dev->node_props.mem_banks_count++;
268 			list_add_tail(&props->list, &dev->mem_props);
269 
270 			break;
271 		}
272 	}
273 
274 	return 0;
275 }
276 
277 /* kfd_parse_subtype_cache - parse cache subtypes and attach it to correct
278  * topology device present in the device_list
279  */
280 static int kfd_parse_subtype_cache(struct crat_subtype_cache *cache,
281 			struct list_head *device_list)
282 {
283 	struct kfd_cache_properties *props;
284 	struct kfd_topology_device *dev;
285 	uint32_t id;
286 	uint32_t total_num_of_cu;
287 
288 	id = cache->processor_id_low;
289 
290 	pr_debug("Found cache entry in CRAT table with processor_id=%d\n", id);
291 	list_for_each_entry(dev, device_list, list) {
292 		total_num_of_cu = (dev->node_props.array_count *
293 					dev->node_props.cu_per_simd_array);
294 
295 		/* Cache infomration in CRAT doesn't have proximity_domain
296 		 * information as it is associated with a CPU core or GPU
297 		 * Compute Unit. So map the cache using CPU core Id or SIMD
298 		 * (GPU) ID.
299 		 * TODO: This works because currently we can safely assume that
300 		 *  Compute Units are parsed before caches are parsed. In
301 		 *  future, remove this dependency
302 		 */
303 		if ((id >= dev->node_props.cpu_core_id_base &&
304 			id <= dev->node_props.cpu_core_id_base +
305 				dev->node_props.cpu_cores_count) ||
306 			(id >= dev->node_props.simd_id_base &&
307 			id < dev->node_props.simd_id_base +
308 				total_num_of_cu)) {
309 			props = kfd_alloc_struct(props);
310 			if (!props)
311 				return -ENOMEM;
312 
313 			props->processor_id_low = id;
314 			props->cache_level = cache->cache_level;
315 			props->cache_size = cache->cache_size;
316 			props->cacheline_size = cache->cache_line_size;
317 			props->cachelines_per_tag = cache->lines_per_tag;
318 			props->cache_assoc = cache->associativity;
319 			props->cache_latency = cache->cache_latency;
320 			memcpy(props->sibling_map, cache->sibling_map,
321 					sizeof(props->sibling_map));
322 
323 			if (cache->flags & CRAT_CACHE_FLAGS_DATA_CACHE)
324 				props->cache_type |= HSA_CACHE_TYPE_DATA;
325 			if (cache->flags & CRAT_CACHE_FLAGS_INST_CACHE)
326 				props->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
327 			if (cache->flags & CRAT_CACHE_FLAGS_CPU_CACHE)
328 				props->cache_type |= HSA_CACHE_TYPE_CPU;
329 			if (cache->flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
330 				props->cache_type |= HSA_CACHE_TYPE_HSACU;
331 
332 			dev->cache_count++;
333 			dev->node_props.caches_count++;
334 			list_add_tail(&props->list, &dev->cache_props);
335 
336 			break;
337 		}
338 	}
339 
340 	return 0;
341 }
342 
343 /* kfd_parse_subtype_iolink - parse iolink subtypes and attach it to correct
344  * topology device present in the device_list
345  */
346 static int kfd_parse_subtype_iolink(struct crat_subtype_iolink *iolink,
347 					struct list_head *device_list)
348 {
349 	struct kfd_iolink_properties *props = NULL, *props2;
350 	struct kfd_topology_device *dev, *to_dev;
351 	uint32_t id_from;
352 	uint32_t id_to;
353 
354 	id_from = iolink->proximity_domain_from;
355 	id_to = iolink->proximity_domain_to;
356 
357 	pr_debug("Found IO link entry in CRAT table with id_from=%d, id_to %d\n",
358 			id_from, id_to);
359 	list_for_each_entry(dev, device_list, list) {
360 		if (id_from == dev->proximity_domain) {
361 			props = kfd_alloc_struct(props);
362 			if (!props)
363 				return -ENOMEM;
364 
365 			props->node_from = id_from;
366 			props->node_to = id_to;
367 			props->ver_maj = iolink->version_major;
368 			props->ver_min = iolink->version_minor;
369 			props->iolink_type = iolink->io_interface_type;
370 
371 			if (props->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
372 				props->weight = 20;
373 			else if (props->iolink_type == CRAT_IOLINK_TYPE_XGMI)
374 				props->weight = 15;
375 			else
376 				props->weight = node_distance(id_from, id_to);
377 
378 			props->min_latency = iolink->minimum_latency;
379 			props->max_latency = iolink->maximum_latency;
380 			props->min_bandwidth = iolink->minimum_bandwidth_mbs;
381 			props->max_bandwidth = iolink->maximum_bandwidth_mbs;
382 			props->rec_transfer_size =
383 					iolink->recommended_transfer_size;
384 
385 			dev->io_link_count++;
386 			dev->node_props.io_links_count++;
387 			list_add_tail(&props->list, &dev->io_link_props);
388 			break;
389 		}
390 	}
391 
392 	/* CPU topology is created before GPUs are detected, so CPU->GPU
393 	 * links are not built at that time. If a PCIe type is discovered, it
394 	 * means a GPU is detected and we are adding GPU->CPU to the topology.
395 	 * At this time, also add the corresponded CPU->GPU link if GPU
396 	 * is large bar.
397 	 * For xGMI, we only added the link with one direction in the crat
398 	 * table, add corresponded reversed direction link now.
399 	 */
400 	if (props && (iolink->flags & CRAT_IOLINK_FLAGS_BI_DIRECTIONAL)) {
401 		to_dev = kfd_topology_device_by_proximity_domain(id_to);
402 		if (!to_dev)
403 			return -ENODEV;
404 		/* same everything but the other direction */
405 		props2 = kmemdup(props, sizeof(*props2), GFP_KERNEL);
406 		props2->node_from = id_to;
407 		props2->node_to = id_from;
408 		props2->kobj = NULL;
409 		to_dev->io_link_count++;
410 		to_dev->node_props.io_links_count++;
411 		list_add_tail(&props2->list, &to_dev->io_link_props);
412 	}
413 
414 	return 0;
415 }
416 
417 /* kfd_parse_subtype - parse subtypes and attach it to correct topology device
418  * present in the device_list
419  *	@sub_type_hdr - subtype section of crat_image
420  *	@device_list - list of topology devices present in this crat_image
421  */
422 static int kfd_parse_subtype(struct crat_subtype_generic *sub_type_hdr,
423 				struct list_head *device_list)
424 {
425 	struct crat_subtype_computeunit *cu;
426 	struct crat_subtype_memory *mem;
427 	struct crat_subtype_cache *cache;
428 	struct crat_subtype_iolink *iolink;
429 	int ret = 0;
430 
431 	switch (sub_type_hdr->type) {
432 	case CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY:
433 		cu = (struct crat_subtype_computeunit *)sub_type_hdr;
434 		ret = kfd_parse_subtype_cu(cu, device_list);
435 		break;
436 	case CRAT_SUBTYPE_MEMORY_AFFINITY:
437 		mem = (struct crat_subtype_memory *)sub_type_hdr;
438 		ret = kfd_parse_subtype_mem(mem, device_list);
439 		break;
440 	case CRAT_SUBTYPE_CACHE_AFFINITY:
441 		cache = (struct crat_subtype_cache *)sub_type_hdr;
442 		ret = kfd_parse_subtype_cache(cache, device_list);
443 		break;
444 	case CRAT_SUBTYPE_TLB_AFFINITY:
445 		/*
446 		 * For now, nothing to do here
447 		 */
448 		pr_debug("Found TLB entry in CRAT table (not processing)\n");
449 		break;
450 	case CRAT_SUBTYPE_CCOMPUTE_AFFINITY:
451 		/*
452 		 * For now, nothing to do here
453 		 */
454 		pr_debug("Found CCOMPUTE entry in CRAT table (not processing)\n");
455 		break;
456 	case CRAT_SUBTYPE_IOLINK_AFFINITY:
457 		iolink = (struct crat_subtype_iolink *)sub_type_hdr;
458 		ret = kfd_parse_subtype_iolink(iolink, device_list);
459 		break;
460 	default:
461 		pr_warn("Unknown subtype %d in CRAT\n",
462 				sub_type_hdr->type);
463 	}
464 
465 	return ret;
466 }
467 
468 /* kfd_parse_crat_table - parse CRAT table. For each node present in CRAT
469  * create a kfd_topology_device and add in to device_list. Also parse
470  * CRAT subtypes and attach it to appropriate kfd_topology_device
471  *	@crat_image - input image containing CRAT
472  *	@device_list - [OUT] list of kfd_topology_device generated after
473  *		       parsing crat_image
474  *	@proximity_domain - Proximity domain of the first device in the table
475  *
476  *	Return - 0 if successful else -ve value
477  */
478 int kfd_parse_crat_table(void *crat_image, struct list_head *device_list,
479 			 uint32_t proximity_domain)
480 {
481 	struct kfd_topology_device *top_dev = NULL;
482 	struct crat_subtype_generic *sub_type_hdr;
483 	uint16_t node_id;
484 	int ret = 0;
485 	struct crat_header *crat_table = (struct crat_header *)crat_image;
486 	uint16_t num_nodes;
487 	uint32_t image_len;
488 
489 	if (!crat_image)
490 		return -EINVAL;
491 
492 	if (!list_empty(device_list)) {
493 		pr_warn("Error device list should be empty\n");
494 		return -EINVAL;
495 	}
496 
497 	num_nodes = crat_table->num_domains;
498 	image_len = crat_table->length;
499 
500 	pr_info("Parsing CRAT table with %d nodes\n", num_nodes);
501 
502 	for (node_id = 0; node_id < num_nodes; node_id++) {
503 		top_dev = kfd_create_topology_device(device_list);
504 		if (!top_dev)
505 			break;
506 		top_dev->proximity_domain = proximity_domain++;
507 	}
508 
509 	if (!top_dev) {
510 		ret = -ENOMEM;
511 		goto err;
512 	}
513 
514 	memcpy(top_dev->oem_id, crat_table->oem_id, CRAT_OEMID_LENGTH);
515 	memcpy(top_dev->oem_table_id, crat_table->oem_table_id,
516 			CRAT_OEMTABLEID_LENGTH);
517 	top_dev->oem_revision = crat_table->oem_revision;
518 
519 	sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
520 	while ((char *)sub_type_hdr + sizeof(struct crat_subtype_generic) <
521 			((char *)crat_image) + image_len) {
522 		if (sub_type_hdr->flags & CRAT_SUBTYPE_FLAGS_ENABLED) {
523 			ret = kfd_parse_subtype(sub_type_hdr, device_list);
524 			if (ret)
525 				break;
526 		}
527 
528 		sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
529 				sub_type_hdr->length);
530 	}
531 
532 err:
533 	if (ret)
534 		kfd_release_topology_device_list(device_list);
535 
536 	return ret;
537 }
538 
539 /* Helper function. See kfd_fill_gpu_cache_info for parameter description */
540 static int fill_in_pcache(struct crat_subtype_cache *pcache,
541 				struct kfd_gpu_cache_info *pcache_info,
542 				struct kfd_cu_info *cu_info,
543 				int mem_available,
544 				int cu_bitmask,
545 				int cache_type, unsigned int cu_processor_id,
546 				int cu_block)
547 {
548 	unsigned int cu_sibling_map_mask;
549 	int first_active_cu;
550 
551 	/* First check if enough memory is available */
552 	if (sizeof(struct crat_subtype_cache) > mem_available)
553 		return -ENOMEM;
554 
555 	cu_sibling_map_mask = cu_bitmask;
556 	cu_sibling_map_mask >>= cu_block;
557 	cu_sibling_map_mask &=
558 		((1 << pcache_info[cache_type].num_cu_shared) - 1);
559 	first_active_cu = ffs(cu_sibling_map_mask);
560 
561 	/* CU could be inactive. In case of shared cache find the first active
562 	 * CU. and incase of non-shared cache check if the CU is inactive. If
563 	 * inactive active skip it
564 	 */
565 	if (first_active_cu) {
566 		memset(pcache, 0, sizeof(struct crat_subtype_cache));
567 		pcache->type = CRAT_SUBTYPE_CACHE_AFFINITY;
568 		pcache->length = sizeof(struct crat_subtype_cache);
569 		pcache->flags = pcache_info[cache_type].flags;
570 		pcache->processor_id_low = cu_processor_id
571 					 + (first_active_cu - 1);
572 		pcache->cache_level = pcache_info[cache_type].cache_level;
573 		pcache->cache_size = pcache_info[cache_type].cache_size;
574 
575 		/* Sibling map is w.r.t processor_id_low, so shift out
576 		 * inactive CU
577 		 */
578 		cu_sibling_map_mask =
579 			cu_sibling_map_mask >> (first_active_cu - 1);
580 
581 		pcache->sibling_map[0] = (uint8_t)(cu_sibling_map_mask & 0xFF);
582 		pcache->sibling_map[1] =
583 				(uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
584 		pcache->sibling_map[2] =
585 				(uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
586 		pcache->sibling_map[3] =
587 				(uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
588 		return 0;
589 	}
590 	return 1;
591 }
592 
593 /* kfd_fill_gpu_cache_info - Fill GPU cache info using kfd_gpu_cache_info
594  * tables
595  *
596  *	@kdev - [IN] GPU device
597  *	@gpu_processor_id - [IN] GPU processor ID to which these caches
598  *			    associate
599  *	@available_size - [IN] Amount of memory available in pcache
600  *	@cu_info - [IN] Compute Unit info obtained from KGD
601  *	@pcache - [OUT] memory into which cache data is to be filled in.
602  *	@size_filled - [OUT] amount of data used up in pcache.
603  *	@num_of_entries - [OUT] number of caches added
604  */
605 static int kfd_fill_gpu_cache_info(struct kfd_dev *kdev,
606 			int gpu_processor_id,
607 			int available_size,
608 			struct kfd_cu_info *cu_info,
609 			struct crat_subtype_cache *pcache,
610 			int *size_filled,
611 			int *num_of_entries)
612 {
613 	struct kfd_gpu_cache_info *pcache_info;
614 	int num_of_cache_types = 0;
615 	int i, j, k;
616 	int ct = 0;
617 	int mem_available = available_size;
618 	unsigned int cu_processor_id;
619 	int ret;
620 
621 	switch (kdev->device_info->asic_family) {
622 	case CHIP_KAVERI:
623 		pcache_info = kaveri_cache_info;
624 		num_of_cache_types = ARRAY_SIZE(kaveri_cache_info);
625 		break;
626 	case CHIP_HAWAII:
627 		pcache_info = hawaii_cache_info;
628 		num_of_cache_types = ARRAY_SIZE(hawaii_cache_info);
629 		break;
630 	case CHIP_CARRIZO:
631 		pcache_info = carrizo_cache_info;
632 		num_of_cache_types = ARRAY_SIZE(carrizo_cache_info);
633 		break;
634 	case CHIP_TONGA:
635 		pcache_info = tonga_cache_info;
636 		num_of_cache_types = ARRAY_SIZE(tonga_cache_info);
637 		break;
638 	case CHIP_FIJI:
639 		pcache_info = fiji_cache_info;
640 		num_of_cache_types = ARRAY_SIZE(fiji_cache_info);
641 		break;
642 	case CHIP_POLARIS10:
643 		pcache_info = polaris10_cache_info;
644 		num_of_cache_types = ARRAY_SIZE(polaris10_cache_info);
645 		break;
646 	case CHIP_POLARIS11:
647 		pcache_info = polaris11_cache_info;
648 		num_of_cache_types = ARRAY_SIZE(polaris11_cache_info);
649 		break;
650 	case CHIP_VEGA10:
651 	case CHIP_VEGA20:
652 		pcache_info = vega10_cache_info;
653 		num_of_cache_types = ARRAY_SIZE(vega10_cache_info);
654 		break;
655 	case CHIP_RAVEN:
656 		pcache_info = raven_cache_info;
657 		num_of_cache_types = ARRAY_SIZE(raven_cache_info);
658 		break;
659 	default:
660 		return -EINVAL;
661 	}
662 
663 	*size_filled = 0;
664 	*num_of_entries = 0;
665 
666 	/* For each type of cache listed in the kfd_gpu_cache_info table,
667 	 * go through all available Compute Units.
668 	 * The [i,j,k] loop will
669 	 *		if kfd_gpu_cache_info.num_cu_shared = 1
670 	 *			will parse through all available CU
671 	 *		If (kfd_gpu_cache_info.num_cu_shared != 1)
672 	 *			then it will consider only one CU from
673 	 *			the shared unit
674 	 */
675 
676 	for (ct = 0; ct < num_of_cache_types; ct++) {
677 		cu_processor_id = gpu_processor_id;
678 		for (i = 0; i < cu_info->num_shader_engines; i++) {
679 			for (j = 0; j < cu_info->num_shader_arrays_per_engine;
680 				j++) {
681 				for (k = 0; k < cu_info->num_cu_per_sh;
682 					k += pcache_info[ct].num_cu_shared) {
683 
684 					ret = fill_in_pcache(pcache,
685 						pcache_info,
686 						cu_info,
687 						mem_available,
688 						cu_info->cu_bitmap[i][j],
689 						ct,
690 						cu_processor_id,
691 						k);
692 
693 					if (ret < 0)
694 						break;
695 
696 					if (!ret) {
697 						pcache++;
698 						(*num_of_entries)++;
699 						mem_available -=
700 							sizeof(*pcache);
701 						(*size_filled) +=
702 							sizeof(*pcache);
703 					}
704 
705 					/* Move to next CU block */
706 					cu_processor_id +=
707 						pcache_info[ct].num_cu_shared;
708 				}
709 			}
710 		}
711 	}
712 
713 	pr_debug("Added [%d] GPU cache entries\n", *num_of_entries);
714 
715 	return 0;
716 }
717 
718 /*
719  * kfd_create_crat_image_acpi - Allocates memory for CRAT image and
720  * copies CRAT from ACPI (if available).
721  * NOTE: Call kfd_destroy_crat_image to free CRAT image memory
722  *
723  *	@crat_image: CRAT read from ACPI. If no CRAT in ACPI then
724  *		     crat_image will be NULL
725  *	@size: [OUT] size of crat_image
726  *
727  *	Return 0 if successful else return error code
728  */
729 int kfd_create_crat_image_acpi(void **crat_image, size_t *size)
730 {
731 	struct acpi_table_header *crat_table;
732 	acpi_status status;
733 	void *pcrat_image;
734 
735 	if (!crat_image)
736 		return -EINVAL;
737 
738 	*crat_image = NULL;
739 
740 	/* Fetch the CRAT table from ACPI */
741 	status = acpi_get_table(CRAT_SIGNATURE, 0, &crat_table);
742 	if (status == AE_NOT_FOUND) {
743 		pr_warn("CRAT table not found\n");
744 		return -ENODATA;
745 	} else if (ACPI_FAILURE(status)) {
746 		const char *err = acpi_format_exception(status);
747 
748 		pr_err("CRAT table error: %s\n", err);
749 		return -EINVAL;
750 	}
751 
752 	if (ignore_crat) {
753 		pr_info("CRAT table disabled by module option\n");
754 		return -ENODATA;
755 	}
756 
757 	pcrat_image = kmemdup(crat_table, crat_table->length, GFP_KERNEL);
758 	if (!pcrat_image)
759 		return -ENOMEM;
760 
761 	*crat_image = pcrat_image;
762 	*size = crat_table->length;
763 
764 	return 0;
765 }
766 
767 /* Memory required to create Virtual CRAT.
768  * Since there is no easy way to predict the amount of memory required, the
769  * following amount are allocated for CPU and GPU Virtual CRAT. This is
770  * expected to cover all known conditions. But to be safe additional check
771  * is put in the code to ensure we don't overwrite.
772  */
773 #define VCRAT_SIZE_FOR_CPU	(2 * PAGE_SIZE)
774 #define VCRAT_SIZE_FOR_GPU	(3 * PAGE_SIZE)
775 
776 /* kfd_fill_cu_for_cpu - Fill in Compute info for the given CPU NUMA node
777  *
778  *	@numa_node_id: CPU NUMA node id
779  *	@avail_size: Available size in the memory
780  *	@sub_type_hdr: Memory into which compute info will be filled in
781  *
782  *	Return 0 if successful else return -ve value
783  */
784 static int kfd_fill_cu_for_cpu(int numa_node_id, int *avail_size,
785 				int proximity_domain,
786 				struct crat_subtype_computeunit *sub_type_hdr)
787 {
788 	const struct cpumask *cpumask;
789 
790 	*avail_size -= sizeof(struct crat_subtype_computeunit);
791 	if (*avail_size < 0)
792 		return -ENOMEM;
793 
794 	memset(sub_type_hdr, 0, sizeof(struct crat_subtype_computeunit));
795 
796 	/* Fill in subtype header data */
797 	sub_type_hdr->type = CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY;
798 	sub_type_hdr->length = sizeof(struct crat_subtype_computeunit);
799 	sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
800 
801 	cpumask = cpumask_of_node(numa_node_id);
802 
803 	/* Fill in CU data */
804 	sub_type_hdr->flags |= CRAT_CU_FLAGS_CPU_PRESENT;
805 	sub_type_hdr->proximity_domain = proximity_domain;
806 	sub_type_hdr->processor_id_low = kfd_numa_node_to_apic_id(numa_node_id);
807 	if (sub_type_hdr->processor_id_low == -1)
808 		return -EINVAL;
809 
810 	sub_type_hdr->num_cpu_cores = cpumask_weight(cpumask);
811 
812 	return 0;
813 }
814 
815 /* kfd_fill_mem_info_for_cpu - Fill in Memory info for the given CPU NUMA node
816  *
817  *	@numa_node_id: CPU NUMA node id
818  *	@avail_size: Available size in the memory
819  *	@sub_type_hdr: Memory into which compute info will be filled in
820  *
821  *	Return 0 if successful else return -ve value
822  */
823 static int kfd_fill_mem_info_for_cpu(int numa_node_id, int *avail_size,
824 			int proximity_domain,
825 			struct crat_subtype_memory *sub_type_hdr)
826 {
827 	uint64_t mem_in_bytes = 0;
828 	pg_data_t *pgdat;
829 	int zone_type;
830 
831 	*avail_size -= sizeof(struct crat_subtype_memory);
832 	if (*avail_size < 0)
833 		return -ENOMEM;
834 
835 	memset(sub_type_hdr, 0, sizeof(struct crat_subtype_memory));
836 
837 	/* Fill in subtype header data */
838 	sub_type_hdr->type = CRAT_SUBTYPE_MEMORY_AFFINITY;
839 	sub_type_hdr->length = sizeof(struct crat_subtype_memory);
840 	sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
841 
842 	/* Fill in Memory Subunit data */
843 
844 	/* Unlike si_meminfo, si_meminfo_node is not exported. So
845 	 * the following lines are duplicated from si_meminfo_node
846 	 * function
847 	 */
848 	pgdat = NODE_DATA(numa_node_id);
849 	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
850 		mem_in_bytes += pgdat->node_zones[zone_type].managed_pages;
851 	mem_in_bytes <<= PAGE_SHIFT;
852 
853 	sub_type_hdr->length_low = lower_32_bits(mem_in_bytes);
854 	sub_type_hdr->length_high = upper_32_bits(mem_in_bytes);
855 	sub_type_hdr->proximity_domain = proximity_domain;
856 
857 	return 0;
858 }
859 
860 static int kfd_fill_iolink_info_for_cpu(int numa_node_id, int *avail_size,
861 				uint32_t *num_entries,
862 				struct crat_subtype_iolink *sub_type_hdr)
863 {
864 	int nid;
865 	struct cpuinfo_x86 *c = &cpu_data(0);
866 	uint8_t link_type;
867 
868 	if (c->x86_vendor == X86_VENDOR_AMD)
869 		link_type = CRAT_IOLINK_TYPE_HYPERTRANSPORT;
870 	else
871 		link_type = CRAT_IOLINK_TYPE_QPI_1_1;
872 
873 	*num_entries = 0;
874 
875 	/* Create IO links from this node to other CPU nodes */
876 	for_each_online_node(nid) {
877 		if (nid == numa_node_id) /* node itself */
878 			continue;
879 
880 		*avail_size -= sizeof(struct crat_subtype_iolink);
881 		if (*avail_size < 0)
882 			return -ENOMEM;
883 
884 		memset(sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
885 
886 		/* Fill in subtype header data */
887 		sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
888 		sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
889 		sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
890 
891 		/* Fill in IO link data */
892 		sub_type_hdr->proximity_domain_from = numa_node_id;
893 		sub_type_hdr->proximity_domain_to = nid;
894 		sub_type_hdr->io_interface_type = link_type;
895 
896 		(*num_entries)++;
897 		sub_type_hdr++;
898 	}
899 
900 	return 0;
901 }
902 
903 /* kfd_create_vcrat_image_cpu - Create Virtual CRAT for CPU
904  *
905  *	@pcrat_image: Fill in VCRAT for CPU
906  *	@size:	[IN] allocated size of crat_image.
907  *		[OUT] actual size of data filled in crat_image
908  */
909 static int kfd_create_vcrat_image_cpu(void *pcrat_image, size_t *size)
910 {
911 	struct crat_header *crat_table = (struct crat_header *)pcrat_image;
912 	struct acpi_table_header *acpi_table;
913 	acpi_status status;
914 	struct crat_subtype_generic *sub_type_hdr;
915 	int avail_size = *size;
916 	int numa_node_id;
917 	uint32_t entries = 0;
918 	int ret = 0;
919 
920 	if (!pcrat_image || avail_size < VCRAT_SIZE_FOR_CPU)
921 		return -EINVAL;
922 
923 	/* Fill in CRAT Header.
924 	 * Modify length and total_entries as subunits are added.
925 	 */
926 	avail_size -= sizeof(struct crat_header);
927 	if (avail_size < 0)
928 		return -ENOMEM;
929 
930 	memset(crat_table, 0, sizeof(struct crat_header));
931 	memcpy(&crat_table->signature, CRAT_SIGNATURE,
932 			sizeof(crat_table->signature));
933 	crat_table->length = sizeof(struct crat_header);
934 
935 	status = acpi_get_table("DSDT", 0, &acpi_table);
936 	if (status != AE_OK)
937 		pr_warn("DSDT table not found for OEM information\n");
938 	else {
939 		crat_table->oem_revision = acpi_table->revision;
940 		memcpy(crat_table->oem_id, acpi_table->oem_id,
941 				CRAT_OEMID_LENGTH);
942 		memcpy(crat_table->oem_table_id, acpi_table->oem_table_id,
943 				CRAT_OEMTABLEID_LENGTH);
944 	}
945 	crat_table->total_entries = 0;
946 	crat_table->num_domains = 0;
947 
948 	sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
949 
950 	for_each_online_node(numa_node_id) {
951 		if (kfd_numa_node_to_apic_id(numa_node_id) == -1)
952 			continue;
953 
954 		/* Fill in Subtype: Compute Unit */
955 		ret = kfd_fill_cu_for_cpu(numa_node_id, &avail_size,
956 			crat_table->num_domains,
957 			(struct crat_subtype_computeunit *)sub_type_hdr);
958 		if (ret < 0)
959 			return ret;
960 		crat_table->length += sub_type_hdr->length;
961 		crat_table->total_entries++;
962 
963 		sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
964 			sub_type_hdr->length);
965 
966 		/* Fill in Subtype: Memory */
967 		ret = kfd_fill_mem_info_for_cpu(numa_node_id, &avail_size,
968 			crat_table->num_domains,
969 			(struct crat_subtype_memory *)sub_type_hdr);
970 		if (ret < 0)
971 			return ret;
972 		crat_table->length += sub_type_hdr->length;
973 		crat_table->total_entries++;
974 
975 		sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
976 			sub_type_hdr->length);
977 
978 		/* Fill in Subtype: IO Link */
979 		ret = kfd_fill_iolink_info_for_cpu(numa_node_id, &avail_size,
980 				&entries,
981 				(struct crat_subtype_iolink *)sub_type_hdr);
982 		if (ret < 0)
983 			return ret;
984 		crat_table->length += (sub_type_hdr->length * entries);
985 		crat_table->total_entries += entries;
986 
987 		sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
988 				sub_type_hdr->length * entries);
989 
990 		crat_table->num_domains++;
991 	}
992 
993 	/* TODO: Add cache Subtype for CPU.
994 	 * Currently, CPU cache information is available in function
995 	 * detect_cache_attributes(cpu) defined in the file
996 	 * ./arch/x86/kernel/cpu/intel_cacheinfo.c. This function is not
997 	 * exported and to get the same information the code needs to be
998 	 * duplicated.
999 	 */
1000 
1001 	*size = crat_table->length;
1002 	pr_info("Virtual CRAT table created for CPU\n");
1003 
1004 	return 0;
1005 }
1006 
1007 static int kfd_fill_gpu_memory_affinity(int *avail_size,
1008 		struct kfd_dev *kdev, uint8_t type, uint64_t size,
1009 		struct crat_subtype_memory *sub_type_hdr,
1010 		uint32_t proximity_domain,
1011 		const struct kfd_local_mem_info *local_mem_info)
1012 {
1013 	*avail_size -= sizeof(struct crat_subtype_memory);
1014 	if (*avail_size < 0)
1015 		return -ENOMEM;
1016 
1017 	memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_memory));
1018 	sub_type_hdr->type = CRAT_SUBTYPE_MEMORY_AFFINITY;
1019 	sub_type_hdr->length = sizeof(struct crat_subtype_memory);
1020 	sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED;
1021 
1022 	sub_type_hdr->proximity_domain = proximity_domain;
1023 
1024 	pr_debug("Fill gpu memory affinity - type 0x%x size 0x%llx\n",
1025 			type, size);
1026 
1027 	sub_type_hdr->length_low = lower_32_bits(size);
1028 	sub_type_hdr->length_high = upper_32_bits(size);
1029 
1030 	sub_type_hdr->width = local_mem_info->vram_width;
1031 	sub_type_hdr->visibility_type = type;
1032 
1033 	return 0;
1034 }
1035 
1036 /* kfd_fill_gpu_direct_io_link - Fill in direct io link from GPU
1037  * to its NUMA node
1038  *	@avail_size: Available size in the memory
1039  *	@kdev - [IN] GPU device
1040  *	@sub_type_hdr: Memory into which io link info will be filled in
1041  *	@proximity_domain - proximity domain of the GPU node
1042  *
1043  *	Return 0 if successful else return -ve value
1044  */
1045 static int kfd_fill_gpu_direct_io_link_to_cpu(int *avail_size,
1046 			struct kfd_dev *kdev,
1047 			struct crat_subtype_iolink *sub_type_hdr,
1048 			uint32_t proximity_domain)
1049 {
1050 	*avail_size -= sizeof(struct crat_subtype_iolink);
1051 	if (*avail_size < 0)
1052 		return -ENOMEM;
1053 
1054 	memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
1055 
1056 	/* Fill in subtype header data */
1057 	sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
1058 	sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
1059 	sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED;
1060 	if (kfd_dev_is_large_bar(kdev))
1061 		sub_type_hdr->flags |= CRAT_IOLINK_FLAGS_BI_DIRECTIONAL;
1062 
1063 	/* Fill in IOLINK subtype.
1064 	 * TODO: Fill-in other fields of iolink subtype
1065 	 */
1066 	sub_type_hdr->io_interface_type = CRAT_IOLINK_TYPE_PCIEXPRESS;
1067 	sub_type_hdr->proximity_domain_from = proximity_domain;
1068 #ifdef CONFIG_NUMA
1069 	if (kdev->pdev->dev.numa_node == NUMA_NO_NODE)
1070 		sub_type_hdr->proximity_domain_to = 0;
1071 	else
1072 		sub_type_hdr->proximity_domain_to = kdev->pdev->dev.numa_node;
1073 #else
1074 	sub_type_hdr->proximity_domain_to = 0;
1075 #endif
1076 	return 0;
1077 }
1078 
1079 static int kfd_fill_gpu_xgmi_link_to_gpu(int *avail_size,
1080 			struct kfd_dev *kdev,
1081 			struct crat_subtype_iolink *sub_type_hdr,
1082 			uint32_t proximity_domain_from,
1083 			uint32_t proximity_domain_to)
1084 {
1085 	*avail_size -= sizeof(struct crat_subtype_iolink);
1086 	if (*avail_size < 0)
1087 		return -ENOMEM;
1088 
1089 	memset((void *)sub_type_hdr, 0, sizeof(struct crat_subtype_iolink));
1090 
1091 	sub_type_hdr->type = CRAT_SUBTYPE_IOLINK_AFFINITY;
1092 	sub_type_hdr->length = sizeof(struct crat_subtype_iolink);
1093 	sub_type_hdr->flags |= CRAT_SUBTYPE_FLAGS_ENABLED |
1094 			       CRAT_IOLINK_FLAGS_BI_DIRECTIONAL;
1095 
1096 	sub_type_hdr->io_interface_type = CRAT_IOLINK_TYPE_XGMI;
1097 	sub_type_hdr->proximity_domain_from = proximity_domain_from;
1098 	sub_type_hdr->proximity_domain_to = proximity_domain_to;
1099 	return 0;
1100 }
1101 
1102 /* kfd_create_vcrat_image_gpu - Create Virtual CRAT for CPU
1103  *
1104  *	@pcrat_image: Fill in VCRAT for GPU
1105  *	@size:	[IN] allocated size of crat_image.
1106  *		[OUT] actual size of data filled in crat_image
1107  */
1108 static int kfd_create_vcrat_image_gpu(void *pcrat_image,
1109 				      size_t *size, struct kfd_dev *kdev,
1110 				      uint32_t proximity_domain)
1111 {
1112 	struct crat_header *crat_table = (struct crat_header *)pcrat_image;
1113 	struct crat_subtype_generic *sub_type_hdr;
1114 	struct kfd_local_mem_info local_mem_info;
1115 	struct kfd_topology_device *peer_dev;
1116 	struct crat_subtype_computeunit *cu;
1117 	struct kfd_cu_info cu_info;
1118 	int avail_size = *size;
1119 	uint32_t total_num_of_cu;
1120 	int num_of_cache_entries = 0;
1121 	int cache_mem_filled = 0;
1122 	uint32_t nid = 0;
1123 	int ret = 0;
1124 
1125 	if (!pcrat_image || avail_size < VCRAT_SIZE_FOR_GPU)
1126 		return -EINVAL;
1127 
1128 	/* Fill the CRAT Header.
1129 	 * Modify length and total_entries as subunits are added.
1130 	 */
1131 	avail_size -= sizeof(struct crat_header);
1132 	if (avail_size < 0)
1133 		return -ENOMEM;
1134 
1135 	memset(crat_table, 0, sizeof(struct crat_header));
1136 
1137 	memcpy(&crat_table->signature, CRAT_SIGNATURE,
1138 			sizeof(crat_table->signature));
1139 	/* Change length as we add more subtypes*/
1140 	crat_table->length = sizeof(struct crat_header);
1141 	crat_table->num_domains = 1;
1142 	crat_table->total_entries = 0;
1143 
1144 	/* Fill in Subtype: Compute Unit
1145 	 * First fill in the sub type header and then sub type data
1146 	 */
1147 	avail_size -= sizeof(struct crat_subtype_computeunit);
1148 	if (avail_size < 0)
1149 		return -ENOMEM;
1150 
1151 	sub_type_hdr = (struct crat_subtype_generic *)(crat_table + 1);
1152 	memset(sub_type_hdr, 0, sizeof(struct crat_subtype_computeunit));
1153 
1154 	sub_type_hdr->type = CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY;
1155 	sub_type_hdr->length = sizeof(struct crat_subtype_computeunit);
1156 	sub_type_hdr->flags = CRAT_SUBTYPE_FLAGS_ENABLED;
1157 
1158 	/* Fill CU subtype data */
1159 	cu = (struct crat_subtype_computeunit *)sub_type_hdr;
1160 	cu->flags |= CRAT_CU_FLAGS_GPU_PRESENT;
1161 	cu->proximity_domain = proximity_domain;
1162 
1163 	amdgpu_amdkfd_get_cu_info(kdev->kgd, &cu_info);
1164 	cu->num_simd_per_cu = cu_info.simd_per_cu;
1165 	cu->num_simd_cores = cu_info.simd_per_cu * cu_info.cu_active_number;
1166 	cu->max_waves_simd = cu_info.max_waves_per_simd;
1167 
1168 	cu->wave_front_size = cu_info.wave_front_size;
1169 	cu->array_count = cu_info.num_shader_arrays_per_engine *
1170 		cu_info.num_shader_engines;
1171 	total_num_of_cu = (cu->array_count * cu_info.num_cu_per_sh);
1172 	cu->processor_id_low = get_and_inc_gpu_processor_id(total_num_of_cu);
1173 	cu->num_cu_per_array = cu_info.num_cu_per_sh;
1174 	cu->max_slots_scatch_cu = cu_info.max_scratch_slots_per_cu;
1175 	cu->num_banks = cu_info.num_shader_engines;
1176 	cu->lds_size_in_kb = cu_info.lds_size;
1177 
1178 	cu->hsa_capability = 0;
1179 
1180 	/* Check if this node supports IOMMU. During parsing this flag will
1181 	 * translate to HSA_CAP_ATS_PRESENT
1182 	 */
1183 	if (!kfd_iommu_check_device(kdev))
1184 		cu->hsa_capability |= CRAT_CU_FLAGS_IOMMU_PRESENT;
1185 
1186 	crat_table->length += sub_type_hdr->length;
1187 	crat_table->total_entries++;
1188 
1189 	/* Fill in Subtype: Memory. Only on systems with large BAR (no
1190 	 * private FB), report memory as public. On other systems
1191 	 * report the total FB size (public+private) as a single
1192 	 * private heap.
1193 	 */
1194 	amdgpu_amdkfd_get_local_mem_info(kdev->kgd, &local_mem_info);
1195 	sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1196 			sub_type_hdr->length);
1197 
1198 	if (debug_largebar)
1199 		local_mem_info.local_mem_size_private = 0;
1200 
1201 	if (local_mem_info.local_mem_size_private == 0)
1202 		ret = kfd_fill_gpu_memory_affinity(&avail_size,
1203 				kdev, HSA_MEM_HEAP_TYPE_FB_PUBLIC,
1204 				local_mem_info.local_mem_size_public,
1205 				(struct crat_subtype_memory *)sub_type_hdr,
1206 				proximity_domain,
1207 				&local_mem_info);
1208 	else
1209 		ret = kfd_fill_gpu_memory_affinity(&avail_size,
1210 				kdev, HSA_MEM_HEAP_TYPE_FB_PRIVATE,
1211 				local_mem_info.local_mem_size_public +
1212 				local_mem_info.local_mem_size_private,
1213 				(struct crat_subtype_memory *)sub_type_hdr,
1214 				proximity_domain,
1215 				&local_mem_info);
1216 	if (ret < 0)
1217 		return ret;
1218 
1219 	crat_table->length += sizeof(struct crat_subtype_memory);
1220 	crat_table->total_entries++;
1221 
1222 	/* TODO: Fill in cache information. This information is NOT readily
1223 	 * available in KGD
1224 	 */
1225 	sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1226 		sub_type_hdr->length);
1227 	ret = kfd_fill_gpu_cache_info(kdev, cu->processor_id_low,
1228 				avail_size,
1229 				&cu_info,
1230 				(struct crat_subtype_cache *)sub_type_hdr,
1231 				&cache_mem_filled,
1232 				&num_of_cache_entries);
1233 
1234 	if (ret < 0)
1235 		return ret;
1236 
1237 	crat_table->length += cache_mem_filled;
1238 	crat_table->total_entries += num_of_cache_entries;
1239 	avail_size -= cache_mem_filled;
1240 
1241 	/* Fill in Subtype: IO_LINKS
1242 	 *  Only direct links are added here which is Link from GPU to
1243 	 *  to its NUMA node. Indirect links are added by userspace.
1244 	 */
1245 	sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
1246 		cache_mem_filled);
1247 	ret = kfd_fill_gpu_direct_io_link_to_cpu(&avail_size, kdev,
1248 		(struct crat_subtype_iolink *)sub_type_hdr, proximity_domain);
1249 
1250 	if (ret < 0)
1251 		return ret;
1252 
1253 	crat_table->length += sub_type_hdr->length;
1254 	crat_table->total_entries++;
1255 
1256 
1257 	/* Fill in Subtype: IO_LINKS
1258 	 * Direct links from GPU to other GPUs through xGMI.
1259 	 * We will loop GPUs that already be processed (with lower value
1260 	 * of proximity_domain), add the link for the GPUs with same
1261 	 * hive id (from this GPU to other GPU) . The reversed iolink
1262 	 * (from other GPU to this GPU) will be added
1263 	 * in kfd_parse_subtype_iolink.
1264 	 */
1265 	if (kdev->hive_id) {
1266 		for (nid = 0; nid < proximity_domain; ++nid) {
1267 			peer_dev = kfd_topology_device_by_proximity_domain(nid);
1268 			if (!peer_dev->gpu)
1269 				continue;
1270 			if (peer_dev->gpu->hive_id != kdev->hive_id)
1271 				continue;
1272 			sub_type_hdr = (typeof(sub_type_hdr))(
1273 				(char *)sub_type_hdr +
1274 				sizeof(struct crat_subtype_iolink));
1275 			ret = kfd_fill_gpu_xgmi_link_to_gpu(
1276 				&avail_size, kdev,
1277 				(struct crat_subtype_iolink *)sub_type_hdr,
1278 				proximity_domain, nid);
1279 			if (ret < 0)
1280 				return ret;
1281 			crat_table->length += sub_type_hdr->length;
1282 			crat_table->total_entries++;
1283 		}
1284 	}
1285 	*size = crat_table->length;
1286 	pr_info("Virtual CRAT table created for GPU\n");
1287 
1288 	return ret;
1289 }
1290 
1291 /* kfd_create_crat_image_virtual - Allocates memory for CRAT image and
1292  *		creates a Virtual CRAT (VCRAT) image
1293  *
1294  * NOTE: Call kfd_destroy_crat_image to free CRAT image memory
1295  *
1296  *	@crat_image: VCRAT image created because ACPI does not have a
1297  *		     CRAT for this device
1298  *	@size: [OUT] size of virtual crat_image
1299  *	@flags:	COMPUTE_UNIT_CPU - Create VCRAT for CPU device
1300  *		COMPUTE_UNIT_GPU - Create VCRAT for GPU
1301  *		(COMPUTE_UNIT_CPU | COMPUTE_UNIT_GPU) - Create VCRAT for APU
1302  *			-- this option is not currently implemented.
1303  *			The assumption is that all AMD APUs will have CRAT
1304  *	@kdev: Valid kfd_device required if flags contain COMPUTE_UNIT_GPU
1305  *
1306  *	Return 0 if successful else return -ve value
1307  */
1308 int kfd_create_crat_image_virtual(void **crat_image, size_t *size,
1309 				  int flags, struct kfd_dev *kdev,
1310 				  uint32_t proximity_domain)
1311 {
1312 	void *pcrat_image = NULL;
1313 	int ret = 0;
1314 
1315 	if (!crat_image)
1316 		return -EINVAL;
1317 
1318 	*crat_image = NULL;
1319 
1320 	/* Allocate one VCRAT_SIZE_FOR_CPU for CPU virtual CRAT image and
1321 	 * VCRAT_SIZE_FOR_GPU for GPU virtual CRAT image. This should cover
1322 	 * all the current conditions. A check is put not to overwrite beyond
1323 	 * allocated size
1324 	 */
1325 	switch (flags) {
1326 	case COMPUTE_UNIT_CPU:
1327 		pcrat_image = kmalloc(VCRAT_SIZE_FOR_CPU, GFP_KERNEL);
1328 		if (!pcrat_image)
1329 			return -ENOMEM;
1330 		*size = VCRAT_SIZE_FOR_CPU;
1331 		ret = kfd_create_vcrat_image_cpu(pcrat_image, size);
1332 		break;
1333 	case COMPUTE_UNIT_GPU:
1334 		if (!kdev)
1335 			return -EINVAL;
1336 		pcrat_image = kmalloc(VCRAT_SIZE_FOR_GPU, GFP_KERNEL);
1337 		if (!pcrat_image)
1338 			return -ENOMEM;
1339 		*size = VCRAT_SIZE_FOR_GPU;
1340 		ret = kfd_create_vcrat_image_gpu(pcrat_image, size, kdev,
1341 						 proximity_domain);
1342 		break;
1343 	case (COMPUTE_UNIT_CPU | COMPUTE_UNIT_GPU):
1344 		/* TODO: */
1345 		ret = -EINVAL;
1346 		pr_err("VCRAT not implemented for APU\n");
1347 		break;
1348 	default:
1349 		ret = -EINVAL;
1350 	}
1351 
1352 	if (!ret)
1353 		*crat_image = pcrat_image;
1354 	else
1355 		kfree(pcrat_image);
1356 
1357 	return ret;
1358 }
1359 
1360 
1361 /* kfd_destroy_crat_image
1362  *
1363  *	@crat_image: [IN] - crat_image from kfd_create_crat_image_xxx(..)
1364  *
1365  */
1366 void kfd_destroy_crat_image(void *crat_image)
1367 {
1368 	kfree(crat_image);
1369 }
1370