xref: /openbmc/linux/drivers/gpu/drm/amd/amdkfd/kfd_topology.c (revision 87fcfa7b7fe6bf819033fe827a27f710e38639b5)
1 /*
2  * Copyright 2014 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/pci.h>
26 #include <linux/errno.h>
27 #include <linux/acpi.h>
28 #include <linux/hash.h>
29 #include <linux/cpufreq.h>
30 #include <linux/log2.h>
31 #include <linux/dmi.h>
32 #include <linux/atomic.h>
33 
34 #include "kfd_priv.h"
35 #include "kfd_crat.h"
36 #include "kfd_topology.h"
37 #include "kfd_device_queue_manager.h"
38 #include "kfd_iommu.h"
39 #include "amdgpu_amdkfd.h"
40 #include "amdgpu_ras.h"
41 
42 /* topology_device_list - Master list of all topology devices */
43 static struct list_head topology_device_list;
44 static struct kfd_system_properties sys_props;
45 
46 static DECLARE_RWSEM(topology_lock);
47 static atomic_t topology_crat_proximity_domain;
48 
49 struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
50 						uint32_t proximity_domain)
51 {
52 	struct kfd_topology_device *top_dev;
53 	struct kfd_topology_device *device = NULL;
54 
55 	down_read(&topology_lock);
56 
57 	list_for_each_entry(top_dev, &topology_device_list, list)
58 		if (top_dev->proximity_domain == proximity_domain) {
59 			device = top_dev;
60 			break;
61 		}
62 
63 	up_read(&topology_lock);
64 
65 	return device;
66 }
67 
68 struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id)
69 {
70 	struct kfd_topology_device *top_dev = NULL;
71 	struct kfd_topology_device *ret = NULL;
72 
73 	down_read(&topology_lock);
74 
75 	list_for_each_entry(top_dev, &topology_device_list, list)
76 		if (top_dev->gpu_id == gpu_id) {
77 			ret = top_dev;
78 			break;
79 		}
80 
81 	up_read(&topology_lock);
82 
83 	return ret;
84 }
85 
86 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
87 {
88 	struct kfd_topology_device *top_dev;
89 
90 	top_dev = kfd_topology_device_by_id(gpu_id);
91 	if (!top_dev)
92 		return NULL;
93 
94 	return top_dev->gpu;
95 }
96 
97 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
98 {
99 	struct kfd_topology_device *top_dev;
100 	struct kfd_dev *device = NULL;
101 
102 	down_read(&topology_lock);
103 
104 	list_for_each_entry(top_dev, &topology_device_list, list)
105 		if (top_dev->gpu && top_dev->gpu->pdev == pdev) {
106 			device = top_dev->gpu;
107 			break;
108 		}
109 
110 	up_read(&topology_lock);
111 
112 	return device;
113 }
114 
115 struct kfd_dev *kfd_device_by_kgd(const struct kgd_dev *kgd)
116 {
117 	struct kfd_topology_device *top_dev;
118 	struct kfd_dev *device = NULL;
119 
120 	down_read(&topology_lock);
121 
122 	list_for_each_entry(top_dev, &topology_device_list, list)
123 		if (top_dev->gpu && top_dev->gpu->kgd == kgd) {
124 			device = top_dev->gpu;
125 			break;
126 		}
127 
128 	up_read(&topology_lock);
129 
130 	return device;
131 }
132 
133 /* Called with write topology_lock acquired */
134 static void kfd_release_topology_device(struct kfd_topology_device *dev)
135 {
136 	struct kfd_mem_properties *mem;
137 	struct kfd_cache_properties *cache;
138 	struct kfd_iolink_properties *iolink;
139 	struct kfd_perf_properties *perf;
140 
141 	list_del(&dev->list);
142 
143 	while (dev->mem_props.next != &dev->mem_props) {
144 		mem = container_of(dev->mem_props.next,
145 				struct kfd_mem_properties, list);
146 		list_del(&mem->list);
147 		kfree(mem);
148 	}
149 
150 	while (dev->cache_props.next != &dev->cache_props) {
151 		cache = container_of(dev->cache_props.next,
152 				struct kfd_cache_properties, list);
153 		list_del(&cache->list);
154 		kfree(cache);
155 	}
156 
157 	while (dev->io_link_props.next != &dev->io_link_props) {
158 		iolink = container_of(dev->io_link_props.next,
159 				struct kfd_iolink_properties, list);
160 		list_del(&iolink->list);
161 		kfree(iolink);
162 	}
163 
164 	while (dev->perf_props.next != &dev->perf_props) {
165 		perf = container_of(dev->perf_props.next,
166 				struct kfd_perf_properties, list);
167 		list_del(&perf->list);
168 		kfree(perf);
169 	}
170 
171 	kfree(dev);
172 }
173 
174 void kfd_release_topology_device_list(struct list_head *device_list)
175 {
176 	struct kfd_topology_device *dev;
177 
178 	while (!list_empty(device_list)) {
179 		dev = list_first_entry(device_list,
180 				       struct kfd_topology_device, list);
181 		kfd_release_topology_device(dev);
182 	}
183 }
184 
185 static void kfd_release_live_view(void)
186 {
187 	kfd_release_topology_device_list(&topology_device_list);
188 	memset(&sys_props, 0, sizeof(sys_props));
189 }
190 
191 struct kfd_topology_device *kfd_create_topology_device(
192 				struct list_head *device_list)
193 {
194 	struct kfd_topology_device *dev;
195 
196 	dev = kfd_alloc_struct(dev);
197 	if (!dev) {
198 		pr_err("No memory to allocate a topology device");
199 		return NULL;
200 	}
201 
202 	INIT_LIST_HEAD(&dev->mem_props);
203 	INIT_LIST_HEAD(&dev->cache_props);
204 	INIT_LIST_HEAD(&dev->io_link_props);
205 	INIT_LIST_HEAD(&dev->perf_props);
206 
207 	list_add_tail(&dev->list, device_list);
208 
209 	return dev;
210 }
211 
212 
213 #define sysfs_show_gen_prop(buffer, fmt, ...) \
214 		snprintf(buffer, PAGE_SIZE, "%s"fmt, buffer, __VA_ARGS__)
215 #define sysfs_show_32bit_prop(buffer, name, value) \
216 		sysfs_show_gen_prop(buffer, "%s %u\n", name, value)
217 #define sysfs_show_64bit_prop(buffer, name, value) \
218 		sysfs_show_gen_prop(buffer, "%s %llu\n", name, value)
219 #define sysfs_show_32bit_val(buffer, value) \
220 		sysfs_show_gen_prop(buffer, "%u\n", value)
221 #define sysfs_show_str_val(buffer, value) \
222 		sysfs_show_gen_prop(buffer, "%s\n", value)
223 
224 static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
225 		char *buffer)
226 {
227 	ssize_t ret;
228 
229 	/* Making sure that the buffer is an empty string */
230 	buffer[0] = 0;
231 
232 	if (attr == &sys_props.attr_genid) {
233 		ret = sysfs_show_32bit_val(buffer, sys_props.generation_count);
234 	} else if (attr == &sys_props.attr_props) {
235 		sysfs_show_64bit_prop(buffer, "platform_oem",
236 				sys_props.platform_oem);
237 		sysfs_show_64bit_prop(buffer, "platform_id",
238 				sys_props.platform_id);
239 		ret = sysfs_show_64bit_prop(buffer, "platform_rev",
240 				sys_props.platform_rev);
241 	} else {
242 		ret = -EINVAL;
243 	}
244 
245 	return ret;
246 }
247 
248 static void kfd_topology_kobj_release(struct kobject *kobj)
249 {
250 	kfree(kobj);
251 }
252 
253 static const struct sysfs_ops sysprops_ops = {
254 	.show = sysprops_show,
255 };
256 
257 static struct kobj_type sysprops_type = {
258 	.release = kfd_topology_kobj_release,
259 	.sysfs_ops = &sysprops_ops,
260 };
261 
262 static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
263 		char *buffer)
264 {
265 	ssize_t ret;
266 	struct kfd_iolink_properties *iolink;
267 
268 	/* Making sure that the buffer is an empty string */
269 	buffer[0] = 0;
270 
271 	iolink = container_of(attr, struct kfd_iolink_properties, attr);
272 	if (iolink->gpu && kfd_devcgroup_check_permission(iolink->gpu))
273 		return -EPERM;
274 	sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
275 	sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
276 	sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
277 	sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
278 	sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
279 	sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
280 	sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
281 	sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
282 	sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
283 	sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
284 	sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
285 			iolink->rec_transfer_size);
286 	ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
287 
288 	return ret;
289 }
290 
291 static const struct sysfs_ops iolink_ops = {
292 	.show = iolink_show,
293 };
294 
295 static struct kobj_type iolink_type = {
296 	.release = kfd_topology_kobj_release,
297 	.sysfs_ops = &iolink_ops,
298 };
299 
300 static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
301 		char *buffer)
302 {
303 	ssize_t ret;
304 	struct kfd_mem_properties *mem;
305 
306 	/* Making sure that the buffer is an empty string */
307 	buffer[0] = 0;
308 
309 	mem = container_of(attr, struct kfd_mem_properties, attr);
310 	if (mem->gpu && kfd_devcgroup_check_permission(mem->gpu))
311 		return -EPERM;
312 	sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
313 	sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
314 	sysfs_show_32bit_prop(buffer, "flags", mem->flags);
315 	sysfs_show_32bit_prop(buffer, "width", mem->width);
316 	ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
317 
318 	return ret;
319 }
320 
321 static const struct sysfs_ops mem_ops = {
322 	.show = mem_show,
323 };
324 
325 static struct kobj_type mem_type = {
326 	.release = kfd_topology_kobj_release,
327 	.sysfs_ops = &mem_ops,
328 };
329 
330 static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
331 		char *buffer)
332 {
333 	ssize_t ret;
334 	uint32_t i, j;
335 	struct kfd_cache_properties *cache;
336 
337 	/* Making sure that the buffer is an empty string */
338 	buffer[0] = 0;
339 
340 	cache = container_of(attr, struct kfd_cache_properties, attr);
341 	if (cache->gpu && kfd_devcgroup_check_permission(cache->gpu))
342 		return -EPERM;
343 	sysfs_show_32bit_prop(buffer, "processor_id_low",
344 			cache->processor_id_low);
345 	sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
346 	sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
347 	sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
348 	sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
349 			cache->cachelines_per_tag);
350 	sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
351 	sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
352 	sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
353 	snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
354 	for (i = 0; i < CRAT_SIBLINGMAP_SIZE; i++)
355 		for (j = 0; j < sizeof(cache->sibling_map[0])*8; j++) {
356 			/* Check each bit */
357 			if (cache->sibling_map[i] & (1 << j))
358 				ret = snprintf(buffer, PAGE_SIZE,
359 					 "%s%d%s", buffer, 1, ",");
360 			else
361 				ret = snprintf(buffer, PAGE_SIZE,
362 					 "%s%d%s", buffer, 0, ",");
363 		}
364 	/* Replace the last "," with end of line */
365 	*(buffer + strlen(buffer) - 1) = 0xA;
366 	return ret;
367 }
368 
369 static const struct sysfs_ops cache_ops = {
370 	.show = kfd_cache_show,
371 };
372 
373 static struct kobj_type cache_type = {
374 	.release = kfd_topology_kobj_release,
375 	.sysfs_ops = &cache_ops,
376 };
377 
378 /****** Sysfs of Performance Counters ******/
379 
380 struct kfd_perf_attr {
381 	struct kobj_attribute attr;
382 	uint32_t data;
383 };
384 
385 static ssize_t perf_show(struct kobject *kobj, struct kobj_attribute *attrs,
386 			char *buf)
387 {
388 	struct kfd_perf_attr *attr;
389 
390 	buf[0] = 0;
391 	attr = container_of(attrs, struct kfd_perf_attr, attr);
392 	if (!attr->data) /* invalid data for PMC */
393 		return 0;
394 	else
395 		return sysfs_show_32bit_val(buf, attr->data);
396 }
397 
398 #define KFD_PERF_DESC(_name, _data)			\
399 {							\
400 	.attr  = __ATTR(_name, 0444, perf_show, NULL),	\
401 	.data = _data,					\
402 }
403 
404 static struct kfd_perf_attr perf_attr_iommu[] = {
405 	KFD_PERF_DESC(max_concurrent, 0),
406 	KFD_PERF_DESC(num_counters, 0),
407 	KFD_PERF_DESC(counter_ids, 0),
408 };
409 /****************************************/
410 
411 static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
412 		char *buffer)
413 {
414 	struct kfd_topology_device *dev;
415 	uint32_t log_max_watch_addr;
416 
417 	/* Making sure that the buffer is an empty string */
418 	buffer[0] = 0;
419 
420 	if (strcmp(attr->name, "gpu_id") == 0) {
421 		dev = container_of(attr, struct kfd_topology_device,
422 				attr_gpuid);
423 		if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
424 			return -EPERM;
425 		return sysfs_show_32bit_val(buffer, dev->gpu_id);
426 	}
427 
428 	if (strcmp(attr->name, "name") == 0) {
429 		dev = container_of(attr, struct kfd_topology_device,
430 				attr_name);
431 
432 		if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
433 			return -EPERM;
434 		return sysfs_show_str_val(buffer, dev->node_props.name);
435 	}
436 
437 	dev = container_of(attr, struct kfd_topology_device,
438 			attr_props);
439 	if (dev->gpu && kfd_devcgroup_check_permission(dev->gpu))
440 		return -EPERM;
441 	sysfs_show_32bit_prop(buffer, "cpu_cores_count",
442 			dev->node_props.cpu_cores_count);
443 	sysfs_show_32bit_prop(buffer, "simd_count",
444 			dev->node_props.simd_count);
445 	sysfs_show_32bit_prop(buffer, "mem_banks_count",
446 			dev->node_props.mem_banks_count);
447 	sysfs_show_32bit_prop(buffer, "caches_count",
448 			dev->node_props.caches_count);
449 	sysfs_show_32bit_prop(buffer, "io_links_count",
450 			dev->node_props.io_links_count);
451 	sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
452 			dev->node_props.cpu_core_id_base);
453 	sysfs_show_32bit_prop(buffer, "simd_id_base",
454 			dev->node_props.simd_id_base);
455 	sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
456 			dev->node_props.max_waves_per_simd);
457 	sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
458 			dev->node_props.lds_size_in_kb);
459 	sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
460 			dev->node_props.gds_size_in_kb);
461 	sysfs_show_32bit_prop(buffer, "num_gws",
462 			dev->node_props.num_gws);
463 	sysfs_show_32bit_prop(buffer, "wave_front_size",
464 			dev->node_props.wave_front_size);
465 	sysfs_show_32bit_prop(buffer, "array_count",
466 			dev->node_props.array_count);
467 	sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
468 			dev->node_props.simd_arrays_per_engine);
469 	sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
470 			dev->node_props.cu_per_simd_array);
471 	sysfs_show_32bit_prop(buffer, "simd_per_cu",
472 			dev->node_props.simd_per_cu);
473 	sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
474 			dev->node_props.max_slots_scratch_cu);
475 	sysfs_show_32bit_prop(buffer, "vendor_id",
476 			dev->node_props.vendor_id);
477 	sysfs_show_32bit_prop(buffer, "device_id",
478 			dev->node_props.device_id);
479 	sysfs_show_32bit_prop(buffer, "location_id",
480 			dev->node_props.location_id);
481 	sysfs_show_32bit_prop(buffer, "drm_render_minor",
482 			dev->node_props.drm_render_minor);
483 	sysfs_show_64bit_prop(buffer, "hive_id",
484 			dev->node_props.hive_id);
485 	sysfs_show_32bit_prop(buffer, "num_sdma_engines",
486 			dev->node_props.num_sdma_engines);
487 	sysfs_show_32bit_prop(buffer, "num_sdma_xgmi_engines",
488 			dev->node_props.num_sdma_xgmi_engines);
489 	sysfs_show_32bit_prop(buffer, "num_sdma_queues_per_engine",
490 			dev->node_props.num_sdma_queues_per_engine);
491 	sysfs_show_32bit_prop(buffer, "num_cp_queues",
492 			dev->node_props.num_cp_queues);
493 
494 	if (dev->gpu) {
495 		log_max_watch_addr =
496 			__ilog2_u32(dev->gpu->device_info->num_of_watch_points);
497 
498 		if (log_max_watch_addr) {
499 			dev->node_props.capability |=
500 					HSA_CAP_WATCH_POINTS_SUPPORTED;
501 
502 			dev->node_props.capability |=
503 				((log_max_watch_addr <<
504 					HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
505 				HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
506 		}
507 
508 		if (dev->gpu->device_info->asic_family == CHIP_TONGA)
509 			dev->node_props.capability |=
510 					HSA_CAP_AQL_QUEUE_DOUBLE_MAP;
511 
512 		sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
513 			dev->node_props.max_engine_clk_fcompute);
514 
515 		sysfs_show_64bit_prop(buffer, "local_mem_size",
516 				(unsigned long long int) 0);
517 
518 		sysfs_show_32bit_prop(buffer, "fw_version",
519 				dev->gpu->mec_fw_version);
520 		sysfs_show_32bit_prop(buffer, "capability",
521 				dev->node_props.capability);
522 		sysfs_show_32bit_prop(buffer, "sdma_fw_version",
523 				dev->gpu->sdma_fw_version);
524 	}
525 
526 	return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
527 					cpufreq_quick_get_max(0)/1000);
528 }
529 
530 static const struct sysfs_ops node_ops = {
531 	.show = node_show,
532 };
533 
534 static struct kobj_type node_type = {
535 	.release = kfd_topology_kobj_release,
536 	.sysfs_ops = &node_ops,
537 };
538 
539 static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
540 {
541 	sysfs_remove_file(kobj, attr);
542 	kobject_del(kobj);
543 	kobject_put(kobj);
544 }
545 
546 static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
547 {
548 	struct kfd_iolink_properties *iolink;
549 	struct kfd_cache_properties *cache;
550 	struct kfd_mem_properties *mem;
551 	struct kfd_perf_properties *perf;
552 
553 	if (dev->kobj_iolink) {
554 		list_for_each_entry(iolink, &dev->io_link_props, list)
555 			if (iolink->kobj) {
556 				kfd_remove_sysfs_file(iolink->kobj,
557 							&iolink->attr);
558 				iolink->kobj = NULL;
559 			}
560 		kobject_del(dev->kobj_iolink);
561 		kobject_put(dev->kobj_iolink);
562 		dev->kobj_iolink = NULL;
563 	}
564 
565 	if (dev->kobj_cache) {
566 		list_for_each_entry(cache, &dev->cache_props, list)
567 			if (cache->kobj) {
568 				kfd_remove_sysfs_file(cache->kobj,
569 							&cache->attr);
570 				cache->kobj = NULL;
571 			}
572 		kobject_del(dev->kobj_cache);
573 		kobject_put(dev->kobj_cache);
574 		dev->kobj_cache = NULL;
575 	}
576 
577 	if (dev->kobj_mem) {
578 		list_for_each_entry(mem, &dev->mem_props, list)
579 			if (mem->kobj) {
580 				kfd_remove_sysfs_file(mem->kobj, &mem->attr);
581 				mem->kobj = NULL;
582 			}
583 		kobject_del(dev->kobj_mem);
584 		kobject_put(dev->kobj_mem);
585 		dev->kobj_mem = NULL;
586 	}
587 
588 	if (dev->kobj_perf) {
589 		list_for_each_entry(perf, &dev->perf_props, list) {
590 			kfree(perf->attr_group);
591 			perf->attr_group = NULL;
592 		}
593 		kobject_del(dev->kobj_perf);
594 		kobject_put(dev->kobj_perf);
595 		dev->kobj_perf = NULL;
596 	}
597 
598 	if (dev->kobj_node) {
599 		sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
600 		sysfs_remove_file(dev->kobj_node, &dev->attr_name);
601 		sysfs_remove_file(dev->kobj_node, &dev->attr_props);
602 		kobject_del(dev->kobj_node);
603 		kobject_put(dev->kobj_node);
604 		dev->kobj_node = NULL;
605 	}
606 }
607 
608 static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
609 		uint32_t id)
610 {
611 	struct kfd_iolink_properties *iolink;
612 	struct kfd_cache_properties *cache;
613 	struct kfd_mem_properties *mem;
614 	struct kfd_perf_properties *perf;
615 	int ret;
616 	uint32_t i, num_attrs;
617 	struct attribute **attrs;
618 
619 	if (WARN_ON(dev->kobj_node))
620 		return -EEXIST;
621 
622 	/*
623 	 * Creating the sysfs folders
624 	 */
625 	dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
626 	if (!dev->kobj_node)
627 		return -ENOMEM;
628 
629 	ret = kobject_init_and_add(dev->kobj_node, &node_type,
630 			sys_props.kobj_nodes, "%d", id);
631 	if (ret < 0)
632 		return ret;
633 
634 	dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
635 	if (!dev->kobj_mem)
636 		return -ENOMEM;
637 
638 	dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
639 	if (!dev->kobj_cache)
640 		return -ENOMEM;
641 
642 	dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
643 	if (!dev->kobj_iolink)
644 		return -ENOMEM;
645 
646 	dev->kobj_perf = kobject_create_and_add("perf", dev->kobj_node);
647 	if (!dev->kobj_perf)
648 		return -ENOMEM;
649 
650 	/*
651 	 * Creating sysfs files for node properties
652 	 */
653 	dev->attr_gpuid.name = "gpu_id";
654 	dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
655 	sysfs_attr_init(&dev->attr_gpuid);
656 	dev->attr_name.name = "name";
657 	dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
658 	sysfs_attr_init(&dev->attr_name);
659 	dev->attr_props.name = "properties";
660 	dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
661 	sysfs_attr_init(&dev->attr_props);
662 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
663 	if (ret < 0)
664 		return ret;
665 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
666 	if (ret < 0)
667 		return ret;
668 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
669 	if (ret < 0)
670 		return ret;
671 
672 	i = 0;
673 	list_for_each_entry(mem, &dev->mem_props, list) {
674 		mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
675 		if (!mem->kobj)
676 			return -ENOMEM;
677 		ret = kobject_init_and_add(mem->kobj, &mem_type,
678 				dev->kobj_mem, "%d", i);
679 		if (ret < 0)
680 			return ret;
681 
682 		mem->attr.name = "properties";
683 		mem->attr.mode = KFD_SYSFS_FILE_MODE;
684 		sysfs_attr_init(&mem->attr);
685 		ret = sysfs_create_file(mem->kobj, &mem->attr);
686 		if (ret < 0)
687 			return ret;
688 		i++;
689 	}
690 
691 	i = 0;
692 	list_for_each_entry(cache, &dev->cache_props, list) {
693 		cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
694 		if (!cache->kobj)
695 			return -ENOMEM;
696 		ret = kobject_init_and_add(cache->kobj, &cache_type,
697 				dev->kobj_cache, "%d", i);
698 		if (ret < 0)
699 			return ret;
700 
701 		cache->attr.name = "properties";
702 		cache->attr.mode = KFD_SYSFS_FILE_MODE;
703 		sysfs_attr_init(&cache->attr);
704 		ret = sysfs_create_file(cache->kobj, &cache->attr);
705 		if (ret < 0)
706 			return ret;
707 		i++;
708 	}
709 
710 	i = 0;
711 	list_for_each_entry(iolink, &dev->io_link_props, list) {
712 		iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
713 		if (!iolink->kobj)
714 			return -ENOMEM;
715 		ret = kobject_init_and_add(iolink->kobj, &iolink_type,
716 				dev->kobj_iolink, "%d", i);
717 		if (ret < 0)
718 			return ret;
719 
720 		iolink->attr.name = "properties";
721 		iolink->attr.mode = KFD_SYSFS_FILE_MODE;
722 		sysfs_attr_init(&iolink->attr);
723 		ret = sysfs_create_file(iolink->kobj, &iolink->attr);
724 		if (ret < 0)
725 			return ret;
726 		i++;
727 	}
728 
729 	/* All hardware blocks have the same number of attributes. */
730 	num_attrs = ARRAY_SIZE(perf_attr_iommu);
731 	list_for_each_entry(perf, &dev->perf_props, list) {
732 		perf->attr_group = kzalloc(sizeof(struct kfd_perf_attr)
733 			* num_attrs + sizeof(struct attribute_group),
734 			GFP_KERNEL);
735 		if (!perf->attr_group)
736 			return -ENOMEM;
737 
738 		attrs = (struct attribute **)(perf->attr_group + 1);
739 		if (!strcmp(perf->block_name, "iommu")) {
740 		/* Information of IOMMU's num_counters and counter_ids is shown
741 		 * under /sys/bus/event_source/devices/amd_iommu. We don't
742 		 * duplicate here.
743 		 */
744 			perf_attr_iommu[0].data = perf->max_concurrent;
745 			for (i = 0; i < num_attrs; i++)
746 				attrs[i] = &perf_attr_iommu[i].attr.attr;
747 		}
748 		perf->attr_group->name = perf->block_name;
749 		perf->attr_group->attrs = attrs;
750 		ret = sysfs_create_group(dev->kobj_perf, perf->attr_group);
751 		if (ret < 0)
752 			return ret;
753 	}
754 
755 	return 0;
756 }
757 
758 /* Called with write topology lock acquired */
759 static int kfd_build_sysfs_node_tree(void)
760 {
761 	struct kfd_topology_device *dev;
762 	int ret;
763 	uint32_t i = 0;
764 
765 	list_for_each_entry(dev, &topology_device_list, list) {
766 		ret = kfd_build_sysfs_node_entry(dev, i);
767 		if (ret < 0)
768 			return ret;
769 		i++;
770 	}
771 
772 	return 0;
773 }
774 
775 /* Called with write topology lock acquired */
776 static void kfd_remove_sysfs_node_tree(void)
777 {
778 	struct kfd_topology_device *dev;
779 
780 	list_for_each_entry(dev, &topology_device_list, list)
781 		kfd_remove_sysfs_node_entry(dev);
782 }
783 
784 static int kfd_topology_update_sysfs(void)
785 {
786 	int ret;
787 
788 	pr_info("Creating topology SYSFS entries\n");
789 	if (!sys_props.kobj_topology) {
790 		sys_props.kobj_topology =
791 				kfd_alloc_struct(sys_props.kobj_topology);
792 		if (!sys_props.kobj_topology)
793 			return -ENOMEM;
794 
795 		ret = kobject_init_and_add(sys_props.kobj_topology,
796 				&sysprops_type,  &kfd_device->kobj,
797 				"topology");
798 		if (ret < 0)
799 			return ret;
800 
801 		sys_props.kobj_nodes = kobject_create_and_add("nodes",
802 				sys_props.kobj_topology);
803 		if (!sys_props.kobj_nodes)
804 			return -ENOMEM;
805 
806 		sys_props.attr_genid.name = "generation_id";
807 		sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
808 		sysfs_attr_init(&sys_props.attr_genid);
809 		ret = sysfs_create_file(sys_props.kobj_topology,
810 				&sys_props.attr_genid);
811 		if (ret < 0)
812 			return ret;
813 
814 		sys_props.attr_props.name = "system_properties";
815 		sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
816 		sysfs_attr_init(&sys_props.attr_props);
817 		ret = sysfs_create_file(sys_props.kobj_topology,
818 				&sys_props.attr_props);
819 		if (ret < 0)
820 			return ret;
821 	}
822 
823 	kfd_remove_sysfs_node_tree();
824 
825 	return kfd_build_sysfs_node_tree();
826 }
827 
828 static void kfd_topology_release_sysfs(void)
829 {
830 	kfd_remove_sysfs_node_tree();
831 	if (sys_props.kobj_topology) {
832 		sysfs_remove_file(sys_props.kobj_topology,
833 				&sys_props.attr_genid);
834 		sysfs_remove_file(sys_props.kobj_topology,
835 				&sys_props.attr_props);
836 		if (sys_props.kobj_nodes) {
837 			kobject_del(sys_props.kobj_nodes);
838 			kobject_put(sys_props.kobj_nodes);
839 			sys_props.kobj_nodes = NULL;
840 		}
841 		kobject_del(sys_props.kobj_topology);
842 		kobject_put(sys_props.kobj_topology);
843 		sys_props.kobj_topology = NULL;
844 	}
845 }
846 
847 /* Called with write topology_lock acquired */
848 static void kfd_topology_update_device_list(struct list_head *temp_list,
849 					struct list_head *master_list)
850 {
851 	while (!list_empty(temp_list)) {
852 		list_move_tail(temp_list->next, master_list);
853 		sys_props.num_devices++;
854 	}
855 }
856 
857 static void kfd_debug_print_topology(void)
858 {
859 	struct kfd_topology_device *dev;
860 
861 	down_read(&topology_lock);
862 
863 	dev = list_last_entry(&topology_device_list,
864 			struct kfd_topology_device, list);
865 	if (dev) {
866 		if (dev->node_props.cpu_cores_count &&
867 				dev->node_props.simd_count) {
868 			pr_info("Topology: Add APU node [0x%0x:0x%0x]\n",
869 				dev->node_props.device_id,
870 				dev->node_props.vendor_id);
871 		} else if (dev->node_props.cpu_cores_count)
872 			pr_info("Topology: Add CPU node\n");
873 		else if (dev->node_props.simd_count)
874 			pr_info("Topology: Add dGPU node [0x%0x:0x%0x]\n",
875 				dev->node_props.device_id,
876 				dev->node_props.vendor_id);
877 	}
878 	up_read(&topology_lock);
879 }
880 
881 /* Helper function for intializing platform_xx members of
882  * kfd_system_properties. Uses OEM info from the last CPU/APU node.
883  */
884 static void kfd_update_system_properties(void)
885 {
886 	struct kfd_topology_device *dev;
887 
888 	down_read(&topology_lock);
889 	dev = list_last_entry(&topology_device_list,
890 			struct kfd_topology_device, list);
891 	if (dev) {
892 		sys_props.platform_id =
893 			(*((uint64_t *)dev->oem_id)) & CRAT_OEMID_64BIT_MASK;
894 		sys_props.platform_oem = *((uint64_t *)dev->oem_table_id);
895 		sys_props.platform_rev = dev->oem_revision;
896 	}
897 	up_read(&topology_lock);
898 }
899 
900 static void find_system_memory(const struct dmi_header *dm,
901 	void *private)
902 {
903 	struct kfd_mem_properties *mem;
904 	u16 mem_width, mem_clock;
905 	struct kfd_topology_device *kdev =
906 		(struct kfd_topology_device *)private;
907 	const u8 *dmi_data = (const u8 *)(dm + 1);
908 
909 	if (dm->type == DMI_ENTRY_MEM_DEVICE && dm->length >= 0x15) {
910 		mem_width = (u16)(*(const u16 *)(dmi_data + 0x6));
911 		mem_clock = (u16)(*(const u16 *)(dmi_data + 0x11));
912 		list_for_each_entry(mem, &kdev->mem_props, list) {
913 			if (mem_width != 0xFFFF && mem_width != 0)
914 				mem->width = mem_width;
915 			if (mem_clock != 0)
916 				mem->mem_clk_max = mem_clock;
917 		}
918 	}
919 }
920 
921 /*
922  * Performance counters information is not part of CRAT but we would like to
923  * put them in the sysfs under topology directory for Thunk to get the data.
924  * This function is called before updating the sysfs.
925  */
926 static int kfd_add_perf_to_topology(struct kfd_topology_device *kdev)
927 {
928 	/* These are the only counters supported so far */
929 	return kfd_iommu_add_perf_counters(kdev);
930 }
931 
932 /* kfd_add_non_crat_information - Add information that is not currently
933  *	defined in CRAT but is necessary for KFD topology
934  * @dev - topology device to which addition info is added
935  */
936 static void kfd_add_non_crat_information(struct kfd_topology_device *kdev)
937 {
938 	/* Check if CPU only node. */
939 	if (!kdev->gpu) {
940 		/* Add system memory information */
941 		dmi_walk(find_system_memory, kdev);
942 	}
943 	/* TODO: For GPU node, rearrange code from kfd_topology_add_device */
944 }
945 
946 /* kfd_is_acpi_crat_invalid - CRAT from ACPI is valid only for AMD APU devices.
947  *	Ignore CRAT for all other devices. AMD APU is identified if both CPU
948  *	and GPU cores are present.
949  * @device_list - topology device list created by parsing ACPI CRAT table.
950  * @return - TRUE if invalid, FALSE is valid.
951  */
952 static bool kfd_is_acpi_crat_invalid(struct list_head *device_list)
953 {
954 	struct kfd_topology_device *dev;
955 
956 	list_for_each_entry(dev, device_list, list) {
957 		if (dev->node_props.cpu_cores_count &&
958 			dev->node_props.simd_count)
959 			return false;
960 	}
961 	pr_info("Ignoring ACPI CRAT on non-APU system\n");
962 	return true;
963 }
964 
965 int kfd_topology_init(void)
966 {
967 	void *crat_image = NULL;
968 	size_t image_size = 0;
969 	int ret;
970 	struct list_head temp_topology_device_list;
971 	int cpu_only_node = 0;
972 	struct kfd_topology_device *kdev;
973 	int proximity_domain;
974 
975 	/* topology_device_list - Master list of all topology devices
976 	 * temp_topology_device_list - temporary list created while parsing CRAT
977 	 * or VCRAT. Once parsing is complete the contents of list is moved to
978 	 * topology_device_list
979 	 */
980 
981 	/* Initialize the head for the both the lists */
982 	INIT_LIST_HEAD(&topology_device_list);
983 	INIT_LIST_HEAD(&temp_topology_device_list);
984 	init_rwsem(&topology_lock);
985 
986 	memset(&sys_props, 0, sizeof(sys_props));
987 
988 	/* Proximity domains in ACPI CRAT tables start counting at
989 	 * 0. The same should be true for virtual CRAT tables created
990 	 * at this stage. GPUs added later in kfd_topology_add_device
991 	 * use a counter.
992 	 */
993 	proximity_domain = 0;
994 
995 	/*
996 	 * Get the CRAT image from the ACPI. If ACPI doesn't have one
997 	 * or if ACPI CRAT is invalid create a virtual CRAT.
998 	 * NOTE: The current implementation expects all AMD APUs to have
999 	 *	CRAT. If no CRAT is available, it is assumed to be a CPU
1000 	 */
1001 	ret = kfd_create_crat_image_acpi(&crat_image, &image_size);
1002 	if (!ret) {
1003 		ret = kfd_parse_crat_table(crat_image,
1004 					   &temp_topology_device_list,
1005 					   proximity_domain);
1006 		if (ret ||
1007 		    kfd_is_acpi_crat_invalid(&temp_topology_device_list)) {
1008 			kfd_release_topology_device_list(
1009 				&temp_topology_device_list);
1010 			kfd_destroy_crat_image(crat_image);
1011 			crat_image = NULL;
1012 		}
1013 	}
1014 
1015 	if (!crat_image) {
1016 		ret = kfd_create_crat_image_virtual(&crat_image, &image_size,
1017 						    COMPUTE_UNIT_CPU, NULL,
1018 						    proximity_domain);
1019 		cpu_only_node = 1;
1020 		if (ret) {
1021 			pr_err("Error creating VCRAT table for CPU\n");
1022 			return ret;
1023 		}
1024 
1025 		ret = kfd_parse_crat_table(crat_image,
1026 					   &temp_topology_device_list,
1027 					   proximity_domain);
1028 		if (ret) {
1029 			pr_err("Error parsing VCRAT table for CPU\n");
1030 			goto err;
1031 		}
1032 	}
1033 
1034 	kdev = list_first_entry(&temp_topology_device_list,
1035 				struct kfd_topology_device, list);
1036 	kfd_add_perf_to_topology(kdev);
1037 
1038 	down_write(&topology_lock);
1039 	kfd_topology_update_device_list(&temp_topology_device_list,
1040 					&topology_device_list);
1041 	atomic_set(&topology_crat_proximity_domain, sys_props.num_devices-1);
1042 	ret = kfd_topology_update_sysfs();
1043 	up_write(&topology_lock);
1044 
1045 	if (!ret) {
1046 		sys_props.generation_count++;
1047 		kfd_update_system_properties();
1048 		kfd_debug_print_topology();
1049 		pr_info("Finished initializing topology\n");
1050 	} else
1051 		pr_err("Failed to update topology in sysfs ret=%d\n", ret);
1052 
1053 	/* For nodes with GPU, this information gets added
1054 	 * when GPU is detected (kfd_topology_add_device).
1055 	 */
1056 	if (cpu_only_node) {
1057 		/* Add additional information to CPU only node created above */
1058 		down_write(&topology_lock);
1059 		kdev = list_first_entry(&topology_device_list,
1060 				struct kfd_topology_device, list);
1061 		up_write(&topology_lock);
1062 		kfd_add_non_crat_information(kdev);
1063 	}
1064 
1065 err:
1066 	kfd_destroy_crat_image(crat_image);
1067 	return ret;
1068 }
1069 
1070 void kfd_topology_shutdown(void)
1071 {
1072 	down_write(&topology_lock);
1073 	kfd_topology_release_sysfs();
1074 	kfd_release_live_view();
1075 	up_write(&topology_lock);
1076 }
1077 
1078 static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
1079 {
1080 	uint32_t hashout;
1081 	uint32_t buf[7];
1082 	uint64_t local_mem_size;
1083 	int i;
1084 	struct kfd_local_mem_info local_mem_info;
1085 
1086 	if (!gpu)
1087 		return 0;
1088 
1089 	amdgpu_amdkfd_get_local_mem_info(gpu->kgd, &local_mem_info);
1090 
1091 	local_mem_size = local_mem_info.local_mem_size_private +
1092 			local_mem_info.local_mem_size_public;
1093 
1094 	buf[0] = gpu->pdev->devfn;
1095 	buf[1] = gpu->pdev->subsystem_vendor |
1096 		(gpu->pdev->subsystem_device << 16);
1097 	buf[2] = pci_domain_nr(gpu->pdev->bus);
1098 	buf[3] = gpu->pdev->device;
1099 	buf[4] = gpu->pdev->bus->number;
1100 	buf[5] = lower_32_bits(local_mem_size);
1101 	buf[6] = upper_32_bits(local_mem_size);
1102 
1103 	for (i = 0, hashout = 0; i < 7; i++)
1104 		hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
1105 
1106 	return hashout;
1107 }
1108 /* kfd_assign_gpu - Attach @gpu to the correct kfd topology device. If
1109  *		the GPU device is not already present in the topology device
1110  *		list then return NULL. This means a new topology device has to
1111  *		be created for this GPU.
1112  */
1113 static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
1114 {
1115 	struct kfd_topology_device *dev;
1116 	struct kfd_topology_device *out_dev = NULL;
1117 	struct kfd_mem_properties *mem;
1118 	struct kfd_cache_properties *cache;
1119 	struct kfd_iolink_properties *iolink;
1120 
1121 	down_write(&topology_lock);
1122 	list_for_each_entry(dev, &topology_device_list, list) {
1123 		/* Discrete GPUs need their own topology device list
1124 		 * entries. Don't assign them to CPU/APU nodes.
1125 		 */
1126 		if (!gpu->device_info->needs_iommu_device &&
1127 		    dev->node_props.cpu_cores_count)
1128 			continue;
1129 
1130 		if (!dev->gpu && (dev->node_props.simd_count > 0)) {
1131 			dev->gpu = gpu;
1132 			out_dev = dev;
1133 
1134 			list_for_each_entry(mem, &dev->mem_props, list)
1135 				mem->gpu = dev->gpu;
1136 			list_for_each_entry(cache, &dev->cache_props, list)
1137 				cache->gpu = dev->gpu;
1138 			list_for_each_entry(iolink, &dev->io_link_props, list)
1139 				iolink->gpu = dev->gpu;
1140 			break;
1141 		}
1142 	}
1143 	up_write(&topology_lock);
1144 	return out_dev;
1145 }
1146 
1147 static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
1148 {
1149 	/*
1150 	 * TODO: Generate an event for thunk about the arrival/removal
1151 	 * of the GPU
1152 	 */
1153 }
1154 
1155 /* kfd_fill_mem_clk_max_info - Since CRAT doesn't have memory clock info,
1156  *		patch this after CRAT parsing.
1157  */
1158 static void kfd_fill_mem_clk_max_info(struct kfd_topology_device *dev)
1159 {
1160 	struct kfd_mem_properties *mem;
1161 	struct kfd_local_mem_info local_mem_info;
1162 
1163 	if (!dev)
1164 		return;
1165 
1166 	/* Currently, amdgpu driver (amdgpu_mc) deals only with GPUs with
1167 	 * single bank of VRAM local memory.
1168 	 * for dGPUs - VCRAT reports only one bank of Local Memory
1169 	 * for APUs - If CRAT from ACPI reports more than one bank, then
1170 	 *	all the banks will report the same mem_clk_max information
1171 	 */
1172 	amdgpu_amdkfd_get_local_mem_info(dev->gpu->kgd, &local_mem_info);
1173 
1174 	list_for_each_entry(mem, &dev->mem_props, list)
1175 		mem->mem_clk_max = local_mem_info.mem_clk_max;
1176 }
1177 
1178 static void kfd_fill_iolink_non_crat_info(struct kfd_topology_device *dev)
1179 {
1180 	struct kfd_iolink_properties *link, *cpu_link;
1181 	struct kfd_topology_device *cpu_dev;
1182 	uint32_t cap;
1183 	uint32_t cpu_flag = CRAT_IOLINK_FLAGS_ENABLED;
1184 	uint32_t flag = CRAT_IOLINK_FLAGS_ENABLED;
1185 
1186 	if (!dev || !dev->gpu)
1187 		return;
1188 
1189 	pcie_capability_read_dword(dev->gpu->pdev,
1190 			PCI_EXP_DEVCAP2, &cap);
1191 
1192 	if (!(cap & (PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
1193 		     PCI_EXP_DEVCAP2_ATOMIC_COMP64)))
1194 		cpu_flag |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1195 			CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1196 
1197 	if (!dev->gpu->pci_atomic_requested ||
1198 	    dev->gpu->device_info->asic_family == CHIP_HAWAII)
1199 		flag |= CRAT_IOLINK_FLAGS_NO_ATOMICS_32_BIT |
1200 			CRAT_IOLINK_FLAGS_NO_ATOMICS_64_BIT;
1201 
1202 	/* GPU only creates direct links so apply flags setting to all */
1203 	list_for_each_entry(link, &dev->io_link_props, list) {
1204 		link->flags = flag;
1205 		cpu_dev = kfd_topology_device_by_proximity_domain(
1206 				link->node_to);
1207 		if (cpu_dev) {
1208 			list_for_each_entry(cpu_link,
1209 					    &cpu_dev->io_link_props, list)
1210 				if (cpu_link->node_to == link->node_from)
1211 					cpu_link->flags = cpu_flag;
1212 		}
1213 	}
1214 }
1215 
1216 int kfd_topology_add_device(struct kfd_dev *gpu)
1217 {
1218 	uint32_t gpu_id;
1219 	struct kfd_topology_device *dev;
1220 	struct kfd_cu_info cu_info;
1221 	int res = 0;
1222 	struct list_head temp_topology_device_list;
1223 	void *crat_image = NULL;
1224 	size_t image_size = 0;
1225 	int proximity_domain;
1226 	struct amdgpu_ras *ctx;
1227 
1228 	INIT_LIST_HEAD(&temp_topology_device_list);
1229 
1230 	gpu_id = kfd_generate_gpu_id(gpu);
1231 
1232 	pr_debug("Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
1233 
1234 	proximity_domain = atomic_inc_return(&topology_crat_proximity_domain);
1235 
1236 	/* Check to see if this gpu device exists in the topology_device_list.
1237 	 * If so, assign the gpu to that device,
1238 	 * else create a Virtual CRAT for this gpu device and then parse that
1239 	 * CRAT to create a new topology device. Once created assign the gpu to
1240 	 * that topology device
1241 	 */
1242 	dev = kfd_assign_gpu(gpu);
1243 	if (!dev) {
1244 		res = kfd_create_crat_image_virtual(&crat_image, &image_size,
1245 						    COMPUTE_UNIT_GPU, gpu,
1246 						    proximity_domain);
1247 		if (res) {
1248 			pr_err("Error creating VCRAT for GPU (ID: 0x%x)\n",
1249 			       gpu_id);
1250 			return res;
1251 		}
1252 		res = kfd_parse_crat_table(crat_image,
1253 					   &temp_topology_device_list,
1254 					   proximity_domain);
1255 		if (res) {
1256 			pr_err("Error parsing VCRAT for GPU (ID: 0x%x)\n",
1257 			       gpu_id);
1258 			goto err;
1259 		}
1260 
1261 		down_write(&topology_lock);
1262 		kfd_topology_update_device_list(&temp_topology_device_list,
1263 			&topology_device_list);
1264 
1265 		/* Update the SYSFS tree, since we added another topology
1266 		 * device
1267 		 */
1268 		res = kfd_topology_update_sysfs();
1269 		up_write(&topology_lock);
1270 
1271 		if (!res)
1272 			sys_props.generation_count++;
1273 		else
1274 			pr_err("Failed to update GPU (ID: 0x%x) to sysfs topology. res=%d\n",
1275 						gpu_id, res);
1276 		dev = kfd_assign_gpu(gpu);
1277 		if (WARN_ON(!dev)) {
1278 			res = -ENODEV;
1279 			goto err;
1280 		}
1281 	}
1282 
1283 	dev->gpu_id = gpu_id;
1284 	gpu->id = gpu_id;
1285 
1286 	/* TODO: Move the following lines to function
1287 	 *	kfd_add_non_crat_information
1288 	 */
1289 
1290 	/* Fill-in additional information that is not available in CRAT but
1291 	 * needed for the topology
1292 	 */
1293 
1294 	amdgpu_amdkfd_get_cu_info(dev->gpu->kgd, &cu_info);
1295 
1296 	strncpy(dev->node_props.name, gpu->device_info->asic_name,
1297 			KFD_TOPOLOGY_PUBLIC_NAME_SIZE);
1298 
1299 	dev->node_props.simd_arrays_per_engine =
1300 		cu_info.num_shader_arrays_per_engine;
1301 
1302 	dev->node_props.vendor_id = gpu->pdev->vendor;
1303 	dev->node_props.device_id = gpu->pdev->device;
1304 	dev->node_props.location_id = pci_dev_id(gpu->pdev);
1305 	dev->node_props.max_engine_clk_fcompute =
1306 		amdgpu_amdkfd_get_max_engine_clock_in_mhz(dev->gpu->kgd);
1307 	dev->node_props.max_engine_clk_ccompute =
1308 		cpufreq_quick_get_max(0) / 1000;
1309 	dev->node_props.drm_render_minor =
1310 		gpu->shared_resources.drm_render_minor;
1311 
1312 	dev->node_props.hive_id = gpu->hive_id;
1313 	dev->node_props.num_sdma_engines = gpu->device_info->num_sdma_engines;
1314 	dev->node_props.num_sdma_xgmi_engines =
1315 				gpu->device_info->num_xgmi_sdma_engines;
1316 	dev->node_props.num_sdma_queues_per_engine =
1317 				gpu->device_info->num_sdma_queues_per_engine;
1318 	dev->node_props.num_gws = (hws_gws_support &&
1319 		dev->gpu->dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) ?
1320 		amdgpu_amdkfd_get_num_gws(dev->gpu->kgd) : 0;
1321 	dev->node_props.num_cp_queues = get_queues_num(dev->gpu->dqm);
1322 
1323 	kfd_fill_mem_clk_max_info(dev);
1324 	kfd_fill_iolink_non_crat_info(dev);
1325 
1326 	switch (dev->gpu->device_info->asic_family) {
1327 	case CHIP_KAVERI:
1328 	case CHIP_HAWAII:
1329 	case CHIP_TONGA:
1330 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_PRE_1_0 <<
1331 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1332 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1333 		break;
1334 	case CHIP_CARRIZO:
1335 	case CHIP_FIJI:
1336 	case CHIP_POLARIS10:
1337 	case CHIP_POLARIS11:
1338 	case CHIP_POLARIS12:
1339 	case CHIP_VEGAM:
1340 		pr_debug("Adding doorbell packet type capability\n");
1341 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_1_0 <<
1342 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1343 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1344 		break;
1345 	case CHIP_VEGA10:
1346 	case CHIP_VEGA12:
1347 	case CHIP_VEGA20:
1348 	case CHIP_RAVEN:
1349 	case CHIP_RENOIR:
1350 	case CHIP_ARCTURUS:
1351 	case CHIP_NAVI10:
1352 	case CHIP_NAVI12:
1353 	case CHIP_NAVI14:
1354 		dev->node_props.capability |= ((HSA_CAP_DOORBELL_TYPE_2_0 <<
1355 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_SHIFT) &
1356 			HSA_CAP_DOORBELL_TYPE_TOTALBITS_MASK);
1357 		break;
1358 	default:
1359 		WARN(1, "Unexpected ASIC family %u",
1360 		     dev->gpu->device_info->asic_family);
1361 	}
1362 
1363 	/*
1364 	* Overwrite ATS capability according to needs_iommu_device to fix
1365 	* potential missing corresponding bit in CRAT of BIOS.
1366 	*/
1367 	if (dev->gpu->device_info->needs_iommu_device)
1368 		dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
1369 	else
1370 		dev->node_props.capability &= ~HSA_CAP_ATS_PRESENT;
1371 
1372 	/* Fix errors in CZ CRAT.
1373 	 * simd_count: Carrizo CRAT reports wrong simd_count, probably
1374 	 *		because it doesn't consider masked out CUs
1375 	 * max_waves_per_simd: Carrizo reports wrong max_waves_per_simd
1376 	 */
1377 	if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
1378 		dev->node_props.simd_count =
1379 			cu_info.simd_per_cu * cu_info.cu_active_number;
1380 		dev->node_props.max_waves_per_simd = 10;
1381 	}
1382 
1383 	ctx = amdgpu_ras_get_context((struct amdgpu_device *)(dev->gpu->kgd));
1384 	if (ctx) {
1385 		/* kfd only concerns sram ecc on GFX/SDMA and HBM ecc on UMC */
1386 		dev->node_props.capability |=
1387 			(((ctx->features & BIT(AMDGPU_RAS_BLOCK__SDMA)) != 0) ||
1388 			 ((ctx->features & BIT(AMDGPU_RAS_BLOCK__GFX)) != 0)) ?
1389 			HSA_CAP_SRAM_EDCSUPPORTED : 0;
1390 		dev->node_props.capability |= ((ctx->features & BIT(AMDGPU_RAS_BLOCK__UMC)) != 0) ?
1391 			HSA_CAP_MEM_EDCSUPPORTED : 0;
1392 
1393 		dev->node_props.capability |= (ctx->features != 0) ?
1394 			HSA_CAP_RASEVENTNOTIFY : 0;
1395 	}
1396 
1397 	kfd_debug_print_topology();
1398 
1399 	if (!res)
1400 		kfd_notify_gpu_change(gpu_id, 1);
1401 err:
1402 	kfd_destroy_crat_image(crat_image);
1403 	return res;
1404 }
1405 
1406 int kfd_topology_remove_device(struct kfd_dev *gpu)
1407 {
1408 	struct kfd_topology_device *dev, *tmp;
1409 	uint32_t gpu_id;
1410 	int res = -ENODEV;
1411 
1412 	down_write(&topology_lock);
1413 
1414 	list_for_each_entry_safe(dev, tmp, &topology_device_list, list)
1415 		if (dev->gpu == gpu) {
1416 			gpu_id = dev->gpu_id;
1417 			kfd_remove_sysfs_node_entry(dev);
1418 			kfd_release_topology_device(dev);
1419 			sys_props.num_devices--;
1420 			res = 0;
1421 			if (kfd_topology_update_sysfs() < 0)
1422 				kfd_topology_release_sysfs();
1423 			break;
1424 		}
1425 
1426 	up_write(&topology_lock);
1427 
1428 	if (!res)
1429 		kfd_notify_gpu_change(gpu_id, 0);
1430 
1431 	return res;
1432 }
1433 
1434 /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
1435  *	topology. If GPU device is found @idx, then valid kfd_dev pointer is
1436  *	returned through @kdev
1437  * Return -	0: On success (@kdev will be NULL for non GPU nodes)
1438  *		-1: If end of list
1439  */
1440 int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev)
1441 {
1442 
1443 	struct kfd_topology_device *top_dev;
1444 	uint8_t device_idx = 0;
1445 
1446 	*kdev = NULL;
1447 	down_read(&topology_lock);
1448 
1449 	list_for_each_entry(top_dev, &topology_device_list, list) {
1450 		if (device_idx == idx) {
1451 			*kdev = top_dev->gpu;
1452 			up_read(&topology_lock);
1453 			return 0;
1454 		}
1455 
1456 		device_idx++;
1457 	}
1458 
1459 	up_read(&topology_lock);
1460 
1461 	return -1;
1462 
1463 }
1464 
1465 static int kfd_cpumask_to_apic_id(const struct cpumask *cpumask)
1466 {
1467 	int first_cpu_of_numa_node;
1468 
1469 	if (!cpumask || cpumask == cpu_none_mask)
1470 		return -1;
1471 	first_cpu_of_numa_node = cpumask_first(cpumask);
1472 	if (first_cpu_of_numa_node >= nr_cpu_ids)
1473 		return -1;
1474 #ifdef CONFIG_X86_64
1475 	return cpu_data(first_cpu_of_numa_node).apicid;
1476 #else
1477 	return first_cpu_of_numa_node;
1478 #endif
1479 }
1480 
1481 /* kfd_numa_node_to_apic_id - Returns the APIC ID of the first logical processor
1482  *	of the given NUMA node (numa_node_id)
1483  * Return -1 on failure
1484  */
1485 int kfd_numa_node_to_apic_id(int numa_node_id)
1486 {
1487 	if (numa_node_id == -1) {
1488 		pr_warn("Invalid NUMA Node. Use online CPU mask\n");
1489 		return kfd_cpumask_to_apic_id(cpu_online_mask);
1490 	}
1491 	return kfd_cpumask_to_apic_id(cpumask_of_node(numa_node_id));
1492 }
1493 
1494 #if defined(CONFIG_DEBUG_FS)
1495 
1496 int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data)
1497 {
1498 	struct kfd_topology_device *dev;
1499 	unsigned int i = 0;
1500 	int r = 0;
1501 
1502 	down_read(&topology_lock);
1503 
1504 	list_for_each_entry(dev, &topology_device_list, list) {
1505 		if (!dev->gpu) {
1506 			i++;
1507 			continue;
1508 		}
1509 
1510 		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1511 		r = dqm_debugfs_hqds(m, dev->gpu->dqm);
1512 		if (r)
1513 			break;
1514 	}
1515 
1516 	up_read(&topology_lock);
1517 
1518 	return r;
1519 }
1520 
1521 int kfd_debugfs_rls_by_device(struct seq_file *m, void *data)
1522 {
1523 	struct kfd_topology_device *dev;
1524 	unsigned int i = 0;
1525 	int r = 0;
1526 
1527 	down_read(&topology_lock);
1528 
1529 	list_for_each_entry(dev, &topology_device_list, list) {
1530 		if (!dev->gpu) {
1531 			i++;
1532 			continue;
1533 		}
1534 
1535 		seq_printf(m, "Node %u, gpu_id %x:\n", i++, dev->gpu->id);
1536 		r = pm_debugfs_runlist(m, &dev->gpu->dqm->packets);
1537 		if (r)
1538 			break;
1539 	}
1540 
1541 	up_read(&topology_lock);
1542 
1543 	return r;
1544 }
1545 
1546 #endif
1547