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/debugfs.h>
6 #include <linux/device.h>
7 #include <linux/module.h>
8 #include <linux/memory.h>
9 #include <linux/slab.h>
10 #include <linux/uuid.h>
11 #include <linux/sort.h>
12 #include <linux/idr.h>
13 #include <linux/memory-tiers.h>
14 #include <linux/string_choices.h>
15 #include <cxlmem.h>
16 #include <cxl.h>
17 #include "core.h"
18 #include "mce.h"
19
20 /**
21 * DOC: cxl core region
22 *
23 * CXL Regions represent mapped memory capacity in system physical address
24 * space. Whereas the CXL Root Decoders identify the bounds of potential CXL
25 * Memory ranges, Regions represent the active mapped capacity by the HDM
26 * Decoder Capability structures throughout the Host Bridges, Switches, and
27 * Endpoints in the topology.
28 *
29 * Region configuration has ordering constraints. UUID may be set at any time
30 * but is only visible for persistent regions.
31 * 1. Interleave granularity
32 * 2. Interleave size
33 * 3. Decoder targets
34 */
35
36 /*
37 * nodemask that sets per node when the access_coordinates for the node has
38 * been updated by the CXL memory hotplug notifier.
39 */
40 static nodemask_t nodemask_region_seen = NODE_MASK_NONE;
41
42 static struct cxl_region *to_cxl_region(struct device *dev);
43
44 #define __ACCESS_ATTR_RO(_level, _name) { \
45 .attr = { .name = __stringify(_name), .mode = 0444 }, \
46 .show = _name##_access##_level##_show, \
47 }
48
49 #define ACCESS_DEVICE_ATTR_RO(level, name) \
50 struct device_attribute dev_attr_access##level##_##name = __ACCESS_ATTR_RO(level, name)
51
52 #define ACCESS_ATTR_RO(level, attrib) \
53 static ssize_t attrib##_access##level##_show(struct device *dev, \
54 struct device_attribute *attr, \
55 char *buf) \
56 { \
57 struct cxl_region *cxlr = to_cxl_region(dev); \
58 \
59 if (cxlr->coord[level].attrib == 0) \
60 return -ENOENT; \
61 \
62 return sysfs_emit(buf, "%u\n", cxlr->coord[level].attrib); \
63 } \
64 static ACCESS_DEVICE_ATTR_RO(level, attrib)
65
66 ACCESS_ATTR_RO(0, read_bandwidth);
67 ACCESS_ATTR_RO(0, read_latency);
68 ACCESS_ATTR_RO(0, write_bandwidth);
69 ACCESS_ATTR_RO(0, write_latency);
70
71 #define ACCESS_ATTR_DECLARE(level, attrib) \
72 (&dev_attr_access##level##_##attrib.attr)
73
74 static struct attribute *access0_coordinate_attrs[] = {
75 ACCESS_ATTR_DECLARE(0, read_bandwidth),
76 ACCESS_ATTR_DECLARE(0, write_bandwidth),
77 ACCESS_ATTR_DECLARE(0, read_latency),
78 ACCESS_ATTR_DECLARE(0, write_latency),
79 NULL
80 };
81
82 ACCESS_ATTR_RO(1, read_bandwidth);
83 ACCESS_ATTR_RO(1, read_latency);
84 ACCESS_ATTR_RO(1, write_bandwidth);
85 ACCESS_ATTR_RO(1, write_latency);
86
87 static struct attribute *access1_coordinate_attrs[] = {
88 ACCESS_ATTR_DECLARE(1, read_bandwidth),
89 ACCESS_ATTR_DECLARE(1, write_bandwidth),
90 ACCESS_ATTR_DECLARE(1, read_latency),
91 ACCESS_ATTR_DECLARE(1, write_latency),
92 NULL
93 };
94
95 #define ACCESS_VISIBLE(level) \
96 static umode_t cxl_region_access##level##_coordinate_visible( \
97 struct kobject *kobj, struct attribute *a, int n) \
98 { \
99 struct device *dev = kobj_to_dev(kobj); \
100 struct cxl_region *cxlr = to_cxl_region(dev); \
101 \
102 if (a == &dev_attr_access##level##_read_latency.attr && \
103 cxlr->coord[level].read_latency == 0) \
104 return 0; \
105 \
106 if (a == &dev_attr_access##level##_write_latency.attr && \
107 cxlr->coord[level].write_latency == 0) \
108 return 0; \
109 \
110 if (a == &dev_attr_access##level##_read_bandwidth.attr && \
111 cxlr->coord[level].read_bandwidth == 0) \
112 return 0; \
113 \
114 if (a == &dev_attr_access##level##_write_bandwidth.attr && \
115 cxlr->coord[level].write_bandwidth == 0) \
116 return 0; \
117 \
118 return a->mode; \
119 }
120
121 ACCESS_VISIBLE(0);
122 ACCESS_VISIBLE(1);
123
124 static const struct attribute_group cxl_region_access0_coordinate_group = {
125 .name = "access0",
126 .attrs = access0_coordinate_attrs,
127 .is_visible = cxl_region_access0_coordinate_visible,
128 };
129
get_cxl_region_access0_group(void)130 static const struct attribute_group *get_cxl_region_access0_group(void)
131 {
132 return &cxl_region_access0_coordinate_group;
133 }
134
135 static const struct attribute_group cxl_region_access1_coordinate_group = {
136 .name = "access1",
137 .attrs = access1_coordinate_attrs,
138 .is_visible = cxl_region_access1_coordinate_visible,
139 };
140
get_cxl_region_access1_group(void)141 static const struct attribute_group *get_cxl_region_access1_group(void)
142 {
143 return &cxl_region_access1_coordinate_group;
144 }
145
uuid_show(struct device * dev,struct device_attribute * attr,char * buf)146 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
147 char *buf)
148 {
149 struct cxl_region *cxlr = to_cxl_region(dev);
150 struct cxl_region_params *p = &cxlr->params;
151 ssize_t rc;
152
153 ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region);
154 if ((rc = ACQUIRE_ERR(rwsem_read_intr, ®ion_rwsem)))
155 return rc;
156 if (cxlr->mode != CXL_PARTMODE_PMEM)
157 return sysfs_emit(buf, "\n");
158 return sysfs_emit(buf, "%pUb\n", &p->uuid);
159 }
160
is_dup(struct device * match,void * data)161 static int is_dup(struct device *match, void *data)
162 {
163 struct cxl_region_params *p;
164 struct cxl_region *cxlr;
165 uuid_t *uuid = data;
166
167 if (!is_cxl_region(match))
168 return 0;
169
170 lockdep_assert_held(&cxl_rwsem.region);
171 cxlr = to_cxl_region(match);
172 p = &cxlr->params;
173
174 if (uuid_equal(&p->uuid, uuid)) {
175 dev_dbg(match, "already has uuid: %pUb\n", uuid);
176 return -EBUSY;
177 }
178
179 return 0;
180 }
181
uuid_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)182 static ssize_t uuid_store(struct device *dev, struct device_attribute *attr,
183 const char *buf, size_t len)
184 {
185 struct cxl_region *cxlr = to_cxl_region(dev);
186 struct cxl_region_params *p = &cxlr->params;
187 uuid_t temp;
188 ssize_t rc;
189
190 if (len != UUID_STRING_LEN + 1)
191 return -EINVAL;
192
193 rc = uuid_parse(buf, &temp);
194 if (rc)
195 return rc;
196
197 if (uuid_is_null(&temp))
198 return -EINVAL;
199
200 ACQUIRE(rwsem_write_kill, region_rwsem)(&cxl_rwsem.region);
201 if ((rc = ACQUIRE_ERR(rwsem_write_kill, ®ion_rwsem)))
202 return rc;
203
204 if (uuid_equal(&p->uuid, &temp))
205 return len;
206
207 if (p->state >= CXL_CONFIG_ACTIVE)
208 return -EBUSY;
209
210 rc = bus_for_each_dev(&cxl_bus_type, NULL, &temp, is_dup);
211 if (rc < 0)
212 return rc;
213
214 uuid_copy(&p->uuid, &temp);
215
216 return len;
217 }
218 static DEVICE_ATTR_RW(uuid);
219
cxl_rr_load(struct cxl_port * port,struct cxl_region * cxlr)220 static struct cxl_region_ref *cxl_rr_load(struct cxl_port *port,
221 struct cxl_region *cxlr)
222 {
223 return xa_load(&port->regions, (unsigned long)cxlr);
224 }
225
cxl_region_invalidate_memregion(struct cxl_region * cxlr)226 static int cxl_region_invalidate_memregion(struct cxl_region *cxlr)
227 {
228 if (!cpu_cache_has_invalidate_memregion()) {
229 if (IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST)) {
230 dev_info_once(
231 &cxlr->dev,
232 "Bypassing cpu_cache_invalidate_memregion() for testing!\n");
233 return 0;
234 }
235 dev_WARN(&cxlr->dev,
236 "Failed to synchronize CPU cache state\n");
237 return -ENXIO;
238 }
239
240 if (!cxlr->params.res)
241 return -ENXIO;
242 cpu_cache_invalidate_memregion(cxlr->params.res->start,
243 resource_size(cxlr->params.res));
244 return 0;
245 }
246
cxl_region_decode_reset(struct cxl_region * cxlr,int count)247 static void cxl_region_decode_reset(struct cxl_region *cxlr, int count)
248 {
249 struct cxl_region_params *p = &cxlr->params;
250 int i;
251
252 if (test_bit(CXL_REGION_F_LOCK, &cxlr->flags))
253 return;
254
255 /*
256 * Before region teardown attempt to flush, evict any data cached for
257 * this region, or scream loudly about missing arch / platform support
258 * for CXL teardown.
259 */
260 cxl_region_invalidate_memregion(cxlr);
261
262 for (i = count - 1; i >= 0; i--) {
263 struct cxl_endpoint_decoder *cxled = p->targets[i];
264 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
265 struct cxl_port *iter = cxled_to_port(cxled);
266 struct cxl_dev_state *cxlds = cxlmd->cxlds;
267 struct cxl_ep *ep;
268
269 if (cxlds->rcd)
270 goto endpoint_reset;
271
272 while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
273 iter = to_cxl_port(iter->dev.parent);
274
275 for (ep = cxl_ep_load(iter, cxlmd); iter;
276 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
277 struct cxl_region_ref *cxl_rr;
278 struct cxl_decoder *cxld;
279
280 cxl_rr = cxl_rr_load(iter, cxlr);
281 cxld = cxl_rr->decoder;
282 if (cxld->reset)
283 cxld->reset(cxld);
284 set_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
285 }
286
287 endpoint_reset:
288 cxled->cxld.reset(&cxled->cxld);
289 set_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
290 }
291
292 /* all decoders associated with this region have been torn down */
293 clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
294 }
295
commit_decoder(struct cxl_decoder * cxld)296 static int commit_decoder(struct cxl_decoder *cxld)
297 {
298 struct cxl_switch_decoder *cxlsd = NULL;
299
300 if (cxld->commit)
301 return cxld->commit(cxld);
302
303 if (is_switch_decoder(&cxld->dev))
304 cxlsd = to_cxl_switch_decoder(&cxld->dev);
305
306 if (dev_WARN_ONCE(&cxld->dev, !cxlsd || cxlsd->nr_targets > 1,
307 "->commit() is required\n"))
308 return -ENXIO;
309 return 0;
310 }
311
cxl_region_decode_commit(struct cxl_region * cxlr)312 static int cxl_region_decode_commit(struct cxl_region *cxlr)
313 {
314 struct cxl_region_params *p = &cxlr->params;
315 int i, rc = 0;
316
317 for (i = 0; i < p->nr_targets; i++) {
318 struct cxl_endpoint_decoder *cxled = p->targets[i];
319 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
320 struct cxl_region_ref *cxl_rr;
321 struct cxl_decoder *cxld;
322 struct cxl_port *iter;
323 struct cxl_ep *ep;
324
325 /* commit bottom up */
326 for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
327 iter = to_cxl_port(iter->dev.parent)) {
328 cxl_rr = cxl_rr_load(iter, cxlr);
329 cxld = cxl_rr->decoder;
330 rc = commit_decoder(cxld);
331 if (rc)
332 break;
333 }
334
335 if (rc) {
336 /* programming @iter failed, teardown */
337 for (ep = cxl_ep_load(iter, cxlmd); ep && iter;
338 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
339 cxl_rr = cxl_rr_load(iter, cxlr);
340 cxld = cxl_rr->decoder;
341 if (cxld->reset)
342 cxld->reset(cxld);
343 }
344
345 cxled->cxld.reset(&cxled->cxld);
346 goto err;
347 }
348 }
349
350 return 0;
351
352 err:
353 /* undo the targets that were successfully committed */
354 cxl_region_decode_reset(cxlr, i);
355 return rc;
356 }
357
queue_reset(struct cxl_region * cxlr)358 static int queue_reset(struct cxl_region *cxlr)
359 {
360 struct cxl_region_params *p = &cxlr->params;
361 int rc;
362
363 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
364 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
365 return rc;
366
367 /* Already in the requested state? */
368 if (p->state < CXL_CONFIG_COMMIT)
369 return 0;
370
371 p->state = CXL_CONFIG_RESET_PENDING;
372
373 return 0;
374 }
375
__commit(struct cxl_region * cxlr)376 static int __commit(struct cxl_region *cxlr)
377 {
378 struct cxl_region_params *p = &cxlr->params;
379 int rc;
380
381 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
382 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
383 return rc;
384
385 /* Already in the requested state? */
386 if (p->state >= CXL_CONFIG_COMMIT)
387 return 0;
388
389 /* Not ready to commit? */
390 if (p->state < CXL_CONFIG_ACTIVE)
391 return -ENXIO;
392
393 /*
394 * Invalidate caches before region setup to drop any speculative
395 * consumption of this address space
396 */
397 rc = cxl_region_invalidate_memregion(cxlr);
398 if (rc)
399 return rc;
400
401 rc = cxl_region_decode_commit(cxlr);
402 if (rc)
403 return rc;
404
405 p->state = CXL_CONFIG_COMMIT;
406
407 return 0;
408 }
409
commit_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)410 static ssize_t commit_store(struct device *dev, struct device_attribute *attr,
411 const char *buf, size_t len)
412 {
413 struct cxl_region *cxlr = to_cxl_region(dev);
414 struct cxl_region_params *p = &cxlr->params;
415 bool commit;
416 ssize_t rc;
417
418 rc = kstrtobool(buf, &commit);
419 if (rc)
420 return rc;
421
422 if (commit) {
423 rc = __commit(cxlr);
424 if (rc)
425 return rc;
426 return len;
427 }
428
429 if (test_bit(CXL_REGION_F_LOCK, &cxlr->flags))
430 return -EPERM;
431
432 rc = queue_reset(cxlr);
433 if (rc)
434 return rc;
435
436 /*
437 * Unmap the region and depend the reset-pending state to ensure
438 * it does not go active again until post reset
439 */
440 device_release_driver(&cxlr->dev);
441
442 /*
443 * With the reset pending take cxl_rwsem.region unconditionally
444 * to ensure the reset gets handled before returning.
445 */
446 guard(rwsem_write)(&cxl_rwsem.region);
447
448 /*
449 * Revalidate that the reset is still pending in case another
450 * thread already handled this reset.
451 */
452 if (p->state == CXL_CONFIG_RESET_PENDING) {
453 cxl_region_decode_reset(cxlr, p->interleave_ways);
454 p->state = CXL_CONFIG_ACTIVE;
455 }
456
457 return len;
458 }
459
commit_show(struct device * dev,struct device_attribute * attr,char * buf)460 static ssize_t commit_show(struct device *dev, struct device_attribute *attr,
461 char *buf)
462 {
463 struct cxl_region *cxlr = to_cxl_region(dev);
464 struct cxl_region_params *p = &cxlr->params;
465 ssize_t rc;
466
467 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
468 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
469 return rc;
470 return sysfs_emit(buf, "%d\n", p->state >= CXL_CONFIG_COMMIT);
471 }
472 static DEVICE_ATTR_RW(commit);
473
interleave_ways_show(struct device * dev,struct device_attribute * attr,char * buf)474 static ssize_t interleave_ways_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
476 {
477 struct cxl_region *cxlr = to_cxl_region(dev);
478 struct cxl_region_params *p = &cxlr->params;
479 int rc;
480
481 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
482 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
483 return rc;
484 return sysfs_emit(buf, "%d\n", p->interleave_ways);
485 }
486
487 static const struct attribute_group *get_cxl_region_target_group(void);
488
set_interleave_ways(struct cxl_region * cxlr,int val)489 static int set_interleave_ways(struct cxl_region *cxlr, int val)
490 {
491 struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
492 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
493 struct cxl_region_params *p = &cxlr->params;
494 int save, rc;
495 u8 iw;
496
497 rc = ways_to_eiw(val, &iw);
498 if (rc)
499 return rc;
500
501 /*
502 * Even for x3, x6, and x12 interleaves the region interleave must be a
503 * power of 2 multiple of the host bridge interleave.
504 */
505 if (!is_power_of_2(val / cxld->interleave_ways) ||
506 (val % cxld->interleave_ways)) {
507 dev_dbg(&cxlr->dev, "invalid interleave: %d\n", val);
508 return -EINVAL;
509 }
510
511 lockdep_assert_held_write(&cxl_rwsem.region);
512
513 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE)
514 return -EBUSY;
515
516 save = p->interleave_ways;
517 p->interleave_ways = val;
518 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
519 if (rc)
520 p->interleave_ways = save;
521
522 return rc;
523 }
524
interleave_ways_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)525 static ssize_t interleave_ways_store(struct device *dev,
526 struct device_attribute *attr,
527 const char *buf, size_t len)
528 {
529 struct cxl_region *cxlr = to_cxl_region(dev);
530 int val;
531 int rc;
532
533 rc = kstrtoint(buf, 0, &val);
534 if (rc)
535 return rc;
536
537 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
538 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
539 return rc;
540
541 rc = set_interleave_ways(cxlr, val);
542 if (rc)
543 return rc;
544
545 return len;
546 }
547 static DEVICE_ATTR_RW(interleave_ways);
548
interleave_granularity_show(struct device * dev,struct device_attribute * attr,char * buf)549 static ssize_t interleave_granularity_show(struct device *dev,
550 struct device_attribute *attr,
551 char *buf)
552 {
553 struct cxl_region *cxlr = to_cxl_region(dev);
554 struct cxl_region_params *p = &cxlr->params;
555 int rc;
556
557 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
558 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
559 return rc;
560 return sysfs_emit(buf, "%d\n", p->interleave_granularity);
561 }
562
set_interleave_granularity(struct cxl_region * cxlr,int val)563 static int set_interleave_granularity(struct cxl_region *cxlr, int val)
564 {
565 struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
566 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
567 struct cxl_region_params *p = &cxlr->params;
568 int rc;
569 u16 ig;
570
571 rc = granularity_to_eig(val, &ig);
572 if (rc)
573 return rc;
574
575 /*
576 * When the host-bridge is interleaved, disallow region granularity !=
577 * root granularity. Regions with a granularity less than the root
578 * interleave result in needing multiple endpoints to support a single
579 * slot in the interleave (possible to support in the future). Regions
580 * with a granularity greater than the root interleave result in invalid
581 * DPA translations (invalid to support).
582 */
583 if (cxld->interleave_ways > 1 && val != cxld->interleave_granularity)
584 return -EINVAL;
585
586 lockdep_assert_held_write(&cxl_rwsem.region);
587
588 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE)
589 return -EBUSY;
590
591 p->interleave_granularity = val;
592 return 0;
593 }
594
interleave_granularity_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)595 static ssize_t interleave_granularity_store(struct device *dev,
596 struct device_attribute *attr,
597 const char *buf, size_t len)
598 {
599 struct cxl_region *cxlr = to_cxl_region(dev);
600 int rc, val;
601
602 rc = kstrtoint(buf, 0, &val);
603 if (rc)
604 return rc;
605
606 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
607 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
608 return rc;
609
610 rc = set_interleave_granularity(cxlr, val);
611 if (rc)
612 return rc;
613
614 return len;
615 }
616 static DEVICE_ATTR_RW(interleave_granularity);
617
resource_show(struct device * dev,struct device_attribute * attr,char * buf)618 static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
619 char *buf)
620 {
621 struct cxl_region *cxlr = to_cxl_region(dev);
622 struct cxl_region_params *p = &cxlr->params;
623 u64 resource = -1ULL;
624 int rc;
625
626 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
627 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
628 return rc;
629
630 if (p->res)
631 resource = p->res->start;
632 return sysfs_emit(buf, "%#llx\n", resource);
633 }
634 static DEVICE_ATTR_RO(resource);
635
mode_show(struct device * dev,struct device_attribute * attr,char * buf)636 static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
637 char *buf)
638 {
639 struct cxl_region *cxlr = to_cxl_region(dev);
640 const char *desc;
641
642 if (cxlr->mode == CXL_PARTMODE_RAM)
643 desc = "ram";
644 else if (cxlr->mode == CXL_PARTMODE_PMEM)
645 desc = "pmem";
646 else
647 desc = "";
648
649 return sysfs_emit(buf, "%s\n", desc);
650 }
651 static DEVICE_ATTR_RO(mode);
652
alloc_hpa(struct cxl_region * cxlr,resource_size_t size)653 static int alloc_hpa(struct cxl_region *cxlr, resource_size_t size)
654 {
655 struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
656 struct cxl_region_params *p = &cxlr->params;
657 struct resource *res;
658 u64 remainder = 0;
659
660 lockdep_assert_held_write(&cxl_rwsem.region);
661
662 /* Nothing to do... */
663 if (p->res && resource_size(p->res) == size)
664 return 0;
665
666 /* To change size the old size must be freed first */
667 if (p->res)
668 return -EBUSY;
669
670 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE)
671 return -EBUSY;
672
673 /* ways, granularity and uuid (if PMEM) need to be set before HPA */
674 if (!p->interleave_ways || !p->interleave_granularity ||
675 (cxlr->mode == CXL_PARTMODE_PMEM && uuid_is_null(&p->uuid)))
676 return -ENXIO;
677
678 div64_u64_rem(size, (u64)SZ_256M * p->interleave_ways, &remainder);
679 if (remainder)
680 return -EINVAL;
681
682 res = alloc_free_mem_region(cxlrd->res, size, SZ_256M,
683 dev_name(&cxlr->dev));
684 if (IS_ERR(res)) {
685 dev_dbg(&cxlr->dev,
686 "HPA allocation error (%pe) for size:%pap in %s %pr\n",
687 res, &size, cxlrd->res->name, cxlrd->res);
688 return PTR_ERR(res);
689 }
690
691 cxlr->hpa_range = DEFINE_RANGE(res->start, res->end);
692
693 p->res = res;
694 p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
695
696 return 0;
697 }
698
cxl_region_iomem_release(struct cxl_region * cxlr)699 static void cxl_region_iomem_release(struct cxl_region *cxlr)
700 {
701 struct cxl_region_params *p = &cxlr->params;
702
703 if (device_is_registered(&cxlr->dev))
704 lockdep_assert_held_write(&cxl_rwsem.region);
705 if (p->res) {
706 /*
707 * Autodiscovered regions may not have been able to insert their
708 * resource.
709 */
710 if (p->res->parent)
711 remove_resource(p->res);
712 kfree(p->res);
713 p->res = NULL;
714 }
715 }
716
free_hpa(struct cxl_region * cxlr)717 static int free_hpa(struct cxl_region *cxlr)
718 {
719 struct cxl_region_params *p = &cxlr->params;
720
721 lockdep_assert_held_write(&cxl_rwsem.region);
722
723 if (!p->res)
724 return 0;
725
726 if (p->state >= CXL_CONFIG_ACTIVE)
727 return -EBUSY;
728
729 cxlr->hpa_range = DEFINE_RANGE(0, -1);
730
731 cxl_region_iomem_release(cxlr);
732 p->state = CXL_CONFIG_IDLE;
733 return 0;
734 }
735
size_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)736 static ssize_t size_store(struct device *dev, struct device_attribute *attr,
737 const char *buf, size_t len)
738 {
739 struct cxl_region *cxlr = to_cxl_region(dev);
740 u64 val;
741 int rc;
742
743 rc = kstrtou64(buf, 0, &val);
744 if (rc)
745 return rc;
746
747 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
748 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
749 return rc;
750
751 if (val)
752 rc = alloc_hpa(cxlr, val);
753 else
754 rc = free_hpa(cxlr);
755
756 if (rc)
757 return rc;
758
759 return len;
760 }
761
size_show(struct device * dev,struct device_attribute * attr,char * buf)762 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
763 char *buf)
764 {
765 struct cxl_region *cxlr = to_cxl_region(dev);
766 struct cxl_region_params *p = &cxlr->params;
767 u64 size = 0;
768 ssize_t rc;
769
770 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
771 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
772 return rc;
773 if (p->res)
774 size = resource_size(p->res);
775 return sysfs_emit(buf, "%#llx\n", size);
776 }
777 static DEVICE_ATTR_RW(size);
778
extended_linear_cache_size_show(struct device * dev,struct device_attribute * attr,char * buf)779 static ssize_t extended_linear_cache_size_show(struct device *dev,
780 struct device_attribute *attr,
781 char *buf)
782 {
783 struct cxl_region *cxlr = to_cxl_region(dev);
784 struct cxl_region_params *p = &cxlr->params;
785 ssize_t rc;
786
787 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
788 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
789 return rc;
790 return sysfs_emit(buf, "%pap\n", &p->cache_size);
791 }
792 static DEVICE_ATTR_RO(extended_linear_cache_size);
793
locked_show(struct device * dev,struct device_attribute * attr,char * buf)794 static ssize_t locked_show(struct device *dev,
795 struct device_attribute *attr,
796 char *buf)
797 {
798 struct cxl_region *cxlr = to_cxl_region(dev);
799 int rc;
800
801 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
802 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
803 return rc;
804
805 rc = test_bit(CXL_REGION_F_LOCK, &cxlr->flags);
806 return sysfs_emit(buf, "%d\n", rc);
807 }
808 static DEVICE_ATTR_RO(locked);
809
810 static struct attribute *cxl_region_attrs[] = {
811 &dev_attr_uuid.attr,
812 &dev_attr_commit.attr,
813 &dev_attr_interleave_ways.attr,
814 &dev_attr_interleave_granularity.attr,
815 &dev_attr_resource.attr,
816 &dev_attr_size.attr,
817 &dev_attr_mode.attr,
818 &dev_attr_extended_linear_cache_size.attr,
819 &dev_attr_locked.attr,
820 NULL,
821 };
822
cxl_region_visible(struct kobject * kobj,struct attribute * a,int n)823 static umode_t cxl_region_visible(struct kobject *kobj, struct attribute *a,
824 int n)
825 {
826 struct device *dev = kobj_to_dev(kobj);
827 struct cxl_region *cxlr = to_cxl_region(dev);
828
829 /*
830 * Support tooling that expects to find a 'uuid' attribute for all
831 * regions regardless of mode.
832 */
833 if (a == &dev_attr_uuid.attr && cxlr->mode != CXL_PARTMODE_PMEM)
834 return 0444;
835
836 /*
837 * Don't display extended linear cache attribute if there is no
838 * extended linear cache.
839 */
840 if (a == &dev_attr_extended_linear_cache_size.attr &&
841 cxlr->params.cache_size == 0)
842 return 0;
843
844 return a->mode;
845 }
846
847 static const struct attribute_group cxl_region_group = {
848 .attrs = cxl_region_attrs,
849 .is_visible = cxl_region_visible,
850 };
851
show_targetN(struct cxl_region * cxlr,char * buf,int pos)852 static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos)
853 {
854 struct cxl_region_params *p = &cxlr->params;
855 struct cxl_endpoint_decoder *cxled;
856 int rc;
857
858 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
859 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
860 return rc;
861
862 if (pos >= p->interleave_ways) {
863 dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
864 p->interleave_ways);
865 return -ENXIO;
866 }
867
868 cxled = p->targets[pos];
869 if (!cxled)
870 return sysfs_emit(buf, "\n");
871 return sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev));
872 }
873
check_commit_order(struct device * dev,void * data)874 static int check_commit_order(struct device *dev, void *data)
875 {
876 struct cxl_decoder *cxld = to_cxl_decoder(dev);
877
878 /*
879 * if port->commit_end is not the only free decoder, then out of
880 * order shutdown has occurred, block further allocations until
881 * that is resolved
882 */
883 if (((cxld->flags & CXL_DECODER_F_ENABLE) == 0))
884 return -EBUSY;
885 return 0;
886 }
887
match_free_decoder(struct device * dev,const void * data)888 static int match_free_decoder(struct device *dev, const void *data)
889 {
890 struct cxl_port *port = to_cxl_port(dev->parent);
891 struct cxl_decoder *cxld;
892 int rc;
893
894 if (!is_switch_decoder(dev))
895 return 0;
896
897 cxld = to_cxl_decoder(dev);
898
899 if (cxld->id != port->commit_end + 1)
900 return 0;
901
902 if (cxld->region) {
903 dev_dbg(dev->parent,
904 "next decoder to commit (%s) is already reserved (%s)\n",
905 dev_name(dev), dev_name(&cxld->region->dev));
906 return 0;
907 }
908
909 rc = device_for_each_child_reverse_from(dev->parent, dev, NULL,
910 check_commit_order);
911 if (rc) {
912 dev_dbg(dev->parent,
913 "unable to allocate %s due to out of order shutdown\n",
914 dev_name(dev));
915 return 0;
916 }
917 return 1;
918 }
919
spa_maps_hpa(const struct cxl_region_params * p,const struct range * range)920 static bool spa_maps_hpa(const struct cxl_region_params *p,
921 const struct range *range)
922 {
923 if (!p->res)
924 return false;
925
926 /*
927 * The extended linear cache region is constructed by a 1:1 ratio
928 * where the SPA maps equal amounts of DRAM and CXL HPA capacity with
929 * CXL decoders at the high end of the SPA range.
930 */
931 return p->res->start + p->cache_size == range->start &&
932 p->res->end == range->end;
933 }
934
match_auto_decoder(struct device * dev,const void * data)935 static int match_auto_decoder(struct device *dev, const void *data)
936 {
937 const struct cxl_region_params *p = data;
938 struct cxl_decoder *cxld;
939 struct range *r;
940
941 if (!is_switch_decoder(dev))
942 return 0;
943
944 cxld = to_cxl_decoder(dev);
945 r = &cxld->hpa_range;
946
947 if (spa_maps_hpa(p, r))
948 return 1;
949
950 return 0;
951 }
952
953 /**
954 * cxl_port_pick_region_decoder() - assign or lookup a decoder for a region
955 * @port: a port in the ancestry of the endpoint implied by @cxled
956 * @cxled: endpoint decoder to be, or currently, mapped by @port
957 * @cxlr: region to establish, or validate, decode @port
958 *
959 * In the region creation path cxl_port_pick_region_decoder() is an
960 * allocator to find a free port. In the region assembly path, it is
961 * recalling the decoder that platform firmware picked for validation
962 * purposes.
963 *
964 * The result is recorded in a 'struct cxl_region_ref' in @port.
965 */
966 static struct cxl_decoder *
cxl_port_pick_region_decoder(struct cxl_port * port,struct cxl_endpoint_decoder * cxled,struct cxl_region * cxlr)967 cxl_port_pick_region_decoder(struct cxl_port *port,
968 struct cxl_endpoint_decoder *cxled,
969 struct cxl_region *cxlr)
970 {
971 struct device *dev;
972
973 if (port == cxled_to_port(cxled))
974 return &cxled->cxld;
975
976 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags))
977 dev = device_find_child(&port->dev, &cxlr->params,
978 match_auto_decoder);
979 else
980 dev = device_find_child(&port->dev, NULL, match_free_decoder);
981 if (!dev)
982 return NULL;
983 /*
984 * This decoder is pinned registered as long as the endpoint decoder is
985 * registered, and endpoint decoder unregistration holds the
986 * cxl_rwsem.region over unregister events, so no need to hold on to
987 * this extra reference.
988 */
989 put_device(dev);
990 return to_cxl_decoder(dev);
991 }
992
auto_order_ok(struct cxl_port * port,struct cxl_region * cxlr_iter,struct cxl_decoder * cxld)993 static bool auto_order_ok(struct cxl_port *port, struct cxl_region *cxlr_iter,
994 struct cxl_decoder *cxld)
995 {
996 struct cxl_region_ref *rr = cxl_rr_load(port, cxlr_iter);
997 struct cxl_decoder *cxld_iter = rr->decoder;
998
999 /*
1000 * Allow the out of order assembly of auto-discovered regions.
1001 * Per CXL Spec 3.1 8.2.4.20.12 software must commit decoders
1002 * in HPA order. Confirm that the decoder with the lesser HPA
1003 * starting address has the lesser id.
1004 */
1005 dev_dbg(&cxld->dev, "check for HPA violation %s:%d < %s:%d\n",
1006 dev_name(&cxld->dev), cxld->id,
1007 dev_name(&cxld_iter->dev), cxld_iter->id);
1008
1009 if (cxld_iter->id > cxld->id)
1010 return true;
1011
1012 return false;
1013 }
1014
1015 static struct cxl_region_ref *
alloc_region_ref(struct cxl_port * port,struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled,struct cxl_decoder * cxld)1016 alloc_region_ref(struct cxl_port *port, struct cxl_region *cxlr,
1017 struct cxl_endpoint_decoder *cxled,
1018 struct cxl_decoder *cxld)
1019 {
1020 struct cxl_region_params *p = &cxlr->params;
1021 struct cxl_region_ref *cxl_rr, *iter;
1022 unsigned long index;
1023 int rc;
1024
1025 xa_for_each(&port->regions, index, iter) {
1026 struct cxl_region_params *ip = &iter->region->params;
1027
1028 if (!ip->res || ip->res->start < p->res->start)
1029 continue;
1030
1031 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1032 if (auto_order_ok(port, iter->region, cxld))
1033 continue;
1034 }
1035 dev_dbg(&cxlr->dev, "%s: HPA order violation %s:%pr vs %pr\n",
1036 dev_name(&port->dev),
1037 dev_name(&iter->region->dev), ip->res, p->res);
1038
1039 return ERR_PTR(-EBUSY);
1040 }
1041
1042 cxl_rr = kzalloc_obj(*cxl_rr);
1043 if (!cxl_rr)
1044 return ERR_PTR(-ENOMEM);
1045 cxl_rr->port = port;
1046 cxl_rr->region = cxlr;
1047 cxl_rr->nr_targets = 1;
1048 xa_init(&cxl_rr->endpoints);
1049
1050 rc = xa_insert(&port->regions, (unsigned long)cxlr, cxl_rr, GFP_KERNEL);
1051 if (rc) {
1052 dev_dbg(&cxlr->dev,
1053 "%s: failed to track region reference: %d\n",
1054 dev_name(&port->dev), rc);
1055 kfree(cxl_rr);
1056 return ERR_PTR(rc);
1057 }
1058
1059 return cxl_rr;
1060 }
1061
cxl_rr_free_decoder(struct cxl_region_ref * cxl_rr)1062 static void cxl_rr_free_decoder(struct cxl_region_ref *cxl_rr)
1063 {
1064 struct cxl_region *cxlr = cxl_rr->region;
1065 struct cxl_decoder *cxld = cxl_rr->decoder;
1066
1067 if (!cxld)
1068 return;
1069
1070 dev_WARN_ONCE(&cxlr->dev, cxld->region != cxlr, "region mismatch\n");
1071 if (cxld->region == cxlr) {
1072 cxld->region = NULL;
1073 put_device(&cxlr->dev);
1074 }
1075 }
1076
free_region_ref(struct cxl_region_ref * cxl_rr)1077 static void free_region_ref(struct cxl_region_ref *cxl_rr)
1078 {
1079 struct cxl_port *port = cxl_rr->port;
1080 struct cxl_region *cxlr = cxl_rr->region;
1081
1082 cxl_rr_free_decoder(cxl_rr);
1083 xa_erase(&port->regions, (unsigned long)cxlr);
1084 xa_destroy(&cxl_rr->endpoints);
1085 kfree(cxl_rr);
1086 }
1087
cxl_rr_ep_add(struct cxl_region_ref * cxl_rr,struct cxl_endpoint_decoder * cxled)1088 static int cxl_rr_ep_add(struct cxl_region_ref *cxl_rr,
1089 struct cxl_endpoint_decoder *cxled)
1090 {
1091 int rc;
1092 struct cxl_port *port = cxl_rr->port;
1093 struct cxl_region *cxlr = cxl_rr->region;
1094 struct cxl_decoder *cxld = cxl_rr->decoder;
1095 struct cxl_ep *ep = cxl_ep_load(port, cxled_to_memdev(cxled));
1096
1097 if (ep) {
1098 rc = xa_insert(&cxl_rr->endpoints, (unsigned long)cxled, ep,
1099 GFP_KERNEL);
1100 if (rc)
1101 return rc;
1102 }
1103 cxl_rr->nr_eps++;
1104
1105 if (!cxld->region) {
1106 cxld->region = cxlr;
1107
1108 /*
1109 * Now that cxld->region is set the intermediate staging state
1110 * can be cleared.
1111 */
1112 if (cxld == &cxled->cxld &&
1113 cxled->state == CXL_DECODER_STATE_AUTO_STAGED)
1114 cxled->state = CXL_DECODER_STATE_AUTO;
1115 get_device(&cxlr->dev);
1116 }
1117
1118 return 0;
1119 }
1120
cxl_rr_assign_decoder(struct cxl_port * port,struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled,struct cxl_region_ref * cxl_rr,struct cxl_decoder * cxld)1121 static int cxl_rr_assign_decoder(struct cxl_port *port, struct cxl_region *cxlr,
1122 struct cxl_endpoint_decoder *cxled,
1123 struct cxl_region_ref *cxl_rr,
1124 struct cxl_decoder *cxld)
1125 {
1126 if (cxld->region) {
1127 dev_dbg(&cxlr->dev, "%s: %s already attached to %s\n",
1128 dev_name(&port->dev), dev_name(&cxld->dev),
1129 dev_name(&cxld->region->dev));
1130 return -EBUSY;
1131 }
1132
1133 /*
1134 * Endpoints should already match the region type, but backstop that
1135 * assumption with an assertion. Switch-decoders change mapping-type
1136 * based on what is mapped when they are assigned to a region.
1137 */
1138 dev_WARN_ONCE(&cxlr->dev,
1139 port == cxled_to_port(cxled) &&
1140 cxld->target_type != cxlr->type,
1141 "%s:%s mismatch decoder type %d -> %d\n",
1142 dev_name(&cxled_to_memdev(cxled)->dev),
1143 dev_name(&cxld->dev), cxld->target_type, cxlr->type);
1144 cxld->target_type = cxlr->type;
1145 cxl_rr->decoder = cxld;
1146 return 0;
1147 }
1148
cxl_region_setup_flags(struct cxl_region * cxlr,struct cxl_decoder * cxld)1149 static void cxl_region_setup_flags(struct cxl_region *cxlr,
1150 struct cxl_decoder *cxld)
1151 {
1152 if (is_endpoint_decoder(&cxld->dev)) {
1153 struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(&cxld->dev);
1154 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1155
1156 /*
1157 * When a region's memdevs specify an @attach method the attach
1158 * provider is responsible for dispositioning the region for
1159 * both probe and userspace management
1160 */
1161 if (cxlmd->attach)
1162 set_bit(CXL_REGION_F_LOCK, &cxlr->flags);
1163 }
1164
1165 if (cxld->flags & CXL_DECODER_F_LOCK) {
1166 set_bit(CXL_REGION_F_LOCK, &cxlr->flags);
1167 clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags);
1168 }
1169
1170 if (cxld->flags & CXL_DECODER_F_NORMALIZED_ADDRESSING)
1171 set_bit(CXL_REGION_F_NORMALIZED_ADDRESSING, &cxlr->flags);
1172 }
1173
1174 /**
1175 * cxl_port_attach_region() - track a region's interest in a port by endpoint
1176 * @port: port to add a new region reference 'struct cxl_region_ref'
1177 * @cxlr: region to attach to @port
1178 * @cxled: endpoint decoder used to create or further pin a region reference
1179 * @pos: interleave position of @cxled in @cxlr
1180 *
1181 * The attach event is an opportunity to validate CXL decode setup
1182 * constraints and record metadata needed for programming HDM decoders,
1183 * in particular decoder target lists.
1184 *
1185 * The steps are:
1186 *
1187 * - validate that there are no other regions with a higher HPA already
1188 * associated with @port
1189 * - establish a region reference if one is not already present
1190 *
1191 * - additionally allocate a decoder instance that will host @cxlr on
1192 * @port
1193 *
1194 * - pin the region reference by the endpoint
1195 * - account for how many entries in @port's target list are needed to
1196 * cover all of the added endpoints.
1197 */
cxl_port_attach_region(struct cxl_port * port,struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled,int pos)1198 static int cxl_port_attach_region(struct cxl_port *port,
1199 struct cxl_region *cxlr,
1200 struct cxl_endpoint_decoder *cxled, int pos)
1201 {
1202 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1203 struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
1204 struct cxl_region_ref *cxl_rr;
1205 bool nr_targets_inc = false;
1206 struct cxl_decoder *cxld;
1207 unsigned long index;
1208 int rc = -EBUSY;
1209
1210 lockdep_assert_held_write(&cxl_rwsem.region);
1211
1212 cxl_rr = cxl_rr_load(port, cxlr);
1213 if (cxl_rr) {
1214 struct cxl_ep *ep_iter;
1215 int found = 0;
1216
1217 /*
1218 * Walk the existing endpoints that have been attached to
1219 * @cxlr at @port and see if they share the same 'next' port
1220 * in the downstream direction. I.e. endpoints that share common
1221 * upstream switch.
1222 */
1223 xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
1224 if (ep_iter == ep)
1225 continue;
1226 if (ep_iter->next == ep->next) {
1227 found++;
1228 break;
1229 }
1230 }
1231
1232 /*
1233 * New target port, or @port is an endpoint port that always
1234 * accounts its own local decode as a target.
1235 */
1236 if (!found || !ep->next) {
1237 cxl_rr->nr_targets++;
1238 nr_targets_inc = true;
1239 }
1240 } else {
1241 cxld = cxl_port_pick_region_decoder(port, cxled, cxlr);
1242 if (!cxld) {
1243 dev_dbg(&cxlr->dev, "%s: no decoder available\n",
1244 dev_name(&port->dev));
1245 return -EBUSY;
1246 }
1247
1248 cxl_rr = alloc_region_ref(port, cxlr, cxled, cxld);
1249 if (IS_ERR(cxl_rr)) {
1250 dev_dbg(&cxlr->dev,
1251 "%s: failed to allocate region reference\n",
1252 dev_name(&port->dev));
1253 return PTR_ERR(cxl_rr);
1254 }
1255 nr_targets_inc = true;
1256
1257 rc = cxl_rr_assign_decoder(port, cxlr, cxled, cxl_rr, cxld);
1258 if (rc)
1259 goto out_erase;
1260 }
1261 cxld = cxl_rr->decoder;
1262
1263 /*
1264 * the number of targets should not exceed the target_count
1265 * of the decoder
1266 */
1267 if (is_switch_decoder(&cxld->dev)) {
1268 struct cxl_switch_decoder *cxlsd;
1269
1270 cxlsd = to_cxl_switch_decoder(&cxld->dev);
1271 if (cxl_rr->nr_targets > cxlsd->nr_targets) {
1272 dev_dbg(&cxlr->dev,
1273 "%s:%s %s add: %s:%s @ %d overflows targets: %d\n",
1274 dev_name(port->uport_dev), dev_name(&port->dev),
1275 dev_name(&cxld->dev), dev_name(&cxlmd->dev),
1276 dev_name(&cxled->cxld.dev), pos,
1277 cxlsd->nr_targets);
1278 rc = -ENXIO;
1279 goto out_erase;
1280 }
1281 }
1282
1283 cxl_region_setup_flags(cxlr, cxld);
1284
1285 rc = cxl_rr_ep_add(cxl_rr, cxled);
1286 if (rc) {
1287 dev_dbg(&cxlr->dev,
1288 "%s: failed to track endpoint %s:%s reference\n",
1289 dev_name(&port->dev), dev_name(&cxlmd->dev),
1290 dev_name(&cxld->dev));
1291 goto out_erase;
1292 }
1293
1294 dev_dbg(&cxlr->dev,
1295 "%s:%s %s add: %s:%s @ %d next: %s nr_eps: %d nr_targets: %d\n",
1296 dev_name(port->uport_dev), dev_name(&port->dev),
1297 dev_name(&cxld->dev), dev_name(&cxlmd->dev),
1298 dev_name(&cxled->cxld.dev), pos,
1299 ep ? ep->next ? dev_name(ep->next->uport_dev) :
1300 dev_name(&cxlmd->dev) :
1301 "none",
1302 cxl_rr->nr_eps, cxl_rr->nr_targets);
1303
1304 return 0;
1305 out_erase:
1306 if (nr_targets_inc)
1307 cxl_rr->nr_targets--;
1308 if (cxl_rr->nr_eps == 0)
1309 free_region_ref(cxl_rr);
1310 return rc;
1311 }
1312
cxl_port_detach_region(struct cxl_port * port,struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled)1313 static void cxl_port_detach_region(struct cxl_port *port,
1314 struct cxl_region *cxlr,
1315 struct cxl_endpoint_decoder *cxled)
1316 {
1317 struct cxl_region_ref *cxl_rr;
1318 struct cxl_ep *ep = NULL;
1319
1320 lockdep_assert_held_write(&cxl_rwsem.region);
1321
1322 cxl_rr = cxl_rr_load(port, cxlr);
1323 if (!cxl_rr)
1324 return;
1325
1326 /*
1327 * Endpoint ports do not carry cxl_ep references, and they
1328 * never target more than one endpoint by definition
1329 */
1330 if (cxl_rr->decoder == &cxled->cxld)
1331 cxl_rr->nr_eps--;
1332 else
1333 ep = xa_erase(&cxl_rr->endpoints, (unsigned long)cxled);
1334 if (ep) {
1335 struct cxl_ep *ep_iter;
1336 unsigned long index;
1337 int found = 0;
1338
1339 cxl_rr->nr_eps--;
1340 xa_for_each(&cxl_rr->endpoints, index, ep_iter) {
1341 if (ep_iter->next == ep->next) {
1342 found++;
1343 break;
1344 }
1345 }
1346 if (!found)
1347 cxl_rr->nr_targets--;
1348 }
1349
1350 if (cxl_rr->nr_eps == 0)
1351 free_region_ref(cxl_rr);
1352 }
1353
check_last_peer(struct cxl_endpoint_decoder * cxled,struct cxl_ep * ep,struct cxl_region_ref * cxl_rr,int distance)1354 static int check_last_peer(struct cxl_endpoint_decoder *cxled,
1355 struct cxl_ep *ep, struct cxl_region_ref *cxl_rr,
1356 int distance)
1357 {
1358 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1359 struct cxl_region *cxlr = cxl_rr->region;
1360 struct cxl_region_params *p = &cxlr->params;
1361 struct cxl_endpoint_decoder *cxled_peer;
1362 struct cxl_port *port = cxl_rr->port;
1363 struct cxl_memdev *cxlmd_peer;
1364 struct cxl_ep *ep_peer;
1365 int pos = cxled->pos;
1366
1367 /*
1368 * If this position wants to share a dport with the last endpoint mapped
1369 * then that endpoint, at index 'position - distance', must also be
1370 * mapped by this dport.
1371 */
1372 if (pos < distance) {
1373 dev_dbg(&cxlr->dev, "%s:%s: cannot host %s:%s at %d\n",
1374 dev_name(port->uport_dev), dev_name(&port->dev),
1375 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1376 return -ENXIO;
1377 }
1378 cxled_peer = p->targets[pos - distance];
1379 cxlmd_peer = cxled_to_memdev(cxled_peer);
1380 ep_peer = cxl_ep_load(port, cxlmd_peer);
1381 if (ep->dport != ep_peer->dport) {
1382 dev_dbg(&cxlr->dev,
1383 "%s:%s: %s:%s pos %d mismatched peer %s:%s\n",
1384 dev_name(port->uport_dev), dev_name(&port->dev),
1385 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos,
1386 dev_name(&cxlmd_peer->dev),
1387 dev_name(&cxled_peer->cxld.dev));
1388 return -ENXIO;
1389 }
1390
1391 return 0;
1392 }
1393
check_interleave_cap(struct cxl_decoder * cxld,int iw,int ig)1394 static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig)
1395 {
1396 struct cxl_port *port = to_cxl_port(cxld->dev.parent);
1397 struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev);
1398 unsigned int interleave_mask;
1399 u8 eiw;
1400 u16 eig;
1401 int high_pos, low_pos;
1402
1403 if (!test_bit(iw, &cxlhdm->iw_cap_mask))
1404 return -ENXIO;
1405 /*
1406 * Per CXL specification r3.1(8.2.4.20.13 Decoder Protection),
1407 * if eiw < 8:
1408 * DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + 8 + eiw]
1409 * DPAOFFSET[eig + 7: 0] = HPAOFFSET[eig + 7: 0]
1410 *
1411 * when the eiw is 0, all the bits of HPAOFFSET[51: 0] are used, the
1412 * interleave bits are none.
1413 *
1414 * if eiw >= 8:
1415 * DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + eiw] / 3
1416 * DPAOFFSET[eig + 7: 0] = HPAOFFSET[eig + 7: 0]
1417 *
1418 * when the eiw is 8, all the bits of HPAOFFSET[51: 0] are used, the
1419 * interleave bits are none.
1420 */
1421 ways_to_eiw(iw, &eiw);
1422 if (eiw == 0 || eiw == 8)
1423 return 0;
1424
1425 granularity_to_eig(ig, &eig);
1426 if (eiw > 8)
1427 high_pos = eiw + eig - 1;
1428 else
1429 high_pos = eiw + eig + 7;
1430 low_pos = eig + 8;
1431 interleave_mask = GENMASK(high_pos, low_pos);
1432 if (interleave_mask & ~cxlhdm->interleave_mask)
1433 return -ENXIO;
1434
1435 return 0;
1436 }
1437
cxl_port_setup_targets(struct cxl_port * port,struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled)1438 static int cxl_port_setup_targets(struct cxl_port *port,
1439 struct cxl_region *cxlr,
1440 struct cxl_endpoint_decoder *cxled)
1441 {
1442 struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
1443 int parent_iw, parent_ig, ig, iw, rc, pos = cxled->pos;
1444 struct cxl_port *parent_port = to_cxl_port(port->dev.parent);
1445 struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
1446 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1447 struct cxl_ep *ep = cxl_ep_load(port, cxlmd);
1448 struct cxl_region_params *p = &cxlr->params;
1449 struct cxl_decoder *cxld = cxl_rr->decoder;
1450 struct cxl_switch_decoder *cxlsd;
1451 struct cxl_port *iter = port;
1452 u16 eig, peig;
1453 u8 eiw, peiw;
1454
1455 /*
1456 * While root level decoders support x3, x6, x12, switch level
1457 * decoders only support powers of 2 up to x16.
1458 */
1459 if (!is_power_of_2(cxl_rr->nr_targets)) {
1460 dev_dbg(&cxlr->dev, "%s:%s: invalid target count %d\n",
1461 dev_name(port->uport_dev), dev_name(&port->dev),
1462 cxl_rr->nr_targets);
1463 return -EINVAL;
1464 }
1465
1466 cxlsd = to_cxl_switch_decoder(&cxld->dev);
1467 if (cxl_rr->nr_targets_set) {
1468 int i, distance = 1;
1469 struct cxl_region_ref *cxl_rr_iter;
1470
1471 /*
1472 * The "distance" between peer downstream ports represents which
1473 * endpoint positions in the region interleave a given port can
1474 * host.
1475 *
1476 * For example, at the root of a hierarchy the distance is
1477 * always 1 as every index targets a different host-bridge. At
1478 * each subsequent switch level those ports map every Nth region
1479 * position where N is the width of the switch == distance.
1480 */
1481 do {
1482 cxl_rr_iter = cxl_rr_load(iter, cxlr);
1483 distance *= cxl_rr_iter->nr_targets;
1484 iter = to_cxl_port(iter->dev.parent);
1485 } while (!is_cxl_root(iter));
1486 distance *= cxlrd->cxlsd.cxld.interleave_ways;
1487
1488 for (i = 0; i < cxl_rr->nr_targets_set; i++)
1489 if (ep->dport == cxlsd->target[i]) {
1490 rc = check_last_peer(cxled, ep, cxl_rr,
1491 distance);
1492 if (rc)
1493 return rc;
1494 goto out_target_set;
1495 }
1496 goto add_target;
1497 }
1498
1499 if (is_cxl_root(parent_port)) {
1500 /*
1501 * Root decoder IG is always set to value in CFMWS which
1502 * may be different than this region's IG. We can use the
1503 * region's IG here since interleave_granularity_store()
1504 * does not allow interleaved host-bridges with
1505 * root IG != region IG.
1506 */
1507 parent_ig = p->interleave_granularity;
1508 parent_iw = cxlrd->cxlsd.cxld.interleave_ways;
1509 /*
1510 * For purposes of address bit routing, use power-of-2 math for
1511 * switch ports.
1512 */
1513 if (!is_power_of_2(parent_iw))
1514 parent_iw /= 3;
1515 } else {
1516 struct cxl_region_ref *parent_rr;
1517 struct cxl_decoder *parent_cxld;
1518
1519 parent_rr = cxl_rr_load(parent_port, cxlr);
1520 parent_cxld = parent_rr->decoder;
1521 parent_ig = parent_cxld->interleave_granularity;
1522 parent_iw = parent_cxld->interleave_ways;
1523 }
1524
1525 rc = granularity_to_eig(parent_ig, &peig);
1526 if (rc) {
1527 dev_dbg(&cxlr->dev, "%s:%s: invalid parent granularity: %d\n",
1528 dev_name(parent_port->uport_dev),
1529 dev_name(&parent_port->dev), parent_ig);
1530 return rc;
1531 }
1532
1533 rc = ways_to_eiw(parent_iw, &peiw);
1534 if (rc) {
1535 dev_dbg(&cxlr->dev, "%s:%s: invalid parent interleave: %d\n",
1536 dev_name(parent_port->uport_dev),
1537 dev_name(&parent_port->dev), parent_iw);
1538 return rc;
1539 }
1540
1541 iw = cxl_rr->nr_targets;
1542 rc = ways_to_eiw(iw, &eiw);
1543 if (rc) {
1544 dev_dbg(&cxlr->dev, "%s:%s: invalid port interleave: %d\n",
1545 dev_name(port->uport_dev), dev_name(&port->dev), iw);
1546 return rc;
1547 }
1548
1549 /*
1550 * Interleave granularity is a multiple of @parent_port granularity.
1551 * Multiplier is the parent port interleave ways.
1552 */
1553 rc = granularity_to_eig(parent_ig * parent_iw, &eig);
1554 if (rc) {
1555 dev_dbg(&cxlr->dev,
1556 "%s: invalid granularity calculation (%d * %d)\n",
1557 dev_name(&parent_port->dev), parent_ig, parent_iw);
1558 return rc;
1559 }
1560
1561 rc = eig_to_granularity(eig, &ig);
1562 if (rc) {
1563 dev_dbg(&cxlr->dev, "%s:%s: invalid interleave: %d\n",
1564 dev_name(port->uport_dev), dev_name(&port->dev),
1565 256 << eig);
1566 return rc;
1567 }
1568
1569 if (iw > 8 || iw > cxlsd->nr_targets) {
1570 dev_dbg(&cxlr->dev,
1571 "%s:%s:%s: ways: %d overflows targets: %d\n",
1572 dev_name(port->uport_dev), dev_name(&port->dev),
1573 dev_name(&cxld->dev), iw, cxlsd->nr_targets);
1574 return -ENXIO;
1575 }
1576
1577 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1578 if (cxld->interleave_ways != iw ||
1579 (iw > 1 && cxld->interleave_granularity != ig) ||
1580 !spa_maps_hpa(p, &cxld->hpa_range) ||
1581 ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) {
1582 dev_err(&cxlr->dev,
1583 "%s:%s %s expected iw: %d ig: %d %pr\n",
1584 dev_name(port->uport_dev), dev_name(&port->dev),
1585 __func__, iw, ig, p->res);
1586 dev_err(&cxlr->dev,
1587 "%s:%s %s got iw: %d ig: %d state: %s %#llx:%#llx\n",
1588 dev_name(port->uport_dev), dev_name(&port->dev),
1589 __func__, cxld->interleave_ways,
1590 cxld->interleave_granularity,
1591 str_enabled_disabled(cxld->flags & CXL_DECODER_F_ENABLE),
1592 cxld->hpa_range.start, cxld->hpa_range.end);
1593 return -ENXIO;
1594 }
1595 } else {
1596 rc = check_interleave_cap(cxld, iw, ig);
1597 if (rc) {
1598 dev_dbg(&cxlr->dev,
1599 "%s:%s iw: %d ig: %d is not supported\n",
1600 dev_name(port->uport_dev),
1601 dev_name(&port->dev), iw, ig);
1602 return rc;
1603 }
1604
1605 cxld->interleave_ways = iw;
1606 cxld->interleave_granularity = ig;
1607 cxld->hpa_range = (struct range) {
1608 .start = p->res->start,
1609 .end = p->res->end,
1610 };
1611 }
1612 dev_dbg(&cxlr->dev, "%s:%s iw: %d ig: %d\n", dev_name(port->uport_dev),
1613 dev_name(&port->dev), iw, ig);
1614 add_target:
1615 if (cxl_rr->nr_targets_set == cxl_rr->nr_targets) {
1616 dev_dbg(&cxlr->dev,
1617 "%s:%s: targets full trying to add %s:%s at %d\n",
1618 dev_name(port->uport_dev), dev_name(&port->dev),
1619 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1620 return -ENXIO;
1621 }
1622 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
1623 if (cxlsd->target[cxl_rr->nr_targets_set] != ep->dport) {
1624 dev_dbg(&cxlr->dev, "%s:%s: %s expected %s at %d\n",
1625 dev_name(port->uport_dev), dev_name(&port->dev),
1626 dev_name(&cxlsd->cxld.dev),
1627 dev_name(ep->dport->dport_dev),
1628 cxl_rr->nr_targets_set);
1629 return -ENXIO;
1630 }
1631 } else {
1632 cxlsd->target[cxl_rr->nr_targets_set] = ep->dport;
1633 cxlsd->cxld.target_map[cxl_rr->nr_targets_set] = ep->dport->port_id;
1634 }
1635 cxl_rr->nr_targets_set++;
1636 out_target_set:
1637 dev_dbg(&cxlr->dev, "%s:%s target[%d] = %s for %s:%s @ %d\n",
1638 dev_name(port->uport_dev), dev_name(&port->dev),
1639 cxl_rr->nr_targets_set - 1, dev_name(ep->dport->dport_dev),
1640 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos);
1641
1642 return 0;
1643 }
1644
cxl_port_reset_targets(struct cxl_port * port,struct cxl_region * cxlr)1645 static void cxl_port_reset_targets(struct cxl_port *port,
1646 struct cxl_region *cxlr)
1647 {
1648 struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr);
1649 struct cxl_decoder *cxld;
1650
1651 /*
1652 * After the last endpoint has been detached the entire cxl_rr may now
1653 * be gone.
1654 */
1655 if (!cxl_rr)
1656 return;
1657 cxl_rr->nr_targets_set = 0;
1658
1659 cxld = cxl_rr->decoder;
1660 cxld->hpa_range = (struct range) {
1661 .start = 0,
1662 .end = -1,
1663 };
1664 }
1665
cxl_region_teardown_targets(struct cxl_region * cxlr)1666 static void cxl_region_teardown_targets(struct cxl_region *cxlr)
1667 {
1668 struct cxl_region_params *p = &cxlr->params;
1669 struct cxl_endpoint_decoder *cxled;
1670 struct cxl_dev_state *cxlds;
1671 struct cxl_memdev *cxlmd;
1672 struct cxl_port *iter;
1673 struct cxl_ep *ep;
1674 int i;
1675
1676 /*
1677 * In the auto-discovery case skip automatic teardown since the
1678 * address space is already active
1679 */
1680 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags))
1681 return;
1682
1683 for (i = 0; i < p->nr_targets; i++) {
1684 cxled = p->targets[i];
1685 cxlmd = cxled_to_memdev(cxled);
1686 cxlds = cxlmd->cxlds;
1687
1688 if (cxlds->rcd)
1689 continue;
1690
1691 iter = cxled_to_port(cxled);
1692 while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
1693 iter = to_cxl_port(iter->dev.parent);
1694
1695 for (ep = cxl_ep_load(iter, cxlmd); iter;
1696 iter = ep->next, ep = cxl_ep_load(iter, cxlmd))
1697 cxl_port_reset_targets(iter, cxlr);
1698 }
1699 }
1700
cxl_region_setup_targets(struct cxl_region * cxlr)1701 static int cxl_region_setup_targets(struct cxl_region *cxlr)
1702 {
1703 struct cxl_region_params *p = &cxlr->params;
1704 struct cxl_endpoint_decoder *cxled;
1705 struct cxl_dev_state *cxlds;
1706 int i, rc, rch = 0, vh = 0;
1707 struct cxl_memdev *cxlmd;
1708 struct cxl_port *iter;
1709 struct cxl_ep *ep;
1710
1711 for (i = 0; i < p->nr_targets; i++) {
1712 cxled = p->targets[i];
1713 cxlmd = cxled_to_memdev(cxled);
1714 cxlds = cxlmd->cxlds;
1715
1716 /* validate that all targets agree on topology */
1717 if (!cxlds->rcd) {
1718 vh++;
1719 } else {
1720 rch++;
1721 continue;
1722 }
1723
1724 iter = cxled_to_port(cxled);
1725 while (!is_cxl_root(to_cxl_port(iter->dev.parent)))
1726 iter = to_cxl_port(iter->dev.parent);
1727
1728 /*
1729 * Descend the topology tree programming / validating
1730 * targets while looking for conflicts.
1731 */
1732 for (ep = cxl_ep_load(iter, cxlmd); iter;
1733 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) {
1734 rc = cxl_port_setup_targets(iter, cxlr, cxled);
1735 if (rc) {
1736 cxl_region_teardown_targets(cxlr);
1737 return rc;
1738 }
1739 }
1740 }
1741
1742 if (rch && vh) {
1743 dev_err(&cxlr->dev, "mismatched CXL topologies detected\n");
1744 cxl_region_teardown_targets(cxlr);
1745 return -ENXIO;
1746 }
1747
1748 return 0;
1749 }
1750
cxl_region_validate_position(struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled,int pos)1751 static int cxl_region_validate_position(struct cxl_region *cxlr,
1752 struct cxl_endpoint_decoder *cxled,
1753 int pos)
1754 {
1755 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1756 struct cxl_region_params *p = &cxlr->params;
1757 int i;
1758
1759 if (pos < 0 || pos >= p->interleave_ways) {
1760 dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
1761 p->interleave_ways);
1762 return -ENXIO;
1763 }
1764
1765 if (p->targets[pos] == cxled)
1766 return 0;
1767
1768 if (p->targets[pos]) {
1769 struct cxl_endpoint_decoder *cxled_target = p->targets[pos];
1770 struct cxl_memdev *cxlmd_target = cxled_to_memdev(cxled_target);
1771
1772 dev_dbg(&cxlr->dev, "position %d already assigned to %s:%s\n",
1773 pos, dev_name(&cxlmd_target->dev),
1774 dev_name(&cxled_target->cxld.dev));
1775 return -EBUSY;
1776 }
1777
1778 for (i = 0; i < p->interleave_ways; i++) {
1779 struct cxl_endpoint_decoder *cxled_target;
1780 struct cxl_memdev *cxlmd_target;
1781
1782 cxled_target = p->targets[i];
1783 if (!cxled_target)
1784 continue;
1785
1786 cxlmd_target = cxled_to_memdev(cxled_target);
1787 if (cxlmd_target == cxlmd) {
1788 dev_dbg(&cxlr->dev,
1789 "%s already specified at position %d via: %s\n",
1790 dev_name(&cxlmd->dev), pos,
1791 dev_name(&cxled_target->cxld.dev));
1792 return -EBUSY;
1793 }
1794 }
1795
1796 return 0;
1797 }
1798
cxl_region_attach_position(struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled,const struct cxl_dport * dport,int pos)1799 static int cxl_region_attach_position(struct cxl_region *cxlr,
1800 struct cxl_endpoint_decoder *cxled,
1801 const struct cxl_dport *dport, int pos)
1802 {
1803 struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
1804 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1805 struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd;
1806 struct cxl_decoder *cxld = &cxlsd->cxld;
1807 int iw = cxld->interleave_ways;
1808 struct cxl_port *iter;
1809 int rc;
1810
1811 if (dport != cxlrd->cxlsd.target[pos % iw]) {
1812 dev_dbg(&cxlr->dev, "%s:%s invalid target position for %s\n",
1813 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
1814 dev_name(&cxlrd->cxlsd.cxld.dev));
1815 return -ENXIO;
1816 }
1817
1818 for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
1819 iter = to_cxl_port(iter->dev.parent)) {
1820 rc = cxl_port_attach_region(iter, cxlr, cxled, pos);
1821 if (rc)
1822 goto err;
1823 }
1824
1825 return 0;
1826
1827 err:
1828 for (iter = cxled_to_port(cxled); !is_cxl_root(iter);
1829 iter = to_cxl_port(iter->dev.parent))
1830 cxl_port_detach_region(iter, cxlr, cxled);
1831 return rc;
1832 }
1833
cxl_region_attach_auto(struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled,int pos)1834 static int cxl_region_attach_auto(struct cxl_region *cxlr,
1835 struct cxl_endpoint_decoder *cxled, int pos)
1836 {
1837 struct cxl_region_params *p = &cxlr->params;
1838
1839 if (cxled->state != CXL_DECODER_STATE_AUTO) {
1840 dev_err(&cxlr->dev,
1841 "%s: unable to add decoder to autodetected region\n",
1842 dev_name(&cxled->cxld.dev));
1843 return -EINVAL;
1844 }
1845
1846 if (pos >= 0) {
1847 dev_dbg(&cxlr->dev, "%s: expected auto position, not %d\n",
1848 dev_name(&cxled->cxld.dev), pos);
1849 return -EINVAL;
1850 }
1851
1852 if (p->nr_targets >= p->interleave_ways) {
1853 dev_err(&cxlr->dev, "%s: no more target slots available\n",
1854 dev_name(&cxled->cxld.dev));
1855 return -ENXIO;
1856 }
1857
1858 /*
1859 * Temporarily record the endpoint decoder into the target array. Yes,
1860 * this means that userspace can view devices in the wrong position
1861 * before the region activates, and must be careful to understand when
1862 * it might be racing region autodiscovery.
1863 *
1864 * The endpoint decoder will be recorded into the first free slot of
1865 * the target array.
1866 */
1867 for (pos = 0; pos < p->interleave_ways; pos++) {
1868 if (!p->targets[pos])
1869 break;
1870 }
1871
1872 if (pos == p->interleave_ways) {
1873 dev_err(&cxlr->dev, "%s: unable to find a free target slot\n",
1874 dev_name(&cxled->cxld.dev));
1875 return -ENXIO;
1876 }
1877
1878 p->targets[pos] = cxled;
1879 cxled->pos = pos;
1880 cxled->state = CXL_DECODER_STATE_AUTO_STAGED;
1881 p->nr_targets++;
1882
1883 return 0;
1884 }
1885
cmp_interleave_pos(const void * a,const void * b)1886 static int cmp_interleave_pos(const void *a, const void *b)
1887 {
1888 struct cxl_endpoint_decoder *cxled_a = *(typeof(cxled_a) *)a;
1889 struct cxl_endpoint_decoder *cxled_b = *(typeof(cxled_b) *)b;
1890
1891 return cxled_a->pos - cxled_b->pos;
1892 }
1893
match_switch_decoder_by_range(struct device * dev,const void * data)1894 static int match_switch_decoder_by_range(struct device *dev,
1895 const void *data)
1896 {
1897 struct cxl_switch_decoder *cxlsd;
1898 const struct range *r1, *r2 = data;
1899
1900
1901 if (!is_switch_decoder(dev))
1902 return 0;
1903
1904 cxlsd = to_cxl_switch_decoder(dev);
1905 r1 = &cxlsd->cxld.hpa_range;
1906
1907 if (is_root_decoder(dev))
1908 return range_contains(r1, r2);
1909 return (r1->start == r2->start && r1->end == r2->end);
1910 }
1911
find_pos_and_ways(struct cxl_port * port,struct range * range,int * pos,int * ways)1912 static int find_pos_and_ways(struct cxl_port *port, struct range *range,
1913 int *pos, int *ways)
1914 {
1915 struct cxl_switch_decoder *cxlsd;
1916 struct cxl_port *parent;
1917 int rc = -ENXIO;
1918
1919 parent = parent_port_of(port);
1920 if (!parent)
1921 return rc;
1922
1923 struct device *dev __free(put_device) =
1924 device_find_child(&parent->dev, range, match_switch_decoder_by_range);
1925 if (!dev) {
1926 dev_err(port->uport_dev,
1927 "failed to find decoder mapping %#llx-%#llx\n",
1928 range->start, range->end);
1929 return rc;
1930 }
1931 cxlsd = to_cxl_switch_decoder(dev);
1932 *ways = cxlsd->cxld.interleave_ways;
1933
1934 for (int i = 0; i < *ways; i++) {
1935 if (cxlsd->target[i] == port->parent_dport) {
1936 *pos = i;
1937 rc = 0;
1938 break;
1939 }
1940 }
1941 if (rc)
1942 dev_err(port->uport_dev,
1943 "failed to find %s:%s in target list of %s\n",
1944 dev_name(&port->dev),
1945 dev_name(port->parent_dport->dport_dev), dev_name(dev));
1946
1947 return rc;
1948 }
1949
1950 /**
1951 * cxl_calc_interleave_pos() - calculate an endpoint position in a region
1952 * @cxled: endpoint decoder member of given region
1953 * @hpa_range: translated HPA range of the endpoint
1954 *
1955 * The endpoint position is calculated by traversing the topology from
1956 * the endpoint to the root decoder and iteratively applying this
1957 * calculation:
1958 *
1959 * position = position * parent_ways + parent_pos;
1960 *
1961 * ...where @position is inferred from switch and root decoder target lists.
1962 *
1963 * Return: position >= 0 on success
1964 * -ENXIO on failure
1965 */
cxl_calc_interleave_pos(struct cxl_endpoint_decoder * cxled,struct range * hpa_range)1966 static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled,
1967 struct range *hpa_range)
1968 {
1969 struct cxl_port *iter, *port = cxled_to_port(cxled);
1970 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
1971 int parent_ways = 0, parent_pos = 0, pos = 0;
1972 int rc;
1973
1974 /*
1975 * Example: the expected interleave order of the 4-way region shown
1976 * below is: mem0, mem2, mem1, mem3
1977 *
1978 * root_port
1979 * / \
1980 * host_bridge_0 host_bridge_1
1981 * | | | |
1982 * mem0 mem1 mem2 mem3
1983 *
1984 * In the example the calculator will iterate twice. The first iteration
1985 * uses the mem position in the host-bridge and the ways of the host-
1986 * bridge to generate the first, or local, position. The second
1987 * iteration uses the host-bridge position in the root_port and the ways
1988 * of the root_port to refine the position.
1989 *
1990 * A trace of the calculation per endpoint looks like this:
1991 * mem0: pos = 0 * 2 + 0 mem2: pos = 0 * 2 + 0
1992 * pos = 0 * 2 + 0 pos = 0 * 2 + 1
1993 * pos: 0 pos: 1
1994 *
1995 * mem1: pos = 0 * 2 + 1 mem3: pos = 0 * 2 + 1
1996 * pos = 1 * 2 + 0 pos = 1 * 2 + 1
1997 * pos: 2 pos = 3
1998 *
1999 * Note that while this example is simple, the method applies to more
2000 * complex topologies, including those with switches.
2001 */
2002
2003 /* Iterate from endpoint to root_port refining the position */
2004 for (iter = port; iter; iter = parent_port_of(iter)) {
2005 if (is_cxl_root(iter))
2006 break;
2007
2008 rc = find_pos_and_ways(iter, hpa_range, &parent_pos,
2009 &parent_ways);
2010 if (rc)
2011 return rc;
2012
2013 pos = pos * parent_ways + parent_pos;
2014 }
2015
2016 dev_dbg(&cxlmd->dev,
2017 "decoder:%s parent:%s port:%s range:%#llx-%#llx pos:%d\n",
2018 dev_name(&cxled->cxld.dev), dev_name(cxlmd->dev.parent),
2019 dev_name(&port->dev), hpa_range->start, hpa_range->end, pos);
2020
2021 return pos;
2022 }
2023
cxl_region_sort_targets(struct cxl_region * cxlr)2024 static int cxl_region_sort_targets(struct cxl_region *cxlr)
2025 {
2026 struct cxl_region_params *p = &cxlr->params;
2027 int i, rc = 0;
2028
2029 for (i = 0; i < p->nr_targets; i++) {
2030 struct cxl_endpoint_decoder *cxled = p->targets[i];
2031
2032 cxled->pos = cxl_calc_interleave_pos(cxled, &cxlr->hpa_range);
2033 /*
2034 * Record that sorting failed, but still continue to calc
2035 * cxled->pos so that cxl_calc_interleave_pos() emits its
2036 * dev_dbg() for every member. which is useful for auto
2037 * discovery debug.
2038 */
2039 if (cxled->pos < 0)
2040 rc = -ENXIO;
2041 }
2042 /* Keep the cxlr target list in interleave position order */
2043 sort(p->targets, p->nr_targets, sizeof(p->targets[0]),
2044 cmp_interleave_pos, NULL);
2045
2046 dev_dbg(&cxlr->dev, "region sort %s\n", rc ? "failed" : "successful");
2047 return rc;
2048 }
2049
cxl_region_attach(struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled,int pos)2050 static int cxl_region_attach(struct cxl_region *cxlr,
2051 struct cxl_endpoint_decoder *cxled, int pos)
2052 {
2053 struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
2054 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
2055 struct cxl_dev_state *cxlds = cxlmd->cxlds;
2056 struct cxl_region_params *p = &cxlr->params;
2057 struct cxl_port *ep_port, *root_port;
2058 struct cxl_dport *dport;
2059 int rc = -ENXIO;
2060
2061 rc = check_interleave_cap(&cxled->cxld, p->interleave_ways,
2062 p->interleave_granularity);
2063 if (rc) {
2064 dev_dbg(&cxlr->dev, "%s iw: %d ig: %d is not supported\n",
2065 dev_name(&cxled->cxld.dev), p->interleave_ways,
2066 p->interleave_granularity);
2067 return rc;
2068 }
2069
2070 if (cxled->part < 0) {
2071 dev_dbg(&cxlr->dev, "%s dead\n", dev_name(&cxled->cxld.dev));
2072 return -ENODEV;
2073 }
2074
2075 if (cxlds->part[cxled->part].mode != cxlr->mode) {
2076 dev_dbg(&cxlr->dev, "%s region mode: %d mismatch\n",
2077 dev_name(&cxled->cxld.dev), cxlr->mode);
2078 return -EINVAL;
2079 }
2080
2081 /* all full of members, or interleave config not established? */
2082 if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) {
2083 dev_dbg(&cxlr->dev, "region already active\n");
2084 return -EBUSY;
2085 }
2086
2087 if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) {
2088 dev_dbg(&cxlr->dev, "interleave config missing\n");
2089 return -ENXIO;
2090 }
2091
2092 if (p->nr_targets >= p->interleave_ways) {
2093 dev_dbg(&cxlr->dev, "region already has %d endpoints\n",
2094 p->nr_targets);
2095 return -EINVAL;
2096 }
2097
2098 ep_port = cxled_to_port(cxled);
2099 root_port = cxlrd_to_port(cxlrd);
2100 dport = cxl_find_dport_by_dev(root_port, ep_port->host_bridge);
2101 if (!dport) {
2102 dev_dbg(&cxlr->dev, "%s:%s invalid target for %s\n",
2103 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2104 dev_name(cxlr->dev.parent));
2105 return -ENXIO;
2106 }
2107
2108 if (cxled->cxld.target_type != cxlr->type) {
2109 dev_dbg(&cxlr->dev, "%s:%s type mismatch: %d vs %d\n",
2110 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2111 cxled->cxld.target_type, cxlr->type);
2112 return -ENXIO;
2113 }
2114
2115 if (!cxled->dpa_res) {
2116 dev_dbg(&cxlr->dev, "%s:%s: missing DPA allocation.\n",
2117 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev));
2118 return -ENXIO;
2119 }
2120
2121 if (resource_size(cxled->dpa_res) * p->interleave_ways + p->cache_size !=
2122 resource_size(p->res)) {
2123 dev_dbg(&cxlr->dev,
2124 "%s:%s-size-%#llx * ways-%d + cache-%#llx != region-size-%#llx\n",
2125 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2126 (u64)resource_size(cxled->dpa_res), p->interleave_ways,
2127 (u64)p->cache_size, (u64)resource_size(p->res));
2128 return -EINVAL;
2129 }
2130
2131 cxl_region_perf_data_calculate(cxlr, cxled);
2132
2133 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) {
2134 int i;
2135
2136 rc = cxl_region_attach_auto(cxlr, cxled, pos);
2137 if (rc)
2138 return rc;
2139
2140 /* await more targets to arrive... */
2141 if (p->nr_targets < p->interleave_ways)
2142 return 0;
2143
2144 /*
2145 * All targets are here, which implies all PCI enumeration that
2146 * affects this region has been completed. Walk the topology to
2147 * sort the devices into their relative region decode position.
2148 */
2149 rc = cxl_region_sort_targets(cxlr);
2150 if (rc)
2151 return rc;
2152
2153 for (i = 0; i < p->nr_targets; i++) {
2154 cxled = p->targets[i];
2155 ep_port = cxled_to_port(cxled);
2156 dport = cxl_find_dport_by_dev(root_port,
2157 ep_port->host_bridge);
2158 rc = cxl_region_attach_position(cxlr, cxled, dport, i);
2159 if (rc)
2160 return rc;
2161 }
2162
2163 rc = cxl_region_setup_targets(cxlr);
2164 if (rc)
2165 return rc;
2166
2167 /*
2168 * If target setup succeeds in the autodiscovery case
2169 * then the region is already committed.
2170 */
2171 p->state = CXL_CONFIG_COMMIT;
2172 cxl_region_shared_upstream_bandwidth_update(cxlr);
2173
2174 return 0;
2175 }
2176
2177 rc = cxl_region_validate_position(cxlr, cxled, pos);
2178 if (rc)
2179 return rc;
2180
2181 rc = cxl_region_attach_position(cxlr, cxled, dport, pos);
2182 if (rc)
2183 return rc;
2184
2185 p->targets[pos] = cxled;
2186 cxled->pos = pos;
2187 p->nr_targets++;
2188
2189 if (p->nr_targets == p->interleave_ways) {
2190 rc = cxl_region_setup_targets(cxlr);
2191 if (rc)
2192 return rc;
2193 p->state = CXL_CONFIG_ACTIVE;
2194 cxl_region_shared_upstream_bandwidth_update(cxlr);
2195 }
2196
2197 cxled->cxld.interleave_ways = p->interleave_ways;
2198 cxled->cxld.interleave_granularity = p->interleave_granularity;
2199 cxled->cxld.hpa_range = (struct range) {
2200 .start = p->res->start,
2201 .end = p->res->end,
2202 };
2203
2204 if (p->nr_targets != p->interleave_ways)
2205 return 0;
2206
2207 /*
2208 * Test the auto-discovery position calculator function
2209 * against this successfully created user-defined region.
2210 * A fail message here means that this interleave config
2211 * will fail when presented as CXL_REGION_F_AUTO.
2212 */
2213 for (int i = 0; i < p->nr_targets; i++) {
2214 struct cxl_endpoint_decoder *target = p->targets[i];
2215 int test_pos;
2216
2217 test_pos = cxl_calc_interleave_pos(target, &cxlr->hpa_range);
2218 dev_dbg(&target->cxld.dev,
2219 "Test cxl_calc_interleave_pos(): %s test_pos:%d target->pos:%d\n",
2220 (test_pos == target->pos) ? "success" : "fail",
2221 test_pos, target->pos);
2222 }
2223
2224 return 0;
2225 }
2226
cxl_region_remove_target(struct device * dev,void * data)2227 static int cxl_region_remove_target(struct device *dev, void *data)
2228 {
2229 struct cxl_endpoint_decoder *cxled = data;
2230 struct cxl_region_params *p;
2231 struct cxl_region *cxlr;
2232 int i;
2233
2234 if (!is_cxl_region(dev))
2235 return 0;
2236
2237 cxlr = to_cxl_region(dev);
2238 p = &cxlr->params;
2239 for (i = 0; i < p->interleave_ways; i++) {
2240 if (p->targets[i] == cxled) {
2241 p->nr_targets--;
2242 cxled->state = CXL_DECODER_STATE_AUTO;
2243 cxled->pos = -1;
2244 p->targets[i] = NULL;
2245
2246 return 1;
2247 }
2248 }
2249
2250 return 0;
2251 }
2252
2253 /*
2254 * When an auto-region fails to assemble the decoder may be listed as a target,
2255 * but not fully attached.
2256 */
cxl_cancel_auto_attach(struct cxl_endpoint_decoder * cxled)2257 static void cxl_cancel_auto_attach(struct cxl_endpoint_decoder *cxled)
2258 {
2259 if (cxled->state != CXL_DECODER_STATE_AUTO_STAGED)
2260 return;
2261
2262 bus_for_each_dev(&cxl_bus_type, NULL, cxled, cxl_region_remove_target);
2263 }
2264
2265 static struct cxl_region *
__cxl_decoder_detach(struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled,int pos,enum cxl_detach_mode mode)2266 __cxl_decoder_detach(struct cxl_region *cxlr,
2267 struct cxl_endpoint_decoder *cxled, int pos,
2268 enum cxl_detach_mode mode)
2269 {
2270 struct cxl_region_params *p;
2271
2272 lockdep_assert_held_write(&cxl_rwsem.region);
2273
2274 if (!cxled) {
2275 p = &cxlr->params;
2276
2277 if (pos >= p->interleave_ways) {
2278 dev_dbg(&cxlr->dev, "position %d out of range %d\n",
2279 pos, p->interleave_ways);
2280 return NULL;
2281 }
2282
2283 if (!p->targets[pos])
2284 return NULL;
2285 cxled = p->targets[pos];
2286 } else {
2287 cxlr = cxled->cxld.region;
2288 if (!cxlr) {
2289 cxl_cancel_auto_attach(cxled);
2290 return NULL;
2291 }
2292 p = &cxlr->params;
2293 }
2294
2295 if (mode == DETACH_INVALIDATE)
2296 cxled->part = -1;
2297
2298 if (p->state > CXL_CONFIG_ACTIVE) {
2299 cxl_region_decode_reset(cxlr, p->interleave_ways);
2300 p->state = CXL_CONFIG_ACTIVE;
2301 }
2302
2303 for (struct cxl_port *iter = cxled_to_port(cxled); !is_cxl_root(iter);
2304 iter = to_cxl_port(iter->dev.parent))
2305 cxl_port_detach_region(iter, cxlr, cxled);
2306
2307 if (cxled->pos < 0 || cxled->pos >= p->interleave_ways ||
2308 p->targets[cxled->pos] != cxled) {
2309 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
2310
2311 dev_WARN_ONCE(&cxlr->dev, 1, "expected %s:%s at position %d\n",
2312 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
2313 cxled->pos);
2314 return NULL;
2315 }
2316
2317 if (p->state == CXL_CONFIG_ACTIVE) {
2318 p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
2319 cxl_region_teardown_targets(cxlr);
2320 }
2321 p->targets[cxled->pos] = NULL;
2322 p->nr_targets--;
2323 cxled->cxld.hpa_range = (struct range) {
2324 .start = 0,
2325 .end = -1,
2326 };
2327
2328 get_device(&cxlr->dev);
2329 return cxlr;
2330 }
2331
2332 /*
2333 * Cleanup a decoder's interest in a region. There are 2 cases to
2334 * handle, removing an unknown @cxled from a known position in a region
2335 * (detach_target()) or removing a known @cxled from an unknown @cxlr
2336 * (cxld_unregister())
2337 *
2338 * When the detachment finds a region release the region driver.
2339 */
cxl_decoder_detach(struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled,int pos,enum cxl_detach_mode mode)2340 int cxl_decoder_detach(struct cxl_region *cxlr,
2341 struct cxl_endpoint_decoder *cxled, int pos,
2342 enum cxl_detach_mode mode)
2343 {
2344 struct cxl_region *detach;
2345
2346 /* when the decoder is being destroyed lock unconditionally */
2347 if (mode == DETACH_INVALIDATE) {
2348 guard(rwsem_write)(&cxl_rwsem.region);
2349 detach = __cxl_decoder_detach(cxlr, cxled, pos, mode);
2350 } else {
2351 int rc;
2352
2353 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
2354 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
2355 return rc;
2356 detach = __cxl_decoder_detach(cxlr, cxled, pos, mode);
2357 }
2358
2359 if (detach) {
2360 device_release_driver(&detach->dev);
2361 put_device(&detach->dev);
2362 }
2363 return 0;
2364 }
2365
__attach_target(struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled,int pos,unsigned int state)2366 static int __attach_target(struct cxl_region *cxlr,
2367 struct cxl_endpoint_decoder *cxled, int pos,
2368 unsigned int state)
2369 {
2370 int rc;
2371
2372 if (state == TASK_INTERRUPTIBLE) {
2373 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
2374 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem)))
2375 return rc;
2376 guard(rwsem_read)(&cxl_rwsem.dpa);
2377 return cxl_region_attach(cxlr, cxled, pos);
2378 }
2379 guard(rwsem_write)(&cxl_rwsem.region);
2380 guard(rwsem_read)(&cxl_rwsem.dpa);
2381 return cxl_region_attach(cxlr, cxled, pos);
2382 }
2383
attach_target(struct cxl_region * cxlr,struct cxl_endpoint_decoder * cxled,int pos,unsigned int state)2384 static int attach_target(struct cxl_region *cxlr,
2385 struct cxl_endpoint_decoder *cxled, int pos,
2386 unsigned int state)
2387 {
2388 int rc = __attach_target(cxlr, cxled, pos, state);
2389
2390 if (rc == 0)
2391 return 0;
2392
2393 dev_warn(cxled->cxld.dev.parent, "failed to attach %s to %s: %d\n",
2394 dev_name(&cxled->cxld.dev), dev_name(&cxlr->dev), rc);
2395 return rc;
2396 }
2397
detach_target(struct cxl_region * cxlr,int pos)2398 static int detach_target(struct cxl_region *cxlr, int pos)
2399 {
2400 return cxl_decoder_detach(cxlr, NULL, pos, DETACH_ONLY);
2401 }
2402
store_targetN(struct cxl_region * cxlr,const char * buf,int pos,size_t len)2403 static size_t store_targetN(struct cxl_region *cxlr, const char *buf, int pos,
2404 size_t len)
2405 {
2406 int rc;
2407
2408 if (sysfs_streq(buf, "\n"))
2409 rc = detach_target(cxlr, pos);
2410 else {
2411 struct device *dev;
2412
2413 dev = bus_find_device_by_name(&cxl_bus_type, NULL, buf);
2414 if (!dev)
2415 return -ENODEV;
2416
2417 if (!is_endpoint_decoder(dev)) {
2418 rc = -EINVAL;
2419 goto out;
2420 }
2421
2422 rc = attach_target(cxlr, to_cxl_endpoint_decoder(dev), pos,
2423 TASK_INTERRUPTIBLE);
2424 out:
2425 put_device(dev);
2426 }
2427
2428 if (rc < 0)
2429 return rc;
2430 return len;
2431 }
2432
2433 #define TARGET_ATTR_RW(n) \
2434 static ssize_t target##n##_show( \
2435 struct device *dev, struct device_attribute *attr, char *buf) \
2436 { \
2437 return show_targetN(to_cxl_region(dev), buf, (n)); \
2438 } \
2439 static ssize_t target##n##_store(struct device *dev, \
2440 struct device_attribute *attr, \
2441 const char *buf, size_t len) \
2442 { \
2443 return store_targetN(to_cxl_region(dev), buf, (n), len); \
2444 } \
2445 static DEVICE_ATTR_RW(target##n)
2446
2447 TARGET_ATTR_RW(0);
2448 TARGET_ATTR_RW(1);
2449 TARGET_ATTR_RW(2);
2450 TARGET_ATTR_RW(3);
2451 TARGET_ATTR_RW(4);
2452 TARGET_ATTR_RW(5);
2453 TARGET_ATTR_RW(6);
2454 TARGET_ATTR_RW(7);
2455 TARGET_ATTR_RW(8);
2456 TARGET_ATTR_RW(9);
2457 TARGET_ATTR_RW(10);
2458 TARGET_ATTR_RW(11);
2459 TARGET_ATTR_RW(12);
2460 TARGET_ATTR_RW(13);
2461 TARGET_ATTR_RW(14);
2462 TARGET_ATTR_RW(15);
2463
2464 static struct attribute *target_attrs[] = {
2465 &dev_attr_target0.attr,
2466 &dev_attr_target1.attr,
2467 &dev_attr_target2.attr,
2468 &dev_attr_target3.attr,
2469 &dev_attr_target4.attr,
2470 &dev_attr_target5.attr,
2471 &dev_attr_target6.attr,
2472 &dev_attr_target7.attr,
2473 &dev_attr_target8.attr,
2474 &dev_attr_target9.attr,
2475 &dev_attr_target10.attr,
2476 &dev_attr_target11.attr,
2477 &dev_attr_target12.attr,
2478 &dev_attr_target13.attr,
2479 &dev_attr_target14.attr,
2480 &dev_attr_target15.attr,
2481 NULL,
2482 };
2483
cxl_region_target_visible(struct kobject * kobj,struct attribute * a,int n)2484 static umode_t cxl_region_target_visible(struct kobject *kobj,
2485 struct attribute *a, int n)
2486 {
2487 struct device *dev = kobj_to_dev(kobj);
2488 struct cxl_region *cxlr = to_cxl_region(dev);
2489 struct cxl_region_params *p = &cxlr->params;
2490
2491 if (n < p->interleave_ways)
2492 return a->mode;
2493 return 0;
2494 }
2495
2496 static const struct attribute_group cxl_region_target_group = {
2497 .attrs = target_attrs,
2498 .is_visible = cxl_region_target_visible,
2499 };
2500
get_cxl_region_target_group(void)2501 static const struct attribute_group *get_cxl_region_target_group(void)
2502 {
2503 return &cxl_region_target_group;
2504 }
2505
2506 static const struct attribute_group *region_groups[] = {
2507 &cxl_base_attribute_group,
2508 &cxl_region_group,
2509 &cxl_region_target_group,
2510 &cxl_region_access0_coordinate_group,
2511 &cxl_region_access1_coordinate_group,
2512 NULL,
2513 };
2514
cxl_region_release(struct device * dev)2515 static void cxl_region_release(struct device *dev)
2516 {
2517 struct cxl_region *cxlr = to_cxl_region(dev);
2518 struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
2519 int id = atomic_read(&cxlrd->region_id);
2520
2521 /*
2522 * Try to reuse the recently idled id rather than the cached
2523 * next id to prevent the region id space from increasing
2524 * unnecessarily.
2525 */
2526 if (cxlr->id < id)
2527 if (atomic_try_cmpxchg(&cxlrd->region_id, &id, cxlr->id)) {
2528 memregion_free(id);
2529 goto out;
2530 }
2531
2532 memregion_free(cxlr->id);
2533 out:
2534 put_device(dev->parent);
2535 kfree(cxlr);
2536 }
2537
2538 const struct device_type cxl_region_type = {
2539 .name = "cxl_region",
2540 .release = cxl_region_release,
2541 .groups = region_groups
2542 };
2543
is_cxl_region(struct device * dev)2544 bool is_cxl_region(struct device *dev)
2545 {
2546 return dev->type == &cxl_region_type;
2547 }
2548 EXPORT_SYMBOL_NS_GPL(is_cxl_region, "CXL");
2549
to_cxl_region(struct device * dev)2550 static struct cxl_region *to_cxl_region(struct device *dev)
2551 {
2552 if (dev_WARN_ONCE(dev, dev->type != &cxl_region_type,
2553 "not a cxl_region device\n"))
2554 return NULL;
2555
2556 return container_of(dev, struct cxl_region, dev);
2557 }
2558
unregister_region(struct cxl_region * cxlr)2559 static void unregister_region(struct cxl_region *cxlr)
2560 {
2561 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
2562 struct cxl_region_params *p = &cxlr->params;
2563 int i;
2564
2565 xa_erase(&cxlrd->regions, cxlr->id);
2566 device_del(&cxlr->dev);
2567
2568 /*
2569 * Now that region sysfs is shutdown, the parameter block is now
2570 * read-only, so no need to hold the region rwsem to access the
2571 * region parameters.
2572 */
2573 for (i = 0; i < p->interleave_ways; i++)
2574 detach_target(cxlr, i);
2575
2576 cxlr->hpa_range = DEFINE_RANGE(0, -1);
2577
2578 cxl_region_iomem_release(cxlr);
2579 put_device(&cxlr->dev);
2580 }
2581
endpoint_unregister_region(void * _cxlr)2582 static void endpoint_unregister_region(void *_cxlr)
2583 {
2584 struct cxl_region *cxlr = _cxlr;
2585 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
2586
2587 guard(mutex)(&cxlrd->regions_lock);
2588 if (xa_load(&cxlrd->regions, cxlr->id))
2589 unregister_region(cxlr);
2590 put_device(&cxlr->dev);
2591 }
2592
2593 static struct lock_class_key cxl_region_key;
2594
cxl_region_alloc(struct cxl_root_decoder * cxlrd,int id)2595 static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id)
2596 {
2597 struct cxl_region *cxlr;
2598 struct device *dev;
2599
2600 cxlr = kzalloc_obj(*cxlr);
2601 if (!cxlr) {
2602 memregion_free(id);
2603 return ERR_PTR(-ENOMEM);
2604 }
2605
2606 dev = &cxlr->dev;
2607 device_initialize(dev);
2608 lockdep_set_class(&dev->mutex, &cxl_region_key);
2609 dev->parent = &cxlrd->cxlsd.cxld.dev;
2610 /*
2611 * Keep root decoder pinned through cxl_region_release to fixup
2612 * region id allocations
2613 */
2614 get_device(dev->parent);
2615 cxlr->cxlrd = cxlrd;
2616 cxlr->id = id;
2617
2618 device_set_pm_not_required(dev);
2619 dev->bus = &cxl_bus_type;
2620 dev->type = &cxl_region_type;
2621 cxl_region_setup_flags(cxlr, &cxlrd->cxlsd.cxld);
2622
2623 return cxlr;
2624 }
2625
cxl_region_update_coordinates(struct cxl_region * cxlr,int nid)2626 static bool cxl_region_update_coordinates(struct cxl_region *cxlr, int nid)
2627 {
2628 int cset = 0;
2629 int rc;
2630
2631 for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
2632 if (cxlr->coord[i].read_bandwidth) {
2633 node_update_perf_attrs(nid, &cxlr->coord[i], i);
2634 cset++;
2635 }
2636 }
2637
2638 if (!cset)
2639 return false;
2640
2641 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_access0_group());
2642 if (rc)
2643 dev_dbg(&cxlr->dev, "Failed to update access0 group\n");
2644
2645 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_access1_group());
2646 if (rc)
2647 dev_dbg(&cxlr->dev, "Failed to update access1 group\n");
2648
2649 return true;
2650 }
2651
cxl_region_perf_attrs_callback(struct notifier_block * nb,unsigned long action,void * arg)2652 static int cxl_region_perf_attrs_callback(struct notifier_block *nb,
2653 unsigned long action, void *arg)
2654 {
2655 struct cxl_region *cxlr = container_of(nb, struct cxl_region,
2656 node_notifier);
2657 struct node_notify *nn = arg;
2658 int nid = nn->nid;
2659 int region_nid;
2660
2661 if (action != NODE_ADDED_FIRST_MEMORY)
2662 return NOTIFY_DONE;
2663
2664 /*
2665 * No need to hold cxl_rwsem.region; region parameters are stable
2666 * within the cxl_region driver.
2667 */
2668 region_nid = phys_to_target_node(cxlr->params.res->start);
2669 if (nid != region_nid)
2670 return NOTIFY_DONE;
2671
2672 /* No action needed if node bit already set */
2673 if (node_test_and_set(nid, nodemask_region_seen))
2674 return NOTIFY_DONE;
2675
2676 if (!cxl_region_update_coordinates(cxlr, nid))
2677 return NOTIFY_DONE;
2678
2679 return NOTIFY_OK;
2680 }
2681
cxl_region_calculate_adistance(struct notifier_block * nb,unsigned long nid,void * data)2682 static int cxl_region_calculate_adistance(struct notifier_block *nb,
2683 unsigned long nid, void *data)
2684 {
2685 struct cxl_region *cxlr = container_of(nb, struct cxl_region,
2686 adist_notifier);
2687 struct access_coordinate *perf;
2688 int *adist = data;
2689 int region_nid;
2690
2691 /*
2692 * No need to hold cxl_rwsem.region; region parameters are stable
2693 * within the cxl_region driver.
2694 */
2695 region_nid = phys_to_target_node(cxlr->params.res->start);
2696 if (nid != region_nid)
2697 return NOTIFY_OK;
2698
2699 perf = &cxlr->coord[ACCESS_COORDINATE_CPU];
2700
2701 if (mt_perf_to_adistance(perf, adist))
2702 return NOTIFY_OK;
2703
2704 return NOTIFY_STOP;
2705 }
2706
2707 /* unwind all remaining regions */
kill_regions(struct cxl_root_decoder * cxlrd)2708 void kill_regions(struct cxl_root_decoder *cxlrd)
2709 {
2710 unsigned long index;
2711 struct cxl_region *cxlr;
2712
2713 guard(mutex)(&cxlrd->regions_lock);
2714 /* no more region creation */
2715 cxlrd->dead = true;
2716 xa_for_each(&cxlrd->regions, index, cxlr)
2717 unregister_region(cxlr);
2718 }
2719
2720 /**
2721 * devm_cxl_add_region - Adds a region to a decoder
2722 * @cxlrd: root decoder
2723 * @id: memregion id to create, or memregion_free() on failure
2724 * @mode: mode for the endpoint decoders of this region
2725 * @type: select whether this is an expander or accelerator (type-2 or type-3)
2726 *
2727 * This is the second step of region initialization. Regions exist within an
2728 * address space which is mapped by a @cxlrd.
2729 *
2730 * Return: 0 if the region was added to the @cxlrd, else returns negative error
2731 * code. The region will be named "regionZ" where Z is the unique region number.
2732 */
devm_cxl_add_region(struct cxl_root_decoder * cxlrd,int id,enum cxl_partition_mode mode,enum cxl_decoder_type type)2733 static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd,
2734 int id,
2735 enum cxl_partition_mode mode,
2736 enum cxl_decoder_type type)
2737 {
2738 struct cxl_port *port = to_cxl_port(cxlrd->cxlsd.cxld.dev.parent);
2739 struct cxl_region *cxlr;
2740 struct device *dev;
2741 int rc;
2742
2743 cxlr = cxl_region_alloc(cxlrd, id);
2744 if (IS_ERR(cxlr))
2745 return cxlr;
2746 cxlr->mode = mode;
2747 cxlr->type = type;
2748
2749 dev = &cxlr->dev;
2750 rc = dev_set_name(dev, "region%d", id);
2751 if (rc)
2752 goto err;
2753
2754 rc = device_add(dev);
2755 if (rc)
2756 goto err;
2757
2758 rc = xa_insert(&cxlrd->regions, cxlr->id, cxlr, GFP_KERNEL);
2759 if (rc) {
2760 unregister_region(cxlr);
2761 return ERR_PTR(rc);
2762 }
2763
2764 dev_dbg(port->uport_dev, "%s: created %s\n",
2765 dev_name(&cxlrd->cxlsd.cxld.dev), dev_name(dev));
2766 return cxlr;
2767 err:
2768 put_device(dev);
2769 return ERR_PTR(rc);
2770 }
2771
__create_region_show(struct cxl_root_decoder * cxlrd,char * buf)2772 static ssize_t __create_region_show(struct cxl_root_decoder *cxlrd, char *buf)
2773 {
2774 return sysfs_emit(buf, "region%u\n", atomic_read(&cxlrd->region_id));
2775 }
2776
create_pmem_region_show(struct device * dev,struct device_attribute * attr,char * buf)2777 static ssize_t create_pmem_region_show(struct device *dev,
2778 struct device_attribute *attr, char *buf)
2779 {
2780 return __create_region_show(to_cxl_root_decoder(dev), buf);
2781 }
2782
create_ram_region_show(struct device * dev,struct device_attribute * attr,char * buf)2783 static ssize_t create_ram_region_show(struct device *dev,
2784 struct device_attribute *attr, char *buf)
2785 {
2786 return __create_region_show(to_cxl_root_decoder(dev), buf);
2787 }
2788
__create_region(struct cxl_root_decoder * cxlrd,enum cxl_partition_mode mode,int id,enum cxl_decoder_type target_type)2789 static struct cxl_region *__create_region(struct cxl_root_decoder *cxlrd,
2790 enum cxl_partition_mode mode, int id,
2791 enum cxl_decoder_type target_type)
2792 {
2793 int rc;
2794
2795 if (cxlrd->dead)
2796 return ERR_PTR(-ENXIO);
2797
2798 switch (mode) {
2799 case CXL_PARTMODE_RAM:
2800 case CXL_PARTMODE_PMEM:
2801 break;
2802 default:
2803 dev_err(&cxlrd->cxlsd.cxld.dev, "unsupported mode %d\n", mode);
2804 return ERR_PTR(-EINVAL);
2805 }
2806
2807 rc = memregion_alloc(GFP_KERNEL);
2808 if (rc < 0)
2809 return ERR_PTR(rc);
2810
2811 if (atomic_cmpxchg(&cxlrd->region_id, id, rc) != id) {
2812 memregion_free(rc);
2813 return ERR_PTR(-EBUSY);
2814 }
2815
2816 return devm_cxl_add_region(cxlrd, id, mode, target_type);
2817 }
2818
create_region_store(struct device * dev,const char * buf,size_t len,enum cxl_partition_mode mode)2819 static ssize_t create_region_store(struct device *dev, const char *buf,
2820 size_t len, enum cxl_partition_mode mode)
2821 {
2822 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2823 struct cxl_region *cxlr;
2824 int rc, id;
2825
2826 rc = sscanf(buf, "region%d\n", &id);
2827 if (rc != 1)
2828 return -EINVAL;
2829
2830 ACQUIRE(mutex_intr, regions_lock)(&cxlrd->regions_lock);
2831 if ((rc = ACQUIRE_ERR(mutex_intr, ®ions_lock)))
2832 return rc;
2833
2834 cxlr = __create_region(cxlrd, mode, id, CXL_DECODER_HOSTONLYMEM);
2835 if (IS_ERR(cxlr))
2836 return PTR_ERR(cxlr);
2837
2838 return len;
2839 }
2840
create_pmem_region_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)2841 static ssize_t create_pmem_region_store(struct device *dev,
2842 struct device_attribute *attr,
2843 const char *buf, size_t len)
2844 {
2845 return create_region_store(dev, buf, len, CXL_PARTMODE_PMEM);
2846 }
2847 DEVICE_ATTR_RW(create_pmem_region);
2848
create_ram_region_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)2849 static ssize_t create_ram_region_store(struct device *dev,
2850 struct device_attribute *attr,
2851 const char *buf, size_t len)
2852 {
2853 return create_region_store(dev, buf, len, CXL_PARTMODE_RAM);
2854 }
2855 DEVICE_ATTR_RW(create_ram_region);
2856
region_show(struct device * dev,struct device_attribute * attr,char * buf)2857 static ssize_t region_show(struct device *dev, struct device_attribute *attr,
2858 char *buf)
2859 {
2860 struct cxl_decoder *cxld = to_cxl_decoder(dev);
2861 ssize_t rc;
2862
2863 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
2864 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
2865 return rc;
2866
2867 if (cxld->region)
2868 return sysfs_emit(buf, "%s\n", dev_name(&cxld->region->dev));
2869 return sysfs_emit(buf, "\n");
2870 }
2871 DEVICE_ATTR_RO(region);
2872
delete_region_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)2873 static ssize_t delete_region_store(struct device *dev,
2874 struct device_attribute *attr,
2875 const char *buf, size_t len)
2876 {
2877 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
2878 struct cxl_region *cxlr;
2879 int rc, id;
2880
2881 ACQUIRE(mutex_intr, regions_lock)(&cxlrd->regions_lock);
2882 if ((rc = ACQUIRE_ERR(mutex_intr, ®ions_lock)))
2883 return rc;
2884
2885 rc = sscanf(buf, "region%d\n", &id);
2886 if (rc != 1)
2887 return -EINVAL;
2888
2889 cxlr = xa_load(&cxlrd->regions, id);
2890 if (!cxlr || !sysfs_streq(buf, dev_name(&cxlr->dev)))
2891 return -ENODEV;
2892
2893 unregister_region(cxlr);
2894
2895 return len;
2896 }
2897 DEVICE_ATTR_WO(delete_region);
2898
2899 struct cxl_poison_context {
2900 struct cxl_port *port;
2901 int part;
2902 u64 offset;
2903 };
2904
cxl_get_poison_unmapped(struct cxl_memdev * cxlmd,struct cxl_poison_context * ctx)2905 static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
2906 struct cxl_poison_context *ctx)
2907 {
2908 struct cxl_dev_state *cxlds = cxlmd->cxlds;
2909 const struct resource *res;
2910 struct resource *p, *last;
2911 u64 offset, length;
2912 int rc = 0;
2913
2914 if (ctx->part < 0)
2915 return 0;
2916
2917 /*
2918 * Collect poison for the remaining unmapped resources after
2919 * poison is collected by committed endpoints decoders.
2920 */
2921 for (int i = ctx->part; i < cxlds->nr_partitions; i++) {
2922 res = &cxlds->part[i].res;
2923 for (p = res->child, last = NULL; p; p = p->sibling)
2924 last = p;
2925 if (last)
2926 offset = last->end + 1;
2927 else
2928 offset = res->start;
2929 length = res->end - offset + 1;
2930 if (!length)
2931 break;
2932 rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
2933 if (rc == -EFAULT && cxlds->part[i].mode == CXL_PARTMODE_RAM)
2934 continue;
2935 if (rc)
2936 break;
2937 }
2938
2939 return rc;
2940 }
2941
poison_by_decoder(struct device * dev,void * arg)2942 static int poison_by_decoder(struct device *dev, void *arg)
2943 {
2944 struct cxl_poison_context *ctx = arg;
2945 struct cxl_endpoint_decoder *cxled;
2946 enum cxl_partition_mode mode;
2947 struct cxl_dev_state *cxlds;
2948 struct cxl_memdev *cxlmd;
2949 u64 offset, length;
2950 int rc = 0;
2951
2952 if (!is_endpoint_decoder(dev))
2953 return rc;
2954
2955 cxled = to_cxl_endpoint_decoder(dev);
2956 if (!cxled->dpa_res)
2957 return rc;
2958
2959 cxlmd = cxled_to_memdev(cxled);
2960 cxlds = cxlmd->cxlds;
2961 mode = cxlds->part[cxled->part].mode;
2962
2963 if (cxled->skip) {
2964 offset = cxled->dpa_res->start - cxled->skip;
2965 length = cxled->skip;
2966 rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
2967 if (rc == -EFAULT && mode == CXL_PARTMODE_RAM)
2968 rc = 0;
2969 if (rc)
2970 return rc;
2971 }
2972
2973 offset = cxled->dpa_res->start;
2974 length = cxled->dpa_res->end - offset + 1;
2975 rc = cxl_mem_get_poison(cxlmd, offset, length, cxled->cxld.region);
2976 if (rc == -EFAULT && mode == CXL_PARTMODE_RAM)
2977 rc = 0;
2978 if (rc)
2979 return rc;
2980
2981 /* Iterate until commit_end is reached */
2982 if (cxled->cxld.id == ctx->port->commit_end) {
2983 ctx->offset = cxled->dpa_res->end + 1;
2984 ctx->part = cxled->part;
2985 return 1;
2986 }
2987
2988 return 0;
2989 }
2990
cxl_get_poison_by_endpoint(struct cxl_port * port)2991 int cxl_get_poison_by_endpoint(struct cxl_port *port)
2992 {
2993 struct cxl_poison_context ctx;
2994 int rc = 0;
2995
2996 ctx = (struct cxl_poison_context) {
2997 .port = port,
2998 .part = -1,
2999 };
3000
3001 rc = device_for_each_child(&port->dev, &ctx, poison_by_decoder);
3002 if (rc == 1)
3003 rc = cxl_get_poison_unmapped(to_cxl_memdev(port->uport_dev),
3004 &ctx);
3005
3006 return rc;
3007 }
3008
3009 struct cxl_dpa_to_region_context {
3010 struct cxl_region *cxlr;
3011 u64 dpa;
3012 };
3013
__cxl_dpa_to_region(struct device * dev,void * arg)3014 static int __cxl_dpa_to_region(struct device *dev, void *arg)
3015 {
3016 struct cxl_dpa_to_region_context *ctx = arg;
3017 struct cxl_endpoint_decoder *cxled;
3018 struct cxl_region *cxlr;
3019 u64 dpa = ctx->dpa;
3020
3021 if (!is_endpoint_decoder(dev))
3022 return 0;
3023
3024 cxled = to_cxl_endpoint_decoder(dev);
3025 if (!cxled || !cxled->dpa_res || !resource_size(cxled->dpa_res))
3026 return 0;
3027
3028 if (!cxl_resource_contains_addr(cxled->dpa_res, dpa))
3029 return 0;
3030
3031 /*
3032 * Stop the region search (return 1) when an endpoint mapping is
3033 * found. The region may not be fully constructed so offering
3034 * the cxlr in the context structure is not guaranteed.
3035 */
3036 cxlr = cxled->cxld.region;
3037 if (cxlr)
3038 dev_dbg(dev, "dpa:0x%llx mapped in region:%s\n", dpa,
3039 dev_name(&cxlr->dev));
3040 else
3041 dev_dbg(dev, "dpa:0x%llx mapped in endpoint:%s\n", dpa,
3042 dev_name(dev));
3043
3044 ctx->cxlr = cxlr;
3045
3046 return 1;
3047 }
3048
cxl_dpa_to_region(const struct cxl_memdev * cxlmd,u64 dpa)3049 struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa)
3050 {
3051 struct cxl_dpa_to_region_context ctx;
3052 struct cxl_port *port = cxlmd->endpoint;
3053
3054 if (!cxlmd->dev.driver)
3055 return NULL;
3056
3057 ctx = (struct cxl_dpa_to_region_context) {
3058 .dpa = dpa,
3059 };
3060 if (cxl_num_decoders_committed(port))
3061 device_for_each_child(&port->dev, &ctx, __cxl_dpa_to_region);
3062
3063 return ctx.cxlr;
3064 }
3065
cxl_is_hpa_in_chunk(u64 hpa,struct cxl_region * cxlr,int pos)3066 static bool cxl_is_hpa_in_chunk(u64 hpa, struct cxl_region *cxlr, int pos)
3067 {
3068 struct cxl_region_params *p = &cxlr->params;
3069 int gran = p->interleave_granularity;
3070 int ways = p->interleave_ways;
3071 u64 offset;
3072
3073 /* Is the hpa in an expected chunk for its pos(-ition) */
3074 offset = hpa - p->res->start;
3075 offset = do_div(offset, gran * ways);
3076 if ((offset >= pos * gran) && (offset < (pos + 1) * gran))
3077 return true;
3078
3079 dev_dbg(&cxlr->dev,
3080 "Addr trans fail: hpa 0x%llx not in expected chunk\n", hpa);
3081
3082 return false;
3083 }
3084
3085 #define CXL_POS_ZERO 0
3086 /**
3087 * cxl_validate_translation_params
3088 * @eiw: encoded interleave ways
3089 * @eig: encoded interleave granularity
3090 * @pos: position in interleave
3091 *
3092 * Callers pass CXL_POS_ZERO when no position parameter needs validating.
3093 *
3094 * Returns: 0 on success, -EINVAL on first invalid parameter
3095 */
cxl_validate_translation_params(u8 eiw,u16 eig,int pos)3096 int cxl_validate_translation_params(u8 eiw, u16 eig, int pos)
3097 {
3098 int ways, gran;
3099
3100 if (eiw_to_ways(eiw, &ways)) {
3101 pr_debug("%s: invalid eiw=%u\n", __func__, eiw);
3102 return -EINVAL;
3103 }
3104 if (eig_to_granularity(eig, &gran)) {
3105 pr_debug("%s: invalid eig=%u\n", __func__, eig);
3106 return -EINVAL;
3107 }
3108 if (pos < 0 || pos >= ways) {
3109 pr_debug("%s: invalid pos=%d for ways=%d\n", __func__, pos,
3110 ways);
3111 return -EINVAL;
3112 }
3113
3114 return 0;
3115 }
3116 EXPORT_SYMBOL_FOR_MODULES(cxl_validate_translation_params, "cxl_translate");
3117
cxl_calculate_dpa_offset(u64 hpa_offset,u8 eiw,u16 eig)3118 u64 cxl_calculate_dpa_offset(u64 hpa_offset, u8 eiw, u16 eig)
3119 {
3120 u64 dpa_offset, bits_lower, bits_upper, temp;
3121 int ret;
3122
3123 ret = cxl_validate_translation_params(eiw, eig, CXL_POS_ZERO);
3124 if (ret)
3125 return ULLONG_MAX;
3126
3127 /*
3128 * DPA offset: CXL Spec 3.2 Section 8.2.4.20.13
3129 * Lower bits [IG+7:0] pass through unchanged
3130 * (eiw < 8)
3131 * Per spec: DPAOffset[51:IG+8] = (HPAOffset[51:IG+IW+8] >> IW)
3132 * Clear the position bits to isolate upper section, then
3133 * reverse the left shift by eiw that occurred during DPA->HPA
3134 * (eiw >= 8)
3135 * Per spec: DPAOffset[51:IG+8] = HPAOffset[51:IG+IW] / 3
3136 * Extract upper bits from the correct bit range and divide by 3
3137 * to recover the original DPA upper bits
3138 */
3139 bits_lower = hpa_offset & GENMASK_ULL(eig + 7, 0);
3140 if (eiw < 8) {
3141 temp = hpa_offset &= ~GENMASK_ULL(eig + eiw + 8 - 1, 0);
3142 dpa_offset = temp >> eiw;
3143 } else {
3144 bits_upper = div64_u64(hpa_offset >> (eig + eiw), 3);
3145 dpa_offset = bits_upper << (eig + 8);
3146 }
3147 dpa_offset |= bits_lower;
3148
3149 return dpa_offset;
3150 }
3151 EXPORT_SYMBOL_FOR_MODULES(cxl_calculate_dpa_offset, "cxl_translate");
3152
cxl_calculate_position(u64 hpa_offset,u8 eiw,u16 eig)3153 int cxl_calculate_position(u64 hpa_offset, u8 eiw, u16 eig)
3154 {
3155 int ways = 0;
3156 u64 shifted, rem;
3157 int pos, ret;
3158
3159 ret = cxl_validate_translation_params(eiw, eig, CXL_POS_ZERO);
3160 if (ret)
3161 return ret;
3162
3163 if (!eiw)
3164 /* position is 0 if no interleaving */
3165 return 0;
3166
3167 /*
3168 * Interleave position: CXL Spec 3.2 Section 8.2.4.20.13
3169 * eiw < 8
3170 * Position is in the IW bits at HPA_OFFSET[IG+8+IW-1:IG+8].
3171 * Per spec "remove IW bits starting with bit position IG+8"
3172 * eiw >= 8
3173 * Position is not explicitly stored in HPA_OFFSET bits. It is
3174 * derived from the modulo operation of the upper bits using
3175 * the total number of interleave ways.
3176 */
3177 if (eiw < 8) {
3178 pos = (hpa_offset >> (eig + 8)) & GENMASK(eiw - 1, 0);
3179 } else {
3180 shifted = hpa_offset >> (eig + 8);
3181 eiw_to_ways(eiw, &ways);
3182 div64_u64_rem(shifted, ways, &rem);
3183 pos = rem;
3184 }
3185
3186 return pos;
3187 }
3188 EXPORT_SYMBOL_FOR_MODULES(cxl_calculate_position, "cxl_translate");
3189
cxl_calculate_hpa_offset(u64 dpa_offset,int pos,u8 eiw,u16 eig)3190 u64 cxl_calculate_hpa_offset(u64 dpa_offset, int pos, u8 eiw, u16 eig)
3191 {
3192 u64 mask_upper, hpa_offset, bits_upper;
3193 int ret;
3194
3195 ret = cxl_validate_translation_params(eiw, eig, pos);
3196 if (ret)
3197 return ULLONG_MAX;
3198
3199 /*
3200 * The device position in the region interleave set was removed
3201 * from the offset at HPA->DPA translation. To reconstruct the
3202 * HPA, place the 'pos' in the offset.
3203 *
3204 * The placement of 'pos' in the HPA is determined by interleave
3205 * ways and granularity and is defined in the CXL Spec 3.0 Section
3206 * 8.2.4.19.13 Implementation Note: Device Decode Logic
3207 */
3208
3209 mask_upper = GENMASK_ULL(51, eig + 8);
3210
3211 if (eiw < 8) {
3212 hpa_offset = (dpa_offset & mask_upper) << eiw;
3213 hpa_offset |= pos << (eig + 8);
3214 } else {
3215 bits_upper = (dpa_offset & mask_upper) >> (eig + 8);
3216 bits_upper = bits_upper * 3;
3217 hpa_offset = ((bits_upper << (eiw - 8)) + pos) << (eig + 8);
3218 }
3219
3220 /* The lower bits remain unchanged */
3221 hpa_offset |= dpa_offset & GENMASK_ULL(eig + 7, 0);
3222
3223 return hpa_offset;
3224 }
3225 EXPORT_SYMBOL_FOR_MODULES(cxl_calculate_hpa_offset, "cxl_translate");
3226
decode_pos(int region_ways,int hb_ways,int pos,int * pos_port,int * pos_hb)3227 static int decode_pos(int region_ways, int hb_ways, int pos, int *pos_port,
3228 int *pos_hb)
3229 {
3230 int devices_per_hb;
3231
3232 /*
3233 * Decode for 3-6-12 way interleaves as defined in the CXL
3234 * Spec 4.0 9.13.1.1 Legal Interleaving Configurations.
3235 * Region creation should prevent invalid combinations but
3236 * sanity check here to avoid a silent bad decode.
3237 */
3238 switch (hb_ways) {
3239 case 3:
3240 if (region_ways != 3 && region_ways != 6 && region_ways != 12)
3241 return -EINVAL;
3242 break;
3243 case 6:
3244 if (region_ways != 6 && region_ways != 12)
3245 return -EINVAL;
3246 break;
3247 case 12:
3248 if (region_ways != 12)
3249 return -EINVAL;
3250 break;
3251 default:
3252 return -EINVAL;
3253 }
3254 /*
3255 * Each host bridge contributes an equal number of endpoints
3256 * that are laid out contiguously per host bridge. Modulo
3257 * selects the port within a host bridge and division selects
3258 * the host bridge position.
3259 */
3260 devices_per_hb = region_ways / hb_ways;
3261 *pos_port = pos % devices_per_hb;
3262 *pos_hb = pos / devices_per_hb;
3263
3264 return 0;
3265 }
3266
3267 /*
3268 * restore_parent() reconstruct the address in parent
3269 *
3270 * This math, specifically the bitmask creation 'mask = gran - 1' relies
3271 * on the CXL Spec requirement that interleave granularity is always a
3272 * power of two.
3273 *
3274 * [mask] isolate the offset with the granularity
3275 * [addr & ~mask] remove the offset leaving the aligned portion
3276 * [* ways] distribute across all interleave ways
3277 * [+ (pos * gran)] add the positional offset
3278 * [+ (addr & mask)] restore the masked offset
3279 */
restore_parent(u64 addr,u64 pos,u64 gran,u64 ways)3280 static u64 restore_parent(u64 addr, u64 pos, u64 gran, u64 ways)
3281 {
3282 u64 mask = gran - 1;
3283
3284 return ((addr & ~mask) * ways) + (pos * gran) + (addr & mask);
3285 }
3286
3287 /*
3288 * unaligned_dpa_to_hpa() translates a DPA to HPA when the region resource
3289 * start address is not aligned at Host Bridge Interleave Ways * 256MB.
3290 *
3291 * Unaligned start addresses only occur with MOD3 interleaves. All power-
3292 * of-two interleaves are guaranteed aligned.
3293 */
unaligned_dpa_to_hpa(struct cxl_decoder * cxld,struct cxl_region_params * p,int pos,u64 dpa)3294 static u64 unaligned_dpa_to_hpa(struct cxl_decoder *cxld,
3295 struct cxl_region_params *p, int pos, u64 dpa)
3296 {
3297 int ways_port = p->interleave_ways / cxld->interleave_ways;
3298 int gran_port = p->interleave_granularity;
3299 int gran_hb = cxld->interleave_granularity;
3300 int ways_hb = cxld->interleave_ways;
3301 int pos_port, pos_hb, gran_shift;
3302 u64 hpa_port = 0;
3303
3304 /* Decode an endpoint 'pos' into port and host-bridge components */
3305 if (decode_pos(p->interleave_ways, ways_hb, pos, &pos_port, &pos_hb)) {
3306 dev_dbg(&cxld->dev, "not supported for region ways:%d\n",
3307 p->interleave_ways);
3308 return ULLONG_MAX;
3309 }
3310
3311 /* Restore the port parent address if needed */
3312 if (gran_hb != gran_port)
3313 hpa_port = restore_parent(dpa, pos_port, gran_port, ways_port);
3314 else
3315 hpa_port = dpa;
3316
3317 /*
3318 * Complete the HPA reconstruction by restoring the address as if
3319 * each HB position is a candidate. Test against expected pos_hb
3320 * to confirm match.
3321 */
3322 gran_shift = ilog2(gran_hb);
3323 for (int position = 0; position < ways_hb; position++) {
3324 u64 shifted, hpa;
3325
3326 hpa = restore_parent(hpa_port, position, gran_hb, ways_hb);
3327 hpa += p->res->start;
3328
3329 shifted = hpa >> gran_shift;
3330 if (do_div(shifted, ways_hb) == pos_hb)
3331 return hpa;
3332 }
3333
3334 dev_dbg(&cxld->dev, "fail dpa:%#llx region:%pr pos:%d\n", dpa, p->res,
3335 pos);
3336 dev_dbg(&cxld->dev, " port-w/g/p:%d/%d/%d hb-w/g/p:%d/%d/%d\n",
3337 ways_port, gran_port, pos_port, ways_hb, gran_hb, pos_hb);
3338
3339 return ULLONG_MAX;
3340 }
3341
region_is_unaligned_mod3(struct cxl_region * cxlr)3342 static bool region_is_unaligned_mod3(struct cxl_region *cxlr)
3343 {
3344 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
3345 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
3346 struct cxl_region_params *p = &cxlr->params;
3347 int hbiw = cxld->interleave_ways;
3348 u64 rem;
3349
3350 if (is_power_of_2(hbiw))
3351 return false;
3352
3353 div64_u64_rem(p->res->start, (u64)hbiw * SZ_256M, &rem);
3354
3355 return (rem != 0);
3356 }
3357
cxl_dpa_to_hpa(struct cxl_region * cxlr,const struct cxl_memdev * cxlmd,u64 dpa)3358 u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
3359 u64 dpa)
3360 {
3361 struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
3362 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
3363 struct cxl_region_params *p = &cxlr->params;
3364 struct cxl_endpoint_decoder *cxled = NULL;
3365 u64 base, dpa_offset, hpa_offset, hpa;
3366 bool unaligned = false;
3367 u16 eig = 0;
3368 u8 eiw = 0;
3369 int pos;
3370
3371 /*
3372 * Conversion between SPA and DPA is not supported in
3373 * Normalized Address mode.
3374 */
3375 if (test_bit(CXL_REGION_F_NORMALIZED_ADDRESSING, &cxlr->flags))
3376 return ULLONG_MAX;
3377
3378 for (int i = 0; i < p->nr_targets; i++) {
3379 if (cxlmd == cxled_to_memdev(p->targets[i])) {
3380 cxled = p->targets[i];
3381 break;
3382 }
3383 }
3384 if (!cxled)
3385 return ULLONG_MAX;
3386
3387 base = cxl_dpa_resource_start(cxled);
3388 if (base == RESOURCE_SIZE_MAX)
3389 return ULLONG_MAX;
3390
3391 dpa_offset = dpa - base;
3392
3393 /* Unaligned calc for MOD3 interleaves not hbiw * 256MB aligned */
3394 unaligned = region_is_unaligned_mod3(cxlr);
3395 if (unaligned) {
3396 hpa = unaligned_dpa_to_hpa(cxld, p, cxled->pos, dpa_offset);
3397 if (hpa == ULLONG_MAX)
3398 return ULLONG_MAX;
3399
3400 goto skip_aligned;
3401 }
3402 /*
3403 * Aligned calc for all power-of-2 interleaves and for MOD3
3404 * interleaves that are aligned at hbiw * 256MB
3405 */
3406 pos = cxled->pos;
3407 ways_to_eiw(p->interleave_ways, &eiw);
3408 granularity_to_eig(p->interleave_granularity, &eig);
3409
3410 hpa_offset = cxl_calculate_hpa_offset(dpa_offset, pos, eiw, eig);
3411 if (hpa_offset == ULLONG_MAX)
3412 return ULLONG_MAX;
3413
3414 /* Apply the hpa_offset to the region base address */
3415 hpa = hpa_offset + p->res->start;
3416
3417 skip_aligned:
3418 hpa += p->cache_size;
3419
3420 /* Root decoder translation overrides typical modulo decode */
3421 if (cxlrd->ops.hpa_to_spa)
3422 hpa = cxlrd->ops.hpa_to_spa(cxlrd, hpa);
3423
3424 if (hpa == ULLONG_MAX)
3425 return ULLONG_MAX;
3426
3427 if (!cxl_resource_contains_addr(p->res, hpa)) {
3428 dev_dbg(&cxlr->dev,
3429 "Addr trans fail: hpa 0x%llx not in region\n", hpa);
3430 return ULLONG_MAX;
3431 }
3432 /* Chunk check applies to aligned modulo decodes only */
3433 if (!unaligned && !cxlrd->ops.hpa_to_spa &&
3434 !cxl_is_hpa_in_chunk(hpa, cxlr, pos))
3435 return ULLONG_MAX;
3436
3437 return hpa;
3438 }
3439
3440 struct dpa_result {
3441 struct cxl_memdev *cxlmd;
3442 u64 dpa;
3443 };
3444
unaligned_region_offset_to_dpa_result(struct cxl_region * cxlr,u64 offset,struct dpa_result * result)3445 static int unaligned_region_offset_to_dpa_result(struct cxl_region *cxlr,
3446 u64 offset,
3447 struct dpa_result *result)
3448 {
3449 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent);
3450 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
3451 struct cxl_region_params *p = &cxlr->params;
3452 u64 interleave_width, interleave_index;
3453 u64 gran, gran_offset, dpa_offset;
3454 u64 hpa = p->res->start + offset;
3455 u64 tmp = offset;
3456
3457 /*
3458 * Unaligned addresses are not algebraically invertible. Calculate
3459 * a dpa_offset independent of the target device and then enumerate
3460 * and test that dpa_offset against each candidate endpoint decoder.
3461 */
3462 gran = cxld->interleave_granularity;
3463 interleave_width = gran * cxld->interleave_ways;
3464 interleave_index = div64_u64(offset, interleave_width);
3465 gran_offset = do_div(tmp, gran);
3466
3467 dpa_offset = interleave_index * gran + gran_offset;
3468
3469 for (int i = 0; i < p->nr_targets; i++) {
3470 struct cxl_endpoint_decoder *cxled = p->targets[i];
3471 int pos = cxled->pos;
3472 u64 test_hpa;
3473
3474 test_hpa = unaligned_dpa_to_hpa(cxld, p, pos, dpa_offset);
3475 if (test_hpa == hpa) {
3476 result->cxlmd = cxled_to_memdev(cxled);
3477 result->dpa =
3478 cxl_dpa_resource_start(cxled) + dpa_offset;
3479 return 0;
3480 }
3481 }
3482 dev_err(&cxlr->dev,
3483 "failed to resolve HPA %#llx in unaligned MOD3 region\n", hpa);
3484
3485 return -ENXIO;
3486 }
3487
region_offset_to_dpa_result(struct cxl_region * cxlr,u64 offset,struct dpa_result * result)3488 static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset,
3489 struct dpa_result *result)
3490 {
3491 struct cxl_region_params *p = &cxlr->params;
3492 struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
3493 struct cxl_endpoint_decoder *cxled;
3494 u64 hpa_offset = offset;
3495 u64 dpa, dpa_offset;
3496 u16 eig = 0;
3497 u8 eiw = 0;
3498 int pos;
3499
3500 lockdep_assert_held(&cxl_rwsem.region);
3501 lockdep_assert_held(&cxl_rwsem.dpa);
3502
3503 /* Input validation ensures valid ways and gran */
3504 granularity_to_eig(p->interleave_granularity, &eig);
3505 ways_to_eiw(p->interleave_ways, &eiw);
3506
3507 /*
3508 * If the root decoder has SPA to CXL HPA callback, use it. Otherwise
3509 * CXL HPA is assumed to equal SPA.
3510 */
3511 if (cxlrd->ops.spa_to_hpa) {
3512 hpa_offset = cxlrd->ops.spa_to_hpa(cxlrd, p->res->start + offset);
3513 if (hpa_offset == ULLONG_MAX) {
3514 dev_dbg(&cxlr->dev, "HPA not found for %pr offset %#llx\n",
3515 p->res, offset);
3516 return -ENXIO;
3517 }
3518 hpa_offset -= p->res->start;
3519 }
3520
3521 if (region_is_unaligned_mod3(cxlr))
3522 return unaligned_region_offset_to_dpa_result(cxlr, offset,
3523 result);
3524
3525 pos = cxl_calculate_position(hpa_offset, eiw, eig);
3526 if (pos < 0 || pos >= p->nr_targets) {
3527 dev_dbg(&cxlr->dev, "Invalid position %d for %d targets\n",
3528 pos, p->nr_targets);
3529 return -ENXIO;
3530 }
3531
3532 dpa_offset = cxl_calculate_dpa_offset(hpa_offset, eiw, eig);
3533
3534 /* Look-up and return the result: a memdev and a DPA */
3535 for (int i = 0; i < p->nr_targets; i++) {
3536 cxled = p->targets[i];
3537 if (cxled->pos != pos)
3538 continue;
3539
3540 dpa = cxl_dpa_resource_start(cxled);
3541 if (dpa != RESOURCE_SIZE_MAX)
3542 dpa += dpa_offset;
3543
3544 result->cxlmd = cxled_to_memdev(cxled);
3545 result->dpa = dpa;
3546
3547 return 0;
3548 }
3549 dev_err(&cxlr->dev, "No device found for position %d\n", pos);
3550
3551 return -ENXIO;
3552 }
3553
match_root_decoder(struct device * dev,const void * data)3554 static int match_root_decoder(struct device *dev, const void *data)
3555 {
3556 const struct range *r1, *r2 = data;
3557 struct cxl_root_decoder *cxlrd;
3558
3559 if (!is_root_decoder(dev))
3560 return 0;
3561
3562 cxlrd = to_cxl_root_decoder(dev);
3563 r1 = &cxlrd->cxlsd.cxld.hpa_range;
3564
3565 return range_contains(r1, r2);
3566 }
3567
cxl_root_setup_translation(struct cxl_root * cxl_root,struct cxl_region_context * ctx)3568 static int cxl_root_setup_translation(struct cxl_root *cxl_root,
3569 struct cxl_region_context *ctx)
3570 {
3571 if (!cxl_root->ops.translation_setup_root)
3572 return 0;
3573
3574 return cxl_root->ops.translation_setup_root(cxl_root, ctx);
3575 }
3576
3577 /*
3578 * Note, when finished with the device, drop the reference with
3579 * put_device() or use the put_cxl_root_decoder helper.
3580 */
3581 static struct cxl_root_decoder *
get_cxl_root_decoder(struct cxl_endpoint_decoder * cxled,struct cxl_region_context * ctx)3582 get_cxl_root_decoder(struct cxl_endpoint_decoder *cxled,
3583 struct cxl_region_context *ctx)
3584 {
3585 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
3586 struct cxl_port *port = cxled_to_port(cxled);
3587 struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
3588 struct device *cxlrd_dev;
3589 int rc;
3590
3591 /*
3592 * Adjust the endpoint's HPA range and interleaving
3593 * configuration to the root decoder’s memory space before
3594 * setting up the root decoder.
3595 */
3596 rc = cxl_root_setup_translation(cxl_root, ctx);
3597 if (rc) {
3598 dev_err(cxlmd->dev.parent,
3599 "%s:%s Failed to setup translation for address range %#llx:%#llx\n",
3600 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
3601 ctx->hpa_range.start, ctx->hpa_range.end);
3602 return ERR_PTR(rc);
3603 }
3604
3605 cxlrd_dev = device_find_child(&cxl_root->port.dev, &ctx->hpa_range,
3606 match_root_decoder);
3607 if (!cxlrd_dev) {
3608 dev_err(cxlmd->dev.parent,
3609 "%s:%s no CXL window for range %#llx:%#llx\n",
3610 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
3611 ctx->hpa_range.start, ctx->hpa_range.end);
3612 return ERR_PTR(-ENXIO);
3613 }
3614
3615 return to_cxl_root_decoder(cxlrd_dev);
3616 }
3617
match_region_by_range(struct device * dev,const void * data)3618 static int match_region_by_range(struct device *dev, const void *data)
3619 {
3620 struct cxl_region_params *p;
3621 struct cxl_region *cxlr;
3622 const struct range *r = data;
3623
3624 if (!is_cxl_region(dev))
3625 return 0;
3626
3627 cxlr = to_cxl_region(dev);
3628 p = &cxlr->params;
3629
3630 guard(rwsem_read)(&cxl_rwsem.region);
3631 return spa_maps_hpa(p, r);
3632 }
3633
cxl_extended_linear_cache_resize(struct cxl_region * cxlr,struct resource * res)3634 static int cxl_extended_linear_cache_resize(struct cxl_region *cxlr,
3635 struct resource *res)
3636 {
3637 struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
3638 struct cxl_region_params *p = &cxlr->params;
3639 resource_size_t size = resource_size(res);
3640 resource_size_t cache_size, start;
3641
3642 cache_size = cxlrd->cache_size;
3643 if (!cache_size)
3644 return 0;
3645
3646 if (size != cache_size) {
3647 dev_warn(&cxlr->dev,
3648 "Extended Linear Cache size %pa != CXL size %pa. No Support!",
3649 &cache_size, &size);
3650 return -ENXIO;
3651 }
3652
3653 /*
3654 * Move the start of the range to where the cache range starts. The
3655 * implementation assumes that the cache range is in front of the
3656 * CXL range. This is not dictated by the HMAT spec but is how the
3657 * current known implementation is configured.
3658 *
3659 * The cache range is expected to be within the CFMWS. The adjusted
3660 * res->start should not be less than cxlrd->res->start.
3661 */
3662 start = res->start - cache_size;
3663 if (start < cxlrd->res->start)
3664 return -ENXIO;
3665
3666 res->start = start;
3667 p->cache_size = cache_size;
3668
3669 return 0;
3670 }
3671
__construct_region(struct cxl_region * cxlr,struct cxl_region_context * ctx)3672 static int __construct_region(struct cxl_region *cxlr,
3673 struct cxl_region_context *ctx)
3674 {
3675 struct cxl_endpoint_decoder *cxled = ctx->cxled;
3676 struct cxl_root_decoder *cxlrd = cxlr->cxlrd;
3677 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
3678 struct range *hpa_range = &ctx->hpa_range;
3679 struct cxl_region_params *p;
3680 struct resource *res;
3681 int rc;
3682
3683 guard(rwsem_write)(&cxl_rwsem.region);
3684 p = &cxlr->params;
3685 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) {
3686 dev_err(cxlmd->dev.parent,
3687 "%s:%s: %s autodiscovery interrupted\n",
3688 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
3689 __func__);
3690 return -EBUSY;
3691 }
3692
3693 set_bit(CXL_REGION_F_AUTO, &cxlr->flags);
3694 cxlr->hpa_range = *hpa_range;
3695
3696 res = kmalloc_obj(*res);
3697 if (!res)
3698 return -ENOMEM;
3699
3700 *res = DEFINE_RES_MEM_NAMED(hpa_range->start, range_len(hpa_range),
3701 dev_name(&cxlr->dev));
3702
3703 rc = cxl_extended_linear_cache_resize(cxlr, res);
3704 if (rc && rc != -EOPNOTSUPP) {
3705 /*
3706 * Failing to support extended linear cache region resize does not
3707 * prevent the region from functioning. Only causes cxl list showing
3708 * incorrect region size.
3709 */
3710 dev_warn(cxlmd->dev.parent,
3711 "Extended linear cache calculation failed rc:%d\n", rc);
3712 }
3713
3714 rc = sysfs_update_group(&cxlr->dev.kobj, &cxl_region_group);
3715 if (rc) {
3716 kfree(res);
3717 return rc;
3718 }
3719
3720 rc = insert_resource(cxlrd->res, res);
3721 if (rc) {
3722 /*
3723 * Platform-firmware may not have split resources like "System
3724 * RAM" on CXL window boundaries see cxl_region_iomem_release()
3725 */
3726 dev_warn(cxlmd->dev.parent,
3727 "%s:%s: %s %s cannot insert resource\n",
3728 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
3729 __func__, dev_name(&cxlr->dev));
3730 }
3731
3732 p->res = res;
3733 p->interleave_ways = ctx->interleave_ways;
3734 p->interleave_granularity = ctx->interleave_granularity;
3735 p->state = CXL_CONFIG_INTERLEAVE_ACTIVE;
3736
3737 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group());
3738 if (rc)
3739 return rc;
3740
3741 dev_dbg(cxlmd->dev.parent, "%s:%s: %s %s res: %pr iw: %d ig: %d\n",
3742 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), __func__,
3743 dev_name(&cxlr->dev), p->res, p->interleave_ways,
3744 p->interleave_granularity);
3745
3746 /* ...to match put_device() in cxl_add_to_region() */
3747 get_device(&cxlr->dev);
3748
3749 return 0;
3750 }
3751
3752 /* Establish an empty region covering the given HPA range */
construct_region(struct cxl_root_decoder * cxlrd,struct cxl_region_context * ctx)3753 static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd,
3754 struct cxl_region_context *ctx)
3755 {
3756 struct cxl_endpoint_decoder *cxled = ctx->cxled;
3757 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
3758 struct cxl_dev_state *cxlds = cxlmd->cxlds;
3759 int rc, part = READ_ONCE(cxled->part);
3760 struct cxl_region *cxlr;
3761
3762 if (part < 0)
3763 return ERR_PTR(-EBUSY);
3764
3765 do {
3766 cxlr = __create_region(cxlrd, cxlds->part[part].mode,
3767 atomic_read(&cxlrd->region_id),
3768 cxled->cxld.target_type);
3769 } while (IS_ERR(cxlr) && PTR_ERR(cxlr) == -EBUSY);
3770
3771 if (IS_ERR(cxlr)) {
3772 dev_err(cxlmd->dev.parent,
3773 "%s:%s: %s failed assign region: %pe\n",
3774 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev),
3775 __func__, cxlr);
3776 return cxlr;
3777 }
3778
3779 rc = __construct_region(cxlr, ctx);
3780 if (rc) {
3781 unregister_region(cxlr);
3782 return ERR_PTR(rc);
3783 }
3784
3785 return cxlr;
3786 }
3787
3788 static struct cxl_region *
cxl_find_region_by_range(struct cxl_root_decoder * cxlrd,struct range * hpa_range)3789 cxl_find_region_by_range(struct cxl_root_decoder *cxlrd,
3790 struct range *hpa_range)
3791 {
3792 struct device *region_dev;
3793
3794 region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa_range,
3795 match_region_by_range);
3796 if (!region_dev)
3797 return NULL;
3798
3799 return to_cxl_region(region_dev);
3800 }
3801
cxl_add_to_region(struct cxl_endpoint_decoder * cxled)3802 int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
3803 {
3804 struct cxl_region_context ctx;
3805 struct cxl_region_params *p;
3806 bool attach = false;
3807 int rc;
3808
3809 ctx = (struct cxl_region_context) {
3810 .cxled = cxled,
3811 .hpa_range = cxled->cxld.hpa_range,
3812 .interleave_ways = cxled->cxld.interleave_ways,
3813 .interleave_granularity = cxled->cxld.interleave_granularity,
3814 };
3815
3816 struct cxl_root_decoder *cxlrd __free(put_cxl_root_decoder) =
3817 get_cxl_root_decoder(cxled, &ctx);
3818
3819 if (IS_ERR(cxlrd))
3820 return PTR_ERR(cxlrd);
3821
3822 /*
3823 * Ensure that, if multiple threads race to construct_region()
3824 * for the HPA range, one does the construction and the others
3825 * add to that.
3826 */
3827 guard(mutex)(&cxlrd->regions_lock);
3828 struct cxl_region *cxlr __free(put_cxl_region) =
3829 cxl_find_region_by_range(cxlrd, &ctx.hpa_range);
3830 if (!cxlr)
3831 cxlr = construct_region(cxlrd, &ctx);
3832
3833 rc = PTR_ERR_OR_ZERO(cxlr);
3834 if (rc)
3835 return rc;
3836
3837 attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE);
3838
3839 scoped_guard(rwsem_read, &cxl_rwsem.region) {
3840 p = &cxlr->params;
3841 attach = p->state == CXL_CONFIG_COMMIT;
3842 }
3843
3844 if (attach) {
3845 /*
3846 * If device_attach() fails the range may still be active via
3847 * the platform-firmware memory map, otherwise the driver for
3848 * regions is local to this file, so driver matching can't fail.
3849 */
3850 if (device_attach(&cxlr->dev) < 0)
3851 dev_err(&cxlr->dev, "failed to enable, range: %pr\n",
3852 p->res);
3853 }
3854
3855 return rc;
3856 }
3857 EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, "CXL");
3858
is_system_ram(struct resource * res,void * arg)3859 static int is_system_ram(struct resource *res, void *arg)
3860 {
3861 struct cxl_region *cxlr = arg;
3862 struct cxl_region_params *p = &cxlr->params;
3863
3864 dev_dbg(&cxlr->dev, "%pr has System RAM: %pr\n", p->res, res);
3865 return 1;
3866 }
3867
shutdown_notifiers(void * _cxlr)3868 static void shutdown_notifiers(void *_cxlr)
3869 {
3870 struct cxl_region *cxlr = _cxlr;
3871
3872 unregister_node_notifier(&cxlr->node_notifier);
3873 unregister_mt_adistance_algorithm(&cxlr->adist_notifier);
3874 }
3875
remove_debugfs(void * dentry)3876 static void remove_debugfs(void *dentry)
3877 {
3878 debugfs_remove_recursive(dentry);
3879 }
3880
validate_region_offset(struct cxl_region * cxlr,u64 offset)3881 static int validate_region_offset(struct cxl_region *cxlr, u64 offset)
3882 {
3883 struct cxl_region_params *p = &cxlr->params;
3884 resource_size_t region_size;
3885 u64 hpa;
3886
3887 if (offset < p->cache_size) {
3888 dev_err(&cxlr->dev,
3889 "Offset %#llx is within extended linear cache %pa\n",
3890 offset, &p->cache_size);
3891 return -EINVAL;
3892 }
3893
3894 region_size = resource_size(p->res);
3895 if (offset >= region_size) {
3896 dev_err(&cxlr->dev, "Offset %#llx exceeds region size %pa\n",
3897 offset, ®ion_size);
3898 return -EINVAL;
3899 }
3900
3901 hpa = p->res->start + offset;
3902 if (hpa < p->res->start || hpa > p->res->end) {
3903 dev_err(&cxlr->dev, "HPA %#llx not in region %pr\n", hpa,
3904 p->res);
3905 return -EINVAL;
3906 }
3907
3908 return 0;
3909 }
3910
cxl_region_debugfs_poison_inject(void * data,u64 offset)3911 static int cxl_region_debugfs_poison_inject(void *data, u64 offset)
3912 {
3913 struct dpa_result result = { .dpa = ULLONG_MAX, .cxlmd = NULL };
3914 struct cxl_region *cxlr = data;
3915 int rc;
3916
3917 ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region);
3918 if ((rc = ACQUIRE_ERR(rwsem_read_intr, ®ion_rwsem)))
3919 return rc;
3920
3921 ACQUIRE(rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa);
3922 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &dpa_rwsem)))
3923 return rc;
3924
3925 if (validate_region_offset(cxlr, offset))
3926 return -EINVAL;
3927
3928 offset -= cxlr->params.cache_size;
3929 rc = region_offset_to_dpa_result(cxlr, offset, &result);
3930 if (rc || !result.cxlmd || result.dpa == ULLONG_MAX) {
3931 dev_dbg(&cxlr->dev,
3932 "Failed to resolve DPA for region offset %#llx rc %d\n",
3933 offset, rc);
3934
3935 return rc ? rc : -EINVAL;
3936 }
3937
3938 return cxl_inject_poison_locked(result.cxlmd, result.dpa);
3939 }
3940
3941 DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_inject_fops, NULL,
3942 cxl_region_debugfs_poison_inject, "%llx\n");
3943
cxl_region_debugfs_poison_clear(void * data,u64 offset)3944 static int cxl_region_debugfs_poison_clear(void *data, u64 offset)
3945 {
3946 struct dpa_result result = { .dpa = ULLONG_MAX, .cxlmd = NULL };
3947 struct cxl_region *cxlr = data;
3948 int rc;
3949
3950 ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region);
3951 if ((rc = ACQUIRE_ERR(rwsem_read_intr, ®ion_rwsem)))
3952 return rc;
3953
3954 ACQUIRE(rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa);
3955 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &dpa_rwsem)))
3956 return rc;
3957
3958 if (validate_region_offset(cxlr, offset))
3959 return -EINVAL;
3960
3961 offset -= cxlr->params.cache_size;
3962 rc = region_offset_to_dpa_result(cxlr, offset, &result);
3963 if (rc || !result.cxlmd || result.dpa == ULLONG_MAX) {
3964 dev_dbg(&cxlr->dev,
3965 "Failed to resolve DPA for region offset %#llx rc %d\n",
3966 offset, rc);
3967
3968 return rc ? rc : -EINVAL;
3969 }
3970
3971 return cxl_clear_poison_locked(result.cxlmd, result.dpa);
3972 }
3973
3974 DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_clear_fops, NULL,
3975 cxl_region_debugfs_poison_clear, "%llx\n");
3976
cxl_region_setup_poison(struct cxl_region * cxlr)3977 static int cxl_region_setup_poison(struct cxl_region *cxlr)
3978 {
3979 struct device *dev = &cxlr->dev;
3980 struct cxl_region_params *p = &cxlr->params;
3981 struct dentry *dentry;
3982
3983 /*
3984 * Do not enable poison injection in Normalized Address mode.
3985 * Conversion between SPA and DPA is required for this, but it is
3986 * not supported in this mode.
3987 */
3988 if (test_bit(CXL_REGION_F_NORMALIZED_ADDRESSING, &cxlr->flags))
3989 return 0;
3990
3991 /* Create poison attributes if all memdevs support the capabilities */
3992 for (int i = 0; i < p->nr_targets; i++) {
3993 struct cxl_endpoint_decoder *cxled = p->targets[i];
3994 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
3995
3996 if (!cxl_memdev_has_poison_cmd(cxlmd, CXL_POISON_ENABLED_INJECT) ||
3997 !cxl_memdev_has_poison_cmd(cxlmd, CXL_POISON_ENABLED_CLEAR))
3998 return 0;
3999 }
4000
4001 dentry = cxl_debugfs_create_dir(dev_name(dev));
4002 debugfs_create_file("inject_poison", 0200, dentry, cxlr,
4003 &cxl_poison_inject_fops);
4004 debugfs_create_file("clear_poison", 0200, dentry, cxlr,
4005 &cxl_poison_clear_fops);
4006
4007 return devm_add_action_or_reset(dev, remove_debugfs, dentry);
4008 }
4009
region_contains_resource(struct device * dev,const void * data)4010 static int region_contains_resource(struct device *dev, const void *data)
4011 {
4012 const struct resource *res = data;
4013 struct cxl_region *cxlr;
4014 struct cxl_region_params *p;
4015
4016 if (!is_cxl_region(dev))
4017 return 0;
4018
4019 cxlr = to_cxl_region(dev);
4020 p = &cxlr->params;
4021
4022 if (p->state != CXL_CONFIG_COMMIT)
4023 return 0;
4024
4025 if (!p->res)
4026 return 0;
4027
4028 return resource_contains(p->res, res) ? 1 : 0;
4029 }
4030
cxl_region_contains_resource(const struct resource * res)4031 bool cxl_region_contains_resource(const struct resource *res)
4032 {
4033 guard(rwsem_read)(&cxl_rwsem.region);
4034 struct device *dev __free(put_device) = bus_find_device(
4035 &cxl_bus_type, NULL, res, region_contains_resource);
4036 return !!dev;
4037 }
4038 EXPORT_SYMBOL_FOR_MODULES(cxl_region_contains_resource, "dax_hmem");
4039
cxl_region_can_probe(struct cxl_region * cxlr)4040 static int cxl_region_can_probe(struct cxl_region *cxlr)
4041 {
4042 struct cxl_region_params *p = &cxlr->params;
4043 int rc;
4044
4045 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
4046 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) {
4047 dev_dbg(&cxlr->dev, "probe interrupted\n");
4048 return rc;
4049 }
4050
4051 if (p->state < CXL_CONFIG_COMMIT) {
4052 dev_dbg(&cxlr->dev, "config state: %d\n", p->state);
4053 return -ENXIO;
4054 }
4055
4056 if (test_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags)) {
4057 dev_err(&cxlr->dev,
4058 "failed to activate, re-commit region and retry\n");
4059 return -ENXIO;
4060 }
4061
4062 return 0;
4063 }
4064
first_mapped_decoder(struct device * dev,const void * data)4065 static int first_mapped_decoder(struct device *dev, const void *data)
4066 {
4067 struct cxl_endpoint_decoder *cxled;
4068
4069 if (!is_endpoint_decoder(dev))
4070 return 0;
4071
4072 cxled = to_cxl_endpoint_decoder(dev);
4073 if (cxled->cxld.region)
4074 return 1;
4075
4076 return 0;
4077 }
4078
4079 /*
4080 * Runs in cxl_mem_probe context after successful endpoint probe, assumes the
4081 * simple case of single mapped decoder per memdev.
4082 */
cxl_memdev_attach_region(struct cxl_memdev * cxlmd)4083 int cxl_memdev_attach_region(struct cxl_memdev *cxlmd)
4084 {
4085 struct cxl_attach_region *attach =
4086 container_of(cxlmd->attach, typeof(*attach), attach);
4087 struct cxl_port *endpoint = cxlmd->endpoint;
4088 struct cxl_endpoint_decoder *cxled;
4089 struct cxl_region *cxlr;
4090 int rc;
4091
4092 /* hold endpoint lock to setup autoremove of the region */
4093 guard(device)(&endpoint->dev);
4094 if (!endpoint->dev.driver)
4095 return -ENXIO;
4096 guard(rwsem_read)(&cxl_rwsem.region);
4097 guard(rwsem_read)(&cxl_rwsem.dpa);
4098
4099 /*
4100 * TODO auto-instantiate a region, for now assume this will find an
4101 * auto-region
4102 */
4103 struct device *dev __free(put_device) =
4104 device_find_child(&endpoint->dev, NULL, first_mapped_decoder);
4105
4106 if (!dev) {
4107 dev_dbg(cxlmd->cxlds->dev, "no region found for memdev %s\n",
4108 dev_name(&cxlmd->dev));
4109 return -ENXIO;
4110 }
4111
4112 cxled = to_cxl_endpoint_decoder(dev);
4113 cxlr = cxled->cxld.region;
4114
4115 if (cxlr->params.state < CXL_CONFIG_COMMIT) {
4116 dev_dbg(cxlmd->cxlds->dev,
4117 "region %s not committed for memdev %s\n",
4118 dev_name(&cxlr->dev), dev_name(&cxlmd->dev));
4119 return -ENXIO;
4120 }
4121
4122 if (cxlr->params.nr_targets > 1) {
4123 dev_dbg(cxlmd->cxlds->dev,
4124 "Only attach to local non-interleaved region\n");
4125 return -ENXIO;
4126 }
4127
4128 /* Only teardown regions that pass validation, ignore the rest */
4129 get_device(&cxlr->dev);
4130 rc = devm_add_action_or_reset(&endpoint->dev,
4131 endpoint_unregister_region, cxlr);
4132 if (rc)
4133 return rc;
4134
4135 attach->hpa_range = (struct range) {
4136 .start = cxlr->params.res->start,
4137 .end = cxlr->params.res->end,
4138 };
4139 return 0;
4140 }
4141 EXPORT_SYMBOL_FOR_MODULES(cxl_memdev_attach_region, "cxl_mem");
4142
4143 /*
4144 * The presence of an attach method indicates that the region is designated for
4145 * a purpose outside of CXL core memory expansion defaults.
4146 */
cxl_region_has_memdev_attach(struct cxl_region * cxlr)4147 static bool cxl_region_has_memdev_attach(struct cxl_region *cxlr)
4148 {
4149 struct cxl_region_params *p = &cxlr->params;
4150
4151 for (int i = 0; i < p->nr_targets; i++) {
4152 struct cxl_endpoint_decoder *cxled = p->targets[i];
4153 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
4154
4155 if (cxlmd->attach)
4156 return true;
4157 }
4158
4159 return false;
4160 }
4161
cxl_region_probe(struct device * dev)4162 static int cxl_region_probe(struct device *dev)
4163 {
4164 struct cxl_region *cxlr = to_cxl_region(dev);
4165 struct cxl_region_params *p = &cxlr->params;
4166 int rc;
4167
4168 rc = cxl_region_can_probe(cxlr);
4169 if (rc)
4170 return rc;
4171
4172 /*
4173 * From this point on any path that changes the region's state away from
4174 * CXL_CONFIG_COMMIT is also responsible for releasing the driver.
4175 */
4176
4177 cxlr->node_notifier.notifier_call = cxl_region_perf_attrs_callback;
4178 cxlr->node_notifier.priority = CXL_CALLBACK_PRI;
4179 register_node_notifier(&cxlr->node_notifier);
4180
4181 cxlr->adist_notifier.notifier_call = cxl_region_calculate_adistance;
4182 cxlr->adist_notifier.priority = 100;
4183 register_mt_adistance_algorithm(&cxlr->adist_notifier);
4184
4185 rc = devm_add_action_or_reset(&cxlr->dev, shutdown_notifiers, cxlr);
4186 if (rc)
4187 return rc;
4188
4189 /*
4190 * Regions fronted by an extended linear cache need the MCE notifier to
4191 * offline the aliased page on a memory error.
4192 */
4193 if (p->cache_size) {
4194 rc = devm_cxl_register_mce_notifier(&cxlr->dev,
4195 &cxlr->mce_notifier);
4196 if (rc == -EOPNOTSUPP)
4197 dev_warn(&cxlr->dev,
4198 "CONFIG_CXL_MCE disabled, MCE notifier not registered\n");
4199 else if (rc)
4200 return rc;
4201 }
4202
4203 rc = cxl_region_setup_poison(cxlr);
4204 if (rc)
4205 return rc;
4206
4207 if (cxl_region_has_memdev_attach(cxlr))
4208 return 0;
4209
4210 switch (cxlr->mode) {
4211 case CXL_PARTMODE_PMEM:
4212 rc = devm_cxl_region_edac_register(cxlr);
4213 if (rc)
4214 dev_dbg(&cxlr->dev, "CXL EDAC registration for region_id=%d failed\n",
4215 cxlr->id);
4216
4217 return devm_cxl_add_pmem_region(cxlr);
4218 case CXL_PARTMODE_RAM:
4219 rc = devm_cxl_region_edac_register(cxlr);
4220 if (rc)
4221 dev_dbg(&cxlr->dev, "CXL EDAC registration for region_id=%d failed\n",
4222 cxlr->id);
4223
4224 /*
4225 * The region can not be manged by CXL if any portion of
4226 * it is already online as 'System RAM'
4227 */
4228 if (walk_iomem_res_desc(IORES_DESC_NONE,
4229 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
4230 p->res->start, p->res->end, cxlr,
4231 is_system_ram) > 0)
4232 return 0;
4233 return devm_cxl_add_dax_region(cxlr);
4234 default:
4235 dev_dbg(&cxlr->dev, "unsupported region mode: %d\n",
4236 cxlr->mode);
4237 return -ENXIO;
4238 }
4239 }
4240
4241 static struct cxl_driver cxl_region_driver = {
4242 .name = "cxl_region",
4243 .probe = cxl_region_probe,
4244 .id = CXL_DEVICE_REGION,
4245 };
4246
cxl_region_init(void)4247 int cxl_region_init(void)
4248 {
4249 return cxl_driver_register(&cxl_region_driver);
4250 }
4251
cxl_region_exit(void)4252 void cxl_region_exit(void)
4253 {
4254 cxl_driver_unregister(&cxl_region_driver);
4255 }
4256
4257 MODULE_IMPORT_NS("CXL");
4258 MODULE_IMPORT_NS("DEVMEM");
4259 MODULE_ALIAS_CXL(CXL_DEVICE_REGION);
4260