1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ 3 #include <linux/platform_device.h> 4 #include <linux/memregion.h> 5 #include <linux/workqueue.h> 6 #include <linux/debugfs.h> 7 #include <linux/device.h> 8 #include <linux/module.h> 9 #include <linux/pci.h> 10 #include <linux/slab.h> 11 #include <linux/idr.h> 12 #include <linux/node.h> 13 #include <cxl/einj.h> 14 #include <cxlmem.h> 15 #include <cxlpci.h> 16 #include <cxl.h> 17 #include "core.h" 18 19 /** 20 * DOC: cxl core 21 * 22 * The CXL core provides a set of interfaces that can be consumed by CXL aware 23 * drivers. The interfaces allow for creation, modification, and destruction of 24 * regions, memory devices, ports, and decoders. CXL aware drivers must register 25 * with the CXL core via these interfaces in order to be able to participate in 26 * cross-device interleave coordination. The CXL core also establishes and 27 * maintains the bridge to the nvdimm subsystem. 28 * 29 * CXL core introduces sysfs hierarchy to control the devices that are 30 * instantiated by the core. 31 */ 32 33 static DEFINE_IDA(cxl_port_ida); 34 static DEFINE_XARRAY(cxl_root_buses); 35 36 /* 37 * The terminal device in PCI is NULL and @platform_bus 38 * for platform devices (for cxl_test) 39 */ 40 static bool is_cxl_host_bridge(struct device *dev) 41 { 42 return (!dev || dev == &platform_bus); 43 } 44 45 int cxl_num_decoders_committed(struct cxl_port *port) 46 { 47 lockdep_assert_held(&cxl_rwsem.region); 48 49 return port->commit_end + 1; 50 } 51 52 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr, 53 char *buf) 54 { 55 return sysfs_emit(buf, "%s\n", dev->type->name); 56 } 57 static DEVICE_ATTR_RO(devtype); 58 59 static int cxl_device_id(const struct device *dev) 60 { 61 if (dev->type == &cxl_nvdimm_bridge_type) 62 return CXL_DEVICE_NVDIMM_BRIDGE; 63 if (dev->type == &cxl_nvdimm_type) 64 return CXL_DEVICE_NVDIMM; 65 if (dev->type == CXL_PMEM_REGION_TYPE()) 66 return CXL_DEVICE_PMEM_REGION; 67 if (dev->type == CXL_DAX_REGION_TYPE()) 68 return CXL_DEVICE_DAX_REGION; 69 if (is_cxl_port(dev)) { 70 if (is_cxl_root(to_cxl_port(dev))) 71 return CXL_DEVICE_ROOT; 72 return CXL_DEVICE_PORT; 73 } 74 if (is_cxl_memdev(dev)) 75 return CXL_DEVICE_MEMORY_EXPANDER; 76 if (dev->type == CXL_REGION_TYPE()) 77 return CXL_DEVICE_REGION; 78 if (dev->type == &cxl_pmu_type) 79 return CXL_DEVICE_PMU; 80 return 0; 81 } 82 83 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 84 char *buf) 85 { 86 return sysfs_emit(buf, CXL_MODALIAS_FMT "\n", cxl_device_id(dev)); 87 } 88 static DEVICE_ATTR_RO(modalias); 89 90 static struct attribute *cxl_base_attributes[] = { 91 &dev_attr_devtype.attr, 92 &dev_attr_modalias.attr, 93 NULL, 94 }; 95 96 struct attribute_group cxl_base_attribute_group = { 97 .attrs = cxl_base_attributes, 98 }; 99 100 static ssize_t start_show(struct device *dev, struct device_attribute *attr, 101 char *buf) 102 { 103 struct cxl_decoder *cxld = to_cxl_decoder(dev); 104 105 return sysfs_emit(buf, "%#llx\n", cxld->hpa_range.start); 106 } 107 static DEVICE_ATTR_ADMIN_RO(start); 108 109 static ssize_t size_show(struct device *dev, struct device_attribute *attr, 110 char *buf) 111 { 112 struct cxl_decoder *cxld = to_cxl_decoder(dev); 113 114 return sysfs_emit(buf, "%#llx\n", range_len(&cxld->hpa_range)); 115 } 116 static DEVICE_ATTR_RO(size); 117 118 #define CXL_DECODER_FLAG_ATTR(name, flag) \ 119 static ssize_t name##_show(struct device *dev, \ 120 struct device_attribute *attr, char *buf) \ 121 { \ 122 struct cxl_decoder *cxld = to_cxl_decoder(dev); \ 123 \ 124 return sysfs_emit(buf, "%s\n", \ 125 (cxld->flags & (flag)) ? "1" : "0"); \ 126 } \ 127 static DEVICE_ATTR_RO(name) 128 129 CXL_DECODER_FLAG_ATTR(cap_pmem, CXL_DECODER_F_PMEM); 130 CXL_DECODER_FLAG_ATTR(cap_ram, CXL_DECODER_F_RAM); 131 CXL_DECODER_FLAG_ATTR(cap_type2, CXL_DECODER_F_TYPE2); 132 CXL_DECODER_FLAG_ATTR(cap_type3, CXL_DECODER_F_TYPE3); 133 CXL_DECODER_FLAG_ATTR(locked, CXL_DECODER_F_LOCK); 134 135 static ssize_t target_type_show(struct device *dev, 136 struct device_attribute *attr, char *buf) 137 { 138 struct cxl_decoder *cxld = to_cxl_decoder(dev); 139 140 switch (cxld->target_type) { 141 case CXL_DECODER_DEVMEM: 142 return sysfs_emit(buf, "accelerator\n"); 143 case CXL_DECODER_HOSTONLYMEM: 144 return sysfs_emit(buf, "expander\n"); 145 } 146 return -ENXIO; 147 } 148 static DEVICE_ATTR_RO(target_type); 149 150 static ssize_t emit_target_list(struct cxl_switch_decoder *cxlsd, char *buf) 151 { 152 struct cxl_decoder *cxld = &cxlsd->cxld; 153 ssize_t offset = 0; 154 int i, rc = 0; 155 156 for (i = 0; i < cxld->interleave_ways; i++) { 157 struct cxl_dport *dport = cxlsd->target[i]; 158 struct cxl_dport *next = NULL; 159 160 if (!dport) 161 break; 162 163 if (i + 1 < cxld->interleave_ways) 164 next = cxlsd->target[i + 1]; 165 rc = sysfs_emit_at(buf, offset, "%d%s", dport->port_id, 166 next ? "," : ""); 167 if (rc < 0) 168 return rc; 169 offset += rc; 170 } 171 172 return offset; 173 } 174 175 static ssize_t target_list_show(struct device *dev, 176 struct device_attribute *attr, char *buf) 177 { 178 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev); 179 ssize_t offset; 180 int rc; 181 182 guard(rwsem_read)(&cxl_rwsem.region); 183 rc = emit_target_list(cxlsd, buf); 184 if (rc < 0) 185 return rc; 186 offset = rc; 187 188 rc = sysfs_emit_at(buf, offset, "\n"); 189 if (rc < 0) 190 return rc; 191 192 return offset + rc; 193 } 194 static DEVICE_ATTR_RO(target_list); 195 196 static ssize_t mode_show(struct device *dev, struct device_attribute *attr, 197 char *buf) 198 { 199 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 200 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 201 struct cxl_dev_state *cxlds = cxlmd->cxlds; 202 /* without @cxl_rwsem.dpa, make sure @part is not reloaded */ 203 int part = READ_ONCE(cxled->part); 204 const char *desc; 205 206 if (part < 0) 207 desc = "none"; 208 else 209 desc = cxlds->part[part].res.name; 210 211 return sysfs_emit(buf, "%s\n", desc); 212 } 213 214 static ssize_t mode_store(struct device *dev, struct device_attribute *attr, 215 const char *buf, size_t len) 216 { 217 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 218 enum cxl_partition_mode mode; 219 ssize_t rc; 220 221 if (sysfs_streq(buf, "pmem")) 222 mode = CXL_PARTMODE_PMEM; 223 else if (sysfs_streq(buf, "ram")) 224 mode = CXL_PARTMODE_RAM; 225 else 226 return -EINVAL; 227 228 rc = cxl_dpa_set_part(cxled, mode); 229 if (rc) 230 return rc; 231 232 return len; 233 } 234 static DEVICE_ATTR_RW(mode); 235 236 static ssize_t dpa_resource_show(struct device *dev, struct device_attribute *attr, 237 char *buf) 238 { 239 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 240 241 guard(rwsem_read)(&cxl_rwsem.dpa); 242 return sysfs_emit(buf, "%#llx\n", (u64)cxl_dpa_resource_start(cxled)); 243 } 244 static DEVICE_ATTR_RO(dpa_resource); 245 246 static ssize_t dpa_size_show(struct device *dev, struct device_attribute *attr, 247 char *buf) 248 { 249 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 250 resource_size_t size = cxl_dpa_size(cxled); 251 252 return sysfs_emit(buf, "%pa\n", &size); 253 } 254 255 static ssize_t dpa_size_store(struct device *dev, struct device_attribute *attr, 256 const char *buf, size_t len) 257 { 258 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 259 unsigned long long size; 260 ssize_t rc; 261 262 rc = kstrtoull(buf, 0, &size); 263 if (rc) 264 return rc; 265 266 if (!IS_ALIGNED(size, SZ_256M)) 267 return -EINVAL; 268 269 rc = cxl_dpa_free(cxled); 270 if (rc) 271 return rc; 272 273 if (size == 0) 274 return len; 275 276 rc = cxl_dpa_alloc(cxled, size); 277 if (rc) 278 return rc; 279 280 return len; 281 } 282 static DEVICE_ATTR_RW(dpa_size); 283 284 static ssize_t interleave_granularity_show(struct device *dev, 285 struct device_attribute *attr, 286 char *buf) 287 { 288 struct cxl_decoder *cxld = to_cxl_decoder(dev); 289 290 return sysfs_emit(buf, "%d\n", cxld->interleave_granularity); 291 } 292 293 static DEVICE_ATTR_RO(interleave_granularity); 294 295 static ssize_t interleave_ways_show(struct device *dev, 296 struct device_attribute *attr, char *buf) 297 { 298 struct cxl_decoder *cxld = to_cxl_decoder(dev); 299 300 return sysfs_emit(buf, "%d\n", cxld->interleave_ways); 301 } 302 303 static DEVICE_ATTR_RO(interleave_ways); 304 305 static ssize_t qos_class_show(struct device *dev, 306 struct device_attribute *attr, char *buf) 307 { 308 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 309 310 return sysfs_emit(buf, "%d\n", cxlrd->qos_class); 311 } 312 static DEVICE_ATTR_RO(qos_class); 313 314 static struct attribute *cxl_decoder_base_attrs[] = { 315 &dev_attr_start.attr, 316 &dev_attr_size.attr, 317 &dev_attr_locked.attr, 318 &dev_attr_interleave_granularity.attr, 319 &dev_attr_interleave_ways.attr, 320 NULL, 321 }; 322 323 static struct attribute_group cxl_decoder_base_attribute_group = { 324 .attrs = cxl_decoder_base_attrs, 325 }; 326 327 static struct attribute *cxl_decoder_root_attrs[] = { 328 &dev_attr_cap_pmem.attr, 329 &dev_attr_cap_ram.attr, 330 &dev_attr_cap_type2.attr, 331 &dev_attr_cap_type3.attr, 332 &dev_attr_target_list.attr, 333 &dev_attr_qos_class.attr, 334 SET_CXL_REGION_ATTR(create_pmem_region) 335 SET_CXL_REGION_ATTR(create_ram_region) 336 SET_CXL_REGION_ATTR(delete_region) 337 NULL, 338 }; 339 340 static bool can_create_pmem(struct cxl_root_decoder *cxlrd) 341 { 342 unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_PMEM; 343 344 return (cxlrd->cxlsd.cxld.flags & flags) == flags; 345 } 346 347 static bool can_create_ram(struct cxl_root_decoder *cxlrd) 348 { 349 unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_RAM; 350 351 return (cxlrd->cxlsd.cxld.flags & flags) == flags; 352 } 353 354 static umode_t cxl_root_decoder_visible(struct kobject *kobj, struct attribute *a, int n) 355 { 356 struct device *dev = kobj_to_dev(kobj); 357 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 358 359 if (a == CXL_REGION_ATTR(create_pmem_region) && !can_create_pmem(cxlrd)) 360 return 0; 361 362 if (a == CXL_REGION_ATTR(create_ram_region) && !can_create_ram(cxlrd)) 363 return 0; 364 365 if (a == CXL_REGION_ATTR(delete_region) && 366 !(can_create_pmem(cxlrd) || can_create_ram(cxlrd))) 367 return 0; 368 369 return a->mode; 370 } 371 372 static struct attribute_group cxl_decoder_root_attribute_group = { 373 .attrs = cxl_decoder_root_attrs, 374 .is_visible = cxl_root_decoder_visible, 375 }; 376 377 static const struct attribute_group *cxl_decoder_root_attribute_groups[] = { 378 &cxl_decoder_root_attribute_group, 379 &cxl_decoder_base_attribute_group, 380 &cxl_base_attribute_group, 381 NULL, 382 }; 383 384 static struct attribute *cxl_decoder_switch_attrs[] = { 385 &dev_attr_target_type.attr, 386 &dev_attr_target_list.attr, 387 SET_CXL_REGION_ATTR(region) 388 NULL, 389 }; 390 391 static struct attribute_group cxl_decoder_switch_attribute_group = { 392 .attrs = cxl_decoder_switch_attrs, 393 }; 394 395 static const struct attribute_group *cxl_decoder_switch_attribute_groups[] = { 396 &cxl_decoder_switch_attribute_group, 397 &cxl_decoder_base_attribute_group, 398 &cxl_base_attribute_group, 399 NULL, 400 }; 401 402 static struct attribute *cxl_decoder_endpoint_attrs[] = { 403 &dev_attr_target_type.attr, 404 &dev_attr_mode.attr, 405 &dev_attr_dpa_size.attr, 406 &dev_attr_dpa_resource.attr, 407 SET_CXL_REGION_ATTR(region) 408 NULL, 409 }; 410 411 static struct attribute_group cxl_decoder_endpoint_attribute_group = { 412 .attrs = cxl_decoder_endpoint_attrs, 413 }; 414 415 static const struct attribute_group *cxl_decoder_endpoint_attribute_groups[] = { 416 &cxl_decoder_base_attribute_group, 417 &cxl_decoder_endpoint_attribute_group, 418 &cxl_base_attribute_group, 419 NULL, 420 }; 421 422 static void __cxl_decoder_release(struct cxl_decoder *cxld) 423 { 424 struct cxl_port *port = to_cxl_port(cxld->dev.parent); 425 426 ida_free(&port->decoder_ida, cxld->id); 427 put_device(&port->dev); 428 } 429 430 static void cxl_endpoint_decoder_release(struct device *dev) 431 { 432 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 433 434 __cxl_decoder_release(&cxled->cxld); 435 kfree(cxled); 436 } 437 438 static void cxl_switch_decoder_release(struct device *dev) 439 { 440 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev); 441 442 __cxl_decoder_release(&cxlsd->cxld); 443 kfree(cxlsd); 444 } 445 446 struct cxl_root_decoder *to_cxl_root_decoder(struct device *dev) 447 { 448 if (dev_WARN_ONCE(dev, !is_root_decoder(dev), 449 "not a cxl_root_decoder device\n")) 450 return NULL; 451 return container_of(dev, struct cxl_root_decoder, cxlsd.cxld.dev); 452 } 453 EXPORT_SYMBOL_NS_GPL(to_cxl_root_decoder, "CXL"); 454 455 static void cxl_root_decoder_release(struct device *dev) 456 { 457 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 458 459 if (atomic_read(&cxlrd->region_id) >= 0) 460 memregion_free(atomic_read(&cxlrd->region_id)); 461 mutex_destroy(&cxlrd->regions_lock); 462 xa_destroy(&cxlrd->regions); 463 __cxl_decoder_release(&cxlrd->cxlsd.cxld); 464 kfree(cxlrd); 465 } 466 467 static const struct device_type cxl_decoder_endpoint_type = { 468 .name = "cxl_decoder_endpoint", 469 .release = cxl_endpoint_decoder_release, 470 .groups = cxl_decoder_endpoint_attribute_groups, 471 }; 472 473 static const struct device_type cxl_decoder_switch_type = { 474 .name = "cxl_decoder_switch", 475 .release = cxl_switch_decoder_release, 476 .groups = cxl_decoder_switch_attribute_groups, 477 }; 478 479 static const struct device_type cxl_decoder_root_type = { 480 .name = "cxl_decoder_root", 481 .release = cxl_root_decoder_release, 482 .groups = cxl_decoder_root_attribute_groups, 483 }; 484 485 bool is_endpoint_decoder(struct device *dev) 486 { 487 return dev->type == &cxl_decoder_endpoint_type; 488 } 489 EXPORT_SYMBOL_NS_GPL(is_endpoint_decoder, "CXL"); 490 491 bool is_root_decoder(struct device *dev) 492 { 493 return dev->type == &cxl_decoder_root_type; 494 } 495 EXPORT_SYMBOL_NS_GPL(is_root_decoder, "CXL"); 496 497 bool is_switch_decoder(struct device *dev) 498 { 499 return is_root_decoder(dev) || dev->type == &cxl_decoder_switch_type; 500 } 501 EXPORT_SYMBOL_NS_GPL(is_switch_decoder, "CXL"); 502 503 struct cxl_decoder *to_cxl_decoder(struct device *dev) 504 { 505 if (dev_WARN_ONCE(dev, 506 !is_switch_decoder(dev) && !is_endpoint_decoder(dev), 507 "not a cxl_decoder device\n")) 508 return NULL; 509 return container_of(dev, struct cxl_decoder, dev); 510 } 511 EXPORT_SYMBOL_NS_GPL(to_cxl_decoder, "CXL"); 512 513 struct cxl_endpoint_decoder *to_cxl_endpoint_decoder(struct device *dev) 514 { 515 if (dev_WARN_ONCE(dev, !is_endpoint_decoder(dev), 516 "not a cxl_endpoint_decoder device\n")) 517 return NULL; 518 return container_of(dev, struct cxl_endpoint_decoder, cxld.dev); 519 } 520 EXPORT_SYMBOL_NS_GPL(to_cxl_endpoint_decoder, "CXL"); 521 522 struct cxl_switch_decoder *to_cxl_switch_decoder(struct device *dev) 523 { 524 if (dev_WARN_ONCE(dev, !is_switch_decoder(dev), 525 "not a cxl_switch_decoder device\n")) 526 return NULL; 527 return container_of(dev, struct cxl_switch_decoder, cxld.dev); 528 } 529 EXPORT_SYMBOL_NS_GPL(to_cxl_switch_decoder, "CXL"); 530 531 static void cxl_ep_release(struct cxl_ep *ep) 532 { 533 put_device(ep->ep); 534 kfree(ep); 535 } 536 537 static void cxl_ep_remove(struct cxl_port *port, struct cxl_ep *ep) 538 { 539 if (!ep) 540 return; 541 xa_erase(&port->endpoints, (unsigned long) ep->ep); 542 cxl_ep_release(ep); 543 } 544 545 static void cxl_port_release(struct device *dev) 546 { 547 struct cxl_port *port = to_cxl_port(dev); 548 unsigned long index; 549 struct cxl_ep *ep; 550 551 xa_for_each(&port->endpoints, index, ep) 552 cxl_ep_remove(port, ep); 553 xa_destroy(&port->endpoints); 554 xa_destroy(&port->dports); 555 xa_destroy(&port->regions); 556 ida_free(&cxl_port_ida, port->id); 557 558 if (is_cxl_root(port)) { 559 kfree(to_cxl_root(port)); 560 } else { 561 put_device(dev->parent); 562 kfree(port); 563 } 564 } 565 566 static ssize_t decoders_committed_show(struct device *dev, 567 struct device_attribute *attr, char *buf) 568 { 569 struct cxl_port *port = to_cxl_port(dev); 570 571 guard(rwsem_read)(&cxl_rwsem.region); 572 return sysfs_emit(buf, "%d\n", cxl_num_decoders_committed(port)); 573 } 574 575 static DEVICE_ATTR_RO(decoders_committed); 576 577 static struct attribute *cxl_port_attrs[] = { 578 &dev_attr_decoders_committed.attr, 579 NULL, 580 }; 581 582 static struct attribute_group cxl_port_attribute_group = { 583 .attrs = cxl_port_attrs, 584 }; 585 586 static const struct attribute_group *cxl_port_attribute_groups[] = { 587 &cxl_base_attribute_group, 588 &cxl_port_attribute_group, 589 NULL, 590 }; 591 592 static const struct device_type cxl_port_type = { 593 .name = "cxl_port", 594 .release = cxl_port_release, 595 .groups = cxl_port_attribute_groups, 596 }; 597 598 bool is_cxl_port(const struct device *dev) 599 { 600 return dev->type == &cxl_port_type; 601 } 602 EXPORT_SYMBOL_NS_GPL(is_cxl_port, "CXL"); 603 604 struct cxl_port *to_cxl_port(const struct device *dev) 605 { 606 if (dev_WARN_ONCE(dev, dev->type != &cxl_port_type, 607 "not a cxl_port device\n")) 608 return NULL; 609 return container_of(dev, struct cxl_port, dev); 610 } 611 EXPORT_SYMBOL_NS_GPL(to_cxl_port, "CXL"); 612 613 struct cxl_port *parent_port_of(struct cxl_port *port) 614 { 615 if (!port || !port->parent_dport) 616 return NULL; 617 return port->parent_dport->port; 618 } 619 620 static void unregister_port(void *_port) 621 { 622 struct cxl_port *port = _port; 623 624 device_lock_assert(port_to_host(port)); 625 port->dead = true; 626 device_unregister(&port->dev); 627 } 628 629 static void cxl_unlink_uport(void *_port) 630 { 631 struct cxl_port *port = _port; 632 633 sysfs_remove_link(&port->dev.kobj, "uport"); 634 } 635 636 static int devm_cxl_link_uport(struct device *host, struct cxl_port *port) 637 { 638 int rc; 639 640 rc = sysfs_create_link(&port->dev.kobj, &port->uport_dev->kobj, 641 "uport"); 642 if (rc) 643 return rc; 644 return devm_add_action_or_reset(host, cxl_unlink_uport, port); 645 } 646 647 static void cxl_unlink_parent_dport(void *_port) 648 { 649 struct cxl_port *port = _port; 650 651 sysfs_remove_link(&port->dev.kobj, "parent_dport"); 652 } 653 654 static int devm_cxl_link_parent_dport(struct device *host, 655 struct cxl_port *port, 656 struct cxl_dport *parent_dport) 657 { 658 int rc; 659 660 if (!parent_dport) 661 return 0; 662 663 rc = sysfs_create_link(&port->dev.kobj, &parent_dport->dport_dev->kobj, 664 "parent_dport"); 665 if (rc) 666 return rc; 667 return devm_add_action_or_reset(host, cxl_unlink_parent_dport, port); 668 } 669 670 static struct lock_class_key cxl_port_key; 671 672 static struct cxl_port *cxl_port_alloc(struct device *uport_dev, 673 struct cxl_dport *parent_dport) 674 { 675 struct cxl_root *cxl_root __free(kfree) = NULL; 676 struct cxl_port *port, *_port __free(kfree) = NULL; 677 struct device *dev; 678 int rc; 679 680 /* No parent_dport, root cxl_port */ 681 if (!parent_dport) { 682 cxl_root = kzalloc_obj(*cxl_root); 683 if (!cxl_root) 684 return ERR_PTR(-ENOMEM); 685 } else { 686 _port = kzalloc_obj(*port); 687 if (!_port) 688 return ERR_PTR(-ENOMEM); 689 } 690 691 rc = ida_alloc(&cxl_port_ida, GFP_KERNEL); 692 if (rc < 0) 693 return ERR_PTR(rc); 694 695 if (cxl_root) 696 port = &no_free_ptr(cxl_root)->port; 697 else 698 port = no_free_ptr(_port); 699 700 port->id = rc; 701 port->uport_dev = uport_dev; 702 703 /* 704 * The top-level cxl_port "cxl_root" does not have a cxl_port as 705 * its parent and it does not have any corresponding component 706 * registers as its decode is described by a fixed platform 707 * description. 708 */ 709 dev = &port->dev; 710 if (parent_dport) { 711 struct cxl_port *parent_port = parent_dport->port; 712 struct cxl_port *iter; 713 714 dev->parent = &parent_port->dev; 715 get_device(dev->parent); 716 port->depth = parent_port->depth + 1; 717 port->parent_dport = parent_dport; 718 719 /* 720 * walk to the host bridge, or the first ancestor that knows 721 * the host bridge 722 */ 723 iter = port; 724 while (!iter->host_bridge && 725 !is_cxl_root(to_cxl_port(iter->dev.parent))) 726 iter = to_cxl_port(iter->dev.parent); 727 if (iter->host_bridge) 728 port->host_bridge = iter->host_bridge; 729 else if (parent_dport->rch) 730 port->host_bridge = parent_dport->dport_dev; 731 else 732 port->host_bridge = iter->uport_dev; 733 dev_dbg(uport_dev, "host-bridge: %s\n", 734 dev_name(port->host_bridge)); 735 } else 736 dev->parent = uport_dev; 737 738 ida_init(&port->decoder_ida); 739 port->hdm_end = -1; 740 port->commit_end = -1; 741 xa_init(&port->dports); 742 xa_init(&port->endpoints); 743 xa_init(&port->regions); 744 port->component_reg_phys = CXL_RESOURCE_NONE; 745 746 device_initialize(dev); 747 lockdep_set_class_and_subclass(&dev->mutex, &cxl_port_key, port->depth); 748 device_set_pm_not_required(dev); 749 dev->bus = &cxl_bus_type; 750 dev->type = &cxl_port_type; 751 752 return port; 753 } 754 755 static int cxl_setup_comp_regs(struct device *host, struct cxl_register_map *map, 756 resource_size_t component_reg_phys) 757 { 758 *map = (struct cxl_register_map) { 759 .host = host, 760 .reg_type = CXL_REGLOC_RBI_EMPTY, 761 .resource = component_reg_phys, 762 }; 763 764 if (component_reg_phys == CXL_RESOURCE_NONE) 765 return 0; 766 767 map->reg_type = CXL_REGLOC_RBI_COMPONENT; 768 map->max_size = CXL_COMPONENT_REG_BLOCK_SIZE; 769 770 return cxl_setup_regs(map); 771 } 772 773 int cxl_port_setup_regs(struct cxl_port *port, 774 resource_size_t component_reg_phys) 775 { 776 if (dev_is_platform(port->uport_dev)) 777 return 0; 778 return cxl_setup_comp_regs(&port->dev, &port->reg_map, 779 component_reg_phys); 780 } 781 EXPORT_SYMBOL_NS_GPL(cxl_port_setup_regs, "CXL"); 782 783 static int cxl_dport_setup_regs(struct device *host, struct cxl_dport *dport, 784 resource_size_t component_reg_phys) 785 { 786 int rc; 787 788 if (dev_is_platform(dport->dport_dev)) 789 return 0; 790 791 /* 792 * use @dport->dport_dev for the context for error messages during 793 * register probing, and fixup @host after the fact, since @host may be 794 * NULL. 795 */ 796 rc = cxl_setup_comp_regs(dport->dport_dev, &dport->reg_map, 797 component_reg_phys); 798 dport->reg_map.host = host; 799 return rc; 800 } 801 802 DEFINE_SHOW_ATTRIBUTE(einj_cxl_available_error_type); 803 804 static int cxl_einj_inject(void *data, u64 type) 805 { 806 struct cxl_dport *dport = data; 807 808 if (dport->rch) 809 return einj_cxl_inject_rch_error(dport->rcrb.base, type); 810 811 return einj_cxl_inject_error(to_pci_dev(dport->dport_dev), type); 812 } 813 DEFINE_DEBUGFS_ATTRIBUTE(cxl_einj_inject_fops, NULL, cxl_einj_inject, 814 "0x%llx\n"); 815 816 static void cxl_debugfs_create_dport_dir(struct cxl_dport *dport) 817 { 818 struct cxl_port *parent = parent_port_of(dport->port); 819 struct dentry *dir; 820 821 if (!einj_cxl_is_initialized()) 822 return; 823 824 /* 825 * Protocol error injection is only available for CXL 2.0+ root ports 826 * and CXL 1.1 downstream ports 827 */ 828 if (!dport->rch && 829 !(dev_is_pci(dport->dport_dev) && parent && is_cxl_root(parent))) 830 return; 831 832 dir = cxl_debugfs_create_dir(dev_name(dport->dport_dev)); 833 834 debugfs_create_file("einj_inject", 0200, dir, dport, 835 &cxl_einj_inject_fops); 836 } 837 838 static int cxl_port_add(struct cxl_port *port, 839 resource_size_t component_reg_phys, 840 struct cxl_dport *parent_dport) 841 { 842 struct device *dev __free(put_device) = &port->dev; 843 int rc; 844 845 if (is_cxl_memdev(port->uport_dev)) { 846 struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev); 847 struct cxl_dev_state *cxlds = cxlmd->cxlds; 848 849 rc = dev_set_name(dev, "endpoint%d", port->id); 850 if (rc) 851 return rc; 852 853 /* 854 * The endpoint driver already enumerated the component and RAS 855 * registers. Reuse that enumeration while prepping them to be 856 * mapped by the cxl_port driver. 857 */ 858 port->reg_map = cxlds->reg_map; 859 port->reg_map.host = &port->dev; 860 cxlmd->endpoint = port; 861 } else if (parent_dport) { 862 rc = dev_set_name(dev, "port%d", port->id); 863 if (rc) 864 return rc; 865 866 port->component_reg_phys = component_reg_phys; 867 } else { 868 rc = dev_set_name(dev, "root%d", port->id); 869 if (rc) 870 return rc; 871 } 872 873 rc = device_add(dev); 874 if (rc) 875 return rc; 876 877 /* Inhibit the cleanup function invoked */ 878 dev = NULL; 879 return 0; 880 } 881 882 static struct cxl_port *__devm_cxl_add_port(struct device *host, 883 struct device *uport_dev, 884 resource_size_t component_reg_phys, 885 struct cxl_dport *parent_dport) 886 { 887 struct cxl_port *port; 888 int rc; 889 890 port = cxl_port_alloc(uport_dev, parent_dport); 891 if (IS_ERR(port)) 892 return port; 893 894 rc = cxl_port_add(port, component_reg_phys, parent_dport); 895 if (rc) 896 return ERR_PTR(rc); 897 898 rc = devm_add_action_or_reset(host, unregister_port, port); 899 if (rc) 900 return ERR_PTR(rc); 901 902 rc = devm_cxl_link_uport(host, port); 903 if (rc) 904 return ERR_PTR(rc); 905 906 rc = devm_cxl_link_parent_dport(host, port, parent_dport); 907 if (rc) 908 return ERR_PTR(rc); 909 910 if (parent_dport && dev_is_pci(uport_dev)) 911 port->pci_latency = cxl_pci_get_latency(to_pci_dev(uport_dev)); 912 913 return port; 914 } 915 916 /** 917 * devm_cxl_add_port - register a cxl_port in CXL memory decode hierarchy 918 * @host: host device for devm operations 919 * @uport_dev: "physical" device implementing this upstream port 920 * @component_reg_phys: (optional) for configurable cxl_port instances 921 * @parent_dport: next hop up in the CXL memory decode hierarchy 922 */ 923 struct cxl_port *devm_cxl_add_port(struct device *host, 924 struct device *uport_dev, 925 resource_size_t component_reg_phys, 926 struct cxl_dport *parent_dport) 927 { 928 struct cxl_port *port, *parent_port; 929 930 port = __devm_cxl_add_port(host, uport_dev, component_reg_phys, 931 parent_dport); 932 933 parent_port = parent_dport ? parent_dport->port : NULL; 934 if (IS_ERR(port)) { 935 dev_dbg(uport_dev, "Failed to add%s%s%s: %ld\n", 936 parent_port ? " port to " : "", 937 parent_port ? dev_name(&parent_port->dev) : "", 938 parent_port ? "" : " root port", 939 PTR_ERR(port)); 940 } else { 941 dev_dbg(uport_dev, "%s added%s%s%s\n", 942 dev_name(&port->dev), 943 parent_port ? " to " : "", 944 parent_port ? dev_name(&parent_port->dev) : "", 945 parent_port ? "" : " (root port)"); 946 } 947 948 return port; 949 } 950 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_port, "CXL"); 951 952 struct cxl_root *devm_cxl_add_root(struct device *host) 953 { 954 struct cxl_port *port; 955 956 port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL); 957 if (IS_ERR(port)) 958 return ERR_CAST(port); 959 960 return to_cxl_root(port); 961 } 962 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_root, "CXL"); 963 964 struct pci_bus *cxl_port_to_pci_bus(struct cxl_port *port) 965 { 966 /* There is no pci_bus associated with a CXL platform-root port */ 967 if (is_cxl_root(port)) 968 return NULL; 969 970 if (dev_is_pci(port->uport_dev)) { 971 struct pci_dev *pdev = to_pci_dev(port->uport_dev); 972 973 return pdev->subordinate; 974 } 975 976 return xa_load(&cxl_root_buses, (unsigned long)port->uport_dev); 977 } 978 EXPORT_SYMBOL_NS_GPL(cxl_port_to_pci_bus, "CXL"); 979 980 static void unregister_pci_bus(void *uport_dev) 981 { 982 xa_erase(&cxl_root_buses, (unsigned long)uport_dev); 983 } 984 985 int devm_cxl_register_pci_bus(struct device *host, struct device *uport_dev, 986 struct pci_bus *bus) 987 { 988 int rc; 989 990 if (dev_is_pci(uport_dev)) 991 return -EINVAL; 992 993 rc = xa_insert(&cxl_root_buses, (unsigned long)uport_dev, bus, 994 GFP_KERNEL); 995 if (rc) 996 return rc; 997 return devm_add_action_or_reset(host, unregister_pci_bus, uport_dev); 998 } 999 EXPORT_SYMBOL_NS_GPL(devm_cxl_register_pci_bus, "CXL"); 1000 1001 static bool dev_is_cxl_root_child(struct device *dev) 1002 { 1003 struct cxl_port *port, *parent; 1004 1005 if (!is_cxl_port(dev)) 1006 return false; 1007 1008 port = to_cxl_port(dev); 1009 if (is_cxl_root(port)) 1010 return false; 1011 1012 parent = to_cxl_port(port->dev.parent); 1013 if (is_cxl_root(parent)) 1014 return true; 1015 1016 return false; 1017 } 1018 1019 struct cxl_root *find_cxl_root(struct cxl_port *port) 1020 { 1021 struct cxl_port *iter = port; 1022 1023 while (iter && !is_cxl_root(iter)) 1024 iter = to_cxl_port(iter->dev.parent); 1025 1026 if (!iter) 1027 return NULL; 1028 get_device(&iter->dev); 1029 return to_cxl_root(iter); 1030 } 1031 EXPORT_SYMBOL_NS_GPL(find_cxl_root, "CXL"); 1032 1033 static struct cxl_dport *find_dport(struct cxl_port *port, int id) 1034 { 1035 struct cxl_dport *dport; 1036 unsigned long index; 1037 1038 device_lock_assert(&port->dev); 1039 xa_for_each(&port->dports, index, dport) 1040 if (dport->port_id == id) 1041 return dport; 1042 return NULL; 1043 } 1044 1045 static int add_dport(struct cxl_port *port, struct cxl_dport *dport) 1046 { 1047 struct cxl_dport *dup; 1048 int rc; 1049 1050 device_lock_assert(&port->dev); 1051 dup = find_dport(port, dport->port_id); 1052 if (dup) { 1053 dev_err(&port->dev, 1054 "unable to add dport%d-%s non-unique port id (%s)\n", 1055 dport->port_id, dev_name(dport->dport_dev), 1056 dev_name(dup->dport_dev)); 1057 return -EBUSY; 1058 } 1059 1060 /* Arrange for dport_dev to be valid through remove_dport() */ 1061 struct device *dev __free(put_device) = get_device(dport->dport_dev); 1062 1063 rc = xa_insert(&port->dports, (unsigned long)dport->dport_dev, dport, 1064 GFP_KERNEL); 1065 if (rc) 1066 return rc; 1067 1068 retain_and_null_ptr(dev); 1069 port->nr_dports++; 1070 return 0; 1071 } 1072 1073 /* 1074 * Since root-level CXL dports cannot be enumerated by PCI they are not 1075 * enumerated by the common port driver that acquires the port lock over 1076 * dport add/remove. Instead, root dports are manually added by a 1077 * platform driver and cond_cxl_root_lock() is used to take the missing 1078 * port lock in that case. 1079 */ 1080 static void cond_cxl_root_lock(struct cxl_port *port) 1081 { 1082 if (is_cxl_root(port)) 1083 device_lock(&port->dev); 1084 } 1085 1086 static void cond_cxl_root_unlock(struct cxl_port *port) 1087 { 1088 if (is_cxl_root(port)) 1089 device_unlock(&port->dev); 1090 } 1091 1092 static void cxl_dport_remove(void *data) 1093 { 1094 struct cxl_dport *dport = data; 1095 struct cxl_port *port = dport->port; 1096 1097 port->nr_dports--; 1098 xa_erase(&port->dports, (unsigned long) dport->dport_dev); 1099 put_device(dport->dport_dev); 1100 } 1101 1102 static void cxl_dport_unlink(void *data) 1103 { 1104 struct cxl_dport *dport = data; 1105 struct cxl_port *port = dport->port; 1106 char link_name[CXL_TARGET_STRLEN]; 1107 1108 sprintf(link_name, "dport%d", dport->port_id); 1109 sysfs_remove_link(&port->dev.kobj, link_name); 1110 } 1111 1112 static void free_dport(void *dport) 1113 { 1114 kfree(dport); 1115 } 1116 1117 /* 1118 * Upon return either a group is established with one action (free_dport()), or 1119 * no group established and @dport is freed. 1120 */ 1121 static void *cxl_dport_open_dr_group_or_free(struct cxl_dport *dport) 1122 { 1123 int rc; 1124 struct device *host = dport_to_host(dport); 1125 void *group = devres_open_group(host, dport, GFP_KERNEL); 1126 1127 if (!group) { 1128 kfree(dport); 1129 return NULL; 1130 } 1131 1132 rc = devm_add_action_or_reset(host, free_dport, dport); 1133 if (rc) { 1134 devres_release_group(host, group); 1135 return NULL; 1136 } 1137 1138 return group; 1139 } 1140 1141 static void cxl_dport_close_dr_group(struct cxl_dport *dport, void *group) 1142 { 1143 devres_close_group(dport_to_host(dport), group); 1144 } 1145 1146 static void del_dport(struct cxl_dport *dport) 1147 { 1148 devres_release_group(dport_to_host(dport), dport); 1149 } 1150 1151 /* The dport group id is the dport */ 1152 DEFINE_FREE(cxl_dport_release_dr_group, void *, if (_T) del_dport(_T)) 1153 1154 static struct cxl_dport * 1155 __devm_cxl_add_dport(struct cxl_port *port, struct device *dport_dev, 1156 int port_id, resource_size_t component_reg_phys, 1157 resource_size_t rcrb) 1158 { 1159 char link_name[CXL_TARGET_STRLEN]; 1160 struct cxl_dport *dport; 1161 struct device *host; 1162 int rc; 1163 1164 if (is_cxl_root(port)) 1165 host = port->uport_dev; 1166 else 1167 host = &port->dev; 1168 1169 if (!host->driver) { 1170 dev_WARN_ONCE(&port->dev, 1, "dport:%s bad devm context\n", 1171 dev_name(dport_dev)); 1172 return ERR_PTR(-ENXIO); 1173 } 1174 1175 if (snprintf(link_name, CXL_TARGET_STRLEN, "dport%d", port_id) >= 1176 CXL_TARGET_STRLEN) 1177 return ERR_PTR(-EINVAL); 1178 1179 dport = kzalloc_obj(*dport); 1180 if (!dport) 1181 return ERR_PTR(-ENOMEM); 1182 1183 /* Just enough init to manage the devres group */ 1184 dport->dport_dev = dport_dev; 1185 dport->port_id = port_id; 1186 dport->port = port; 1187 1188 void *dport_dr_group __free(cxl_dport_release_dr_group) = 1189 cxl_dport_open_dr_group_or_free(dport); 1190 if (!dport_dr_group) 1191 return ERR_PTR(-ENOMEM); 1192 1193 if (rcrb == CXL_RESOURCE_NONE) { 1194 rc = cxl_dport_setup_regs(&port->dev, dport, 1195 component_reg_phys); 1196 if (rc) 1197 return ERR_PTR(rc); 1198 } else { 1199 dport->rcrb.base = rcrb; 1200 component_reg_phys = __rcrb_to_component(dport_dev, &dport->rcrb, 1201 CXL_RCRB_DOWNSTREAM); 1202 if (component_reg_phys == CXL_RESOURCE_NONE) { 1203 dev_warn(dport_dev, "Invalid Component Registers in RCRB"); 1204 return ERR_PTR(-ENXIO); 1205 } 1206 1207 /* 1208 * RCH @dport is not ready to map until associated with its 1209 * memdev 1210 */ 1211 rc = cxl_dport_setup_regs(NULL, dport, component_reg_phys); 1212 if (rc) 1213 return ERR_PTR(rc); 1214 1215 dport->rch = true; 1216 } 1217 1218 if (component_reg_phys != CXL_RESOURCE_NONE) 1219 dev_dbg(dport_dev, "Component Registers found for dport: %pa\n", 1220 &component_reg_phys); 1221 1222 cond_cxl_root_lock(port); 1223 rc = add_dport(port, dport); 1224 cond_cxl_root_unlock(port); 1225 if (rc) 1226 return ERR_PTR(rc); 1227 1228 rc = devm_add_action_or_reset(host, cxl_dport_remove, dport); 1229 if (rc) 1230 return ERR_PTR(rc); 1231 1232 rc = sysfs_create_link(&port->dev.kobj, &dport_dev->kobj, link_name); 1233 if (rc) 1234 return ERR_PTR(rc); 1235 1236 rc = devm_add_action_or_reset(host, cxl_dport_unlink, dport); 1237 if (rc) 1238 return ERR_PTR(rc); 1239 1240 if (dev_is_pci(dport_dev)) 1241 dport->link_latency = cxl_pci_get_latency(to_pci_dev(dport_dev)); 1242 1243 cxl_debugfs_create_dport_dir(dport); 1244 1245 if (!dport->rch) 1246 devm_cxl_dport_ras_setup(dport); 1247 1248 /* keep the group, and mark the end of devm actions */ 1249 cxl_dport_close_dr_group(dport, no_free_ptr(dport_dr_group)); 1250 1251 return dport; 1252 } 1253 1254 /** 1255 * devm_cxl_add_dport - append VH downstream port data to a cxl_port 1256 * @port: the cxl_port that references this dport 1257 * @dport_dev: firmware or PCI device representing the dport 1258 * @port_id: identifier for this dport in a decoder's target list 1259 * @component_reg_phys: optional location of CXL component registers 1260 * 1261 * Note that dports are appended to the devm release action's of the 1262 * either the port's host (for root ports), or the port itself (for 1263 * switch ports) 1264 */ 1265 struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port, 1266 struct device *dport_dev, int port_id, 1267 resource_size_t component_reg_phys) 1268 { 1269 struct cxl_dport *dport; 1270 1271 dport = __devm_cxl_add_dport(port, dport_dev, port_id, 1272 component_reg_phys, CXL_RESOURCE_NONE); 1273 if (IS_ERR(dport)) { 1274 dev_dbg(dport_dev, "failed to add dport to %s: %ld\n", 1275 dev_name(&port->dev), PTR_ERR(dport)); 1276 } else { 1277 dev_dbg(dport_dev, "dport added to %s\n", 1278 dev_name(&port->dev)); 1279 } 1280 1281 return dport; 1282 } 1283 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_dport, "CXL"); 1284 1285 /** 1286 * devm_cxl_add_rch_dport - append RCH downstream port data to a cxl_port 1287 * @port: the cxl_port that references this dport 1288 * @dport_dev: firmware or PCI device representing the dport 1289 * @port_id: identifier for this dport in a decoder's target list 1290 * @rcrb: mandatory location of a Root Complex Register Block 1291 * 1292 * See CXL 3.0 9.11.8 CXL Devices Attached to an RCH 1293 */ 1294 struct cxl_dport *devm_cxl_add_rch_dport(struct cxl_port *port, 1295 struct device *dport_dev, int port_id, 1296 resource_size_t rcrb) 1297 { 1298 struct cxl_dport *dport; 1299 1300 if (rcrb == CXL_RESOURCE_NONE) { 1301 dev_dbg(&port->dev, "failed to add RCH dport, missing RCRB\n"); 1302 return ERR_PTR(-EINVAL); 1303 } 1304 1305 dport = __devm_cxl_add_dport(port, dport_dev, port_id, 1306 CXL_RESOURCE_NONE, rcrb); 1307 if (IS_ERR(dport)) { 1308 dev_dbg(dport_dev, "failed to add RCH dport to %s: %ld\n", 1309 dev_name(&port->dev), PTR_ERR(dport)); 1310 } else { 1311 dev_dbg(dport_dev, "RCH dport added to %s\n", 1312 dev_name(&port->dev)); 1313 } 1314 1315 return dport; 1316 } 1317 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_rch_dport, "CXL"); 1318 1319 static int add_ep(struct cxl_ep *new) 1320 { 1321 struct cxl_port *port = new->dport->port; 1322 1323 guard(device)(&port->dev); 1324 if (port->dead) 1325 return -ENXIO; 1326 1327 return xa_insert(&port->endpoints, (unsigned long)new->ep, 1328 new, GFP_KERNEL); 1329 } 1330 1331 /** 1332 * cxl_add_ep - register an endpoint's interest in a port 1333 * @dport: the dport that routes to @ep_dev 1334 * @ep_dev: device representing the endpoint 1335 * 1336 * Intermediate CXL ports are scanned based on the arrival of endpoints. 1337 * When those endpoints depart the port can be destroyed once all 1338 * endpoints that care about that port have been removed. 1339 */ 1340 static int cxl_add_ep(struct cxl_dport *dport, struct device *ep_dev) 1341 { 1342 struct cxl_ep *ep; 1343 int rc; 1344 1345 ep = kzalloc_obj(*ep); 1346 if (!ep) 1347 return -ENOMEM; 1348 1349 ep->ep = get_device(ep_dev); 1350 ep->dport = dport; 1351 1352 rc = add_ep(ep); 1353 if (rc) 1354 cxl_ep_release(ep); 1355 return rc; 1356 } 1357 1358 struct cxl_find_port_ctx { 1359 const struct device *dport_dev; 1360 const struct cxl_port *parent_port; 1361 struct cxl_dport **dport; 1362 }; 1363 1364 static int match_port_by_dport(struct device *dev, const void *data) 1365 { 1366 const struct cxl_find_port_ctx *ctx = data; 1367 struct cxl_dport *dport; 1368 struct cxl_port *port; 1369 1370 if (!is_cxl_port(dev)) 1371 return 0; 1372 if (ctx->parent_port && dev->parent != &ctx->parent_port->dev) 1373 return 0; 1374 1375 port = to_cxl_port(dev); 1376 dport = cxl_find_dport_by_dev(port, ctx->dport_dev); 1377 if (ctx->dport) 1378 *ctx->dport = dport; 1379 return dport != NULL; 1380 } 1381 1382 static struct cxl_port *__find_cxl_port(struct cxl_find_port_ctx *ctx) 1383 { 1384 struct device *dev; 1385 1386 if (!ctx->dport_dev) 1387 return NULL; 1388 1389 dev = bus_find_device(&cxl_bus_type, NULL, ctx, match_port_by_dport); 1390 if (dev) 1391 return to_cxl_port(dev); 1392 return NULL; 1393 } 1394 1395 static struct cxl_port *find_cxl_port(struct device *dport_dev, 1396 struct cxl_dport **dport) 1397 { 1398 struct cxl_find_port_ctx ctx = { 1399 .dport_dev = dport_dev, 1400 .dport = dport, 1401 }; 1402 struct cxl_port *port; 1403 1404 port = __find_cxl_port(&ctx); 1405 return port; 1406 } 1407 1408 /* 1409 * All users of grandparent() are using it to walk PCIe-like switch port 1410 * hierarchy. A PCIe switch is comprised of a bridge device representing the 1411 * upstream switch port and N bridges representing downstream switch ports. When 1412 * bridges stack the grand-parent of a downstream switch port is another 1413 * downstream switch port in the immediate ancestor switch. 1414 */ 1415 static struct device *grandparent(struct device *dev) 1416 { 1417 if (dev && dev->parent) 1418 return dev->parent->parent; 1419 return NULL; 1420 } 1421 1422 static void delete_endpoint(void *data) 1423 { 1424 struct cxl_memdev *cxlmd = data; 1425 struct cxl_port *endpoint = cxlmd->endpoint; 1426 struct device *host = port_to_host(endpoint); 1427 1428 scoped_guard(device, host) { 1429 if (host->driver && !endpoint->dead) { 1430 devm_release_action(host, cxl_unlink_parent_dport, endpoint); 1431 devm_release_action(host, cxl_unlink_uport, endpoint); 1432 devm_release_action(host, unregister_port, endpoint); 1433 } 1434 cxlmd->endpoint = NULL; 1435 } 1436 put_device(&endpoint->dev); 1437 put_device(host); 1438 } 1439 1440 int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint) 1441 { 1442 struct device *host = port_to_host(endpoint); 1443 struct device *dev = &cxlmd->dev; 1444 1445 get_device(host); 1446 get_device(&endpoint->dev); 1447 cxlmd->depth = endpoint->depth; 1448 return devm_add_action_or_reset(dev, delete_endpoint, cxlmd); 1449 } 1450 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_autoremove, "CXL"); 1451 1452 /* 1453 * The natural end of life of a non-root 'cxl_port' is when its parent port goes 1454 * through a ->remove() event ("top-down" unregistration). The unnatural trigger 1455 * for a port to be unregistered is when all memdevs beneath that port have gone 1456 * through ->remove(). This "bottom-up" removal selectively removes individual 1457 * child ports manually. This depends on devm_cxl_add_port() to not change is 1458 * devm action registration order, and for dports to have already been 1459 * destroyed by del_dports(). 1460 */ 1461 static void delete_switch_port(struct cxl_port *port) 1462 { 1463 devm_release_action(port->dev.parent, cxl_unlink_parent_dport, port); 1464 devm_release_action(port->dev.parent, cxl_unlink_uport, port); 1465 devm_release_action(port->dev.parent, unregister_port, port); 1466 } 1467 1468 static void del_dports(struct cxl_port *port) 1469 { 1470 struct cxl_dport *dport; 1471 unsigned long index; 1472 1473 device_lock_assert(&port->dev); 1474 1475 xa_for_each(&port->dports, index, dport) 1476 del_dport(dport); 1477 } 1478 1479 struct detach_ctx { 1480 struct cxl_memdev *cxlmd; 1481 int depth; 1482 }; 1483 1484 static int port_has_memdev(struct device *dev, const void *data) 1485 { 1486 const struct detach_ctx *ctx = data; 1487 struct cxl_port *port; 1488 1489 if (!is_cxl_port(dev)) 1490 return 0; 1491 1492 port = to_cxl_port(dev); 1493 if (port->depth != ctx->depth) 1494 return 0; 1495 1496 return !!cxl_ep_load(port, ctx->cxlmd); 1497 } 1498 1499 static void cxl_detach_ep(void *data) 1500 { 1501 struct cxl_memdev *cxlmd = data; 1502 1503 for (int i = cxlmd->depth - 1; i >= 1; i--) { 1504 struct cxl_port *port, *parent_port; 1505 struct detach_ctx ctx = { 1506 .cxlmd = cxlmd, 1507 .depth = i, 1508 }; 1509 struct cxl_ep *ep; 1510 bool died = false; 1511 1512 struct device *dev __free(put_device) = 1513 bus_find_device(&cxl_bus_type, NULL, &ctx, port_has_memdev); 1514 if (!dev) 1515 continue; 1516 port = to_cxl_port(dev); 1517 1518 parent_port = to_cxl_port(port->dev.parent); 1519 device_lock(&parent_port->dev); 1520 device_lock(&port->dev); 1521 ep = cxl_ep_load(port, cxlmd); 1522 dev_dbg(&cxlmd->dev, "disconnect %s from %s\n", 1523 ep ? dev_name(ep->ep) : "", dev_name(&port->dev)); 1524 cxl_ep_remove(port, ep); 1525 if (ep && !port->dead && xa_empty(&port->endpoints) && 1526 !is_cxl_root(parent_port) && parent_port->dev.driver) { 1527 /* 1528 * This was the last ep attached to a dynamically 1529 * enumerated port. Block new cxl_add_ep() and garbage 1530 * collect the port. 1531 */ 1532 died = true; 1533 port->dead = true; 1534 del_dports(port); 1535 } 1536 device_unlock(&port->dev); 1537 1538 if (died) { 1539 dev_dbg(&cxlmd->dev, "delete %s\n", 1540 dev_name(&port->dev)); 1541 delete_switch_port(port); 1542 } 1543 device_unlock(&parent_port->dev); 1544 } 1545 } 1546 1547 static resource_size_t find_component_registers(struct device *dev) 1548 { 1549 struct cxl_register_map map; 1550 struct pci_dev *pdev; 1551 1552 /* 1553 * Theoretically, CXL component registers can be hosted on a 1554 * non-PCI device, in practice, only cxl_test hits this case. 1555 */ 1556 if (!dev_is_pci(dev)) 1557 return CXL_RESOURCE_NONE; 1558 1559 pdev = to_pci_dev(dev); 1560 1561 cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map); 1562 return map.resource; 1563 } 1564 1565 static int match_port_by_uport(struct device *dev, const void *data) 1566 { 1567 const struct device *uport_dev = data; 1568 struct cxl_port *port; 1569 1570 if (!is_cxl_port(dev)) 1571 return 0; 1572 1573 port = to_cxl_port(dev); 1574 /* Endpoint ports are hosted by memdevs */ 1575 if (is_cxl_memdev(port->uport_dev)) 1576 return uport_dev == port->uport_dev->parent; 1577 return uport_dev == port->uport_dev; 1578 } 1579 1580 /** 1581 * find_cxl_port_by_uport - Find a CXL port device companion 1582 * @uport_dev: Device that acts as a switch or endpoint in the CXL hierarchy 1583 * 1584 * In the case of endpoint ports recall that port->uport_dev points to a 'struct 1585 * cxl_memdev' device. So, the @uport_dev argument is the parent device of the 1586 * 'struct cxl_memdev' in that case. 1587 * 1588 * Function takes a device reference on the port device. Caller should do a 1589 * put_device() when done. 1590 */ 1591 static struct cxl_port *find_cxl_port_by_uport(struct device *uport_dev) 1592 { 1593 struct device *dev; 1594 1595 dev = bus_find_device(&cxl_bus_type, NULL, uport_dev, match_port_by_uport); 1596 if (dev) 1597 return to_cxl_port(dev); 1598 return NULL; 1599 } 1600 1601 static int update_decoder_targets(struct device *dev, void *data) 1602 { 1603 struct cxl_dport *dport = data; 1604 struct cxl_switch_decoder *cxlsd; 1605 struct cxl_decoder *cxld; 1606 int i; 1607 1608 if (!is_switch_decoder(dev)) 1609 return 0; 1610 1611 cxlsd = to_cxl_switch_decoder(dev); 1612 cxld = &cxlsd->cxld; 1613 guard(rwsem_write)(&cxl_rwsem.region); 1614 1615 for (i = 0; i < cxld->interleave_ways; i++) { 1616 if (cxld->target_map[i] == dport->port_id) { 1617 cxlsd->target[i] = dport; 1618 dev_dbg(dev, "dport%d found in target list, index %d\n", 1619 dport->port_id, i); 1620 return 0; 1621 } 1622 } 1623 1624 return 0; 1625 } 1626 1627 void cxl_port_update_decoder_targets(struct cxl_port *port, 1628 struct cxl_dport *dport) 1629 { 1630 device_for_each_child(&port->dev, dport, update_decoder_targets); 1631 } 1632 EXPORT_SYMBOL_NS_GPL(cxl_port_update_decoder_targets, "CXL"); 1633 1634 static bool dport_exists(struct cxl_port *port, struct device *dport_dev) 1635 { 1636 struct cxl_dport *dport = cxl_find_dport_by_dev(port, dport_dev); 1637 1638 if (dport) { 1639 dev_dbg(&port->dev, "dport%d:%s already exists\n", 1640 dport->port_id, dev_name(dport_dev)); 1641 return true; 1642 } 1643 1644 return false; 1645 } 1646 1647 static struct cxl_dport *probe_dport(struct cxl_port *port, 1648 struct device *dport_dev) 1649 { 1650 struct cxl_driver *drv; 1651 1652 device_lock_assert(&port->dev); 1653 if (!port->dev.driver) 1654 return ERR_PTR(-ENXIO); 1655 1656 if (dport_exists(port, dport_dev)) 1657 return ERR_PTR(-EBUSY); 1658 1659 drv = container_of(port->dev.driver, struct cxl_driver, drv); 1660 if (!drv->add_dport) 1661 return ERR_PTR(-ENXIO); 1662 1663 /* see cxl_port_add_dport() */ 1664 return drv->add_dport(port, dport_dev); 1665 } 1666 1667 static struct cxl_dport *devm_cxl_create_port(struct device *ep_dev, 1668 struct cxl_port *parent_port, 1669 struct cxl_dport *parent_dport, 1670 struct device *uport_dev, 1671 struct device *dport_dev) 1672 { 1673 resource_size_t component_reg_phys; 1674 1675 device_lock_assert(&parent_port->dev); 1676 if (!parent_port->dev.driver) { 1677 dev_warn(ep_dev, 1678 "port %s:%s:%s disabled, failed to enumerate CXL.mem\n", 1679 dev_name(&parent_port->dev), dev_name(uport_dev), 1680 dev_name(dport_dev)); 1681 } 1682 1683 struct cxl_port *port __free(put_cxl_port) = 1684 find_cxl_port_by_uport(uport_dev); 1685 if (!port) { 1686 component_reg_phys = find_component_registers(uport_dev); 1687 port = devm_cxl_add_port(&parent_port->dev, uport_dev, 1688 component_reg_phys, parent_dport); 1689 if (IS_ERR(port)) 1690 return ERR_CAST(port); 1691 1692 /* 1693 * retry to make sure a port is found. a port device 1694 * reference is taken. 1695 */ 1696 port = find_cxl_port_by_uport(uport_dev); 1697 if (!port) 1698 return ERR_PTR(-ENODEV); 1699 1700 dev_dbg(ep_dev, "created port %s:%s\n", 1701 dev_name(&port->dev), dev_name(port->uport_dev)); 1702 } else { 1703 /* 1704 * Port was created before right before this function is 1705 * called. Signal the caller to deal with it. 1706 */ 1707 return ERR_PTR(-EAGAIN); 1708 } 1709 1710 guard(device)(&port->dev); 1711 return probe_dport(port, dport_dev); 1712 } 1713 1714 static int add_port_attach_ep(struct cxl_memdev *cxlmd, 1715 struct device *uport_dev, 1716 struct device *dport_dev) 1717 { 1718 struct device *dparent = grandparent(dport_dev); 1719 struct cxl_dport *dport, *parent_dport; 1720 int rc; 1721 1722 if (is_cxl_host_bridge(dparent)) { 1723 /* 1724 * The iteration reached the topology root without finding the 1725 * CXL-root 'cxl_port' on a previous iteration, fail for now to 1726 * be re-probed after platform driver attaches. 1727 */ 1728 dev_dbg(&cxlmd->dev, "%s is a root dport\n", 1729 dev_name(dport_dev)); 1730 return -ENXIO; 1731 } 1732 1733 struct cxl_port *parent_port __free(put_cxl_port) = 1734 find_cxl_port_by_uport(dparent->parent); 1735 if (!parent_port) { 1736 /* iterate to create this parent_port */ 1737 return -EAGAIN; 1738 } 1739 1740 scoped_guard(device, &parent_port->dev) { 1741 parent_dport = cxl_find_dport_by_dev(parent_port, dparent); 1742 if (!parent_dport) { 1743 parent_dport = probe_dport(parent_port, dparent); 1744 if (IS_ERR(parent_dport)) 1745 return PTR_ERR(parent_dport); 1746 } 1747 1748 dport = devm_cxl_create_port(&cxlmd->dev, parent_port, 1749 parent_dport, uport_dev, 1750 dport_dev); 1751 if (IS_ERR(dport)) { 1752 /* Port already exists, restart iteration */ 1753 if (PTR_ERR(dport) == -EAGAIN) 1754 return 0; 1755 return PTR_ERR(dport); 1756 } 1757 } 1758 1759 rc = cxl_add_ep(dport, &cxlmd->dev); 1760 if (rc == -EBUSY) { 1761 /* 1762 * "can't" happen, but this error code means 1763 * something to the caller, so translate it. 1764 */ 1765 rc = -ENXIO; 1766 } 1767 1768 return rc; 1769 } 1770 1771 static struct cxl_dport *find_or_add_dport(struct cxl_port *port, 1772 struct device *dport_dev) 1773 { 1774 struct cxl_dport *dport; 1775 1776 /* 1777 * The port is already visible in CXL hierarchy, but it may still 1778 * be in the process of binding to the CXL port driver at this point. 1779 * 1780 * port creation and driver binding are protected by the port's host 1781 * lock, so acquire the host lock here to ensure the port has completed 1782 * driver binding before proceeding with dport addition. 1783 */ 1784 guard(device)(port_to_host(port)); 1785 guard(device)(&port->dev); 1786 dport = cxl_find_dport_by_dev(port, dport_dev); 1787 if (!dport) { 1788 dport = probe_dport(port, dport_dev); 1789 if (IS_ERR(dport)) 1790 return dport; 1791 1792 /* New dport added, restart iteration */ 1793 return ERR_PTR(-EAGAIN); 1794 } 1795 1796 return dport; 1797 } 1798 1799 int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd) 1800 { 1801 struct device *dev = &cxlmd->dev; 1802 struct device *iter; 1803 int rc; 1804 1805 /* 1806 * Skip intermediate port enumeration in the RCH case, there 1807 * are no ports in between a host bridge and an endpoint. 1808 */ 1809 if (cxlmd->cxlds->rcd) 1810 return 0; 1811 1812 rc = devm_add_action_or_reset(&cxlmd->dev, cxl_detach_ep, cxlmd); 1813 if (rc) 1814 return rc; 1815 1816 /* 1817 * Scan for and add all cxl_ports in this device's ancestry. 1818 * Repeat until no more ports are added. Abort if a port add 1819 * attempt fails. 1820 */ 1821 retry: 1822 for (iter = dev; iter; iter = grandparent(iter)) { 1823 struct device *dport_dev = grandparent(iter); 1824 struct device *uport_dev; 1825 struct cxl_dport *dport; 1826 1827 if (is_cxl_host_bridge(dport_dev)) 1828 return 0; 1829 1830 uport_dev = dport_dev->parent; 1831 if (!uport_dev) { 1832 dev_warn(dev, "at %s no parent for dport: %s\n", 1833 dev_name(iter), dev_name(dport_dev)); 1834 return -ENXIO; 1835 } 1836 1837 dev_dbg(dev, "scan: iter: %s dport_dev: %s parent: %s\n", 1838 dev_name(iter), dev_name(dport_dev), 1839 dev_name(uport_dev)); 1840 struct cxl_port *port __free(put_cxl_port) = 1841 find_cxl_port_by_uport(uport_dev); 1842 if (port) { 1843 dev_dbg(&cxlmd->dev, 1844 "found already registered port %s:%s\n", 1845 dev_name(&port->dev), 1846 dev_name(port->uport_dev)); 1847 1848 /* 1849 * RP port enumerated by cxl_acpi without dport will 1850 * have the dport added here. 1851 */ 1852 dport = find_or_add_dport(port, dport_dev); 1853 if (IS_ERR(dport)) { 1854 if (PTR_ERR(dport) == -EAGAIN) 1855 goto retry; 1856 return PTR_ERR(dport); 1857 } 1858 1859 rc = cxl_add_ep(dport, &cxlmd->dev); 1860 1861 /* 1862 * If the endpoint already exists in the port's list, 1863 * that's ok, it was added on a previous pass. 1864 * Otherwise, retry in add_port_attach_ep() after taking 1865 * the parent_port lock as the current port may be being 1866 * reaped. 1867 */ 1868 if (rc && rc != -EBUSY) 1869 return rc; 1870 1871 cxl_gpf_port_setup(dport); 1872 1873 /* Any more ports to add between this one and the root? */ 1874 if (!dev_is_cxl_root_child(&port->dev)) 1875 continue; 1876 1877 return 0; 1878 } 1879 1880 rc = add_port_attach_ep(cxlmd, uport_dev, dport_dev); 1881 /* port missing, try to add parent */ 1882 if (rc == -EAGAIN) 1883 continue; 1884 /* failed to add ep or port */ 1885 if (rc) 1886 return rc; 1887 /* port added, new descendants possible, start over */ 1888 goto retry; 1889 } 1890 1891 return 0; 1892 } 1893 EXPORT_SYMBOL_NS_GPL(devm_cxl_enumerate_ports, "CXL"); 1894 1895 struct cxl_port *cxl_pci_find_port(struct pci_dev *pdev, 1896 struct cxl_dport **dport) 1897 { 1898 return find_cxl_port(pdev->dev.parent, dport); 1899 } 1900 EXPORT_SYMBOL_NS_GPL(cxl_pci_find_port, "CXL"); 1901 1902 struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd, 1903 struct cxl_dport **dport) 1904 { 1905 return find_cxl_port(grandparent(&cxlmd->dev), dport); 1906 } 1907 EXPORT_SYMBOL_NS_GPL(cxl_mem_find_port, "CXL"); 1908 1909 static int decoder_populate_targets(struct cxl_switch_decoder *cxlsd, 1910 struct cxl_port *port) 1911 { 1912 struct cxl_decoder *cxld = &cxlsd->cxld; 1913 int i; 1914 1915 device_lock_assert(&port->dev); 1916 1917 if (xa_empty(&port->dports)) 1918 return 0; 1919 1920 guard(rwsem_write)(&cxl_rwsem.region); 1921 for (i = 0; i < cxlsd->cxld.interleave_ways; i++) { 1922 struct cxl_dport *dport = find_dport(port, cxld->target_map[i]); 1923 1924 if (!dport) { 1925 /* dport may be activated later */ 1926 continue; 1927 } 1928 cxlsd->target[i] = dport; 1929 } 1930 1931 return 0; 1932 } 1933 1934 static struct lock_class_key cxl_decoder_key; 1935 1936 /** 1937 * cxl_decoder_init - Common decoder setup / initialization 1938 * @port: owning port of this decoder 1939 * @cxld: common decoder properties to initialize 1940 * 1941 * A port may contain one or more decoders. Each of those decoders 1942 * enable some address space for CXL.mem utilization. A decoder is 1943 * expected to be configured by the caller before registering via 1944 * cxl_decoder_add() 1945 */ 1946 static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld) 1947 { 1948 struct device *dev; 1949 int rc; 1950 1951 rc = ida_alloc(&port->decoder_ida, GFP_KERNEL); 1952 if (rc < 0) 1953 return rc; 1954 1955 /* need parent to stick around to release the id */ 1956 get_device(&port->dev); 1957 cxld->id = rc; 1958 1959 dev = &cxld->dev; 1960 device_initialize(dev); 1961 lockdep_set_class(&dev->mutex, &cxl_decoder_key); 1962 device_set_pm_not_required(dev); 1963 dev->parent = &port->dev; 1964 dev->bus = &cxl_bus_type; 1965 1966 /* Pre initialize an "empty" decoder */ 1967 cxld->interleave_ways = 1; 1968 cxld->interleave_granularity = PAGE_SIZE; 1969 cxld->target_type = CXL_DECODER_HOSTONLYMEM; 1970 cxld->hpa_range = (struct range) { 1971 .start = 0, 1972 .end = -1, 1973 }; 1974 1975 return 0; 1976 } 1977 1978 static int cxl_switch_decoder_init(struct cxl_port *port, 1979 struct cxl_switch_decoder *cxlsd, 1980 int nr_targets) 1981 { 1982 if (nr_targets > CXL_DECODER_MAX_INTERLEAVE) 1983 return -EINVAL; 1984 1985 cxlsd->nr_targets = nr_targets; 1986 return cxl_decoder_init(port, &cxlsd->cxld); 1987 } 1988 1989 /** 1990 * cxl_root_decoder_alloc - Allocate a root level decoder 1991 * @port: owning CXL root of this decoder 1992 * @nr_targets: static number of downstream targets 1993 * 1994 * Return: A new cxl decoder to be registered by cxl_decoder_add(). A 1995 * 'CXL root' decoder is one that decodes from a top-level / static platform 1996 * firmware description of CXL resources into a CXL standard decode 1997 * topology. 1998 */ 1999 struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port, 2000 unsigned int nr_targets) 2001 { 2002 struct cxl_root_decoder *cxlrd; 2003 struct cxl_switch_decoder *cxlsd; 2004 struct cxl_decoder *cxld; 2005 int rc; 2006 2007 if (!is_cxl_root(port)) 2008 return ERR_PTR(-EINVAL); 2009 2010 cxlrd = kzalloc_flex(*cxlrd, cxlsd.target, nr_targets); 2011 if (!cxlrd) 2012 return ERR_PTR(-ENOMEM); 2013 2014 cxlsd = &cxlrd->cxlsd; 2015 rc = cxl_switch_decoder_init(port, cxlsd, nr_targets); 2016 if (rc) { 2017 kfree(cxlrd); 2018 return ERR_PTR(rc); 2019 } 2020 2021 mutex_init(&cxlrd->regions_lock); 2022 xa_init(&cxlrd->regions); 2023 2024 cxld = &cxlsd->cxld; 2025 cxld->dev.type = &cxl_decoder_root_type; 2026 /* 2027 * cxl_root_decoder_release() special cases negative ids to 2028 * detect memregion_alloc() failures. 2029 */ 2030 atomic_set(&cxlrd->region_id, -1); 2031 rc = memregion_alloc(GFP_KERNEL); 2032 if (rc < 0) { 2033 put_device(&cxld->dev); 2034 return ERR_PTR(rc); 2035 } 2036 2037 atomic_set(&cxlrd->region_id, rc); 2038 cxlrd->qos_class = CXL_QOS_CLASS_INVALID; 2039 return cxlrd; 2040 } 2041 EXPORT_SYMBOL_NS_GPL(cxl_root_decoder_alloc, "CXL"); 2042 2043 /** 2044 * cxl_switch_decoder_alloc - Allocate a switch level decoder 2045 * @port: owning CXL switch port of this decoder 2046 * @nr_targets: max number of dynamically addressable downstream targets 2047 * 2048 * Return: A new cxl decoder to be registered by cxl_decoder_add(). A 2049 * 'switch' decoder is any decoder that can be enumerated by PCIe 2050 * topology and the HDM Decoder Capability. This includes the decoders 2051 * that sit between Switch Upstream Ports / Switch Downstream Ports and 2052 * Host Bridges / Root Ports. 2053 */ 2054 struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port, 2055 unsigned int nr_targets) 2056 { 2057 struct cxl_switch_decoder *cxlsd; 2058 struct cxl_decoder *cxld; 2059 int rc; 2060 2061 if (is_cxl_root(port) || is_cxl_endpoint(port)) 2062 return ERR_PTR(-EINVAL); 2063 2064 cxlsd = kzalloc_flex(*cxlsd, target, nr_targets); 2065 if (!cxlsd) 2066 return ERR_PTR(-ENOMEM); 2067 2068 rc = cxl_switch_decoder_init(port, cxlsd, nr_targets); 2069 if (rc) { 2070 kfree(cxlsd); 2071 return ERR_PTR(rc); 2072 } 2073 2074 cxld = &cxlsd->cxld; 2075 cxld->dev.type = &cxl_decoder_switch_type; 2076 return cxlsd; 2077 } 2078 EXPORT_SYMBOL_NS_GPL(cxl_switch_decoder_alloc, "CXL"); 2079 2080 /** 2081 * cxl_endpoint_decoder_alloc - Allocate an endpoint decoder 2082 * @port: owning port of this decoder 2083 * 2084 * Return: A new cxl decoder to be registered by cxl_decoder_add() 2085 */ 2086 struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port) 2087 { 2088 struct cxl_endpoint_decoder *cxled; 2089 struct cxl_decoder *cxld; 2090 int rc; 2091 2092 if (!is_cxl_endpoint(port)) 2093 return ERR_PTR(-EINVAL); 2094 2095 cxled = kzalloc_obj(*cxled); 2096 if (!cxled) 2097 return ERR_PTR(-ENOMEM); 2098 2099 cxled->pos = -1; 2100 cxled->part = -1; 2101 cxld = &cxled->cxld; 2102 rc = cxl_decoder_init(port, cxld); 2103 if (rc) { 2104 kfree(cxled); 2105 return ERR_PTR(rc); 2106 } 2107 2108 cxld->dev.type = &cxl_decoder_endpoint_type; 2109 return cxled; 2110 } 2111 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_decoder_alloc, "CXL"); 2112 2113 /** 2114 * cxl_decoder_add_locked - Add a decoder with targets 2115 * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc() 2116 * 2117 * Certain types of decoders may not have any targets. The main example of this 2118 * is an endpoint device. A more awkward example is a hostbridge whose root 2119 * ports get hot added (technically possible, though unlikely). 2120 * 2121 * This is the locked variant of cxl_decoder_add(). 2122 * 2123 * Context: Process context. Expects the device lock of the port that owns the 2124 * @cxld to be held. 2125 * 2126 * Return: Negative error code if the decoder wasn't properly configured; else 2127 * returns 0. 2128 */ 2129 int cxl_decoder_add_locked(struct cxl_decoder *cxld) 2130 { 2131 struct cxl_port *port; 2132 struct device *dev; 2133 int rc; 2134 2135 if (WARN_ON_ONCE(!cxld)) 2136 return -EINVAL; 2137 2138 if (WARN_ON_ONCE(IS_ERR(cxld))) 2139 return PTR_ERR(cxld); 2140 2141 if (cxld->interleave_ways < 1) 2142 return -EINVAL; 2143 2144 dev = &cxld->dev; 2145 2146 port = to_cxl_port(cxld->dev.parent); 2147 if (!is_endpoint_decoder(dev)) { 2148 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev); 2149 2150 rc = decoder_populate_targets(cxlsd, port); 2151 if (rc && (cxld->flags & CXL_DECODER_F_ENABLE)) { 2152 dev_err(&port->dev, 2153 "Failed to populate active decoder targets\n"); 2154 return rc; 2155 } 2156 } 2157 2158 rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id); 2159 if (rc) 2160 return rc; 2161 2162 return device_add(dev); 2163 } 2164 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add_locked, "CXL"); 2165 2166 /** 2167 * cxl_decoder_add - Add a decoder with targets 2168 * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc() 2169 * 2170 * This is the unlocked variant of cxl_decoder_add_locked(). 2171 * See cxl_decoder_add_locked(). 2172 * 2173 * Context: Process context. Takes and releases the device lock of the port that 2174 * owns the @cxld. 2175 */ 2176 int cxl_decoder_add(struct cxl_decoder *cxld) 2177 { 2178 struct cxl_port *port; 2179 2180 if (WARN_ON_ONCE(!cxld)) 2181 return -EINVAL; 2182 2183 if (WARN_ON_ONCE(IS_ERR(cxld))) 2184 return PTR_ERR(cxld); 2185 2186 port = to_cxl_port(cxld->dev.parent); 2187 2188 guard(device)(&port->dev); 2189 return cxl_decoder_add_locked(cxld); 2190 } 2191 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, "CXL"); 2192 2193 static void cxld_unregister(void *dev) 2194 { 2195 if (is_endpoint_decoder(dev)) 2196 cxl_decoder_detach(NULL, to_cxl_endpoint_decoder(dev), -1, 2197 DETACH_INVALIDATE); 2198 if (is_root_decoder(dev)) 2199 kill_regions(to_cxl_root_decoder(dev)); 2200 2201 device_unregister(dev); 2202 } 2203 2204 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld) 2205 { 2206 return devm_add_action_or_reset(host, cxld_unregister, &cxld->dev); 2207 } 2208 EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, "CXL"); 2209 2210 /** 2211 * __cxl_driver_register - register a driver for the cxl bus 2212 * @cxl_drv: cxl driver structure to attach 2213 * @owner: owning module/driver 2214 * @modname: KBUILD_MODNAME for parent driver 2215 */ 2216 int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner, 2217 const char *modname) 2218 { 2219 if (!cxl_drv->probe) { 2220 pr_debug("%s ->probe() must be specified\n", modname); 2221 return -EINVAL; 2222 } 2223 2224 if (!cxl_drv->name) { 2225 pr_debug("%s ->name must be specified\n", modname); 2226 return -EINVAL; 2227 } 2228 2229 if (!cxl_drv->id) { 2230 pr_debug("%s ->id must be specified\n", modname); 2231 return -EINVAL; 2232 } 2233 2234 cxl_drv->drv.bus = &cxl_bus_type; 2235 cxl_drv->drv.owner = owner; 2236 cxl_drv->drv.mod_name = modname; 2237 cxl_drv->drv.name = cxl_drv->name; 2238 2239 return driver_register(&cxl_drv->drv); 2240 } 2241 EXPORT_SYMBOL_NS_GPL(__cxl_driver_register, "CXL"); 2242 2243 void cxl_driver_unregister(struct cxl_driver *cxl_drv) 2244 { 2245 driver_unregister(&cxl_drv->drv); 2246 } 2247 EXPORT_SYMBOL_NS_GPL(cxl_driver_unregister, "CXL"); 2248 2249 static int cxl_bus_uevent(const struct device *dev, struct kobj_uevent_env *env) 2250 { 2251 return add_uevent_var(env, "MODALIAS=" CXL_MODALIAS_FMT, 2252 cxl_device_id(dev)); 2253 } 2254 2255 static int cxl_bus_match(struct device *dev, const struct device_driver *drv) 2256 { 2257 return cxl_device_id(dev) == to_cxl_drv(drv)->id; 2258 } 2259 2260 static int cxl_bus_probe(struct device *dev) 2261 { 2262 int rc; 2263 2264 rc = to_cxl_drv(dev->driver)->probe(dev); 2265 dev_dbg(dev, "probe: %d\n", rc); 2266 return rc; 2267 } 2268 2269 static void cxl_bus_remove(struct device *dev) 2270 { 2271 struct cxl_driver *cxl_drv = to_cxl_drv(dev->driver); 2272 2273 if (cxl_drv->remove) 2274 cxl_drv->remove(dev); 2275 } 2276 2277 static struct workqueue_struct *cxl_bus_wq; 2278 2279 static int cxl_rescan_attach(struct device *dev, void *data) 2280 { 2281 int rc = device_attach(dev); 2282 2283 dev_vdbg(dev, "rescan: %s\n", rc ? "attach" : "detached"); 2284 2285 return 0; 2286 } 2287 2288 static void cxl_bus_rescan_queue(struct work_struct *w) 2289 { 2290 bus_for_each_dev(&cxl_bus_type, NULL, NULL, cxl_rescan_attach); 2291 } 2292 2293 void cxl_bus_rescan(void) 2294 { 2295 static DECLARE_WORK(rescan_work, cxl_bus_rescan_queue); 2296 2297 queue_work(cxl_bus_wq, &rescan_work); 2298 } 2299 EXPORT_SYMBOL_NS_GPL(cxl_bus_rescan, "CXL"); 2300 2301 void cxl_bus_drain(void) 2302 { 2303 drain_workqueue(cxl_bus_wq); 2304 } 2305 EXPORT_SYMBOL_NS_GPL(cxl_bus_drain, "CXL"); 2306 2307 bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd) 2308 { 2309 return queue_work(cxl_bus_wq, &cxlmd->detach_work); 2310 } 2311 EXPORT_SYMBOL_NS_GPL(schedule_cxl_memdev_detach, "CXL"); 2312 2313 static void add_latency(struct access_coordinate *c, long latency) 2314 { 2315 for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) { 2316 c[i].write_latency += latency; 2317 c[i].read_latency += latency; 2318 } 2319 } 2320 2321 static bool coordinates_valid(struct access_coordinate *c) 2322 { 2323 for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) { 2324 if (c[i].read_bandwidth && c[i].write_bandwidth && 2325 c[i].read_latency && c[i].write_latency) 2326 continue; 2327 return false; 2328 } 2329 2330 return true; 2331 } 2332 2333 static void set_min_bandwidth(struct access_coordinate *c, unsigned int bw) 2334 { 2335 for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) { 2336 c[i].write_bandwidth = min(c[i].write_bandwidth, bw); 2337 c[i].read_bandwidth = min(c[i].read_bandwidth, bw); 2338 } 2339 } 2340 2341 static void set_access_coordinates(struct access_coordinate *out, 2342 struct access_coordinate *in) 2343 { 2344 for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) 2345 out[i] = in[i]; 2346 } 2347 2348 static bool parent_port_is_cxl_root(struct cxl_port *port) 2349 { 2350 return is_cxl_root(to_cxl_port(port->dev.parent)); 2351 } 2352 2353 /** 2354 * cxl_endpoint_get_perf_coordinates - Retrieve performance numbers stored in dports 2355 * of CXL path 2356 * @port: endpoint cxl_port 2357 * @coord: output performance data 2358 * 2359 * Return: errno on failure, 0 on success. 2360 */ 2361 int cxl_endpoint_get_perf_coordinates(struct cxl_port *port, 2362 struct access_coordinate *coord) 2363 { 2364 struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev); 2365 struct access_coordinate c[] = { 2366 { 2367 .read_bandwidth = UINT_MAX, 2368 .write_bandwidth = UINT_MAX, 2369 }, 2370 { 2371 .read_bandwidth = UINT_MAX, 2372 .write_bandwidth = UINT_MAX, 2373 }, 2374 }; 2375 struct cxl_port *iter = port; 2376 struct cxl_dport *dport; 2377 struct pci_dev *pdev; 2378 struct device *dev; 2379 unsigned int bw; 2380 bool is_cxl_root; 2381 2382 if (!is_cxl_endpoint(port)) 2383 return -EINVAL; 2384 2385 /* 2386 * Skip calculation for RCD. Expectation is HMAT already covers RCD case 2387 * since RCH does not support hotplug. 2388 */ 2389 if (cxlmd->cxlds->rcd) 2390 return 0; 2391 2392 /* 2393 * Exit the loop when the parent port of the current iter port is cxl 2394 * root. The iterative loop starts at the endpoint and gathers the 2395 * latency of the CXL link from the current device/port to the connected 2396 * downstream port each iteration. 2397 */ 2398 do { 2399 dport = iter->parent_dport; 2400 iter = to_cxl_port(iter->dev.parent); 2401 is_cxl_root = parent_port_is_cxl_root(iter); 2402 2403 /* 2404 * There's no valid access_coordinate for a root port since RPs do not 2405 * have CDAT and therefore needs to be skipped. 2406 */ 2407 if (!is_cxl_root) { 2408 if (!coordinates_valid(dport->coord)) 2409 return -EINVAL; 2410 cxl_coordinates_combine(c, c, dport->coord); 2411 } 2412 add_latency(c, dport->link_latency); 2413 } while (!is_cxl_root); 2414 2415 dport = iter->parent_dport; 2416 /* Retrieve HB coords */ 2417 if (!coordinates_valid(dport->coord)) 2418 return -EINVAL; 2419 cxl_coordinates_combine(c, c, dport->coord); 2420 2421 dev = port->uport_dev->parent; 2422 if (!dev_is_pci(dev)) 2423 return -ENODEV; 2424 2425 /* Get the calculated PCI paths bandwidth */ 2426 pdev = to_pci_dev(dev); 2427 bw = pcie_bandwidth_available(pdev, NULL, NULL, NULL); 2428 if (bw == 0) 2429 return -ENXIO; 2430 bw /= BITS_PER_BYTE; 2431 2432 set_min_bandwidth(c, bw); 2433 set_access_coordinates(coord, c); 2434 2435 return 0; 2436 } 2437 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_get_perf_coordinates, "CXL"); 2438 2439 int cxl_port_get_switch_dport_bandwidth(struct cxl_port *port, 2440 struct access_coordinate *c) 2441 { 2442 struct cxl_dport *dport = port->parent_dport; 2443 2444 /* Check this port is connected to a switch DSP and not an RP */ 2445 if (parent_port_is_cxl_root(to_cxl_port(port->dev.parent))) 2446 return -ENODEV; 2447 2448 if (!coordinates_valid(dport->coord)) 2449 return -EINVAL; 2450 2451 for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) { 2452 c[i].read_bandwidth = dport->coord[i].read_bandwidth; 2453 c[i].write_bandwidth = dport->coord[i].write_bandwidth; 2454 } 2455 2456 return 0; 2457 } 2458 2459 /* for user tooling to ensure port disable work has completed */ 2460 static ssize_t flush_store(const struct bus_type *bus, const char *buf, size_t count) 2461 { 2462 if (sysfs_streq(buf, "1")) { 2463 flush_workqueue(cxl_bus_wq); 2464 return count; 2465 } 2466 2467 return -EINVAL; 2468 } 2469 2470 static BUS_ATTR_WO(flush); 2471 2472 static struct attribute *cxl_bus_attributes[] = { 2473 &bus_attr_flush.attr, 2474 NULL, 2475 }; 2476 2477 static struct attribute_group cxl_bus_attribute_group = { 2478 .attrs = cxl_bus_attributes, 2479 }; 2480 2481 static const struct attribute_group *cxl_bus_attribute_groups[] = { 2482 &cxl_bus_attribute_group, 2483 NULL, 2484 }; 2485 2486 const struct bus_type cxl_bus_type = { 2487 .name = "cxl", 2488 .uevent = cxl_bus_uevent, 2489 .match = cxl_bus_match, 2490 .probe = cxl_bus_probe, 2491 .remove = cxl_bus_remove, 2492 .bus_groups = cxl_bus_attribute_groups, 2493 }; 2494 EXPORT_SYMBOL_NS_GPL(cxl_bus_type, "CXL"); 2495 2496 static struct dentry *cxl_debugfs; 2497 2498 struct dentry *cxl_debugfs_create_dir(const char *dir) 2499 { 2500 return debugfs_create_dir(dir, cxl_debugfs); 2501 } 2502 EXPORT_SYMBOL_NS_GPL(cxl_debugfs_create_dir, "CXL"); 2503 2504 static __init int cxl_core_init(void) 2505 { 2506 int rc; 2507 2508 cxl_debugfs = debugfs_create_dir("cxl", NULL); 2509 2510 if (einj_cxl_is_initialized()) 2511 debugfs_create_file("einj_types", 0400, cxl_debugfs, NULL, 2512 &einj_cxl_available_error_type_fops); 2513 2514 cxl_mbox_init(); 2515 2516 rc = cxl_memdev_init(); 2517 if (rc) 2518 return rc; 2519 2520 cxl_bus_wq = alloc_ordered_workqueue("cxl_port", 0); 2521 if (!cxl_bus_wq) { 2522 rc = -ENOMEM; 2523 goto err_wq; 2524 } 2525 2526 rc = bus_register(&cxl_bus_type); 2527 if (rc) 2528 goto err_bus; 2529 2530 rc = cxl_region_init(); 2531 if (rc) 2532 goto err_region; 2533 2534 rc = cxl_ras_init(); 2535 if (rc) 2536 goto err_ras; 2537 2538 return 0; 2539 2540 err_ras: 2541 cxl_region_exit(); 2542 err_region: 2543 bus_unregister(&cxl_bus_type); 2544 err_bus: 2545 destroy_workqueue(cxl_bus_wq); 2546 err_wq: 2547 cxl_memdev_exit(); 2548 return rc; 2549 } 2550 2551 static void cxl_core_exit(void) 2552 { 2553 cxl_ras_exit(); 2554 cxl_region_exit(); 2555 bus_unregister(&cxl_bus_type); 2556 destroy_workqueue(cxl_bus_wq); 2557 cxl_memdev_exit(); 2558 debugfs_remove_recursive(cxl_debugfs); 2559 } 2560 2561 subsys_initcall(cxl_core_init); 2562 module_exit(cxl_core_exit); 2563 MODULE_DESCRIPTION("CXL: Core Compute Express Link support"); 2564 MODULE_LICENSE("GPL v2"); 2565 MODULE_IMPORT_NS("CXL"); 2566