xref: /linux/drivers/cxl/core/port.c (revision 8457669db968c98edb781892d73fa559e1efcbd4)
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 
619 	device_lock_assert(port_to_host(port));
620 	port->dead = true;
621 	device_unregister(&port->dev);
622 }
623 
cxl_unlink_uport(void * _port)624 static void cxl_unlink_uport(void *_port)
625 {
626 	struct cxl_port *port = _port;
627 
628 	sysfs_remove_link(&port->dev.kobj, "uport");
629 }
630 
devm_cxl_link_uport(struct device * host,struct cxl_port * port)631 static int devm_cxl_link_uport(struct device *host, struct cxl_port *port)
632 {
633 	int rc;
634 
635 	rc = sysfs_create_link(&port->dev.kobj, &port->uport_dev->kobj,
636 			       "uport");
637 	if (rc)
638 		return rc;
639 	return devm_add_action_or_reset(host, cxl_unlink_uport, port);
640 }
641 
cxl_unlink_parent_dport(void * _port)642 static void cxl_unlink_parent_dport(void *_port)
643 {
644 	struct cxl_port *port = _port;
645 
646 	sysfs_remove_link(&port->dev.kobj, "parent_dport");
647 }
648 
devm_cxl_link_parent_dport(struct device * host,struct cxl_port * port,struct cxl_dport * parent_dport)649 static int devm_cxl_link_parent_dport(struct device *host,
650 				      struct cxl_port *port,
651 				      struct cxl_dport *parent_dport)
652 {
653 	int rc;
654 
655 	if (!parent_dport)
656 		return 0;
657 
658 	rc = sysfs_create_link(&port->dev.kobj, &parent_dport->dport_dev->kobj,
659 			       "parent_dport");
660 	if (rc)
661 		return rc;
662 	return devm_add_action_or_reset(host, cxl_unlink_parent_dport, port);
663 }
664 
665 static struct lock_class_key cxl_port_key;
666 
cxl_port_alloc(struct device * uport_dev,struct cxl_dport * parent_dport)667 static struct cxl_port *cxl_port_alloc(struct device *uport_dev,
668 				       struct cxl_dport *parent_dport)
669 {
670 	struct cxl_root *cxl_root __free(kfree) = NULL;
671 	struct cxl_port *port, *_port __free(kfree) = NULL;
672 	struct device *dev;
673 	int rc;
674 
675 	/* No parent_dport, root cxl_port */
676 	if (!parent_dport) {
677 		cxl_root = kzalloc_obj(*cxl_root);
678 		if (!cxl_root)
679 			return ERR_PTR(-ENOMEM);
680 	} else {
681 		_port = kzalloc_obj(*port);
682 		if (!_port)
683 			return ERR_PTR(-ENOMEM);
684 	}
685 
686 	rc = ida_alloc(&cxl_port_ida, GFP_KERNEL);
687 	if (rc < 0)
688 		return ERR_PTR(rc);
689 
690 	if (cxl_root)
691 		port = &no_free_ptr(cxl_root)->port;
692 	else
693 		port = no_free_ptr(_port);
694 
695 	port->id = rc;
696 	port->uport_dev = uport_dev;
697 
698 	/*
699 	 * The top-level cxl_port "cxl_root" does not have a cxl_port as
700 	 * its parent and it does not have any corresponding component
701 	 * registers as its decode is described by a fixed platform
702 	 * description.
703 	 */
704 	dev = &port->dev;
705 	if (parent_dport) {
706 		struct cxl_port *parent_port = parent_dport->port;
707 		struct cxl_port *iter;
708 
709 		dev->parent = &parent_port->dev;
710 		port->depth = parent_port->depth + 1;
711 		port->parent_dport = parent_dport;
712 
713 		/*
714 		 * walk to the host bridge, or the first ancestor that knows
715 		 * the host bridge
716 		 */
717 		iter = port;
718 		while (!iter->host_bridge &&
719 		       !is_cxl_root(to_cxl_port(iter->dev.parent)))
720 			iter = to_cxl_port(iter->dev.parent);
721 		if (iter->host_bridge)
722 			port->host_bridge = iter->host_bridge;
723 		else if (parent_dport->rch)
724 			port->host_bridge = parent_dport->dport_dev;
725 		else
726 			port->host_bridge = iter->uport_dev;
727 		dev_dbg(uport_dev, "host-bridge: %s\n",
728 			dev_name(port->host_bridge));
729 	} else
730 		dev->parent = uport_dev;
731 
732 	ida_init(&port->decoder_ida);
733 	port->hdm_end = -1;
734 	port->commit_end = -1;
735 	xa_init(&port->dports);
736 	xa_init(&port->endpoints);
737 	xa_init(&port->regions);
738 	port->component_reg_phys = CXL_RESOURCE_NONE;
739 
740 	device_initialize(dev);
741 	lockdep_set_class_and_subclass(&dev->mutex, &cxl_port_key, port->depth);
742 	device_set_pm_not_required(dev);
743 	dev->bus = &cxl_bus_type;
744 	dev->type = &cxl_port_type;
745 
746 	return port;
747 }
748 
cxl_setup_comp_regs(struct device * host,struct cxl_register_map * map,resource_size_t component_reg_phys)749 static int cxl_setup_comp_regs(struct device *host, struct cxl_register_map *map,
750 			       resource_size_t component_reg_phys)
751 {
752 	*map = (struct cxl_register_map) {
753 		.host = host,
754 		.reg_type = CXL_REGLOC_RBI_EMPTY,
755 		.resource = component_reg_phys,
756 	};
757 
758 	if (component_reg_phys == CXL_RESOURCE_NONE)
759 		return 0;
760 
761 	map->reg_type = CXL_REGLOC_RBI_COMPONENT;
762 	map->max_size = CXL_COMPONENT_REG_BLOCK_SIZE;
763 
764 	return cxl_setup_regs(map);
765 }
766 
cxl_port_setup_regs(struct cxl_port * port,resource_size_t component_reg_phys)767 int cxl_port_setup_regs(struct cxl_port *port,
768 			resource_size_t component_reg_phys)
769 {
770 	if (dev_is_platform(port->uport_dev))
771 		return 0;
772 	return cxl_setup_comp_regs(&port->dev, &port->reg_map,
773 				   component_reg_phys);
774 }
775 EXPORT_SYMBOL_NS_GPL(cxl_port_setup_regs, "CXL");
776 
cxl_dport_setup_regs(struct device * host,struct cxl_dport * dport,resource_size_t component_reg_phys)777 static int cxl_dport_setup_regs(struct device *host, struct cxl_dport *dport,
778 				resource_size_t component_reg_phys)
779 {
780 	int rc;
781 
782 	if (dev_is_platform(dport->dport_dev))
783 		return 0;
784 
785 	/*
786 	 * use @dport->dport_dev for the context for error messages during
787 	 * register probing, and fixup @host after the fact, since @host may be
788 	 * NULL.
789 	 */
790 	rc = cxl_setup_comp_regs(dport->dport_dev, &dport->reg_map,
791 				 component_reg_phys);
792 	dport->reg_map.host = host;
793 	return rc;
794 }
795 
796 DEFINE_SHOW_ATTRIBUTE(einj_cxl_available_error_type);
797 
cxl_einj_inject(void * data,u64 type)798 static int cxl_einj_inject(void *data, u64 type)
799 {
800 	struct cxl_dport *dport = data;
801 
802 	if (dport->rch)
803 		return einj_cxl_inject_rch_error(dport->rcrb.base, type);
804 
805 	return einj_cxl_inject_error(to_pci_dev(dport->dport_dev), type);
806 }
807 DEFINE_DEBUGFS_ATTRIBUTE(cxl_einj_inject_fops, NULL, cxl_einj_inject,
808 			 "0x%llx\n");
809 
cxl_debugfs_create_dport_dir(struct cxl_dport * dport)810 static void cxl_debugfs_create_dport_dir(struct cxl_dport *dport)
811 {
812 	struct cxl_port *parent = parent_port_of(dport->port);
813 	struct dentry *dir;
814 
815 	if (!einj_cxl_is_initialized())
816 		return;
817 
818 	/*
819 	 * Protocol error injection is only available for CXL 2.0+ root ports
820 	 * and CXL 1.1 downstream ports
821 	 */
822 	if (!dport->rch &&
823 	    !(dev_is_pci(dport->dport_dev) && parent && is_cxl_root(parent)))
824 		return;
825 
826 	dir = cxl_debugfs_create_dir(dev_name(dport->dport_dev));
827 
828 	debugfs_create_file("einj_inject", 0200, dir, dport,
829 			    &cxl_einj_inject_fops);
830 }
831 
cxl_port_add(struct cxl_port * port,resource_size_t component_reg_phys,struct cxl_dport * parent_dport)832 static int cxl_port_add(struct cxl_port *port,
833 			resource_size_t component_reg_phys,
834 			struct cxl_dport *parent_dport)
835 {
836 	struct device *dev __free(put_device) = &port->dev;
837 	int rc;
838 
839 	if (is_cxl_memdev(port->uport_dev)) {
840 		struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev);
841 		struct cxl_dev_state *cxlds = cxlmd->cxlds;
842 
843 		rc = dev_set_name(dev, "endpoint%d", port->id);
844 		if (rc)
845 			return rc;
846 
847 		/*
848 		 * The endpoint driver already enumerated the component and RAS
849 		 * registers. Reuse that enumeration while prepping them to be
850 		 * mapped by the cxl_port driver.
851 		 */
852 		port->reg_map = cxlds->reg_map;
853 		port->reg_map.host = &port->dev;
854 		cxlmd->endpoint = port;
855 	} else if (parent_dport) {
856 		rc = dev_set_name(dev, "port%d", port->id);
857 		if (rc)
858 			return rc;
859 
860 		port->component_reg_phys = component_reg_phys;
861 	} else {
862 		rc = dev_set_name(dev, "root%d", port->id);
863 		if (rc)
864 			return rc;
865 	}
866 
867 	rc = device_add(dev);
868 	if (rc)
869 		return rc;
870 
871 	/* Inhibit the cleanup function invoked */
872 	dev = NULL;
873 	return 0;
874 }
875 
__devm_cxl_add_port(struct device * host,struct device * uport_dev,resource_size_t component_reg_phys,struct cxl_dport * parent_dport)876 static struct cxl_port *__devm_cxl_add_port(struct device *host,
877 					    struct device *uport_dev,
878 					    resource_size_t component_reg_phys,
879 					    struct cxl_dport *parent_dport)
880 {
881 	struct cxl_port *port;
882 	int rc;
883 
884 	port = cxl_port_alloc(uport_dev, parent_dport);
885 	if (IS_ERR(port))
886 		return port;
887 
888 	rc = cxl_port_add(port, component_reg_phys, parent_dport);
889 	if (rc)
890 		return ERR_PTR(rc);
891 
892 	rc = devm_add_action_or_reset(host, unregister_port, port);
893 	if (rc)
894 		return ERR_PTR(rc);
895 
896 	rc = devm_cxl_link_uport(host, port);
897 	if (rc)
898 		return ERR_PTR(rc);
899 
900 	rc = devm_cxl_link_parent_dport(host, port, parent_dport);
901 	if (rc)
902 		return ERR_PTR(rc);
903 
904 	if (parent_dport && dev_is_pci(uport_dev))
905 		port->pci_latency = cxl_pci_get_latency(to_pci_dev(uport_dev));
906 
907 	return port;
908 }
909 
910 /**
911  * devm_cxl_add_port - register a cxl_port in CXL memory decode hierarchy
912  * @host: host device for devm operations
913  * @uport_dev: "physical" device implementing this upstream port
914  * @component_reg_phys: (optional) for configurable cxl_port instances
915  * @parent_dport: next hop up in the CXL memory decode hierarchy
916  */
devm_cxl_add_port(struct device * host,struct device * uport_dev,resource_size_t component_reg_phys,struct cxl_dport * parent_dport)917 struct cxl_port *devm_cxl_add_port(struct device *host,
918 				   struct device *uport_dev,
919 				   resource_size_t component_reg_phys,
920 				   struct cxl_dport *parent_dport)
921 {
922 	struct cxl_port *port, *parent_port;
923 
924 	port = __devm_cxl_add_port(host, uport_dev, component_reg_phys,
925 				   parent_dport);
926 
927 	parent_port = parent_dport ? parent_dport->port : NULL;
928 	if (IS_ERR(port)) {
929 		dev_dbg(uport_dev, "Failed to add%s%s%s: %ld\n",
930 			parent_port ? " port to " : "",
931 			parent_port ? dev_name(&parent_port->dev) : "",
932 			parent_port ? "" : " root port",
933 			PTR_ERR(port));
934 	} else {
935 		dev_dbg(uport_dev, "%s added%s%s%s\n",
936 			dev_name(&port->dev),
937 			parent_port ? " to " : "",
938 			parent_port ? dev_name(&parent_port->dev) : "",
939 			parent_port ? "" : " (root port)");
940 	}
941 
942 	return port;
943 }
944 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_port, "CXL");
945 
devm_cxl_add_root(struct device * host)946 struct cxl_root *devm_cxl_add_root(struct device *host)
947 {
948 	struct cxl_port *port;
949 
950 	port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL);
951 	if (IS_ERR(port))
952 		return ERR_CAST(port);
953 
954 	return to_cxl_root(port);
955 }
956 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_root, "CXL");
957 
cxl_port_to_pci_bus(struct cxl_port * port)958 struct pci_bus *cxl_port_to_pci_bus(struct cxl_port *port)
959 {
960 	/* There is no pci_bus associated with a CXL platform-root port */
961 	if (is_cxl_root(port))
962 		return NULL;
963 
964 	if (dev_is_pci(port->uport_dev)) {
965 		struct pci_dev *pdev = to_pci_dev(port->uport_dev);
966 
967 		return pdev->subordinate;
968 	}
969 
970 	return xa_load(&cxl_root_buses, (unsigned long)port->uport_dev);
971 }
972 EXPORT_SYMBOL_NS_GPL(cxl_port_to_pci_bus, "CXL");
973 
unregister_pci_bus(void * uport_dev)974 static void unregister_pci_bus(void *uport_dev)
975 {
976 	xa_erase(&cxl_root_buses, (unsigned long)uport_dev);
977 }
978 
devm_cxl_register_pci_bus(struct device * host,struct device * uport_dev,struct pci_bus * bus)979 int devm_cxl_register_pci_bus(struct device *host, struct device *uport_dev,
980 			      struct pci_bus *bus)
981 {
982 	int rc;
983 
984 	if (dev_is_pci(uport_dev))
985 		return -EINVAL;
986 
987 	rc = xa_insert(&cxl_root_buses, (unsigned long)uport_dev, bus,
988 		       GFP_KERNEL);
989 	if (rc)
990 		return rc;
991 	return devm_add_action_or_reset(host, unregister_pci_bus, uport_dev);
992 }
993 EXPORT_SYMBOL_NS_GPL(devm_cxl_register_pci_bus, "CXL");
994 
dev_is_cxl_root_child(struct device * dev)995 static bool dev_is_cxl_root_child(struct device *dev)
996 {
997 	struct cxl_port *port, *parent;
998 
999 	if (!is_cxl_port(dev))
1000 		return false;
1001 
1002 	port = to_cxl_port(dev);
1003 	if (is_cxl_root(port))
1004 		return false;
1005 
1006 	parent = to_cxl_port(port->dev.parent);
1007 	if (is_cxl_root(parent))
1008 		return true;
1009 
1010 	return false;
1011 }
1012 
find_cxl_root(struct cxl_port * port)1013 struct cxl_root *find_cxl_root(struct cxl_port *port)
1014 {
1015 	struct cxl_port *iter = port;
1016 
1017 	while (iter && !is_cxl_root(iter))
1018 		iter = to_cxl_port(iter->dev.parent);
1019 
1020 	if (!iter)
1021 		return NULL;
1022 	get_device(&iter->dev);
1023 	return to_cxl_root(iter);
1024 }
1025 EXPORT_SYMBOL_NS_GPL(find_cxl_root, "CXL");
1026 
find_dport(struct cxl_port * port,int id)1027 static struct cxl_dport *find_dport(struct cxl_port *port, int id)
1028 {
1029 	struct cxl_dport *dport;
1030 	unsigned long index;
1031 
1032 	device_lock_assert(&port->dev);
1033 	xa_for_each(&port->dports, index, dport)
1034 		if (dport->port_id == id)
1035 			return dport;
1036 	return NULL;
1037 }
1038 
add_dport(struct cxl_port * port,struct cxl_dport * dport)1039 static int add_dport(struct cxl_port *port, struct cxl_dport *dport)
1040 {
1041 	struct cxl_dport *dup;
1042 	int rc;
1043 
1044 	device_lock_assert(&port->dev);
1045 	dup = find_dport(port, dport->port_id);
1046 	if (dup) {
1047 		dev_err(&port->dev,
1048 			"unable to add dport%d-%s non-unique port id (%s)\n",
1049 			dport->port_id, dev_name(dport->dport_dev),
1050 			dev_name(dup->dport_dev));
1051 		return -EBUSY;
1052 	}
1053 
1054 	/* Arrange for dport_dev to be valid through remove_dport() */
1055 	struct device *dev __free(put_device) = get_device(dport->dport_dev);
1056 
1057 	rc = xa_insert(&port->dports, (unsigned long)dport->dport_dev, dport,
1058 		       GFP_KERNEL);
1059 	if (rc)
1060 		return rc;
1061 
1062 	retain_and_null_ptr(dev);
1063 	port->nr_dports++;
1064 	return 0;
1065 }
1066 
1067 /*
1068  * Since root-level CXL dports cannot be enumerated by PCI they are not
1069  * enumerated by the common port driver that acquires the port lock over
1070  * dport add/remove. Instead, root dports are manually added by a
1071  * platform driver and cond_cxl_root_lock() is used to take the missing
1072  * port lock in that case.
1073  */
cond_cxl_root_lock(struct cxl_port * port)1074 static void cond_cxl_root_lock(struct cxl_port *port)
1075 {
1076 	if (is_cxl_root(port))
1077 		device_lock(&port->dev);
1078 }
1079 
cond_cxl_root_unlock(struct cxl_port * port)1080 static void cond_cxl_root_unlock(struct cxl_port *port)
1081 {
1082 	if (is_cxl_root(port))
1083 		device_unlock(&port->dev);
1084 }
1085 
cxl_dport_remove(void * data)1086 static void cxl_dport_remove(void *data)
1087 {
1088 	struct cxl_dport *dport = data;
1089 	struct cxl_port *port = dport->port;
1090 
1091 	port->nr_dports--;
1092 	xa_erase(&port->dports, (unsigned long) dport->dport_dev);
1093 	put_device(dport->dport_dev);
1094 }
1095 
cxl_dport_unlink(void * data)1096 static void cxl_dport_unlink(void *data)
1097 {
1098 	struct cxl_dport *dport = data;
1099 	struct cxl_port *port = dport->port;
1100 	char link_name[CXL_TARGET_STRLEN];
1101 
1102 	sprintf(link_name, "dport%d", dport->port_id);
1103 	sysfs_remove_link(&port->dev.kobj, link_name);
1104 }
1105 
free_dport(void * dport)1106 static void free_dport(void *dport)
1107 {
1108 	kfree(dport);
1109 }
1110 
1111 /*
1112  * Upon return either a group is established with one action (free_dport()), or
1113  * no group established and @dport is freed.
1114  */
cxl_dport_open_dr_group_or_free(struct cxl_dport * dport)1115 static void *cxl_dport_open_dr_group_or_free(struct cxl_dport *dport)
1116 {
1117 	int rc;
1118 	struct device *host = dport_to_host(dport);
1119 	void *group = devres_open_group(host, dport, GFP_KERNEL);
1120 
1121 	if (!group) {
1122 		kfree(dport);
1123 		return NULL;
1124 	}
1125 
1126 	rc = devm_add_action_or_reset(host, free_dport, dport);
1127 	if (rc) {
1128 		devres_release_group(host, group);
1129 		return NULL;
1130 	}
1131 
1132 	return group;
1133 }
1134 
cxl_dport_close_dr_group(struct cxl_dport * dport,void * group)1135 static void cxl_dport_close_dr_group(struct cxl_dport *dport, void *group)
1136 {
1137 	devres_close_group(dport_to_host(dport), group);
1138 }
1139 
del_dport(struct cxl_dport * dport)1140 static void del_dport(struct cxl_dport *dport)
1141 {
1142 	devres_release_group(dport_to_host(dport), dport);
1143 }
1144 
1145 /* The dport group id is the dport */
DEFINE_FREE(cxl_dport_release_dr_group,void *,if (_T)del_dport (_T))1146 DEFINE_FREE(cxl_dport_release_dr_group, void *, if (_T) del_dport(_T))
1147 
1148 static struct cxl_dport *
1149 __devm_cxl_add_dport(struct cxl_port *port, struct device *dport_dev,
1150 		     int port_id, resource_size_t component_reg_phys,
1151 		     resource_size_t rcrb)
1152 {
1153 	char link_name[CXL_TARGET_STRLEN];
1154 	struct cxl_dport *dport;
1155 	struct device *host;
1156 	int rc;
1157 
1158 	if (is_cxl_root(port))
1159 		host = port->uport_dev;
1160 	else
1161 		host = &port->dev;
1162 
1163 	if (!host->driver) {
1164 		dev_WARN_ONCE(&port->dev, 1, "dport:%s bad devm context\n",
1165 			      dev_name(dport_dev));
1166 		return ERR_PTR(-ENXIO);
1167 	}
1168 
1169 	if (snprintf(link_name, CXL_TARGET_STRLEN, "dport%d", port_id) >=
1170 	    CXL_TARGET_STRLEN)
1171 		return ERR_PTR(-EINVAL);
1172 
1173 	dport = kzalloc_obj(*dport);
1174 	if (!dport)
1175 		return ERR_PTR(-ENOMEM);
1176 
1177 	/* Just enough init to manage the devres group */
1178 	dport->dport_dev = dport_dev;
1179 	dport->port_id = port_id;
1180 	dport->port = port;
1181 
1182 	void *dport_dr_group __free(cxl_dport_release_dr_group) =
1183 		cxl_dport_open_dr_group_or_free(dport);
1184 	if (!dport_dr_group)
1185 		return ERR_PTR(-ENOMEM);
1186 
1187 	if (rcrb == CXL_RESOURCE_NONE) {
1188 		rc = cxl_dport_setup_regs(&port->dev, dport,
1189 					  component_reg_phys);
1190 		if (rc)
1191 			return ERR_PTR(rc);
1192 	} else {
1193 		dport->rcrb.base = rcrb;
1194 		component_reg_phys = __rcrb_to_component(dport_dev, &dport->rcrb,
1195 							 CXL_RCRB_DOWNSTREAM);
1196 		if (component_reg_phys == CXL_RESOURCE_NONE) {
1197 			dev_warn(dport_dev, "Invalid Component Registers in RCRB");
1198 			return ERR_PTR(-ENXIO);
1199 		}
1200 
1201 		/*
1202 		 * RCH @dport is not ready to map until associated with its
1203 		 * memdev
1204 		 */
1205 		rc = cxl_dport_setup_regs(NULL, dport, component_reg_phys);
1206 		if (rc)
1207 			return ERR_PTR(rc);
1208 
1209 		dport->rch = true;
1210 	}
1211 
1212 	if (component_reg_phys != CXL_RESOURCE_NONE)
1213 		dev_dbg(dport_dev, "Component Registers found for dport: %pa\n",
1214 			&component_reg_phys);
1215 
1216 	cond_cxl_root_lock(port);
1217 	rc = add_dport(port, dport);
1218 	cond_cxl_root_unlock(port);
1219 	if (rc)
1220 		return ERR_PTR(rc);
1221 
1222 	rc = devm_add_action_or_reset(host, cxl_dport_remove, dport);
1223 	if (rc)
1224 		return ERR_PTR(rc);
1225 
1226 	rc = sysfs_create_link(&port->dev.kobj, &dport_dev->kobj, link_name);
1227 	if (rc)
1228 		return ERR_PTR(rc);
1229 
1230 	rc = devm_add_action_or_reset(host, cxl_dport_unlink, dport);
1231 	if (rc)
1232 		return ERR_PTR(rc);
1233 
1234 	if (dev_is_pci(dport_dev))
1235 		dport->link_latency = cxl_pci_get_latency(to_pci_dev(dport_dev));
1236 
1237 	cxl_debugfs_create_dport_dir(dport);
1238 
1239 	if (!dport->rch)
1240 		devm_cxl_dport_ras_setup(dport);
1241 
1242 	/* keep the group, and mark the end of devm actions */
1243 	cxl_dport_close_dr_group(dport, no_free_ptr(dport_dr_group));
1244 
1245 	return dport;
1246 }
1247 
1248 /**
1249  * devm_cxl_add_dport - append VH downstream port data to a cxl_port
1250  * @port: the cxl_port that references this dport
1251  * @dport_dev: firmware or PCI device representing the dport
1252  * @port_id: identifier for this dport in a decoder's target list
1253  * @component_reg_phys: optional location of CXL component registers
1254  *
1255  * Note that dports are appended to the devm release action's of the
1256  * either the port's host (for root ports), or the port itself (for
1257  * switch ports)
1258  */
devm_cxl_add_dport(struct cxl_port * port,struct device * dport_dev,int port_id,resource_size_t component_reg_phys)1259 struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port,
1260 				     struct device *dport_dev, int port_id,
1261 				     resource_size_t component_reg_phys)
1262 {
1263 	struct cxl_dport *dport;
1264 
1265 	dport = __devm_cxl_add_dport(port, dport_dev, port_id,
1266 				     component_reg_phys, CXL_RESOURCE_NONE);
1267 	if (IS_ERR(dport)) {
1268 		dev_dbg(dport_dev, "failed to add dport to %s: %ld\n",
1269 			dev_name(&port->dev), PTR_ERR(dport));
1270 	} else {
1271 		dev_dbg(dport_dev, "dport added to %s\n",
1272 			dev_name(&port->dev));
1273 	}
1274 
1275 	return dport;
1276 }
1277 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_dport, "CXL");
1278 
1279 /**
1280  * devm_cxl_add_rch_dport - append RCH downstream port data to a cxl_port
1281  * @port: the cxl_port that references this dport
1282  * @dport_dev: firmware or PCI device representing the dport
1283  * @port_id: identifier for this dport in a decoder's target list
1284  * @rcrb: mandatory location of a Root Complex Register Block
1285  *
1286  * See CXL 3.0 9.11.8 CXL Devices Attached to an RCH
1287  */
devm_cxl_add_rch_dport(struct cxl_port * port,struct device * dport_dev,int port_id,resource_size_t rcrb)1288 struct cxl_dport *devm_cxl_add_rch_dport(struct cxl_port *port,
1289 					 struct device *dport_dev, int port_id,
1290 					 resource_size_t rcrb)
1291 {
1292 	struct cxl_dport *dport;
1293 
1294 	if (rcrb == CXL_RESOURCE_NONE) {
1295 		dev_dbg(&port->dev, "failed to add RCH dport, missing RCRB\n");
1296 		return ERR_PTR(-EINVAL);
1297 	}
1298 
1299 	dport = __devm_cxl_add_dport(port, dport_dev, port_id,
1300 				     CXL_RESOURCE_NONE, rcrb);
1301 	if (IS_ERR(dport)) {
1302 		dev_dbg(dport_dev, "failed to add RCH dport to %s: %ld\n",
1303 			dev_name(&port->dev), PTR_ERR(dport));
1304 	} else {
1305 		dev_dbg(dport_dev, "RCH dport added to %s\n",
1306 			dev_name(&port->dev));
1307 	}
1308 
1309 	return dport;
1310 }
1311 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_rch_dport, "CXL");
1312 
add_ep(struct cxl_ep * new)1313 static int add_ep(struct cxl_ep *new)
1314 {
1315 	struct cxl_port *port = new->dport->port;
1316 
1317 	guard(device)(&port->dev);
1318 	if (port->dead)
1319 		return -ENXIO;
1320 
1321 	return xa_insert(&port->endpoints, (unsigned long)new->ep,
1322 			 new, GFP_KERNEL);
1323 }
1324 
1325 /**
1326  * cxl_add_ep - register an endpoint's interest in a port
1327  * @dport: the dport that routes to @ep_dev
1328  * @ep_dev: device representing the endpoint
1329  *
1330  * Intermediate CXL ports are scanned based on the arrival of endpoints.
1331  * When those endpoints depart the port can be destroyed once all
1332  * endpoints that care about that port have been removed.
1333  */
cxl_add_ep(struct cxl_dport * dport,struct device * ep_dev)1334 static int cxl_add_ep(struct cxl_dport *dport, struct device *ep_dev)
1335 {
1336 	struct cxl_ep *ep;
1337 	int rc;
1338 
1339 	ep = kzalloc_obj(*ep);
1340 	if (!ep)
1341 		return -ENOMEM;
1342 
1343 	ep->ep = get_device(ep_dev);
1344 	ep->dport = dport;
1345 
1346 	rc = add_ep(ep);
1347 	if (rc)
1348 		cxl_ep_release(ep);
1349 	return rc;
1350 }
1351 
1352 struct cxl_find_port_ctx {
1353 	const struct device *dport_dev;
1354 	const struct cxl_port *parent_port;
1355 	struct cxl_dport **dport;
1356 };
1357 
match_port_by_dport(struct device * dev,const void * data)1358 static int match_port_by_dport(struct device *dev, const void *data)
1359 {
1360 	const struct cxl_find_port_ctx *ctx = data;
1361 	struct cxl_dport *dport;
1362 	struct cxl_port *port;
1363 
1364 	if (!is_cxl_port(dev))
1365 		return 0;
1366 	if (ctx->parent_port && dev->parent != &ctx->parent_port->dev)
1367 		return 0;
1368 
1369 	port = to_cxl_port(dev);
1370 	dport = cxl_find_dport_by_dev(port, ctx->dport_dev);
1371 	if (ctx->dport)
1372 		*ctx->dport = dport;
1373 	return dport != NULL;
1374 }
1375 
__find_cxl_port(struct cxl_find_port_ctx * ctx)1376 static struct cxl_port *__find_cxl_port(struct cxl_find_port_ctx *ctx)
1377 {
1378 	struct device *dev;
1379 
1380 	if (!ctx->dport_dev)
1381 		return NULL;
1382 
1383 	dev = bus_find_device(&cxl_bus_type, NULL, ctx, match_port_by_dport);
1384 	if (dev)
1385 		return to_cxl_port(dev);
1386 	return NULL;
1387 }
1388 
find_cxl_port(struct device * dport_dev,struct cxl_dport ** dport)1389 static struct cxl_port *find_cxl_port(struct device *dport_dev,
1390 				      struct cxl_dport **dport)
1391 {
1392 	struct cxl_find_port_ctx ctx = {
1393 		.dport_dev = dport_dev,
1394 		.dport = dport,
1395 	};
1396 	struct cxl_port *port;
1397 
1398 	port = __find_cxl_port(&ctx);
1399 	return port;
1400 }
1401 
1402 /*
1403  * All users of grandparent() are using it to walk PCIe-like switch port
1404  * hierarchy. A PCIe switch is comprised of a bridge device representing the
1405  * upstream switch port and N bridges representing downstream switch ports. When
1406  * bridges stack the grand-parent of a downstream switch port is another
1407  * downstream switch port in the immediate ancestor switch.
1408  */
grandparent(struct device * dev)1409 static struct device *grandparent(struct device *dev)
1410 {
1411 	if (dev && dev->parent)
1412 		return dev->parent->parent;
1413 	return NULL;
1414 }
1415 
delete_endpoint(void * data)1416 static void delete_endpoint(void *data)
1417 {
1418 	struct cxl_memdev *cxlmd = data;
1419 	struct cxl_port *endpoint = cxlmd->endpoint;
1420 	struct device *host = port_to_host(endpoint);
1421 
1422 	scoped_guard(device, host) {
1423 		if (host->driver && !endpoint->dead) {
1424 			devm_release_action(host, cxl_unlink_parent_dport, endpoint);
1425 			devm_release_action(host, cxl_unlink_uport, endpoint);
1426 			devm_release_action(host, unregister_port, endpoint);
1427 		}
1428 		cxlmd->endpoint = NULL;
1429 	}
1430 	put_device(&endpoint->dev);
1431 	put_device(host);
1432 }
1433 
cxl_endpoint_autoremove(struct cxl_memdev * cxlmd,struct cxl_port * endpoint)1434 int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint)
1435 {
1436 	struct device *host = port_to_host(endpoint);
1437 	struct device *dev = &cxlmd->dev;
1438 
1439 	get_device(host);
1440 	get_device(&endpoint->dev);
1441 	cxlmd->depth = endpoint->depth;
1442 	return devm_add_action_or_reset(dev, delete_endpoint, cxlmd);
1443 }
1444 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_autoremove, "CXL");
1445 
1446 /*
1447  * The natural end of life of a non-root 'cxl_port' is when its parent port goes
1448  * through a ->remove() event ("top-down" unregistration). The unnatural trigger
1449  * for a port to be unregistered is when all memdevs beneath that port have gone
1450  * through ->remove(). This "bottom-up" removal selectively removes individual
1451  * child ports manually. This depends on devm_cxl_add_port() to not change is
1452  * devm action registration order, and for dports to have already been
1453  * destroyed by del_dports().
1454  */
delete_switch_port(struct cxl_port * port)1455 static void delete_switch_port(struct cxl_port *port)
1456 {
1457 	devm_release_action(port->dev.parent, cxl_unlink_parent_dport, port);
1458 	devm_release_action(port->dev.parent, cxl_unlink_uport, port);
1459 	devm_release_action(port->dev.parent, unregister_port, port);
1460 }
1461 
del_dports(struct cxl_port * port)1462 static void del_dports(struct cxl_port *port)
1463 {
1464 	struct cxl_dport *dport;
1465 	unsigned long index;
1466 
1467 	device_lock_assert(&port->dev);
1468 
1469 	xa_for_each(&port->dports, index, dport)
1470 		del_dport(dport);
1471 }
1472 
1473 struct detach_ctx {
1474 	struct cxl_memdev *cxlmd;
1475 	int depth;
1476 };
1477 
port_has_memdev(struct device * dev,const void * data)1478 static int port_has_memdev(struct device *dev, const void *data)
1479 {
1480 	const struct detach_ctx *ctx = data;
1481 	struct cxl_port *port;
1482 
1483 	if (!is_cxl_port(dev))
1484 		return 0;
1485 
1486 	port = to_cxl_port(dev);
1487 	if (port->depth != ctx->depth)
1488 		return 0;
1489 
1490 	return !!cxl_ep_load(port, ctx->cxlmd);
1491 }
1492 
cxl_detach_ep(void * data)1493 static void cxl_detach_ep(void *data)
1494 {
1495 	struct cxl_memdev *cxlmd = data;
1496 
1497 	for (int i = cxlmd->depth - 1; i >= 1; i--) {
1498 		struct cxl_port *port, *parent_port;
1499 		struct detach_ctx ctx = {
1500 			.cxlmd = cxlmd,
1501 			.depth = i,
1502 		};
1503 		struct cxl_ep *ep;
1504 		bool died = false;
1505 
1506 		struct device *dev __free(put_device) =
1507 			bus_find_device(&cxl_bus_type, NULL, &ctx, port_has_memdev);
1508 		if (!dev)
1509 			continue;
1510 		port = to_cxl_port(dev);
1511 
1512 		parent_port = to_cxl_port(port->dev.parent);
1513 		device_lock(&parent_port->dev);
1514 		device_lock(&port->dev);
1515 		ep = cxl_ep_load(port, cxlmd);
1516 		dev_dbg(&cxlmd->dev, "disconnect %s from %s\n",
1517 			ep ? dev_name(ep->ep) : "", dev_name(&port->dev));
1518 		cxl_ep_remove(port, ep);
1519 		if (ep && !port->dead && xa_empty(&port->endpoints) &&
1520 		    !is_cxl_root(parent_port) && parent_port->dev.driver) {
1521 			/*
1522 			 * This was the last ep attached to a dynamically
1523 			 * enumerated port. Block new cxl_add_ep() and garbage
1524 			 * collect the port.
1525 			 */
1526 			died = true;
1527 			port->dead = true;
1528 			del_dports(port);
1529 		}
1530 		device_unlock(&port->dev);
1531 
1532 		if (died) {
1533 			dev_dbg(&cxlmd->dev, "delete %s\n",
1534 				dev_name(&port->dev));
1535 			delete_switch_port(port);
1536 		}
1537 		device_unlock(&parent_port->dev);
1538 	}
1539 }
1540 
find_component_registers(struct device * dev)1541 static resource_size_t find_component_registers(struct device *dev)
1542 {
1543 	struct cxl_register_map map;
1544 	struct pci_dev *pdev;
1545 
1546 	/*
1547 	 * Theoretically, CXL component registers can be hosted on a
1548 	 * non-PCI device, in practice, only cxl_test hits this case.
1549 	 */
1550 	if (!dev_is_pci(dev))
1551 		return CXL_RESOURCE_NONE;
1552 
1553 	pdev = to_pci_dev(dev);
1554 
1555 	cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
1556 	return map.resource;
1557 }
1558 
match_port_by_uport(struct device * dev,const void * data)1559 static int match_port_by_uport(struct device *dev, const void *data)
1560 {
1561 	const struct device *uport_dev = data;
1562 	struct cxl_port *port;
1563 
1564 	if (!is_cxl_port(dev))
1565 		return 0;
1566 
1567 	port = to_cxl_port(dev);
1568 	/* Endpoint ports are hosted by memdevs */
1569 	if (is_cxl_memdev(port->uport_dev))
1570 		return uport_dev == port->uport_dev->parent;
1571 	return uport_dev == port->uport_dev;
1572 }
1573 
1574 /**
1575  * find_cxl_port_by_uport - Find a CXL port device companion
1576  * @uport_dev: Device that acts as a switch or endpoint in the CXL hierarchy
1577  *
1578  * In the case of endpoint ports recall that port->uport_dev points to a 'struct
1579  * cxl_memdev' device. So, the @uport_dev argument is the parent device of the
1580  * 'struct cxl_memdev' in that case.
1581  *
1582  * Function takes a device reference on the port device. Caller should do a
1583  * put_device() when done.
1584  */
find_cxl_port_by_uport(struct device * uport_dev)1585 static struct cxl_port *find_cxl_port_by_uport(struct device *uport_dev)
1586 {
1587 	struct device *dev;
1588 
1589 	dev = bus_find_device(&cxl_bus_type, NULL, uport_dev, match_port_by_uport);
1590 	if (dev)
1591 		return to_cxl_port(dev);
1592 	return NULL;
1593 }
1594 
update_decoder_targets(struct device * dev,void * data)1595 static int update_decoder_targets(struct device *dev, void *data)
1596 {
1597 	struct cxl_dport *dport = data;
1598 	struct cxl_switch_decoder *cxlsd;
1599 	struct cxl_decoder *cxld;
1600 	int i;
1601 
1602 	if (!is_switch_decoder(dev))
1603 		return 0;
1604 
1605 	cxlsd = to_cxl_switch_decoder(dev);
1606 	cxld = &cxlsd->cxld;
1607 	guard(rwsem_write)(&cxl_rwsem.region);
1608 
1609 	for (i = 0; i < cxld->interleave_ways; i++) {
1610 		if (cxld->target_map[i] == dport->port_id) {
1611 			cxlsd->target[i] = dport;
1612 			dev_dbg(dev, "dport%d found in target list, index %d\n",
1613 				dport->port_id, i);
1614 			return 0;
1615 		}
1616 	}
1617 
1618 	return 0;
1619 }
1620 
cxl_port_update_decoder_targets(struct cxl_port * port,struct cxl_dport * dport)1621 void cxl_port_update_decoder_targets(struct cxl_port *port,
1622 				     struct cxl_dport *dport)
1623 {
1624 	device_for_each_child(&port->dev, dport, update_decoder_targets);
1625 }
1626 EXPORT_SYMBOL_NS_GPL(cxl_port_update_decoder_targets, "CXL");
1627 
dport_exists(struct cxl_port * port,struct device * dport_dev)1628 static bool dport_exists(struct cxl_port *port, struct device *dport_dev)
1629 {
1630 	struct cxl_dport *dport = cxl_find_dport_by_dev(port, dport_dev);
1631 
1632 	if (dport) {
1633 		dev_dbg(&port->dev, "dport%d:%s already exists\n",
1634 			dport->port_id, dev_name(dport_dev));
1635 		return true;
1636 	}
1637 
1638 	return false;
1639 }
1640 
probe_dport(struct cxl_port * port,struct device * dport_dev)1641 static struct cxl_dport *probe_dport(struct cxl_port *port,
1642 				     struct device *dport_dev)
1643 {
1644 	struct cxl_driver *drv;
1645 
1646 	device_lock_assert(&port->dev);
1647 	if (!port->dev.driver)
1648 		return ERR_PTR(-ENXIO);
1649 
1650 	if (dport_exists(port, dport_dev))
1651 		return ERR_PTR(-EBUSY);
1652 
1653 	drv = container_of(port->dev.driver, struct cxl_driver, drv);
1654 	if (!drv->add_dport)
1655 		return ERR_PTR(-ENXIO);
1656 
1657 	/* see cxl_port_add_dport() */
1658 	return drv->add_dport(port, dport_dev);
1659 }
1660 
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)1661 static struct cxl_dport *devm_cxl_create_port(struct device *ep_dev,
1662 					      struct cxl_port *parent_port,
1663 					      struct cxl_dport *parent_dport,
1664 					      struct device *uport_dev,
1665 					      struct device *dport_dev)
1666 {
1667 	resource_size_t component_reg_phys;
1668 
1669 	device_lock_assert(&parent_port->dev);
1670 	if (!parent_port->dev.driver) {
1671 		dev_warn(ep_dev,
1672 			 "port %s:%s:%s disabled, failed to enumerate CXL.mem\n",
1673 			 dev_name(&parent_port->dev), dev_name(uport_dev),
1674 			 dev_name(dport_dev));
1675 	}
1676 
1677 	struct cxl_port *port __free(put_cxl_port) =
1678 		find_cxl_port_by_uport(uport_dev);
1679 	if (!port) {
1680 		component_reg_phys = find_component_registers(uport_dev);
1681 		port = devm_cxl_add_port(&parent_port->dev, uport_dev,
1682 					 component_reg_phys, parent_dport);
1683 		if (IS_ERR(port))
1684 			return ERR_CAST(port);
1685 
1686 		/*
1687 		 * retry to make sure a port is found. a port device
1688 		 * reference is taken.
1689 		 */
1690 		port = find_cxl_port_by_uport(uport_dev);
1691 		if (!port)
1692 			return ERR_PTR(-ENODEV);
1693 
1694 		dev_dbg(ep_dev, "created port %s:%s\n",
1695 			dev_name(&port->dev), dev_name(port->uport_dev));
1696 	} else {
1697 		/*
1698 		 * Port was created before right before this function is
1699 		 * called. Signal the caller to deal with it.
1700 		 */
1701 		return ERR_PTR(-EAGAIN);
1702 	}
1703 
1704 	guard(device)(&port->dev);
1705 	return probe_dport(port, dport_dev);
1706 }
1707 
add_port_attach_ep(struct cxl_memdev * cxlmd,struct device * uport_dev,struct device * dport_dev)1708 static int add_port_attach_ep(struct cxl_memdev *cxlmd,
1709 			      struct device *uport_dev,
1710 			      struct device *dport_dev)
1711 {
1712 	struct device *dparent = grandparent(dport_dev);
1713 	struct cxl_dport *dport, *parent_dport;
1714 	int rc;
1715 
1716 	if (is_cxl_host_bridge(dparent)) {
1717 		/*
1718 		 * The iteration reached the topology root without finding the
1719 		 * CXL-root 'cxl_port' on a previous iteration, fail for now to
1720 		 * be re-probed after platform driver attaches.
1721 		 */
1722 		dev_dbg(&cxlmd->dev, "%s is a root dport\n",
1723 			dev_name(dport_dev));
1724 		return -ENXIO;
1725 	}
1726 
1727 	struct cxl_port *parent_port __free(put_cxl_port) =
1728 		find_cxl_port_by_uport(dparent->parent);
1729 	if (!parent_port) {
1730 		/* iterate to create this parent_port */
1731 		return -EAGAIN;
1732 	}
1733 
1734 	scoped_guard(device, &parent_port->dev) {
1735 		parent_dport = cxl_find_dport_by_dev(parent_port, dparent);
1736 		if (!parent_dport) {
1737 			parent_dport = probe_dport(parent_port, dparent);
1738 			if (IS_ERR(parent_dport))
1739 				return PTR_ERR(parent_dport);
1740 		}
1741 
1742 		dport = devm_cxl_create_port(&cxlmd->dev, parent_port,
1743 					     parent_dport, uport_dev,
1744 					     dport_dev);
1745 		if (IS_ERR(dport)) {
1746 			/* Port already exists, restart iteration */
1747 			if (PTR_ERR(dport) == -EAGAIN)
1748 				return 0;
1749 			return PTR_ERR(dport);
1750 		}
1751 	}
1752 
1753 	rc = cxl_add_ep(dport, &cxlmd->dev);
1754 	if (rc == -EBUSY) {
1755 		/*
1756 		 * "can't" happen, but this error code means
1757 		 * something to the caller, so translate it.
1758 		 */
1759 		rc = -ENXIO;
1760 	}
1761 
1762 	return rc;
1763 }
1764 
find_or_add_dport(struct cxl_port * port,struct device * dport_dev)1765 static struct cxl_dport *find_or_add_dport(struct cxl_port *port,
1766 					   struct device *dport_dev)
1767 {
1768 	struct cxl_dport *dport;
1769 
1770 	/*
1771 	 * The port is already visible in CXL hierarchy, but it may still
1772 	 * be in the process of binding to the CXL port driver at this point.
1773 	 *
1774 	 * port creation and driver binding are protected by the port's host
1775 	 * lock, so acquire the host lock here to ensure the port has completed
1776 	 * driver binding before proceeding with dport addition.
1777 	 */
1778 	guard(device)(port_to_host(port));
1779 	guard(device)(&port->dev);
1780 	dport = cxl_find_dport_by_dev(port, dport_dev);
1781 	if (!dport) {
1782 		dport = probe_dport(port, dport_dev);
1783 		if (IS_ERR(dport))
1784 			return dport;
1785 
1786 		/* New dport added, restart iteration */
1787 		return ERR_PTR(-EAGAIN);
1788 	}
1789 
1790 	return dport;
1791 }
1792 
devm_cxl_enumerate_ports(struct cxl_memdev * cxlmd)1793 int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd)
1794 {
1795 	struct device *dev = &cxlmd->dev;
1796 	struct device *iter;
1797 	int rc;
1798 
1799 	/*
1800 	 * Skip intermediate port enumeration in the RCH case, there
1801 	 * are no ports in between a host bridge and an endpoint.
1802 	 */
1803 	if (cxlmd->cxlds->rcd)
1804 		return 0;
1805 
1806 	rc = devm_add_action_or_reset(&cxlmd->dev, cxl_detach_ep, cxlmd);
1807 	if (rc)
1808 		return rc;
1809 
1810 	/*
1811 	 * Scan for and add all cxl_ports in this device's ancestry.
1812 	 * Repeat until no more ports are added. Abort if a port add
1813 	 * attempt fails.
1814 	 */
1815 retry:
1816 	for (iter = dev; iter; iter = grandparent(iter)) {
1817 		struct device *dport_dev = grandparent(iter);
1818 		struct device *uport_dev;
1819 		struct cxl_dport *dport;
1820 
1821 		if (is_cxl_host_bridge(dport_dev))
1822 			return 0;
1823 
1824 		uport_dev = dport_dev->parent;
1825 		if (!uport_dev) {
1826 			dev_warn(dev, "at %s no parent for dport: %s\n",
1827 				 dev_name(iter), dev_name(dport_dev));
1828 			return -ENXIO;
1829 		}
1830 
1831 		dev_dbg(dev, "scan: iter: %s dport_dev: %s parent: %s\n",
1832 			dev_name(iter), dev_name(dport_dev),
1833 			dev_name(uport_dev));
1834 		struct cxl_port *port __free(put_cxl_port) =
1835 			find_cxl_port_by_uport(uport_dev);
1836 		if (port) {
1837 			dev_dbg(&cxlmd->dev,
1838 				"found already registered port %s:%s\n",
1839 				dev_name(&port->dev),
1840 				dev_name(port->uport_dev));
1841 
1842 			/*
1843 			 * RP port enumerated by cxl_acpi without dport will
1844 			 * have the dport added here.
1845 			 */
1846 			dport = find_or_add_dport(port, dport_dev);
1847 			if (IS_ERR(dport)) {
1848 				if (PTR_ERR(dport) == -EAGAIN)
1849 					goto retry;
1850 				return PTR_ERR(dport);
1851 			}
1852 
1853 			rc = cxl_add_ep(dport, &cxlmd->dev);
1854 
1855 			/*
1856 			 * If the endpoint already exists in the port's list,
1857 			 * that's ok, it was added on a previous pass.
1858 			 * Otherwise, retry in add_port_attach_ep() after taking
1859 			 * the parent_port lock as the current port may be being
1860 			 * reaped.
1861 			 */
1862 			if (rc && rc != -EBUSY)
1863 				return rc;
1864 
1865 			cxl_gpf_port_setup(dport);
1866 
1867 			/* Any more ports to add between this one and the root? */
1868 			if (!dev_is_cxl_root_child(&port->dev))
1869 				continue;
1870 
1871 			return 0;
1872 		}
1873 
1874 		rc = add_port_attach_ep(cxlmd, uport_dev, dport_dev);
1875 		/* port missing, try to add parent */
1876 		if (rc == -EAGAIN)
1877 			continue;
1878 		/* failed to add ep or port */
1879 		if (rc)
1880 			return rc;
1881 		/* port added, new descendants possible, start over */
1882 		goto retry;
1883 	}
1884 
1885 	return 0;
1886 }
1887 EXPORT_SYMBOL_NS_GPL(devm_cxl_enumerate_ports, "CXL");
1888 
cxl_pci_find_port(struct pci_dev * pdev,struct cxl_dport ** dport)1889 struct cxl_port *cxl_pci_find_port(struct pci_dev *pdev,
1890 				   struct cxl_dport **dport)
1891 {
1892 	return find_cxl_port(pdev->dev.parent, dport);
1893 }
1894 EXPORT_SYMBOL_NS_GPL(cxl_pci_find_port, "CXL");
1895 
cxl_mem_find_port(struct cxl_memdev * cxlmd,struct cxl_dport ** dport)1896 struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd,
1897 				   struct cxl_dport **dport)
1898 {
1899 	return find_cxl_port(grandparent(&cxlmd->dev), dport);
1900 }
1901 EXPORT_SYMBOL_NS_GPL(cxl_mem_find_port, "CXL");
1902 
decoder_populate_targets(struct cxl_switch_decoder * cxlsd,struct cxl_port * port)1903 static int decoder_populate_targets(struct cxl_switch_decoder *cxlsd,
1904 				    struct cxl_port *port)
1905 {
1906 	struct cxl_decoder *cxld = &cxlsd->cxld;
1907 	int i;
1908 
1909 	device_lock_assert(&port->dev);
1910 
1911 	if (xa_empty(&port->dports))
1912 		return 0;
1913 
1914 	guard(rwsem_write)(&cxl_rwsem.region);
1915 	for (i = 0; i < cxlsd->cxld.interleave_ways; i++) {
1916 		struct cxl_dport *dport = find_dport(port, cxld->target_map[i]);
1917 
1918 		if (!dport) {
1919 			/* dport may be activated later */
1920 			continue;
1921 		}
1922 		cxlsd->target[i] = dport;
1923 	}
1924 
1925 	return 0;
1926 }
1927 
1928 static struct lock_class_key cxl_decoder_key;
1929 
1930 /**
1931  * cxl_decoder_init - Common decoder setup / initialization
1932  * @port: owning port of this decoder
1933  * @cxld: common decoder properties to initialize
1934  *
1935  * A port may contain one or more decoders. Each of those decoders
1936  * enable some address space for CXL.mem utilization. A decoder is
1937  * expected to be configured by the caller before registering via
1938  * cxl_decoder_add()
1939  */
cxl_decoder_init(struct cxl_port * port,struct cxl_decoder * cxld)1940 static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld)
1941 {
1942 	struct device *dev;
1943 	int rc;
1944 
1945 	rc = ida_alloc(&port->decoder_ida, GFP_KERNEL);
1946 	if (rc < 0)
1947 		return rc;
1948 
1949 	/* need parent to stick around to release the id */
1950 	get_device(&port->dev);
1951 	cxld->id = rc;
1952 
1953 	dev = &cxld->dev;
1954 	device_initialize(dev);
1955 	lockdep_set_class(&dev->mutex, &cxl_decoder_key);
1956 	device_set_pm_not_required(dev);
1957 	dev->parent = &port->dev;
1958 	dev->bus = &cxl_bus_type;
1959 
1960 	/* Pre initialize an "empty" decoder */
1961 	cxld->interleave_ways = 1;
1962 	cxld->interleave_granularity = PAGE_SIZE;
1963 	cxld->target_type = CXL_DECODER_HOSTONLYMEM;
1964 	cxld->hpa_range = (struct range) {
1965 		.start = 0,
1966 		.end = -1,
1967 	};
1968 
1969 	return 0;
1970 }
1971 
cxl_switch_decoder_init(struct cxl_port * port,struct cxl_switch_decoder * cxlsd,int nr_targets)1972 static int cxl_switch_decoder_init(struct cxl_port *port,
1973 				   struct cxl_switch_decoder *cxlsd,
1974 				   int nr_targets)
1975 {
1976 	if (nr_targets > CXL_DECODER_MAX_INTERLEAVE)
1977 		return -EINVAL;
1978 
1979 	cxlsd->nr_targets = nr_targets;
1980 	return cxl_decoder_init(port, &cxlsd->cxld);
1981 }
1982 
1983 /**
1984  * cxl_root_decoder_alloc - Allocate a root level decoder
1985  * @port: owning CXL root of this decoder
1986  * @nr_targets: static number of downstream targets
1987  *
1988  * Return: A new cxl decoder to be registered by cxl_decoder_add(). A
1989  * 'CXL root' decoder is one that decodes from a top-level / static platform
1990  * firmware description of CXL resources into a CXL standard decode
1991  * topology.
1992  */
cxl_root_decoder_alloc(struct cxl_port * port,unsigned int nr_targets)1993 struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
1994 						unsigned int nr_targets)
1995 {
1996 	struct cxl_root_decoder *cxlrd;
1997 	struct cxl_switch_decoder *cxlsd;
1998 	struct cxl_decoder *cxld;
1999 	int rc;
2000 
2001 	if (!is_cxl_root(port))
2002 		return ERR_PTR(-EINVAL);
2003 
2004 	cxlrd = kzalloc_flex(*cxlrd, cxlsd.target, nr_targets);
2005 	if (!cxlrd)
2006 		return ERR_PTR(-ENOMEM);
2007 
2008 	cxlsd = &cxlrd->cxlsd;
2009 	rc = cxl_switch_decoder_init(port, cxlsd, nr_targets);
2010 	if (rc) {
2011 		kfree(cxlrd);
2012 		return ERR_PTR(rc);
2013 	}
2014 
2015 	mutex_init(&cxlrd->range_lock);
2016 
2017 	cxld = &cxlsd->cxld;
2018 	cxld->dev.type = &cxl_decoder_root_type;
2019 	/*
2020 	 * cxl_root_decoder_release() special cases negative ids to
2021 	 * detect memregion_alloc() failures.
2022 	 */
2023 	atomic_set(&cxlrd->region_id, -1);
2024 	rc = memregion_alloc(GFP_KERNEL);
2025 	if (rc < 0) {
2026 		put_device(&cxld->dev);
2027 		return ERR_PTR(rc);
2028 	}
2029 
2030 	atomic_set(&cxlrd->region_id, rc);
2031 	cxlrd->qos_class = CXL_QOS_CLASS_INVALID;
2032 	return cxlrd;
2033 }
2034 EXPORT_SYMBOL_NS_GPL(cxl_root_decoder_alloc, "CXL");
2035 
2036 /**
2037  * cxl_switch_decoder_alloc - Allocate a switch level decoder
2038  * @port: owning CXL switch port of this decoder
2039  * @nr_targets: max number of dynamically addressable downstream targets
2040  *
2041  * Return: A new cxl decoder to be registered by cxl_decoder_add(). A
2042  * 'switch' decoder is any decoder that can be enumerated by PCIe
2043  * topology and the HDM Decoder Capability. This includes the decoders
2044  * that sit between Switch Upstream Ports / Switch Downstream Ports and
2045  * Host Bridges / Root Ports.
2046  */
cxl_switch_decoder_alloc(struct cxl_port * port,unsigned int nr_targets)2047 struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port,
2048 						    unsigned int nr_targets)
2049 {
2050 	struct cxl_switch_decoder *cxlsd;
2051 	struct cxl_decoder *cxld;
2052 	int rc;
2053 
2054 	if (is_cxl_root(port) || is_cxl_endpoint(port))
2055 		return ERR_PTR(-EINVAL);
2056 
2057 	cxlsd = kzalloc_flex(*cxlsd, target, nr_targets);
2058 	if (!cxlsd)
2059 		return ERR_PTR(-ENOMEM);
2060 
2061 	rc = cxl_switch_decoder_init(port, cxlsd, nr_targets);
2062 	if (rc) {
2063 		kfree(cxlsd);
2064 		return ERR_PTR(rc);
2065 	}
2066 
2067 	cxld = &cxlsd->cxld;
2068 	cxld->dev.type = &cxl_decoder_switch_type;
2069 	return cxlsd;
2070 }
2071 EXPORT_SYMBOL_NS_GPL(cxl_switch_decoder_alloc, "CXL");
2072 
2073 /**
2074  * cxl_endpoint_decoder_alloc - Allocate an endpoint decoder
2075  * @port: owning port of this decoder
2076  *
2077  * Return: A new cxl decoder to be registered by cxl_decoder_add()
2078  */
cxl_endpoint_decoder_alloc(struct cxl_port * port)2079 struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port)
2080 {
2081 	struct cxl_endpoint_decoder *cxled;
2082 	struct cxl_decoder *cxld;
2083 	int rc;
2084 
2085 	if (!is_cxl_endpoint(port))
2086 		return ERR_PTR(-EINVAL);
2087 
2088 	cxled = kzalloc_obj(*cxled);
2089 	if (!cxled)
2090 		return ERR_PTR(-ENOMEM);
2091 
2092 	cxled->pos = -1;
2093 	cxled->part = -1;
2094 	cxld = &cxled->cxld;
2095 	rc = cxl_decoder_init(port, cxld);
2096 	if (rc)	 {
2097 		kfree(cxled);
2098 		return ERR_PTR(rc);
2099 	}
2100 
2101 	cxld->dev.type = &cxl_decoder_endpoint_type;
2102 	return cxled;
2103 }
2104 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_decoder_alloc, "CXL");
2105 
2106 /**
2107  * cxl_decoder_add_locked - Add a decoder with targets
2108  * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc()
2109  *
2110  * Certain types of decoders may not have any targets. The main example of this
2111  * is an endpoint device. A more awkward example is a hostbridge whose root
2112  * ports get hot added (technically possible, though unlikely).
2113  *
2114  * This is the locked variant of cxl_decoder_add().
2115  *
2116  * Context: Process context. Expects the device lock of the port that owns the
2117  *	    @cxld to be held.
2118  *
2119  * Return: Negative error code if the decoder wasn't properly configured; else
2120  *	   returns 0.
2121  */
cxl_decoder_add_locked(struct cxl_decoder * cxld)2122 int cxl_decoder_add_locked(struct cxl_decoder *cxld)
2123 {
2124 	struct cxl_port *port;
2125 	struct device *dev;
2126 	int rc;
2127 
2128 	if (WARN_ON_ONCE(!cxld))
2129 		return -EINVAL;
2130 
2131 	if (WARN_ON_ONCE(IS_ERR(cxld)))
2132 		return PTR_ERR(cxld);
2133 
2134 	if (cxld->interleave_ways < 1)
2135 		return -EINVAL;
2136 
2137 	dev = &cxld->dev;
2138 
2139 	port = to_cxl_port(cxld->dev.parent);
2140 	if (!is_endpoint_decoder(dev)) {
2141 		struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
2142 
2143 		rc = decoder_populate_targets(cxlsd, port);
2144 		if (rc && (cxld->flags & CXL_DECODER_F_ENABLE)) {
2145 			dev_err(&port->dev,
2146 				"Failed to populate active decoder targets\n");
2147 			return rc;
2148 		}
2149 	}
2150 
2151 	rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
2152 	if (rc)
2153 		return rc;
2154 
2155 	return device_add(dev);
2156 }
2157 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add_locked, "CXL");
2158 
2159 /**
2160  * cxl_decoder_add - Add a decoder with targets
2161  * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc()
2162  *
2163  * This is the unlocked variant of cxl_decoder_add_locked().
2164  * See cxl_decoder_add_locked().
2165  *
2166  * Context: Process context. Takes and releases the device lock of the port that
2167  *	    owns the @cxld.
2168  */
cxl_decoder_add(struct cxl_decoder * cxld)2169 int cxl_decoder_add(struct cxl_decoder *cxld)
2170 {
2171 	struct cxl_port *port;
2172 
2173 	if (WARN_ON_ONCE(!cxld))
2174 		return -EINVAL;
2175 
2176 	if (WARN_ON_ONCE(IS_ERR(cxld)))
2177 		return PTR_ERR(cxld);
2178 
2179 	port = to_cxl_port(cxld->dev.parent);
2180 
2181 	guard(device)(&port->dev);
2182 	return cxl_decoder_add_locked(cxld);
2183 }
2184 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, "CXL");
2185 
cxld_unregister(void * dev)2186 static void cxld_unregister(void *dev)
2187 {
2188 	if (is_endpoint_decoder(dev))
2189 		cxl_decoder_detach(NULL, to_cxl_endpoint_decoder(dev), -1,
2190 				   DETACH_INVALIDATE);
2191 
2192 	device_unregister(dev);
2193 }
2194 
cxl_decoder_autoremove(struct device * host,struct cxl_decoder * cxld)2195 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld)
2196 {
2197 	return devm_add_action_or_reset(host, cxld_unregister, &cxld->dev);
2198 }
2199 EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, "CXL");
2200 
2201 /**
2202  * __cxl_driver_register - register a driver for the cxl bus
2203  * @cxl_drv: cxl driver structure to attach
2204  * @owner: owning module/driver
2205  * @modname: KBUILD_MODNAME for parent driver
2206  */
__cxl_driver_register(struct cxl_driver * cxl_drv,struct module * owner,const char * modname)2207 int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
2208 			  const char *modname)
2209 {
2210 	if (!cxl_drv->probe) {
2211 		pr_debug("%s ->probe() must be specified\n", modname);
2212 		return -EINVAL;
2213 	}
2214 
2215 	if (!cxl_drv->name) {
2216 		pr_debug("%s ->name must be specified\n", modname);
2217 		return -EINVAL;
2218 	}
2219 
2220 	if (!cxl_drv->id) {
2221 		pr_debug("%s ->id must be specified\n", modname);
2222 		return -EINVAL;
2223 	}
2224 
2225 	cxl_drv->drv.bus = &cxl_bus_type;
2226 	cxl_drv->drv.owner = owner;
2227 	cxl_drv->drv.mod_name = modname;
2228 	cxl_drv->drv.name = cxl_drv->name;
2229 
2230 	return driver_register(&cxl_drv->drv);
2231 }
2232 EXPORT_SYMBOL_NS_GPL(__cxl_driver_register, "CXL");
2233 
cxl_driver_unregister(struct cxl_driver * cxl_drv)2234 void cxl_driver_unregister(struct cxl_driver *cxl_drv)
2235 {
2236 	driver_unregister(&cxl_drv->drv);
2237 }
2238 EXPORT_SYMBOL_NS_GPL(cxl_driver_unregister, "CXL");
2239 
cxl_bus_uevent(const struct device * dev,struct kobj_uevent_env * env)2240 static int cxl_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
2241 {
2242 	return add_uevent_var(env, "MODALIAS=" CXL_MODALIAS_FMT,
2243 			      cxl_device_id(dev));
2244 }
2245 
cxl_bus_match(struct device * dev,const struct device_driver * drv)2246 static int cxl_bus_match(struct device *dev, const struct device_driver *drv)
2247 {
2248 	return cxl_device_id(dev) == to_cxl_drv(drv)->id;
2249 }
2250 
cxl_bus_probe(struct device * dev)2251 static int cxl_bus_probe(struct device *dev)
2252 {
2253 	int rc;
2254 
2255 	rc = to_cxl_drv(dev->driver)->probe(dev);
2256 	dev_dbg(dev, "probe: %d\n", rc);
2257 	return rc;
2258 }
2259 
cxl_bus_remove(struct device * dev)2260 static void cxl_bus_remove(struct device *dev)
2261 {
2262 	struct cxl_driver *cxl_drv = to_cxl_drv(dev->driver);
2263 
2264 	if (cxl_drv->remove)
2265 		cxl_drv->remove(dev);
2266 }
2267 
2268 static struct workqueue_struct *cxl_bus_wq;
2269 
cxl_rescan_attach(struct device * dev,void * data)2270 static int cxl_rescan_attach(struct device *dev, void *data)
2271 {
2272 	int rc = device_attach(dev);
2273 
2274 	dev_vdbg(dev, "rescan: %s\n", rc ? "attach" : "detached");
2275 
2276 	return 0;
2277 }
2278 
cxl_bus_rescan_queue(struct work_struct * w)2279 static void cxl_bus_rescan_queue(struct work_struct *w)
2280 {
2281 	bus_for_each_dev(&cxl_bus_type, NULL, NULL, cxl_rescan_attach);
2282 }
2283 
cxl_bus_rescan(void)2284 void cxl_bus_rescan(void)
2285 {
2286 	static DECLARE_WORK(rescan_work, cxl_bus_rescan_queue);
2287 
2288 	queue_work(cxl_bus_wq, &rescan_work);
2289 }
2290 EXPORT_SYMBOL_NS_GPL(cxl_bus_rescan, "CXL");
2291 
cxl_bus_drain(void)2292 void cxl_bus_drain(void)
2293 {
2294 	drain_workqueue(cxl_bus_wq);
2295 }
2296 EXPORT_SYMBOL_NS_GPL(cxl_bus_drain, "CXL");
2297 
schedule_cxl_memdev_detach(struct cxl_memdev * cxlmd)2298 bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd)
2299 {
2300 	return queue_work(cxl_bus_wq, &cxlmd->detach_work);
2301 }
2302 EXPORT_SYMBOL_NS_GPL(schedule_cxl_memdev_detach, "CXL");
2303 
add_latency(struct access_coordinate * c,long latency)2304 static void add_latency(struct access_coordinate *c, long latency)
2305 {
2306 	for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
2307 		c[i].write_latency += latency;
2308 		c[i].read_latency += latency;
2309 	}
2310 }
2311 
coordinates_valid(struct access_coordinate * c)2312 static bool coordinates_valid(struct access_coordinate *c)
2313 {
2314 	for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
2315 		if (c[i].read_bandwidth && c[i].write_bandwidth &&
2316 		    c[i].read_latency && c[i].write_latency)
2317 			continue;
2318 		return false;
2319 	}
2320 
2321 	return true;
2322 }
2323 
set_min_bandwidth(struct access_coordinate * c,unsigned int bw)2324 static void set_min_bandwidth(struct access_coordinate *c, unsigned int bw)
2325 {
2326 	for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
2327 		c[i].write_bandwidth = min(c[i].write_bandwidth, bw);
2328 		c[i].read_bandwidth = min(c[i].read_bandwidth, bw);
2329 	}
2330 }
2331 
set_access_coordinates(struct access_coordinate * out,struct access_coordinate * in)2332 static void set_access_coordinates(struct access_coordinate *out,
2333 				   struct access_coordinate *in)
2334 {
2335 	for (int i = 0; i < ACCESS_COORDINATE_MAX; i++)
2336 		out[i] = in[i];
2337 }
2338 
parent_port_is_cxl_root(struct cxl_port * port)2339 static bool parent_port_is_cxl_root(struct cxl_port *port)
2340 {
2341 	return is_cxl_root(to_cxl_port(port->dev.parent));
2342 }
2343 
2344 /**
2345  * cxl_endpoint_get_perf_coordinates - Retrieve performance numbers stored in dports
2346  *				   of CXL path
2347  * @port: endpoint cxl_port
2348  * @coord: output performance data
2349  *
2350  * Return: errno on failure, 0 on success.
2351  */
cxl_endpoint_get_perf_coordinates(struct cxl_port * port,struct access_coordinate * coord)2352 int cxl_endpoint_get_perf_coordinates(struct cxl_port *port,
2353 				      struct access_coordinate *coord)
2354 {
2355 	struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev);
2356 	struct access_coordinate c[] = {
2357 		{
2358 			.read_bandwidth = UINT_MAX,
2359 			.write_bandwidth = UINT_MAX,
2360 		},
2361 		{
2362 			.read_bandwidth = UINT_MAX,
2363 			.write_bandwidth = UINT_MAX,
2364 		},
2365 	};
2366 	struct cxl_port *iter = port;
2367 	struct cxl_dport *dport;
2368 	struct pci_dev *pdev;
2369 	struct device *dev;
2370 	unsigned int bw;
2371 	bool is_cxl_root;
2372 
2373 	if (!is_cxl_endpoint(port))
2374 		return -EINVAL;
2375 
2376 	/*
2377 	 * Skip calculation for RCD. Expectation is HMAT already covers RCD case
2378 	 * since RCH does not support hotplug.
2379 	 */
2380 	if (cxlmd->cxlds->rcd)
2381 		return 0;
2382 
2383 	/*
2384 	 * Exit the loop when the parent port of the current iter port is cxl
2385 	 * root. The iterative loop starts at the endpoint and gathers the
2386 	 * latency of the CXL link from the current device/port to the connected
2387 	 * downstream port each iteration.
2388 	 */
2389 	do {
2390 		dport = iter->parent_dport;
2391 		iter = to_cxl_port(iter->dev.parent);
2392 		is_cxl_root = parent_port_is_cxl_root(iter);
2393 
2394 		/*
2395 		 * There's no valid access_coordinate for a root port since RPs do not
2396 		 * have CDAT and therefore needs to be skipped.
2397 		 */
2398 		if (!is_cxl_root) {
2399 			if (!coordinates_valid(dport->coord))
2400 				return -EINVAL;
2401 			cxl_coordinates_combine(c, c, dport->coord);
2402 		}
2403 		add_latency(c, dport->link_latency);
2404 	} while (!is_cxl_root);
2405 
2406 	dport = iter->parent_dport;
2407 	/* Retrieve HB coords */
2408 	if (!coordinates_valid(dport->coord))
2409 		return -EINVAL;
2410 	cxl_coordinates_combine(c, c, dport->coord);
2411 
2412 	dev = port->uport_dev->parent;
2413 	if (!dev_is_pci(dev))
2414 		return -ENODEV;
2415 
2416 	/* Get the calculated PCI paths bandwidth */
2417 	pdev = to_pci_dev(dev);
2418 	bw = pcie_bandwidth_available(pdev, NULL, NULL, NULL);
2419 	if (bw == 0)
2420 		return -ENXIO;
2421 	bw /= BITS_PER_BYTE;
2422 
2423 	set_min_bandwidth(c, bw);
2424 	set_access_coordinates(coord, c);
2425 
2426 	return 0;
2427 }
2428 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_get_perf_coordinates, "CXL");
2429 
cxl_port_get_switch_dport_bandwidth(struct cxl_port * port,struct access_coordinate * c)2430 int cxl_port_get_switch_dport_bandwidth(struct cxl_port *port,
2431 					struct access_coordinate *c)
2432 {
2433 	struct cxl_dport *dport = port->parent_dport;
2434 
2435 	/* Check this port is connected to a switch DSP and not an RP */
2436 	if (parent_port_is_cxl_root(to_cxl_port(port->dev.parent)))
2437 		return -ENODEV;
2438 
2439 	if (!coordinates_valid(dport->coord))
2440 		return -EINVAL;
2441 
2442 	for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
2443 		c[i].read_bandwidth = dport->coord[i].read_bandwidth;
2444 		c[i].write_bandwidth = dport->coord[i].write_bandwidth;
2445 	}
2446 
2447 	return 0;
2448 }
2449 
2450 /* for user tooling to ensure port disable work has completed */
flush_store(const struct bus_type * bus,const char * buf,size_t count)2451 static ssize_t flush_store(const struct bus_type *bus, const char *buf, size_t count)
2452 {
2453 	if (sysfs_streq(buf, "1")) {
2454 		flush_workqueue(cxl_bus_wq);
2455 		return count;
2456 	}
2457 
2458 	return -EINVAL;
2459 }
2460 
2461 static BUS_ATTR_WO(flush);
2462 
2463 static struct attribute *cxl_bus_attributes[] = {
2464 	&bus_attr_flush.attr,
2465 	NULL,
2466 };
2467 
2468 static struct attribute_group cxl_bus_attribute_group = {
2469 	.attrs = cxl_bus_attributes,
2470 };
2471 
2472 static const struct attribute_group *cxl_bus_attribute_groups[] = {
2473 	&cxl_bus_attribute_group,
2474 	NULL,
2475 };
2476 
2477 const struct bus_type cxl_bus_type = {
2478 	.name = "cxl",
2479 	.uevent = cxl_bus_uevent,
2480 	.match = cxl_bus_match,
2481 	.probe = cxl_bus_probe,
2482 	.remove = cxl_bus_remove,
2483 	.bus_groups = cxl_bus_attribute_groups,
2484 };
2485 EXPORT_SYMBOL_NS_GPL(cxl_bus_type, "CXL");
2486 
2487 static struct dentry *cxl_debugfs;
2488 
cxl_debugfs_create_dir(const char * dir)2489 struct dentry *cxl_debugfs_create_dir(const char *dir)
2490 {
2491 	return debugfs_create_dir(dir, cxl_debugfs);
2492 }
2493 EXPORT_SYMBOL_NS_GPL(cxl_debugfs_create_dir, "CXL");
2494 
cxl_core_init(void)2495 static __init int cxl_core_init(void)
2496 {
2497 	int rc;
2498 
2499 	cxl_debugfs = debugfs_create_dir("cxl", NULL);
2500 
2501 	if (einj_cxl_is_initialized())
2502 		debugfs_create_file("einj_types", 0400, cxl_debugfs, NULL,
2503 				    &einj_cxl_available_error_type_fops);
2504 
2505 	cxl_mbox_init();
2506 
2507 	rc = cxl_memdev_init();
2508 	if (rc)
2509 		return rc;
2510 
2511 	cxl_bus_wq = alloc_ordered_workqueue("cxl_port", 0);
2512 	if (!cxl_bus_wq) {
2513 		rc = -ENOMEM;
2514 		goto err_wq;
2515 	}
2516 
2517 	rc = bus_register(&cxl_bus_type);
2518 	if (rc)
2519 		goto err_bus;
2520 
2521 	rc = cxl_region_init();
2522 	if (rc)
2523 		goto err_region;
2524 
2525 	rc = cxl_ras_init();
2526 	if (rc)
2527 		goto err_ras;
2528 
2529 	return 0;
2530 
2531 err_ras:
2532 	cxl_region_exit();
2533 err_region:
2534 	bus_unregister(&cxl_bus_type);
2535 err_bus:
2536 	destroy_workqueue(cxl_bus_wq);
2537 err_wq:
2538 	cxl_memdev_exit();
2539 	return rc;
2540 }
2541 
cxl_core_exit(void)2542 static void cxl_core_exit(void)
2543 {
2544 	cxl_ras_exit();
2545 	cxl_region_exit();
2546 	bus_unregister(&cxl_bus_type);
2547 	destroy_workqueue(cxl_bus_wq);
2548 	cxl_memdev_exit();
2549 	debugfs_remove_recursive(cxl_debugfs);
2550 }
2551 
2552 subsys_initcall(cxl_core_init);
2553 module_exit(cxl_core_exit);
2554 MODULE_DESCRIPTION("CXL: Core Compute Express Link support");
2555 MODULE_LICENSE("GPL v2");
2556 MODULE_IMPORT_NS("CXL");
2557