xref: /linux/drivers/cxl/core/region.c (revision 07f0148aafe8c95a3a76cd59e9e75b4d78d1d31d)
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/idr.h>
10 #include <cxlmem.h>
11 #include <cxl.h>
12 #include "core.h"
13 
14 /**
15  * DOC: cxl core region
16  *
17  * CXL Regions represent mapped memory capacity in system physical address
18  * space. Whereas the CXL Root Decoders identify the bounds of potential CXL
19  * Memory ranges, Regions represent the active mapped capacity by the HDM
20  * Decoder Capability structures throughout the Host Bridges, Switches, and
21  * Endpoints in the topology.
22  *
23  * Region configuration has ordering constraints. UUID may be set at any time
24  * but is only visible for persistent regions.
25  * 1. Interleave granularity
26  * 2. Interleave size
27  * 3. Decoder targets
28  */
29 
30 /*
31  * All changes to the interleave configuration occur with this lock held
32  * for write.
33  */
34 static DECLARE_RWSEM(cxl_region_rwsem);
35 
36 static struct cxl_region *to_cxl_region(struct device *dev);
37 
38 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
39 			 char *buf)
40 {
41 	struct cxl_region *cxlr = to_cxl_region(dev);
42 	struct cxl_region_params *p = &cxlr->params;
43 	ssize_t rc;
44 
45 	rc = down_read_interruptible(&cxl_region_rwsem);
46 	if (rc)
47 		return rc;
48 	rc = sysfs_emit(buf, "%pUb\n", &p->uuid);
49 	up_read(&cxl_region_rwsem);
50 
51 	return rc;
52 }
53 
54 static int is_dup(struct device *match, void *data)
55 {
56 	struct cxl_region_params *p;
57 	struct cxl_region *cxlr;
58 	uuid_t *uuid = data;
59 
60 	if (!is_cxl_region(match))
61 		return 0;
62 
63 	lockdep_assert_held(&cxl_region_rwsem);
64 	cxlr = to_cxl_region(match);
65 	p = &cxlr->params;
66 
67 	if (uuid_equal(&p->uuid, uuid)) {
68 		dev_dbg(match, "already has uuid: %pUb\n", uuid);
69 		return -EBUSY;
70 	}
71 
72 	return 0;
73 }
74 
75 static ssize_t uuid_store(struct device *dev, struct device_attribute *attr,
76 			  const char *buf, size_t len)
77 {
78 	struct cxl_region *cxlr = to_cxl_region(dev);
79 	struct cxl_region_params *p = &cxlr->params;
80 	uuid_t temp;
81 	ssize_t rc;
82 
83 	if (len != UUID_STRING_LEN + 1)
84 		return -EINVAL;
85 
86 	rc = uuid_parse(buf, &temp);
87 	if (rc)
88 		return rc;
89 
90 	if (uuid_is_null(&temp))
91 		return -EINVAL;
92 
93 	rc = down_write_killable(&cxl_region_rwsem);
94 	if (rc)
95 		return rc;
96 
97 	if (uuid_equal(&p->uuid, &temp))
98 		goto out;
99 
100 	rc = -EBUSY;
101 	if (p->state >= CXL_CONFIG_ACTIVE)
102 		goto out;
103 
104 	rc = bus_for_each_dev(&cxl_bus_type, NULL, &temp, is_dup);
105 	if (rc < 0)
106 		goto out;
107 
108 	uuid_copy(&p->uuid, &temp);
109 out:
110 	up_write(&cxl_region_rwsem);
111 
112 	if (rc)
113 		return rc;
114 	return len;
115 }
116 static DEVICE_ATTR_RW(uuid);
117 
118 static struct cxl_region_ref *cxl_rr_load(struct cxl_port *port,
119 					  struct cxl_region *cxlr)
120 {
121 	return xa_load(&port->regions, (unsigned long)cxlr);
122 }
123 
124 static int cxl_region_decode_reset(struct cxl_region *cxlr, int count)
125 {
126 	struct cxl_region_params *p = &cxlr->params;
127 	int i;
128 
129 	for (i = count - 1; i >= 0; i--) {
130 		struct cxl_endpoint_decoder *cxled = p->targets[i];
131 		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
132 		struct cxl_port *iter = cxled_to_port(cxled);
133 		struct cxl_ep *ep;
134 		int rc;
135 
136 		while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
137 			iter = to_cxl_port(iter->dev.parent);
138 
139 		for (ep = cxl_ep_load(iter, cxlmd); iter;
140 		     iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
141 			struct cxl_region_ref *cxl_rr;
142 			struct cxl_decoder *cxld;
143 
144 			cxl_rr = cxl_rr_load(iter, cxlr);
145 			cxld = cxl_rr->decoder;
146 			rc = cxld->reset(cxld);
147 			if (rc)
148 				return rc;
149 		}
150 
151 		rc = cxled->cxld.reset(&cxled->cxld);
152 		if (rc)
153 			return rc;
154 	}
155 
156 	return 0;
157 }
158 
159 static int cxl_region_decode_commit(struct cxl_region *cxlr)
160 {
161 	struct cxl_region_params *p = &cxlr->params;
162 	int i, rc = 0;
163 
164 	for (i = 0; i < p->nr_targets; i++) {
165 		struct cxl_endpoint_decoder *cxled = p->targets[i];
166 		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
167 		struct cxl_region_ref *cxl_rr;
168 		struct cxl_decoder *cxld;
169 		struct cxl_port *iter;
170 		struct cxl_ep *ep;
171 
172 		/* commit bottom up */
173 		for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
174 		     iter = to_cxl_port(iter->dev.parent)) {
175 			cxl_rr = cxl_rr_load(iter, cxlr);
176 			cxld = cxl_rr->decoder;
177 			if (cxld->commit)
178 				rc = cxld->commit(cxld);
179 			if (rc)
180 				break;
181 		}
182 
183 		if (rc) {
184 			/* programming @iter failed, teardown */
185 			for (ep = cxl_ep_load(iter, cxlmd); ep && iter;
186 			     iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
187 				cxl_rr = cxl_rr_load(iter, cxlr);
188 				cxld = cxl_rr->decoder;
189 				cxld->reset(cxld);
190 			}
191 
192 			cxled->cxld.reset(&cxled->cxld);
193 			goto err;
194 		}
195 	}
196 
197 	return 0;
198 
199 err:
200 	/* undo the targets that were successfully committed */
201 	cxl_region_decode_reset(cxlr, i);
202 	return rc;
203 }
204 
205 static ssize_t commit_store(struct device *dev, struct device_attribute *attr,
206 			    const char *buf, size_t len)
207 {
208 	struct cxl_region *cxlr = to_cxl_region(dev);
209 	struct cxl_region_params *p = &cxlr->params;
210 	bool commit;
211 	ssize_t rc;
212 
213 	rc = kstrtobool(buf, &commit);
214 	if (rc)
215 		return rc;
216 
217 	rc = down_write_killable(&cxl_region_rwsem);
218 	if (rc)
219 		return rc;
220 
221 	/* Already in the requested state? */
222 	if (commit && p->state >= CXL_CONFIG_COMMIT)
223 		goto out;
224 	if (!commit && p->state < CXL_CONFIG_COMMIT)
225 		goto out;
226 
227 	/* Not ready to commit? */
228 	if (commit && p->state < CXL_CONFIG_ACTIVE) {
229 		rc = -ENXIO;
230 		goto out;
231 	}
232 
233 	if (commit)
234 		rc = cxl_region_decode_commit(cxlr);
235 	else {
236 		p->state = CXL_CONFIG_RESET_PENDING;
237 		up_write(&cxl_region_rwsem);
238 		device_release_driver(&cxlr->dev);
239 		down_write(&cxl_region_rwsem);
240 
241 		/*
242 		 * The lock was dropped, so need to revalidate that the reset is
243 		 * still pending.
244 		 */
245 		if (p->state == CXL_CONFIG_RESET_PENDING)
246 			rc = cxl_region_decode_reset(cxlr, p->interleave_ways);
247 	}
248 
249 	if (rc)
250 		goto out;
251 
252 	if (commit)
253 		p->state = CXL_CONFIG_COMMIT;
254 	else if (p->state == CXL_CONFIG_RESET_PENDING)
255 		p->state = CXL_CONFIG_ACTIVE;
256 
257 out:
258 	up_write(&cxl_region_rwsem);
259 
260 	if (rc)
261 		return rc;
262 	return len;
263 }
264 
265 static ssize_t commit_show(struct device *dev, struct device_attribute *attr,
266 			   char *buf)
267 {
268 	struct cxl_region *cxlr = to_cxl_region(dev);
269 	struct cxl_region_params *p = &cxlr->params;
270 	ssize_t rc;
271 
272 	rc = down_read_interruptible(&cxl_region_rwsem);
273 	if (rc)
274 		return rc;
275 	rc = sysfs_emit(buf, "%d\n", p->state >= CXL_CONFIG_COMMIT);
276 	up_read(&cxl_region_rwsem);
277 
278 	return rc;
279 }
280 static DEVICE_ATTR_RW(commit);
281 
282 static umode_t cxl_region_visible(struct kobject *kobj, struct attribute *a,
283 				  int n)
284 {
285 	struct device *dev = kobj_to_dev(kobj);
286 	struct cxl_region *cxlr = to_cxl_region(dev);
287 
288 	if (a == &dev_attr_uuid.attr && cxlr->mode != CXL_DECODER_PMEM)
289 		return 0;
290 	return a->mode;
291 }
292 
293 static ssize_t interleave_ways_show(struct device *dev,
294 				    struct device_attribute *attr, char *buf)
295 {
296 	struct cxl_region *cxlr = to_cxl_region(dev);
297 	struct cxl_region_params *p = &cxlr->params;
298 	ssize_t rc;
299 
300 	rc = down_read_interruptible(&cxl_region_rwsem);
301 	if (rc)
302 		return rc;
303 	rc = sysfs_emit(buf, "%d\n", p->interleave_ways);
304 	up_read(&cxl_region_rwsem);
305 
306 	return rc;
307 }
308 
309 static const struct attribute_group *get_cxl_region_target_group(void);
310 
311 static ssize_t interleave_ways_store(struct device *dev,
312 				     struct device_attribute *attr,
313 				     const char *buf, size_t len)
314 {
315 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
316 	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
317 	struct cxl_region *cxlr = to_cxl_region(dev);
318 	struct cxl_region_params *p = &cxlr->params;
319 	unsigned int val, save;
320 	int rc;
321 	u8 iw;
322 
323 	rc = kstrtouint(buf, 0, &val);
324 	if (rc)
325 		return rc;
326 
327 	rc = ways_to_eiw(val, &iw);
328 	if (rc)
329 		return rc;
330 
331 	/*
332 	 * Even for x3, x9, and x12 interleaves the region interleave must be a
333 	 * power of 2 multiple of the host bridge interleave.
334 	 */
335 	if (!is_power_of_2(val / cxld->interleave_ways) ||
336 	    (val % cxld->interleave_ways)) {
337 		dev_dbg(&cxlr->dev, "invalid interleave: %d\n", val);
338 		return -EINVAL;
339 	}
340 
341 	rc = down_write_killable(&cxl_region_rwsem);
342 	if (rc)
343 		return rc;
344 	if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
345 		rc = -EBUSY;
346 		goto out;
347 	}
348 
349 	save = p->interleave_ways;
350 	p->interleave_ways = val;
351 	rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
352 	if (rc)
353 		p->interleave_ways = save;
354 out:
355 	up_write(&cxl_region_rwsem);
356 	if (rc)
357 		return rc;
358 	return len;
359 }
360 static DEVICE_ATTR_RW(interleave_ways);
361 
362 static ssize_t interleave_granularity_show(struct device *dev,
363 					   struct device_attribute *attr,
364 					   char *buf)
365 {
366 	struct cxl_region *cxlr = to_cxl_region(dev);
367 	struct cxl_region_params *p = &cxlr->params;
368 	ssize_t rc;
369 
370 	rc = down_read_interruptible(&cxl_region_rwsem);
371 	if (rc)
372 		return rc;
373 	rc = sysfs_emit(buf, "%d\n", p->interleave_granularity);
374 	up_read(&cxl_region_rwsem);
375 
376 	return rc;
377 }
378 
379 static ssize_t interleave_granularity_store(struct device *dev,
380 					    struct device_attribute *attr,
381 					    const char *buf, size_t len)
382 {
383 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
384 	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
385 	struct cxl_region *cxlr = to_cxl_region(dev);
386 	struct cxl_region_params *p = &cxlr->params;
387 	int rc, val;
388 	u16 ig;
389 
390 	rc = kstrtoint(buf, 0, &val);
391 	if (rc)
392 		return rc;
393 
394 	rc = granularity_to_eig(val, &ig);
395 	if (rc)
396 		return rc;
397 
398 	/*
399 	 * When the host-bridge is interleaved, disallow region granularity !=
400 	 * root granularity. Regions with a granularity less than the root
401 	 * interleave result in needing multiple endpoints to support a single
402 	 * slot in the interleave (possible to suport in the future). Regions
403 	 * with a granularity greater than the root interleave result in invalid
404 	 * DPA translations (invalid to support).
405 	 */
406 	if (cxld->interleave_ways > 1 && val != cxld->interleave_granularity)
407 		return -EINVAL;
408 
409 	rc = down_write_killable(&cxl_region_rwsem);
410 	if (rc)
411 		return rc;
412 	if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
413 		rc = -EBUSY;
414 		goto out;
415 	}
416 
417 	p->interleave_granularity = val;
418 out:
419 	up_write(&cxl_region_rwsem);
420 	if (rc)
421 		return rc;
422 	return len;
423 }
424 static DEVICE_ATTR_RW(interleave_granularity);
425 
426 static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
427 			     char *buf)
428 {
429 	struct cxl_region *cxlr = to_cxl_region(dev);
430 	struct cxl_region_params *p = &cxlr->params;
431 	u64 resource = -1ULL;
432 	ssize_t rc;
433 
434 	rc = down_read_interruptible(&cxl_region_rwsem);
435 	if (rc)
436 		return rc;
437 	if (p->res)
438 		resource = p->res->start;
439 	rc = sysfs_emit(buf, "%#llx\n", resource);
440 	up_read(&cxl_region_rwsem);
441 
442 	return rc;
443 }
444 static DEVICE_ATTR_RO(resource);
445 
446 static int alloc_hpa(struct cxl_region *cxlr, resource_size_t size)
447 {
448 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
449 	struct cxl_region_params *p = &cxlr->params;
450 	struct resource *res;
451 	u32 remainder = 0;
452 
453 	lockdep_assert_held_write(&cxl_region_rwsem);
454 
455 	/* Nothing to do... */
456 	if (p->res && resource_size(p->res) == size)
457 		return 0;
458 
459 	/* To change size the old size must be freed first */
460 	if (p->res)
461 		return -EBUSY;
462 
463 	if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE)
464 		return -EBUSY;
465 
466 	/* ways, granularity and uuid (if PMEM) need to be set before HPA */
467 	if (!p->interleave_ways || !p->interleave_granularity ||
468 	    (cxlr->mode == CXL_DECODER_PMEM && uuid_is_null(&p->uuid)))
469 		return -ENXIO;
470 
471 	div_u64_rem(size, SZ_256M * p->interleave_ways, &remainder);
472 	if (remainder)
473 		return -EINVAL;
474 
475 	res = alloc_free_mem_region(cxlrd->res, size, SZ_256M,
476 				    dev_name(&cxlr->dev));
477 	if (IS_ERR(res)) {
478 		dev_dbg(&cxlr->dev, "failed to allocate HPA: %ld\n",
479 			PTR_ERR(res));
480 		return PTR_ERR(res);
481 	}
482 
483 	p->res = res;
484 	p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
485 
486 	return 0;
487 }
488 
489 static void cxl_region_iomem_release(struct cxl_region *cxlr)
490 {
491 	struct cxl_region_params *p = &cxlr->params;
492 
493 	if (device_is_registered(&cxlr->dev))
494 		lockdep_assert_held_write(&cxl_region_rwsem);
495 	if (p->res) {
496 		remove_resource(p->res);
497 		kfree(p->res);
498 		p->res = NULL;
499 	}
500 }
501 
502 static int free_hpa(struct cxl_region *cxlr)
503 {
504 	struct cxl_region_params *p = &cxlr->params;
505 
506 	lockdep_assert_held_write(&cxl_region_rwsem);
507 
508 	if (!p->res)
509 		return 0;
510 
511 	if (p->state >= CXL_CONFIG_ACTIVE)
512 		return -EBUSY;
513 
514 	cxl_region_iomem_release(cxlr);
515 	p->state = CXL_CONFIG_IDLE;
516 	return 0;
517 }
518 
519 static ssize_t size_store(struct device *dev, struct device_attribute *attr,
520 			  const char *buf, size_t len)
521 {
522 	struct cxl_region *cxlr = to_cxl_region(dev);
523 	u64 val;
524 	int rc;
525 
526 	rc = kstrtou64(buf, 0, &val);
527 	if (rc)
528 		return rc;
529 
530 	rc = down_write_killable(&cxl_region_rwsem);
531 	if (rc)
532 		return rc;
533 
534 	if (val)
535 		rc = alloc_hpa(cxlr, val);
536 	else
537 		rc = free_hpa(cxlr);
538 	up_write(&cxl_region_rwsem);
539 
540 	if (rc)
541 		return rc;
542 
543 	return len;
544 }
545 
546 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
547 			 char *buf)
548 {
549 	struct cxl_region *cxlr = to_cxl_region(dev);
550 	struct cxl_region_params *p = &cxlr->params;
551 	u64 size = 0;
552 	ssize_t rc;
553 
554 	rc = down_read_interruptible(&cxl_region_rwsem);
555 	if (rc)
556 		return rc;
557 	if (p->res)
558 		size = resource_size(p->res);
559 	rc = sysfs_emit(buf, "%#llx\n", size);
560 	up_read(&cxl_region_rwsem);
561 
562 	return rc;
563 }
564 static DEVICE_ATTR_RW(size);
565 
566 static struct attribute *cxl_region_attrs[] = {
567 	&dev_attr_uuid.attr,
568 	&dev_attr_commit.attr,
569 	&dev_attr_interleave_ways.attr,
570 	&dev_attr_interleave_granularity.attr,
571 	&dev_attr_resource.attr,
572 	&dev_attr_size.attr,
573 	NULL,
574 };
575 
576 static const struct attribute_group cxl_region_group = {
577 	.attrs = cxl_region_attrs,
578 	.is_visible = cxl_region_visible,
579 };
580 
581 static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos)
582 {
583 	struct cxl_region_params *p = &cxlr->params;
584 	struct cxl_endpoint_decoder *cxled;
585 	int rc;
586 
587 	rc = down_read_interruptible(&cxl_region_rwsem);
588 	if (rc)
589 		return rc;
590 
591 	if (pos >= p->interleave_ways) {
592 		dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
593 			p->interleave_ways);
594 		rc = -ENXIO;
595 		goto out;
596 	}
597 
598 	cxled = p->targets[pos];
599 	if (!cxled)
600 		rc = sysfs_emit(buf, "\n");
601 	else
602 		rc = sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev));
603 out:
604 	up_read(&cxl_region_rwsem);
605 
606 	return rc;
607 }
608 
609 static int match_free_decoder(struct device *dev, void *data)
610 {
611 	struct cxl_decoder *cxld;
612 	int *id = data;
613 
614 	if (!is_switch_decoder(dev))
615 		return 0;
616 
617 	cxld = to_cxl_decoder(dev);
618 
619 	/* enforce ordered allocation */
620 	if (cxld->id != *id)
621 		return 0;
622 
623 	if (!cxld->region)
624 		return 1;
625 
626 	(*id)++;
627 
628 	return 0;
629 }
630 
631 static struct cxl_decoder *cxl_region_find_decoder(struct cxl_port *port,
632 						   struct cxl_region *cxlr)
633 {
634 	struct device *dev;
635 	int id = 0;
636 
637 	dev = device_find_child(&port->dev, &id, match_free_decoder);
638 	if (!dev)
639 		return NULL;
640 	/*
641 	 * This decoder is pinned registered as long as the endpoint decoder is
642 	 * registered, and endpoint decoder unregistration holds the
643 	 * cxl_region_rwsem over unregister events, so no need to hold on to
644 	 * this extra reference.
645 	 */
646 	put_device(dev);
647 	return to_cxl_decoder(dev);
648 }
649 
650 static struct cxl_region_ref *alloc_region_ref(struct cxl_port *port,
651 					       struct cxl_region *cxlr)
652 {
653 	struct cxl_region_params *p = &cxlr->params;
654 	struct cxl_region_ref *cxl_rr, *iter;
655 	unsigned long index;
656 	int rc;
657 
658 	xa_for_each(&port->regions, index, iter) {
659 		struct cxl_region_params *ip = &iter->region->params;
660 
661 		if (!ip->res)
662 			continue;
663 
664 		if (ip->res->start > p->res->start) {
665 			dev_dbg(&cxlr->dev,
666 				"%s: HPA order violation %s:%pr vs %pr\n",
667 				dev_name(&port->dev),
668 				dev_name(&iter->region->dev), ip->res, p->res);
669 			return ERR_PTR(-EBUSY);
670 		}
671 	}
672 
673 	cxl_rr = kzalloc(sizeof(*cxl_rr), GFP_KERNEL);
674 	if (!cxl_rr)
675 		return ERR_PTR(-ENOMEM);
676 	cxl_rr->port = port;
677 	cxl_rr->region = cxlr;
678 	cxl_rr->nr_targets = 1;
679 	xa_init(&cxl_rr->endpoints);
680 
681 	rc = xa_insert(&port->regions, (unsigned long)cxlr, cxl_rr, GFP_KERNEL);
682 	if (rc) {
683 		dev_dbg(&cxlr->dev,
684 			"%s: failed to track region reference: %d\n",
685 			dev_name(&port->dev), rc);
686 		kfree(cxl_rr);
687 		return ERR_PTR(rc);
688 	}
689 
690 	return cxl_rr;
691 }
692 
693 static void cxl_rr_free_decoder(struct cxl_region_ref *cxl_rr)
694 {
695 	struct cxl_region *cxlr = cxl_rr->region;
696 	struct cxl_decoder *cxld = cxl_rr->decoder;
697 
698 	if (!cxld)
699 		return;
700 
701 	dev_WARN_ONCE(&cxlr->dev, cxld->region != cxlr, "region mismatch\n");
702 	if (cxld->region == cxlr) {
703 		cxld->region = NULL;
704 		put_device(&cxlr->dev);
705 	}
706 }
707 
708 static void free_region_ref(struct cxl_region_ref *cxl_rr)
709 {
710 	struct cxl_port *port = cxl_rr->port;
711 	struct cxl_region *cxlr = cxl_rr->region;
712 
713 	cxl_rr_free_decoder(cxl_rr);
714 	xa_erase(&port->regions, (unsigned long)cxlr);
715 	xa_destroy(&cxl_rr->endpoints);
716 	kfree(cxl_rr);
717 }
718 
719 static int cxl_rr_ep_add(struct cxl_region_ref *cxl_rr,
720 			 struct cxl_endpoint_decoder *cxled)
721 {
722 	int rc;
723 	struct cxl_port *port = cxl_rr->port;
724 	struct cxl_region *cxlr = cxl_rr->region;
725 	struct cxl_decoder *cxld = cxl_rr->decoder;
726 	struct cxl_ep *ep = cxl_ep_load(port, cxled_to_memdev(cxled));
727 
728 	if (ep) {
729 		rc = xa_insert(&cxl_rr->endpoints, (unsigned long)cxled, ep,
730 			       GFP_KERNEL);
731 		if (rc)
732 			return rc;
733 	}
734 	cxl_rr->nr_eps++;
735 
736 	if (!cxld->region) {
737 		cxld->region = cxlr;
738 		get_device(&cxlr->dev);
739 	}
740 
741 	return 0;
742 }
743 
744 static int cxl_rr_alloc_decoder(struct cxl_port *port, struct cxl_region *cxlr,
745 				struct cxl_endpoint_decoder *cxled,
746 				struct cxl_region_ref *cxl_rr)
747 {
748 	struct cxl_decoder *cxld;
749 
750 	if (port == cxled_to_port(cxled))
751 		cxld = &cxled->cxld;
752 	else
753 		cxld = cxl_region_find_decoder(port, cxlr);
754 	if (!cxld) {
755 		dev_dbg(&cxlr->dev, "%s: no decoder available\n",
756 			dev_name(&port->dev));
757 		return -EBUSY;
758 	}
759 
760 	if (cxld->region) {
761 		dev_dbg(&cxlr->dev, "%s: %s already attached to %s\n",
762 			dev_name(&port->dev), dev_name(&cxld->dev),
763 			dev_name(&cxld->region->dev));
764 		return -EBUSY;
765 	}
766 
767 	cxl_rr->decoder = cxld;
768 	return 0;
769 }
770 
771 /**
772  * cxl_port_attach_region() - track a region's interest in a port by endpoint
773  * @port: port to add a new region reference 'struct cxl_region_ref'
774  * @cxlr: region to attach to @port
775  * @cxled: endpoint decoder used to create or further pin a region reference
776  * @pos: interleave position of @cxled in @cxlr
777  *
778  * The attach event is an opportunity to validate CXL decode setup
779  * constraints and record metadata needed for programming HDM decoders,
780  * in particular decoder target lists.
781  *
782  * The steps are:
783  *
784  * - validate that there are no other regions with a higher HPA already
785  *   associated with @port
786  * - establish a region reference if one is not already present
787  *
788  *   - additionally allocate a decoder instance that will host @cxlr on
789  *     @port
790  *
791  * - pin the region reference by the endpoint
792  * - account for how many entries in @port's target list are needed to
793  *   cover all of the added endpoints.
794  */
795 static int cxl_port_attach_region(struct cxl_port *port,
796 				  struct cxl_region *cxlr,
797 				  struct cxl_endpoint_decoder *cxled, int pos)
798 {
799 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
800 	struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
801 	struct cxl_region_ref *cxl_rr;
802 	bool nr_targets_inc = false;
803 	struct cxl_decoder *cxld;
804 	unsigned long index;
805 	int rc = -EBUSY;
806 
807 	lockdep_assert_held_write(&cxl_region_rwsem);
808 
809 	cxl_rr = cxl_rr_load(port, cxlr);
810 	if (cxl_rr) {
811 		struct cxl_ep *ep_iter;
812 		int found = 0;
813 
814 		/*
815 		 * Walk the existing endpoints that have been attached to
816 		 * @cxlr at @port and see if they share the same 'next' port
817 		 * in the downstream direction. I.e. endpoints that share common
818 		 * upstream switch.
819 		 */
820 		xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
821 			if (ep_iter == ep)
822 				continue;
823 			if (ep_iter->next == ep->next) {
824 				found++;
825 				break;
826 			}
827 		}
828 
829 		/*
830 		 * New target port, or @port is an endpoint port that always
831 		 * accounts its own local decode as a target.
832 		 */
833 		if (!found || !ep->next) {
834 			cxl_rr->nr_targets++;
835 			nr_targets_inc = true;
836 		}
837 	} else {
838 		cxl_rr = alloc_region_ref(port, cxlr);
839 		if (IS_ERR(cxl_rr)) {
840 			dev_dbg(&cxlr->dev,
841 				"%s: failed to allocate region reference\n",
842 				dev_name(&port->dev));
843 			return PTR_ERR(cxl_rr);
844 		}
845 		nr_targets_inc = true;
846 
847 		rc = cxl_rr_alloc_decoder(port, cxlr, cxled, cxl_rr);
848 		if (rc)
849 			goto out_erase;
850 	}
851 	cxld = cxl_rr->decoder;
852 
853 	rc = cxl_rr_ep_add(cxl_rr, cxled);
854 	if (rc) {
855 		dev_dbg(&cxlr->dev,
856 			"%s: failed to track endpoint %s:%s reference\n",
857 			dev_name(&port->dev), dev_name(&cxlmd->dev),
858 			dev_name(&cxld->dev));
859 		goto out_erase;
860 	}
861 
862 	dev_dbg(&cxlr->dev,
863 		"%s:%s %s add: %s:%s @ %d next: %s nr_eps: %d nr_targets: %d\n",
864 		dev_name(port->uport), dev_name(&port->dev),
865 		dev_name(&cxld->dev), dev_name(&cxlmd->dev),
866 		dev_name(&cxled->cxld.dev), pos,
867 		ep ? ep->next ? dev_name(ep->next->uport) :
868 				      dev_name(&cxlmd->dev) :
869 			   "none",
870 		cxl_rr->nr_eps, cxl_rr->nr_targets);
871 
872 	return 0;
873 out_erase:
874 	if (nr_targets_inc)
875 		cxl_rr->nr_targets--;
876 	if (cxl_rr->nr_eps == 0)
877 		free_region_ref(cxl_rr);
878 	return rc;
879 }
880 
881 static void cxl_port_detach_region(struct cxl_port *port,
882 				   struct cxl_region *cxlr,
883 				   struct cxl_endpoint_decoder *cxled)
884 {
885 	struct cxl_region_ref *cxl_rr;
886 	struct cxl_ep *ep = NULL;
887 
888 	lockdep_assert_held_write(&cxl_region_rwsem);
889 
890 	cxl_rr = cxl_rr_load(port, cxlr);
891 	if (!cxl_rr)
892 		return;
893 
894 	/*
895 	 * Endpoint ports do not carry cxl_ep references, and they
896 	 * never target more than one endpoint by definition
897 	 */
898 	if (cxl_rr->decoder == &cxled->cxld)
899 		cxl_rr->nr_eps--;
900 	else
901 		ep = xa_erase(&cxl_rr->endpoints, (unsigned long)cxled);
902 	if (ep) {
903 		struct cxl_ep *ep_iter;
904 		unsigned long index;
905 		int found = 0;
906 
907 		cxl_rr->nr_eps--;
908 		xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
909 			if (ep_iter->next == ep->next) {
910 				found++;
911 				break;
912 			}
913 		}
914 		if (!found)
915 			cxl_rr->nr_targets--;
916 	}
917 
918 	if (cxl_rr->nr_eps == 0)
919 		free_region_ref(cxl_rr);
920 }
921 
922 static int check_last_peer(struct cxl_endpoint_decoder *cxled,
923 			   struct cxl_ep *ep, struct cxl_region_ref *cxl_rr,
924 			   int distance)
925 {
926 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
927 	struct cxl_region *cxlr = cxl_rr->region;
928 	struct cxl_region_params *p = &cxlr->params;
929 	struct cxl_endpoint_decoder *cxled_peer;
930 	struct cxl_port *port = cxl_rr->port;
931 	struct cxl_memdev *cxlmd_peer;
932 	struct cxl_ep *ep_peer;
933 	int pos = cxled->pos;
934 
935 	/*
936 	 * If this position wants to share a dport with the last endpoint mapped
937 	 * then that endpoint, at index 'position - distance', must also be
938 	 * mapped by this dport.
939 	 */
940 	if (pos < distance) {
941 		dev_dbg(&cxlr->dev, "%s:%s: cannot host %s:%s at %d\n",
942 			dev_name(port->uport), dev_name(&port->dev),
943 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
944 		return -ENXIO;
945 	}
946 	cxled_peer = p->targets[pos - distance];
947 	cxlmd_peer = cxled_to_memdev(cxled_peer);
948 	ep_peer = cxl_ep_load(port, cxlmd_peer);
949 	if (ep->dport != ep_peer->dport) {
950 		dev_dbg(&cxlr->dev,
951 			"%s:%s: %s:%s pos %d mismatched peer %s:%s\n",
952 			dev_name(port->uport), dev_name(&port->dev),
953 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos,
954 			dev_name(&cxlmd_peer->dev),
955 			dev_name(&cxled_peer->cxld.dev));
956 		return -ENXIO;
957 	}
958 
959 	return 0;
960 }
961 
962 static int cxl_port_setup_targets(struct cxl_port *port,
963 				  struct cxl_region *cxlr,
964 				  struct cxl_endpoint_decoder *cxled)
965 {
966 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
967 	int parent_iw, parent_ig, ig, iw, rc, inc = 0, pos = cxled->pos;
968 	struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
969 	struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
970 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
971 	struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
972 	struct cxl_region_params *p = &cxlr->params;
973 	struct cxl_decoder *cxld = cxl_rr->decoder;
974 	struct cxl_switch_decoder *cxlsd;
975 	u16 eig, peig;
976 	u8 eiw, peiw;
977 
978 	/*
979 	 * While root level decoders support x3, x6, x12, switch level
980 	 * decoders only support powers of 2 up to x16.
981 	 */
982 	if (!is_power_of_2(cxl_rr->nr_targets)) {
983 		dev_dbg(&cxlr->dev, "%s:%s: invalid target count %d\n",
984 			dev_name(port->uport), dev_name(&port->dev),
985 			cxl_rr->nr_targets);
986 		return -EINVAL;
987 	}
988 
989 	cxlsd = to_cxl_switch_decoder(&cxld->dev);
990 	if (cxl_rr->nr_targets_set) {
991 		int i, distance;
992 
993 		/*
994 		 * Passthrough ports impose no distance requirements between
995 		 * peers
996 		 */
997 		if (port->nr_dports == 1)
998 			distance = 0;
999 		else
1000 			distance = p->nr_targets / cxl_rr->nr_targets;
1001 		for (i = 0; i < cxl_rr->nr_targets_set; i++)
1002 			if (ep->dport == cxlsd->target[i]) {
1003 				rc = check_last_peer(cxled, ep, cxl_rr,
1004 						     distance);
1005 				if (rc)
1006 					return rc;
1007 				goto out_target_set;
1008 			}
1009 		goto add_target;
1010 	}
1011 
1012 	if (is_cxl_root(parent_port)) {
1013 		parent_ig = cxlrd->cxlsd.cxld.interleave_granularity;
1014 		parent_iw = cxlrd->cxlsd.cxld.interleave_ways;
1015 		/*
1016 		 * For purposes of address bit routing, use power-of-2 math for
1017 		 * switch ports.
1018 		 */
1019 		if (!is_power_of_2(parent_iw))
1020 			parent_iw /= 3;
1021 	} else {
1022 		struct cxl_region_ref *parent_rr;
1023 		struct cxl_decoder *parent_cxld;
1024 
1025 		parent_rr = cxl_rr_load(parent_port, cxlr);
1026 		parent_cxld = parent_rr->decoder;
1027 		parent_ig = parent_cxld->interleave_granularity;
1028 		parent_iw = parent_cxld->interleave_ways;
1029 	}
1030 
1031 	rc = granularity_to_eig(parent_ig, &peig);
1032 	if (rc) {
1033 		dev_dbg(&cxlr->dev, "%s:%s: invalid parent granularity: %d\n",
1034 			dev_name(parent_port->uport),
1035 			dev_name(&parent_port->dev), parent_ig);
1036 		return rc;
1037 	}
1038 
1039 	rc = ways_to_eiw(parent_iw, &peiw);
1040 	if (rc) {
1041 		dev_dbg(&cxlr->dev, "%s:%s: invalid parent interleave: %d\n",
1042 			dev_name(parent_port->uport),
1043 			dev_name(&parent_port->dev), parent_iw);
1044 		return rc;
1045 	}
1046 
1047 	iw = cxl_rr->nr_targets;
1048 	rc = ways_to_eiw(iw, &eiw);
1049 	if (rc) {
1050 		dev_dbg(&cxlr->dev, "%s:%s: invalid port interleave: %d\n",
1051 			dev_name(port->uport), dev_name(&port->dev), iw);
1052 		return rc;
1053 	}
1054 
1055 	/*
1056 	 * If @parent_port is masking address bits, pick the next unused address
1057 	 * bit to route @port's targets.
1058 	 */
1059 	if (parent_iw > 1 && cxl_rr->nr_targets > 1) {
1060 		u32 address_bit = max(peig + peiw, eiw + peig);
1061 
1062 		eig = address_bit - eiw + 1;
1063 	} else {
1064 		eiw = peiw;
1065 		eig = peig;
1066 	}
1067 
1068 	rc = eig_to_granularity(eig, &ig);
1069 	if (rc) {
1070 		dev_dbg(&cxlr->dev, "%s:%s: invalid interleave: %d\n",
1071 			dev_name(port->uport), dev_name(&port->dev),
1072 			256 << eig);
1073 		return rc;
1074 	}
1075 
1076 	cxld->interleave_ways = iw;
1077 	cxld->interleave_granularity = ig;
1078 	cxld->hpa_range = (struct range) {
1079 		.start = p->res->start,
1080 		.end = p->res->end,
1081 	};
1082 	dev_dbg(&cxlr->dev, "%s:%s iw: %d ig: %d\n", dev_name(port->uport),
1083 		dev_name(&port->dev), iw, ig);
1084 add_target:
1085 	if (cxl_rr->nr_targets_set == cxl_rr->nr_targets) {
1086 		dev_dbg(&cxlr->dev,
1087 			"%s:%s: targets full trying to add %s:%s at %d\n",
1088 			dev_name(port->uport), dev_name(&port->dev),
1089 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1090 		return -ENXIO;
1091 	}
1092 	cxlsd->target[cxl_rr->nr_targets_set] = ep->dport;
1093 	inc = 1;
1094 out_target_set:
1095 	cxl_rr->nr_targets_set += inc;
1096 	dev_dbg(&cxlr->dev, "%s:%s target[%d] = %s for %s:%s @ %d\n",
1097 		dev_name(port->uport), dev_name(&port->dev),
1098 		cxl_rr->nr_targets_set - 1, dev_name(ep->dport->dport),
1099 		dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1100 
1101 	return 0;
1102 }
1103 
1104 static void cxl_port_reset_targets(struct cxl_port *port,
1105 				   struct cxl_region *cxlr)
1106 {
1107 	struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
1108 	struct cxl_decoder *cxld;
1109 
1110 	/*
1111 	 * After the last endpoint has been detached the entire cxl_rr may now
1112 	 * be gone.
1113 	 */
1114 	if (!cxl_rr)
1115 		return;
1116 	cxl_rr->nr_targets_set = 0;
1117 
1118 	cxld = cxl_rr->decoder;
1119 	cxld->hpa_range = (struct range) {
1120 		.start = 0,
1121 		.end = -1,
1122 	};
1123 }
1124 
1125 static void cxl_region_teardown_targets(struct cxl_region *cxlr)
1126 {
1127 	struct cxl_region_params *p = &cxlr->params;
1128 	struct cxl_endpoint_decoder *cxled;
1129 	struct cxl_memdev *cxlmd;
1130 	struct cxl_port *iter;
1131 	struct cxl_ep *ep;
1132 	int i;
1133 
1134 	for (i = 0; i < p->nr_targets; i++) {
1135 		cxled = p->targets[i];
1136 		cxlmd = cxled_to_memdev(cxled);
1137 
1138 		iter = cxled_to_port(cxled);
1139 		while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
1140 			iter = to_cxl_port(iter->dev.parent);
1141 
1142 		for (ep = cxl_ep_load(iter, cxlmd); iter;
1143 		     iter = ep->next, ep = cxl_ep_load(iter, cxlmd))
1144 			cxl_port_reset_targets(iter, cxlr);
1145 	}
1146 }
1147 
1148 static int cxl_region_setup_targets(struct cxl_region *cxlr)
1149 {
1150 	struct cxl_region_params *p = &cxlr->params;
1151 	struct cxl_endpoint_decoder *cxled;
1152 	struct cxl_memdev *cxlmd;
1153 	struct cxl_port *iter;
1154 	struct cxl_ep *ep;
1155 	int i, rc;
1156 
1157 	for (i = 0; i < p->nr_targets; i++) {
1158 		cxled = p->targets[i];
1159 		cxlmd = cxled_to_memdev(cxled);
1160 
1161 		iter = cxled_to_port(cxled);
1162 		while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
1163 			iter = to_cxl_port(iter->dev.parent);
1164 
1165 		/*
1166 		 * Descend the topology tree programming targets while
1167 		 * looking for conflicts.
1168 		 */
1169 		for (ep = cxl_ep_load(iter, cxlmd); iter;
1170 		     iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
1171 			rc = cxl_port_setup_targets(iter, cxlr, cxled);
1172 			if (rc) {
1173 				cxl_region_teardown_targets(cxlr);
1174 				return rc;
1175 			}
1176 		}
1177 	}
1178 
1179 	return 0;
1180 }
1181 
1182 static int cxl_region_attach(struct cxl_region *cxlr,
1183 			     struct cxl_endpoint_decoder *cxled, int pos)
1184 {
1185 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
1186 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1187 	struct cxl_port *ep_port, *root_port, *iter;
1188 	struct cxl_region_params *p = &cxlr->params;
1189 	struct cxl_dport *dport;
1190 	int i, rc = -ENXIO;
1191 
1192 	if (cxled->mode == CXL_DECODER_DEAD) {
1193 		dev_dbg(&cxlr->dev, "%s dead\n", dev_name(&cxled->cxld.dev));
1194 		return -ENODEV;
1195 	}
1196 
1197 	/* all full of members, or interleave config not established? */
1198 	if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) {
1199 		dev_dbg(&cxlr->dev, "region already active\n");
1200 		return -EBUSY;
1201 	} else if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
1202 		dev_dbg(&cxlr->dev, "interleave config missing\n");
1203 		return -ENXIO;
1204 	}
1205 
1206 	if (pos < 0 || pos >= p->interleave_ways) {
1207 		dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
1208 			p->interleave_ways);
1209 		return -ENXIO;
1210 	}
1211 
1212 	if (p->targets[pos] == cxled)
1213 		return 0;
1214 
1215 	if (p->targets[pos]) {
1216 		struct cxl_endpoint_decoder *cxled_target = p->targets[pos];
1217 		struct cxl_memdev *cxlmd_target = cxled_to_memdev(cxled_target);
1218 
1219 		dev_dbg(&cxlr->dev, "position %d already assigned to %s:%s\n",
1220 			pos, dev_name(&cxlmd_target->dev),
1221 			dev_name(&cxled_target->cxld.dev));
1222 		return -EBUSY;
1223 	}
1224 
1225 	for (i = 0; i < p->interleave_ways; i++) {
1226 		struct cxl_endpoint_decoder *cxled_target;
1227 		struct cxl_memdev *cxlmd_target;
1228 
1229 		cxled_target = p->targets[i];
1230 		if (!cxled_target)
1231 			continue;
1232 
1233 		cxlmd_target = cxled_to_memdev(cxled_target);
1234 		if (cxlmd_target == cxlmd) {
1235 			dev_dbg(&cxlr->dev,
1236 				"%s already specified at position %d via: %s\n",
1237 				dev_name(&cxlmd->dev), pos,
1238 				dev_name(&cxled_target->cxld.dev));
1239 			return -EBUSY;
1240 		}
1241 	}
1242 
1243 	ep_port = cxled_to_port(cxled);
1244 	root_port = cxlrd_to_port(cxlrd);
1245 	dport = cxl_find_dport_by_dev(root_port, ep_port->host_bridge);
1246 	if (!dport) {
1247 		dev_dbg(&cxlr->dev, "%s:%s invalid target for %s\n",
1248 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1249 			dev_name(cxlr->dev.parent));
1250 		return -ENXIO;
1251 	}
1252 
1253 	if (cxlrd->calc_hb(cxlrd, pos) != dport) {
1254 		dev_dbg(&cxlr->dev, "%s:%s invalid target position for %s\n",
1255 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1256 			dev_name(&cxlrd->cxlsd.cxld.dev));
1257 		return -ENXIO;
1258 	}
1259 
1260 	if (cxled->cxld.target_type != cxlr->type) {
1261 		dev_dbg(&cxlr->dev, "%s:%s type mismatch: %d vs %d\n",
1262 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1263 			cxled->cxld.target_type, cxlr->type);
1264 		return -ENXIO;
1265 	}
1266 
1267 	if (!cxled->dpa_res) {
1268 		dev_dbg(&cxlr->dev, "%s:%s: missing DPA allocation.\n",
1269 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev));
1270 		return -ENXIO;
1271 	}
1272 
1273 	if (resource_size(cxled->dpa_res) * p->interleave_ways !=
1274 	    resource_size(p->res)) {
1275 		dev_dbg(&cxlr->dev,
1276 			"%s:%s: decoder-size-%#llx * ways-%d != region-size-%#llx\n",
1277 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1278 			(u64)resource_size(cxled->dpa_res), p->interleave_ways,
1279 			(u64)resource_size(p->res));
1280 		return -EINVAL;
1281 	}
1282 
1283 	for (iter = ep_port; !is_cxl_root(iter);
1284 	     iter = to_cxl_port(iter->dev.parent)) {
1285 		rc = cxl_port_attach_region(iter, cxlr, cxled, pos);
1286 		if (rc)
1287 			goto err;
1288 	}
1289 
1290 	p->targets[pos] = cxled;
1291 	cxled->pos = pos;
1292 	p->nr_targets++;
1293 
1294 	if (p->nr_targets == p->interleave_ways) {
1295 		rc = cxl_region_setup_targets(cxlr);
1296 		if (rc)
1297 			goto err_decrement;
1298 		p->state = CXL_CONFIG_ACTIVE;
1299 	}
1300 
1301 	cxled->cxld.interleave_ways = p->interleave_ways;
1302 	cxled->cxld.interleave_granularity = p->interleave_granularity;
1303 	cxled->cxld.hpa_range = (struct range) {
1304 		.start = p->res->start,
1305 		.end = p->res->end,
1306 	};
1307 
1308 	return 0;
1309 
1310 err_decrement:
1311 	p->nr_targets--;
1312 err:
1313 	for (iter = ep_port; !is_cxl_root(iter);
1314 	     iter = to_cxl_port(iter->dev.parent))
1315 		cxl_port_detach_region(iter, cxlr, cxled);
1316 	return rc;
1317 }
1318 
1319 static int cxl_region_detach(struct cxl_endpoint_decoder *cxled)
1320 {
1321 	struct cxl_port *iter, *ep_port = cxled_to_port(cxled);
1322 	struct cxl_region *cxlr = cxled->cxld.region;
1323 	struct cxl_region_params *p;
1324 	int rc = 0;
1325 
1326 	lockdep_assert_held_write(&cxl_region_rwsem);
1327 
1328 	if (!cxlr)
1329 		return 0;
1330 
1331 	p = &cxlr->params;
1332 	get_device(&cxlr->dev);
1333 
1334 	if (p->state > CXL_CONFIG_ACTIVE) {
1335 		/*
1336 		 * TODO: tear down all impacted regions if a device is
1337 		 * removed out of order
1338 		 */
1339 		rc = cxl_region_decode_reset(cxlr, p->interleave_ways);
1340 		if (rc)
1341 			goto out;
1342 		p->state = CXL_CONFIG_ACTIVE;
1343 	}
1344 
1345 	for (iter = ep_port; !is_cxl_root(iter);
1346 	     iter = to_cxl_port(iter->dev.parent))
1347 		cxl_port_detach_region(iter, cxlr, cxled);
1348 
1349 	if (cxled->pos < 0 || cxled->pos >= p->interleave_ways ||
1350 	    p->targets[cxled->pos] != cxled) {
1351 		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1352 
1353 		dev_WARN_ONCE(&cxlr->dev, 1, "expected %s:%s at position %d\n",
1354 			      dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1355 			      cxled->pos);
1356 		goto out;
1357 	}
1358 
1359 	if (p->state == CXL_CONFIG_ACTIVE) {
1360 		p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
1361 		cxl_region_teardown_targets(cxlr);
1362 	}
1363 	p->targets[cxled->pos] = NULL;
1364 	p->nr_targets--;
1365 	cxled->cxld.hpa_range = (struct range) {
1366 		.start = 0,
1367 		.end = -1,
1368 	};
1369 
1370 	/* notify the region driver that one of its targets has departed */
1371 	up_write(&cxl_region_rwsem);
1372 	device_release_driver(&cxlr->dev);
1373 	down_write(&cxl_region_rwsem);
1374 out:
1375 	put_device(&cxlr->dev);
1376 	return rc;
1377 }
1378 
1379 void cxl_decoder_kill_region(struct cxl_endpoint_decoder *cxled)
1380 {
1381 	down_write(&cxl_region_rwsem);
1382 	cxled->mode = CXL_DECODER_DEAD;
1383 	cxl_region_detach(cxled);
1384 	up_write(&cxl_region_rwsem);
1385 }
1386 
1387 static int attach_target(struct cxl_region *cxlr, const char *decoder, int pos)
1388 {
1389 	struct device *dev;
1390 	int rc;
1391 
1392 	dev = bus_find_device_by_name(&cxl_bus_type, NULL, decoder);
1393 	if (!dev)
1394 		return -ENODEV;
1395 
1396 	if (!is_endpoint_decoder(dev)) {
1397 		put_device(dev);
1398 		return -EINVAL;
1399 	}
1400 
1401 	rc = down_write_killable(&cxl_region_rwsem);
1402 	if (rc)
1403 		goto out;
1404 	down_read(&cxl_dpa_rwsem);
1405 	rc = cxl_region_attach(cxlr, to_cxl_endpoint_decoder(dev), pos);
1406 	if (rc == 0)
1407 		set_bit(CXL_REGION_F_INCOHERENT, &cxlr->flags);
1408 	up_read(&cxl_dpa_rwsem);
1409 	up_write(&cxl_region_rwsem);
1410 out:
1411 	put_device(dev);
1412 	return rc;
1413 }
1414 
1415 static int detach_target(struct cxl_region *cxlr, int pos)
1416 {
1417 	struct cxl_region_params *p = &cxlr->params;
1418 	int rc;
1419 
1420 	rc = down_write_killable(&cxl_region_rwsem);
1421 	if (rc)
1422 		return rc;
1423 
1424 	if (pos >= p->interleave_ways) {
1425 		dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
1426 			p->interleave_ways);
1427 		rc = -ENXIO;
1428 		goto out;
1429 	}
1430 
1431 	if (!p->targets[pos]) {
1432 		rc = 0;
1433 		goto out;
1434 	}
1435 
1436 	rc = cxl_region_detach(p->targets[pos]);
1437 out:
1438 	up_write(&cxl_region_rwsem);
1439 	return rc;
1440 }
1441 
1442 static size_t store_targetN(struct cxl_region *cxlr, const char *buf, int pos,
1443 			    size_t len)
1444 {
1445 	int rc;
1446 
1447 	if (sysfs_streq(buf, "\n"))
1448 		rc = detach_target(cxlr, pos);
1449 	else
1450 		rc = attach_target(cxlr, buf, pos);
1451 
1452 	if (rc < 0)
1453 		return rc;
1454 	return len;
1455 }
1456 
1457 #define TARGET_ATTR_RW(n)                                              \
1458 static ssize_t target##n##_show(                                       \
1459 	struct device *dev, struct device_attribute *attr, char *buf)  \
1460 {                                                                      \
1461 	return show_targetN(to_cxl_region(dev), buf, (n));             \
1462 }                                                                      \
1463 static ssize_t target##n##_store(struct device *dev,                   \
1464 				 struct device_attribute *attr,        \
1465 				 const char *buf, size_t len)          \
1466 {                                                                      \
1467 	return store_targetN(to_cxl_region(dev), buf, (n), len);       \
1468 }                                                                      \
1469 static DEVICE_ATTR_RW(target##n)
1470 
1471 TARGET_ATTR_RW(0);
1472 TARGET_ATTR_RW(1);
1473 TARGET_ATTR_RW(2);
1474 TARGET_ATTR_RW(3);
1475 TARGET_ATTR_RW(4);
1476 TARGET_ATTR_RW(5);
1477 TARGET_ATTR_RW(6);
1478 TARGET_ATTR_RW(7);
1479 TARGET_ATTR_RW(8);
1480 TARGET_ATTR_RW(9);
1481 TARGET_ATTR_RW(10);
1482 TARGET_ATTR_RW(11);
1483 TARGET_ATTR_RW(12);
1484 TARGET_ATTR_RW(13);
1485 TARGET_ATTR_RW(14);
1486 TARGET_ATTR_RW(15);
1487 
1488 static struct attribute *target_attrs[] = {
1489 	&dev_attr_target0.attr,
1490 	&dev_attr_target1.attr,
1491 	&dev_attr_target2.attr,
1492 	&dev_attr_target3.attr,
1493 	&dev_attr_target4.attr,
1494 	&dev_attr_target5.attr,
1495 	&dev_attr_target6.attr,
1496 	&dev_attr_target7.attr,
1497 	&dev_attr_target8.attr,
1498 	&dev_attr_target9.attr,
1499 	&dev_attr_target10.attr,
1500 	&dev_attr_target11.attr,
1501 	&dev_attr_target12.attr,
1502 	&dev_attr_target13.attr,
1503 	&dev_attr_target14.attr,
1504 	&dev_attr_target15.attr,
1505 	NULL,
1506 };
1507 
1508 static umode_t cxl_region_target_visible(struct kobject *kobj,
1509 					 struct attribute *a, int n)
1510 {
1511 	struct device *dev = kobj_to_dev(kobj);
1512 	struct cxl_region *cxlr = to_cxl_region(dev);
1513 	struct cxl_region_params *p = &cxlr->params;
1514 
1515 	if (n < p->interleave_ways)
1516 		return a->mode;
1517 	return 0;
1518 }
1519 
1520 static const struct attribute_group cxl_region_target_group = {
1521 	.attrs = target_attrs,
1522 	.is_visible = cxl_region_target_visible,
1523 };
1524 
1525 static const struct attribute_group *get_cxl_region_target_group(void)
1526 {
1527 	return &cxl_region_target_group;
1528 }
1529 
1530 static const struct attribute_group *region_groups[] = {
1531 	&cxl_base_attribute_group,
1532 	&cxl_region_group,
1533 	&cxl_region_target_group,
1534 	NULL,
1535 };
1536 
1537 static void cxl_region_release(struct device *dev)
1538 {
1539 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
1540 	struct cxl_region *cxlr = to_cxl_region(dev);
1541 	int id = atomic_read(&cxlrd->region_id);
1542 
1543 	/*
1544 	 * Try to reuse the recently idled id rather than the cached
1545 	 * next id to prevent the region id space from increasing
1546 	 * unnecessarily.
1547 	 */
1548 	if (cxlr->id < id)
1549 		if (atomic_try_cmpxchg(&cxlrd->region_id, &id, cxlr->id)) {
1550 			memregion_free(id);
1551 			goto out;
1552 		}
1553 
1554 	memregion_free(cxlr->id);
1555 out:
1556 	put_device(dev->parent);
1557 	kfree(cxlr);
1558 }
1559 
1560 const struct device_type cxl_region_type = {
1561 	.name = "cxl_region",
1562 	.release = cxl_region_release,
1563 	.groups = region_groups
1564 };
1565 
1566 bool is_cxl_region(struct device *dev)
1567 {
1568 	return dev->type == &cxl_region_type;
1569 }
1570 EXPORT_SYMBOL_NS_GPL(is_cxl_region, CXL);
1571 
1572 static struct cxl_region *to_cxl_region(struct device *dev)
1573 {
1574 	if (dev_WARN_ONCE(dev, dev->type != &cxl_region_type,
1575 			  "not a cxl_region device\n"))
1576 		return NULL;
1577 
1578 	return container_of(dev, struct cxl_region, dev);
1579 }
1580 
1581 static void unregister_region(void *dev)
1582 {
1583 	struct cxl_region *cxlr = to_cxl_region(dev);
1584 	struct cxl_region_params *p = &cxlr->params;
1585 	int i;
1586 
1587 	device_del(dev);
1588 
1589 	/*
1590 	 * Now that region sysfs is shutdown, the parameter block is now
1591 	 * read-only, so no need to hold the region rwsem to access the
1592 	 * region parameters.
1593 	 */
1594 	for (i = 0; i < p->interleave_ways; i++)
1595 		detach_target(cxlr, i);
1596 
1597 	cxl_region_iomem_release(cxlr);
1598 	put_device(dev);
1599 }
1600 
1601 static struct lock_class_key cxl_region_key;
1602 
1603 static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id)
1604 {
1605 	struct cxl_region *cxlr;
1606 	struct device *dev;
1607 
1608 	cxlr = kzalloc(sizeof(*cxlr), GFP_KERNEL);
1609 	if (!cxlr) {
1610 		memregion_free(id);
1611 		return ERR_PTR(-ENOMEM);
1612 	}
1613 
1614 	dev = &cxlr->dev;
1615 	device_initialize(dev);
1616 	lockdep_set_class(&dev->mutex, &cxl_region_key);
1617 	dev->parent = &cxlrd->cxlsd.cxld.dev;
1618 	/*
1619 	 * Keep root decoder pinned through cxl_region_release to fixup
1620 	 * region id allocations
1621 	 */
1622 	get_device(dev->parent);
1623 	device_set_pm_not_required(dev);
1624 	dev->bus = &cxl_bus_type;
1625 	dev->type = &cxl_region_type;
1626 	cxlr->id = id;
1627 
1628 	return cxlr;
1629 }
1630 
1631 /**
1632  * devm_cxl_add_region - Adds a region to a decoder
1633  * @cxlrd: root decoder
1634  * @id: memregion id to create, or memregion_free() on failure
1635  * @mode: mode for the endpoint decoders of this region
1636  * @type: select whether this is an expander or accelerator (type-2 or type-3)
1637  *
1638  * This is the second step of region initialization. Regions exist within an
1639  * address space which is mapped by a @cxlrd.
1640  *
1641  * Return: 0 if the region was added to the @cxlrd, else returns negative error
1642  * code. The region will be named "regionZ" where Z is the unique region number.
1643  */
1644 static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd,
1645 					      int id,
1646 					      enum cxl_decoder_mode mode,
1647 					      enum cxl_decoder_type type)
1648 {
1649 	struct cxl_port *port = to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
1650 	struct cxl_region *cxlr;
1651 	struct device *dev;
1652 	int rc;
1653 
1654 	cxlr = cxl_region_alloc(cxlrd, id);
1655 	if (IS_ERR(cxlr))
1656 		return cxlr;
1657 	cxlr->mode = mode;
1658 	cxlr->type = type;
1659 
1660 	dev = &cxlr->dev;
1661 	rc = dev_set_name(dev, "region%d", id);
1662 	if (rc)
1663 		goto err;
1664 
1665 	rc = device_add(dev);
1666 	if (rc)
1667 		goto err;
1668 
1669 	rc = devm_add_action_or_reset(port->uport, unregister_region, cxlr);
1670 	if (rc)
1671 		return ERR_PTR(rc);
1672 
1673 	dev_dbg(port->uport, "%s: created %s\n",
1674 		dev_name(&cxlrd->cxlsd.cxld.dev), dev_name(dev));
1675 	return cxlr;
1676 
1677 err:
1678 	put_device(dev);
1679 	return ERR_PTR(rc);
1680 }
1681 
1682 static ssize_t create_pmem_region_show(struct device *dev,
1683 				       struct device_attribute *attr, char *buf)
1684 {
1685 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
1686 
1687 	return sysfs_emit(buf, "region%u\n", atomic_read(&cxlrd->region_id));
1688 }
1689 
1690 static ssize_t create_pmem_region_store(struct device *dev,
1691 					struct device_attribute *attr,
1692 					const char *buf, size_t len)
1693 {
1694 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
1695 	struct cxl_region *cxlr;
1696 	int id, rc;
1697 
1698 	rc = sscanf(buf, "region%d\n", &id);
1699 	if (rc != 1)
1700 		return -EINVAL;
1701 
1702 	rc = memregion_alloc(GFP_KERNEL);
1703 	if (rc < 0)
1704 		return rc;
1705 
1706 	if (atomic_cmpxchg(&cxlrd->region_id, id, rc) != id) {
1707 		memregion_free(rc);
1708 		return -EBUSY;
1709 	}
1710 
1711 	cxlr = devm_cxl_add_region(cxlrd, id, CXL_DECODER_PMEM,
1712 				   CXL_DECODER_EXPANDER);
1713 	if (IS_ERR(cxlr))
1714 		return PTR_ERR(cxlr);
1715 
1716 	return len;
1717 }
1718 DEVICE_ATTR_RW(create_pmem_region);
1719 
1720 static ssize_t region_show(struct device *dev, struct device_attribute *attr,
1721 			   char *buf)
1722 {
1723 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
1724 	ssize_t rc;
1725 
1726 	rc = down_read_interruptible(&cxl_region_rwsem);
1727 	if (rc)
1728 		return rc;
1729 
1730 	if (cxld->region)
1731 		rc = sysfs_emit(buf, "%s\n", dev_name(&cxld->region->dev));
1732 	else
1733 		rc = sysfs_emit(buf, "\n");
1734 	up_read(&cxl_region_rwsem);
1735 
1736 	return rc;
1737 }
1738 DEVICE_ATTR_RO(region);
1739 
1740 static struct cxl_region *
1741 cxl_find_region_by_name(struct cxl_root_decoder *cxlrd, const char *name)
1742 {
1743 	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
1744 	struct device *region_dev;
1745 
1746 	region_dev = device_find_child_by_name(&cxld->dev, name);
1747 	if (!region_dev)
1748 		return ERR_PTR(-ENODEV);
1749 
1750 	return to_cxl_region(region_dev);
1751 }
1752 
1753 static ssize_t delete_region_store(struct device *dev,
1754 				   struct device_attribute *attr,
1755 				   const char *buf, size_t len)
1756 {
1757 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
1758 	struct cxl_port *port = to_cxl_port(dev->parent);
1759 	struct cxl_region *cxlr;
1760 
1761 	cxlr = cxl_find_region_by_name(cxlrd, buf);
1762 	if (IS_ERR(cxlr))
1763 		return PTR_ERR(cxlr);
1764 
1765 	devm_release_action(port->uport, unregister_region, cxlr);
1766 	put_device(&cxlr->dev);
1767 
1768 	return len;
1769 }
1770 DEVICE_ATTR_WO(delete_region);
1771 
1772 static void cxl_pmem_region_release(struct device *dev)
1773 {
1774 	struct cxl_pmem_region *cxlr_pmem = to_cxl_pmem_region(dev);
1775 	int i;
1776 
1777 	for (i = 0; i < cxlr_pmem->nr_mappings; i++) {
1778 		struct cxl_memdev *cxlmd = cxlr_pmem->mapping[i].cxlmd;
1779 
1780 		put_device(&cxlmd->dev);
1781 	}
1782 
1783 	kfree(cxlr_pmem);
1784 }
1785 
1786 static const struct attribute_group *cxl_pmem_region_attribute_groups[] = {
1787 	&cxl_base_attribute_group,
1788 	NULL,
1789 };
1790 
1791 const struct device_type cxl_pmem_region_type = {
1792 	.name = "cxl_pmem_region",
1793 	.release = cxl_pmem_region_release,
1794 	.groups = cxl_pmem_region_attribute_groups,
1795 };
1796 
1797 bool is_cxl_pmem_region(struct device *dev)
1798 {
1799 	return dev->type == &cxl_pmem_region_type;
1800 }
1801 EXPORT_SYMBOL_NS_GPL(is_cxl_pmem_region, CXL);
1802 
1803 struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev)
1804 {
1805 	if (dev_WARN_ONCE(dev, !is_cxl_pmem_region(dev),
1806 			  "not a cxl_pmem_region device\n"))
1807 		return NULL;
1808 	return container_of(dev, struct cxl_pmem_region, dev);
1809 }
1810 EXPORT_SYMBOL_NS_GPL(to_cxl_pmem_region, CXL);
1811 
1812 static struct lock_class_key cxl_pmem_region_key;
1813 
1814 static struct cxl_pmem_region *cxl_pmem_region_alloc(struct cxl_region *cxlr)
1815 {
1816 	struct cxl_region_params *p = &cxlr->params;
1817 	struct cxl_nvdimm_bridge *cxl_nvb;
1818 	struct cxl_pmem_region *cxlr_pmem;
1819 	struct device *dev;
1820 	int i;
1821 
1822 	down_read(&cxl_region_rwsem);
1823 	if (p->state != CXL_CONFIG_COMMIT) {
1824 		cxlr_pmem = ERR_PTR(-ENXIO);
1825 		goto out;
1826 	}
1827 
1828 	cxlr_pmem = kzalloc(struct_size(cxlr_pmem, mapping, p->nr_targets),
1829 			    GFP_KERNEL);
1830 	if (!cxlr_pmem) {
1831 		cxlr_pmem = ERR_PTR(-ENOMEM);
1832 		goto out;
1833 	}
1834 
1835 	cxlr_pmem->hpa_range.start = p->res->start;
1836 	cxlr_pmem->hpa_range.end = p->res->end;
1837 
1838 	/* Snapshot the region configuration underneath the cxl_region_rwsem */
1839 	cxlr_pmem->nr_mappings = p->nr_targets;
1840 	for (i = 0; i < p->nr_targets; i++) {
1841 		struct cxl_endpoint_decoder *cxled = p->targets[i];
1842 		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1843 		struct cxl_pmem_region_mapping *m = &cxlr_pmem->mapping[i];
1844 
1845 		/*
1846 		 * Regions never span CXL root devices, so by definition the
1847 		 * bridge for one device is the same for all.
1848 		 */
1849 		if (i == 0) {
1850 			cxl_nvb = cxl_find_nvdimm_bridge(&cxlmd->dev);
1851 			if (!cxl_nvb) {
1852 				cxlr_pmem = ERR_PTR(-ENODEV);
1853 				goto out;
1854 			}
1855 			cxlr->cxl_nvb = cxl_nvb;
1856 		}
1857 		m->cxlmd = cxlmd;
1858 		get_device(&cxlmd->dev);
1859 		m->start = cxled->dpa_res->start;
1860 		m->size = resource_size(cxled->dpa_res);
1861 		m->position = i;
1862 	}
1863 
1864 	dev = &cxlr_pmem->dev;
1865 	cxlr_pmem->cxlr = cxlr;
1866 	cxlr->cxlr_pmem = cxlr_pmem;
1867 	device_initialize(dev);
1868 	lockdep_set_class(&dev->mutex, &cxl_pmem_region_key);
1869 	device_set_pm_not_required(dev);
1870 	dev->parent = &cxlr->dev;
1871 	dev->bus = &cxl_bus_type;
1872 	dev->type = &cxl_pmem_region_type;
1873 out:
1874 	up_read(&cxl_region_rwsem);
1875 
1876 	return cxlr_pmem;
1877 }
1878 
1879 static void cxlr_pmem_unregister(void *_cxlr_pmem)
1880 {
1881 	struct cxl_pmem_region *cxlr_pmem = _cxlr_pmem;
1882 	struct cxl_region *cxlr = cxlr_pmem->cxlr;
1883 	struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
1884 
1885 	/*
1886 	 * Either the bridge is in ->remove() context under the device_lock(),
1887 	 * or cxlr_release_nvdimm() is cancelling the bridge's release action
1888 	 * for @cxlr_pmem and doing it itself (while manually holding the bridge
1889 	 * lock).
1890 	 */
1891 	device_lock_assert(&cxl_nvb->dev);
1892 	cxlr->cxlr_pmem = NULL;
1893 	cxlr_pmem->cxlr = NULL;
1894 	device_unregister(&cxlr_pmem->dev);
1895 }
1896 
1897 static void cxlr_release_nvdimm(void *_cxlr)
1898 {
1899 	struct cxl_region *cxlr = _cxlr;
1900 	struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
1901 
1902 	device_lock(&cxl_nvb->dev);
1903 	if (cxlr->cxlr_pmem)
1904 		devm_release_action(&cxl_nvb->dev, cxlr_pmem_unregister,
1905 				    cxlr->cxlr_pmem);
1906 	device_unlock(&cxl_nvb->dev);
1907 	cxlr->cxl_nvb = NULL;
1908 	put_device(&cxl_nvb->dev);
1909 }
1910 
1911 /**
1912  * devm_cxl_add_pmem_region() - add a cxl_region-to-nd_region bridge
1913  * @cxlr: parent CXL region for this pmem region bridge device
1914  *
1915  * Return: 0 on success negative error code on failure.
1916  */
1917 static int devm_cxl_add_pmem_region(struct cxl_region *cxlr)
1918 {
1919 	struct cxl_pmem_region *cxlr_pmem;
1920 	struct cxl_nvdimm_bridge *cxl_nvb;
1921 	struct device *dev;
1922 	int rc;
1923 
1924 	cxlr_pmem = cxl_pmem_region_alloc(cxlr);
1925 	if (IS_ERR(cxlr_pmem))
1926 		return PTR_ERR(cxlr_pmem);
1927 	cxl_nvb = cxlr->cxl_nvb;
1928 
1929 	dev = &cxlr_pmem->dev;
1930 	rc = dev_set_name(dev, "pmem_region%d", cxlr->id);
1931 	if (rc)
1932 		goto err;
1933 
1934 	rc = device_add(dev);
1935 	if (rc)
1936 		goto err;
1937 
1938 	dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
1939 		dev_name(dev));
1940 
1941 	device_lock(&cxl_nvb->dev);
1942 	if (cxl_nvb->dev.driver)
1943 		rc = devm_add_action_or_reset(&cxl_nvb->dev,
1944 					      cxlr_pmem_unregister, cxlr_pmem);
1945 	else
1946 		rc = -ENXIO;
1947 	device_unlock(&cxl_nvb->dev);
1948 
1949 	if (rc)
1950 		goto err_bridge;
1951 
1952 	/* @cxlr carries a reference on @cxl_nvb until cxlr_release_nvdimm */
1953 	return devm_add_action_or_reset(&cxlr->dev, cxlr_release_nvdimm, cxlr);
1954 
1955 err:
1956 	put_device(dev);
1957 err_bridge:
1958 	put_device(&cxl_nvb->dev);
1959 	cxlr->cxl_nvb = NULL;
1960 	return rc;
1961 }
1962 
1963 static int cxl_region_invalidate_memregion(struct cxl_region *cxlr)
1964 {
1965 	if (!test_bit(CXL_REGION_F_INCOHERENT, &cxlr->flags))
1966 		return 0;
1967 
1968 	if (!cpu_cache_has_invalidate_memregion()) {
1969 		if (IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST)) {
1970 			dev_warn(
1971 				&cxlr->dev,
1972 				"Bypassing cpu_cache_invalidate_memregion() for testing!\n");
1973 			clear_bit(CXL_REGION_F_INCOHERENT, &cxlr->flags);
1974 			return 0;
1975 		} else {
1976 			dev_err(&cxlr->dev,
1977 				"Failed to synchronize CPU cache state\n");
1978 			return -ENXIO;
1979 		}
1980 	}
1981 
1982 	cpu_cache_invalidate_memregion(IORES_DESC_CXL);
1983 	clear_bit(CXL_REGION_F_INCOHERENT, &cxlr->flags);
1984 	return 0;
1985 }
1986 
1987 static int cxl_region_probe(struct device *dev)
1988 {
1989 	struct cxl_region *cxlr = to_cxl_region(dev);
1990 	struct cxl_region_params *p = &cxlr->params;
1991 	int rc;
1992 
1993 	rc = down_read_interruptible(&cxl_region_rwsem);
1994 	if (rc) {
1995 		dev_dbg(&cxlr->dev, "probe interrupted\n");
1996 		return rc;
1997 	}
1998 
1999 	if (p->state < CXL_CONFIG_COMMIT) {
2000 		dev_dbg(&cxlr->dev, "config state: %d\n", p->state);
2001 		rc = -ENXIO;
2002 		goto out;
2003 	}
2004 
2005 	rc = cxl_region_invalidate_memregion(cxlr);
2006 
2007 	/*
2008 	 * From this point on any path that changes the region's state away from
2009 	 * CXL_CONFIG_COMMIT is also responsible for releasing the driver.
2010 	 */
2011 out:
2012 	up_read(&cxl_region_rwsem);
2013 
2014 	if (rc)
2015 		return rc;
2016 
2017 	switch (cxlr->mode) {
2018 	case CXL_DECODER_PMEM:
2019 		return devm_cxl_add_pmem_region(cxlr);
2020 	default:
2021 		dev_dbg(&cxlr->dev, "unsupported region mode: %d\n",
2022 			cxlr->mode);
2023 		return -ENXIO;
2024 	}
2025 }
2026 
2027 static struct cxl_driver cxl_region_driver = {
2028 	.name = "cxl_region",
2029 	.probe = cxl_region_probe,
2030 	.id = CXL_DEVICE_REGION,
2031 };
2032 
2033 int cxl_region_init(void)
2034 {
2035 	return cxl_driver_register(&cxl_region_driver);
2036 }
2037 
2038 void cxl_region_exit(void)
2039 {
2040 	cxl_driver_unregister(&cxl_region_driver);
2041 }
2042 
2043 MODULE_IMPORT_NS(CXL);
2044 MODULE_IMPORT_NS(DEVMEM);
2045 MODULE_ALIAS_CXL(CXL_DEVICE_REGION);
2046