1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4 * Copyright 2022 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 *
7 */
8
9 #include <linux/debugfs.h>
10 #include <linux/device.h>
11 #include <linux/xarray.h>
12
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>
19
20 DEFINE_XARRAY_ALLOC(accel_minors_xa);
21
22 static struct dentry *accel_debugfs_root;
23 static struct class *accel_class;
24
25 static struct device_type accel_sysfs_device_minor = {
26 .name = "accel_minor"
27 };
28
accel_devnode(const struct device * dev,umode_t * mode)29 static char *accel_devnode(const struct device *dev, umode_t *mode)
30 {
31 return kasprintf(GFP_KERNEL, "accel/%s", dev_name(dev));
32 }
33
accel_sysfs_init(void)34 static int accel_sysfs_init(void)
35 {
36 accel_class = class_create("accel");
37 if (IS_ERR(accel_class))
38 return PTR_ERR(accel_class);
39
40 accel_class->devnode = accel_devnode;
41
42 return 0;
43 }
44
accel_sysfs_destroy(void)45 static void accel_sysfs_destroy(void)
46 {
47 if (IS_ERR_OR_NULL(accel_class))
48 return;
49 class_destroy(accel_class);
50 accel_class = NULL;
51 }
52
accel_name_info(struct seq_file * m,void * data)53 static int accel_name_info(struct seq_file *m, void *data)
54 {
55 struct drm_info_node *node = (struct drm_info_node *) m->private;
56 struct drm_minor *minor = node->minor;
57 struct drm_device *dev = minor->dev;
58 struct drm_master *master;
59
60 mutex_lock(&dev->master_mutex);
61 master = dev->master;
62 seq_printf(m, "%s", dev->driver->name);
63 if (dev->dev)
64 seq_printf(m, " dev=%s", dev_name(dev->dev));
65 if (master && master->unique)
66 seq_printf(m, " master=%s", master->unique);
67 if (dev->unique)
68 seq_printf(m, " unique=%s", dev->unique);
69 seq_puts(m, "\n");
70 mutex_unlock(&dev->master_mutex);
71
72 return 0;
73 }
74
75 static const struct drm_info_list accel_debugfs_list[] = {
76 {"name", accel_name_info, 0}
77 };
78 #define ACCEL_DEBUGFS_ENTRIES ARRAY_SIZE(accel_debugfs_list)
79
80 /**
81 * accel_debugfs_init() - Initialize debugfs for accel minor
82 * @minor: Pointer to the drm_minor instance.
83 * @minor_id: The minor's id
84 *
85 * This function initializes the drm minor's debugfs members and creates
86 * a root directory for the minor in debugfs. It also creates common files
87 * for accelerators and calls the driver's debugfs init callback.
88 */
accel_debugfs_init(struct drm_minor * minor,int minor_id)89 void accel_debugfs_init(struct drm_minor *minor, int minor_id)
90 {
91 struct drm_device *dev = minor->dev;
92 char name[64];
93
94 INIT_LIST_HEAD(&minor->debugfs_list);
95 mutex_init(&minor->debugfs_lock);
96 sprintf(name, "%d", minor_id);
97 minor->debugfs_root = debugfs_create_dir(name, accel_debugfs_root);
98
99 drm_debugfs_create_files(accel_debugfs_list, ACCEL_DEBUGFS_ENTRIES,
100 minor->debugfs_root, minor);
101
102 if (dev->driver->debugfs_init)
103 dev->driver->debugfs_init(minor);
104 }
105
106 /**
107 * accel_set_device_instance_params() - Set some device parameters for accel device
108 * @kdev: Pointer to the device instance.
109 * @index: The minor's index
110 *
111 * This function creates the dev_t of the device using the accel major and
112 * the device's minor number. In addition, it sets the class and type of the
113 * device instance to the accel sysfs class and device type, respectively.
114 */
accel_set_device_instance_params(struct device * kdev,int index)115 void accel_set_device_instance_params(struct device *kdev, int index)
116 {
117 kdev->devt = MKDEV(ACCEL_MAJOR, index);
118 kdev->class = accel_class;
119 kdev->type = &accel_sysfs_device_minor;
120 }
121
122 /**
123 * accel_open - open method for ACCEL file
124 * @inode: device inode
125 * @filp: file pointer.
126 *
127 * This function must be used by drivers as their &file_operations.open method.
128 * It looks up the correct ACCEL device and instantiates all the per-file
129 * resources for it. It also calls the &drm_driver.open driver callback.
130 *
131 * Return: 0 on success or negative errno value on failure.
132 */
accel_open(struct inode * inode,struct file * filp)133 int accel_open(struct inode *inode, struct file *filp)
134 {
135 struct drm_device *dev;
136 struct drm_minor *minor;
137 int retcode;
138
139 minor = drm_minor_acquire(&accel_minors_xa, iminor(inode));
140 if (IS_ERR(minor))
141 return PTR_ERR(minor);
142
143 dev = minor->dev;
144
145 atomic_fetch_inc(&dev->open_count);
146
147 /* share address_space across all char-devs of a single device */
148 filp->f_mapping = dev->anon_inode->i_mapping;
149
150 retcode = drm_open_helper(filp, minor);
151 if (retcode)
152 goto err_undo;
153
154 return 0;
155
156 err_undo:
157 atomic_dec(&dev->open_count);
158 drm_minor_release(minor);
159 return retcode;
160 }
161 EXPORT_SYMBOL_GPL(accel_open);
162
accel_stub_open(struct inode * inode,struct file * filp)163 static int accel_stub_open(struct inode *inode, struct file *filp)
164 {
165 const struct file_operations *new_fops;
166 struct drm_minor *minor;
167 int err;
168
169 minor = drm_minor_acquire(&accel_minors_xa, iminor(inode));
170 if (IS_ERR(minor))
171 return PTR_ERR(minor);
172
173 new_fops = fops_get(minor->dev->driver->fops);
174 if (!new_fops) {
175 err = -ENODEV;
176 goto out;
177 }
178
179 replace_fops(filp, new_fops);
180 if (filp->f_op->open)
181 err = filp->f_op->open(inode, filp);
182 else
183 err = 0;
184
185 out:
186 drm_minor_release(minor);
187
188 return err;
189 }
190
191 static const struct file_operations accel_stub_fops = {
192 .owner = THIS_MODULE,
193 .open = accel_stub_open,
194 .llseek = noop_llseek,
195 };
196
accel_core_exit(void)197 void accel_core_exit(void)
198 {
199 unregister_chrdev(ACCEL_MAJOR, "accel");
200 debugfs_remove(accel_debugfs_root);
201 accel_sysfs_destroy();
202 WARN_ON(!xa_empty(&accel_minors_xa));
203 }
204
accel_core_init(void)205 int __init accel_core_init(void)
206 {
207 int ret;
208
209 ret = accel_sysfs_init();
210 if (ret < 0) {
211 DRM_ERROR("Cannot create ACCEL class: %d\n", ret);
212 goto error;
213 }
214
215 accel_debugfs_root = debugfs_create_dir("accel", NULL);
216
217 ret = register_chrdev(ACCEL_MAJOR, "accel", &accel_stub_fops);
218 if (ret < 0)
219 DRM_ERROR("Cannot register ACCEL major: %d\n", ret);
220
221 error:
222 /*
223 * Any cleanup due to errors will be done in drm_core_exit() that
224 * will call accel_core_exit()
225 */
226 return ret;
227 }
228