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 int rc; 52 53 /* 54 * Never wait for this lock: the debugfs proxy holds a file reference 55 * across the callback and unbind removes the file under the same 56 * device lock, so waiting here deadlocks against unbind. 57 */ 58 ACQUIRE(device_try, devlock)(&cxlmd->dev); 59 if ((rc = ACQUIRE_ERR(device_try, &devlock))) 60 return rc; 61 62 return cxl_inject_poison(cxlmd, dpa); 63 } 64 65 DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_inject_fops, NULL, 66 cxl_debugfs_poison_inject, "%llx\n"); 67 68 static int cxl_debugfs_poison_clear(void *data, u64 dpa) 69 { 70 struct cxl_memdev *cxlmd = data; 71 int rc; 72 73 /* Never wait, per the inject path above. */ 74 ACQUIRE(device_try, devlock)(&cxlmd->dev); 75 if ((rc = ACQUIRE_ERR(device_try, &devlock))) 76 return rc; 77 78 return cxl_clear_poison(cxlmd, dpa); 79 } 80 81 DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_clear_fops, NULL, 82 cxl_debugfs_poison_clear, "%llx\n"); 83 84 static void cxl_memdev_poison_enable(struct cxl_memdev_state *mds, 85 struct cxl_memdev *cxlmd, 86 struct dentry *dentry) 87 { 88 /* 89 * Avoid poison debugfs for DEVMEM aka accelerators as they rely on 90 * cxl_memdev_state. 91 */ 92 if (!mds) 93 return; 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 99 if (test_bit(CXL_POISON_ENABLED_CLEAR, mds->poison.enabled_cmds)) 100 debugfs_create_file("clear_poison", 0200, dentry, cxlmd, 101 &cxl_poison_clear_fops); 102 } 103 104 static int cxl_mem_probe(struct device *dev) 105 { 106 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 107 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds); 108 struct cxl_dev_state *cxlds = cxlmd->cxlds; 109 struct device *endpoint_parent; 110 struct cxl_dport *dport; 111 struct dentry *dentry; 112 int rc; 113 114 if (!cxlds->media_ready) 115 return -EBUSY; 116 117 /* 118 * Someone is trying to reattach this device after it lost its port 119 * connection (an endpoint port previously registered by this memdev was 120 * disabled). This racy check is ok because if the port is still gone, 121 * no harm done, and if the port hierarchy comes back it will re-trigger 122 * this probe. Port rescan and memdev detach work share the same 123 * single-threaded workqueue. 124 */ 125 if (work_pending(&cxlmd->detach_work)) 126 return -EBUSY; 127 128 dentry = cxl_debugfs_create_dir(dev_name(dev)); 129 debugfs_create_devm_seqfile(dev, "dpamem", dentry, cxl_mem_dpa_show); 130 131 cxl_memdev_poison_enable(mds, cxlmd, dentry); 132 133 rc = devm_add_action_or_reset(dev, remove_debugfs, dentry); 134 if (rc) 135 return rc; 136 137 rc = devm_cxl_enumerate_ports(cxlmd); 138 if (rc) 139 return rc; 140 141 struct cxl_port *parent_port __free(put_cxl_port) = 142 cxl_mem_find_port(cxlmd); 143 if (!parent_port) { 144 dev_err(dev, "CXL port topology not found\n"); 145 return -ENXIO; 146 } 147 148 if (cxl_part_size(cxlds, CXL_PARTMODE_PMEM) && IS_ENABLED(CONFIG_CXL_PMEM)) { 149 rc = devm_cxl_add_nvdimm(dev, parent_port, cxlmd); 150 if (rc) { 151 if (rc == -ENODEV) 152 dev_info(dev, "PMEM disabled by platform\n"); 153 return rc; 154 } 155 } 156 157 endpoint_parent = cxl_port_dport_host(parent_port); 158 159 scoped_guard(device, endpoint_parent) { 160 if (!endpoint_parent->driver) { 161 dev_err(dev, "CXL port topology %s not enabled\n", 162 dev_name(endpoint_parent)); 163 return -ENXIO; 164 } 165 166 /* @dport is only valid while @endpoint_parent stays bound */ 167 dport = cxl_mem_find_dport(cxlmd, parent_port); 168 if (!dport) { 169 dev_err(dev, "CXL topology dport not found\n"); 170 return -ENXIO; 171 } 172 173 rc = devm_cxl_add_endpoint(endpoint_parent, cxlmd, dport); 174 if (rc) 175 return rc; 176 } 177 178 if (cxlmd->attach) { 179 rc = cxlmd->attach->probe(cxlmd); 180 if (rc) 181 return rc; 182 } 183 184 rc = devm_cxl_memdev_edac_register(cxlmd); 185 if (rc) 186 dev_dbg(dev, "CXL memdev EDAC registration failed rc=%d\n", rc); 187 188 /* 189 * The kernel may be operating out of CXL memory on this device, 190 * there is no spec defined way to determine whether this device 191 * preserves contents over suspend, and there is no simple way 192 * to arrange for the suspend image to avoid CXL memory which 193 * would setup a circular dependency between PCI resume and save 194 * state restoration. 195 * 196 * TODO: support suspend when all the regions this device is 197 * hosting are locked and covered by the system address map, 198 * i.e. platform firmware owns restoring the HDM configuration 199 * that it locked. 200 */ 201 cxl_mem_active_inc(); 202 return devm_add_action_or_reset(dev, enable_suspend, NULL); 203 } 204 205 /** 206 * devm_cxl_add_classdev - Add a CXL memory class-code device 207 * @cxlds: CXL device state to associate with the memdev 208 * 209 * Upon return the device will have had a chance to attach to the 210 * cxl_mem driver, but may fail to attach if the CXL topology is not ready 211 * (hardware CXL link down, or software platform CXL root not attached). 212 * 213 * The parent of the resulting device and the devm context for allocations is 214 * @cxlds->dev. 215 */ 216 struct cxl_memdev *devm_cxl_add_classdev(struct cxl_dev_state *cxlds) 217 { 218 return __devm_cxl_add_memdev(cxlds, NULL); 219 } 220 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_classdev, "CXL"); 221 222 /** 223 * devm_cxl_probe_mem - Add a CXL memory device and probe its region 224 * @cxlds: CXL device state to associate with the memdev 225 * @hpa_range: CXL.mem physical address range result 226 * 227 * Upon return the device will have had a chance to attach to the 228 * cxl_mem driver, but may fail to attach if the CXL topology is not ready 229 * (hardware CXL link down, or software platform CXL root not attached). 230 * 231 * Failure to probe the memdev and/or setup a region for the memdev 232 * results in this function failing. 233 * 234 * The parent of the resulting device and the devm context for allocations is 235 * @cxlds->dev. 236 */ 237 struct cxl_memdev *devm_cxl_probe_mem(struct cxl_dev_state *cxlds, 238 struct range *hpa_range) 239 { 240 struct cxl_attach_region *attach = 241 devm_kmalloc(cxlds->dev, sizeof(*attach), GFP_KERNEL); 242 struct cxl_memdev *cxlmd; 243 244 if (!attach) 245 return ERR_PTR(-ENOMEM); 246 247 *attach = (struct cxl_attach_region) { 248 .attach = { 249 .probe = cxl_memdev_attach_region, 250 }, 251 .hpa_range = { 0, -1 }, 252 }; 253 254 cxlmd = __devm_cxl_add_memdev(cxlds, &attach->attach); 255 *hpa_range = attach->hpa_range; 256 return cxlmd; 257 } 258 EXPORT_SYMBOL_NS_GPL(devm_cxl_probe_mem, "CXL"); 259 260 static ssize_t trigger_poison_list_store(struct device *dev, 261 struct device_attribute *attr, 262 const char *buf, size_t len) 263 { 264 bool trigger; 265 int rc; 266 267 if (kstrtobool(buf, &trigger) || !trigger) 268 return -EINVAL; 269 270 rc = cxl_trigger_poison_list(to_cxl_memdev(dev)); 271 272 return rc ? rc : len; 273 } 274 static DEVICE_ATTR_WO(trigger_poison_list); 275 276 static bool cxl_poison_attr_visible(struct kobject *kobj, struct attribute *a) 277 { 278 struct device *dev = kobj_to_dev(kobj); 279 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 280 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds); 281 282 if (!mds || 283 !test_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds)) 284 return false; 285 286 return true; 287 } 288 289 static umode_t cxl_mem_visible(struct kobject *kobj, struct attribute *a, int n) 290 { 291 if (a == &dev_attr_trigger_poison_list.attr && 292 !cxl_poison_attr_visible(kobj, a)) 293 return 0; 294 295 return a->mode; 296 } 297 298 static struct attribute *cxl_mem_attrs[] = { 299 &dev_attr_trigger_poison_list.attr, 300 NULL 301 }; 302 303 static struct attribute_group cxl_mem_group = { 304 .attrs = cxl_mem_attrs, 305 .is_visible = cxl_mem_visible, 306 }; 307 308 __ATTRIBUTE_GROUPS(cxl_mem); 309 310 static struct cxl_driver cxl_mem_driver = { 311 .name = "cxl_mem", 312 .probe = cxl_mem_probe, 313 .id = CXL_DEVICE_MEMORY_EXPANDER, 314 .drv = { 315 .probe_type = PROBE_FORCE_SYNCHRONOUS, 316 .dev_groups = cxl_mem_groups, 317 }, 318 }; 319 320 module_cxl_driver(cxl_mem_driver); 321 322 MODULE_DESCRIPTION("CXL: Memory Expansion"); 323 MODULE_LICENSE("GPL v2"); 324 MODULE_IMPORT_NS("CXL"); 325 MODULE_ALIAS_CXL(CXL_DEVICE_MEMORY_EXPANDER); 326