1
2
3#include <linux/io-64-nonatomic-lo-hi.h>
4#include <linux/device.h>
5#include <linux/module.h>
6#include <linux/pci.h>
7#include <linux/slab.h>
8#include <linux/idr.h>
9#include <cxlmem.h>
10#include <cxl.h>
11#include "core.h"
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27static DEFINE_IDA(cxl_port_ida);
28
29static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
30 char *buf)
31{
32 return sysfs_emit(buf, "%s\n", dev->type->name);
33}
34static DEVICE_ATTR_RO(devtype);
35
36static struct attribute *cxl_base_attributes[] = {
37 &dev_attr_devtype.attr,
38 NULL,
39};
40
41struct attribute_group cxl_base_attribute_group = {
42 .attrs = cxl_base_attributes,
43};
44
45static ssize_t start_show(struct device *dev, struct device_attribute *attr,
46 char *buf)
47{
48 struct cxl_decoder *cxld = to_cxl_decoder(dev);
49
50 return sysfs_emit(buf, "%#llx\n", cxld->range.start);
51}
52static DEVICE_ATTR_RO(start);
53
54static ssize_t size_show(struct device *dev, struct device_attribute *attr,
55 char *buf)
56{
57 struct cxl_decoder *cxld = to_cxl_decoder(dev);
58
59 return sysfs_emit(buf, "%#llx\n", range_len(&cxld->range));
60}
61static DEVICE_ATTR_RO(size);
62
63#define CXL_DECODER_FLAG_ATTR(name, flag) \
64static ssize_t name##_show(struct device *dev, \
65 struct device_attribute *attr, char *buf) \
66{ \
67 struct cxl_decoder *cxld = to_cxl_decoder(dev); \
68 \
69 return sysfs_emit(buf, "%s\n", \
70 (cxld->flags & (flag)) ? "1" : "0"); \
71} \
72static DEVICE_ATTR_RO(name)
73
74CXL_DECODER_FLAG_ATTR(cap_pmem, CXL_DECODER_F_PMEM);
75CXL_DECODER_FLAG_ATTR(cap_ram, CXL_DECODER_F_RAM);
76CXL_DECODER_FLAG_ATTR(cap_type2, CXL_DECODER_F_TYPE2);
77CXL_DECODER_FLAG_ATTR(cap_type3, CXL_DECODER_F_TYPE3);
78CXL_DECODER_FLAG_ATTR(locked, CXL_DECODER_F_LOCK);
79
80static ssize_t target_type_show(struct device *dev,
81 struct device_attribute *attr, char *buf)
82{
83 struct cxl_decoder *cxld = to_cxl_decoder(dev);
84
85 switch (cxld->target_type) {
86 case CXL_DECODER_ACCELERATOR:
87 return sysfs_emit(buf, "accelerator\n");
88 case CXL_DECODER_EXPANDER:
89 return sysfs_emit(buf, "expander\n");
90 }
91 return -ENXIO;
92}
93static DEVICE_ATTR_RO(target_type);
94
95static ssize_t target_list_show(struct device *dev,
96 struct device_attribute *attr, char *buf)
97{
98 struct cxl_decoder *cxld = to_cxl_decoder(dev);
99 ssize_t offset = 0;
100 int i, rc = 0;
101
102 device_lock(dev);
103 for (i = 0; i < cxld->interleave_ways; i++) {
104 struct cxl_dport *dport = cxld->target[i];
105 struct cxl_dport *next = NULL;
106
107 if (!dport)
108 break;
109
110 if (i + 1 < cxld->interleave_ways)
111 next = cxld->target[i + 1];
112 rc = sysfs_emit_at(buf, offset, "%d%s", dport->port_id,
113 next ? "," : "");
114 if (rc < 0)
115 break;
116 offset += rc;
117 }
118 device_unlock(dev);
119
120 if (rc < 0)
121 return rc;
122
123 rc = sysfs_emit_at(buf, offset, "\n");
124 if (rc < 0)
125 return rc;
126
127 return offset + rc;
128}
129static DEVICE_ATTR_RO(target_list);
130
131static struct attribute *cxl_decoder_base_attrs[] = {
132 &dev_attr_start.attr,
133 &dev_attr_size.attr,
134 &dev_attr_locked.attr,
135 &dev_attr_target_list.attr,
136 NULL,
137};
138
139static struct attribute_group cxl_decoder_base_attribute_group = {
140 .attrs = cxl_decoder_base_attrs,
141};
142
143static struct attribute *cxl_decoder_root_attrs[] = {
144 &dev_attr_cap_pmem.attr,
145 &dev_attr_cap_ram.attr,
146 &dev_attr_cap_type2.attr,
147 &dev_attr_cap_type3.attr,
148 NULL,
149};
150
151static struct attribute_group cxl_decoder_root_attribute_group = {
152 .attrs = cxl_decoder_root_attrs,
153};
154
155static const struct attribute_group *cxl_decoder_root_attribute_groups[] = {
156 &cxl_decoder_root_attribute_group,
157 &cxl_decoder_base_attribute_group,
158 &cxl_base_attribute_group,
159 NULL,
160};
161
162static struct attribute *cxl_decoder_switch_attrs[] = {
163 &dev_attr_target_type.attr,
164 NULL,
165};
166
167static struct attribute_group cxl_decoder_switch_attribute_group = {
168 .attrs = cxl_decoder_switch_attrs,
169};
170
171static const struct attribute_group *cxl_decoder_switch_attribute_groups[] = {
172 &cxl_decoder_switch_attribute_group,
173 &cxl_decoder_base_attribute_group,
174 &cxl_base_attribute_group,
175 NULL,
176};
177
178static void cxl_decoder_release(struct device *dev)
179{
180 struct cxl_decoder *cxld = to_cxl_decoder(dev);
181 struct cxl_port *port = to_cxl_port(dev->parent);
182
183 ida_free(&port->decoder_ida, cxld->id);
184 kfree(cxld);
185}
186
187static const struct device_type cxl_decoder_switch_type = {
188 .name = "cxl_decoder_switch",
189 .release = cxl_decoder_release,
190 .groups = cxl_decoder_switch_attribute_groups,
191};
192
193static const struct device_type cxl_decoder_root_type = {
194 .name = "cxl_decoder_root",
195 .release = cxl_decoder_release,
196 .groups = cxl_decoder_root_attribute_groups,
197};
198
199bool is_root_decoder(struct device *dev)
200{
201 return dev->type == &cxl_decoder_root_type;
202}
203EXPORT_SYMBOL_NS_GPL(is_root_decoder, CXL);
204
205struct cxl_decoder *to_cxl_decoder(struct device *dev)
206{
207 if (dev_WARN_ONCE(dev, dev->type->release != cxl_decoder_release,
208 "not a cxl_decoder device\n"))
209 return NULL;
210 return container_of(dev, struct cxl_decoder, dev);
211}
212EXPORT_SYMBOL_NS_GPL(to_cxl_decoder, CXL);
213
214static void cxl_dport_release(struct cxl_dport *dport)
215{
216 list_del(&dport->list);
217 put_device(dport->dport);
218 kfree(dport);
219}
220
221static void cxl_port_release(struct device *dev)
222{
223 struct cxl_port *port = to_cxl_port(dev);
224 struct cxl_dport *dport, *_d;
225
226 device_lock(dev);
227 list_for_each_entry_safe(dport, _d, &port->dports, list)
228 cxl_dport_release(dport);
229 device_unlock(dev);
230 ida_free(&cxl_port_ida, port->id);
231 kfree(port);
232}
233
234static const struct attribute_group *cxl_port_attribute_groups[] = {
235 &cxl_base_attribute_group,
236 NULL,
237};
238
239static const struct device_type cxl_port_type = {
240 .name = "cxl_port",
241 .release = cxl_port_release,
242 .groups = cxl_port_attribute_groups,
243};
244
245struct cxl_port *to_cxl_port(struct device *dev)
246{
247 if (dev_WARN_ONCE(dev, dev->type != &cxl_port_type,
248 "not a cxl_port device\n"))
249 return NULL;
250 return container_of(dev, struct cxl_port, dev);
251}
252
253static void unregister_port(void *_port)
254{
255 struct cxl_port *port = _port;
256 struct cxl_dport *dport;
257
258 device_lock(&port->dev);
259 list_for_each_entry(dport, &port->dports, list) {
260 char link_name[CXL_TARGET_STRLEN];
261
262 if (snprintf(link_name, CXL_TARGET_STRLEN, "dport%d",
263 dport->port_id) >= CXL_TARGET_STRLEN)
264 continue;
265 sysfs_remove_link(&port->dev.kobj, link_name);
266 }
267 device_unlock(&port->dev);
268 device_unregister(&port->dev);
269}
270
271static void cxl_unlink_uport(void *_port)
272{
273 struct cxl_port *port = _port;
274
275 sysfs_remove_link(&port->dev.kobj, "uport");
276}
277
278static int devm_cxl_link_uport(struct device *host, struct cxl_port *port)
279{
280 int rc;
281
282 rc = sysfs_create_link(&port->dev.kobj, &port->uport->kobj, "uport");
283 if (rc)
284 return rc;
285 return devm_add_action_or_reset(host, cxl_unlink_uport, port);
286}
287
288static struct cxl_port *cxl_port_alloc(struct device *uport,
289 resource_size_t component_reg_phys,
290 struct cxl_port *parent_port)
291{
292 struct cxl_port *port;
293 struct device *dev;
294 int rc;
295
296 port = kzalloc(sizeof(*port), GFP_KERNEL);
297 if (!port)
298 return ERR_PTR(-ENOMEM);
299
300 rc = ida_alloc(&cxl_port_ida, GFP_KERNEL);
301 if (rc < 0)
302 goto err;
303 port->id = rc;
304
305
306
307
308
309
310
311 dev = &port->dev;
312 if (parent_port)
313 dev->parent = &parent_port->dev;
314 else
315 dev->parent = uport;
316
317 port->uport = uport;
318 port->component_reg_phys = component_reg_phys;
319 ida_init(&port->decoder_ida);
320 INIT_LIST_HEAD(&port->dports);
321
322 device_initialize(dev);
323 device_set_pm_not_required(dev);
324 dev->bus = &cxl_bus_type;
325 dev->type = &cxl_port_type;
326
327 return port;
328
329err:
330 kfree(port);
331 return ERR_PTR(rc);
332}
333
334
335
336
337
338
339
340
341struct cxl_port *devm_cxl_add_port(struct device *host, struct device *uport,
342 resource_size_t component_reg_phys,
343 struct cxl_port *parent_port)
344{
345 struct cxl_port *port;
346 struct device *dev;
347 int rc;
348
349 port = cxl_port_alloc(uport, component_reg_phys, parent_port);
350 if (IS_ERR(port))
351 return port;
352
353 dev = &port->dev;
354 if (parent_port)
355 rc = dev_set_name(dev, "port%d", port->id);
356 else
357 rc = dev_set_name(dev, "root%d", port->id);
358 if (rc)
359 goto err;
360
361 rc = device_add(dev);
362 if (rc)
363 goto err;
364
365 rc = devm_add_action_or_reset(host, unregister_port, port);
366 if (rc)
367 return ERR_PTR(rc);
368
369 rc = devm_cxl_link_uport(host, port);
370 if (rc)
371 return ERR_PTR(rc);
372
373 return port;
374
375err:
376 put_device(dev);
377 return ERR_PTR(rc);
378}
379EXPORT_SYMBOL_NS_GPL(devm_cxl_add_port, CXL);
380
381static struct cxl_dport *find_dport(struct cxl_port *port, int id)
382{
383 struct cxl_dport *dport;
384
385 device_lock_assert(&port->dev);
386 list_for_each_entry (dport, &port->dports, list)
387 if (dport->port_id == id)
388 return dport;
389 return NULL;
390}
391
392static int add_dport(struct cxl_port *port, struct cxl_dport *new)
393{
394 struct cxl_dport *dup;
395
396 device_lock(&port->dev);
397 dup = find_dport(port, new->port_id);
398 if (dup)
399 dev_err(&port->dev,
400 "unable to add dport%d-%s non-unique port id (%s)\n",
401 new->port_id, dev_name(new->dport),
402 dev_name(dup->dport));
403 else
404 list_add_tail(&new->list, &port->dports);
405 device_unlock(&port->dev);
406
407 return dup ? -EEXIST : 0;
408}
409
410
411
412
413
414
415
416
417
418
419
420int cxl_add_dport(struct cxl_port *port, struct device *dport_dev, int port_id,
421 resource_size_t component_reg_phys)
422{
423 char link_name[CXL_TARGET_STRLEN];
424 struct cxl_dport *dport;
425 int rc;
426
427 if (snprintf(link_name, CXL_TARGET_STRLEN, "dport%d", port_id) >=
428 CXL_TARGET_STRLEN)
429 return -EINVAL;
430
431 dport = kzalloc(sizeof(*dport), GFP_KERNEL);
432 if (!dport)
433 return -ENOMEM;
434
435 INIT_LIST_HEAD(&dport->list);
436 dport->dport = get_device(dport_dev);
437 dport->port_id = port_id;
438 dport->component_reg_phys = component_reg_phys;
439 dport->port = port;
440
441 rc = add_dport(port, dport);
442 if (rc)
443 goto err;
444
445 rc = sysfs_create_link(&port->dev.kobj, &dport_dev->kobj, link_name);
446 if (rc)
447 goto err;
448
449 return 0;
450err:
451 cxl_dport_release(dport);
452 return rc;
453}
454EXPORT_SYMBOL_NS_GPL(cxl_add_dport, CXL);
455
456static int decoder_populate_targets(struct cxl_decoder *cxld,
457 struct cxl_port *port, int *target_map)
458{
459 int rc = 0, i;
460
461 if (!target_map)
462 return 0;
463
464 device_lock(&port->dev);
465 if (list_empty(&port->dports)) {
466 rc = -EINVAL;
467 goto out_unlock;
468 }
469
470 for (i = 0; i < cxld->nr_targets; i++) {
471 struct cxl_dport *dport = find_dport(port, target_map[i]);
472
473 if (!dport) {
474 rc = -ENXIO;
475 goto out_unlock;
476 }
477 cxld->target[i] = dport;
478 }
479
480out_unlock:
481 device_unlock(&port->dev);
482
483 return rc;
484}
485
486struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets)
487{
488 struct cxl_decoder *cxld;
489 struct device *dev;
490 int rc = 0;
491
492 if (nr_targets > CXL_DECODER_MAX_INTERLEAVE || nr_targets < 1)
493 return ERR_PTR(-EINVAL);
494
495 cxld = kzalloc(struct_size(cxld, target, nr_targets), GFP_KERNEL);
496 if (!cxld)
497 return ERR_PTR(-ENOMEM);
498
499 rc = ida_alloc(&port->decoder_ida, GFP_KERNEL);
500 if (rc < 0)
501 goto err;
502
503 cxld->id = rc;
504 cxld->nr_targets = nr_targets;
505 dev = &cxld->dev;
506 device_initialize(dev);
507 device_set_pm_not_required(dev);
508 dev->parent = &port->dev;
509 dev->bus = &cxl_bus_type;
510
511
512 if (port->dev.parent->type == &cxl_port_type)
513 dev->type = &cxl_decoder_switch_type;
514 else
515 dev->type = &cxl_decoder_root_type;
516
517 return cxld;
518err:
519 kfree(cxld);
520 return ERR_PTR(rc);
521}
522EXPORT_SYMBOL_NS_GPL(cxl_decoder_alloc, CXL);
523
524int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
525{
526 struct cxl_port *port;
527 struct device *dev;
528 int rc;
529
530 if (WARN_ON_ONCE(!cxld))
531 return -EINVAL;
532
533 if (WARN_ON_ONCE(IS_ERR(cxld)))
534 return PTR_ERR(cxld);
535
536 if (cxld->interleave_ways < 1)
537 return -EINVAL;
538
539 port = to_cxl_port(cxld->dev.parent);
540 rc = decoder_populate_targets(cxld, port, target_map);
541 if (rc)
542 return rc;
543
544 dev = &cxld->dev;
545 rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
546 if (rc)
547 return rc;
548
549 return device_add(dev);
550}
551EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, CXL);
552
553static void cxld_unregister(void *dev)
554{
555 device_unregister(dev);
556}
557
558int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld)
559{
560 return devm_add_action_or_reset(host, cxld_unregister, &cxld->dev);
561}
562EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, CXL);
563
564
565
566
567
568
569
570int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
571 const char *modname)
572{
573 if (!cxl_drv->probe) {
574 pr_debug("%s ->probe() must be specified\n", modname);
575 return -EINVAL;
576 }
577
578 if (!cxl_drv->name) {
579 pr_debug("%s ->name must be specified\n", modname);
580 return -EINVAL;
581 }
582
583 if (!cxl_drv->id) {
584 pr_debug("%s ->id must be specified\n", modname);
585 return -EINVAL;
586 }
587
588 cxl_drv->drv.bus = &cxl_bus_type;
589 cxl_drv->drv.owner = owner;
590 cxl_drv->drv.mod_name = modname;
591 cxl_drv->drv.name = cxl_drv->name;
592
593 return driver_register(&cxl_drv->drv);
594}
595EXPORT_SYMBOL_NS_GPL(__cxl_driver_register, CXL);
596
597void cxl_driver_unregister(struct cxl_driver *cxl_drv)
598{
599 driver_unregister(&cxl_drv->drv);
600}
601EXPORT_SYMBOL_NS_GPL(cxl_driver_unregister, CXL);
602
603static int cxl_device_id(struct device *dev)
604{
605 if (dev->type == &cxl_nvdimm_bridge_type)
606 return CXL_DEVICE_NVDIMM_BRIDGE;
607 if (dev->type == &cxl_nvdimm_type)
608 return CXL_DEVICE_NVDIMM;
609 return 0;
610}
611
612static int cxl_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
613{
614 return add_uevent_var(env, "MODALIAS=" CXL_MODALIAS_FMT,
615 cxl_device_id(dev));
616}
617
618static int cxl_bus_match(struct device *dev, struct device_driver *drv)
619{
620 return cxl_device_id(dev) == to_cxl_drv(drv)->id;
621}
622
623static int cxl_bus_probe(struct device *dev)
624{
625 return to_cxl_drv(dev->driver)->probe(dev);
626}
627
628static void cxl_bus_remove(struct device *dev)
629{
630 struct cxl_driver *cxl_drv = to_cxl_drv(dev->driver);
631
632 if (cxl_drv->remove)
633 cxl_drv->remove(dev);
634}
635
636struct bus_type cxl_bus_type = {
637 .name = "cxl",
638 .uevent = cxl_bus_uevent,
639 .match = cxl_bus_match,
640 .probe = cxl_bus_probe,
641 .remove = cxl_bus_remove,
642};
643EXPORT_SYMBOL_NS_GPL(cxl_bus_type, CXL);
644
645static __init int cxl_core_init(void)
646{
647 int rc;
648
649 cxl_mbox_init();
650
651 rc = cxl_memdev_init();
652 if (rc)
653 return rc;
654
655 rc = bus_register(&cxl_bus_type);
656 if (rc)
657 goto err;
658 return 0;
659
660err:
661 cxl_memdev_exit();
662 cxl_mbox_exit();
663 return rc;
664}
665
666static void cxl_core_exit(void)
667{
668 bus_unregister(&cxl_bus_type);
669 cxl_memdev_exit();
670 cxl_mbox_exit();
671}
672
673module_init(cxl_core_init);
674module_exit(cxl_core_exit);
675MODULE_LICENSE("GPL v2");
676