1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ 3 #include <linux/memregion.h> 4 #include <linux/workqueue.h> 5 #include <linux/debugfs.h> 6 #include <linux/device.h> 7 #include <linux/module.h> 8 #include <linux/pci.h> 9 #include <linux/slab.h> 10 #include <linux/idr.h> 11 #include <cxlmem.h> 12 #include <cxlpci.h> 13 #include <cxl.h> 14 #include "core.h" 15 16 /** 17 * DOC: cxl core 18 * 19 * The CXL core provides a set of interfaces that can be consumed by CXL aware 20 * drivers. The interfaces allow for creation, modification, and destruction of 21 * regions, memory devices, ports, and decoders. CXL aware drivers must register 22 * with the CXL core via these interfaces in order to be able to participate in 23 * cross-device interleave coordination. The CXL core also establishes and 24 * maintains the bridge to the nvdimm subsystem. 25 * 26 * CXL core introduces sysfs hierarchy to control the devices that are 27 * instantiated by the core. 28 */ 29 30 static DEFINE_IDA(cxl_port_ida); 31 static DEFINE_XARRAY(cxl_root_buses); 32 33 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr, 34 char *buf) 35 { 36 return sysfs_emit(buf, "%s\n", dev->type->name); 37 } 38 static DEVICE_ATTR_RO(devtype); 39 40 static int cxl_device_id(const struct device *dev) 41 { 42 if (dev->type == &cxl_nvdimm_bridge_type) 43 return CXL_DEVICE_NVDIMM_BRIDGE; 44 if (dev->type == &cxl_nvdimm_type) 45 return CXL_DEVICE_NVDIMM; 46 if (dev->type == CXL_PMEM_REGION_TYPE()) 47 return CXL_DEVICE_PMEM_REGION; 48 if (dev->type == CXL_DAX_REGION_TYPE()) 49 return CXL_DEVICE_DAX_REGION; 50 if (is_cxl_port(dev)) { 51 if (is_cxl_root(to_cxl_port(dev))) 52 return CXL_DEVICE_ROOT; 53 return CXL_DEVICE_PORT; 54 } 55 if (is_cxl_memdev(dev)) 56 return CXL_DEVICE_MEMORY_EXPANDER; 57 if (dev->type == CXL_REGION_TYPE()) 58 return CXL_DEVICE_REGION; 59 if (dev->type == &cxl_pmu_type) 60 return CXL_DEVICE_PMU; 61 return 0; 62 } 63 64 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 65 char *buf) 66 { 67 return sysfs_emit(buf, CXL_MODALIAS_FMT "\n", cxl_device_id(dev)); 68 } 69 static DEVICE_ATTR_RO(modalias); 70 71 static struct attribute *cxl_base_attributes[] = { 72 &dev_attr_devtype.attr, 73 &dev_attr_modalias.attr, 74 NULL, 75 }; 76 77 struct attribute_group cxl_base_attribute_group = { 78 .attrs = cxl_base_attributes, 79 }; 80 81 static ssize_t start_show(struct device *dev, struct device_attribute *attr, 82 char *buf) 83 { 84 struct cxl_decoder *cxld = to_cxl_decoder(dev); 85 86 return sysfs_emit(buf, "%#llx\n", cxld->hpa_range.start); 87 } 88 static DEVICE_ATTR_ADMIN_RO(start); 89 90 static ssize_t size_show(struct device *dev, struct device_attribute *attr, 91 char *buf) 92 { 93 struct cxl_decoder *cxld = to_cxl_decoder(dev); 94 95 return sysfs_emit(buf, "%#llx\n", range_len(&cxld->hpa_range)); 96 } 97 static DEVICE_ATTR_RO(size); 98 99 #define CXL_DECODER_FLAG_ATTR(name, flag) \ 100 static ssize_t name##_show(struct device *dev, \ 101 struct device_attribute *attr, char *buf) \ 102 { \ 103 struct cxl_decoder *cxld = to_cxl_decoder(dev); \ 104 \ 105 return sysfs_emit(buf, "%s\n", \ 106 (cxld->flags & (flag)) ? "1" : "0"); \ 107 } \ 108 static DEVICE_ATTR_RO(name) 109 110 CXL_DECODER_FLAG_ATTR(cap_pmem, CXL_DECODER_F_PMEM); 111 CXL_DECODER_FLAG_ATTR(cap_ram, CXL_DECODER_F_RAM); 112 CXL_DECODER_FLAG_ATTR(cap_type2, CXL_DECODER_F_TYPE2); 113 CXL_DECODER_FLAG_ATTR(cap_type3, CXL_DECODER_F_TYPE3); 114 CXL_DECODER_FLAG_ATTR(locked, CXL_DECODER_F_LOCK); 115 116 static ssize_t target_type_show(struct device *dev, 117 struct device_attribute *attr, char *buf) 118 { 119 struct cxl_decoder *cxld = to_cxl_decoder(dev); 120 121 switch (cxld->target_type) { 122 case CXL_DECODER_DEVMEM: 123 return sysfs_emit(buf, "accelerator\n"); 124 case CXL_DECODER_HOSTONLYMEM: 125 return sysfs_emit(buf, "expander\n"); 126 } 127 return -ENXIO; 128 } 129 static DEVICE_ATTR_RO(target_type); 130 131 static ssize_t emit_target_list(struct cxl_switch_decoder *cxlsd, char *buf) 132 { 133 struct cxl_decoder *cxld = &cxlsd->cxld; 134 ssize_t offset = 0; 135 int i, rc = 0; 136 137 for (i = 0; i < cxld->interleave_ways; i++) { 138 struct cxl_dport *dport = cxlsd->target[i]; 139 struct cxl_dport *next = NULL; 140 141 if (!dport) 142 break; 143 144 if (i + 1 < cxld->interleave_ways) 145 next = cxlsd->target[i + 1]; 146 rc = sysfs_emit_at(buf, offset, "%d%s", dport->port_id, 147 next ? "," : ""); 148 if (rc < 0) 149 return rc; 150 offset += rc; 151 } 152 153 return offset; 154 } 155 156 static ssize_t target_list_show(struct device *dev, 157 struct device_attribute *attr, char *buf) 158 { 159 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev); 160 ssize_t offset; 161 unsigned int seq; 162 int rc; 163 164 do { 165 seq = read_seqbegin(&cxlsd->target_lock); 166 rc = emit_target_list(cxlsd, buf); 167 } while (read_seqretry(&cxlsd->target_lock, seq)); 168 169 if (rc < 0) 170 return rc; 171 offset = rc; 172 173 rc = sysfs_emit_at(buf, offset, "\n"); 174 if (rc < 0) 175 return rc; 176 177 return offset + rc; 178 } 179 static DEVICE_ATTR_RO(target_list); 180 181 static ssize_t mode_show(struct device *dev, struct device_attribute *attr, 182 char *buf) 183 { 184 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 185 186 return sysfs_emit(buf, "%s\n", cxl_decoder_mode_name(cxled->mode)); 187 } 188 189 static ssize_t mode_store(struct device *dev, struct device_attribute *attr, 190 const char *buf, size_t len) 191 { 192 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 193 enum cxl_decoder_mode mode; 194 ssize_t rc; 195 196 if (sysfs_streq(buf, "pmem")) 197 mode = CXL_DECODER_PMEM; 198 else if (sysfs_streq(buf, "ram")) 199 mode = CXL_DECODER_RAM; 200 else 201 return -EINVAL; 202 203 rc = cxl_dpa_set_mode(cxled, mode); 204 if (rc) 205 return rc; 206 207 return len; 208 } 209 static DEVICE_ATTR_RW(mode); 210 211 static ssize_t dpa_resource_show(struct device *dev, struct device_attribute *attr, 212 char *buf) 213 { 214 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 215 u64 base = cxl_dpa_resource_start(cxled); 216 217 return sysfs_emit(buf, "%#llx\n", base); 218 } 219 static DEVICE_ATTR_RO(dpa_resource); 220 221 static ssize_t dpa_size_show(struct device *dev, struct device_attribute *attr, 222 char *buf) 223 { 224 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 225 resource_size_t size = cxl_dpa_size(cxled); 226 227 return sysfs_emit(buf, "%pa\n", &size); 228 } 229 230 static ssize_t dpa_size_store(struct device *dev, struct device_attribute *attr, 231 const char *buf, size_t len) 232 { 233 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 234 unsigned long long size; 235 ssize_t rc; 236 237 rc = kstrtoull(buf, 0, &size); 238 if (rc) 239 return rc; 240 241 if (!IS_ALIGNED(size, SZ_256M)) 242 return -EINVAL; 243 244 rc = cxl_dpa_free(cxled); 245 if (rc) 246 return rc; 247 248 if (size == 0) 249 return len; 250 251 rc = cxl_dpa_alloc(cxled, size); 252 if (rc) 253 return rc; 254 255 return len; 256 } 257 static DEVICE_ATTR_RW(dpa_size); 258 259 static ssize_t interleave_granularity_show(struct device *dev, 260 struct device_attribute *attr, 261 char *buf) 262 { 263 struct cxl_decoder *cxld = to_cxl_decoder(dev); 264 265 return sysfs_emit(buf, "%d\n", cxld->interleave_granularity); 266 } 267 268 static DEVICE_ATTR_RO(interleave_granularity); 269 270 static ssize_t interleave_ways_show(struct device *dev, 271 struct device_attribute *attr, char *buf) 272 { 273 struct cxl_decoder *cxld = to_cxl_decoder(dev); 274 275 return sysfs_emit(buf, "%d\n", cxld->interleave_ways); 276 } 277 278 static DEVICE_ATTR_RO(interleave_ways); 279 280 static struct attribute *cxl_decoder_base_attrs[] = { 281 &dev_attr_start.attr, 282 &dev_attr_size.attr, 283 &dev_attr_locked.attr, 284 &dev_attr_interleave_granularity.attr, 285 &dev_attr_interleave_ways.attr, 286 NULL, 287 }; 288 289 static struct attribute_group cxl_decoder_base_attribute_group = { 290 .attrs = cxl_decoder_base_attrs, 291 }; 292 293 static struct attribute *cxl_decoder_root_attrs[] = { 294 &dev_attr_cap_pmem.attr, 295 &dev_attr_cap_ram.attr, 296 &dev_attr_cap_type2.attr, 297 &dev_attr_cap_type3.attr, 298 &dev_attr_target_list.attr, 299 SET_CXL_REGION_ATTR(create_pmem_region) 300 SET_CXL_REGION_ATTR(create_ram_region) 301 SET_CXL_REGION_ATTR(delete_region) 302 NULL, 303 }; 304 305 static bool can_create_pmem(struct cxl_root_decoder *cxlrd) 306 { 307 unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_PMEM; 308 309 return (cxlrd->cxlsd.cxld.flags & flags) == flags; 310 } 311 312 static bool can_create_ram(struct cxl_root_decoder *cxlrd) 313 { 314 unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_RAM; 315 316 return (cxlrd->cxlsd.cxld.flags & flags) == flags; 317 } 318 319 static umode_t cxl_root_decoder_visible(struct kobject *kobj, struct attribute *a, int n) 320 { 321 struct device *dev = kobj_to_dev(kobj); 322 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 323 324 if (a == CXL_REGION_ATTR(create_pmem_region) && !can_create_pmem(cxlrd)) 325 return 0; 326 327 if (a == CXL_REGION_ATTR(create_ram_region) && !can_create_ram(cxlrd)) 328 return 0; 329 330 if (a == CXL_REGION_ATTR(delete_region) && 331 !(can_create_pmem(cxlrd) || can_create_ram(cxlrd))) 332 return 0; 333 334 return a->mode; 335 } 336 337 static struct attribute_group cxl_decoder_root_attribute_group = { 338 .attrs = cxl_decoder_root_attrs, 339 .is_visible = cxl_root_decoder_visible, 340 }; 341 342 static const struct attribute_group *cxl_decoder_root_attribute_groups[] = { 343 &cxl_decoder_root_attribute_group, 344 &cxl_decoder_base_attribute_group, 345 &cxl_base_attribute_group, 346 NULL, 347 }; 348 349 static struct attribute *cxl_decoder_switch_attrs[] = { 350 &dev_attr_target_type.attr, 351 &dev_attr_target_list.attr, 352 SET_CXL_REGION_ATTR(region) 353 NULL, 354 }; 355 356 static struct attribute_group cxl_decoder_switch_attribute_group = { 357 .attrs = cxl_decoder_switch_attrs, 358 }; 359 360 static const struct attribute_group *cxl_decoder_switch_attribute_groups[] = { 361 &cxl_decoder_switch_attribute_group, 362 &cxl_decoder_base_attribute_group, 363 &cxl_base_attribute_group, 364 NULL, 365 }; 366 367 static struct attribute *cxl_decoder_endpoint_attrs[] = { 368 &dev_attr_target_type.attr, 369 &dev_attr_mode.attr, 370 &dev_attr_dpa_size.attr, 371 &dev_attr_dpa_resource.attr, 372 SET_CXL_REGION_ATTR(region) 373 NULL, 374 }; 375 376 static struct attribute_group cxl_decoder_endpoint_attribute_group = { 377 .attrs = cxl_decoder_endpoint_attrs, 378 }; 379 380 static const struct attribute_group *cxl_decoder_endpoint_attribute_groups[] = { 381 &cxl_decoder_base_attribute_group, 382 &cxl_decoder_endpoint_attribute_group, 383 &cxl_base_attribute_group, 384 NULL, 385 }; 386 387 static void __cxl_decoder_release(struct cxl_decoder *cxld) 388 { 389 struct cxl_port *port = to_cxl_port(cxld->dev.parent); 390 391 ida_free(&port->decoder_ida, cxld->id); 392 put_device(&port->dev); 393 } 394 395 static void cxl_endpoint_decoder_release(struct device *dev) 396 { 397 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 398 399 __cxl_decoder_release(&cxled->cxld); 400 kfree(cxled); 401 } 402 403 static void cxl_switch_decoder_release(struct device *dev) 404 { 405 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev); 406 407 __cxl_decoder_release(&cxlsd->cxld); 408 kfree(cxlsd); 409 } 410 411 struct cxl_root_decoder *to_cxl_root_decoder(struct device *dev) 412 { 413 if (dev_WARN_ONCE(dev, !is_root_decoder(dev), 414 "not a cxl_root_decoder device\n")) 415 return NULL; 416 return container_of(dev, struct cxl_root_decoder, cxlsd.cxld.dev); 417 } 418 EXPORT_SYMBOL_NS_GPL(to_cxl_root_decoder, CXL); 419 420 static void cxl_root_decoder_release(struct device *dev) 421 { 422 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 423 424 if (atomic_read(&cxlrd->region_id) >= 0) 425 memregion_free(atomic_read(&cxlrd->region_id)); 426 __cxl_decoder_release(&cxlrd->cxlsd.cxld); 427 kfree(cxlrd); 428 } 429 430 static const struct device_type cxl_decoder_endpoint_type = { 431 .name = "cxl_decoder_endpoint", 432 .release = cxl_endpoint_decoder_release, 433 .groups = cxl_decoder_endpoint_attribute_groups, 434 }; 435 436 static const struct device_type cxl_decoder_switch_type = { 437 .name = "cxl_decoder_switch", 438 .release = cxl_switch_decoder_release, 439 .groups = cxl_decoder_switch_attribute_groups, 440 }; 441 442 static const struct device_type cxl_decoder_root_type = { 443 .name = "cxl_decoder_root", 444 .release = cxl_root_decoder_release, 445 .groups = cxl_decoder_root_attribute_groups, 446 }; 447 448 bool is_endpoint_decoder(struct device *dev) 449 { 450 return dev->type == &cxl_decoder_endpoint_type; 451 } 452 EXPORT_SYMBOL_NS_GPL(is_endpoint_decoder, CXL); 453 454 bool is_root_decoder(struct device *dev) 455 { 456 return dev->type == &cxl_decoder_root_type; 457 } 458 EXPORT_SYMBOL_NS_GPL(is_root_decoder, CXL); 459 460 bool is_switch_decoder(struct device *dev) 461 { 462 return is_root_decoder(dev) || dev->type == &cxl_decoder_switch_type; 463 } 464 EXPORT_SYMBOL_NS_GPL(is_switch_decoder, CXL); 465 466 struct cxl_decoder *to_cxl_decoder(struct device *dev) 467 { 468 if (dev_WARN_ONCE(dev, 469 !is_switch_decoder(dev) && !is_endpoint_decoder(dev), 470 "not a cxl_decoder device\n")) 471 return NULL; 472 return container_of(dev, struct cxl_decoder, dev); 473 } 474 EXPORT_SYMBOL_NS_GPL(to_cxl_decoder, CXL); 475 476 struct cxl_endpoint_decoder *to_cxl_endpoint_decoder(struct device *dev) 477 { 478 if (dev_WARN_ONCE(dev, !is_endpoint_decoder(dev), 479 "not a cxl_endpoint_decoder device\n")) 480 return NULL; 481 return container_of(dev, struct cxl_endpoint_decoder, cxld.dev); 482 } 483 EXPORT_SYMBOL_NS_GPL(to_cxl_endpoint_decoder, CXL); 484 485 struct cxl_switch_decoder *to_cxl_switch_decoder(struct device *dev) 486 { 487 if (dev_WARN_ONCE(dev, !is_switch_decoder(dev), 488 "not a cxl_switch_decoder device\n")) 489 return NULL; 490 return container_of(dev, struct cxl_switch_decoder, cxld.dev); 491 } 492 EXPORT_SYMBOL_NS_GPL(to_cxl_switch_decoder, CXL); 493 494 static void cxl_ep_release(struct cxl_ep *ep) 495 { 496 put_device(ep->ep); 497 kfree(ep); 498 } 499 500 static void cxl_ep_remove(struct cxl_port *port, struct cxl_ep *ep) 501 { 502 if (!ep) 503 return; 504 xa_erase(&port->endpoints, (unsigned long) ep->ep); 505 cxl_ep_release(ep); 506 } 507 508 static void cxl_port_release(struct device *dev) 509 { 510 struct cxl_port *port = to_cxl_port(dev); 511 unsigned long index; 512 struct cxl_ep *ep; 513 514 xa_for_each(&port->endpoints, index, ep) 515 cxl_ep_remove(port, ep); 516 xa_destroy(&port->endpoints); 517 xa_destroy(&port->dports); 518 xa_destroy(&port->regions); 519 ida_free(&cxl_port_ida, port->id); 520 kfree(port); 521 } 522 523 static const struct attribute_group *cxl_port_attribute_groups[] = { 524 &cxl_base_attribute_group, 525 NULL, 526 }; 527 528 static const struct device_type cxl_port_type = { 529 .name = "cxl_port", 530 .release = cxl_port_release, 531 .groups = cxl_port_attribute_groups, 532 }; 533 534 bool is_cxl_port(const struct device *dev) 535 { 536 return dev->type == &cxl_port_type; 537 } 538 EXPORT_SYMBOL_NS_GPL(is_cxl_port, CXL); 539 540 struct cxl_port *to_cxl_port(const struct device *dev) 541 { 542 if (dev_WARN_ONCE(dev, dev->type != &cxl_port_type, 543 "not a cxl_port device\n")) 544 return NULL; 545 return container_of(dev, struct cxl_port, dev); 546 } 547 EXPORT_SYMBOL_NS_GPL(to_cxl_port, CXL); 548 549 static void unregister_port(void *_port) 550 { 551 struct cxl_port *port = _port; 552 struct cxl_port *parent; 553 struct device *lock_dev; 554 555 if (is_cxl_root(port)) 556 parent = NULL; 557 else 558 parent = to_cxl_port(port->dev.parent); 559 560 /* 561 * CXL root port's and the first level of ports are unregistered 562 * under the platform firmware device lock, all other ports are 563 * unregistered while holding their parent port lock. 564 */ 565 if (!parent) 566 lock_dev = port->uport; 567 else if (is_cxl_root(parent)) 568 lock_dev = parent->uport; 569 else 570 lock_dev = &parent->dev; 571 572 device_lock_assert(lock_dev); 573 port->dead = true; 574 device_unregister(&port->dev); 575 } 576 577 static void cxl_unlink_uport(void *_port) 578 { 579 struct cxl_port *port = _port; 580 581 sysfs_remove_link(&port->dev.kobj, "uport"); 582 } 583 584 static int devm_cxl_link_uport(struct device *host, struct cxl_port *port) 585 { 586 int rc; 587 588 rc = sysfs_create_link(&port->dev.kobj, &port->uport->kobj, "uport"); 589 if (rc) 590 return rc; 591 return devm_add_action_or_reset(host, cxl_unlink_uport, port); 592 } 593 594 static void cxl_unlink_parent_dport(void *_port) 595 { 596 struct cxl_port *port = _port; 597 598 sysfs_remove_link(&port->dev.kobj, "parent_dport"); 599 } 600 601 static int devm_cxl_link_parent_dport(struct device *host, 602 struct cxl_port *port, 603 struct cxl_dport *parent_dport) 604 { 605 int rc; 606 607 if (!parent_dport) 608 return 0; 609 610 rc = sysfs_create_link(&port->dev.kobj, &parent_dport->dport->kobj, 611 "parent_dport"); 612 if (rc) 613 return rc; 614 return devm_add_action_or_reset(host, cxl_unlink_parent_dport, port); 615 } 616 617 static struct lock_class_key cxl_port_key; 618 619 static struct cxl_port *cxl_port_alloc(struct device *uport, 620 resource_size_t component_reg_phys, 621 struct cxl_dport *parent_dport) 622 { 623 struct cxl_port *port; 624 struct device *dev; 625 int rc; 626 627 port = kzalloc(sizeof(*port), GFP_KERNEL); 628 if (!port) 629 return ERR_PTR(-ENOMEM); 630 631 rc = ida_alloc(&cxl_port_ida, GFP_KERNEL); 632 if (rc < 0) 633 goto err; 634 port->id = rc; 635 port->uport = uport; 636 637 /* 638 * The top-level cxl_port "cxl_root" does not have a cxl_port as 639 * its parent and it does not have any corresponding component 640 * registers as its decode is described by a fixed platform 641 * description. 642 */ 643 dev = &port->dev; 644 if (parent_dport) { 645 struct cxl_port *parent_port = parent_dport->port; 646 struct cxl_port *iter; 647 648 dev->parent = &parent_port->dev; 649 port->depth = parent_port->depth + 1; 650 port->parent_dport = parent_dport; 651 652 /* 653 * walk to the host bridge, or the first ancestor that knows 654 * the host bridge 655 */ 656 iter = port; 657 while (!iter->host_bridge && 658 !is_cxl_root(to_cxl_port(iter->dev.parent))) 659 iter = to_cxl_port(iter->dev.parent); 660 if (iter->host_bridge) 661 port->host_bridge = iter->host_bridge; 662 else if (parent_dport->rch) 663 port->host_bridge = parent_dport->dport; 664 else 665 port->host_bridge = iter->uport; 666 dev_dbg(uport, "host-bridge: %s\n", dev_name(port->host_bridge)); 667 } else 668 dev->parent = uport; 669 670 port->component_reg_phys = component_reg_phys; 671 ida_init(&port->decoder_ida); 672 port->hdm_end = -1; 673 port->commit_end = -1; 674 xa_init(&port->dports); 675 xa_init(&port->endpoints); 676 xa_init(&port->regions); 677 678 device_initialize(dev); 679 lockdep_set_class_and_subclass(&dev->mutex, &cxl_port_key, port->depth); 680 device_set_pm_not_required(dev); 681 dev->bus = &cxl_bus_type; 682 dev->type = &cxl_port_type; 683 684 return port; 685 686 err: 687 kfree(port); 688 return ERR_PTR(rc); 689 } 690 691 static struct cxl_port *__devm_cxl_add_port(struct device *host, 692 struct device *uport, 693 resource_size_t component_reg_phys, 694 struct cxl_dport *parent_dport) 695 { 696 struct cxl_port *port; 697 struct device *dev; 698 int rc; 699 700 port = cxl_port_alloc(uport, component_reg_phys, parent_dport); 701 if (IS_ERR(port)) 702 return port; 703 704 dev = &port->dev; 705 if (is_cxl_memdev(uport)) 706 rc = dev_set_name(dev, "endpoint%d", port->id); 707 else if (parent_dport) 708 rc = dev_set_name(dev, "port%d", port->id); 709 else 710 rc = dev_set_name(dev, "root%d", port->id); 711 if (rc) 712 goto err; 713 714 rc = device_add(dev); 715 if (rc) 716 goto err; 717 718 rc = devm_add_action_or_reset(host, unregister_port, port); 719 if (rc) 720 return ERR_PTR(rc); 721 722 rc = devm_cxl_link_uport(host, port); 723 if (rc) 724 return ERR_PTR(rc); 725 726 rc = devm_cxl_link_parent_dport(host, port, parent_dport); 727 if (rc) 728 return ERR_PTR(rc); 729 730 return port; 731 732 err: 733 put_device(dev); 734 return ERR_PTR(rc); 735 } 736 737 /** 738 * devm_cxl_add_port - register a cxl_port in CXL memory decode hierarchy 739 * @host: host device for devm operations 740 * @uport: "physical" device implementing this upstream port 741 * @component_reg_phys: (optional) for configurable cxl_port instances 742 * @parent_dport: next hop up in the CXL memory decode hierarchy 743 */ 744 struct cxl_port *devm_cxl_add_port(struct device *host, struct device *uport, 745 resource_size_t component_reg_phys, 746 struct cxl_dport *parent_dport) 747 { 748 struct cxl_port *port, *parent_port; 749 750 port = __devm_cxl_add_port(host, uport, component_reg_phys, 751 parent_dport); 752 753 parent_port = parent_dport ? parent_dport->port : NULL; 754 if (IS_ERR(port)) { 755 dev_dbg(uport, "Failed to add%s%s%s: %ld\n", 756 parent_port ? " port to " : "", 757 parent_port ? dev_name(&parent_port->dev) : "", 758 parent_port ? "" : " root port", 759 PTR_ERR(port)); 760 } else { 761 dev_dbg(uport, "%s added%s%s%s\n", 762 dev_name(&port->dev), 763 parent_port ? " to " : "", 764 parent_port ? dev_name(&parent_port->dev) : "", 765 parent_port ? "" : " (root port)"); 766 } 767 768 return port; 769 } 770 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_port, CXL); 771 772 struct pci_bus *cxl_port_to_pci_bus(struct cxl_port *port) 773 { 774 /* There is no pci_bus associated with a CXL platform-root port */ 775 if (is_cxl_root(port)) 776 return NULL; 777 778 if (dev_is_pci(port->uport)) { 779 struct pci_dev *pdev = to_pci_dev(port->uport); 780 781 return pdev->subordinate; 782 } 783 784 return xa_load(&cxl_root_buses, (unsigned long)port->uport); 785 } 786 EXPORT_SYMBOL_NS_GPL(cxl_port_to_pci_bus, CXL); 787 788 static void unregister_pci_bus(void *uport) 789 { 790 xa_erase(&cxl_root_buses, (unsigned long)uport); 791 } 792 793 int devm_cxl_register_pci_bus(struct device *host, struct device *uport, 794 struct pci_bus *bus) 795 { 796 int rc; 797 798 if (dev_is_pci(uport)) 799 return -EINVAL; 800 801 rc = xa_insert(&cxl_root_buses, (unsigned long)uport, bus, GFP_KERNEL); 802 if (rc) 803 return rc; 804 return devm_add_action_or_reset(host, unregister_pci_bus, uport); 805 } 806 EXPORT_SYMBOL_NS_GPL(devm_cxl_register_pci_bus, CXL); 807 808 static bool dev_is_cxl_root_child(struct device *dev) 809 { 810 struct cxl_port *port, *parent; 811 812 if (!is_cxl_port(dev)) 813 return false; 814 815 port = to_cxl_port(dev); 816 if (is_cxl_root(port)) 817 return false; 818 819 parent = to_cxl_port(port->dev.parent); 820 if (is_cxl_root(parent)) 821 return true; 822 823 return false; 824 } 825 826 struct cxl_port *find_cxl_root(struct cxl_port *port) 827 { 828 struct cxl_port *iter = port; 829 830 while (iter && !is_cxl_root(iter)) 831 iter = to_cxl_port(iter->dev.parent); 832 833 if (!iter) 834 return NULL; 835 get_device(&iter->dev); 836 return iter; 837 } 838 EXPORT_SYMBOL_NS_GPL(find_cxl_root, CXL); 839 840 static struct cxl_dport *find_dport(struct cxl_port *port, int id) 841 { 842 struct cxl_dport *dport; 843 unsigned long index; 844 845 device_lock_assert(&port->dev); 846 xa_for_each(&port->dports, index, dport) 847 if (dport->port_id == id) 848 return dport; 849 return NULL; 850 } 851 852 static int add_dport(struct cxl_port *port, struct cxl_dport *new) 853 { 854 struct cxl_dport *dup; 855 int rc; 856 857 device_lock_assert(&port->dev); 858 dup = find_dport(port, new->port_id); 859 if (dup) { 860 dev_err(&port->dev, 861 "unable to add dport%d-%s non-unique port id (%s)\n", 862 new->port_id, dev_name(new->dport), 863 dev_name(dup->dport)); 864 return -EBUSY; 865 } 866 867 rc = xa_insert(&port->dports, (unsigned long)new->dport, new, 868 GFP_KERNEL); 869 if (rc) 870 return rc; 871 872 port->nr_dports++; 873 return 0; 874 } 875 876 /* 877 * Since root-level CXL dports cannot be enumerated by PCI they are not 878 * enumerated by the common port driver that acquires the port lock over 879 * dport add/remove. Instead, root dports are manually added by a 880 * platform driver and cond_cxl_root_lock() is used to take the missing 881 * port lock in that case. 882 */ 883 static void cond_cxl_root_lock(struct cxl_port *port) 884 { 885 if (is_cxl_root(port)) 886 device_lock(&port->dev); 887 } 888 889 static void cond_cxl_root_unlock(struct cxl_port *port) 890 { 891 if (is_cxl_root(port)) 892 device_unlock(&port->dev); 893 } 894 895 static void cxl_dport_remove(void *data) 896 { 897 struct cxl_dport *dport = data; 898 struct cxl_port *port = dport->port; 899 900 xa_erase(&port->dports, (unsigned long) dport->dport); 901 put_device(dport->dport); 902 } 903 904 static void cxl_dport_unlink(void *data) 905 { 906 struct cxl_dport *dport = data; 907 struct cxl_port *port = dport->port; 908 char link_name[CXL_TARGET_STRLEN]; 909 910 sprintf(link_name, "dport%d", dport->port_id); 911 sysfs_remove_link(&port->dev.kobj, link_name); 912 } 913 914 static struct cxl_dport * 915 __devm_cxl_add_dport(struct cxl_port *port, struct device *dport_dev, 916 int port_id, resource_size_t component_reg_phys, 917 resource_size_t rcrb) 918 { 919 char link_name[CXL_TARGET_STRLEN]; 920 struct cxl_dport *dport; 921 struct device *host; 922 int rc; 923 924 if (is_cxl_root(port)) 925 host = port->uport; 926 else 927 host = &port->dev; 928 929 if (!host->driver) { 930 dev_WARN_ONCE(&port->dev, 1, "dport:%s bad devm context\n", 931 dev_name(dport_dev)); 932 return ERR_PTR(-ENXIO); 933 } 934 935 if (snprintf(link_name, CXL_TARGET_STRLEN, "dport%d", port_id) >= 936 CXL_TARGET_STRLEN) 937 return ERR_PTR(-EINVAL); 938 939 dport = devm_kzalloc(host, sizeof(*dport), GFP_KERNEL); 940 if (!dport) 941 return ERR_PTR(-ENOMEM); 942 943 dport->dport = dport_dev; 944 dport->port_id = port_id; 945 dport->component_reg_phys = component_reg_phys; 946 dport->port = port; 947 if (rcrb != CXL_RESOURCE_NONE) 948 dport->rch = true; 949 dport->rcrb = rcrb; 950 951 cond_cxl_root_lock(port); 952 rc = add_dport(port, dport); 953 cond_cxl_root_unlock(port); 954 if (rc) 955 return ERR_PTR(rc); 956 957 get_device(dport_dev); 958 rc = devm_add_action_or_reset(host, cxl_dport_remove, dport); 959 if (rc) 960 return ERR_PTR(rc); 961 962 rc = sysfs_create_link(&port->dev.kobj, &dport_dev->kobj, link_name); 963 if (rc) 964 return ERR_PTR(rc); 965 966 rc = devm_add_action_or_reset(host, cxl_dport_unlink, dport); 967 if (rc) 968 return ERR_PTR(rc); 969 970 return dport; 971 } 972 973 /** 974 * devm_cxl_add_dport - append VH downstream port data to a cxl_port 975 * @port: the cxl_port that references this dport 976 * @dport_dev: firmware or PCI device representing the dport 977 * @port_id: identifier for this dport in a decoder's target list 978 * @component_reg_phys: optional location of CXL component registers 979 * 980 * Note that dports are appended to the devm release action's of the 981 * either the port's host (for root ports), or the port itself (for 982 * switch ports) 983 */ 984 struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port, 985 struct device *dport_dev, int port_id, 986 resource_size_t component_reg_phys) 987 { 988 struct cxl_dport *dport; 989 990 dport = __devm_cxl_add_dport(port, dport_dev, port_id, 991 component_reg_phys, CXL_RESOURCE_NONE); 992 if (IS_ERR(dport)) { 993 dev_dbg(dport_dev, "failed to add dport to %s: %ld\n", 994 dev_name(&port->dev), PTR_ERR(dport)); 995 } else { 996 dev_dbg(dport_dev, "dport added to %s\n", 997 dev_name(&port->dev)); 998 } 999 1000 return dport; 1001 } 1002 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_dport, CXL); 1003 1004 /** 1005 * devm_cxl_add_rch_dport - append RCH downstream port data to a cxl_port 1006 * @port: the cxl_port that references this dport 1007 * @dport_dev: firmware or PCI device representing the dport 1008 * @port_id: identifier for this dport in a decoder's target list 1009 * @component_reg_phys: optional location of CXL component registers 1010 * @rcrb: mandatory location of a Root Complex Register Block 1011 * 1012 * See CXL 3.0 9.11.8 CXL Devices Attached to an RCH 1013 */ 1014 struct cxl_dport *devm_cxl_add_rch_dport(struct cxl_port *port, 1015 struct device *dport_dev, int port_id, 1016 resource_size_t component_reg_phys, 1017 resource_size_t rcrb) 1018 { 1019 struct cxl_dport *dport; 1020 1021 if (rcrb == CXL_RESOURCE_NONE) { 1022 dev_dbg(&port->dev, "failed to add RCH dport, missing RCRB\n"); 1023 return ERR_PTR(-EINVAL); 1024 } 1025 1026 dport = __devm_cxl_add_dport(port, dport_dev, port_id, 1027 component_reg_phys, rcrb); 1028 if (IS_ERR(dport)) { 1029 dev_dbg(dport_dev, "failed to add RCH dport to %s: %ld\n", 1030 dev_name(&port->dev), PTR_ERR(dport)); 1031 } else { 1032 dev_dbg(dport_dev, "RCH dport added to %s\n", 1033 dev_name(&port->dev)); 1034 } 1035 1036 return dport; 1037 } 1038 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_rch_dport, CXL); 1039 1040 static int add_ep(struct cxl_ep *new) 1041 { 1042 struct cxl_port *port = new->dport->port; 1043 int rc; 1044 1045 device_lock(&port->dev); 1046 if (port->dead) { 1047 device_unlock(&port->dev); 1048 return -ENXIO; 1049 } 1050 rc = xa_insert(&port->endpoints, (unsigned long)new->ep, new, 1051 GFP_KERNEL); 1052 device_unlock(&port->dev); 1053 1054 return rc; 1055 } 1056 1057 /** 1058 * cxl_add_ep - register an endpoint's interest in a port 1059 * @dport: the dport that routes to @ep_dev 1060 * @ep_dev: device representing the endpoint 1061 * 1062 * Intermediate CXL ports are scanned based on the arrival of endpoints. 1063 * When those endpoints depart the port can be destroyed once all 1064 * endpoints that care about that port have been removed. 1065 */ 1066 static int cxl_add_ep(struct cxl_dport *dport, struct device *ep_dev) 1067 { 1068 struct cxl_ep *ep; 1069 int rc; 1070 1071 ep = kzalloc(sizeof(*ep), GFP_KERNEL); 1072 if (!ep) 1073 return -ENOMEM; 1074 1075 ep->ep = get_device(ep_dev); 1076 ep->dport = dport; 1077 1078 rc = add_ep(ep); 1079 if (rc) 1080 cxl_ep_release(ep); 1081 return rc; 1082 } 1083 1084 struct cxl_find_port_ctx { 1085 const struct device *dport_dev; 1086 const struct cxl_port *parent_port; 1087 struct cxl_dport **dport; 1088 }; 1089 1090 static int match_port_by_dport(struct device *dev, const void *data) 1091 { 1092 const struct cxl_find_port_ctx *ctx = data; 1093 struct cxl_dport *dport; 1094 struct cxl_port *port; 1095 1096 if (!is_cxl_port(dev)) 1097 return 0; 1098 if (ctx->parent_port && dev->parent != &ctx->parent_port->dev) 1099 return 0; 1100 1101 port = to_cxl_port(dev); 1102 dport = cxl_find_dport_by_dev(port, ctx->dport_dev); 1103 if (ctx->dport) 1104 *ctx->dport = dport; 1105 return dport != NULL; 1106 } 1107 1108 static struct cxl_port *__find_cxl_port(struct cxl_find_port_ctx *ctx) 1109 { 1110 struct device *dev; 1111 1112 if (!ctx->dport_dev) 1113 return NULL; 1114 1115 dev = bus_find_device(&cxl_bus_type, NULL, ctx, match_port_by_dport); 1116 if (dev) 1117 return to_cxl_port(dev); 1118 return NULL; 1119 } 1120 1121 static struct cxl_port *find_cxl_port(struct device *dport_dev, 1122 struct cxl_dport **dport) 1123 { 1124 struct cxl_find_port_ctx ctx = { 1125 .dport_dev = dport_dev, 1126 .dport = dport, 1127 }; 1128 struct cxl_port *port; 1129 1130 port = __find_cxl_port(&ctx); 1131 return port; 1132 } 1133 1134 static struct cxl_port *find_cxl_port_at(struct cxl_port *parent_port, 1135 struct device *dport_dev, 1136 struct cxl_dport **dport) 1137 { 1138 struct cxl_find_port_ctx ctx = { 1139 .dport_dev = dport_dev, 1140 .parent_port = parent_port, 1141 .dport = dport, 1142 }; 1143 struct cxl_port *port; 1144 1145 port = __find_cxl_port(&ctx); 1146 return port; 1147 } 1148 1149 /* 1150 * All users of grandparent() are using it to walk PCIe-like switch port 1151 * hierarchy. A PCIe switch is comprised of a bridge device representing the 1152 * upstream switch port and N bridges representing downstream switch ports. When 1153 * bridges stack the grand-parent of a downstream switch port is another 1154 * downstream switch port in the immediate ancestor switch. 1155 */ 1156 static struct device *grandparent(struct device *dev) 1157 { 1158 if (dev && dev->parent) 1159 return dev->parent->parent; 1160 return NULL; 1161 } 1162 1163 static void delete_endpoint(void *data) 1164 { 1165 struct cxl_memdev *cxlmd = data; 1166 struct cxl_port *endpoint = cxlmd->endpoint; 1167 struct cxl_port *parent_port; 1168 struct device *parent; 1169 1170 parent_port = cxl_mem_find_port(cxlmd, NULL); 1171 if (!parent_port) 1172 goto out; 1173 parent = &parent_port->dev; 1174 1175 device_lock(parent); 1176 if (parent->driver && !endpoint->dead) { 1177 devm_release_action(parent, cxl_unlink_parent_dport, endpoint); 1178 devm_release_action(parent, cxl_unlink_uport, endpoint); 1179 devm_release_action(parent, unregister_port, endpoint); 1180 } 1181 cxlmd->endpoint = NULL; 1182 device_unlock(parent); 1183 put_device(parent); 1184 out: 1185 put_device(&endpoint->dev); 1186 } 1187 1188 int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint) 1189 { 1190 struct device *dev = &cxlmd->dev; 1191 1192 get_device(&endpoint->dev); 1193 cxlmd->endpoint = endpoint; 1194 cxlmd->depth = endpoint->depth; 1195 return devm_add_action_or_reset(dev, delete_endpoint, cxlmd); 1196 } 1197 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_autoremove, CXL); 1198 1199 /* 1200 * The natural end of life of a non-root 'cxl_port' is when its parent port goes 1201 * through a ->remove() event ("top-down" unregistration). The unnatural trigger 1202 * for a port to be unregistered is when all memdevs beneath that port have gone 1203 * through ->remove(). This "bottom-up" removal selectively removes individual 1204 * child ports manually. This depends on devm_cxl_add_port() to not change is 1205 * devm action registration order, and for dports to have already been 1206 * destroyed by reap_dports(). 1207 */ 1208 static void delete_switch_port(struct cxl_port *port) 1209 { 1210 devm_release_action(port->dev.parent, cxl_unlink_parent_dport, port); 1211 devm_release_action(port->dev.parent, cxl_unlink_uport, port); 1212 devm_release_action(port->dev.parent, unregister_port, port); 1213 } 1214 1215 static void reap_dports(struct cxl_port *port) 1216 { 1217 struct cxl_dport *dport; 1218 unsigned long index; 1219 1220 device_lock_assert(&port->dev); 1221 1222 xa_for_each(&port->dports, index, dport) { 1223 devm_release_action(&port->dev, cxl_dport_unlink, dport); 1224 devm_release_action(&port->dev, cxl_dport_remove, dport); 1225 devm_kfree(&port->dev, dport); 1226 } 1227 } 1228 1229 struct detach_ctx { 1230 struct cxl_memdev *cxlmd; 1231 int depth; 1232 }; 1233 1234 static int port_has_memdev(struct device *dev, const void *data) 1235 { 1236 const struct detach_ctx *ctx = data; 1237 struct cxl_port *port; 1238 1239 if (!is_cxl_port(dev)) 1240 return 0; 1241 1242 port = to_cxl_port(dev); 1243 if (port->depth != ctx->depth) 1244 return 0; 1245 1246 return !!cxl_ep_load(port, ctx->cxlmd); 1247 } 1248 1249 static void cxl_detach_ep(void *data) 1250 { 1251 struct cxl_memdev *cxlmd = data; 1252 1253 for (int i = cxlmd->depth - 1; i >= 1; i--) { 1254 struct cxl_port *port, *parent_port; 1255 struct detach_ctx ctx = { 1256 .cxlmd = cxlmd, 1257 .depth = i, 1258 }; 1259 struct device *dev; 1260 struct cxl_ep *ep; 1261 bool died = false; 1262 1263 dev = bus_find_device(&cxl_bus_type, NULL, &ctx, 1264 port_has_memdev); 1265 if (!dev) 1266 continue; 1267 port = to_cxl_port(dev); 1268 1269 parent_port = to_cxl_port(port->dev.parent); 1270 device_lock(&parent_port->dev); 1271 device_lock(&port->dev); 1272 ep = cxl_ep_load(port, cxlmd); 1273 dev_dbg(&cxlmd->dev, "disconnect %s from %s\n", 1274 ep ? dev_name(ep->ep) : "", dev_name(&port->dev)); 1275 cxl_ep_remove(port, ep); 1276 if (ep && !port->dead && xa_empty(&port->endpoints) && 1277 !is_cxl_root(parent_port) && parent_port->dev.driver) { 1278 /* 1279 * This was the last ep attached to a dynamically 1280 * enumerated port. Block new cxl_add_ep() and garbage 1281 * collect the port. 1282 */ 1283 died = true; 1284 port->dead = true; 1285 reap_dports(port); 1286 } 1287 device_unlock(&port->dev); 1288 1289 if (died) { 1290 dev_dbg(&cxlmd->dev, "delete %s\n", 1291 dev_name(&port->dev)); 1292 delete_switch_port(port); 1293 } 1294 put_device(&port->dev); 1295 device_unlock(&parent_port->dev); 1296 } 1297 } 1298 1299 static resource_size_t find_component_registers(struct device *dev) 1300 { 1301 struct cxl_register_map map; 1302 struct pci_dev *pdev; 1303 1304 /* 1305 * Theoretically, CXL component registers can be hosted on a 1306 * non-PCI device, in practice, only cxl_test hits this case. 1307 */ 1308 if (!dev_is_pci(dev)) 1309 return CXL_RESOURCE_NONE; 1310 1311 pdev = to_pci_dev(dev); 1312 1313 cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map); 1314 return map.resource; 1315 } 1316 1317 static int add_port_attach_ep(struct cxl_memdev *cxlmd, 1318 struct device *uport_dev, 1319 struct device *dport_dev) 1320 { 1321 struct device *dparent = grandparent(dport_dev); 1322 struct cxl_port *port, *parent_port = NULL; 1323 struct cxl_dport *dport, *parent_dport; 1324 resource_size_t component_reg_phys; 1325 int rc; 1326 1327 if (!dparent) { 1328 /* 1329 * The iteration reached the topology root without finding the 1330 * CXL-root 'cxl_port' on a previous iteration, fail for now to 1331 * be re-probed after platform driver attaches. 1332 */ 1333 dev_dbg(&cxlmd->dev, "%s is a root dport\n", 1334 dev_name(dport_dev)); 1335 return -ENXIO; 1336 } 1337 1338 parent_port = find_cxl_port(dparent, &parent_dport); 1339 if (!parent_port) { 1340 /* iterate to create this parent_port */ 1341 return -EAGAIN; 1342 } 1343 1344 device_lock(&parent_port->dev); 1345 if (!parent_port->dev.driver) { 1346 dev_warn(&cxlmd->dev, 1347 "port %s:%s disabled, failed to enumerate CXL.mem\n", 1348 dev_name(&parent_port->dev), dev_name(uport_dev)); 1349 port = ERR_PTR(-ENXIO); 1350 goto out; 1351 } 1352 1353 port = find_cxl_port_at(parent_port, dport_dev, &dport); 1354 if (!port) { 1355 component_reg_phys = find_component_registers(uport_dev); 1356 port = devm_cxl_add_port(&parent_port->dev, uport_dev, 1357 component_reg_phys, parent_dport); 1358 /* retry find to pick up the new dport information */ 1359 if (!IS_ERR(port)) 1360 port = find_cxl_port_at(parent_port, dport_dev, &dport); 1361 } 1362 out: 1363 device_unlock(&parent_port->dev); 1364 1365 if (IS_ERR(port)) 1366 rc = PTR_ERR(port); 1367 else { 1368 dev_dbg(&cxlmd->dev, "add to new port %s:%s\n", 1369 dev_name(&port->dev), dev_name(port->uport)); 1370 rc = cxl_add_ep(dport, &cxlmd->dev); 1371 if (rc == -EBUSY) { 1372 /* 1373 * "can't" happen, but this error code means 1374 * something to the caller, so translate it. 1375 */ 1376 rc = -ENXIO; 1377 } 1378 put_device(&port->dev); 1379 } 1380 1381 put_device(&parent_port->dev); 1382 return rc; 1383 } 1384 1385 int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd) 1386 { 1387 struct device *dev = &cxlmd->dev; 1388 struct device *iter; 1389 int rc; 1390 1391 /* 1392 * Skip intermediate port enumeration in the RCH case, there 1393 * are no ports in between a host bridge and an endpoint. 1394 */ 1395 if (cxlmd->cxlds->rcd) 1396 return 0; 1397 1398 rc = devm_add_action_or_reset(&cxlmd->dev, cxl_detach_ep, cxlmd); 1399 if (rc) 1400 return rc; 1401 1402 /* 1403 * Scan for and add all cxl_ports in this device's ancestry. 1404 * Repeat until no more ports are added. Abort if a port add 1405 * attempt fails. 1406 */ 1407 retry: 1408 for (iter = dev; iter; iter = grandparent(iter)) { 1409 struct device *dport_dev = grandparent(iter); 1410 struct device *uport_dev; 1411 struct cxl_dport *dport; 1412 struct cxl_port *port; 1413 1414 if (!dport_dev) 1415 return 0; 1416 1417 uport_dev = dport_dev->parent; 1418 if (!uport_dev) { 1419 dev_warn(dev, "at %s no parent for dport: %s\n", 1420 dev_name(iter), dev_name(dport_dev)); 1421 return -ENXIO; 1422 } 1423 1424 dev_dbg(dev, "scan: iter: %s dport_dev: %s parent: %s\n", 1425 dev_name(iter), dev_name(dport_dev), 1426 dev_name(uport_dev)); 1427 port = find_cxl_port(dport_dev, &dport); 1428 if (port) { 1429 dev_dbg(&cxlmd->dev, 1430 "found already registered port %s:%s\n", 1431 dev_name(&port->dev), dev_name(port->uport)); 1432 rc = cxl_add_ep(dport, &cxlmd->dev); 1433 1434 /* 1435 * If the endpoint already exists in the port's list, 1436 * that's ok, it was added on a previous pass. 1437 * Otherwise, retry in add_port_attach_ep() after taking 1438 * the parent_port lock as the current port may be being 1439 * reaped. 1440 */ 1441 if (rc && rc != -EBUSY) { 1442 put_device(&port->dev); 1443 return rc; 1444 } 1445 1446 /* Any more ports to add between this one and the root? */ 1447 if (!dev_is_cxl_root_child(&port->dev)) { 1448 put_device(&port->dev); 1449 continue; 1450 } 1451 1452 put_device(&port->dev); 1453 return 0; 1454 } 1455 1456 rc = add_port_attach_ep(cxlmd, uport_dev, dport_dev); 1457 /* port missing, try to add parent */ 1458 if (rc == -EAGAIN) 1459 continue; 1460 /* failed to add ep or port */ 1461 if (rc) 1462 return rc; 1463 /* port added, new descendants possible, start over */ 1464 goto retry; 1465 } 1466 1467 return 0; 1468 } 1469 EXPORT_SYMBOL_NS_GPL(devm_cxl_enumerate_ports, CXL); 1470 1471 struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd, 1472 struct cxl_dport **dport) 1473 { 1474 return find_cxl_port(grandparent(&cxlmd->dev), dport); 1475 } 1476 EXPORT_SYMBOL_NS_GPL(cxl_mem_find_port, CXL); 1477 1478 static int decoder_populate_targets(struct cxl_switch_decoder *cxlsd, 1479 struct cxl_port *port, int *target_map) 1480 { 1481 int i, rc = 0; 1482 1483 if (!target_map) 1484 return 0; 1485 1486 device_lock_assert(&port->dev); 1487 1488 if (xa_empty(&port->dports)) 1489 return -EINVAL; 1490 1491 write_seqlock(&cxlsd->target_lock); 1492 for (i = 0; i < cxlsd->nr_targets; i++) { 1493 struct cxl_dport *dport = find_dport(port, target_map[i]); 1494 1495 if (!dport) { 1496 rc = -ENXIO; 1497 break; 1498 } 1499 cxlsd->target[i] = dport; 1500 } 1501 write_sequnlock(&cxlsd->target_lock); 1502 1503 return rc; 1504 } 1505 1506 struct cxl_dport *cxl_hb_modulo(struct cxl_root_decoder *cxlrd, int pos) 1507 { 1508 struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd; 1509 struct cxl_decoder *cxld = &cxlsd->cxld; 1510 int iw; 1511 1512 iw = cxld->interleave_ways; 1513 if (dev_WARN_ONCE(&cxld->dev, iw != cxlsd->nr_targets, 1514 "misconfigured root decoder\n")) 1515 return NULL; 1516 1517 return cxlrd->cxlsd.target[pos % iw]; 1518 } 1519 EXPORT_SYMBOL_NS_GPL(cxl_hb_modulo, CXL); 1520 1521 static struct lock_class_key cxl_decoder_key; 1522 1523 /** 1524 * cxl_decoder_init - Common decoder setup / initialization 1525 * @port: owning port of this decoder 1526 * @cxld: common decoder properties to initialize 1527 * 1528 * A port may contain one or more decoders. Each of those decoders 1529 * enable some address space for CXL.mem utilization. A decoder is 1530 * expected to be configured by the caller before registering via 1531 * cxl_decoder_add() 1532 */ 1533 static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld) 1534 { 1535 struct device *dev; 1536 int rc; 1537 1538 rc = ida_alloc(&port->decoder_ida, GFP_KERNEL); 1539 if (rc < 0) 1540 return rc; 1541 1542 /* need parent to stick around to release the id */ 1543 get_device(&port->dev); 1544 cxld->id = rc; 1545 1546 dev = &cxld->dev; 1547 device_initialize(dev); 1548 lockdep_set_class(&dev->mutex, &cxl_decoder_key); 1549 device_set_pm_not_required(dev); 1550 dev->parent = &port->dev; 1551 dev->bus = &cxl_bus_type; 1552 1553 /* Pre initialize an "empty" decoder */ 1554 cxld->interleave_ways = 1; 1555 cxld->interleave_granularity = PAGE_SIZE; 1556 cxld->target_type = CXL_DECODER_HOSTONLYMEM; 1557 cxld->hpa_range = (struct range) { 1558 .start = 0, 1559 .end = -1, 1560 }; 1561 1562 return 0; 1563 } 1564 1565 static int cxl_switch_decoder_init(struct cxl_port *port, 1566 struct cxl_switch_decoder *cxlsd, 1567 int nr_targets) 1568 { 1569 if (nr_targets > CXL_DECODER_MAX_INTERLEAVE) 1570 return -EINVAL; 1571 1572 cxlsd->nr_targets = nr_targets; 1573 seqlock_init(&cxlsd->target_lock); 1574 return cxl_decoder_init(port, &cxlsd->cxld); 1575 } 1576 1577 /** 1578 * cxl_root_decoder_alloc - Allocate a root level decoder 1579 * @port: owning CXL root of this decoder 1580 * @nr_targets: static number of downstream targets 1581 * @calc_hb: which host bridge covers the n'th position by granularity 1582 * 1583 * Return: A new cxl decoder to be registered by cxl_decoder_add(). A 1584 * 'CXL root' decoder is one that decodes from a top-level / static platform 1585 * firmware description of CXL resources into a CXL standard decode 1586 * topology. 1587 */ 1588 struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port, 1589 unsigned int nr_targets, 1590 cxl_calc_hb_fn calc_hb) 1591 { 1592 struct cxl_root_decoder *cxlrd; 1593 struct cxl_switch_decoder *cxlsd; 1594 struct cxl_decoder *cxld; 1595 int rc; 1596 1597 if (!is_cxl_root(port)) 1598 return ERR_PTR(-EINVAL); 1599 1600 cxlrd = kzalloc(struct_size(cxlrd, cxlsd.target, nr_targets), 1601 GFP_KERNEL); 1602 if (!cxlrd) 1603 return ERR_PTR(-ENOMEM); 1604 1605 cxlsd = &cxlrd->cxlsd; 1606 rc = cxl_switch_decoder_init(port, cxlsd, nr_targets); 1607 if (rc) { 1608 kfree(cxlrd); 1609 return ERR_PTR(rc); 1610 } 1611 1612 cxlrd->calc_hb = calc_hb; 1613 mutex_init(&cxlrd->range_lock); 1614 1615 cxld = &cxlsd->cxld; 1616 cxld->dev.type = &cxl_decoder_root_type; 1617 /* 1618 * cxl_root_decoder_release() special cases negative ids to 1619 * detect memregion_alloc() failures. 1620 */ 1621 atomic_set(&cxlrd->region_id, -1); 1622 rc = memregion_alloc(GFP_KERNEL); 1623 if (rc < 0) { 1624 put_device(&cxld->dev); 1625 return ERR_PTR(rc); 1626 } 1627 1628 atomic_set(&cxlrd->region_id, rc); 1629 return cxlrd; 1630 } 1631 EXPORT_SYMBOL_NS_GPL(cxl_root_decoder_alloc, CXL); 1632 1633 /** 1634 * cxl_switch_decoder_alloc - Allocate a switch level decoder 1635 * @port: owning CXL switch port of this decoder 1636 * @nr_targets: max number of dynamically addressable downstream targets 1637 * 1638 * Return: A new cxl decoder to be registered by cxl_decoder_add(). A 1639 * 'switch' decoder is any decoder that can be enumerated by PCIe 1640 * topology and the HDM Decoder Capability. This includes the decoders 1641 * that sit between Switch Upstream Ports / Switch Downstream Ports and 1642 * Host Bridges / Root Ports. 1643 */ 1644 struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port, 1645 unsigned int nr_targets) 1646 { 1647 struct cxl_switch_decoder *cxlsd; 1648 struct cxl_decoder *cxld; 1649 int rc; 1650 1651 if (is_cxl_root(port) || is_cxl_endpoint(port)) 1652 return ERR_PTR(-EINVAL); 1653 1654 cxlsd = kzalloc(struct_size(cxlsd, target, nr_targets), GFP_KERNEL); 1655 if (!cxlsd) 1656 return ERR_PTR(-ENOMEM); 1657 1658 rc = cxl_switch_decoder_init(port, cxlsd, nr_targets); 1659 if (rc) { 1660 kfree(cxlsd); 1661 return ERR_PTR(rc); 1662 } 1663 1664 cxld = &cxlsd->cxld; 1665 cxld->dev.type = &cxl_decoder_switch_type; 1666 return cxlsd; 1667 } 1668 EXPORT_SYMBOL_NS_GPL(cxl_switch_decoder_alloc, CXL); 1669 1670 /** 1671 * cxl_endpoint_decoder_alloc - Allocate an endpoint decoder 1672 * @port: owning port of this decoder 1673 * 1674 * Return: A new cxl decoder to be registered by cxl_decoder_add() 1675 */ 1676 struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port) 1677 { 1678 struct cxl_endpoint_decoder *cxled; 1679 struct cxl_decoder *cxld; 1680 int rc; 1681 1682 if (!is_cxl_endpoint(port)) 1683 return ERR_PTR(-EINVAL); 1684 1685 cxled = kzalloc(sizeof(*cxled), GFP_KERNEL); 1686 if (!cxled) 1687 return ERR_PTR(-ENOMEM); 1688 1689 cxled->pos = -1; 1690 cxld = &cxled->cxld; 1691 rc = cxl_decoder_init(port, cxld); 1692 if (rc) { 1693 kfree(cxled); 1694 return ERR_PTR(rc); 1695 } 1696 1697 cxld->dev.type = &cxl_decoder_endpoint_type; 1698 return cxled; 1699 } 1700 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_decoder_alloc, CXL); 1701 1702 /** 1703 * cxl_decoder_add_locked - Add a decoder with targets 1704 * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc() 1705 * @target_map: A list of downstream ports that this decoder can direct memory 1706 * traffic to. These numbers should correspond with the port number 1707 * in the PCIe Link Capabilities structure. 1708 * 1709 * Certain types of decoders may not have any targets. The main example of this 1710 * is an endpoint device. A more awkward example is a hostbridge whose root 1711 * ports get hot added (technically possible, though unlikely). 1712 * 1713 * This is the locked variant of cxl_decoder_add(). 1714 * 1715 * Context: Process context. Expects the device lock of the port that owns the 1716 * @cxld to be held. 1717 * 1718 * Return: Negative error code if the decoder wasn't properly configured; else 1719 * returns 0. 1720 */ 1721 int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map) 1722 { 1723 struct cxl_port *port; 1724 struct device *dev; 1725 int rc; 1726 1727 if (WARN_ON_ONCE(!cxld)) 1728 return -EINVAL; 1729 1730 if (WARN_ON_ONCE(IS_ERR(cxld))) 1731 return PTR_ERR(cxld); 1732 1733 if (cxld->interleave_ways < 1) 1734 return -EINVAL; 1735 1736 dev = &cxld->dev; 1737 1738 port = to_cxl_port(cxld->dev.parent); 1739 if (!is_endpoint_decoder(dev)) { 1740 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev); 1741 1742 rc = decoder_populate_targets(cxlsd, port, target_map); 1743 if (rc && (cxld->flags & CXL_DECODER_F_ENABLE)) { 1744 dev_err(&port->dev, 1745 "Failed to populate active decoder targets\n"); 1746 return rc; 1747 } 1748 } 1749 1750 rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id); 1751 if (rc) 1752 return rc; 1753 1754 return device_add(dev); 1755 } 1756 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add_locked, CXL); 1757 1758 /** 1759 * cxl_decoder_add - Add a decoder with targets 1760 * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc() 1761 * @target_map: A list of downstream ports that this decoder can direct memory 1762 * traffic to. These numbers should correspond with the port number 1763 * in the PCIe Link Capabilities structure. 1764 * 1765 * This is the unlocked variant of cxl_decoder_add_locked(). 1766 * See cxl_decoder_add_locked(). 1767 * 1768 * Context: Process context. Takes and releases the device lock of the port that 1769 * owns the @cxld. 1770 */ 1771 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map) 1772 { 1773 struct cxl_port *port; 1774 int rc; 1775 1776 if (WARN_ON_ONCE(!cxld)) 1777 return -EINVAL; 1778 1779 if (WARN_ON_ONCE(IS_ERR(cxld))) 1780 return PTR_ERR(cxld); 1781 1782 port = to_cxl_port(cxld->dev.parent); 1783 1784 device_lock(&port->dev); 1785 rc = cxl_decoder_add_locked(cxld, target_map); 1786 device_unlock(&port->dev); 1787 1788 return rc; 1789 } 1790 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, CXL); 1791 1792 static void cxld_unregister(void *dev) 1793 { 1794 struct cxl_endpoint_decoder *cxled; 1795 1796 if (is_endpoint_decoder(dev)) { 1797 cxled = to_cxl_endpoint_decoder(dev); 1798 cxl_decoder_kill_region(cxled); 1799 } 1800 1801 device_unregister(dev); 1802 } 1803 1804 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld) 1805 { 1806 return devm_add_action_or_reset(host, cxld_unregister, &cxld->dev); 1807 } 1808 EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, CXL); 1809 1810 /** 1811 * __cxl_driver_register - register a driver for the cxl bus 1812 * @cxl_drv: cxl driver structure to attach 1813 * @owner: owning module/driver 1814 * @modname: KBUILD_MODNAME for parent driver 1815 */ 1816 int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner, 1817 const char *modname) 1818 { 1819 if (!cxl_drv->probe) { 1820 pr_debug("%s ->probe() must be specified\n", modname); 1821 return -EINVAL; 1822 } 1823 1824 if (!cxl_drv->name) { 1825 pr_debug("%s ->name must be specified\n", modname); 1826 return -EINVAL; 1827 } 1828 1829 if (!cxl_drv->id) { 1830 pr_debug("%s ->id must be specified\n", modname); 1831 return -EINVAL; 1832 } 1833 1834 cxl_drv->drv.bus = &cxl_bus_type; 1835 cxl_drv->drv.owner = owner; 1836 cxl_drv->drv.mod_name = modname; 1837 cxl_drv->drv.name = cxl_drv->name; 1838 1839 return driver_register(&cxl_drv->drv); 1840 } 1841 EXPORT_SYMBOL_NS_GPL(__cxl_driver_register, CXL); 1842 1843 void cxl_driver_unregister(struct cxl_driver *cxl_drv) 1844 { 1845 driver_unregister(&cxl_drv->drv); 1846 } 1847 EXPORT_SYMBOL_NS_GPL(cxl_driver_unregister, CXL); 1848 1849 static int cxl_bus_uevent(const struct device *dev, struct kobj_uevent_env *env) 1850 { 1851 return add_uevent_var(env, "MODALIAS=" CXL_MODALIAS_FMT, 1852 cxl_device_id(dev)); 1853 } 1854 1855 static int cxl_bus_match(struct device *dev, struct device_driver *drv) 1856 { 1857 return cxl_device_id(dev) == to_cxl_drv(drv)->id; 1858 } 1859 1860 static int cxl_bus_probe(struct device *dev) 1861 { 1862 int rc; 1863 1864 rc = to_cxl_drv(dev->driver)->probe(dev); 1865 dev_dbg(dev, "probe: %d\n", rc); 1866 return rc; 1867 } 1868 1869 static void cxl_bus_remove(struct device *dev) 1870 { 1871 struct cxl_driver *cxl_drv = to_cxl_drv(dev->driver); 1872 1873 if (cxl_drv->remove) 1874 cxl_drv->remove(dev); 1875 } 1876 1877 static struct workqueue_struct *cxl_bus_wq; 1878 1879 static void cxl_bus_rescan_queue(struct work_struct *w) 1880 { 1881 int rc = bus_rescan_devices(&cxl_bus_type); 1882 1883 pr_debug("CXL bus rescan result: %d\n", rc); 1884 } 1885 1886 void cxl_bus_rescan(void) 1887 { 1888 static DECLARE_WORK(rescan_work, cxl_bus_rescan_queue); 1889 1890 queue_work(cxl_bus_wq, &rescan_work); 1891 } 1892 EXPORT_SYMBOL_NS_GPL(cxl_bus_rescan, CXL); 1893 1894 void cxl_bus_drain(void) 1895 { 1896 drain_workqueue(cxl_bus_wq); 1897 } 1898 EXPORT_SYMBOL_NS_GPL(cxl_bus_drain, CXL); 1899 1900 bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd) 1901 { 1902 return queue_work(cxl_bus_wq, &cxlmd->detach_work); 1903 } 1904 EXPORT_SYMBOL_NS_GPL(schedule_cxl_memdev_detach, CXL); 1905 1906 /* for user tooling to ensure port disable work has completed */ 1907 static ssize_t flush_store(const struct bus_type *bus, const char *buf, size_t count) 1908 { 1909 if (sysfs_streq(buf, "1")) { 1910 flush_workqueue(cxl_bus_wq); 1911 return count; 1912 } 1913 1914 return -EINVAL; 1915 } 1916 1917 static BUS_ATTR_WO(flush); 1918 1919 static struct attribute *cxl_bus_attributes[] = { 1920 &bus_attr_flush.attr, 1921 NULL, 1922 }; 1923 1924 static struct attribute_group cxl_bus_attribute_group = { 1925 .attrs = cxl_bus_attributes, 1926 }; 1927 1928 static const struct attribute_group *cxl_bus_attribute_groups[] = { 1929 &cxl_bus_attribute_group, 1930 NULL, 1931 }; 1932 1933 struct bus_type cxl_bus_type = { 1934 .name = "cxl", 1935 .uevent = cxl_bus_uevent, 1936 .match = cxl_bus_match, 1937 .probe = cxl_bus_probe, 1938 .remove = cxl_bus_remove, 1939 .bus_groups = cxl_bus_attribute_groups, 1940 }; 1941 EXPORT_SYMBOL_NS_GPL(cxl_bus_type, CXL); 1942 1943 static struct dentry *cxl_debugfs; 1944 1945 struct dentry *cxl_debugfs_create_dir(const char *dir) 1946 { 1947 return debugfs_create_dir(dir, cxl_debugfs); 1948 } 1949 EXPORT_SYMBOL_NS_GPL(cxl_debugfs_create_dir, CXL); 1950 1951 static __init int cxl_core_init(void) 1952 { 1953 int rc; 1954 1955 cxl_debugfs = debugfs_create_dir("cxl", NULL); 1956 1957 cxl_mbox_init(); 1958 1959 rc = cxl_memdev_init(); 1960 if (rc) 1961 return rc; 1962 1963 cxl_bus_wq = alloc_ordered_workqueue("cxl_port", 0); 1964 if (!cxl_bus_wq) { 1965 rc = -ENOMEM; 1966 goto err_wq; 1967 } 1968 1969 rc = bus_register(&cxl_bus_type); 1970 if (rc) 1971 goto err_bus; 1972 1973 rc = cxl_region_init(); 1974 if (rc) 1975 goto err_region; 1976 1977 return 0; 1978 1979 err_region: 1980 bus_unregister(&cxl_bus_type); 1981 err_bus: 1982 destroy_workqueue(cxl_bus_wq); 1983 err_wq: 1984 cxl_memdev_exit(); 1985 return rc; 1986 } 1987 1988 static void cxl_core_exit(void) 1989 { 1990 cxl_region_exit(); 1991 bus_unregister(&cxl_bus_type); 1992 destroy_workqueue(cxl_bus_wq); 1993 cxl_memdev_exit(); 1994 debugfs_remove_recursive(cxl_debugfs); 1995 } 1996 1997 subsys_initcall(cxl_core_init); 1998 module_exit(cxl_core_exit); 1999 MODULE_LICENSE("GPL v2"); 2000