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