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