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