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