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