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