xref: /linux/drivers/cxl/core/port.c (revision 323bbfcf1ef8836d0d2ad9e2c1f1c684f0e3b5b3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2020 Intel Corporation. All rights reserved. */
3 #include <linux/platform_device.h>
4 #include <linux/memregion.h>
5 #include <linux/workqueue.h>
6 #include <linux/debugfs.h>
7 #include <linux/device.h>
8 #include <linux/module.h>
9 #include <linux/pci.h>
10 #include <linux/slab.h>
11 #include <linux/idr.h>
12 #include <linux/node.h>
13 #include <cxl/einj.h>
14 #include <cxlmem.h>
15 #include <cxlpci.h>
16 #include <cxl.h>
17 #include "core.h"
18 
19 /**
20  * DOC: cxl core
21  *
22  * The CXL core provides a set of interfaces that can be consumed by CXL aware
23  * drivers. The interfaces allow for creation, modification, and destruction of
24  * regions, memory devices, ports, and decoders. CXL aware drivers must register
25  * with the CXL core via these interfaces in order to be able to participate in
26  * cross-device interleave coordination. The CXL core also establishes and
27  * maintains the bridge to the nvdimm subsystem.
28  *
29  * CXL core introduces sysfs hierarchy to control the devices that are
30  * instantiated by the core.
31  */
32 
33 static DEFINE_IDA(cxl_port_ida);
34 static DEFINE_XARRAY(cxl_root_buses);
35 
36 /*
37  * The terminal device in PCI is NULL and @platform_bus
38  * for platform devices (for cxl_test)
39  */
is_cxl_host_bridge(struct device * dev)40 static bool is_cxl_host_bridge(struct device *dev)
41 {
42 	return (!dev || dev == &platform_bus);
43 }
44 
cxl_num_decoders_committed(struct cxl_port * port)45 int cxl_num_decoders_committed(struct cxl_port *port)
46 {
47 	lockdep_assert_held(&cxl_rwsem.region);
48 
49 	return port->commit_end + 1;
50 }
51 
devtype_show(struct device * dev,struct device_attribute * attr,char * buf)52 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
53 			    char *buf)
54 {
55 	return sysfs_emit(buf, "%s\n", dev->type->name);
56 }
57 static DEVICE_ATTR_RO(devtype);
58 
cxl_device_id(const struct device * dev)59 static int cxl_device_id(const struct device *dev)
60 {
61 	if (dev->type == &cxl_nvdimm_bridge_type)
62 		return CXL_DEVICE_NVDIMM_BRIDGE;
63 	if (dev->type == &cxl_nvdimm_type)
64 		return CXL_DEVICE_NVDIMM;
65 	if (dev->type == CXL_PMEM_REGION_TYPE())
66 		return CXL_DEVICE_PMEM_REGION;
67 	if (dev->type == CXL_DAX_REGION_TYPE())
68 		return CXL_DEVICE_DAX_REGION;
69 	if (is_cxl_port(dev)) {
70 		if (is_cxl_root(to_cxl_port(dev)))
71 			return CXL_DEVICE_ROOT;
72 		return CXL_DEVICE_PORT;
73 	}
74 	if (is_cxl_memdev(dev))
75 		return CXL_DEVICE_MEMORY_EXPANDER;
76 	if (dev->type == CXL_REGION_TYPE())
77 		return CXL_DEVICE_REGION;
78 	if (dev->type == &cxl_pmu_type)
79 		return CXL_DEVICE_PMU;
80 	return 0;
81 }
82 
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)83 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
84 			     char *buf)
85 {
86 	return sysfs_emit(buf, CXL_MODALIAS_FMT "\n", cxl_device_id(dev));
87 }
88 static DEVICE_ATTR_RO(modalias);
89 
90 static struct attribute *cxl_base_attributes[] = {
91 	&dev_attr_devtype.attr,
92 	&dev_attr_modalias.attr,
93 	NULL,
94 };
95 
96 struct attribute_group cxl_base_attribute_group = {
97 	.attrs = cxl_base_attributes,
98 };
99 
start_show(struct device * dev,struct device_attribute * attr,char * buf)100 static ssize_t start_show(struct device *dev, struct device_attribute *attr,
101 			  char *buf)
102 {
103 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
104 
105 	return sysfs_emit(buf, "%#llx\n", cxld->hpa_range.start);
106 }
107 static DEVICE_ATTR_ADMIN_RO(start);
108 
size_show(struct device * dev,struct device_attribute * attr,char * buf)109 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
110 			char *buf)
111 {
112 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
113 
114 	return sysfs_emit(buf, "%#llx\n", range_len(&cxld->hpa_range));
115 }
116 static DEVICE_ATTR_RO(size);
117 
118 #define CXL_DECODER_FLAG_ATTR(name, flag)                            \
119 static ssize_t name##_show(struct device *dev,                       \
120 			   struct device_attribute *attr, char *buf) \
121 {                                                                    \
122 	struct cxl_decoder *cxld = to_cxl_decoder(dev);              \
123                                                                      \
124 	return sysfs_emit(buf, "%s\n",                               \
125 			  (cxld->flags & (flag)) ? "1" : "0");       \
126 }                                                                    \
127 static DEVICE_ATTR_RO(name)
128 
129 CXL_DECODER_FLAG_ATTR(cap_pmem, CXL_DECODER_F_PMEM);
130 CXL_DECODER_FLAG_ATTR(cap_ram, CXL_DECODER_F_RAM);
131 CXL_DECODER_FLAG_ATTR(cap_type2, CXL_DECODER_F_TYPE2);
132 CXL_DECODER_FLAG_ATTR(cap_type3, CXL_DECODER_F_TYPE3);
133 CXL_DECODER_FLAG_ATTR(locked, CXL_DECODER_F_LOCK);
134 
target_type_show(struct device * dev,struct device_attribute * attr,char * buf)135 static ssize_t target_type_show(struct device *dev,
136 				struct device_attribute *attr, char *buf)
137 {
138 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
139 
140 	switch (cxld->target_type) {
141 	case CXL_DECODER_DEVMEM:
142 		return sysfs_emit(buf, "accelerator\n");
143 	case CXL_DECODER_HOSTONLYMEM:
144 		return sysfs_emit(buf, "expander\n");
145 	}
146 	return -ENXIO;
147 }
148 static DEVICE_ATTR_RO(target_type);
149 
emit_target_list(struct cxl_switch_decoder * cxlsd,char * buf)150 static ssize_t emit_target_list(struct cxl_switch_decoder *cxlsd, char *buf)
151 {
152 	struct cxl_decoder *cxld = &cxlsd->cxld;
153 	ssize_t offset = 0;
154 	int i, rc = 0;
155 
156 	for (i = 0; i < cxld->interleave_ways; i++) {
157 		struct cxl_dport *dport = cxlsd->target[i];
158 		struct cxl_dport *next = NULL;
159 
160 		if (!dport)
161 			break;
162 
163 		if (i + 1 < cxld->interleave_ways)
164 			next = cxlsd->target[i + 1];
165 		rc = sysfs_emit_at(buf, offset, "%d%s", dport->port_id,
166 				   next ? "," : "");
167 		if (rc < 0)
168 			return rc;
169 		offset += rc;
170 	}
171 
172 	return offset;
173 }
174 
target_list_show(struct device * dev,struct device_attribute * attr,char * buf)175 static ssize_t target_list_show(struct device *dev,
176 				struct device_attribute *attr, char *buf)
177 {
178 	struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
179 	ssize_t offset;
180 	int rc;
181 
182 	guard(rwsem_read)(&cxl_rwsem.region);
183 	rc = emit_target_list(cxlsd, buf);
184 	if (rc < 0)
185 		return rc;
186 	offset = rc;
187 
188 	rc = sysfs_emit_at(buf, offset, "\n");
189 	if (rc < 0)
190 		return rc;
191 
192 	return offset + rc;
193 }
194 static DEVICE_ATTR_RO(target_list);
195 
mode_show(struct device * dev,struct device_attribute * attr,char * buf)196 static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
197 			 char *buf)
198 {
199 	struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
200 	struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
201 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
202 	/* without @cxl_rwsem.dpa, make sure @part is not reloaded */
203 	int part = READ_ONCE(cxled->part);
204 	const char *desc;
205 
206 	if (part < 0)
207 		desc = "none";
208 	else
209 		desc = cxlds->part[part].res.name;
210 
211 	return sysfs_emit(buf, "%s\n", desc);
212 }
213 
mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)214 static ssize_t mode_store(struct device *dev, struct device_attribute *attr,
215 			  const char *buf, size_t len)
216 {
217 	struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
218 	enum cxl_partition_mode mode;
219 	ssize_t rc;
220 
221 	if (sysfs_streq(buf, "pmem"))
222 		mode = CXL_PARTMODE_PMEM;
223 	else if (sysfs_streq(buf, "ram"))
224 		mode = CXL_PARTMODE_RAM;
225 	else
226 		return -EINVAL;
227 
228 	rc = cxl_dpa_set_part(cxled, mode);
229 	if (rc)
230 		return rc;
231 
232 	return len;
233 }
234 static DEVICE_ATTR_RW(mode);
235 
dpa_resource_show(struct device * dev,struct device_attribute * attr,char * buf)236 static ssize_t dpa_resource_show(struct device *dev, struct device_attribute *attr,
237 			    char *buf)
238 {
239 	struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
240 
241 	guard(rwsem_read)(&cxl_rwsem.dpa);
242 	return sysfs_emit(buf, "%#llx\n", (u64)cxl_dpa_resource_start(cxled));
243 }
244 static DEVICE_ATTR_RO(dpa_resource);
245 
dpa_size_show(struct device * dev,struct device_attribute * attr,char * buf)246 static ssize_t dpa_size_show(struct device *dev, struct device_attribute *attr,
247 			     char *buf)
248 {
249 	struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
250 	resource_size_t size = cxl_dpa_size(cxled);
251 
252 	return sysfs_emit(buf, "%pa\n", &size);
253 }
254 
dpa_size_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)255 static ssize_t dpa_size_store(struct device *dev, struct device_attribute *attr,
256 			      const char *buf, size_t len)
257 {
258 	struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
259 	unsigned long long size;
260 	ssize_t rc;
261 
262 	rc = kstrtoull(buf, 0, &size);
263 	if (rc)
264 		return rc;
265 
266 	if (!IS_ALIGNED(size, SZ_256M))
267 		return -EINVAL;
268 
269 	rc = cxl_dpa_free(cxled);
270 	if (rc)
271 		return rc;
272 
273 	if (size == 0)
274 		return len;
275 
276 	rc = cxl_dpa_alloc(cxled, size);
277 	if (rc)
278 		return rc;
279 
280 	return len;
281 }
282 static DEVICE_ATTR_RW(dpa_size);
283 
interleave_granularity_show(struct device * dev,struct device_attribute * attr,char * buf)284 static ssize_t interleave_granularity_show(struct device *dev,
285 					   struct device_attribute *attr,
286 					   char *buf)
287 {
288 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
289 
290 	return sysfs_emit(buf, "%d\n", cxld->interleave_granularity);
291 }
292 
293 static DEVICE_ATTR_RO(interleave_granularity);
294 
interleave_ways_show(struct device * dev,struct device_attribute * attr,char * buf)295 static ssize_t interleave_ways_show(struct device *dev,
296 				    struct device_attribute *attr, char *buf)
297 {
298 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
299 
300 	return sysfs_emit(buf, "%d\n", cxld->interleave_ways);
301 }
302 
303 static DEVICE_ATTR_RO(interleave_ways);
304 
qos_class_show(struct device * dev,struct device_attribute * attr,char * buf)305 static ssize_t qos_class_show(struct device *dev,
306 			      struct device_attribute *attr, char *buf)
307 {
308 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
309 
310 	return sysfs_emit(buf, "%d\n", cxlrd->qos_class);
311 }
312 static DEVICE_ATTR_RO(qos_class);
313 
314 static struct attribute *cxl_decoder_base_attrs[] = {
315 	&dev_attr_start.attr,
316 	&dev_attr_size.attr,
317 	&dev_attr_locked.attr,
318 	&dev_attr_interleave_granularity.attr,
319 	&dev_attr_interleave_ways.attr,
320 	NULL,
321 };
322 
323 static struct attribute_group cxl_decoder_base_attribute_group = {
324 	.attrs = cxl_decoder_base_attrs,
325 };
326 
327 static struct attribute *cxl_decoder_root_attrs[] = {
328 	&dev_attr_cap_pmem.attr,
329 	&dev_attr_cap_ram.attr,
330 	&dev_attr_cap_type2.attr,
331 	&dev_attr_cap_type3.attr,
332 	&dev_attr_target_list.attr,
333 	&dev_attr_qos_class.attr,
334 	SET_CXL_REGION_ATTR(create_pmem_region)
335 	SET_CXL_REGION_ATTR(create_ram_region)
336 	SET_CXL_REGION_ATTR(delete_region)
337 	NULL,
338 };
339 
can_create_pmem(struct cxl_root_decoder * cxlrd)340 static bool can_create_pmem(struct cxl_root_decoder *cxlrd)
341 {
342 	unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_PMEM;
343 
344 	return (cxlrd->cxlsd.cxld.flags & flags) == flags;
345 }
346 
can_create_ram(struct cxl_root_decoder * cxlrd)347 static bool can_create_ram(struct cxl_root_decoder *cxlrd)
348 {
349 	unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_RAM;
350 
351 	return (cxlrd->cxlsd.cxld.flags & flags) == flags;
352 }
353 
cxl_root_decoder_visible(struct kobject * kobj,struct attribute * a,int n)354 static umode_t cxl_root_decoder_visible(struct kobject *kobj, struct attribute *a, int n)
355 {
356 	struct device *dev = kobj_to_dev(kobj);
357 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
358 
359 	if (a == CXL_REGION_ATTR(create_pmem_region) && !can_create_pmem(cxlrd))
360 		return 0;
361 
362 	if (a == CXL_REGION_ATTR(create_ram_region) && !can_create_ram(cxlrd))
363 		return 0;
364 
365 	if (a == CXL_REGION_ATTR(delete_region) &&
366 	    !(can_create_pmem(cxlrd) || can_create_ram(cxlrd)))
367 		return 0;
368 
369 	return a->mode;
370 }
371 
372 static struct attribute_group cxl_decoder_root_attribute_group = {
373 	.attrs = cxl_decoder_root_attrs,
374 	.is_visible = cxl_root_decoder_visible,
375 };
376 
377 static const struct attribute_group *cxl_decoder_root_attribute_groups[] = {
378 	&cxl_decoder_root_attribute_group,
379 	&cxl_decoder_base_attribute_group,
380 	&cxl_base_attribute_group,
381 	NULL,
382 };
383 
384 static struct attribute *cxl_decoder_switch_attrs[] = {
385 	&dev_attr_target_type.attr,
386 	&dev_attr_target_list.attr,
387 	SET_CXL_REGION_ATTR(region)
388 	NULL,
389 };
390 
391 static struct attribute_group cxl_decoder_switch_attribute_group = {
392 	.attrs = cxl_decoder_switch_attrs,
393 };
394 
395 static const struct attribute_group *cxl_decoder_switch_attribute_groups[] = {
396 	&cxl_decoder_switch_attribute_group,
397 	&cxl_decoder_base_attribute_group,
398 	&cxl_base_attribute_group,
399 	NULL,
400 };
401 
402 static struct attribute *cxl_decoder_endpoint_attrs[] = {
403 	&dev_attr_target_type.attr,
404 	&dev_attr_mode.attr,
405 	&dev_attr_dpa_size.attr,
406 	&dev_attr_dpa_resource.attr,
407 	SET_CXL_REGION_ATTR(region)
408 	NULL,
409 };
410 
411 static struct attribute_group cxl_decoder_endpoint_attribute_group = {
412 	.attrs = cxl_decoder_endpoint_attrs,
413 };
414 
415 static const struct attribute_group *cxl_decoder_endpoint_attribute_groups[] = {
416 	&cxl_decoder_base_attribute_group,
417 	&cxl_decoder_endpoint_attribute_group,
418 	&cxl_base_attribute_group,
419 	NULL,
420 };
421 
__cxl_decoder_release(struct cxl_decoder * cxld)422 static void __cxl_decoder_release(struct cxl_decoder *cxld)
423 {
424 	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
425 
426 	ida_free(&port->decoder_ida, cxld->id);
427 	put_device(&port->dev);
428 }
429 
cxl_endpoint_decoder_release(struct device * dev)430 static void cxl_endpoint_decoder_release(struct device *dev)
431 {
432 	struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
433 
434 	__cxl_decoder_release(&cxled->cxld);
435 	kfree(cxled);
436 }
437 
cxl_switch_decoder_release(struct device * dev)438 static void cxl_switch_decoder_release(struct device *dev)
439 {
440 	struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
441 
442 	__cxl_decoder_release(&cxlsd->cxld);
443 	kfree(cxlsd);
444 }
445 
to_cxl_root_decoder(struct device * dev)446 struct cxl_root_decoder *to_cxl_root_decoder(struct device *dev)
447 {
448 	if (dev_WARN_ONCE(dev, !is_root_decoder(dev),
449 			  "not a cxl_root_decoder device\n"))
450 		return NULL;
451 	return container_of(dev, struct cxl_root_decoder, cxlsd.cxld.dev);
452 }
453 EXPORT_SYMBOL_NS_GPL(to_cxl_root_decoder, "CXL");
454 
cxl_root_decoder_release(struct device * dev)455 static void cxl_root_decoder_release(struct device *dev)
456 {
457 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
458 
459 	if (atomic_read(&cxlrd->region_id) >= 0)
460 		memregion_free(atomic_read(&cxlrd->region_id));
461 	__cxl_decoder_release(&cxlrd->cxlsd.cxld);
462 	kfree(cxlrd);
463 }
464 
465 static const struct device_type cxl_decoder_endpoint_type = {
466 	.name = "cxl_decoder_endpoint",
467 	.release = cxl_endpoint_decoder_release,
468 	.groups = cxl_decoder_endpoint_attribute_groups,
469 };
470 
471 static const struct device_type cxl_decoder_switch_type = {
472 	.name = "cxl_decoder_switch",
473 	.release = cxl_switch_decoder_release,
474 	.groups = cxl_decoder_switch_attribute_groups,
475 };
476 
477 static const struct device_type cxl_decoder_root_type = {
478 	.name = "cxl_decoder_root",
479 	.release = cxl_root_decoder_release,
480 	.groups = cxl_decoder_root_attribute_groups,
481 };
482 
is_endpoint_decoder(struct device * dev)483 bool is_endpoint_decoder(struct device *dev)
484 {
485 	return dev->type == &cxl_decoder_endpoint_type;
486 }
487 EXPORT_SYMBOL_NS_GPL(is_endpoint_decoder, "CXL");
488 
is_root_decoder(struct device * dev)489 bool is_root_decoder(struct device *dev)
490 {
491 	return dev->type == &cxl_decoder_root_type;
492 }
493 EXPORT_SYMBOL_NS_GPL(is_root_decoder, "CXL");
494 
is_switch_decoder(struct device * dev)495 bool is_switch_decoder(struct device *dev)
496 {
497 	return is_root_decoder(dev) || dev->type == &cxl_decoder_switch_type;
498 }
499 EXPORT_SYMBOL_NS_GPL(is_switch_decoder, "CXL");
500 
to_cxl_decoder(struct device * dev)501 struct cxl_decoder *to_cxl_decoder(struct device *dev)
502 {
503 	if (dev_WARN_ONCE(dev,
504 			  !is_switch_decoder(dev) && !is_endpoint_decoder(dev),
505 			  "not a cxl_decoder device\n"))
506 		return NULL;
507 	return container_of(dev, struct cxl_decoder, dev);
508 }
509 EXPORT_SYMBOL_NS_GPL(to_cxl_decoder, "CXL");
510 
to_cxl_endpoint_decoder(struct device * dev)511 struct cxl_endpoint_decoder *to_cxl_endpoint_decoder(struct device *dev)
512 {
513 	if (dev_WARN_ONCE(dev, !is_endpoint_decoder(dev),
514 			  "not a cxl_endpoint_decoder device\n"))
515 		return NULL;
516 	return container_of(dev, struct cxl_endpoint_decoder, cxld.dev);
517 }
518 EXPORT_SYMBOL_NS_GPL(to_cxl_endpoint_decoder, "CXL");
519 
to_cxl_switch_decoder(struct device * dev)520 struct cxl_switch_decoder *to_cxl_switch_decoder(struct device *dev)
521 {
522 	if (dev_WARN_ONCE(dev, !is_switch_decoder(dev),
523 			  "not a cxl_switch_decoder device\n"))
524 		return NULL;
525 	return container_of(dev, struct cxl_switch_decoder, cxld.dev);
526 }
527 EXPORT_SYMBOL_NS_GPL(to_cxl_switch_decoder, "CXL");
528 
cxl_ep_release(struct cxl_ep * ep)529 static void cxl_ep_release(struct cxl_ep *ep)
530 {
531 	put_device(ep->ep);
532 	kfree(ep);
533 }
534 
cxl_ep_remove(struct cxl_port * port,struct cxl_ep * ep)535 static void cxl_ep_remove(struct cxl_port *port, struct cxl_ep *ep)
536 {
537 	if (!ep)
538 		return;
539 	xa_erase(&port->endpoints, (unsigned long) ep->ep);
540 	cxl_ep_release(ep);
541 }
542 
cxl_port_release(struct device * dev)543 static void cxl_port_release(struct device *dev)
544 {
545 	struct cxl_port *port = to_cxl_port(dev);
546 	unsigned long index;
547 	struct cxl_ep *ep;
548 
549 	xa_for_each(&port->endpoints, index, ep)
550 		cxl_ep_remove(port, ep);
551 	xa_destroy(&port->endpoints);
552 	xa_destroy(&port->dports);
553 	xa_destroy(&port->regions);
554 	ida_free(&cxl_port_ida, port->id);
555 	if (is_cxl_root(port))
556 		kfree(to_cxl_root(port));
557 	else
558 		kfree(port);
559 }
560 
decoders_committed_show(struct device * dev,struct device_attribute * attr,char * buf)561 static ssize_t decoders_committed_show(struct device *dev,
562 				       struct device_attribute *attr, char *buf)
563 {
564 	struct cxl_port *port = to_cxl_port(dev);
565 
566 	guard(rwsem_read)(&cxl_rwsem.region);
567 	return sysfs_emit(buf, "%d\n", cxl_num_decoders_committed(port));
568 }
569 
570 static DEVICE_ATTR_RO(decoders_committed);
571 
572 static struct attribute *cxl_port_attrs[] = {
573 	&dev_attr_decoders_committed.attr,
574 	NULL,
575 };
576 
577 static struct attribute_group cxl_port_attribute_group = {
578 	.attrs = cxl_port_attrs,
579 };
580 
581 static const struct attribute_group *cxl_port_attribute_groups[] = {
582 	&cxl_base_attribute_group,
583 	&cxl_port_attribute_group,
584 	NULL,
585 };
586 
587 static const struct device_type cxl_port_type = {
588 	.name = "cxl_port",
589 	.release = cxl_port_release,
590 	.groups = cxl_port_attribute_groups,
591 };
592 
is_cxl_port(const struct device * dev)593 bool is_cxl_port(const struct device *dev)
594 {
595 	return dev->type == &cxl_port_type;
596 }
597 EXPORT_SYMBOL_NS_GPL(is_cxl_port, "CXL");
598 
to_cxl_port(const struct device * dev)599 struct cxl_port *to_cxl_port(const struct device *dev)
600 {
601 	if (dev_WARN_ONCE(dev, dev->type != &cxl_port_type,
602 			  "not a cxl_port device\n"))
603 		return NULL;
604 	return container_of(dev, struct cxl_port, dev);
605 }
606 EXPORT_SYMBOL_NS_GPL(to_cxl_port, "CXL");
607 
parent_port_of(struct cxl_port * port)608 struct cxl_port *parent_port_of(struct cxl_port *port)
609 {
610 	if (!port || !port->parent_dport)
611 		return NULL;
612 	return port->parent_dport->port;
613 }
614 
unregister_port(void * _port)615 static void unregister_port(void *_port)
616 {
617 	struct cxl_port *port = _port;
618 	struct cxl_port *parent = parent_port_of(port);
619 	struct device *lock_dev;
620 
621 	/*
622 	 * CXL root port's and the first level of ports are unregistered
623 	 * under the platform firmware device lock, all other ports are
624 	 * unregistered while holding their parent port lock.
625 	 */
626 	if (!parent)
627 		lock_dev = port->uport_dev;
628 	else if (is_cxl_root(parent))
629 		lock_dev = parent->uport_dev;
630 	else
631 		lock_dev = &parent->dev;
632 
633 	device_lock_assert(lock_dev);
634 	port->dead = true;
635 	device_unregister(&port->dev);
636 }
637 
cxl_unlink_uport(void * _port)638 static void cxl_unlink_uport(void *_port)
639 {
640 	struct cxl_port *port = _port;
641 
642 	sysfs_remove_link(&port->dev.kobj, "uport");
643 }
644 
devm_cxl_link_uport(struct device * host,struct cxl_port * port)645 static int devm_cxl_link_uport(struct device *host, struct cxl_port *port)
646 {
647 	int rc;
648 
649 	rc = sysfs_create_link(&port->dev.kobj, &port->uport_dev->kobj,
650 			       "uport");
651 	if (rc)
652 		return rc;
653 	return devm_add_action_or_reset(host, cxl_unlink_uport, port);
654 }
655 
cxl_unlink_parent_dport(void * _port)656 static void cxl_unlink_parent_dport(void *_port)
657 {
658 	struct cxl_port *port = _port;
659 
660 	sysfs_remove_link(&port->dev.kobj, "parent_dport");
661 }
662 
devm_cxl_link_parent_dport(struct device * host,struct cxl_port * port,struct cxl_dport * parent_dport)663 static int devm_cxl_link_parent_dport(struct device *host,
664 				      struct cxl_port *port,
665 				      struct cxl_dport *parent_dport)
666 {
667 	int rc;
668 
669 	if (!parent_dport)
670 		return 0;
671 
672 	rc = sysfs_create_link(&port->dev.kobj, &parent_dport->dport_dev->kobj,
673 			       "parent_dport");
674 	if (rc)
675 		return rc;
676 	return devm_add_action_or_reset(host, cxl_unlink_parent_dport, port);
677 }
678 
679 static struct lock_class_key cxl_port_key;
680 
cxl_port_alloc(struct device * uport_dev,struct cxl_dport * parent_dport)681 static struct cxl_port *cxl_port_alloc(struct device *uport_dev,
682 				       struct cxl_dport *parent_dport)
683 {
684 	struct cxl_root *cxl_root __free(kfree) = NULL;
685 	struct cxl_port *port, *_port __free(kfree) = NULL;
686 	struct device *dev;
687 	int rc;
688 
689 	/* No parent_dport, root cxl_port */
690 	if (!parent_dport) {
691 		cxl_root = kzalloc_obj(*cxl_root);
692 		if (!cxl_root)
693 			return ERR_PTR(-ENOMEM);
694 	} else {
695 		_port = kzalloc_obj(*port);
696 		if (!_port)
697 			return ERR_PTR(-ENOMEM);
698 	}
699 
700 	rc = ida_alloc(&cxl_port_ida, GFP_KERNEL);
701 	if (rc < 0)
702 		return ERR_PTR(rc);
703 
704 	if (cxl_root)
705 		port = &no_free_ptr(cxl_root)->port;
706 	else
707 		port = no_free_ptr(_port);
708 
709 	port->id = rc;
710 	port->uport_dev = uport_dev;
711 
712 	/*
713 	 * The top-level cxl_port "cxl_root" does not have a cxl_port as
714 	 * its parent and it does not have any corresponding component
715 	 * registers as its decode is described by a fixed platform
716 	 * description.
717 	 */
718 	dev = &port->dev;
719 	if (parent_dport) {
720 		struct cxl_port *parent_port = parent_dport->port;
721 		struct cxl_port *iter;
722 
723 		dev->parent = &parent_port->dev;
724 		port->depth = parent_port->depth + 1;
725 		port->parent_dport = parent_dport;
726 
727 		/*
728 		 * walk to the host bridge, or the first ancestor that knows
729 		 * the host bridge
730 		 */
731 		iter = port;
732 		while (!iter->host_bridge &&
733 		       !is_cxl_root(to_cxl_port(iter->dev.parent)))
734 			iter = to_cxl_port(iter->dev.parent);
735 		if (iter->host_bridge)
736 			port->host_bridge = iter->host_bridge;
737 		else if (parent_dport->rch)
738 			port->host_bridge = parent_dport->dport_dev;
739 		else
740 			port->host_bridge = iter->uport_dev;
741 		dev_dbg(uport_dev, "host-bridge: %s\n",
742 			dev_name(port->host_bridge));
743 	} else
744 		dev->parent = uport_dev;
745 
746 	ida_init(&port->decoder_ida);
747 	port->hdm_end = -1;
748 	port->commit_end = -1;
749 	xa_init(&port->dports);
750 	xa_init(&port->endpoints);
751 	xa_init(&port->regions);
752 	port->component_reg_phys = CXL_RESOURCE_NONE;
753 
754 	device_initialize(dev);
755 	lockdep_set_class_and_subclass(&dev->mutex, &cxl_port_key, port->depth);
756 	device_set_pm_not_required(dev);
757 	dev->bus = &cxl_bus_type;
758 	dev->type = &cxl_port_type;
759 
760 	return port;
761 }
762 
cxl_setup_comp_regs(struct device * host,struct cxl_register_map * map,resource_size_t component_reg_phys)763 static int cxl_setup_comp_regs(struct device *host, struct cxl_register_map *map,
764 			       resource_size_t component_reg_phys)
765 {
766 	*map = (struct cxl_register_map) {
767 		.host = host,
768 		.reg_type = CXL_REGLOC_RBI_EMPTY,
769 		.resource = component_reg_phys,
770 	};
771 
772 	if (component_reg_phys == CXL_RESOURCE_NONE)
773 		return 0;
774 
775 	map->reg_type = CXL_REGLOC_RBI_COMPONENT;
776 	map->max_size = CXL_COMPONENT_REG_BLOCK_SIZE;
777 
778 	return cxl_setup_regs(map);
779 }
780 
cxl_port_setup_regs(struct cxl_port * port,resource_size_t component_reg_phys)781 int cxl_port_setup_regs(struct cxl_port *port,
782 			resource_size_t component_reg_phys)
783 {
784 	if (dev_is_platform(port->uport_dev))
785 		return 0;
786 	return cxl_setup_comp_regs(&port->dev, &port->reg_map,
787 				   component_reg_phys);
788 }
789 EXPORT_SYMBOL_NS_GPL(cxl_port_setup_regs, "CXL");
790 
cxl_dport_setup_regs(struct device * host,struct cxl_dport * dport,resource_size_t component_reg_phys)791 static int cxl_dport_setup_regs(struct device *host, struct cxl_dport *dport,
792 				resource_size_t component_reg_phys)
793 {
794 	int rc;
795 
796 	if (dev_is_platform(dport->dport_dev))
797 		return 0;
798 
799 	/*
800 	 * use @dport->dport_dev for the context for error messages during
801 	 * register probing, and fixup @host after the fact, since @host may be
802 	 * NULL.
803 	 */
804 	rc = cxl_setup_comp_regs(dport->dport_dev, &dport->reg_map,
805 				 component_reg_phys);
806 	dport->reg_map.host = host;
807 	return rc;
808 }
809 
810 DEFINE_SHOW_ATTRIBUTE(einj_cxl_available_error_type);
811 
cxl_einj_inject(void * data,u64 type)812 static int cxl_einj_inject(void *data, u64 type)
813 {
814 	struct cxl_dport *dport = data;
815 
816 	if (dport->rch)
817 		return einj_cxl_inject_rch_error(dport->rcrb.base, type);
818 
819 	return einj_cxl_inject_error(to_pci_dev(dport->dport_dev), type);
820 }
821 DEFINE_DEBUGFS_ATTRIBUTE(cxl_einj_inject_fops, NULL, cxl_einj_inject,
822 			 "0x%llx\n");
823 
cxl_debugfs_create_dport_dir(struct cxl_dport * dport)824 static void cxl_debugfs_create_dport_dir(struct cxl_dport *dport)
825 {
826 	struct cxl_port *parent = parent_port_of(dport->port);
827 	struct dentry *dir;
828 
829 	if (!einj_cxl_is_initialized())
830 		return;
831 
832 	/*
833 	 * Protocol error injection is only available for CXL 2.0+ root ports
834 	 * and CXL 1.1 downstream ports
835 	 */
836 	if (!dport->rch &&
837 	    !(dev_is_pci(dport->dport_dev) && parent && is_cxl_root(parent)))
838 		return;
839 
840 	dir = cxl_debugfs_create_dir(dev_name(dport->dport_dev));
841 
842 	debugfs_create_file("einj_inject", 0200, dir, dport,
843 			    &cxl_einj_inject_fops);
844 }
845 
cxl_port_add(struct cxl_port * port,resource_size_t component_reg_phys,struct cxl_dport * parent_dport)846 static int cxl_port_add(struct cxl_port *port,
847 			resource_size_t component_reg_phys,
848 			struct cxl_dport *parent_dport)
849 {
850 	struct device *dev __free(put_device) = &port->dev;
851 	int rc;
852 
853 	if (is_cxl_memdev(port->uport_dev)) {
854 		struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev);
855 		struct cxl_dev_state *cxlds = cxlmd->cxlds;
856 
857 		rc = dev_set_name(dev, "endpoint%d", port->id);
858 		if (rc)
859 			return rc;
860 
861 		/*
862 		 * The endpoint driver already enumerated the component and RAS
863 		 * registers. Reuse that enumeration while prepping them to be
864 		 * mapped by the cxl_port driver.
865 		 */
866 		port->reg_map = cxlds->reg_map;
867 		port->reg_map.host = &port->dev;
868 		cxlmd->endpoint = port;
869 	} else if (parent_dport) {
870 		rc = dev_set_name(dev, "port%d", port->id);
871 		if (rc)
872 			return rc;
873 
874 		port->component_reg_phys = component_reg_phys;
875 	} else {
876 		rc = dev_set_name(dev, "root%d", port->id);
877 		if (rc)
878 			return rc;
879 	}
880 
881 	rc = device_add(dev);
882 	if (rc)
883 		return rc;
884 
885 	/* Inhibit the cleanup function invoked */
886 	dev = NULL;
887 	return 0;
888 }
889 
__devm_cxl_add_port(struct device * host,struct device * uport_dev,resource_size_t component_reg_phys,struct cxl_dport * parent_dport)890 static struct cxl_port *__devm_cxl_add_port(struct device *host,
891 					    struct device *uport_dev,
892 					    resource_size_t component_reg_phys,
893 					    struct cxl_dport *parent_dport)
894 {
895 	struct cxl_port *port;
896 	int rc;
897 
898 	port = cxl_port_alloc(uport_dev, parent_dport);
899 	if (IS_ERR(port))
900 		return port;
901 
902 	rc = cxl_port_add(port, component_reg_phys, parent_dport);
903 	if (rc)
904 		return ERR_PTR(rc);
905 
906 	rc = devm_add_action_or_reset(host, unregister_port, port);
907 	if (rc)
908 		return ERR_PTR(rc);
909 
910 	rc = devm_cxl_link_uport(host, port);
911 	if (rc)
912 		return ERR_PTR(rc);
913 
914 	rc = devm_cxl_link_parent_dport(host, port, parent_dport);
915 	if (rc)
916 		return ERR_PTR(rc);
917 
918 	if (parent_dport && dev_is_pci(uport_dev))
919 		port->pci_latency = cxl_pci_get_latency(to_pci_dev(uport_dev));
920 
921 	return port;
922 }
923 
924 /**
925  * devm_cxl_add_port - register a cxl_port in CXL memory decode hierarchy
926  * @host: host device for devm operations
927  * @uport_dev: "physical" device implementing this upstream port
928  * @component_reg_phys: (optional) for configurable cxl_port instances
929  * @parent_dport: next hop up in the CXL memory decode hierarchy
930  */
devm_cxl_add_port(struct device * host,struct device * uport_dev,resource_size_t component_reg_phys,struct cxl_dport * parent_dport)931 struct cxl_port *devm_cxl_add_port(struct device *host,
932 				   struct device *uport_dev,
933 				   resource_size_t component_reg_phys,
934 				   struct cxl_dport *parent_dport)
935 {
936 	struct cxl_port *port, *parent_port;
937 
938 	port = __devm_cxl_add_port(host, uport_dev, component_reg_phys,
939 				   parent_dport);
940 
941 	parent_port = parent_dport ? parent_dport->port : NULL;
942 	if (IS_ERR(port)) {
943 		dev_dbg(uport_dev, "Failed to add%s%s%s: %ld\n",
944 			parent_port ? " port to " : "",
945 			parent_port ? dev_name(&parent_port->dev) : "",
946 			parent_port ? "" : " root port",
947 			PTR_ERR(port));
948 	} else {
949 		dev_dbg(uport_dev, "%s added%s%s%s\n",
950 			dev_name(&port->dev),
951 			parent_port ? " to " : "",
952 			parent_port ? dev_name(&parent_port->dev) : "",
953 			parent_port ? "" : " (root port)");
954 	}
955 
956 	return port;
957 }
958 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_port, "CXL");
959 
devm_cxl_add_root(struct device * host)960 struct cxl_root *devm_cxl_add_root(struct device *host)
961 {
962 	struct cxl_port *port;
963 
964 	port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL);
965 	if (IS_ERR(port))
966 		return ERR_CAST(port);
967 
968 	return to_cxl_root(port);
969 }
970 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_root, "CXL");
971 
cxl_port_to_pci_bus(struct cxl_port * port)972 struct pci_bus *cxl_port_to_pci_bus(struct cxl_port *port)
973 {
974 	/* There is no pci_bus associated with a CXL platform-root port */
975 	if (is_cxl_root(port))
976 		return NULL;
977 
978 	if (dev_is_pci(port->uport_dev)) {
979 		struct pci_dev *pdev = to_pci_dev(port->uport_dev);
980 
981 		return pdev->subordinate;
982 	}
983 
984 	return xa_load(&cxl_root_buses, (unsigned long)port->uport_dev);
985 }
986 EXPORT_SYMBOL_NS_GPL(cxl_port_to_pci_bus, "CXL");
987 
unregister_pci_bus(void * uport_dev)988 static void unregister_pci_bus(void *uport_dev)
989 {
990 	xa_erase(&cxl_root_buses, (unsigned long)uport_dev);
991 }
992 
devm_cxl_register_pci_bus(struct device * host,struct device * uport_dev,struct pci_bus * bus)993 int devm_cxl_register_pci_bus(struct device *host, struct device *uport_dev,
994 			      struct pci_bus *bus)
995 {
996 	int rc;
997 
998 	if (dev_is_pci(uport_dev))
999 		return -EINVAL;
1000 
1001 	rc = xa_insert(&cxl_root_buses, (unsigned long)uport_dev, bus,
1002 		       GFP_KERNEL);
1003 	if (rc)
1004 		return rc;
1005 	return devm_add_action_or_reset(host, unregister_pci_bus, uport_dev);
1006 }
1007 EXPORT_SYMBOL_NS_GPL(devm_cxl_register_pci_bus, "CXL");
1008 
dev_is_cxl_root_child(struct device * dev)1009 static bool dev_is_cxl_root_child(struct device *dev)
1010 {
1011 	struct cxl_port *port, *parent;
1012 
1013 	if (!is_cxl_port(dev))
1014 		return false;
1015 
1016 	port = to_cxl_port(dev);
1017 	if (is_cxl_root(port))
1018 		return false;
1019 
1020 	parent = to_cxl_port(port->dev.parent);
1021 	if (is_cxl_root(parent))
1022 		return true;
1023 
1024 	return false;
1025 }
1026 
find_cxl_root(struct cxl_port * port)1027 struct cxl_root *find_cxl_root(struct cxl_port *port)
1028 {
1029 	struct cxl_port *iter = port;
1030 
1031 	while (iter && !is_cxl_root(iter))
1032 		iter = to_cxl_port(iter->dev.parent);
1033 
1034 	if (!iter)
1035 		return NULL;
1036 	get_device(&iter->dev);
1037 	return to_cxl_root(iter);
1038 }
1039 EXPORT_SYMBOL_NS_GPL(find_cxl_root, "CXL");
1040 
find_dport(struct cxl_port * port,int id)1041 static struct cxl_dport *find_dport(struct cxl_port *port, int id)
1042 {
1043 	struct cxl_dport *dport;
1044 	unsigned long index;
1045 
1046 	device_lock_assert(&port->dev);
1047 	xa_for_each(&port->dports, index, dport)
1048 		if (dport->port_id == id)
1049 			return dport;
1050 	return NULL;
1051 }
1052 
add_dport(struct cxl_port * port,struct cxl_dport * dport)1053 static int add_dport(struct cxl_port *port, struct cxl_dport *dport)
1054 {
1055 	struct cxl_dport *dup;
1056 	int rc;
1057 
1058 	device_lock_assert(&port->dev);
1059 	dup = find_dport(port, dport->port_id);
1060 	if (dup) {
1061 		dev_err(&port->dev,
1062 			"unable to add dport%d-%s non-unique port id (%s)\n",
1063 			dport->port_id, dev_name(dport->dport_dev),
1064 			dev_name(dup->dport_dev));
1065 		return -EBUSY;
1066 	}
1067 
1068 	/* Arrange for dport_dev to be valid through remove_dport() */
1069 	struct device *dev __free(put_device) = get_device(dport->dport_dev);
1070 
1071 	rc = xa_insert(&port->dports, (unsigned long)dport->dport_dev, dport,
1072 		       GFP_KERNEL);
1073 	if (rc)
1074 		return rc;
1075 
1076 	retain_and_null_ptr(dev);
1077 	port->nr_dports++;
1078 	return 0;
1079 }
1080 
1081 /*
1082  * Since root-level CXL dports cannot be enumerated by PCI they are not
1083  * enumerated by the common port driver that acquires the port lock over
1084  * dport add/remove. Instead, root dports are manually added by a
1085  * platform driver and cond_cxl_root_lock() is used to take the missing
1086  * port lock in that case.
1087  */
cond_cxl_root_lock(struct cxl_port * port)1088 static void cond_cxl_root_lock(struct cxl_port *port)
1089 {
1090 	if (is_cxl_root(port))
1091 		device_lock(&port->dev);
1092 }
1093 
cond_cxl_root_unlock(struct cxl_port * port)1094 static void cond_cxl_root_unlock(struct cxl_port *port)
1095 {
1096 	if (is_cxl_root(port))
1097 		device_unlock(&port->dev);
1098 }
1099 
cxl_dport_remove(void * data)1100 static void cxl_dport_remove(void *data)
1101 {
1102 	struct cxl_dport *dport = data;
1103 	struct cxl_port *port = dport->port;
1104 
1105 	port->nr_dports--;
1106 	xa_erase(&port->dports, (unsigned long) dport->dport_dev);
1107 	put_device(dport->dport_dev);
1108 }
1109 
cxl_dport_unlink(void * data)1110 static void cxl_dport_unlink(void *data)
1111 {
1112 	struct cxl_dport *dport = data;
1113 	struct cxl_port *port = dport->port;
1114 	char link_name[CXL_TARGET_STRLEN];
1115 
1116 	sprintf(link_name, "dport%d", dport->port_id);
1117 	sysfs_remove_link(&port->dev.kobj, link_name);
1118 }
1119 
free_dport(void * dport)1120 static void free_dport(void *dport)
1121 {
1122 	kfree(dport);
1123 }
1124 
1125 /*
1126  * Upon return either a group is established with one action (free_dport()), or
1127  * no group established and @dport is freed.
1128  */
cxl_dport_open_dr_group_or_free(struct cxl_dport * dport)1129 static void *cxl_dport_open_dr_group_or_free(struct cxl_dport *dport)
1130 {
1131 	int rc;
1132 	struct device *host = dport_to_host(dport);
1133 	void *group = devres_open_group(host, dport, GFP_KERNEL);
1134 
1135 	if (!group) {
1136 		kfree(dport);
1137 		return NULL;
1138 	}
1139 
1140 	rc = devm_add_action_or_reset(host, free_dport, dport);
1141 	if (rc) {
1142 		devres_release_group(host, group);
1143 		return NULL;
1144 	}
1145 
1146 	return group;
1147 }
1148 
cxl_dport_close_dr_group(struct cxl_dport * dport,void * group)1149 static void cxl_dport_close_dr_group(struct cxl_dport *dport, void *group)
1150 {
1151 	devres_close_group(dport_to_host(dport), group);
1152 }
1153 
del_dport(struct cxl_dport * dport)1154 static void del_dport(struct cxl_dport *dport)
1155 {
1156 	devres_release_group(dport_to_host(dport), dport);
1157 }
1158 
1159 /* The dport group id is the dport */
DEFINE_FREE(cxl_dport_release_dr_group,void *,if (_T)del_dport (_T))1160 DEFINE_FREE(cxl_dport_release_dr_group, void *, if (_T) del_dport(_T))
1161 
1162 static struct cxl_dport *
1163 __devm_cxl_add_dport(struct cxl_port *port, struct device *dport_dev,
1164 		     int port_id, resource_size_t component_reg_phys,
1165 		     resource_size_t rcrb)
1166 {
1167 	char link_name[CXL_TARGET_STRLEN];
1168 	struct cxl_dport *dport;
1169 	struct device *host;
1170 	int rc;
1171 
1172 	if (is_cxl_root(port))
1173 		host = port->uport_dev;
1174 	else
1175 		host = &port->dev;
1176 
1177 	if (!host->driver) {
1178 		dev_WARN_ONCE(&port->dev, 1, "dport:%s bad devm context\n",
1179 			      dev_name(dport_dev));
1180 		return ERR_PTR(-ENXIO);
1181 	}
1182 
1183 	if (snprintf(link_name, CXL_TARGET_STRLEN, "dport%d", port_id) >=
1184 	    CXL_TARGET_STRLEN)
1185 		return ERR_PTR(-EINVAL);
1186 
1187 	dport = kzalloc_obj(*dport);
1188 	if (!dport)
1189 		return ERR_PTR(-ENOMEM);
1190 
1191 	/* Just enough init to manage the devres group */
1192 	dport->dport_dev = dport_dev;
1193 	dport->port_id = port_id;
1194 	dport->port = port;
1195 
1196 	void *dport_dr_group __free(cxl_dport_release_dr_group) =
1197 		cxl_dport_open_dr_group_or_free(dport);
1198 	if (!dport_dr_group)
1199 		return ERR_PTR(-ENOMEM);
1200 
1201 	if (rcrb == CXL_RESOURCE_NONE) {
1202 		rc = cxl_dport_setup_regs(&port->dev, dport,
1203 					  component_reg_phys);
1204 		if (rc)
1205 			return ERR_PTR(rc);
1206 	} else {
1207 		dport->rcrb.base = rcrb;
1208 		component_reg_phys = __rcrb_to_component(dport_dev, &dport->rcrb,
1209 							 CXL_RCRB_DOWNSTREAM);
1210 		if (component_reg_phys == CXL_RESOURCE_NONE) {
1211 			dev_warn(dport_dev, "Invalid Component Registers in RCRB");
1212 			return ERR_PTR(-ENXIO);
1213 		}
1214 
1215 		/*
1216 		 * RCH @dport is not ready to map until associated with its
1217 		 * memdev
1218 		 */
1219 		rc = cxl_dport_setup_regs(NULL, dport, component_reg_phys);
1220 		if (rc)
1221 			return ERR_PTR(rc);
1222 
1223 		dport->rch = true;
1224 	}
1225 
1226 	if (component_reg_phys != CXL_RESOURCE_NONE)
1227 		dev_dbg(dport_dev, "Component Registers found for dport: %pa\n",
1228 			&component_reg_phys);
1229 
1230 	cond_cxl_root_lock(port);
1231 	rc = add_dport(port, dport);
1232 	cond_cxl_root_unlock(port);
1233 	if (rc)
1234 		return ERR_PTR(rc);
1235 
1236 	rc = devm_add_action_or_reset(host, cxl_dport_remove, dport);
1237 	if (rc)
1238 		return ERR_PTR(rc);
1239 
1240 	rc = sysfs_create_link(&port->dev.kobj, &dport_dev->kobj, link_name);
1241 	if (rc)
1242 		return ERR_PTR(rc);
1243 
1244 	rc = devm_add_action_or_reset(host, cxl_dport_unlink, dport);
1245 	if (rc)
1246 		return ERR_PTR(rc);
1247 
1248 	if (dev_is_pci(dport_dev))
1249 		dport->link_latency = cxl_pci_get_latency(to_pci_dev(dport_dev));
1250 
1251 	cxl_debugfs_create_dport_dir(dport);
1252 
1253 	if (!dport->rch)
1254 		devm_cxl_dport_ras_setup(dport);
1255 
1256 	/* keep the group, and mark the end of devm actions */
1257 	cxl_dport_close_dr_group(dport, no_free_ptr(dport_dr_group));
1258 
1259 	return dport;
1260 }
1261 
1262 /**
1263  * devm_cxl_add_dport - append VH downstream port data to a cxl_port
1264  * @port: the cxl_port that references this dport
1265  * @dport_dev: firmware or PCI device representing the dport
1266  * @port_id: identifier for this dport in a decoder's target list
1267  * @component_reg_phys: optional location of CXL component registers
1268  *
1269  * Note that dports are appended to the devm release action's of the
1270  * either the port's host (for root ports), or the port itself (for
1271  * switch ports)
1272  */
devm_cxl_add_dport(struct cxl_port * port,struct device * dport_dev,int port_id,resource_size_t component_reg_phys)1273 struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port,
1274 				     struct device *dport_dev, int port_id,
1275 				     resource_size_t component_reg_phys)
1276 {
1277 	struct cxl_dport *dport;
1278 
1279 	dport = __devm_cxl_add_dport(port, dport_dev, port_id,
1280 				     component_reg_phys, CXL_RESOURCE_NONE);
1281 	if (IS_ERR(dport)) {
1282 		dev_dbg(dport_dev, "failed to add dport to %s: %ld\n",
1283 			dev_name(&port->dev), PTR_ERR(dport));
1284 	} else {
1285 		dev_dbg(dport_dev, "dport added to %s\n",
1286 			dev_name(&port->dev));
1287 	}
1288 
1289 	return dport;
1290 }
1291 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_dport, "CXL");
1292 
1293 /**
1294  * devm_cxl_add_rch_dport - append RCH downstream port data to a cxl_port
1295  * @port: the cxl_port that references this dport
1296  * @dport_dev: firmware or PCI device representing the dport
1297  * @port_id: identifier for this dport in a decoder's target list
1298  * @rcrb: mandatory location of a Root Complex Register Block
1299  *
1300  * See CXL 3.0 9.11.8 CXL Devices Attached to an RCH
1301  */
devm_cxl_add_rch_dport(struct cxl_port * port,struct device * dport_dev,int port_id,resource_size_t rcrb)1302 struct cxl_dport *devm_cxl_add_rch_dport(struct cxl_port *port,
1303 					 struct device *dport_dev, int port_id,
1304 					 resource_size_t rcrb)
1305 {
1306 	struct cxl_dport *dport;
1307 
1308 	if (rcrb == CXL_RESOURCE_NONE) {
1309 		dev_dbg(&port->dev, "failed to add RCH dport, missing RCRB\n");
1310 		return ERR_PTR(-EINVAL);
1311 	}
1312 
1313 	dport = __devm_cxl_add_dport(port, dport_dev, port_id,
1314 				     CXL_RESOURCE_NONE, rcrb);
1315 	if (IS_ERR(dport)) {
1316 		dev_dbg(dport_dev, "failed to add RCH dport to %s: %ld\n",
1317 			dev_name(&port->dev), PTR_ERR(dport));
1318 	} else {
1319 		dev_dbg(dport_dev, "RCH dport added to %s\n",
1320 			dev_name(&port->dev));
1321 	}
1322 
1323 	return dport;
1324 }
1325 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_rch_dport, "CXL");
1326 
add_ep(struct cxl_ep * new)1327 static int add_ep(struct cxl_ep *new)
1328 {
1329 	struct cxl_port *port = new->dport->port;
1330 
1331 	guard(device)(&port->dev);
1332 	if (port->dead)
1333 		return -ENXIO;
1334 
1335 	return xa_insert(&port->endpoints, (unsigned long)new->ep,
1336 			 new, GFP_KERNEL);
1337 }
1338 
1339 /**
1340  * cxl_add_ep - register an endpoint's interest in a port
1341  * @dport: the dport that routes to @ep_dev
1342  * @ep_dev: device representing the endpoint
1343  *
1344  * Intermediate CXL ports are scanned based on the arrival of endpoints.
1345  * When those endpoints depart the port can be destroyed once all
1346  * endpoints that care about that port have been removed.
1347  */
cxl_add_ep(struct cxl_dport * dport,struct device * ep_dev)1348 static int cxl_add_ep(struct cxl_dport *dport, struct device *ep_dev)
1349 {
1350 	struct cxl_ep *ep;
1351 	int rc;
1352 
1353 	ep = kzalloc_obj(*ep);
1354 	if (!ep)
1355 		return -ENOMEM;
1356 
1357 	ep->ep = get_device(ep_dev);
1358 	ep->dport = dport;
1359 
1360 	rc = add_ep(ep);
1361 	if (rc)
1362 		cxl_ep_release(ep);
1363 	return rc;
1364 }
1365 
1366 struct cxl_find_port_ctx {
1367 	const struct device *dport_dev;
1368 	const struct cxl_port *parent_port;
1369 	struct cxl_dport **dport;
1370 };
1371 
match_port_by_dport(struct device * dev,const void * data)1372 static int match_port_by_dport(struct device *dev, const void *data)
1373 {
1374 	const struct cxl_find_port_ctx *ctx = data;
1375 	struct cxl_dport *dport;
1376 	struct cxl_port *port;
1377 
1378 	if (!is_cxl_port(dev))
1379 		return 0;
1380 	if (ctx->parent_port && dev->parent != &ctx->parent_port->dev)
1381 		return 0;
1382 
1383 	port = to_cxl_port(dev);
1384 	dport = cxl_find_dport_by_dev(port, ctx->dport_dev);
1385 	if (ctx->dport)
1386 		*ctx->dport = dport;
1387 	return dport != NULL;
1388 }
1389 
__find_cxl_port(struct cxl_find_port_ctx * ctx)1390 static struct cxl_port *__find_cxl_port(struct cxl_find_port_ctx *ctx)
1391 {
1392 	struct device *dev;
1393 
1394 	if (!ctx->dport_dev)
1395 		return NULL;
1396 
1397 	dev = bus_find_device(&cxl_bus_type, NULL, ctx, match_port_by_dport);
1398 	if (dev)
1399 		return to_cxl_port(dev);
1400 	return NULL;
1401 }
1402 
find_cxl_port(struct device * dport_dev,struct cxl_dport ** dport)1403 static struct cxl_port *find_cxl_port(struct device *dport_dev,
1404 				      struct cxl_dport **dport)
1405 {
1406 	struct cxl_find_port_ctx ctx = {
1407 		.dport_dev = dport_dev,
1408 		.dport = dport,
1409 	};
1410 	struct cxl_port *port;
1411 
1412 	port = __find_cxl_port(&ctx);
1413 	return port;
1414 }
1415 
1416 /*
1417  * All users of grandparent() are using it to walk PCIe-like switch port
1418  * hierarchy. A PCIe switch is comprised of a bridge device representing the
1419  * upstream switch port and N bridges representing downstream switch ports. When
1420  * bridges stack the grand-parent of a downstream switch port is another
1421  * downstream switch port in the immediate ancestor switch.
1422  */
grandparent(struct device * dev)1423 static struct device *grandparent(struct device *dev)
1424 {
1425 	if (dev && dev->parent)
1426 		return dev->parent->parent;
1427 	return NULL;
1428 }
1429 
endpoint_host(struct cxl_port * endpoint)1430 static struct device *endpoint_host(struct cxl_port *endpoint)
1431 {
1432 	struct cxl_port *port = to_cxl_port(endpoint->dev.parent);
1433 
1434 	if (is_cxl_root(port))
1435 		return port->uport_dev;
1436 	return &port->dev;
1437 }
1438 
delete_endpoint(void * data)1439 static void delete_endpoint(void *data)
1440 {
1441 	struct cxl_memdev *cxlmd = data;
1442 	struct cxl_port *endpoint = cxlmd->endpoint;
1443 	struct device *host = endpoint_host(endpoint);
1444 
1445 	scoped_guard(device, host) {
1446 		if (host->driver && !endpoint->dead) {
1447 			devm_release_action(host, cxl_unlink_parent_dport, endpoint);
1448 			devm_release_action(host, cxl_unlink_uport, endpoint);
1449 			devm_release_action(host, unregister_port, endpoint);
1450 		}
1451 		cxlmd->endpoint = NULL;
1452 	}
1453 	put_device(&endpoint->dev);
1454 	put_device(host);
1455 }
1456 
cxl_endpoint_autoremove(struct cxl_memdev * cxlmd,struct cxl_port * endpoint)1457 int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint)
1458 {
1459 	struct device *host = endpoint_host(endpoint);
1460 	struct device *dev = &cxlmd->dev;
1461 
1462 	get_device(host);
1463 	get_device(&endpoint->dev);
1464 	cxlmd->depth = endpoint->depth;
1465 	return devm_add_action_or_reset(dev, delete_endpoint, cxlmd);
1466 }
1467 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_autoremove, "CXL");
1468 
1469 /*
1470  * The natural end of life of a non-root 'cxl_port' is when its parent port goes
1471  * through a ->remove() event ("top-down" unregistration). The unnatural trigger
1472  * for a port to be unregistered is when all memdevs beneath that port have gone
1473  * through ->remove(). This "bottom-up" removal selectively removes individual
1474  * child ports manually. This depends on devm_cxl_add_port() to not change is
1475  * devm action registration order, and for dports to have already been
1476  * destroyed by del_dports().
1477  */
delete_switch_port(struct cxl_port * port)1478 static void delete_switch_port(struct cxl_port *port)
1479 {
1480 	devm_release_action(port->dev.parent, cxl_unlink_parent_dport, port);
1481 	devm_release_action(port->dev.parent, cxl_unlink_uport, port);
1482 	devm_release_action(port->dev.parent, unregister_port, port);
1483 }
1484 
del_dports(struct cxl_port * port)1485 static void del_dports(struct cxl_port *port)
1486 {
1487 	struct cxl_dport *dport;
1488 	unsigned long index;
1489 
1490 	device_lock_assert(&port->dev);
1491 
1492 	xa_for_each(&port->dports, index, dport)
1493 		del_dport(dport);
1494 }
1495 
1496 struct detach_ctx {
1497 	struct cxl_memdev *cxlmd;
1498 	int depth;
1499 };
1500 
port_has_memdev(struct device * dev,const void * data)1501 static int port_has_memdev(struct device *dev, const void *data)
1502 {
1503 	const struct detach_ctx *ctx = data;
1504 	struct cxl_port *port;
1505 
1506 	if (!is_cxl_port(dev))
1507 		return 0;
1508 
1509 	port = to_cxl_port(dev);
1510 	if (port->depth != ctx->depth)
1511 		return 0;
1512 
1513 	return !!cxl_ep_load(port, ctx->cxlmd);
1514 }
1515 
cxl_detach_ep(void * data)1516 static void cxl_detach_ep(void *data)
1517 {
1518 	struct cxl_memdev *cxlmd = data;
1519 
1520 	for (int i = cxlmd->depth - 1; i >= 1; i--) {
1521 		struct cxl_port *port, *parent_port;
1522 		struct detach_ctx ctx = {
1523 			.cxlmd = cxlmd,
1524 			.depth = i,
1525 		};
1526 		struct cxl_ep *ep;
1527 		bool died = false;
1528 
1529 		struct device *dev __free(put_device) =
1530 			bus_find_device(&cxl_bus_type, NULL, &ctx, port_has_memdev);
1531 		if (!dev)
1532 			continue;
1533 		port = to_cxl_port(dev);
1534 
1535 		parent_port = to_cxl_port(port->dev.parent);
1536 		device_lock(&parent_port->dev);
1537 		device_lock(&port->dev);
1538 		ep = cxl_ep_load(port, cxlmd);
1539 		dev_dbg(&cxlmd->dev, "disconnect %s from %s\n",
1540 			ep ? dev_name(ep->ep) : "", dev_name(&port->dev));
1541 		cxl_ep_remove(port, ep);
1542 		if (ep && !port->dead && xa_empty(&port->endpoints) &&
1543 		    !is_cxl_root(parent_port) && parent_port->dev.driver) {
1544 			/*
1545 			 * This was the last ep attached to a dynamically
1546 			 * enumerated port. Block new cxl_add_ep() and garbage
1547 			 * collect the port.
1548 			 */
1549 			died = true;
1550 			port->dead = true;
1551 			del_dports(port);
1552 		}
1553 		device_unlock(&port->dev);
1554 
1555 		if (died) {
1556 			dev_dbg(&cxlmd->dev, "delete %s\n",
1557 				dev_name(&port->dev));
1558 			delete_switch_port(port);
1559 		}
1560 		device_unlock(&parent_port->dev);
1561 	}
1562 }
1563 
find_component_registers(struct device * dev)1564 static resource_size_t find_component_registers(struct device *dev)
1565 {
1566 	struct cxl_register_map map;
1567 	struct pci_dev *pdev;
1568 
1569 	/*
1570 	 * Theoretically, CXL component registers can be hosted on a
1571 	 * non-PCI device, in practice, only cxl_test hits this case.
1572 	 */
1573 	if (!dev_is_pci(dev))
1574 		return CXL_RESOURCE_NONE;
1575 
1576 	pdev = to_pci_dev(dev);
1577 
1578 	cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
1579 	return map.resource;
1580 }
1581 
match_port_by_uport(struct device * dev,const void * data)1582 static int match_port_by_uport(struct device *dev, const void *data)
1583 {
1584 	const struct device *uport_dev = data;
1585 	struct cxl_port *port;
1586 
1587 	if (!is_cxl_port(dev))
1588 		return 0;
1589 
1590 	port = to_cxl_port(dev);
1591 	/* Endpoint ports are hosted by memdevs */
1592 	if (is_cxl_memdev(port->uport_dev))
1593 		return uport_dev == port->uport_dev->parent;
1594 	return uport_dev == port->uport_dev;
1595 }
1596 
1597 /**
1598  * find_cxl_port_by_uport - Find a CXL port device companion
1599  * @uport_dev: Device that acts as a switch or endpoint in the CXL hierarchy
1600  *
1601  * In the case of endpoint ports recall that port->uport_dev points to a 'struct
1602  * cxl_memdev' device. So, the @uport_dev argument is the parent device of the
1603  * 'struct cxl_memdev' in that case.
1604  *
1605  * Function takes a device reference on the port device. Caller should do a
1606  * put_device() when done.
1607  */
find_cxl_port_by_uport(struct device * uport_dev)1608 static struct cxl_port *find_cxl_port_by_uport(struct device *uport_dev)
1609 {
1610 	struct device *dev;
1611 
1612 	dev = bus_find_device(&cxl_bus_type, NULL, uport_dev, match_port_by_uport);
1613 	if (dev)
1614 		return to_cxl_port(dev);
1615 	return NULL;
1616 }
1617 
update_decoder_targets(struct device * dev,void * data)1618 static int update_decoder_targets(struct device *dev, void *data)
1619 {
1620 	struct cxl_dport *dport = data;
1621 	struct cxl_switch_decoder *cxlsd;
1622 	struct cxl_decoder *cxld;
1623 	int i;
1624 
1625 	if (!is_switch_decoder(dev))
1626 		return 0;
1627 
1628 	cxlsd = to_cxl_switch_decoder(dev);
1629 	cxld = &cxlsd->cxld;
1630 	guard(rwsem_write)(&cxl_rwsem.region);
1631 
1632 	for (i = 0; i < cxld->interleave_ways; i++) {
1633 		if (cxld->target_map[i] == dport->port_id) {
1634 			cxlsd->target[i] = dport;
1635 			dev_dbg(dev, "dport%d found in target list, index %d\n",
1636 				dport->port_id, i);
1637 			return 0;
1638 		}
1639 	}
1640 
1641 	return 0;
1642 }
1643 
cxl_port_update_decoder_targets(struct cxl_port * port,struct cxl_dport * dport)1644 void cxl_port_update_decoder_targets(struct cxl_port *port,
1645 				     struct cxl_dport *dport)
1646 {
1647 	device_for_each_child(&port->dev, dport, update_decoder_targets);
1648 }
1649 EXPORT_SYMBOL_NS_GPL(cxl_port_update_decoder_targets, "CXL");
1650 
dport_exists(struct cxl_port * port,struct device * dport_dev)1651 static bool dport_exists(struct cxl_port *port, struct device *dport_dev)
1652 {
1653 	struct cxl_dport *dport = cxl_find_dport_by_dev(port, dport_dev);
1654 
1655 	if (dport) {
1656 		dev_dbg(&port->dev, "dport%d:%s already exists\n",
1657 			dport->port_id, dev_name(dport_dev));
1658 		return true;
1659 	}
1660 
1661 	return false;
1662 }
1663 
probe_dport(struct cxl_port * port,struct device * dport_dev)1664 static struct cxl_dport *probe_dport(struct cxl_port *port,
1665 				     struct device *dport_dev)
1666 {
1667 	struct cxl_driver *drv;
1668 
1669 	device_lock_assert(&port->dev);
1670 	if (!port->dev.driver)
1671 		return ERR_PTR(-ENXIO);
1672 
1673 	if (dport_exists(port, dport_dev))
1674 		return ERR_PTR(-EBUSY);
1675 
1676 	drv = container_of(port->dev.driver, struct cxl_driver, drv);
1677 	if (!drv->add_dport)
1678 		return ERR_PTR(-ENXIO);
1679 
1680 	/* see cxl_port_add_dport() */
1681 	return drv->add_dport(port, dport_dev);
1682 }
1683 
devm_cxl_create_port(struct device * ep_dev,struct cxl_port * parent_port,struct cxl_dport * parent_dport,struct device * uport_dev,struct device * dport_dev)1684 static struct cxl_dport *devm_cxl_create_port(struct device *ep_dev,
1685 					      struct cxl_port *parent_port,
1686 					      struct cxl_dport *parent_dport,
1687 					      struct device *uport_dev,
1688 					      struct device *dport_dev)
1689 {
1690 	resource_size_t component_reg_phys;
1691 
1692 	device_lock_assert(&parent_port->dev);
1693 	if (!parent_port->dev.driver) {
1694 		dev_warn(ep_dev,
1695 			 "port %s:%s:%s disabled, failed to enumerate CXL.mem\n",
1696 			 dev_name(&parent_port->dev), dev_name(uport_dev),
1697 			 dev_name(dport_dev));
1698 	}
1699 
1700 	struct cxl_port *port __free(put_cxl_port) =
1701 		find_cxl_port_by_uport(uport_dev);
1702 	if (!port) {
1703 		component_reg_phys = find_component_registers(uport_dev);
1704 		port = devm_cxl_add_port(&parent_port->dev, uport_dev,
1705 					 component_reg_phys, parent_dport);
1706 		if (IS_ERR(port))
1707 			return ERR_CAST(port);
1708 
1709 		/*
1710 		 * retry to make sure a port is found. a port device
1711 		 * reference is taken.
1712 		 */
1713 		port = find_cxl_port_by_uport(uport_dev);
1714 		if (!port)
1715 			return ERR_PTR(-ENODEV);
1716 
1717 		dev_dbg(ep_dev, "created port %s:%s\n",
1718 			dev_name(&port->dev), dev_name(port->uport_dev));
1719 	} else {
1720 		/*
1721 		 * Port was created before right before this function is
1722 		 * called. Signal the caller to deal with it.
1723 		 */
1724 		return ERR_PTR(-EAGAIN);
1725 	}
1726 
1727 	guard(device)(&port->dev);
1728 	return probe_dport(port, dport_dev);
1729 }
1730 
add_port_attach_ep(struct cxl_memdev * cxlmd,struct device * uport_dev,struct device * dport_dev)1731 static int add_port_attach_ep(struct cxl_memdev *cxlmd,
1732 			      struct device *uport_dev,
1733 			      struct device *dport_dev)
1734 {
1735 	struct device *dparent = grandparent(dport_dev);
1736 	struct cxl_dport *dport, *parent_dport;
1737 	int rc;
1738 
1739 	if (is_cxl_host_bridge(dparent)) {
1740 		/*
1741 		 * The iteration reached the topology root without finding the
1742 		 * CXL-root 'cxl_port' on a previous iteration, fail for now to
1743 		 * be re-probed after platform driver attaches.
1744 		 */
1745 		dev_dbg(&cxlmd->dev, "%s is a root dport\n",
1746 			dev_name(dport_dev));
1747 		return -ENXIO;
1748 	}
1749 
1750 	struct cxl_port *parent_port __free(put_cxl_port) =
1751 		find_cxl_port_by_uport(dparent->parent);
1752 	if (!parent_port) {
1753 		/* iterate to create this parent_port */
1754 		return -EAGAIN;
1755 	}
1756 
1757 	scoped_guard(device, &parent_port->dev) {
1758 		parent_dport = cxl_find_dport_by_dev(parent_port, dparent);
1759 		if (!parent_dport) {
1760 			parent_dport = probe_dport(parent_port, dparent);
1761 			if (IS_ERR(parent_dport))
1762 				return PTR_ERR(parent_dport);
1763 		}
1764 
1765 		dport = devm_cxl_create_port(&cxlmd->dev, parent_port,
1766 					     parent_dport, uport_dev,
1767 					     dport_dev);
1768 		if (IS_ERR(dport)) {
1769 			/* Port already exists, restart iteration */
1770 			if (PTR_ERR(dport) == -EAGAIN)
1771 				return 0;
1772 			return PTR_ERR(dport);
1773 		}
1774 	}
1775 
1776 	rc = cxl_add_ep(dport, &cxlmd->dev);
1777 	if (rc == -EBUSY) {
1778 		/*
1779 		 * "can't" happen, but this error code means
1780 		 * something to the caller, so translate it.
1781 		 */
1782 		rc = -ENXIO;
1783 	}
1784 
1785 	return rc;
1786 }
1787 
find_or_add_dport(struct cxl_port * port,struct device * dport_dev)1788 static struct cxl_dport *find_or_add_dport(struct cxl_port *port,
1789 					   struct device *dport_dev)
1790 {
1791 	struct cxl_dport *dport;
1792 
1793 	device_lock_assert(&port->dev);
1794 	dport = cxl_find_dport_by_dev(port, dport_dev);
1795 	if (!dport) {
1796 		dport = probe_dport(port, dport_dev);
1797 		if (IS_ERR(dport))
1798 			return dport;
1799 
1800 		/* New dport added, restart iteration */
1801 		return ERR_PTR(-EAGAIN);
1802 	}
1803 
1804 	return dport;
1805 }
1806 
devm_cxl_enumerate_ports(struct cxl_memdev * cxlmd)1807 int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd)
1808 {
1809 	struct device *dev = &cxlmd->dev;
1810 	struct device *iter;
1811 	int rc;
1812 
1813 	/*
1814 	 * Skip intermediate port enumeration in the RCH case, there
1815 	 * are no ports in between a host bridge and an endpoint.
1816 	 */
1817 	if (cxlmd->cxlds->rcd)
1818 		return 0;
1819 
1820 	rc = devm_add_action_or_reset(&cxlmd->dev, cxl_detach_ep, cxlmd);
1821 	if (rc)
1822 		return rc;
1823 
1824 	/*
1825 	 * Scan for and add all cxl_ports in this device's ancestry.
1826 	 * Repeat until no more ports are added. Abort if a port add
1827 	 * attempt fails.
1828 	 */
1829 retry:
1830 	for (iter = dev; iter; iter = grandparent(iter)) {
1831 		struct device *dport_dev = grandparent(iter);
1832 		struct device *uport_dev;
1833 		struct cxl_dport *dport;
1834 
1835 		if (is_cxl_host_bridge(dport_dev))
1836 			return 0;
1837 
1838 		uport_dev = dport_dev->parent;
1839 		if (!uport_dev) {
1840 			dev_warn(dev, "at %s no parent for dport: %s\n",
1841 				 dev_name(iter), dev_name(dport_dev));
1842 			return -ENXIO;
1843 		}
1844 
1845 		dev_dbg(dev, "scan: iter: %s dport_dev: %s parent: %s\n",
1846 			dev_name(iter), dev_name(dport_dev),
1847 			dev_name(uport_dev));
1848 		struct cxl_port *port __free(put_cxl_port) =
1849 			find_cxl_port_by_uport(uport_dev);
1850 		if (port) {
1851 			dev_dbg(&cxlmd->dev,
1852 				"found already registered port %s:%s\n",
1853 				dev_name(&port->dev),
1854 				dev_name(port->uport_dev));
1855 
1856 			/*
1857 			 * RP port enumerated by cxl_acpi without dport will
1858 			 * have the dport added here.
1859 			 */
1860 			scoped_guard(device, &port->dev) {
1861 				dport = find_or_add_dport(port, dport_dev);
1862 				if (IS_ERR(dport)) {
1863 					if (PTR_ERR(dport) == -EAGAIN)
1864 						goto retry;
1865 					return PTR_ERR(dport);
1866 				}
1867 			}
1868 
1869 			rc = cxl_add_ep(dport, &cxlmd->dev);
1870 
1871 			/*
1872 			 * If the endpoint already exists in the port's list,
1873 			 * that's ok, it was added on a previous pass.
1874 			 * Otherwise, retry in add_port_attach_ep() after taking
1875 			 * the parent_port lock as the current port may be being
1876 			 * reaped.
1877 			 */
1878 			if (rc && rc != -EBUSY)
1879 				return rc;
1880 
1881 			cxl_gpf_port_setup(dport);
1882 
1883 			/* Any more ports to add between this one and the root? */
1884 			if (!dev_is_cxl_root_child(&port->dev))
1885 				continue;
1886 
1887 			return 0;
1888 		}
1889 
1890 		rc = add_port_attach_ep(cxlmd, uport_dev, dport_dev);
1891 		/* port missing, try to add parent */
1892 		if (rc == -EAGAIN)
1893 			continue;
1894 		/* failed to add ep or port */
1895 		if (rc)
1896 			return rc;
1897 		/* port added, new descendants possible, start over */
1898 		goto retry;
1899 	}
1900 
1901 	return 0;
1902 }
1903 EXPORT_SYMBOL_NS_GPL(devm_cxl_enumerate_ports, "CXL");
1904 
cxl_pci_find_port(struct pci_dev * pdev,struct cxl_dport ** dport)1905 struct cxl_port *cxl_pci_find_port(struct pci_dev *pdev,
1906 				   struct cxl_dport **dport)
1907 {
1908 	return find_cxl_port(pdev->dev.parent, dport);
1909 }
1910 EXPORT_SYMBOL_NS_GPL(cxl_pci_find_port, "CXL");
1911 
cxl_mem_find_port(struct cxl_memdev * cxlmd,struct cxl_dport ** dport)1912 struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd,
1913 				   struct cxl_dport **dport)
1914 {
1915 	return find_cxl_port(grandparent(&cxlmd->dev), dport);
1916 }
1917 EXPORT_SYMBOL_NS_GPL(cxl_mem_find_port, "CXL");
1918 
decoder_populate_targets(struct cxl_switch_decoder * cxlsd,struct cxl_port * port)1919 static int decoder_populate_targets(struct cxl_switch_decoder *cxlsd,
1920 				    struct cxl_port *port)
1921 {
1922 	struct cxl_decoder *cxld = &cxlsd->cxld;
1923 	int i;
1924 
1925 	device_lock_assert(&port->dev);
1926 
1927 	if (xa_empty(&port->dports))
1928 		return 0;
1929 
1930 	guard(rwsem_write)(&cxl_rwsem.region);
1931 	for (i = 0; i < cxlsd->cxld.interleave_ways; i++) {
1932 		struct cxl_dport *dport = find_dport(port, cxld->target_map[i]);
1933 
1934 		if (!dport) {
1935 			/* dport may be activated later */
1936 			continue;
1937 		}
1938 		cxlsd->target[i] = dport;
1939 	}
1940 
1941 	return 0;
1942 }
1943 
1944 static struct lock_class_key cxl_decoder_key;
1945 
1946 /**
1947  * cxl_decoder_init - Common decoder setup / initialization
1948  * @port: owning port of this decoder
1949  * @cxld: common decoder properties to initialize
1950  *
1951  * A port may contain one or more decoders. Each of those decoders
1952  * enable some address space for CXL.mem utilization. A decoder is
1953  * expected to be configured by the caller before registering via
1954  * cxl_decoder_add()
1955  */
cxl_decoder_init(struct cxl_port * port,struct cxl_decoder * cxld)1956 static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld)
1957 {
1958 	struct device *dev;
1959 	int rc;
1960 
1961 	rc = ida_alloc(&port->decoder_ida, GFP_KERNEL);
1962 	if (rc < 0)
1963 		return rc;
1964 
1965 	/* need parent to stick around to release the id */
1966 	get_device(&port->dev);
1967 	cxld->id = rc;
1968 
1969 	dev = &cxld->dev;
1970 	device_initialize(dev);
1971 	lockdep_set_class(&dev->mutex, &cxl_decoder_key);
1972 	device_set_pm_not_required(dev);
1973 	dev->parent = &port->dev;
1974 	dev->bus = &cxl_bus_type;
1975 
1976 	/* Pre initialize an "empty" decoder */
1977 	cxld->interleave_ways = 1;
1978 	cxld->interleave_granularity = PAGE_SIZE;
1979 	cxld->target_type = CXL_DECODER_HOSTONLYMEM;
1980 	cxld->hpa_range = (struct range) {
1981 		.start = 0,
1982 		.end = -1,
1983 	};
1984 
1985 	return 0;
1986 }
1987 
cxl_switch_decoder_init(struct cxl_port * port,struct cxl_switch_decoder * cxlsd,int nr_targets)1988 static int cxl_switch_decoder_init(struct cxl_port *port,
1989 				   struct cxl_switch_decoder *cxlsd,
1990 				   int nr_targets)
1991 {
1992 	if (nr_targets > CXL_DECODER_MAX_INTERLEAVE)
1993 		return -EINVAL;
1994 
1995 	cxlsd->nr_targets = nr_targets;
1996 	return cxl_decoder_init(port, &cxlsd->cxld);
1997 }
1998 
1999 /**
2000  * cxl_root_decoder_alloc - Allocate a root level decoder
2001  * @port: owning CXL root of this decoder
2002  * @nr_targets: static number of downstream targets
2003  *
2004  * Return: A new cxl decoder to be registered by cxl_decoder_add(). A
2005  * 'CXL root' decoder is one that decodes from a top-level / static platform
2006  * firmware description of CXL resources into a CXL standard decode
2007  * topology.
2008  */
cxl_root_decoder_alloc(struct cxl_port * port,unsigned int nr_targets)2009 struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
2010 						unsigned int nr_targets)
2011 {
2012 	struct cxl_root_decoder *cxlrd;
2013 	struct cxl_switch_decoder *cxlsd;
2014 	struct cxl_decoder *cxld;
2015 	int rc;
2016 
2017 	if (!is_cxl_root(port))
2018 		return ERR_PTR(-EINVAL);
2019 
2020 	cxlrd = kzalloc_flex(*cxlrd, cxlsd.target, nr_targets);
2021 	if (!cxlrd)
2022 		return ERR_PTR(-ENOMEM);
2023 
2024 	cxlsd = &cxlrd->cxlsd;
2025 	rc = cxl_switch_decoder_init(port, cxlsd, nr_targets);
2026 	if (rc) {
2027 		kfree(cxlrd);
2028 		return ERR_PTR(rc);
2029 	}
2030 
2031 	mutex_init(&cxlrd->range_lock);
2032 
2033 	cxld = &cxlsd->cxld;
2034 	cxld->dev.type = &cxl_decoder_root_type;
2035 	/*
2036 	 * cxl_root_decoder_release() special cases negative ids to
2037 	 * detect memregion_alloc() failures.
2038 	 */
2039 	atomic_set(&cxlrd->region_id, -1);
2040 	rc = memregion_alloc(GFP_KERNEL);
2041 	if (rc < 0) {
2042 		put_device(&cxld->dev);
2043 		return ERR_PTR(rc);
2044 	}
2045 
2046 	atomic_set(&cxlrd->region_id, rc);
2047 	cxlrd->qos_class = CXL_QOS_CLASS_INVALID;
2048 	return cxlrd;
2049 }
2050 EXPORT_SYMBOL_NS_GPL(cxl_root_decoder_alloc, "CXL");
2051 
2052 /**
2053  * cxl_switch_decoder_alloc - Allocate a switch level decoder
2054  * @port: owning CXL switch port of this decoder
2055  * @nr_targets: max number of dynamically addressable downstream targets
2056  *
2057  * Return: A new cxl decoder to be registered by cxl_decoder_add(). A
2058  * 'switch' decoder is any decoder that can be enumerated by PCIe
2059  * topology and the HDM Decoder Capability. This includes the decoders
2060  * that sit between Switch Upstream Ports / Switch Downstream Ports and
2061  * Host Bridges / Root Ports.
2062  */
cxl_switch_decoder_alloc(struct cxl_port * port,unsigned int nr_targets)2063 struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port,
2064 						    unsigned int nr_targets)
2065 {
2066 	struct cxl_switch_decoder *cxlsd;
2067 	struct cxl_decoder *cxld;
2068 	int rc;
2069 
2070 	if (is_cxl_root(port) || is_cxl_endpoint(port))
2071 		return ERR_PTR(-EINVAL);
2072 
2073 	cxlsd = kzalloc_flex(*cxlsd, target, nr_targets);
2074 	if (!cxlsd)
2075 		return ERR_PTR(-ENOMEM);
2076 
2077 	rc = cxl_switch_decoder_init(port, cxlsd, nr_targets);
2078 	if (rc) {
2079 		kfree(cxlsd);
2080 		return ERR_PTR(rc);
2081 	}
2082 
2083 	cxld = &cxlsd->cxld;
2084 	cxld->dev.type = &cxl_decoder_switch_type;
2085 	return cxlsd;
2086 }
2087 EXPORT_SYMBOL_NS_GPL(cxl_switch_decoder_alloc, "CXL");
2088 
2089 /**
2090  * cxl_endpoint_decoder_alloc - Allocate an endpoint decoder
2091  * @port: owning port of this decoder
2092  *
2093  * Return: A new cxl decoder to be registered by cxl_decoder_add()
2094  */
cxl_endpoint_decoder_alloc(struct cxl_port * port)2095 struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port)
2096 {
2097 	struct cxl_endpoint_decoder *cxled;
2098 	struct cxl_decoder *cxld;
2099 	int rc;
2100 
2101 	if (!is_cxl_endpoint(port))
2102 		return ERR_PTR(-EINVAL);
2103 
2104 	cxled = kzalloc_obj(*cxled);
2105 	if (!cxled)
2106 		return ERR_PTR(-ENOMEM);
2107 
2108 	cxled->pos = -1;
2109 	cxled->part = -1;
2110 	cxld = &cxled->cxld;
2111 	rc = cxl_decoder_init(port, cxld);
2112 	if (rc)	 {
2113 		kfree(cxled);
2114 		return ERR_PTR(rc);
2115 	}
2116 
2117 	cxld->dev.type = &cxl_decoder_endpoint_type;
2118 	return cxled;
2119 }
2120 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_decoder_alloc, "CXL");
2121 
2122 /**
2123  * cxl_decoder_add_locked - Add a decoder with targets
2124  * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc()
2125  *
2126  * Certain types of decoders may not have any targets. The main example of this
2127  * is an endpoint device. A more awkward example is a hostbridge whose root
2128  * ports get hot added (technically possible, though unlikely).
2129  *
2130  * This is the locked variant of cxl_decoder_add().
2131  *
2132  * Context: Process context. Expects the device lock of the port that owns the
2133  *	    @cxld to be held.
2134  *
2135  * Return: Negative error code if the decoder wasn't properly configured; else
2136  *	   returns 0.
2137  */
cxl_decoder_add_locked(struct cxl_decoder * cxld)2138 int cxl_decoder_add_locked(struct cxl_decoder *cxld)
2139 {
2140 	struct cxl_port *port;
2141 	struct device *dev;
2142 	int rc;
2143 
2144 	if (WARN_ON_ONCE(!cxld))
2145 		return -EINVAL;
2146 
2147 	if (WARN_ON_ONCE(IS_ERR(cxld)))
2148 		return PTR_ERR(cxld);
2149 
2150 	if (cxld->interleave_ways < 1)
2151 		return -EINVAL;
2152 
2153 	dev = &cxld->dev;
2154 
2155 	port = to_cxl_port(cxld->dev.parent);
2156 	if (!is_endpoint_decoder(dev)) {
2157 		struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
2158 
2159 		rc = decoder_populate_targets(cxlsd, port);
2160 		if (rc && (cxld->flags & CXL_DECODER_F_ENABLE)) {
2161 			dev_err(&port->dev,
2162 				"Failed to populate active decoder targets\n");
2163 			return rc;
2164 		}
2165 	}
2166 
2167 	rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
2168 	if (rc)
2169 		return rc;
2170 
2171 	return device_add(dev);
2172 }
2173 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add_locked, "CXL");
2174 
2175 /**
2176  * cxl_decoder_add - Add a decoder with targets
2177  * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc()
2178  *
2179  * This is the unlocked variant of cxl_decoder_add_locked().
2180  * See cxl_decoder_add_locked().
2181  *
2182  * Context: Process context. Takes and releases the device lock of the port that
2183  *	    owns the @cxld.
2184  */
cxl_decoder_add(struct cxl_decoder * cxld)2185 int cxl_decoder_add(struct cxl_decoder *cxld)
2186 {
2187 	struct cxl_port *port;
2188 
2189 	if (WARN_ON_ONCE(!cxld))
2190 		return -EINVAL;
2191 
2192 	if (WARN_ON_ONCE(IS_ERR(cxld)))
2193 		return PTR_ERR(cxld);
2194 
2195 	port = to_cxl_port(cxld->dev.parent);
2196 
2197 	guard(device)(&port->dev);
2198 	return cxl_decoder_add_locked(cxld);
2199 }
2200 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, "CXL");
2201 
cxld_unregister(void * dev)2202 static void cxld_unregister(void *dev)
2203 {
2204 	if (is_endpoint_decoder(dev))
2205 		cxl_decoder_detach(NULL, to_cxl_endpoint_decoder(dev), -1,
2206 				   DETACH_INVALIDATE);
2207 
2208 	device_unregister(dev);
2209 }
2210 
cxl_decoder_autoremove(struct device * host,struct cxl_decoder * cxld)2211 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld)
2212 {
2213 	return devm_add_action_or_reset(host, cxld_unregister, &cxld->dev);
2214 }
2215 EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, "CXL");
2216 
2217 /**
2218  * __cxl_driver_register - register a driver for the cxl bus
2219  * @cxl_drv: cxl driver structure to attach
2220  * @owner: owning module/driver
2221  * @modname: KBUILD_MODNAME for parent driver
2222  */
__cxl_driver_register(struct cxl_driver * cxl_drv,struct module * owner,const char * modname)2223 int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
2224 			  const char *modname)
2225 {
2226 	if (!cxl_drv->probe) {
2227 		pr_debug("%s ->probe() must be specified\n", modname);
2228 		return -EINVAL;
2229 	}
2230 
2231 	if (!cxl_drv->name) {
2232 		pr_debug("%s ->name must be specified\n", modname);
2233 		return -EINVAL;
2234 	}
2235 
2236 	if (!cxl_drv->id) {
2237 		pr_debug("%s ->id must be specified\n", modname);
2238 		return -EINVAL;
2239 	}
2240 
2241 	cxl_drv->drv.bus = &cxl_bus_type;
2242 	cxl_drv->drv.owner = owner;
2243 	cxl_drv->drv.mod_name = modname;
2244 	cxl_drv->drv.name = cxl_drv->name;
2245 
2246 	return driver_register(&cxl_drv->drv);
2247 }
2248 EXPORT_SYMBOL_NS_GPL(__cxl_driver_register, "CXL");
2249 
cxl_driver_unregister(struct cxl_driver * cxl_drv)2250 void cxl_driver_unregister(struct cxl_driver *cxl_drv)
2251 {
2252 	driver_unregister(&cxl_drv->drv);
2253 }
2254 EXPORT_SYMBOL_NS_GPL(cxl_driver_unregister, "CXL");
2255 
cxl_bus_uevent(const struct device * dev,struct kobj_uevent_env * env)2256 static int cxl_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
2257 {
2258 	return add_uevent_var(env, "MODALIAS=" CXL_MODALIAS_FMT,
2259 			      cxl_device_id(dev));
2260 }
2261 
cxl_bus_match(struct device * dev,const struct device_driver * drv)2262 static int cxl_bus_match(struct device *dev, const struct device_driver *drv)
2263 {
2264 	return cxl_device_id(dev) == to_cxl_drv(drv)->id;
2265 }
2266 
cxl_bus_probe(struct device * dev)2267 static int cxl_bus_probe(struct device *dev)
2268 {
2269 	int rc;
2270 
2271 	rc = to_cxl_drv(dev->driver)->probe(dev);
2272 	dev_dbg(dev, "probe: %d\n", rc);
2273 	return rc;
2274 }
2275 
cxl_bus_remove(struct device * dev)2276 static void cxl_bus_remove(struct device *dev)
2277 {
2278 	struct cxl_driver *cxl_drv = to_cxl_drv(dev->driver);
2279 
2280 	if (cxl_drv->remove)
2281 		cxl_drv->remove(dev);
2282 }
2283 
2284 static struct workqueue_struct *cxl_bus_wq;
2285 
cxl_rescan_attach(struct device * dev,void * data)2286 static int cxl_rescan_attach(struct device *dev, void *data)
2287 {
2288 	int rc = device_attach(dev);
2289 
2290 	dev_vdbg(dev, "rescan: %s\n", rc ? "attach" : "detached");
2291 
2292 	return 0;
2293 }
2294 
cxl_bus_rescan_queue(struct work_struct * w)2295 static void cxl_bus_rescan_queue(struct work_struct *w)
2296 {
2297 	bus_for_each_dev(&cxl_bus_type, NULL, NULL, cxl_rescan_attach);
2298 }
2299 
cxl_bus_rescan(void)2300 void cxl_bus_rescan(void)
2301 {
2302 	static DECLARE_WORK(rescan_work, cxl_bus_rescan_queue);
2303 
2304 	queue_work(cxl_bus_wq, &rescan_work);
2305 }
2306 EXPORT_SYMBOL_NS_GPL(cxl_bus_rescan, "CXL");
2307 
cxl_bus_drain(void)2308 void cxl_bus_drain(void)
2309 {
2310 	drain_workqueue(cxl_bus_wq);
2311 }
2312 EXPORT_SYMBOL_NS_GPL(cxl_bus_drain, "CXL");
2313 
schedule_cxl_memdev_detach(struct cxl_memdev * cxlmd)2314 bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd)
2315 {
2316 	return queue_work(cxl_bus_wq, &cxlmd->detach_work);
2317 }
2318 EXPORT_SYMBOL_NS_GPL(schedule_cxl_memdev_detach, "CXL");
2319 
add_latency(struct access_coordinate * c,long latency)2320 static void add_latency(struct access_coordinate *c, long latency)
2321 {
2322 	for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
2323 		c[i].write_latency += latency;
2324 		c[i].read_latency += latency;
2325 	}
2326 }
2327 
coordinates_valid(struct access_coordinate * c)2328 static bool coordinates_valid(struct access_coordinate *c)
2329 {
2330 	for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
2331 		if (c[i].read_bandwidth && c[i].write_bandwidth &&
2332 		    c[i].read_latency && c[i].write_latency)
2333 			continue;
2334 		return false;
2335 	}
2336 
2337 	return true;
2338 }
2339 
set_min_bandwidth(struct access_coordinate * c,unsigned int bw)2340 static void set_min_bandwidth(struct access_coordinate *c, unsigned int bw)
2341 {
2342 	for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
2343 		c[i].write_bandwidth = min(c[i].write_bandwidth, bw);
2344 		c[i].read_bandwidth = min(c[i].read_bandwidth, bw);
2345 	}
2346 }
2347 
set_access_coordinates(struct access_coordinate * out,struct access_coordinate * in)2348 static void set_access_coordinates(struct access_coordinate *out,
2349 				   struct access_coordinate *in)
2350 {
2351 	for (int i = 0; i < ACCESS_COORDINATE_MAX; i++)
2352 		out[i] = in[i];
2353 }
2354 
parent_port_is_cxl_root(struct cxl_port * port)2355 static bool parent_port_is_cxl_root(struct cxl_port *port)
2356 {
2357 	return is_cxl_root(to_cxl_port(port->dev.parent));
2358 }
2359 
2360 /**
2361  * cxl_endpoint_get_perf_coordinates - Retrieve performance numbers stored in dports
2362  *				   of CXL path
2363  * @port: endpoint cxl_port
2364  * @coord: output performance data
2365  *
2366  * Return: errno on failure, 0 on success.
2367  */
cxl_endpoint_get_perf_coordinates(struct cxl_port * port,struct access_coordinate * coord)2368 int cxl_endpoint_get_perf_coordinates(struct cxl_port *port,
2369 				      struct access_coordinate *coord)
2370 {
2371 	struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev);
2372 	struct access_coordinate c[] = {
2373 		{
2374 			.read_bandwidth = UINT_MAX,
2375 			.write_bandwidth = UINT_MAX,
2376 		},
2377 		{
2378 			.read_bandwidth = UINT_MAX,
2379 			.write_bandwidth = UINT_MAX,
2380 		},
2381 	};
2382 	struct cxl_port *iter = port;
2383 	struct cxl_dport *dport;
2384 	struct pci_dev *pdev;
2385 	struct device *dev;
2386 	unsigned int bw;
2387 	bool is_cxl_root;
2388 
2389 	if (!is_cxl_endpoint(port))
2390 		return -EINVAL;
2391 
2392 	/*
2393 	 * Skip calculation for RCD. Expectation is HMAT already covers RCD case
2394 	 * since RCH does not support hotplug.
2395 	 */
2396 	if (cxlmd->cxlds->rcd)
2397 		return 0;
2398 
2399 	/*
2400 	 * Exit the loop when the parent port of the current iter port is cxl
2401 	 * root. The iterative loop starts at the endpoint and gathers the
2402 	 * latency of the CXL link from the current device/port to the connected
2403 	 * downstream port each iteration.
2404 	 */
2405 	do {
2406 		dport = iter->parent_dport;
2407 		iter = to_cxl_port(iter->dev.parent);
2408 		is_cxl_root = parent_port_is_cxl_root(iter);
2409 
2410 		/*
2411 		 * There's no valid access_coordinate for a root port since RPs do not
2412 		 * have CDAT and therefore needs to be skipped.
2413 		 */
2414 		if (!is_cxl_root) {
2415 			if (!coordinates_valid(dport->coord))
2416 				return -EINVAL;
2417 			cxl_coordinates_combine(c, c, dport->coord);
2418 		}
2419 		add_latency(c, dport->link_latency);
2420 	} while (!is_cxl_root);
2421 
2422 	dport = iter->parent_dport;
2423 	/* Retrieve HB coords */
2424 	if (!coordinates_valid(dport->coord))
2425 		return -EINVAL;
2426 	cxl_coordinates_combine(c, c, dport->coord);
2427 
2428 	dev = port->uport_dev->parent;
2429 	if (!dev_is_pci(dev))
2430 		return -ENODEV;
2431 
2432 	/* Get the calculated PCI paths bandwidth */
2433 	pdev = to_pci_dev(dev);
2434 	bw = pcie_bandwidth_available(pdev, NULL, NULL, NULL);
2435 	if (bw == 0)
2436 		return -ENXIO;
2437 	bw /= BITS_PER_BYTE;
2438 
2439 	set_min_bandwidth(c, bw);
2440 	set_access_coordinates(coord, c);
2441 
2442 	return 0;
2443 }
2444 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_get_perf_coordinates, "CXL");
2445 
cxl_port_get_switch_dport_bandwidth(struct cxl_port * port,struct access_coordinate * c)2446 int cxl_port_get_switch_dport_bandwidth(struct cxl_port *port,
2447 					struct access_coordinate *c)
2448 {
2449 	struct cxl_dport *dport = port->parent_dport;
2450 
2451 	/* Check this port is connected to a switch DSP and not an RP */
2452 	if (parent_port_is_cxl_root(to_cxl_port(port->dev.parent)))
2453 		return -ENODEV;
2454 
2455 	if (!coordinates_valid(dport->coord))
2456 		return -EINVAL;
2457 
2458 	for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
2459 		c[i].read_bandwidth = dport->coord[i].read_bandwidth;
2460 		c[i].write_bandwidth = dport->coord[i].write_bandwidth;
2461 	}
2462 
2463 	return 0;
2464 }
2465 
2466 /* for user tooling to ensure port disable work has completed */
flush_store(const struct bus_type * bus,const char * buf,size_t count)2467 static ssize_t flush_store(const struct bus_type *bus, const char *buf, size_t count)
2468 {
2469 	if (sysfs_streq(buf, "1")) {
2470 		flush_workqueue(cxl_bus_wq);
2471 		return count;
2472 	}
2473 
2474 	return -EINVAL;
2475 }
2476 
2477 static BUS_ATTR_WO(flush);
2478 
2479 static struct attribute *cxl_bus_attributes[] = {
2480 	&bus_attr_flush.attr,
2481 	NULL,
2482 };
2483 
2484 static struct attribute_group cxl_bus_attribute_group = {
2485 	.attrs = cxl_bus_attributes,
2486 };
2487 
2488 static const struct attribute_group *cxl_bus_attribute_groups[] = {
2489 	&cxl_bus_attribute_group,
2490 	NULL,
2491 };
2492 
2493 const struct bus_type cxl_bus_type = {
2494 	.name = "cxl",
2495 	.uevent = cxl_bus_uevent,
2496 	.match = cxl_bus_match,
2497 	.probe = cxl_bus_probe,
2498 	.remove = cxl_bus_remove,
2499 	.bus_groups = cxl_bus_attribute_groups,
2500 };
2501 EXPORT_SYMBOL_NS_GPL(cxl_bus_type, "CXL");
2502 
2503 static struct dentry *cxl_debugfs;
2504 
cxl_debugfs_create_dir(const char * dir)2505 struct dentry *cxl_debugfs_create_dir(const char *dir)
2506 {
2507 	return debugfs_create_dir(dir, cxl_debugfs);
2508 }
2509 EXPORT_SYMBOL_NS_GPL(cxl_debugfs_create_dir, "CXL");
2510 
cxl_core_init(void)2511 static __init int cxl_core_init(void)
2512 {
2513 	int rc;
2514 
2515 	cxl_debugfs = debugfs_create_dir("cxl", NULL);
2516 
2517 	if (einj_cxl_is_initialized())
2518 		debugfs_create_file("einj_types", 0400, cxl_debugfs, NULL,
2519 				    &einj_cxl_available_error_type_fops);
2520 
2521 	cxl_mbox_init();
2522 
2523 	rc = cxl_memdev_init();
2524 	if (rc)
2525 		return rc;
2526 
2527 	cxl_bus_wq = alloc_ordered_workqueue("cxl_port", 0);
2528 	if (!cxl_bus_wq) {
2529 		rc = -ENOMEM;
2530 		goto err_wq;
2531 	}
2532 
2533 	rc = bus_register(&cxl_bus_type);
2534 	if (rc)
2535 		goto err_bus;
2536 
2537 	rc = cxl_region_init();
2538 	if (rc)
2539 		goto err_region;
2540 
2541 	rc = cxl_ras_init();
2542 	if (rc)
2543 		goto err_ras;
2544 
2545 	return 0;
2546 
2547 err_ras:
2548 	cxl_region_exit();
2549 err_region:
2550 	bus_unregister(&cxl_bus_type);
2551 err_bus:
2552 	destroy_workqueue(cxl_bus_wq);
2553 err_wq:
2554 	cxl_memdev_exit();
2555 	return rc;
2556 }
2557 
cxl_core_exit(void)2558 static void cxl_core_exit(void)
2559 {
2560 	cxl_ras_exit();
2561 	cxl_region_exit();
2562 	bus_unregister(&cxl_bus_type);
2563 	destroy_workqueue(cxl_bus_wq);
2564 	cxl_memdev_exit();
2565 	debugfs_remove_recursive(cxl_debugfs);
2566 }
2567 
2568 subsys_initcall(cxl_core_init);
2569 module_exit(cxl_core_exit);
2570 MODULE_DESCRIPTION("CXL: Core Compute Express Link support");
2571 MODULE_LICENSE("GPL v2");
2572 MODULE_IMPORT_NS("CXL");
2573