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