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_debugfs.h>
15 #include <drm/drm_drv.h>
16 #include <drm/drm_file.h>
17 #include <drm/drm_ioctl.h>
18 #include <drm/drm_print.h>
20 static DEFINE_SPINLOCK(accel_minor_lock);
21 static struct idr accel_minors_idr;
23 static struct dentry *accel_debugfs_root;
24 static struct class *accel_class;
26 static struct device_type accel_sysfs_device_minor = {
30 static char *accel_devnode(struct device *dev, umode_t *mode)
32 return kasprintf(GFP_KERNEL, "accel/%s", dev_name(dev));
35 static int accel_sysfs_init(void)
37 accel_class = class_create(THIS_MODULE, "accel");
38 if (IS_ERR(accel_class))
39 return PTR_ERR(accel_class);
41 accel_class->devnode = accel_devnode;
46 static void accel_sysfs_destroy(void)
48 if (IS_ERR_OR_NULL(accel_class))
50 class_destroy(accel_class);
54 static int accel_name_info(struct seq_file *m, void *data)
56 struct drm_info_node *node = (struct drm_info_node *) m->private;
57 struct drm_minor *minor = node->minor;
58 struct drm_device *dev = minor->dev;
59 struct drm_master *master;
61 mutex_lock(&dev->master_mutex);
63 seq_printf(m, "%s", dev->driver->name);
65 seq_printf(m, " dev=%s", dev_name(dev->dev));
66 if (master && master->unique)
67 seq_printf(m, " master=%s", master->unique);
69 seq_printf(m, " unique=%s", dev->unique);
71 mutex_unlock(&dev->master_mutex);
76 static const struct drm_info_list accel_debugfs_list[] = {
77 {"name", accel_name_info, 0}
79 #define ACCEL_DEBUGFS_ENTRIES ARRAY_SIZE(accel_debugfs_list)
82 * accel_debugfs_init() - Initialize debugfs for accel minor
83 * @minor: Pointer to the drm_minor instance.
84 * @minor_id: The minor's id
86 * This function initializes the drm minor's debugfs members and creates
87 * a root directory for the minor in debugfs. It also creates common files
88 * for accelerators and calls the driver's debugfs init callback.
90 void accel_debugfs_init(struct drm_minor *minor, int minor_id)
92 struct drm_device *dev = minor->dev;
95 INIT_LIST_HEAD(&minor->debugfs_list);
96 mutex_init(&minor->debugfs_lock);
97 sprintf(name, "%d", minor_id);
98 minor->debugfs_root = debugfs_create_dir(name, accel_debugfs_root);
100 drm_debugfs_create_files(accel_debugfs_list, ACCEL_DEBUGFS_ENTRIES,
101 minor->debugfs_root, minor);
103 if (dev->driver->debugfs_init)
104 dev->driver->debugfs_init(minor);
108 * accel_set_device_instance_params() - Set some device parameters for accel device
109 * @kdev: Pointer to the device instance.
110 * @index: The minor's index
112 * This function creates the dev_t of the device using the accel major and
113 * the device's minor number. In addition, it sets the class and type of the
114 * device instance to the accel sysfs class and device type, respectively.
116 void accel_set_device_instance_params(struct device *kdev, int index)
118 kdev->devt = MKDEV(ACCEL_MAJOR, index);
119 kdev->class = accel_class;
120 kdev->type = &accel_sysfs_device_minor;
124 * accel_minor_alloc() - Allocates a new accel minor
126 * This function access the accel minors idr and allocates from it
127 * a new id to represent a new accel minor
129 * Return: A new id on success or error code in case idr_alloc failed
131 int accel_minor_alloc(void)
136 spin_lock_irqsave(&accel_minor_lock, flags);
137 r = idr_alloc(&accel_minors_idr, NULL, 0, ACCEL_MAX_MINORS, GFP_NOWAIT);
138 spin_unlock_irqrestore(&accel_minor_lock, flags);
144 * accel_minor_remove() - Remove an accel minor
145 * @index: The minor id to remove.
147 * This function access the accel minors idr and removes from
148 * it the member with the id that is passed to this function.
150 void accel_minor_remove(int index)
154 spin_lock_irqsave(&accel_minor_lock, flags);
155 idr_remove(&accel_minors_idr, index);
156 spin_unlock_irqrestore(&accel_minor_lock, flags);
160 * accel_minor_replace() - Replace minor pointer in accel minors idr.
161 * @minor: Pointer to the new minor.
162 * @index: The minor id to replace.
164 * This function access the accel minors idr structure and replaces the pointer
165 * that is associated with an existing id. Because the minor pointer can be
166 * NULL, we need to explicitly pass the index.
168 * Return: 0 for success, negative value for error
170 void accel_minor_replace(struct drm_minor *minor, int index)
174 spin_lock_irqsave(&accel_minor_lock, flags);
175 idr_replace(&accel_minors_idr, minor, index);
176 spin_unlock_irqrestore(&accel_minor_lock, flags);
180 * Looks up the given minor-ID and returns the respective DRM-minor object. The
181 * refence-count of the underlying device is increased so you must release this
182 * object with accel_minor_release().
184 * The object can be only a drm_minor that represents an accel device.
186 * As long as you hold this minor, it is guaranteed that the object and the
187 * minor->dev pointer will stay valid! However, the device may get unplugged and
188 * unregistered while you hold the minor.
190 static struct drm_minor *accel_minor_acquire(unsigned int minor_id)
192 struct drm_minor *minor;
195 spin_lock_irqsave(&accel_minor_lock, flags);
196 minor = idr_find(&accel_minors_idr, minor_id);
198 drm_dev_get(minor->dev);
199 spin_unlock_irqrestore(&accel_minor_lock, flags);
202 return ERR_PTR(-ENODEV);
203 } else if (drm_dev_is_unplugged(minor->dev)) {
204 drm_dev_put(minor->dev);
205 return ERR_PTR(-ENODEV);
211 static void accel_minor_release(struct drm_minor *minor)
213 drm_dev_put(minor->dev);
217 * accel_open - open method for ACCEL file
218 * @inode: device inode
219 * @filp: file pointer.
221 * This function must be used by drivers as their &file_operations.open method.
222 * It looks up the correct ACCEL device and instantiates all the per-file
223 * resources for it. It also calls the &drm_driver.open driver callback.
225 * Return: 0 on success or negative errno value on failure.
227 int accel_open(struct inode *inode, struct file *filp)
229 struct drm_device *dev;
230 struct drm_minor *minor;
233 minor = accel_minor_acquire(iminor(inode));
235 return PTR_ERR(minor);
239 atomic_fetch_inc(&dev->open_count);
241 /* share address_space across all char-devs of a single device */
242 filp->f_mapping = dev->anon_inode->i_mapping;
244 retcode = drm_open_helper(filp, minor);
251 atomic_dec(&dev->open_count);
252 accel_minor_release(minor);
255 EXPORT_SYMBOL_GPL(accel_open);
257 static int accel_stub_open(struct inode *inode, struct file *filp)
259 const struct file_operations *new_fops;
260 struct drm_minor *minor;
263 minor = accel_minor_acquire(iminor(inode));
265 return PTR_ERR(minor);
267 new_fops = fops_get(minor->dev->driver->fops);
273 replace_fops(filp, new_fops);
274 if (filp->f_op->open)
275 err = filp->f_op->open(inode, filp);
280 accel_minor_release(minor);
285 static const struct file_operations accel_stub_fops = {
286 .owner = THIS_MODULE,
287 .open = accel_stub_open,
288 .llseek = noop_llseek,
291 void accel_core_exit(void)
293 unregister_chrdev(ACCEL_MAJOR, "accel");
294 debugfs_remove(accel_debugfs_root);
295 accel_sysfs_destroy();
296 idr_destroy(&accel_minors_idr);
299 int __init accel_core_init(void)
303 idr_init(&accel_minors_idr);
305 ret = accel_sysfs_init();
307 DRM_ERROR("Cannot create ACCEL class: %d\n", ret);
311 accel_debugfs_root = debugfs_create_dir("accel", NULL);
313 ret = register_chrdev(ACCEL_MAJOR, "accel", &accel_stub_fops);
315 DRM_ERROR("Cannot register ACCEL major: %d\n", ret);
319 * Any cleanup due to errors will be done in drm_core_exit() that
320 * will call accel_core_exit()