xref: /openbmc/linux/drivers/cxl/core/region.c (revision 2ae1beb3)
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_switch_decoder *cxlsd = &cxlrd->cxlsd;
1532 	struct cxl_decoder *cxld = &cxlsd->cxld;
1533 	int iw = cxld->interleave_ways;
1534 	struct cxl_port *iter;
1535 	int rc;
1536 
1537 	if (dport != cxlrd->cxlsd.target[pos % iw]) {
1538 		dev_dbg(&cxlr->dev, "%s:%s invalid target position for %s\n",
1539 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1540 			dev_name(&cxlrd->cxlsd.cxld.dev));
1541 		return -ENXIO;
1542 	}
1543 
1544 	for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
1545 	     iter = to_cxl_port(iter->dev.parent)) {
1546 		rc = cxl_port_attach_region(iter, cxlr, cxled, pos);
1547 		if (rc)
1548 			goto err;
1549 	}
1550 
1551 	return 0;
1552 
1553 err:
1554 	for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
1555 	     iter = to_cxl_port(iter->dev.parent))
1556 		cxl_port_detach_region(iter, cxlr, cxled);
1557 	return rc;
1558 }
1559 
1560 static int cxl_region_attach_auto(struct cxl_region *cxlr,
1561 				  struct cxl_endpoint_decoder *cxled, int pos)
1562 {
1563 	struct cxl_region_params *p = &cxlr->params;
1564 
1565 	if (cxled->state != CXL_DECODER_STATE_AUTO) {
1566 		dev_err(&cxlr->dev,
1567 			"%s: unable to add decoder to autodetected region\n",
1568 			dev_name(&cxled->cxld.dev));
1569 		return -EINVAL;
1570 	}
1571 
1572 	if (pos >= 0) {
1573 		dev_dbg(&cxlr->dev, "%s: expected auto position, not %d\n",
1574 			dev_name(&cxled->cxld.dev), pos);
1575 		return -EINVAL;
1576 	}
1577 
1578 	if (p->nr_targets >= p->interleave_ways) {
1579 		dev_err(&cxlr->dev, "%s: no more target slots available\n",
1580 			dev_name(&cxled->cxld.dev));
1581 		return -ENXIO;
1582 	}
1583 
1584 	/*
1585 	 * Temporarily record the endpoint decoder into the target array. Yes,
1586 	 * this means that userspace can view devices in the wrong position
1587 	 * before the region activates, and must be careful to understand when
1588 	 * it might be racing region autodiscovery.
1589 	 */
1590 	pos = p->nr_targets;
1591 	p->targets[pos] = cxled;
1592 	cxled->pos = pos;
1593 	p->nr_targets++;
1594 
1595 	return 0;
1596 }
1597 
1598 static int cmp_interleave_pos(const void *a, const void *b)
1599 {
1600 	struct cxl_endpoint_decoder *cxled_a = *(typeof(cxled_a) *)a;
1601 	struct cxl_endpoint_decoder *cxled_b = *(typeof(cxled_b) *)b;
1602 
1603 	return cxled_a->pos - cxled_b->pos;
1604 }
1605 
1606 static struct cxl_port *next_port(struct cxl_port *port)
1607 {
1608 	if (!port->parent_dport)
1609 		return NULL;
1610 	return port->parent_dport->port;
1611 }
1612 
1613 static int match_switch_decoder_by_range(struct device *dev, void *data)
1614 {
1615 	struct cxl_switch_decoder *cxlsd;
1616 	struct range *r1, *r2 = data;
1617 
1618 	if (!is_switch_decoder(dev))
1619 		return 0;
1620 
1621 	cxlsd = to_cxl_switch_decoder(dev);
1622 	r1 = &cxlsd->cxld.hpa_range;
1623 
1624 	if (is_root_decoder(dev))
1625 		return range_contains(r1, r2);
1626 	return (r1->start == r2->start && r1->end == r2->end);
1627 }
1628 
1629 static int find_pos_and_ways(struct cxl_port *port, struct range *range,
1630 			     int *pos, int *ways)
1631 {
1632 	struct cxl_switch_decoder *cxlsd;
1633 	struct cxl_port *parent;
1634 	struct device *dev;
1635 	int rc = -ENXIO;
1636 
1637 	parent = next_port(port);
1638 	if (!parent)
1639 		return rc;
1640 
1641 	dev = device_find_child(&parent->dev, range,
1642 				match_switch_decoder_by_range);
1643 	if (!dev) {
1644 		dev_err(port->uport_dev,
1645 			"failed to find decoder mapping %#llx-%#llx\n",
1646 			range->start, range->end);
1647 		return rc;
1648 	}
1649 	cxlsd = to_cxl_switch_decoder(dev);
1650 	*ways = cxlsd->cxld.interleave_ways;
1651 
1652 	for (int i = 0; i < *ways; i++) {
1653 		if (cxlsd->target[i] == port->parent_dport) {
1654 			*pos = i;
1655 			rc = 0;
1656 			break;
1657 		}
1658 	}
1659 	put_device(dev);
1660 
1661 	return rc;
1662 }
1663 
1664 /**
1665  * cxl_calc_interleave_pos() - calculate an endpoint position in a region
1666  * @cxled: endpoint decoder member of given region
1667  *
1668  * The endpoint position is calculated by traversing the topology from
1669  * the endpoint to the root decoder and iteratively applying this
1670  * calculation:
1671  *
1672  *    position = position * parent_ways + parent_pos;
1673  *
1674  * ...where @position is inferred from switch and root decoder target lists.
1675  *
1676  * Return: position >= 0 on success
1677  *	   -ENXIO on failure
1678  */
1679 static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled)
1680 {
1681 	struct cxl_port *iter, *port = cxled_to_port(cxled);
1682 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1683 	struct range *range = &cxled->cxld.hpa_range;
1684 	int parent_ways = 0, parent_pos = 0, pos = 0;
1685 	int rc;
1686 
1687 	/*
1688 	 * Example: the expected interleave order of the 4-way region shown
1689 	 * below is: mem0, mem2, mem1, mem3
1690 	 *
1691 	 *		  root_port
1692 	 *                 /      \
1693 	 *      host_bridge_0    host_bridge_1
1694 	 *        |    |           |    |
1695 	 *       mem0 mem1        mem2 mem3
1696 	 *
1697 	 * In the example the calculator will iterate twice. The first iteration
1698 	 * uses the mem position in the host-bridge and the ways of the host-
1699 	 * bridge to generate the first, or local, position. The second
1700 	 * iteration uses the host-bridge position in the root_port and the ways
1701 	 * of the root_port to refine the position.
1702 	 *
1703 	 * A trace of the calculation per endpoint looks like this:
1704 	 * mem0: pos = 0 * 2 + 0    mem2: pos = 0 * 2 + 0
1705 	 *       pos = 0 * 2 + 0          pos = 0 * 2 + 1
1706 	 *       pos: 0                   pos: 1
1707 	 *
1708 	 * mem1: pos = 0 * 2 + 1    mem3: pos = 0 * 2 + 1
1709 	 *       pos = 1 * 2 + 0          pos = 1 * 2 + 1
1710 	 *       pos: 2                   pos = 3
1711 	 *
1712 	 * Note that while this example is simple, the method applies to more
1713 	 * complex topologies, including those with switches.
1714 	 */
1715 
1716 	/* Iterate from endpoint to root_port refining the position */
1717 	for (iter = port; iter; iter = next_port(iter)) {
1718 		if (is_cxl_root(iter))
1719 			break;
1720 
1721 		rc = find_pos_and_ways(iter, range, &parent_pos, &parent_ways);
1722 		if (rc)
1723 			return rc;
1724 
1725 		pos = pos * parent_ways + parent_pos;
1726 	}
1727 
1728 	dev_dbg(&cxlmd->dev,
1729 		"decoder:%s parent:%s port:%s range:%#llx-%#llx pos:%d\n",
1730 		dev_name(&cxled->cxld.dev), dev_name(cxlmd->dev.parent),
1731 		dev_name(&port->dev), range->start, range->end, pos);
1732 
1733 	return pos;
1734 }
1735 
1736 static int cxl_region_sort_targets(struct cxl_region *cxlr)
1737 {
1738 	struct cxl_region_params *p = &cxlr->params;
1739 	int i, rc = 0;
1740 
1741 	for (i = 0; i < p->nr_targets; i++) {
1742 		struct cxl_endpoint_decoder *cxled = p->targets[i];
1743 
1744 		cxled->pos = cxl_calc_interleave_pos(cxled);
1745 		/*
1746 		 * Record that sorting failed, but still continue to calc
1747 		 * cxled->pos so that follow-on code paths can reliably
1748 		 * do p->targets[cxled->pos] to self-reference their entry.
1749 		 */
1750 		if (cxled->pos < 0)
1751 			rc = -ENXIO;
1752 	}
1753 	/* Keep the cxlr target list in interleave position order */
1754 	sort(p->targets, p->nr_targets, sizeof(p->targets[0]),
1755 	     cmp_interleave_pos, NULL);
1756 
1757 	dev_dbg(&cxlr->dev, "region sort %s\n", rc ? "failed" : "successful");
1758 	return rc;
1759 }
1760 
1761 static int cxl_region_attach(struct cxl_region *cxlr,
1762 			     struct cxl_endpoint_decoder *cxled, int pos)
1763 {
1764 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
1765 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1766 	struct cxl_region_params *p = &cxlr->params;
1767 	struct cxl_port *ep_port, *root_port;
1768 	struct cxl_dport *dport;
1769 	int rc = -ENXIO;
1770 
1771 	rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
1772 				  p->interleave_granularity);
1773 	if (rc) {
1774 		dev_dbg(&cxlr->dev, "%s iw: %d ig: %d is not supported\n",
1775 			dev_name(&cxled->cxld.dev), p->interleave_ways,
1776 			p->interleave_granularity);
1777 		return rc;
1778 	}
1779 
1780 	if (cxled->mode != cxlr->mode) {
1781 		dev_dbg(&cxlr->dev, "%s region mode: %d mismatch: %d\n",
1782 			dev_name(&cxled->cxld.dev), cxlr->mode, cxled->mode);
1783 		return -EINVAL;
1784 	}
1785 
1786 	if (cxled->mode == CXL_DECODER_DEAD) {
1787 		dev_dbg(&cxlr->dev, "%s dead\n", dev_name(&cxled->cxld.dev));
1788 		return -ENODEV;
1789 	}
1790 
1791 	/* all full of members, or interleave config not established? */
1792 	if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) {
1793 		dev_dbg(&cxlr->dev, "region already active\n");
1794 		return -EBUSY;
1795 	} else if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
1796 		dev_dbg(&cxlr->dev, "interleave config missing\n");
1797 		return -ENXIO;
1798 	}
1799 
1800 	if (p->nr_targets >= p->interleave_ways) {
1801 		dev_dbg(&cxlr->dev, "region already has %d endpoints\n",
1802 			p->nr_targets);
1803 		return -EINVAL;
1804 	}
1805 
1806 	ep_port = cxled_to_port(cxled);
1807 	root_port = cxlrd_to_port(cxlrd);
1808 	dport = cxl_find_dport_by_dev(root_port, ep_port->host_bridge);
1809 	if (!dport) {
1810 		dev_dbg(&cxlr->dev, "%s:%s invalid target for %s\n",
1811 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1812 			dev_name(cxlr->dev.parent));
1813 		return -ENXIO;
1814 	}
1815 
1816 	if (cxled->cxld.target_type != cxlr->type) {
1817 		dev_dbg(&cxlr->dev, "%s:%s type mismatch: %d vs %d\n",
1818 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1819 			cxled->cxld.target_type, cxlr->type);
1820 		return -ENXIO;
1821 	}
1822 
1823 	if (!cxled->dpa_res) {
1824 		dev_dbg(&cxlr->dev, "%s:%s: missing DPA allocation.\n",
1825 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev));
1826 		return -ENXIO;
1827 	}
1828 
1829 	if (resource_size(cxled->dpa_res) * p->interleave_ways !=
1830 	    resource_size(p->res)) {
1831 		dev_dbg(&cxlr->dev,
1832 			"%s:%s: decoder-size-%#llx * ways-%d != region-size-%#llx\n",
1833 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1834 			(u64)resource_size(cxled->dpa_res), p->interleave_ways,
1835 			(u64)resource_size(p->res));
1836 		return -EINVAL;
1837 	}
1838 
1839 	if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1840 		int i;
1841 
1842 		rc = cxl_region_attach_auto(cxlr, cxled, pos);
1843 		if (rc)
1844 			return rc;
1845 
1846 		/* await more targets to arrive... */
1847 		if (p->nr_targets < p->interleave_ways)
1848 			return 0;
1849 
1850 		/*
1851 		 * All targets are here, which implies all PCI enumeration that
1852 		 * affects this region has been completed. Walk the topology to
1853 		 * sort the devices into their relative region decode position.
1854 		 */
1855 		rc = cxl_region_sort_targets(cxlr);
1856 		if (rc)
1857 			return rc;
1858 
1859 		for (i = 0; i < p->nr_targets; i++) {
1860 			cxled = p->targets[i];
1861 			ep_port = cxled_to_port(cxled);
1862 			dport = cxl_find_dport_by_dev(root_port,
1863 						      ep_port->host_bridge);
1864 			rc = cxl_region_attach_position(cxlr, cxlrd, cxled,
1865 							dport, i);
1866 			if (rc)
1867 				return rc;
1868 		}
1869 
1870 		rc = cxl_region_setup_targets(cxlr);
1871 		if (rc)
1872 			return rc;
1873 
1874 		/*
1875 		 * If target setup succeeds in the autodiscovery case
1876 		 * then the region is already committed.
1877 		 */
1878 		p->state = CXL_CONFIG_COMMIT;
1879 
1880 		return 0;
1881 	}
1882 
1883 	rc = cxl_region_validate_position(cxlr, cxled, pos);
1884 	if (rc)
1885 		return rc;
1886 
1887 	rc = cxl_region_attach_position(cxlr, cxlrd, cxled, dport, pos);
1888 	if (rc)
1889 		return rc;
1890 
1891 	p->targets[pos] = cxled;
1892 	cxled->pos = pos;
1893 	p->nr_targets++;
1894 
1895 	if (p->nr_targets == p->interleave_ways) {
1896 		rc = cxl_region_setup_targets(cxlr);
1897 		if (rc)
1898 			return rc;
1899 		p->state = CXL_CONFIG_ACTIVE;
1900 	}
1901 
1902 	cxled->cxld.interleave_ways = p->interleave_ways;
1903 	cxled->cxld.interleave_granularity = p->interleave_granularity;
1904 	cxled->cxld.hpa_range = (struct range) {
1905 		.start = p->res->start,
1906 		.end = p->res->end,
1907 	};
1908 
1909 	if (p->nr_targets != p->interleave_ways)
1910 		return 0;
1911 
1912 	/*
1913 	 * Test the auto-discovery position calculator function
1914 	 * against this successfully created user-defined region.
1915 	 * A fail message here means that this interleave config
1916 	 * will fail when presented as CXL_REGION_F_AUTO.
1917 	 */
1918 	for (int i = 0; i < p->nr_targets; i++) {
1919 		struct cxl_endpoint_decoder *cxled = p->targets[i];
1920 		int test_pos;
1921 
1922 		test_pos = cxl_calc_interleave_pos(cxled);
1923 		dev_dbg(&cxled->cxld.dev,
1924 			"Test cxl_calc_interleave_pos(): %s test_pos:%d cxled->pos:%d\n",
1925 			(test_pos == cxled->pos) ? "success" : "fail",
1926 			test_pos, cxled->pos);
1927 	}
1928 
1929 	return 0;
1930 }
1931 
1932 static int cxl_region_detach(struct cxl_endpoint_decoder *cxled)
1933 {
1934 	struct cxl_port *iter, *ep_port = cxled_to_port(cxled);
1935 	struct cxl_region *cxlr = cxled->cxld.region;
1936 	struct cxl_region_params *p;
1937 	int rc = 0;
1938 
1939 	lockdep_assert_held_write(&cxl_region_rwsem);
1940 
1941 	if (!cxlr)
1942 		return 0;
1943 
1944 	p = &cxlr->params;
1945 	get_device(&cxlr->dev);
1946 
1947 	if (p->state > CXL_CONFIG_ACTIVE) {
1948 		/*
1949 		 * TODO: tear down all impacted regions if a device is
1950 		 * removed out of order
1951 		 */
1952 		rc = cxl_region_decode_reset(cxlr, p->interleave_ways);
1953 		if (rc)
1954 			goto out;
1955 		p->state = CXL_CONFIG_ACTIVE;
1956 	}
1957 
1958 	for (iter = ep_port; !is_cxl_root(iter);
1959 	     iter = to_cxl_port(iter->dev.parent))
1960 		cxl_port_detach_region(iter, cxlr, cxled);
1961 
1962 	if (cxled->pos < 0 || cxled->pos >= p->interleave_ways ||
1963 	    p->targets[cxled->pos] != cxled) {
1964 		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1965 
1966 		dev_WARN_ONCE(&cxlr->dev, 1, "expected %s:%s at position %d\n",
1967 			      dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1968 			      cxled->pos);
1969 		goto out;
1970 	}
1971 
1972 	if (p->state == CXL_CONFIG_ACTIVE) {
1973 		p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
1974 		cxl_region_teardown_targets(cxlr);
1975 	}
1976 	p->targets[cxled->pos] = NULL;
1977 	p->nr_targets--;
1978 	cxled->cxld.hpa_range = (struct range) {
1979 		.start = 0,
1980 		.end = -1,
1981 	};
1982 
1983 	/* notify the region driver that one of its targets has departed */
1984 	up_write(&cxl_region_rwsem);
1985 	device_release_driver(&cxlr->dev);
1986 	down_write(&cxl_region_rwsem);
1987 out:
1988 	put_device(&cxlr->dev);
1989 	return rc;
1990 }
1991 
1992 void cxl_decoder_kill_region(struct cxl_endpoint_decoder *cxled)
1993 {
1994 	down_write(&cxl_region_rwsem);
1995 	cxled->mode = CXL_DECODER_DEAD;
1996 	cxl_region_detach(cxled);
1997 	up_write(&cxl_region_rwsem);
1998 }
1999 
2000 static int attach_target(struct cxl_region *cxlr,
2001 			 struct cxl_endpoint_decoder *cxled, int pos,
2002 			 unsigned int state)
2003 {
2004 	int rc = 0;
2005 
2006 	if (state == TASK_INTERRUPTIBLE)
2007 		rc = down_write_killable(&cxl_region_rwsem);
2008 	else
2009 		down_write(&cxl_region_rwsem);
2010 	if (rc)
2011 		return rc;
2012 
2013 	down_read(&cxl_dpa_rwsem);
2014 	rc = cxl_region_attach(cxlr, cxled, pos);
2015 	up_read(&cxl_dpa_rwsem);
2016 	up_write(&cxl_region_rwsem);
2017 	return rc;
2018 }
2019 
2020 static int detach_target(struct cxl_region *cxlr, int pos)
2021 {
2022 	struct cxl_region_params *p = &cxlr->params;
2023 	int rc;
2024 
2025 	rc = down_write_killable(&cxl_region_rwsem);
2026 	if (rc)
2027 		return rc;
2028 
2029 	if (pos >= p->interleave_ways) {
2030 		dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
2031 			p->interleave_ways);
2032 		rc = -ENXIO;
2033 		goto out;
2034 	}
2035 
2036 	if (!p->targets[pos]) {
2037 		rc = 0;
2038 		goto out;
2039 	}
2040 
2041 	rc = cxl_region_detach(p->targets[pos]);
2042 out:
2043 	up_write(&cxl_region_rwsem);
2044 	return rc;
2045 }
2046 
2047 static size_t store_targetN(struct cxl_region *cxlr, const char *buf, int pos,
2048 			    size_t len)
2049 {
2050 	int rc;
2051 
2052 	if (sysfs_streq(buf, "\n"))
2053 		rc = detach_target(cxlr, pos);
2054 	else {
2055 		struct device *dev;
2056 
2057 		dev = bus_find_device_by_name(&cxl_bus_type, NULL, buf);
2058 		if (!dev)
2059 			return -ENODEV;
2060 
2061 		if (!is_endpoint_decoder(dev)) {
2062 			rc = -EINVAL;
2063 			goto out;
2064 		}
2065 
2066 		rc = attach_target(cxlr, to_cxl_endpoint_decoder(dev), pos,
2067 				   TASK_INTERRUPTIBLE);
2068 out:
2069 		put_device(dev);
2070 	}
2071 
2072 	if (rc < 0)
2073 		return rc;
2074 	return len;
2075 }
2076 
2077 #define TARGET_ATTR_RW(n)                                              \
2078 static ssize_t target##n##_show(                                       \
2079 	struct device *dev, struct device_attribute *attr, char *buf)  \
2080 {                                                                      \
2081 	return show_targetN(to_cxl_region(dev), buf, (n));             \
2082 }                                                                      \
2083 static ssize_t target##n##_store(struct device *dev,                   \
2084 				 struct device_attribute *attr,        \
2085 				 const char *buf, size_t len)          \
2086 {                                                                      \
2087 	return store_targetN(to_cxl_region(dev), buf, (n), len);       \
2088 }                                                                      \
2089 static DEVICE_ATTR_RW(target##n)
2090 
2091 TARGET_ATTR_RW(0);
2092 TARGET_ATTR_RW(1);
2093 TARGET_ATTR_RW(2);
2094 TARGET_ATTR_RW(3);
2095 TARGET_ATTR_RW(4);
2096 TARGET_ATTR_RW(5);
2097 TARGET_ATTR_RW(6);
2098 TARGET_ATTR_RW(7);
2099 TARGET_ATTR_RW(8);
2100 TARGET_ATTR_RW(9);
2101 TARGET_ATTR_RW(10);
2102 TARGET_ATTR_RW(11);
2103 TARGET_ATTR_RW(12);
2104 TARGET_ATTR_RW(13);
2105 TARGET_ATTR_RW(14);
2106 TARGET_ATTR_RW(15);
2107 
2108 static struct attribute *target_attrs[] = {
2109 	&dev_attr_target0.attr,
2110 	&dev_attr_target1.attr,
2111 	&dev_attr_target2.attr,
2112 	&dev_attr_target3.attr,
2113 	&dev_attr_target4.attr,
2114 	&dev_attr_target5.attr,
2115 	&dev_attr_target6.attr,
2116 	&dev_attr_target7.attr,
2117 	&dev_attr_target8.attr,
2118 	&dev_attr_target9.attr,
2119 	&dev_attr_target10.attr,
2120 	&dev_attr_target11.attr,
2121 	&dev_attr_target12.attr,
2122 	&dev_attr_target13.attr,
2123 	&dev_attr_target14.attr,
2124 	&dev_attr_target15.attr,
2125 	NULL,
2126 };
2127 
2128 static umode_t cxl_region_target_visible(struct kobject *kobj,
2129 					 struct attribute *a, int n)
2130 {
2131 	struct device *dev = kobj_to_dev(kobj);
2132 	struct cxl_region *cxlr = to_cxl_region(dev);
2133 	struct cxl_region_params *p = &cxlr->params;
2134 
2135 	if (n < p->interleave_ways)
2136 		return a->mode;
2137 	return 0;
2138 }
2139 
2140 static const struct attribute_group cxl_region_target_group = {
2141 	.attrs = target_attrs,
2142 	.is_visible = cxl_region_target_visible,
2143 };
2144 
2145 static const struct attribute_group *get_cxl_region_target_group(void)
2146 {
2147 	return &cxl_region_target_group;
2148 }
2149 
2150 static const struct attribute_group *region_groups[] = {
2151 	&cxl_base_attribute_group,
2152 	&cxl_region_group,
2153 	&cxl_region_target_group,
2154 	NULL,
2155 };
2156 
2157 static void cxl_region_release(struct device *dev)
2158 {
2159 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent);
2160 	struct cxl_region *cxlr = to_cxl_region(dev);
2161 	int id = atomic_read(&cxlrd->region_id);
2162 
2163 	/*
2164 	 * Try to reuse the recently idled id rather than the cached
2165 	 * next id to prevent the region id space from increasing
2166 	 * unnecessarily.
2167 	 */
2168 	if (cxlr->id < id)
2169 		if (atomic_try_cmpxchg(&cxlrd->region_id, &id, cxlr->id)) {
2170 			memregion_free(id);
2171 			goto out;
2172 		}
2173 
2174 	memregion_free(cxlr->id);
2175 out:
2176 	put_device(dev->parent);
2177 	kfree(cxlr);
2178 }
2179 
2180 const struct device_type cxl_region_type = {
2181 	.name = "cxl_region",
2182 	.release = cxl_region_release,
2183 	.groups = region_groups
2184 };
2185 
2186 bool is_cxl_region(struct device *dev)
2187 {
2188 	return dev->type == &cxl_region_type;
2189 }
2190 EXPORT_SYMBOL_NS_GPL(is_cxl_region, CXL);
2191 
2192 static struct cxl_region *to_cxl_region(struct device *dev)
2193 {
2194 	if (dev_WARN_ONCE(dev, dev->type != &cxl_region_type,
2195 			  "not a cxl_region device\n"))
2196 		return NULL;
2197 
2198 	return container_of(dev, struct cxl_region, dev);
2199 }
2200 
2201 static void unregister_region(void *dev)
2202 {
2203 	struct cxl_region *cxlr = to_cxl_region(dev);
2204 	struct cxl_region_params *p = &cxlr->params;
2205 	int i;
2206 
2207 	device_del(dev);
2208 
2209 	/*
2210 	 * Now that region sysfs is shutdown, the parameter block is now
2211 	 * read-only, so no need to hold the region rwsem to access the
2212 	 * region parameters.
2213 	 */
2214 	for (i = 0; i < p->interleave_ways; i++)
2215 		detach_target(cxlr, i);
2216 
2217 	cxl_region_iomem_release(cxlr);
2218 	put_device(dev);
2219 }
2220 
2221 static struct lock_class_key cxl_region_key;
2222 
2223 static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id)
2224 {
2225 	struct cxl_region *cxlr;
2226 	struct device *dev;
2227 
2228 	cxlr = kzalloc(sizeof(*cxlr), GFP_KERNEL);
2229 	if (!cxlr) {
2230 		memregion_free(id);
2231 		return ERR_PTR(-ENOMEM);
2232 	}
2233 
2234 	dev = &cxlr->dev;
2235 	device_initialize(dev);
2236 	lockdep_set_class(&dev->mutex, &cxl_region_key);
2237 	dev->parent = &cxlrd->cxlsd.cxld.dev;
2238 	/*
2239 	 * Keep root decoder pinned through cxl_region_release to fixup
2240 	 * region id allocations
2241 	 */
2242 	get_device(dev->parent);
2243 	device_set_pm_not_required(dev);
2244 	dev->bus = &cxl_bus_type;
2245 	dev->type = &cxl_region_type;
2246 	cxlr->id = id;
2247 
2248 	return cxlr;
2249 }
2250 
2251 /**
2252  * devm_cxl_add_region - Adds a region to a decoder
2253  * @cxlrd: root decoder
2254  * @id: memregion id to create, or memregion_free() on failure
2255  * @mode: mode for the endpoint decoders of this region
2256  * @type: select whether this is an expander or accelerator (type-2 or type-3)
2257  *
2258  * This is the second step of region initialization. Regions exist within an
2259  * address space which is mapped by a @cxlrd.
2260  *
2261  * Return: 0 if the region was added to the @cxlrd, else returns negative error
2262  * code. The region will be named "regionZ" where Z is the unique region number.
2263  */
2264 static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd,
2265 					      int id,
2266 					      enum cxl_decoder_mode mode,
2267 					      enum cxl_decoder_type type)
2268 {
2269 	struct cxl_port *port = to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
2270 	struct cxl_region *cxlr;
2271 	struct device *dev;
2272 	int rc;
2273 
2274 	cxlr = cxl_region_alloc(cxlrd, id);
2275 	if (IS_ERR(cxlr))
2276 		return cxlr;
2277 	cxlr->mode = mode;
2278 	cxlr->type = type;
2279 
2280 	dev = &cxlr->dev;
2281 	rc = dev_set_name(dev, "region%d", id);
2282 	if (rc)
2283 		goto err;
2284 
2285 	rc = device_add(dev);
2286 	if (rc)
2287 		goto err;
2288 
2289 	rc = devm_add_action_or_reset(port->uport_dev, unregister_region, cxlr);
2290 	if (rc)
2291 		return ERR_PTR(rc);
2292 
2293 	dev_dbg(port->uport_dev, "%s: created %s\n",
2294 		dev_name(&cxlrd->cxlsd.cxld.dev), dev_name(dev));
2295 	return cxlr;
2296 
2297 err:
2298 	put_device(dev);
2299 	return ERR_PTR(rc);
2300 }
2301 
2302 static ssize_t __create_region_show(struct cxl_root_decoder *cxlrd, char *buf)
2303 {
2304 	return sysfs_emit(buf, "region%u\n", atomic_read(&cxlrd->region_id));
2305 }
2306 
2307 static ssize_t create_pmem_region_show(struct device *dev,
2308 				       struct device_attribute *attr, char *buf)
2309 {
2310 	return __create_region_show(to_cxl_root_decoder(dev), buf);
2311 }
2312 
2313 static ssize_t create_ram_region_show(struct device *dev,
2314 				      struct device_attribute *attr, char *buf)
2315 {
2316 	return __create_region_show(to_cxl_root_decoder(dev), buf);
2317 }
2318 
2319 static struct cxl_region *__create_region(struct cxl_root_decoder *cxlrd,
2320 					  enum cxl_decoder_mode mode, int id)
2321 {
2322 	int rc;
2323 
2324 	switch (mode) {
2325 	case CXL_DECODER_RAM:
2326 	case CXL_DECODER_PMEM:
2327 		break;
2328 	default:
2329 		dev_err(&cxlrd->cxlsd.cxld.dev, "unsupported mode %d\n", mode);
2330 		return ERR_PTR(-EINVAL);
2331 	}
2332 
2333 	rc = memregion_alloc(GFP_KERNEL);
2334 	if (rc < 0)
2335 		return ERR_PTR(rc);
2336 
2337 	if (atomic_cmpxchg(&cxlrd->region_id, id, rc) != id) {
2338 		memregion_free(rc);
2339 		return ERR_PTR(-EBUSY);
2340 	}
2341 
2342 	return devm_cxl_add_region(cxlrd, id, mode, CXL_DECODER_HOSTONLYMEM);
2343 }
2344 
2345 static ssize_t create_pmem_region_store(struct device *dev,
2346 					struct device_attribute *attr,
2347 					const char *buf, size_t len)
2348 {
2349 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2350 	struct cxl_region *cxlr;
2351 	int rc, id;
2352 
2353 	rc = sscanf(buf, "region%d\n", &id);
2354 	if (rc != 1)
2355 		return -EINVAL;
2356 
2357 	cxlr = __create_region(cxlrd, CXL_DECODER_PMEM, id);
2358 	if (IS_ERR(cxlr))
2359 		return PTR_ERR(cxlr);
2360 
2361 	return len;
2362 }
2363 DEVICE_ATTR_RW(create_pmem_region);
2364 
2365 static ssize_t create_ram_region_store(struct device *dev,
2366 				       struct device_attribute *attr,
2367 				       const char *buf, size_t len)
2368 {
2369 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2370 	struct cxl_region *cxlr;
2371 	int rc, id;
2372 
2373 	rc = sscanf(buf, "region%d\n", &id);
2374 	if (rc != 1)
2375 		return -EINVAL;
2376 
2377 	cxlr = __create_region(cxlrd, CXL_DECODER_RAM, id);
2378 	if (IS_ERR(cxlr))
2379 		return PTR_ERR(cxlr);
2380 
2381 	return len;
2382 }
2383 DEVICE_ATTR_RW(create_ram_region);
2384 
2385 static ssize_t region_show(struct device *dev, struct device_attribute *attr,
2386 			   char *buf)
2387 {
2388 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
2389 	ssize_t rc;
2390 
2391 	rc = down_read_interruptible(&cxl_region_rwsem);
2392 	if (rc)
2393 		return rc;
2394 
2395 	if (cxld->region)
2396 		rc = sysfs_emit(buf, "%s\n", dev_name(&cxld->region->dev));
2397 	else
2398 		rc = sysfs_emit(buf, "\n");
2399 	up_read(&cxl_region_rwsem);
2400 
2401 	return rc;
2402 }
2403 DEVICE_ATTR_RO(region);
2404 
2405 static struct cxl_region *
2406 cxl_find_region_by_name(struct cxl_root_decoder *cxlrd, const char *name)
2407 {
2408 	struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
2409 	struct device *region_dev;
2410 
2411 	region_dev = device_find_child_by_name(&cxld->dev, name);
2412 	if (!region_dev)
2413 		return ERR_PTR(-ENODEV);
2414 
2415 	return to_cxl_region(region_dev);
2416 }
2417 
2418 static ssize_t delete_region_store(struct device *dev,
2419 				   struct device_attribute *attr,
2420 				   const char *buf, size_t len)
2421 {
2422 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2423 	struct cxl_port *port = to_cxl_port(dev->parent);
2424 	struct cxl_region *cxlr;
2425 
2426 	cxlr = cxl_find_region_by_name(cxlrd, buf);
2427 	if (IS_ERR(cxlr))
2428 		return PTR_ERR(cxlr);
2429 
2430 	devm_release_action(port->uport_dev, unregister_region, cxlr);
2431 	put_device(&cxlr->dev);
2432 
2433 	return len;
2434 }
2435 DEVICE_ATTR_WO(delete_region);
2436 
2437 static void cxl_pmem_region_release(struct device *dev)
2438 {
2439 	struct cxl_pmem_region *cxlr_pmem = to_cxl_pmem_region(dev);
2440 	int i;
2441 
2442 	for (i = 0; i < cxlr_pmem->nr_mappings; i++) {
2443 		struct cxl_memdev *cxlmd = cxlr_pmem->mapping[i].cxlmd;
2444 
2445 		put_device(&cxlmd->dev);
2446 	}
2447 
2448 	kfree(cxlr_pmem);
2449 }
2450 
2451 static const struct attribute_group *cxl_pmem_region_attribute_groups[] = {
2452 	&cxl_base_attribute_group,
2453 	NULL,
2454 };
2455 
2456 const struct device_type cxl_pmem_region_type = {
2457 	.name = "cxl_pmem_region",
2458 	.release = cxl_pmem_region_release,
2459 	.groups = cxl_pmem_region_attribute_groups,
2460 };
2461 
2462 bool is_cxl_pmem_region(struct device *dev)
2463 {
2464 	return dev->type == &cxl_pmem_region_type;
2465 }
2466 EXPORT_SYMBOL_NS_GPL(is_cxl_pmem_region, CXL);
2467 
2468 struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev)
2469 {
2470 	if (dev_WARN_ONCE(dev, !is_cxl_pmem_region(dev),
2471 			  "not a cxl_pmem_region device\n"))
2472 		return NULL;
2473 	return container_of(dev, struct cxl_pmem_region, dev);
2474 }
2475 EXPORT_SYMBOL_NS_GPL(to_cxl_pmem_region, CXL);
2476 
2477 struct cxl_poison_context {
2478 	struct cxl_port *port;
2479 	enum cxl_decoder_mode mode;
2480 	u64 offset;
2481 };
2482 
2483 static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
2484 				   struct cxl_poison_context *ctx)
2485 {
2486 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
2487 	u64 offset, length;
2488 	int rc = 0;
2489 
2490 	/*
2491 	 * Collect poison for the remaining unmapped resources
2492 	 * after poison is collected by committed endpoints.
2493 	 *
2494 	 * Knowing that PMEM must always follow RAM, get poison
2495 	 * for unmapped resources based on the last decoder's mode:
2496 	 *	ram: scan remains of ram range, then any pmem range
2497 	 *	pmem: scan remains of pmem range
2498 	 */
2499 
2500 	if (ctx->mode == CXL_DECODER_RAM) {
2501 		offset = ctx->offset;
2502 		length = resource_size(&cxlds->ram_res) - offset;
2503 		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
2504 		if (rc == -EFAULT)
2505 			rc = 0;
2506 		if (rc)
2507 			return rc;
2508 	}
2509 	if (ctx->mode == CXL_DECODER_PMEM) {
2510 		offset = ctx->offset;
2511 		length = resource_size(&cxlds->dpa_res) - offset;
2512 		if (!length)
2513 			return 0;
2514 	} else if (resource_size(&cxlds->pmem_res)) {
2515 		offset = cxlds->pmem_res.start;
2516 		length = resource_size(&cxlds->pmem_res);
2517 	} else {
2518 		return 0;
2519 	}
2520 
2521 	return cxl_mem_get_poison(cxlmd, offset, length, NULL);
2522 }
2523 
2524 static int poison_by_decoder(struct device *dev, void *arg)
2525 {
2526 	struct cxl_poison_context *ctx = arg;
2527 	struct cxl_endpoint_decoder *cxled;
2528 	struct cxl_memdev *cxlmd;
2529 	u64 offset, length;
2530 	int rc = 0;
2531 
2532 	if (!is_endpoint_decoder(dev))
2533 		return rc;
2534 
2535 	cxled = to_cxl_endpoint_decoder(dev);
2536 	if (!cxled->dpa_res || !resource_size(cxled->dpa_res))
2537 		return rc;
2538 
2539 	/*
2540 	 * Regions are only created with single mode decoders: pmem or ram.
2541 	 * Linux does not support mixed mode decoders. This means that
2542 	 * reading poison per endpoint decoder adheres to the requirement
2543 	 * that poison reads of pmem and ram must be separated.
2544 	 * CXL 3.0 Spec 8.2.9.8.4.1
2545 	 */
2546 	if (cxled->mode == CXL_DECODER_MIXED) {
2547 		dev_dbg(dev, "poison list read unsupported in mixed mode\n");
2548 		return rc;
2549 	}
2550 
2551 	cxlmd = cxled_to_memdev(cxled);
2552 	if (cxled->skip) {
2553 		offset = cxled->dpa_res->start - cxled->skip;
2554 		length = cxled->skip;
2555 		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
2556 		if (rc == -EFAULT && cxled->mode == CXL_DECODER_RAM)
2557 			rc = 0;
2558 		if (rc)
2559 			return rc;
2560 	}
2561 
2562 	offset = cxled->dpa_res->start;
2563 	length = cxled->dpa_res->end - offset + 1;
2564 	rc = cxl_mem_get_poison(cxlmd, offset, length, cxled->cxld.region);
2565 	if (rc == -EFAULT && cxled->mode == CXL_DECODER_RAM)
2566 		rc = 0;
2567 	if (rc)
2568 		return rc;
2569 
2570 	/* Iterate until commit_end is reached */
2571 	if (cxled->cxld.id == ctx->port->commit_end) {
2572 		ctx->offset = cxled->dpa_res->end + 1;
2573 		ctx->mode = cxled->mode;
2574 		return 1;
2575 	}
2576 
2577 	return 0;
2578 }
2579 
2580 int cxl_get_poison_by_endpoint(struct cxl_port *port)
2581 {
2582 	struct cxl_poison_context ctx;
2583 	int rc = 0;
2584 
2585 	ctx = (struct cxl_poison_context) {
2586 		.port = port
2587 	};
2588 
2589 	rc = device_for_each_child(&port->dev, &ctx, poison_by_decoder);
2590 	if (rc == 1)
2591 		rc = cxl_get_poison_unmapped(to_cxl_memdev(port->uport_dev),
2592 					     &ctx);
2593 
2594 	return rc;
2595 }
2596 
2597 struct cxl_dpa_to_region_context {
2598 	struct cxl_region *cxlr;
2599 	u64 dpa;
2600 };
2601 
2602 static int __cxl_dpa_to_region(struct device *dev, void *arg)
2603 {
2604 	struct cxl_dpa_to_region_context *ctx = arg;
2605 	struct cxl_endpoint_decoder *cxled;
2606 	struct cxl_region *cxlr;
2607 	u64 dpa = ctx->dpa;
2608 
2609 	if (!is_endpoint_decoder(dev))
2610 		return 0;
2611 
2612 	cxled = to_cxl_endpoint_decoder(dev);
2613 	if (!cxled || !cxled->dpa_res || !resource_size(cxled->dpa_res))
2614 		return 0;
2615 
2616 	if (dpa > cxled->dpa_res->end || dpa < cxled->dpa_res->start)
2617 		return 0;
2618 
2619 	/*
2620 	 * Stop the region search (return 1) when an endpoint mapping is
2621 	 * found. The region may not be fully constructed so offering
2622 	 * the cxlr in the context structure is not guaranteed.
2623 	 */
2624 	cxlr = cxled->cxld.region;
2625 	if (cxlr)
2626 		dev_dbg(dev, "dpa:0x%llx mapped in region:%s\n", dpa,
2627 			dev_name(&cxlr->dev));
2628 	else
2629 		dev_dbg(dev, "dpa:0x%llx mapped in endpoint:%s\n", dpa,
2630 			dev_name(dev));
2631 
2632 	ctx->cxlr = cxlr;
2633 
2634 	return 1;
2635 }
2636 
2637 struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa)
2638 {
2639 	struct cxl_dpa_to_region_context ctx;
2640 	struct cxl_port *port;
2641 
2642 	ctx = (struct cxl_dpa_to_region_context) {
2643 		.dpa = dpa,
2644 	};
2645 	port = cxlmd->endpoint;
2646 	if (port && is_cxl_endpoint(port) && cxl_num_decoders_committed(port))
2647 		device_for_each_child(&port->dev, &ctx, __cxl_dpa_to_region);
2648 
2649 	return ctx.cxlr;
2650 }
2651 
2652 static struct lock_class_key cxl_pmem_region_key;
2653 
2654 static struct cxl_pmem_region *cxl_pmem_region_alloc(struct cxl_region *cxlr)
2655 {
2656 	struct cxl_region_params *p = &cxlr->params;
2657 	struct cxl_nvdimm_bridge *cxl_nvb;
2658 	struct cxl_pmem_region *cxlr_pmem;
2659 	struct device *dev;
2660 	int i;
2661 
2662 	down_read(&cxl_region_rwsem);
2663 	if (p->state != CXL_CONFIG_COMMIT) {
2664 		cxlr_pmem = ERR_PTR(-ENXIO);
2665 		goto out;
2666 	}
2667 
2668 	cxlr_pmem = kzalloc(struct_size(cxlr_pmem, mapping, p->nr_targets),
2669 			    GFP_KERNEL);
2670 	if (!cxlr_pmem) {
2671 		cxlr_pmem = ERR_PTR(-ENOMEM);
2672 		goto out;
2673 	}
2674 
2675 	cxlr_pmem->hpa_range.start = p->res->start;
2676 	cxlr_pmem->hpa_range.end = p->res->end;
2677 
2678 	/* Snapshot the region configuration underneath the cxl_region_rwsem */
2679 	cxlr_pmem->nr_mappings = p->nr_targets;
2680 	for (i = 0; i < p->nr_targets; i++) {
2681 		struct cxl_endpoint_decoder *cxled = p->targets[i];
2682 		struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
2683 		struct cxl_pmem_region_mapping *m = &cxlr_pmem->mapping[i];
2684 
2685 		/*
2686 		 * Regions never span CXL root devices, so by definition the
2687 		 * bridge for one device is the same for all.
2688 		 */
2689 		if (i == 0) {
2690 			cxl_nvb = cxl_find_nvdimm_bridge(cxlmd);
2691 			if (!cxl_nvb) {
2692 				kfree(cxlr_pmem);
2693 				cxlr_pmem = ERR_PTR(-ENODEV);
2694 				goto out;
2695 			}
2696 			cxlr->cxl_nvb = cxl_nvb;
2697 		}
2698 		m->cxlmd = cxlmd;
2699 		get_device(&cxlmd->dev);
2700 		m->start = cxled->dpa_res->start;
2701 		m->size = resource_size(cxled->dpa_res);
2702 		m->position = i;
2703 	}
2704 
2705 	dev = &cxlr_pmem->dev;
2706 	cxlr_pmem->cxlr = cxlr;
2707 	cxlr->cxlr_pmem = cxlr_pmem;
2708 	device_initialize(dev);
2709 	lockdep_set_class(&dev->mutex, &cxl_pmem_region_key);
2710 	device_set_pm_not_required(dev);
2711 	dev->parent = &cxlr->dev;
2712 	dev->bus = &cxl_bus_type;
2713 	dev->type = &cxl_pmem_region_type;
2714 out:
2715 	up_read(&cxl_region_rwsem);
2716 
2717 	return cxlr_pmem;
2718 }
2719 
2720 static void cxl_dax_region_release(struct device *dev)
2721 {
2722 	struct cxl_dax_region *cxlr_dax = to_cxl_dax_region(dev);
2723 
2724 	kfree(cxlr_dax);
2725 }
2726 
2727 static const struct attribute_group *cxl_dax_region_attribute_groups[] = {
2728 	&cxl_base_attribute_group,
2729 	NULL,
2730 };
2731 
2732 const struct device_type cxl_dax_region_type = {
2733 	.name = "cxl_dax_region",
2734 	.release = cxl_dax_region_release,
2735 	.groups = cxl_dax_region_attribute_groups,
2736 };
2737 
2738 static bool is_cxl_dax_region(struct device *dev)
2739 {
2740 	return dev->type == &cxl_dax_region_type;
2741 }
2742 
2743 struct cxl_dax_region *to_cxl_dax_region(struct device *dev)
2744 {
2745 	if (dev_WARN_ONCE(dev, !is_cxl_dax_region(dev),
2746 			  "not a cxl_dax_region device\n"))
2747 		return NULL;
2748 	return container_of(dev, struct cxl_dax_region, dev);
2749 }
2750 EXPORT_SYMBOL_NS_GPL(to_cxl_dax_region, CXL);
2751 
2752 static struct lock_class_key cxl_dax_region_key;
2753 
2754 static struct cxl_dax_region *cxl_dax_region_alloc(struct cxl_region *cxlr)
2755 {
2756 	struct cxl_region_params *p = &cxlr->params;
2757 	struct cxl_dax_region *cxlr_dax;
2758 	struct device *dev;
2759 
2760 	down_read(&cxl_region_rwsem);
2761 	if (p->state != CXL_CONFIG_COMMIT) {
2762 		cxlr_dax = ERR_PTR(-ENXIO);
2763 		goto out;
2764 	}
2765 
2766 	cxlr_dax = kzalloc(sizeof(*cxlr_dax), GFP_KERNEL);
2767 	if (!cxlr_dax) {
2768 		cxlr_dax = ERR_PTR(-ENOMEM);
2769 		goto out;
2770 	}
2771 
2772 	cxlr_dax->hpa_range.start = p->res->start;
2773 	cxlr_dax->hpa_range.end = p->res->end;
2774 
2775 	dev = &cxlr_dax->dev;
2776 	cxlr_dax->cxlr = cxlr;
2777 	device_initialize(dev);
2778 	lockdep_set_class(&dev->mutex, &cxl_dax_region_key);
2779 	device_set_pm_not_required(dev);
2780 	dev->parent = &cxlr->dev;
2781 	dev->bus = &cxl_bus_type;
2782 	dev->type = &cxl_dax_region_type;
2783 out:
2784 	up_read(&cxl_region_rwsem);
2785 
2786 	return cxlr_dax;
2787 }
2788 
2789 static void cxlr_pmem_unregister(void *_cxlr_pmem)
2790 {
2791 	struct cxl_pmem_region *cxlr_pmem = _cxlr_pmem;
2792 	struct cxl_region *cxlr = cxlr_pmem->cxlr;
2793 	struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
2794 
2795 	/*
2796 	 * Either the bridge is in ->remove() context under the device_lock(),
2797 	 * or cxlr_release_nvdimm() is cancelling the bridge's release action
2798 	 * for @cxlr_pmem and doing it itself (while manually holding the bridge
2799 	 * lock).
2800 	 */
2801 	device_lock_assert(&cxl_nvb->dev);
2802 	cxlr->cxlr_pmem = NULL;
2803 	cxlr_pmem->cxlr = NULL;
2804 	device_unregister(&cxlr_pmem->dev);
2805 }
2806 
2807 static void cxlr_release_nvdimm(void *_cxlr)
2808 {
2809 	struct cxl_region *cxlr = _cxlr;
2810 	struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
2811 
2812 	device_lock(&cxl_nvb->dev);
2813 	if (cxlr->cxlr_pmem)
2814 		devm_release_action(&cxl_nvb->dev, cxlr_pmem_unregister,
2815 				    cxlr->cxlr_pmem);
2816 	device_unlock(&cxl_nvb->dev);
2817 	cxlr->cxl_nvb = NULL;
2818 	put_device(&cxl_nvb->dev);
2819 }
2820 
2821 /**
2822  * devm_cxl_add_pmem_region() - add a cxl_region-to-nd_region bridge
2823  * @cxlr: parent CXL region for this pmem region bridge device
2824  *
2825  * Return: 0 on success negative error code on failure.
2826  */
2827 static int devm_cxl_add_pmem_region(struct cxl_region *cxlr)
2828 {
2829 	struct cxl_pmem_region *cxlr_pmem;
2830 	struct cxl_nvdimm_bridge *cxl_nvb;
2831 	struct device *dev;
2832 	int rc;
2833 
2834 	cxlr_pmem = cxl_pmem_region_alloc(cxlr);
2835 	if (IS_ERR(cxlr_pmem))
2836 		return PTR_ERR(cxlr_pmem);
2837 	cxl_nvb = cxlr->cxl_nvb;
2838 
2839 	dev = &cxlr_pmem->dev;
2840 	rc = dev_set_name(dev, "pmem_region%d", cxlr->id);
2841 	if (rc)
2842 		goto err;
2843 
2844 	rc = device_add(dev);
2845 	if (rc)
2846 		goto err;
2847 
2848 	dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
2849 		dev_name(dev));
2850 
2851 	device_lock(&cxl_nvb->dev);
2852 	if (cxl_nvb->dev.driver)
2853 		rc = devm_add_action_or_reset(&cxl_nvb->dev,
2854 					      cxlr_pmem_unregister, cxlr_pmem);
2855 	else
2856 		rc = -ENXIO;
2857 	device_unlock(&cxl_nvb->dev);
2858 
2859 	if (rc)
2860 		goto err_bridge;
2861 
2862 	/* @cxlr carries a reference on @cxl_nvb until cxlr_release_nvdimm */
2863 	return devm_add_action_or_reset(&cxlr->dev, cxlr_release_nvdimm, cxlr);
2864 
2865 err:
2866 	put_device(dev);
2867 err_bridge:
2868 	put_device(&cxl_nvb->dev);
2869 	cxlr->cxl_nvb = NULL;
2870 	return rc;
2871 }
2872 
2873 static void cxlr_dax_unregister(void *_cxlr_dax)
2874 {
2875 	struct cxl_dax_region *cxlr_dax = _cxlr_dax;
2876 
2877 	device_unregister(&cxlr_dax->dev);
2878 }
2879 
2880 static int devm_cxl_add_dax_region(struct cxl_region *cxlr)
2881 {
2882 	struct cxl_dax_region *cxlr_dax;
2883 	struct device *dev;
2884 	int rc;
2885 
2886 	cxlr_dax = cxl_dax_region_alloc(cxlr);
2887 	if (IS_ERR(cxlr_dax))
2888 		return PTR_ERR(cxlr_dax);
2889 
2890 	dev = &cxlr_dax->dev;
2891 	rc = dev_set_name(dev, "dax_region%d", cxlr->id);
2892 	if (rc)
2893 		goto err;
2894 
2895 	rc = device_add(dev);
2896 	if (rc)
2897 		goto err;
2898 
2899 	dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
2900 		dev_name(dev));
2901 
2902 	return devm_add_action_or_reset(&cxlr->dev, cxlr_dax_unregister,
2903 					cxlr_dax);
2904 err:
2905 	put_device(dev);
2906 	return rc;
2907 }
2908 
2909 static int match_root_decoder_by_range(struct device *dev, void *data)
2910 {
2911 	struct range *r1, *r2 = data;
2912 	struct cxl_root_decoder *cxlrd;
2913 
2914 	if (!is_root_decoder(dev))
2915 		return 0;
2916 
2917 	cxlrd = to_cxl_root_decoder(dev);
2918 	r1 = &cxlrd->cxlsd.cxld.hpa_range;
2919 	return range_contains(r1, r2);
2920 }
2921 
2922 static int match_region_by_range(struct device *dev, void *data)
2923 {
2924 	struct cxl_region_params *p;
2925 	struct cxl_region *cxlr;
2926 	struct range *r = data;
2927 	int rc = 0;
2928 
2929 	if (!is_cxl_region(dev))
2930 		return 0;
2931 
2932 	cxlr = to_cxl_region(dev);
2933 	p = &cxlr->params;
2934 
2935 	down_read(&cxl_region_rwsem);
2936 	if (p->res && p->res->start == r->start && p->res->end == r->end)
2937 		rc = 1;
2938 	up_read(&cxl_region_rwsem);
2939 
2940 	return rc;
2941 }
2942 
2943 /* Establish an empty region covering the given HPA range */
2944 static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
2945 					   struct cxl_endpoint_decoder *cxled)
2946 {
2947 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
2948 	struct cxl_port *port = cxlrd_to_port(cxlrd);
2949 	struct range *hpa = &cxled->cxld.hpa_range;
2950 	struct cxl_region_params *p;
2951 	struct cxl_region *cxlr;
2952 	struct resource *res;
2953 	int rc;
2954 
2955 	do {
2956 		cxlr = __create_region(cxlrd, cxled->mode,
2957 				       atomic_read(&cxlrd->region_id));
2958 	} while (IS_ERR(cxlr) && PTR_ERR(cxlr) == -EBUSY);
2959 
2960 	if (IS_ERR(cxlr)) {
2961 		dev_err(cxlmd->dev.parent,
2962 			"%s:%s: %s failed assign region: %ld\n",
2963 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2964 			__func__, PTR_ERR(cxlr));
2965 		return cxlr;
2966 	}
2967 
2968 	down_write(&cxl_region_rwsem);
2969 	p = &cxlr->params;
2970 	if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
2971 		dev_err(cxlmd->dev.parent,
2972 			"%s:%s: %s autodiscovery interrupted\n",
2973 			dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2974 			__func__);
2975 		rc = -EBUSY;
2976 		goto err;
2977 	}
2978 
2979 	set_bit(CXL_REGION_F_AUTO, &cxlr->flags);
2980 
2981 	res = kmalloc(sizeof(*res), GFP_KERNEL);
2982 	if (!res) {
2983 		rc = -ENOMEM;
2984 		goto err;
2985 	}
2986 
2987 	*res = DEFINE_RES_MEM_NAMED(hpa->start, range_len(hpa),
2988 				    dev_name(&cxlr->dev));
2989 	rc = insert_resource(cxlrd->res, res);
2990 	if (rc) {
2991 		/*
2992 		 * Platform-firmware may not have split resources like "System
2993 		 * RAM" on CXL window boundaries see cxl_region_iomem_release()
2994 		 */
2995 		dev_warn(cxlmd->dev.parent,
2996 			 "%s:%s: %s %s cannot insert resource\n",
2997 			 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2998 			 __func__, dev_name(&cxlr->dev));
2999 	}
3000 
3001 	p->res = res;
3002 	p->interleave_ways = cxled->cxld.interleave_ways;
3003 	p->interleave_granularity = cxled->cxld.interleave_granularity;
3004 	p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
3005 
3006 	rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
3007 	if (rc)
3008 		goto err;
3009 
3010 	dev_dbg(cxlmd->dev.parent, "%s:%s: %s %s res: %pr iw: %d ig: %d\n",
3011 		dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), __func__,
3012 		dev_name(&cxlr->dev), p->res, p->interleave_ways,
3013 		p->interleave_granularity);
3014 
3015 	/* ...to match put_device() in cxl_add_to_region() */
3016 	get_device(&cxlr->dev);
3017 	up_write(&cxl_region_rwsem);
3018 
3019 	return cxlr;
3020 
3021 err:
3022 	up_write(&cxl_region_rwsem);
3023 	devm_release_action(port->uport_dev, unregister_region, cxlr);
3024 	return ERR_PTR(rc);
3025 }
3026 
3027 int cxl_add_to_region(struct cxl_port *root, struct cxl_endpoint_decoder *cxled)
3028 {
3029 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
3030 	struct range *hpa = &cxled->cxld.hpa_range;
3031 	struct cxl_decoder *cxld = &cxled->cxld;
3032 	struct device *cxlrd_dev, *region_dev;
3033 	struct cxl_root_decoder *cxlrd;
3034 	struct cxl_region_params *p;
3035 	struct cxl_region *cxlr;
3036 	bool attach = false;
3037 	int rc;
3038 
3039 	cxlrd_dev = device_find_child(&root->dev, &cxld->hpa_range,
3040 				      match_root_decoder_by_range);
3041 	if (!cxlrd_dev) {
3042 		dev_err(cxlmd->dev.parent,
3043 			"%s:%s no CXL window for range %#llx:%#llx\n",
3044 			dev_name(&cxlmd->dev), dev_name(&cxld->dev),
3045 			cxld->hpa_range.start, cxld->hpa_range.end);
3046 		return -ENXIO;
3047 	}
3048 
3049 	cxlrd = to_cxl_root_decoder(cxlrd_dev);
3050 
3051 	/*
3052 	 * Ensure that if multiple threads race to construct_region() for @hpa
3053 	 * one does the construction and the others add to that.
3054 	 */
3055 	mutex_lock(&cxlrd->range_lock);
3056 	region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa,
3057 				       match_region_by_range);
3058 	if (!region_dev) {
3059 		cxlr = construct_region(cxlrd, cxled);
3060 		region_dev = &cxlr->dev;
3061 	} else
3062 		cxlr = to_cxl_region(region_dev);
3063 	mutex_unlock(&cxlrd->range_lock);
3064 
3065 	rc = PTR_ERR_OR_ZERO(cxlr);
3066 	if (rc)
3067 		goto out;
3068 
3069 	attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE);
3070 
3071 	down_read(&cxl_region_rwsem);
3072 	p = &cxlr->params;
3073 	attach = p->state == CXL_CONFIG_COMMIT;
3074 	up_read(&cxl_region_rwsem);
3075 
3076 	if (attach) {
3077 		/*
3078 		 * If device_attach() fails the range may still be active via
3079 		 * the platform-firmware memory map, otherwise the driver for
3080 		 * regions is local to this file, so driver matching can't fail.
3081 		 */
3082 		if (device_attach(&cxlr->dev) < 0)
3083 			dev_err(&cxlr->dev, "failed to enable, range: %pr\n",
3084 				p->res);
3085 	}
3086 
3087 	put_device(region_dev);
3088 out:
3089 	put_device(cxlrd_dev);
3090 	return rc;
3091 }
3092 EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, CXL);
3093 
3094 static int is_system_ram(struct resource *res, void *arg)
3095 {
3096 	struct cxl_region *cxlr = arg;
3097 	struct cxl_region_params *p = &cxlr->params;
3098 
3099 	dev_dbg(&cxlr->dev, "%pr has System RAM: %pr\n", p->res, res);
3100 	return 1;
3101 }
3102 
3103 static int cxl_region_probe(struct device *dev)
3104 {
3105 	struct cxl_region *cxlr = to_cxl_region(dev);
3106 	struct cxl_region_params *p = &cxlr->params;
3107 	int rc;
3108 
3109 	rc = down_read_interruptible(&cxl_region_rwsem);
3110 	if (rc) {
3111 		dev_dbg(&cxlr->dev, "probe interrupted\n");
3112 		return rc;
3113 	}
3114 
3115 	if (p->state < CXL_CONFIG_COMMIT) {
3116 		dev_dbg(&cxlr->dev, "config state: %d\n", p->state);
3117 		rc = -ENXIO;
3118 		goto out;
3119 	}
3120 
3121 	if (test_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags)) {
3122 		dev_err(&cxlr->dev,
3123 			"failed to activate, re-commit region and retry\n");
3124 		rc = -ENXIO;
3125 		goto out;
3126 	}
3127 
3128 	/*
3129 	 * From this point on any path that changes the region's state away from
3130 	 * CXL_CONFIG_COMMIT is also responsible for releasing the driver.
3131 	 */
3132 out:
3133 	up_read(&cxl_region_rwsem);
3134 
3135 	if (rc)
3136 		return rc;
3137 
3138 	switch (cxlr->mode) {
3139 	case CXL_DECODER_PMEM:
3140 		return devm_cxl_add_pmem_region(cxlr);
3141 	case CXL_DECODER_RAM:
3142 		/*
3143 		 * The region can not be manged by CXL if any portion of
3144 		 * it is already online as 'System RAM'
3145 		 */
3146 		if (walk_iomem_res_desc(IORES_DESC_NONE,
3147 					IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
3148 					p->res->start, p->res->end, cxlr,
3149 					is_system_ram) > 0)
3150 			return 0;
3151 		return devm_cxl_add_dax_region(cxlr);
3152 	default:
3153 		dev_dbg(&cxlr->dev, "unsupported region mode: %d\n",
3154 			cxlr->mode);
3155 		return -ENXIO;
3156 	}
3157 }
3158 
3159 static struct cxl_driver cxl_region_driver = {
3160 	.name = "cxl_region",
3161 	.probe = cxl_region_probe,
3162 	.id = CXL_DEVICE_REGION,
3163 };
3164 
3165 int cxl_region_init(void)
3166 {
3167 	return cxl_driver_register(&cxl_region_driver);
3168 }
3169 
3170 void cxl_region_exit(void)
3171 {
3172 	cxl_driver_unregister(&cxl_region_driver);
3173 }
3174 
3175 MODULE_IMPORT_NS(CXL);
3176 MODULE_IMPORT_NS(DEVMEM);
3177 MODULE_ALIAS_CXL(CXL_DEVICE_REGION);
3178