xref: /openbmc/linux/drivers/cxl/core/region.c (revision 586b4106)
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>
11 #include <cxlmem.h>
12 #include <cxl.h>
13 #include "core.h"
14 
15 /**
16  * DOC: cxl core region
17  *
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.
23  *
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
27  * 2. Interleave size
28  * 3. Decoder targets
29  */
30 
31 static struct cxl_region *to_cxl_region(struct device *dev);
32 
33 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
34 			 char *buf)
35 {
36 	struct cxl_region *cxlr = to_cxl_region(dev);
37 	struct cxl_region_params *p = &cxlr->params;
38 	ssize_t rc;
39 
40 	rc = down_read_interruptible(&cxl_region_rwsem);
41 	if (rc)
42 		return rc;
43 	if (cxlr->mode != CXL_DECODER_PMEM)
44 		rc = sysfs_emit(buf, "\n");
45 	else
46 		rc = sysfs_emit(buf, "%pUb\n", &p->uuid);
47 	up_read(&cxl_region_rwsem);
48 
49 	return rc;
50 }
51 
52 static int is_dup(struct device *match, void *data)
53 {
54 	struct cxl_region_params *p;
55 	struct cxl_region *cxlr;
56 	uuid_t *uuid = data;
57 
58 	if (!is_cxl_region(match))
59 		return 0;
60 
61 	lockdep_assert_held(&cxl_region_rwsem);
62 	cxlr = to_cxl_region(match);
63 	p = &cxlr->params;
64 
65 	if (uuid_equal(&p->uuid, uuid)) {
66 		dev_dbg(match, "already has uuid: %pUb\n", uuid);
67 		return -EBUSY;
68 	}
69 
70 	return 0;
71 }
72 
73 static ssize_t uuid_store(struct device *dev, struct device_attribute *attr,
74 			  const char *buf, size_t len)
75 {
76 	struct cxl_region *cxlr = to_cxl_region(dev);
77 	struct cxl_region_params *p = &cxlr->params;
78 	uuid_t temp;
79 	ssize_t rc;
80 
81 	if (len != UUID_STRING_LEN + 1)
82 		return -EINVAL;
83 
84 	rc = uuid_parse(buf, &temp);
85 	if (rc)
86 		return rc;
87 
88 	if (uuid_is_null(&temp))
89 		return -EINVAL;
90 
91 	rc = down_write_killable(&cxl_region_rwsem);
92 	if (rc)
93 		return rc;
94 
95 	if (uuid_equal(&p->uuid, &temp))
96 		goto out;
97 
98 	rc = -EBUSY;
99 	if (p->state >= CXL_CONFIG_ACTIVE)
100 		goto out;
101 
102 	rc = bus_for_each_dev(&cxl_bus_type, NULL, &temp, is_dup);
103 	if (rc < 0)
104 		goto out;
105 
106 	uuid_copy(&p->uuid, &temp);
107 out:
108 	up_write(&cxl_region_rwsem);
109 
110 	if (rc)
111 		return rc;
112 	return len;
113 }
114 static DEVICE_ATTR_RW(uuid);
115 
116 static struct cxl_region_ref *cxl_rr_load(struct cxl_port *port,
117 					  struct cxl_region *cxlr)
118 {
119 	return xa_load(&port->regions, (unsigned long)cxlr);
120 }
121 
122 static int cxl_region_invalidate_memregion(struct cxl_region *cxlr)
123 {
124 	if (!cpu_cache_has_invalidate_memregion()) {
125 		if (IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST)) {
126 			dev_warn_once(
127 				&cxlr->dev,
128 				"Bypassing cpu_cache_invalidate_memregion() for testing!\n");
129 			return 0;
130 		} else {
131 			dev_err(&cxlr->dev,
132 				"Failed to synchronize CPU cache state\n");
133 			return -ENXIO;
134 		}
135 	}
136 
137 	cpu_cache_invalidate_memregion(IORES_DESC_CXL);
138 	return 0;
139 }
140 
141 static int cxl_region_decode_reset(struct cxl_region *cxlr, int count)
142 {
143 	struct cxl_region_params *p = &cxlr->params;
144 	int i, rc = 0;
145 
146 	/*
147 	 * Before region teardown attempt to flush, and if the flush
148 	 * fails cancel the region teardown for data consistency
149 	 * concerns
150 	 */
151 	rc = cxl_region_invalidate_memregion(cxlr);
152 	if (rc)
153 		return rc;
154 
155 	for (i = count - 1; i >= 0; i--) {
156 		struct cxl_endpoint_decoder *cxled = p->targets[i];
157 		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
158 		struct cxl_port *iter = cxled_to_port(cxled);
159 		struct cxl_dev_state *cxlds = cxlmd->cxlds;
160 		struct cxl_ep *ep;
161 
162 		if (cxlds->rcd)
163 			goto endpoint_reset;
164 
165 		while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
166 			iter = to_cxl_port(iter->dev.parent);
167 
168 		for (ep = cxl_ep_load(iter, cxlmd); iter;
169 		     iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
170 			struct cxl_region_ref *cxl_rr;
171 			struct cxl_decoder *cxld;
172 
173 			cxl_rr = cxl_rr_load(iter, cxlr);
174 			cxld = cxl_rr->decoder;
175 			if (cxld->reset)
176 				rc = cxld->reset(cxld);
177 			if (rc)
178 				return rc;
179 			set_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
180 		}
181 
182 endpoint_reset:
183 		rc = cxled->cxld.reset(&cxled->cxld);
184 		if (rc)
185 			return rc;
186 		set_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
187 	}
188 
189 	/* all decoders associated with this region have been torn down */
190 	clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
191 
192 	return 0;
193 }
194 
195 static int commit_decoder(struct cxl_decoder *cxld)
196 {
197 	struct cxl_switch_decoder *cxlsd = NULL;
198 
199 	if (cxld->commit)
200 		return cxld->commit(cxld);
201 
202 	if (is_switch_decoder(&cxld->dev))
203 		cxlsd = to_cxl_switch_decoder(&cxld->dev);
204 
205 	if (dev_WARN_ONCE(&cxld->dev, !cxlsd || cxlsd->nr_targets > 1,
206 			  "->commit() is required\n"))
207 		return -ENXIO;
208 	return 0;
209 }
210 
211 static int cxl_region_decode_commit(struct cxl_region *cxlr)
212 {
213 	struct cxl_region_params *p = &cxlr->params;
214 	int i, rc = 0;
215 
216 	for (i = 0; i < p->nr_targets; i++) {
217 		struct cxl_endpoint_decoder *cxled = p->targets[i];
218 		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
219 		struct cxl_region_ref *cxl_rr;
220 		struct cxl_decoder *cxld;
221 		struct cxl_port *iter;
222 		struct cxl_ep *ep;
223 
224 		/* commit bottom up */
225 		for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
226 		     iter = to_cxl_port(iter->dev.parent)) {
227 			cxl_rr = cxl_rr_load(iter, cxlr);
228 			cxld = cxl_rr->decoder;
229 			rc = commit_decoder(cxld);
230 			if (rc)
231 				break;
232 		}
233 
234 		if (rc) {
235 			/* programming @iter failed, teardown */
236 			for (ep = cxl_ep_load(iter, cxlmd); ep && iter;
237 			     iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
238 				cxl_rr = cxl_rr_load(iter, cxlr);
239 				cxld = cxl_rr->decoder;
240 				if (cxld->reset)
241 					cxld->reset(cxld);
242 			}
243 
244 			cxled->cxld.reset(&cxled->cxld);
245 			goto err;
246 		}
247 	}
248 
249 	return 0;
250 
251 err:
252 	/* undo the targets that were successfully committed */
253 	cxl_region_decode_reset(cxlr, i);
254 	return rc;
255 }
256 
257 static ssize_t commit_store(struct device *dev, struct device_attribute *attr,
258 			    const char *buf, size_t len)
259 {
260 	struct cxl_region *cxlr = to_cxl_region(dev);
261 	struct cxl_region_params *p = &cxlr->params;
262 	bool commit;
263 	ssize_t rc;
264 
265 	rc = kstrtobool(buf, &commit);
266 	if (rc)
267 		return rc;
268 
269 	rc = down_write_killable(&cxl_region_rwsem);
270 	if (rc)
271 		return rc;
272 
273 	/* Already in the requested state? */
274 	if (commit && p->state >= CXL_CONFIG_COMMIT)
275 		goto out;
276 	if (!commit && p->state < CXL_CONFIG_COMMIT)
277 		goto out;
278 
279 	/* Not ready to commit? */
280 	if (commit && p->state < CXL_CONFIG_ACTIVE) {
281 		rc = -ENXIO;
282 		goto out;
283 	}
284 
285 	/*
286 	 * Invalidate caches before region setup to drop any speculative
287 	 * consumption of this address space
288 	 */
289 	rc = cxl_region_invalidate_memregion(cxlr);
290 	if (rc)
291 		goto out;
292 
293 	if (commit) {
294 		rc = cxl_region_decode_commit(cxlr);
295 		if (rc == 0)
296 			p->state = CXL_CONFIG_COMMIT;
297 	} else {
298 		p->state = CXL_CONFIG_RESET_PENDING;
299 		up_write(&cxl_region_rwsem);
300 		device_release_driver(&cxlr->dev);
301 		down_write(&cxl_region_rwsem);
302 
303 		/*
304 		 * The lock was dropped, so need to revalidate that the reset is
305 		 * still pending.
306 		 */
307 		if (p->state == CXL_CONFIG_RESET_PENDING) {
308 			rc = cxl_region_decode_reset(cxlr, p->interleave_ways);
309 			/*
310 			 * Revert to committed since there may still be active
311 			 * decoders associated with this region, or move forward
312 			 * to active to mark the reset successful
313 			 */
314 			if (rc)
315 				p->state = CXL_CONFIG_COMMIT;
316 			else
317 				p->state = CXL_CONFIG_ACTIVE;
318 		}
319 	}
320 
321 out:
322 	up_write(&cxl_region_rwsem);
323 
324 	if (rc)
325 		return rc;
326 	return len;
327 }
328 
329 static ssize_t commit_show(struct device *dev, struct device_attribute *attr,
330 			   char *buf)
331 {
332 	struct cxl_region *cxlr = to_cxl_region(dev);
333 	struct cxl_region_params *p = &cxlr->params;
334 	ssize_t rc;
335 
336 	rc = down_read_interruptible(&cxl_region_rwsem);
337 	if (rc)
338 		return rc;
339 	rc = sysfs_emit(buf, "%d\n", p->state >= CXL_CONFIG_COMMIT);
340 	up_read(&cxl_region_rwsem);
341 
342 	return rc;
343 }
344 static DEVICE_ATTR_RW(commit);
345 
346 static umode_t cxl_region_visible(struct kobject *kobj, struct attribute *a,
347 				  int n)
348 {
349 	struct device *dev = kobj_to_dev(kobj);
350 	struct cxl_region *cxlr = to_cxl_region(dev);
351 
352 	/*
353 	 * Support tooling that expects to find a 'uuid' attribute for all
354 	 * regions regardless of mode.
355 	 */
356 	if (a == &dev_attr_uuid.attr && cxlr->mode != CXL_DECODER_PMEM)
357 		return 0444;
358 	return a->mode;
359 }
360 
361 static ssize_t interleave_ways_show(struct device *dev,
362 				    struct device_attribute *attr, char *buf)
363 {
364 	struct cxl_region *cxlr = to_cxl_region(dev);
365 	struct cxl_region_params *p = &cxlr->params;
366 	ssize_t rc;
367 
368 	rc = down_read_interruptible(&cxl_region_rwsem);
369 	if (rc)
370 		return rc;
371 	rc = sysfs_emit(buf, "%d\n", p->interleave_ways);
372 	up_read(&cxl_region_rwsem);
373 
374 	return rc;
375 }
376 
377 static const struct attribute_group *get_cxl_region_target_group(void);
378 
379 static ssize_t interleave_ways_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 	unsigned int val, save;
388 	int rc;
389 	u8 iw;
390 
391 	rc = kstrtouint(buf, 0, &val);
392 	if (rc)
393 		return rc;
394 
395 	rc = ways_to_eiw(val, &iw);
396 	if (rc)
397 		return rc;
398 
399 	/*
400 	 * Even for x3, x6, and x12 interleaves the region interleave must be a
401 	 * power of 2 multiple of the host bridge interleave.
402 	 */
403 	if (!is_power_of_2(val / cxld->interleave_ways) ||
404 	    (val % cxld->interleave_ways)) {
405 		dev_dbg(&cxlr->dev, "invalid interleave: %d\n", val);
406 		return -EINVAL;
407 	}
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 	save = p->interleave_ways;
418 	p->interleave_ways = val;
419 	rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
420 	if (rc)
421 		p->interleave_ways = save;
422 out:
423 	up_write(&cxl_region_rwsem);
424 	if (rc)
425 		return rc;
426 	return len;
427 }
428 static DEVICE_ATTR_RW(interleave_ways);
429 
430 static ssize_t interleave_granularity_show(struct device *dev,
431 					   struct device_attribute *attr,
432 					   char *buf)
433 {
434 	struct cxl_region *cxlr = to_cxl_region(dev);
435 	struct cxl_region_params *p = &cxlr->params;
436 	ssize_t rc;
437 
438 	rc = down_read_interruptible(&cxl_region_rwsem);
439 	if (rc)
440 		return rc;
441 	rc = sysfs_emit(buf, "%d\n", p->interleave_granularity);
442 	up_read(&cxl_region_rwsem);
443 
444 	return rc;
445 }
446 
447 static ssize_t interleave_granularity_store(struct device *dev,
448 					    struct device_attribute *attr,
449 					    const char *buf, size_t len)
450 {
451 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
452 	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
453 	struct cxl_region *cxlr = to_cxl_region(dev);
454 	struct cxl_region_params *p = &cxlr->params;
455 	int rc, val;
456 	u16 ig;
457 
458 	rc = kstrtoint(buf, 0, &val);
459 	if (rc)
460 		return rc;
461 
462 	rc = granularity_to_eig(val, &ig);
463 	if (rc)
464 		return rc;
465 
466 	/*
467 	 * When the host-bridge is interleaved, disallow region granularity !=
468 	 * root granularity. Regions with a granularity less than the root
469 	 * interleave result in needing multiple endpoints to support a single
470 	 * slot in the interleave (possible to support in the future). Regions
471 	 * with a granularity greater than the root interleave result in invalid
472 	 * DPA translations (invalid to support).
473 	 */
474 	if (cxld->interleave_ways > 1 && val != cxld->interleave_granularity)
475 		return -EINVAL;
476 
477 	rc = down_write_killable(&cxl_region_rwsem);
478 	if (rc)
479 		return rc;
480 	if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
481 		rc = -EBUSY;
482 		goto out;
483 	}
484 
485 	p->interleave_granularity = val;
486 out:
487 	up_write(&cxl_region_rwsem);
488 	if (rc)
489 		return rc;
490 	return len;
491 }
492 static DEVICE_ATTR_RW(interleave_granularity);
493 
494 static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
495 			     char *buf)
496 {
497 	struct cxl_region *cxlr = to_cxl_region(dev);
498 	struct cxl_region_params *p = &cxlr->params;
499 	u64 resource = -1ULL;
500 	ssize_t rc;
501 
502 	rc = down_read_interruptible(&cxl_region_rwsem);
503 	if (rc)
504 		return rc;
505 	if (p->res)
506 		resource = p->res->start;
507 	rc = sysfs_emit(buf, "%#llx\n", resource);
508 	up_read(&cxl_region_rwsem);
509 
510 	return rc;
511 }
512 static DEVICE_ATTR_RO(resource);
513 
514 static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
515 			 char *buf)
516 {
517 	struct cxl_region *cxlr = to_cxl_region(dev);
518 
519 	return sysfs_emit(buf, "%s\n", cxl_decoder_mode_name(cxlr->mode));
520 }
521 static DEVICE_ATTR_RO(mode);
522 
523 static int alloc_hpa(struct cxl_region *cxlr, resource_size_t size)
524 {
525 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
526 	struct cxl_region_params *p = &cxlr->params;
527 	struct resource *res;
528 	u64 remainder = 0;
529 
530 	lockdep_assert_held_write(&cxl_region_rwsem);
531 
532 	/* Nothing to do... */
533 	if (p->res && resource_size(p->res) == size)
534 		return 0;
535 
536 	/* To change size the old size must be freed first */
537 	if (p->res)
538 		return -EBUSY;
539 
540 	if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE)
541 		return -EBUSY;
542 
543 	/* ways, granularity and uuid (if PMEM) need to be set before HPA */
544 	if (!p->interleave_ways || !p->interleave_granularity ||
545 	    (cxlr->mode == CXL_DECODER_PMEM && uuid_is_null(&p->uuid)))
546 		return -ENXIO;
547 
548 	div64_u64_rem(size, (u64)SZ_256M * p->interleave_ways, &remainder);
549 	if (remainder)
550 		return -EINVAL;
551 
552 	res = alloc_free_mem_region(cxlrd->res, size, SZ_256M,
553 				    dev_name(&cxlr->dev));
554 	if (IS_ERR(res)) {
555 		dev_dbg(&cxlr->dev, "failed to allocate HPA: %ld\n",
556 			PTR_ERR(res));
557 		return PTR_ERR(res);
558 	}
559 
560 	p->res = res;
561 	p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
562 
563 	return 0;
564 }
565 
566 static void cxl_region_iomem_release(struct cxl_region *cxlr)
567 {
568 	struct cxl_region_params *p = &cxlr->params;
569 
570 	if (device_is_registered(&cxlr->dev))
571 		lockdep_assert_held_write(&cxl_region_rwsem);
572 	if (p->res) {
573 		/*
574 		 * Autodiscovered regions may not have been able to insert their
575 		 * resource.
576 		 */
577 		if (p->res->parent)
578 			remove_resource(p->res);
579 		kfree(p->res);
580 		p->res = NULL;
581 	}
582 }
583 
584 static int free_hpa(struct cxl_region *cxlr)
585 {
586 	struct cxl_region_params *p = &cxlr->params;
587 
588 	lockdep_assert_held_write(&cxl_region_rwsem);
589 
590 	if (!p->res)
591 		return 0;
592 
593 	if (p->state >= CXL_CONFIG_ACTIVE)
594 		return -EBUSY;
595 
596 	cxl_region_iomem_release(cxlr);
597 	p->state = CXL_CONFIG_IDLE;
598 	return 0;
599 }
600 
601 static ssize_t size_store(struct device *dev, struct device_attribute *attr,
602 			  const char *buf, size_t len)
603 {
604 	struct cxl_region *cxlr = to_cxl_region(dev);
605 	u64 val;
606 	int rc;
607 
608 	rc = kstrtou64(buf, 0, &val);
609 	if (rc)
610 		return rc;
611 
612 	rc = down_write_killable(&cxl_region_rwsem);
613 	if (rc)
614 		return rc;
615 
616 	if (val)
617 		rc = alloc_hpa(cxlr, val);
618 	else
619 		rc = free_hpa(cxlr);
620 	up_write(&cxl_region_rwsem);
621 
622 	if (rc)
623 		return rc;
624 
625 	return len;
626 }
627 
628 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
629 			 char *buf)
630 {
631 	struct cxl_region *cxlr = to_cxl_region(dev);
632 	struct cxl_region_params *p = &cxlr->params;
633 	u64 size = 0;
634 	ssize_t rc;
635 
636 	rc = down_read_interruptible(&cxl_region_rwsem);
637 	if (rc)
638 		return rc;
639 	if (p->res)
640 		size = resource_size(p->res);
641 	rc = sysfs_emit(buf, "%#llx\n", size);
642 	up_read(&cxl_region_rwsem);
643 
644 	return rc;
645 }
646 static DEVICE_ATTR_RW(size);
647 
648 static struct attribute *cxl_region_attrs[] = {
649 	&dev_attr_uuid.attr,
650 	&dev_attr_commit.attr,
651 	&dev_attr_interleave_ways.attr,
652 	&dev_attr_interleave_granularity.attr,
653 	&dev_attr_resource.attr,
654 	&dev_attr_size.attr,
655 	&dev_attr_mode.attr,
656 	NULL,
657 };
658 
659 static const struct attribute_group cxl_region_group = {
660 	.attrs = cxl_region_attrs,
661 	.is_visible = cxl_region_visible,
662 };
663 
664 static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos)
665 {
666 	struct cxl_region_params *p = &cxlr->params;
667 	struct cxl_endpoint_decoder *cxled;
668 	int rc;
669 
670 	rc = down_read_interruptible(&cxl_region_rwsem);
671 	if (rc)
672 		return rc;
673 
674 	if (pos >= p->interleave_ways) {
675 		dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
676 			p->interleave_ways);
677 		rc = -ENXIO;
678 		goto out;
679 	}
680 
681 	cxled = p->targets[pos];
682 	if (!cxled)
683 		rc = sysfs_emit(buf, "\n");
684 	else
685 		rc = sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev));
686 out:
687 	up_read(&cxl_region_rwsem);
688 
689 	return rc;
690 }
691 
692 static int match_free_decoder(struct device *dev, void *data)
693 {
694 	struct cxl_decoder *cxld;
695 	int *id = data;
696 
697 	if (!is_switch_decoder(dev))
698 		return 0;
699 
700 	cxld = to_cxl_decoder(dev);
701 
702 	/* enforce ordered allocation */
703 	if (cxld->id != *id)
704 		return 0;
705 
706 	if (!cxld->region)
707 		return 1;
708 
709 	(*id)++;
710 
711 	return 0;
712 }
713 
714 static int match_auto_decoder(struct device *dev, void *data)
715 {
716 	struct cxl_region_params *p = data;
717 	struct cxl_decoder *cxld;
718 	struct range *r;
719 
720 	if (!is_switch_decoder(dev))
721 		return 0;
722 
723 	cxld = to_cxl_decoder(dev);
724 	r = &cxld->hpa_range;
725 
726 	if (p->res && p->res->start == r->start && p->res->end == r->end)
727 		return 1;
728 
729 	return 0;
730 }
731 
732 static struct cxl_decoder *
733 cxl_region_find_decoder(struct cxl_port *port,
734 			struct cxl_endpoint_decoder *cxled,
735 			struct cxl_region *cxlr)
736 {
737 	struct device *dev;
738 	int id = 0;
739 
740 	if (port == cxled_to_port(cxled))
741 		return &cxled->cxld;
742 
743 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags))
744 		dev = device_find_child(&port->dev, &cxlr->params,
745 					match_auto_decoder);
746 	else
747 		dev = device_find_child(&port->dev, &id, match_free_decoder);
748 	if (!dev)
749 		return NULL;
750 	/*
751 	 * This decoder is pinned registered as long as the endpoint decoder is
752 	 * registered, and endpoint decoder unregistration holds the
753 	 * cxl_region_rwsem over unregister events, so no need to hold on to
754 	 * this extra reference.
755 	 */
756 	put_device(dev);
757 	return to_cxl_decoder(dev);
758 }
759 
760 static bool auto_order_ok(struct cxl_port *port, struct cxl_region *cxlr_iter,
761 			  struct cxl_decoder *cxld)
762 {
763 	struct cxl_region_ref *rr = cxl_rr_load(port, cxlr_iter);
764 	struct cxl_decoder *cxld_iter = rr->decoder;
765 
766 	/*
767 	 * Allow the out of order assembly of auto-discovered regions.
768 	 * Per CXL Spec 3.1 8.2.4.20.12 software must commit decoders
769 	 * in HPA order. Confirm that the decoder with the lesser HPA
770 	 * starting address has the lesser id.
771 	 */
772 	dev_dbg(&cxld->dev, "check for HPA violation %s:%d < %s:%d\n",
773 		dev_name(&cxld->dev), cxld->id,
774 		dev_name(&cxld_iter->dev), cxld_iter->id);
775 
776 	if (cxld_iter->id > cxld->id)
777 		return true;
778 
779 	return false;
780 }
781 
782 static struct cxl_region_ref *
783 alloc_region_ref(struct cxl_port *port, struct cxl_region *cxlr,
784 		 struct cxl_endpoint_decoder *cxled)
785 {
786 	struct cxl_region_params *p = &cxlr->params;
787 	struct cxl_region_ref *cxl_rr, *iter;
788 	unsigned long index;
789 	int rc;
790 
791 	xa_for_each(&port->regions, index, iter) {
792 		struct cxl_region_params *ip = &iter->region->params;
793 
794 		if (!ip->res || ip->res->start < p->res->start)
795 			continue;
796 
797 		if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
798 			struct cxl_decoder *cxld;
799 
800 			cxld = cxl_region_find_decoder(port, cxled, cxlr);
801 			if (auto_order_ok(port, iter->region, cxld))
802 				continue;
803 		}
804 		dev_dbg(&cxlr->dev, "%s: HPA order violation %s:%pr vs %pr\n",
805 			dev_name(&port->dev),
806 			dev_name(&iter->region->dev), ip->res, p->res);
807 
808 		return ERR_PTR(-EBUSY);
809 	}
810 
811 	cxl_rr = kzalloc(sizeof(*cxl_rr), GFP_KERNEL);
812 	if (!cxl_rr)
813 		return ERR_PTR(-ENOMEM);
814 	cxl_rr->port = port;
815 	cxl_rr->region = cxlr;
816 	cxl_rr->nr_targets = 1;
817 	xa_init(&cxl_rr->endpoints);
818 
819 	rc = xa_insert(&port->regions, (unsigned long)cxlr, cxl_rr, GFP_KERNEL);
820 	if (rc) {
821 		dev_dbg(&cxlr->dev,
822 			"%s: failed to track region reference: %d\n",
823 			dev_name(&port->dev), rc);
824 		kfree(cxl_rr);
825 		return ERR_PTR(rc);
826 	}
827 
828 	return cxl_rr;
829 }
830 
831 static void cxl_rr_free_decoder(struct cxl_region_ref *cxl_rr)
832 {
833 	struct cxl_region *cxlr = cxl_rr->region;
834 	struct cxl_decoder *cxld = cxl_rr->decoder;
835 
836 	if (!cxld)
837 		return;
838 
839 	dev_WARN_ONCE(&cxlr->dev, cxld->region != cxlr, "region mismatch\n");
840 	if (cxld->region == cxlr) {
841 		cxld->region = NULL;
842 		put_device(&cxlr->dev);
843 	}
844 }
845 
846 static void free_region_ref(struct cxl_region_ref *cxl_rr)
847 {
848 	struct cxl_port *port = cxl_rr->port;
849 	struct cxl_region *cxlr = cxl_rr->region;
850 
851 	cxl_rr_free_decoder(cxl_rr);
852 	xa_erase(&port->regions, (unsigned long)cxlr);
853 	xa_destroy(&cxl_rr->endpoints);
854 	kfree(cxl_rr);
855 }
856 
857 static int cxl_rr_ep_add(struct cxl_region_ref *cxl_rr,
858 			 struct cxl_endpoint_decoder *cxled)
859 {
860 	int rc;
861 	struct cxl_port *port = cxl_rr->port;
862 	struct cxl_region *cxlr = cxl_rr->region;
863 	struct cxl_decoder *cxld = cxl_rr->decoder;
864 	struct cxl_ep *ep = cxl_ep_load(port, cxled_to_memdev(cxled));
865 
866 	if (ep) {
867 		rc = xa_insert(&cxl_rr->endpoints, (unsigned long)cxled, ep,
868 			       GFP_KERNEL);
869 		if (rc)
870 			return rc;
871 	}
872 	cxl_rr->nr_eps++;
873 
874 	if (!cxld->region) {
875 		cxld->region = cxlr;
876 		get_device(&cxlr->dev);
877 	}
878 
879 	return 0;
880 }
881 
882 static int cxl_rr_alloc_decoder(struct cxl_port *port, struct cxl_region *cxlr,
883 				struct cxl_endpoint_decoder *cxled,
884 				struct cxl_region_ref *cxl_rr)
885 {
886 	struct cxl_decoder *cxld;
887 
888 	cxld = cxl_region_find_decoder(port, cxled, cxlr);
889 	if (!cxld) {
890 		dev_dbg(&cxlr->dev, "%s: no decoder available\n",
891 			dev_name(&port->dev));
892 		return -EBUSY;
893 	}
894 
895 	if (cxld->region) {
896 		dev_dbg(&cxlr->dev, "%s: %s already attached to %s\n",
897 			dev_name(&port->dev), dev_name(&cxld->dev),
898 			dev_name(&cxld->region->dev));
899 		return -EBUSY;
900 	}
901 
902 	/*
903 	 * Endpoints should already match the region type, but backstop that
904 	 * assumption with an assertion. Switch-decoders change mapping-type
905 	 * based on what is mapped when they are assigned to a region.
906 	 */
907 	dev_WARN_ONCE(&cxlr->dev,
908 		      port == cxled_to_port(cxled) &&
909 			      cxld->target_type != cxlr->type,
910 		      "%s:%s mismatch decoder type %d -> %d\n",
911 		      dev_name(&cxled_to_memdev(cxled)->dev),
912 		      dev_name(&cxld->dev), cxld->target_type, cxlr->type);
913 	cxld->target_type = cxlr->type;
914 	cxl_rr->decoder = cxld;
915 	return 0;
916 }
917 
918 /**
919  * cxl_port_attach_region() - track a region's interest in a port by endpoint
920  * @port: port to add a new region reference 'struct cxl_region_ref'
921  * @cxlr: region to attach to @port
922  * @cxled: endpoint decoder used to create or further pin a region reference
923  * @pos: interleave position of @cxled in @cxlr
924  *
925  * The attach event is an opportunity to validate CXL decode setup
926  * constraints and record metadata needed for programming HDM decoders,
927  * in particular decoder target lists.
928  *
929  * The steps are:
930  *
931  * - validate that there are no other regions with a higher HPA already
932  *   associated with @port
933  * - establish a region reference if one is not already present
934  *
935  *   - additionally allocate a decoder instance that will host @cxlr on
936  *     @port
937  *
938  * - pin the region reference by the endpoint
939  * - account for how many entries in @port's target list are needed to
940  *   cover all of the added endpoints.
941  */
942 static int cxl_port_attach_region(struct cxl_port *port,
943 				  struct cxl_region *cxlr,
944 				  struct cxl_endpoint_decoder *cxled, int pos)
945 {
946 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
947 	struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
948 	struct cxl_region_ref *cxl_rr;
949 	bool nr_targets_inc = false;
950 	struct cxl_decoder *cxld;
951 	unsigned long index;
952 	int rc = -EBUSY;
953 
954 	lockdep_assert_held_write(&cxl_region_rwsem);
955 
956 	cxl_rr = cxl_rr_load(port, cxlr);
957 	if (cxl_rr) {
958 		struct cxl_ep *ep_iter;
959 		int found = 0;
960 
961 		/*
962 		 * Walk the existing endpoints that have been attached to
963 		 * @cxlr at @port and see if they share the same 'next' port
964 		 * in the downstream direction. I.e. endpoints that share common
965 		 * upstream switch.
966 		 */
967 		xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
968 			if (ep_iter == ep)
969 				continue;
970 			if (ep_iter->next == ep->next) {
971 				found++;
972 				break;
973 			}
974 		}
975 
976 		/*
977 		 * New target port, or @port is an endpoint port that always
978 		 * accounts its own local decode as a target.
979 		 */
980 		if (!found || !ep->next) {
981 			cxl_rr->nr_targets++;
982 			nr_targets_inc = true;
983 		}
984 	} else {
985 		cxl_rr = alloc_region_ref(port, cxlr, cxled);
986 		if (IS_ERR(cxl_rr)) {
987 			dev_dbg(&cxlr->dev,
988 				"%s: failed to allocate region reference\n",
989 				dev_name(&port->dev));
990 			return PTR_ERR(cxl_rr);
991 		}
992 		nr_targets_inc = true;
993 
994 		rc = cxl_rr_alloc_decoder(port, cxlr, cxled, cxl_rr);
995 		if (rc)
996 			goto out_erase;
997 	}
998 	cxld = cxl_rr->decoder;
999 
1000 	/*
1001 	 * the number of targets should not exceed the target_count
1002 	 * of the decoder
1003 	 */
1004 	if (is_switch_decoder(&cxld->dev)) {
1005 		struct cxl_switch_decoder *cxlsd;
1006 
1007 		cxlsd = to_cxl_switch_decoder(&cxld->dev);
1008 		if (cxl_rr->nr_targets > cxlsd->nr_targets) {
1009 			dev_dbg(&cxlr->dev,
1010 				"%s:%s %s add: %s:%s @ %d overflows targets: %d\n",
1011 				dev_name(port->uport_dev), dev_name(&port->dev),
1012 				dev_name(&cxld->dev), dev_name(&cxlmd->dev),
1013 				dev_name(&cxled->cxld.dev), pos,
1014 				cxlsd->nr_targets);
1015 			rc = -ENXIO;
1016 			goto out_erase;
1017 		}
1018 	}
1019 
1020 	rc = cxl_rr_ep_add(cxl_rr, cxled);
1021 	if (rc) {
1022 		dev_dbg(&cxlr->dev,
1023 			"%s: failed to track endpoint %s:%s reference\n",
1024 			dev_name(&port->dev), dev_name(&cxlmd->dev),
1025 			dev_name(&cxld->dev));
1026 		goto out_erase;
1027 	}
1028 
1029 	dev_dbg(&cxlr->dev,
1030 		"%s:%s %s add: %s:%s @ %d next: %s nr_eps: %d nr_targets: %d\n",
1031 		dev_name(port->uport_dev), dev_name(&port->dev),
1032 		dev_name(&cxld->dev), dev_name(&cxlmd->dev),
1033 		dev_name(&cxled->cxld.dev), pos,
1034 		ep ? ep->next ? dev_name(ep->next->uport_dev) :
1035 				      dev_name(&cxlmd->dev) :
1036 			   "none",
1037 		cxl_rr->nr_eps, cxl_rr->nr_targets);
1038 
1039 	return 0;
1040 out_erase:
1041 	if (nr_targets_inc)
1042 		cxl_rr->nr_targets--;
1043 	if (cxl_rr->nr_eps == 0)
1044 		free_region_ref(cxl_rr);
1045 	return rc;
1046 }
1047 
1048 static void cxl_port_detach_region(struct cxl_port *port,
1049 				   struct cxl_region *cxlr,
1050 				   struct cxl_endpoint_decoder *cxled)
1051 {
1052 	struct cxl_region_ref *cxl_rr;
1053 	struct cxl_ep *ep = NULL;
1054 
1055 	lockdep_assert_held_write(&cxl_region_rwsem);
1056 
1057 	cxl_rr = cxl_rr_load(port, cxlr);
1058 	if (!cxl_rr)
1059 		return;
1060 
1061 	/*
1062 	 * Endpoint ports do not carry cxl_ep references, and they
1063 	 * never target more than one endpoint by definition
1064 	 */
1065 	if (cxl_rr->decoder == &cxled->cxld)
1066 		cxl_rr->nr_eps--;
1067 	else
1068 		ep = xa_erase(&cxl_rr->endpoints, (unsigned long)cxled);
1069 	if (ep) {
1070 		struct cxl_ep *ep_iter;
1071 		unsigned long index;
1072 		int found = 0;
1073 
1074 		cxl_rr->nr_eps--;
1075 		xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
1076 			if (ep_iter->next == ep->next) {
1077 				found++;
1078 				break;
1079 			}
1080 		}
1081 		if (!found)
1082 			cxl_rr->nr_targets--;
1083 	}
1084 
1085 	if (cxl_rr->nr_eps == 0)
1086 		free_region_ref(cxl_rr);
1087 }
1088 
1089 static int check_last_peer(struct cxl_endpoint_decoder *cxled,
1090 			   struct cxl_ep *ep, struct cxl_region_ref *cxl_rr,
1091 			   int distance)
1092 {
1093 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1094 	struct cxl_region *cxlr = cxl_rr->region;
1095 	struct cxl_region_params *p = &cxlr->params;
1096 	struct cxl_endpoint_decoder *cxled_peer;
1097 	struct cxl_port *port = cxl_rr->port;
1098 	struct cxl_memdev *cxlmd_peer;
1099 	struct cxl_ep *ep_peer;
1100 	int pos = cxled->pos;
1101 
1102 	/*
1103 	 * If this position wants to share a dport with the last endpoint mapped
1104 	 * then that endpoint, at index 'position - distance', must also be
1105 	 * mapped by this dport.
1106 	 */
1107 	if (pos < distance) {
1108 		dev_dbg(&cxlr->dev, "%s:%s: cannot host %s:%s at %d\n",
1109 			dev_name(port->uport_dev), dev_name(&port->dev),
1110 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1111 		return -ENXIO;
1112 	}
1113 	cxled_peer = p->targets[pos - distance];
1114 	cxlmd_peer = cxled_to_memdev(cxled_peer);
1115 	ep_peer = cxl_ep_load(port, cxlmd_peer);
1116 	if (ep->dport != ep_peer->dport) {
1117 		dev_dbg(&cxlr->dev,
1118 			"%s:%s: %s:%s pos %d mismatched peer %s:%s\n",
1119 			dev_name(port->uport_dev), dev_name(&port->dev),
1120 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos,
1121 			dev_name(&cxlmd_peer->dev),
1122 			dev_name(&cxled_peer->cxld.dev));
1123 		return -ENXIO;
1124 	}
1125 
1126 	return 0;
1127 }
1128 
1129 static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
1130 {
1131 	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
1132 	struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
1133 	unsigned int interleave_mask;
1134 	u8 eiw;
1135 	u16 eig;
1136 	int high_pos, low_pos;
1137 
1138 	if (!test_bit(iw, &cxlhdm->iw_cap_mask))
1139 		return -ENXIO;
1140 	/*
1141 	 * Per CXL specification r3.1(8.2.4.20.13 Decoder Protection),
1142 	 * if eiw < 8:
1143 	 *   DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + 8 + eiw]
1144 	 *   DPAOFFSET[eig + 7: 0]  = HPAOFFSET[eig + 7: 0]
1145 	 *
1146 	 *   when the eiw is 0, all the bits of HPAOFFSET[51: 0] are used, the
1147 	 *   interleave bits are none.
1148 	 *
1149 	 * if eiw >= 8:
1150 	 *   DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + eiw] / 3
1151 	 *   DPAOFFSET[eig + 7: 0]  = HPAOFFSET[eig + 7: 0]
1152 	 *
1153 	 *   when the eiw is 8, all the bits of HPAOFFSET[51: 0] are used, the
1154 	 *   interleave bits are none.
1155 	 */
1156 	ways_to_eiw(iw, &eiw);
1157 	if (eiw == 0 || eiw == 8)
1158 		return 0;
1159 
1160 	granularity_to_eig(ig, &eig);
1161 	if (eiw > 8)
1162 		high_pos = eiw + eig - 1;
1163 	else
1164 		high_pos = eiw + eig + 7;
1165 	low_pos = eig + 8;
1166 	interleave_mask = GENMASK(high_pos, low_pos);
1167 	if (interleave_mask & ~cxlhdm->interleave_mask)
1168 		return -ENXIO;
1169 
1170 	return 0;
1171 }
1172 
1173 static int cxl_port_setup_targets(struct cxl_port *port,
1174 				  struct cxl_region *cxlr,
1175 				  struct cxl_endpoint_decoder *cxled)
1176 {
1177 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
1178 	int parent_iw, parent_ig, ig, iw, rc, inc = 0, pos = cxled->pos;
1179 	struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
1180 	struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
1181 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1182 	struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
1183 	struct cxl_region_params *p = &cxlr->params;
1184 	struct cxl_decoder *cxld = cxl_rr->decoder;
1185 	struct cxl_switch_decoder *cxlsd;
1186 	u16 eig, peig;
1187 	u8 eiw, peiw;
1188 
1189 	/*
1190 	 * While root level decoders support x3, x6, x12, switch level
1191 	 * decoders only support powers of 2 up to x16.
1192 	 */
1193 	if (!is_power_of_2(cxl_rr->nr_targets)) {
1194 		dev_dbg(&cxlr->dev, "%s:%s: invalid target count %d\n",
1195 			dev_name(port->uport_dev), dev_name(&port->dev),
1196 			cxl_rr->nr_targets);
1197 		return -EINVAL;
1198 	}
1199 
1200 	cxlsd = to_cxl_switch_decoder(&cxld->dev);
1201 	if (cxl_rr->nr_targets_set) {
1202 		int i, distance;
1203 
1204 		/*
1205 		 * Passthrough decoders impose no distance requirements between
1206 		 * peers
1207 		 */
1208 		if (cxl_rr->nr_targets == 1)
1209 			distance = 0;
1210 		else
1211 			distance = p->nr_targets / cxl_rr->nr_targets;
1212 		for (i = 0; i < cxl_rr->nr_targets_set; i++)
1213 			if (ep->dport == cxlsd->target[i]) {
1214 				rc = check_last_peer(cxled, ep, cxl_rr,
1215 						     distance);
1216 				if (rc)
1217 					return rc;
1218 				goto out_target_set;
1219 			}
1220 		goto add_target;
1221 	}
1222 
1223 	if (is_cxl_root(parent_port)) {
1224 		/*
1225 		 * Root decoder IG is always set to value in CFMWS which
1226 		 * may be different than this region's IG.  We can use the
1227 		 * region's IG here since interleave_granularity_store()
1228 		 * does not allow interleaved host-bridges with
1229 		 * root IG != region IG.
1230 		 */
1231 		parent_ig = p->interleave_granularity;
1232 		parent_iw = cxlrd->cxlsd.cxld.interleave_ways;
1233 		/*
1234 		 * For purposes of address bit routing, use power-of-2 math for
1235 		 * switch ports.
1236 		 */
1237 		if (!is_power_of_2(parent_iw))
1238 			parent_iw /= 3;
1239 	} else {
1240 		struct cxl_region_ref *parent_rr;
1241 		struct cxl_decoder *parent_cxld;
1242 
1243 		parent_rr = cxl_rr_load(parent_port, cxlr);
1244 		parent_cxld = parent_rr->decoder;
1245 		parent_ig = parent_cxld->interleave_granularity;
1246 		parent_iw = parent_cxld->interleave_ways;
1247 	}
1248 
1249 	rc = granularity_to_eig(parent_ig, &peig);
1250 	if (rc) {
1251 		dev_dbg(&cxlr->dev, "%s:%s: invalid parent granularity: %d\n",
1252 			dev_name(parent_port->uport_dev),
1253 			dev_name(&parent_port->dev), parent_ig);
1254 		return rc;
1255 	}
1256 
1257 	rc = ways_to_eiw(parent_iw, &peiw);
1258 	if (rc) {
1259 		dev_dbg(&cxlr->dev, "%s:%s: invalid parent interleave: %d\n",
1260 			dev_name(parent_port->uport_dev),
1261 			dev_name(&parent_port->dev), parent_iw);
1262 		return rc;
1263 	}
1264 
1265 	iw = cxl_rr->nr_targets;
1266 	rc = ways_to_eiw(iw, &eiw);
1267 	if (rc) {
1268 		dev_dbg(&cxlr->dev, "%s:%s: invalid port interleave: %d\n",
1269 			dev_name(port->uport_dev), dev_name(&port->dev), iw);
1270 		return rc;
1271 	}
1272 
1273 	/*
1274 	 * Interleave granularity is a multiple of @parent_port granularity.
1275 	 * Multiplier is the parent port interleave ways.
1276 	 */
1277 	rc = granularity_to_eig(parent_ig * parent_iw, &eig);
1278 	if (rc) {
1279 		dev_dbg(&cxlr->dev,
1280 			"%s: invalid granularity calculation (%d * %d)\n",
1281 			dev_name(&parent_port->dev), parent_ig, parent_iw);
1282 		return rc;
1283 	}
1284 
1285 	rc = eig_to_granularity(eig, &ig);
1286 	if (rc) {
1287 		dev_dbg(&cxlr->dev, "%s:%s: invalid interleave: %d\n",
1288 			dev_name(port->uport_dev), dev_name(&port->dev),
1289 			256 << eig);
1290 		return rc;
1291 	}
1292 
1293 	if (iw > 8 || iw > cxlsd->nr_targets) {
1294 		dev_dbg(&cxlr->dev,
1295 			"%s:%s:%s: ways: %d overflows targets: %d\n",
1296 			dev_name(port->uport_dev), dev_name(&port->dev),
1297 			dev_name(&cxld->dev), iw, cxlsd->nr_targets);
1298 		return -ENXIO;
1299 	}
1300 
1301 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1302 		if (cxld->interleave_ways != iw ||
1303 		    cxld->interleave_granularity != ig ||
1304 		    cxld->hpa_range.start != p->res->start ||
1305 		    cxld->hpa_range.end != p->res->end ||
1306 		    ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) {
1307 			dev_err(&cxlr->dev,
1308 				"%s:%s %s expected iw: %d ig: %d %pr\n",
1309 				dev_name(port->uport_dev), dev_name(&port->dev),
1310 				__func__, iw, ig, p->res);
1311 			dev_err(&cxlr->dev,
1312 				"%s:%s %s got iw: %d ig: %d state: %s %#llx:%#llx\n",
1313 				dev_name(port->uport_dev), dev_name(&port->dev),
1314 				__func__, cxld->interleave_ways,
1315 				cxld->interleave_granularity,
1316 				(cxld->flags & CXL_DECODER_F_ENABLE) ?
1317 					"enabled" :
1318 					"disabled",
1319 				cxld->hpa_range.start, cxld->hpa_range.end);
1320 			return -ENXIO;
1321 		}
1322 	} else {
1323 		rc = check_interleave_cap(cxld, iw, ig);
1324 		if (rc) {
1325 			dev_dbg(&cxlr->dev,
1326 				"%s:%s iw: %d ig: %d is not supported\n",
1327 				dev_name(port->uport_dev),
1328 				dev_name(&port->dev), iw, ig);
1329 			return rc;
1330 		}
1331 
1332 		cxld->interleave_ways = iw;
1333 		cxld->interleave_granularity = ig;
1334 		cxld->hpa_range = (struct range) {
1335 			.start = p->res->start,
1336 			.end = p->res->end,
1337 		};
1338 	}
1339 	dev_dbg(&cxlr->dev, "%s:%s iw: %d ig: %d\n", dev_name(port->uport_dev),
1340 		dev_name(&port->dev), iw, ig);
1341 add_target:
1342 	if (cxl_rr->nr_targets_set == cxl_rr->nr_targets) {
1343 		dev_dbg(&cxlr->dev,
1344 			"%s:%s: targets full trying to add %s:%s at %d\n",
1345 			dev_name(port->uport_dev), dev_name(&port->dev),
1346 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1347 		return -ENXIO;
1348 	}
1349 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1350 		if (cxlsd->target[cxl_rr->nr_targets_set] != ep->dport) {
1351 			dev_dbg(&cxlr->dev, "%s:%s: %s expected %s at %d\n",
1352 				dev_name(port->uport_dev), dev_name(&port->dev),
1353 				dev_name(&cxlsd->cxld.dev),
1354 				dev_name(ep->dport->dport_dev),
1355 				cxl_rr->nr_targets_set);
1356 			return -ENXIO;
1357 		}
1358 	} else
1359 		cxlsd->target[cxl_rr->nr_targets_set] = ep->dport;
1360 	inc = 1;
1361 out_target_set:
1362 	cxl_rr->nr_targets_set += inc;
1363 	dev_dbg(&cxlr->dev, "%s:%s target[%d] = %s for %s:%s @ %d\n",
1364 		dev_name(port->uport_dev), dev_name(&port->dev),
1365 		cxl_rr->nr_targets_set - 1, dev_name(ep->dport->dport_dev),
1366 		dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1367 
1368 	return 0;
1369 }
1370 
1371 static void cxl_port_reset_targets(struct cxl_port *port,
1372 				   struct cxl_region *cxlr)
1373 {
1374 	struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
1375 	struct cxl_decoder *cxld;
1376 
1377 	/*
1378 	 * After the last endpoint has been detached the entire cxl_rr may now
1379 	 * be gone.
1380 	 */
1381 	if (!cxl_rr)
1382 		return;
1383 	cxl_rr->nr_targets_set = 0;
1384 
1385 	cxld = cxl_rr->decoder;
1386 	cxld->hpa_range = (struct range) {
1387 		.start = 0,
1388 		.end = -1,
1389 	};
1390 }
1391 
1392 static void cxl_region_teardown_targets(struct cxl_region *cxlr)
1393 {
1394 	struct cxl_region_params *p = &cxlr->params;
1395 	struct cxl_endpoint_decoder *cxled;
1396 	struct cxl_dev_state *cxlds;
1397 	struct cxl_memdev *cxlmd;
1398 	struct cxl_port *iter;
1399 	struct cxl_ep *ep;
1400 	int i;
1401 
1402 	/*
1403 	 * In the auto-discovery case skip automatic teardown since the
1404 	 * address space is already active
1405 	 */
1406 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags))
1407 		return;
1408 
1409 	for (i = 0; i < p->nr_targets; i++) {
1410 		cxled = p->targets[i];
1411 		cxlmd = cxled_to_memdev(cxled);
1412 		cxlds = cxlmd->cxlds;
1413 
1414 		if (cxlds->rcd)
1415 			continue;
1416 
1417 		iter = cxled_to_port(cxled);
1418 		while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
1419 			iter = to_cxl_port(iter->dev.parent);
1420 
1421 		for (ep = cxl_ep_load(iter, cxlmd); iter;
1422 		     iter = ep->next, ep = cxl_ep_load(iter, cxlmd))
1423 			cxl_port_reset_targets(iter, cxlr);
1424 	}
1425 }
1426 
1427 static int cxl_region_setup_targets(struct cxl_region *cxlr)
1428 {
1429 	struct cxl_region_params *p = &cxlr->params;
1430 	struct cxl_endpoint_decoder *cxled;
1431 	struct cxl_dev_state *cxlds;
1432 	int i, rc, rch = 0, vh = 0;
1433 	struct cxl_memdev *cxlmd;
1434 	struct cxl_port *iter;
1435 	struct cxl_ep *ep;
1436 
1437 	for (i = 0; i < p->nr_targets; i++) {
1438 		cxled = p->targets[i];
1439 		cxlmd = cxled_to_memdev(cxled);
1440 		cxlds = cxlmd->cxlds;
1441 
1442 		/* validate that all targets agree on topology */
1443 		if (!cxlds->rcd) {
1444 			vh++;
1445 		} else {
1446 			rch++;
1447 			continue;
1448 		}
1449 
1450 		iter = cxled_to_port(cxled);
1451 		while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
1452 			iter = to_cxl_port(iter->dev.parent);
1453 
1454 		/*
1455 		 * Descend the topology tree programming / validating
1456 		 * targets while looking for conflicts.
1457 		 */
1458 		for (ep = cxl_ep_load(iter, cxlmd); iter;
1459 		     iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
1460 			rc = cxl_port_setup_targets(iter, cxlr, cxled);
1461 			if (rc) {
1462 				cxl_region_teardown_targets(cxlr);
1463 				return rc;
1464 			}
1465 		}
1466 	}
1467 
1468 	if (rch && vh) {
1469 		dev_err(&cxlr->dev, "mismatched CXL topologies detected\n");
1470 		cxl_region_teardown_targets(cxlr);
1471 		return -ENXIO;
1472 	}
1473 
1474 	return 0;
1475 }
1476 
1477 static int cxl_region_validate_position(struct cxl_region *cxlr,
1478 					struct cxl_endpoint_decoder *cxled,
1479 					int pos)
1480 {
1481 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1482 	struct cxl_region_params *p = &cxlr->params;
1483 	int i;
1484 
1485 	if (pos < 0 || pos >= p->interleave_ways) {
1486 		dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
1487 			p->interleave_ways);
1488 		return -ENXIO;
1489 	}
1490 
1491 	if (p->targets[pos] == cxled)
1492 		return 0;
1493 
1494 	if (p->targets[pos]) {
1495 		struct cxl_endpoint_decoder *cxled_target = p->targets[pos];
1496 		struct cxl_memdev *cxlmd_target = cxled_to_memdev(cxled_target);
1497 
1498 		dev_dbg(&cxlr->dev, "position %d already assigned to %s:%s\n",
1499 			pos, dev_name(&cxlmd_target->dev),
1500 			dev_name(&cxled_target->cxld.dev));
1501 		return -EBUSY;
1502 	}
1503 
1504 	for (i = 0; i < p->interleave_ways; i++) {
1505 		struct cxl_endpoint_decoder *cxled_target;
1506 		struct cxl_memdev *cxlmd_target;
1507 
1508 		cxled_target = p->targets[i];
1509 		if (!cxled_target)
1510 			continue;
1511 
1512 		cxlmd_target = cxled_to_memdev(cxled_target);
1513 		if (cxlmd_target == cxlmd) {
1514 			dev_dbg(&cxlr->dev,
1515 				"%s already specified at position %d via: %s\n",
1516 				dev_name(&cxlmd->dev), pos,
1517 				dev_name(&cxled_target->cxld.dev));
1518 			return -EBUSY;
1519 		}
1520 	}
1521 
1522 	return 0;
1523 }
1524 
1525 static int cxl_region_attach_position(struct cxl_region *cxlr,
1526 				      struct cxl_root_decoder *cxlrd,
1527 				      struct cxl_endpoint_decoder *cxled,
1528 				      const struct cxl_dport *dport, int pos)
1529 {
1530 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1531 	struct cxl_port *iter;
1532 	int rc;
1533 
1534 	if (cxlrd->calc_hb(cxlrd, pos) != dport) {
1535 		dev_dbg(&cxlr->dev, "%s:%s invalid target position for %s\n",
1536 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1537 			dev_name(&cxlrd->cxlsd.cxld.dev));
1538 		return -ENXIO;
1539 	}
1540 
1541 	for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
1542 	     iter = to_cxl_port(iter->dev.parent)) {
1543 		rc = cxl_port_attach_region(iter, cxlr, cxled, pos);
1544 		if (rc)
1545 			goto err;
1546 	}
1547 
1548 	return 0;
1549 
1550 err:
1551 	for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
1552 	     iter = to_cxl_port(iter->dev.parent))
1553 		cxl_port_detach_region(iter, cxlr, cxled);
1554 	return rc;
1555 }
1556 
1557 static int cxl_region_attach_auto(struct cxl_region *cxlr,
1558 				  struct cxl_endpoint_decoder *cxled, int pos)
1559 {
1560 	struct cxl_region_params *p = &cxlr->params;
1561 
1562 	if (cxled->state != CXL_DECODER_STATE_AUTO) {
1563 		dev_err(&cxlr->dev,
1564 			"%s: unable to add decoder to autodetected region\n",
1565 			dev_name(&cxled->cxld.dev));
1566 		return -EINVAL;
1567 	}
1568 
1569 	if (pos >= 0) {
1570 		dev_dbg(&cxlr->dev, "%s: expected auto position, not %d\n",
1571 			dev_name(&cxled->cxld.dev), pos);
1572 		return -EINVAL;
1573 	}
1574 
1575 	if (p->nr_targets >= p->interleave_ways) {
1576 		dev_err(&cxlr->dev, "%s: no more target slots available\n",
1577 			dev_name(&cxled->cxld.dev));
1578 		return -ENXIO;
1579 	}
1580 
1581 	/*
1582 	 * Temporarily record the endpoint decoder into the target array. Yes,
1583 	 * this means that userspace can view devices in the wrong position
1584 	 * before the region activates, and must be careful to understand when
1585 	 * it might be racing region autodiscovery.
1586 	 */
1587 	pos = p->nr_targets;
1588 	p->targets[pos] = cxled;
1589 	cxled->pos = pos;
1590 	p->nr_targets++;
1591 
1592 	return 0;
1593 }
1594 
1595 static int cmp_interleave_pos(const void *a, const void *b)
1596 {
1597 	struct cxl_endpoint_decoder *cxled_a = *(typeof(cxled_a) *)a;
1598 	struct cxl_endpoint_decoder *cxled_b = *(typeof(cxled_b) *)b;
1599 
1600 	return cxled_a->pos - cxled_b->pos;
1601 }
1602 
1603 static struct cxl_port *next_port(struct cxl_port *port)
1604 {
1605 	if (!port->parent_dport)
1606 		return NULL;
1607 	return port->parent_dport->port;
1608 }
1609 
1610 static int match_switch_decoder_by_range(struct device *dev, void *data)
1611 {
1612 	struct cxl_switch_decoder *cxlsd;
1613 	struct range *r1, *r2 = data;
1614 
1615 	if (!is_switch_decoder(dev))
1616 		return 0;
1617 
1618 	cxlsd = to_cxl_switch_decoder(dev);
1619 	r1 = &cxlsd->cxld.hpa_range;
1620 
1621 	if (is_root_decoder(dev))
1622 		return range_contains(r1, r2);
1623 	return (r1->start == r2->start && r1->end == r2->end);
1624 }
1625 
1626 static int find_pos_and_ways(struct cxl_port *port, struct range *range,
1627 			     int *pos, int *ways)
1628 {
1629 	struct cxl_switch_decoder *cxlsd;
1630 	struct cxl_port *parent;
1631 	struct device *dev;
1632 	int rc = -ENXIO;
1633 
1634 	parent = next_port(port);
1635 	if (!parent)
1636 		return rc;
1637 
1638 	dev = device_find_child(&parent->dev, range,
1639 				match_switch_decoder_by_range);
1640 	if (!dev) {
1641 		dev_err(port->uport_dev,
1642 			"failed to find decoder mapping %#llx-%#llx\n",
1643 			range->start, range->end);
1644 		return rc;
1645 	}
1646 	cxlsd = to_cxl_switch_decoder(dev);
1647 	*ways = cxlsd->cxld.interleave_ways;
1648 
1649 	for (int i = 0; i < *ways; i++) {
1650 		if (cxlsd->target[i] == port->parent_dport) {
1651 			*pos = i;
1652 			rc = 0;
1653 			break;
1654 		}
1655 	}
1656 	put_device(dev);
1657 
1658 	return rc;
1659 }
1660 
1661 /**
1662  * cxl_calc_interleave_pos() - calculate an endpoint position in a region
1663  * @cxled: endpoint decoder member of given region
1664  *
1665  * The endpoint position is calculated by traversing the topology from
1666  * the endpoint to the root decoder and iteratively applying this
1667  * calculation:
1668  *
1669  *    position = position * parent_ways + parent_pos;
1670  *
1671  * ...where @position is inferred from switch and root decoder target lists.
1672  *
1673  * Return: position >= 0 on success
1674  *	   -ENXIO on failure
1675  */
1676 static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled)
1677 {
1678 	struct cxl_port *iter, *port = cxled_to_port(cxled);
1679 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1680 	struct range *range = &cxled->cxld.hpa_range;
1681 	int parent_ways = 0, parent_pos = 0, pos = 0;
1682 	int rc;
1683 
1684 	/*
1685 	 * Example: the expected interleave order of the 4-way region shown
1686 	 * below is: mem0, mem2, mem1, mem3
1687 	 *
1688 	 *		  root_port
1689 	 *                 /      \
1690 	 *      host_bridge_0    host_bridge_1
1691 	 *        |    |           |    |
1692 	 *       mem0 mem1        mem2 mem3
1693 	 *
1694 	 * In the example the calculator will iterate twice. The first iteration
1695 	 * uses the mem position in the host-bridge and the ways of the host-
1696 	 * bridge to generate the first, or local, position. The second
1697 	 * iteration uses the host-bridge position in the root_port and the ways
1698 	 * of the root_port to refine the position.
1699 	 *
1700 	 * A trace of the calculation per endpoint looks like this:
1701 	 * mem0: pos = 0 * 2 + 0    mem2: pos = 0 * 2 + 0
1702 	 *       pos = 0 * 2 + 0          pos = 0 * 2 + 1
1703 	 *       pos: 0                   pos: 1
1704 	 *
1705 	 * mem1: pos = 0 * 2 + 1    mem3: pos = 0 * 2 + 1
1706 	 *       pos = 1 * 2 + 0          pos = 1 * 2 + 1
1707 	 *       pos: 2                   pos = 3
1708 	 *
1709 	 * Note that while this example is simple, the method applies to more
1710 	 * complex topologies, including those with switches.
1711 	 */
1712 
1713 	/* Iterate from endpoint to root_port refining the position */
1714 	for (iter = port; iter; iter = next_port(iter)) {
1715 		if (is_cxl_root(iter))
1716 			break;
1717 
1718 		rc = find_pos_and_ways(iter, range, &parent_pos, &parent_ways);
1719 		if (rc)
1720 			return rc;
1721 
1722 		pos = pos * parent_ways + parent_pos;
1723 	}
1724 
1725 	dev_dbg(&cxlmd->dev,
1726 		"decoder:%s parent:%s port:%s range:%#llx-%#llx pos:%d\n",
1727 		dev_name(&cxled->cxld.dev), dev_name(cxlmd->dev.parent),
1728 		dev_name(&port->dev), range->start, range->end, pos);
1729 
1730 	return pos;
1731 }
1732 
1733 static int cxl_region_sort_targets(struct cxl_region *cxlr)
1734 {
1735 	struct cxl_region_params *p = &cxlr->params;
1736 	int i, rc = 0;
1737 
1738 	for (i = 0; i < p->nr_targets; i++) {
1739 		struct cxl_endpoint_decoder *cxled = p->targets[i];
1740 
1741 		cxled->pos = cxl_calc_interleave_pos(cxled);
1742 		/*
1743 		 * Record that sorting failed, but still continue to calc
1744 		 * cxled->pos so that follow-on code paths can reliably
1745 		 * do p->targets[cxled->pos] to self-reference their entry.
1746 		 */
1747 		if (cxled->pos < 0)
1748 			rc = -ENXIO;
1749 	}
1750 	/* Keep the cxlr target list in interleave position order */
1751 	sort(p->targets, p->nr_targets, sizeof(p->targets[0]),
1752 	     cmp_interleave_pos, NULL);
1753 
1754 	dev_dbg(&cxlr->dev, "region sort %s\n", rc ? "failed" : "successful");
1755 	return rc;
1756 }
1757 
1758 static int cxl_region_attach(struct cxl_region *cxlr,
1759 			     struct cxl_endpoint_decoder *cxled, int pos)
1760 {
1761 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
1762 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1763 	struct cxl_region_params *p = &cxlr->params;
1764 	struct cxl_port *ep_port, *root_port;
1765 	struct cxl_dport *dport;
1766 	int rc = -ENXIO;
1767 
1768 	rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
1769 				  p->interleave_granularity);
1770 	if (rc) {
1771 		dev_dbg(&cxlr->dev, "%s iw: %d ig: %d is not supported\n",
1772 			dev_name(&cxled->cxld.dev), p->interleave_ways,
1773 			p->interleave_granularity);
1774 		return rc;
1775 	}
1776 
1777 	if (cxled->mode != cxlr->mode) {
1778 		dev_dbg(&cxlr->dev, "%s region mode: %d mismatch: %d\n",
1779 			dev_name(&cxled->cxld.dev), cxlr->mode, cxled->mode);
1780 		return -EINVAL;
1781 	}
1782 
1783 	if (cxled->mode == CXL_DECODER_DEAD) {
1784 		dev_dbg(&cxlr->dev, "%s dead\n", dev_name(&cxled->cxld.dev));
1785 		return -ENODEV;
1786 	}
1787 
1788 	/* all full of members, or interleave config not established? */
1789 	if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) {
1790 		dev_dbg(&cxlr->dev, "region already active\n");
1791 		return -EBUSY;
1792 	} else if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
1793 		dev_dbg(&cxlr->dev, "interleave config missing\n");
1794 		return -ENXIO;
1795 	}
1796 
1797 	if (p->nr_targets >= p->interleave_ways) {
1798 		dev_dbg(&cxlr->dev, "region already has %d endpoints\n",
1799 			p->nr_targets);
1800 		return -EINVAL;
1801 	}
1802 
1803 	ep_port = cxled_to_port(cxled);
1804 	root_port = cxlrd_to_port(cxlrd);
1805 	dport = cxl_find_dport_by_dev(root_port, ep_port->host_bridge);
1806 	if (!dport) {
1807 		dev_dbg(&cxlr->dev, "%s:%s invalid target for %s\n",
1808 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1809 			dev_name(cxlr->dev.parent));
1810 		return -ENXIO;
1811 	}
1812 
1813 	if (cxled->cxld.target_type != cxlr->type) {
1814 		dev_dbg(&cxlr->dev, "%s:%s type mismatch: %d vs %d\n",
1815 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1816 			cxled->cxld.target_type, cxlr->type);
1817 		return -ENXIO;
1818 	}
1819 
1820 	if (!cxled->dpa_res) {
1821 		dev_dbg(&cxlr->dev, "%s:%s: missing DPA allocation.\n",
1822 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev));
1823 		return -ENXIO;
1824 	}
1825 
1826 	if (resource_size(cxled->dpa_res) * p->interleave_ways !=
1827 	    resource_size(p->res)) {
1828 		dev_dbg(&cxlr->dev,
1829 			"%s:%s: decoder-size-%#llx * ways-%d != region-size-%#llx\n",
1830 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1831 			(u64)resource_size(cxled->dpa_res), p->interleave_ways,
1832 			(u64)resource_size(p->res));
1833 		return -EINVAL;
1834 	}
1835 
1836 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1837 		int i;
1838 
1839 		rc = cxl_region_attach_auto(cxlr, cxled, pos);
1840 		if (rc)
1841 			return rc;
1842 
1843 		/* await more targets to arrive... */
1844 		if (p->nr_targets < p->interleave_ways)
1845 			return 0;
1846 
1847 		/*
1848 		 * All targets are here, which implies all PCI enumeration that
1849 		 * affects this region has been completed. Walk the topology to
1850 		 * sort the devices into their relative region decode position.
1851 		 */
1852 		rc = cxl_region_sort_targets(cxlr);
1853 		if (rc)
1854 			return rc;
1855 
1856 		for (i = 0; i < p->nr_targets; i++) {
1857 			cxled = p->targets[i];
1858 			ep_port = cxled_to_port(cxled);
1859 			dport = cxl_find_dport_by_dev(root_port,
1860 						      ep_port->host_bridge);
1861 			rc = cxl_region_attach_position(cxlr, cxlrd, cxled,
1862 							dport, i);
1863 			if (rc)
1864 				return rc;
1865 		}
1866 
1867 		rc = cxl_region_setup_targets(cxlr);
1868 		if (rc)
1869 			return rc;
1870 
1871 		/*
1872 		 * If target setup succeeds in the autodiscovery case
1873 		 * then the region is already committed.
1874 		 */
1875 		p->state = CXL_CONFIG_COMMIT;
1876 
1877 		return 0;
1878 	}
1879 
1880 	rc = cxl_region_validate_position(cxlr, cxled, pos);
1881 	if (rc)
1882 		return rc;
1883 
1884 	rc = cxl_region_attach_position(cxlr, cxlrd, cxled, dport, pos);
1885 	if (rc)
1886 		return rc;
1887 
1888 	p->targets[pos] = cxled;
1889 	cxled->pos = pos;
1890 	p->nr_targets++;
1891 
1892 	if (p->nr_targets == p->interleave_ways) {
1893 		rc = cxl_region_setup_targets(cxlr);
1894 		if (rc)
1895 			return rc;
1896 		p->state = CXL_CONFIG_ACTIVE;
1897 	}
1898 
1899 	cxled->cxld.interleave_ways = p->interleave_ways;
1900 	cxled->cxld.interleave_granularity = p->interleave_granularity;
1901 	cxled->cxld.hpa_range = (struct range) {
1902 		.start = p->res->start,
1903 		.end = p->res->end,
1904 	};
1905 
1906 	if (p->nr_targets != p->interleave_ways)
1907 		return 0;
1908 
1909 	/*
1910 	 * Test the auto-discovery position calculator function
1911 	 * against this successfully created user-defined region.
1912 	 * A fail message here means that this interleave config
1913 	 * will fail when presented as CXL_REGION_F_AUTO.
1914 	 */
1915 	for (int i = 0; i < p->nr_targets; i++) {
1916 		struct cxl_endpoint_decoder *cxled = p->targets[i];
1917 		int test_pos;
1918 
1919 		test_pos = cxl_calc_interleave_pos(cxled);
1920 		dev_dbg(&cxled->cxld.dev,
1921 			"Test cxl_calc_interleave_pos(): %s test_pos:%d cxled->pos:%d\n",
1922 			(test_pos == cxled->pos) ? "success" : "fail",
1923 			test_pos, cxled->pos);
1924 	}
1925 
1926 	return 0;
1927 }
1928 
1929 static int cxl_region_detach(struct cxl_endpoint_decoder *cxled)
1930 {
1931 	struct cxl_port *iter, *ep_port = cxled_to_port(cxled);
1932 	struct cxl_region *cxlr = cxled->cxld.region;
1933 	struct cxl_region_params *p;
1934 	int rc = 0;
1935 
1936 	lockdep_assert_held_write(&cxl_region_rwsem);
1937 
1938 	if (!cxlr)
1939 		return 0;
1940 
1941 	p = &cxlr->params;
1942 	get_device(&cxlr->dev);
1943 
1944 	if (p->state > CXL_CONFIG_ACTIVE) {
1945 		/*
1946 		 * TODO: tear down all impacted regions if a device is
1947 		 * removed out of order
1948 		 */
1949 		rc = cxl_region_decode_reset(cxlr, p->interleave_ways);
1950 		if (rc)
1951 			goto out;
1952 		p->state = CXL_CONFIG_ACTIVE;
1953 	}
1954 
1955 	for (iter = ep_port; !is_cxl_root(iter);
1956 	     iter = to_cxl_port(iter->dev.parent))
1957 		cxl_port_detach_region(iter, cxlr, cxled);
1958 
1959 	if (cxled->pos < 0 || cxled->pos >= p->interleave_ways ||
1960 	    p->targets[cxled->pos] != cxled) {
1961 		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1962 
1963 		dev_WARN_ONCE(&cxlr->dev, 1, "expected %s:%s at position %d\n",
1964 			      dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1965 			      cxled->pos);
1966 		goto out;
1967 	}
1968 
1969 	if (p->state == CXL_CONFIG_ACTIVE) {
1970 		p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
1971 		cxl_region_teardown_targets(cxlr);
1972 	}
1973 	p->targets[cxled->pos] = NULL;
1974 	p->nr_targets--;
1975 	cxled->cxld.hpa_range = (struct range) {
1976 		.start = 0,
1977 		.end = -1,
1978 	};
1979 
1980 	/* notify the region driver that one of its targets has departed */
1981 	up_write(&cxl_region_rwsem);
1982 	device_release_driver(&cxlr->dev);
1983 	down_write(&cxl_region_rwsem);
1984 out:
1985 	put_device(&cxlr->dev);
1986 	return rc;
1987 }
1988 
1989 void cxl_decoder_kill_region(struct cxl_endpoint_decoder *cxled)
1990 {
1991 	down_write(&cxl_region_rwsem);
1992 	cxled->mode = CXL_DECODER_DEAD;
1993 	cxl_region_detach(cxled);
1994 	up_write(&cxl_region_rwsem);
1995 }
1996 
1997 static int attach_target(struct cxl_region *cxlr,
1998 			 struct cxl_endpoint_decoder *cxled, int pos,
1999 			 unsigned int state)
2000 {
2001 	int rc = 0;
2002 
2003 	if (state == TASK_INTERRUPTIBLE)
2004 		rc = down_write_killable(&cxl_region_rwsem);
2005 	else
2006 		down_write(&cxl_region_rwsem);
2007 	if (rc)
2008 		return rc;
2009 
2010 	down_read(&cxl_dpa_rwsem);
2011 	rc = cxl_region_attach(cxlr, cxled, pos);
2012 	up_read(&cxl_dpa_rwsem);
2013 	up_write(&cxl_region_rwsem);
2014 	return rc;
2015 }
2016 
2017 static int detach_target(struct cxl_region *cxlr, int pos)
2018 {
2019 	struct cxl_region_params *p = &cxlr->params;
2020 	int rc;
2021 
2022 	rc = down_write_killable(&cxl_region_rwsem);
2023 	if (rc)
2024 		return rc;
2025 
2026 	if (pos >= p->interleave_ways) {
2027 		dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
2028 			p->interleave_ways);
2029 		rc = -ENXIO;
2030 		goto out;
2031 	}
2032 
2033 	if (!p->targets[pos]) {
2034 		rc = 0;
2035 		goto out;
2036 	}
2037 
2038 	rc = cxl_region_detach(p->targets[pos]);
2039 out:
2040 	up_write(&cxl_region_rwsem);
2041 	return rc;
2042 }
2043 
2044 static size_t store_targetN(struct cxl_region *cxlr, const char *buf, int pos,
2045 			    size_t len)
2046 {
2047 	int rc;
2048 
2049 	if (sysfs_streq(buf, "\n"))
2050 		rc = detach_target(cxlr, pos);
2051 	else {
2052 		struct device *dev;
2053 
2054 		dev = bus_find_device_by_name(&cxl_bus_type, NULL, buf);
2055 		if (!dev)
2056 			return -ENODEV;
2057 
2058 		if (!is_endpoint_decoder(dev)) {
2059 			rc = -EINVAL;
2060 			goto out;
2061 		}
2062 
2063 		rc = attach_target(cxlr, to_cxl_endpoint_decoder(dev), pos,
2064 				   TASK_INTERRUPTIBLE);
2065 out:
2066 		put_device(dev);
2067 	}
2068 
2069 	if (rc < 0)
2070 		return rc;
2071 	return len;
2072 }
2073 
2074 #define TARGET_ATTR_RW(n)                                              \
2075 static ssize_t target##n##_show(                                       \
2076 	struct device *dev, struct device_attribute *attr, char *buf)  \
2077 {                                                                      \
2078 	return show_targetN(to_cxl_region(dev), buf, (n));             \
2079 }                                                                      \
2080 static ssize_t target##n##_store(struct device *dev,                   \
2081 				 struct device_attribute *attr,        \
2082 				 const char *buf, size_t len)          \
2083 {                                                                      \
2084 	return store_targetN(to_cxl_region(dev), buf, (n), len);       \
2085 }                                                                      \
2086 static DEVICE_ATTR_RW(target##n)
2087 
2088 TARGET_ATTR_RW(0);
2089 TARGET_ATTR_RW(1);
2090 TARGET_ATTR_RW(2);
2091 TARGET_ATTR_RW(3);
2092 TARGET_ATTR_RW(4);
2093 TARGET_ATTR_RW(5);
2094 TARGET_ATTR_RW(6);
2095 TARGET_ATTR_RW(7);
2096 TARGET_ATTR_RW(8);
2097 TARGET_ATTR_RW(9);
2098 TARGET_ATTR_RW(10);
2099 TARGET_ATTR_RW(11);
2100 TARGET_ATTR_RW(12);
2101 TARGET_ATTR_RW(13);
2102 TARGET_ATTR_RW(14);
2103 TARGET_ATTR_RW(15);
2104 
2105 static struct attribute *target_attrs[] = {
2106 	&dev_attr_target0.attr,
2107 	&dev_attr_target1.attr,
2108 	&dev_attr_target2.attr,
2109 	&dev_attr_target3.attr,
2110 	&dev_attr_target4.attr,
2111 	&dev_attr_target5.attr,
2112 	&dev_attr_target6.attr,
2113 	&dev_attr_target7.attr,
2114 	&dev_attr_target8.attr,
2115 	&dev_attr_target9.attr,
2116 	&dev_attr_target10.attr,
2117 	&dev_attr_target11.attr,
2118 	&dev_attr_target12.attr,
2119 	&dev_attr_target13.attr,
2120 	&dev_attr_target14.attr,
2121 	&dev_attr_target15.attr,
2122 	NULL,
2123 };
2124 
2125 static umode_t cxl_region_target_visible(struct kobject *kobj,
2126 					 struct attribute *a, int n)
2127 {
2128 	struct device *dev = kobj_to_dev(kobj);
2129 	struct cxl_region *cxlr = to_cxl_region(dev);
2130 	struct cxl_region_params *p = &cxlr->params;
2131 
2132 	if (n < p->interleave_ways)
2133 		return a->mode;
2134 	return 0;
2135 }
2136 
2137 static const struct attribute_group cxl_region_target_group = {
2138 	.attrs = target_attrs,
2139 	.is_visible = cxl_region_target_visible,
2140 };
2141 
2142 static const struct attribute_group *get_cxl_region_target_group(void)
2143 {
2144 	return &cxl_region_target_group;
2145 }
2146 
2147 static const struct attribute_group *region_groups[] = {
2148 	&cxl_base_attribute_group,
2149 	&cxl_region_group,
2150 	&cxl_region_target_group,
2151 	NULL,
2152 };
2153 
2154 static void cxl_region_release(struct device *dev)
2155 {
2156 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
2157 	struct cxl_region *cxlr = to_cxl_region(dev);
2158 	int id = atomic_read(&cxlrd->region_id);
2159 
2160 	/*
2161 	 * Try to reuse the recently idled id rather than the cached
2162 	 * next id to prevent the region id space from increasing
2163 	 * unnecessarily.
2164 	 */
2165 	if (cxlr->id < id)
2166 		if (atomic_try_cmpxchg(&cxlrd->region_id, &id, cxlr->id)) {
2167 			memregion_free(id);
2168 			goto out;
2169 		}
2170 
2171 	memregion_free(cxlr->id);
2172 out:
2173 	put_device(dev->parent);
2174 	kfree(cxlr);
2175 }
2176 
2177 const struct device_type cxl_region_type = {
2178 	.name = "cxl_region",
2179 	.release = cxl_region_release,
2180 	.groups = region_groups
2181 };
2182 
2183 bool is_cxl_region(struct device *dev)
2184 {
2185 	return dev->type == &cxl_region_type;
2186 }
2187 EXPORT_SYMBOL_NS_GPL(is_cxl_region, CXL);
2188 
2189 static struct cxl_region *to_cxl_region(struct device *dev)
2190 {
2191 	if (dev_WARN_ONCE(dev, dev->type != &cxl_region_type,
2192 			  "not a cxl_region device\n"))
2193 		return NULL;
2194 
2195 	return container_of(dev, struct cxl_region, dev);
2196 }
2197 
2198 static void unregister_region(void *dev)
2199 {
2200 	struct cxl_region *cxlr = to_cxl_region(dev);
2201 	struct cxl_region_params *p = &cxlr->params;
2202 	int i;
2203 
2204 	device_del(dev);
2205 
2206 	/*
2207 	 * Now that region sysfs is shutdown, the parameter block is now
2208 	 * read-only, so no need to hold the region rwsem to access the
2209 	 * region parameters.
2210 	 */
2211 	for (i = 0; i < p->interleave_ways; i++)
2212 		detach_target(cxlr, i);
2213 
2214 	cxl_region_iomem_release(cxlr);
2215 	put_device(dev);
2216 }
2217 
2218 static struct lock_class_key cxl_region_key;
2219 
2220 static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id)
2221 {
2222 	struct cxl_region *cxlr;
2223 	struct device *dev;
2224 
2225 	cxlr = kzalloc(sizeof(*cxlr), GFP_KERNEL);
2226 	if (!cxlr) {
2227 		memregion_free(id);
2228 		return ERR_PTR(-ENOMEM);
2229 	}
2230 
2231 	dev = &cxlr->dev;
2232 	device_initialize(dev);
2233 	lockdep_set_class(&dev->mutex, &cxl_region_key);
2234 	dev->parent = &cxlrd->cxlsd.cxld.dev;
2235 	/*
2236 	 * Keep root decoder pinned through cxl_region_release to fixup
2237 	 * region id allocations
2238 	 */
2239 	get_device(dev->parent);
2240 	device_set_pm_not_required(dev);
2241 	dev->bus = &cxl_bus_type;
2242 	dev->type = &cxl_region_type;
2243 	cxlr->id = id;
2244 
2245 	return cxlr;
2246 }
2247 
2248 /**
2249  * devm_cxl_add_region - Adds a region to a decoder
2250  * @cxlrd: root decoder
2251  * @id: memregion id to create, or memregion_free() on failure
2252  * @mode: mode for the endpoint decoders of this region
2253  * @type: select whether this is an expander or accelerator (type-2 or type-3)
2254  *
2255  * This is the second step of region initialization. Regions exist within an
2256  * address space which is mapped by a @cxlrd.
2257  *
2258  * Return: 0 if the region was added to the @cxlrd, else returns negative error
2259  * code. The region will be named "regionZ" where Z is the unique region number.
2260  */
2261 static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd,
2262 					      int id,
2263 					      enum cxl_decoder_mode mode,
2264 					      enum cxl_decoder_type type)
2265 {
2266 	struct cxl_port *port = to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
2267 	struct cxl_region *cxlr;
2268 	struct device *dev;
2269 	int rc;
2270 
2271 	cxlr = cxl_region_alloc(cxlrd, id);
2272 	if (IS_ERR(cxlr))
2273 		return cxlr;
2274 	cxlr->mode = mode;
2275 	cxlr->type = type;
2276 
2277 	dev = &cxlr->dev;
2278 	rc = dev_set_name(dev, "region%d", id);
2279 	if (rc)
2280 		goto err;
2281 
2282 	rc = device_add(dev);
2283 	if (rc)
2284 		goto err;
2285 
2286 	rc = devm_add_action_or_reset(port->uport_dev, unregister_region, cxlr);
2287 	if (rc)
2288 		return ERR_PTR(rc);
2289 
2290 	dev_dbg(port->uport_dev, "%s: created %s\n",
2291 		dev_name(&cxlrd->cxlsd.cxld.dev), dev_name(dev));
2292 	return cxlr;
2293 
2294 err:
2295 	put_device(dev);
2296 	return ERR_PTR(rc);
2297 }
2298 
2299 static ssize_t __create_region_show(struct cxl_root_decoder *cxlrd, char *buf)
2300 {
2301 	return sysfs_emit(buf, "region%u\n", atomic_read(&cxlrd->region_id));
2302 }
2303 
2304 static ssize_t create_pmem_region_show(struct device *dev,
2305 				       struct device_attribute *attr, char *buf)
2306 {
2307 	return __create_region_show(to_cxl_root_decoder(dev), buf);
2308 }
2309 
2310 static ssize_t create_ram_region_show(struct device *dev,
2311 				      struct device_attribute *attr, char *buf)
2312 {
2313 	return __create_region_show(to_cxl_root_decoder(dev), buf);
2314 }
2315 
2316 static struct cxl_region *__create_region(struct cxl_root_decoder *cxlrd,
2317 					  enum cxl_decoder_mode mode, int id)
2318 {
2319 	int rc;
2320 
2321 	switch (mode) {
2322 	case CXL_DECODER_RAM:
2323 	case CXL_DECODER_PMEM:
2324 		break;
2325 	default:
2326 		dev_err(&cxlrd->cxlsd.cxld.dev, "unsupported mode %d\n", mode);
2327 		return ERR_PTR(-EINVAL);
2328 	}
2329 
2330 	rc = memregion_alloc(GFP_KERNEL);
2331 	if (rc < 0)
2332 		return ERR_PTR(rc);
2333 
2334 	if (atomic_cmpxchg(&cxlrd->region_id, id, rc) != id) {
2335 		memregion_free(rc);
2336 		return ERR_PTR(-EBUSY);
2337 	}
2338 
2339 	return devm_cxl_add_region(cxlrd, id, mode, CXL_DECODER_HOSTONLYMEM);
2340 }
2341 
2342 static ssize_t create_pmem_region_store(struct device *dev,
2343 					struct device_attribute *attr,
2344 					const char *buf, size_t len)
2345 {
2346 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2347 	struct cxl_region *cxlr;
2348 	int rc, id;
2349 
2350 	rc = sscanf(buf, "region%d\n", &id);
2351 	if (rc != 1)
2352 		return -EINVAL;
2353 
2354 	cxlr = __create_region(cxlrd, CXL_DECODER_PMEM, id);
2355 	if (IS_ERR(cxlr))
2356 		return PTR_ERR(cxlr);
2357 
2358 	return len;
2359 }
2360 DEVICE_ATTR_RW(create_pmem_region);
2361 
2362 static ssize_t create_ram_region_store(struct device *dev,
2363 				       struct device_attribute *attr,
2364 				       const char *buf, size_t len)
2365 {
2366 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2367 	struct cxl_region *cxlr;
2368 	int rc, id;
2369 
2370 	rc = sscanf(buf, "region%d\n", &id);
2371 	if (rc != 1)
2372 		return -EINVAL;
2373 
2374 	cxlr = __create_region(cxlrd, CXL_DECODER_RAM, id);
2375 	if (IS_ERR(cxlr))
2376 		return PTR_ERR(cxlr);
2377 
2378 	return len;
2379 }
2380 DEVICE_ATTR_RW(create_ram_region);
2381 
2382 static ssize_t region_show(struct device *dev, struct device_attribute *attr,
2383 			   char *buf)
2384 {
2385 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
2386 	ssize_t rc;
2387 
2388 	rc = down_read_interruptible(&cxl_region_rwsem);
2389 	if (rc)
2390 		return rc;
2391 
2392 	if (cxld->region)
2393 		rc = sysfs_emit(buf, "%s\n", dev_name(&cxld->region->dev));
2394 	else
2395 		rc = sysfs_emit(buf, "\n");
2396 	up_read(&cxl_region_rwsem);
2397 
2398 	return rc;
2399 }
2400 DEVICE_ATTR_RO(region);
2401 
2402 static struct cxl_region *
2403 cxl_find_region_by_name(struct cxl_root_decoder *cxlrd, const char *name)
2404 {
2405 	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
2406 	struct device *region_dev;
2407 
2408 	region_dev = device_find_child_by_name(&cxld->dev, name);
2409 	if (!region_dev)
2410 		return ERR_PTR(-ENODEV);
2411 
2412 	return to_cxl_region(region_dev);
2413 }
2414 
2415 static ssize_t delete_region_store(struct device *dev,
2416 				   struct device_attribute *attr,
2417 				   const char *buf, size_t len)
2418 {
2419 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2420 	struct cxl_port *port = to_cxl_port(dev->parent);
2421 	struct cxl_region *cxlr;
2422 
2423 	cxlr = cxl_find_region_by_name(cxlrd, buf);
2424 	if (IS_ERR(cxlr))
2425 		return PTR_ERR(cxlr);
2426 
2427 	devm_release_action(port->uport_dev, unregister_region, cxlr);
2428 	put_device(&cxlr->dev);
2429 
2430 	return len;
2431 }
2432 DEVICE_ATTR_WO(delete_region);
2433 
2434 static void cxl_pmem_region_release(struct device *dev)
2435 {
2436 	struct cxl_pmem_region *cxlr_pmem = to_cxl_pmem_region(dev);
2437 	int i;
2438 
2439 	for (i = 0; i < cxlr_pmem->nr_mappings; i++) {
2440 		struct cxl_memdev *cxlmd = cxlr_pmem->mapping[i].cxlmd;
2441 
2442 		put_device(&cxlmd->dev);
2443 	}
2444 
2445 	kfree(cxlr_pmem);
2446 }
2447 
2448 static const struct attribute_group *cxl_pmem_region_attribute_groups[] = {
2449 	&cxl_base_attribute_group,
2450 	NULL,
2451 };
2452 
2453 const struct device_type cxl_pmem_region_type = {
2454 	.name = "cxl_pmem_region",
2455 	.release = cxl_pmem_region_release,
2456 	.groups = cxl_pmem_region_attribute_groups,
2457 };
2458 
2459 bool is_cxl_pmem_region(struct device *dev)
2460 {
2461 	return dev->type == &cxl_pmem_region_type;
2462 }
2463 EXPORT_SYMBOL_NS_GPL(is_cxl_pmem_region, CXL);
2464 
2465 struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev)
2466 {
2467 	if (dev_WARN_ONCE(dev, !is_cxl_pmem_region(dev),
2468 			  "not a cxl_pmem_region device\n"))
2469 		return NULL;
2470 	return container_of(dev, struct cxl_pmem_region, dev);
2471 }
2472 EXPORT_SYMBOL_NS_GPL(to_cxl_pmem_region, CXL);
2473 
2474 struct cxl_poison_context {
2475 	struct cxl_port *port;
2476 	enum cxl_decoder_mode mode;
2477 	u64 offset;
2478 };
2479 
2480 static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
2481 				   struct cxl_poison_context *ctx)
2482 {
2483 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
2484 	u64 offset, length;
2485 	int rc = 0;
2486 
2487 	/*
2488 	 * Collect poison for the remaining unmapped resources
2489 	 * after poison is collected by committed endpoints.
2490 	 *
2491 	 * Knowing that PMEM must always follow RAM, get poison
2492 	 * for unmapped resources based on the last decoder's mode:
2493 	 *	ram: scan remains of ram range, then any pmem range
2494 	 *	pmem: scan remains of pmem range
2495 	 */
2496 
2497 	if (ctx->mode == CXL_DECODER_RAM) {
2498 		offset = ctx->offset;
2499 		length = resource_size(&cxlds->ram_res) - offset;
2500 		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
2501 		if (rc == -EFAULT)
2502 			rc = 0;
2503 		if (rc)
2504 			return rc;
2505 	}
2506 	if (ctx->mode == CXL_DECODER_PMEM) {
2507 		offset = ctx->offset;
2508 		length = resource_size(&cxlds->dpa_res) - offset;
2509 		if (!length)
2510 			return 0;
2511 	} else if (resource_size(&cxlds->pmem_res)) {
2512 		offset = cxlds->pmem_res.start;
2513 		length = resource_size(&cxlds->pmem_res);
2514 	} else {
2515 		return 0;
2516 	}
2517 
2518 	return cxl_mem_get_poison(cxlmd, offset, length, NULL);
2519 }
2520 
2521 static int poison_by_decoder(struct device *dev, void *arg)
2522 {
2523 	struct cxl_poison_context *ctx = arg;
2524 	struct cxl_endpoint_decoder *cxled;
2525 	struct cxl_memdev *cxlmd;
2526 	u64 offset, length;
2527 	int rc = 0;
2528 
2529 	if (!is_endpoint_decoder(dev))
2530 		return rc;
2531 
2532 	cxled = to_cxl_endpoint_decoder(dev);
2533 	if (!cxled->dpa_res || !resource_size(cxled->dpa_res))
2534 		return rc;
2535 
2536 	/*
2537 	 * Regions are only created with single mode decoders: pmem or ram.
2538 	 * Linux does not support mixed mode decoders. This means that
2539 	 * reading poison per endpoint decoder adheres to the requirement
2540 	 * that poison reads of pmem and ram must be separated.
2541 	 * CXL 3.0 Spec 8.2.9.8.4.1
2542 	 */
2543 	if (cxled->mode == CXL_DECODER_MIXED) {
2544 		dev_dbg(dev, "poison list read unsupported in mixed mode\n");
2545 		return rc;
2546 	}
2547 
2548 	cxlmd = cxled_to_memdev(cxled);
2549 	if (cxled->skip) {
2550 		offset = cxled->dpa_res->start - cxled->skip;
2551 		length = cxled->skip;
2552 		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
2553 		if (rc == -EFAULT && cxled->mode == CXL_DECODER_RAM)
2554 			rc = 0;
2555 		if (rc)
2556 			return rc;
2557 	}
2558 
2559 	offset = cxled->dpa_res->start;
2560 	length = cxled->dpa_res->end - offset + 1;
2561 	rc = cxl_mem_get_poison(cxlmd, offset, length, cxled->cxld.region);
2562 	if (rc == -EFAULT && cxled->mode == CXL_DECODER_RAM)
2563 		rc = 0;
2564 	if (rc)
2565 		return rc;
2566 
2567 	/* Iterate until commit_end is reached */
2568 	if (cxled->cxld.id == ctx->port->commit_end) {
2569 		ctx->offset = cxled->dpa_res->end + 1;
2570 		ctx->mode = cxled->mode;
2571 		return 1;
2572 	}
2573 
2574 	return 0;
2575 }
2576 
2577 int cxl_get_poison_by_endpoint(struct cxl_port *port)
2578 {
2579 	struct cxl_poison_context ctx;
2580 	int rc = 0;
2581 
2582 	ctx = (struct cxl_poison_context) {
2583 		.port = port
2584 	};
2585 
2586 	rc = device_for_each_child(&port->dev, &ctx, poison_by_decoder);
2587 	if (rc == 1)
2588 		rc = cxl_get_poison_unmapped(to_cxl_memdev(port->uport_dev),
2589 					     &ctx);
2590 
2591 	return rc;
2592 }
2593 
2594 struct cxl_dpa_to_region_context {
2595 	struct cxl_region *cxlr;
2596 	u64 dpa;
2597 };
2598 
2599 static int __cxl_dpa_to_region(struct device *dev, void *arg)
2600 {
2601 	struct cxl_dpa_to_region_context *ctx = arg;
2602 	struct cxl_endpoint_decoder *cxled;
2603 	struct cxl_region *cxlr;
2604 	u64 dpa = ctx->dpa;
2605 
2606 	if (!is_endpoint_decoder(dev))
2607 		return 0;
2608 
2609 	cxled = to_cxl_endpoint_decoder(dev);
2610 	if (!cxled || !cxled->dpa_res || !resource_size(cxled->dpa_res))
2611 		return 0;
2612 
2613 	if (dpa > cxled->dpa_res->end || dpa < cxled->dpa_res->start)
2614 		return 0;
2615 
2616 	/*
2617 	 * Stop the region search (return 1) when an endpoint mapping is
2618 	 * found. The region may not be fully constructed so offering
2619 	 * the cxlr in the context structure is not guaranteed.
2620 	 */
2621 	cxlr = cxled->cxld.region;
2622 	if (cxlr)
2623 		dev_dbg(dev, "dpa:0x%llx mapped in region:%s\n", dpa,
2624 			dev_name(&cxlr->dev));
2625 	else
2626 		dev_dbg(dev, "dpa:0x%llx mapped in endpoint:%s\n", dpa,
2627 			dev_name(dev));
2628 
2629 	ctx->cxlr = cxlr;
2630 
2631 	return 1;
2632 }
2633 
2634 struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa)
2635 {
2636 	struct cxl_dpa_to_region_context ctx;
2637 	struct cxl_port *port;
2638 
2639 	ctx = (struct cxl_dpa_to_region_context) {
2640 		.dpa = dpa,
2641 	};
2642 	port = cxlmd->endpoint;
2643 	if (port && is_cxl_endpoint(port) && cxl_num_decoders_committed(port))
2644 		device_for_each_child(&port->dev, &ctx, __cxl_dpa_to_region);
2645 
2646 	return ctx.cxlr;
2647 }
2648 
2649 static struct lock_class_key cxl_pmem_region_key;
2650 
2651 static struct cxl_pmem_region *cxl_pmem_region_alloc(struct cxl_region *cxlr)
2652 {
2653 	struct cxl_region_params *p = &cxlr->params;
2654 	struct cxl_nvdimm_bridge *cxl_nvb;
2655 	struct cxl_pmem_region *cxlr_pmem;
2656 	struct device *dev;
2657 	int i;
2658 
2659 	down_read(&cxl_region_rwsem);
2660 	if (p->state != CXL_CONFIG_COMMIT) {
2661 		cxlr_pmem = ERR_PTR(-ENXIO);
2662 		goto out;
2663 	}
2664 
2665 	cxlr_pmem = kzalloc(struct_size(cxlr_pmem, mapping, p->nr_targets),
2666 			    GFP_KERNEL);
2667 	if (!cxlr_pmem) {
2668 		cxlr_pmem = ERR_PTR(-ENOMEM);
2669 		goto out;
2670 	}
2671 
2672 	cxlr_pmem->hpa_range.start = p->res->start;
2673 	cxlr_pmem->hpa_range.end = p->res->end;
2674 
2675 	/* Snapshot the region configuration underneath the cxl_region_rwsem */
2676 	cxlr_pmem->nr_mappings = p->nr_targets;
2677 	for (i = 0; i < p->nr_targets; i++) {
2678 		struct cxl_endpoint_decoder *cxled = p->targets[i];
2679 		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
2680 		struct cxl_pmem_region_mapping *m = &cxlr_pmem->mapping[i];
2681 
2682 		/*
2683 		 * Regions never span CXL root devices, so by definition the
2684 		 * bridge for one device is the same for all.
2685 		 */
2686 		if (i == 0) {
2687 			cxl_nvb = cxl_find_nvdimm_bridge(cxlmd);
2688 			if (!cxl_nvb) {
2689 				kfree(cxlr_pmem);
2690 				cxlr_pmem = ERR_PTR(-ENODEV);
2691 				goto out;
2692 			}
2693 			cxlr->cxl_nvb = cxl_nvb;
2694 		}
2695 		m->cxlmd = cxlmd;
2696 		get_device(&cxlmd->dev);
2697 		m->start = cxled->dpa_res->start;
2698 		m->size = resource_size(cxled->dpa_res);
2699 		m->position = i;
2700 	}
2701 
2702 	dev = &cxlr_pmem->dev;
2703 	cxlr_pmem->cxlr = cxlr;
2704 	cxlr->cxlr_pmem = cxlr_pmem;
2705 	device_initialize(dev);
2706 	lockdep_set_class(&dev->mutex, &cxl_pmem_region_key);
2707 	device_set_pm_not_required(dev);
2708 	dev->parent = &cxlr->dev;
2709 	dev->bus = &cxl_bus_type;
2710 	dev->type = &cxl_pmem_region_type;
2711 out:
2712 	up_read(&cxl_region_rwsem);
2713 
2714 	return cxlr_pmem;
2715 }
2716 
2717 static void cxl_dax_region_release(struct device *dev)
2718 {
2719 	struct cxl_dax_region *cxlr_dax = to_cxl_dax_region(dev);
2720 
2721 	kfree(cxlr_dax);
2722 }
2723 
2724 static const struct attribute_group *cxl_dax_region_attribute_groups[] = {
2725 	&cxl_base_attribute_group,
2726 	NULL,
2727 };
2728 
2729 const struct device_type cxl_dax_region_type = {
2730 	.name = "cxl_dax_region",
2731 	.release = cxl_dax_region_release,
2732 	.groups = cxl_dax_region_attribute_groups,
2733 };
2734 
2735 static bool is_cxl_dax_region(struct device *dev)
2736 {
2737 	return dev->type == &cxl_dax_region_type;
2738 }
2739 
2740 struct cxl_dax_region *to_cxl_dax_region(struct device *dev)
2741 {
2742 	if (dev_WARN_ONCE(dev, !is_cxl_dax_region(dev),
2743 			  "not a cxl_dax_region device\n"))
2744 		return NULL;
2745 	return container_of(dev, struct cxl_dax_region, dev);
2746 }
2747 EXPORT_SYMBOL_NS_GPL(to_cxl_dax_region, CXL);
2748 
2749 static struct lock_class_key cxl_dax_region_key;
2750 
2751 static struct cxl_dax_region *cxl_dax_region_alloc(struct cxl_region *cxlr)
2752 {
2753 	struct cxl_region_params *p = &cxlr->params;
2754 	struct cxl_dax_region *cxlr_dax;
2755 	struct device *dev;
2756 
2757 	down_read(&cxl_region_rwsem);
2758 	if (p->state != CXL_CONFIG_COMMIT) {
2759 		cxlr_dax = ERR_PTR(-ENXIO);
2760 		goto out;
2761 	}
2762 
2763 	cxlr_dax = kzalloc(sizeof(*cxlr_dax), GFP_KERNEL);
2764 	if (!cxlr_dax) {
2765 		cxlr_dax = ERR_PTR(-ENOMEM);
2766 		goto out;
2767 	}
2768 
2769 	cxlr_dax->hpa_range.start = p->res->start;
2770 	cxlr_dax->hpa_range.end = p->res->end;
2771 
2772 	dev = &cxlr_dax->dev;
2773 	cxlr_dax->cxlr = cxlr;
2774 	device_initialize(dev);
2775 	lockdep_set_class(&dev->mutex, &cxl_dax_region_key);
2776 	device_set_pm_not_required(dev);
2777 	dev->parent = &cxlr->dev;
2778 	dev->bus = &cxl_bus_type;
2779 	dev->type = &cxl_dax_region_type;
2780 out:
2781 	up_read(&cxl_region_rwsem);
2782 
2783 	return cxlr_dax;
2784 }
2785 
2786 static void cxlr_pmem_unregister(void *_cxlr_pmem)
2787 {
2788 	struct cxl_pmem_region *cxlr_pmem = _cxlr_pmem;
2789 	struct cxl_region *cxlr = cxlr_pmem->cxlr;
2790 	struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
2791 
2792 	/*
2793 	 * Either the bridge is in ->remove() context under the device_lock(),
2794 	 * or cxlr_release_nvdimm() is cancelling the bridge's release action
2795 	 * for @cxlr_pmem and doing it itself (while manually holding the bridge
2796 	 * lock).
2797 	 */
2798 	device_lock_assert(&cxl_nvb->dev);
2799 	cxlr->cxlr_pmem = NULL;
2800 	cxlr_pmem->cxlr = NULL;
2801 	device_unregister(&cxlr_pmem->dev);
2802 }
2803 
2804 static void cxlr_release_nvdimm(void *_cxlr)
2805 {
2806 	struct cxl_region *cxlr = _cxlr;
2807 	struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
2808 
2809 	device_lock(&cxl_nvb->dev);
2810 	if (cxlr->cxlr_pmem)
2811 		devm_release_action(&cxl_nvb->dev, cxlr_pmem_unregister,
2812 				    cxlr->cxlr_pmem);
2813 	device_unlock(&cxl_nvb->dev);
2814 	cxlr->cxl_nvb = NULL;
2815 	put_device(&cxl_nvb->dev);
2816 }
2817 
2818 /**
2819  * devm_cxl_add_pmem_region() - add a cxl_region-to-nd_region bridge
2820  * @cxlr: parent CXL region for this pmem region bridge device
2821  *
2822  * Return: 0 on success negative error code on failure.
2823  */
2824 static int devm_cxl_add_pmem_region(struct cxl_region *cxlr)
2825 {
2826 	struct cxl_pmem_region *cxlr_pmem;
2827 	struct cxl_nvdimm_bridge *cxl_nvb;
2828 	struct device *dev;
2829 	int rc;
2830 
2831 	cxlr_pmem = cxl_pmem_region_alloc(cxlr);
2832 	if (IS_ERR(cxlr_pmem))
2833 		return PTR_ERR(cxlr_pmem);
2834 	cxl_nvb = cxlr->cxl_nvb;
2835 
2836 	dev = &cxlr_pmem->dev;
2837 	rc = dev_set_name(dev, "pmem_region%d", cxlr->id);
2838 	if (rc)
2839 		goto err;
2840 
2841 	rc = device_add(dev);
2842 	if (rc)
2843 		goto err;
2844 
2845 	dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
2846 		dev_name(dev));
2847 
2848 	device_lock(&cxl_nvb->dev);
2849 	if (cxl_nvb->dev.driver)
2850 		rc = devm_add_action_or_reset(&cxl_nvb->dev,
2851 					      cxlr_pmem_unregister, cxlr_pmem);
2852 	else
2853 		rc = -ENXIO;
2854 	device_unlock(&cxl_nvb->dev);
2855 
2856 	if (rc)
2857 		goto err_bridge;
2858 
2859 	/* @cxlr carries a reference on @cxl_nvb until cxlr_release_nvdimm */
2860 	return devm_add_action_or_reset(&cxlr->dev, cxlr_release_nvdimm, cxlr);
2861 
2862 err:
2863 	put_device(dev);
2864 err_bridge:
2865 	put_device(&cxl_nvb->dev);
2866 	cxlr->cxl_nvb = NULL;
2867 	return rc;
2868 }
2869 
2870 static void cxlr_dax_unregister(void *_cxlr_dax)
2871 {
2872 	struct cxl_dax_region *cxlr_dax = _cxlr_dax;
2873 
2874 	device_unregister(&cxlr_dax->dev);
2875 }
2876 
2877 static int devm_cxl_add_dax_region(struct cxl_region *cxlr)
2878 {
2879 	struct cxl_dax_region *cxlr_dax;
2880 	struct device *dev;
2881 	int rc;
2882 
2883 	cxlr_dax = cxl_dax_region_alloc(cxlr);
2884 	if (IS_ERR(cxlr_dax))
2885 		return PTR_ERR(cxlr_dax);
2886 
2887 	dev = &cxlr_dax->dev;
2888 	rc = dev_set_name(dev, "dax_region%d", cxlr->id);
2889 	if (rc)
2890 		goto err;
2891 
2892 	rc = device_add(dev);
2893 	if (rc)
2894 		goto err;
2895 
2896 	dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
2897 		dev_name(dev));
2898 
2899 	return devm_add_action_or_reset(&cxlr->dev, cxlr_dax_unregister,
2900 					cxlr_dax);
2901 err:
2902 	put_device(dev);
2903 	return rc;
2904 }
2905 
2906 static int match_root_decoder_by_range(struct device *dev, void *data)
2907 {
2908 	struct range *r1, *r2 = data;
2909 	struct cxl_root_decoder *cxlrd;
2910 
2911 	if (!is_root_decoder(dev))
2912 		return 0;
2913 
2914 	cxlrd = to_cxl_root_decoder(dev);
2915 	r1 = &cxlrd->cxlsd.cxld.hpa_range;
2916 	return range_contains(r1, r2);
2917 }
2918 
2919 static int match_region_by_range(struct device *dev, void *data)
2920 {
2921 	struct cxl_region_params *p;
2922 	struct cxl_region *cxlr;
2923 	struct range *r = data;
2924 	int rc = 0;
2925 
2926 	if (!is_cxl_region(dev))
2927 		return 0;
2928 
2929 	cxlr = to_cxl_region(dev);
2930 	p = &cxlr->params;
2931 
2932 	down_read(&cxl_region_rwsem);
2933 	if (p->res && p->res->start == r->start && p->res->end == r->end)
2934 		rc = 1;
2935 	up_read(&cxl_region_rwsem);
2936 
2937 	return rc;
2938 }
2939 
2940 /* Establish an empty region covering the given HPA range */
2941 static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
2942 					   struct cxl_endpoint_decoder *cxled)
2943 {
2944 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
2945 	struct cxl_port *port = cxlrd_to_port(cxlrd);
2946 	struct range *hpa = &cxled->cxld.hpa_range;
2947 	struct cxl_region_params *p;
2948 	struct cxl_region *cxlr;
2949 	struct resource *res;
2950 	int rc;
2951 
2952 	do {
2953 		cxlr = __create_region(cxlrd, cxled->mode,
2954 				       atomic_read(&cxlrd->region_id));
2955 	} while (IS_ERR(cxlr) && PTR_ERR(cxlr) == -EBUSY);
2956 
2957 	if (IS_ERR(cxlr)) {
2958 		dev_err(cxlmd->dev.parent,
2959 			"%s:%s: %s failed assign region: %ld\n",
2960 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2961 			__func__, PTR_ERR(cxlr));
2962 		return cxlr;
2963 	}
2964 
2965 	down_write(&cxl_region_rwsem);
2966 	p = &cxlr->params;
2967 	if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
2968 		dev_err(cxlmd->dev.parent,
2969 			"%s:%s: %s autodiscovery interrupted\n",
2970 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2971 			__func__);
2972 		rc = -EBUSY;
2973 		goto err;
2974 	}
2975 
2976 	set_bit(CXL_REGION_F_AUTO, &cxlr->flags);
2977 
2978 	res = kmalloc(sizeof(*res), GFP_KERNEL);
2979 	if (!res) {
2980 		rc = -ENOMEM;
2981 		goto err;
2982 	}
2983 
2984 	*res = DEFINE_RES_MEM_NAMED(hpa->start, range_len(hpa),
2985 				    dev_name(&cxlr->dev));
2986 	rc = insert_resource(cxlrd->res, res);
2987 	if (rc) {
2988 		/*
2989 		 * Platform-firmware may not have split resources like "System
2990 		 * RAM" on CXL window boundaries see cxl_region_iomem_release()
2991 		 */
2992 		dev_warn(cxlmd->dev.parent,
2993 			 "%s:%s: %s %s cannot insert resource\n",
2994 			 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2995 			 __func__, dev_name(&cxlr->dev));
2996 	}
2997 
2998 	p->res = res;
2999 	p->interleave_ways = cxled->cxld.interleave_ways;
3000 	p->interleave_granularity = cxled->cxld.interleave_granularity;
3001 	p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
3002 
3003 	rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
3004 	if (rc)
3005 		goto err;
3006 
3007 	dev_dbg(cxlmd->dev.parent, "%s:%s: %s %s res: %pr iw: %d ig: %d\n",
3008 		dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), __func__,
3009 		dev_name(&cxlr->dev), p->res, p->interleave_ways,
3010 		p->interleave_granularity);
3011 
3012 	/* ...to match put_device() in cxl_add_to_region() */
3013 	get_device(&cxlr->dev);
3014 	up_write(&cxl_region_rwsem);
3015 
3016 	return cxlr;
3017 
3018 err:
3019 	up_write(&cxl_region_rwsem);
3020 	devm_release_action(port->uport_dev, unregister_region, cxlr);
3021 	return ERR_PTR(rc);
3022 }
3023 
3024 int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled)
3025 {
3026 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
3027 	struct range *hpa = &cxled->cxld.hpa_range;
3028 	struct cxl_decoder *cxld = &cxled->cxld;
3029 	struct device *cxlrd_dev, *region_dev;
3030 	struct cxl_root_decoder *cxlrd;
3031 	struct cxl_region_params *p;
3032 	struct cxl_region *cxlr;
3033 	bool attach = false;
3034 	int rc;
3035 
3036 	cxlrd_dev = device_find_child(&root->dev, &cxld->hpa_range,
3037 				      match_root_decoder_by_range);
3038 	if (!cxlrd_dev) {
3039 		dev_err(cxlmd->dev.parent,
3040 			"%s:%s no CXL window for range %#llx:%#llx\n",
3041 			dev_name(&cxlmd->dev), dev_name(&cxld->dev),
3042 			cxld->hpa_range.start, cxld->hpa_range.end);
3043 		return -ENXIO;
3044 	}
3045 
3046 	cxlrd = to_cxl_root_decoder(cxlrd_dev);
3047 
3048 	/*
3049 	 * Ensure that if multiple threads race to construct_region() for @hpa
3050 	 * one does the construction and the others add to that.
3051 	 */
3052 	mutex_lock(&cxlrd->range_lock);
3053 	region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa,
3054 				       match_region_by_range);
3055 	if (!region_dev) {
3056 		cxlr = construct_region(cxlrd, cxled);
3057 		region_dev = &cxlr->dev;
3058 	} else
3059 		cxlr = to_cxl_region(region_dev);
3060 	mutex_unlock(&cxlrd->range_lock);
3061 
3062 	rc = PTR_ERR_OR_ZERO(cxlr);
3063 	if (rc)
3064 		goto out;
3065 
3066 	attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE);
3067 
3068 	down_read(&cxl_region_rwsem);
3069 	p = &cxlr->params;
3070 	attach = p->state == CXL_CONFIG_COMMIT;
3071 	up_read(&cxl_region_rwsem);
3072 
3073 	if (attach) {
3074 		/*
3075 		 * If device_attach() fails the range may still be active via
3076 		 * the platform-firmware memory map, otherwise the driver for
3077 		 * regions is local to this file, so driver matching can't fail.
3078 		 */
3079 		if (device_attach(&cxlr->dev) < 0)
3080 			dev_err(&cxlr->dev, "failed to enable, range: %pr\n",
3081 				p->res);
3082 	}
3083 
3084 	put_device(region_dev);
3085 out:
3086 	put_device(cxlrd_dev);
3087 	return rc;
3088 }
3089 EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, CXL);
3090 
3091 static int is_system_ram(struct resource *res, void *arg)
3092 {
3093 	struct cxl_region *cxlr = arg;
3094 	struct cxl_region_params *p = &cxlr->params;
3095 
3096 	dev_dbg(&cxlr->dev, "%pr has System RAM: %pr\n", p->res, res);
3097 	return 1;
3098 }
3099 
3100 static int cxl_region_probe(struct device *dev)
3101 {
3102 	struct cxl_region *cxlr = to_cxl_region(dev);
3103 	struct cxl_region_params *p = &cxlr->params;
3104 	int rc;
3105 
3106 	rc = down_read_interruptible(&cxl_region_rwsem);
3107 	if (rc) {
3108 		dev_dbg(&cxlr->dev, "probe interrupted\n");
3109 		return rc;
3110 	}
3111 
3112 	if (p->state < CXL_CONFIG_COMMIT) {
3113 		dev_dbg(&cxlr->dev, "config state: %d\n", p->state);
3114 		rc = -ENXIO;
3115 		goto out;
3116 	}
3117 
3118 	if (test_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags)) {
3119 		dev_err(&cxlr->dev,
3120 			"failed to activate, re-commit region and retry\n");
3121 		rc = -ENXIO;
3122 		goto out;
3123 	}
3124 
3125 	/*
3126 	 * From this point on any path that changes the region's state away from
3127 	 * CXL_CONFIG_COMMIT is also responsible for releasing the driver.
3128 	 */
3129 out:
3130 	up_read(&cxl_region_rwsem);
3131 
3132 	if (rc)
3133 		return rc;
3134 
3135 	switch (cxlr->mode) {
3136 	case CXL_DECODER_PMEM:
3137 		return devm_cxl_add_pmem_region(cxlr);
3138 	case CXL_DECODER_RAM:
3139 		/*
3140 		 * The region can not be manged by CXL if any portion of
3141 		 * it is already online as 'System RAM'
3142 		 */
3143 		if (walk_iomem_res_desc(IORES_DESC_NONE,
3144 					IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
3145 					p->res->start, p->res->end, cxlr,
3146 					is_system_ram) > 0)
3147 			return 0;
3148 		return devm_cxl_add_dax_region(cxlr);
3149 	default:
3150 		dev_dbg(&cxlr->dev, "unsupported region mode: %d\n",
3151 			cxlr->mode);
3152 		return -ENXIO;
3153 	}
3154 }
3155 
3156 static struct cxl_driver cxl_region_driver = {
3157 	.name = "cxl_region",
3158 	.probe = cxl_region_probe,
3159 	.id = CXL_DEVICE_REGION,
3160 };
3161 
3162 int cxl_region_init(void)
3163 {
3164 	return cxl_driver_register(&cxl_region_driver);
3165 }
3166 
3167 void cxl_region_exit(void)
3168 {
3169 	cxl_driver_unregister(&cxl_region_driver);
3170 }
3171 
3172 MODULE_IMPORT_NS(CXL);
3173 MODULE_IMPORT_NS(DEVMEM);
3174 MODULE_ALIAS_CXL(CXL_DEVICE_REGION);
3175