1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2022 Intel Corporation. All rights reserved. */
3 #include <linux/device.h>
4 #include <linux/module.h>
13 * CXL memory endpoint devices and switches are CXL capable devices that are
14 * participating in CXL.mem protocol. Their functionality builds on top of the
15 * CXL.io protocol that allows enumerating and configuring components via
16 * standard PCI mechanisms.
18 * The cxl_mem driver owns kicking off the enumeration of this CXL.mem
19 * capability. With the detection of a CXL capable endpoint, the driver will
20 * walk up to find the platform specific port it is connected to, and determine
21 * if there are intervening switches in the path. If there are switches, a
22 * secondary action is to enumerate those (implemented in cxl_core). Finally the
23 * cxl_mem driver adds the device it is bound to as a CXL endpoint-port for use
24 * in higher level operations.
27 static int create_endpoint(struct cxl_memdev *cxlmd,
28 struct cxl_port *parent_port)
30 struct cxl_dev_state *cxlds = cxlmd->cxlds;
31 struct cxl_port *endpoint;
34 endpoint = devm_cxl_add_port(&parent_port->dev, &cxlmd->dev,
35 cxlds->component_reg_phys, parent_port);
37 return PTR_ERR(endpoint);
39 dev_dbg(&cxlmd->dev, "add: %s\n", dev_name(&endpoint->dev));
41 rc = cxl_endpoint_autoremove(cxlmd, endpoint);
45 if (!endpoint->dev.driver) {
46 dev_err(&cxlmd->dev, "%s failed probe\n",
47 dev_name(&endpoint->dev));
54 static void enable_suspend(void *data)
59 static int cxl_mem_probe(struct device *dev)
61 struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
62 struct cxl_port *parent_port;
66 * Someone is trying to reattach this device after it lost its port
67 * connection (an endpoint port previously registered by this memdev was
68 * disabled). This racy check is ok because if the port is still gone,
69 * no harm done, and if the port hierarchy comes back it will re-trigger
70 * this probe. Port rescan and memdev detach work share the same
71 * single-threaded workqueue.
73 if (work_pending(&cxlmd->detach_work))
76 rc = devm_cxl_enumerate_ports(cxlmd);
80 parent_port = cxl_mem_find_port(cxlmd);
82 dev_err(dev, "CXL port topology not found\n");
86 device_lock(&parent_port->dev);
87 if (!parent_port->dev.driver) {
88 dev_err(dev, "CXL port topology %s not enabled\n",
89 dev_name(&parent_port->dev));
94 rc = create_endpoint(cxlmd, parent_port);
96 device_unlock(&parent_port->dev);
97 put_device(&parent_port->dev);
102 * The kernel may be operating out of CXL memory on this device,
103 * there is no spec defined way to determine whether this device
104 * preserves contents over suspend, and there is no simple way
105 * to arrange for the suspend image to avoid CXL memory which
106 * would setup a circular dependency between PCI resume and save
109 * TODO: support suspend when all the regions this device is
110 * hosting are locked and covered by the system address map,
111 * i.e. platform firmware owns restoring the HDM configuration
114 cxl_mem_active_inc();
115 return devm_add_action_or_reset(dev, enable_suspend, NULL);
118 static struct cxl_driver cxl_mem_driver = {
120 .probe = cxl_mem_probe,
121 .id = CXL_DEVICE_MEMORY_EXPANDER,
124 module_cxl_driver(cxl_mem_driver);
126 MODULE_LICENSE("GPL v2");
127 MODULE_IMPORT_NS(CXL);
128 MODULE_ALIAS_CXL(CXL_DEVICE_MEMORY_EXPANDER);
130 * create_endpoint() wants to validate port driver attach immediately after
131 * endpoint registration.
133 MODULE_SOFTDEP("pre: cxl_port");