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