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 devm_cxl_add_endpoint(struct device *host, struct cxl_memdev *cxlmd, 49 struct cxl_dport *parent_dport) 50 { 51 struct cxl_port *parent_port = parent_dport->port; 52 struct cxl_dev_state *cxlds = cxlmd->cxlds; 53 struct cxl_port *endpoint, *iter, *down; 54 resource_size_t component_reg_phys; 55 int rc; 56 57 /* 58 * Now that the path to the root is established record all the 59 * intervening ports in the chain. 60 */ 61 for (iter = parent_port, down = NULL; !is_cxl_root(iter); 62 down = iter, iter = to_cxl_port(iter->dev.parent)) { 63 struct cxl_ep *ep; 64 65 ep = cxl_ep_load(iter, cxlmd); 66 ep->next = down; 67 } 68 69 /* 70 * The component registers for an RCD might come from the 71 * host-bridge RCRB if they are not already mapped via the 72 * typical register locator mechanism. 73 */ 74 if (parent_dport->rch && cxlds->component_reg_phys == CXL_RESOURCE_NONE) 75 component_reg_phys = cxl_rcrb_to_component( 76 &cxlmd->dev, parent_dport->rcrb, CXL_RCRB_UPSTREAM); 77 else 78 component_reg_phys = cxlds->component_reg_phys; 79 endpoint = devm_cxl_add_port(host, &cxlmd->dev, component_reg_phys, 80 parent_dport); 81 if (IS_ERR(endpoint)) 82 return PTR_ERR(endpoint); 83 84 rc = cxl_endpoint_autoremove(cxlmd, endpoint); 85 if (rc) 86 return rc; 87 88 if (!endpoint->dev.driver) { 89 dev_err(&cxlmd->dev, "%s failed probe\n", 90 dev_name(&endpoint->dev)); 91 return -ENXIO; 92 } 93 94 return 0; 95 } 96 97 static int cxl_debugfs_poison_inject(void *data, u64 dpa) 98 { 99 struct cxl_memdev *cxlmd = data; 100 101 return cxl_inject_poison(cxlmd, dpa); 102 } 103 104 DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_inject_fops, NULL, 105 cxl_debugfs_poison_inject, "%llx\n"); 106 107 static int cxl_debugfs_poison_clear(void *data, u64 dpa) 108 { 109 struct cxl_memdev *cxlmd = data; 110 111 return cxl_clear_poison(cxlmd, dpa); 112 } 113 114 DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_clear_fops, NULL, 115 cxl_debugfs_poison_clear, "%llx\n"); 116 117 static int cxl_mem_probe(struct device *dev) 118 { 119 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 120 struct cxl_dev_state *cxlds = cxlmd->cxlds; 121 struct device *endpoint_parent; 122 struct cxl_port *parent_port; 123 struct cxl_dport *dport; 124 struct dentry *dentry; 125 int rc; 126 127 if (!cxlds->media_ready) 128 return -EBUSY; 129 130 /* 131 * Someone is trying to reattach this device after it lost its port 132 * connection (an endpoint port previously registered by this memdev was 133 * disabled). This racy check is ok because if the port is still gone, 134 * no harm done, and if the port hierarchy comes back it will re-trigger 135 * this probe. Port rescan and memdev detach work share the same 136 * single-threaded workqueue. 137 */ 138 if (work_pending(&cxlmd->detach_work)) 139 return -EBUSY; 140 141 dentry = cxl_debugfs_create_dir(dev_name(dev)); 142 debugfs_create_devm_seqfile(dev, "dpamem", dentry, cxl_mem_dpa_show); 143 144 if (test_bit(CXL_POISON_ENABLED_INJECT, cxlds->poison.enabled_cmds)) 145 debugfs_create_file("inject_poison", 0200, dentry, cxlmd, 146 &cxl_poison_inject_fops); 147 if (test_bit(CXL_POISON_ENABLED_CLEAR, cxlds->poison.enabled_cmds)) 148 debugfs_create_file("clear_poison", 0200, dentry, cxlmd, 149 &cxl_poison_clear_fops); 150 151 rc = devm_add_action_or_reset(dev, remove_debugfs, dentry); 152 if (rc) 153 return rc; 154 155 rc = devm_cxl_enumerate_ports(cxlmd); 156 if (rc) 157 return rc; 158 159 parent_port = cxl_mem_find_port(cxlmd, &dport); 160 if (!parent_port) { 161 dev_err(dev, "CXL port topology not found\n"); 162 return -ENXIO; 163 } 164 165 if (dport->rch) 166 endpoint_parent = parent_port->uport; 167 else 168 endpoint_parent = &parent_port->dev; 169 170 device_lock(endpoint_parent); 171 if (!endpoint_parent->driver) { 172 dev_err(dev, "CXL port topology %s not enabled\n", 173 dev_name(endpoint_parent)); 174 rc = -ENXIO; 175 goto unlock; 176 } 177 178 rc = devm_cxl_add_endpoint(endpoint_parent, cxlmd, dport); 179 unlock: 180 device_unlock(endpoint_parent); 181 put_device(&parent_port->dev); 182 if (rc) 183 return rc; 184 185 if (resource_size(&cxlds->pmem_res) && IS_ENABLED(CONFIG_CXL_PMEM)) { 186 rc = devm_cxl_add_nvdimm(cxlmd); 187 if (rc == -ENODEV) 188 dev_info(dev, "PMEM disabled by platform\n"); 189 else 190 return rc; 191 } 192 193 /* 194 * The kernel may be operating out of CXL memory on this device, 195 * there is no spec defined way to determine whether this device 196 * preserves contents over suspend, and there is no simple way 197 * to arrange for the suspend image to avoid CXL memory which 198 * would setup a circular dependency between PCI resume and save 199 * state restoration. 200 * 201 * TODO: support suspend when all the regions this device is 202 * hosting are locked and covered by the system address map, 203 * i.e. platform firmware owns restoring the HDM configuration 204 * that it locked. 205 */ 206 cxl_mem_active_inc(); 207 return devm_add_action_or_reset(dev, enable_suspend, NULL); 208 } 209 210 static ssize_t trigger_poison_list_store(struct device *dev, 211 struct device_attribute *attr, 212 const char *buf, size_t len) 213 { 214 bool trigger; 215 int rc; 216 217 if (kstrtobool(buf, &trigger) || !trigger) 218 return -EINVAL; 219 220 rc = cxl_trigger_poison_list(to_cxl_memdev(dev)); 221 222 return rc ? rc : len; 223 } 224 static DEVICE_ATTR_WO(trigger_poison_list); 225 226 static umode_t cxl_mem_visible(struct kobject *kobj, struct attribute *a, int n) 227 { 228 if (a == &dev_attr_trigger_poison_list.attr) { 229 struct device *dev = kobj_to_dev(kobj); 230 231 if (!test_bit(CXL_POISON_ENABLED_LIST, 232 to_cxl_memdev(dev)->cxlds->poison.enabled_cmds)) 233 return 0; 234 } 235 return a->mode; 236 } 237 238 static struct attribute *cxl_mem_attrs[] = { 239 &dev_attr_trigger_poison_list.attr, 240 NULL 241 }; 242 243 static struct attribute_group cxl_mem_group = { 244 .attrs = cxl_mem_attrs, 245 .is_visible = cxl_mem_visible, 246 }; 247 248 __ATTRIBUTE_GROUPS(cxl_mem); 249 250 static struct cxl_driver cxl_mem_driver = { 251 .name = "cxl_mem", 252 .probe = cxl_mem_probe, 253 .id = CXL_DEVICE_MEMORY_EXPANDER, 254 .drv = { 255 .dev_groups = cxl_mem_groups, 256 }, 257 }; 258 259 module_cxl_driver(cxl_mem_driver); 260 261 MODULE_LICENSE("GPL v2"); 262 MODULE_IMPORT_NS(CXL); 263 MODULE_ALIAS_CXL(CXL_DEVICE_MEMORY_EXPANDER); 264 /* 265 * create_endpoint() wants to validate port driver attach immediately after 266 * endpoint registration. 267 */ 268 MODULE_SOFTDEP("pre: cxl_port"); 269