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