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/xarray.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 DEFINE_XARRAY_ALLOC(accel_minors_xa);
23 static struct dentry *accel_debugfs_root;
25 static const struct device_type accel_sysfs_device_minor = {
29 static char *accel_devnode(const struct device *dev, umode_t *mode)
31 return kasprintf(GFP_KERNEL, "accel/%s", dev_name(dev));
34 static const struct class accel_class = {
36 .devnode = accel_devnode,
39 static int accel_sysfs_init(void)
41 return class_register(&accel_class);
44 static void accel_sysfs_destroy(void)
46 class_unregister(&accel_class);
49 static int accel_name_info(struct seq_file *m, void *data)
51 struct drm_info_node *node = (struct drm_info_node *) m->private;
52 struct drm_minor *minor = node->minor;
53 struct drm_device *dev = minor->dev;
54 struct drm_master *master;
56 mutex_lock(&dev->master_mutex);
58 seq_printf(m, "%s", dev->driver->name);
60 seq_printf(m, " dev=%s", dev_name(dev->dev));
61 if (master && master->unique)
62 seq_printf(m, " master=%s", master->unique);
64 seq_printf(m, " unique=%s", dev->unique);
66 mutex_unlock(&dev->master_mutex);
71 static const struct drm_info_list accel_debugfs_list[] = {
72 {"name", accel_name_info, 0}
74 #define ACCEL_DEBUGFS_ENTRIES ARRAY_SIZE(accel_debugfs_list)
77 * accel_debugfs_init() - Initialize debugfs for device
78 * @dev: Pointer to the device instance.
80 * This function creates a root directory for the device in debugfs.
82 void accel_debugfs_init(struct drm_device *dev)
84 drm_debugfs_dev_init(dev, accel_debugfs_root);
88 * accel_debugfs_register() - Register debugfs for device
89 * @dev: Pointer to the device instance.
91 * Creates common files for accelerators.
93 void accel_debugfs_register(struct drm_device *dev)
95 struct drm_minor *minor = dev->accel;
97 minor->debugfs_root = dev->debugfs_root;
99 drm_debugfs_create_files(accel_debugfs_list, ACCEL_DEBUGFS_ENTRIES,
100 dev->debugfs_root, minor);
104 * accel_set_device_instance_params() - Set some device parameters for accel device
105 * @kdev: Pointer to the device instance.
106 * @index: The minor's index
108 * This function creates the dev_t of the device using the accel major and
109 * the device's minor number. In addition, it sets the class and type of the
110 * device instance to the accel sysfs class and device type, respectively.
112 void accel_set_device_instance_params(struct device *kdev, int index)
114 kdev->devt = MKDEV(ACCEL_MAJOR, index);
115 kdev->class = &accel_class;
116 kdev->type = &accel_sysfs_device_minor;
120 * accel_open - open method for ACCEL file
121 * @inode: device inode
122 * @filp: file pointer.
124 * This function must be used by drivers as their &file_operations.open method.
125 * It looks up the correct ACCEL device and instantiates all the per-file
126 * resources for it. It also calls the &drm_driver.open driver callback.
128 * Return: 0 on success or negative errno value on failure.
130 int accel_open(struct inode *inode, struct file *filp)
132 struct drm_device *dev;
133 struct drm_minor *minor;
136 minor = drm_minor_acquire(&accel_minors_xa, iminor(inode));
138 return PTR_ERR(minor);
142 atomic_fetch_inc(&dev->open_count);
144 /* share address_space across all char-devs of a single device */
145 filp->f_mapping = dev->anon_inode->i_mapping;
147 retcode = drm_open_helper(filp, minor);
154 atomic_dec(&dev->open_count);
155 drm_minor_release(minor);
158 EXPORT_SYMBOL_GPL(accel_open);
160 static int accel_stub_open(struct inode *inode, struct file *filp)
162 const struct file_operations *new_fops;
163 struct drm_minor *minor;
166 minor = drm_minor_acquire(&accel_minors_xa, iminor(inode));
168 return PTR_ERR(minor);
170 new_fops = fops_get(minor->dev->driver->fops);
176 replace_fops(filp, new_fops);
177 if (filp->f_op->open)
178 err = filp->f_op->open(inode, filp);
183 drm_minor_release(minor);
188 static const struct file_operations accel_stub_fops = {
189 .owner = THIS_MODULE,
190 .open = accel_stub_open,
191 .llseek = noop_llseek,
194 void accel_core_exit(void)
196 unregister_chrdev(ACCEL_MAJOR, "accel");
197 debugfs_remove(accel_debugfs_root);
198 accel_sysfs_destroy();
199 WARN_ON(!xa_empty(&accel_minors_xa));
202 int __init accel_core_init(void)
206 ret = accel_sysfs_init();
208 DRM_ERROR("Cannot create ACCEL class: %d\n", ret);
212 accel_debugfs_root = debugfs_create_dir("accel", NULL);
214 ret = register_chrdev(ACCEL_MAJOR, "accel", &accel_stub_fops);
216 DRM_ERROR("Cannot register ACCEL major: %d\n", ret);
220 * Any cleanup due to errors will be done in drm_core_exit() that
221 * will call accel_core_exit()