xref: /openbmc/linux/drivers/cxl/mem.c (revision 58d33131)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2022 Intel Corporation. All rights reserved. */
3 #include <linux/debugfs.h>
4 #include <linux/device.h>
5 #include <linux/module.h>
6 #include <linux/pci.h>
7 
8 #include "cxlmem.h"
9 #include "cxlpci.h"
10 
11 /**
12  * DOC: cxl mem
13  *
14  * CXL memory endpoint devices and switches are CXL capable devices that are
15  * participating in CXL.mem protocol. Their functionality builds on top of the
16  * CXL.io protocol that allows enumerating and configuring components via
17  * standard PCI mechanisms.
18  *
19  * The cxl_mem driver owns kicking off the enumeration of this CXL.mem
20  * capability. With the detection of a CXL capable endpoint, the driver will
21  * walk up to find the platform specific port it is connected to, and determine
22  * if there are intervening switches in the path. If there are switches, a
23  * secondary action is to enumerate those (implemented in cxl_core). Finally the
24  * cxl_mem driver adds the device it is bound to as a CXL endpoint-port for use
25  * in higher level operations.
26  */
27 
28 static void enable_suspend(void *data)
29 {
30 	cxl_mem_active_dec();
31 }
32 
33 static void remove_debugfs(void *dentry)
34 {
35 	debugfs_remove_recursive(dentry);
36 }
37 
38 static int cxl_mem_dpa_show(struct seq_file *file, void *data)
39 {
40 	struct device *dev = file->private;
41 	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
42 
43 	cxl_dpa_debug(file, cxlmd->cxlds);
44 
45 	return 0;
46 }
47 
48 static int cxl_mem_probe(struct device *dev)
49 {
50 	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
51 	struct cxl_port *parent_port;
52 	struct cxl_dport *dport;
53 	struct dentry *dentry;
54 	int rc;
55 
56 	/*
57 	 * Someone is trying to reattach this device after it lost its port
58 	 * connection (an endpoint port previously registered by this memdev was
59 	 * disabled). This racy check is ok because if the port is still gone,
60 	 * no harm done, and if the port hierarchy comes back it will re-trigger
61 	 * this probe. Port rescan and memdev detach work share the same
62 	 * single-threaded workqueue.
63 	 */
64 	if (work_pending(&cxlmd->detach_work))
65 		return -EBUSY;
66 
67 	dentry = cxl_debugfs_create_dir(dev_name(dev));
68 	debugfs_create_devm_seqfile(dev, "dpamem", dentry, cxl_mem_dpa_show);
69 	rc = devm_add_action_or_reset(dev, remove_debugfs, dentry);
70 	if (rc)
71 		return rc;
72 
73 	rc = devm_cxl_enumerate_ports(cxlmd);
74 	if (rc)
75 		return rc;
76 
77 	parent_port = cxl_mem_find_port(cxlmd, &dport);
78 	if (!parent_port) {
79 		dev_err(dev, "CXL port topology not found\n");
80 		return -ENXIO;
81 	}
82 
83 	device_lock(&parent_port->dev);
84 	if (!parent_port->dev.driver) {
85 		dev_err(dev, "CXL port topology %s not enabled\n",
86 			dev_name(&parent_port->dev));
87 		rc = -ENXIO;
88 		goto unlock;
89 	}
90 
91 	rc = devm_cxl_add_endpoint(cxlmd, dport);
92 unlock:
93 	device_unlock(&parent_port->dev);
94 	put_device(&parent_port->dev);
95 	if (rc)
96 		return rc;
97 
98 	/*
99 	 * The kernel may be operating out of CXL memory on this device,
100 	 * there is no spec defined way to determine whether this device
101 	 * preserves contents over suspend, and there is no simple way
102 	 * to arrange for the suspend image to avoid CXL memory which
103 	 * would setup a circular dependency between PCI resume and save
104 	 * state restoration.
105 	 *
106 	 * TODO: support suspend when all the regions this device is
107 	 * hosting are locked and covered by the system address map,
108 	 * i.e. platform firmware owns restoring the HDM configuration
109 	 * that it locked.
110 	 */
111 	cxl_mem_active_inc();
112 	return devm_add_action_or_reset(dev, enable_suspend, NULL);
113 }
114 
115 static struct cxl_driver cxl_mem_driver = {
116 	.name = "cxl_mem",
117 	.probe = cxl_mem_probe,
118 	.id = CXL_DEVICE_MEMORY_EXPANDER,
119 };
120 
121 module_cxl_driver(cxl_mem_driver);
122 
123 MODULE_LICENSE("GPL v2");
124 MODULE_IMPORT_NS(CXL);
125 MODULE_ALIAS_CXL(CXL_DEVICE_MEMORY_EXPANDER);
126 /*
127  * create_endpoint() wants to validate port driver attach immediately after
128  * endpoint registration.
129  */
130 MODULE_SOFTDEP("pre: cxl_port");
131