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