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