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