1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2014-2022 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/pci.h>
27 #include <linux/errno.h>
28 #include <linux/acpi.h>
29 #include <linux/hash.h>
30 #include <linux/cpufreq.h>
31 #include <linux/log2.h>
32 #include <linux/dmi.h>
33 #include <linux/atomic.h>
34 
35 #include "kfd_priv.h"
36 #include "kfd_crat.h"
37 #include "kfd_topology.h"
38 #include "kfd_device_queue_manager.h"
39 #include "kfd_iommu.h"
40 #include "kfd_svm.h"
41 #include "amdgpu_amdkfd.h"
42 #include "amdgpu_ras.h"
43 #include "amdgpu.h"
44 
45 /* topology_device_list - Master list of all topology devices */
46 static struct list_head topology_device_list;
47 static struct kfd_system_properties sys_props;
48 
49 static DECLARE_RWSEM(topology_lock);
50 static uint32_t topology_crat_proximity_domain;
51 
52 struct kfd_topology_device *kfd_topology_device_by_proximity_domain_no_lock(
53 						uint32_t proximity_domain)
54 {
55 	struct kfd_topology_device *top_dev;
56 	struct kfd_topology_device *device = NULL;
57 
58 	list_for_each_entry(top_dev, &topology_device_list, list)
59 		if (top_dev->proximity_domain == proximity_domain) {
60 			device = top_dev;
61 			break;
62 		}
63 
64 	return device;
65 }
66 
67 struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
68 						uint32_t proximity_domain)
69 {
70 	struct kfd_topology_device *device = NULL;
71 
72 	down_read(&topology_lock);
73 
74 	device = kfd_topology_device_by_proximity_domain_no_lock(
75 							proximity_domain);
76 	up_read(&topology_lock);
77 
78 	return device;
79 }
80 
81 struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
82 {
83 	struct kfd_topology_device *top_dev = NULL;
84 	struct kfd_topology_device *ret = NULL;
85 
86 	down_read(&topology_lock);
87 
88 	list_for_each_entry(top_dev, &topology_device_list, list)
89 		if (top_dev->gpu_id == gpu_id) {
90 			ret = top_dev;
91 			break;
92 		}
93 
94 	up_read(&topology_lock);
95 
96 	return ret;
97 }
98 
99 struct kfd_node *kfd_device_by_id(uint32_t gpu_id)
100 {
101 	struct kfd_topology_device *top_dev;
102 
103 	top_dev = kfd_topology_device_by_id(gpu_id);
104 	if (!top_dev)
105 		return NULL;
106 
107 	return top_dev->gpu;
108 }
109 
110 struct kfd_node *kfd_device_by_pci_dev(const struct pci_dev *pdev)
111 {
112 	struct kfd_topology_device *top_dev;
113 	struct kfd_node *device = NULL;
114 
115 	down_read(&topology_lock);
116 
117 	list_for_each_entry(top_dev, &topology_device_list, list)
118 		if (top_dev->gpu && top_dev->gpu->adev->pdev == pdev) {
119 			device = top_dev->gpu;
120 			break;
121 		}
122 
123 	up_read(&topology_lock);
124 
125 	return device;
126 }
127 
128 struct kfd_node *kfd_device_by_adev(const struct amdgpu_device *adev)
129 {
130 	struct kfd_topology_device *top_dev;
131 	struct kfd_node *device = NULL;
132 
133 	down_read(&topology_lock);
134 
135 	list_for_each_entry(top_dev, &topology_device_list, list)
136 		if (top_dev->gpu && top_dev->gpu->adev == adev) {
137 			device = top_dev->gpu;
138 			break;
139 		}
140 
141 	up_read(&topology_lock);
142 
143 	return device;
144 }
145 
146 /* Called with write topology_lock acquired */
147 static void kfd_release_topology_device(struct kfd_topology_device *dev)
148 {
149 	struct kfd_mem_properties *mem;
150 	struct kfd_cache_properties *cache;
151 	struct kfd_iolink_properties *iolink;
152 	struct kfd_iolink_properties *p2plink;
153 	struct kfd_perf_properties *perf;
154 
155 	list_del(&dev->list);
156 
157 	while (dev->mem_props.next != &dev->mem_props) {
158 		mem = container_of(dev->mem_props.next,
159 				struct kfd_mem_properties, list);
160 		list_del(&mem->list);
161 		kfree(mem);
162 	}
163 
164 	while (dev->cache_props.next != &dev->cache_props) {
165 		cache = container_of(dev->cache_props.next,
166 				struct kfd_cache_properties, list);
167 		list_del(&cache->list);
168 		kfree(cache);
169 	}
170 
171 	while (dev->io_link_props.next != &dev->io_link_props) {
172 		iolink = container_of(dev->io_link_props.next,
173 				struct kfd_iolink_properties, list);
174 		list_del(&iolink->list);
175 		kfree(iolink);
176 	}
177 
178 	while (dev->p2p_link_props.next != &dev->p2p_link_props) {
179 		p2plink = container_of(dev->p2p_link_props.next,
180 				struct kfd_iolink_properties, list);
181 		list_del(&p2plink->list);
182 		kfree(p2plink);
183 	}
184 
185 	while (dev->perf_props.next != &dev->perf_props) {
186 		perf = container_of(dev->perf_props.next,
187 				struct kfd_perf_properties, list);
188 		list_del(&perf->list);
189 		kfree(perf);
190 	}
191 
192 	kfree(dev);
193 }
194 
195 void kfd_release_topology_device_list(struct list_head *device_list)
196 {
197 	struct kfd_topology_device *dev;
198 
199 	while (!list_empty(device_list)) {
200 		dev = list_first_entry(device_list,
201 				       struct kfd_topology_device, list);
202 		kfd_release_topology_device(dev);
203 	}
204 }
205 
206 static void kfd_release_live_view(void)
207 {
208 	kfd_release_topology_device_list(&topology_device_list);
209 	memset(&sys_props, 0, sizeof(sys_props));
210 }
211 
212 struct kfd_topology_device *kfd_create_topology_device(
213 				struct list_head *device_list)
214 {
215 	struct kfd_topology_device *dev;
216 
217 	dev = kfd_alloc_struct(dev);
218 	if (!dev) {
219 		pr_err("No memory to allocate a topology device");
220 		return NULL;
221 	}
222 
223 	INIT_LIST_HEAD(&dev->mem_props);
224 	INIT_LIST_HEAD(&dev->cache_props);
225 	INIT_LIST_HEAD(&dev->io_link_props);
226 	INIT_LIST_HEAD(&dev->p2p_link_props);
227 	INIT_LIST_HEAD(&dev->perf_props);
228 
229 	list_add_tail(&dev->list, device_list);
230 
231 	return dev;
232 }
233 
234 
235 #define sysfs_show_gen_prop(buffer, offs, fmt, ...)		\
236 		(offs += snprintf(buffer+offs, PAGE_SIZE-offs,	\
237 				  fmt, __VA_ARGS__))
238 #define sysfs_show_32bit_prop(buffer, offs, name, value) \
239 		sysfs_show_gen_prop(buffer, offs, "%s %u\n", name, value)
240 #define sysfs_show_64bit_prop(buffer, offs, name, value) \
241 		sysfs_show_gen_prop(buffer, offs, "%s %llu\n", name, value)
242 #define sysfs_show_32bit_val(buffer, offs, value) \
243 		sysfs_show_gen_prop(buffer, offs, "%u\n", value)
244 #define sysfs_show_str_val(buffer, offs, value) \
245 		sysfs_show_gen_prop(buffer, offs, "%s\n", value)
246 
247 static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
248 		char *buffer)
249 {
250 	int offs = 0;
251 
252 	/* Making sure that the buffer is an empty string */
253 	buffer[0] = 0;
254 
255 	if (attr == &sys_props.attr_genid) {
256 		sysfs_show_32bit_val(buffer, offs,
257 				     sys_props.generation_count);
258 	} else if (attr == &sys_props.attr_props) {
259 		sysfs_show_64bit_prop(buffer, offs, "platform_oem",
260 				      sys_props.platform_oem);
261 		sysfs_show_64bit_prop(buffer, offs, "platform_id",
262 				      sys_props.platform_id);
263 		sysfs_show_64bit_prop(buffer, offs, "platform_rev",
264 				      sys_props.platform_rev);
265 	} else {
266 		offs = -EINVAL;
267 	}
268 
269 	return offs;
270 }
271 
272 static void kfd_topology_kobj_release(struct kobject *kobj)
273 {
274 	kfree(kobj);
275 }
276 
277 static const struct sysfs_ops sysprops_ops = {
278 	.show = sysprops_show,
279 };
280 
281 static const struct kobj_type sysprops_type = {
282 	.release = kfd_topology_kobj_release,
283 	.sysfs_ops = &sysprops_ops,
284 };
285 
286 static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
287 		char *buffer)
288 {
289 	int offs = 0;
290 	struct kfd_iolink_properties *iolink;
291 
292 	/* Making sure that the buffer is an empty string */
293 	buffer[0] = 0;
294 
295 	iolink = container_of(attr, struct kfd_iolink_properties, attr);
296 	if (iolink->gpu && kfd_devcgroup_check_permission(iolink->gpu))
297 		return -EPERM;
298 	sysfs_show_32bit_prop(buffer, offs, "type", iolink->iolink_type);
299 	sysfs_show_32bit_prop(buffer, offs, "version_major", iolink->ver_maj);
300 	sysfs_show_32bit_prop(buffer, offs, "version_minor", iolink->ver_min);
301 	sysfs_show_32bit_prop(buffer, offs, "node_from", iolink->node_from);
302 	sysfs_show_32bit_prop(buffer, offs, "node_to", iolink->node_to);
303 	sysfs_show_32bit_prop(buffer, offs, "weight", iolink->weight);
304 	sysfs_show_32bit_prop(buffer, offs, "min_latency", iolink->min_latency);
305 	sysfs_show_32bit_prop(buffer, offs, "max_latency", iolink->max_latency);
306 	sysfs_show_32bit_prop(buffer, offs, "min_bandwidth",
307 			      iolink->min_bandwidth);
308 	sysfs_show_32bit_prop(buffer, offs, "max_bandwidth",
309 			      iolink->max_bandwidth);
310 	sysfs_show_32bit_prop(buffer, offs, "recommended_transfer_size",
311 			      iolink->rec_transfer_size);
312 	sysfs_show_32bit_prop(buffer, offs, "flags", iolink->flags);
313 
314 	return offs;
315 }
316 
317 static const struct sysfs_ops iolink_ops = {
318 	.show = iolink_show,
319 };
320 
321 static const struct kobj_type iolink_type = {
322 	.release = kfd_topology_kobj_release,
323 	.sysfs_ops = &iolink_ops,
324 };
325 
326 static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
327 		char *buffer)
328 {
329 	int offs = 0;
330 	struct kfd_mem_properties *mem;
331 
332 	/* Making sure that the buffer is an empty string */
333 	buffer[0] = 0;
334 
335 	mem = container_of(attr, struct kfd_mem_properties, attr);
336 	if (mem->gpu && kfd_devcgroup_check_permission(mem->gpu))
337 		return -EPERM;
338 	sysfs_show_32bit_prop(buffer, offs, "heap_type", mem->heap_type);
339 	sysfs_show_64bit_prop(buffer, offs, "size_in_bytes",
340 			      mem->size_in_bytes);
341 	sysfs_show_32bit_prop(buffer, offs, "flags", mem->flags);
342 	sysfs_show_32bit_prop(buffer, offs, "width", mem->width);
343 	sysfs_show_32bit_prop(buffer, offs, "mem_clk_max",
344 			      mem->mem_clk_max);
345 
346 	return offs;
347 }
348 
349 static const struct sysfs_ops mem_ops = {
350 	.show = mem_show,
351 };
352 
353 static const struct kobj_type mem_type = {
354 	.release = kfd_topology_kobj_release,
355 	.sysfs_ops = &mem_ops,
356 };
357 
358 static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
359 		char *buffer)
360 {
361 	int offs = 0;
362 	uint32_t i, j;
363 	struct kfd_cache_properties *cache;
364 
365 	/* Making sure that the buffer is an empty string */
366 	buffer[0] = 0;
367 	cache = container_of(attr, struct kfd_cache_properties, attr);
368 	if (cache->gpu && kfd_devcgroup_check_permission(cache->gpu))
369 		return -EPERM;
370 	sysfs_show_32bit_prop(buffer, offs, "processor_id_low",
371 			cache->processor_id_low);
372 	sysfs_show_32bit_prop(buffer, offs, "level", cache->cache_level);
373 	sysfs_show_32bit_prop(buffer, offs, "size", cache->cache_size);
374 	sysfs_show_32bit_prop(buffer, offs, "cache_line_size",
375 			      cache->cacheline_size);
376 	sysfs_show_32bit_prop(buffer, offs, "cache_lines_per_tag",
377 			      cache->cachelines_per_tag);
378 	sysfs_show_32bit_prop(buffer, offs, "association", cache->cache_assoc);
379 	sysfs_show_32bit_prop(buffer, offs, "latency", cache->cache_latency);
380 	sysfs_show_32bit_prop(buffer, offs, "type", cache->cache_type);
381 
382 	offs += snprintf(buffer+offs, PAGE_SIZE-offs, "sibling_map ");
383 	for (i = 0; i < cache->sibling_map_size; i++)
384 		for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++)
385 			/* Check each bit */
386 			offs += snprintf(buffer+offs, PAGE_SIZE-offs, "%d,",
387 						(cache->sibling_map[i] >> j) & 1);
388 
389 	/* Replace the last "," with end of line */
390 	buffer[offs-1] = '\n';
391 	return offs;
392 }
393 
394 static const struct sysfs_ops cache_ops = {
395 	.show = kfd_cache_show,
396 };
397 
398 static const struct kobj_type cache_type = {
399 	.release = kfd_topology_kobj_release,
400 	.sysfs_ops = &cache_ops,
401 };
402 
403 /****** Sysfs of Performance Counters ******/
404 
405 struct kfd_perf_attr {
406 	struct kobj_attribute attr;
407 	uint32_t data;
408 };
409 
410 static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
411 			char *buf)
412 {
413 	int offs = 0;
414 	struct kfd_perf_attr *attr;
415 
416 	buf[0] = 0;
417 	attr = container_of(attrs, struct kfd_perf_attr, attr);
418 	if (!attr->data) /* invalid data for PMC */
419 		return 0;
420 	else
421 		return sysfs_show_32bit_val(buf, offs, attr->data);
422 }
423 
424 #define KFD_PERF_DESC(_name, _data)			\
425 {							\
426 	.attr  = __ATTR(_name, 0444, perf_show, NULL),	\
427 	.data = _data,					\
428 }
429 
430 static struct kfd_perf_attr perf_attr_iommu[] = {
431 	KFD_PERF_DESC(max_concurrent, 0),
432 	KFD_PERF_DESC(num_counters, 0),
433 	KFD_PERF_DESC(counter_ids, 0),
434 };
435 /****************************************/
436 
437 static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
438 		char *buffer)
439 {
440 	int offs = 0;
441 	struct kfd_topology_device *dev;
442 	uint32_t log_max_watch_addr;
443 
444 	/* Making sure that the buffer is an empty string */
445 	buffer[0] = 0;
446 
447 	if (strcmp(attr->name, "gpu_id") == 0) {
448 		dev = container_of(attr, struct kfd_topology_device,
449 				attr_gpuid);
450 		if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
451 			return -EPERM;
452 		return sysfs_show_32bit_val(buffer, offs, dev->gpu_id);
453 	}
454 
455 	if (strcmp(attr->name, "name") == 0) {
456 		dev = container_of(attr, struct kfd_topology_device,
457 				attr_name);
458 
459 		if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
460 			return -EPERM;
461 		return sysfs_show_str_val(buffer, offs, dev->node_props.name);
462 	}
463 
464 	dev = container_of(attr, struct kfd_topology_device,
465 			attr_props);
466 	if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
467 		return -EPERM;
468 	sysfs_show_32bit_prop(buffer, offs, "cpu_cores_count",
469 			      dev->node_props.cpu_cores_count);
470 	sysfs_show_32bit_prop(buffer, offs, "simd_count",
471 			      dev->gpu ? (dev->node_props.simd_count *
472 					  dev->gpu->num_xcc_per_node) : 0);
473 	sysfs_show_32bit_prop(buffer, offs, "mem_banks_count",
474 			      dev->node_props.mem_banks_count);
475 	sysfs_show_32bit_prop(buffer, offs, "caches_count",
476 			      dev->node_props.caches_count);
477 	sysfs_show_32bit_prop(buffer, offs, "io_links_count",
478 			      dev->node_props.io_links_count);
479 	sysfs_show_32bit_prop(buffer, offs, "p2p_links_count",
480 			      dev->node_props.p2p_links_count);
481 	sysfs_show_32bit_prop(buffer, offs, "cpu_core_id_base",
482 			      dev->node_props.cpu_core_id_base);
483 	sysfs_show_32bit_prop(buffer, offs, "simd_id_base",
484 			      dev->node_props.simd_id_base);
485 	sysfs_show_32bit_prop(buffer, offs, "max_waves_per_simd",
486 			      dev->node_props.max_waves_per_simd);
487 	sysfs_show_32bit_prop(buffer, offs, "lds_size_in_kb",
488 			      dev->node_props.lds_size_in_kb);
489 	sysfs_show_32bit_prop(buffer, offs, "gds_size_in_kb",
490 			      dev->node_props.gds_size_in_kb);
491 	sysfs_show_32bit_prop(buffer, offs, "num_gws",
492 			      dev->node_props.num_gws);
493 	sysfs_show_32bit_prop(buffer, offs, "wave_front_size",
494 			      dev->node_props.wave_front_size);
495 	sysfs_show_32bit_prop(buffer, offs, "array_count",
496 			      dev->gpu ? (dev->node_props.array_count *
497 					  dev->gpu->num_xcc_per_node) : 0);
498 	sysfs_show_32bit_prop(buffer, offs, "simd_arrays_per_engine",
499 			      dev->node_props.simd_arrays_per_engine);
500 	sysfs_show_32bit_prop(buffer, offs, "cu_per_simd_array",
501 			      dev->node_props.cu_per_simd_array);
502 	sysfs_show_32bit_prop(buffer, offs, "simd_per_cu",
503 			      dev->node_props.simd_per_cu);
504 	sysfs_show_32bit_prop(buffer, offs, "max_slots_scratch_cu",
505 			      dev->node_props.max_slots_scratch_cu);
506 	sysfs_show_32bit_prop(buffer, offs, "gfx_target_version",
507 			      dev->node_props.gfx_target_version);
508 	sysfs_show_32bit_prop(buffer, offs, "vendor_id",
509 			      dev->node_props.vendor_id);
510 	sysfs_show_32bit_prop(buffer, offs, "device_id",
511 			      dev->node_props.device_id);
512 	sysfs_show_32bit_prop(buffer, offs, "location_id",
513 			      dev->node_props.location_id);
514 	sysfs_show_32bit_prop(buffer, offs, "domain",
515 			      dev->node_props.domain);
516 	sysfs_show_32bit_prop(buffer, offs, "drm_render_minor",
517 			      dev->node_props.drm_render_minor);
518 	sysfs_show_64bit_prop(buffer, offs, "hive_id",
519 			      dev->node_props.hive_id);
520 	sysfs_show_32bit_prop(buffer, offs, "num_sdma_engines",
521 			      dev->node_props.num_sdma_engines);
522 	sysfs_show_32bit_prop(buffer, offs, "num_sdma_xgmi_engines",
523 			      dev->node_props.num_sdma_xgmi_engines);
524 	sysfs_show_32bit_prop(buffer, offs, "num_sdma_queues_per_engine",
525 			      dev->node_props.num_sdma_queues_per_engine);
526 	sysfs_show_32bit_prop(buffer, offs, "num_cp_queues",
527 			      dev->node_props.num_cp_queues);
528 
529 	if (dev->gpu) {
530 		log_max_watch_addr =
531 			__ilog2_u32(dev->gpu->kfd->device_info.num_of_watch_points);
532 
533 		if (log_max_watch_addr) {
534 			dev->node_props.capability |=
535 					HSA_CAP_WATCH_POINTS_SUPPORTED;
536 
537 			dev->node_props.capability |=
538 				((log_max_watch_addr <<
539 					HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
540 				HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
541 		}
542 
543 		if (dev->gpu->adev->asic_type == CHIP_TONGA)
544 			dev->node_props.capability |=
545 					HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
546 
547 		sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_fcompute",
548 			dev->node_props.max_engine_clk_fcompute);
549 
550 		sysfs_show_64bit_prop(buffer, offs, "local_mem_size", 0ULL);
551 
552 		sysfs_show_32bit_prop(buffer, offs, "fw_version",
553 				      dev->gpu->kfd->mec_fw_version);
554 		sysfs_show_32bit_prop(buffer, offs, "capability",
555 				      dev->node_props.capability);
556 		sysfs_show_32bit_prop(buffer, offs, "sdma_fw_version",
557 				      dev->gpu->kfd->sdma_fw_version);
558 		sysfs_show_64bit_prop(buffer, offs, "unique_id",
559 				      dev->gpu->adev->unique_id);
560 		sysfs_show_32bit_prop(buffer, offs, "num_xcc",
561 				      dev->gpu->num_xcc_per_node);
562 	}
563 
564 	return sysfs_show_32bit_prop(buffer, offs, "max_engine_clk_ccompute",
565 				     cpufreq_quick_get_max(0)/1000);
566 }
567 
568 static const struct sysfs_ops node_ops = {
569 	.show = node_show,
570 };
571 
572 static const struct kobj_type node_type = {
573 	.release = kfd_topology_kobj_release,
574 	.sysfs_ops = &node_ops,
575 };
576 
577 static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
578 {
579 	sysfs_remove_file(kobj, attr);
580 	kobject_del(kobj);
581 	kobject_put(kobj);
582 }
583 
584 static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
585 {
586 	struct kfd_iolink_properties *p2plink;
587 	struct kfd_iolink_properties *iolink;
588 	struct kfd_cache_properties *cache;
589 	struct kfd_mem_properties *mem;
590 	struct kfd_perf_properties *perf;
591 
592 	if (dev->kobj_iolink) {
593 		list_for_each_entry(iolink, &dev->io_link_props, list)
594 			if (iolink->kobj) {
595 				kfd_remove_sysfs_file(iolink->kobj,
596 							&iolink->attr);
597 				iolink->kobj = NULL;
598 			}
599 		kobject_del(dev->kobj_iolink);
600 		kobject_put(dev->kobj_iolink);
601 		dev->kobj_iolink = NULL;
602 	}
603 
604 	if (dev->kobj_p2plink) {
605 		list_for_each_entry(p2plink, &dev->p2p_link_props, list)
606 			if (p2plink->kobj) {
607 				kfd_remove_sysfs_file(p2plink->kobj,
608 							&p2plink->attr);
609 				p2plink->kobj = NULL;
610 			}
611 		kobject_del(dev->kobj_p2plink);
612 		kobject_put(dev->kobj_p2plink);
613 		dev->kobj_p2plink = NULL;
614 	}
615 
616 	if (dev->kobj_cache) {
617 		list_for_each_entry(cache, &dev->cache_props, list)
618 			if (cache->kobj) {
619 				kfd_remove_sysfs_file(cache->kobj,
620 							&cache->attr);
621 				cache->kobj = NULL;
622 			}
623 		kobject_del(dev->kobj_cache);
624 		kobject_put(dev->kobj_cache);
625 		dev->kobj_cache = NULL;
626 	}
627 
628 	if (dev->kobj_mem) {
629 		list_for_each_entry(mem, &dev->mem_props, list)
630 			if (mem->kobj) {
631 				kfd_remove_sysfs_file(mem->kobj, &mem->attr);
632 				mem->kobj = NULL;
633 			}
634 		kobject_del(dev->kobj_mem);
635 		kobject_put(dev->kobj_mem);
636 		dev->kobj_mem = NULL;
637 	}
638 
639 	if (dev->kobj_perf) {
640 		list_for_each_entry(perf, &dev->perf_props, list) {
641 			kfree(perf->attr_group);
642 			perf->attr_group = NULL;
643 		}
644 		kobject_del(dev->kobj_perf);
645 		kobject_put(dev->kobj_perf);
646 		dev->kobj_perf = NULL;
647 	}
648 
649 	if (dev->kobj_node) {
650 		sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
651 		sysfs_remove_file(dev->kobj_node, &dev->attr_name);
652 		sysfs_remove_file(dev->kobj_node, &dev->attr_props);
653 		kobject_del(dev->kobj_node);
654 		kobject_put(dev->kobj_node);
655 		dev->kobj_node = NULL;
656 	}
657 }
658 
659 static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
660 		uint32_t id)
661 {
662 	struct kfd_iolink_properties *p2plink;
663 	struct kfd_iolink_properties *iolink;
664 	struct kfd_cache_properties *cache;
665 	struct kfd_mem_properties *mem;
666 	struct kfd_perf_properties *perf;
667 	int ret;
668 	uint32_t i, num_attrs;
669 	struct attribute **attrs;
670 
671 	if (WARN_ON(dev->kobj_node))
672 		return -EEXIST;
673 
674 	/*
675 	 * Creating the sysfs folders
676 	 */
677 	dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
678 	if (!dev->kobj_node)
679 		return -ENOMEM;
680 
681 	ret = kobject_init_and_add(dev->kobj_node, &node_type,
682 			sys_props.kobj_nodes, "%d", id);
683 	if (ret < 0) {
684 		kobject_put(dev->kobj_node);
685 		return ret;
686 	}
687 
688 	dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
689 	if (!dev->kobj_mem)
690 		return -ENOMEM;
691 
692 	dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
693 	if (!dev->kobj_cache)
694 		return -ENOMEM;
695 
696 	dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
697 	if (!dev->kobj_iolink)
698 		return -ENOMEM;
699 
700 	dev->kobj_p2plink = kobject_create_and_add("p2p_links", dev->kobj_node);
701 	if (!dev->kobj_p2plink)
702 		return -ENOMEM;
703 
704 	dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
705 	if (!dev->kobj_perf)
706 		return -ENOMEM;
707 
708 	/*
709 	 * Creating sysfs files for node properties
710 	 */
711 	dev->attr_gpuid.name = "gpu_id";
712 	dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
713 	sysfs_attr_init(&dev->attr_gpuid);
714 	dev->attr_name.name = "name";
715 	dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
716 	sysfs_attr_init(&dev->attr_name);
717 	dev->attr_props.name = "properties";
718 	dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
719 	sysfs_attr_init(&dev->attr_props);
720 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
721 	if (ret < 0)
722 		return ret;
723 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
724 	if (ret < 0)
725 		return ret;
726 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
727 	if (ret < 0)
728 		return ret;
729 
730 	i = 0;
731 	list_for_each_entry(mem, &dev->mem_props, list) {
732 		mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
733 		if (!mem->kobj)
734 			return -ENOMEM;
735 		ret = kobject_init_and_add(mem->kobj, &mem_type,
736 				dev->kobj_mem, "%d", i);
737 		if (ret < 0) {
738 			kobject_put(mem->kobj);
739 			return ret;
740 		}
741 
742 		mem->attr.name = "properties";
743 		mem->attr.mode = KFD_SYSFS_FILE_MODE;
744 		sysfs_attr_init(&mem->attr);
745 		ret = sysfs_create_file(mem->kobj, &mem->attr);
746 		if (ret < 0)
747 			return ret;
748 		i++;
749 	}
750 
751 	i = 0;
752 	list_for_each_entry(cache, &dev->cache_props, list) {
753 		cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
754 		if (!cache->kobj)
755 			return -ENOMEM;
756 		ret = kobject_init_and_add(cache->kobj, &cache_type,
757 				dev->kobj_cache, "%d", i);
758 		if (ret < 0) {
759 			kobject_put(cache->kobj);
760 			return ret;
761 		}
762 
763 		cache->attr.name = "properties";
764 		cache->attr.mode = KFD_SYSFS_FILE_MODE;
765 		sysfs_attr_init(&cache->attr);
766 		ret = sysfs_create_file(cache->kobj, &cache->attr);
767 		if (ret < 0)
768 			return ret;
769 		i++;
770 	}
771 
772 	i = 0;
773 	list_for_each_entry(iolink, &dev->io_link_props, list) {
774 		iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
775 		if (!iolink->kobj)
776 			return -ENOMEM;
777 		ret = kobject_init_and_add(iolink->kobj, &iolink_type,
778 				dev->kobj_iolink, "%d", i);
779 		if (ret < 0) {
780 			kobject_put(iolink->kobj);
781 			return ret;
782 		}
783 
784 		iolink->attr.name = "properties";
785 		iolink->attr.mode = KFD_SYSFS_FILE_MODE;
786 		sysfs_attr_init(&iolink->attr);
787 		ret = sysfs_create_file(iolink->kobj, &iolink->attr);
788 		if (ret < 0)
789 			return ret;
790 		i++;
791 	}
792 
793 	i = 0;
794 	list_for_each_entry(p2plink, &dev->p2p_link_props, list) {
795 		p2plink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
796 		if (!p2plink->kobj)
797 			return -ENOMEM;
798 		ret = kobject_init_and_add(p2plink->kobj, &iolink_type,
799 				dev->kobj_p2plink, "%d", i);
800 		if (ret < 0) {
801 			kobject_put(p2plink->kobj);
802 			return ret;
803 		}
804 
805 		p2plink->attr.name = "properties";
806 		p2plink->attr.mode = KFD_SYSFS_FILE_MODE;
807 		sysfs_attr_init(&p2plink->attr);
808 		ret = sysfs_create_file(p2plink->kobj, &p2plink->attr);
809 		if (ret < 0)
810 			return ret;
811 		i++;
812 	}
813 
814 	/* All hardware blocks have the same number of attributes. */
815 	num_attrs = ARRAY_SIZE(perf_attr_iommu);
816 	list_for_each_entry(perf, &dev->perf_props, list) {
817 		perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
818 			* num_attrs + sizeof(struct attribute_group),
819 			GFP_KERNEL);
820 		if (!perf->attr_group)
821 			return -ENOMEM;
822 
823 		attrs = (struct attribute **)(perf->attr_group + 1);
824 		if (!strcmp(perf->block_name, "iommu")) {
825 		/* Information of IOMMU's num_counters and counter_ids is shown
826 		 * under /sys/bus/event_source/devices/amd_iommu. We don't
827 		 * duplicate here.
828 		 */
829 			perf_attr_iommu[0].data = perf->max_concurrent;
830 			for (i = 0; i < num_attrs; i++)
831 				attrs[i] = &perf_attr_iommu[i].attr.attr;
832 		}
833 		perf->attr_group->name = perf->block_name;
834 		perf->attr_group->attrs = attrs;
835 		ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
836 		if (ret < 0)
837 			return ret;
838 	}
839 
840 	return 0;
841 }
842 
843 /* Called with write topology lock acquired */
844 static int kfd_build_sysfs_node_tree(void)
845 {
846 	struct kfd_topology_device *dev;
847 	int ret;
848 	uint32_t i = 0;
849 
850 	list_for_each_entry(dev, &topology_device_list, list) {
851 		ret = kfd_build_sysfs_node_entry(dev, i);
852 		if (ret < 0)
853 			return ret;
854 		i++;
855 	}
856 
857 	return 0;
858 }
859 
860 /* Called with write topology lock acquired */
861 static void kfd_remove_sysfs_node_tree(void)
862 {
863 	struct kfd_topology_device *dev;
864 
865 	list_for_each_entry(dev, &topology_device_list, list)
866 		kfd_remove_sysfs_node_entry(dev);
867 }
868 
869 static int kfd_topology_update_sysfs(void)
870 {
871 	int ret;
872 
873 	if (!sys_props.kobj_topology) {
874 		sys_props.kobj_topology =
875 				kfd_alloc_struct(sys_props.kobj_topology);
876 		if (!sys_props.kobj_topology)
877 			return -ENOMEM;
878 
879 		ret = kobject_init_and_add(sys_props.kobj_topology,
880 				&sysprops_type,  &kfd_device->kobj,
881 				"topology");
882 		if (ret < 0) {
883 			kobject_put(sys_props.kobj_topology);
884 			return ret;
885 		}
886 
887 		sys_props.kobj_nodes = kobject_create_and_add("nodes",
888 				sys_props.kobj_topology);
889 		if (!sys_props.kobj_nodes)
890 			return -ENOMEM;
891 
892 		sys_props.attr_genid.name = "generation_id";
893 		sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
894 		sysfs_attr_init(&sys_props.attr_genid);
895 		ret = sysfs_create_file(sys_props.kobj_topology,
896 				&sys_props.attr_genid);
897 		if (ret < 0)
898 			return ret;
899 
900 		sys_props.attr_props.name = "system_properties";
901 		sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
902 		sysfs_attr_init(&sys_props.attr_props);
903 		ret = sysfs_create_file(sys_props.kobj_topology,
904 				&sys_props.attr_props);
905 		if (ret < 0)
906 			return ret;
907 	}
908 
909 	kfd_remove_sysfs_node_tree();
910 
911 	return kfd_build_sysfs_node_tree();
912 }
913 
914 static void kfd_topology_release_sysfs(void)
915 {
916 	kfd_remove_sysfs_node_tree();
917 	if (sys_props.kobj_topology) {
918 		sysfs_remove_file(sys_props.kobj_topology,
919 				&sys_props.attr_genid);
920 		sysfs_remove_file(sys_props.kobj_topology,
921 				&sys_props.attr_props);
922 		if (sys_props.kobj_nodes) {
923 			kobject_del(sys_props.kobj_nodes);
924 			kobject_put(sys_props.kobj_nodes);
925 			sys_props.kobj_nodes = NULL;
926 		}
927 		kobject_del(sys_props.kobj_topology);
928 		kobject_put(sys_props.kobj_topology);
929 		sys_props.kobj_topology = NULL;
930 	}
931 }
932 
933 /* Called with write topology_lock acquired */
934 static void kfd_topology_update_device_list(struct list_head *temp_list,
935 					struct list_head *master_list)
936 {
937 	while (!list_empty(temp_list)) {
938 		list_move_tail(temp_list->next, master_list);
939 		sys_props.num_devices++;
940 	}
941 }
942 
943 static void kfd_debug_print_topology(void)
944 {
945 	struct kfd_topology_device *dev;
946 
947 	down_read(&topology_lock);
948 
949 	dev = list_last_entry(&topology_device_list,
950 			struct kfd_topology_device, list);
951 	if (dev) {
952 		if (dev->node_props.cpu_cores_count &&
953 				dev->node_props.simd_count) {
954 			pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
955 				dev->node_props.device_id,
956 				dev->node_props.vendor_id);
957 		} else if (dev->node_props.cpu_cores_count)
958 			pr_info("Topology: Add CPU node\n");
959 		else if (dev->node_props.simd_count)
960 			pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
961 				dev->node_props.device_id,
962 				dev->node_props.vendor_id);
963 	}
964 	up_read(&topology_lock);
965 }
966 
967 /* Helper function for intializing platform_xx members of
968  * kfd_system_properties. Uses OEM info from the last CPU/APU node.
969  */
970 static void kfd_update_system_properties(void)
971 {
972 	struct kfd_topology_device *dev;
973 
974 	down_read(&topology_lock);
975 	dev = list_last_entry(&topology_device_list,
976 			struct kfd_topology_device, list);
977 	if (dev) {
978 		sys_props.platform_id =
979 			(*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
980 		sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
981 		sys_props.platform_rev = dev->oem_revision;
982 	}
983 	up_read(&topology_lock);
984 }
985 
986 static void find_system_memory(const struct dmi_header *dm,
987 	void *private)
988 {
989 	struct kfd_mem_properties *mem;
990 	u16 mem_width, mem_clock;
991 	struct kfd_topology_device *kdev =
992 		(struct kfd_topology_device *)private;
993 	const u8 *dmi_data = (const u8 *)(dm + 1);
994 
995 	if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
996 		mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
997 		mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
998 		list_for_each_entry(mem, &kdev->mem_props, list) {
999 			if (mem_width != 0xFFFF && mem_width != 0)
1000 				mem->width = mem_width;
1001 			if (mem_clock != 0)
1002 				mem->mem_clk_max = mem_clock;
1003 		}
1004 	}
1005 }
1006 
1007 /*
1008  * Performance counters information is not part of CRAT but we would like to
1009  * put them in the sysfs under topology directory for Thunk to get the data.
1010  * This function is called before updating the sysfs.
1011  */
1012 static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
1013 {
1014 	/* These are the only counters supported so far */
1015 	return kfd_iommu_add_perf_counters(kdev);
1016 }
1017 
1018 /* kfd_add_non_crat_information - Add information that is not currently
1019  *	defined in CRAT but is necessary for KFD topology
1020  * @dev - topology device to which addition info is added
1021  */
1022 static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
1023 {
1024 	/* Check if CPU only node. */
1025 	if (!kdev->gpu) {
1026 		/* Add system memory information */
1027 		dmi_walk(find_system_memory, kdev);
1028 	}
1029 	/* TODO: For GPU node, rearrange code from kfd_topology_add_device */
1030 }
1031 
1032 /* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
1033  *	Ignore CRAT for all other devices. AMD APU is identified if both CPU
1034  *	and GPU cores are present.
1035  * @device_list - topology device list created by parsing ACPI CRAT table.
1036  * @return - TRUE if invalid, FALSE is valid.
1037  */
1038 static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
1039 {
1040 	struct kfd_topology_device *dev;
1041 
1042 	list_for_each_entry(dev, device_list, list) {
1043 		if (dev->node_props.cpu_cores_count &&
1044 			dev->node_props.simd_count)
1045 			return false;
1046 	}
1047 	pr_info("Ignoring ACPI CRAT on non-APU system\n");
1048 	return true;
1049 }
1050 
1051 int kfd_topology_init(void)
1052 {
1053 	void *crat_image = NULL;
1054 	size_t image_size = 0;
1055 	int ret;
1056 	struct list_head temp_topology_device_list;
1057 	int cpu_only_node = 0;
1058 	struct kfd_topology_device *kdev;
1059 	int proximity_domain;
1060 
1061 	/* topology_device_list - Master list of all topology devices
1062 	 * temp_topology_device_list - temporary list created while parsing CRAT
1063 	 * or VCRAT. Once parsing is complete the contents of list is moved to
1064 	 * topology_device_list
1065 	 */
1066 
1067 	/* Initialize the head for the both the lists */
1068 	INIT_LIST_HEAD(&topology_device_list);
1069 	INIT_LIST_HEAD(&temp_topology_device_list);
1070 	init_rwsem(&topology_lock);
1071 
1072 	memset(&sys_props, 0, sizeof(sys_props));
1073 
1074 	/* Proximity domains in ACPI CRAT tables start counting at
1075 	 * 0. The same should be true for virtual CRAT tables created
1076 	 * at this stage. GPUs added later in kfd_topology_add_device
1077 	 * use a counter.
1078 	 */
1079 	proximity_domain = 0;
1080 
1081 	/*
1082 	 * Get the CRAT image from the ACPI. If ACPI doesn't have one
1083 	 * or if ACPI CRAT is invalid create a virtual CRAT.
1084 	 * NOTE: The current implementation expects all AMD APUs to have
1085 	 *	CRAT. If no CRAT is available, it is assumed to be a CPU
1086 	 */
1087 	ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
1088 	if (!ret) {
1089 		ret = kfd_parse_crat_table(crat_image,
1090 					   &temp_topology_device_list,
1091 					   proximity_domain);
1092 		if (ret ||
1093 		    kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
1094 			kfd_release_topology_device_list(
1095 				&temp_topology_device_list);
1096 			kfd_destroy_crat_image(crat_image);
1097 			crat_image = NULL;
1098 		}
1099 	}
1100 
1101 	if (!crat_image) {
1102 		ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
1103 						    COMPUTE_UNIT_CPU, NULL,
1104 						    proximity_domain);
1105 		cpu_only_node = 1;
1106 		if (ret) {
1107 			pr_err("Error creating VCRAT table for CPU\n");
1108 			return ret;
1109 		}
1110 
1111 		ret = kfd_parse_crat_table(crat_image,
1112 					   &temp_topology_device_list,
1113 					   proximity_domain);
1114 		if (ret) {
1115 			pr_err("Error parsing VCRAT table for CPU\n");
1116 			goto err;
1117 		}
1118 	}
1119 
1120 	kdev = list_first_entry(&temp_topology_device_list,
1121 				struct kfd_topology_device, list);
1122 	kfd_add_perf_to_topology(kdev);
1123 
1124 	down_write(&topology_lock);
1125 	kfd_topology_update_device_list(&temp_topology_device_list,
1126 					&topology_device_list);
1127 	topology_crat_proximity_domain = sys_props.num_devices-1;
1128 	ret = kfd_topology_update_sysfs();
1129 	up_write(&topology_lock);
1130 
1131 	if (!ret) {
1132 		sys_props.generation_count++;
1133 		kfd_update_system_properties();
1134 		kfd_debug_print_topology();
1135 	} else
1136 		pr_err("Failed to update topology in sysfs ret=%d\n", ret);
1137 
1138 	/* For nodes with GPU, this information gets added
1139 	 * when GPU is detected (kfd_topology_add_device).
1140 	 */
1141 	if (cpu_only_node) {
1142 		/* Add additional information to CPU only node created above */
1143 		down_write(&topology_lock);
1144 		kdev = list_first_entry(&topology_device_list,
1145 				struct kfd_topology_device, list);
1146 		up_write(&topology_lock);
1147 		kfd_add_non_crat_information(kdev);
1148 	}
1149 
1150 err:
1151 	kfd_destroy_crat_image(crat_image);
1152 	return ret;
1153 }
1154 
1155 void kfd_topology_shutdown(void)
1156 {
1157 	down_write(&topology_lock);
1158 	kfd_topology_release_sysfs();
1159 	kfd_release_live_view();
1160 	up_write(&topology_lock);
1161 }
1162 
1163 static uint32_t kfd_generate_gpu_id(struct kfd_node *gpu)
1164 {
1165 	uint32_t hashout;
1166 	uint32_t buf[8];
1167 	uint64_t local_mem_size;
1168 	int i;
1169 
1170 	if (!gpu)
1171 		return 0;
1172 
1173 	local_mem_size = gpu->kfd->local_mem_info.local_mem_size_private +
1174 			gpu->kfd->local_mem_info.local_mem_size_public;
1175 	buf[0] = gpu->adev->pdev->devfn;
1176 	buf[1] = gpu->adev->pdev->subsystem_vendor |
1177 		(gpu->adev->pdev->subsystem_device << 16);
1178 	buf[2] = pci_domain_nr(gpu->adev->pdev->bus);
1179 	buf[3] = gpu->adev->pdev->device;
1180 	buf[4] = gpu->adev->pdev->bus->number;
1181 	buf[5] = lower_32_bits(local_mem_size);
1182 	buf[6] = upper_32_bits(local_mem_size);
1183 	buf[7] = gpu->start_xcc_id | (gpu->num_xcc_per_node << 16);
1184 
1185 	for (i = 0, hashout = 0; i < 8; i++)
1186 		hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
1187 
1188 	return hashout;
1189 }
1190 /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
1191  *		the GPU device is not already present in the topology device
1192  *		list then return NULL. This means a new topology device has to
1193  *		be created for this GPU.
1194  */
1195 static struct kfd_topology_device *kfd_assign_gpu(struct kfd_node *gpu)
1196 {
1197 	struct kfd_topology_device *dev;
1198 	struct kfd_topology_device *out_dev = NULL;
1199 	struct kfd_mem_properties *mem;
1200 	struct kfd_cache_properties *cache;
1201 	struct kfd_iolink_properties *iolink;
1202 	struct kfd_iolink_properties *p2plink;
1203 
1204 	list_for_each_entry(dev, &topology_device_list, list) {
1205 		/* Discrete GPUs need their own topology device list
1206 		 * entries. Don't assign them to CPU/APU nodes.
1207 		 */
1208 		if (!gpu->kfd->use_iommu_v2 &&
1209 		    dev->node_props.cpu_cores_count)
1210 			continue;
1211 
1212 		if (!dev->gpu && (dev->node_props.simd_count > 0)) {
1213 			dev->gpu = gpu;
1214 			out_dev = dev;
1215 
1216 			list_for_each_entry(mem, &dev->mem_props, list)
1217 				mem->gpu = dev->gpu;
1218 			list_for_each_entry(cache, &dev->cache_props, list)
1219 				cache->gpu = dev->gpu;
1220 			list_for_each_entry(iolink, &dev->io_link_props, list)
1221 				iolink->gpu = dev->gpu;
1222 			list_for_each_entry(p2plink, &dev->p2p_link_props, list)
1223 				p2plink->gpu = dev->gpu;
1224 			break;
1225 		}
1226 	}
1227 	return out_dev;
1228 }
1229 
1230 static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
1231 {
1232 	/*
1233 	 * TODO: Generate an event for thunk about the arrival/removal
1234 	 * of the GPU
1235 	 */
1236 }
1237 
1238 /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
1239  *		patch this after CRAT parsing.
1240  */
1241 static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
1242 {
1243 	struct kfd_mem_properties *mem;
1244 	struct kfd_local_mem_info local_mem_info;
1245 
1246 	if (!dev)
1247 		return;
1248 
1249 	/* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
1250 	 * single bank of VRAM local memory.
1251 	 * for dGPUs - VCRAT reports only one bank of Local Memory
1252 	 * for APUs - If CRAT from ACPI reports more than one bank, then
1253 	 *	all the banks will report the same mem_clk_max information
1254 	 */
1255 	amdgpu_amdkfd_get_local_mem_info(dev->gpu->adev, &local_mem_info);
1256 
1257 	list_for_each_entry(mem, &dev->mem_props, list)
1258 		mem->mem_clk_max = local_mem_info.mem_clk_max;
1259 }
1260 
1261 static void kfd_set_iolink_no_atomics(struct kfd_topology_device *dev,
1262 					struct kfd_topology_device *target_gpu_dev,
1263 					struct kfd_iolink_properties *link)
1264 {
1265 	/* xgmi always supports atomics between links. */
1266 	if (link->iolink_type == CRAT_IOLINK_TYPE_XGMI)
1267 		return;
1268 
1269 	/* check pcie support to set cpu(dev) flags for target_gpu_dev link. */
1270 	if (target_gpu_dev) {
1271 		uint32_t cap;
1272 
1273 		pcie_capability_read_dword(target_gpu_dev->gpu->adev->pdev,
1274 				PCI_EXP_DEVCAP2, &cap);
1275 
1276 		if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1277 			     PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1278 			link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1279 				CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1280 	/* set gpu (dev) flags. */
1281 	} else {
1282 		if (!dev->gpu->kfd->pci_atomic_requested ||
1283 				dev->gpu->adev->asic_type == CHIP_HAWAII)
1284 			link->flags |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1285 				CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1286 	}
1287 }
1288 
1289 static void kfd_set_iolink_non_coherent(struct kfd_topology_device *to_dev,
1290 		struct kfd_iolink_properties *outbound_link,
1291 		struct kfd_iolink_properties *inbound_link)
1292 {
1293 	/* CPU -> GPU with PCIe */
1294 	if (!to_dev->gpu &&
1295 	    inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS)
1296 		inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1297 
1298 	if (to_dev->gpu) {
1299 		/* GPU <-> GPU with PCIe and
1300 		 * Vega20 with XGMI
1301 		 */
1302 		if (inbound_link->iolink_type == CRAT_IOLINK_TYPE_PCIEXPRESS ||
1303 		    (inbound_link->iolink_type == CRAT_IOLINK_TYPE_XGMI &&
1304 		    KFD_GC_VERSION(to_dev->gpu) == IP_VERSION(9, 4, 0))) {
1305 			outbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1306 			inbound_link->flags |= CRAT_IOLINK_FLAGS_NON_COHERENT;
1307 		}
1308 	}
1309 }
1310 
1311 static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
1312 {
1313 	struct kfd_iolink_properties *link, *inbound_link;
1314 	struct kfd_topology_device *peer_dev;
1315 
1316 	if (!dev || !dev->gpu)
1317 		return;
1318 
1319 	/* GPU only creates direct links so apply flags setting to all */
1320 	list_for_each_entry(link, &dev->io_link_props, list) {
1321 		link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1322 		kfd_set_iolink_no_atomics(dev, NULL, link);
1323 		peer_dev = kfd_topology_device_by_proximity_domain(
1324 				link->node_to);
1325 
1326 		if (!peer_dev)
1327 			continue;
1328 
1329 		/* Include the CPU peer in GPU hive if connected over xGMI. */
1330 		if (!peer_dev->gpu &&
1331 		    link->iolink_type == CRAT_IOLINK_TYPE_XGMI) {
1332 			/*
1333 			 * If the GPU is not part of a GPU hive, use its pci
1334 			 * device location as the hive ID to bind with the CPU.
1335 			 */
1336 			if (!dev->node_props.hive_id)
1337 				dev->node_props.hive_id = pci_dev_id(dev->gpu->adev->pdev);
1338 			peer_dev->node_props.hive_id = dev->node_props.hive_id;
1339 		}
1340 
1341 		list_for_each_entry(inbound_link, &peer_dev->io_link_props,
1342 									list) {
1343 			if (inbound_link->node_to != link->node_from)
1344 				continue;
1345 
1346 			inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1347 			kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
1348 			kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
1349 		}
1350 	}
1351 
1352 	/* Create indirect links so apply flags setting to all */
1353 	list_for_each_entry(link, &dev->p2p_link_props, list) {
1354 		link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1355 		kfd_set_iolink_no_atomics(dev, NULL, link);
1356 		peer_dev = kfd_topology_device_by_proximity_domain(
1357 				link->node_to);
1358 
1359 		if (!peer_dev)
1360 			continue;
1361 
1362 		list_for_each_entry(inbound_link, &peer_dev->p2p_link_props,
1363 									list) {
1364 			if (inbound_link->node_to != link->node_from)
1365 				continue;
1366 
1367 			inbound_link->flags = CRAT_IOLINK_FLAGS_ENABLED;
1368 			kfd_set_iolink_no_atomics(peer_dev, dev, inbound_link);
1369 			kfd_set_iolink_non_coherent(peer_dev, link, inbound_link);
1370 		}
1371 	}
1372 }
1373 
1374 static int kfd_build_p2p_node_entry(struct kfd_topology_device *dev,
1375 				struct kfd_iolink_properties *p2plink)
1376 {
1377 	int ret;
1378 
1379 	p2plink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
1380 	if (!p2plink->kobj)
1381 		return -ENOMEM;
1382 
1383 	ret = kobject_init_and_add(p2plink->kobj, &iolink_type,
1384 			dev->kobj_p2plink, "%d", dev->node_props.p2p_links_count - 1);
1385 	if (ret < 0) {
1386 		kobject_put(p2plink->kobj);
1387 		return ret;
1388 	}
1389 
1390 	p2plink->attr.name = "properties";
1391 	p2plink->attr.mode = KFD_SYSFS_FILE_MODE;
1392 	sysfs_attr_init(&p2plink->attr);
1393 	ret = sysfs_create_file(p2plink->kobj, &p2plink->attr);
1394 	if (ret < 0)
1395 		return ret;
1396 
1397 	return 0;
1398 }
1399 
1400 static int kfd_create_indirect_link_prop(struct kfd_topology_device *kdev, int gpu_node)
1401 {
1402 	struct kfd_iolink_properties *gpu_link, *tmp_link, *cpu_link;
1403 	struct kfd_iolink_properties *props = NULL, *props2 = NULL;
1404 	struct kfd_topology_device *cpu_dev;
1405 	int ret = 0;
1406 	int i, num_cpu;
1407 
1408 	num_cpu = 0;
1409 	list_for_each_entry(cpu_dev, &topology_device_list, list) {
1410 		if (cpu_dev->gpu)
1411 			break;
1412 		num_cpu++;
1413 	}
1414 
1415 	gpu_link = list_first_entry(&kdev->io_link_props,
1416 					struct kfd_iolink_properties, list);
1417 	if (!gpu_link)
1418 		return -ENOMEM;
1419 
1420 	for (i = 0; i < num_cpu; i++) {
1421 		/* CPU <--> GPU */
1422 		if (gpu_link->node_to == i)
1423 			continue;
1424 
1425 		/* find CPU <-->  CPU links */
1426 		cpu_link = NULL;
1427 		cpu_dev = kfd_topology_device_by_proximity_domain(i);
1428 		if (cpu_dev) {
1429 			list_for_each_entry(tmp_link,
1430 					&cpu_dev->io_link_props, list) {
1431 				if (tmp_link->node_to == gpu_link->node_to) {
1432 					cpu_link = tmp_link;
1433 					break;
1434 				}
1435 			}
1436 		}
1437 
1438 		if (!cpu_link)
1439 			return -ENOMEM;
1440 
1441 		/* CPU <--> CPU <--> GPU, GPU node*/
1442 		props = kfd_alloc_struct(props);
1443 		if (!props)
1444 			return -ENOMEM;
1445 
1446 		memcpy(props, gpu_link, sizeof(struct kfd_iolink_properties));
1447 		props->weight = gpu_link->weight + cpu_link->weight;
1448 		props->min_latency = gpu_link->min_latency + cpu_link->min_latency;
1449 		props->max_latency = gpu_link->max_latency + cpu_link->max_latency;
1450 		props->min_bandwidth = min(gpu_link->min_bandwidth, cpu_link->min_bandwidth);
1451 		props->max_bandwidth = min(gpu_link->max_bandwidth, cpu_link->max_bandwidth);
1452 
1453 		props->node_from = gpu_node;
1454 		props->node_to = i;
1455 		kdev->node_props.p2p_links_count++;
1456 		list_add_tail(&props->list, &kdev->p2p_link_props);
1457 		ret = kfd_build_p2p_node_entry(kdev, props);
1458 		if (ret < 0)
1459 			return ret;
1460 
1461 		/* for small Bar, no CPU --> GPU in-direct links */
1462 		if (kfd_dev_is_large_bar(kdev->gpu)) {
1463 			/* CPU <--> CPU <--> GPU, CPU node*/
1464 			props2 = kfd_alloc_struct(props2);
1465 			if (!props2)
1466 				return -ENOMEM;
1467 
1468 			memcpy(props2, props, sizeof(struct kfd_iolink_properties));
1469 			props2->node_from = i;
1470 			props2->node_to = gpu_node;
1471 			props2->kobj = NULL;
1472 			cpu_dev->node_props.p2p_links_count++;
1473 			list_add_tail(&props2->list, &cpu_dev->p2p_link_props);
1474 			ret = kfd_build_p2p_node_entry(cpu_dev, props2);
1475 			if (ret < 0)
1476 				return ret;
1477 		}
1478 	}
1479 	return ret;
1480 }
1481 
1482 #if defined(CONFIG_HSA_AMD_P2P)
1483 static int kfd_add_peer_prop(struct kfd_topology_device *kdev,
1484 		struct kfd_topology_device *peer, int from, int to)
1485 {
1486 	struct kfd_iolink_properties *props = NULL;
1487 	struct kfd_iolink_properties *iolink1, *iolink2, *iolink3;
1488 	struct kfd_topology_device *cpu_dev;
1489 	int ret = 0;
1490 
1491 	if (!amdgpu_device_is_peer_accessible(
1492 				kdev->gpu->adev,
1493 				peer->gpu->adev))
1494 		return ret;
1495 
1496 	iolink1 = list_first_entry(&kdev->io_link_props,
1497 							struct kfd_iolink_properties, list);
1498 	if (!iolink1)
1499 		return -ENOMEM;
1500 
1501 	iolink2 = list_first_entry(&peer->io_link_props,
1502 							struct kfd_iolink_properties, list);
1503 	if (!iolink2)
1504 		return -ENOMEM;
1505 
1506 	props = kfd_alloc_struct(props);
1507 	if (!props)
1508 		return -ENOMEM;
1509 
1510 	memcpy(props, iolink1, sizeof(struct kfd_iolink_properties));
1511 
1512 	props->weight = iolink1->weight + iolink2->weight;
1513 	props->min_latency = iolink1->min_latency + iolink2->min_latency;
1514 	props->max_latency = iolink1->max_latency + iolink2->max_latency;
1515 	props->min_bandwidth = min(iolink1->min_bandwidth, iolink2->min_bandwidth);
1516 	props->max_bandwidth = min(iolink2->max_bandwidth, iolink2->max_bandwidth);
1517 
1518 	if (iolink1->node_to != iolink2->node_to) {
1519 		/* CPU->CPU  link*/
1520 		cpu_dev = kfd_topology_device_by_proximity_domain(iolink1->node_to);
1521 		if (cpu_dev) {
1522 			list_for_each_entry(iolink3, &cpu_dev->io_link_props, list)
1523 				if (iolink3->node_to == iolink2->node_to)
1524 					break;
1525 
1526 			props->weight += iolink3->weight;
1527 			props->min_latency += iolink3->min_latency;
1528 			props->max_latency += iolink3->max_latency;
1529 			props->min_bandwidth = min(props->min_bandwidth,
1530 							iolink3->min_bandwidth);
1531 			props->max_bandwidth = min(props->max_bandwidth,
1532 							iolink3->max_bandwidth);
1533 		} else {
1534 			WARN(1, "CPU node not found");
1535 		}
1536 	}
1537 
1538 	props->node_from = from;
1539 	props->node_to = to;
1540 	peer->node_props.p2p_links_count++;
1541 	list_add_tail(&props->list, &peer->p2p_link_props);
1542 	ret = kfd_build_p2p_node_entry(peer, props);
1543 
1544 	return ret;
1545 }
1546 #endif
1547 
1548 static int kfd_dev_create_p2p_links(void)
1549 {
1550 	struct kfd_topology_device *dev;
1551 	struct kfd_topology_device *new_dev;
1552 #if defined(CONFIG_HSA_AMD_P2P)
1553 	uint32_t i;
1554 #endif
1555 	uint32_t k;
1556 	int ret = 0;
1557 
1558 	k = 0;
1559 	list_for_each_entry(dev, &topology_device_list, list)
1560 		k++;
1561 	if (k < 2)
1562 		return 0;
1563 
1564 	new_dev = list_last_entry(&topology_device_list, struct kfd_topology_device, list);
1565 	if (WARN_ON(!new_dev->gpu))
1566 		return 0;
1567 
1568 	k--;
1569 
1570 	/* create in-direct links */
1571 	ret = kfd_create_indirect_link_prop(new_dev, k);
1572 	if (ret < 0)
1573 		goto out;
1574 
1575 	/* create p2p links */
1576 #if defined(CONFIG_HSA_AMD_P2P)
1577 	i = 0;
1578 	list_for_each_entry(dev, &topology_device_list, list) {
1579 		if (dev == new_dev)
1580 			break;
1581 		if (!dev->gpu || !dev->gpu->adev ||
1582 		    (dev->gpu->kfd->hive_id &&
1583 		     dev->gpu->kfd->hive_id == new_dev->gpu->kfd->hive_id))
1584 			goto next;
1585 
1586 		/* check if node(s) is/are peer accessible in one direction or bi-direction */
1587 		ret = kfd_add_peer_prop(new_dev, dev, i, k);
1588 		if (ret < 0)
1589 			goto out;
1590 
1591 		ret = kfd_add_peer_prop(dev, new_dev, k, i);
1592 		if (ret < 0)
1593 			goto out;
1594 next:
1595 		i++;
1596 	}
1597 #endif
1598 
1599 out:
1600 	return ret;
1601 }
1602 
1603 /* Helper function. See kfd_fill_gpu_cache_info for parameter description */
1604 static int fill_in_l1_pcache(struct kfd_cache_properties **props_ext,
1605 				struct kfd_gpu_cache_info *pcache_info,
1606 				struct kfd_cu_info *cu_info,
1607 				int cu_bitmask,
1608 				int cache_type, unsigned int cu_processor_id,
1609 				int cu_block)
1610 {
1611 	unsigned int cu_sibling_map_mask;
1612 	int first_active_cu;
1613 	struct kfd_cache_properties *pcache = NULL;
1614 
1615 	cu_sibling_map_mask = cu_bitmask;
1616 	cu_sibling_map_mask >>= cu_block;
1617 	cu_sibling_map_mask &= ((1 << pcache_info[cache_type].num_cu_shared) - 1);
1618 	first_active_cu = ffs(cu_sibling_map_mask);
1619 
1620 	/* CU could be inactive. In case of shared cache find the first active
1621 	 * CU. and incase of non-shared cache check if the CU is inactive. If
1622 	 * inactive active skip it
1623 	 */
1624 	if (first_active_cu) {
1625 		pcache = kfd_alloc_struct(pcache);
1626 		if (!pcache)
1627 			return -ENOMEM;
1628 
1629 		memset(pcache, 0, sizeof(struct kfd_cache_properties));
1630 		pcache->processor_id_low = cu_processor_id + (first_active_cu - 1);
1631 		pcache->cache_level = pcache_info[cache_type].cache_level;
1632 		pcache->cache_size = pcache_info[cache_type].cache_size;
1633 
1634 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_DATA_CACHE)
1635 			pcache->cache_type |= HSA_CACHE_TYPE_DATA;
1636 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_INST_CACHE)
1637 			pcache->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
1638 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_CPU_CACHE)
1639 			pcache->cache_type |= HSA_CACHE_TYPE_CPU;
1640 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
1641 			pcache->cache_type |= HSA_CACHE_TYPE_HSACU;
1642 
1643 		/* Sibling map is w.r.t processor_id_low, so shift out
1644 		 * inactive CU
1645 		 */
1646 		cu_sibling_map_mask =
1647 			cu_sibling_map_mask >> (first_active_cu - 1);
1648 
1649 		pcache->sibling_map[0] = (uint8_t)(cu_sibling_map_mask & 0xFF);
1650 		pcache->sibling_map[1] =
1651 				(uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
1652 		pcache->sibling_map[2] =
1653 				(uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
1654 		pcache->sibling_map[3] =
1655 				(uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
1656 
1657 		pcache->sibling_map_size = 4;
1658 		*props_ext = pcache;
1659 
1660 		return 0;
1661 	}
1662 	return 1;
1663 }
1664 
1665 /* Helper function. See kfd_fill_gpu_cache_info for parameter description */
1666 static int fill_in_l2_l3_pcache(struct kfd_cache_properties **props_ext,
1667 				struct kfd_gpu_cache_info *pcache_info,
1668 				struct kfd_cu_info *cu_info,
1669 				int cache_type, unsigned int cu_processor_id)
1670 {
1671 	unsigned int cu_sibling_map_mask;
1672 	int first_active_cu;
1673 	int i, j, k;
1674 	struct kfd_cache_properties *pcache = NULL;
1675 
1676 	cu_sibling_map_mask = cu_info->cu_bitmap[0][0];
1677 	cu_sibling_map_mask &=
1678 		((1 << pcache_info[cache_type].num_cu_shared) - 1);
1679 	first_active_cu = ffs(cu_sibling_map_mask);
1680 
1681 	/* CU could be inactive. In case of shared cache find the first active
1682 	 * CU. and incase of non-shared cache check if the CU is inactive. If
1683 	 * inactive active skip it
1684 	 */
1685 	if (first_active_cu) {
1686 		pcache = kfd_alloc_struct(pcache);
1687 		if (!pcache)
1688 			return -ENOMEM;
1689 
1690 		memset(pcache, 0, sizeof(struct kfd_cache_properties));
1691 		pcache->processor_id_low = cu_processor_id
1692 					+ (first_active_cu - 1);
1693 		pcache->cache_level = pcache_info[cache_type].cache_level;
1694 		pcache->cache_size = pcache_info[cache_type].cache_size;
1695 
1696 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_DATA_CACHE)
1697 			pcache->cache_type |= HSA_CACHE_TYPE_DATA;
1698 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_INST_CACHE)
1699 			pcache->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
1700 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_CPU_CACHE)
1701 			pcache->cache_type |= HSA_CACHE_TYPE_CPU;
1702 		if (pcache_info[cache_type].flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
1703 			pcache->cache_type |= HSA_CACHE_TYPE_HSACU;
1704 
1705 		/* Sibling map is w.r.t processor_id_low, so shift out
1706 		 * inactive CU
1707 		 */
1708 		cu_sibling_map_mask = cu_sibling_map_mask >> (first_active_cu - 1);
1709 		k = 0;
1710 
1711 		for (i = 0; i < cu_info->num_shader_engines; i++) {
1712 			for (j = 0; j < cu_info->num_shader_arrays_per_engine; j++) {
1713 				pcache->sibling_map[k] = (uint8_t)(cu_sibling_map_mask & 0xFF);
1714 				pcache->sibling_map[k+1] = (uint8_t)((cu_sibling_map_mask >> 8) & 0xFF);
1715 				pcache->sibling_map[k+2] = (uint8_t)((cu_sibling_map_mask >> 16) & 0xFF);
1716 				pcache->sibling_map[k+3] = (uint8_t)((cu_sibling_map_mask >> 24) & 0xFF);
1717 				k += 4;
1718 
1719 				cu_sibling_map_mask = cu_info->cu_bitmap[i % 4][j + i / 4];
1720 				cu_sibling_map_mask &= ((1 << pcache_info[cache_type].num_cu_shared) - 1);
1721 			}
1722 		}
1723 		pcache->sibling_map_size = k;
1724 		*props_ext = pcache;
1725 		return 0;
1726 	}
1727 	return 1;
1728 }
1729 
1730 #define KFD_MAX_CACHE_TYPES 6
1731 
1732 /* kfd_fill_cache_non_crat_info - Fill GPU cache info using kfd_gpu_cache_info
1733  * tables
1734  */
1735 static void kfd_fill_cache_non_crat_info(struct kfd_topology_device *dev, struct kfd_node *kdev)
1736 {
1737 	struct kfd_gpu_cache_info *pcache_info = NULL;
1738 	int i, j, k;
1739 	int ct = 0;
1740 	unsigned int cu_processor_id;
1741 	int ret;
1742 	unsigned int num_cu_shared;
1743 	struct kfd_cu_info cu_info;
1744 	struct kfd_cu_info *pcu_info;
1745 	int gpu_processor_id;
1746 	struct kfd_cache_properties *props_ext;
1747 	int num_of_entries = 0;
1748 	int num_of_cache_types = 0;
1749 	struct kfd_gpu_cache_info cache_info[KFD_MAX_CACHE_TYPES];
1750 
1751 	amdgpu_amdkfd_get_cu_info(kdev->adev, &cu_info);
1752 	pcu_info = &cu_info;
1753 
1754 	gpu_processor_id = dev->node_props.simd_id_base;
1755 
1756 	pcache_info = cache_info;
1757 	num_of_cache_types = kfd_get_gpu_cache_info(kdev, &pcache_info);
1758 	if (!num_of_cache_types) {
1759 		pr_warn("no cache info found\n");
1760 		return;
1761 	}
1762 
1763 	/* For each type of cache listed in the kfd_gpu_cache_info table,
1764 	 * go through all available Compute Units.
1765 	 * The [i,j,k] loop will
1766 	 *		if kfd_gpu_cache_info.num_cu_shared = 1
1767 	 *			will parse through all available CU
1768 	 *		If (kfd_gpu_cache_info.num_cu_shared != 1)
1769 	 *			then it will consider only one CU from
1770 	 *			the shared unit
1771 	 */
1772 	for (ct = 0; ct < num_of_cache_types; ct++) {
1773 		cu_processor_id = gpu_processor_id;
1774 		if (pcache_info[ct].cache_level == 1) {
1775 			for (i = 0; i < pcu_info->num_shader_engines; i++) {
1776 				for (j = 0; j < pcu_info->num_shader_arrays_per_engine; j++) {
1777 					for (k = 0; k < pcu_info->num_cu_per_sh; k += pcache_info[ct].num_cu_shared) {
1778 
1779 						ret = fill_in_l1_pcache(&props_ext, pcache_info, pcu_info,
1780 										pcu_info->cu_bitmap[i % 4][j + i / 4], ct,
1781 										cu_processor_id, k);
1782 
1783 						if (ret < 0)
1784 							break;
1785 
1786 						if (!ret) {
1787 							num_of_entries++;
1788 							list_add_tail(&props_ext->list, &dev->cache_props);
1789 						}
1790 
1791 						/* Move to next CU block */
1792 						num_cu_shared = ((k + pcache_info[ct].num_cu_shared) <=
1793 							pcu_info->num_cu_per_sh) ?
1794 							pcache_info[ct].num_cu_shared :
1795 							(pcu_info->num_cu_per_sh - k);
1796 						cu_processor_id += num_cu_shared;
1797 					}
1798 				}
1799 			}
1800 		} else {
1801 			ret = fill_in_l2_l3_pcache(&props_ext, pcache_info,
1802 								pcu_info, ct, cu_processor_id);
1803 
1804 			if (ret < 0)
1805 				break;
1806 
1807 			if (!ret) {
1808 				num_of_entries++;
1809 				list_add_tail(&props_ext->list, &dev->cache_props);
1810 			}
1811 		}
1812 	}
1813 	dev->node_props.caches_count += num_of_entries;
1814 	pr_debug("Added [%d] GPU cache entries\n", num_of_entries);
1815 }
1816 
1817 static int kfd_topology_add_device_locked(struct kfd_node *gpu, uint32_t gpu_id,
1818 					  struct kfd_topology_device **dev)
1819 {
1820 	int proximity_domain = ++topology_crat_proximity_domain;
1821 	struct list_head temp_topology_device_list;
1822 	void *crat_image = NULL;
1823 	size_t image_size = 0;
1824 	int res;
1825 
1826 	res = kfd_create_crat_image_virtual(&crat_image, &image_size,
1827 					    COMPUTE_UNIT_GPU, gpu,
1828 					    proximity_domain);
1829 	if (res) {
1830 		pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
1831 		       gpu_id);
1832 		topology_crat_proximity_domain--;
1833 		goto err;
1834 	}
1835 
1836 	INIT_LIST_HEAD(&temp_topology_device_list);
1837 
1838 	res = kfd_parse_crat_table(crat_image,
1839 				   &temp_topology_device_list,
1840 				   proximity_domain);
1841 	if (res) {
1842 		pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
1843 		       gpu_id);
1844 		topology_crat_proximity_domain--;
1845 		goto err;
1846 	}
1847 
1848 	kfd_topology_update_device_list(&temp_topology_device_list,
1849 					&topology_device_list);
1850 
1851 	*dev = kfd_assign_gpu(gpu);
1852 	if (WARN_ON(!*dev)) {
1853 		res = -ENODEV;
1854 		goto err;
1855 	}
1856 
1857 	/* Fill the cache affinity information here for the GPUs
1858 	 * using VCRAT
1859 	 */
1860 	kfd_fill_cache_non_crat_info(*dev, gpu);
1861 
1862 	/* Update the SYSFS tree, since we added another topology
1863 	 * device
1864 	 */
1865 	res = kfd_topology_update_sysfs();
1866 	if (!res)
1867 		sys_props.generation_count++;
1868 	else
1869 		pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
1870 		       gpu_id, res);
1871 
1872 err:
1873 	kfd_destroy_crat_image(crat_image);
1874 	return res;
1875 }
1876 
1877 int kfd_topology_add_device(struct kfd_node *gpu)
1878 {
1879 	uint32_t gpu_id;
1880 	struct kfd_topology_device *dev;
1881 	struct kfd_cu_info cu_info;
1882 	int res = 0;
1883 	int i;
1884 	const char *asic_name = amdgpu_asic_name[gpu->adev->asic_type];
1885 
1886 	gpu_id = kfd_generate_gpu_id(gpu);
1887 	pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1888 
1889 	/* Check to see if this gpu device exists in the topology_device_list.
1890 	 * If so, assign the gpu to that device,
1891 	 * else create a Virtual CRAT for this gpu device and then parse that
1892 	 * CRAT to create a new topology device. Once created assign the gpu to
1893 	 * that topology device
1894 	 */
1895 	down_write(&topology_lock);
1896 	dev = kfd_assign_gpu(gpu);
1897 	if (!dev)
1898 		res = kfd_topology_add_device_locked(gpu, gpu_id, &dev);
1899 	up_write(&topology_lock);
1900 	if (res)
1901 		return res;
1902 
1903 	dev->gpu_id = gpu_id;
1904 	gpu->id = gpu_id;
1905 
1906 	kfd_dev_create_p2p_links();
1907 
1908 	/* TODO: Move the following lines to function
1909 	 *	kfd_add_non_crat_information
1910 	 */
1911 
1912 	/* Fill-in additional information that is not available in CRAT but
1913 	 * needed for the topology
1914 	 */
1915 
1916 	amdgpu_amdkfd_get_cu_info(dev->gpu->adev, &cu_info);
1917 
1918 	for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1; i++) {
1919 		dev->node_props.name[i] = __tolower(asic_name[i]);
1920 		if (asic_name[i] == '\0')
1921 			break;
1922 	}
1923 	dev->node_props.name[i] = '\0';
1924 
1925 	dev->node_props.simd_arrays_per_engine =
1926 		cu_info.num_shader_arrays_per_engine;
1927 
1928 	dev->node_props.gfx_target_version =
1929 				gpu->kfd->device_info.gfx_target_version;
1930 	dev->node_props.vendor_id = gpu->adev->pdev->vendor;
1931 	dev->node_props.device_id = gpu->adev->pdev->device;
1932 	dev->node_props.capability |=
1933 		((dev->gpu->adev->rev_id << HSA_CAP_ASIC_REVISION_SHIFT) &
1934 			HSA_CAP_ASIC_REVISION_MASK);
1935 
1936 	dev->node_props.location_id = pci_dev_id(gpu->adev->pdev);
1937 	if (KFD_GC_VERSION(dev->gpu->kfd) == IP_VERSION(9, 4, 3))
1938 		dev->node_props.location_id |= dev->gpu->node_id;
1939 
1940 	dev->node_props.domain = pci_domain_nr(gpu->adev->pdev->bus);
1941 	dev->node_props.max_engine_clk_fcompute =
1942 		amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->adev);
1943 	dev->node_props.max_engine_clk_ccompute =
1944 		cpufreq_quick_get_max(0) / 1000;
1945 	dev->node_props.drm_render_minor =
1946 		gpu->kfd->shared_resources.drm_render_minor;
1947 
1948 	dev->node_props.hive_id = gpu->kfd->hive_id;
1949 	dev->node_props.num_sdma_engines = kfd_get_num_sdma_engines(gpu);
1950 	dev->node_props.num_sdma_xgmi_engines =
1951 					kfd_get_num_xgmi_sdma_engines(gpu);
1952 	dev->node_props.num_sdma_queues_per_engine =
1953 				gpu->kfd->device_info.num_sdma_queues_per_engine -
1954 				gpu->kfd->device_info.num_reserved_sdma_queues_per_engine;
1955 	dev->node_props.num_gws = (dev->gpu->gws &&
1956 		dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
1957 		dev->gpu->adev->gds.gws_size : 0;
1958 	dev->node_props.num_cp_queues = get_cp_queues_num(dev->gpu->dqm);
1959 
1960 	kfd_fill_mem_clk_max_info(dev);
1961 	kfd_fill_iolink_non_crat_info(dev);
1962 
1963 	switch (dev->gpu->adev->asic_type) {
1964 	case CHIP_KAVERI:
1965 	case CHIP_HAWAII:
1966 	case CHIP_TONGA:
1967 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
1968 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1969 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1970 		break;
1971 	case CHIP_CARRIZO:
1972 	case CHIP_FIJI:
1973 	case CHIP_POLARIS10:
1974 	case CHIP_POLARIS11:
1975 	case CHIP_POLARIS12:
1976 	case CHIP_VEGAM:
1977 		pr_debug("Adding doorbell packet type capability\n");
1978 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
1979 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1980 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1981 		break;
1982 	default:
1983 		if (KFD_GC_VERSION(dev->gpu) >= IP_VERSION(9, 0, 1))
1984 			dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1985 				HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1986 				HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1987 		else
1988 			WARN(1, "Unexpected ASIC family %u",
1989 			     dev->gpu->adev->asic_type);
1990 	}
1991 
1992 	/*
1993 	 * Overwrite ATS capability according to needs_iommu_device to fix
1994 	 * potential missing corresponding bit in CRAT of BIOS.
1995 	 */
1996 	if (dev->gpu->kfd->use_iommu_v2)
1997 		dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
1998 	else
1999 		dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;
2000 
2001 	/* Fix errors in CZ CRAT.
2002 	 * simd_count: Carrizo CRAT reports wrong simd_count, probably
2003 	 *		because it doesn't consider masked out CUs
2004 	 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
2005 	 */
2006 	if (dev->gpu->adev->asic_type == CHIP_CARRIZO) {
2007 		dev->node_props.simd_count =
2008 			cu_info.simd_per_cu * cu_info.cu_active_number;
2009 		dev->node_props.max_waves_per_simd = 10;
2010 	}
2011 
2012 	/* kfd only concerns sram ecc on GFX and HBM ecc on UMC */
2013 	dev->node_props.capability |=
2014 		((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0) ?
2015 		HSA_CAP_SRAM_EDCSUPPORTED : 0;
2016 	dev->node_props.capability |=
2017 		((dev->gpu->adev->ras_enabled & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
2018 		HSA_CAP_MEM_EDCSUPPORTED : 0;
2019 
2020 	if (KFD_GC_VERSION(dev->gpu) != IP_VERSION(9, 0, 1))
2021 		dev->node_props.capability |= (dev->gpu->adev->ras_enabled != 0) ?
2022 			HSA_CAP_RASEVENTNOTIFY : 0;
2023 
2024 	if (KFD_IS_SVM_API_SUPPORTED(dev->gpu->adev->kfd.dev))
2025 		dev->node_props.capability |= HSA_CAP_SVMAPI_SUPPORTED;
2026 
2027 	kfd_debug_print_topology();
2028 
2029 	kfd_notify_gpu_change(gpu_id, 1);
2030 
2031 	return 0;
2032 }
2033 
2034 /**
2035  * kfd_topology_update_io_links() - Update IO links after device removal.
2036  * @proximity_domain: Proximity domain value of the dev being removed.
2037  *
2038  * The topology list currently is arranged in increasing order of
2039  * proximity domain.
2040  *
2041  * Two things need to be done when a device is removed:
2042  * 1. All the IO links to this device need to be removed.
2043  * 2. All nodes after the current device node need to move
2044  *    up once this device node is removed from the topology
2045  *    list. As a result, the proximity domain values for
2046  *    all nodes after the node being deleted reduce by 1.
2047  *    This would also cause the proximity domain values for
2048  *    io links to be updated based on new proximity domain
2049  *    values.
2050  *
2051  * Context: The caller must hold write topology_lock.
2052  */
2053 static void kfd_topology_update_io_links(int proximity_domain)
2054 {
2055 	struct kfd_topology_device *dev;
2056 	struct kfd_iolink_properties *iolink, *p2plink, *tmp;
2057 
2058 	list_for_each_entry(dev, &topology_device_list, list) {
2059 		if (dev->proximity_domain > proximity_domain)
2060 			dev->proximity_domain--;
2061 
2062 		list_for_each_entry_safe(iolink, tmp, &dev->io_link_props, list) {
2063 			/*
2064 			 * If there is an io link to the dev being deleted
2065 			 * then remove that IO link also.
2066 			 */
2067 			if (iolink->node_to == proximity_domain) {
2068 				list_del(&iolink->list);
2069 				dev->node_props.io_links_count--;
2070 			} else {
2071 				if (iolink->node_from > proximity_domain)
2072 					iolink->node_from--;
2073 				if (iolink->node_to > proximity_domain)
2074 					iolink->node_to--;
2075 			}
2076 		}
2077 
2078 		list_for_each_entry_safe(p2plink, tmp, &dev->p2p_link_props, list) {
2079 			/*
2080 			 * If there is a p2p link to the dev being deleted
2081 			 * then remove that p2p link also.
2082 			 */
2083 			if (p2plink->node_to == proximity_domain) {
2084 				list_del(&p2plink->list);
2085 				dev->node_props.p2p_links_count--;
2086 			} else {
2087 				if (p2plink->node_from > proximity_domain)
2088 					p2plink->node_from--;
2089 				if (p2plink->node_to > proximity_domain)
2090 					p2plink->node_to--;
2091 			}
2092 		}
2093 	}
2094 }
2095 
2096 int kfd_topology_remove_device(struct kfd_node *gpu)
2097 {
2098 	struct kfd_topology_device *dev, *tmp;
2099 	uint32_t gpu_id;
2100 	int res = -ENODEV;
2101 	int i = 0;
2102 
2103 	down_write(&topology_lock);
2104 
2105 	list_for_each_entry_safe(dev, tmp, &topology_device_list, list) {
2106 		if (dev->gpu == gpu) {
2107 			gpu_id = dev->gpu_id;
2108 			kfd_remove_sysfs_node_entry(dev);
2109 			kfd_release_topology_device(dev);
2110 			sys_props.num_devices--;
2111 			kfd_topology_update_io_links(i);
2112 			topology_crat_proximity_domain = sys_props.num_devices-1;
2113 			sys_props.generation_count++;
2114 			res = 0;
2115 			if (kfd_topology_update_sysfs() < 0)
2116 				kfd_topology_release_sysfs();
2117 			break;
2118 		}
2119 		i++;
2120 	}
2121 
2122 	up_write(&topology_lock);
2123 
2124 	if (!res)
2125 		kfd_notify_gpu_change(gpu_id, 0);
2126 
2127 	return res;
2128 }
2129 
2130 /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
2131  *	topology. If GPU device is found @idx, then valid kfd_dev pointer is
2132  *	returned through @kdev
2133  * Return -	0: On success (@kdev will be NULL for non GPU nodes)
2134  *		-1: If end of list
2135  */
2136 int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_node **kdev)
2137 {
2138 
2139 	struct kfd_topology_device *top_dev;
2140 	uint8_t device_idx = 0;
2141 
2142 	*kdev = NULL;
2143 	down_read(&topology_lock);
2144 
2145 	list_for_each_entry(top_dev, &topology_device_list, list) {
2146 		if (device_idx == idx) {
2147 			*kdev = top_dev->gpu;
2148 			up_read(&topology_lock);
2149 			return 0;
2150 		}
2151 
2152 		device_idx++;
2153 	}
2154 
2155 	up_read(&topology_lock);
2156 
2157 	return -1;
2158 
2159 }
2160 
2161 static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
2162 {
2163 	int first_cpu_of_numa_node;
2164 
2165 	if (!cpumask || cpumask == cpu_none_mask)
2166 		return -1;
2167 	first_cpu_of_numa_node = cpumask_first(cpumask);
2168 	if (first_cpu_of_numa_node >= nr_cpu_ids)
2169 		return -1;
2170 #ifdef CONFIG_X86_64
2171 	return cpu_data(first_cpu_of_numa_node).apicid;
2172 #else
2173 	return first_cpu_of_numa_node;
2174 #endif
2175 }
2176 
2177 /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
2178  *	of the given NUMA node (numa_node_id)
2179  * Return -1 on failure
2180  */
2181 int kfd_numa_node_to_apic_id(int numa_node_id)
2182 {
2183 	if (numa_node_id == -1) {
2184 		pr_warn("Invalid NUMA Node. Use online CPU mask\n");
2185 		return kfd_cpumask_to_apic_id(cpu_online_mask);
2186 	}
2187 	return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
2188 }
2189 
2190 void kfd_double_confirm_iommu_support(struct kfd_dev *gpu)
2191 {
2192 	struct kfd_topology_device *dev;
2193 
2194 	gpu->use_iommu_v2 = false;
2195 
2196 	if (!gpu->device_info.needs_iommu_device)
2197 		return;
2198 
2199 	down_read(&topology_lock);
2200 
2201 	/* Only use IOMMUv2 if there is an APU topology node with no GPU
2202 	 * assigned yet. This GPU will be assigned to it.
2203 	 */
2204 	list_for_each_entry(dev, &topology_device_list, list)
2205 		if (dev->node_props.cpu_cores_count &&
2206 		    dev->node_props.simd_count &&
2207 		    !dev->gpu)
2208 			gpu->use_iommu_v2 = true;
2209 
2210 	up_read(&topology_lock);
2211 }
2212 
2213 #if defined(CONFIG_DEBUG_FS)
2214 
2215 int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
2216 {
2217 	struct kfd_topology_device *dev;
2218 	unsigned int i = 0;
2219 	int r = 0;
2220 
2221 	down_read(&topology_lock);
2222 
2223 	list_for_each_entry(dev, &topology_device_list, list) {
2224 		if (!dev->gpu) {
2225 			i++;
2226 			continue;
2227 		}
2228 
2229 		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
2230 		r = dqm_debugfs_hqds(m, dev->gpu->dqm);
2231 		if (r)
2232 			break;
2233 	}
2234 
2235 	up_read(&topology_lock);
2236 
2237 	return r;
2238 }
2239 
2240 int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
2241 {
2242 	struct kfd_topology_device *dev;
2243 	unsigned int i = 0;
2244 	int r = 0;
2245 
2246 	down_read(&topology_lock);
2247 
2248 	list_for_each_entry(dev, &topology_device_list, list) {
2249 		if (!dev->gpu) {
2250 			i++;
2251 			continue;
2252 		}
2253 
2254 		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
2255 		r = pm_debugfs_runlist(m, &dev->gpu->dqm->packet_mgr);
2256 		if (r)
2257 			break;
2258 	}
2259 
2260 	up_read(&topology_lock);
2261 
2262 	return r;
2263 }
2264 
2265 #endif
2266