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/slab.h>
8 #include <linux/uuid.h>
9 #include <linux/sort.h>
10 #include <linux/idr.h>
16 * DOC: cxl core region
18 * CXL Regions represent mapped memory capacity in system physical address
19 * space. Whereas the CXL Root Decoders identify the bounds of potential CXL
20 * Memory ranges, Regions represent the active mapped capacity by the HDM
21 * Decoder Capability structures throughout the Host Bridges, Switches, and
22 * Endpoints in the topology.
24 * Region configuration has ordering constraints. UUID may be set at any time
25 * but is only visible for persistent regions.
26 * 1. Interleave granularity
32 * All changes to the interleave configuration occur with this lock held
35 static DECLARE_RWSEM(cxl_region_rwsem);
37 static struct cxl_region *to_cxl_region(struct device *dev);
39 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
42 struct cxl_region *cxlr = to_cxl_region(dev);
43 struct cxl_region_params *p = &cxlr->params;
46 rc = down_read_interruptible(&cxl_region_rwsem);
49 if (cxlr->mode != CXL_DECODER_PMEM)
50 rc = sysfs_emit(buf, "\n");
52 rc = sysfs_emit(buf, "%pUb\n", &p->uuid);
53 up_read(&cxl_region_rwsem);
58 static int is_dup(struct device *match, void *data)
60 struct cxl_region_params *p;
61 struct cxl_region *cxlr;
64 if (!is_cxl_region(match))
67 lockdep_assert_held(&cxl_region_rwsem);
68 cxlr = to_cxl_region(match);
71 if (uuid_equal(&p->uuid, uuid)) {
72 dev_dbg(match, "already has uuid: %pUb\n", uuid);
79 static ssize_t uuid_store(struct device *dev, struct device_attribute *attr,
80 const char *buf, size_t len)
82 struct cxl_region *cxlr = to_cxl_region(dev);
83 struct cxl_region_params *p = &cxlr->params;
87 if (len != UUID_STRING_LEN + 1)
90 rc = uuid_parse(buf, &temp);
94 if (uuid_is_null(&temp))
97 rc = down_write_killable(&cxl_region_rwsem);
101 if (uuid_equal(&p->uuid, &temp))
105 if (p->state >= CXL_CONFIG_ACTIVE)
108 rc = bus_for_each_dev(&cxl_bus_type, NULL, &temp, is_dup);
112 uuid_copy(&p->uuid, &temp);
114 up_write(&cxl_region_rwsem);
120 static DEVICE_ATTR_RW(uuid);
122 static struct cxl_region_ref *cxl_rr_load(struct cxl_port *port,
123 struct cxl_region *cxlr)
125 return xa_load(&port->regions, (unsigned long)cxlr);
128 static int cxl_region_decode_reset(struct cxl_region *cxlr, int count)
130 struct cxl_region_params *p = &cxlr->params;
133 for (i = count - 1; i >= 0; i--) {
134 struct cxl_endpoint_decoder *cxled = p->targets[i];
135 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
136 struct cxl_port *iter = cxled_to_port(cxled);
137 struct cxl_dev_state *cxlds = cxlmd->cxlds;
144 while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
145 iter = to_cxl_port(iter->dev.parent);
147 for (ep = cxl_ep_load(iter, cxlmd); iter;
148 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
149 struct cxl_region_ref *cxl_rr;
150 struct cxl_decoder *cxld;
152 cxl_rr = cxl_rr_load(iter, cxlr);
153 cxld = cxl_rr->decoder;
155 rc = cxld->reset(cxld);
161 rc = cxled->cxld.reset(&cxled->cxld);
169 static int commit_decoder(struct cxl_decoder *cxld)
171 struct cxl_switch_decoder *cxlsd = NULL;
174 return cxld->commit(cxld);
176 if (is_switch_decoder(&cxld->dev))
177 cxlsd = to_cxl_switch_decoder(&cxld->dev);
179 if (dev_WARN_ONCE(&cxld->dev, !cxlsd || cxlsd->nr_targets > 1,
180 "->commit() is required\n"))
185 static int cxl_region_decode_commit(struct cxl_region *cxlr)
187 struct cxl_region_params *p = &cxlr->params;
190 for (i = 0; i < p->nr_targets; i++) {
191 struct cxl_endpoint_decoder *cxled = p->targets[i];
192 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
193 struct cxl_region_ref *cxl_rr;
194 struct cxl_decoder *cxld;
195 struct cxl_port *iter;
198 /* commit bottom up */
199 for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
200 iter = to_cxl_port(iter->dev.parent)) {
201 cxl_rr = cxl_rr_load(iter, cxlr);
202 cxld = cxl_rr->decoder;
203 rc = commit_decoder(cxld);
209 /* programming @iter failed, teardown */
210 for (ep = cxl_ep_load(iter, cxlmd); ep && iter;
211 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
212 cxl_rr = cxl_rr_load(iter, cxlr);
213 cxld = cxl_rr->decoder;
218 cxled->cxld.reset(&cxled->cxld);
226 /* undo the targets that were successfully committed */
227 cxl_region_decode_reset(cxlr, i);
231 static ssize_t commit_store(struct device *dev, struct device_attribute *attr,
232 const char *buf, size_t len)
234 struct cxl_region *cxlr = to_cxl_region(dev);
235 struct cxl_region_params *p = &cxlr->params;
239 rc = kstrtobool(buf, &commit);
243 rc = down_write_killable(&cxl_region_rwsem);
247 /* Already in the requested state? */
248 if (commit && p->state >= CXL_CONFIG_COMMIT)
250 if (!commit && p->state < CXL_CONFIG_COMMIT)
253 /* Not ready to commit? */
254 if (commit && p->state < CXL_CONFIG_ACTIVE) {
260 rc = cxl_region_decode_commit(cxlr);
262 p->state = CXL_CONFIG_RESET_PENDING;
263 up_write(&cxl_region_rwsem);
264 device_release_driver(&cxlr->dev);
265 down_write(&cxl_region_rwsem);
268 * The lock was dropped, so need to revalidate that the reset is
271 if (p->state == CXL_CONFIG_RESET_PENDING)
272 rc = cxl_region_decode_reset(cxlr, p->interleave_ways);
279 p->state = CXL_CONFIG_COMMIT;
280 else if (p->state == CXL_CONFIG_RESET_PENDING)
281 p->state = CXL_CONFIG_ACTIVE;
284 up_write(&cxl_region_rwsem);
291 static ssize_t commit_show(struct device *dev, struct device_attribute *attr,
294 struct cxl_region *cxlr = to_cxl_region(dev);
295 struct cxl_region_params *p = &cxlr->params;
298 rc = down_read_interruptible(&cxl_region_rwsem);
301 rc = sysfs_emit(buf, "%d\n", p->state >= CXL_CONFIG_COMMIT);
302 up_read(&cxl_region_rwsem);
306 static DEVICE_ATTR_RW(commit);
308 static umode_t cxl_region_visible(struct kobject *kobj, struct attribute *a,
311 struct device *dev = kobj_to_dev(kobj);
312 struct cxl_region *cxlr = to_cxl_region(dev);
315 * Support tooling that expects to find a 'uuid' attribute for all
316 * regions regardless of mode.
318 if (a == &dev_attr_uuid.attr && cxlr->mode != CXL_DECODER_PMEM)
323 static ssize_t interleave_ways_show(struct device *dev,
324 struct device_attribute *attr, char *buf)
326 struct cxl_region *cxlr = to_cxl_region(dev);
327 struct cxl_region_params *p = &cxlr->params;
330 rc = down_read_interruptible(&cxl_region_rwsem);
333 rc = sysfs_emit(buf, "%d\n", p->interleave_ways);
334 up_read(&cxl_region_rwsem);
339 static const struct attribute_group *get_cxl_region_target_group(void);
341 static ssize_t interleave_ways_store(struct device *dev,
342 struct device_attribute *attr,
343 const char *buf, size_t len)
345 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
346 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
347 struct cxl_region *cxlr = to_cxl_region(dev);
348 struct cxl_region_params *p = &cxlr->params;
349 unsigned int val, save;
353 rc = kstrtouint(buf, 0, &val);
357 rc = ways_to_eiw(val, &iw);
362 * Even for x3, x9, and x12 interleaves the region interleave must be a
363 * power of 2 multiple of the host bridge interleave.
365 if (!is_power_of_2(val / cxld->interleave_ways) ||
366 (val % cxld->interleave_ways)) {
367 dev_dbg(&cxlr->dev, "invalid interleave: %d\n", val);
371 rc = down_write_killable(&cxl_region_rwsem);
374 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
379 save = p->interleave_ways;
380 p->interleave_ways = val;
381 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
383 p->interleave_ways = save;
385 up_write(&cxl_region_rwsem);
390 static DEVICE_ATTR_RW(interleave_ways);
392 static ssize_t interleave_granularity_show(struct device *dev,
393 struct device_attribute *attr,
396 struct cxl_region *cxlr = to_cxl_region(dev);
397 struct cxl_region_params *p = &cxlr->params;
400 rc = down_read_interruptible(&cxl_region_rwsem);
403 rc = sysfs_emit(buf, "%d\n", p->interleave_granularity);
404 up_read(&cxl_region_rwsem);
409 static ssize_t interleave_granularity_store(struct device *dev,
410 struct device_attribute *attr,
411 const char *buf, size_t len)
413 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
414 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
415 struct cxl_region *cxlr = to_cxl_region(dev);
416 struct cxl_region_params *p = &cxlr->params;
420 rc = kstrtoint(buf, 0, &val);
424 rc = granularity_to_eig(val, &ig);
429 * When the host-bridge is interleaved, disallow region granularity !=
430 * root granularity. Regions with a granularity less than the root
431 * interleave result in needing multiple endpoints to support a single
432 * slot in the interleave (possible to support in the future). Regions
433 * with a granularity greater than the root interleave result in invalid
434 * DPA translations (invalid to support).
436 if (cxld->interleave_ways > 1 && val != cxld->interleave_granularity)
439 rc = down_write_killable(&cxl_region_rwsem);
442 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
447 p->interleave_granularity = val;
449 up_write(&cxl_region_rwsem);
454 static DEVICE_ATTR_RW(interleave_granularity);
456 static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
459 struct cxl_region *cxlr = to_cxl_region(dev);
460 struct cxl_region_params *p = &cxlr->params;
461 u64 resource = -1ULL;
464 rc = down_read_interruptible(&cxl_region_rwsem);
468 resource = p->res->start;
469 rc = sysfs_emit(buf, "%#llx\n", resource);
470 up_read(&cxl_region_rwsem);
474 static DEVICE_ATTR_RO(resource);
476 static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
479 struct cxl_region *cxlr = to_cxl_region(dev);
481 return sysfs_emit(buf, "%s\n", cxl_decoder_mode_name(cxlr->mode));
483 static DEVICE_ATTR_RO(mode);
485 static int alloc_hpa(struct cxl_region *cxlr, resource_size_t size)
487 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
488 struct cxl_region_params *p = &cxlr->params;
489 struct resource *res;
492 lockdep_assert_held_write(&cxl_region_rwsem);
494 /* Nothing to do... */
495 if (p->res && resource_size(p->res) == size)
498 /* To change size the old size must be freed first */
502 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE)
505 /* ways, granularity and uuid (if PMEM) need to be set before HPA */
506 if (!p->interleave_ways || !p->interleave_granularity ||
507 (cxlr->mode == CXL_DECODER_PMEM && uuid_is_null(&p->uuid)))
510 div_u64_rem(size, SZ_256M * p->interleave_ways, &remainder);
514 res = alloc_free_mem_region(cxlrd->res, size, SZ_256M,
515 dev_name(&cxlr->dev));
517 dev_dbg(&cxlr->dev, "failed to allocate HPA: %ld\n",
523 p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
528 static void cxl_region_iomem_release(struct cxl_region *cxlr)
530 struct cxl_region_params *p = &cxlr->params;
532 if (device_is_registered(&cxlr->dev))
533 lockdep_assert_held_write(&cxl_region_rwsem);
536 * Autodiscovered regions may not have been able to insert their
540 remove_resource(p->res);
546 static int free_hpa(struct cxl_region *cxlr)
548 struct cxl_region_params *p = &cxlr->params;
550 lockdep_assert_held_write(&cxl_region_rwsem);
555 if (p->state >= CXL_CONFIG_ACTIVE)
558 cxl_region_iomem_release(cxlr);
559 p->state = CXL_CONFIG_IDLE;
563 static ssize_t size_store(struct device *dev, struct device_attribute *attr,
564 const char *buf, size_t len)
566 struct cxl_region *cxlr = to_cxl_region(dev);
570 rc = kstrtou64(buf, 0, &val);
574 rc = down_write_killable(&cxl_region_rwsem);
579 rc = alloc_hpa(cxlr, val);
582 up_write(&cxl_region_rwsem);
590 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
593 struct cxl_region *cxlr = to_cxl_region(dev);
594 struct cxl_region_params *p = &cxlr->params;
598 rc = down_read_interruptible(&cxl_region_rwsem);
602 size = resource_size(p->res);
603 rc = sysfs_emit(buf, "%#llx\n", size);
604 up_read(&cxl_region_rwsem);
608 static DEVICE_ATTR_RW(size);
610 static struct attribute *cxl_region_attrs[] = {
612 &dev_attr_commit.attr,
613 &dev_attr_interleave_ways.attr,
614 &dev_attr_interleave_granularity.attr,
615 &dev_attr_resource.attr,
621 static const struct attribute_group cxl_region_group = {
622 .attrs = cxl_region_attrs,
623 .is_visible = cxl_region_visible,
626 static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos)
628 struct cxl_region_params *p = &cxlr->params;
629 struct cxl_endpoint_decoder *cxled;
632 rc = down_read_interruptible(&cxl_region_rwsem);
636 if (pos >= p->interleave_ways) {
637 dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
643 cxled = p->targets[pos];
645 rc = sysfs_emit(buf, "\n");
647 rc = sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev));
649 up_read(&cxl_region_rwsem);
654 static int match_free_decoder(struct device *dev, void *data)
656 struct cxl_decoder *cxld;
659 if (!is_switch_decoder(dev))
662 cxld = to_cxl_decoder(dev);
664 /* enforce ordered allocation */
676 static struct cxl_decoder *cxl_region_find_decoder(struct cxl_port *port,
677 struct cxl_region *cxlr)
682 dev = device_find_child(&port->dev, &id, match_free_decoder);
686 * This decoder is pinned registered as long as the endpoint decoder is
687 * registered, and endpoint decoder unregistration holds the
688 * cxl_region_rwsem over unregister events, so no need to hold on to
689 * this extra reference.
692 return to_cxl_decoder(dev);
695 static struct cxl_region_ref *alloc_region_ref(struct cxl_port *port,
696 struct cxl_region *cxlr)
698 struct cxl_region_params *p = &cxlr->params;
699 struct cxl_region_ref *cxl_rr, *iter;
703 xa_for_each(&port->regions, index, iter) {
704 struct cxl_region_params *ip = &iter->region->params;
709 if (ip->res->start > p->res->start) {
711 "%s: HPA order violation %s:%pr vs %pr\n",
712 dev_name(&port->dev),
713 dev_name(&iter->region->dev), ip->res, p->res);
714 return ERR_PTR(-EBUSY);
718 cxl_rr = kzalloc(sizeof(*cxl_rr), GFP_KERNEL);
720 return ERR_PTR(-ENOMEM);
722 cxl_rr->region = cxlr;
723 cxl_rr->nr_targets = 1;
724 xa_init(&cxl_rr->endpoints);
726 rc = xa_insert(&port->regions, (unsigned long)cxlr, cxl_rr, GFP_KERNEL);
729 "%s: failed to track region reference: %d\n",
730 dev_name(&port->dev), rc);
738 static void cxl_rr_free_decoder(struct cxl_region_ref *cxl_rr)
740 struct cxl_region *cxlr = cxl_rr->region;
741 struct cxl_decoder *cxld = cxl_rr->decoder;
746 dev_WARN_ONCE(&cxlr->dev, cxld->region != cxlr, "region mismatch\n");
747 if (cxld->region == cxlr) {
749 put_device(&cxlr->dev);
753 static void free_region_ref(struct cxl_region_ref *cxl_rr)
755 struct cxl_port *port = cxl_rr->port;
756 struct cxl_region *cxlr = cxl_rr->region;
758 cxl_rr_free_decoder(cxl_rr);
759 xa_erase(&port->regions, (unsigned long)cxlr);
760 xa_destroy(&cxl_rr->endpoints);
764 static int cxl_rr_ep_add(struct cxl_region_ref *cxl_rr,
765 struct cxl_endpoint_decoder *cxled)
768 struct cxl_port *port = cxl_rr->port;
769 struct cxl_region *cxlr = cxl_rr->region;
770 struct cxl_decoder *cxld = cxl_rr->decoder;
771 struct cxl_ep *ep = cxl_ep_load(port, cxled_to_memdev(cxled));
774 rc = xa_insert(&cxl_rr->endpoints, (unsigned long)cxled, ep,
783 get_device(&cxlr->dev);
789 static int cxl_rr_alloc_decoder(struct cxl_port *port, struct cxl_region *cxlr,
790 struct cxl_endpoint_decoder *cxled,
791 struct cxl_region_ref *cxl_rr)
793 struct cxl_decoder *cxld;
795 if (port == cxled_to_port(cxled))
798 cxld = cxl_region_find_decoder(port, cxlr);
800 dev_dbg(&cxlr->dev, "%s: no decoder available\n",
801 dev_name(&port->dev));
806 dev_dbg(&cxlr->dev, "%s: %s already attached to %s\n",
807 dev_name(&port->dev), dev_name(&cxld->dev),
808 dev_name(&cxld->region->dev));
812 cxl_rr->decoder = cxld;
817 * cxl_port_attach_region() - track a region's interest in a port by endpoint
818 * @port: port to add a new region reference 'struct cxl_region_ref'
819 * @cxlr: region to attach to @port
820 * @cxled: endpoint decoder used to create or further pin a region reference
821 * @pos: interleave position of @cxled in @cxlr
823 * The attach event is an opportunity to validate CXL decode setup
824 * constraints and record metadata needed for programming HDM decoders,
825 * in particular decoder target lists.
829 * - validate that there are no other regions with a higher HPA already
830 * associated with @port
831 * - establish a region reference if one is not already present
833 * - additionally allocate a decoder instance that will host @cxlr on
836 * - pin the region reference by the endpoint
837 * - account for how many entries in @port's target list are needed to
838 * cover all of the added endpoints.
840 static int cxl_port_attach_region(struct cxl_port *port,
841 struct cxl_region *cxlr,
842 struct cxl_endpoint_decoder *cxled, int pos)
844 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
845 struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
846 struct cxl_region_ref *cxl_rr;
847 bool nr_targets_inc = false;
848 struct cxl_decoder *cxld;
852 lockdep_assert_held_write(&cxl_region_rwsem);
854 cxl_rr = cxl_rr_load(port, cxlr);
856 struct cxl_ep *ep_iter;
860 * Walk the existing endpoints that have been attached to
861 * @cxlr at @port and see if they share the same 'next' port
862 * in the downstream direction. I.e. endpoints that share common
865 xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
868 if (ep_iter->next == ep->next) {
875 * New target port, or @port is an endpoint port that always
876 * accounts its own local decode as a target.
878 if (!found || !ep->next) {
879 cxl_rr->nr_targets++;
880 nr_targets_inc = true;
883 cxl_rr = alloc_region_ref(port, cxlr);
884 if (IS_ERR(cxl_rr)) {
886 "%s: failed to allocate region reference\n",
887 dev_name(&port->dev));
888 return PTR_ERR(cxl_rr);
890 nr_targets_inc = true;
892 rc = cxl_rr_alloc_decoder(port, cxlr, cxled, cxl_rr);
896 cxld = cxl_rr->decoder;
898 rc = cxl_rr_ep_add(cxl_rr, cxled);
901 "%s: failed to track endpoint %s:%s reference\n",
902 dev_name(&port->dev), dev_name(&cxlmd->dev),
903 dev_name(&cxld->dev));
908 "%s:%s %s add: %s:%s @ %d next: %s nr_eps: %d nr_targets: %d\n",
909 dev_name(port->uport), dev_name(&port->dev),
910 dev_name(&cxld->dev), dev_name(&cxlmd->dev),
911 dev_name(&cxled->cxld.dev), pos,
912 ep ? ep->next ? dev_name(ep->next->uport) :
913 dev_name(&cxlmd->dev) :
915 cxl_rr->nr_eps, cxl_rr->nr_targets);
920 cxl_rr->nr_targets--;
921 if (cxl_rr->nr_eps == 0)
922 free_region_ref(cxl_rr);
926 static void cxl_port_detach_region(struct cxl_port *port,
927 struct cxl_region *cxlr,
928 struct cxl_endpoint_decoder *cxled)
930 struct cxl_region_ref *cxl_rr;
931 struct cxl_ep *ep = NULL;
933 lockdep_assert_held_write(&cxl_region_rwsem);
935 cxl_rr = cxl_rr_load(port, cxlr);
940 * Endpoint ports do not carry cxl_ep references, and they
941 * never target more than one endpoint by definition
943 if (cxl_rr->decoder == &cxled->cxld)
946 ep = xa_erase(&cxl_rr->endpoints, (unsigned long)cxled);
948 struct cxl_ep *ep_iter;
953 xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
954 if (ep_iter->next == ep->next) {
960 cxl_rr->nr_targets--;
963 if (cxl_rr->nr_eps == 0)
964 free_region_ref(cxl_rr);
967 static int check_last_peer(struct cxl_endpoint_decoder *cxled,
968 struct cxl_ep *ep, struct cxl_region_ref *cxl_rr,
971 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
972 struct cxl_region *cxlr = cxl_rr->region;
973 struct cxl_region_params *p = &cxlr->params;
974 struct cxl_endpoint_decoder *cxled_peer;
975 struct cxl_port *port = cxl_rr->port;
976 struct cxl_memdev *cxlmd_peer;
977 struct cxl_ep *ep_peer;
978 int pos = cxled->pos;
981 * If this position wants to share a dport with the last endpoint mapped
982 * then that endpoint, at index 'position - distance', must also be
983 * mapped by this dport.
985 if (pos < distance) {
986 dev_dbg(&cxlr->dev, "%s:%s: cannot host %s:%s at %d\n",
987 dev_name(port->uport), dev_name(&port->dev),
988 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
991 cxled_peer = p->targets[pos - distance];
992 cxlmd_peer = cxled_to_memdev(cxled_peer);
993 ep_peer = cxl_ep_load(port, cxlmd_peer);
994 if (ep->dport != ep_peer->dport) {
996 "%s:%s: %s:%s pos %d mismatched peer %s:%s\n",
997 dev_name(port->uport), dev_name(&port->dev),
998 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos,
999 dev_name(&cxlmd_peer->dev),
1000 dev_name(&cxled_peer->cxld.dev));
1007 static int cxl_port_setup_targets(struct cxl_port *port,
1008 struct cxl_region *cxlr,
1009 struct cxl_endpoint_decoder *cxled)
1011 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
1012 int parent_iw, parent_ig, ig, iw, rc, inc = 0, pos = cxled->pos;
1013 struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
1014 struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
1015 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1016 struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
1017 struct cxl_region_params *p = &cxlr->params;
1018 struct cxl_decoder *cxld = cxl_rr->decoder;
1019 struct cxl_switch_decoder *cxlsd;
1024 * While root level decoders support x3, x6, x12, switch level
1025 * decoders only support powers of 2 up to x16.
1027 if (!is_power_of_2(cxl_rr->nr_targets)) {
1028 dev_dbg(&cxlr->dev, "%s:%s: invalid target count %d\n",
1029 dev_name(port->uport), dev_name(&port->dev),
1030 cxl_rr->nr_targets);
1034 cxlsd = to_cxl_switch_decoder(&cxld->dev);
1035 if (cxl_rr->nr_targets_set) {
1039 * Passthrough decoders impose no distance requirements between
1042 if (cxl_rr->nr_targets == 1)
1045 distance = p->nr_targets / cxl_rr->nr_targets;
1046 for (i = 0; i < cxl_rr->nr_targets_set; i++)
1047 if (ep->dport == cxlsd->target[i]) {
1048 rc = check_last_peer(cxled, ep, cxl_rr,
1052 goto out_target_set;
1057 if (is_cxl_root(parent_port)) {
1058 parent_ig = cxlrd->cxlsd.cxld.interleave_granularity;
1059 parent_iw = cxlrd->cxlsd.cxld.interleave_ways;
1061 * For purposes of address bit routing, use power-of-2 math for
1064 if (!is_power_of_2(parent_iw))
1067 struct cxl_region_ref *parent_rr;
1068 struct cxl_decoder *parent_cxld;
1070 parent_rr = cxl_rr_load(parent_port, cxlr);
1071 parent_cxld = parent_rr->decoder;
1072 parent_ig = parent_cxld->interleave_granularity;
1073 parent_iw = parent_cxld->interleave_ways;
1076 rc = granularity_to_eig(parent_ig, &peig);
1078 dev_dbg(&cxlr->dev, "%s:%s: invalid parent granularity: %d\n",
1079 dev_name(parent_port->uport),
1080 dev_name(&parent_port->dev), parent_ig);
1084 rc = ways_to_eiw(parent_iw, &peiw);
1086 dev_dbg(&cxlr->dev, "%s:%s: invalid parent interleave: %d\n",
1087 dev_name(parent_port->uport),
1088 dev_name(&parent_port->dev), parent_iw);
1092 iw = cxl_rr->nr_targets;
1093 rc = ways_to_eiw(iw, &eiw);
1095 dev_dbg(&cxlr->dev, "%s:%s: invalid port interleave: %d\n",
1096 dev_name(port->uport), dev_name(&port->dev), iw);
1101 * If @parent_port is masking address bits, pick the next unused address
1102 * bit to route @port's targets.
1104 if (parent_iw > 1 && cxl_rr->nr_targets > 1) {
1105 u32 address_bit = max(peig + peiw, eiw + peig);
1107 eig = address_bit - eiw + 1;
1113 rc = eig_to_granularity(eig, &ig);
1115 dev_dbg(&cxlr->dev, "%s:%s: invalid interleave: %d\n",
1116 dev_name(port->uport), dev_name(&port->dev),
1121 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1122 if (cxld->interleave_ways != iw ||
1123 cxld->interleave_granularity != ig ||
1124 cxld->hpa_range.start != p->res->start ||
1125 cxld->hpa_range.end != p->res->end ||
1126 ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) {
1128 "%s:%s %s expected iw: %d ig: %d %pr\n",
1129 dev_name(port->uport), dev_name(&port->dev),
1130 __func__, iw, ig, p->res);
1132 "%s:%s %s got iw: %d ig: %d state: %s %#llx:%#llx\n",
1133 dev_name(port->uport), dev_name(&port->dev),
1134 __func__, cxld->interleave_ways,
1135 cxld->interleave_granularity,
1136 (cxld->flags & CXL_DECODER_F_ENABLE) ?
1139 cxld->hpa_range.start, cxld->hpa_range.end);
1143 cxld->interleave_ways = iw;
1144 cxld->interleave_granularity = ig;
1145 cxld->hpa_range = (struct range) {
1146 .start = p->res->start,
1150 dev_dbg(&cxlr->dev, "%s:%s iw: %d ig: %d\n", dev_name(port->uport),
1151 dev_name(&port->dev), iw, ig);
1153 if (cxl_rr->nr_targets_set == cxl_rr->nr_targets) {
1155 "%s:%s: targets full trying to add %s:%s at %d\n",
1156 dev_name(port->uport), dev_name(&port->dev),
1157 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1160 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1161 if (cxlsd->target[cxl_rr->nr_targets_set] != ep->dport) {
1162 dev_dbg(&cxlr->dev, "%s:%s: %s expected %s at %d\n",
1163 dev_name(port->uport), dev_name(&port->dev),
1164 dev_name(&cxlsd->cxld.dev),
1165 dev_name(ep->dport->dport),
1166 cxl_rr->nr_targets_set);
1170 cxlsd->target[cxl_rr->nr_targets_set] = ep->dport;
1173 cxl_rr->nr_targets_set += inc;
1174 dev_dbg(&cxlr->dev, "%s:%s target[%d] = %s for %s:%s @ %d\n",
1175 dev_name(port->uport), dev_name(&port->dev),
1176 cxl_rr->nr_targets_set - 1, dev_name(ep->dport->dport),
1177 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1182 static void cxl_port_reset_targets(struct cxl_port *port,
1183 struct cxl_region *cxlr)
1185 struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
1186 struct cxl_decoder *cxld;
1189 * After the last endpoint has been detached the entire cxl_rr may now
1194 cxl_rr->nr_targets_set = 0;
1196 cxld = cxl_rr->decoder;
1197 cxld->hpa_range = (struct range) {
1203 static void cxl_region_teardown_targets(struct cxl_region *cxlr)
1205 struct cxl_region_params *p = &cxlr->params;
1206 struct cxl_endpoint_decoder *cxled;
1207 struct cxl_dev_state *cxlds;
1208 struct cxl_memdev *cxlmd;
1209 struct cxl_port *iter;
1214 * In the auto-discovery case skip automatic teardown since the
1215 * address space is already active
1217 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags))
1220 for (i = 0; i < p->nr_targets; i++) {
1221 cxled = p->targets[i];
1222 cxlmd = cxled_to_memdev(cxled);
1223 cxlds = cxlmd->cxlds;
1228 iter = cxled_to_port(cxled);
1229 while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
1230 iter = to_cxl_port(iter->dev.parent);
1232 for (ep = cxl_ep_load(iter, cxlmd); iter;
1233 iter = ep->next, ep = cxl_ep_load(iter, cxlmd))
1234 cxl_port_reset_targets(iter, cxlr);
1238 static int cxl_region_setup_targets(struct cxl_region *cxlr)
1240 struct cxl_region_params *p = &cxlr->params;
1241 struct cxl_endpoint_decoder *cxled;
1242 struct cxl_dev_state *cxlds;
1243 int i, rc, rch = 0, vh = 0;
1244 struct cxl_memdev *cxlmd;
1245 struct cxl_port *iter;
1248 for (i = 0; i < p->nr_targets; i++) {
1249 cxled = p->targets[i];
1250 cxlmd = cxled_to_memdev(cxled);
1251 cxlds = cxlmd->cxlds;
1253 /* validate that all targets agree on topology */
1261 iter = cxled_to_port(cxled);
1262 while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
1263 iter = to_cxl_port(iter->dev.parent);
1266 * Descend the topology tree programming / validating
1267 * targets while looking for conflicts.
1269 for (ep = cxl_ep_load(iter, cxlmd); iter;
1270 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
1271 rc = cxl_port_setup_targets(iter, cxlr, cxled);
1273 cxl_region_teardown_targets(cxlr);
1280 dev_err(&cxlr->dev, "mismatched CXL topologies detected\n");
1281 cxl_region_teardown_targets(cxlr);
1288 static int cxl_region_validate_position(struct cxl_region *cxlr,
1289 struct cxl_endpoint_decoder *cxled,
1292 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1293 struct cxl_region_params *p = &cxlr->params;
1296 if (pos < 0 || pos >= p->interleave_ways) {
1297 dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
1298 p->interleave_ways);
1302 if (p->targets[pos] == cxled)
1305 if (p->targets[pos]) {
1306 struct cxl_endpoint_decoder *cxled_target = p->targets[pos];
1307 struct cxl_memdev *cxlmd_target = cxled_to_memdev(cxled_target);
1309 dev_dbg(&cxlr->dev, "position %d already assigned to %s:%s\n",
1310 pos, dev_name(&cxlmd_target->dev),
1311 dev_name(&cxled_target->cxld.dev));
1315 for (i = 0; i < p->interleave_ways; i++) {
1316 struct cxl_endpoint_decoder *cxled_target;
1317 struct cxl_memdev *cxlmd_target;
1319 cxled_target = p->targets[i];
1323 cxlmd_target = cxled_to_memdev(cxled_target);
1324 if (cxlmd_target == cxlmd) {
1326 "%s already specified at position %d via: %s\n",
1327 dev_name(&cxlmd->dev), pos,
1328 dev_name(&cxled_target->cxld.dev));
1336 static int cxl_region_attach_position(struct cxl_region *cxlr,
1337 struct cxl_root_decoder *cxlrd,
1338 struct cxl_endpoint_decoder *cxled,
1339 const struct cxl_dport *dport, int pos)
1341 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1342 struct cxl_port *iter;
1345 if (cxlrd->calc_hb(cxlrd, pos) != dport) {
1346 dev_dbg(&cxlr->dev, "%s:%s invalid target position for %s\n",
1347 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1348 dev_name(&cxlrd->cxlsd.cxld.dev));
1352 for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
1353 iter = to_cxl_port(iter->dev.parent)) {
1354 rc = cxl_port_attach_region(iter, cxlr, cxled, pos);
1362 for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
1363 iter = to_cxl_port(iter->dev.parent))
1364 cxl_port_detach_region(iter, cxlr, cxled);
1368 static int cxl_region_attach_auto(struct cxl_region *cxlr,
1369 struct cxl_endpoint_decoder *cxled, int pos)
1371 struct cxl_region_params *p = &cxlr->params;
1373 if (cxled->state != CXL_DECODER_STATE_AUTO) {
1375 "%s: unable to add decoder to autodetected region\n",
1376 dev_name(&cxled->cxld.dev));
1381 dev_dbg(&cxlr->dev, "%s: expected auto position, not %d\n",
1382 dev_name(&cxled->cxld.dev), pos);
1386 if (p->nr_targets >= p->interleave_ways) {
1387 dev_err(&cxlr->dev, "%s: no more target slots available\n",
1388 dev_name(&cxled->cxld.dev));
1393 * Temporarily record the endpoint decoder into the target array. Yes,
1394 * this means that userspace can view devices in the wrong position
1395 * before the region activates, and must be careful to understand when
1396 * it might be racing region autodiscovery.
1398 pos = p->nr_targets;
1399 p->targets[pos] = cxled;
1406 static struct cxl_port *next_port(struct cxl_port *port)
1408 if (!port->parent_dport)
1410 return port->parent_dport->port;
1413 static int decoder_match_range(struct device *dev, void *data)
1415 struct cxl_endpoint_decoder *cxled = data;
1416 struct cxl_switch_decoder *cxlsd;
1418 if (!is_switch_decoder(dev))
1421 cxlsd = to_cxl_switch_decoder(dev);
1422 return range_contains(&cxlsd->cxld.hpa_range, &cxled->cxld.hpa_range);
1425 static void find_positions(const struct cxl_switch_decoder *cxlsd,
1426 const struct cxl_port *iter_a,
1427 const struct cxl_port *iter_b, int *a_pos,
1432 for (i = 0, *a_pos = -1, *b_pos = -1; i < cxlsd->nr_targets; i++) {
1433 if (cxlsd->target[i] == iter_a->parent_dport)
1435 else if (cxlsd->target[i] == iter_b->parent_dport)
1437 if (*a_pos >= 0 && *b_pos >= 0)
1442 static int cmp_decode_pos(const void *a, const void *b)
1444 struct cxl_endpoint_decoder *cxled_a = *(typeof(cxled_a) *)a;
1445 struct cxl_endpoint_decoder *cxled_b = *(typeof(cxled_b) *)b;
1446 struct cxl_memdev *cxlmd_a = cxled_to_memdev(cxled_a);
1447 struct cxl_memdev *cxlmd_b = cxled_to_memdev(cxled_b);
1448 struct cxl_port *port_a = cxled_to_port(cxled_a);
1449 struct cxl_port *port_b = cxled_to_port(cxled_b);
1450 struct cxl_port *iter_a, *iter_b, *port = NULL;
1451 struct cxl_switch_decoder *cxlsd;
1456 /* Exit early if any prior sorting failed */
1457 if (cxled_a->pos < 0 || cxled_b->pos < 0)
1461 * Walk up the hierarchy to find a shared port, find the decoder that
1462 * maps the range, compare the relative position of those dport
1465 for (iter_a = port_a; iter_a; iter_a = next_port(iter_a)) {
1466 struct cxl_port *next_a, *next_b;
1468 next_a = next_port(iter_a);
1472 for (iter_b = port_b; iter_b; iter_b = next_port(iter_b)) {
1473 next_b = next_port(iter_b);
1474 if (next_a != next_b)
1485 dev_err(cxlmd_a->dev.parent,
1486 "failed to find shared port with %s\n",
1487 dev_name(cxlmd_b->dev.parent));
1491 dev = device_find_child(&port->dev, cxled_a, decoder_match_range);
1493 struct range *range = &cxled_a->cxld.hpa_range;
1495 dev_err(port->uport,
1496 "failed to find decoder that maps %#llx-%#llx\n",
1497 range->start, range->end);
1501 cxlsd = to_cxl_switch_decoder(dev);
1503 seq = read_seqbegin(&cxlsd->target_lock);
1504 find_positions(cxlsd, iter_a, iter_b, &a_pos, &b_pos);
1505 } while (read_seqretry(&cxlsd->target_lock, seq));
1509 if (a_pos < 0 || b_pos < 0) {
1510 dev_err(port->uport,
1511 "failed to find shared decoder for %s and %s\n",
1512 dev_name(cxlmd_a->dev.parent),
1513 dev_name(cxlmd_b->dev.parent));
1517 dev_dbg(port->uport, "%s comes %s %s\n", dev_name(cxlmd_a->dev.parent),
1518 a_pos - b_pos < 0 ? "before" : "after",
1519 dev_name(cxlmd_b->dev.parent));
1521 return a_pos - b_pos;
1527 static int cxl_region_sort_targets(struct cxl_region *cxlr)
1529 struct cxl_region_params *p = &cxlr->params;
1532 sort(p->targets, p->nr_targets, sizeof(p->targets[0]), cmp_decode_pos,
1535 for (i = 0; i < p->nr_targets; i++) {
1536 struct cxl_endpoint_decoder *cxled = p->targets[i];
1539 * Record that sorting failed, but still continue to restore
1540 * cxled->pos with its ->targets[] position so that follow-on
1541 * code paths can reliably do p->targets[cxled->pos] to
1542 * self-reference their entry.
1549 dev_dbg(&cxlr->dev, "region sort %s\n", rc ? "failed" : "successful");
1553 static int cxl_region_attach(struct cxl_region *cxlr,
1554 struct cxl_endpoint_decoder *cxled, int pos)
1556 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
1557 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1558 struct cxl_region_params *p = &cxlr->params;
1559 struct cxl_port *ep_port, *root_port;
1560 struct cxl_dport *dport;
1563 if (cxled->mode != cxlr->mode) {
1564 dev_dbg(&cxlr->dev, "%s region mode: %d mismatch: %d\n",
1565 dev_name(&cxled->cxld.dev), cxlr->mode, cxled->mode);
1569 if (cxled->mode == CXL_DECODER_DEAD) {
1570 dev_dbg(&cxlr->dev, "%s dead\n", dev_name(&cxled->cxld.dev));
1574 /* all full of members, or interleave config not established? */
1575 if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) {
1576 dev_dbg(&cxlr->dev, "region already active\n");
1578 } else if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
1579 dev_dbg(&cxlr->dev, "interleave config missing\n");
1583 ep_port = cxled_to_port(cxled);
1584 root_port = cxlrd_to_port(cxlrd);
1585 dport = cxl_find_dport_by_dev(root_port, ep_port->host_bridge);
1587 dev_dbg(&cxlr->dev, "%s:%s invalid target for %s\n",
1588 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1589 dev_name(cxlr->dev.parent));
1593 if (cxled->cxld.target_type != cxlr->type) {
1594 dev_dbg(&cxlr->dev, "%s:%s type mismatch: %d vs %d\n",
1595 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1596 cxled->cxld.target_type, cxlr->type);
1600 if (!cxled->dpa_res) {
1601 dev_dbg(&cxlr->dev, "%s:%s: missing DPA allocation.\n",
1602 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev));
1606 if (resource_size(cxled->dpa_res) * p->interleave_ways !=
1607 resource_size(p->res)) {
1609 "%s:%s: decoder-size-%#llx * ways-%d != region-size-%#llx\n",
1610 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1611 (u64)resource_size(cxled->dpa_res), p->interleave_ways,
1612 (u64)resource_size(p->res));
1616 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1619 rc = cxl_region_attach_auto(cxlr, cxled, pos);
1623 /* await more targets to arrive... */
1624 if (p->nr_targets < p->interleave_ways)
1628 * All targets are here, which implies all PCI enumeration that
1629 * affects this region has been completed. Walk the topology to
1630 * sort the devices into their relative region decode position.
1632 rc = cxl_region_sort_targets(cxlr);
1636 for (i = 0; i < p->nr_targets; i++) {
1637 cxled = p->targets[i];
1638 ep_port = cxled_to_port(cxled);
1639 dport = cxl_find_dport_by_dev(root_port,
1640 ep_port->host_bridge);
1641 rc = cxl_region_attach_position(cxlr, cxlrd, cxled,
1647 rc = cxl_region_setup_targets(cxlr);
1652 * If target setup succeeds in the autodiscovery case
1653 * then the region is already committed.
1655 p->state = CXL_CONFIG_COMMIT;
1660 rc = cxl_region_validate_position(cxlr, cxled, pos);
1664 rc = cxl_region_attach_position(cxlr, cxlrd, cxled, dport, pos);
1668 p->targets[pos] = cxled;
1672 if (p->nr_targets == p->interleave_ways) {
1673 rc = cxl_region_setup_targets(cxlr);
1676 p->state = CXL_CONFIG_ACTIVE;
1677 set_bit(CXL_REGION_F_INCOHERENT, &cxlr->flags);
1680 cxled->cxld.interleave_ways = p->interleave_ways;
1681 cxled->cxld.interleave_granularity = p->interleave_granularity;
1682 cxled->cxld.hpa_range = (struct range) {
1683 .start = p->res->start,
1692 p->targets[pos] = NULL;
1696 static int cxl_region_detach(struct cxl_endpoint_decoder *cxled)
1698 struct cxl_port *iter, *ep_port = cxled_to_port(cxled);
1699 struct cxl_region *cxlr = cxled->cxld.region;
1700 struct cxl_region_params *p;
1703 lockdep_assert_held_write(&cxl_region_rwsem);
1709 get_device(&cxlr->dev);
1711 if (p->state > CXL_CONFIG_ACTIVE) {
1713 * TODO: tear down all impacted regions if a device is
1714 * removed out of order
1716 rc = cxl_region_decode_reset(cxlr, p->interleave_ways);
1719 p->state = CXL_CONFIG_ACTIVE;
1722 for (iter = ep_port; !is_cxl_root(iter);
1723 iter = to_cxl_port(iter->dev.parent))
1724 cxl_port_detach_region(iter, cxlr, cxled);
1726 if (cxled->pos < 0 || cxled->pos >= p->interleave_ways ||
1727 p->targets[cxled->pos] != cxled) {
1728 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1730 dev_WARN_ONCE(&cxlr->dev, 1, "expected %s:%s at position %d\n",
1731 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1736 if (p->state == CXL_CONFIG_ACTIVE) {
1737 p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
1738 cxl_region_teardown_targets(cxlr);
1740 p->targets[cxled->pos] = NULL;
1742 cxled->cxld.hpa_range = (struct range) {
1747 /* notify the region driver that one of its targets has departed */
1748 up_write(&cxl_region_rwsem);
1749 device_release_driver(&cxlr->dev);
1750 down_write(&cxl_region_rwsem);
1752 put_device(&cxlr->dev);
1756 void cxl_decoder_kill_region(struct cxl_endpoint_decoder *cxled)
1758 down_write(&cxl_region_rwsem);
1759 cxled->mode = CXL_DECODER_DEAD;
1760 cxl_region_detach(cxled);
1761 up_write(&cxl_region_rwsem);
1764 static int attach_target(struct cxl_region *cxlr,
1765 struct cxl_endpoint_decoder *cxled, int pos,
1770 if (state == TASK_INTERRUPTIBLE)
1771 rc = down_write_killable(&cxl_region_rwsem);
1773 down_write(&cxl_region_rwsem);
1777 down_read(&cxl_dpa_rwsem);
1778 rc = cxl_region_attach(cxlr, cxled, pos);
1779 up_read(&cxl_dpa_rwsem);
1780 up_write(&cxl_region_rwsem);
1784 static int detach_target(struct cxl_region *cxlr, int pos)
1786 struct cxl_region_params *p = &cxlr->params;
1789 rc = down_write_killable(&cxl_region_rwsem);
1793 if (pos >= p->interleave_ways) {
1794 dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
1795 p->interleave_ways);
1800 if (!p->targets[pos]) {
1805 rc = cxl_region_detach(p->targets[pos]);
1807 up_write(&cxl_region_rwsem);
1811 static size_t store_targetN(struct cxl_region *cxlr, const char *buf, int pos,
1816 if (sysfs_streq(buf, "\n"))
1817 rc = detach_target(cxlr, pos);
1821 dev = bus_find_device_by_name(&cxl_bus_type, NULL, buf);
1825 if (!is_endpoint_decoder(dev)) {
1830 rc = attach_target(cxlr, to_cxl_endpoint_decoder(dev), pos,
1831 TASK_INTERRUPTIBLE);
1841 #define TARGET_ATTR_RW(n) \
1842 static ssize_t target##n##_show( \
1843 struct device *dev, struct device_attribute *attr, char *buf) \
1845 return show_targetN(to_cxl_region(dev), buf, (n)); \
1847 static ssize_t target##n##_store(struct device *dev, \
1848 struct device_attribute *attr, \
1849 const char *buf, size_t len) \
1851 return store_targetN(to_cxl_region(dev), buf, (n), len); \
1853 static DEVICE_ATTR_RW(target##n)
1872 static struct attribute *target_attrs[] = {
1873 &dev_attr_target0.attr,
1874 &dev_attr_target1.attr,
1875 &dev_attr_target2.attr,
1876 &dev_attr_target3.attr,
1877 &dev_attr_target4.attr,
1878 &dev_attr_target5.attr,
1879 &dev_attr_target6.attr,
1880 &dev_attr_target7.attr,
1881 &dev_attr_target8.attr,
1882 &dev_attr_target9.attr,
1883 &dev_attr_target10.attr,
1884 &dev_attr_target11.attr,
1885 &dev_attr_target12.attr,
1886 &dev_attr_target13.attr,
1887 &dev_attr_target14.attr,
1888 &dev_attr_target15.attr,
1892 static umode_t cxl_region_target_visible(struct kobject *kobj,
1893 struct attribute *a, int n)
1895 struct device *dev = kobj_to_dev(kobj);
1896 struct cxl_region *cxlr = to_cxl_region(dev);
1897 struct cxl_region_params *p = &cxlr->params;
1899 if (n < p->interleave_ways)
1904 static const struct attribute_group cxl_region_target_group = {
1905 .attrs = target_attrs,
1906 .is_visible = cxl_region_target_visible,
1909 static const struct attribute_group *get_cxl_region_target_group(void)
1911 return &cxl_region_target_group;
1914 static const struct attribute_group *region_groups[] = {
1915 &cxl_base_attribute_group,
1917 &cxl_region_target_group,
1921 static void cxl_region_release(struct device *dev)
1923 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
1924 struct cxl_region *cxlr = to_cxl_region(dev);
1925 int id = atomic_read(&cxlrd->region_id);
1928 * Try to reuse the recently idled id rather than the cached
1929 * next id to prevent the region id space from increasing
1933 if (atomic_try_cmpxchg(&cxlrd->region_id, &id, cxlr->id)) {
1938 memregion_free(cxlr->id);
1940 put_device(dev->parent);
1944 const struct device_type cxl_region_type = {
1945 .name = "cxl_region",
1946 .release = cxl_region_release,
1947 .groups = region_groups
1950 bool is_cxl_region(struct device *dev)
1952 return dev->type == &cxl_region_type;
1954 EXPORT_SYMBOL_NS_GPL(is_cxl_region, CXL);
1956 static struct cxl_region *to_cxl_region(struct device *dev)
1958 if (dev_WARN_ONCE(dev, dev->type != &cxl_region_type,
1959 "not a cxl_region device\n"))
1962 return container_of(dev, struct cxl_region, dev);
1965 static void unregister_region(void *dev)
1967 struct cxl_region *cxlr = to_cxl_region(dev);
1968 struct cxl_region_params *p = &cxlr->params;
1974 * Now that region sysfs is shutdown, the parameter block is now
1975 * read-only, so no need to hold the region rwsem to access the
1976 * region parameters.
1978 for (i = 0; i < p->interleave_ways; i++)
1979 detach_target(cxlr, i);
1981 cxl_region_iomem_release(cxlr);
1985 static struct lock_class_key cxl_region_key;
1987 static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id)
1989 struct cxl_region *cxlr;
1992 cxlr = kzalloc(sizeof(*cxlr), GFP_KERNEL);
1995 return ERR_PTR(-ENOMEM);
1999 device_initialize(dev);
2000 lockdep_set_class(&dev->mutex, &cxl_region_key);
2001 dev->parent = &cxlrd->cxlsd.cxld.dev;
2003 * Keep root decoder pinned through cxl_region_release to fixup
2004 * region id allocations
2006 get_device(dev->parent);
2007 device_set_pm_not_required(dev);
2008 dev->bus = &cxl_bus_type;
2009 dev->type = &cxl_region_type;
2016 * devm_cxl_add_region - Adds a region to a decoder
2017 * @cxlrd: root decoder
2018 * @id: memregion id to create, or memregion_free() on failure
2019 * @mode: mode for the endpoint decoders of this region
2020 * @type: select whether this is an expander or accelerator (type-2 or type-3)
2022 * This is the second step of region initialization. Regions exist within an
2023 * address space which is mapped by a @cxlrd.
2025 * Return: 0 if the region was added to the @cxlrd, else returns negative error
2026 * code. The region will be named "regionZ" where Z is the unique region number.
2028 static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd,
2030 enum cxl_decoder_mode mode,
2031 enum cxl_decoder_type type)
2033 struct cxl_port *port = to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
2034 struct cxl_region *cxlr;
2039 case CXL_DECODER_RAM:
2040 case CXL_DECODER_PMEM:
2043 dev_err(&cxlrd->cxlsd.cxld.dev, "unsupported mode %d\n", mode);
2044 return ERR_PTR(-EINVAL);
2047 cxlr = cxl_region_alloc(cxlrd, id);
2054 rc = dev_set_name(dev, "region%d", id);
2058 rc = device_add(dev);
2062 rc = devm_add_action_or_reset(port->uport, unregister_region, cxlr);
2066 dev_dbg(port->uport, "%s: created %s\n",
2067 dev_name(&cxlrd->cxlsd.cxld.dev), dev_name(dev));
2075 static ssize_t __create_region_show(struct cxl_root_decoder *cxlrd, char *buf)
2077 return sysfs_emit(buf, "region%u\n", atomic_read(&cxlrd->region_id));
2080 static ssize_t create_pmem_region_show(struct device *dev,
2081 struct device_attribute *attr, char *buf)
2083 return __create_region_show(to_cxl_root_decoder(dev), buf);
2086 static ssize_t create_ram_region_show(struct device *dev,
2087 struct device_attribute *attr, char *buf)
2089 return __create_region_show(to_cxl_root_decoder(dev), buf);
2092 static struct cxl_region *__create_region(struct cxl_root_decoder *cxlrd,
2093 enum cxl_decoder_mode mode, int id)
2097 rc = memregion_alloc(GFP_KERNEL);
2101 if (atomic_cmpxchg(&cxlrd->region_id, id, rc) != id) {
2103 return ERR_PTR(-EBUSY);
2106 return devm_cxl_add_region(cxlrd, id, mode, CXL_DECODER_EXPANDER);
2109 static ssize_t create_pmem_region_store(struct device *dev,
2110 struct device_attribute *attr,
2111 const char *buf, size_t len)
2113 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2114 struct cxl_region *cxlr;
2117 rc = sscanf(buf, "region%d\n", &id);
2121 cxlr = __create_region(cxlrd, CXL_DECODER_PMEM, id);
2123 return PTR_ERR(cxlr);
2127 DEVICE_ATTR_RW(create_pmem_region);
2129 static ssize_t create_ram_region_store(struct device *dev,
2130 struct device_attribute *attr,
2131 const char *buf, size_t len)
2133 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2134 struct cxl_region *cxlr;
2137 rc = sscanf(buf, "region%d\n", &id);
2141 cxlr = __create_region(cxlrd, CXL_DECODER_RAM, id);
2143 return PTR_ERR(cxlr);
2147 DEVICE_ATTR_RW(create_ram_region);
2149 static ssize_t region_show(struct device *dev, struct device_attribute *attr,
2152 struct cxl_decoder *cxld = to_cxl_decoder(dev);
2155 rc = down_read_interruptible(&cxl_region_rwsem);
2160 rc = sysfs_emit(buf, "%s\n", dev_name(&cxld->region->dev));
2162 rc = sysfs_emit(buf, "\n");
2163 up_read(&cxl_region_rwsem);
2167 DEVICE_ATTR_RO(region);
2169 static struct cxl_region *
2170 cxl_find_region_by_name(struct cxl_root_decoder *cxlrd, const char *name)
2172 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
2173 struct device *region_dev;
2175 region_dev = device_find_child_by_name(&cxld->dev, name);
2177 return ERR_PTR(-ENODEV);
2179 return to_cxl_region(region_dev);
2182 static ssize_t delete_region_store(struct device *dev,
2183 struct device_attribute *attr,
2184 const char *buf, size_t len)
2186 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2187 struct cxl_port *port = to_cxl_port(dev->parent);
2188 struct cxl_region *cxlr;
2190 cxlr = cxl_find_region_by_name(cxlrd, buf);
2192 return PTR_ERR(cxlr);
2194 devm_release_action(port->uport, unregister_region, cxlr);
2195 put_device(&cxlr->dev);
2199 DEVICE_ATTR_WO(delete_region);
2201 static void cxl_pmem_region_release(struct device *dev)
2203 struct cxl_pmem_region *cxlr_pmem = to_cxl_pmem_region(dev);
2206 for (i = 0; i < cxlr_pmem->nr_mappings; i++) {
2207 struct cxl_memdev *cxlmd = cxlr_pmem->mapping[i].cxlmd;
2209 put_device(&cxlmd->dev);
2215 static const struct attribute_group *cxl_pmem_region_attribute_groups[] = {
2216 &cxl_base_attribute_group,
2220 const struct device_type cxl_pmem_region_type = {
2221 .name = "cxl_pmem_region",
2222 .release = cxl_pmem_region_release,
2223 .groups = cxl_pmem_region_attribute_groups,
2226 bool is_cxl_pmem_region(struct device *dev)
2228 return dev->type == &cxl_pmem_region_type;
2230 EXPORT_SYMBOL_NS_GPL(is_cxl_pmem_region, CXL);
2232 struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev)
2234 if (dev_WARN_ONCE(dev, !is_cxl_pmem_region(dev),
2235 "not a cxl_pmem_region device\n"))
2237 return container_of(dev, struct cxl_pmem_region, dev);
2239 EXPORT_SYMBOL_NS_GPL(to_cxl_pmem_region, CXL);
2241 struct cxl_poison_context {
2242 struct cxl_port *port;
2243 enum cxl_decoder_mode mode;
2247 static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
2248 struct cxl_poison_context *ctx)
2250 struct cxl_dev_state *cxlds = cxlmd->cxlds;
2255 * Collect poison for the remaining unmapped resources
2256 * after poison is collected by committed endpoints.
2258 * Knowing that PMEM must always follow RAM, get poison
2259 * for unmapped resources based on the last decoder's mode:
2260 * ram: scan remains of ram range, then any pmem range
2261 * pmem: scan remains of pmem range
2264 if (ctx->mode == CXL_DECODER_RAM) {
2265 offset = ctx->offset;
2266 length = resource_size(&cxlds->ram_res) - offset;
2267 rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
2273 if (ctx->mode == CXL_DECODER_PMEM) {
2274 offset = ctx->offset;
2275 length = resource_size(&cxlds->dpa_res) - offset;
2278 } else if (resource_size(&cxlds->pmem_res)) {
2279 offset = cxlds->pmem_res.start;
2280 length = resource_size(&cxlds->pmem_res);
2285 return cxl_mem_get_poison(cxlmd, offset, length, NULL);
2288 static int poison_by_decoder(struct device *dev, void *arg)
2290 struct cxl_poison_context *ctx = arg;
2291 struct cxl_endpoint_decoder *cxled;
2292 struct cxl_memdev *cxlmd;
2296 if (!is_endpoint_decoder(dev))
2299 cxled = to_cxl_endpoint_decoder(dev);
2300 if (!cxled->dpa_res || !resource_size(cxled->dpa_res))
2304 * Regions are only created with single mode decoders: pmem or ram.
2305 * Linux does not support mixed mode decoders. This means that
2306 * reading poison per endpoint decoder adheres to the requirement
2307 * that poison reads of pmem and ram must be separated.
2308 * CXL 3.0 Spec 8.2.9.8.4.1
2310 if (cxled->mode == CXL_DECODER_MIXED) {
2311 dev_dbg(dev, "poison list read unsupported in mixed mode\n");
2315 cxlmd = cxled_to_memdev(cxled);
2317 offset = cxled->dpa_res->start - cxled->skip;
2318 length = cxled->skip;
2319 rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
2320 if (rc == -EFAULT && cxled->mode == CXL_DECODER_RAM)
2326 offset = cxled->dpa_res->start;
2327 length = cxled->dpa_res->end - offset + 1;
2328 rc = cxl_mem_get_poison(cxlmd, offset, length, cxled->cxld.region);
2329 if (rc == -EFAULT && cxled->mode == CXL_DECODER_RAM)
2334 /* Iterate until commit_end is reached */
2335 if (cxled->cxld.id == ctx->port->commit_end) {
2336 ctx->offset = cxled->dpa_res->end + 1;
2337 ctx->mode = cxled->mode;
2344 int cxl_get_poison_by_endpoint(struct cxl_port *port)
2346 struct cxl_poison_context ctx;
2349 rc = down_read_interruptible(&cxl_region_rwsem);
2353 ctx = (struct cxl_poison_context) {
2357 rc = device_for_each_child(&port->dev, &ctx, poison_by_decoder);
2359 rc = cxl_get_poison_unmapped(to_cxl_memdev(port->uport), &ctx);
2361 up_read(&cxl_region_rwsem);
2365 static struct lock_class_key cxl_pmem_region_key;
2367 static struct cxl_pmem_region *cxl_pmem_region_alloc(struct cxl_region *cxlr)
2369 struct cxl_region_params *p = &cxlr->params;
2370 struct cxl_nvdimm_bridge *cxl_nvb;
2371 struct cxl_pmem_region *cxlr_pmem;
2375 down_read(&cxl_region_rwsem);
2376 if (p->state != CXL_CONFIG_COMMIT) {
2377 cxlr_pmem = ERR_PTR(-ENXIO);
2381 cxlr_pmem = kzalloc(struct_size(cxlr_pmem, mapping, p->nr_targets),
2384 cxlr_pmem = ERR_PTR(-ENOMEM);
2388 cxlr_pmem->hpa_range.start = p->res->start;
2389 cxlr_pmem->hpa_range.end = p->res->end;
2391 /* Snapshot the region configuration underneath the cxl_region_rwsem */
2392 cxlr_pmem->nr_mappings = p->nr_targets;
2393 for (i = 0; i < p->nr_targets; i++) {
2394 struct cxl_endpoint_decoder *cxled = p->targets[i];
2395 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
2396 struct cxl_pmem_region_mapping *m = &cxlr_pmem->mapping[i];
2399 * Regions never span CXL root devices, so by definition the
2400 * bridge for one device is the same for all.
2403 cxl_nvb = cxl_find_nvdimm_bridge(cxlmd);
2405 cxlr_pmem = ERR_PTR(-ENODEV);
2408 cxlr->cxl_nvb = cxl_nvb;
2411 get_device(&cxlmd->dev);
2412 m->start = cxled->dpa_res->start;
2413 m->size = resource_size(cxled->dpa_res);
2417 dev = &cxlr_pmem->dev;
2418 cxlr_pmem->cxlr = cxlr;
2419 cxlr->cxlr_pmem = cxlr_pmem;
2420 device_initialize(dev);
2421 lockdep_set_class(&dev->mutex, &cxl_pmem_region_key);
2422 device_set_pm_not_required(dev);
2423 dev->parent = &cxlr->dev;
2424 dev->bus = &cxl_bus_type;
2425 dev->type = &cxl_pmem_region_type;
2427 up_read(&cxl_region_rwsem);
2432 static void cxl_dax_region_release(struct device *dev)
2434 struct cxl_dax_region *cxlr_dax = to_cxl_dax_region(dev);
2439 static const struct attribute_group *cxl_dax_region_attribute_groups[] = {
2440 &cxl_base_attribute_group,
2444 const struct device_type cxl_dax_region_type = {
2445 .name = "cxl_dax_region",
2446 .release = cxl_dax_region_release,
2447 .groups = cxl_dax_region_attribute_groups,
2450 static bool is_cxl_dax_region(struct device *dev)
2452 return dev->type == &cxl_dax_region_type;
2455 struct cxl_dax_region *to_cxl_dax_region(struct device *dev)
2457 if (dev_WARN_ONCE(dev, !is_cxl_dax_region(dev),
2458 "not a cxl_dax_region device\n"))
2460 return container_of(dev, struct cxl_dax_region, dev);
2462 EXPORT_SYMBOL_NS_GPL(to_cxl_dax_region, CXL);
2464 static struct lock_class_key cxl_dax_region_key;
2466 static struct cxl_dax_region *cxl_dax_region_alloc(struct cxl_region *cxlr)
2468 struct cxl_region_params *p = &cxlr->params;
2469 struct cxl_dax_region *cxlr_dax;
2472 down_read(&cxl_region_rwsem);
2473 if (p->state != CXL_CONFIG_COMMIT) {
2474 cxlr_dax = ERR_PTR(-ENXIO);
2478 cxlr_dax = kzalloc(sizeof(*cxlr_dax), GFP_KERNEL);
2480 cxlr_dax = ERR_PTR(-ENOMEM);
2484 cxlr_dax->hpa_range.start = p->res->start;
2485 cxlr_dax->hpa_range.end = p->res->end;
2487 dev = &cxlr_dax->dev;
2488 cxlr_dax->cxlr = cxlr;
2489 device_initialize(dev);
2490 lockdep_set_class(&dev->mutex, &cxl_dax_region_key);
2491 device_set_pm_not_required(dev);
2492 dev->parent = &cxlr->dev;
2493 dev->bus = &cxl_bus_type;
2494 dev->type = &cxl_dax_region_type;
2496 up_read(&cxl_region_rwsem);
2501 static void cxlr_pmem_unregister(void *_cxlr_pmem)
2503 struct cxl_pmem_region *cxlr_pmem = _cxlr_pmem;
2504 struct cxl_region *cxlr = cxlr_pmem->cxlr;
2505 struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
2508 * Either the bridge is in ->remove() context under the device_lock(),
2509 * or cxlr_release_nvdimm() is cancelling the bridge's release action
2510 * for @cxlr_pmem and doing it itself (while manually holding the bridge
2513 device_lock_assert(&cxl_nvb->dev);
2514 cxlr->cxlr_pmem = NULL;
2515 cxlr_pmem->cxlr = NULL;
2516 device_unregister(&cxlr_pmem->dev);
2519 static void cxlr_release_nvdimm(void *_cxlr)
2521 struct cxl_region *cxlr = _cxlr;
2522 struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
2524 device_lock(&cxl_nvb->dev);
2525 if (cxlr->cxlr_pmem)
2526 devm_release_action(&cxl_nvb->dev, cxlr_pmem_unregister,
2528 device_unlock(&cxl_nvb->dev);
2529 cxlr->cxl_nvb = NULL;
2530 put_device(&cxl_nvb->dev);
2534 * devm_cxl_add_pmem_region() - add a cxl_region-to-nd_region bridge
2535 * @cxlr: parent CXL region for this pmem region bridge device
2537 * Return: 0 on success negative error code on failure.
2539 static int devm_cxl_add_pmem_region(struct cxl_region *cxlr)
2541 struct cxl_pmem_region *cxlr_pmem;
2542 struct cxl_nvdimm_bridge *cxl_nvb;
2546 cxlr_pmem = cxl_pmem_region_alloc(cxlr);
2547 if (IS_ERR(cxlr_pmem))
2548 return PTR_ERR(cxlr_pmem);
2549 cxl_nvb = cxlr->cxl_nvb;
2551 dev = &cxlr_pmem->dev;
2552 rc = dev_set_name(dev, "pmem_region%d", cxlr->id);
2556 rc = device_add(dev);
2560 dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
2563 device_lock(&cxl_nvb->dev);
2564 if (cxl_nvb->dev.driver)
2565 rc = devm_add_action_or_reset(&cxl_nvb->dev,
2566 cxlr_pmem_unregister, cxlr_pmem);
2569 device_unlock(&cxl_nvb->dev);
2574 /* @cxlr carries a reference on @cxl_nvb until cxlr_release_nvdimm */
2575 return devm_add_action_or_reset(&cxlr->dev, cxlr_release_nvdimm, cxlr);
2580 put_device(&cxl_nvb->dev);
2581 cxlr->cxl_nvb = NULL;
2585 static void cxlr_dax_unregister(void *_cxlr_dax)
2587 struct cxl_dax_region *cxlr_dax = _cxlr_dax;
2589 device_unregister(&cxlr_dax->dev);
2592 static int devm_cxl_add_dax_region(struct cxl_region *cxlr)
2594 struct cxl_dax_region *cxlr_dax;
2598 cxlr_dax = cxl_dax_region_alloc(cxlr);
2599 if (IS_ERR(cxlr_dax))
2600 return PTR_ERR(cxlr_dax);
2602 dev = &cxlr_dax->dev;
2603 rc = dev_set_name(dev, "dax_region%d", cxlr->id);
2607 rc = device_add(dev);
2611 dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
2614 return devm_add_action_or_reset(&cxlr->dev, cxlr_dax_unregister,
2621 static int match_decoder_by_range(struct device *dev, void *data)
2623 struct range *r1, *r2 = data;
2624 struct cxl_root_decoder *cxlrd;
2626 if (!is_root_decoder(dev))
2629 cxlrd = to_cxl_root_decoder(dev);
2630 r1 = &cxlrd->cxlsd.cxld.hpa_range;
2631 return range_contains(r1, r2);
2634 static int match_region_by_range(struct device *dev, void *data)
2636 struct cxl_region_params *p;
2637 struct cxl_region *cxlr;
2638 struct range *r = data;
2641 if (!is_cxl_region(dev))
2644 cxlr = to_cxl_region(dev);
2647 down_read(&cxl_region_rwsem);
2648 if (p->res && p->res->start == r->start && p->res->end == r->end)
2650 up_read(&cxl_region_rwsem);
2655 /* Establish an empty region covering the given HPA range */
2656 static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
2657 struct cxl_endpoint_decoder *cxled)
2659 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
2660 struct cxl_port *port = cxlrd_to_port(cxlrd);
2661 struct range *hpa = &cxled->cxld.hpa_range;
2662 struct cxl_region_params *p;
2663 struct cxl_region *cxlr;
2664 struct resource *res;
2668 cxlr = __create_region(cxlrd, cxled->mode,
2669 atomic_read(&cxlrd->region_id));
2670 } while (IS_ERR(cxlr) && PTR_ERR(cxlr) == -EBUSY);
2673 dev_err(cxlmd->dev.parent,
2674 "%s:%s: %s failed assign region: %ld\n",
2675 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2676 __func__, PTR_ERR(cxlr));
2680 down_write(&cxl_region_rwsem);
2682 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
2683 dev_err(cxlmd->dev.parent,
2684 "%s:%s: %s autodiscovery interrupted\n",
2685 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2691 set_bit(CXL_REGION_F_AUTO, &cxlr->flags);
2693 res = kmalloc(sizeof(*res), GFP_KERNEL);
2699 *res = DEFINE_RES_MEM_NAMED(hpa->start, range_len(hpa),
2700 dev_name(&cxlr->dev));
2701 rc = insert_resource(cxlrd->res, res);
2704 * Platform-firmware may not have split resources like "System
2705 * RAM" on CXL window boundaries see cxl_region_iomem_release()
2707 dev_warn(cxlmd->dev.parent,
2708 "%s:%s: %s %s cannot insert resource\n",
2709 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2710 __func__, dev_name(&cxlr->dev));
2714 p->interleave_ways = cxled->cxld.interleave_ways;
2715 p->interleave_granularity = cxled->cxld.interleave_granularity;
2716 p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
2718 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
2722 dev_dbg(cxlmd->dev.parent, "%s:%s: %s %s res: %pr iw: %d ig: %d\n",
2723 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), __func__,
2724 dev_name(&cxlr->dev), p->res, p->interleave_ways,
2725 p->interleave_granularity);
2727 /* ...to match put_device() in cxl_add_to_region() */
2728 get_device(&cxlr->dev);
2729 up_write(&cxl_region_rwsem);
2734 up_write(&cxl_region_rwsem);
2735 devm_release_action(port->uport, unregister_region, cxlr);
2739 int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled)
2741 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
2742 struct range *hpa = &cxled->cxld.hpa_range;
2743 struct cxl_decoder *cxld = &cxled->cxld;
2744 struct device *cxlrd_dev, *region_dev;
2745 struct cxl_root_decoder *cxlrd;
2746 struct cxl_region_params *p;
2747 struct cxl_region *cxlr;
2748 bool attach = false;
2751 cxlrd_dev = device_find_child(&root->dev, &cxld->hpa_range,
2752 match_decoder_by_range);
2754 dev_err(cxlmd->dev.parent,
2755 "%s:%s no CXL window for range %#llx:%#llx\n",
2756 dev_name(&cxlmd->dev), dev_name(&cxld->dev),
2757 cxld->hpa_range.start, cxld->hpa_range.end);
2761 cxlrd = to_cxl_root_decoder(cxlrd_dev);
2764 * Ensure that if multiple threads race to construct_region() for @hpa
2765 * one does the construction and the others add to that.
2767 mutex_lock(&cxlrd->range_lock);
2768 region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa,
2769 match_region_by_range);
2771 cxlr = construct_region(cxlrd, cxled);
2772 region_dev = &cxlr->dev;
2774 cxlr = to_cxl_region(region_dev);
2775 mutex_unlock(&cxlrd->range_lock);
2777 rc = PTR_ERR_OR_ZERO(cxlr);
2781 attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE);
2783 down_read(&cxl_region_rwsem);
2785 attach = p->state == CXL_CONFIG_COMMIT;
2786 up_read(&cxl_region_rwsem);
2790 * If device_attach() fails the range may still be active via
2791 * the platform-firmware memory map, otherwise the driver for
2792 * regions is local to this file, so driver matching can't fail.
2794 if (device_attach(&cxlr->dev) < 0)
2795 dev_err(&cxlr->dev, "failed to enable, range: %pr\n",
2799 put_device(region_dev);
2801 put_device(cxlrd_dev);
2804 EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, CXL);
2806 static int cxl_region_invalidate_memregion(struct cxl_region *cxlr)
2808 if (!test_bit(CXL_REGION_F_INCOHERENT, &cxlr->flags))
2811 if (!cpu_cache_has_invalidate_memregion()) {
2812 if (IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST)) {
2815 "Bypassing cpu_cache_invalidate_memregion() for testing!\n");
2816 clear_bit(CXL_REGION_F_INCOHERENT, &cxlr->flags);
2820 "Failed to synchronize CPU cache state\n");
2825 cpu_cache_invalidate_memregion(IORES_DESC_CXL);
2826 clear_bit(CXL_REGION_F_INCOHERENT, &cxlr->flags);
2830 static int is_system_ram(struct resource *res, void *arg)
2832 struct cxl_region *cxlr = arg;
2833 struct cxl_region_params *p = &cxlr->params;
2835 dev_dbg(&cxlr->dev, "%pr has System RAM: %pr\n", p->res, res);
2839 static int cxl_region_probe(struct device *dev)
2841 struct cxl_region *cxlr = to_cxl_region(dev);
2842 struct cxl_region_params *p = &cxlr->params;
2845 rc = down_read_interruptible(&cxl_region_rwsem);
2847 dev_dbg(&cxlr->dev, "probe interrupted\n");
2851 if (p->state < CXL_CONFIG_COMMIT) {
2852 dev_dbg(&cxlr->dev, "config state: %d\n", p->state);
2857 rc = cxl_region_invalidate_memregion(cxlr);
2860 * From this point on any path that changes the region's state away from
2861 * CXL_CONFIG_COMMIT is also responsible for releasing the driver.
2864 up_read(&cxl_region_rwsem);
2869 switch (cxlr->mode) {
2870 case CXL_DECODER_PMEM:
2871 return devm_cxl_add_pmem_region(cxlr);
2872 case CXL_DECODER_RAM:
2874 * The region can not be manged by CXL if any portion of
2875 * it is already online as 'System RAM'
2877 if (walk_iomem_res_desc(IORES_DESC_NONE,
2878 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
2879 p->res->start, p->res->end, cxlr,
2882 return devm_cxl_add_dax_region(cxlr);
2884 dev_dbg(&cxlr->dev, "unsupported region mode: %d\n",
2890 static struct cxl_driver cxl_region_driver = {
2891 .name = "cxl_region",
2892 .probe = cxl_region_probe,
2893 .id = CXL_DEVICE_REGION,
2896 int cxl_region_init(void)
2898 return cxl_driver_register(&cxl_region_driver);
2901 void cxl_region_exit(void)
2903 cxl_driver_unregister(&cxl_region_driver);
2906 MODULE_IMPORT_NS(CXL);
2907 MODULE_IMPORT_NS(DEVMEM);
2908 MODULE_ALIAS_CXL(CXL_DEVICE_REGION);