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(const 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 (dev->type == CXL_DAX_REGION_TYPE()) 50 return CXL_DEVICE_DAX_REGION; 51 if (is_cxl_port(dev)) { 52 if (is_cxl_root(to_cxl_port(dev))) 53 return CXL_DEVICE_ROOT; 54 return CXL_DEVICE_PORT; 55 } 56 if (is_cxl_memdev(dev)) 57 return CXL_DEVICE_MEMORY_EXPANDER; 58 if (dev->type == CXL_REGION_TYPE()) 59 return CXL_DEVICE_REGION; 60 return 0; 61 } 62 63 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 64 char *buf) 65 { 66 return sysfs_emit(buf, CXL_MODALIAS_FMT "\n", cxl_device_id(dev)); 67 } 68 static DEVICE_ATTR_RO(modalias); 69 70 static struct attribute *cxl_base_attributes[] = { 71 &dev_attr_devtype.attr, 72 &dev_attr_modalias.attr, 73 NULL, 74 }; 75 76 struct attribute_group cxl_base_attribute_group = { 77 .attrs = cxl_base_attributes, 78 }; 79 80 static ssize_t start_show(struct device *dev, struct device_attribute *attr, 81 char *buf) 82 { 83 struct cxl_decoder *cxld = to_cxl_decoder(dev); 84 85 return sysfs_emit(buf, "%#llx\n", cxld->hpa_range.start); 86 } 87 static DEVICE_ATTR_ADMIN_RO(start); 88 89 static ssize_t size_show(struct device *dev, struct device_attribute *attr, 90 char *buf) 91 { 92 struct cxl_decoder *cxld = to_cxl_decoder(dev); 93 94 return sysfs_emit(buf, "%#llx\n", range_len(&cxld->hpa_range)); 95 } 96 static DEVICE_ATTR_RO(size); 97 98 #define CXL_DECODER_FLAG_ATTR(name, flag) \ 99 static ssize_t name##_show(struct device *dev, \ 100 struct device_attribute *attr, char *buf) \ 101 { \ 102 struct cxl_decoder *cxld = to_cxl_decoder(dev); \ 103 \ 104 return sysfs_emit(buf, "%s\n", \ 105 (cxld->flags & (flag)) ? "1" : "0"); \ 106 } \ 107 static DEVICE_ATTR_RO(name) 108 109 CXL_DECODER_FLAG_ATTR(cap_pmem, CXL_DECODER_F_PMEM); 110 CXL_DECODER_FLAG_ATTR(cap_ram, CXL_DECODER_F_RAM); 111 CXL_DECODER_FLAG_ATTR(cap_type2, CXL_DECODER_F_TYPE2); 112 CXL_DECODER_FLAG_ATTR(cap_type3, CXL_DECODER_F_TYPE3); 113 CXL_DECODER_FLAG_ATTR(locked, CXL_DECODER_F_LOCK); 114 115 static ssize_t target_type_show(struct device *dev, 116 struct device_attribute *attr, char *buf) 117 { 118 struct cxl_decoder *cxld = to_cxl_decoder(dev); 119 120 switch (cxld->target_type) { 121 case CXL_DECODER_ACCELERATOR: 122 return sysfs_emit(buf, "accelerator\n"); 123 case CXL_DECODER_EXPANDER: 124 return sysfs_emit(buf, "expander\n"); 125 } 126 return -ENXIO; 127 } 128 static DEVICE_ATTR_RO(target_type); 129 130 static ssize_t emit_target_list(struct cxl_switch_decoder *cxlsd, char *buf) 131 { 132 struct cxl_decoder *cxld = &cxlsd->cxld; 133 ssize_t offset = 0; 134 int i, rc = 0; 135 136 for (i = 0; i < cxld->interleave_ways; i++) { 137 struct cxl_dport *dport = cxlsd->target[i]; 138 struct cxl_dport *next = NULL; 139 140 if (!dport) 141 break; 142 143 if (i + 1 < cxld->interleave_ways) 144 next = cxlsd->target[i + 1]; 145 rc = sysfs_emit_at(buf, offset, "%d%s", dport->port_id, 146 next ? "," : ""); 147 if (rc < 0) 148 return rc; 149 offset += rc; 150 } 151 152 return offset; 153 } 154 155 static ssize_t target_list_show(struct device *dev, 156 struct device_attribute *attr, char *buf) 157 { 158 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev); 159 ssize_t offset; 160 unsigned int seq; 161 int rc; 162 163 do { 164 seq = read_seqbegin(&cxlsd->target_lock); 165 rc = emit_target_list(cxlsd, buf); 166 } while (read_seqretry(&cxlsd->target_lock, seq)); 167 168 if (rc < 0) 169 return rc; 170 offset = rc; 171 172 rc = sysfs_emit_at(buf, offset, "\n"); 173 if (rc < 0) 174 return rc; 175 176 return offset + rc; 177 } 178 static DEVICE_ATTR_RO(target_list); 179 180 static ssize_t mode_show(struct device *dev, struct device_attribute *attr, 181 char *buf) 182 { 183 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 184 185 return sysfs_emit(buf, "%s\n", cxl_decoder_mode_name(cxled->mode)); 186 } 187 188 static ssize_t mode_store(struct device *dev, struct device_attribute *attr, 189 const char *buf, size_t len) 190 { 191 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 192 enum cxl_decoder_mode mode; 193 ssize_t rc; 194 195 if (sysfs_streq(buf, "pmem")) 196 mode = CXL_DECODER_PMEM; 197 else if (sysfs_streq(buf, "ram")) 198 mode = CXL_DECODER_RAM; 199 else 200 return -EINVAL; 201 202 rc = cxl_dpa_set_mode(cxled, mode); 203 if (rc) 204 return rc; 205 206 return len; 207 } 208 static DEVICE_ATTR_RW(mode); 209 210 static ssize_t dpa_resource_show(struct device *dev, struct device_attribute *attr, 211 char *buf) 212 { 213 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 214 u64 base = cxl_dpa_resource_start(cxled); 215 216 return sysfs_emit(buf, "%#llx\n", base); 217 } 218 static DEVICE_ATTR_RO(dpa_resource); 219 220 static ssize_t dpa_size_show(struct device *dev, struct device_attribute *attr, 221 char *buf) 222 { 223 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 224 resource_size_t size = cxl_dpa_size(cxled); 225 226 return sysfs_emit(buf, "%pa\n", &size); 227 } 228 229 static ssize_t dpa_size_store(struct device *dev, struct device_attribute *attr, 230 const char *buf, size_t len) 231 { 232 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 233 unsigned long long size; 234 ssize_t rc; 235 236 rc = kstrtoull(buf, 0, &size); 237 if (rc) 238 return rc; 239 240 if (!IS_ALIGNED(size, SZ_256M)) 241 return -EINVAL; 242 243 rc = cxl_dpa_free(cxled); 244 if (rc) 245 return rc; 246 247 if (size == 0) 248 return len; 249 250 rc = cxl_dpa_alloc(cxled, size); 251 if (rc) 252 return rc; 253 254 return len; 255 } 256 static DEVICE_ATTR_RW(dpa_size); 257 258 static ssize_t interleave_granularity_show(struct device *dev, 259 struct device_attribute *attr, 260 char *buf) 261 { 262 struct cxl_decoder *cxld = to_cxl_decoder(dev); 263 264 return sysfs_emit(buf, "%d\n", cxld->interleave_granularity); 265 } 266 267 static DEVICE_ATTR_RO(interleave_granularity); 268 269 static ssize_t interleave_ways_show(struct device *dev, 270 struct device_attribute *attr, char *buf) 271 { 272 struct cxl_decoder *cxld = to_cxl_decoder(dev); 273 274 return sysfs_emit(buf, "%d\n", cxld->interleave_ways); 275 } 276 277 static DEVICE_ATTR_RO(interleave_ways); 278 279 static struct attribute *cxl_decoder_base_attrs[] = { 280 &dev_attr_start.attr, 281 &dev_attr_size.attr, 282 &dev_attr_locked.attr, 283 &dev_attr_interleave_granularity.attr, 284 &dev_attr_interleave_ways.attr, 285 NULL, 286 }; 287 288 static struct attribute_group cxl_decoder_base_attribute_group = { 289 .attrs = cxl_decoder_base_attrs, 290 }; 291 292 static struct attribute *cxl_decoder_root_attrs[] = { 293 &dev_attr_cap_pmem.attr, 294 &dev_attr_cap_ram.attr, 295 &dev_attr_cap_type2.attr, 296 &dev_attr_cap_type3.attr, 297 &dev_attr_target_list.attr, 298 SET_CXL_REGION_ATTR(create_pmem_region) 299 SET_CXL_REGION_ATTR(create_ram_region) 300 SET_CXL_REGION_ATTR(delete_region) 301 NULL, 302 }; 303 304 static bool can_create_pmem(struct cxl_root_decoder *cxlrd) 305 { 306 unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_PMEM; 307 308 return (cxlrd->cxlsd.cxld.flags & flags) == flags; 309 } 310 311 static bool can_create_ram(struct cxl_root_decoder *cxlrd) 312 { 313 unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_RAM; 314 315 return (cxlrd->cxlsd.cxld.flags & flags) == flags; 316 } 317 318 static umode_t cxl_root_decoder_visible(struct kobject *kobj, struct attribute *a, int n) 319 { 320 struct device *dev = kobj_to_dev(kobj); 321 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 322 323 if (a == CXL_REGION_ATTR(create_pmem_region) && !can_create_pmem(cxlrd)) 324 return 0; 325 326 if (a == CXL_REGION_ATTR(create_ram_region) && !can_create_ram(cxlrd)) 327 return 0; 328 329 if (a == CXL_REGION_ATTR(delete_region) && 330 !(can_create_pmem(cxlrd) || can_create_ram(cxlrd))) 331 return 0; 332 333 return a->mode; 334 } 335 336 static struct attribute_group cxl_decoder_root_attribute_group = { 337 .attrs = cxl_decoder_root_attrs, 338 .is_visible = cxl_root_decoder_visible, 339 }; 340 341 static const struct attribute_group *cxl_decoder_root_attribute_groups[] = { 342 &cxl_decoder_root_attribute_group, 343 &cxl_decoder_base_attribute_group, 344 &cxl_base_attribute_group, 345 NULL, 346 }; 347 348 static struct attribute *cxl_decoder_switch_attrs[] = { 349 &dev_attr_target_type.attr, 350 &dev_attr_target_list.attr, 351 SET_CXL_REGION_ATTR(region) 352 NULL, 353 }; 354 355 static struct attribute_group cxl_decoder_switch_attribute_group = { 356 .attrs = cxl_decoder_switch_attrs, 357 }; 358 359 static const struct attribute_group *cxl_decoder_switch_attribute_groups[] = { 360 &cxl_decoder_switch_attribute_group, 361 &cxl_decoder_base_attribute_group, 362 &cxl_base_attribute_group, 363 NULL, 364 }; 365 366 static struct attribute *cxl_decoder_endpoint_attrs[] = { 367 &dev_attr_target_type.attr, 368 &dev_attr_mode.attr, 369 &dev_attr_dpa_size.attr, 370 &dev_attr_dpa_resource.attr, 371 SET_CXL_REGION_ATTR(region) 372 NULL, 373 }; 374 375 static struct attribute_group cxl_decoder_endpoint_attribute_group = { 376 .attrs = cxl_decoder_endpoint_attrs, 377 }; 378 379 static const struct attribute_group *cxl_decoder_endpoint_attribute_groups[] = { 380 &cxl_decoder_base_attribute_group, 381 &cxl_decoder_endpoint_attribute_group, 382 &cxl_base_attribute_group, 383 NULL, 384 }; 385 386 static void __cxl_decoder_release(struct cxl_decoder *cxld) 387 { 388 struct cxl_port *port = to_cxl_port(cxld->dev.parent); 389 390 ida_free(&port->decoder_ida, cxld->id); 391 put_device(&port->dev); 392 } 393 394 static void cxl_endpoint_decoder_release(struct device *dev) 395 { 396 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev); 397 398 __cxl_decoder_release(&cxled->cxld); 399 kfree(cxled); 400 } 401 402 static void cxl_switch_decoder_release(struct device *dev) 403 { 404 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev); 405 406 __cxl_decoder_release(&cxlsd->cxld); 407 kfree(cxlsd); 408 } 409 410 struct cxl_root_decoder *to_cxl_root_decoder(struct device *dev) 411 { 412 if (dev_WARN_ONCE(dev, !is_root_decoder(dev), 413 "not a cxl_root_decoder device\n")) 414 return NULL; 415 return container_of(dev, struct cxl_root_decoder, cxlsd.cxld.dev); 416 } 417 EXPORT_SYMBOL_NS_GPL(to_cxl_root_decoder, CXL); 418 419 static void cxl_root_decoder_release(struct device *dev) 420 { 421 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 422 423 if (atomic_read(&cxlrd->region_id) >= 0) 424 memregion_free(atomic_read(&cxlrd->region_id)); 425 __cxl_decoder_release(&cxlrd->cxlsd.cxld); 426 kfree(cxlrd); 427 } 428 429 static const struct device_type cxl_decoder_endpoint_type = { 430 .name = "cxl_decoder_endpoint", 431 .release = cxl_endpoint_decoder_release, 432 .groups = cxl_decoder_endpoint_attribute_groups, 433 }; 434 435 static const struct device_type cxl_decoder_switch_type = { 436 .name = "cxl_decoder_switch", 437 .release = cxl_switch_decoder_release, 438 .groups = cxl_decoder_switch_attribute_groups, 439 }; 440 441 static const struct device_type cxl_decoder_root_type = { 442 .name = "cxl_decoder_root", 443 .release = cxl_root_decoder_release, 444 .groups = cxl_decoder_root_attribute_groups, 445 }; 446 447 bool is_endpoint_decoder(struct device *dev) 448 { 449 return dev->type == &cxl_decoder_endpoint_type; 450 } 451 EXPORT_SYMBOL_NS_GPL(is_endpoint_decoder, CXL); 452 453 bool is_root_decoder(struct device *dev) 454 { 455 return dev->type == &cxl_decoder_root_type; 456 } 457 EXPORT_SYMBOL_NS_GPL(is_root_decoder, CXL); 458 459 bool is_switch_decoder(struct device *dev) 460 { 461 return is_root_decoder(dev) || dev->type == &cxl_decoder_switch_type; 462 } 463 EXPORT_SYMBOL_NS_GPL(is_switch_decoder, CXL); 464 465 struct cxl_decoder *to_cxl_decoder(struct device *dev) 466 { 467 if (dev_WARN_ONCE(dev, 468 !is_switch_decoder(dev) && !is_endpoint_decoder(dev), 469 "not a cxl_decoder device\n")) 470 return NULL; 471 return container_of(dev, struct cxl_decoder, dev); 472 } 473 EXPORT_SYMBOL_NS_GPL(to_cxl_decoder, CXL); 474 475 struct cxl_endpoint_decoder *to_cxl_endpoint_decoder(struct device *dev) 476 { 477 if (dev_WARN_ONCE(dev, !is_endpoint_decoder(dev), 478 "not a cxl_endpoint_decoder device\n")) 479 return NULL; 480 return container_of(dev, struct cxl_endpoint_decoder, cxld.dev); 481 } 482 EXPORT_SYMBOL_NS_GPL(to_cxl_endpoint_decoder, CXL); 483 484 struct cxl_switch_decoder *to_cxl_switch_decoder(struct device *dev) 485 { 486 if (dev_WARN_ONCE(dev, !is_switch_decoder(dev), 487 "not a cxl_switch_decoder device\n")) 488 return NULL; 489 return container_of(dev, struct cxl_switch_decoder, cxld.dev); 490 } 491 EXPORT_SYMBOL_NS_GPL(to_cxl_switch_decoder, CXL); 492 493 static void cxl_ep_release(struct cxl_ep *ep) 494 { 495 put_device(ep->ep); 496 kfree(ep); 497 } 498 499 static void cxl_ep_remove(struct cxl_port *port, struct cxl_ep *ep) 500 { 501 if (!ep) 502 return; 503 xa_erase(&port->endpoints, (unsigned long) ep->ep); 504 cxl_ep_release(ep); 505 } 506 507 static void cxl_port_release(struct device *dev) 508 { 509 struct cxl_port *port = to_cxl_port(dev); 510 unsigned long index; 511 struct cxl_ep *ep; 512 513 xa_for_each(&port->endpoints, index, ep) 514 cxl_ep_remove(port, ep); 515 xa_destroy(&port->endpoints); 516 xa_destroy(&port->dports); 517 xa_destroy(&port->regions); 518 ida_free(&cxl_port_ida, port->id); 519 kfree(port); 520 } 521 522 static const struct attribute_group *cxl_port_attribute_groups[] = { 523 &cxl_base_attribute_group, 524 NULL, 525 }; 526 527 static const struct device_type cxl_port_type = { 528 .name = "cxl_port", 529 .release = cxl_port_release, 530 .groups = cxl_port_attribute_groups, 531 }; 532 533 bool is_cxl_port(const struct device *dev) 534 { 535 return dev->type == &cxl_port_type; 536 } 537 EXPORT_SYMBOL_NS_GPL(is_cxl_port, CXL); 538 539 struct cxl_port *to_cxl_port(const struct device *dev) 540 { 541 if (dev_WARN_ONCE(dev, dev->type != &cxl_port_type, 542 "not a cxl_port device\n")) 543 return NULL; 544 return container_of(dev, struct cxl_port, dev); 545 } 546 EXPORT_SYMBOL_NS_GPL(to_cxl_port, CXL); 547 548 static void unregister_port(void *_port) 549 { 550 struct cxl_port *port = _port; 551 struct cxl_port *parent; 552 struct device *lock_dev; 553 554 if (is_cxl_root(port)) 555 parent = NULL; 556 else 557 parent = to_cxl_port(port->dev.parent); 558 559 /* 560 * CXL root port's and the first level of ports are unregistered 561 * under the platform firmware device lock, all other ports are 562 * unregistered while holding their parent port lock. 563 */ 564 if (!parent) 565 lock_dev = port->uport; 566 else if (is_cxl_root(parent)) 567 lock_dev = parent->uport; 568 else 569 lock_dev = &parent->dev; 570 571 device_lock_assert(lock_dev); 572 port->dead = true; 573 device_unregister(&port->dev); 574 } 575 576 static void cxl_unlink_uport(void *_port) 577 { 578 struct cxl_port *port = _port; 579 580 sysfs_remove_link(&port->dev.kobj, "uport"); 581 } 582 583 static int devm_cxl_link_uport(struct device *host, struct cxl_port *port) 584 { 585 int rc; 586 587 rc = sysfs_create_link(&port->dev.kobj, &port->uport->kobj, "uport"); 588 if (rc) 589 return rc; 590 return devm_add_action_or_reset(host, cxl_unlink_uport, port); 591 } 592 593 static void cxl_unlink_parent_dport(void *_port) 594 { 595 struct cxl_port *port = _port; 596 597 sysfs_remove_link(&port->dev.kobj, "parent_dport"); 598 } 599 600 static int devm_cxl_link_parent_dport(struct device *host, 601 struct cxl_port *port, 602 struct cxl_dport *parent_dport) 603 { 604 int rc; 605 606 if (!parent_dport) 607 return 0; 608 609 rc = sysfs_create_link(&port->dev.kobj, &parent_dport->dport->kobj, 610 "parent_dport"); 611 if (rc) 612 return rc; 613 return devm_add_action_or_reset(host, cxl_unlink_parent_dport, port); 614 } 615 616 static struct lock_class_key cxl_port_key; 617 618 static struct cxl_port *cxl_port_alloc(struct device *uport, 619 resource_size_t component_reg_phys, 620 struct cxl_dport *parent_dport) 621 { 622 struct cxl_port *port; 623 struct device *dev; 624 int rc; 625 626 port = kzalloc(sizeof(*port), GFP_KERNEL); 627 if (!port) 628 return ERR_PTR(-ENOMEM); 629 630 rc = ida_alloc(&cxl_port_ida, GFP_KERNEL); 631 if (rc < 0) 632 goto err; 633 port->id = rc; 634 port->uport = uport; 635 636 /* 637 * The top-level cxl_port "cxl_root" does not have a cxl_port as 638 * its parent and it does not have any corresponding component 639 * registers as its decode is described by a fixed platform 640 * description. 641 */ 642 dev = &port->dev; 643 if (parent_dport) { 644 struct cxl_port *parent_port = parent_dport->port; 645 struct cxl_port *iter; 646 647 dev->parent = &parent_port->dev; 648 port->depth = parent_port->depth + 1; 649 port->parent_dport = parent_dport; 650 651 /* 652 * walk to the host bridge, or the first ancestor that knows 653 * the host bridge 654 */ 655 iter = port; 656 while (!iter->host_bridge && 657 !is_cxl_root(to_cxl_port(iter->dev.parent))) 658 iter = to_cxl_port(iter->dev.parent); 659 if (iter->host_bridge) 660 port->host_bridge = iter->host_bridge; 661 else if (parent_dport->rch) 662 port->host_bridge = parent_dport->dport; 663 else 664 port->host_bridge = iter->uport; 665 dev_dbg(uport, "host-bridge: %s\n", dev_name(port->host_bridge)); 666 } else 667 dev->parent = uport; 668 669 port->component_reg_phys = component_reg_phys; 670 ida_init(&port->decoder_ida); 671 port->hdm_end = -1; 672 port->commit_end = -1; 673 xa_init(&port->dports); 674 xa_init(&port->endpoints); 675 xa_init(&port->regions); 676 677 device_initialize(dev); 678 lockdep_set_class_and_subclass(&dev->mutex, &cxl_port_key, port->depth); 679 device_set_pm_not_required(dev); 680 dev->bus = &cxl_bus_type; 681 dev->type = &cxl_port_type; 682 683 return port; 684 685 err: 686 kfree(port); 687 return ERR_PTR(rc); 688 } 689 690 static struct cxl_port *__devm_cxl_add_port(struct device *host, 691 struct device *uport, 692 resource_size_t component_reg_phys, 693 struct cxl_dport *parent_dport) 694 { 695 struct cxl_port *port; 696 struct device *dev; 697 int rc; 698 699 port = cxl_port_alloc(uport, component_reg_phys, parent_dport); 700 if (IS_ERR(port)) 701 return port; 702 703 dev = &port->dev; 704 if (is_cxl_memdev(uport)) 705 rc = dev_set_name(dev, "endpoint%d", port->id); 706 else if (parent_dport) 707 rc = dev_set_name(dev, "port%d", port->id); 708 else 709 rc = dev_set_name(dev, "root%d", port->id); 710 if (rc) 711 goto err; 712 713 rc = device_add(dev); 714 if (rc) 715 goto err; 716 717 rc = devm_add_action_or_reset(host, unregister_port, port); 718 if (rc) 719 return ERR_PTR(rc); 720 721 rc = devm_cxl_link_uport(host, port); 722 if (rc) 723 return ERR_PTR(rc); 724 725 rc = devm_cxl_link_parent_dport(host, port, parent_dport); 726 if (rc) 727 return ERR_PTR(rc); 728 729 return port; 730 731 err: 732 put_device(dev); 733 return ERR_PTR(rc); 734 } 735 736 /** 737 * devm_cxl_add_port - register a cxl_port in CXL memory decode hierarchy 738 * @host: host device for devm operations 739 * @uport: "physical" device implementing this upstream port 740 * @component_reg_phys: (optional) for configurable cxl_port instances 741 * @parent_dport: next hop up in the CXL memory decode hierarchy 742 */ 743 struct cxl_port *devm_cxl_add_port(struct device *host, struct device *uport, 744 resource_size_t component_reg_phys, 745 struct cxl_dport *parent_dport) 746 { 747 struct cxl_port *port, *parent_port; 748 749 port = __devm_cxl_add_port(host, uport, component_reg_phys, 750 parent_dport); 751 752 parent_port = parent_dport ? parent_dport->port : NULL; 753 if (IS_ERR(port)) { 754 dev_dbg(uport, "Failed to add %s%s%s%s: %ld\n", 755 dev_name(&port->dev), 756 parent_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 = dev_get_drvdata(&cxlmd->dev); 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 device_unlock(parent); 1182 put_device(parent); 1183 out: 1184 put_device(&endpoint->dev); 1185 } 1186 1187 int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint) 1188 { 1189 struct device *dev = &cxlmd->dev; 1190 1191 get_device(&endpoint->dev); 1192 dev_set_drvdata(dev, endpoint); 1193 cxlmd->depth = endpoint->depth; 1194 return devm_add_action_or_reset(dev, delete_endpoint, cxlmd); 1195 } 1196 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_autoremove, CXL); 1197 1198 /* 1199 * The natural end of life of a non-root 'cxl_port' is when its parent port goes 1200 * through a ->remove() event ("top-down" unregistration). The unnatural trigger 1201 * for a port to be unregistered is when all memdevs beneath that port have gone 1202 * through ->remove(). This "bottom-up" removal selectively removes individual 1203 * child ports manually. This depends on devm_cxl_add_port() to not change is 1204 * devm action registration order, and for dports to have already been 1205 * destroyed by reap_dports(). 1206 */ 1207 static void delete_switch_port(struct cxl_port *port) 1208 { 1209 devm_release_action(port->dev.parent, cxl_unlink_parent_dport, port); 1210 devm_release_action(port->dev.parent, cxl_unlink_uport, port); 1211 devm_release_action(port->dev.parent, unregister_port, port); 1212 } 1213 1214 static void reap_dports(struct cxl_port *port) 1215 { 1216 struct cxl_dport *dport; 1217 unsigned long index; 1218 1219 device_lock_assert(&port->dev); 1220 1221 xa_for_each(&port->dports, index, dport) { 1222 devm_release_action(&port->dev, cxl_dport_unlink, dport); 1223 devm_release_action(&port->dev, cxl_dport_remove, dport); 1224 devm_kfree(&port->dev, dport); 1225 } 1226 } 1227 1228 struct detach_ctx { 1229 struct cxl_memdev *cxlmd; 1230 int depth; 1231 }; 1232 1233 static int port_has_memdev(struct device *dev, const void *data) 1234 { 1235 const struct detach_ctx *ctx = data; 1236 struct cxl_port *port; 1237 1238 if (!is_cxl_port(dev)) 1239 return 0; 1240 1241 port = to_cxl_port(dev); 1242 if (port->depth != ctx->depth) 1243 return 0; 1244 1245 return !!cxl_ep_load(port, ctx->cxlmd); 1246 } 1247 1248 static void cxl_detach_ep(void *data) 1249 { 1250 struct cxl_memdev *cxlmd = data; 1251 1252 for (int i = cxlmd->depth - 1; i >= 1; i--) { 1253 struct cxl_port *port, *parent_port; 1254 struct detach_ctx ctx = { 1255 .cxlmd = cxlmd, 1256 .depth = i, 1257 }; 1258 struct device *dev; 1259 struct cxl_ep *ep; 1260 bool died = false; 1261 1262 dev = bus_find_device(&cxl_bus_type, NULL, &ctx, 1263 port_has_memdev); 1264 if (!dev) 1265 continue; 1266 port = to_cxl_port(dev); 1267 1268 parent_port = to_cxl_port(port->dev.parent); 1269 device_lock(&parent_port->dev); 1270 device_lock(&port->dev); 1271 ep = cxl_ep_load(port, cxlmd); 1272 dev_dbg(&cxlmd->dev, "disconnect %s from %s\n", 1273 ep ? dev_name(ep->ep) : "", dev_name(&port->dev)); 1274 cxl_ep_remove(port, ep); 1275 if (ep && !port->dead && xa_empty(&port->endpoints) && 1276 !is_cxl_root(parent_port) && parent_port->dev.driver) { 1277 /* 1278 * This was the last ep attached to a dynamically 1279 * enumerated port. Block new cxl_add_ep() and garbage 1280 * collect the port. 1281 */ 1282 died = true; 1283 port->dead = true; 1284 reap_dports(port); 1285 } 1286 device_unlock(&port->dev); 1287 1288 if (died) { 1289 dev_dbg(&cxlmd->dev, "delete %s\n", 1290 dev_name(&port->dev)); 1291 delete_switch_port(port); 1292 } 1293 put_device(&port->dev); 1294 device_unlock(&parent_port->dev); 1295 } 1296 } 1297 1298 static resource_size_t find_component_registers(struct device *dev) 1299 { 1300 struct cxl_register_map map; 1301 struct pci_dev *pdev; 1302 1303 /* 1304 * Theoretically, CXL component registers can be hosted on a 1305 * non-PCI device, in practice, only cxl_test hits this case. 1306 */ 1307 if (!dev_is_pci(dev)) 1308 return CXL_RESOURCE_NONE; 1309 1310 pdev = to_pci_dev(dev); 1311 1312 cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map); 1313 return map.resource; 1314 } 1315 1316 static int add_port_attach_ep(struct cxl_memdev *cxlmd, 1317 struct device *uport_dev, 1318 struct device *dport_dev) 1319 { 1320 struct device *dparent = grandparent(dport_dev); 1321 struct cxl_port *port, *parent_port = NULL; 1322 struct cxl_dport *dport, *parent_dport; 1323 resource_size_t component_reg_phys; 1324 int rc; 1325 1326 if (!dparent) { 1327 /* 1328 * The iteration reached the topology root without finding the 1329 * CXL-root 'cxl_port' on a previous iteration, fail for now to 1330 * be re-probed after platform driver attaches. 1331 */ 1332 dev_dbg(&cxlmd->dev, "%s is a root dport\n", 1333 dev_name(dport_dev)); 1334 return -ENXIO; 1335 } 1336 1337 parent_port = find_cxl_port(dparent, &parent_dport); 1338 if (!parent_port) { 1339 /* iterate to create this parent_port */ 1340 return -EAGAIN; 1341 } 1342 1343 device_lock(&parent_port->dev); 1344 if (!parent_port->dev.driver) { 1345 dev_warn(&cxlmd->dev, 1346 "port %s:%s disabled, failed to enumerate CXL.mem\n", 1347 dev_name(&parent_port->dev), dev_name(uport_dev)); 1348 port = ERR_PTR(-ENXIO); 1349 goto out; 1350 } 1351 1352 port = find_cxl_port_at(parent_port, dport_dev, &dport); 1353 if (!port) { 1354 component_reg_phys = find_component_registers(uport_dev); 1355 port = devm_cxl_add_port(&parent_port->dev, uport_dev, 1356 component_reg_phys, parent_dport); 1357 /* retry find to pick up the new dport information */ 1358 if (!IS_ERR(port)) 1359 port = find_cxl_port_at(parent_port, dport_dev, &dport); 1360 } 1361 out: 1362 device_unlock(&parent_port->dev); 1363 1364 if (IS_ERR(port)) 1365 rc = PTR_ERR(port); 1366 else { 1367 dev_dbg(&cxlmd->dev, "add to new port %s:%s\n", 1368 dev_name(&port->dev), dev_name(port->uport)); 1369 rc = cxl_add_ep(dport, &cxlmd->dev); 1370 if (rc == -EBUSY) { 1371 /* 1372 * "can't" happen, but this error code means 1373 * something to the caller, so translate it. 1374 */ 1375 rc = -ENXIO; 1376 } 1377 put_device(&port->dev); 1378 } 1379 1380 put_device(&parent_port->dev); 1381 return rc; 1382 } 1383 1384 int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd) 1385 { 1386 struct device *dev = &cxlmd->dev; 1387 struct device *iter; 1388 int rc; 1389 1390 /* 1391 * Skip intermediate port enumeration in the RCH case, there 1392 * are no ports in between a host bridge and an endpoint. 1393 */ 1394 if (cxlmd->cxlds->rcd) 1395 return 0; 1396 1397 rc = devm_add_action_or_reset(&cxlmd->dev, cxl_detach_ep, cxlmd); 1398 if (rc) 1399 return rc; 1400 1401 /* 1402 * Scan for and add all cxl_ports in this device's ancestry. 1403 * Repeat until no more ports are added. Abort if a port add 1404 * attempt fails. 1405 */ 1406 retry: 1407 for (iter = dev; iter; iter = grandparent(iter)) { 1408 struct device *dport_dev = grandparent(iter); 1409 struct device *uport_dev; 1410 struct cxl_dport *dport; 1411 struct cxl_port *port; 1412 1413 if (!dport_dev) 1414 return 0; 1415 1416 uport_dev = dport_dev->parent; 1417 if (!uport_dev) { 1418 dev_warn(dev, "at %s no parent for dport: %s\n", 1419 dev_name(iter), dev_name(dport_dev)); 1420 return -ENXIO; 1421 } 1422 1423 dev_dbg(dev, "scan: iter: %s dport_dev: %s parent: %s\n", 1424 dev_name(iter), dev_name(dport_dev), 1425 dev_name(uport_dev)); 1426 port = find_cxl_port(dport_dev, &dport); 1427 if (port) { 1428 dev_dbg(&cxlmd->dev, 1429 "found already registered port %s:%s\n", 1430 dev_name(&port->dev), dev_name(port->uport)); 1431 rc = cxl_add_ep(dport, &cxlmd->dev); 1432 1433 /* 1434 * If the endpoint already exists in the port's list, 1435 * that's ok, it was added on a previous pass. 1436 * Otherwise, retry in add_port_attach_ep() after taking 1437 * the parent_port lock as the current port may be being 1438 * reaped. 1439 */ 1440 if (rc && rc != -EBUSY) { 1441 put_device(&port->dev); 1442 return rc; 1443 } 1444 1445 /* Any more ports to add between this one and the root? */ 1446 if (!dev_is_cxl_root_child(&port->dev)) { 1447 put_device(&port->dev); 1448 continue; 1449 } 1450 1451 put_device(&port->dev); 1452 return 0; 1453 } 1454 1455 rc = add_port_attach_ep(cxlmd, uport_dev, dport_dev); 1456 /* port missing, try to add parent */ 1457 if (rc == -EAGAIN) 1458 continue; 1459 /* failed to add ep or port */ 1460 if (rc) 1461 return rc; 1462 /* port added, new descendants possible, start over */ 1463 goto retry; 1464 } 1465 1466 return 0; 1467 } 1468 EXPORT_SYMBOL_NS_GPL(devm_cxl_enumerate_ports, CXL); 1469 1470 struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd, 1471 struct cxl_dport **dport) 1472 { 1473 return find_cxl_port(grandparent(&cxlmd->dev), dport); 1474 } 1475 EXPORT_SYMBOL_NS_GPL(cxl_mem_find_port, CXL); 1476 1477 static int decoder_populate_targets(struct cxl_switch_decoder *cxlsd, 1478 struct cxl_port *port, int *target_map) 1479 { 1480 int i, rc = 0; 1481 1482 if (!target_map) 1483 return 0; 1484 1485 device_lock_assert(&port->dev); 1486 1487 if (xa_empty(&port->dports)) 1488 return -EINVAL; 1489 1490 write_seqlock(&cxlsd->target_lock); 1491 for (i = 0; i < cxlsd->nr_targets; i++) { 1492 struct cxl_dport *dport = find_dport(port, target_map[i]); 1493 1494 if (!dport) { 1495 rc = -ENXIO; 1496 break; 1497 } 1498 cxlsd->target[i] = dport; 1499 } 1500 write_sequnlock(&cxlsd->target_lock); 1501 1502 return rc; 1503 } 1504 1505 struct cxl_dport *cxl_hb_modulo(struct cxl_root_decoder *cxlrd, int pos) 1506 { 1507 struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd; 1508 struct cxl_decoder *cxld = &cxlsd->cxld; 1509 int iw; 1510 1511 iw = cxld->interleave_ways; 1512 if (dev_WARN_ONCE(&cxld->dev, iw != cxlsd->nr_targets, 1513 "misconfigured root decoder\n")) 1514 return NULL; 1515 1516 return cxlrd->cxlsd.target[pos % iw]; 1517 } 1518 EXPORT_SYMBOL_NS_GPL(cxl_hb_modulo, CXL); 1519 1520 static struct lock_class_key cxl_decoder_key; 1521 1522 /** 1523 * cxl_decoder_init - Common decoder setup / initialization 1524 * @port: owning port of this decoder 1525 * @cxld: common decoder properties to initialize 1526 * 1527 * A port may contain one or more decoders. Each of those decoders 1528 * enable some address space for CXL.mem utilization. A decoder is 1529 * expected to be configured by the caller before registering via 1530 * cxl_decoder_add() 1531 */ 1532 static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld) 1533 { 1534 struct device *dev; 1535 int rc; 1536 1537 rc = ida_alloc(&port->decoder_ida, GFP_KERNEL); 1538 if (rc < 0) 1539 return rc; 1540 1541 /* need parent to stick around to release the id */ 1542 get_device(&port->dev); 1543 cxld->id = rc; 1544 1545 dev = &cxld->dev; 1546 device_initialize(dev); 1547 lockdep_set_class(&dev->mutex, &cxl_decoder_key); 1548 device_set_pm_not_required(dev); 1549 dev->parent = &port->dev; 1550 dev->bus = &cxl_bus_type; 1551 1552 /* Pre initialize an "empty" decoder */ 1553 cxld->interleave_ways = 1; 1554 cxld->interleave_granularity = PAGE_SIZE; 1555 cxld->target_type = CXL_DECODER_EXPANDER; 1556 cxld->hpa_range = (struct range) { 1557 .start = 0, 1558 .end = -1, 1559 }; 1560 1561 return 0; 1562 } 1563 1564 static int cxl_switch_decoder_init(struct cxl_port *port, 1565 struct cxl_switch_decoder *cxlsd, 1566 int nr_targets) 1567 { 1568 if (nr_targets > CXL_DECODER_MAX_INTERLEAVE) 1569 return -EINVAL; 1570 1571 cxlsd->nr_targets = nr_targets; 1572 seqlock_init(&cxlsd->target_lock); 1573 return cxl_decoder_init(port, &cxlsd->cxld); 1574 } 1575 1576 /** 1577 * cxl_root_decoder_alloc - Allocate a root level decoder 1578 * @port: owning CXL root of this decoder 1579 * @nr_targets: static number of downstream targets 1580 * @calc_hb: which host bridge covers the n'th position by granularity 1581 * 1582 * Return: A new cxl decoder to be registered by cxl_decoder_add(). A 1583 * 'CXL root' decoder is one that decodes from a top-level / static platform 1584 * firmware description of CXL resources into a CXL standard decode 1585 * topology. 1586 */ 1587 struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port, 1588 unsigned int nr_targets, 1589 cxl_calc_hb_fn calc_hb) 1590 { 1591 struct cxl_root_decoder *cxlrd; 1592 struct cxl_switch_decoder *cxlsd; 1593 struct cxl_decoder *cxld; 1594 int rc; 1595 1596 if (!is_cxl_root(port)) 1597 return ERR_PTR(-EINVAL); 1598 1599 cxlrd = kzalloc(struct_size(cxlrd, cxlsd.target, nr_targets), 1600 GFP_KERNEL); 1601 if (!cxlrd) 1602 return ERR_PTR(-ENOMEM); 1603 1604 cxlsd = &cxlrd->cxlsd; 1605 rc = cxl_switch_decoder_init(port, cxlsd, nr_targets); 1606 if (rc) { 1607 kfree(cxlrd); 1608 return ERR_PTR(rc); 1609 } 1610 1611 cxlrd->calc_hb = calc_hb; 1612 mutex_init(&cxlrd->range_lock); 1613 1614 cxld = &cxlsd->cxld; 1615 cxld->dev.type = &cxl_decoder_root_type; 1616 /* 1617 * cxl_root_decoder_release() special cases negative ids to 1618 * detect memregion_alloc() failures. 1619 */ 1620 atomic_set(&cxlrd->region_id, -1); 1621 rc = memregion_alloc(GFP_KERNEL); 1622 if (rc < 0) { 1623 put_device(&cxld->dev); 1624 return ERR_PTR(rc); 1625 } 1626 1627 atomic_set(&cxlrd->region_id, rc); 1628 return cxlrd; 1629 } 1630 EXPORT_SYMBOL_NS_GPL(cxl_root_decoder_alloc, CXL); 1631 1632 /** 1633 * cxl_switch_decoder_alloc - Allocate a switch level decoder 1634 * @port: owning CXL switch port of this decoder 1635 * @nr_targets: max number of dynamically addressable downstream targets 1636 * 1637 * Return: A new cxl decoder to be registered by cxl_decoder_add(). A 1638 * 'switch' decoder is any decoder that can be enumerated by PCIe 1639 * topology and the HDM Decoder Capability. This includes the decoders 1640 * that sit between Switch Upstream Ports / Switch Downstream Ports and 1641 * Host Bridges / Root Ports. 1642 */ 1643 struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port, 1644 unsigned int nr_targets) 1645 { 1646 struct cxl_switch_decoder *cxlsd; 1647 struct cxl_decoder *cxld; 1648 int rc; 1649 1650 if (is_cxl_root(port) || is_cxl_endpoint(port)) 1651 return ERR_PTR(-EINVAL); 1652 1653 cxlsd = kzalloc(struct_size(cxlsd, target, nr_targets), GFP_KERNEL); 1654 if (!cxlsd) 1655 return ERR_PTR(-ENOMEM); 1656 1657 rc = cxl_switch_decoder_init(port, cxlsd, nr_targets); 1658 if (rc) { 1659 kfree(cxlsd); 1660 return ERR_PTR(rc); 1661 } 1662 1663 cxld = &cxlsd->cxld; 1664 cxld->dev.type = &cxl_decoder_switch_type; 1665 return cxlsd; 1666 } 1667 EXPORT_SYMBOL_NS_GPL(cxl_switch_decoder_alloc, CXL); 1668 1669 /** 1670 * cxl_endpoint_decoder_alloc - Allocate an endpoint decoder 1671 * @port: owning port of this decoder 1672 * 1673 * Return: A new cxl decoder to be registered by cxl_decoder_add() 1674 */ 1675 struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port) 1676 { 1677 struct cxl_endpoint_decoder *cxled; 1678 struct cxl_decoder *cxld; 1679 int rc; 1680 1681 if (!is_cxl_endpoint(port)) 1682 return ERR_PTR(-EINVAL); 1683 1684 cxled = kzalloc(sizeof(*cxled), GFP_KERNEL); 1685 if (!cxled) 1686 return ERR_PTR(-ENOMEM); 1687 1688 cxled->pos = -1; 1689 cxld = &cxled->cxld; 1690 rc = cxl_decoder_init(port, cxld); 1691 if (rc) { 1692 kfree(cxled); 1693 return ERR_PTR(rc); 1694 } 1695 1696 cxld->dev.type = &cxl_decoder_endpoint_type; 1697 return cxled; 1698 } 1699 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_decoder_alloc, CXL); 1700 1701 /** 1702 * cxl_decoder_add_locked - Add a decoder with targets 1703 * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc() 1704 * @target_map: A list of downstream ports that this decoder can direct memory 1705 * traffic to. These numbers should correspond with the port number 1706 * in the PCIe Link Capabilities structure. 1707 * 1708 * Certain types of decoders may not have any targets. The main example of this 1709 * is an endpoint device. A more awkward example is a hostbridge whose root 1710 * ports get hot added (technically possible, though unlikely). 1711 * 1712 * This is the locked variant of cxl_decoder_add(). 1713 * 1714 * Context: Process context. Expects the device lock of the port that owns the 1715 * @cxld to be held. 1716 * 1717 * Return: Negative error code if the decoder wasn't properly configured; else 1718 * returns 0. 1719 */ 1720 int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map) 1721 { 1722 struct cxl_port *port; 1723 struct device *dev; 1724 int rc; 1725 1726 if (WARN_ON_ONCE(!cxld)) 1727 return -EINVAL; 1728 1729 if (WARN_ON_ONCE(IS_ERR(cxld))) 1730 return PTR_ERR(cxld); 1731 1732 if (cxld->interleave_ways < 1) 1733 return -EINVAL; 1734 1735 dev = &cxld->dev; 1736 1737 port = to_cxl_port(cxld->dev.parent); 1738 if (!is_endpoint_decoder(dev)) { 1739 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev); 1740 1741 rc = decoder_populate_targets(cxlsd, port, target_map); 1742 if (rc && (cxld->flags & CXL_DECODER_F_ENABLE)) { 1743 dev_err(&port->dev, 1744 "Failed to populate active decoder targets\n"); 1745 return rc; 1746 } 1747 } 1748 1749 rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id); 1750 if (rc) 1751 return rc; 1752 1753 return device_add(dev); 1754 } 1755 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add_locked, CXL); 1756 1757 /** 1758 * cxl_decoder_add - Add a decoder with targets 1759 * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc() 1760 * @target_map: A list of downstream ports that this decoder can direct memory 1761 * traffic to. These numbers should correspond with the port number 1762 * in the PCIe Link Capabilities structure. 1763 * 1764 * This is the unlocked variant of cxl_decoder_add_locked(). 1765 * See cxl_decoder_add_locked(). 1766 * 1767 * Context: Process context. Takes and releases the device lock of the port that 1768 * owns the @cxld. 1769 */ 1770 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map) 1771 { 1772 struct cxl_port *port; 1773 int rc; 1774 1775 if (WARN_ON_ONCE(!cxld)) 1776 return -EINVAL; 1777 1778 if (WARN_ON_ONCE(IS_ERR(cxld))) 1779 return PTR_ERR(cxld); 1780 1781 port = to_cxl_port(cxld->dev.parent); 1782 1783 device_lock(&port->dev); 1784 rc = cxl_decoder_add_locked(cxld, target_map); 1785 device_unlock(&port->dev); 1786 1787 return rc; 1788 } 1789 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, CXL); 1790 1791 static void cxld_unregister(void *dev) 1792 { 1793 struct cxl_endpoint_decoder *cxled; 1794 1795 if (is_endpoint_decoder(dev)) { 1796 cxled = to_cxl_endpoint_decoder(dev); 1797 cxl_decoder_kill_region(cxled); 1798 } 1799 1800 device_unregister(dev); 1801 } 1802 1803 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld) 1804 { 1805 return devm_add_action_or_reset(host, cxld_unregister, &cxld->dev); 1806 } 1807 EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, CXL); 1808 1809 /** 1810 * __cxl_driver_register - register a driver for the cxl bus 1811 * @cxl_drv: cxl driver structure to attach 1812 * @owner: owning module/driver 1813 * @modname: KBUILD_MODNAME for parent driver 1814 */ 1815 int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner, 1816 const char *modname) 1817 { 1818 if (!cxl_drv->probe) { 1819 pr_debug("%s ->probe() must be specified\n", modname); 1820 return -EINVAL; 1821 } 1822 1823 if (!cxl_drv->name) { 1824 pr_debug("%s ->name must be specified\n", modname); 1825 return -EINVAL; 1826 } 1827 1828 if (!cxl_drv->id) { 1829 pr_debug("%s ->id must be specified\n", modname); 1830 return -EINVAL; 1831 } 1832 1833 cxl_drv->drv.bus = &cxl_bus_type; 1834 cxl_drv->drv.owner = owner; 1835 cxl_drv->drv.mod_name = modname; 1836 cxl_drv->drv.name = cxl_drv->name; 1837 1838 return driver_register(&cxl_drv->drv); 1839 } 1840 EXPORT_SYMBOL_NS_GPL(__cxl_driver_register, CXL); 1841 1842 void cxl_driver_unregister(struct cxl_driver *cxl_drv) 1843 { 1844 driver_unregister(&cxl_drv->drv); 1845 } 1846 EXPORT_SYMBOL_NS_GPL(cxl_driver_unregister, CXL); 1847 1848 static int cxl_bus_uevent(const struct device *dev, struct kobj_uevent_env *env) 1849 { 1850 return add_uevent_var(env, "MODALIAS=" CXL_MODALIAS_FMT, 1851 cxl_device_id(dev)); 1852 } 1853 1854 static int cxl_bus_match(struct device *dev, struct device_driver *drv) 1855 { 1856 return cxl_device_id(dev) == to_cxl_drv(drv)->id; 1857 } 1858 1859 static int cxl_bus_probe(struct device *dev) 1860 { 1861 int rc; 1862 1863 rc = to_cxl_drv(dev->driver)->probe(dev); 1864 dev_dbg(dev, "probe: %d\n", rc); 1865 return rc; 1866 } 1867 1868 static void cxl_bus_remove(struct device *dev) 1869 { 1870 struct cxl_driver *cxl_drv = to_cxl_drv(dev->driver); 1871 1872 if (cxl_drv->remove) 1873 cxl_drv->remove(dev); 1874 } 1875 1876 static struct workqueue_struct *cxl_bus_wq; 1877 1878 static void cxl_bus_rescan_queue(struct work_struct *w) 1879 { 1880 int rc = bus_rescan_devices(&cxl_bus_type); 1881 1882 pr_debug("CXL bus rescan result: %d\n", rc); 1883 } 1884 1885 void cxl_bus_rescan(void) 1886 { 1887 static DECLARE_WORK(rescan_work, cxl_bus_rescan_queue); 1888 1889 queue_work(cxl_bus_wq, &rescan_work); 1890 } 1891 EXPORT_SYMBOL_NS_GPL(cxl_bus_rescan, CXL); 1892 1893 void cxl_bus_drain(void) 1894 { 1895 drain_workqueue(cxl_bus_wq); 1896 } 1897 EXPORT_SYMBOL_NS_GPL(cxl_bus_drain, CXL); 1898 1899 bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd) 1900 { 1901 return queue_work(cxl_bus_wq, &cxlmd->detach_work); 1902 } 1903 EXPORT_SYMBOL_NS_GPL(schedule_cxl_memdev_detach, CXL); 1904 1905 /* for user tooling to ensure port disable work has completed */ 1906 static ssize_t flush_store(struct bus_type *bus, const char *buf, size_t count) 1907 { 1908 if (sysfs_streq(buf, "1")) { 1909 flush_workqueue(cxl_bus_wq); 1910 return count; 1911 } 1912 1913 return -EINVAL; 1914 } 1915 1916 static BUS_ATTR_WO(flush); 1917 1918 static struct attribute *cxl_bus_attributes[] = { 1919 &bus_attr_flush.attr, 1920 NULL, 1921 }; 1922 1923 static struct attribute_group cxl_bus_attribute_group = { 1924 .attrs = cxl_bus_attributes, 1925 }; 1926 1927 static const struct attribute_group *cxl_bus_attribute_groups[] = { 1928 &cxl_bus_attribute_group, 1929 NULL, 1930 }; 1931 1932 struct bus_type cxl_bus_type = { 1933 .name = "cxl", 1934 .uevent = cxl_bus_uevent, 1935 .match = cxl_bus_match, 1936 .probe = cxl_bus_probe, 1937 .remove = cxl_bus_remove, 1938 .bus_groups = cxl_bus_attribute_groups, 1939 }; 1940 EXPORT_SYMBOL_NS_GPL(cxl_bus_type, CXL); 1941 1942 static struct dentry *cxl_debugfs; 1943 1944 struct dentry *cxl_debugfs_create_dir(const char *dir) 1945 { 1946 return debugfs_create_dir(dir, cxl_debugfs); 1947 } 1948 EXPORT_SYMBOL_NS_GPL(cxl_debugfs_create_dir, CXL); 1949 1950 static __init int cxl_core_init(void) 1951 { 1952 int rc; 1953 1954 cxl_debugfs = debugfs_create_dir("cxl", NULL); 1955 1956 cxl_mbox_init(); 1957 1958 rc = cxl_memdev_init(); 1959 if (rc) 1960 return rc; 1961 1962 cxl_bus_wq = alloc_ordered_workqueue("cxl_port", 0); 1963 if (!cxl_bus_wq) { 1964 rc = -ENOMEM; 1965 goto err_wq; 1966 } 1967 1968 rc = bus_register(&cxl_bus_type); 1969 if (rc) 1970 goto err_bus; 1971 1972 rc = cxl_region_init(); 1973 if (rc) 1974 goto err_region; 1975 1976 return 0; 1977 1978 err_region: 1979 bus_unregister(&cxl_bus_type); 1980 err_bus: 1981 destroy_workqueue(cxl_bus_wq); 1982 err_wq: 1983 cxl_memdev_exit(); 1984 return rc; 1985 } 1986 1987 static void cxl_core_exit(void) 1988 { 1989 cxl_region_exit(); 1990 bus_unregister(&cxl_bus_type); 1991 destroy_workqueue(cxl_bus_wq); 1992 cxl_memdev_exit(); 1993 debugfs_remove_recursive(cxl_debugfs); 1994 } 1995 1996 subsys_initcall(cxl_core_init); 1997 module_exit(cxl_core_exit); 1998 MODULE_LICENSE("GPL v2"); 1999