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.
32 * All changes to the interleave configuration occur with this lock held
35 DECLARE_RWSEM(cxl_region_rwsem);
37 static DEFINE_IDA(cxl_port_ida);
38 static DEFINE_XARRAY(cxl_root_buses);
40 int cxl_num_decoders_committed(struct cxl_port *port)
42 lockdep_assert_held(&cxl_region_rwsem);
44 return port->commit_end + 1;
47 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
50 return sysfs_emit(buf, "%s\n", dev->type->name);
52 static DEVICE_ATTR_RO(devtype);
54 static int cxl_device_id(const struct device *dev)
56 if (dev->type == &cxl_nvdimm_bridge_type)
57 return CXL_DEVICE_NVDIMM_BRIDGE;
58 if (dev->type == &cxl_nvdimm_type)
59 return CXL_DEVICE_NVDIMM;
60 if (dev->type == CXL_PMEM_REGION_TYPE())
61 return CXL_DEVICE_PMEM_REGION;
62 if (dev->type == CXL_DAX_REGION_TYPE())
63 return CXL_DEVICE_DAX_REGION;
64 if (is_cxl_port(dev)) {
65 if (is_cxl_root(to_cxl_port(dev)))
66 return CXL_DEVICE_ROOT;
67 return CXL_DEVICE_PORT;
69 if (is_cxl_memdev(dev))
70 return CXL_DEVICE_MEMORY_EXPANDER;
71 if (dev->type == CXL_REGION_TYPE())
72 return CXL_DEVICE_REGION;
73 if (dev->type == &cxl_pmu_type)
74 return CXL_DEVICE_PMU;
78 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
81 return sysfs_emit(buf, CXL_MODALIAS_FMT "\n", cxl_device_id(dev));
83 static DEVICE_ATTR_RO(modalias);
85 static struct attribute *cxl_base_attributes[] = {
86 &dev_attr_devtype.attr,
87 &dev_attr_modalias.attr,
91 struct attribute_group cxl_base_attribute_group = {
92 .attrs = cxl_base_attributes,
95 static ssize_t start_show(struct device *dev, struct device_attribute *attr,
98 struct cxl_decoder *cxld = to_cxl_decoder(dev);
100 return sysfs_emit(buf, "%#llx\n", cxld->hpa_range.start);
102 static DEVICE_ATTR_ADMIN_RO(start);
104 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
107 struct cxl_decoder *cxld = to_cxl_decoder(dev);
109 return sysfs_emit(buf, "%#llx\n", range_len(&cxld->hpa_range));
111 static DEVICE_ATTR_RO(size);
113 #define CXL_DECODER_FLAG_ATTR(name, flag) \
114 static ssize_t name##_show(struct device *dev, \
115 struct device_attribute *attr, char *buf) \
117 struct cxl_decoder *cxld = to_cxl_decoder(dev); \
119 return sysfs_emit(buf, "%s\n", \
120 (cxld->flags & (flag)) ? "1" : "0"); \
122 static DEVICE_ATTR_RO(name)
124 CXL_DECODER_FLAG_ATTR(cap_pmem, CXL_DECODER_F_PMEM);
125 CXL_DECODER_FLAG_ATTR(cap_ram, CXL_DECODER_F_RAM);
126 CXL_DECODER_FLAG_ATTR(cap_type2, CXL_DECODER_F_TYPE2);
127 CXL_DECODER_FLAG_ATTR(cap_type3, CXL_DECODER_F_TYPE3);
128 CXL_DECODER_FLAG_ATTR(locked, CXL_DECODER_F_LOCK);
130 static ssize_t target_type_show(struct device *dev,
131 struct device_attribute *attr, char *buf)
133 struct cxl_decoder *cxld = to_cxl_decoder(dev);
135 switch (cxld->target_type) {
136 case CXL_DECODER_DEVMEM:
137 return sysfs_emit(buf, "accelerator\n");
138 case CXL_DECODER_HOSTONLYMEM:
139 return sysfs_emit(buf, "expander\n");
143 static DEVICE_ATTR_RO(target_type);
145 static ssize_t emit_target_list(struct cxl_switch_decoder *cxlsd, char *buf)
147 struct cxl_decoder *cxld = &cxlsd->cxld;
151 for (i = 0; i < cxld->interleave_ways; i++) {
152 struct cxl_dport *dport = cxlsd->target[i];
153 struct cxl_dport *next = NULL;
158 if (i + 1 < cxld->interleave_ways)
159 next = cxlsd->target[i + 1];
160 rc = sysfs_emit_at(buf, offset, "%d%s", dport->port_id,
170 static ssize_t target_list_show(struct device *dev,
171 struct device_attribute *attr, char *buf)
173 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
177 guard(rwsem_read)(&cxl_region_rwsem);
178 rc = emit_target_list(cxlsd, buf);
183 rc = sysfs_emit_at(buf, offset, "\n");
189 static DEVICE_ATTR_RO(target_list);
191 static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
194 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
196 return sysfs_emit(buf, "%s\n", cxl_decoder_mode_name(cxled->mode));
199 static ssize_t mode_store(struct device *dev, struct device_attribute *attr,
200 const char *buf, size_t len)
202 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
203 enum cxl_decoder_mode mode;
206 if (sysfs_streq(buf, "pmem"))
207 mode = CXL_DECODER_PMEM;
208 else if (sysfs_streq(buf, "ram"))
209 mode = CXL_DECODER_RAM;
213 rc = cxl_dpa_set_mode(cxled, mode);
219 static DEVICE_ATTR_RW(mode);
221 static ssize_t dpa_resource_show(struct device *dev, struct device_attribute *attr,
224 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
226 guard(rwsem_read)(&cxl_dpa_rwsem);
227 return sysfs_emit(buf, "%#llx\n", (u64)cxl_dpa_resource_start(cxled));
229 static DEVICE_ATTR_RO(dpa_resource);
231 static ssize_t dpa_size_show(struct device *dev, struct device_attribute *attr,
234 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
235 resource_size_t size = cxl_dpa_size(cxled);
237 return sysfs_emit(buf, "%pa\n", &size);
240 static ssize_t dpa_size_store(struct device *dev, struct device_attribute *attr,
241 const char *buf, size_t len)
243 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
244 unsigned long long size;
247 rc = kstrtoull(buf, 0, &size);
251 if (!IS_ALIGNED(size, SZ_256M))
254 rc = cxl_dpa_free(cxled);
261 rc = cxl_dpa_alloc(cxled, size);
267 static DEVICE_ATTR_RW(dpa_size);
269 static ssize_t interleave_granularity_show(struct device *dev,
270 struct device_attribute *attr,
273 struct cxl_decoder *cxld = to_cxl_decoder(dev);
275 return sysfs_emit(buf, "%d\n", cxld->interleave_granularity);
278 static DEVICE_ATTR_RO(interleave_granularity);
280 static ssize_t interleave_ways_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
283 struct cxl_decoder *cxld = to_cxl_decoder(dev);
285 return sysfs_emit(buf, "%d\n", cxld->interleave_ways);
288 static DEVICE_ATTR_RO(interleave_ways);
290 static struct attribute *cxl_decoder_base_attrs[] = {
291 &dev_attr_start.attr,
293 &dev_attr_locked.attr,
294 &dev_attr_interleave_granularity.attr,
295 &dev_attr_interleave_ways.attr,
299 static struct attribute_group cxl_decoder_base_attribute_group = {
300 .attrs = cxl_decoder_base_attrs,
303 static struct attribute *cxl_decoder_root_attrs[] = {
304 &dev_attr_cap_pmem.attr,
305 &dev_attr_cap_ram.attr,
306 &dev_attr_cap_type2.attr,
307 &dev_attr_cap_type3.attr,
308 &dev_attr_target_list.attr,
309 SET_CXL_REGION_ATTR(create_pmem_region)
310 SET_CXL_REGION_ATTR(create_ram_region)
311 SET_CXL_REGION_ATTR(delete_region)
315 static bool can_create_pmem(struct cxl_root_decoder *cxlrd)
317 unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_PMEM;
319 return (cxlrd->cxlsd.cxld.flags & flags) == flags;
322 static bool can_create_ram(struct cxl_root_decoder *cxlrd)
324 unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_RAM;
326 return (cxlrd->cxlsd.cxld.flags & flags) == flags;
329 static umode_t cxl_root_decoder_visible(struct kobject *kobj, struct attribute *a, int n)
331 struct device *dev = kobj_to_dev(kobj);
332 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
334 if (a == CXL_REGION_ATTR(create_pmem_region) && !can_create_pmem(cxlrd))
337 if (a == CXL_REGION_ATTR(create_ram_region) && !can_create_ram(cxlrd))
340 if (a == CXL_REGION_ATTR(delete_region) &&
341 !(can_create_pmem(cxlrd) || can_create_ram(cxlrd)))
347 static struct attribute_group cxl_decoder_root_attribute_group = {
348 .attrs = cxl_decoder_root_attrs,
349 .is_visible = cxl_root_decoder_visible,
352 static const struct attribute_group *cxl_decoder_root_attribute_groups[] = {
353 &cxl_decoder_root_attribute_group,
354 &cxl_decoder_base_attribute_group,
355 &cxl_base_attribute_group,
359 static struct attribute *cxl_decoder_switch_attrs[] = {
360 &dev_attr_target_type.attr,
361 &dev_attr_target_list.attr,
362 SET_CXL_REGION_ATTR(region)
366 static struct attribute_group cxl_decoder_switch_attribute_group = {
367 .attrs = cxl_decoder_switch_attrs,
370 static const struct attribute_group *cxl_decoder_switch_attribute_groups[] = {
371 &cxl_decoder_switch_attribute_group,
372 &cxl_decoder_base_attribute_group,
373 &cxl_base_attribute_group,
377 static struct attribute *cxl_decoder_endpoint_attrs[] = {
378 &dev_attr_target_type.attr,
380 &dev_attr_dpa_size.attr,
381 &dev_attr_dpa_resource.attr,
382 SET_CXL_REGION_ATTR(region)
386 static struct attribute_group cxl_decoder_endpoint_attribute_group = {
387 .attrs = cxl_decoder_endpoint_attrs,
390 static const struct attribute_group *cxl_decoder_endpoint_attribute_groups[] = {
391 &cxl_decoder_base_attribute_group,
392 &cxl_decoder_endpoint_attribute_group,
393 &cxl_base_attribute_group,
397 static void __cxl_decoder_release(struct cxl_decoder *cxld)
399 struct cxl_port *port = to_cxl_port(cxld->dev.parent);
401 ida_free(&port->decoder_ida, cxld->id);
402 put_device(&port->dev);
405 static void cxl_endpoint_decoder_release(struct device *dev)
407 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
409 __cxl_decoder_release(&cxled->cxld);
413 static void cxl_switch_decoder_release(struct device *dev)
415 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
417 __cxl_decoder_release(&cxlsd->cxld);
421 struct cxl_root_decoder *to_cxl_root_decoder(struct device *dev)
423 if (dev_WARN_ONCE(dev, !is_root_decoder(dev),
424 "not a cxl_root_decoder device\n"))
426 return container_of(dev, struct cxl_root_decoder, cxlsd.cxld.dev);
428 EXPORT_SYMBOL_NS_GPL(to_cxl_root_decoder, CXL);
430 static void cxl_root_decoder_release(struct device *dev)
432 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
434 if (atomic_read(&cxlrd->region_id) >= 0)
435 memregion_free(atomic_read(&cxlrd->region_id));
436 __cxl_decoder_release(&cxlrd->cxlsd.cxld);
440 static const struct device_type cxl_decoder_endpoint_type = {
441 .name = "cxl_decoder_endpoint",
442 .release = cxl_endpoint_decoder_release,
443 .groups = cxl_decoder_endpoint_attribute_groups,
446 static const struct device_type cxl_decoder_switch_type = {
447 .name = "cxl_decoder_switch",
448 .release = cxl_switch_decoder_release,
449 .groups = cxl_decoder_switch_attribute_groups,
452 static const struct device_type cxl_decoder_root_type = {
453 .name = "cxl_decoder_root",
454 .release = cxl_root_decoder_release,
455 .groups = cxl_decoder_root_attribute_groups,
458 bool is_endpoint_decoder(struct device *dev)
460 return dev->type == &cxl_decoder_endpoint_type;
462 EXPORT_SYMBOL_NS_GPL(is_endpoint_decoder, CXL);
464 bool is_root_decoder(struct device *dev)
466 return dev->type == &cxl_decoder_root_type;
468 EXPORT_SYMBOL_NS_GPL(is_root_decoder, CXL);
470 bool is_switch_decoder(struct device *dev)
472 return is_root_decoder(dev) || dev->type == &cxl_decoder_switch_type;
474 EXPORT_SYMBOL_NS_GPL(is_switch_decoder, CXL);
476 struct cxl_decoder *to_cxl_decoder(struct device *dev)
478 if (dev_WARN_ONCE(dev,
479 !is_switch_decoder(dev) && !is_endpoint_decoder(dev),
480 "not a cxl_decoder device\n"))
482 return container_of(dev, struct cxl_decoder, dev);
484 EXPORT_SYMBOL_NS_GPL(to_cxl_decoder, CXL);
486 struct cxl_endpoint_decoder *to_cxl_endpoint_decoder(struct device *dev)
488 if (dev_WARN_ONCE(dev, !is_endpoint_decoder(dev),
489 "not a cxl_endpoint_decoder device\n"))
491 return container_of(dev, struct cxl_endpoint_decoder, cxld.dev);
493 EXPORT_SYMBOL_NS_GPL(to_cxl_endpoint_decoder, CXL);
495 struct cxl_switch_decoder *to_cxl_switch_decoder(struct device *dev)
497 if (dev_WARN_ONCE(dev, !is_switch_decoder(dev),
498 "not a cxl_switch_decoder device\n"))
500 return container_of(dev, struct cxl_switch_decoder, cxld.dev);
502 EXPORT_SYMBOL_NS_GPL(to_cxl_switch_decoder, CXL);
504 static void cxl_ep_release(struct cxl_ep *ep)
510 static void cxl_ep_remove(struct cxl_port *port, struct cxl_ep *ep)
514 xa_erase(&port->endpoints, (unsigned long) ep->ep);
518 static void cxl_port_release(struct device *dev)
520 struct cxl_port *port = to_cxl_port(dev);
524 xa_for_each(&port->endpoints, index, ep)
525 cxl_ep_remove(port, ep);
526 xa_destroy(&port->endpoints);
527 xa_destroy(&port->dports);
528 xa_destroy(&port->regions);
529 ida_free(&cxl_port_ida, port->id);
533 static const struct attribute_group *cxl_port_attribute_groups[] = {
534 &cxl_base_attribute_group,
538 static const struct device_type cxl_port_type = {
540 .release = cxl_port_release,
541 .groups = cxl_port_attribute_groups,
544 bool is_cxl_port(const struct device *dev)
546 return dev->type == &cxl_port_type;
548 EXPORT_SYMBOL_NS_GPL(is_cxl_port, CXL);
550 struct cxl_port *to_cxl_port(const struct device *dev)
552 if (dev_WARN_ONCE(dev, dev->type != &cxl_port_type,
553 "not a cxl_port device\n"))
555 return container_of(dev, struct cxl_port, dev);
557 EXPORT_SYMBOL_NS_GPL(to_cxl_port, CXL);
559 static void unregister_port(void *_port)
561 struct cxl_port *port = _port;
562 struct cxl_port *parent;
563 struct device *lock_dev;
565 if (is_cxl_root(port))
568 parent = to_cxl_port(port->dev.parent);
571 * CXL root port's and the first level of ports are unregistered
572 * under the platform firmware device lock, all other ports are
573 * unregistered while holding their parent port lock.
576 lock_dev = port->uport_dev;
577 else if (is_cxl_root(parent))
578 lock_dev = parent->uport_dev;
580 lock_dev = &parent->dev;
582 device_lock_assert(lock_dev);
584 device_unregister(&port->dev);
587 static void cxl_unlink_uport(void *_port)
589 struct cxl_port *port = _port;
591 sysfs_remove_link(&port->dev.kobj, "uport");
594 static int devm_cxl_link_uport(struct device *host, struct cxl_port *port)
598 rc = sysfs_create_link(&port->dev.kobj, &port->uport_dev->kobj,
602 return devm_add_action_or_reset(host, cxl_unlink_uport, port);
605 static void cxl_unlink_parent_dport(void *_port)
607 struct cxl_port *port = _port;
609 sysfs_remove_link(&port->dev.kobj, "parent_dport");
612 static int devm_cxl_link_parent_dport(struct device *host,
613 struct cxl_port *port,
614 struct cxl_dport *parent_dport)
621 rc = sysfs_create_link(&port->dev.kobj, &parent_dport->dport_dev->kobj,
625 return devm_add_action_or_reset(host, cxl_unlink_parent_dport, port);
628 static struct lock_class_key cxl_port_key;
630 static struct cxl_port *cxl_port_alloc(struct device *uport_dev,
631 resource_size_t component_reg_phys,
632 struct cxl_dport *parent_dport)
634 struct cxl_port *port;
638 port = kzalloc(sizeof(*port), GFP_KERNEL);
640 return ERR_PTR(-ENOMEM);
642 rc = ida_alloc(&cxl_port_ida, GFP_KERNEL);
646 port->uport_dev = uport_dev;
649 * The top-level cxl_port "cxl_root" does not have a cxl_port as
650 * its parent and it does not have any corresponding component
651 * registers as its decode is described by a fixed platform
656 struct cxl_port *parent_port = parent_dport->port;
657 struct cxl_port *iter;
659 dev->parent = &parent_port->dev;
660 port->depth = parent_port->depth + 1;
661 port->parent_dport = parent_dport;
664 * walk to the host bridge, or the first ancestor that knows
668 while (!iter->host_bridge &&
669 !is_cxl_root(to_cxl_port(iter->dev.parent)))
670 iter = to_cxl_port(iter->dev.parent);
671 if (iter->host_bridge)
672 port->host_bridge = iter->host_bridge;
673 else if (parent_dport->rch)
674 port->host_bridge = parent_dport->dport_dev;
676 port->host_bridge = iter->uport_dev;
677 dev_dbg(uport_dev, "host-bridge: %s\n",
678 dev_name(port->host_bridge));
680 dev->parent = uport_dev;
682 port->component_reg_phys = component_reg_phys;
683 ida_init(&port->decoder_ida);
685 port->commit_end = -1;
686 xa_init(&port->dports);
687 xa_init(&port->endpoints);
688 xa_init(&port->regions);
690 device_initialize(dev);
691 lockdep_set_class_and_subclass(&dev->mutex, &cxl_port_key, port->depth);
692 device_set_pm_not_required(dev);
693 dev->bus = &cxl_bus_type;
694 dev->type = &cxl_port_type;
703 static int cxl_setup_comp_regs(struct device *host, struct cxl_register_map *map,
704 resource_size_t component_reg_phys)
706 if (component_reg_phys == CXL_RESOURCE_NONE)
709 *map = (struct cxl_register_map) {
711 .reg_type = CXL_REGLOC_RBI_COMPONENT,
712 .resource = component_reg_phys,
713 .max_size = CXL_COMPONENT_REG_BLOCK_SIZE,
716 return cxl_setup_regs(map);
719 static int cxl_port_setup_regs(struct cxl_port *port,
720 resource_size_t component_reg_phys)
722 if (dev_is_platform(port->uport_dev))
724 return cxl_setup_comp_regs(&port->dev, &port->comp_map,
728 static int cxl_dport_setup_regs(struct device *host, struct cxl_dport *dport,
729 resource_size_t component_reg_phys)
733 if (dev_is_platform(dport->dport_dev))
737 * use @dport->dport_dev for the context for error messages during
738 * register probing, and fixup @host after the fact, since @host may be
741 rc = cxl_setup_comp_regs(dport->dport_dev, &dport->comp_map,
743 dport->comp_map.host = host;
747 static struct cxl_port *__devm_cxl_add_port(struct device *host,
748 struct device *uport_dev,
749 resource_size_t component_reg_phys,
750 struct cxl_dport *parent_dport)
752 struct cxl_port *port;
756 port = cxl_port_alloc(uport_dev, component_reg_phys, parent_dport);
761 if (is_cxl_memdev(uport_dev))
762 rc = dev_set_name(dev, "endpoint%d", port->id);
763 else if (parent_dport)
764 rc = dev_set_name(dev, "port%d", port->id);
766 rc = dev_set_name(dev, "root%d", port->id);
770 rc = cxl_port_setup_regs(port, component_reg_phys);
774 rc = device_add(dev);
778 rc = devm_add_action_or_reset(host, unregister_port, port);
782 rc = devm_cxl_link_uport(host, port);
786 rc = devm_cxl_link_parent_dport(host, port, parent_dport);
798 * devm_cxl_add_port - register a cxl_port in CXL memory decode hierarchy
799 * @host: host device for devm operations
800 * @uport_dev: "physical" device implementing this upstream port
801 * @component_reg_phys: (optional) for configurable cxl_port instances
802 * @parent_dport: next hop up in the CXL memory decode hierarchy
804 struct cxl_port *devm_cxl_add_port(struct device *host,
805 struct device *uport_dev,
806 resource_size_t component_reg_phys,
807 struct cxl_dport *parent_dport)
809 struct cxl_port *port, *parent_port;
811 port = __devm_cxl_add_port(host, uport_dev, component_reg_phys,
814 parent_port = parent_dport ? parent_dport->port : NULL;
816 dev_dbg(uport_dev, "Failed to add%s%s%s: %ld\n",
817 parent_port ? " port to " : "",
818 parent_port ? dev_name(&parent_port->dev) : "",
819 parent_port ? "" : " root port",
822 dev_dbg(uport_dev, "%s added%s%s%s\n",
823 dev_name(&port->dev),
824 parent_port ? " to " : "",
825 parent_port ? dev_name(&parent_port->dev) : "",
826 parent_port ? "" : " (root port)");
831 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_port, CXL);
833 struct pci_bus *cxl_port_to_pci_bus(struct cxl_port *port)
835 /* There is no pci_bus associated with a CXL platform-root port */
836 if (is_cxl_root(port))
839 if (dev_is_pci(port->uport_dev)) {
840 struct pci_dev *pdev = to_pci_dev(port->uport_dev);
842 return pdev->subordinate;
845 return xa_load(&cxl_root_buses, (unsigned long)port->uport_dev);
847 EXPORT_SYMBOL_NS_GPL(cxl_port_to_pci_bus, CXL);
849 static void unregister_pci_bus(void *uport_dev)
851 xa_erase(&cxl_root_buses, (unsigned long)uport_dev);
854 int devm_cxl_register_pci_bus(struct device *host, struct device *uport_dev,
859 if (dev_is_pci(uport_dev))
862 rc = xa_insert(&cxl_root_buses, (unsigned long)uport_dev, bus,
866 return devm_add_action_or_reset(host, unregister_pci_bus, uport_dev);
868 EXPORT_SYMBOL_NS_GPL(devm_cxl_register_pci_bus, CXL);
870 static bool dev_is_cxl_root_child(struct device *dev)
872 struct cxl_port *port, *parent;
874 if (!is_cxl_port(dev))
877 port = to_cxl_port(dev);
878 if (is_cxl_root(port))
881 parent = to_cxl_port(port->dev.parent);
882 if (is_cxl_root(parent))
888 struct cxl_port *find_cxl_root(struct cxl_port *port)
890 struct cxl_port *iter = port;
892 while (iter && !is_cxl_root(iter))
893 iter = to_cxl_port(iter->dev.parent);
897 get_device(&iter->dev);
900 EXPORT_SYMBOL_NS_GPL(find_cxl_root, CXL);
902 static struct cxl_dport *find_dport(struct cxl_port *port, int id)
904 struct cxl_dport *dport;
907 device_lock_assert(&port->dev);
908 xa_for_each(&port->dports, index, dport)
909 if (dport->port_id == id)
914 static int add_dport(struct cxl_port *port, struct cxl_dport *dport)
916 struct cxl_dport *dup;
919 device_lock_assert(&port->dev);
920 dup = find_dport(port, dport->port_id);
923 "unable to add dport%d-%s non-unique port id (%s)\n",
924 dport->port_id, dev_name(dport->dport_dev),
925 dev_name(dup->dport_dev));
929 rc = xa_insert(&port->dports, (unsigned long)dport->dport_dev, dport,
939 * Since root-level CXL dports cannot be enumerated by PCI they are not
940 * enumerated by the common port driver that acquires the port lock over
941 * dport add/remove. Instead, root dports are manually added by a
942 * platform driver and cond_cxl_root_lock() is used to take the missing
943 * port lock in that case.
945 static void cond_cxl_root_lock(struct cxl_port *port)
947 if (is_cxl_root(port))
948 device_lock(&port->dev);
951 static void cond_cxl_root_unlock(struct cxl_port *port)
953 if (is_cxl_root(port))
954 device_unlock(&port->dev);
957 static void cxl_dport_remove(void *data)
959 struct cxl_dport *dport = data;
960 struct cxl_port *port = dport->port;
962 xa_erase(&port->dports, (unsigned long) dport->dport_dev);
963 put_device(dport->dport_dev);
966 static void cxl_dport_unlink(void *data)
968 struct cxl_dport *dport = data;
969 struct cxl_port *port = dport->port;
970 char link_name[CXL_TARGET_STRLEN];
972 sprintf(link_name, "dport%d", dport->port_id);
973 sysfs_remove_link(&port->dev.kobj, link_name);
976 static struct cxl_dport *
977 __devm_cxl_add_dport(struct cxl_port *port, struct device *dport_dev,
978 int port_id, resource_size_t component_reg_phys,
979 resource_size_t rcrb)
981 char link_name[CXL_TARGET_STRLEN];
982 struct cxl_dport *dport;
986 if (is_cxl_root(port))
987 host = port->uport_dev;
992 dev_WARN_ONCE(&port->dev, 1, "dport:%s bad devm context\n",
993 dev_name(dport_dev));
994 return ERR_PTR(-ENXIO);
997 if (snprintf(link_name, CXL_TARGET_STRLEN, "dport%d", port_id) >=
999 return ERR_PTR(-EINVAL);
1001 dport = devm_kzalloc(host, sizeof(*dport), GFP_KERNEL);
1003 return ERR_PTR(-ENOMEM);
1005 dport->dport_dev = dport_dev;
1006 dport->port_id = port_id;
1009 if (rcrb == CXL_RESOURCE_NONE) {
1010 rc = cxl_dport_setup_regs(&port->dev, dport,
1011 component_reg_phys);
1015 dport->rcrb.base = rcrb;
1016 component_reg_phys = __rcrb_to_component(dport_dev, &dport->rcrb,
1017 CXL_RCRB_DOWNSTREAM);
1018 if (component_reg_phys == CXL_RESOURCE_NONE) {
1019 dev_warn(dport_dev, "Invalid Component Registers in RCRB");
1020 return ERR_PTR(-ENXIO);
1024 * RCH @dport is not ready to map until associated with its
1027 rc = cxl_dport_setup_regs(NULL, dport, component_reg_phys);
1034 if (component_reg_phys != CXL_RESOURCE_NONE)
1035 dev_dbg(dport_dev, "Component Registers found for dport: %pa\n",
1036 &component_reg_phys);
1038 cond_cxl_root_lock(port);
1039 rc = add_dport(port, dport);
1040 cond_cxl_root_unlock(port);
1044 get_device(dport_dev);
1045 rc = devm_add_action_or_reset(host, cxl_dport_remove, dport);
1049 rc = sysfs_create_link(&port->dev.kobj, &dport_dev->kobj, link_name);
1053 rc = devm_add_action_or_reset(host, cxl_dport_unlink, dport);
1061 * devm_cxl_add_dport - append VH downstream port data to a cxl_port
1062 * @port: the cxl_port that references this dport
1063 * @dport_dev: firmware or PCI device representing the dport
1064 * @port_id: identifier for this dport in a decoder's target list
1065 * @component_reg_phys: optional location of CXL component registers
1067 * Note that dports are appended to the devm release action's of the
1068 * either the port's host (for root ports), or the port itself (for
1071 struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port,
1072 struct device *dport_dev, int port_id,
1073 resource_size_t component_reg_phys)
1075 struct cxl_dport *dport;
1077 dport = __devm_cxl_add_dport(port, dport_dev, port_id,
1078 component_reg_phys, CXL_RESOURCE_NONE);
1079 if (IS_ERR(dport)) {
1080 dev_dbg(dport_dev, "failed to add dport to %s: %ld\n",
1081 dev_name(&port->dev), PTR_ERR(dport));
1083 dev_dbg(dport_dev, "dport added to %s\n",
1084 dev_name(&port->dev));
1089 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_dport, CXL);
1092 * devm_cxl_add_rch_dport - append RCH downstream port data to a cxl_port
1093 * @port: the cxl_port that references this dport
1094 * @dport_dev: firmware or PCI device representing the dport
1095 * @port_id: identifier for this dport in a decoder's target list
1096 * @rcrb: mandatory location of a Root Complex Register Block
1098 * See CXL 3.0 9.11.8 CXL Devices Attached to an RCH
1100 struct cxl_dport *devm_cxl_add_rch_dport(struct cxl_port *port,
1101 struct device *dport_dev, int port_id,
1102 resource_size_t rcrb)
1104 struct cxl_dport *dport;
1106 if (rcrb == CXL_RESOURCE_NONE) {
1107 dev_dbg(&port->dev, "failed to add RCH dport, missing RCRB\n");
1108 return ERR_PTR(-EINVAL);
1111 dport = __devm_cxl_add_dport(port, dport_dev, port_id,
1112 CXL_RESOURCE_NONE, rcrb);
1113 if (IS_ERR(dport)) {
1114 dev_dbg(dport_dev, "failed to add RCH dport to %s: %ld\n",
1115 dev_name(&port->dev), PTR_ERR(dport));
1117 dev_dbg(dport_dev, "RCH dport added to %s\n",
1118 dev_name(&port->dev));
1123 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_rch_dport, CXL);
1125 static int add_ep(struct cxl_ep *new)
1127 struct cxl_port *port = new->dport->port;
1130 device_lock(&port->dev);
1132 device_unlock(&port->dev);
1135 rc = xa_insert(&port->endpoints, (unsigned long)new->ep, new,
1137 device_unlock(&port->dev);
1143 * cxl_add_ep - register an endpoint's interest in a port
1144 * @dport: the dport that routes to @ep_dev
1145 * @ep_dev: device representing the endpoint
1147 * Intermediate CXL ports are scanned based on the arrival of endpoints.
1148 * When those endpoints depart the port can be destroyed once all
1149 * endpoints that care about that port have been removed.
1151 static int cxl_add_ep(struct cxl_dport *dport, struct device *ep_dev)
1156 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1160 ep->ep = get_device(ep_dev);
1169 struct cxl_find_port_ctx {
1170 const struct device *dport_dev;
1171 const struct cxl_port *parent_port;
1172 struct cxl_dport **dport;
1175 static int match_port_by_dport(struct device *dev, const void *data)
1177 const struct cxl_find_port_ctx *ctx = data;
1178 struct cxl_dport *dport;
1179 struct cxl_port *port;
1181 if (!is_cxl_port(dev))
1183 if (ctx->parent_port && dev->parent != &ctx->parent_port->dev)
1186 port = to_cxl_port(dev);
1187 dport = cxl_find_dport_by_dev(port, ctx->dport_dev);
1189 *ctx->dport = dport;
1190 return dport != NULL;
1193 static struct cxl_port *__find_cxl_port(struct cxl_find_port_ctx *ctx)
1197 if (!ctx->dport_dev)
1200 dev = bus_find_device(&cxl_bus_type, NULL, ctx, match_port_by_dport);
1202 return to_cxl_port(dev);
1206 static struct cxl_port *find_cxl_port(struct device *dport_dev,
1207 struct cxl_dport **dport)
1209 struct cxl_find_port_ctx ctx = {
1210 .dport_dev = dport_dev,
1213 struct cxl_port *port;
1215 port = __find_cxl_port(&ctx);
1219 static struct cxl_port *find_cxl_port_at(struct cxl_port *parent_port,
1220 struct device *dport_dev,
1221 struct cxl_dport **dport)
1223 struct cxl_find_port_ctx ctx = {
1224 .dport_dev = dport_dev,
1225 .parent_port = parent_port,
1228 struct cxl_port *port;
1230 port = __find_cxl_port(&ctx);
1235 * All users of grandparent() are using it to walk PCIe-like switch port
1236 * hierarchy. A PCIe switch is comprised of a bridge device representing the
1237 * upstream switch port and N bridges representing downstream switch ports. When
1238 * bridges stack the grand-parent of a downstream switch port is another
1239 * downstream switch port in the immediate ancestor switch.
1241 static struct device *grandparent(struct device *dev)
1243 if (dev && dev->parent)
1244 return dev->parent->parent;
1248 static struct device *endpoint_host(struct cxl_port *endpoint)
1250 struct cxl_port *port = to_cxl_port(endpoint->dev.parent);
1252 if (is_cxl_root(port))
1253 return port->uport_dev;
1257 static void delete_endpoint(void *data)
1259 struct cxl_memdev *cxlmd = data;
1260 struct cxl_port *endpoint = cxlmd->endpoint;
1261 struct device *host = endpoint_host(endpoint);
1264 if (host->driver && !endpoint->dead) {
1265 devm_release_action(host, cxl_unlink_parent_dport, endpoint);
1266 devm_release_action(host, cxl_unlink_uport, endpoint);
1267 devm_release_action(host, unregister_port, endpoint);
1269 cxlmd->endpoint = NULL;
1270 device_unlock(host);
1271 put_device(&endpoint->dev);
1275 int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint)
1277 struct device *host = endpoint_host(endpoint);
1278 struct device *dev = &cxlmd->dev;
1281 get_device(&endpoint->dev);
1282 cxlmd->endpoint = endpoint;
1283 cxlmd->depth = endpoint->depth;
1284 return devm_add_action_or_reset(dev, delete_endpoint, cxlmd);
1286 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_autoremove, CXL);
1289 * The natural end of life of a non-root 'cxl_port' is when its parent port goes
1290 * through a ->remove() event ("top-down" unregistration). The unnatural trigger
1291 * for a port to be unregistered is when all memdevs beneath that port have gone
1292 * through ->remove(). This "bottom-up" removal selectively removes individual
1293 * child ports manually. This depends on devm_cxl_add_port() to not change is
1294 * devm action registration order, and for dports to have already been
1295 * destroyed by reap_dports().
1297 static void delete_switch_port(struct cxl_port *port)
1299 devm_release_action(port->dev.parent, cxl_unlink_parent_dport, port);
1300 devm_release_action(port->dev.parent, cxl_unlink_uport, port);
1301 devm_release_action(port->dev.parent, unregister_port, port);
1304 static void reap_dports(struct cxl_port *port)
1306 struct cxl_dport *dport;
1307 unsigned long index;
1309 device_lock_assert(&port->dev);
1311 xa_for_each(&port->dports, index, dport) {
1312 devm_release_action(&port->dev, cxl_dport_unlink, dport);
1313 devm_release_action(&port->dev, cxl_dport_remove, dport);
1314 devm_kfree(&port->dev, dport);
1319 struct cxl_memdev *cxlmd;
1323 static int port_has_memdev(struct device *dev, const void *data)
1325 const struct detach_ctx *ctx = data;
1326 struct cxl_port *port;
1328 if (!is_cxl_port(dev))
1331 port = to_cxl_port(dev);
1332 if (port->depth != ctx->depth)
1335 return !!cxl_ep_load(port, ctx->cxlmd);
1338 static void cxl_detach_ep(void *data)
1340 struct cxl_memdev *cxlmd = data;
1342 for (int i = cxlmd->depth - 1; i >= 1; i--) {
1343 struct cxl_port *port, *parent_port;
1344 struct detach_ctx ctx = {
1352 dev = bus_find_device(&cxl_bus_type, NULL, &ctx,
1356 port = to_cxl_port(dev);
1358 parent_port = to_cxl_port(port->dev.parent);
1359 device_lock(&parent_port->dev);
1360 device_lock(&port->dev);
1361 ep = cxl_ep_load(port, cxlmd);
1362 dev_dbg(&cxlmd->dev, "disconnect %s from %s\n",
1363 ep ? dev_name(ep->ep) : "", dev_name(&port->dev));
1364 cxl_ep_remove(port, ep);
1365 if (ep && !port->dead && xa_empty(&port->endpoints) &&
1366 !is_cxl_root(parent_port) && parent_port->dev.driver) {
1368 * This was the last ep attached to a dynamically
1369 * enumerated port. Block new cxl_add_ep() and garbage
1376 device_unlock(&port->dev);
1379 dev_dbg(&cxlmd->dev, "delete %s\n",
1380 dev_name(&port->dev));
1381 delete_switch_port(port);
1383 put_device(&port->dev);
1384 device_unlock(&parent_port->dev);
1388 static resource_size_t find_component_registers(struct device *dev)
1390 struct cxl_register_map map;
1391 struct pci_dev *pdev;
1394 * Theoretically, CXL component registers can be hosted on a
1395 * non-PCI device, in practice, only cxl_test hits this case.
1397 if (!dev_is_pci(dev))
1398 return CXL_RESOURCE_NONE;
1400 pdev = to_pci_dev(dev);
1402 cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
1403 return map.resource;
1406 static int add_port_attach_ep(struct cxl_memdev *cxlmd,
1407 struct device *uport_dev,
1408 struct device *dport_dev)
1410 struct device *dparent = grandparent(dport_dev);
1411 struct cxl_port *port, *parent_port = NULL;
1412 struct cxl_dport *dport, *parent_dport;
1413 resource_size_t component_reg_phys;
1418 * The iteration reached the topology root without finding the
1419 * CXL-root 'cxl_port' on a previous iteration, fail for now to
1420 * be re-probed after platform driver attaches.
1422 dev_dbg(&cxlmd->dev, "%s is a root dport\n",
1423 dev_name(dport_dev));
1427 parent_port = find_cxl_port(dparent, &parent_dport);
1429 /* iterate to create this parent_port */
1433 device_lock(&parent_port->dev);
1434 if (!parent_port->dev.driver) {
1435 dev_warn(&cxlmd->dev,
1436 "port %s:%s disabled, failed to enumerate CXL.mem\n",
1437 dev_name(&parent_port->dev), dev_name(uport_dev));
1438 port = ERR_PTR(-ENXIO);
1442 port = find_cxl_port_at(parent_port, dport_dev, &dport);
1444 component_reg_phys = find_component_registers(uport_dev);
1445 port = devm_cxl_add_port(&parent_port->dev, uport_dev,
1446 component_reg_phys, parent_dport);
1447 /* retry find to pick up the new dport information */
1449 port = find_cxl_port_at(parent_port, dport_dev, &dport);
1452 device_unlock(&parent_port->dev);
1457 dev_dbg(&cxlmd->dev, "add to new port %s:%s\n",
1458 dev_name(&port->dev), dev_name(port->uport_dev));
1459 rc = cxl_add_ep(dport, &cxlmd->dev);
1462 * "can't" happen, but this error code means
1463 * something to the caller, so translate it.
1467 put_device(&port->dev);
1470 put_device(&parent_port->dev);
1474 int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd)
1476 struct device *dev = &cxlmd->dev;
1477 struct device *iter;
1481 * Skip intermediate port enumeration in the RCH case, there
1482 * are no ports in between a host bridge and an endpoint.
1484 if (cxlmd->cxlds->rcd)
1487 rc = devm_add_action_or_reset(&cxlmd->dev, cxl_detach_ep, cxlmd);
1492 * Scan for and add all cxl_ports in this device's ancestry.
1493 * Repeat until no more ports are added. Abort if a port add
1497 for (iter = dev; iter; iter = grandparent(iter)) {
1498 struct device *dport_dev = grandparent(iter);
1499 struct device *uport_dev;
1500 struct cxl_dport *dport;
1501 struct cxl_port *port;
1506 uport_dev = dport_dev->parent;
1508 dev_warn(dev, "at %s no parent for dport: %s\n",
1509 dev_name(iter), dev_name(dport_dev));
1513 dev_dbg(dev, "scan: iter: %s dport_dev: %s parent: %s\n",
1514 dev_name(iter), dev_name(dport_dev),
1515 dev_name(uport_dev));
1516 port = find_cxl_port(dport_dev, &dport);
1518 dev_dbg(&cxlmd->dev,
1519 "found already registered port %s:%s\n",
1520 dev_name(&port->dev),
1521 dev_name(port->uport_dev));
1522 rc = cxl_add_ep(dport, &cxlmd->dev);
1525 * If the endpoint already exists in the port's list,
1526 * that's ok, it was added on a previous pass.
1527 * Otherwise, retry in add_port_attach_ep() after taking
1528 * the parent_port lock as the current port may be being
1531 if (rc && rc != -EBUSY) {
1532 put_device(&port->dev);
1536 /* Any more ports to add between this one and the root? */
1537 if (!dev_is_cxl_root_child(&port->dev)) {
1538 put_device(&port->dev);
1542 put_device(&port->dev);
1546 rc = add_port_attach_ep(cxlmd, uport_dev, dport_dev);
1547 /* port missing, try to add parent */
1550 /* failed to add ep or port */
1553 /* port added, new descendants possible, start over */
1559 EXPORT_SYMBOL_NS_GPL(devm_cxl_enumerate_ports, CXL);
1561 struct cxl_port *cxl_pci_find_port(struct pci_dev *pdev,
1562 struct cxl_dport **dport)
1564 return find_cxl_port(pdev->dev.parent, dport);
1566 EXPORT_SYMBOL_NS_GPL(cxl_pci_find_port, CXL);
1568 struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd,
1569 struct cxl_dport **dport)
1571 return find_cxl_port(grandparent(&cxlmd->dev), dport);
1573 EXPORT_SYMBOL_NS_GPL(cxl_mem_find_port, CXL);
1575 static int decoder_populate_targets(struct cxl_switch_decoder *cxlsd,
1576 struct cxl_port *port, int *target_map)
1583 device_lock_assert(&port->dev);
1585 if (xa_empty(&port->dports))
1588 guard(rwsem_write)(&cxl_region_rwsem);
1589 for (i = 0; i < cxlsd->cxld.interleave_ways; i++) {
1590 struct cxl_dport *dport = find_dport(port, target_map[i]);
1594 cxlsd->target[i] = dport;
1600 struct cxl_dport *cxl_hb_modulo(struct cxl_root_decoder *cxlrd, int pos)
1602 struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd;
1603 struct cxl_decoder *cxld = &cxlsd->cxld;
1606 iw = cxld->interleave_ways;
1607 if (dev_WARN_ONCE(&cxld->dev, iw != cxlsd->nr_targets,
1608 "misconfigured root decoder\n"))
1611 return cxlrd->cxlsd.target[pos % iw];
1613 EXPORT_SYMBOL_NS_GPL(cxl_hb_modulo, CXL);
1615 static struct lock_class_key cxl_decoder_key;
1618 * cxl_decoder_init - Common decoder setup / initialization
1619 * @port: owning port of this decoder
1620 * @cxld: common decoder properties to initialize
1622 * A port may contain one or more decoders. Each of those decoders
1623 * enable some address space for CXL.mem utilization. A decoder is
1624 * expected to be configured by the caller before registering via
1627 static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld)
1632 rc = ida_alloc(&port->decoder_ida, GFP_KERNEL);
1636 /* need parent to stick around to release the id */
1637 get_device(&port->dev);
1641 device_initialize(dev);
1642 lockdep_set_class(&dev->mutex, &cxl_decoder_key);
1643 device_set_pm_not_required(dev);
1644 dev->parent = &port->dev;
1645 dev->bus = &cxl_bus_type;
1647 /* Pre initialize an "empty" decoder */
1648 cxld->interleave_ways = 1;
1649 cxld->interleave_granularity = PAGE_SIZE;
1650 cxld->target_type = CXL_DECODER_HOSTONLYMEM;
1651 cxld->hpa_range = (struct range) {
1659 static int cxl_switch_decoder_init(struct cxl_port *port,
1660 struct cxl_switch_decoder *cxlsd,
1663 if (nr_targets > CXL_DECODER_MAX_INTERLEAVE)
1666 cxlsd->nr_targets = nr_targets;
1667 return cxl_decoder_init(port, &cxlsd->cxld);
1671 * cxl_root_decoder_alloc - Allocate a root level decoder
1672 * @port: owning CXL root of this decoder
1673 * @nr_targets: static number of downstream targets
1674 * @calc_hb: which host bridge covers the n'th position by granularity
1676 * Return: A new cxl decoder to be registered by cxl_decoder_add(). A
1677 * 'CXL root' decoder is one that decodes from a top-level / static platform
1678 * firmware description of CXL resources into a CXL standard decode
1681 struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
1682 unsigned int nr_targets,
1683 cxl_calc_hb_fn calc_hb)
1685 struct cxl_root_decoder *cxlrd;
1686 struct cxl_switch_decoder *cxlsd;
1687 struct cxl_decoder *cxld;
1690 if (!is_cxl_root(port))
1691 return ERR_PTR(-EINVAL);
1693 cxlrd = kzalloc(struct_size(cxlrd, cxlsd.target, nr_targets),
1696 return ERR_PTR(-ENOMEM);
1698 cxlsd = &cxlrd->cxlsd;
1699 rc = cxl_switch_decoder_init(port, cxlsd, nr_targets);
1705 cxlrd->calc_hb = calc_hb;
1706 mutex_init(&cxlrd->range_lock);
1708 cxld = &cxlsd->cxld;
1709 cxld->dev.type = &cxl_decoder_root_type;
1711 * cxl_root_decoder_release() special cases negative ids to
1712 * detect memregion_alloc() failures.
1714 atomic_set(&cxlrd->region_id, -1);
1715 rc = memregion_alloc(GFP_KERNEL);
1717 put_device(&cxld->dev);
1721 atomic_set(&cxlrd->region_id, rc);
1724 EXPORT_SYMBOL_NS_GPL(cxl_root_decoder_alloc, CXL);
1727 * cxl_switch_decoder_alloc - Allocate a switch level decoder
1728 * @port: owning CXL switch port of this decoder
1729 * @nr_targets: max number of dynamically addressable downstream targets
1731 * Return: A new cxl decoder to be registered by cxl_decoder_add(). A
1732 * 'switch' decoder is any decoder that can be enumerated by PCIe
1733 * topology and the HDM Decoder Capability. This includes the decoders
1734 * that sit between Switch Upstream Ports / Switch Downstream Ports and
1735 * Host Bridges / Root Ports.
1737 struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port,
1738 unsigned int nr_targets)
1740 struct cxl_switch_decoder *cxlsd;
1741 struct cxl_decoder *cxld;
1744 if (is_cxl_root(port) || is_cxl_endpoint(port))
1745 return ERR_PTR(-EINVAL);
1747 cxlsd = kzalloc(struct_size(cxlsd, target, nr_targets), GFP_KERNEL);
1749 return ERR_PTR(-ENOMEM);
1751 rc = cxl_switch_decoder_init(port, cxlsd, nr_targets);
1757 cxld = &cxlsd->cxld;
1758 cxld->dev.type = &cxl_decoder_switch_type;
1761 EXPORT_SYMBOL_NS_GPL(cxl_switch_decoder_alloc, CXL);
1764 * cxl_endpoint_decoder_alloc - Allocate an endpoint decoder
1765 * @port: owning port of this decoder
1767 * Return: A new cxl decoder to be registered by cxl_decoder_add()
1769 struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port)
1771 struct cxl_endpoint_decoder *cxled;
1772 struct cxl_decoder *cxld;
1775 if (!is_cxl_endpoint(port))
1776 return ERR_PTR(-EINVAL);
1778 cxled = kzalloc(sizeof(*cxled), GFP_KERNEL);
1780 return ERR_PTR(-ENOMEM);
1783 cxld = &cxled->cxld;
1784 rc = cxl_decoder_init(port, cxld);
1790 cxld->dev.type = &cxl_decoder_endpoint_type;
1793 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_decoder_alloc, CXL);
1796 * cxl_decoder_add_locked - Add a decoder with targets
1797 * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc()
1798 * @target_map: A list of downstream ports that this decoder can direct memory
1799 * traffic to. These numbers should correspond with the port number
1800 * in the PCIe Link Capabilities structure.
1802 * Certain types of decoders may not have any targets. The main example of this
1803 * is an endpoint device. A more awkward example is a hostbridge whose root
1804 * ports get hot added (technically possible, though unlikely).
1806 * This is the locked variant of cxl_decoder_add().
1808 * Context: Process context. Expects the device lock of the port that owns the
1811 * Return: Negative error code if the decoder wasn't properly configured; else
1814 int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map)
1816 struct cxl_port *port;
1820 if (WARN_ON_ONCE(!cxld))
1823 if (WARN_ON_ONCE(IS_ERR(cxld)))
1824 return PTR_ERR(cxld);
1826 if (cxld->interleave_ways < 1)
1831 port = to_cxl_port(cxld->dev.parent);
1832 if (!is_endpoint_decoder(dev)) {
1833 struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
1835 rc = decoder_populate_targets(cxlsd, port, target_map);
1836 if (rc && (cxld->flags & CXL_DECODER_F_ENABLE)) {
1838 "Failed to populate active decoder targets\n");
1843 rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
1847 return device_add(dev);
1849 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add_locked, CXL);
1852 * cxl_decoder_add - Add a decoder with targets
1853 * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc()
1854 * @target_map: A list of downstream ports that this decoder can direct memory
1855 * traffic to. These numbers should correspond with the port number
1856 * in the PCIe Link Capabilities structure.
1858 * This is the unlocked variant of cxl_decoder_add_locked().
1859 * See cxl_decoder_add_locked().
1861 * Context: Process context. Takes and releases the device lock of the port that
1864 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
1866 struct cxl_port *port;
1869 if (WARN_ON_ONCE(!cxld))
1872 if (WARN_ON_ONCE(IS_ERR(cxld)))
1873 return PTR_ERR(cxld);
1875 port = to_cxl_port(cxld->dev.parent);
1877 device_lock(&port->dev);
1878 rc = cxl_decoder_add_locked(cxld, target_map);
1879 device_unlock(&port->dev);
1883 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, CXL);
1885 static void cxld_unregister(void *dev)
1887 struct cxl_endpoint_decoder *cxled;
1889 if (is_endpoint_decoder(dev)) {
1890 cxled = to_cxl_endpoint_decoder(dev);
1891 cxl_decoder_kill_region(cxled);
1894 device_unregister(dev);
1897 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld)
1899 return devm_add_action_or_reset(host, cxld_unregister, &cxld->dev);
1901 EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, CXL);
1904 * __cxl_driver_register - register a driver for the cxl bus
1905 * @cxl_drv: cxl driver structure to attach
1906 * @owner: owning module/driver
1907 * @modname: KBUILD_MODNAME for parent driver
1909 int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
1910 const char *modname)
1912 if (!cxl_drv->probe) {
1913 pr_debug("%s ->probe() must be specified\n", modname);
1917 if (!cxl_drv->name) {
1918 pr_debug("%s ->name must be specified\n", modname);
1923 pr_debug("%s ->id must be specified\n", modname);
1927 cxl_drv->drv.bus = &cxl_bus_type;
1928 cxl_drv->drv.owner = owner;
1929 cxl_drv->drv.mod_name = modname;
1930 cxl_drv->drv.name = cxl_drv->name;
1932 return driver_register(&cxl_drv->drv);
1934 EXPORT_SYMBOL_NS_GPL(__cxl_driver_register, CXL);
1936 void cxl_driver_unregister(struct cxl_driver *cxl_drv)
1938 driver_unregister(&cxl_drv->drv);
1940 EXPORT_SYMBOL_NS_GPL(cxl_driver_unregister, CXL);
1942 static int cxl_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
1944 return add_uevent_var(env, "MODALIAS=" CXL_MODALIAS_FMT,
1945 cxl_device_id(dev));
1948 static int cxl_bus_match(struct device *dev, struct device_driver *drv)
1950 return cxl_device_id(dev) == to_cxl_drv(drv)->id;
1953 static int cxl_bus_probe(struct device *dev)
1957 rc = to_cxl_drv(dev->driver)->probe(dev);
1958 dev_dbg(dev, "probe: %d\n", rc);
1962 static void cxl_bus_remove(struct device *dev)
1964 struct cxl_driver *cxl_drv = to_cxl_drv(dev->driver);
1966 if (cxl_drv->remove)
1967 cxl_drv->remove(dev);
1970 static struct workqueue_struct *cxl_bus_wq;
1972 static void cxl_bus_rescan_queue(struct work_struct *w)
1974 int rc = bus_rescan_devices(&cxl_bus_type);
1976 pr_debug("CXL bus rescan result: %d\n", rc);
1979 void cxl_bus_rescan(void)
1981 static DECLARE_WORK(rescan_work, cxl_bus_rescan_queue);
1983 queue_work(cxl_bus_wq, &rescan_work);
1985 EXPORT_SYMBOL_NS_GPL(cxl_bus_rescan, CXL);
1987 void cxl_bus_drain(void)
1989 drain_workqueue(cxl_bus_wq);
1991 EXPORT_SYMBOL_NS_GPL(cxl_bus_drain, CXL);
1993 bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd)
1995 return queue_work(cxl_bus_wq, &cxlmd->detach_work);
1997 EXPORT_SYMBOL_NS_GPL(schedule_cxl_memdev_detach, CXL);
1999 /* for user tooling to ensure port disable work has completed */
2000 static ssize_t flush_store(const struct bus_type *bus, const char *buf, size_t count)
2002 if (sysfs_streq(buf, "1")) {
2003 flush_workqueue(cxl_bus_wq);
2010 static BUS_ATTR_WO(flush);
2012 static struct attribute *cxl_bus_attributes[] = {
2013 &bus_attr_flush.attr,
2017 static struct attribute_group cxl_bus_attribute_group = {
2018 .attrs = cxl_bus_attributes,
2021 static const struct attribute_group *cxl_bus_attribute_groups[] = {
2022 &cxl_bus_attribute_group,
2026 struct bus_type cxl_bus_type = {
2028 .uevent = cxl_bus_uevent,
2029 .match = cxl_bus_match,
2030 .probe = cxl_bus_probe,
2031 .remove = cxl_bus_remove,
2032 .bus_groups = cxl_bus_attribute_groups,
2034 EXPORT_SYMBOL_NS_GPL(cxl_bus_type, CXL);
2036 static struct dentry *cxl_debugfs;
2038 struct dentry *cxl_debugfs_create_dir(const char *dir)
2040 return debugfs_create_dir(dir, cxl_debugfs);
2042 EXPORT_SYMBOL_NS_GPL(cxl_debugfs_create_dir, CXL);
2044 static __init int cxl_core_init(void)
2048 cxl_debugfs = debugfs_create_dir("cxl", NULL);
2052 rc = cxl_memdev_init();
2056 cxl_bus_wq = alloc_ordered_workqueue("cxl_port", 0);
2062 rc = bus_register(&cxl_bus_type);
2066 rc = cxl_region_init();
2073 bus_unregister(&cxl_bus_type);
2075 destroy_workqueue(cxl_bus_wq);
2081 static void cxl_core_exit(void)
2084 bus_unregister(&cxl_bus_type);
2085 destroy_workqueue(cxl_bus_wq);
2087 debugfs_remove_recursive(cxl_debugfs);
2090 subsys_initcall(cxl_core_init);
2091 module_exit(cxl_core_exit);
2092 MODULE_LICENSE("GPL v2");