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