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