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