Home | History | Annotate | Line # | Download | only in amdkfd
kfd_topology.c revision 1.1
      1 /*	$NetBSD: kfd_topology.c,v 1.1 2018/08/27 01:34:46 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2014 Advanced Micro Devices, Inc.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 #include <sys/cdefs.h>
     26 __KERNEL_RCSID(0, "$NetBSD: kfd_topology.c,v 1.1 2018/08/27 01:34:46 riastradh Exp $");
     27 
     28 #include <linux/types.h>
     29 #include <linux/kernel.h>
     30 #include <linux/pci.h>
     31 #include <linux/errno.h>
     32 #include <linux/acpi.h>
     33 #include <linux/hash.h>
     34 #include <linux/cpufreq.h>
     35 #include <linux/log2.h>
     36 
     37 #include "kfd_priv.h"
     38 #include "kfd_crat.h"
     39 #include "kfd_topology.h"
     40 
     41 static struct list_head topology_device_list;
     42 static int topology_crat_parsed;
     43 static struct kfd_system_properties sys_props;
     44 
     45 static DECLARE_RWSEM(topology_lock);
     46 
     47 struct kfd_dev *kfd_device_by_id(uint32_t gpu_id)
     48 {
     49 	struct kfd_topology_device *top_dev;
     50 	struct kfd_dev *device = NULL;
     51 
     52 	down_read(&topology_lock);
     53 
     54 	list_for_each_entry(top_dev, &topology_device_list, list)
     55 		if (top_dev->gpu_id == gpu_id) {
     56 			device = top_dev->gpu;
     57 			break;
     58 		}
     59 
     60 	up_read(&topology_lock);
     61 
     62 	return device;
     63 }
     64 
     65 struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev)
     66 {
     67 	struct kfd_topology_device *top_dev;
     68 	struct kfd_dev *device = NULL;
     69 
     70 	down_read(&topology_lock);
     71 
     72 	list_for_each_entry(top_dev, &topology_device_list, list)
     73 		if (top_dev->gpu->pdev == pdev) {
     74 			device = top_dev->gpu;
     75 			break;
     76 		}
     77 
     78 	up_read(&topology_lock);
     79 
     80 	return device;
     81 }
     82 
     83 static int kfd_topology_get_crat_acpi(void *crat_image, size_t *size)
     84 {
     85 	struct acpi_table_header *crat_table;
     86 	acpi_status status;
     87 
     88 	if (!size)
     89 		return -EINVAL;
     90 
     91 	/*
     92 	 * Fetch the CRAT table from ACPI
     93 	 */
     94 	status = acpi_get_table(CRAT_SIGNATURE, 0, &crat_table);
     95 	if (status == AE_NOT_FOUND) {
     96 		pr_warn("CRAT table not found\n");
     97 		return -ENODATA;
     98 	} else if (ACPI_FAILURE(status)) {
     99 		const char *err = acpi_format_exception(status);
    100 
    101 		pr_err("CRAT table error: %s\n", err);
    102 		return -EINVAL;
    103 	}
    104 
    105 	if (*size >= crat_table->length && crat_image != NULL)
    106 		memcpy(crat_image, crat_table, crat_table->length);
    107 
    108 	*size = crat_table->length;
    109 
    110 	return 0;
    111 }
    112 
    113 static void kfd_populated_cu_info_cpu(struct kfd_topology_device *dev,
    114 		struct crat_subtype_computeunit *cu)
    115 {
    116 	BUG_ON(!dev);
    117 	BUG_ON(!cu);
    118 
    119 	dev->node_props.cpu_cores_count = cu->num_cpu_cores;
    120 	dev->node_props.cpu_core_id_base = cu->processor_id_low;
    121 	if (cu->hsa_capability & CRAT_CU_FLAGS_IOMMU_PRESENT)
    122 		dev->node_props.capability |= HSA_CAP_ATS_PRESENT;
    123 
    124 	pr_info("CU CPU: cores=%d id_base=%d\n", cu->num_cpu_cores,
    125 			cu->processor_id_low);
    126 }
    127 
    128 static void kfd_populated_cu_info_gpu(struct kfd_topology_device *dev,
    129 		struct crat_subtype_computeunit *cu)
    130 {
    131 	BUG_ON(!dev);
    132 	BUG_ON(!cu);
    133 
    134 	dev->node_props.simd_id_base = cu->processor_id_low;
    135 	dev->node_props.simd_count = cu->num_simd_cores;
    136 	dev->node_props.lds_size_in_kb = cu->lds_size_in_kb;
    137 	dev->node_props.max_waves_per_simd = cu->max_waves_simd;
    138 	dev->node_props.wave_front_size = cu->wave_front_size;
    139 	dev->node_props.mem_banks_count = cu->num_banks;
    140 	dev->node_props.array_count = cu->num_arrays;
    141 	dev->node_props.cu_per_simd_array = cu->num_cu_per_array;
    142 	dev->node_props.simd_per_cu = cu->num_simd_per_cu;
    143 	dev->node_props.max_slots_scratch_cu = cu->max_slots_scatch_cu;
    144 	if (cu->hsa_capability & CRAT_CU_FLAGS_HOT_PLUGGABLE)
    145 		dev->node_props.capability |= HSA_CAP_HOT_PLUGGABLE;
    146 	pr_info("CU GPU: simds=%d id_base=%d\n", cu->num_simd_cores,
    147 				cu->processor_id_low);
    148 }
    149 
    150 /* kfd_parse_subtype_cu is called when the topology mutex is already acquired */
    151 static int kfd_parse_subtype_cu(struct crat_subtype_computeunit *cu)
    152 {
    153 	struct kfd_topology_device *dev;
    154 	int i = 0;
    155 
    156 	BUG_ON(!cu);
    157 
    158 	pr_info("Found CU entry in CRAT table with proximity_domain=%d caps=%x\n",
    159 			cu->proximity_domain, cu->hsa_capability);
    160 	list_for_each_entry(dev, &topology_device_list, list) {
    161 		if (cu->proximity_domain == i) {
    162 			if (cu->flags & CRAT_CU_FLAGS_CPU_PRESENT)
    163 				kfd_populated_cu_info_cpu(dev, cu);
    164 
    165 			if (cu->flags & CRAT_CU_FLAGS_GPU_PRESENT)
    166 				kfd_populated_cu_info_gpu(dev, cu);
    167 			break;
    168 		}
    169 		i++;
    170 	}
    171 
    172 	return 0;
    173 }
    174 
    175 /*
    176  * kfd_parse_subtype_mem is called when the topology mutex is
    177  * already acquired
    178  */
    179 static int kfd_parse_subtype_mem(struct crat_subtype_memory *mem)
    180 {
    181 	struct kfd_mem_properties *props;
    182 	struct kfd_topology_device *dev;
    183 	int i = 0;
    184 
    185 	BUG_ON(!mem);
    186 
    187 	pr_info("Found memory entry in CRAT table with proximity_domain=%d\n",
    188 			mem->promixity_domain);
    189 	list_for_each_entry(dev, &topology_device_list, list) {
    190 		if (mem->promixity_domain == i) {
    191 			props = kfd_alloc_struct(props);
    192 			if (props == NULL)
    193 				return -ENOMEM;
    194 
    195 			if (dev->node_props.cpu_cores_count == 0)
    196 				props->heap_type = HSA_MEM_HEAP_TYPE_FB_PRIVATE;
    197 			else
    198 				props->heap_type = HSA_MEM_HEAP_TYPE_SYSTEM;
    199 
    200 			if (mem->flags & CRAT_MEM_FLAGS_HOT_PLUGGABLE)
    201 				props->flags |= HSA_MEM_FLAGS_HOT_PLUGGABLE;
    202 			if (mem->flags & CRAT_MEM_FLAGS_NON_VOLATILE)
    203 				props->flags |= HSA_MEM_FLAGS_NON_VOLATILE;
    204 
    205 			props->size_in_bytes =
    206 				((uint64_t)mem->length_high << 32) +
    207 							mem->length_low;
    208 			props->width = mem->width;
    209 
    210 			dev->mem_bank_count++;
    211 			list_add_tail(&props->list, &dev->mem_props);
    212 
    213 			break;
    214 		}
    215 		i++;
    216 	}
    217 
    218 	return 0;
    219 }
    220 
    221 /*
    222  * kfd_parse_subtype_cache is called when the topology mutex
    223  * is already acquired
    224  */
    225 static int kfd_parse_subtype_cache(struct crat_subtype_cache *cache)
    226 {
    227 	struct kfd_cache_properties *props;
    228 	struct kfd_topology_device *dev;
    229 	uint32_t id;
    230 
    231 	BUG_ON(!cache);
    232 
    233 	id = cache->processor_id_low;
    234 
    235 	pr_info("Found cache entry in CRAT table with processor_id=%d\n", id);
    236 	list_for_each_entry(dev, &topology_device_list, list)
    237 		if (id == dev->node_props.cpu_core_id_base ||
    238 		    id == dev->node_props.simd_id_base) {
    239 			props = kfd_alloc_struct(props);
    240 			if (props == NULL)
    241 				return -ENOMEM;
    242 
    243 			props->processor_id_low = id;
    244 			props->cache_level = cache->cache_level;
    245 			props->cache_size = cache->cache_size;
    246 			props->cacheline_size = cache->cache_line_size;
    247 			props->cachelines_per_tag = cache->lines_per_tag;
    248 			props->cache_assoc = cache->associativity;
    249 			props->cache_latency = cache->cache_latency;
    250 
    251 			if (cache->flags & CRAT_CACHE_FLAGS_DATA_CACHE)
    252 				props->cache_type |= HSA_CACHE_TYPE_DATA;
    253 			if (cache->flags & CRAT_CACHE_FLAGS_INST_CACHE)
    254 				props->cache_type |= HSA_CACHE_TYPE_INSTRUCTION;
    255 			if (cache->flags & CRAT_CACHE_FLAGS_CPU_CACHE)
    256 				props->cache_type |= HSA_CACHE_TYPE_CPU;
    257 			if (cache->flags & CRAT_CACHE_FLAGS_SIMD_CACHE)
    258 				props->cache_type |= HSA_CACHE_TYPE_HSACU;
    259 
    260 			dev->cache_count++;
    261 			dev->node_props.caches_count++;
    262 			list_add_tail(&props->list, &dev->cache_props);
    263 
    264 			break;
    265 		}
    266 
    267 	return 0;
    268 }
    269 
    270 /*
    271  * kfd_parse_subtype_iolink is called when the topology mutex
    272  * is already acquired
    273  */
    274 static int kfd_parse_subtype_iolink(struct crat_subtype_iolink *iolink)
    275 {
    276 	struct kfd_iolink_properties *props;
    277 	struct kfd_topology_device *dev;
    278 	uint32_t i = 0;
    279 	uint32_t id_from;
    280 	uint32_t id_to;
    281 
    282 	BUG_ON(!iolink);
    283 
    284 	id_from = iolink->proximity_domain_from;
    285 	id_to = iolink->proximity_domain_to;
    286 
    287 	pr_info("Found IO link entry in CRAT table with id_from=%d\n", id_from);
    288 	list_for_each_entry(dev, &topology_device_list, list) {
    289 		if (id_from == i) {
    290 			props = kfd_alloc_struct(props);
    291 			if (props == NULL)
    292 				return -ENOMEM;
    293 
    294 			props->node_from = id_from;
    295 			props->node_to = id_to;
    296 			props->ver_maj = iolink->version_major;
    297 			props->ver_min = iolink->version_minor;
    298 
    299 			/*
    300 			 * weight factor (derived from CDIR), currently always 1
    301 			 */
    302 			props->weight = 1;
    303 
    304 			props->min_latency = iolink->minimum_latency;
    305 			props->max_latency = iolink->maximum_latency;
    306 			props->min_bandwidth = iolink->minimum_bandwidth_mbs;
    307 			props->max_bandwidth = iolink->maximum_bandwidth_mbs;
    308 			props->rec_transfer_size =
    309 					iolink->recommended_transfer_size;
    310 
    311 			dev->io_link_count++;
    312 			dev->node_props.io_links_count++;
    313 			list_add_tail(&props->list, &dev->io_link_props);
    314 
    315 			break;
    316 		}
    317 		i++;
    318 	}
    319 
    320 	return 0;
    321 }
    322 
    323 static int kfd_parse_subtype(struct crat_subtype_generic *sub_type_hdr)
    324 {
    325 	struct crat_subtype_computeunit *cu;
    326 	struct crat_subtype_memory *mem;
    327 	struct crat_subtype_cache *cache;
    328 	struct crat_subtype_iolink *iolink;
    329 	int ret = 0;
    330 
    331 	BUG_ON(!sub_type_hdr);
    332 
    333 	switch (sub_type_hdr->type) {
    334 	case CRAT_SUBTYPE_COMPUTEUNIT_AFFINITY:
    335 		cu = (struct crat_subtype_computeunit *)sub_type_hdr;
    336 		ret = kfd_parse_subtype_cu(cu);
    337 		break;
    338 	case CRAT_SUBTYPE_MEMORY_AFFINITY:
    339 		mem = (struct crat_subtype_memory *)sub_type_hdr;
    340 		ret = kfd_parse_subtype_mem(mem);
    341 		break;
    342 	case CRAT_SUBTYPE_CACHE_AFFINITY:
    343 		cache = (struct crat_subtype_cache *)sub_type_hdr;
    344 		ret = kfd_parse_subtype_cache(cache);
    345 		break;
    346 	case CRAT_SUBTYPE_TLB_AFFINITY:
    347 		/*
    348 		 * For now, nothing to do here
    349 		 */
    350 		pr_info("Found TLB entry in CRAT table (not processing)\n");
    351 		break;
    352 	case CRAT_SUBTYPE_CCOMPUTE_AFFINITY:
    353 		/*
    354 		 * For now, nothing to do here
    355 		 */
    356 		pr_info("Found CCOMPUTE entry in CRAT table (not processing)\n");
    357 		break;
    358 	case CRAT_SUBTYPE_IOLINK_AFFINITY:
    359 		iolink = (struct crat_subtype_iolink *)sub_type_hdr;
    360 		ret = kfd_parse_subtype_iolink(iolink);
    361 		break;
    362 	default:
    363 		pr_warn("Unknown subtype (%d) in CRAT\n",
    364 				sub_type_hdr->type);
    365 	}
    366 
    367 	return ret;
    368 }
    369 
    370 static void kfd_release_topology_device(struct kfd_topology_device *dev)
    371 {
    372 	struct kfd_mem_properties *mem;
    373 	struct kfd_cache_properties *cache;
    374 	struct kfd_iolink_properties *iolink;
    375 
    376 	BUG_ON(!dev);
    377 
    378 	list_del(&dev->list);
    379 
    380 	while (dev->mem_props.next != &dev->mem_props) {
    381 		mem = container_of(dev->mem_props.next,
    382 				struct kfd_mem_properties, list);
    383 		list_del(&mem->list);
    384 		kfree(mem);
    385 	}
    386 
    387 	while (dev->cache_props.next != &dev->cache_props) {
    388 		cache = container_of(dev->cache_props.next,
    389 				struct kfd_cache_properties, list);
    390 		list_del(&cache->list);
    391 		kfree(cache);
    392 	}
    393 
    394 	while (dev->io_link_props.next != &dev->io_link_props) {
    395 		iolink = container_of(dev->io_link_props.next,
    396 				struct kfd_iolink_properties, list);
    397 		list_del(&iolink->list);
    398 		kfree(iolink);
    399 	}
    400 
    401 	kfree(dev);
    402 
    403 	sys_props.num_devices--;
    404 }
    405 
    406 static void kfd_release_live_view(void)
    407 {
    408 	struct kfd_topology_device *dev;
    409 
    410 	while (topology_device_list.next != &topology_device_list) {
    411 		dev = container_of(topology_device_list.next,
    412 				 struct kfd_topology_device, list);
    413 		kfd_release_topology_device(dev);
    414 }
    415 
    416 	memset(&sys_props, 0, sizeof(sys_props));
    417 }
    418 
    419 static struct kfd_topology_device *kfd_create_topology_device(void)
    420 {
    421 	struct kfd_topology_device *dev;
    422 
    423 	dev = kfd_alloc_struct(dev);
    424 	if (dev == NULL) {
    425 		pr_err("No memory to allocate a topology device");
    426 		return NULL;
    427 	}
    428 
    429 	INIT_LIST_HEAD(&dev->mem_props);
    430 	INIT_LIST_HEAD(&dev->cache_props);
    431 	INIT_LIST_HEAD(&dev->io_link_props);
    432 
    433 	list_add_tail(&dev->list, &topology_device_list);
    434 	sys_props.num_devices++;
    435 
    436 	return dev;
    437 }
    438 
    439 static int kfd_parse_crat_table(void *crat_image)
    440 {
    441 	struct kfd_topology_device *top_dev;
    442 	struct crat_subtype_generic *sub_type_hdr;
    443 	uint16_t node_id;
    444 	int ret;
    445 	struct crat_header *crat_table = (struct crat_header *)crat_image;
    446 	uint16_t num_nodes;
    447 	uint32_t image_len;
    448 
    449 	if (!crat_image)
    450 		return -EINVAL;
    451 
    452 	num_nodes = crat_table->num_domains;
    453 	image_len = crat_table->length;
    454 
    455 	pr_info("Parsing CRAT table with %d nodes\n", num_nodes);
    456 
    457 	for (node_id = 0; node_id < num_nodes; node_id++) {
    458 		top_dev = kfd_create_topology_device();
    459 		if (!top_dev) {
    460 			kfd_release_live_view();
    461 			return -ENOMEM;
    462 		}
    463 	}
    464 
    465 	sys_props.platform_id =
    466 		(*((uint64_t *)crat_table->oem_id)) & CRAT_OEMID_64BIT_MASK;
    467 	sys_props.platform_oem = *((uint64_t *)crat_table->oem_table_id);
    468 	sys_props.platform_rev = crat_table->revision;
    469 
    470 	sub_type_hdr = (struct crat_subtype_generic *)(crat_table+1);
    471 	while ((char *)sub_type_hdr + sizeof(struct crat_subtype_generic) <
    472 			((char *)crat_image) + image_len) {
    473 		if (sub_type_hdr->flags & CRAT_SUBTYPE_FLAGS_ENABLED) {
    474 			ret = kfd_parse_subtype(sub_type_hdr);
    475 			if (ret != 0) {
    476 				kfd_release_live_view();
    477 				return ret;
    478 			}
    479 		}
    480 
    481 		sub_type_hdr = (typeof(sub_type_hdr))((char *)sub_type_hdr +
    482 				sub_type_hdr->length);
    483 	}
    484 
    485 	sys_props.generation_count++;
    486 	topology_crat_parsed = 1;
    487 
    488 	return 0;
    489 }
    490 
    491 
    492 #define sysfs_show_gen_prop(buffer, fmt, ...) \
    493 		snprintf(buffer, PAGE_SIZE, "%s"fmt, buffer, __VA_ARGS__)
    494 #define sysfs_show_32bit_prop(buffer, name, value) \
    495 		sysfs_show_gen_prop(buffer, "%s %u\n", name, value)
    496 #define sysfs_show_64bit_prop(buffer, name, value) \
    497 		sysfs_show_gen_prop(buffer, "%s %llu\n", name, value)
    498 #define sysfs_show_32bit_val(buffer, value) \
    499 		sysfs_show_gen_prop(buffer, "%u\n", value)
    500 #define sysfs_show_str_val(buffer, value) \
    501 		sysfs_show_gen_prop(buffer, "%s\n", value)
    502 
    503 static ssize_t sysprops_show(struct kobject *kobj, struct attribute *attr,
    504 		char *buffer)
    505 {
    506 	ssize_t ret;
    507 
    508 	/* Making sure that the buffer is an empty string */
    509 	buffer[0] = 0;
    510 
    511 	if (attr == &sys_props.attr_genid) {
    512 		ret = sysfs_show_32bit_val(buffer, sys_props.generation_count);
    513 	} else if (attr == &sys_props.attr_props) {
    514 		sysfs_show_64bit_prop(buffer, "platform_oem",
    515 				sys_props.platform_oem);
    516 		sysfs_show_64bit_prop(buffer, "platform_id",
    517 				sys_props.platform_id);
    518 		ret = sysfs_show_64bit_prop(buffer, "platform_rev",
    519 				sys_props.platform_rev);
    520 	} else {
    521 		ret = -EINVAL;
    522 	}
    523 
    524 	return ret;
    525 }
    526 
    527 static void kfd_topology_kobj_release(struct kobject *kobj)
    528 {
    529 	kfree(kobj);
    530 }
    531 
    532 static const struct sysfs_ops sysprops_ops = {
    533 	.show = sysprops_show,
    534 };
    535 
    536 static struct kobj_type sysprops_type = {
    537 	.release = kfd_topology_kobj_release,
    538 	.sysfs_ops = &sysprops_ops,
    539 };
    540 
    541 static ssize_t iolink_show(struct kobject *kobj, struct attribute *attr,
    542 		char *buffer)
    543 {
    544 	ssize_t ret;
    545 	struct kfd_iolink_properties *iolink;
    546 
    547 	/* Making sure that the buffer is an empty string */
    548 	buffer[0] = 0;
    549 
    550 	iolink = container_of(attr, struct kfd_iolink_properties, attr);
    551 	sysfs_show_32bit_prop(buffer, "type", iolink->iolink_type);
    552 	sysfs_show_32bit_prop(buffer, "version_major", iolink->ver_maj);
    553 	sysfs_show_32bit_prop(buffer, "version_minor", iolink->ver_min);
    554 	sysfs_show_32bit_prop(buffer, "node_from", iolink->node_from);
    555 	sysfs_show_32bit_prop(buffer, "node_to", iolink->node_to);
    556 	sysfs_show_32bit_prop(buffer, "weight", iolink->weight);
    557 	sysfs_show_32bit_prop(buffer, "min_latency", iolink->min_latency);
    558 	sysfs_show_32bit_prop(buffer, "max_latency", iolink->max_latency);
    559 	sysfs_show_32bit_prop(buffer, "min_bandwidth", iolink->min_bandwidth);
    560 	sysfs_show_32bit_prop(buffer, "max_bandwidth", iolink->max_bandwidth);
    561 	sysfs_show_32bit_prop(buffer, "recommended_transfer_size",
    562 			iolink->rec_transfer_size);
    563 	ret = sysfs_show_32bit_prop(buffer, "flags", iolink->flags);
    564 
    565 	return ret;
    566 }
    567 
    568 static const struct sysfs_ops iolink_ops = {
    569 	.show = iolink_show,
    570 };
    571 
    572 static struct kobj_type iolink_type = {
    573 	.release = kfd_topology_kobj_release,
    574 	.sysfs_ops = &iolink_ops,
    575 };
    576 
    577 static ssize_t mem_show(struct kobject *kobj, struct attribute *attr,
    578 		char *buffer)
    579 {
    580 	ssize_t ret;
    581 	struct kfd_mem_properties *mem;
    582 
    583 	/* Making sure that the buffer is an empty string */
    584 	buffer[0] = 0;
    585 
    586 	mem = container_of(attr, struct kfd_mem_properties, attr);
    587 	sysfs_show_32bit_prop(buffer, "heap_type", mem->heap_type);
    588 	sysfs_show_64bit_prop(buffer, "size_in_bytes", mem->size_in_bytes);
    589 	sysfs_show_32bit_prop(buffer, "flags", mem->flags);
    590 	sysfs_show_32bit_prop(buffer, "width", mem->width);
    591 	ret = sysfs_show_32bit_prop(buffer, "mem_clk_max", mem->mem_clk_max);
    592 
    593 	return ret;
    594 }
    595 
    596 static const struct sysfs_ops mem_ops = {
    597 	.show = mem_show,
    598 };
    599 
    600 static struct kobj_type mem_type = {
    601 	.release = kfd_topology_kobj_release,
    602 	.sysfs_ops = &mem_ops,
    603 };
    604 
    605 static ssize_t kfd_cache_show(struct kobject *kobj, struct attribute *attr,
    606 		char *buffer)
    607 {
    608 	ssize_t ret;
    609 	uint32_t i;
    610 	struct kfd_cache_properties *cache;
    611 
    612 	/* Making sure that the buffer is an empty string */
    613 	buffer[0] = 0;
    614 
    615 	cache = container_of(attr, struct kfd_cache_properties, attr);
    616 	sysfs_show_32bit_prop(buffer, "processor_id_low",
    617 			cache->processor_id_low);
    618 	sysfs_show_32bit_prop(buffer, "level", cache->cache_level);
    619 	sysfs_show_32bit_prop(buffer, "size", cache->cache_size);
    620 	sysfs_show_32bit_prop(buffer, "cache_line_size", cache->cacheline_size);
    621 	sysfs_show_32bit_prop(buffer, "cache_lines_per_tag",
    622 			cache->cachelines_per_tag);
    623 	sysfs_show_32bit_prop(buffer, "association", cache->cache_assoc);
    624 	sysfs_show_32bit_prop(buffer, "latency", cache->cache_latency);
    625 	sysfs_show_32bit_prop(buffer, "type", cache->cache_type);
    626 	snprintf(buffer, PAGE_SIZE, "%ssibling_map ", buffer);
    627 	for (i = 0; i < KFD_TOPOLOGY_CPU_SIBLINGS; i++)
    628 		ret = snprintf(buffer, PAGE_SIZE, "%s%d%s",
    629 				buffer, cache->sibling_map[i],
    630 				(i == KFD_TOPOLOGY_CPU_SIBLINGS-1) ?
    631 						"\n" : ",");
    632 
    633 	return ret;
    634 }
    635 
    636 static const struct sysfs_ops cache_ops = {
    637 	.show = kfd_cache_show,
    638 };
    639 
    640 static struct kobj_type cache_type = {
    641 	.release = kfd_topology_kobj_release,
    642 	.sysfs_ops = &cache_ops,
    643 };
    644 
    645 static ssize_t node_show(struct kobject *kobj, struct attribute *attr,
    646 		char *buffer)
    647 {
    648 	struct kfd_topology_device *dev;
    649 	char public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE];
    650 	uint32_t i;
    651 	uint32_t log_max_watch_addr;
    652 
    653 	/* Making sure that the buffer is an empty string */
    654 	buffer[0] = 0;
    655 
    656 	if (strcmp(attr->name, "gpu_id") == 0) {
    657 		dev = container_of(attr, struct kfd_topology_device,
    658 				attr_gpuid);
    659 		return sysfs_show_32bit_val(buffer, dev->gpu_id);
    660 	}
    661 
    662 	if (strcmp(attr->name, "name") == 0) {
    663 		dev = container_of(attr, struct kfd_topology_device,
    664 				attr_name);
    665 		for (i = 0; i < KFD_TOPOLOGY_PUBLIC_NAME_SIZE; i++) {
    666 			public_name[i] =
    667 					(char)dev->node_props.marketing_name[i];
    668 			if (dev->node_props.marketing_name[i] == 0)
    669 				break;
    670 		}
    671 		public_name[KFD_TOPOLOGY_PUBLIC_NAME_SIZE-1] = 0x0;
    672 		return sysfs_show_str_val(buffer, public_name);
    673 	}
    674 
    675 	dev = container_of(attr, struct kfd_topology_device,
    676 			attr_props);
    677 	sysfs_show_32bit_prop(buffer, "cpu_cores_count",
    678 			dev->node_props.cpu_cores_count);
    679 	sysfs_show_32bit_prop(buffer, "simd_count",
    680 			dev->node_props.simd_count);
    681 
    682 	if (dev->mem_bank_count < dev->node_props.mem_banks_count) {
    683 		pr_warn("kfd: mem_banks_count truncated from %d to %d\n",
    684 				dev->node_props.mem_banks_count,
    685 				dev->mem_bank_count);
    686 		sysfs_show_32bit_prop(buffer, "mem_banks_count",
    687 				dev->mem_bank_count);
    688 	} else {
    689 		sysfs_show_32bit_prop(buffer, "mem_banks_count",
    690 				dev->node_props.mem_banks_count);
    691 	}
    692 
    693 	sysfs_show_32bit_prop(buffer, "caches_count",
    694 			dev->node_props.caches_count);
    695 	sysfs_show_32bit_prop(buffer, "io_links_count",
    696 			dev->node_props.io_links_count);
    697 	sysfs_show_32bit_prop(buffer, "cpu_core_id_base",
    698 			dev->node_props.cpu_core_id_base);
    699 	sysfs_show_32bit_prop(buffer, "simd_id_base",
    700 			dev->node_props.simd_id_base);
    701 	sysfs_show_32bit_prop(buffer, "max_waves_per_simd",
    702 			dev->node_props.max_waves_per_simd);
    703 	sysfs_show_32bit_prop(buffer, "lds_size_in_kb",
    704 			dev->node_props.lds_size_in_kb);
    705 	sysfs_show_32bit_prop(buffer, "gds_size_in_kb",
    706 			dev->node_props.gds_size_in_kb);
    707 	sysfs_show_32bit_prop(buffer, "wave_front_size",
    708 			dev->node_props.wave_front_size);
    709 	sysfs_show_32bit_prop(buffer, "array_count",
    710 			dev->node_props.array_count);
    711 	sysfs_show_32bit_prop(buffer, "simd_arrays_per_engine",
    712 			dev->node_props.simd_arrays_per_engine);
    713 	sysfs_show_32bit_prop(buffer, "cu_per_simd_array",
    714 			dev->node_props.cu_per_simd_array);
    715 	sysfs_show_32bit_prop(buffer, "simd_per_cu",
    716 			dev->node_props.simd_per_cu);
    717 	sysfs_show_32bit_prop(buffer, "max_slots_scratch_cu",
    718 			dev->node_props.max_slots_scratch_cu);
    719 	sysfs_show_32bit_prop(buffer, "vendor_id",
    720 			dev->node_props.vendor_id);
    721 	sysfs_show_32bit_prop(buffer, "device_id",
    722 			dev->node_props.device_id);
    723 	sysfs_show_32bit_prop(buffer, "location_id",
    724 			dev->node_props.location_id);
    725 
    726 	if (dev->gpu) {
    727 		log_max_watch_addr =
    728 			__ilog2_u32(dev->gpu->device_info->num_of_watch_points);
    729 
    730 		if (log_max_watch_addr) {
    731 			dev->node_props.capability |=
    732 					HSA_CAP_WATCH_POINTS_SUPPORTED;
    733 
    734 			dev->node_props.capability |=
    735 				((log_max_watch_addr <<
    736 					HSA_CAP_WATCH_POINTS_TOTALBITS_SHIFT) &
    737 				HSA_CAP_WATCH_POINTS_TOTALBITS_MASK);
    738 		}
    739 
    740 		sysfs_show_32bit_prop(buffer, "max_engine_clk_fcompute",
    741 			dev->gpu->kfd2kgd->get_max_engine_clock_in_mhz(
    742 					dev->gpu->kgd));
    743 
    744 		sysfs_show_64bit_prop(buffer, "local_mem_size",
    745 				(unsigned long long int) 0);
    746 
    747 		sysfs_show_32bit_prop(buffer, "fw_version",
    748 			dev->gpu->kfd2kgd->get_fw_version(
    749 						dev->gpu->kgd,
    750 						KGD_ENGINE_MEC1));
    751 		sysfs_show_32bit_prop(buffer, "capability",
    752 				dev->node_props.capability);
    753 	}
    754 
    755 	return sysfs_show_32bit_prop(buffer, "max_engine_clk_ccompute",
    756 					cpufreq_quick_get_max(0)/1000);
    757 }
    758 
    759 static const struct sysfs_ops node_ops = {
    760 	.show = node_show,
    761 };
    762 
    763 static struct kobj_type node_type = {
    764 	.release = kfd_topology_kobj_release,
    765 	.sysfs_ops = &node_ops,
    766 };
    767 
    768 static void kfd_remove_sysfs_file(struct kobject *kobj, struct attribute *attr)
    769 {
    770 	sysfs_remove_file(kobj, attr);
    771 	kobject_del(kobj);
    772 	kobject_put(kobj);
    773 }
    774 
    775 static void kfd_remove_sysfs_node_entry(struct kfd_topology_device *dev)
    776 {
    777 	struct kfd_iolink_properties *iolink;
    778 	struct kfd_cache_properties *cache;
    779 	struct kfd_mem_properties *mem;
    780 
    781 	BUG_ON(!dev);
    782 
    783 	if (dev->kobj_iolink) {
    784 		list_for_each_entry(iolink, &dev->io_link_props, list)
    785 			if (iolink->kobj) {
    786 				kfd_remove_sysfs_file(iolink->kobj,
    787 							&iolink->attr);
    788 				iolink->kobj = NULL;
    789 			}
    790 		kobject_del(dev->kobj_iolink);
    791 		kobject_put(dev->kobj_iolink);
    792 		dev->kobj_iolink = NULL;
    793 	}
    794 
    795 	if (dev->kobj_cache) {
    796 		list_for_each_entry(cache, &dev->cache_props, list)
    797 			if (cache->kobj) {
    798 				kfd_remove_sysfs_file(cache->kobj,
    799 							&cache->attr);
    800 				cache->kobj = NULL;
    801 			}
    802 		kobject_del(dev->kobj_cache);
    803 		kobject_put(dev->kobj_cache);
    804 		dev->kobj_cache = NULL;
    805 	}
    806 
    807 	if (dev->kobj_mem) {
    808 		list_for_each_entry(mem, &dev->mem_props, list)
    809 			if (mem->kobj) {
    810 				kfd_remove_sysfs_file(mem->kobj, &mem->attr);
    811 				mem->kobj = NULL;
    812 			}
    813 		kobject_del(dev->kobj_mem);
    814 		kobject_put(dev->kobj_mem);
    815 		dev->kobj_mem = NULL;
    816 	}
    817 
    818 	if (dev->kobj_node) {
    819 		sysfs_remove_file(dev->kobj_node, &dev->attr_gpuid);
    820 		sysfs_remove_file(dev->kobj_node, &dev->attr_name);
    821 		sysfs_remove_file(dev->kobj_node, &dev->attr_props);
    822 		kobject_del(dev->kobj_node);
    823 		kobject_put(dev->kobj_node);
    824 		dev->kobj_node = NULL;
    825 	}
    826 }
    827 
    828 static int kfd_build_sysfs_node_entry(struct kfd_topology_device *dev,
    829 		uint32_t id)
    830 {
    831 	struct kfd_iolink_properties *iolink;
    832 	struct kfd_cache_properties *cache;
    833 	struct kfd_mem_properties *mem;
    834 	int ret;
    835 	uint32_t i;
    836 
    837 	BUG_ON(!dev);
    838 
    839 	/*
    840 	 * Creating the sysfs folders
    841 	 */
    842 	BUG_ON(dev->kobj_node);
    843 	dev->kobj_node = kfd_alloc_struct(dev->kobj_node);
    844 	if (!dev->kobj_node)
    845 		return -ENOMEM;
    846 
    847 	ret = kobject_init_and_add(dev->kobj_node, &node_type,
    848 			sys_props.kobj_nodes, "%d", id);
    849 	if (ret < 0)
    850 		return ret;
    851 
    852 	dev->kobj_mem = kobject_create_and_add("mem_banks", dev->kobj_node);
    853 	if (!dev->kobj_mem)
    854 		return -ENOMEM;
    855 
    856 	dev->kobj_cache = kobject_create_and_add("caches", dev->kobj_node);
    857 	if (!dev->kobj_cache)
    858 		return -ENOMEM;
    859 
    860 	dev->kobj_iolink = kobject_create_and_add("io_links", dev->kobj_node);
    861 	if (!dev->kobj_iolink)
    862 		return -ENOMEM;
    863 
    864 	/*
    865 	 * Creating sysfs files for node properties
    866 	 */
    867 	dev->attr_gpuid.name = "gpu_id";
    868 	dev->attr_gpuid.mode = KFD_SYSFS_FILE_MODE;
    869 	sysfs_attr_init(&dev->attr_gpuid);
    870 	dev->attr_name.name = "name";
    871 	dev->attr_name.mode = KFD_SYSFS_FILE_MODE;
    872 	sysfs_attr_init(&dev->attr_name);
    873 	dev->attr_props.name = "properties";
    874 	dev->attr_props.mode = KFD_SYSFS_FILE_MODE;
    875 	sysfs_attr_init(&dev->attr_props);
    876 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_gpuid);
    877 	if (ret < 0)
    878 		return ret;
    879 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_name);
    880 	if (ret < 0)
    881 		return ret;
    882 	ret = sysfs_create_file(dev->kobj_node, &dev->attr_props);
    883 	if (ret < 0)
    884 		return ret;
    885 
    886 	i = 0;
    887 	list_for_each_entry(mem, &dev->mem_props, list) {
    888 		mem->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    889 		if (!mem->kobj)
    890 			return -ENOMEM;
    891 		ret = kobject_init_and_add(mem->kobj, &mem_type,
    892 				dev->kobj_mem, "%d", i);
    893 		if (ret < 0)
    894 			return ret;
    895 
    896 		mem->attr.name = "properties";
    897 		mem->attr.mode = KFD_SYSFS_FILE_MODE;
    898 		sysfs_attr_init(&mem->attr);
    899 		ret = sysfs_create_file(mem->kobj, &mem->attr);
    900 		if (ret < 0)
    901 			return ret;
    902 		i++;
    903 	}
    904 
    905 	i = 0;
    906 	list_for_each_entry(cache, &dev->cache_props, list) {
    907 		cache->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    908 		if (!cache->kobj)
    909 			return -ENOMEM;
    910 		ret = kobject_init_and_add(cache->kobj, &cache_type,
    911 				dev->kobj_cache, "%d", i);
    912 		if (ret < 0)
    913 			return ret;
    914 
    915 		cache->attr.name = "properties";
    916 		cache->attr.mode = KFD_SYSFS_FILE_MODE;
    917 		sysfs_attr_init(&cache->attr);
    918 		ret = sysfs_create_file(cache->kobj, &cache->attr);
    919 		if (ret < 0)
    920 			return ret;
    921 		i++;
    922 	}
    923 
    924 	i = 0;
    925 	list_for_each_entry(iolink, &dev->io_link_props, list) {
    926 		iolink->kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    927 		if (!iolink->kobj)
    928 			return -ENOMEM;
    929 		ret = kobject_init_and_add(iolink->kobj, &iolink_type,
    930 				dev->kobj_iolink, "%d", i);
    931 		if (ret < 0)
    932 			return ret;
    933 
    934 		iolink->attr.name = "properties";
    935 		iolink->attr.mode = KFD_SYSFS_FILE_MODE;
    936 		sysfs_attr_init(&iolink->attr);
    937 		ret = sysfs_create_file(iolink->kobj, &iolink->attr);
    938 		if (ret < 0)
    939 			return ret;
    940 		i++;
    941 }
    942 
    943 	return 0;
    944 }
    945 
    946 static int kfd_build_sysfs_node_tree(void)
    947 {
    948 	struct kfd_topology_device *dev;
    949 	int ret;
    950 	uint32_t i = 0;
    951 
    952 	list_for_each_entry(dev, &topology_device_list, list) {
    953 		ret = kfd_build_sysfs_node_entry(dev, i);
    954 		if (ret < 0)
    955 			return ret;
    956 		i++;
    957 	}
    958 
    959 	return 0;
    960 }
    961 
    962 static void kfd_remove_sysfs_node_tree(void)
    963 {
    964 	struct kfd_topology_device *dev;
    965 
    966 	list_for_each_entry(dev, &topology_device_list, list)
    967 		kfd_remove_sysfs_node_entry(dev);
    968 }
    969 
    970 static int kfd_topology_update_sysfs(void)
    971 {
    972 	int ret;
    973 
    974 	pr_info("Creating topology SYSFS entries\n");
    975 	if (sys_props.kobj_topology == NULL) {
    976 		sys_props.kobj_topology =
    977 				kfd_alloc_struct(sys_props.kobj_topology);
    978 		if (!sys_props.kobj_topology)
    979 			return -ENOMEM;
    980 
    981 		ret = kobject_init_and_add(sys_props.kobj_topology,
    982 				&sysprops_type,  &kfd_device->kobj,
    983 				"topology");
    984 		if (ret < 0)
    985 			return ret;
    986 
    987 		sys_props.kobj_nodes = kobject_create_and_add("nodes",
    988 				sys_props.kobj_topology);
    989 		if (!sys_props.kobj_nodes)
    990 			return -ENOMEM;
    991 
    992 		sys_props.attr_genid.name = "generation_id";
    993 		sys_props.attr_genid.mode = KFD_SYSFS_FILE_MODE;
    994 		sysfs_attr_init(&sys_props.attr_genid);
    995 		ret = sysfs_create_file(sys_props.kobj_topology,
    996 				&sys_props.attr_genid);
    997 		if (ret < 0)
    998 			return ret;
    999 
   1000 		sys_props.attr_props.name = "system_properties";
   1001 		sys_props.attr_props.mode = KFD_SYSFS_FILE_MODE;
   1002 		sysfs_attr_init(&sys_props.attr_props);
   1003 		ret = sysfs_create_file(sys_props.kobj_topology,
   1004 				&sys_props.attr_props);
   1005 		if (ret < 0)
   1006 			return ret;
   1007 	}
   1008 
   1009 	kfd_remove_sysfs_node_tree();
   1010 
   1011 	return kfd_build_sysfs_node_tree();
   1012 }
   1013 
   1014 static void kfd_topology_release_sysfs(void)
   1015 {
   1016 	kfd_remove_sysfs_node_tree();
   1017 	if (sys_props.kobj_topology) {
   1018 		sysfs_remove_file(sys_props.kobj_topology,
   1019 				&sys_props.attr_genid);
   1020 		sysfs_remove_file(sys_props.kobj_topology,
   1021 				&sys_props.attr_props);
   1022 		if (sys_props.kobj_nodes) {
   1023 			kobject_del(sys_props.kobj_nodes);
   1024 			kobject_put(sys_props.kobj_nodes);
   1025 			sys_props.kobj_nodes = NULL;
   1026 		}
   1027 		kobject_del(sys_props.kobj_topology);
   1028 		kobject_put(sys_props.kobj_topology);
   1029 		sys_props.kobj_topology = NULL;
   1030 	}
   1031 }
   1032 
   1033 int kfd_topology_init(void)
   1034 {
   1035 	void *crat_image = NULL;
   1036 	size_t image_size = 0;
   1037 	int ret;
   1038 
   1039 	/*
   1040 	 * Initialize the head for the topology device list
   1041 	 */
   1042 	INIT_LIST_HEAD(&topology_device_list);
   1043 	init_rwsem(&topology_lock);
   1044 	topology_crat_parsed = 0;
   1045 
   1046 	memset(&sys_props, 0, sizeof(sys_props));
   1047 
   1048 	/*
   1049 	 * Get the CRAT image from the ACPI
   1050 	 */
   1051 	ret = kfd_topology_get_crat_acpi(crat_image, &image_size);
   1052 	if (ret == 0 && image_size > 0) {
   1053 		pr_info("Found CRAT image with size=%zd\n", image_size);
   1054 		crat_image = kmalloc(image_size, GFP_KERNEL);
   1055 		if (!crat_image) {
   1056 			ret = -ENOMEM;
   1057 			pr_err("No memory for allocating CRAT image\n");
   1058 			goto err;
   1059 		}
   1060 		ret = kfd_topology_get_crat_acpi(crat_image, &image_size);
   1061 
   1062 		if (ret == 0) {
   1063 			down_write(&topology_lock);
   1064 			ret = kfd_parse_crat_table(crat_image);
   1065 			if (ret == 0)
   1066 				ret = kfd_topology_update_sysfs();
   1067 			up_write(&topology_lock);
   1068 		} else {
   1069 			pr_err("Couldn't get CRAT table size from ACPI\n");
   1070 		}
   1071 		kfree(crat_image);
   1072 	} else if (ret == -ENODATA) {
   1073 		ret = 0;
   1074 	} else {
   1075 		pr_err("Couldn't get CRAT table size from ACPI\n");
   1076 	}
   1077 
   1078 err:
   1079 	pr_info("Finished initializing topology ret=%d\n", ret);
   1080 	return ret;
   1081 }
   1082 
   1083 void kfd_topology_shutdown(void)
   1084 {
   1085 	kfd_topology_release_sysfs();
   1086 	kfd_release_live_view();
   1087 }
   1088 
   1089 static void kfd_debug_print_topology(void)
   1090 {
   1091 	struct kfd_topology_device *dev;
   1092 	uint32_t i = 0;
   1093 
   1094 	pr_info("DEBUG PRINT OF TOPOLOGY:");
   1095 	list_for_each_entry(dev, &topology_device_list, list) {
   1096 		pr_info("Node: %d\n", i);
   1097 		pr_info("\tGPU assigned: %s\n", (dev->gpu ? "yes" : "no"));
   1098 		pr_info("\tCPU count: %d\n", dev->node_props.cpu_cores_count);
   1099 		pr_info("\tSIMD count: %d", dev->node_props.simd_count);
   1100 		i++;
   1101 	}
   1102 }
   1103 
   1104 static uint32_t kfd_generate_gpu_id(struct kfd_dev *gpu)
   1105 {
   1106 	uint32_t hashout;
   1107 	uint32_t buf[7];
   1108 	int i;
   1109 
   1110 	if (!gpu)
   1111 		return 0;
   1112 
   1113 	buf[0] = gpu->pdev->devfn;
   1114 	buf[1] = gpu->pdev->subsystem_vendor;
   1115 	buf[2] = gpu->pdev->subsystem_device;
   1116 	buf[3] = gpu->pdev->device;
   1117 	buf[4] = gpu->pdev->bus->number;
   1118 	buf[5] = (uint32_t)(gpu->kfd2kgd->get_vmem_size(gpu->kgd)
   1119 			& 0xffffffff);
   1120 	buf[6] = (uint32_t)(gpu->kfd2kgd->get_vmem_size(gpu->kgd) >> 32);
   1121 
   1122 	for (i = 0, hashout = 0; i < 7; i++)
   1123 		hashout ^= hash_32(buf[i], KFD_GPU_ID_HASH_WIDTH);
   1124 
   1125 	return hashout;
   1126 }
   1127 
   1128 static struct kfd_topology_device *kfd_assign_gpu(struct kfd_dev *gpu)
   1129 {
   1130 	struct kfd_topology_device *dev;
   1131 	struct kfd_topology_device *out_dev = NULL;
   1132 
   1133 	BUG_ON(!gpu);
   1134 
   1135 	list_for_each_entry(dev, &topology_device_list, list)
   1136 		if (dev->gpu == NULL && dev->node_props.simd_count > 0) {
   1137 			dev->gpu = gpu;
   1138 			out_dev = dev;
   1139 			break;
   1140 		}
   1141 
   1142 	return out_dev;
   1143 }
   1144 
   1145 static void kfd_notify_gpu_change(uint32_t gpu_id, int arrival)
   1146 {
   1147 	/*
   1148 	 * TODO: Generate an event for thunk about the arrival/removal
   1149 	 * of the GPU
   1150 	 */
   1151 }
   1152 
   1153 int kfd_topology_add_device(struct kfd_dev *gpu)
   1154 {
   1155 	uint32_t gpu_id;
   1156 	struct kfd_topology_device *dev;
   1157 	int res;
   1158 
   1159 	BUG_ON(!gpu);
   1160 
   1161 	gpu_id = kfd_generate_gpu_id(gpu);
   1162 
   1163 	pr_debug("kfd: Adding new GPU (ID: 0x%x) to topology\n", gpu_id);
   1164 
   1165 	down_write(&topology_lock);
   1166 	/*
   1167 	 * Try to assign the GPU to existing topology device (generated from
   1168 	 * CRAT table
   1169 	 */
   1170 	dev = kfd_assign_gpu(gpu);
   1171 	if (!dev) {
   1172 		pr_info("GPU was not found in the current topology. Extending.\n");
   1173 		kfd_debug_print_topology();
   1174 		dev = kfd_create_topology_device();
   1175 		if (!dev) {
   1176 			res = -ENOMEM;
   1177 			goto err;
   1178 		}
   1179 		dev->gpu = gpu;
   1180 
   1181 		/*
   1182 		 * TODO: Make a call to retrieve topology information from the
   1183 		 * GPU vBIOS
   1184 		 */
   1185 
   1186 		/*
   1187 		 * Update the SYSFS tree, since we added another topology device
   1188 		 */
   1189 		if (kfd_topology_update_sysfs() < 0)
   1190 			kfd_topology_release_sysfs();
   1191 
   1192 	}
   1193 
   1194 	dev->gpu_id = gpu_id;
   1195 	gpu->id = gpu_id;
   1196 	dev->node_props.vendor_id = gpu->pdev->vendor;
   1197 	dev->node_props.device_id = gpu->pdev->device;
   1198 	dev->node_props.location_id = (gpu->pdev->bus->number << 24) +
   1199 			(gpu->pdev->devfn & 0xffffff);
   1200 	/*
   1201 	 * TODO: Retrieve max engine clock values from KGD
   1202 	 */
   1203 
   1204 	if (dev->gpu->device_info->asic_family == CHIP_CARRIZO) {
   1205 		dev->node_props.capability |= HSA_CAP_DOORBELL_PACKET_TYPE;
   1206 		pr_info("amdkfd: adding doorbell packet type capability\n");
   1207 	}
   1208 
   1209 	res = 0;
   1210 
   1211 err:
   1212 	up_write(&topology_lock);
   1213 
   1214 	if (res == 0)
   1215 		kfd_notify_gpu_change(gpu_id, 1);
   1216 
   1217 	return res;
   1218 }
   1219 
   1220 int kfd_topology_remove_device(struct kfd_dev *gpu)
   1221 {
   1222 	struct kfd_topology_device *dev;
   1223 	uint32_t gpu_id;
   1224 	int res = -ENODEV;
   1225 
   1226 	BUG_ON(!gpu);
   1227 
   1228 	down_write(&topology_lock);
   1229 
   1230 	list_for_each_entry(dev, &topology_device_list, list)
   1231 		if (dev->gpu == gpu) {
   1232 			gpu_id = dev->gpu_id;
   1233 			kfd_remove_sysfs_node_entry(dev);
   1234 			kfd_release_topology_device(dev);
   1235 			res = 0;
   1236 			if (kfd_topology_update_sysfs() < 0)
   1237 				kfd_topology_release_sysfs();
   1238 			break;
   1239 		}
   1240 
   1241 	up_write(&topology_lock);
   1242 
   1243 	if (res == 0)
   1244 		kfd_notify_gpu_change(gpu_id, 0);
   1245 
   1246 	return res;
   1247 }
   1248 
   1249 /*
   1250  * When idx is out of bounds, the function will return NULL
   1251  */
   1252 struct kfd_dev *kfd_topology_enum_kfd_devices(uint8_t idx)
   1253 {
   1254 
   1255 	struct kfd_topology_device *top_dev;
   1256 	struct kfd_dev *device = NULL;
   1257 	uint8_t device_idx = 0;
   1258 
   1259 	down_read(&topology_lock);
   1260 
   1261 	list_for_each_entry(top_dev, &topology_device_list, list) {
   1262 		if (device_idx == idx) {
   1263 			device = top_dev->gpu;
   1264 			break;
   1265 		}
   1266 
   1267 		device_idx++;
   1268 	}
   1269 
   1270 	up_read(&topology_lock);
   1271 
   1272 	return device;
   1273 
   1274 }
   1275