xref: /linux/drivers/cxl/mem.c (revision ca220141fa8ebae09765a242076b2b77338106b0)
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_debugfs_poison_inject(void *data, u64 dpa)
49 {
50 	struct cxl_memdev *cxlmd = data;
51 
52 	return cxl_inject_poison(cxlmd, dpa);
53 }
54 
55 DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_inject_fops, NULL,
56 			 cxl_debugfs_poison_inject, "%llx\n");
57 
58 static int cxl_debugfs_poison_clear(void *data, u64 dpa)
59 {
60 	struct cxl_memdev *cxlmd = data;
61 
62 	return cxl_clear_poison(cxlmd, dpa);
63 }
64 
65 DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_clear_fops, NULL,
66 			 cxl_debugfs_poison_clear, "%llx\n");
67 
68 static int cxl_mem_probe(struct device *dev)
69 {
70 	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
71 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
72 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
73 	struct device *endpoint_parent;
74 	struct cxl_dport *dport;
75 	struct dentry *dentry;
76 	int rc;
77 
78 	if (!cxlds->media_ready)
79 		return -EBUSY;
80 
81 	/*
82 	 * Someone is trying to reattach this device after it lost its port
83 	 * connection (an endpoint port previously registered by this memdev was
84 	 * disabled). This racy check is ok because if the port is still gone,
85 	 * no harm done, and if the port hierarchy comes back it will re-trigger
86 	 * this probe. Port rescan and memdev detach work share the same
87 	 * single-threaded workqueue.
88 	 */
89 	if (work_pending(&cxlmd->detach_work))
90 		return -EBUSY;
91 
92 	dentry = cxl_debugfs_create_dir(dev_name(dev));
93 	debugfs_create_devm_seqfile(dev, "dpamem", dentry, cxl_mem_dpa_show);
94 
95 	if (test_bit(CXL_POISON_ENABLED_INJECT, mds->poison.enabled_cmds))
96 		debugfs_create_file("inject_poison", 0200, dentry, cxlmd,
97 				    &cxl_poison_inject_fops);
98 	if (test_bit(CXL_POISON_ENABLED_CLEAR, mds->poison.enabled_cmds))
99 		debugfs_create_file("clear_poison", 0200, dentry, cxlmd,
100 				    &cxl_poison_clear_fops);
101 
102 	rc = devm_add_action_or_reset(dev, remove_debugfs, dentry);
103 	if (rc)
104 		return rc;
105 
106 	rc = devm_cxl_enumerate_ports(cxlmd);
107 	if (rc)
108 		return rc;
109 
110 	struct cxl_port *parent_port __free(put_cxl_port) =
111 		cxl_mem_find_port(cxlmd, &dport);
112 	if (!parent_port) {
113 		dev_err(dev, "CXL port topology not found\n");
114 		return -ENXIO;
115 	}
116 
117 	if (cxl_pmem_size(cxlds) && IS_ENABLED(CONFIG_CXL_PMEM)) {
118 		rc = devm_cxl_add_nvdimm(dev, parent_port, cxlmd);
119 		if (rc) {
120 			if (rc == -ENODEV)
121 				dev_info(dev, "PMEM disabled by platform\n");
122 			return rc;
123 		}
124 	}
125 
126 	if (dport->rch)
127 		endpoint_parent = parent_port->uport_dev;
128 	else
129 		endpoint_parent = &parent_port->dev;
130 
131 	scoped_guard(device, endpoint_parent) {
132 		if (!endpoint_parent->driver) {
133 			dev_err(dev, "CXL port topology %s not enabled\n",
134 				dev_name(endpoint_parent));
135 			return -ENXIO;
136 		}
137 
138 		rc = devm_cxl_add_endpoint(endpoint_parent, cxlmd, dport);
139 		if (rc)
140 			return rc;
141 	}
142 
143 	if (cxlmd->attach) {
144 		rc = cxlmd->attach->probe(cxlmd);
145 		if (rc)
146 			return rc;
147 	}
148 
149 	rc = devm_cxl_memdev_edac_register(cxlmd);
150 	if (rc)
151 		dev_dbg(dev, "CXL memdev EDAC registration failed rc=%d\n", rc);
152 
153 	/*
154 	 * The kernel may be operating out of CXL memory on this device,
155 	 * there is no spec defined way to determine whether this device
156 	 * preserves contents over suspend, and there is no simple way
157 	 * to arrange for the suspend image to avoid CXL memory which
158 	 * would setup a circular dependency between PCI resume and save
159 	 * state restoration.
160 	 *
161 	 * TODO: support suspend when all the regions this device is
162 	 * hosting are locked and covered by the system address map,
163 	 * i.e. platform firmware owns restoring the HDM configuration
164 	 * that it locked.
165 	 */
166 	cxl_mem_active_inc();
167 	return devm_add_action_or_reset(dev, enable_suspend, NULL);
168 }
169 
170 /**
171  * devm_cxl_add_memdev - Add a CXL memory device
172  * @cxlds: CXL device state to associate with the memdev
173  * @attach: Caller depends on CXL topology attachment
174  *
175  * Upon return the device will have had a chance to attach to the
176  * cxl_mem driver, but may fail to attach if the CXL topology is not ready
177  * (hardware CXL link down, or software platform CXL root not attached).
178  *
179  * When @attach is NULL it indicates the caller wants the memdev to remain
180  * registered even if it does not immediately attach to the CXL hierarchy. When
181  * @attach is provided a cxl_mem_probe() failure leads to failure of this routine.
182  *
183  * The parent of the resulting device and the devm context for allocations is
184  * @cxlds->dev.
185  */
186 struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds,
187 				       const struct cxl_memdev_attach *attach)
188 {
189 	return __devm_cxl_add_memdev(cxlds, attach);
190 }
191 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_memdev, "CXL");
192 
193 static ssize_t trigger_poison_list_store(struct device *dev,
194 					 struct device_attribute *attr,
195 					 const char *buf, size_t len)
196 {
197 	bool trigger;
198 	int rc;
199 
200 	if (kstrtobool(buf, &trigger) || !trigger)
201 		return -EINVAL;
202 
203 	rc = cxl_trigger_poison_list(to_cxl_memdev(dev));
204 
205 	return rc ? rc : len;
206 }
207 static DEVICE_ATTR_WO(trigger_poison_list);
208 
209 static umode_t cxl_mem_visible(struct kobject *kobj, struct attribute *a, int n)
210 {
211 	struct device *dev = kobj_to_dev(kobj);
212 	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
213 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
214 
215 	if (a == &dev_attr_trigger_poison_list.attr)
216 		if (!test_bit(CXL_POISON_ENABLED_LIST,
217 			      mds->poison.enabled_cmds))
218 			return 0;
219 
220 	return a->mode;
221 }
222 
223 static struct attribute *cxl_mem_attrs[] = {
224 	&dev_attr_trigger_poison_list.attr,
225 	NULL
226 };
227 
228 static struct attribute_group cxl_mem_group = {
229 	.attrs = cxl_mem_attrs,
230 	.is_visible = cxl_mem_visible,
231 };
232 
233 __ATTRIBUTE_GROUPS(cxl_mem);
234 
235 static struct cxl_driver cxl_mem_driver = {
236 	.name = "cxl_mem",
237 	.probe = cxl_mem_probe,
238 	.id = CXL_DEVICE_MEMORY_EXPANDER,
239 	.drv = {
240 		.probe_type = PROBE_FORCE_SYNCHRONOUS,
241 		.dev_groups = cxl_mem_groups,
242 	},
243 };
244 
245 module_cxl_driver(cxl_mem_driver);
246 
247 MODULE_DESCRIPTION("CXL: Memory Expansion");
248 MODULE_LICENSE("GPL v2");
249 MODULE_IMPORT_NS("CXL");
250 MODULE_ALIAS_CXL(CXL_DEVICE_MEMORY_EXPANDER);
251