Loading...
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/idr.h>
12
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>
20
21static DEFINE_SPINLOCK(accel_minor_lock);
22static struct idr accel_minors_idr;
23
24static struct dentry *accel_debugfs_root;
25
26static struct device_type accel_sysfs_device_minor = {
27 .name = "accel_minor"
28};
29
30static char *accel_devnode(const struct device *dev, umode_t *mode)
31{
32 return kasprintf(GFP_KERNEL, "accel/%s", dev_name(dev));
33}
34
35static const struct class accel_class = {
36 .name = "accel",
37 .devnode = accel_devnode,
38};
39
40static int accel_sysfs_init(void)
41{
42 return class_register(&accel_class);
43}
44
45static void accel_sysfs_destroy(void)
46{
47 class_unregister(&accel_class);
48}
49
50static int accel_name_info(struct seq_file *m, void *data)
51{
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;
56
57 mutex_lock(&dev->master_mutex);
58 master = dev->master;
59 seq_printf(m, "%s", dev->driver->name);
60 if (dev->dev)
61 seq_printf(m, " dev=%s", dev_name(dev->dev));
62 if (master && master->unique)
63 seq_printf(m, " master=%s", master->unique);
64 if (dev->unique)
65 seq_printf(m, " unique=%s", dev->unique);
66 seq_puts(m, "\n");
67 mutex_unlock(&dev->master_mutex);
68
69 return 0;
70}
71
72static const struct drm_info_list accel_debugfs_list[] = {
73 {"name", accel_name_info, 0}
74};
75#define ACCEL_DEBUGFS_ENTRIES ARRAY_SIZE(accel_debugfs_list)
76
77/**
78 * accel_debugfs_init() - Initialize debugfs for device
79 * @dev: Pointer to the device instance.
80 *
81 * This function creates a root directory for the device in debugfs.
82 */
83void accel_debugfs_init(struct drm_device *dev)
84{
85 drm_debugfs_dev_init(dev, accel_debugfs_root);
86}
87
88/**
89 * accel_debugfs_register() - Register debugfs for device
90 * @dev: Pointer to the device instance.
91 *
92 * Creates common files for accelerators.
93 */
94void accel_debugfs_register(struct drm_device *dev)
95{
96 struct drm_minor *minor = dev->accel;
97
98 minor->debugfs_root = dev->debugfs_root;
99
100 drm_debugfs_create_files(accel_debugfs_list, ACCEL_DEBUGFS_ENTRIES,
101 dev->debugfs_root, minor);
102}
103
104/**
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
108 *
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.
112 */
113void accel_set_device_instance_params(struct device *kdev, int index)
114{
115 kdev->devt = MKDEV(ACCEL_MAJOR, index);
116 kdev->class = &accel_class;
117 kdev->type = &accel_sysfs_device_minor;
118}
119
120/**
121 * accel_minor_alloc() - Allocates a new accel minor
122 *
123 * This function access the accel minors idr and allocates from it
124 * a new id to represent a new accel minor
125 *
126 * Return: A new id on success or error code in case idr_alloc failed
127 */
128int accel_minor_alloc(void)
129{
130 unsigned long flags;
131 int r;
132
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);
136
137 return r;
138}
139
140/**
141 * accel_minor_remove() - Remove an accel minor
142 * @index: The minor id to remove.
143 *
144 * This function access the accel minors idr and removes from
145 * it the member with the id that is passed to this function.
146 */
147void accel_minor_remove(int index)
148{
149 unsigned long flags;
150
151 spin_lock_irqsave(&accel_minor_lock, flags);
152 idr_remove(&accel_minors_idr, index);
153 spin_unlock_irqrestore(&accel_minor_lock, flags);
154}
155
156/**
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.
160 *
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.
164 *
165 * Return: 0 for success, negative value for error
166 */
167void accel_minor_replace(struct drm_minor *minor, int index)
168{
169 unsigned long flags;
170
171 spin_lock_irqsave(&accel_minor_lock, flags);
172 idr_replace(&accel_minors_idr, minor, index);
173 spin_unlock_irqrestore(&accel_minor_lock, flags);
174}
175
176/*
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().
180 *
181 * The object can be only a drm_minor that represents an accel device.
182 *
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.
186 */
187static struct drm_minor *accel_minor_acquire(unsigned int minor_id)
188{
189 struct drm_minor *minor;
190 unsigned long flags;
191
192 spin_lock_irqsave(&accel_minor_lock, flags);
193 minor = idr_find(&accel_minors_idr, minor_id);
194 if (minor)
195 drm_dev_get(minor->dev);
196 spin_unlock_irqrestore(&accel_minor_lock, flags);
197
198 if (!minor) {
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);
203 }
204
205 return minor;
206}
207
208static void accel_minor_release(struct drm_minor *minor)
209{
210 drm_dev_put(minor->dev);
211}
212
213/**
214 * accel_open - open method for ACCEL file
215 * @inode: device inode
216 * @filp: file pointer.
217 *
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.
221 *
222 * Return: 0 on success or negative errno value on failure.
223 */
224int accel_open(struct inode *inode, struct file *filp)
225{
226 struct drm_device *dev;
227 struct drm_minor *minor;
228 int retcode;
229
230 minor = accel_minor_acquire(iminor(inode));
231 if (IS_ERR(minor))
232 return PTR_ERR(minor);
233
234 dev = minor->dev;
235
236 atomic_fetch_inc(&dev->open_count);
237
238 /* share address_space across all char-devs of a single device */
239 filp->f_mapping = dev->anon_inode->i_mapping;
240
241 retcode = drm_open_helper(filp, minor);
242 if (retcode)
243 goto err_undo;
244
245 return 0;
246
247err_undo:
248 atomic_dec(&dev->open_count);
249 accel_minor_release(minor);
250 return retcode;
251}
252EXPORT_SYMBOL_GPL(accel_open);
253
254static int accel_stub_open(struct inode *inode, struct file *filp)
255{
256 const struct file_operations *new_fops;
257 struct drm_minor *minor;
258 int err;
259
260 minor = accel_minor_acquire(iminor(inode));
261 if (IS_ERR(minor))
262 return PTR_ERR(minor);
263
264 new_fops = fops_get(minor->dev->driver->fops);
265 if (!new_fops) {
266 err = -ENODEV;
267 goto out;
268 }
269
270 replace_fops(filp, new_fops);
271 if (filp->f_op->open)
272 err = filp->f_op->open(inode, filp);
273 else
274 err = 0;
275
276out:
277 accel_minor_release(minor);
278
279 return err;
280}
281
282static const struct file_operations accel_stub_fops = {
283 .owner = THIS_MODULE,
284 .open = accel_stub_open,
285 .llseek = noop_llseek,
286};
287
288void accel_core_exit(void)
289{
290 unregister_chrdev(ACCEL_MAJOR, "accel");
291 debugfs_remove(accel_debugfs_root);
292 accel_sysfs_destroy();
293 idr_destroy(&accel_minors_idr);
294}
295
296int __init accel_core_init(void)
297{
298 int ret;
299
300 idr_init(&accel_minors_idr);
301
302 ret = accel_sysfs_init();
303 if (ret < 0) {
304 DRM_ERROR("Cannot create ACCEL class: %d\n", ret);
305 goto error;
306 }
307
308 accel_debugfs_root = debugfs_create_dir("accel", NULL);
309
310 ret = register_chrdev(ACCEL_MAJOR, "accel", &accel_stub_fops);
311 if (ret < 0)
312 DRM_ERROR("Cannot register ACCEL major: %d\n", ret);
313
314error:
315 /*
316 * Any cleanup due to errors will be done in drm_core_exit() that
317 * will call accel_core_exit()
318 */
319 return ret;
320}
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/idr.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
20static DEFINE_SPINLOCK(accel_minor_lock);
21static struct idr accel_minors_idr;
22
23static struct dentry *accel_debugfs_root;
24static struct class *accel_class;
25
26static struct device_type accel_sysfs_device_minor = {
27 .name = "accel_minor"
28};
29
30static char *accel_devnode(const struct device *dev, umode_t *mode)
31{
32 return kasprintf(GFP_KERNEL, "accel/%s", dev_name(dev));
33}
34
35static int accel_sysfs_init(void)
36{
37 accel_class = class_create(THIS_MODULE, "accel");
38 if (IS_ERR(accel_class))
39 return PTR_ERR(accel_class);
40
41 accel_class->devnode = accel_devnode;
42
43 return 0;
44}
45
46static void accel_sysfs_destroy(void)
47{
48 if (IS_ERR_OR_NULL(accel_class))
49 return;
50 class_destroy(accel_class);
51 accel_class = NULL;
52}
53
54static int accel_name_info(struct seq_file *m, void *data)
55{
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;
60
61 mutex_lock(&dev->master_mutex);
62 master = dev->master;
63 seq_printf(m, "%s", dev->driver->name);
64 if (dev->dev)
65 seq_printf(m, " dev=%s", dev_name(dev->dev));
66 if (master && master->unique)
67 seq_printf(m, " master=%s", master->unique);
68 if (dev->unique)
69 seq_printf(m, " unique=%s", dev->unique);
70 seq_puts(m, "\n");
71 mutex_unlock(&dev->master_mutex);
72
73 return 0;
74}
75
76static const struct drm_info_list accel_debugfs_list[] = {
77 {"name", accel_name_info, 0}
78};
79#define ACCEL_DEBUGFS_ENTRIES ARRAY_SIZE(accel_debugfs_list)
80
81/**
82 * accel_debugfs_init() - Initialize debugfs for accel minor
83 * @minor: Pointer to the drm_minor instance.
84 * @minor_id: The minor's id
85 *
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.
89 */
90void accel_debugfs_init(struct drm_minor *minor, int minor_id)
91{
92 struct drm_device *dev = minor->dev;
93 char name[64];
94
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);
99
100 drm_debugfs_create_files(accel_debugfs_list, ACCEL_DEBUGFS_ENTRIES,
101 minor->debugfs_root, minor);
102
103 if (dev->driver->debugfs_init)
104 dev->driver->debugfs_init(minor);
105}
106
107/**
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
111 *
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.
115 */
116void accel_set_device_instance_params(struct device *kdev, int index)
117{
118 kdev->devt = MKDEV(ACCEL_MAJOR, index);
119 kdev->class = accel_class;
120 kdev->type = &accel_sysfs_device_minor;
121}
122
123/**
124 * accel_minor_alloc() - Allocates a new accel minor
125 *
126 * This function access the accel minors idr and allocates from it
127 * a new id to represent a new accel minor
128 *
129 * Return: A new id on success or error code in case idr_alloc failed
130 */
131int accel_minor_alloc(void)
132{
133 unsigned long flags;
134 int r;
135
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);
139
140 return r;
141}
142
143/**
144 * accel_minor_remove() - Remove an accel minor
145 * @index: The minor id to remove.
146 *
147 * This function access the accel minors idr and removes from
148 * it the member with the id that is passed to this function.
149 */
150void accel_minor_remove(int index)
151{
152 unsigned long flags;
153
154 spin_lock_irqsave(&accel_minor_lock, flags);
155 idr_remove(&accel_minors_idr, index);
156 spin_unlock_irqrestore(&accel_minor_lock, flags);
157}
158
159/**
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.
163 *
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.
167 *
168 * Return: 0 for success, negative value for error
169 */
170void accel_minor_replace(struct drm_minor *minor, int index)
171{
172 unsigned long flags;
173
174 spin_lock_irqsave(&accel_minor_lock, flags);
175 idr_replace(&accel_minors_idr, minor, index);
176 spin_unlock_irqrestore(&accel_minor_lock, flags);
177}
178
179/*
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().
183 *
184 * The object can be only a drm_minor that represents an accel device.
185 *
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.
189 */
190static struct drm_minor *accel_minor_acquire(unsigned int minor_id)
191{
192 struct drm_minor *minor;
193 unsigned long flags;
194
195 spin_lock_irqsave(&accel_minor_lock, flags);
196 minor = idr_find(&accel_minors_idr, minor_id);
197 if (minor)
198 drm_dev_get(minor->dev);
199 spin_unlock_irqrestore(&accel_minor_lock, flags);
200
201 if (!minor) {
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);
206 }
207
208 return minor;
209}
210
211static void accel_minor_release(struct drm_minor *minor)
212{
213 drm_dev_put(minor->dev);
214}
215
216/**
217 * accel_open - open method for ACCEL file
218 * @inode: device inode
219 * @filp: file pointer.
220 *
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.
224 *
225 * Return: 0 on success or negative errno value on failure.
226 */
227int accel_open(struct inode *inode, struct file *filp)
228{
229 struct drm_device *dev;
230 struct drm_minor *minor;
231 int retcode;
232
233 minor = accel_minor_acquire(iminor(inode));
234 if (IS_ERR(minor))
235 return PTR_ERR(minor);
236
237 dev = minor->dev;
238
239 atomic_fetch_inc(&dev->open_count);
240
241 /* share address_space across all char-devs of a single device */
242 filp->f_mapping = dev->anon_inode->i_mapping;
243
244 retcode = drm_open_helper(filp, minor);
245 if (retcode)
246 goto err_undo;
247
248 return 0;
249
250err_undo:
251 atomic_dec(&dev->open_count);
252 accel_minor_release(minor);
253 return retcode;
254}
255EXPORT_SYMBOL_GPL(accel_open);
256
257static int accel_stub_open(struct inode *inode, struct file *filp)
258{
259 const struct file_operations *new_fops;
260 struct drm_minor *minor;
261 int err;
262
263 minor = accel_minor_acquire(iminor(inode));
264 if (IS_ERR(minor))
265 return PTR_ERR(minor);
266
267 new_fops = fops_get(minor->dev->driver->fops);
268 if (!new_fops) {
269 err = -ENODEV;
270 goto out;
271 }
272
273 replace_fops(filp, new_fops);
274 if (filp->f_op->open)
275 err = filp->f_op->open(inode, filp);
276 else
277 err = 0;
278
279out:
280 accel_minor_release(minor);
281
282 return err;
283}
284
285static const struct file_operations accel_stub_fops = {
286 .owner = THIS_MODULE,
287 .open = accel_stub_open,
288 .llseek = noop_llseek,
289};
290
291void accel_core_exit(void)
292{
293 unregister_chrdev(ACCEL_MAJOR, "accel");
294 debugfs_remove(accel_debugfs_root);
295 accel_sysfs_destroy();
296 idr_destroy(&accel_minors_idr);
297}
298
299int __init accel_core_init(void)
300{
301 int ret;
302
303 idr_init(&accel_minors_idr);
304
305 ret = accel_sysfs_init();
306 if (ret < 0) {
307 DRM_ERROR("Cannot create ACCEL class: %d\n", ret);
308 goto error;
309 }
310
311 accel_debugfs_root = debugfs_create_dir("accel", NULL);
312
313 ret = register_chrdev(ACCEL_MAJOR, "accel", &accel_stub_fops);
314 if (ret < 0)
315 DRM_ERROR("Cannot register ACCEL major: %d\n", ret);
316
317error:
318 /*
319 * Any cleanup due to errors will be done in drm_core_exit() that
320 * will call accel_core_exit()
321 */
322 return ret;
323}