1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2020 Intel Corporation. All rights reserved. */
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 * DOC: cxl core
15 *
16 * The CXL core provides a set of interfaces that can be consumed by CXL aware
17 * drivers. The interfaces allow for creation, modification, and destruction of
18 * regions, memory devices, ports, and decoders. CXL aware drivers must register
19 * with the CXL core via these interfaces in order to be able to participate in
20 * cross-device interleave coordination. The CXL core also establishes and
21 * maintains the bridge to the nvdimm subsystem.
22 *
23 * CXL core introduces sysfs hierarchy to control the devices that are
24 * instantiated by the core.
25 */
26
27 static DEFINE_IDA(cxl_port_ida);
28
devtype_show(struct device * dev,struct device_attribute * attr,char * buf)29 static 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 }
34 static DEVICE_ATTR_RO(devtype);
35
36 static struct attribute *cxl_base_attributes[] = {
37 &dev_attr_devtype.attr,
38 NULL,
39 };
40
41 struct attribute_group cxl_base_attribute_group = {
42 .attrs = cxl_base_attributes,
43 };
44
start_show(struct device * dev,struct device_attribute * attr,char * buf)45 static 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 }
52 static DEVICE_ATTR_RO(start);
53
size_show(struct device * dev,struct device_attribute * attr,char * buf)54 static 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 }
61 static DEVICE_ATTR_RO(size);
62
63 #define CXL_DECODER_FLAG_ATTR(name, flag) \
64 static 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 } \
72 static DEVICE_ATTR_RO(name)
73
74 CXL_DECODER_FLAG_ATTR(cap_pmem, CXL_DECODER_F_PMEM);
75 CXL_DECODER_FLAG_ATTR(cap_ram, CXL_DECODER_F_RAM);
76 CXL_DECODER_FLAG_ATTR(cap_type2, CXL_DECODER_F_TYPE2);
77 CXL_DECODER_FLAG_ATTR(cap_type3, CXL_DECODER_F_TYPE3);
78 CXL_DECODER_FLAG_ATTR(locked, CXL_DECODER_F_LOCK);
79
target_type_show(struct device * dev,struct device_attribute * attr,char * buf)80 static 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 }
93 static DEVICE_ATTR_RO(target_type);
94
target_list_show(struct device * dev,struct device_attribute * attr,char * buf)95 static 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 }
129 static DEVICE_ATTR_RO(target_list);
130
131 static 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
139 static struct attribute_group cxl_decoder_base_attribute_group = {
140 .attrs = cxl_decoder_base_attrs,
141 };
142
143 static 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
151 static struct attribute_group cxl_decoder_root_attribute_group = {
152 .attrs = cxl_decoder_root_attrs,
153 };
154
155 static 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
162 static struct attribute *cxl_decoder_switch_attrs[] = {
163 &dev_attr_target_type.attr,
164 NULL,
165 };
166
167 static struct attribute_group cxl_decoder_switch_attribute_group = {
168 .attrs = cxl_decoder_switch_attrs,
169 };
170
171 static 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
cxl_decoder_release(struct device * dev)178 static 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
187 static 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
193 static 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
is_root_decoder(struct device * dev)199 bool is_root_decoder(struct device *dev)
200 {
201 return dev->type == &cxl_decoder_root_type;
202 }
203 EXPORT_SYMBOL_GPL(is_root_decoder);
204
to_cxl_decoder(struct device * dev)205 struct 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 }
212 EXPORT_SYMBOL_GPL(to_cxl_decoder);
213
cxl_dport_release(struct cxl_dport * dport)214 static void cxl_dport_release(struct cxl_dport *dport)
215 {
216 list_del(&dport->list);
217 put_device(dport->dport);
218 kfree(dport);
219 }
220
cxl_port_release(struct device * dev)221 static 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
234 static const struct attribute_group *cxl_port_attribute_groups[] = {
235 &cxl_base_attribute_group,
236 NULL,
237 };
238
239 static const struct device_type cxl_port_type = {
240 .name = "cxl_port",
241 .release = cxl_port_release,
242 .groups = cxl_port_attribute_groups,
243 };
244
to_cxl_port(struct device * dev)245 struct 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
unregister_port(void * _port)253 static 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
cxl_unlink_uport(void * _port)271 static void cxl_unlink_uport(void *_port)
272 {
273 struct cxl_port *port = _port;
274
275 sysfs_remove_link(&port->dev.kobj, "uport");
276 }
277
devm_cxl_link_uport(struct device * host,struct cxl_port * port)278 static 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
cxl_port_alloc(struct device * uport,resource_size_t component_reg_phys,struct cxl_port * parent_port)288 static 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 * The top-level cxl_port "cxl_root" does not have a cxl_port as
307 * its parent and it does not have any corresponding component
308 * registers as its decode is described by a fixed platform
309 * description.
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
329 err:
330 kfree(port);
331 return ERR_PTR(rc);
332 }
333
334 /**
335 * devm_cxl_add_port - register a cxl_port in CXL memory decode hierarchy
336 * @host: host device for devm operations
337 * @uport: "physical" device implementing this upstream port
338 * @component_reg_phys: (optional) for configurable cxl_port instances
339 * @parent_port: next hop up in the CXL memory decode hierarchy
340 */
devm_cxl_add_port(struct device * host,struct device * uport,resource_size_t component_reg_phys,struct cxl_port * parent_port)341 struct 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
375 err:
376 put_device(dev);
377 return ERR_PTR(rc);
378 }
379 EXPORT_SYMBOL_GPL(devm_cxl_add_port);
380
find_dport(struct cxl_port * port,int id)381 static 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
add_dport(struct cxl_port * port,struct cxl_dport * new)392 static 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 * cxl_add_dport - append downstream port data to a cxl_port
412 * @port: the cxl_port that references this dport
413 * @dport_dev: firmware or PCI device representing the dport
414 * @port_id: identifier for this dport in a decoder's target list
415 * @component_reg_phys: optional location of CXL component registers
416 *
417 * Note that all allocations and links are undone by cxl_port deletion
418 * and release.
419 */
cxl_add_dport(struct cxl_port * port,struct device * dport_dev,int port_id,resource_size_t component_reg_phys)420 int 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;
450 err:
451 cxl_dport_release(dport);
452 return rc;
453 }
454 EXPORT_SYMBOL_GPL(cxl_add_dport);
455
decoder_populate_targets(struct cxl_decoder * cxld,struct cxl_port * port,int * target_map)456 static 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
480 out_unlock:
481 device_unlock(&port->dev);
482
483 return rc;
484 }
485
cxl_decoder_alloc(struct cxl_port * port,int nr_targets)486 struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets)
487 {
488 struct cxl_decoder *cxld, cxld_const_init = {
489 .nr_targets = nr_targets,
490 };
491 struct device *dev;
492 int rc = 0;
493
494 if (nr_targets > CXL_DECODER_MAX_INTERLEAVE || nr_targets < 1)
495 return ERR_PTR(-EINVAL);
496
497 cxld = kzalloc(struct_size(cxld, target, nr_targets), GFP_KERNEL);
498 if (!cxld)
499 return ERR_PTR(-ENOMEM);
500 memcpy(cxld, &cxld_const_init, sizeof(cxld_const_init));
501
502 rc = ida_alloc(&port->decoder_ida, GFP_KERNEL);
503 if (rc < 0)
504 goto err;
505
506 cxld->id = rc;
507 dev = &cxld->dev;
508 device_initialize(dev);
509 device_set_pm_not_required(dev);
510 dev->parent = &port->dev;
511 dev->bus = &cxl_bus_type;
512
513 /* root ports do not have a cxl_port_type parent */
514 if (port->dev.parent->type == &cxl_port_type)
515 dev->type = &cxl_decoder_switch_type;
516 else
517 dev->type = &cxl_decoder_root_type;
518
519 return cxld;
520 err:
521 kfree(cxld);
522 return ERR_PTR(rc);
523 }
524 EXPORT_SYMBOL_GPL(cxl_decoder_alloc);
525
cxl_decoder_add(struct cxl_decoder * cxld,int * target_map)526 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
527 {
528 struct cxl_port *port;
529 struct device *dev;
530 int rc;
531
532 if (WARN_ON_ONCE(!cxld))
533 return -EINVAL;
534
535 if (WARN_ON_ONCE(IS_ERR(cxld)))
536 return PTR_ERR(cxld);
537
538 if (cxld->interleave_ways < 1)
539 return -EINVAL;
540
541 port = to_cxl_port(cxld->dev.parent);
542 rc = decoder_populate_targets(cxld, port, target_map);
543 if (rc)
544 return rc;
545
546 dev = &cxld->dev;
547 rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
548 if (rc)
549 return rc;
550
551 return device_add(dev);
552 }
553 EXPORT_SYMBOL_GPL(cxl_decoder_add);
554
cxld_unregister(void * dev)555 static void cxld_unregister(void *dev)
556 {
557 device_unregister(dev);
558 }
559
cxl_decoder_autoremove(struct device * host,struct cxl_decoder * cxld)560 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld)
561 {
562 return devm_add_action_or_reset(host, cxld_unregister, &cxld->dev);
563 }
564 EXPORT_SYMBOL_GPL(cxl_decoder_autoremove);
565
566 /**
567 * __cxl_driver_register - register a driver for the cxl bus
568 * @cxl_drv: cxl driver structure to attach
569 * @owner: owning module/driver
570 * @modname: KBUILD_MODNAME for parent driver
571 */
__cxl_driver_register(struct cxl_driver * cxl_drv,struct module * owner,const char * modname)572 int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
573 const char *modname)
574 {
575 if (!cxl_drv->probe) {
576 pr_debug("%s ->probe() must be specified\n", modname);
577 return -EINVAL;
578 }
579
580 if (!cxl_drv->name) {
581 pr_debug("%s ->name must be specified\n", modname);
582 return -EINVAL;
583 }
584
585 if (!cxl_drv->id) {
586 pr_debug("%s ->id must be specified\n", modname);
587 return -EINVAL;
588 }
589
590 cxl_drv->drv.bus = &cxl_bus_type;
591 cxl_drv->drv.owner = owner;
592 cxl_drv->drv.mod_name = modname;
593 cxl_drv->drv.name = cxl_drv->name;
594
595 return driver_register(&cxl_drv->drv);
596 }
597 EXPORT_SYMBOL_GPL(__cxl_driver_register);
598
cxl_driver_unregister(struct cxl_driver * cxl_drv)599 void cxl_driver_unregister(struct cxl_driver *cxl_drv)
600 {
601 driver_unregister(&cxl_drv->drv);
602 }
603 EXPORT_SYMBOL_GPL(cxl_driver_unregister);
604
cxl_device_id(struct device * dev)605 static int cxl_device_id(struct device *dev)
606 {
607 if (dev->type == &cxl_nvdimm_bridge_type)
608 return CXL_DEVICE_NVDIMM_BRIDGE;
609 if (dev->type == &cxl_nvdimm_type)
610 return CXL_DEVICE_NVDIMM;
611 return 0;
612 }
613
cxl_bus_uevent(struct device * dev,struct kobj_uevent_env * env)614 static int cxl_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
615 {
616 return add_uevent_var(env, "MODALIAS=" CXL_MODALIAS_FMT,
617 cxl_device_id(dev));
618 }
619
cxl_bus_match(struct device * dev,struct device_driver * drv)620 static int cxl_bus_match(struct device *dev, struct device_driver *drv)
621 {
622 return cxl_device_id(dev) == to_cxl_drv(drv)->id;
623 }
624
cxl_bus_probe(struct device * dev)625 static int cxl_bus_probe(struct device *dev)
626 {
627 return to_cxl_drv(dev->driver)->probe(dev);
628 }
629
cxl_bus_remove(struct device * dev)630 static void cxl_bus_remove(struct device *dev)
631 {
632 struct cxl_driver *cxl_drv = to_cxl_drv(dev->driver);
633
634 if (cxl_drv->remove)
635 cxl_drv->remove(dev);
636 }
637
638 struct bus_type cxl_bus_type = {
639 .name = "cxl",
640 .uevent = cxl_bus_uevent,
641 .match = cxl_bus_match,
642 .probe = cxl_bus_probe,
643 .remove = cxl_bus_remove,
644 };
645 EXPORT_SYMBOL_GPL(cxl_bus_type);
646
cxl_core_init(void)647 static __init int cxl_core_init(void)
648 {
649 int rc;
650
651 cxl_mbox_init();
652
653 rc = cxl_memdev_init();
654 if (rc)
655 return rc;
656
657 rc = bus_register(&cxl_bus_type);
658 if (rc)
659 goto err;
660 return 0;
661
662 err:
663 cxl_memdev_exit();
664 cxl_mbox_exit();
665 return rc;
666 }
667
cxl_core_exit(void)668 static void cxl_core_exit(void)
669 {
670 bus_unregister(&cxl_bus_type);
671 cxl_memdev_exit();
672 cxl_mbox_exit();
673 }
674
675 module_init(cxl_core_init);
676 module_exit(cxl_core_exit);
677 MODULE_LICENSE("GPL v2");
678