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