1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2020 Intel Corporation. */
4 #include <linux/device.h>
5 #include <linux/slab.h>
11 static DECLARE_RWSEM(cxl_memdev_rwsem);
14 * An entire PCI topology full of devices should be enough for any
17 #define CXL_MEM_MAX_DEVS 65536
19 static int cxl_mem_major;
20 static DEFINE_IDA(cxl_memdev_ida);
22 static void cxl_memdev_release(struct device *dev)
24 struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
26 ida_free(&cxl_memdev_ida, cxlmd->id);
30 static char *cxl_memdev_devnode(struct device *dev, umode_t *mode, kuid_t *uid,
33 return kasprintf(GFP_KERNEL, "cxl/%s", dev_name(dev));
36 static ssize_t firmware_version_show(struct device *dev,
37 struct device_attribute *attr, char *buf)
39 struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
40 struct cxl_dev_state *cxlds = cxlmd->cxlds;
42 return sysfs_emit(buf, "%.16s\n", cxlds->firmware_version);
44 static DEVICE_ATTR_RO(firmware_version);
46 static ssize_t payload_max_show(struct device *dev,
47 struct device_attribute *attr, char *buf)
49 struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
50 struct cxl_dev_state *cxlds = cxlmd->cxlds;
52 return sysfs_emit(buf, "%zu\n", cxlds->payload_size);
54 static DEVICE_ATTR_RO(payload_max);
56 static ssize_t label_storage_size_show(struct device *dev,
57 struct device_attribute *attr, char *buf)
59 struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
60 struct cxl_dev_state *cxlds = cxlmd->cxlds;
62 return sysfs_emit(buf, "%zu\n", cxlds->lsa_size);
64 static DEVICE_ATTR_RO(label_storage_size);
66 static ssize_t ram_size_show(struct device *dev, struct device_attribute *attr,
69 struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
70 struct cxl_dev_state *cxlds = cxlmd->cxlds;
71 unsigned long long len = range_len(&cxlds->ram_range);
73 return sysfs_emit(buf, "%#llx\n", len);
76 static struct device_attribute dev_attr_ram_size =
77 __ATTR(size, 0444, ram_size_show, NULL);
79 static ssize_t pmem_size_show(struct device *dev, struct device_attribute *attr,
82 struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
83 struct cxl_dev_state *cxlds = cxlmd->cxlds;
84 unsigned long long len = range_len(&cxlds->pmem_range);
86 return sysfs_emit(buf, "%#llx\n", len);
89 static struct device_attribute dev_attr_pmem_size =
90 __ATTR(size, 0444, pmem_size_show, NULL);
92 static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
95 struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
96 struct cxl_dev_state *cxlds = cxlmd->cxlds;
98 return sysfs_emit(buf, "%#llx\n", cxlds->serial);
100 static DEVICE_ATTR_RO(serial);
102 static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
105 return sprintf(buf, "%d\n", dev_to_node(dev));
107 static DEVICE_ATTR_RO(numa_node);
109 static struct attribute *cxl_memdev_attributes[] = {
110 &dev_attr_serial.attr,
111 &dev_attr_firmware_version.attr,
112 &dev_attr_payload_max.attr,
113 &dev_attr_label_storage_size.attr,
114 &dev_attr_numa_node.attr,
118 static struct attribute *cxl_memdev_pmem_attributes[] = {
119 &dev_attr_pmem_size.attr,
123 static struct attribute *cxl_memdev_ram_attributes[] = {
124 &dev_attr_ram_size.attr,
128 static umode_t cxl_memdev_visible(struct kobject *kobj, struct attribute *a,
131 if (!IS_ENABLED(CONFIG_NUMA) && a == &dev_attr_numa_node.attr)
136 static struct attribute_group cxl_memdev_attribute_group = {
137 .attrs = cxl_memdev_attributes,
138 .is_visible = cxl_memdev_visible,
141 static struct attribute_group cxl_memdev_ram_attribute_group = {
143 .attrs = cxl_memdev_ram_attributes,
146 static struct attribute_group cxl_memdev_pmem_attribute_group = {
148 .attrs = cxl_memdev_pmem_attributes,
151 static const struct attribute_group *cxl_memdev_attribute_groups[] = {
152 &cxl_memdev_attribute_group,
153 &cxl_memdev_ram_attribute_group,
154 &cxl_memdev_pmem_attribute_group,
158 static const struct device_type cxl_memdev_type = {
159 .name = "cxl_memdev",
160 .release = cxl_memdev_release,
161 .devnode = cxl_memdev_devnode,
162 .groups = cxl_memdev_attribute_groups,
165 bool is_cxl_memdev(struct device *dev)
167 return dev->type == &cxl_memdev_type;
169 EXPORT_SYMBOL_NS_GPL(is_cxl_memdev, CXL);
172 * set_exclusive_cxl_commands() - atomically disable user cxl commands
173 * @cxlds: The device state to operate on
174 * @cmds: bitmap of commands to mark exclusive
176 * Grab the cxl_memdev_rwsem in write mode to flush in-flight
177 * invocations of the ioctl path and then disable future execution of
178 * commands with the command ids set in @cmds.
180 void set_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cmds)
182 down_write(&cxl_memdev_rwsem);
183 bitmap_or(cxlds->exclusive_cmds, cxlds->exclusive_cmds, cmds,
184 CXL_MEM_COMMAND_ID_MAX);
185 up_write(&cxl_memdev_rwsem);
187 EXPORT_SYMBOL_NS_GPL(set_exclusive_cxl_commands, CXL);
190 * clear_exclusive_cxl_commands() - atomically enable user cxl commands
191 * @cxlds: The device state to modify
192 * @cmds: bitmap of commands to mark available for userspace
194 void clear_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cmds)
196 down_write(&cxl_memdev_rwsem);
197 bitmap_andnot(cxlds->exclusive_cmds, cxlds->exclusive_cmds, cmds,
198 CXL_MEM_COMMAND_ID_MAX);
199 up_write(&cxl_memdev_rwsem);
201 EXPORT_SYMBOL_NS_GPL(clear_exclusive_cxl_commands, CXL);
203 static void cxl_memdev_shutdown(struct device *dev)
205 struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
207 down_write(&cxl_memdev_rwsem);
209 up_write(&cxl_memdev_rwsem);
212 static void cxl_memdev_unregister(void *_cxlmd)
214 struct cxl_memdev *cxlmd = _cxlmd;
215 struct device *dev = &cxlmd->dev;
217 cxl_memdev_shutdown(dev);
218 cdev_device_del(&cxlmd->cdev, dev);
222 static void detach_memdev(struct work_struct *work)
224 struct cxl_memdev *cxlmd;
226 cxlmd = container_of(work, typeof(*cxlmd), detach_work);
227 device_release_driver(&cxlmd->dev);
228 put_device(&cxlmd->dev);
231 static struct lock_class_key cxl_memdev_key;
233 static struct cxl_memdev *cxl_memdev_alloc(struct cxl_dev_state *cxlds,
234 const struct file_operations *fops)
236 struct cxl_memdev *cxlmd;
241 cxlmd = kzalloc(sizeof(*cxlmd), GFP_KERNEL);
243 return ERR_PTR(-ENOMEM);
245 rc = ida_alloc_range(&cxl_memdev_ida, 0, CXL_MEM_MAX_DEVS, GFP_KERNEL);
251 device_initialize(dev);
252 lockdep_set_class(&dev->mutex, &cxl_memdev_key);
253 dev->parent = cxlds->dev;
254 dev->bus = &cxl_bus_type;
255 dev->devt = MKDEV(cxl_mem_major, cxlmd->id);
256 dev->type = &cxl_memdev_type;
257 device_set_pm_not_required(dev);
258 INIT_WORK(&cxlmd->detach_work, detach_memdev);
261 cdev_init(cdev, fops);
269 static long __cxl_memdev_ioctl(struct cxl_memdev *cxlmd, unsigned int cmd,
273 case CXL_MEM_QUERY_COMMANDS:
274 return cxl_query_cmd(cxlmd, (void __user *)arg);
275 case CXL_MEM_SEND_COMMAND:
276 return cxl_send_cmd(cxlmd, (void __user *)arg);
282 static long cxl_memdev_ioctl(struct file *file, unsigned int cmd,
285 struct cxl_memdev *cxlmd = file->private_data;
288 down_read(&cxl_memdev_rwsem);
290 rc = __cxl_memdev_ioctl(cxlmd, cmd, arg);
291 up_read(&cxl_memdev_rwsem);
296 static int cxl_memdev_open(struct inode *inode, struct file *file)
298 struct cxl_memdev *cxlmd =
299 container_of(inode->i_cdev, typeof(*cxlmd), cdev);
301 get_device(&cxlmd->dev);
302 file->private_data = cxlmd;
307 static int cxl_memdev_release_file(struct inode *inode, struct file *file)
309 struct cxl_memdev *cxlmd =
310 container_of(inode->i_cdev, typeof(*cxlmd), cdev);
312 put_device(&cxlmd->dev);
317 static const struct file_operations cxl_memdev_fops = {
318 .owner = THIS_MODULE,
319 .unlocked_ioctl = cxl_memdev_ioctl,
320 .open = cxl_memdev_open,
321 .release = cxl_memdev_release_file,
322 .compat_ioctl = compat_ptr_ioctl,
323 .llseek = noop_llseek,
326 struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds)
328 struct cxl_memdev *cxlmd;
333 cxlmd = cxl_memdev_alloc(cxlds, &cxl_memdev_fops);
338 rc = dev_set_name(dev, "mem%d", cxlmd->id);
343 * Activate ioctl operations, no cxl_memdev_rwsem manipulation
344 * needed as this is ordered with cdev_add() publishing the device.
346 cxlmd->cxlds = cxlds;
349 rc = cdev_device_add(cdev, dev);
353 rc = devm_add_action_or_reset(cxlds->dev, cxl_memdev_unregister, cxlmd);
360 * The cdev was briefly live, shutdown any ioctl operations that
363 cxl_memdev_shutdown(dev);
367 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_memdev, CXL);
369 __init int cxl_memdev_init(void)
374 rc = alloc_chrdev_region(&devt, 0, CXL_MEM_MAX_DEVS, "cxl");
378 cxl_mem_major = MAJOR(devt);
383 void cxl_memdev_exit(void)
385 unregister_chrdev_region(MKDEV(cxl_mem_major, 0), CXL_MEM_MAX_DEVS);