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