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>
10 #include <linux/slab.h>
11 #include <linux/idr.h>
20 * The CXL core provides a set of interfaces that can be consumed by CXL aware
21 * drivers. The interfaces allow for creation, modification, and destruction of
22 * regions, memory devices, ports, and decoders. CXL aware drivers must register
23 * with the CXL core via these interfaces in order to be able to participate in
24 * cross-device interleave coordination. The CXL core also establishes and
25 * maintains the bridge to the nvdimm subsystem.
27 * CXL core introduces sysfs hierarchy to control the devices that are
28 * instantiated by the core.
31 static DEFINE_IDA(cxl_port_ida);
32 static DEFINE_XARRAY(cxl_root_buses);
34 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
37 return sysfs_emit(buf, "%s\n", dev->type->name);
39 static DEVICE_ATTR_RO(devtype);
41 static int cxl_device_id(const struct device *dev)
43 if (dev->type == &cxl_nvdimm_bridge_type)
44 return CXL_DEVICE_NVDIMM_BRIDGE;
45 if (dev->type == &cxl_nvdimm_type)
46 return CXL_DEVICE_NVDIMM;
47 if (dev->type == CXL_PMEM_REGION_TYPE())
48 return CXL_DEVICE_PMEM_REGION;
49 if (dev->type == CXL_DAX_REGION_TYPE())
50 return CXL_DEVICE_DAX_REGION;
51 if (is_cxl_port(dev)) {
52 if (is_cxl_root(to_cxl_port(dev)))
53 return CXL_DEVICE_ROOT;
54 return CXL_DEVICE_PORT;
56 if (is_cxl_memdev(dev))
57 return CXL_DEVICE_MEMORY_EXPANDER;
58 if (dev->type == CXL_REGION_TYPE())
59 return CXL_DEVICE_REGION;
60 if (dev->type == &cxl_pmu_type)
61 return CXL_DEVICE_PMU;
65 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
68 return sysfs_emit(buf, CXL_MODALIAS_FMT "\n", cxl_device_id(dev));
70 static DEVICE_ATTR_RO(modalias);
72 static struct attribute *cxl_base_attributes[] = {
73 &dev_attr_devtype.attr,
74 &dev_attr_modalias.attr,
78 struct attribute_group cxl_base_attribute_group = {
79 .attrs = cxl_base_attributes,
82 static ssize_t start_show(struct device *dev, struct device_attribute *attr,
85 struct cxl_decoder *cxld = to_cxl_decoder(dev);
87 return sysfs_emit(buf, "%#llx\n", cxld->hpa_range.start);
89 static DEVICE_ATTR_ADMIN_RO(start);
91 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
94 struct cxl_decoder *cxld = to_cxl_decoder(dev);
96 return sysfs_emit(buf, "%#llx\n", range_len(&cxld->hpa_range));
98 static DEVICE_ATTR_RO(size);
100 #define CXL_DECODER_FLAG_ATTR(name, flag) \
101 static ssize_t name##_show(struct device *dev, \
102 struct device_attribute *attr, char *buf) \
104 struct cxl_decoder *cxld = to_cxl_decoder(dev); \
106 return sysfs_emit(buf, "%s\n", \
107 (cxld->flags & (flag)) ? "1" : "0"); \
109 static DEVICE_ATTR_RO(name)
111 CXL_DECODER_FLAG_ATTR(cap_pmem, CXL_DECODER_F_PMEM);
112 CXL_DECODER_FLAG_ATTR(cap_ram, CXL_DECODER_F_RAM);
113 CXL_DECODER_FLAG_ATTR(cap_type2, CXL_DECODER_F_TYPE2);
114 CXL_DECODER_FLAG_ATTR(cap_type3, CXL_DECODER_F_TYPE3);
115 CXL_DECODER_FLAG_ATTR(locked, CXL_DECODER_F_LOCK);
117 static ssize_t target_type_show(struct device *dev,
118 struct device_attribute *attr, char *buf)
120 struct cxl_decoder *cxld = to_cxl_decoder(dev);
122 switch (cxld->target_type) {
123 case CXL_DECODER_DEVMEM:
124 return sysfs_emit(buf, "accelerator\n");
125 case CXL_DECODER_HOSTONLYMEM:
126 return sysfs_emit(buf, "expander\n");
130 static DEVICE_ATTR_RO(target_type);
132 static ssize_t emit_target_list(struct cxl_switch_decoder *cxlsd, char *buf)
134 struct cxl_decoder *cxld = &cxlsd->cxld;
138 for (i = 0; i < cxld->interleave_ways; i++) {
139 struct cxl_dport *dport = cxlsd->target[i];
140 struct cxl_dport *next = NULL;
145 if (i + 1 < cxld->interleave_ways)
146 next = cxlsd->target[i + 1];
147 rc = sysfs_emit_at(buf, offset, "%d%s", dport->port_id,
157 static ssize_t target_list_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
160 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
166 seq = read_seqbegin(&cxlsd->target_lock);
167 rc = emit_target_list(cxlsd, buf);
168 } while (read_seqretry(&cxlsd->target_lock, seq));
174 rc = sysfs_emit_at(buf, offset, "\n");
180 static DEVICE_ATTR_RO(target_list);
182 static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
185 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
187 return sysfs_emit(buf, "%s\n", cxl_decoder_mode_name(cxled->mode));
190 static ssize_t mode_store(struct device *dev, struct device_attribute *attr,
191 const char *buf, size_t len)
193 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
194 enum cxl_decoder_mode mode;
197 if (sysfs_streq(buf, "pmem"))
198 mode = CXL_DECODER_PMEM;
199 else if (sysfs_streq(buf, "ram"))
200 mode = CXL_DECODER_RAM;
204 rc = cxl_dpa_set_mode(cxled, mode);
210 static DEVICE_ATTR_RW(mode);
212 static ssize_t dpa_resource_show(struct device *dev, struct device_attribute *attr,
215 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
216 u64 base = cxl_dpa_resource_start(cxled);
218 return sysfs_emit(buf, "%#llx\n", base);
220 static DEVICE_ATTR_RO(dpa_resource);
222 static ssize_t dpa_size_show(struct device *dev, struct device_attribute *attr,
225 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
226 resource_size_t size = cxl_dpa_size(cxled);
228 return sysfs_emit(buf, "%pa\n", &size);
231 static ssize_t dpa_size_store(struct device *dev, struct device_attribute *attr,
232 const char *buf, size_t len)
234 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
235 unsigned long long size;
238 rc = kstrtoull(buf, 0, &size);
242 if (!IS_ALIGNED(size, SZ_256M))
245 rc = cxl_dpa_free(cxled);
252 rc = cxl_dpa_alloc(cxled, size);
258 static DEVICE_ATTR_RW(dpa_size);
260 static ssize_t interleave_granularity_show(struct device *dev,
261 struct device_attribute *attr,
264 struct cxl_decoder *cxld = to_cxl_decoder(dev);
266 return sysfs_emit(buf, "%d\n", cxld->interleave_granularity);
269 static DEVICE_ATTR_RO(interleave_granularity);
271 static ssize_t interleave_ways_show(struct device *dev,
272 struct device_attribute *attr, char *buf)
274 struct cxl_decoder *cxld = to_cxl_decoder(dev);
276 return sysfs_emit(buf, "%d\n", cxld->interleave_ways);
279 static DEVICE_ATTR_RO(interleave_ways);
281 static struct attribute *cxl_decoder_base_attrs[] = {
282 &dev_attr_start.attr,
284 &dev_attr_locked.attr,
285 &dev_attr_interleave_granularity.attr,
286 &dev_attr_interleave_ways.attr,
290 static struct attribute_group cxl_decoder_base_attribute_group = {
291 .attrs = cxl_decoder_base_attrs,
294 static struct attribute *cxl_decoder_root_attrs[] = {
295 &dev_attr_cap_pmem.attr,
296 &dev_attr_cap_ram.attr,
297 &dev_attr_cap_type2.attr,
298 &dev_attr_cap_type3.attr,
299 &dev_attr_target_list.attr,
300 SET_CXL_REGION_ATTR(create_pmem_region)
301 SET_CXL_REGION_ATTR(create_ram_region)
302 SET_CXL_REGION_ATTR(delete_region)
306 static bool can_create_pmem(struct cxl_root_decoder *cxlrd)
308 unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_PMEM;
310 return (cxlrd->cxlsd.cxld.flags & flags) == flags;
313 static bool can_create_ram(struct cxl_root_decoder *cxlrd)
315 unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_RAM;
317 return (cxlrd->cxlsd.cxld.flags & flags) == flags;
320 static umode_t cxl_root_decoder_visible(struct kobject *kobj, struct attribute *a, int n)
322 struct device *dev = kobj_to_dev(kobj);
323 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
325 if (a == CXL_REGION_ATTR(create_pmem_region) && !can_create_pmem(cxlrd))
328 if (a == CXL_REGION_ATTR(create_ram_region) && !can_create_ram(cxlrd))
331 if (a == CXL_REGION_ATTR(delete_region) &&
332 !(can_create_pmem(cxlrd) || can_create_ram(cxlrd)))
338 static struct attribute_group cxl_decoder_root_attribute_group = {
339 .attrs = cxl_decoder_root_attrs,
340 .is_visible = cxl_root_decoder_visible,
343 static const struct attribute_group *cxl_decoder_root_attribute_groups[] = {
344 &cxl_decoder_root_attribute_group,
345 &cxl_decoder_base_attribute_group,
346 &cxl_base_attribute_group,
350 static struct attribute *cxl_decoder_switch_attrs[] = {
351 &dev_attr_target_type.attr,
352 &dev_attr_target_list.attr,
353 SET_CXL_REGION_ATTR(region)
357 static struct attribute_group cxl_decoder_switch_attribute_group = {
358 .attrs = cxl_decoder_switch_attrs,
361 static const struct attribute_group *cxl_decoder_switch_attribute_groups[] = {
362 &cxl_decoder_switch_attribute_group,
363 &cxl_decoder_base_attribute_group,
364 &cxl_base_attribute_group,
368 static struct attribute *cxl_decoder_endpoint_attrs[] = {
369 &dev_attr_target_type.attr,
371 &dev_attr_dpa_size.attr,
372 &dev_attr_dpa_resource.attr,
373 SET_CXL_REGION_ATTR(region)
377 static struct attribute_group cxl_decoder_endpoint_attribute_group = {
378 .attrs = cxl_decoder_endpoint_attrs,
381 static const struct attribute_group *cxl_decoder_endpoint_attribute_groups[] = {
382 &cxl_decoder_base_attribute_group,
383 &cxl_decoder_endpoint_attribute_group,
384 &cxl_base_attribute_group,
388 static void __cxl_decoder_release(struct cxl_decoder *cxld)
390 struct cxl_port *port = to_cxl_port(cxld->dev.parent);
392 ida_free(&port->decoder_ida, cxld->id);
393 put_device(&port->dev);
396 static void cxl_endpoint_decoder_release(struct device *dev)
398 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
400 __cxl_decoder_release(&cxled->cxld);
404 static void cxl_switch_decoder_release(struct device *dev)
406 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
408 __cxl_decoder_release(&cxlsd->cxld);
412 struct cxl_root_decoder *to_cxl_root_decoder(struct device *dev)
414 if (dev_WARN_ONCE(dev, !is_root_decoder(dev),
415 "not a cxl_root_decoder device\n"))
417 return container_of(dev, struct cxl_root_decoder, cxlsd.cxld.dev);
419 EXPORT_SYMBOL_NS_GPL(to_cxl_root_decoder, CXL);
421 static void cxl_root_decoder_release(struct device *dev)
423 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
425 if (atomic_read(&cxlrd->region_id) >= 0)
426 memregion_free(atomic_read(&cxlrd->region_id));
427 __cxl_decoder_release(&cxlrd->cxlsd.cxld);
431 static const struct device_type cxl_decoder_endpoint_type = {
432 .name = "cxl_decoder_endpoint",
433 .release = cxl_endpoint_decoder_release,
434 .groups = cxl_decoder_endpoint_attribute_groups,
437 static const struct device_type cxl_decoder_switch_type = {
438 .name = "cxl_decoder_switch",
439 .release = cxl_switch_decoder_release,
440 .groups = cxl_decoder_switch_attribute_groups,
443 static const struct device_type cxl_decoder_root_type = {
444 .name = "cxl_decoder_root",
445 .release = cxl_root_decoder_release,
446 .groups = cxl_decoder_root_attribute_groups,
449 bool is_endpoint_decoder(struct device *dev)
451 return dev->type == &cxl_decoder_endpoint_type;
453 EXPORT_SYMBOL_NS_GPL(is_endpoint_decoder, CXL);
455 bool is_root_decoder(struct device *dev)
457 return dev->type == &cxl_decoder_root_type;
459 EXPORT_SYMBOL_NS_GPL(is_root_decoder, CXL);
461 bool is_switch_decoder(struct device *dev)
463 return is_root_decoder(dev) || dev->type == &cxl_decoder_switch_type;
465 EXPORT_SYMBOL_NS_GPL(is_switch_decoder, CXL);
467 struct cxl_decoder *to_cxl_decoder(struct device *dev)
469 if (dev_WARN_ONCE(dev,
470 !is_switch_decoder(dev) && !is_endpoint_decoder(dev),
471 "not a cxl_decoder device\n"))
473 return container_of(dev, struct cxl_decoder, dev);
475 EXPORT_SYMBOL_NS_GPL(to_cxl_decoder, CXL);
477 struct cxl_endpoint_decoder *to_cxl_endpoint_decoder(struct device *dev)
479 if (dev_WARN_ONCE(dev, !is_endpoint_decoder(dev),
480 "not a cxl_endpoint_decoder device\n"))
482 return container_of(dev, struct cxl_endpoint_decoder, cxld.dev);
484 EXPORT_SYMBOL_NS_GPL(to_cxl_endpoint_decoder, CXL);
486 struct cxl_switch_decoder *to_cxl_switch_decoder(struct device *dev)
488 if (dev_WARN_ONCE(dev, !is_switch_decoder(dev),
489 "not a cxl_switch_decoder device\n"))
491 return container_of(dev, struct cxl_switch_decoder, cxld.dev);
493 EXPORT_SYMBOL_NS_GPL(to_cxl_switch_decoder, CXL);
495 static void cxl_ep_release(struct cxl_ep *ep)
501 static void cxl_ep_remove(struct cxl_port *port, struct cxl_ep *ep)
505 xa_erase(&port->endpoints, (unsigned long) ep->ep);
509 static void cxl_port_release(struct device *dev)
511 struct cxl_port *port = to_cxl_port(dev);
515 xa_for_each(&port->endpoints, index, ep)
516 cxl_ep_remove(port, ep);
517 xa_destroy(&port->endpoints);
518 xa_destroy(&port->dports);
519 xa_destroy(&port->regions);
520 ida_free(&cxl_port_ida, port->id);
524 static const struct attribute_group *cxl_port_attribute_groups[] = {
525 &cxl_base_attribute_group,
529 static const struct device_type cxl_port_type = {
531 .release = cxl_port_release,
532 .groups = cxl_port_attribute_groups,
535 bool is_cxl_port(const struct device *dev)
537 return dev->type == &cxl_port_type;
539 EXPORT_SYMBOL_NS_GPL(is_cxl_port, CXL);
541 struct cxl_port *to_cxl_port(const struct device *dev)
543 if (dev_WARN_ONCE(dev, dev->type != &cxl_port_type,
544 "not a cxl_port device\n"))
546 return container_of(dev, struct cxl_port, dev);
548 EXPORT_SYMBOL_NS_GPL(to_cxl_port, CXL);
550 static void unregister_port(void *_port)
552 struct cxl_port *port = _port;
553 struct cxl_port *parent;
554 struct device *lock_dev;
556 if (is_cxl_root(port))
559 parent = to_cxl_port(port->dev.parent);
562 * CXL root port's and the first level of ports are unregistered
563 * under the platform firmware device lock, all other ports are
564 * unregistered while holding their parent port lock.
567 lock_dev = port->uport_dev;
568 else if (is_cxl_root(parent))
569 lock_dev = parent->uport_dev;
571 lock_dev = &parent->dev;
573 device_lock_assert(lock_dev);
575 device_unregister(&port->dev);
578 static void cxl_unlink_uport(void *_port)
580 struct cxl_port *port = _port;
582 sysfs_remove_link(&port->dev.kobj, "uport");
585 static int devm_cxl_link_uport(struct device *host, struct cxl_port *port)
589 rc = sysfs_create_link(&port->dev.kobj, &port->uport_dev->kobj,
593 return devm_add_action_or_reset(host, cxl_unlink_uport, port);
596 static void cxl_unlink_parent_dport(void *_port)
598 struct cxl_port *port = _port;
600 sysfs_remove_link(&port->dev.kobj, "parent_dport");
603 static int devm_cxl_link_parent_dport(struct device *host,
604 struct cxl_port *port,
605 struct cxl_dport *parent_dport)
612 rc = sysfs_create_link(&port->dev.kobj, &parent_dport->dport_dev->kobj,
616 return devm_add_action_or_reset(host, cxl_unlink_parent_dport, port);
619 static struct lock_class_key cxl_port_key;
621 static struct cxl_port *cxl_port_alloc(struct device *uport_dev,
622 resource_size_t component_reg_phys,
623 struct cxl_dport *parent_dport)
625 struct cxl_port *port;
629 port = kzalloc(sizeof(*port), GFP_KERNEL);
631 return ERR_PTR(-ENOMEM);
633 rc = ida_alloc(&cxl_port_ida, GFP_KERNEL);
637 port->uport_dev = uport_dev;
640 * The top-level cxl_port "cxl_root" does not have a cxl_port as
641 * its parent and it does not have any corresponding component
642 * registers as its decode is described by a fixed platform
647 struct cxl_port *parent_port = parent_dport->port;
648 struct cxl_port *iter;
650 dev->parent = &parent_port->dev;
651 port->depth = parent_port->depth + 1;
652 port->parent_dport = parent_dport;
655 * walk to the host bridge, or the first ancestor that knows
659 while (!iter->host_bridge &&
660 !is_cxl_root(to_cxl_port(iter->dev.parent)))
661 iter = to_cxl_port(iter->dev.parent);
662 if (iter->host_bridge)
663 port->host_bridge = iter->host_bridge;
664 else if (parent_dport->rch)
665 port->host_bridge = parent_dport->dport_dev;
667 port->host_bridge = iter->uport_dev;
668 dev_dbg(uport_dev, "host-bridge: %s\n",
669 dev_name(port->host_bridge));
671 dev->parent = uport_dev;
673 port->component_reg_phys = component_reg_phys;
674 ida_init(&port->decoder_ida);
676 port->commit_end = -1;
677 xa_init(&port->dports);
678 xa_init(&port->endpoints);
679 xa_init(&port->regions);
681 device_initialize(dev);
682 lockdep_set_class_and_subclass(&dev->mutex, &cxl_port_key, port->depth);
683 device_set_pm_not_required(dev);
684 dev->bus = &cxl_bus_type;
685 dev->type = &cxl_port_type;
694 static int cxl_setup_comp_regs(struct device *dev, struct cxl_register_map *map,
695 resource_size_t component_reg_phys)
697 if (component_reg_phys == CXL_RESOURCE_NONE)
700 *map = (struct cxl_register_map) {
702 .reg_type = CXL_REGLOC_RBI_COMPONENT,
703 .resource = component_reg_phys,
704 .max_size = CXL_COMPONENT_REG_BLOCK_SIZE,
707 return cxl_setup_regs(map);
710 static int cxl_port_setup_regs(struct cxl_port *port,
711 resource_size_t component_reg_phys)
713 if (dev_is_platform(port->uport_dev))
715 return cxl_setup_comp_regs(&port->dev, &port->comp_map,
719 static int cxl_dport_setup_regs(struct cxl_dport *dport,
720 resource_size_t component_reg_phys)
722 if (dev_is_platform(dport->dport_dev))
724 return cxl_setup_comp_regs(dport->dport_dev, &dport->comp_map,
728 static struct cxl_port *__devm_cxl_add_port(struct device *host,
729 struct device *uport_dev,
730 resource_size_t component_reg_phys,
731 struct cxl_dport *parent_dport)
733 struct cxl_port *port;
737 port = cxl_port_alloc(uport_dev, component_reg_phys, parent_dport);
742 if (is_cxl_memdev(uport_dev))
743 rc = dev_set_name(dev, "endpoint%d", port->id);
744 else if (parent_dport)
745 rc = dev_set_name(dev, "port%d", port->id);
747 rc = dev_set_name(dev, "root%d", port->id);
751 rc = cxl_port_setup_regs(port, component_reg_phys);
755 rc = device_add(dev);
759 rc = devm_add_action_or_reset(host, unregister_port, port);
763 rc = devm_cxl_link_uport(host, port);
767 rc = devm_cxl_link_parent_dport(host, port, parent_dport);
779 * devm_cxl_add_port - register a cxl_port in CXL memory decode hierarchy
780 * @host: host device for devm operations
781 * @uport_dev: "physical" device implementing this upstream port
782 * @component_reg_phys: (optional) for configurable cxl_port instances
783 * @parent_dport: next hop up in the CXL memory decode hierarchy
785 struct cxl_port *devm_cxl_add_port(struct device *host,
786 struct device *uport_dev,
787 resource_size_t component_reg_phys,
788 struct cxl_dport *parent_dport)
790 struct cxl_port *port, *parent_port;
792 port = __devm_cxl_add_port(host, uport_dev, component_reg_phys,
795 parent_port = parent_dport ? parent_dport->port : NULL;
797 dev_dbg(uport_dev, "Failed to add%s%s%s: %ld\n",
798 parent_port ? " port to " : "",
799 parent_port ? dev_name(&parent_port->dev) : "",
800 parent_port ? "" : " root port",
803 dev_dbg(uport_dev, "%s added%s%s%s\n",
804 dev_name(&port->dev),
805 parent_port ? " to " : "",
806 parent_port ? dev_name(&parent_port->dev) : "",
807 parent_port ? "" : " (root port)");
812 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_port, CXL);
814 struct pci_bus *cxl_port_to_pci_bus(struct cxl_port *port)
816 /* There is no pci_bus associated with a CXL platform-root port */
817 if (is_cxl_root(port))
820 if (dev_is_pci(port->uport_dev)) {
821 struct pci_dev *pdev = to_pci_dev(port->uport_dev);
823 return pdev->subordinate;
826 return xa_load(&cxl_root_buses, (unsigned long)port->uport_dev);
828 EXPORT_SYMBOL_NS_GPL(cxl_port_to_pci_bus, CXL);
830 static void unregister_pci_bus(void *uport_dev)
832 xa_erase(&cxl_root_buses, (unsigned long)uport_dev);
835 int devm_cxl_register_pci_bus(struct device *host, struct device *uport_dev,
840 if (dev_is_pci(uport_dev))
843 rc = xa_insert(&cxl_root_buses, (unsigned long)uport_dev, bus,
847 return devm_add_action_or_reset(host, unregister_pci_bus, uport_dev);
849 EXPORT_SYMBOL_NS_GPL(devm_cxl_register_pci_bus, CXL);
851 static bool dev_is_cxl_root_child(struct device *dev)
853 struct cxl_port *port, *parent;
855 if (!is_cxl_port(dev))
858 port = to_cxl_port(dev);
859 if (is_cxl_root(port))
862 parent = to_cxl_port(port->dev.parent);
863 if (is_cxl_root(parent))
869 struct cxl_port *find_cxl_root(struct cxl_port *port)
871 struct cxl_port *iter = port;
873 while (iter && !is_cxl_root(iter))
874 iter = to_cxl_port(iter->dev.parent);
878 get_device(&iter->dev);
881 EXPORT_SYMBOL_NS_GPL(find_cxl_root, CXL);
883 static struct cxl_dport *find_dport(struct cxl_port *port, int id)
885 struct cxl_dport *dport;
888 device_lock_assert(&port->dev);
889 xa_for_each(&port->dports, index, dport)
890 if (dport->port_id == id)
895 static int add_dport(struct cxl_port *port, struct cxl_dport *dport)
897 struct cxl_dport *dup;
900 device_lock_assert(&port->dev);
901 dup = find_dport(port, dport->port_id);
904 "unable to add dport%d-%s non-unique port id (%s)\n",
905 dport->port_id, dev_name(dport->dport_dev),
906 dev_name(dup->dport_dev));
910 rc = xa_insert(&port->dports, (unsigned long)dport->dport_dev, dport,
920 * Since root-level CXL dports cannot be enumerated by PCI they are not
921 * enumerated by the common port driver that acquires the port lock over
922 * dport add/remove. Instead, root dports are manually added by a
923 * platform driver and cond_cxl_root_lock() is used to take the missing
924 * port lock in that case.
926 static void cond_cxl_root_lock(struct cxl_port *port)
928 if (is_cxl_root(port))
929 device_lock(&port->dev);
932 static void cond_cxl_root_unlock(struct cxl_port *port)
934 if (is_cxl_root(port))
935 device_unlock(&port->dev);
938 static void cxl_dport_remove(void *data)
940 struct cxl_dport *dport = data;
941 struct cxl_port *port = dport->port;
943 xa_erase(&port->dports, (unsigned long) dport->dport_dev);
944 put_device(dport->dport_dev);
947 static void cxl_dport_unlink(void *data)
949 struct cxl_dport *dport = data;
950 struct cxl_port *port = dport->port;
951 char link_name[CXL_TARGET_STRLEN];
953 sprintf(link_name, "dport%d", dport->port_id);
954 sysfs_remove_link(&port->dev.kobj, link_name);
957 static struct cxl_dport *
958 __devm_cxl_add_dport(struct cxl_port *port, struct device *dport_dev,
959 int port_id, resource_size_t component_reg_phys,
960 resource_size_t rcrb)
962 char link_name[CXL_TARGET_STRLEN];
963 struct cxl_dport *dport;
967 if (is_cxl_root(port))
968 host = port->uport_dev;
973 dev_WARN_ONCE(&port->dev, 1, "dport:%s bad devm context\n",
974 dev_name(dport_dev));
975 return ERR_PTR(-ENXIO);
978 if (snprintf(link_name, CXL_TARGET_STRLEN, "dport%d", port_id) >=
980 return ERR_PTR(-EINVAL);
982 dport = devm_kzalloc(host, sizeof(*dport), GFP_KERNEL);
984 return ERR_PTR(-ENOMEM);
986 if (rcrb != CXL_RESOURCE_NONE) {
987 dport->rcrb.base = rcrb;
988 component_reg_phys = __rcrb_to_component(dport_dev, &dport->rcrb,
989 CXL_RCRB_DOWNSTREAM);
990 if (component_reg_phys == CXL_RESOURCE_NONE) {
991 dev_warn(dport_dev, "Invalid Component Registers in RCRB");
992 return ERR_PTR(-ENXIO);
998 if (component_reg_phys != CXL_RESOURCE_NONE)
999 dev_dbg(dport_dev, "Component Registers found for dport: %pa\n",
1000 &component_reg_phys);
1002 dport->dport_dev = dport_dev;
1003 dport->port_id = port_id;
1006 rc = cxl_dport_setup_regs(dport, component_reg_phys);
1010 cond_cxl_root_lock(port);
1011 rc = add_dport(port, dport);
1012 cond_cxl_root_unlock(port);
1016 get_device(dport_dev);
1017 rc = devm_add_action_or_reset(host, cxl_dport_remove, dport);
1021 rc = sysfs_create_link(&port->dev.kobj, &dport_dev->kobj, link_name);
1025 rc = devm_add_action_or_reset(host, cxl_dport_unlink, dport);
1033 * devm_cxl_add_dport - append VH downstream port data to a cxl_port
1034 * @port: the cxl_port that references this dport
1035 * @dport_dev: firmware or PCI device representing the dport
1036 * @port_id: identifier for this dport in a decoder's target list
1037 * @component_reg_phys: optional location of CXL component registers
1039 * Note that dports are appended to the devm release action's of the
1040 * either the port's host (for root ports), or the port itself (for
1043 struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port,
1044 struct device *dport_dev, int port_id,
1045 resource_size_t component_reg_phys)
1047 struct cxl_dport *dport;
1049 dport = __devm_cxl_add_dport(port, dport_dev, port_id,
1050 component_reg_phys, CXL_RESOURCE_NONE);
1051 if (IS_ERR(dport)) {
1052 dev_dbg(dport_dev, "failed to add dport to %s: %ld\n",
1053 dev_name(&port->dev), PTR_ERR(dport));
1055 dev_dbg(dport_dev, "dport added to %s\n",
1056 dev_name(&port->dev));
1061 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_dport, CXL);
1064 * devm_cxl_add_rch_dport - append RCH downstream port data to a cxl_port
1065 * @port: the cxl_port that references this dport
1066 * @dport_dev: firmware or PCI device representing the dport
1067 * @port_id: identifier for this dport in a decoder's target list
1068 * @rcrb: mandatory location of a Root Complex Register Block
1070 * See CXL 3.0 9.11.8 CXL Devices Attached to an RCH
1072 struct cxl_dport *devm_cxl_add_rch_dport(struct cxl_port *port,
1073 struct device *dport_dev, int port_id,
1074 resource_size_t rcrb)
1076 struct cxl_dport *dport;
1078 if (rcrb == CXL_RESOURCE_NONE) {
1079 dev_dbg(&port->dev, "failed to add RCH dport, missing RCRB\n");
1080 return ERR_PTR(-EINVAL);
1083 dport = __devm_cxl_add_dport(port, dport_dev, port_id,
1084 CXL_RESOURCE_NONE, rcrb);
1085 if (IS_ERR(dport)) {
1086 dev_dbg(dport_dev, "failed to add RCH dport to %s: %ld\n",
1087 dev_name(&port->dev), PTR_ERR(dport));
1089 dev_dbg(dport_dev, "RCH dport added to %s\n",
1090 dev_name(&port->dev));
1095 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_rch_dport, CXL);
1097 static int add_ep(struct cxl_ep *new)
1099 struct cxl_port *port = new->dport->port;
1102 device_lock(&port->dev);
1104 device_unlock(&port->dev);
1107 rc = xa_insert(&port->endpoints, (unsigned long)new->ep, new,
1109 device_unlock(&port->dev);
1115 * cxl_add_ep - register an endpoint's interest in a port
1116 * @dport: the dport that routes to @ep_dev
1117 * @ep_dev: device representing the endpoint
1119 * Intermediate CXL ports are scanned based on the arrival of endpoints.
1120 * When those endpoints depart the port can be destroyed once all
1121 * endpoints that care about that port have been removed.
1123 static int cxl_add_ep(struct cxl_dport *dport, struct device *ep_dev)
1128 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1132 ep->ep = get_device(ep_dev);
1141 struct cxl_find_port_ctx {
1142 const struct device *dport_dev;
1143 const struct cxl_port *parent_port;
1144 struct cxl_dport **dport;
1147 static int match_port_by_dport(struct device *dev, const void *data)
1149 const struct cxl_find_port_ctx *ctx = data;
1150 struct cxl_dport *dport;
1151 struct cxl_port *port;
1153 if (!is_cxl_port(dev))
1155 if (ctx->parent_port && dev->parent != &ctx->parent_port->dev)
1158 port = to_cxl_port(dev);
1159 dport = cxl_find_dport_by_dev(port, ctx->dport_dev);
1161 *ctx->dport = dport;
1162 return dport != NULL;
1165 static struct cxl_port *__find_cxl_port(struct cxl_find_port_ctx *ctx)
1169 if (!ctx->dport_dev)
1172 dev = bus_find_device(&cxl_bus_type, NULL, ctx, match_port_by_dport);
1174 return to_cxl_port(dev);
1178 static struct cxl_port *find_cxl_port(struct device *dport_dev,
1179 struct cxl_dport **dport)
1181 struct cxl_find_port_ctx ctx = {
1182 .dport_dev = dport_dev,
1185 struct cxl_port *port;
1187 port = __find_cxl_port(&ctx);
1191 static struct cxl_port *find_cxl_port_at(struct cxl_port *parent_port,
1192 struct device *dport_dev,
1193 struct cxl_dport **dport)
1195 struct cxl_find_port_ctx ctx = {
1196 .dport_dev = dport_dev,
1197 .parent_port = parent_port,
1200 struct cxl_port *port;
1202 port = __find_cxl_port(&ctx);
1207 * All users of grandparent() are using it to walk PCIe-like switch port
1208 * hierarchy. A PCIe switch is comprised of a bridge device representing the
1209 * upstream switch port and N bridges representing downstream switch ports. When
1210 * bridges stack the grand-parent of a downstream switch port is another
1211 * downstream switch port in the immediate ancestor switch.
1213 static struct device *grandparent(struct device *dev)
1215 if (dev && dev->parent)
1216 return dev->parent->parent;
1220 static void delete_endpoint(void *data)
1222 struct cxl_memdev *cxlmd = data;
1223 struct cxl_port *endpoint = cxlmd->endpoint;
1224 struct cxl_port *parent_port;
1225 struct device *parent;
1227 parent_port = cxl_mem_find_port(cxlmd, NULL);
1230 parent = &parent_port->dev;
1232 device_lock(parent);
1233 if (parent->driver && !endpoint->dead) {
1234 devm_release_action(parent, cxl_unlink_parent_dport, endpoint);
1235 devm_release_action(parent, cxl_unlink_uport, endpoint);
1236 devm_release_action(parent, unregister_port, endpoint);
1238 cxlmd->endpoint = NULL;
1239 device_unlock(parent);
1242 put_device(&endpoint->dev);
1245 int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint)
1247 struct device *dev = &cxlmd->dev;
1249 get_device(&endpoint->dev);
1250 cxlmd->endpoint = endpoint;
1251 cxlmd->depth = endpoint->depth;
1252 return devm_add_action_or_reset(dev, delete_endpoint, cxlmd);
1254 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_autoremove, CXL);
1257 * The natural end of life of a non-root 'cxl_port' is when its parent port goes
1258 * through a ->remove() event ("top-down" unregistration). The unnatural trigger
1259 * for a port to be unregistered is when all memdevs beneath that port have gone
1260 * through ->remove(). This "bottom-up" removal selectively removes individual
1261 * child ports manually. This depends on devm_cxl_add_port() to not change is
1262 * devm action registration order, and for dports to have already been
1263 * destroyed by reap_dports().
1265 static void delete_switch_port(struct cxl_port *port)
1267 devm_release_action(port->dev.parent, cxl_unlink_parent_dport, port);
1268 devm_release_action(port->dev.parent, cxl_unlink_uport, port);
1269 devm_release_action(port->dev.parent, unregister_port, port);
1272 static void reap_dports(struct cxl_port *port)
1274 struct cxl_dport *dport;
1275 unsigned long index;
1277 device_lock_assert(&port->dev);
1279 xa_for_each(&port->dports, index, dport) {
1280 devm_release_action(&port->dev, cxl_dport_unlink, dport);
1281 devm_release_action(&port->dev, cxl_dport_remove, dport);
1282 devm_kfree(&port->dev, dport);
1287 struct cxl_memdev *cxlmd;
1291 static int port_has_memdev(struct device *dev, const void *data)
1293 const struct detach_ctx *ctx = data;
1294 struct cxl_port *port;
1296 if (!is_cxl_port(dev))
1299 port = to_cxl_port(dev);
1300 if (port->depth != ctx->depth)
1303 return !!cxl_ep_load(port, ctx->cxlmd);
1306 static void cxl_detach_ep(void *data)
1308 struct cxl_memdev *cxlmd = data;
1310 for (int i = cxlmd->depth - 1; i >= 1; i--) {
1311 struct cxl_port *port, *parent_port;
1312 struct detach_ctx ctx = {
1320 dev = bus_find_device(&cxl_bus_type, NULL, &ctx,
1324 port = to_cxl_port(dev);
1326 parent_port = to_cxl_port(port->dev.parent);
1327 device_lock(&parent_port->dev);
1328 device_lock(&port->dev);
1329 ep = cxl_ep_load(port, cxlmd);
1330 dev_dbg(&cxlmd->dev, "disconnect %s from %s\n",
1331 ep ? dev_name(ep->ep) : "", dev_name(&port->dev));
1332 cxl_ep_remove(port, ep);
1333 if (ep && !port->dead && xa_empty(&port->endpoints) &&
1334 !is_cxl_root(parent_port) && parent_port->dev.driver) {
1336 * This was the last ep attached to a dynamically
1337 * enumerated port. Block new cxl_add_ep() and garbage
1344 device_unlock(&port->dev);
1347 dev_dbg(&cxlmd->dev, "delete %s\n",
1348 dev_name(&port->dev));
1349 delete_switch_port(port);
1351 put_device(&port->dev);
1352 device_unlock(&parent_port->dev);
1356 static resource_size_t find_component_registers(struct device *dev)
1358 struct cxl_register_map map;
1359 struct pci_dev *pdev;
1362 * Theoretically, CXL component registers can be hosted on a
1363 * non-PCI device, in practice, only cxl_test hits this case.
1365 if (!dev_is_pci(dev))
1366 return CXL_RESOURCE_NONE;
1368 pdev = to_pci_dev(dev);
1370 cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
1371 return map.resource;
1374 static int add_port_attach_ep(struct cxl_memdev *cxlmd,
1375 struct device *uport_dev,
1376 struct device *dport_dev)
1378 struct device *dparent = grandparent(dport_dev);
1379 struct cxl_port *port, *parent_port = NULL;
1380 struct cxl_dport *dport, *parent_dport;
1381 resource_size_t component_reg_phys;
1386 * The iteration reached the topology root without finding the
1387 * CXL-root 'cxl_port' on a previous iteration, fail for now to
1388 * be re-probed after platform driver attaches.
1390 dev_dbg(&cxlmd->dev, "%s is a root dport\n",
1391 dev_name(dport_dev));
1395 parent_port = find_cxl_port(dparent, &parent_dport);
1397 /* iterate to create this parent_port */
1401 device_lock(&parent_port->dev);
1402 if (!parent_port->dev.driver) {
1403 dev_warn(&cxlmd->dev,
1404 "port %s:%s disabled, failed to enumerate CXL.mem\n",
1405 dev_name(&parent_port->dev), dev_name(uport_dev));
1406 port = ERR_PTR(-ENXIO);
1410 port = find_cxl_port_at(parent_port, dport_dev, &dport);
1412 component_reg_phys = find_component_registers(uport_dev);
1413 port = devm_cxl_add_port(&parent_port->dev, uport_dev,
1414 component_reg_phys, parent_dport);
1415 /* retry find to pick up the new dport information */
1417 port = find_cxl_port_at(parent_port, dport_dev, &dport);
1420 device_unlock(&parent_port->dev);
1425 dev_dbg(&cxlmd->dev, "add to new port %s:%s\n",
1426 dev_name(&port->dev), dev_name(port->uport_dev));
1427 rc = cxl_add_ep(dport, &cxlmd->dev);
1430 * "can't" happen, but this error code means
1431 * something to the caller, so translate it.
1435 put_device(&port->dev);
1438 put_device(&parent_port->dev);
1442 int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd)
1444 struct device *dev = &cxlmd->dev;
1445 struct device *iter;
1449 * Skip intermediate port enumeration in the RCH case, there
1450 * are no ports in between a host bridge and an endpoint.
1452 if (cxlmd->cxlds->rcd)
1455 rc = devm_add_action_or_reset(&cxlmd->dev, cxl_detach_ep, cxlmd);
1460 * Scan for and add all cxl_ports in this device's ancestry.
1461 * Repeat until no more ports are added. Abort if a port add
1465 for (iter = dev; iter; iter = grandparent(iter)) {
1466 struct device *dport_dev = grandparent(iter);
1467 struct device *uport_dev;
1468 struct cxl_dport *dport;
1469 struct cxl_port *port;
1474 uport_dev = dport_dev->parent;
1476 dev_warn(dev, "at %s no parent for dport: %s\n",
1477 dev_name(iter), dev_name(dport_dev));
1481 dev_dbg(dev, "scan: iter: %s dport_dev: %s parent: %s\n",
1482 dev_name(iter), dev_name(dport_dev),
1483 dev_name(uport_dev));
1484 port = find_cxl_port(dport_dev, &dport);
1486 dev_dbg(&cxlmd->dev,
1487 "found already registered port %s:%s\n",
1488 dev_name(&port->dev),
1489 dev_name(port->uport_dev));
1490 rc = cxl_add_ep(dport, &cxlmd->dev);
1493 * If the endpoint already exists in the port's list,
1494 * that's ok, it was added on a previous pass.
1495 * Otherwise, retry in add_port_attach_ep() after taking
1496 * the parent_port lock as the current port may be being
1499 if (rc && rc != -EBUSY) {
1500 put_device(&port->dev);
1504 /* Any more ports to add between this one and the root? */
1505 if (!dev_is_cxl_root_child(&port->dev)) {
1506 put_device(&port->dev);
1510 put_device(&port->dev);
1514 rc = add_port_attach_ep(cxlmd, uport_dev, dport_dev);
1515 /* port missing, try to add parent */
1518 /* failed to add ep or port */
1521 /* port added, new descendants possible, start over */
1527 EXPORT_SYMBOL_NS_GPL(devm_cxl_enumerate_ports, CXL);
1529 struct cxl_port *cxl_pci_find_port(struct pci_dev *pdev,
1530 struct cxl_dport **dport)
1532 return find_cxl_port(pdev->dev.parent, dport);
1534 EXPORT_SYMBOL_NS_GPL(cxl_pci_find_port, CXL);
1536 struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd,
1537 struct cxl_dport **dport)
1539 return find_cxl_port(grandparent(&cxlmd->dev), dport);
1541 EXPORT_SYMBOL_NS_GPL(cxl_mem_find_port, CXL);
1543 static int decoder_populate_targets(struct cxl_switch_decoder *cxlsd,
1544 struct cxl_port *port, int *target_map)
1551 device_lock_assert(&port->dev);
1553 if (xa_empty(&port->dports))
1556 write_seqlock(&cxlsd->target_lock);
1557 for (i = 0; i < cxlsd->nr_targets; i++) {
1558 struct cxl_dport *dport = find_dport(port, target_map[i]);
1564 cxlsd->target[i] = dport;
1566 write_sequnlock(&cxlsd->target_lock);
1571 struct cxl_dport *cxl_hb_modulo(struct cxl_root_decoder *cxlrd, int pos)
1573 struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd;
1574 struct cxl_decoder *cxld = &cxlsd->cxld;
1577 iw = cxld->interleave_ways;
1578 if (dev_WARN_ONCE(&cxld->dev, iw != cxlsd->nr_targets,
1579 "misconfigured root decoder\n"))
1582 return cxlrd->cxlsd.target[pos % iw];
1584 EXPORT_SYMBOL_NS_GPL(cxl_hb_modulo, CXL);
1586 static struct lock_class_key cxl_decoder_key;
1589 * cxl_decoder_init - Common decoder setup / initialization
1590 * @port: owning port of this decoder
1591 * @cxld: common decoder properties to initialize
1593 * A port may contain one or more decoders. Each of those decoders
1594 * enable some address space for CXL.mem utilization. A decoder is
1595 * expected to be configured by the caller before registering via
1598 static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld)
1603 rc = ida_alloc(&port->decoder_ida, GFP_KERNEL);
1607 /* need parent to stick around to release the id */
1608 get_device(&port->dev);
1612 device_initialize(dev);
1613 lockdep_set_class(&dev->mutex, &cxl_decoder_key);
1614 device_set_pm_not_required(dev);
1615 dev->parent = &port->dev;
1616 dev->bus = &cxl_bus_type;
1618 /* Pre initialize an "empty" decoder */
1619 cxld->interleave_ways = 1;
1620 cxld->interleave_granularity = PAGE_SIZE;
1621 cxld->target_type = CXL_DECODER_HOSTONLYMEM;
1622 cxld->hpa_range = (struct range) {
1630 static int cxl_switch_decoder_init(struct cxl_port *port,
1631 struct cxl_switch_decoder *cxlsd,
1634 if (nr_targets > CXL_DECODER_MAX_INTERLEAVE)
1637 cxlsd->nr_targets = nr_targets;
1638 seqlock_init(&cxlsd->target_lock);
1639 return cxl_decoder_init(port, &cxlsd->cxld);
1643 * cxl_root_decoder_alloc - Allocate a root level decoder
1644 * @port: owning CXL root of this decoder
1645 * @nr_targets: static number of downstream targets
1646 * @calc_hb: which host bridge covers the n'th position by granularity
1648 * Return: A new cxl decoder to be registered by cxl_decoder_add(). A
1649 * 'CXL root' decoder is one that decodes from a top-level / static platform
1650 * firmware description of CXL resources into a CXL standard decode
1653 struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
1654 unsigned int nr_targets,
1655 cxl_calc_hb_fn calc_hb)
1657 struct cxl_root_decoder *cxlrd;
1658 struct cxl_switch_decoder *cxlsd;
1659 struct cxl_decoder *cxld;
1662 if (!is_cxl_root(port))
1663 return ERR_PTR(-EINVAL);
1665 cxlrd = kzalloc(struct_size(cxlrd, cxlsd.target, nr_targets),
1668 return ERR_PTR(-ENOMEM);
1670 cxlsd = &cxlrd->cxlsd;
1671 rc = cxl_switch_decoder_init(port, cxlsd, nr_targets);
1677 cxlrd->calc_hb = calc_hb;
1678 mutex_init(&cxlrd->range_lock);
1680 cxld = &cxlsd->cxld;
1681 cxld->dev.type = &cxl_decoder_root_type;
1683 * cxl_root_decoder_release() special cases negative ids to
1684 * detect memregion_alloc() failures.
1686 atomic_set(&cxlrd->region_id, -1);
1687 rc = memregion_alloc(GFP_KERNEL);
1689 put_device(&cxld->dev);
1693 atomic_set(&cxlrd->region_id, rc);
1696 EXPORT_SYMBOL_NS_GPL(cxl_root_decoder_alloc, CXL);
1699 * cxl_switch_decoder_alloc - Allocate a switch level decoder
1700 * @port: owning CXL switch port of this decoder
1701 * @nr_targets: max number of dynamically addressable downstream targets
1703 * Return: A new cxl decoder to be registered by cxl_decoder_add(). A
1704 * 'switch' decoder is any decoder that can be enumerated by PCIe
1705 * topology and the HDM Decoder Capability. This includes the decoders
1706 * that sit between Switch Upstream Ports / Switch Downstream Ports and
1707 * Host Bridges / Root Ports.
1709 struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port,
1710 unsigned int nr_targets)
1712 struct cxl_switch_decoder *cxlsd;
1713 struct cxl_decoder *cxld;
1716 if (is_cxl_root(port) || is_cxl_endpoint(port))
1717 return ERR_PTR(-EINVAL);
1719 cxlsd = kzalloc(struct_size(cxlsd, target, nr_targets), GFP_KERNEL);
1721 return ERR_PTR(-ENOMEM);
1723 rc = cxl_switch_decoder_init(port, cxlsd, nr_targets);
1729 cxld = &cxlsd->cxld;
1730 cxld->dev.type = &cxl_decoder_switch_type;
1733 EXPORT_SYMBOL_NS_GPL(cxl_switch_decoder_alloc, CXL);
1736 * cxl_endpoint_decoder_alloc - Allocate an endpoint decoder
1737 * @port: owning port of this decoder
1739 * Return: A new cxl decoder to be registered by cxl_decoder_add()
1741 struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port)
1743 struct cxl_endpoint_decoder *cxled;
1744 struct cxl_decoder *cxld;
1747 if (!is_cxl_endpoint(port))
1748 return ERR_PTR(-EINVAL);
1750 cxled = kzalloc(sizeof(*cxled), GFP_KERNEL);
1752 return ERR_PTR(-ENOMEM);
1755 cxld = &cxled->cxld;
1756 rc = cxl_decoder_init(port, cxld);
1762 cxld->dev.type = &cxl_decoder_endpoint_type;
1765 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_decoder_alloc, CXL);
1768 * cxl_decoder_add_locked - Add a decoder with targets
1769 * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc()
1770 * @target_map: A list of downstream ports that this decoder can direct memory
1771 * traffic to. These numbers should correspond with the port number
1772 * in the PCIe Link Capabilities structure.
1774 * Certain types of decoders may not have any targets. The main example of this
1775 * is an endpoint device. A more awkward example is a hostbridge whose root
1776 * ports get hot added (technically possible, though unlikely).
1778 * This is the locked variant of cxl_decoder_add().
1780 * Context: Process context. Expects the device lock of the port that owns the
1783 * Return: Negative error code if the decoder wasn't properly configured; else
1786 int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map)
1788 struct cxl_port *port;
1792 if (WARN_ON_ONCE(!cxld))
1795 if (WARN_ON_ONCE(IS_ERR(cxld)))
1796 return PTR_ERR(cxld);
1798 if (cxld->interleave_ways < 1)
1803 port = to_cxl_port(cxld->dev.parent);
1804 if (!is_endpoint_decoder(dev)) {
1805 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
1807 rc = decoder_populate_targets(cxlsd, port, target_map);
1808 if (rc && (cxld->flags & CXL_DECODER_F_ENABLE)) {
1810 "Failed to populate active decoder targets\n");
1815 rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
1819 return device_add(dev);
1821 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add_locked, CXL);
1824 * cxl_decoder_add - Add a decoder with targets
1825 * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc()
1826 * @target_map: A list of downstream ports that this decoder can direct memory
1827 * traffic to. These numbers should correspond with the port number
1828 * in the PCIe Link Capabilities structure.
1830 * This is the unlocked variant of cxl_decoder_add_locked().
1831 * See cxl_decoder_add_locked().
1833 * Context: Process context. Takes and releases the device lock of the port that
1836 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
1838 struct cxl_port *port;
1841 if (WARN_ON_ONCE(!cxld))
1844 if (WARN_ON_ONCE(IS_ERR(cxld)))
1845 return PTR_ERR(cxld);
1847 port = to_cxl_port(cxld->dev.parent);
1849 device_lock(&port->dev);
1850 rc = cxl_decoder_add_locked(cxld, target_map);
1851 device_unlock(&port->dev);
1855 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, CXL);
1857 static void cxld_unregister(void *dev)
1859 struct cxl_endpoint_decoder *cxled;
1861 if (is_endpoint_decoder(dev)) {
1862 cxled = to_cxl_endpoint_decoder(dev);
1863 cxl_decoder_kill_region(cxled);
1866 device_unregister(dev);
1869 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld)
1871 return devm_add_action_or_reset(host, cxld_unregister, &cxld->dev);
1873 EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, CXL);
1876 * __cxl_driver_register - register a driver for the cxl bus
1877 * @cxl_drv: cxl driver structure to attach
1878 * @owner: owning module/driver
1879 * @modname: KBUILD_MODNAME for parent driver
1881 int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
1882 const char *modname)
1884 if (!cxl_drv->probe) {
1885 pr_debug("%s ->probe() must be specified\n", modname);
1889 if (!cxl_drv->name) {
1890 pr_debug("%s ->name must be specified\n", modname);
1895 pr_debug("%s ->id must be specified\n", modname);
1899 cxl_drv->drv.bus = &cxl_bus_type;
1900 cxl_drv->drv.owner = owner;
1901 cxl_drv->drv.mod_name = modname;
1902 cxl_drv->drv.name = cxl_drv->name;
1904 return driver_register(&cxl_drv->drv);
1906 EXPORT_SYMBOL_NS_GPL(__cxl_driver_register, CXL);
1908 void cxl_driver_unregister(struct cxl_driver *cxl_drv)
1910 driver_unregister(&cxl_drv->drv);
1912 EXPORT_SYMBOL_NS_GPL(cxl_driver_unregister, CXL);
1914 static int cxl_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
1916 return add_uevent_var(env, "MODALIAS=" CXL_MODALIAS_FMT,
1917 cxl_device_id(dev));
1920 static int cxl_bus_match(struct device *dev, struct device_driver *drv)
1922 return cxl_device_id(dev) == to_cxl_drv(drv)->id;
1925 static int cxl_bus_probe(struct device *dev)
1929 rc = to_cxl_drv(dev->driver)->probe(dev);
1930 dev_dbg(dev, "probe: %d\n", rc);
1934 static void cxl_bus_remove(struct device *dev)
1936 struct cxl_driver *cxl_drv = to_cxl_drv(dev->driver);
1938 if (cxl_drv->remove)
1939 cxl_drv->remove(dev);
1942 static struct workqueue_struct *cxl_bus_wq;
1944 static void cxl_bus_rescan_queue(struct work_struct *w)
1946 int rc = bus_rescan_devices(&cxl_bus_type);
1948 pr_debug("CXL bus rescan result: %d\n", rc);
1951 void cxl_bus_rescan(void)
1953 static DECLARE_WORK(rescan_work, cxl_bus_rescan_queue);
1955 queue_work(cxl_bus_wq, &rescan_work);
1957 EXPORT_SYMBOL_NS_GPL(cxl_bus_rescan, CXL);
1959 void cxl_bus_drain(void)
1961 drain_workqueue(cxl_bus_wq);
1963 EXPORT_SYMBOL_NS_GPL(cxl_bus_drain, CXL);
1965 bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd)
1967 return queue_work(cxl_bus_wq, &cxlmd->detach_work);
1969 EXPORT_SYMBOL_NS_GPL(schedule_cxl_memdev_detach, CXL);
1971 /* for user tooling to ensure port disable work has completed */
1972 static ssize_t flush_store(const struct bus_type *bus, const char *buf, size_t count)
1974 if (sysfs_streq(buf, "1")) {
1975 flush_workqueue(cxl_bus_wq);
1982 static BUS_ATTR_WO(flush);
1984 static struct attribute *cxl_bus_attributes[] = {
1985 &bus_attr_flush.attr,
1989 static struct attribute_group cxl_bus_attribute_group = {
1990 .attrs = cxl_bus_attributes,
1993 static const struct attribute_group *cxl_bus_attribute_groups[] = {
1994 &cxl_bus_attribute_group,
1998 struct bus_type cxl_bus_type = {
2000 .uevent = cxl_bus_uevent,
2001 .match = cxl_bus_match,
2002 .probe = cxl_bus_probe,
2003 .remove = cxl_bus_remove,
2004 .bus_groups = cxl_bus_attribute_groups,
2006 EXPORT_SYMBOL_NS_GPL(cxl_bus_type, CXL);
2008 static struct dentry *cxl_debugfs;
2010 struct dentry *cxl_debugfs_create_dir(const char *dir)
2012 return debugfs_create_dir(dir, cxl_debugfs);
2014 EXPORT_SYMBOL_NS_GPL(cxl_debugfs_create_dir, CXL);
2016 static __init int cxl_core_init(void)
2020 cxl_debugfs = debugfs_create_dir("cxl", NULL);
2024 rc = cxl_memdev_init();
2028 cxl_bus_wq = alloc_ordered_workqueue("cxl_port", 0);
2034 rc = bus_register(&cxl_bus_type);
2038 rc = cxl_region_init();
2045 bus_unregister(&cxl_bus_type);
2047 destroy_workqueue(cxl_bus_wq);
2053 static void cxl_core_exit(void)
2056 bus_unregister(&cxl_bus_type);
2057 destroy_workqueue(cxl_bus_wq);
2059 debugfs_remove_recursive(cxl_debugfs);
2062 subsys_initcall(cxl_core_init);
2063 module_exit(cxl_core_exit);
2064 MODULE_LICENSE("GPL v2");