1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2022 Intel Corporation. All rights reserved. */
3 #include <linux/memregion.h>
4 #include <linux/genalloc.h>
5 #include <linux/device.h>
6 #include <linux/module.h>
7 #include <linux/memory.h>
8 #include <linux/slab.h>
9 #include <linux/uuid.h>
10 #include <linux/sort.h>
11 #include <linux/idr.h>
12 #include <linux/memory-tiers.h>
18 * DOC: cxl core region
20 * CXL Regions represent mapped memory capacity in system physical address
21 * space. Whereas the CXL Root Decoders identify the bounds of potential CXL
22 * Memory ranges, Regions represent the active mapped capacity by the HDM
23 * Decoder Capability structures throughout the Host Bridges, Switches, and
24 * Endpoints in the topology.
26 * Region configuration has ordering constraints. UUID may be set at any time
27 * but is only visible for persistent regions.
28 * 1. Interleave granularity
33 static struct cxl_region *to_cxl_region(struct device *dev);
35 #define __ACCESS_ATTR_RO(_level, _name) { \
36 .attr = { .name = __stringify(_name), .mode = 0444 }, \
37 .show = _name##_access##_level##_show, \
40 #define ACCESS_DEVICE_ATTR_RO(level, name) \
41 struct device_attribute dev_attr_access##level##_##name = __ACCESS_ATTR_RO(level, name)
43 #define ACCESS_ATTR_RO(level, attrib) \
44 static ssize_t attrib##_access##level##_show(struct device *dev, \
45 struct device_attribute *attr, \
48 struct cxl_region *cxlr = to_cxl_region(dev); \
50 if (cxlr->coord[level].attrib == 0) \
53 return sysfs_emit(buf, "%u\n", cxlr->coord[level].attrib); \
55 static ACCESS_DEVICE_ATTR_RO(level, attrib)
57 ACCESS_ATTR_RO(0, read_bandwidth);
58 ACCESS_ATTR_RO(0, read_latency);
59 ACCESS_ATTR_RO(0, write_bandwidth);
60 ACCESS_ATTR_RO(0, write_latency);
62 #define ACCESS_ATTR_DECLARE(level, attrib) \
63 (&dev_attr_access##level##_##attrib.attr)
65 static struct attribute *access0_coordinate_attrs[] = {
66 ACCESS_ATTR_DECLARE(0, read_bandwidth),
67 ACCESS_ATTR_DECLARE(0, write_bandwidth),
68 ACCESS_ATTR_DECLARE(0, read_latency),
69 ACCESS_ATTR_DECLARE(0, write_latency),
73 ACCESS_ATTR_RO(1, read_bandwidth);
74 ACCESS_ATTR_RO(1, read_latency);
75 ACCESS_ATTR_RO(1, write_bandwidth);
76 ACCESS_ATTR_RO(1, write_latency);
78 static struct attribute *access1_coordinate_attrs[] = {
79 ACCESS_ATTR_DECLARE(1, read_bandwidth),
80 ACCESS_ATTR_DECLARE(1, write_bandwidth),
81 ACCESS_ATTR_DECLARE(1, read_latency),
82 ACCESS_ATTR_DECLARE(1, write_latency),
86 #define ACCESS_VISIBLE(level) \
87 static umode_t cxl_region_access##level##_coordinate_visible( \
88 struct kobject *kobj, struct attribute *a, int n) \
90 struct device *dev = kobj_to_dev(kobj); \
91 struct cxl_region *cxlr = to_cxl_region(dev); \
93 if (a == &dev_attr_access##level##_read_latency.attr && \
94 cxlr->coord[level].read_latency == 0) \
97 if (a == &dev_attr_access##level##_write_latency.attr && \
98 cxlr->coord[level].write_latency == 0) \
101 if (a == &dev_attr_access##level##_read_bandwidth.attr && \
102 cxlr->coord[level].read_bandwidth == 0) \
105 if (a == &dev_attr_access##level##_write_bandwidth.attr && \
106 cxlr->coord[level].write_bandwidth == 0) \
115 static const struct attribute_group cxl_region_access0_coordinate_group = {
117 .attrs = access0_coordinate_attrs,
118 .is_visible = cxl_region_access0_coordinate_visible,
121 static const struct attribute_group *get_cxl_region_access0_group(void)
123 return &cxl_region_access0_coordinate_group;
126 static const struct attribute_group cxl_region_access1_coordinate_group = {
128 .attrs = access1_coordinate_attrs,
129 .is_visible = cxl_region_access1_coordinate_visible,
132 static const struct attribute_group *get_cxl_region_access1_group(void)
134 return &cxl_region_access1_coordinate_group;
137 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
140 struct cxl_region *cxlr = to_cxl_region(dev);
141 struct cxl_region_params *p = &cxlr->params;
144 rc = down_read_interruptible(&cxl_region_rwsem);
147 if (cxlr->mode != CXL_DECODER_PMEM)
148 rc = sysfs_emit(buf, "\n");
150 rc = sysfs_emit(buf, "%pUb\n", &p->uuid);
151 up_read(&cxl_region_rwsem);
156 static int is_dup(struct device *match, void *data)
158 struct cxl_region_params *p;
159 struct cxl_region *cxlr;
162 if (!is_cxl_region(match))
165 lockdep_assert_held(&cxl_region_rwsem);
166 cxlr = to_cxl_region(match);
169 if (uuid_equal(&p->uuid, uuid)) {
170 dev_dbg(match, "already has uuid: %pUb\n", uuid);
177 static ssize_t uuid_store(struct device *dev, struct device_attribute *attr,
178 const char *buf, size_t len)
180 struct cxl_region *cxlr = to_cxl_region(dev);
181 struct cxl_region_params *p = &cxlr->params;
185 if (len != UUID_STRING_LEN + 1)
188 rc = uuid_parse(buf, &temp);
192 if (uuid_is_null(&temp))
195 rc = down_write_killable(&cxl_region_rwsem);
199 if (uuid_equal(&p->uuid, &temp))
203 if (p->state >= CXL_CONFIG_ACTIVE)
206 rc = bus_for_each_dev(&cxl_bus_type, NULL, &temp, is_dup);
210 uuid_copy(&p->uuid, &temp);
212 up_write(&cxl_region_rwsem);
218 static DEVICE_ATTR_RW(uuid);
220 static struct cxl_region_ref *cxl_rr_load(struct cxl_port *port,
221 struct cxl_region *cxlr)
223 return xa_load(&port->regions, (unsigned long)cxlr);
226 static int cxl_region_invalidate_memregion(struct cxl_region *cxlr)
228 if (!cpu_cache_has_invalidate_memregion()) {
229 if (IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST)) {
232 "Bypassing cpu_cache_invalidate_memregion() for testing!\n");
236 "Failed to synchronize CPU cache state\n");
241 cpu_cache_invalidate_memregion(IORES_DESC_CXL);
245 static int cxl_region_decode_reset(struct cxl_region *cxlr, int count)
247 struct cxl_region_params *p = &cxlr->params;
251 * Before region teardown attempt to flush, and if the flush
252 * fails cancel the region teardown for data consistency
255 rc = cxl_region_invalidate_memregion(cxlr);
259 for (i = count - 1; i >= 0; i--) {
260 struct cxl_endpoint_decoder *cxled = p->targets[i];
261 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
262 struct cxl_port *iter = cxled_to_port(cxled);
263 struct cxl_dev_state *cxlds = cxlmd->cxlds;
269 while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
270 iter = to_cxl_port(iter->dev.parent);
272 for (ep = cxl_ep_load(iter, cxlmd); iter;
273 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
274 struct cxl_region_ref *cxl_rr;
275 struct cxl_decoder *cxld;
277 cxl_rr = cxl_rr_load(iter, cxlr);
278 cxld = cxl_rr->decoder;
280 rc = cxld->reset(cxld);
283 set_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
287 rc = cxled->cxld.reset(&cxled->cxld);
290 set_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
293 /* all decoders associated with this region have been torn down */
294 clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
299 static int commit_decoder(struct cxl_decoder *cxld)
301 struct cxl_switch_decoder *cxlsd = NULL;
304 return cxld->commit(cxld);
306 if (is_switch_decoder(&cxld->dev))
307 cxlsd = to_cxl_switch_decoder(&cxld->dev);
309 if (dev_WARN_ONCE(&cxld->dev, !cxlsd || cxlsd->nr_targets > 1,
310 "->commit() is required\n"))
315 static int cxl_region_decode_commit(struct cxl_region *cxlr)
317 struct cxl_region_params *p = &cxlr->params;
320 for (i = 0; i < p->nr_targets; i++) {
321 struct cxl_endpoint_decoder *cxled = p->targets[i];
322 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
323 struct cxl_region_ref *cxl_rr;
324 struct cxl_decoder *cxld;
325 struct cxl_port *iter;
328 /* commit bottom up */
329 for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
330 iter = to_cxl_port(iter->dev.parent)) {
331 cxl_rr = cxl_rr_load(iter, cxlr);
332 cxld = cxl_rr->decoder;
333 rc = commit_decoder(cxld);
339 /* programming @iter failed, teardown */
340 for (ep = cxl_ep_load(iter, cxlmd); ep && iter;
341 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
342 cxl_rr = cxl_rr_load(iter, cxlr);
343 cxld = cxl_rr->decoder;
348 cxled->cxld.reset(&cxled->cxld);
356 /* undo the targets that were successfully committed */
357 cxl_region_decode_reset(cxlr, i);
361 static ssize_t commit_store(struct device *dev, struct device_attribute *attr,
362 const char *buf, size_t len)
364 struct cxl_region *cxlr = to_cxl_region(dev);
365 struct cxl_region_params *p = &cxlr->params;
369 rc = kstrtobool(buf, &commit);
373 rc = down_write_killable(&cxl_region_rwsem);
377 /* Already in the requested state? */
378 if (commit && p->state >= CXL_CONFIG_COMMIT)
380 if (!commit && p->state < CXL_CONFIG_COMMIT)
383 /* Not ready to commit? */
384 if (commit && p->state < CXL_CONFIG_ACTIVE) {
390 * Invalidate caches before region setup to drop any speculative
391 * consumption of this address space
393 rc = cxl_region_invalidate_memregion(cxlr);
398 rc = cxl_region_decode_commit(cxlr);
400 p->state = CXL_CONFIG_COMMIT;
402 p->state = CXL_CONFIG_RESET_PENDING;
403 up_write(&cxl_region_rwsem);
404 device_release_driver(&cxlr->dev);
405 down_write(&cxl_region_rwsem);
408 * The lock was dropped, so need to revalidate that the reset is
411 if (p->state == CXL_CONFIG_RESET_PENDING) {
412 rc = cxl_region_decode_reset(cxlr, p->interleave_ways);
414 * Revert to committed since there may still be active
415 * decoders associated with this region, or move forward
416 * to active to mark the reset successful
419 p->state = CXL_CONFIG_COMMIT;
421 p->state = CXL_CONFIG_ACTIVE;
426 up_write(&cxl_region_rwsem);
433 static ssize_t commit_show(struct device *dev, struct device_attribute *attr,
436 struct cxl_region *cxlr = to_cxl_region(dev);
437 struct cxl_region_params *p = &cxlr->params;
440 rc = down_read_interruptible(&cxl_region_rwsem);
443 rc = sysfs_emit(buf, "%d\n", p->state >= CXL_CONFIG_COMMIT);
444 up_read(&cxl_region_rwsem);
448 static DEVICE_ATTR_RW(commit);
450 static umode_t cxl_region_visible(struct kobject *kobj, struct attribute *a,
453 struct device *dev = kobj_to_dev(kobj);
454 struct cxl_region *cxlr = to_cxl_region(dev);
457 * Support tooling that expects to find a 'uuid' attribute for all
458 * regions regardless of mode.
460 if (a == &dev_attr_uuid.attr && cxlr->mode != CXL_DECODER_PMEM)
465 static ssize_t interleave_ways_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct cxl_region *cxlr = to_cxl_region(dev);
469 struct cxl_region_params *p = &cxlr->params;
472 rc = down_read_interruptible(&cxl_region_rwsem);
475 rc = sysfs_emit(buf, "%d\n", p->interleave_ways);
476 up_read(&cxl_region_rwsem);
481 static const struct attribute_group *get_cxl_region_target_group(void);
483 static ssize_t interleave_ways_store(struct device *dev,
484 struct device_attribute *attr,
485 const char *buf, size_t len)
487 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
488 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
489 struct cxl_region *cxlr = to_cxl_region(dev);
490 struct cxl_region_params *p = &cxlr->params;
491 unsigned int val, save;
495 rc = kstrtouint(buf, 0, &val);
499 rc = ways_to_eiw(val, &iw);
504 * Even for x3, x6, and x12 interleaves the region interleave must be a
505 * power of 2 multiple of the host bridge interleave.
507 if (!is_power_of_2(val / cxld->interleave_ways) ||
508 (val % cxld->interleave_ways)) {
509 dev_dbg(&cxlr->dev, "invalid interleave: %d\n", val);
513 rc = down_write_killable(&cxl_region_rwsem);
516 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
521 save = p->interleave_ways;
522 p->interleave_ways = val;
523 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
525 p->interleave_ways = save;
527 up_write(&cxl_region_rwsem);
532 static DEVICE_ATTR_RW(interleave_ways);
534 static ssize_t interleave_granularity_show(struct device *dev,
535 struct device_attribute *attr,
538 struct cxl_region *cxlr = to_cxl_region(dev);
539 struct cxl_region_params *p = &cxlr->params;
542 rc = down_read_interruptible(&cxl_region_rwsem);
545 rc = sysfs_emit(buf, "%d\n", p->interleave_granularity);
546 up_read(&cxl_region_rwsem);
551 static ssize_t interleave_granularity_store(struct device *dev,
552 struct device_attribute *attr,
553 const char *buf, size_t len)
555 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
556 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
557 struct cxl_region *cxlr = to_cxl_region(dev);
558 struct cxl_region_params *p = &cxlr->params;
562 rc = kstrtoint(buf, 0, &val);
566 rc = granularity_to_eig(val, &ig);
571 * When the host-bridge is interleaved, disallow region granularity !=
572 * root granularity. Regions with a granularity less than the root
573 * interleave result in needing multiple endpoints to support a single
574 * slot in the interleave (possible to support in the future). Regions
575 * with a granularity greater than the root interleave result in invalid
576 * DPA translations (invalid to support).
578 if (cxld->interleave_ways > 1 && val != cxld->interleave_granularity)
581 rc = down_write_killable(&cxl_region_rwsem);
584 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
589 p->interleave_granularity = val;
591 up_write(&cxl_region_rwsem);
596 static DEVICE_ATTR_RW(interleave_granularity);
598 static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
601 struct cxl_region *cxlr = to_cxl_region(dev);
602 struct cxl_region_params *p = &cxlr->params;
603 u64 resource = -1ULL;
606 rc = down_read_interruptible(&cxl_region_rwsem);
610 resource = p->res->start;
611 rc = sysfs_emit(buf, "%#llx\n", resource);
612 up_read(&cxl_region_rwsem);
616 static DEVICE_ATTR_RO(resource);
618 static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
621 struct cxl_region *cxlr = to_cxl_region(dev);
623 return sysfs_emit(buf, "%s\n", cxl_decoder_mode_name(cxlr->mode));
625 static DEVICE_ATTR_RO(mode);
627 static int alloc_hpa(struct cxl_region *cxlr, resource_size_t size)
629 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
630 struct cxl_region_params *p = &cxlr->params;
631 struct resource *res;
634 lockdep_assert_held_write(&cxl_region_rwsem);
636 /* Nothing to do... */
637 if (p->res && resource_size(p->res) == size)
640 /* To change size the old size must be freed first */
644 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE)
647 /* ways, granularity and uuid (if PMEM) need to be set before HPA */
648 if (!p->interleave_ways || !p->interleave_granularity ||
649 (cxlr->mode == CXL_DECODER_PMEM && uuid_is_null(&p->uuid)))
652 div64_u64_rem(size, (u64)SZ_256M * p->interleave_ways, &remainder);
656 res = alloc_free_mem_region(cxlrd->res, size, SZ_256M,
657 dev_name(&cxlr->dev));
660 "HPA allocation error (%ld) for size:%pap in %s %pr\n",
661 PTR_ERR(res), &size, cxlrd->res->name, cxlrd->res);
666 p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
671 static void cxl_region_iomem_release(struct cxl_region *cxlr)
673 struct cxl_region_params *p = &cxlr->params;
675 if (device_is_registered(&cxlr->dev))
676 lockdep_assert_held_write(&cxl_region_rwsem);
679 * Autodiscovered regions may not have been able to insert their
683 remove_resource(p->res);
689 static int free_hpa(struct cxl_region *cxlr)
691 struct cxl_region_params *p = &cxlr->params;
693 lockdep_assert_held_write(&cxl_region_rwsem);
698 if (p->state >= CXL_CONFIG_ACTIVE)
701 cxl_region_iomem_release(cxlr);
702 p->state = CXL_CONFIG_IDLE;
706 static ssize_t size_store(struct device *dev, struct device_attribute *attr,
707 const char *buf, size_t len)
709 struct cxl_region *cxlr = to_cxl_region(dev);
713 rc = kstrtou64(buf, 0, &val);
717 rc = down_write_killable(&cxl_region_rwsem);
722 rc = alloc_hpa(cxlr, val);
725 up_write(&cxl_region_rwsem);
733 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
736 struct cxl_region *cxlr = to_cxl_region(dev);
737 struct cxl_region_params *p = &cxlr->params;
741 rc = down_read_interruptible(&cxl_region_rwsem);
745 size = resource_size(p->res);
746 rc = sysfs_emit(buf, "%#llx\n", size);
747 up_read(&cxl_region_rwsem);
751 static DEVICE_ATTR_RW(size);
753 static struct attribute *cxl_region_attrs[] = {
755 &dev_attr_commit.attr,
756 &dev_attr_interleave_ways.attr,
757 &dev_attr_interleave_granularity.attr,
758 &dev_attr_resource.attr,
764 static const struct attribute_group cxl_region_group = {
765 .attrs = cxl_region_attrs,
766 .is_visible = cxl_region_visible,
769 static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos)
771 struct cxl_region_params *p = &cxlr->params;
772 struct cxl_endpoint_decoder *cxled;
775 rc = down_read_interruptible(&cxl_region_rwsem);
779 if (pos >= p->interleave_ways) {
780 dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
786 cxled = p->targets[pos];
788 rc = sysfs_emit(buf, "\n");
790 rc = sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev));
792 up_read(&cxl_region_rwsem);
797 static int match_free_decoder(struct device *dev, void *data)
799 struct cxl_decoder *cxld;
802 if (!is_switch_decoder(dev))
805 cxld = to_cxl_decoder(dev);
807 /* enforce ordered allocation */
819 static int match_auto_decoder(struct device *dev, void *data)
821 struct cxl_region_params *p = data;
822 struct cxl_decoder *cxld;
825 if (!is_switch_decoder(dev))
828 cxld = to_cxl_decoder(dev);
829 r = &cxld->hpa_range;
831 if (p->res && p->res->start == r->start && p->res->end == r->end)
837 static struct cxl_decoder *
838 cxl_region_find_decoder(struct cxl_port *port,
839 struct cxl_endpoint_decoder *cxled,
840 struct cxl_region *cxlr)
845 if (port == cxled_to_port(cxled))
848 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags))
849 dev = device_find_child(&port->dev, &cxlr->params,
852 dev = device_find_child(&port->dev, &id, match_free_decoder);
856 * This decoder is pinned registered as long as the endpoint decoder is
857 * registered, and endpoint decoder unregistration holds the
858 * cxl_region_rwsem over unregister events, so no need to hold on to
859 * this extra reference.
862 return to_cxl_decoder(dev);
865 static bool auto_order_ok(struct cxl_port *port, struct cxl_region *cxlr_iter,
866 struct cxl_decoder *cxld)
868 struct cxl_region_ref *rr = cxl_rr_load(port, cxlr_iter);
869 struct cxl_decoder *cxld_iter = rr->decoder;
872 * Allow the out of order assembly of auto-discovered regions.
873 * Per CXL Spec 3.1 8.2.4.20.12 software must commit decoders
874 * in HPA order. Confirm that the decoder with the lesser HPA
875 * starting address has the lesser id.
877 dev_dbg(&cxld->dev, "check for HPA violation %s:%d < %s:%d\n",
878 dev_name(&cxld->dev), cxld->id,
879 dev_name(&cxld_iter->dev), cxld_iter->id);
881 if (cxld_iter->id > cxld->id)
887 static struct cxl_region_ref *
888 alloc_region_ref(struct cxl_port *port, struct cxl_region *cxlr,
889 struct cxl_endpoint_decoder *cxled)
891 struct cxl_region_params *p = &cxlr->params;
892 struct cxl_region_ref *cxl_rr, *iter;
896 xa_for_each(&port->regions, index, iter) {
897 struct cxl_region_params *ip = &iter->region->params;
899 if (!ip->res || ip->res->start < p->res->start)
902 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
903 struct cxl_decoder *cxld;
905 cxld = cxl_region_find_decoder(port, cxled, cxlr);
906 if (auto_order_ok(port, iter->region, cxld))
909 dev_dbg(&cxlr->dev, "%s: HPA order violation %s:%pr vs %pr\n",
910 dev_name(&port->dev),
911 dev_name(&iter->region->dev), ip->res, p->res);
913 return ERR_PTR(-EBUSY);
916 cxl_rr = kzalloc(sizeof(*cxl_rr), GFP_KERNEL);
918 return ERR_PTR(-ENOMEM);
920 cxl_rr->region = cxlr;
921 cxl_rr->nr_targets = 1;
922 xa_init(&cxl_rr->endpoints);
924 rc = xa_insert(&port->regions, (unsigned long)cxlr, cxl_rr, GFP_KERNEL);
927 "%s: failed to track region reference: %d\n",
928 dev_name(&port->dev), rc);
936 static void cxl_rr_free_decoder(struct cxl_region_ref *cxl_rr)
938 struct cxl_region *cxlr = cxl_rr->region;
939 struct cxl_decoder *cxld = cxl_rr->decoder;
944 dev_WARN_ONCE(&cxlr->dev, cxld->region != cxlr, "region mismatch\n");
945 if (cxld->region == cxlr) {
947 put_device(&cxlr->dev);
951 static void free_region_ref(struct cxl_region_ref *cxl_rr)
953 struct cxl_port *port = cxl_rr->port;
954 struct cxl_region *cxlr = cxl_rr->region;
956 cxl_rr_free_decoder(cxl_rr);
957 xa_erase(&port->regions, (unsigned long)cxlr);
958 xa_destroy(&cxl_rr->endpoints);
962 static int cxl_rr_ep_add(struct cxl_region_ref *cxl_rr,
963 struct cxl_endpoint_decoder *cxled)
966 struct cxl_port *port = cxl_rr->port;
967 struct cxl_region *cxlr = cxl_rr->region;
968 struct cxl_decoder *cxld = cxl_rr->decoder;
969 struct cxl_ep *ep = cxl_ep_load(port, cxled_to_memdev(cxled));
972 rc = xa_insert(&cxl_rr->endpoints, (unsigned long)cxled, ep,
981 get_device(&cxlr->dev);
987 static int cxl_rr_alloc_decoder(struct cxl_port *port, struct cxl_region *cxlr,
988 struct cxl_endpoint_decoder *cxled,
989 struct cxl_region_ref *cxl_rr)
991 struct cxl_decoder *cxld;
993 cxld = cxl_region_find_decoder(port, cxled, cxlr);
995 dev_dbg(&cxlr->dev, "%s: no decoder available\n",
996 dev_name(&port->dev));
1001 dev_dbg(&cxlr->dev, "%s: %s already attached to %s\n",
1002 dev_name(&port->dev), dev_name(&cxld->dev),
1003 dev_name(&cxld->region->dev));
1008 * Endpoints should already match the region type, but backstop that
1009 * assumption with an assertion. Switch-decoders change mapping-type
1010 * based on what is mapped when they are assigned to a region.
1012 dev_WARN_ONCE(&cxlr->dev,
1013 port == cxled_to_port(cxled) &&
1014 cxld->target_type != cxlr->type,
1015 "%s:%s mismatch decoder type %d -> %d\n",
1016 dev_name(&cxled_to_memdev(cxled)->dev),
1017 dev_name(&cxld->dev), cxld->target_type, cxlr->type);
1018 cxld->target_type = cxlr->type;
1019 cxl_rr->decoder = cxld;
1024 * cxl_port_attach_region() - track a region's interest in a port by endpoint
1025 * @port: port to add a new region reference 'struct cxl_region_ref'
1026 * @cxlr: region to attach to @port
1027 * @cxled: endpoint decoder used to create or further pin a region reference
1028 * @pos: interleave position of @cxled in @cxlr
1030 * The attach event is an opportunity to validate CXL decode setup
1031 * constraints and record metadata needed for programming HDM decoders,
1032 * in particular decoder target lists.
1036 * - validate that there are no other regions with a higher HPA already
1037 * associated with @port
1038 * - establish a region reference if one is not already present
1040 * - additionally allocate a decoder instance that will host @cxlr on
1043 * - pin the region reference by the endpoint
1044 * - account for how many entries in @port's target list are needed to
1045 * cover all of the added endpoints.
1047 static int cxl_port_attach_region(struct cxl_port *port,
1048 struct cxl_region *cxlr,
1049 struct cxl_endpoint_decoder *cxled, int pos)
1051 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1052 struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
1053 struct cxl_region_ref *cxl_rr;
1054 bool nr_targets_inc = false;
1055 struct cxl_decoder *cxld;
1056 unsigned long index;
1059 lockdep_assert_held_write(&cxl_region_rwsem);
1061 cxl_rr = cxl_rr_load(port, cxlr);
1063 struct cxl_ep *ep_iter;
1067 * Walk the existing endpoints that have been attached to
1068 * @cxlr at @port and see if they share the same 'next' port
1069 * in the downstream direction. I.e. endpoints that share common
1072 xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
1075 if (ep_iter->next == ep->next) {
1082 * New target port, or @port is an endpoint port that always
1083 * accounts its own local decode as a target.
1085 if (!found || !ep->next) {
1086 cxl_rr->nr_targets++;
1087 nr_targets_inc = true;
1090 cxl_rr = alloc_region_ref(port, cxlr, cxled);
1091 if (IS_ERR(cxl_rr)) {
1093 "%s: failed to allocate region reference\n",
1094 dev_name(&port->dev));
1095 return PTR_ERR(cxl_rr);
1097 nr_targets_inc = true;
1099 rc = cxl_rr_alloc_decoder(port, cxlr, cxled, cxl_rr);
1103 cxld = cxl_rr->decoder;
1106 * the number of targets should not exceed the target_count
1109 if (is_switch_decoder(&cxld->dev)) {
1110 struct cxl_switch_decoder *cxlsd;
1112 cxlsd = to_cxl_switch_decoder(&cxld->dev);
1113 if (cxl_rr->nr_targets > cxlsd->nr_targets) {
1115 "%s:%s %s add: %s:%s @ %d overflows targets: %d\n",
1116 dev_name(port->uport_dev), dev_name(&port->dev),
1117 dev_name(&cxld->dev), dev_name(&cxlmd->dev),
1118 dev_name(&cxled->cxld.dev), pos,
1125 rc = cxl_rr_ep_add(cxl_rr, cxled);
1128 "%s: failed to track endpoint %s:%s reference\n",
1129 dev_name(&port->dev), dev_name(&cxlmd->dev),
1130 dev_name(&cxld->dev));
1135 "%s:%s %s add: %s:%s @ %d next: %s nr_eps: %d nr_targets: %d\n",
1136 dev_name(port->uport_dev), dev_name(&port->dev),
1137 dev_name(&cxld->dev), dev_name(&cxlmd->dev),
1138 dev_name(&cxled->cxld.dev), pos,
1139 ep ? ep->next ? dev_name(ep->next->uport_dev) :
1140 dev_name(&cxlmd->dev) :
1142 cxl_rr->nr_eps, cxl_rr->nr_targets);
1147 cxl_rr->nr_targets--;
1148 if (cxl_rr->nr_eps == 0)
1149 free_region_ref(cxl_rr);
1153 static void cxl_port_detach_region(struct cxl_port *port,
1154 struct cxl_region *cxlr,
1155 struct cxl_endpoint_decoder *cxled)
1157 struct cxl_region_ref *cxl_rr;
1158 struct cxl_ep *ep = NULL;
1160 lockdep_assert_held_write(&cxl_region_rwsem);
1162 cxl_rr = cxl_rr_load(port, cxlr);
1167 * Endpoint ports do not carry cxl_ep references, and they
1168 * never target more than one endpoint by definition
1170 if (cxl_rr->decoder == &cxled->cxld)
1173 ep = xa_erase(&cxl_rr->endpoints, (unsigned long)cxled);
1175 struct cxl_ep *ep_iter;
1176 unsigned long index;
1180 xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
1181 if (ep_iter->next == ep->next) {
1187 cxl_rr->nr_targets--;
1190 if (cxl_rr->nr_eps == 0)
1191 free_region_ref(cxl_rr);
1194 static int check_last_peer(struct cxl_endpoint_decoder *cxled,
1195 struct cxl_ep *ep, struct cxl_region_ref *cxl_rr,
1198 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1199 struct cxl_region *cxlr = cxl_rr->region;
1200 struct cxl_region_params *p = &cxlr->params;
1201 struct cxl_endpoint_decoder *cxled_peer;
1202 struct cxl_port *port = cxl_rr->port;
1203 struct cxl_memdev *cxlmd_peer;
1204 struct cxl_ep *ep_peer;
1205 int pos = cxled->pos;
1208 * If this position wants to share a dport with the last endpoint mapped
1209 * then that endpoint, at index 'position - distance', must also be
1210 * mapped by this dport.
1212 if (pos < distance) {
1213 dev_dbg(&cxlr->dev, "%s:%s: cannot host %s:%s at %d\n",
1214 dev_name(port->uport_dev), dev_name(&port->dev),
1215 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1218 cxled_peer = p->targets[pos - distance];
1219 cxlmd_peer = cxled_to_memdev(cxled_peer);
1220 ep_peer = cxl_ep_load(port, cxlmd_peer);
1221 if (ep->dport != ep_peer->dport) {
1223 "%s:%s: %s:%s pos %d mismatched peer %s:%s\n",
1224 dev_name(port->uport_dev), dev_name(&port->dev),
1225 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos,
1226 dev_name(&cxlmd_peer->dev),
1227 dev_name(&cxled_peer->cxld.dev));
1234 static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
1236 struct cxl_port *port = to_cxl_port(cxld->dev.parent);
1237 struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
1238 unsigned int interleave_mask;
1241 int high_pos, low_pos;
1243 if (!test_bit(iw, &cxlhdm->iw_cap_mask))
1246 * Per CXL specification r3.1(8.2.4.20.13 Decoder Protection),
1248 * DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + 8 + eiw]
1249 * DPAOFFSET[eig + 7: 0] = HPAOFFSET[eig + 7: 0]
1251 * when the eiw is 0, all the bits of HPAOFFSET[51: 0] are used, the
1252 * interleave bits are none.
1255 * DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + eiw] / 3
1256 * DPAOFFSET[eig + 7: 0] = HPAOFFSET[eig + 7: 0]
1258 * when the eiw is 8, all the bits of HPAOFFSET[51: 0] are used, the
1259 * interleave bits are none.
1261 ways_to_eiw(iw, &eiw);
1262 if (eiw == 0 || eiw == 8)
1265 granularity_to_eig(ig, &eig);
1267 high_pos = eiw + eig - 1;
1269 high_pos = eiw + eig + 7;
1271 interleave_mask = GENMASK(high_pos, low_pos);
1272 if (interleave_mask & ~cxlhdm->interleave_mask)
1278 static int cxl_port_setup_targets(struct cxl_port *port,
1279 struct cxl_region *cxlr,
1280 struct cxl_endpoint_decoder *cxled)
1282 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
1283 int parent_iw, parent_ig, ig, iw, rc, inc = 0, pos = cxled->pos;
1284 struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
1285 struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
1286 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1287 struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
1288 struct cxl_region_params *p = &cxlr->params;
1289 struct cxl_decoder *cxld = cxl_rr->decoder;
1290 struct cxl_switch_decoder *cxlsd;
1295 * While root level decoders support x3, x6, x12, switch level
1296 * decoders only support powers of 2 up to x16.
1298 if (!is_power_of_2(cxl_rr->nr_targets)) {
1299 dev_dbg(&cxlr->dev, "%s:%s: invalid target count %d\n",
1300 dev_name(port->uport_dev), dev_name(&port->dev),
1301 cxl_rr->nr_targets);
1305 cxlsd = to_cxl_switch_decoder(&cxld->dev);
1306 if (cxl_rr->nr_targets_set) {
1310 * Passthrough decoders impose no distance requirements between
1313 if (cxl_rr->nr_targets == 1)
1316 distance = p->nr_targets / cxl_rr->nr_targets;
1317 for (i = 0; i < cxl_rr->nr_targets_set; i++)
1318 if (ep->dport == cxlsd->target[i]) {
1319 rc = check_last_peer(cxled, ep, cxl_rr,
1323 goto out_target_set;
1328 if (is_cxl_root(parent_port)) {
1330 * Root decoder IG is always set to value in CFMWS which
1331 * may be different than this region's IG. We can use the
1332 * region's IG here since interleave_granularity_store()
1333 * does not allow interleaved host-bridges with
1334 * root IG != region IG.
1336 parent_ig = p->interleave_granularity;
1337 parent_iw = cxlrd->cxlsd.cxld.interleave_ways;
1339 * For purposes of address bit routing, use power-of-2 math for
1342 if (!is_power_of_2(parent_iw))
1345 struct cxl_region_ref *parent_rr;
1346 struct cxl_decoder *parent_cxld;
1348 parent_rr = cxl_rr_load(parent_port, cxlr);
1349 parent_cxld = parent_rr->decoder;
1350 parent_ig = parent_cxld->interleave_granularity;
1351 parent_iw = parent_cxld->interleave_ways;
1354 rc = granularity_to_eig(parent_ig, &peig);
1356 dev_dbg(&cxlr->dev, "%s:%s: invalid parent granularity: %d\n",
1357 dev_name(parent_port->uport_dev),
1358 dev_name(&parent_port->dev), parent_ig);
1362 rc = ways_to_eiw(parent_iw, &peiw);
1364 dev_dbg(&cxlr->dev, "%s:%s: invalid parent interleave: %d\n",
1365 dev_name(parent_port->uport_dev),
1366 dev_name(&parent_port->dev), parent_iw);
1370 iw = cxl_rr->nr_targets;
1371 rc = ways_to_eiw(iw, &eiw);
1373 dev_dbg(&cxlr->dev, "%s:%s: invalid port interleave: %d\n",
1374 dev_name(port->uport_dev), dev_name(&port->dev), iw);
1379 * Interleave granularity is a multiple of @parent_port granularity.
1380 * Multiplier is the parent port interleave ways.
1382 rc = granularity_to_eig(parent_ig * parent_iw, &eig);
1385 "%s: invalid granularity calculation (%d * %d)\n",
1386 dev_name(&parent_port->dev), parent_ig, parent_iw);
1390 rc = eig_to_granularity(eig, &ig);
1392 dev_dbg(&cxlr->dev, "%s:%s: invalid interleave: %d\n",
1393 dev_name(port->uport_dev), dev_name(&port->dev),
1398 if (iw > 8 || iw > cxlsd->nr_targets) {
1400 "%s:%s:%s: ways: %d overflows targets: %d\n",
1401 dev_name(port->uport_dev), dev_name(&port->dev),
1402 dev_name(&cxld->dev), iw, cxlsd->nr_targets);
1406 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1407 if (cxld->interleave_ways != iw ||
1408 cxld->interleave_granularity != ig ||
1409 cxld->hpa_range.start != p->res->start ||
1410 cxld->hpa_range.end != p->res->end ||
1411 ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) {
1413 "%s:%s %s expected iw: %d ig: %d %pr\n",
1414 dev_name(port->uport_dev), dev_name(&port->dev),
1415 __func__, iw, ig, p->res);
1417 "%s:%s %s got iw: %d ig: %d state: %s %#llx:%#llx\n",
1418 dev_name(port->uport_dev), dev_name(&port->dev),
1419 __func__, cxld->interleave_ways,
1420 cxld->interleave_granularity,
1421 (cxld->flags & CXL_DECODER_F_ENABLE) ?
1424 cxld->hpa_range.start, cxld->hpa_range.end);
1428 rc = check_interleave_cap(cxld, iw, ig);
1431 "%s:%s iw: %d ig: %d is not supported\n",
1432 dev_name(port->uport_dev),
1433 dev_name(&port->dev), iw, ig);
1437 cxld->interleave_ways = iw;
1438 cxld->interleave_granularity = ig;
1439 cxld->hpa_range = (struct range) {
1440 .start = p->res->start,
1444 dev_dbg(&cxlr->dev, "%s:%s iw: %d ig: %d\n", dev_name(port->uport_dev),
1445 dev_name(&port->dev), iw, ig);
1447 if (cxl_rr->nr_targets_set == cxl_rr->nr_targets) {
1449 "%s:%s: targets full trying to add %s:%s at %d\n",
1450 dev_name(port->uport_dev), dev_name(&port->dev),
1451 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1454 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1455 if (cxlsd->target[cxl_rr->nr_targets_set] != ep->dport) {
1456 dev_dbg(&cxlr->dev, "%s:%s: %s expected %s at %d\n",
1457 dev_name(port->uport_dev), dev_name(&port->dev),
1458 dev_name(&cxlsd->cxld.dev),
1459 dev_name(ep->dport->dport_dev),
1460 cxl_rr->nr_targets_set);
1464 cxlsd->target[cxl_rr->nr_targets_set] = ep->dport;
1467 cxl_rr->nr_targets_set += inc;
1468 dev_dbg(&cxlr->dev, "%s:%s target[%d] = %s for %s:%s @ %d\n",
1469 dev_name(port->uport_dev), dev_name(&port->dev),
1470 cxl_rr->nr_targets_set - 1, dev_name(ep->dport->dport_dev),
1471 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1476 static void cxl_port_reset_targets(struct cxl_port *port,
1477 struct cxl_region *cxlr)
1479 struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
1480 struct cxl_decoder *cxld;
1483 * After the last endpoint has been detached the entire cxl_rr may now
1488 cxl_rr->nr_targets_set = 0;
1490 cxld = cxl_rr->decoder;
1491 cxld->hpa_range = (struct range) {
1497 static void cxl_region_teardown_targets(struct cxl_region *cxlr)
1499 struct cxl_region_params *p = &cxlr->params;
1500 struct cxl_endpoint_decoder *cxled;
1501 struct cxl_dev_state *cxlds;
1502 struct cxl_memdev *cxlmd;
1503 struct cxl_port *iter;
1508 * In the auto-discovery case skip automatic teardown since the
1509 * address space is already active
1511 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags))
1514 for (i = 0; i < p->nr_targets; i++) {
1515 cxled = p->targets[i];
1516 cxlmd = cxled_to_memdev(cxled);
1517 cxlds = cxlmd->cxlds;
1522 iter = cxled_to_port(cxled);
1523 while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
1524 iter = to_cxl_port(iter->dev.parent);
1526 for (ep = cxl_ep_load(iter, cxlmd); iter;
1527 iter = ep->next, ep = cxl_ep_load(iter, cxlmd))
1528 cxl_port_reset_targets(iter, cxlr);
1532 static int cxl_region_setup_targets(struct cxl_region *cxlr)
1534 struct cxl_region_params *p = &cxlr->params;
1535 struct cxl_endpoint_decoder *cxled;
1536 struct cxl_dev_state *cxlds;
1537 int i, rc, rch = 0, vh = 0;
1538 struct cxl_memdev *cxlmd;
1539 struct cxl_port *iter;
1542 for (i = 0; i < p->nr_targets; i++) {
1543 cxled = p->targets[i];
1544 cxlmd = cxled_to_memdev(cxled);
1545 cxlds = cxlmd->cxlds;
1547 /* validate that all targets agree on topology */
1555 iter = cxled_to_port(cxled);
1556 while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
1557 iter = to_cxl_port(iter->dev.parent);
1560 * Descend the topology tree programming / validating
1561 * targets while looking for conflicts.
1563 for (ep = cxl_ep_load(iter, cxlmd); iter;
1564 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
1565 rc = cxl_port_setup_targets(iter, cxlr, cxled);
1567 cxl_region_teardown_targets(cxlr);
1574 dev_err(&cxlr->dev, "mismatched CXL topologies detected\n");
1575 cxl_region_teardown_targets(cxlr);
1582 static int cxl_region_validate_position(struct cxl_region *cxlr,
1583 struct cxl_endpoint_decoder *cxled,
1586 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1587 struct cxl_region_params *p = &cxlr->params;
1590 if (pos < 0 || pos >= p->interleave_ways) {
1591 dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
1592 p->interleave_ways);
1596 if (p->targets[pos] == cxled)
1599 if (p->targets[pos]) {
1600 struct cxl_endpoint_decoder *cxled_target = p->targets[pos];
1601 struct cxl_memdev *cxlmd_target = cxled_to_memdev(cxled_target);
1603 dev_dbg(&cxlr->dev, "position %d already assigned to %s:%s\n",
1604 pos, dev_name(&cxlmd_target->dev),
1605 dev_name(&cxled_target->cxld.dev));
1609 for (i = 0; i < p->interleave_ways; i++) {
1610 struct cxl_endpoint_decoder *cxled_target;
1611 struct cxl_memdev *cxlmd_target;
1613 cxled_target = p->targets[i];
1617 cxlmd_target = cxled_to_memdev(cxled_target);
1618 if (cxlmd_target == cxlmd) {
1620 "%s already specified at position %d via: %s\n",
1621 dev_name(&cxlmd->dev), pos,
1622 dev_name(&cxled_target->cxld.dev));
1630 static int cxl_region_attach_position(struct cxl_region *cxlr,
1631 struct cxl_root_decoder *cxlrd,
1632 struct cxl_endpoint_decoder *cxled,
1633 const struct cxl_dport *dport, int pos)
1635 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1636 struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd;
1637 struct cxl_decoder *cxld = &cxlsd->cxld;
1638 int iw = cxld->interleave_ways;
1639 struct cxl_port *iter;
1642 if (dport != cxlrd->cxlsd.target[pos % iw]) {
1643 dev_dbg(&cxlr->dev, "%s:%s invalid target position for %s\n",
1644 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1645 dev_name(&cxlrd->cxlsd.cxld.dev));
1649 for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
1650 iter = to_cxl_port(iter->dev.parent)) {
1651 rc = cxl_port_attach_region(iter, cxlr, cxled, pos);
1659 for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
1660 iter = to_cxl_port(iter->dev.parent))
1661 cxl_port_detach_region(iter, cxlr, cxled);
1665 static int cxl_region_attach_auto(struct cxl_region *cxlr,
1666 struct cxl_endpoint_decoder *cxled, int pos)
1668 struct cxl_region_params *p = &cxlr->params;
1670 if (cxled->state != CXL_DECODER_STATE_AUTO) {
1672 "%s: unable to add decoder to autodetected region\n",
1673 dev_name(&cxled->cxld.dev));
1678 dev_dbg(&cxlr->dev, "%s: expected auto position, not %d\n",
1679 dev_name(&cxled->cxld.dev), pos);
1683 if (p->nr_targets >= p->interleave_ways) {
1684 dev_err(&cxlr->dev, "%s: no more target slots available\n",
1685 dev_name(&cxled->cxld.dev));
1690 * Temporarily record the endpoint decoder into the target array. Yes,
1691 * this means that userspace can view devices in the wrong position
1692 * before the region activates, and must be careful to understand when
1693 * it might be racing region autodiscovery.
1695 pos = p->nr_targets;
1696 p->targets[pos] = cxled;
1703 static int cmp_interleave_pos(const void *a, const void *b)
1705 struct cxl_endpoint_decoder *cxled_a = *(typeof(cxled_a) *)a;
1706 struct cxl_endpoint_decoder *cxled_b = *(typeof(cxled_b) *)b;
1708 return cxled_a->pos - cxled_b->pos;
1711 static struct cxl_port *next_port(struct cxl_port *port)
1713 if (!port->parent_dport)
1715 return port->parent_dport->port;
1718 static int match_switch_decoder_by_range(struct device *dev, void *data)
1720 struct cxl_switch_decoder *cxlsd;
1721 struct range *r1, *r2 = data;
1723 if (!is_switch_decoder(dev))
1726 cxlsd = to_cxl_switch_decoder(dev);
1727 r1 = &cxlsd->cxld.hpa_range;
1729 if (is_root_decoder(dev))
1730 return range_contains(r1, r2);
1731 return (r1->start == r2->start && r1->end == r2->end);
1734 static int find_pos_and_ways(struct cxl_port *port, struct range *range,
1735 int *pos, int *ways)
1737 struct cxl_switch_decoder *cxlsd;
1738 struct cxl_port *parent;
1742 parent = next_port(port);
1746 dev = device_find_child(&parent->dev, range,
1747 match_switch_decoder_by_range);
1749 dev_err(port->uport_dev,
1750 "failed to find decoder mapping %#llx-%#llx\n",
1751 range->start, range->end);
1754 cxlsd = to_cxl_switch_decoder(dev);
1755 *ways = cxlsd->cxld.interleave_ways;
1757 for (int i = 0; i < *ways; i++) {
1758 if (cxlsd->target[i] == port->parent_dport) {
1770 * cxl_calc_interleave_pos() - calculate an endpoint position in a region
1771 * @cxled: endpoint decoder member of given region
1773 * The endpoint position is calculated by traversing the topology from
1774 * the endpoint to the root decoder and iteratively applying this
1777 * position = position * parent_ways + parent_pos;
1779 * ...where @position is inferred from switch and root decoder target lists.
1781 * Return: position >= 0 on success
1784 static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled)
1786 struct cxl_port *iter, *port = cxled_to_port(cxled);
1787 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1788 struct range *range = &cxled->cxld.hpa_range;
1789 int parent_ways = 0, parent_pos = 0, pos = 0;
1793 * Example: the expected interleave order of the 4-way region shown
1794 * below is: mem0, mem2, mem1, mem3
1798 * host_bridge_0 host_bridge_1
1800 * mem0 mem1 mem2 mem3
1802 * In the example the calculator will iterate twice. The first iteration
1803 * uses the mem position in the host-bridge and the ways of the host-
1804 * bridge to generate the first, or local, position. The second
1805 * iteration uses the host-bridge position in the root_port and the ways
1806 * of the root_port to refine the position.
1808 * A trace of the calculation per endpoint looks like this:
1809 * mem0: pos = 0 * 2 + 0 mem2: pos = 0 * 2 + 0
1810 * pos = 0 * 2 + 0 pos = 0 * 2 + 1
1813 * mem1: pos = 0 * 2 + 1 mem3: pos = 0 * 2 + 1
1814 * pos = 1 * 2 + 0 pos = 1 * 2 + 1
1817 * Note that while this example is simple, the method applies to more
1818 * complex topologies, including those with switches.
1821 /* Iterate from endpoint to root_port refining the position */
1822 for (iter = port; iter; iter = next_port(iter)) {
1823 if (is_cxl_root(iter))
1826 rc = find_pos_and_ways(iter, range, &parent_pos, &parent_ways);
1830 pos = pos * parent_ways + parent_pos;
1833 dev_dbg(&cxlmd->dev,
1834 "decoder:%s parent:%s port:%s range:%#llx-%#llx pos:%d\n",
1835 dev_name(&cxled->cxld.dev), dev_name(cxlmd->dev.parent),
1836 dev_name(&port->dev), range->start, range->end, pos);
1841 static int cxl_region_sort_targets(struct cxl_region *cxlr)
1843 struct cxl_region_params *p = &cxlr->params;
1846 for (i = 0; i < p->nr_targets; i++) {
1847 struct cxl_endpoint_decoder *cxled = p->targets[i];
1849 cxled->pos = cxl_calc_interleave_pos(cxled);
1851 * Record that sorting failed, but still continue to calc
1852 * cxled->pos so that follow-on code paths can reliably
1853 * do p->targets[cxled->pos] to self-reference their entry.
1858 /* Keep the cxlr target list in interleave position order */
1859 sort(p->targets, p->nr_targets, sizeof(p->targets[0]),
1860 cmp_interleave_pos, NULL);
1862 dev_dbg(&cxlr->dev, "region sort %s\n", rc ? "failed" : "successful");
1866 static int cxl_region_attach(struct cxl_region *cxlr,
1867 struct cxl_endpoint_decoder *cxled, int pos)
1869 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
1870 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1871 struct cxl_region_params *p = &cxlr->params;
1872 struct cxl_port *ep_port, *root_port;
1873 struct cxl_dport *dport;
1876 rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
1877 p->interleave_granularity);
1879 dev_dbg(&cxlr->dev, "%s iw: %d ig: %d is not supported\n",
1880 dev_name(&cxled->cxld.dev), p->interleave_ways,
1881 p->interleave_granularity);
1885 if (cxled->mode != cxlr->mode) {
1886 dev_dbg(&cxlr->dev, "%s region mode: %d mismatch: %d\n",
1887 dev_name(&cxled->cxld.dev), cxlr->mode, cxled->mode);
1891 if (cxled->mode == CXL_DECODER_DEAD) {
1892 dev_dbg(&cxlr->dev, "%s dead\n", dev_name(&cxled->cxld.dev));
1896 /* all full of members, or interleave config not established? */
1897 if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) {
1898 dev_dbg(&cxlr->dev, "region already active\n");
1900 } else if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
1901 dev_dbg(&cxlr->dev, "interleave config missing\n");
1905 if (p->nr_targets >= p->interleave_ways) {
1906 dev_dbg(&cxlr->dev, "region already has %d endpoints\n",
1911 ep_port = cxled_to_port(cxled);
1912 root_port = cxlrd_to_port(cxlrd);
1913 dport = cxl_find_dport_by_dev(root_port, ep_port->host_bridge);
1915 dev_dbg(&cxlr->dev, "%s:%s invalid target for %s\n",
1916 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1917 dev_name(cxlr->dev.parent));
1921 if (cxled->cxld.target_type != cxlr->type) {
1922 dev_dbg(&cxlr->dev, "%s:%s type mismatch: %d vs %d\n",
1923 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1924 cxled->cxld.target_type, cxlr->type);
1928 if (!cxled->dpa_res) {
1929 dev_dbg(&cxlr->dev, "%s:%s: missing DPA allocation.\n",
1930 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev));
1934 if (resource_size(cxled->dpa_res) * p->interleave_ways !=
1935 resource_size(p->res)) {
1937 "%s:%s: decoder-size-%#llx * ways-%d != region-size-%#llx\n",
1938 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1939 (u64)resource_size(cxled->dpa_res), p->interleave_ways,
1940 (u64)resource_size(p->res));
1944 cxl_region_perf_data_calculate(cxlr, cxled);
1946 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1949 rc = cxl_region_attach_auto(cxlr, cxled, pos);
1953 /* await more targets to arrive... */
1954 if (p->nr_targets < p->interleave_ways)
1958 * All targets are here, which implies all PCI enumeration that
1959 * affects this region has been completed. Walk the topology to
1960 * sort the devices into their relative region decode position.
1962 rc = cxl_region_sort_targets(cxlr);
1966 for (i = 0; i < p->nr_targets; i++) {
1967 cxled = p->targets[i];
1968 ep_port = cxled_to_port(cxled);
1969 dport = cxl_find_dport_by_dev(root_port,
1970 ep_port->host_bridge);
1971 rc = cxl_region_attach_position(cxlr, cxlrd, cxled,
1977 rc = cxl_region_setup_targets(cxlr);
1982 * If target setup succeeds in the autodiscovery case
1983 * then the region is already committed.
1985 p->state = CXL_CONFIG_COMMIT;
1990 rc = cxl_region_validate_position(cxlr, cxled, pos);
1994 rc = cxl_region_attach_position(cxlr, cxlrd, cxled, dport, pos);
1998 p->targets[pos] = cxled;
2002 if (p->nr_targets == p->interleave_ways) {
2003 rc = cxl_region_setup_targets(cxlr);
2006 p->state = CXL_CONFIG_ACTIVE;
2009 cxled->cxld.interleave_ways = p->interleave_ways;
2010 cxled->cxld.interleave_granularity = p->interleave_granularity;
2011 cxled->cxld.hpa_range = (struct range) {
2012 .start = p->res->start,
2016 if (p->nr_targets != p->interleave_ways)
2020 * Test the auto-discovery position calculator function
2021 * against this successfully created user-defined region.
2022 * A fail message here means that this interleave config
2023 * will fail when presented as CXL_REGION_F_AUTO.
2025 for (int i = 0; i < p->nr_targets; i++) {
2026 struct cxl_endpoint_decoder *cxled = p->targets[i];
2029 test_pos = cxl_calc_interleave_pos(cxled);
2030 dev_dbg(&cxled->cxld.dev,
2031 "Test cxl_calc_interleave_pos(): %s test_pos:%d cxled->pos:%d\n",
2032 (test_pos == cxled->pos) ? "success" : "fail",
2033 test_pos, cxled->pos);
2039 static int cxl_region_detach(struct cxl_endpoint_decoder *cxled)
2041 struct cxl_port *iter, *ep_port = cxled_to_port(cxled);
2042 struct cxl_region *cxlr = cxled->cxld.region;
2043 struct cxl_region_params *p;
2046 lockdep_assert_held_write(&cxl_region_rwsem);
2052 get_device(&cxlr->dev);
2054 if (p->state > CXL_CONFIG_ACTIVE) {
2056 * TODO: tear down all impacted regions if a device is
2057 * removed out of order
2059 rc = cxl_region_decode_reset(cxlr, p->interleave_ways);
2062 p->state = CXL_CONFIG_ACTIVE;
2065 for (iter = ep_port; !is_cxl_root(iter);
2066 iter = to_cxl_port(iter->dev.parent))
2067 cxl_port_detach_region(iter, cxlr, cxled);
2069 if (cxled->pos < 0 || cxled->pos >= p->interleave_ways ||
2070 p->targets[cxled->pos] != cxled) {
2071 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
2073 dev_WARN_ONCE(&cxlr->dev, 1, "expected %s:%s at position %d\n",
2074 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2079 if (p->state == CXL_CONFIG_ACTIVE) {
2080 p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
2081 cxl_region_teardown_targets(cxlr);
2083 p->targets[cxled->pos] = NULL;
2085 cxled->cxld.hpa_range = (struct range) {
2090 /* notify the region driver that one of its targets has departed */
2091 up_write(&cxl_region_rwsem);
2092 device_release_driver(&cxlr->dev);
2093 down_write(&cxl_region_rwsem);
2095 put_device(&cxlr->dev);
2099 void cxl_decoder_kill_region(struct cxl_endpoint_decoder *cxled)
2101 down_write(&cxl_region_rwsem);
2102 cxled->mode = CXL_DECODER_DEAD;
2103 cxl_region_detach(cxled);
2104 up_write(&cxl_region_rwsem);
2107 static int attach_target(struct cxl_region *cxlr,
2108 struct cxl_endpoint_decoder *cxled, int pos,
2113 if (state == TASK_INTERRUPTIBLE)
2114 rc = down_write_killable(&cxl_region_rwsem);
2116 down_write(&cxl_region_rwsem);
2120 down_read(&cxl_dpa_rwsem);
2121 rc = cxl_region_attach(cxlr, cxled, pos);
2122 up_read(&cxl_dpa_rwsem);
2123 up_write(&cxl_region_rwsem);
2127 static int detach_target(struct cxl_region *cxlr, int pos)
2129 struct cxl_region_params *p = &cxlr->params;
2132 rc = down_write_killable(&cxl_region_rwsem);
2136 if (pos >= p->interleave_ways) {
2137 dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
2138 p->interleave_ways);
2143 if (!p->targets[pos]) {
2148 rc = cxl_region_detach(p->targets[pos]);
2150 up_write(&cxl_region_rwsem);
2154 static size_t store_targetN(struct cxl_region *cxlr, const char *buf, int pos,
2159 if (sysfs_streq(buf, "\n"))
2160 rc = detach_target(cxlr, pos);
2164 dev = bus_find_device_by_name(&cxl_bus_type, NULL, buf);
2168 if (!is_endpoint_decoder(dev)) {
2173 rc = attach_target(cxlr, to_cxl_endpoint_decoder(dev), pos,
2174 TASK_INTERRUPTIBLE);
2184 #define TARGET_ATTR_RW(n) \
2185 static ssize_t target##n##_show( \
2186 struct device *dev, struct device_attribute *attr, char *buf) \
2188 return show_targetN(to_cxl_region(dev), buf, (n)); \
2190 static ssize_t target##n##_store(struct device *dev, \
2191 struct device_attribute *attr, \
2192 const char *buf, size_t len) \
2194 return store_targetN(to_cxl_region(dev), buf, (n), len); \
2196 static DEVICE_ATTR_RW(target##n)
2215 static struct attribute *target_attrs[] = {
2216 &dev_attr_target0.attr,
2217 &dev_attr_target1.attr,
2218 &dev_attr_target2.attr,
2219 &dev_attr_target3.attr,
2220 &dev_attr_target4.attr,
2221 &dev_attr_target5.attr,
2222 &dev_attr_target6.attr,
2223 &dev_attr_target7.attr,
2224 &dev_attr_target8.attr,
2225 &dev_attr_target9.attr,
2226 &dev_attr_target10.attr,
2227 &dev_attr_target11.attr,
2228 &dev_attr_target12.attr,
2229 &dev_attr_target13.attr,
2230 &dev_attr_target14.attr,
2231 &dev_attr_target15.attr,
2235 static umode_t cxl_region_target_visible(struct kobject *kobj,
2236 struct attribute *a, int n)
2238 struct device *dev = kobj_to_dev(kobj);
2239 struct cxl_region *cxlr = to_cxl_region(dev);
2240 struct cxl_region_params *p = &cxlr->params;
2242 if (n < p->interleave_ways)
2247 static const struct attribute_group cxl_region_target_group = {
2248 .attrs = target_attrs,
2249 .is_visible = cxl_region_target_visible,
2252 static const struct attribute_group *get_cxl_region_target_group(void)
2254 return &cxl_region_target_group;
2257 static const struct attribute_group *region_groups[] = {
2258 &cxl_base_attribute_group,
2260 &cxl_region_target_group,
2261 &cxl_region_access0_coordinate_group,
2262 &cxl_region_access1_coordinate_group,
2266 static void cxl_region_release(struct device *dev)
2268 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
2269 struct cxl_region *cxlr = to_cxl_region(dev);
2270 int id = atomic_read(&cxlrd->region_id);
2273 * Try to reuse the recently idled id rather than the cached
2274 * next id to prevent the region id space from increasing
2278 if (atomic_try_cmpxchg(&cxlrd->region_id, &id, cxlr->id)) {
2283 memregion_free(cxlr->id);
2285 put_device(dev->parent);
2289 const struct device_type cxl_region_type = {
2290 .name = "cxl_region",
2291 .release = cxl_region_release,
2292 .groups = region_groups
2295 bool is_cxl_region(struct device *dev)
2297 return dev->type == &cxl_region_type;
2299 EXPORT_SYMBOL_NS_GPL(is_cxl_region, CXL);
2301 static struct cxl_region *to_cxl_region(struct device *dev)
2303 if (dev_WARN_ONCE(dev, dev->type != &cxl_region_type,
2304 "not a cxl_region device\n"))
2307 return container_of(dev, struct cxl_region, dev);
2310 static void unregister_region(void *_cxlr)
2312 struct cxl_region *cxlr = _cxlr;
2313 struct cxl_region_params *p = &cxlr->params;
2316 unregister_memory_notifier(&cxlr->memory_notifier);
2317 unregister_mt_adistance_algorithm(&cxlr->adist_notifier);
2318 device_del(&cxlr->dev);
2321 * Now that region sysfs is shutdown, the parameter block is now
2322 * read-only, so no need to hold the region rwsem to access the
2323 * region parameters.
2325 for (i = 0; i < p->interleave_ways; i++)
2326 detach_target(cxlr, i);
2328 cxl_region_iomem_release(cxlr);
2329 put_device(&cxlr->dev);
2332 static struct lock_class_key cxl_region_key;
2334 static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id)
2336 struct cxl_region *cxlr;
2339 cxlr = kzalloc(sizeof(*cxlr), GFP_KERNEL);
2342 return ERR_PTR(-ENOMEM);
2346 device_initialize(dev);
2347 lockdep_set_class(&dev->mutex, &cxl_region_key);
2348 dev->parent = &cxlrd->cxlsd.cxld.dev;
2350 * Keep root decoder pinned through cxl_region_release to fixup
2351 * region id allocations
2353 get_device(dev->parent);
2354 device_set_pm_not_required(dev);
2355 dev->bus = &cxl_bus_type;
2356 dev->type = &cxl_region_type;
2362 static bool cxl_region_update_coordinates(struct cxl_region *cxlr, int nid)
2367 for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
2368 if (cxlr->coord[i].read_bandwidth) {
2370 if (cxl_need_node_perf_attrs_update(nid))
2371 node_set_perf_attrs(nid, &cxlr->coord[i], i);
2373 rc = cxl_update_hmat_access_coordinates(nid, cxlr, i);
2383 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_access0_group());
2385 dev_dbg(&cxlr->dev, "Failed to update access0 group\n");
2387 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_access1_group());
2389 dev_dbg(&cxlr->dev, "Failed to update access1 group\n");
2394 static int cxl_region_nid(struct cxl_region *cxlr)
2396 struct cxl_region_params *p = &cxlr->params;
2397 struct resource *res;
2399 guard(rwsem_read)(&cxl_region_rwsem);
2402 return NUMA_NO_NODE;
2403 return phys_to_target_node(res->start);
2406 static int cxl_region_perf_attrs_callback(struct notifier_block *nb,
2407 unsigned long action, void *arg)
2409 struct cxl_region *cxlr = container_of(nb, struct cxl_region,
2411 struct memory_notify *mnb = arg;
2412 int nid = mnb->status_change_nid;
2415 if (nid == NUMA_NO_NODE || action != MEM_ONLINE)
2418 region_nid = cxl_region_nid(cxlr);
2419 if (nid != region_nid)
2422 if (!cxl_region_update_coordinates(cxlr, nid))
2428 static int cxl_region_calculate_adistance(struct notifier_block *nb,
2429 unsigned long nid, void *data)
2431 struct cxl_region *cxlr = container_of(nb, struct cxl_region,
2433 struct access_coordinate *perf;
2437 region_nid = cxl_region_nid(cxlr);
2438 if (nid != region_nid)
2441 perf = &cxlr->coord[ACCESS_COORDINATE_CPU];
2443 if (mt_perf_to_adistance(perf, adist))
2450 * devm_cxl_add_region - Adds a region to a decoder
2451 * @cxlrd: root decoder
2452 * @id: memregion id to create, or memregion_free() on failure
2453 * @mode: mode for the endpoint decoders of this region
2454 * @type: select whether this is an expander or accelerator (type-2 or type-3)
2456 * This is the second step of region initialization. Regions exist within an
2457 * address space which is mapped by a @cxlrd.
2459 * Return: 0 if the region was added to the @cxlrd, else returns negative error
2460 * code. The region will be named "regionZ" where Z is the unique region number.
2462 static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd,
2464 enum cxl_decoder_mode mode,
2465 enum cxl_decoder_type type)
2467 struct cxl_port *port = to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
2468 struct cxl_region *cxlr;
2472 cxlr = cxl_region_alloc(cxlrd, id);
2479 rc = dev_set_name(dev, "region%d", id);
2483 rc = device_add(dev);
2487 cxlr->memory_notifier.notifier_call = cxl_region_perf_attrs_callback;
2488 cxlr->memory_notifier.priority = CXL_CALLBACK_PRI;
2489 register_memory_notifier(&cxlr->memory_notifier);
2491 cxlr->adist_notifier.notifier_call = cxl_region_calculate_adistance;
2492 cxlr->adist_notifier.priority = 100;
2493 register_mt_adistance_algorithm(&cxlr->adist_notifier);
2495 rc = devm_add_action_or_reset(port->uport_dev, unregister_region, cxlr);
2499 dev_dbg(port->uport_dev, "%s: created %s\n",
2500 dev_name(&cxlrd->cxlsd.cxld.dev), dev_name(dev));
2508 static ssize_t __create_region_show(struct cxl_root_decoder *cxlrd, char *buf)
2510 return sysfs_emit(buf, "region%u\n", atomic_read(&cxlrd->region_id));
2513 static ssize_t create_pmem_region_show(struct device *dev,
2514 struct device_attribute *attr, char *buf)
2516 return __create_region_show(to_cxl_root_decoder(dev), buf);
2519 static ssize_t create_ram_region_show(struct device *dev,
2520 struct device_attribute *attr, char *buf)
2522 return __create_region_show(to_cxl_root_decoder(dev), buf);
2525 static struct cxl_region *__create_region(struct cxl_root_decoder *cxlrd,
2526 enum cxl_decoder_mode mode, int id)
2531 case CXL_DECODER_RAM:
2532 case CXL_DECODER_PMEM:
2535 dev_err(&cxlrd->cxlsd.cxld.dev, "unsupported mode %d\n", mode);
2536 return ERR_PTR(-EINVAL);
2539 rc = memregion_alloc(GFP_KERNEL);
2543 if (atomic_cmpxchg(&cxlrd->region_id, id, rc) != id) {
2545 return ERR_PTR(-EBUSY);
2548 return devm_cxl_add_region(cxlrd, id, mode, CXL_DECODER_HOSTONLYMEM);
2551 static ssize_t create_pmem_region_store(struct device *dev,
2552 struct device_attribute *attr,
2553 const char *buf, size_t len)
2555 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2556 struct cxl_region *cxlr;
2559 rc = sscanf(buf, "region%d\n", &id);
2563 cxlr = __create_region(cxlrd, CXL_DECODER_PMEM, id);
2565 return PTR_ERR(cxlr);
2569 DEVICE_ATTR_RW(create_pmem_region);
2571 static ssize_t create_ram_region_store(struct device *dev,
2572 struct device_attribute *attr,
2573 const char *buf, size_t len)
2575 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2576 struct cxl_region *cxlr;
2579 rc = sscanf(buf, "region%d\n", &id);
2583 cxlr = __create_region(cxlrd, CXL_DECODER_RAM, id);
2585 return PTR_ERR(cxlr);
2589 DEVICE_ATTR_RW(create_ram_region);
2591 static ssize_t region_show(struct device *dev, struct device_attribute *attr,
2594 struct cxl_decoder *cxld = to_cxl_decoder(dev);
2597 rc = down_read_interruptible(&cxl_region_rwsem);
2602 rc = sysfs_emit(buf, "%s\n", dev_name(&cxld->region->dev));
2604 rc = sysfs_emit(buf, "\n");
2605 up_read(&cxl_region_rwsem);
2609 DEVICE_ATTR_RO(region);
2611 static struct cxl_region *
2612 cxl_find_region_by_name(struct cxl_root_decoder *cxlrd, const char *name)
2614 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
2615 struct device *region_dev;
2617 region_dev = device_find_child_by_name(&cxld->dev, name);
2619 return ERR_PTR(-ENODEV);
2621 return to_cxl_region(region_dev);
2624 static ssize_t delete_region_store(struct device *dev,
2625 struct device_attribute *attr,
2626 const char *buf, size_t len)
2628 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2629 struct cxl_port *port = to_cxl_port(dev->parent);
2630 struct cxl_region *cxlr;
2632 cxlr = cxl_find_region_by_name(cxlrd, buf);
2634 return PTR_ERR(cxlr);
2636 devm_release_action(port->uport_dev, unregister_region, cxlr);
2637 put_device(&cxlr->dev);
2641 DEVICE_ATTR_WO(delete_region);
2643 static void cxl_pmem_region_release(struct device *dev)
2645 struct cxl_pmem_region *cxlr_pmem = to_cxl_pmem_region(dev);
2648 for (i = 0; i < cxlr_pmem->nr_mappings; i++) {
2649 struct cxl_memdev *cxlmd = cxlr_pmem->mapping[i].cxlmd;
2651 put_device(&cxlmd->dev);
2657 static const struct attribute_group *cxl_pmem_region_attribute_groups[] = {
2658 &cxl_base_attribute_group,
2662 const struct device_type cxl_pmem_region_type = {
2663 .name = "cxl_pmem_region",
2664 .release = cxl_pmem_region_release,
2665 .groups = cxl_pmem_region_attribute_groups,
2668 bool is_cxl_pmem_region(struct device *dev)
2670 return dev->type == &cxl_pmem_region_type;
2672 EXPORT_SYMBOL_NS_GPL(is_cxl_pmem_region, CXL);
2674 struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev)
2676 if (dev_WARN_ONCE(dev, !is_cxl_pmem_region(dev),
2677 "not a cxl_pmem_region device\n"))
2679 return container_of(dev, struct cxl_pmem_region, dev);
2681 EXPORT_SYMBOL_NS_GPL(to_cxl_pmem_region, CXL);
2683 struct cxl_poison_context {
2684 struct cxl_port *port;
2685 enum cxl_decoder_mode mode;
2689 static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
2690 struct cxl_poison_context *ctx)
2692 struct cxl_dev_state *cxlds = cxlmd->cxlds;
2697 * Collect poison for the remaining unmapped resources
2698 * after poison is collected by committed endpoints.
2700 * Knowing that PMEM must always follow RAM, get poison
2701 * for unmapped resources based on the last decoder's mode:
2702 * ram: scan remains of ram range, then any pmem range
2703 * pmem: scan remains of pmem range
2706 if (ctx->mode == CXL_DECODER_RAM) {
2707 offset = ctx->offset;
2708 length = resource_size(&cxlds->ram_res) - offset;
2709 rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
2715 if (ctx->mode == CXL_DECODER_PMEM) {
2716 offset = ctx->offset;
2717 length = resource_size(&cxlds->dpa_res) - offset;
2720 } else if (resource_size(&cxlds->pmem_res)) {
2721 offset = cxlds->pmem_res.start;
2722 length = resource_size(&cxlds->pmem_res);
2727 return cxl_mem_get_poison(cxlmd, offset, length, NULL);
2730 static int poison_by_decoder(struct device *dev, void *arg)
2732 struct cxl_poison_context *ctx = arg;
2733 struct cxl_endpoint_decoder *cxled;
2734 struct cxl_memdev *cxlmd;
2738 if (!is_endpoint_decoder(dev))
2741 cxled = to_cxl_endpoint_decoder(dev);
2742 if (!cxled->dpa_res || !resource_size(cxled->dpa_res))
2746 * Regions are only created with single mode decoders: pmem or ram.
2747 * Linux does not support mixed mode decoders. This means that
2748 * reading poison per endpoint decoder adheres to the requirement
2749 * that poison reads of pmem and ram must be separated.
2750 * CXL 3.0 Spec 8.2.9.8.4.1
2752 if (cxled->mode == CXL_DECODER_MIXED) {
2753 dev_dbg(dev, "poison list read unsupported in mixed mode\n");
2757 cxlmd = cxled_to_memdev(cxled);
2759 offset = cxled->dpa_res->start - cxled->skip;
2760 length = cxled->skip;
2761 rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
2762 if (rc == -EFAULT && cxled->mode == CXL_DECODER_RAM)
2768 offset = cxled->dpa_res->start;
2769 length = cxled->dpa_res->end - offset + 1;
2770 rc = cxl_mem_get_poison(cxlmd, offset, length, cxled->cxld.region);
2771 if (rc == -EFAULT && cxled->mode == CXL_DECODER_RAM)
2776 /* Iterate until commit_end is reached */
2777 if (cxled->cxld.id == ctx->port->commit_end) {
2778 ctx->offset = cxled->dpa_res->end + 1;
2779 ctx->mode = cxled->mode;
2786 int cxl_get_poison_by_endpoint(struct cxl_port *port)
2788 struct cxl_poison_context ctx;
2791 ctx = (struct cxl_poison_context) {
2795 rc = device_for_each_child(&port->dev, &ctx, poison_by_decoder);
2797 rc = cxl_get_poison_unmapped(to_cxl_memdev(port->uport_dev),
2803 struct cxl_dpa_to_region_context {
2804 struct cxl_region *cxlr;
2808 static int __cxl_dpa_to_region(struct device *dev, void *arg)
2810 struct cxl_dpa_to_region_context *ctx = arg;
2811 struct cxl_endpoint_decoder *cxled;
2812 struct cxl_region *cxlr;
2815 if (!is_endpoint_decoder(dev))
2818 cxled = to_cxl_endpoint_decoder(dev);
2819 if (!cxled || !cxled->dpa_res || !resource_size(cxled->dpa_res))
2822 if (dpa > cxled->dpa_res->end || dpa < cxled->dpa_res->start)
2826 * Stop the region search (return 1) when an endpoint mapping is
2827 * found. The region may not be fully constructed so offering
2828 * the cxlr in the context structure is not guaranteed.
2830 cxlr = cxled->cxld.region;
2832 dev_dbg(dev, "dpa:0x%llx mapped in region:%s\n", dpa,
2833 dev_name(&cxlr->dev));
2835 dev_dbg(dev, "dpa:0x%llx mapped in endpoint:%s\n", dpa,
2843 struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa)
2845 struct cxl_dpa_to_region_context ctx;
2846 struct cxl_port *port;
2848 ctx = (struct cxl_dpa_to_region_context) {
2851 port = cxlmd->endpoint;
2852 if (port && is_cxl_endpoint(port) && cxl_num_decoders_committed(port))
2853 device_for_each_child(&port->dev, &ctx, __cxl_dpa_to_region);
2858 static bool cxl_is_hpa_in_chunk(u64 hpa, struct cxl_region *cxlr, int pos)
2860 struct cxl_region_params *p = &cxlr->params;
2861 int gran = p->interleave_granularity;
2862 int ways = p->interleave_ways;
2865 /* Is the hpa in an expected chunk for its pos(-ition) */
2866 offset = hpa - p->res->start;
2867 offset = do_div(offset, gran * ways);
2868 if ((offset >= pos * gran) && (offset < (pos + 1) * gran))
2872 "Addr trans fail: hpa 0x%llx not in expected chunk\n", hpa);
2877 u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
2880 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
2881 u64 dpa_offset, hpa_offset, bits_upper, mask_upper, hpa;
2882 struct cxl_region_params *p = &cxlr->params;
2883 struct cxl_endpoint_decoder *cxled = NULL;
2888 for (int i = 0; i < p->nr_targets; i++) {
2889 cxled = p->targets[i];
2890 if (cxlmd == cxled_to_memdev(cxled))
2893 if (!cxled || cxlmd != cxled_to_memdev(cxled))
2897 ways_to_eiw(p->interleave_ways, &eiw);
2898 granularity_to_eig(p->interleave_granularity, &eig);
2901 * The device position in the region interleave set was removed
2902 * from the offset at HPA->DPA translation. To reconstruct the
2903 * HPA, place the 'pos' in the offset.
2905 * The placement of 'pos' in the HPA is determined by interleave
2906 * ways and granularity and is defined in the CXL Spec 3.0 Section
2907 * 8.2.4.19.13 Implementation Note: Device Decode Logic
2910 /* Remove the dpa base */
2911 dpa_offset = dpa - cxl_dpa_resource_start(cxled);
2913 mask_upper = GENMASK_ULL(51, eig + 8);
2916 hpa_offset = (dpa_offset & mask_upper) << eiw;
2917 hpa_offset |= pos << (eig + 8);
2919 bits_upper = (dpa_offset & mask_upper) >> (eig + 8);
2920 bits_upper = bits_upper * 3;
2921 hpa_offset = ((bits_upper << (eiw - 8)) + pos) << (eig + 8);
2924 /* The lower bits remain unchanged */
2925 hpa_offset |= dpa_offset & GENMASK_ULL(eig + 7, 0);
2927 /* Apply the hpa_offset to the region base address */
2928 hpa = hpa_offset + p->res->start;
2930 /* Root decoder translation overrides typical modulo decode */
2931 if (cxlrd->hpa_to_spa)
2932 hpa = cxlrd->hpa_to_spa(cxlrd, hpa);
2934 if (hpa < p->res->start || hpa > p->res->end) {
2936 "Addr trans fail: hpa 0x%llx not in region\n", hpa);
2940 /* Simple chunk check, by pos & gran, only applies to modulo decodes */
2941 if (!cxlrd->hpa_to_spa && (!cxl_is_hpa_in_chunk(hpa, cxlr, pos)))
2947 static struct lock_class_key cxl_pmem_region_key;
2949 static int cxl_pmem_region_alloc(struct cxl_region *cxlr)
2951 struct cxl_region_params *p = &cxlr->params;
2952 struct cxl_nvdimm_bridge *cxl_nvb;
2956 guard(rwsem_read)(&cxl_region_rwsem);
2957 if (p->state != CXL_CONFIG_COMMIT)
2960 struct cxl_pmem_region *cxlr_pmem __free(kfree) =
2961 kzalloc(struct_size(cxlr_pmem, mapping, p->nr_targets), GFP_KERNEL);
2965 cxlr_pmem->hpa_range.start = p->res->start;
2966 cxlr_pmem->hpa_range.end = p->res->end;
2968 /* Snapshot the region configuration underneath the cxl_region_rwsem */
2969 cxlr_pmem->nr_mappings = p->nr_targets;
2970 for (i = 0; i < p->nr_targets; i++) {
2971 struct cxl_endpoint_decoder *cxled = p->targets[i];
2972 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
2973 struct cxl_pmem_region_mapping *m = &cxlr_pmem->mapping[i];
2976 * Regions never span CXL root devices, so by definition the
2977 * bridge for one device is the same for all.
2980 cxl_nvb = cxl_find_nvdimm_bridge(cxlmd->endpoint);
2983 cxlr->cxl_nvb = cxl_nvb;
2986 get_device(&cxlmd->dev);
2987 m->start = cxled->dpa_res->start;
2988 m->size = resource_size(cxled->dpa_res);
2992 dev = &cxlr_pmem->dev;
2993 device_initialize(dev);
2994 lockdep_set_class(&dev->mutex, &cxl_pmem_region_key);
2995 device_set_pm_not_required(dev);
2996 dev->parent = &cxlr->dev;
2997 dev->bus = &cxl_bus_type;
2998 dev->type = &cxl_pmem_region_type;
2999 cxlr_pmem->cxlr = cxlr;
3000 cxlr->cxlr_pmem = no_free_ptr(cxlr_pmem);
3005 static void cxl_dax_region_release(struct device *dev)
3007 struct cxl_dax_region *cxlr_dax = to_cxl_dax_region(dev);
3012 static const struct attribute_group *cxl_dax_region_attribute_groups[] = {
3013 &cxl_base_attribute_group,
3017 const struct device_type cxl_dax_region_type = {
3018 .name = "cxl_dax_region",
3019 .release = cxl_dax_region_release,
3020 .groups = cxl_dax_region_attribute_groups,
3023 static bool is_cxl_dax_region(struct device *dev)
3025 return dev->type == &cxl_dax_region_type;
3028 struct cxl_dax_region *to_cxl_dax_region(struct device *dev)
3030 if (dev_WARN_ONCE(dev, !is_cxl_dax_region(dev),
3031 "not a cxl_dax_region device\n"))
3033 return container_of(dev, struct cxl_dax_region, dev);
3035 EXPORT_SYMBOL_NS_GPL(to_cxl_dax_region, CXL);
3037 static struct lock_class_key cxl_dax_region_key;
3039 static struct cxl_dax_region *cxl_dax_region_alloc(struct cxl_region *cxlr)
3041 struct cxl_region_params *p = &cxlr->params;
3042 struct cxl_dax_region *cxlr_dax;
3045 down_read(&cxl_region_rwsem);
3046 if (p->state != CXL_CONFIG_COMMIT) {
3047 cxlr_dax = ERR_PTR(-ENXIO);
3051 cxlr_dax = kzalloc(sizeof(*cxlr_dax), GFP_KERNEL);
3053 cxlr_dax = ERR_PTR(-ENOMEM);
3057 cxlr_dax->hpa_range.start = p->res->start;
3058 cxlr_dax->hpa_range.end = p->res->end;
3060 dev = &cxlr_dax->dev;
3061 cxlr_dax->cxlr = cxlr;
3062 device_initialize(dev);
3063 lockdep_set_class(&dev->mutex, &cxl_dax_region_key);
3064 device_set_pm_not_required(dev);
3065 dev->parent = &cxlr->dev;
3066 dev->bus = &cxl_bus_type;
3067 dev->type = &cxl_dax_region_type;
3069 up_read(&cxl_region_rwsem);
3074 static void cxlr_pmem_unregister(void *_cxlr_pmem)
3076 struct cxl_pmem_region *cxlr_pmem = _cxlr_pmem;
3077 struct cxl_region *cxlr = cxlr_pmem->cxlr;
3078 struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
3081 * Either the bridge is in ->remove() context under the device_lock(),
3082 * or cxlr_release_nvdimm() is cancelling the bridge's release action
3083 * for @cxlr_pmem and doing it itself (while manually holding the bridge
3086 device_lock_assert(&cxl_nvb->dev);
3087 cxlr->cxlr_pmem = NULL;
3088 cxlr_pmem->cxlr = NULL;
3089 device_unregister(&cxlr_pmem->dev);
3092 static void cxlr_release_nvdimm(void *_cxlr)
3094 struct cxl_region *cxlr = _cxlr;
3095 struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
3097 device_lock(&cxl_nvb->dev);
3098 if (cxlr->cxlr_pmem)
3099 devm_release_action(&cxl_nvb->dev, cxlr_pmem_unregister,
3101 device_unlock(&cxl_nvb->dev);
3102 cxlr->cxl_nvb = NULL;
3103 put_device(&cxl_nvb->dev);
3107 * devm_cxl_add_pmem_region() - add a cxl_region-to-nd_region bridge
3108 * @cxlr: parent CXL region for this pmem region bridge device
3110 * Return: 0 on success negative error code on failure.
3112 static int devm_cxl_add_pmem_region(struct cxl_region *cxlr)
3114 struct cxl_pmem_region *cxlr_pmem;
3115 struct cxl_nvdimm_bridge *cxl_nvb;
3119 rc = cxl_pmem_region_alloc(cxlr);
3122 cxlr_pmem = cxlr->cxlr_pmem;
3123 cxl_nvb = cxlr->cxl_nvb;
3125 dev = &cxlr_pmem->dev;
3126 rc = dev_set_name(dev, "pmem_region%d", cxlr->id);
3130 rc = device_add(dev);
3134 dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
3137 device_lock(&cxl_nvb->dev);
3138 if (cxl_nvb->dev.driver)
3139 rc = devm_add_action_or_reset(&cxl_nvb->dev,
3140 cxlr_pmem_unregister, cxlr_pmem);
3143 device_unlock(&cxl_nvb->dev);
3148 /* @cxlr carries a reference on @cxl_nvb until cxlr_release_nvdimm */
3149 return devm_add_action_or_reset(&cxlr->dev, cxlr_release_nvdimm, cxlr);
3154 put_device(&cxl_nvb->dev);
3155 cxlr->cxl_nvb = NULL;
3159 static void cxlr_dax_unregister(void *_cxlr_dax)
3161 struct cxl_dax_region *cxlr_dax = _cxlr_dax;
3163 device_unregister(&cxlr_dax->dev);
3166 static int devm_cxl_add_dax_region(struct cxl_region *cxlr)
3168 struct cxl_dax_region *cxlr_dax;
3172 cxlr_dax = cxl_dax_region_alloc(cxlr);
3173 if (IS_ERR(cxlr_dax))
3174 return PTR_ERR(cxlr_dax);
3176 dev = &cxlr_dax->dev;
3177 rc = dev_set_name(dev, "dax_region%d", cxlr->id);
3181 rc = device_add(dev);
3185 dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
3188 return devm_add_action_or_reset(&cxlr->dev, cxlr_dax_unregister,
3195 static int match_root_decoder_by_range(struct device *dev, void *data)
3197 struct range *r1, *r2 = data;
3198 struct cxl_root_decoder *cxlrd;
3200 if (!is_root_decoder(dev))
3203 cxlrd = to_cxl_root_decoder(dev);
3204 r1 = &cxlrd->cxlsd.cxld.hpa_range;
3205 return range_contains(r1, r2);
3208 static int match_region_by_range(struct device *dev, void *data)
3210 struct cxl_region_params *p;
3211 struct cxl_region *cxlr;
3212 struct range *r = data;
3215 if (!is_cxl_region(dev))
3218 cxlr = to_cxl_region(dev);
3221 down_read(&cxl_region_rwsem);
3222 if (p->res && p->res->start == r->start && p->res->end == r->end)
3224 up_read(&cxl_region_rwsem);
3229 /* Establish an empty region covering the given HPA range */
3230 static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
3231 struct cxl_endpoint_decoder *cxled)
3233 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
3234 struct cxl_port *port = cxlrd_to_port(cxlrd);
3235 struct range *hpa = &cxled->cxld.hpa_range;
3236 struct cxl_region_params *p;
3237 struct cxl_region *cxlr;
3238 struct resource *res;
3242 cxlr = __create_region(cxlrd, cxled->mode,
3243 atomic_read(&cxlrd->region_id));
3244 } while (IS_ERR(cxlr) && PTR_ERR(cxlr) == -EBUSY);
3247 dev_err(cxlmd->dev.parent,
3248 "%s:%s: %s failed assign region: %ld\n",
3249 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
3250 __func__, PTR_ERR(cxlr));
3254 down_write(&cxl_region_rwsem);
3256 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
3257 dev_err(cxlmd->dev.parent,
3258 "%s:%s: %s autodiscovery interrupted\n",
3259 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
3265 set_bit(CXL_REGION_F_AUTO, &cxlr->flags);
3267 res = kmalloc(sizeof(*res), GFP_KERNEL);
3273 *res = DEFINE_RES_MEM_NAMED(hpa->start, range_len(hpa),
3274 dev_name(&cxlr->dev));
3275 rc = insert_resource(cxlrd->res, res);
3278 * Platform-firmware may not have split resources like "System
3279 * RAM" on CXL window boundaries see cxl_region_iomem_release()
3281 dev_warn(cxlmd->dev.parent,
3282 "%s:%s: %s %s cannot insert resource\n",
3283 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
3284 __func__, dev_name(&cxlr->dev));
3288 p->interleave_ways = cxled->cxld.interleave_ways;
3289 p->interleave_granularity = cxled->cxld.interleave_granularity;
3290 p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
3292 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
3296 dev_dbg(cxlmd->dev.parent, "%s:%s: %s %s res: %pr iw: %d ig: %d\n",
3297 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), __func__,
3298 dev_name(&cxlr->dev), p->res, p->interleave_ways,
3299 p->interleave_granularity);
3301 /* ...to match put_device() in cxl_add_to_region() */
3302 get_device(&cxlr->dev);
3303 up_write(&cxl_region_rwsem);
3308 up_write(&cxl_region_rwsem);
3309 devm_release_action(port->uport_dev, unregister_region, cxlr);
3313 int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled)
3315 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
3316 struct range *hpa = &cxled->cxld.hpa_range;
3317 struct cxl_decoder *cxld = &cxled->cxld;
3318 struct device *cxlrd_dev, *region_dev;
3319 struct cxl_root_decoder *cxlrd;
3320 struct cxl_region_params *p;
3321 struct cxl_region *cxlr;
3322 bool attach = false;
3325 cxlrd_dev = device_find_child(&root->dev, &cxld->hpa_range,
3326 match_root_decoder_by_range);
3328 dev_err(cxlmd->dev.parent,
3329 "%s:%s no CXL window for range %#llx:%#llx\n",
3330 dev_name(&cxlmd->dev), dev_name(&cxld->dev),
3331 cxld->hpa_range.start, cxld->hpa_range.end);
3335 cxlrd = to_cxl_root_decoder(cxlrd_dev);
3338 * Ensure that if multiple threads race to construct_region() for @hpa
3339 * one does the construction and the others add to that.
3341 mutex_lock(&cxlrd->range_lock);
3342 region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa,
3343 match_region_by_range);
3345 cxlr = construct_region(cxlrd, cxled);
3346 region_dev = &cxlr->dev;
3348 cxlr = to_cxl_region(region_dev);
3349 mutex_unlock(&cxlrd->range_lock);
3351 rc = PTR_ERR_OR_ZERO(cxlr);
3355 attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE);
3357 down_read(&cxl_region_rwsem);
3359 attach = p->state == CXL_CONFIG_COMMIT;
3360 up_read(&cxl_region_rwsem);
3364 * If device_attach() fails the range may still be active via
3365 * the platform-firmware memory map, otherwise the driver for
3366 * regions is local to this file, so driver matching can't fail.
3368 if (device_attach(&cxlr->dev) < 0)
3369 dev_err(&cxlr->dev, "failed to enable, range: %pr\n",
3373 put_device(region_dev);
3375 put_device(cxlrd_dev);
3378 EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, CXL);
3380 static int is_system_ram(struct resource *res, void *arg)
3382 struct cxl_region *cxlr = arg;
3383 struct cxl_region_params *p = &cxlr->params;
3385 dev_dbg(&cxlr->dev, "%pr has System RAM: %pr\n", p->res, res);
3389 static int cxl_region_probe(struct device *dev)
3391 struct cxl_region *cxlr = to_cxl_region(dev);
3392 struct cxl_region_params *p = &cxlr->params;
3395 rc = down_read_interruptible(&cxl_region_rwsem);
3397 dev_dbg(&cxlr->dev, "probe interrupted\n");
3401 if (p->state < CXL_CONFIG_COMMIT) {
3402 dev_dbg(&cxlr->dev, "config state: %d\n", p->state);
3407 if (test_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags)) {
3409 "failed to activate, re-commit region and retry\n");
3415 * From this point on any path that changes the region's state away from
3416 * CXL_CONFIG_COMMIT is also responsible for releasing the driver.
3419 up_read(&cxl_region_rwsem);
3424 switch (cxlr->mode) {
3425 case CXL_DECODER_PMEM:
3426 return devm_cxl_add_pmem_region(cxlr);
3427 case CXL_DECODER_RAM:
3429 * The region can not be manged by CXL if any portion of
3430 * it is already online as 'System RAM'
3432 if (walk_iomem_res_desc(IORES_DESC_NONE,
3433 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
3434 p->res->start, p->res->end, cxlr,
3437 return devm_cxl_add_dax_region(cxlr);
3439 dev_dbg(&cxlr->dev, "unsupported region mode: %d\n",
3445 static struct cxl_driver cxl_region_driver = {
3446 .name = "cxl_region",
3447 .probe = cxl_region_probe,
3448 .id = CXL_DEVICE_REGION,
3451 int cxl_region_init(void)
3453 return cxl_driver_register(&cxl_region_driver);
3456 void cxl_region_exit(void)
3458 cxl_driver_unregister(&cxl_region_driver);
3461 MODULE_IMPORT_NS(CXL);
3462 MODULE_IMPORT_NS(DEVMEM);
3463 MODULE_ALIAS_CXL(CXL_DEVICE_REGION);