1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2022 HabanaLabs, Ltd.
9 #include <linux/debugfs.h>
10 #include <linux/device.h>
11 #include <linux/idr.h>
13 #include <drm/drm_accel.h>
14 #include <drm/drm_auth.h>
15 #include <drm/drm_debugfs.h>
16 #include <drm/drm_drv.h>
17 #include <drm/drm_file.h>
18 #include <drm/drm_ioctl.h>
19 #include <drm/drm_print.h>
21 static DEFINE_SPINLOCK(accel_minor_lock);
22 static struct idr accel_minors_idr;
24 static struct dentry *accel_debugfs_root;
26 static const struct device_type accel_sysfs_device_minor = {
30 static char *accel_devnode(const struct device *dev, umode_t *mode)
32 return kasprintf(GFP_KERNEL, "accel/%s", dev_name(dev));
35 static const struct class accel_class = {
37 .devnode = accel_devnode,
40 static int accel_sysfs_init(void)
42 return class_register(&accel_class);
45 static void accel_sysfs_destroy(void)
47 class_unregister(&accel_class);
50 static int accel_name_info(struct seq_file *m, void *data)
52 struct drm_info_node *node = (struct drm_info_node *) m->private;
53 struct drm_minor *minor = node->minor;
54 struct drm_device *dev = minor->dev;
55 struct drm_master *master;
57 mutex_lock(&dev->master_mutex);
59 seq_printf(m, "%s", dev->driver->name);
61 seq_printf(m, " dev=%s", dev_name(dev->dev));
62 if (master && master->unique)
63 seq_printf(m, " master=%s", master->unique);
65 seq_printf(m, " unique=%s", dev->unique);
67 mutex_unlock(&dev->master_mutex);
72 static const struct drm_info_list accel_debugfs_list[] = {
73 {"name", accel_name_info, 0}
75 #define ACCEL_DEBUGFS_ENTRIES ARRAY_SIZE(accel_debugfs_list)
78 * accel_debugfs_init() - Initialize debugfs for device
79 * @dev: Pointer to the device instance.
81 * This function creates a root directory for the device in debugfs.
83 void accel_debugfs_init(struct drm_device *dev)
85 drm_debugfs_dev_init(dev, accel_debugfs_root);
89 * accel_debugfs_register() - Register debugfs for device
90 * @dev: Pointer to the device instance.
92 * Creates common files for accelerators.
94 void accel_debugfs_register(struct drm_device *dev)
96 struct drm_minor *minor = dev->accel;
98 minor->debugfs_root = dev->debugfs_root;
100 drm_debugfs_create_files(accel_debugfs_list, ACCEL_DEBUGFS_ENTRIES,
101 dev->debugfs_root, minor);
105 * accel_set_device_instance_params() - Set some device parameters for accel device
106 * @kdev: Pointer to the device instance.
107 * @index: The minor's index
109 * This function creates the dev_t of the device using the accel major and
110 * the device's minor number. In addition, it sets the class and type of the
111 * device instance to the accel sysfs class and device type, respectively.
113 void accel_set_device_instance_params(struct device *kdev, int index)
115 kdev->devt = MKDEV(ACCEL_MAJOR, index);
116 kdev->class = &accel_class;
117 kdev->type = &accel_sysfs_device_minor;
121 * accel_minor_alloc() - Allocates a new accel minor
123 * This function access the accel minors idr and allocates from it
124 * a new id to represent a new accel minor
126 * Return: A new id on success or error code in case idr_alloc failed
128 int accel_minor_alloc(void)
133 spin_lock_irqsave(&accel_minor_lock, flags);
134 r = idr_alloc(&accel_minors_idr, NULL, 0, ACCEL_MAX_MINORS, GFP_NOWAIT);
135 spin_unlock_irqrestore(&accel_minor_lock, flags);
141 * accel_minor_remove() - Remove an accel minor
142 * @index: The minor id to remove.
144 * This function access the accel minors idr and removes from
145 * it the member with the id that is passed to this function.
147 void accel_minor_remove(int index)
151 spin_lock_irqsave(&accel_minor_lock, flags);
152 idr_remove(&accel_minors_idr, index);
153 spin_unlock_irqrestore(&accel_minor_lock, flags);
157 * accel_minor_replace() - Replace minor pointer in accel minors idr.
158 * @minor: Pointer to the new minor.
159 * @index: The minor id to replace.
161 * This function access the accel minors idr structure and replaces the pointer
162 * that is associated with an existing id. Because the minor pointer can be
163 * NULL, we need to explicitly pass the index.
165 * Return: 0 for success, negative value for error
167 void accel_minor_replace(struct drm_minor *minor, int index)
171 spin_lock_irqsave(&accel_minor_lock, flags);
172 idr_replace(&accel_minors_idr, minor, index);
173 spin_unlock_irqrestore(&accel_minor_lock, flags);
177 * Looks up the given minor-ID and returns the respective DRM-minor object. The
178 * refence-count of the underlying device is increased so you must release this
179 * object with accel_minor_release().
181 * The object can be only a drm_minor that represents an accel device.
183 * As long as you hold this minor, it is guaranteed that the object and the
184 * minor->dev pointer will stay valid! However, the device may get unplugged and
185 * unregistered while you hold the minor.
187 static struct drm_minor *accel_minor_acquire(unsigned int minor_id)
189 struct drm_minor *minor;
192 spin_lock_irqsave(&accel_minor_lock, flags);
193 minor = idr_find(&accel_minors_idr, minor_id);
195 drm_dev_get(minor->dev);
196 spin_unlock_irqrestore(&accel_minor_lock, flags);
199 return ERR_PTR(-ENODEV);
200 } else if (drm_dev_is_unplugged(minor->dev)) {
201 drm_dev_put(minor->dev);
202 return ERR_PTR(-ENODEV);
208 static void accel_minor_release(struct drm_minor *minor)
210 drm_dev_put(minor->dev);
214 * accel_open - open method for ACCEL file
215 * @inode: device inode
216 * @filp: file pointer.
218 * This function must be used by drivers as their &file_operations.open method.
219 * It looks up the correct ACCEL device and instantiates all the per-file
220 * resources for it. It also calls the &drm_driver.open driver callback.
222 * Return: 0 on success or negative errno value on failure.
224 int accel_open(struct inode *inode, struct file *filp)
226 struct drm_device *dev;
227 struct drm_minor *minor;
230 minor = accel_minor_acquire(iminor(inode));
232 return PTR_ERR(minor);
236 atomic_fetch_inc(&dev->open_count);
238 /* share address_space across all char-devs of a single device */
239 filp->f_mapping = dev->anon_inode->i_mapping;
241 retcode = drm_open_helper(filp, minor);
248 atomic_dec(&dev->open_count);
249 accel_minor_release(minor);
252 EXPORT_SYMBOL_GPL(accel_open);
254 static int accel_stub_open(struct inode *inode, struct file *filp)
256 const struct file_operations *new_fops;
257 struct drm_minor *minor;
260 minor = accel_minor_acquire(iminor(inode));
262 return PTR_ERR(minor);
264 new_fops = fops_get(minor->dev->driver->fops);
270 replace_fops(filp, new_fops);
271 if (filp->f_op->open)
272 err = filp->f_op->open(inode, filp);
277 accel_minor_release(minor);
282 static const struct file_operations accel_stub_fops = {
283 .owner = THIS_MODULE,
284 .open = accel_stub_open,
285 .llseek = noop_llseek,
288 void accel_core_exit(void)
290 unregister_chrdev(ACCEL_MAJOR, "accel");
291 debugfs_remove(accel_debugfs_root);
292 accel_sysfs_destroy();
293 idr_destroy(&accel_minors_idr);
296 int __init accel_core_init(void)
300 idr_init(&accel_minors_idr);
302 ret = accel_sysfs_init();
304 DRM_ERROR("Cannot create ACCEL class: %d\n", ret);
308 accel_debugfs_root = debugfs_create_dir("accel", NULL);
310 ret = register_chrdev(ACCEL_MAJOR, "accel", &accel_stub_fops);
312 DRM_ERROR("Cannot register ACCEL major: %d\n", ret);
316 * Any cleanup due to errors will be done in drm_core_exit() that
317 * will call accel_core_exit()