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