drm_drv.c revision 1.9 1 1.2 riastrad /* $NetBSD: drm_drv.c,v 1.9 2018/08/27 14:41:41 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*
4 1.2 riastrad * Created: Fri Jan 19 10:48:35 2001 by faith (at) acm.org
5 1.1 riastrad *
6 1.2 riastrad * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
7 1.1 riastrad * All Rights Reserved.
8 1.1 riastrad *
9 1.2 riastrad * Author Rickard E. (Rik) Faith <faith (at) valinux.com>
10 1.2 riastrad *
11 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
12 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
13 1.1 riastrad * to deal in the Software without restriction, including without limitation
14 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
16 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
17 1.1 riastrad *
18 1.1 riastrad * The above copyright notice and this permission notice (including the next
19 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the
20 1.1 riastrad * Software.
21 1.1 riastrad *
22 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 1.2 riastrad * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 1.2 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 1.2 riastrad * DEALINGS IN THE SOFTWARE.
29 1.1 riastrad */
30 1.1 riastrad
31 1.2 riastrad #include <sys/cdefs.h>
32 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_drv.c,v 1.9 2018/08/27 14:41:41 riastradh Exp $");
33 1.2 riastrad
34 1.2 riastrad #include <linux/err.h>
35 1.2 riastrad #include <linux/export.h>
36 1.1 riastrad #include <linux/debugfs.h>
37 1.2 riastrad #include <linux/fs.h>
38 1.2 riastrad #include <linux/module.h>
39 1.2 riastrad #include <linux/moduleparam.h>
40 1.2 riastrad #include <linux/mount.h>
41 1.2 riastrad #include <linux/printk.h>
42 1.1 riastrad #include <linux/slab.h>
43 1.1 riastrad #include <drm/drmP.h>
44 1.1 riastrad #include <drm/drm_core.h>
45 1.2 riastrad #include "drm_legacy.h"
46 1.2 riastrad #include "drm_internal.h"
47 1.2 riastrad
48 1.7 riastrad unsigned int drm_debug = ~(unsigned int)0; /* bitmask of DRM_UT_x */
49 1.2 riastrad EXPORT_SYMBOL(drm_debug);
50 1.2 riastrad
51 1.2 riastrad MODULE_AUTHOR(CORE_AUTHOR);
52 1.2 riastrad MODULE_DESCRIPTION(CORE_DESC);
53 1.2 riastrad MODULE_LICENSE("GPL and additional rights");
54 1.2 riastrad MODULE_PARM_DESC(debug, "Enable debug output");
55 1.2 riastrad MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
56 1.2 riastrad MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
57 1.2 riastrad MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
58 1.2 riastrad
59 1.2 riastrad module_param_named(debug, drm_debug, int, 0600);
60 1.2 riastrad
61 1.2 riastrad #ifdef __NetBSD__
62 1.5 riastrad spinlock_t drm_minor_lock;
63 1.5 riastrad struct idr drm_minors_idr;
64 1.2 riastrad #else
65 1.2 riastrad static DEFINE_SPINLOCK(drm_minor_lock);
66 1.5 riastrad static struct idr drm_minors_idr;
67 1.2 riastrad #endif
68 1.2 riastrad
69 1.2 riastrad #ifndef __NetBSD__
70 1.2 riastrad static struct dentry *drm_debugfs_root;
71 1.2 riastrad #endif
72 1.2 riastrad
73 1.8 riastrad #ifdef __NetBSD__
74 1.8 riastrad void
75 1.8 riastrad drm_err(const char *file, int line, const char *func, const char *format, ...)
76 1.2 riastrad {
77 1.2 riastrad va_list args;
78 1.2 riastrad
79 1.2 riastrad va_start(args, format);
80 1.8 riastrad printf(KERN_ERR "[" DRM_NAME ":(%s:%d)%s] *ERROR* ", file, line, func);
81 1.2 riastrad vprintf(format, args);
82 1.2 riastrad va_end(args);
83 1.8 riastrad }
84 1.2 riastrad #else
85 1.8 riastrad void drm_err(const char *format, ...)
86 1.8 riastrad {
87 1.2 riastrad struct va_format vaf;
88 1.2 riastrad va_list args;
89 1.2 riastrad
90 1.2 riastrad va_start(args, format);
91 1.2 riastrad
92 1.2 riastrad vaf.fmt = format;
93 1.2 riastrad vaf.va = &args;
94 1.2 riastrad
95 1.2 riastrad printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
96 1.2 riastrad __builtin_return_address(0), &vaf);
97 1.2 riastrad
98 1.2 riastrad va_end(args);
99 1.8 riastrad }
100 1.2 riastrad #endif
101 1.2 riastrad EXPORT_SYMBOL(drm_err);
102 1.2 riastrad
103 1.2 riastrad void drm_ut_debug_printk(const char *function_name, const char *format, ...)
104 1.2 riastrad {
105 1.2 riastrad #ifdef __NetBSD__
106 1.2 riastrad va_list args;
107 1.2 riastrad
108 1.2 riastrad va_start(args, format);
109 1.2 riastrad printf("DRM debug in %s: ", function_name);
110 1.2 riastrad vprintf(format, args);
111 1.2 riastrad va_end(args);
112 1.2 riastrad #else
113 1.2 riastrad struct va_format vaf;
114 1.2 riastrad va_list args;
115 1.2 riastrad
116 1.2 riastrad va_start(args, format);
117 1.2 riastrad vaf.fmt = format;
118 1.2 riastrad vaf.va = &args;
119 1.2 riastrad
120 1.2 riastrad printk(KERN_DEBUG "[" DRM_NAME ":%s] %pV", function_name, &vaf);
121 1.2 riastrad
122 1.2 riastrad va_end(args);
123 1.2 riastrad #endif
124 1.2 riastrad }
125 1.2 riastrad EXPORT_SYMBOL(drm_ut_debug_printk);
126 1.2 riastrad
127 1.2 riastrad struct drm_master *drm_master_create(struct drm_minor *minor)
128 1.2 riastrad {
129 1.2 riastrad struct drm_master *master;
130 1.2 riastrad
131 1.2 riastrad master = kzalloc(sizeof(*master), GFP_KERNEL);
132 1.2 riastrad if (!master)
133 1.2 riastrad return NULL;
134 1.2 riastrad
135 1.2 riastrad kref_init(&master->refcount);
136 1.2 riastrad spin_lock_init(&master->lock.spinlock);
137 1.2 riastrad #ifdef __NetBSD__
138 1.2 riastrad DRM_INIT_WAITQUEUE(&master->lock.lock_queue, "drmlockq");
139 1.2 riastrad #else
140 1.2 riastrad init_waitqueue_head(&master->lock.lock_queue);
141 1.2 riastrad #endif
142 1.2 riastrad idr_init(&master->magic_map);
143 1.2 riastrad master->minor = minor;
144 1.2 riastrad
145 1.2 riastrad return master;
146 1.2 riastrad }
147 1.2 riastrad
148 1.2 riastrad struct drm_master *drm_master_get(struct drm_master *master)
149 1.2 riastrad {
150 1.2 riastrad kref_get(&master->refcount);
151 1.2 riastrad return master;
152 1.2 riastrad }
153 1.2 riastrad EXPORT_SYMBOL(drm_master_get);
154 1.2 riastrad
155 1.2 riastrad static void drm_master_destroy(struct kref *kref)
156 1.2 riastrad {
157 1.2 riastrad struct drm_master *master = container_of(kref, struct drm_master, refcount);
158 1.2 riastrad struct drm_device *dev = master->minor->dev;
159 1.2 riastrad struct drm_map_list *r_list, *list_temp;
160 1.2 riastrad
161 1.2 riastrad mutex_lock(&dev->struct_mutex);
162 1.2 riastrad if (dev->driver->master_destroy)
163 1.2 riastrad dev->driver->master_destroy(dev, master);
164 1.2 riastrad
165 1.2 riastrad list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
166 1.2 riastrad if (r_list->master == master) {
167 1.2 riastrad drm_legacy_rmmap_locked(dev, r_list->map);
168 1.2 riastrad r_list = NULL;
169 1.2 riastrad }
170 1.2 riastrad }
171 1.2 riastrad mutex_unlock(&dev->struct_mutex);
172 1.2 riastrad
173 1.2 riastrad idr_destroy(&master->magic_map);
174 1.2 riastrad #ifdef __NetBSD__
175 1.2 riastrad DRM_DESTROY_WAITQUEUE(&master->lock.lock_queue);
176 1.2 riastrad spin_lock_destroy(&master->lock.spinlock);
177 1.2 riastrad #endif
178 1.2 riastrad kfree(master->unique);
179 1.2 riastrad kfree(master);
180 1.2 riastrad }
181 1.2 riastrad
182 1.2 riastrad void drm_master_put(struct drm_master **master)
183 1.2 riastrad {
184 1.2 riastrad kref_put(&(*master)->refcount, drm_master_destroy);
185 1.2 riastrad *master = NULL;
186 1.2 riastrad }
187 1.2 riastrad EXPORT_SYMBOL(drm_master_put);
188 1.2 riastrad
189 1.2 riastrad int drm_setmaster_ioctl(struct drm_device *dev, void *data,
190 1.2 riastrad struct drm_file *file_priv)
191 1.2 riastrad {
192 1.2 riastrad int ret = 0;
193 1.2 riastrad
194 1.2 riastrad mutex_lock(&dev->master_mutex);
195 1.2 riastrad if (file_priv->is_master)
196 1.2 riastrad goto out_unlock;
197 1.2 riastrad
198 1.2 riastrad if (file_priv->minor->master) {
199 1.2 riastrad ret = -EINVAL;
200 1.2 riastrad goto out_unlock;
201 1.2 riastrad }
202 1.2 riastrad
203 1.2 riastrad if (!file_priv->master) {
204 1.2 riastrad ret = -EINVAL;
205 1.2 riastrad goto out_unlock;
206 1.2 riastrad }
207 1.2 riastrad
208 1.2 riastrad if (!file_priv->allowed_master) {
209 1.2 riastrad ret = drm_new_set_master(dev, file_priv);
210 1.2 riastrad goto out_unlock;
211 1.2 riastrad }
212 1.2 riastrad
213 1.2 riastrad file_priv->minor->master = drm_master_get(file_priv->master);
214 1.2 riastrad file_priv->is_master = 1;
215 1.2 riastrad if (dev->driver->master_set) {
216 1.2 riastrad ret = dev->driver->master_set(dev, file_priv, false);
217 1.2 riastrad if (unlikely(ret != 0)) {
218 1.2 riastrad file_priv->is_master = 0;
219 1.2 riastrad drm_master_put(&file_priv->minor->master);
220 1.2 riastrad }
221 1.2 riastrad }
222 1.2 riastrad
223 1.2 riastrad out_unlock:
224 1.2 riastrad mutex_unlock(&dev->master_mutex);
225 1.2 riastrad return ret;
226 1.2 riastrad }
227 1.2 riastrad
228 1.2 riastrad int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
229 1.2 riastrad struct drm_file *file_priv)
230 1.2 riastrad {
231 1.2 riastrad int ret = -EINVAL;
232 1.2 riastrad
233 1.2 riastrad mutex_lock(&dev->master_mutex);
234 1.2 riastrad if (!file_priv->is_master)
235 1.2 riastrad goto out_unlock;
236 1.2 riastrad
237 1.2 riastrad if (!file_priv->minor->master)
238 1.2 riastrad goto out_unlock;
239 1.2 riastrad
240 1.2 riastrad ret = 0;
241 1.2 riastrad if (dev->driver->master_drop)
242 1.2 riastrad dev->driver->master_drop(dev, file_priv, false);
243 1.2 riastrad drm_master_put(&file_priv->minor->master);
244 1.2 riastrad file_priv->is_master = 0;
245 1.2 riastrad
246 1.2 riastrad out_unlock:
247 1.2 riastrad mutex_unlock(&dev->master_mutex);
248 1.2 riastrad return ret;
249 1.2 riastrad }
250 1.2 riastrad
251 1.2 riastrad /*
252 1.2 riastrad * DRM Minors
253 1.2 riastrad * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
254 1.2 riastrad * of them is represented by a drm_minor object. Depending on the capabilities
255 1.2 riastrad * of the device-driver, different interfaces are registered.
256 1.2 riastrad *
257 1.2 riastrad * Minors can be accessed via dev->$minor_name. This pointer is either
258 1.2 riastrad * NULL or a valid drm_minor pointer and stays valid as long as the device is
259 1.2 riastrad * valid. This means, DRM minors have the same life-time as the underlying
260 1.2 riastrad * device. However, this doesn't mean that the minor is active. Minors are
261 1.2 riastrad * registered and unregistered dynamically according to device-state.
262 1.2 riastrad */
263 1.1 riastrad
264 1.2 riastrad static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
265 1.2 riastrad unsigned int type)
266 1.2 riastrad {
267 1.2 riastrad switch (type) {
268 1.2 riastrad case DRM_MINOR_LEGACY:
269 1.2 riastrad return &dev->primary;
270 1.2 riastrad case DRM_MINOR_RENDER:
271 1.2 riastrad return &dev->render;
272 1.2 riastrad case DRM_MINOR_CONTROL:
273 1.2 riastrad return &dev->control;
274 1.2 riastrad default:
275 1.2 riastrad return NULL;
276 1.2 riastrad }
277 1.2 riastrad }
278 1.2 riastrad
279 1.2 riastrad static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
280 1.2 riastrad {
281 1.2 riastrad struct drm_minor *minor;
282 1.2 riastrad unsigned long flags;
283 1.2 riastrad int r;
284 1.2 riastrad
285 1.2 riastrad minor = kzalloc(sizeof(*minor), GFP_KERNEL);
286 1.2 riastrad if (!minor)
287 1.2 riastrad return -ENOMEM;
288 1.2 riastrad
289 1.2 riastrad minor->type = type;
290 1.2 riastrad minor->dev = dev;
291 1.2 riastrad
292 1.2 riastrad idr_preload(GFP_KERNEL);
293 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
294 1.2 riastrad r = idr_alloc(&drm_minors_idr,
295 1.2 riastrad NULL,
296 1.2 riastrad 64 * type,
297 1.2 riastrad 64 * (type + 1),
298 1.2 riastrad GFP_NOWAIT);
299 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
300 1.2 riastrad idr_preload_end();
301 1.2 riastrad
302 1.2 riastrad if (r < 0)
303 1.2 riastrad goto err_free;
304 1.2 riastrad
305 1.2 riastrad minor->index = r;
306 1.2 riastrad
307 1.6 riastrad #ifndef __NetBSD__ /* XXX drm sysfs */
308 1.2 riastrad minor->kdev = drm_sysfs_minor_alloc(minor);
309 1.2 riastrad if (IS_ERR(minor->kdev)) {
310 1.2 riastrad r = PTR_ERR(minor->kdev);
311 1.2 riastrad goto err_index;
312 1.2 riastrad }
313 1.6 riastrad #endif
314 1.1 riastrad
315 1.2 riastrad *drm_minor_get_slot(dev, type) = minor;
316 1.2 riastrad return 0;
317 1.2 riastrad
318 1.6 riastrad err_index: __unused
319 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
320 1.2 riastrad idr_remove(&drm_minors_idr, minor->index);
321 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
322 1.2 riastrad err_free:
323 1.2 riastrad kfree(minor);
324 1.2 riastrad return r;
325 1.2 riastrad }
326 1.2 riastrad
327 1.2 riastrad static void drm_minor_free(struct drm_device *dev, unsigned int type)
328 1.2 riastrad {
329 1.2 riastrad struct drm_minor **slot, *minor;
330 1.2 riastrad unsigned long flags;
331 1.1 riastrad
332 1.2 riastrad slot = drm_minor_get_slot(dev, type);
333 1.2 riastrad minor = *slot;
334 1.2 riastrad if (!minor)
335 1.2 riastrad return;
336 1.2 riastrad
337 1.6 riastrad #ifndef __NetBSD__ /* XXX drm sysfs */
338 1.2 riastrad put_device(minor->kdev);
339 1.5 riastrad #endif
340 1.2 riastrad
341 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
342 1.2 riastrad idr_remove(&drm_minors_idr, minor->index);
343 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
344 1.1 riastrad
345 1.2 riastrad kfree(minor);
346 1.2 riastrad *slot = NULL;
347 1.2 riastrad }
348 1.2 riastrad
349 1.2 riastrad static int drm_minor_register(struct drm_device *dev, unsigned int type)
350 1.2 riastrad {
351 1.2 riastrad struct drm_minor *minor;
352 1.2 riastrad unsigned long flags;
353 1.2 riastrad #ifndef __NetBSD__
354 1.2 riastrad int ret;
355 1.2 riastrad #endif
356 1.2 riastrad
357 1.2 riastrad DRM_DEBUG("\n");
358 1.2 riastrad
359 1.2 riastrad minor = *drm_minor_get_slot(dev, type);
360 1.2 riastrad if (!minor)
361 1.2 riastrad return 0;
362 1.2 riastrad
363 1.2 riastrad #ifndef __NetBSD__
364 1.2 riastrad ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
365 1.2 riastrad if (ret) {
366 1.2 riastrad DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
367 1.2 riastrad goto err_debugfs;
368 1.2 riastrad }
369 1.2 riastrad
370 1.2 riastrad ret = device_add(minor->kdev);
371 1.2 riastrad if (ret)
372 1.2 riastrad goto err_debugfs;
373 1.2 riastrad #endif
374 1.2 riastrad
375 1.2 riastrad /* replace NULL with @minor so lookups will succeed from now on */
376 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
377 1.2 riastrad idr_replace(&drm_minors_idr, minor, minor->index);
378 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
379 1.2 riastrad
380 1.2 riastrad DRM_DEBUG("new minor registered %d\n", minor->index);
381 1.2 riastrad return 0;
382 1.2 riastrad
383 1.2 riastrad #ifndef __NetBSD__
384 1.2 riastrad err_debugfs:
385 1.2 riastrad drm_debugfs_cleanup(minor);
386 1.2 riastrad return ret;
387 1.2 riastrad #endif
388 1.2 riastrad }
389 1.2 riastrad
390 1.2 riastrad static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
391 1.2 riastrad {
392 1.2 riastrad struct drm_minor *minor;
393 1.2 riastrad unsigned long flags;
394 1.1 riastrad
395 1.2 riastrad minor = *drm_minor_get_slot(dev, type);
396 1.5 riastrad #ifdef __NetBSD__
397 1.5 riastrad if (!minor)
398 1.5 riastrad #else
399 1.2 riastrad if (!minor || !device_is_registered(minor->kdev))
400 1.5 riastrad #endif
401 1.2 riastrad return;
402 1.2 riastrad
403 1.2 riastrad /* replace @minor with NULL so lookups will fail from now on */
404 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
405 1.2 riastrad idr_replace(&drm_minors_idr, NULL, minor->index);
406 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
407 1.2 riastrad
408 1.5 riastrad #ifndef __NetBSD__
409 1.2 riastrad device_del(minor->kdev);
410 1.2 riastrad dev_set_drvdata(minor->kdev, NULL); /* safety belt */
411 1.2 riastrad drm_debugfs_cleanup(minor);
412 1.5 riastrad #endif
413 1.2 riastrad }
414 1.1 riastrad
415 1.1 riastrad /**
416 1.2 riastrad * drm_minor_acquire - Acquire a DRM minor
417 1.2 riastrad * @minor_id: Minor ID of the DRM-minor
418 1.1 riastrad *
419 1.2 riastrad * Looks up the given minor-ID and returns the respective DRM-minor object. The
420 1.2 riastrad * refence-count of the underlying device is increased so you must release this
421 1.2 riastrad * object with drm_minor_release().
422 1.1 riastrad *
423 1.2 riastrad * As long as you hold this minor, it is guaranteed that the object and the
424 1.2 riastrad * minor->dev pointer will stay valid! However, the device may get unplugged and
425 1.2 riastrad * unregistered while you hold the minor.
426 1.1 riastrad *
427 1.2 riastrad * Returns:
428 1.2 riastrad * Pointer to minor-object with increased device-refcount, or PTR_ERR on
429 1.2 riastrad * failure.
430 1.1 riastrad */
431 1.2 riastrad struct drm_minor *drm_minor_acquire(unsigned int minor_id)
432 1.1 riastrad {
433 1.2 riastrad struct drm_minor *minor;
434 1.2 riastrad unsigned long flags;
435 1.2 riastrad
436 1.2 riastrad spin_lock_irqsave(&drm_minor_lock, flags);
437 1.2 riastrad minor = idr_find(&drm_minors_idr, minor_id);
438 1.2 riastrad if (minor)
439 1.2 riastrad drm_dev_ref(minor->dev);
440 1.2 riastrad spin_unlock_irqrestore(&drm_minor_lock, flags);
441 1.2 riastrad
442 1.2 riastrad if (!minor) {
443 1.2 riastrad return ERR_PTR(-ENODEV);
444 1.2 riastrad } else if (drm_device_is_unplugged(minor->dev)) {
445 1.2 riastrad drm_dev_unref(minor->dev);
446 1.2 riastrad return ERR_PTR(-ENODEV);
447 1.2 riastrad }
448 1.2 riastrad
449 1.2 riastrad return minor;
450 1.2 riastrad }
451 1.1 riastrad
452 1.2 riastrad /**
453 1.2 riastrad * drm_minor_release - Release DRM minor
454 1.2 riastrad * @minor: Pointer to DRM minor object
455 1.2 riastrad *
456 1.2 riastrad * Release a minor that was previously acquired via drm_minor_acquire().
457 1.2 riastrad */
458 1.2 riastrad void drm_minor_release(struct drm_minor *minor)
459 1.2 riastrad {
460 1.2 riastrad drm_dev_unref(minor->dev);
461 1.2 riastrad }
462 1.2 riastrad
463 1.2 riastrad /**
464 1.2 riastrad * DOC: driver instance overview
465 1.2 riastrad *
466 1.2 riastrad * A device instance for a drm driver is represented by struct &drm_device. This
467 1.2 riastrad * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
468 1.2 riastrad * callbacks implemented by the driver. The driver then needs to initialize all
469 1.2 riastrad * the various subsystems for the drm device like memory management, vblank
470 1.2 riastrad * handling, modesetting support and intial output configuration plus obviously
471 1.2 riastrad * initialize all the corresponding hardware bits. An important part of this is
472 1.2 riastrad * also calling drm_dev_set_unique() to set the userspace-visible unique name of
473 1.2 riastrad * this device instance. Finally when everything is up and running and ready for
474 1.2 riastrad * userspace the device instance can be published using drm_dev_register().
475 1.2 riastrad *
476 1.2 riastrad * There is also deprecated support for initalizing device instances using
477 1.2 riastrad * bus-specific helpers and the ->load() callback. But due to
478 1.2 riastrad * backwards-compatibility needs the device instance have to be published too
479 1.2 riastrad * early, which requires unpretty global locking to make safe and is therefore
480 1.2 riastrad * only support for existing drivers not yet converted to the new scheme.
481 1.2 riastrad *
482 1.2 riastrad * When cleaning up a device instance everything needs to be done in reverse:
483 1.2 riastrad * First unpublish the device instance with drm_dev_unregister(). Then clean up
484 1.2 riastrad * any other resources allocated at device initialization and drop the driver's
485 1.2 riastrad * reference to &drm_device using drm_dev_unref().
486 1.2 riastrad *
487 1.2 riastrad * Note that the lifetime rules for &drm_device instance has still a lot of
488 1.2 riastrad * historical baggage. Hence use the reference counting provided by
489 1.2 riastrad * drm_dev_ref() and drm_dev_unref() only carefully.
490 1.2 riastrad *
491 1.2 riastrad * Also note that embedding of &drm_device is currently not (yet) supported (but
492 1.2 riastrad * it would be easy to add). Drivers can store driver-private data in the
493 1.2 riastrad * dev_priv field of &drm_device.
494 1.2 riastrad */
495 1.2 riastrad
496 1.2 riastrad /**
497 1.2 riastrad * drm_put_dev - Unregister and release a DRM device
498 1.2 riastrad * @dev: DRM device
499 1.2 riastrad *
500 1.2 riastrad * Called at module unload time or when a PCI device is unplugged.
501 1.2 riastrad *
502 1.2 riastrad * Cleans up all DRM device, calling drm_lastclose().
503 1.2 riastrad *
504 1.2 riastrad * Note: Use of this function is deprecated. It will eventually go away
505 1.2 riastrad * completely. Please use drm_dev_unregister() and drm_dev_unref() explicitly
506 1.2 riastrad * instead to make sure that the device isn't userspace accessible any more
507 1.2 riastrad * while teardown is in progress, ensuring that userspace can't access an
508 1.2 riastrad * inconsistent state.
509 1.2 riastrad */
510 1.2 riastrad void drm_put_dev(struct drm_device *dev)
511 1.2 riastrad {
512 1.1 riastrad DRM_DEBUG("\n");
513 1.1 riastrad
514 1.2 riastrad if (!dev) {
515 1.2 riastrad DRM_ERROR("cleanup called no dev\n");
516 1.2 riastrad return;
517 1.2 riastrad }
518 1.2 riastrad
519 1.2 riastrad drm_dev_unregister(dev);
520 1.2 riastrad drm_dev_unref(dev);
521 1.2 riastrad }
522 1.2 riastrad EXPORT_SYMBOL(drm_put_dev);
523 1.2 riastrad
524 1.2 riastrad void drm_unplug_dev(struct drm_device *dev)
525 1.2 riastrad {
526 1.2 riastrad /* for a USB device */
527 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_LEGACY);
528 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_RENDER);
529 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_CONTROL);
530 1.2 riastrad
531 1.2 riastrad mutex_lock(&drm_global_mutex);
532 1.2 riastrad
533 1.2 riastrad drm_device_set_unplugged(dev);
534 1.2 riastrad
535 1.2 riastrad if (dev->open_count == 0) {
536 1.2 riastrad drm_put_dev(dev);
537 1.2 riastrad }
538 1.2 riastrad mutex_unlock(&drm_global_mutex);
539 1.2 riastrad }
540 1.2 riastrad EXPORT_SYMBOL(drm_unplug_dev);
541 1.2 riastrad
542 1.2 riastrad #ifdef __NetBSD__
543 1.2 riastrad
544 1.2 riastrad struct inode;
545 1.2 riastrad
546 1.2 riastrad static struct inode *
547 1.2 riastrad drm_fs_inode_new(void)
548 1.2 riastrad {
549 1.2 riastrad return NULL;
550 1.2 riastrad }
551 1.2 riastrad
552 1.2 riastrad static void
553 1.2 riastrad drm_fs_inode_free(struct inode *inode)
554 1.2 riastrad {
555 1.2 riastrad KASSERT(inode == NULL);
556 1.2 riastrad }
557 1.2 riastrad
558 1.2 riastrad #else
559 1.2 riastrad
560 1.2 riastrad /*
561 1.2 riastrad * DRM internal mount
562 1.2 riastrad * We want to be able to allocate our own "struct address_space" to control
563 1.2 riastrad * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
564 1.2 riastrad * stand-alone address_space objects, so we need an underlying inode. As there
565 1.2 riastrad * is no way to allocate an independent inode easily, we need a fake internal
566 1.2 riastrad * VFS mount-point.
567 1.2 riastrad *
568 1.2 riastrad * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
569 1.2 riastrad * frees it again. You are allowed to use iget() and iput() to get references to
570 1.2 riastrad * the inode. But each drm_fs_inode_new() call must be paired with exactly one
571 1.2 riastrad * drm_fs_inode_free() call (which does not have to be the last iput()).
572 1.2 riastrad * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
573 1.2 riastrad * between multiple inode-users. You could, technically, call
574 1.2 riastrad * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
575 1.2 riastrad * iput(), but this way you'd end up with a new vfsmount for each inode.
576 1.2 riastrad */
577 1.2 riastrad
578 1.2 riastrad static int drm_fs_cnt;
579 1.2 riastrad static struct vfsmount *drm_fs_mnt;
580 1.1 riastrad
581 1.2 riastrad static const struct dentry_operations drm_fs_dops = {
582 1.2 riastrad .d_dname = simple_dname,
583 1.2 riastrad };
584 1.2 riastrad
585 1.2 riastrad static const struct super_operations drm_fs_sops = {
586 1.2 riastrad .statfs = simple_statfs,
587 1.2 riastrad };
588 1.2 riastrad
589 1.2 riastrad static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
590 1.2 riastrad const char *dev_name, void *data)
591 1.2 riastrad {
592 1.2 riastrad return mount_pseudo(fs_type,
593 1.2 riastrad "drm:",
594 1.2 riastrad &drm_fs_sops,
595 1.2 riastrad &drm_fs_dops,
596 1.2 riastrad 0x010203ff);
597 1.2 riastrad }
598 1.2 riastrad
599 1.2 riastrad static struct file_system_type drm_fs_type = {
600 1.2 riastrad .name = "drm",
601 1.2 riastrad .owner = THIS_MODULE,
602 1.2 riastrad .mount = drm_fs_mount,
603 1.2 riastrad .kill_sb = kill_anon_super,
604 1.2 riastrad };
605 1.2 riastrad
606 1.2 riastrad static struct inode *drm_fs_inode_new(void)
607 1.2 riastrad {
608 1.2 riastrad struct inode *inode;
609 1.2 riastrad int r;
610 1.2 riastrad
611 1.2 riastrad r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
612 1.2 riastrad if (r < 0) {
613 1.2 riastrad DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
614 1.2 riastrad return ERR_PTR(r);
615 1.2 riastrad }
616 1.2 riastrad
617 1.2 riastrad inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
618 1.2 riastrad if (IS_ERR(inode))
619 1.2 riastrad simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
620 1.2 riastrad
621 1.2 riastrad return inode;
622 1.2 riastrad }
623 1.2 riastrad
624 1.2 riastrad static void drm_fs_inode_free(struct inode *inode)
625 1.2 riastrad {
626 1.2 riastrad if (inode) {
627 1.2 riastrad iput(inode);
628 1.2 riastrad simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
629 1.2 riastrad }
630 1.2 riastrad }
631 1.2 riastrad
632 1.2 riastrad #endif
633 1.1 riastrad
634 1.2 riastrad /**
635 1.2 riastrad * drm_dev_alloc - Allocate new DRM device
636 1.2 riastrad * @driver: DRM driver to allocate device for
637 1.2 riastrad * @parent: Parent device object
638 1.2 riastrad *
639 1.2 riastrad * Allocate and initialize a new DRM device. No device registration is done.
640 1.2 riastrad * Call drm_dev_register() to advertice the device to user space and register it
641 1.2 riastrad * with other core subsystems. This should be done last in the device
642 1.2 riastrad * initialization sequence to make sure userspace can't access an inconsistent
643 1.2 riastrad * state.
644 1.2 riastrad *
645 1.2 riastrad * The initial ref-count of the object is 1. Use drm_dev_ref() and
646 1.2 riastrad * drm_dev_unref() to take and drop further ref-counts.
647 1.2 riastrad *
648 1.2 riastrad * Note that for purely virtual devices @parent can be NULL.
649 1.2 riastrad *
650 1.2 riastrad * RETURNS:
651 1.2 riastrad * Pointer to new DRM device, or NULL if out of memory.
652 1.2 riastrad */
653 1.2 riastrad struct drm_device *drm_dev_alloc(struct drm_driver *driver,
654 1.2 riastrad struct device *parent)
655 1.2 riastrad {
656 1.2 riastrad struct drm_device *dev;
657 1.2 riastrad int ret;
658 1.2 riastrad
659 1.2 riastrad dev = kzalloc(sizeof(*dev), GFP_KERNEL);
660 1.2 riastrad if (!dev)
661 1.2 riastrad return NULL;
662 1.2 riastrad
663 1.2 riastrad kref_init(&dev->ref);
664 1.2 riastrad dev->dev = parent;
665 1.2 riastrad dev->driver = driver;
666 1.2 riastrad
667 1.2 riastrad INIT_LIST_HEAD(&dev->filelist);
668 1.2 riastrad INIT_LIST_HEAD(&dev->ctxlist);
669 1.2 riastrad INIT_LIST_HEAD(&dev->vmalist);
670 1.2 riastrad INIT_LIST_HEAD(&dev->maplist);
671 1.2 riastrad INIT_LIST_HEAD(&dev->vblank_event_list);
672 1.2 riastrad
673 1.2 riastrad spin_lock_init(&dev->buf_lock);
674 1.2 riastrad spin_lock_init(&dev->event_lock);
675 1.2 riastrad #ifdef __NetBSD__
676 1.2 riastrad linux_mutex_init(&dev->struct_mutex);
677 1.2 riastrad linux_mutex_init(&dev->ctxlist_mutex);
678 1.2 riastrad linux_mutex_init(&dev->master_mutex);
679 1.2 riastrad #else
680 1.2 riastrad mutex_init(&dev->struct_mutex);
681 1.2 riastrad mutex_init(&dev->ctxlist_mutex);
682 1.2 riastrad mutex_init(&dev->master_mutex);
683 1.2 riastrad #endif
684 1.1 riastrad
685 1.2 riastrad dev->anon_inode = drm_fs_inode_new();
686 1.2 riastrad if (IS_ERR(dev->anon_inode)) {
687 1.2 riastrad ret = PTR_ERR(dev->anon_inode);
688 1.2 riastrad DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
689 1.2 riastrad goto err_free;
690 1.2 riastrad }
691 1.1 riastrad
692 1.2 riastrad if (drm_core_check_feature(dev, DRIVER_MODESET)) {
693 1.2 riastrad ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
694 1.2 riastrad if (ret)
695 1.2 riastrad goto err_minors;
696 1.1 riastrad
697 1.2 riastrad WARN_ON(driver->suspend || driver->resume);
698 1.1 riastrad }
699 1.2 riastrad
700 1.2 riastrad if (drm_core_check_feature(dev, DRIVER_RENDER)) {
701 1.2 riastrad ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
702 1.2 riastrad if (ret)
703 1.2 riastrad goto err_minors;
704 1.1 riastrad }
705 1.1 riastrad
706 1.2 riastrad ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
707 1.2 riastrad if (ret)
708 1.2 riastrad goto err_minors;
709 1.2 riastrad
710 1.2 riastrad if (drm_ht_create(&dev->map_hash, 12))
711 1.2 riastrad goto err_minors;
712 1.2 riastrad
713 1.2 riastrad drm_legacy_ctxbitmap_init(dev);
714 1.2 riastrad
715 1.2 riastrad if (drm_core_check_feature(dev, DRIVER_GEM)) {
716 1.2 riastrad ret = drm_gem_init(dev);
717 1.2 riastrad if (ret) {
718 1.2 riastrad DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
719 1.2 riastrad goto err_ctxbitmap;
720 1.2 riastrad }
721 1.1 riastrad }
722 1.1 riastrad
723 1.2 riastrad return dev;
724 1.2 riastrad
725 1.2 riastrad err_ctxbitmap:
726 1.2 riastrad drm_legacy_ctxbitmap_cleanup(dev);
727 1.2 riastrad drm_ht_remove(&dev->map_hash);
728 1.2 riastrad err_minors:
729 1.2 riastrad drm_minor_free(dev, DRM_MINOR_LEGACY);
730 1.2 riastrad drm_minor_free(dev, DRM_MINOR_RENDER);
731 1.2 riastrad drm_minor_free(dev, DRM_MINOR_CONTROL);
732 1.2 riastrad drm_fs_inode_free(dev->anon_inode);
733 1.2 riastrad err_free:
734 1.2 riastrad #ifdef __NetBSD__
735 1.9 riastrad linux_mutex_destroy(&dev->master_mutex);
736 1.9 riastrad linux_mutex_destroy(&dev->ctxlist_mutex);
737 1.2 riastrad linux_mutex_destroy(&dev->struct_mutex);
738 1.2 riastrad spin_lock_destroy(&dev->event_lock);
739 1.9 riastrad spin_lock_destroy(&dev->buf_lock);
740 1.2 riastrad #else
741 1.2 riastrad mutex_destroy(&dev->master_mutex);
742 1.9 riastrad mutex_destroy(&dev->ctxlist_mutex);
743 1.9 riastrad mutex_destroy(&dev->struct_mutex);
744 1.2 riastrad #endif
745 1.2 riastrad kfree(dev);
746 1.2 riastrad return NULL;
747 1.2 riastrad }
748 1.2 riastrad EXPORT_SYMBOL(drm_dev_alloc);
749 1.2 riastrad
750 1.2 riastrad static void drm_dev_release(struct kref *ref)
751 1.2 riastrad {
752 1.2 riastrad struct drm_device *dev = container_of(ref, struct drm_device, ref);
753 1.2 riastrad
754 1.2 riastrad if (drm_core_check_feature(dev, DRIVER_GEM))
755 1.2 riastrad drm_gem_destroy(dev);
756 1.2 riastrad
757 1.2 riastrad drm_legacy_ctxbitmap_cleanup(dev);
758 1.2 riastrad drm_ht_remove(&dev->map_hash);
759 1.2 riastrad drm_fs_inode_free(dev->anon_inode);
760 1.2 riastrad
761 1.2 riastrad drm_minor_free(dev, DRM_MINOR_LEGACY);
762 1.2 riastrad drm_minor_free(dev, DRM_MINOR_RENDER);
763 1.2 riastrad drm_minor_free(dev, DRM_MINOR_CONTROL);
764 1.2 riastrad
765 1.2 riastrad #ifdef __NetBSD__
766 1.9 riastrad linux_mutex_destroy(&dev->master_mutex);
767 1.9 riastrad linux_mutex_destroy(&dev->ctxlist_mutex);
768 1.2 riastrad linux_mutex_destroy(&dev->struct_mutex);
769 1.2 riastrad spin_lock_destroy(&dev->event_lock);
770 1.9 riastrad spin_lock_destroy(&dev->buf_lock);
771 1.2 riastrad #else
772 1.2 riastrad mutex_destroy(&dev->master_mutex);
773 1.9 riastrad mutex_destroy(&dev->ctxlist_mutex);
774 1.9 riastrad mutex_destroy(&dev->struct_mutex);
775 1.2 riastrad #endif
776 1.2 riastrad kfree(dev->unique);
777 1.2 riastrad kfree(dev);
778 1.2 riastrad }
779 1.2 riastrad
780 1.2 riastrad /**
781 1.2 riastrad * drm_dev_ref - Take reference of a DRM device
782 1.2 riastrad * @dev: device to take reference of or NULL
783 1.2 riastrad *
784 1.2 riastrad * This increases the ref-count of @dev by one. You *must* already own a
785 1.2 riastrad * reference when calling this. Use drm_dev_unref() to drop this reference
786 1.2 riastrad * again.
787 1.2 riastrad *
788 1.2 riastrad * This function never fails. However, this function does not provide *any*
789 1.2 riastrad * guarantee whether the device is alive or running. It only provides a
790 1.2 riastrad * reference to the object and the memory associated with it.
791 1.2 riastrad */
792 1.2 riastrad void drm_dev_ref(struct drm_device *dev)
793 1.2 riastrad {
794 1.2 riastrad if (dev)
795 1.2 riastrad kref_get(&dev->ref);
796 1.2 riastrad }
797 1.2 riastrad EXPORT_SYMBOL(drm_dev_ref);
798 1.2 riastrad
799 1.2 riastrad /**
800 1.2 riastrad * drm_dev_unref - Drop reference of a DRM device
801 1.2 riastrad * @dev: device to drop reference of or NULL
802 1.2 riastrad *
803 1.2 riastrad * This decreases the ref-count of @dev by one. The device is destroyed if the
804 1.2 riastrad * ref-count drops to zero.
805 1.2 riastrad */
806 1.2 riastrad void drm_dev_unref(struct drm_device *dev)
807 1.2 riastrad {
808 1.2 riastrad if (dev)
809 1.2 riastrad kref_put(&dev->ref, drm_dev_release);
810 1.2 riastrad }
811 1.2 riastrad EXPORT_SYMBOL(drm_dev_unref);
812 1.2 riastrad
813 1.2 riastrad /**
814 1.2 riastrad * drm_dev_register - Register DRM device
815 1.2 riastrad * @dev: Device to register
816 1.2 riastrad * @flags: Flags passed to the driver's .load() function
817 1.2 riastrad *
818 1.2 riastrad * Register the DRM device @dev with the system, advertise device to user-space
819 1.2 riastrad * and start normal device operation. @dev must be allocated via drm_dev_alloc()
820 1.2 riastrad * previously.
821 1.2 riastrad *
822 1.2 riastrad * Never call this twice on any device!
823 1.2 riastrad *
824 1.2 riastrad * NOTE: To ensure backward compatibility with existing drivers method this
825 1.2 riastrad * function calls the ->load() method after registering the device nodes,
826 1.2 riastrad * creating race conditions. Usage of the ->load() methods is therefore
827 1.2 riastrad * deprecated, drivers must perform all initialization before calling
828 1.2 riastrad * drm_dev_register().
829 1.2 riastrad *
830 1.2 riastrad * RETURNS:
831 1.2 riastrad * 0 on success, negative error code on failure.
832 1.2 riastrad */
833 1.2 riastrad int drm_dev_register(struct drm_device *dev, unsigned long flags)
834 1.2 riastrad {
835 1.2 riastrad int ret;
836 1.2 riastrad
837 1.2 riastrad #ifndef __NetBSD__
838 1.2 riastrad mutex_lock(&drm_global_mutex);
839 1.2 riastrad #endif
840 1.2 riastrad
841 1.2 riastrad ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
842 1.2 riastrad if (ret)
843 1.2 riastrad goto err_minors;
844 1.2 riastrad
845 1.2 riastrad ret = drm_minor_register(dev, DRM_MINOR_RENDER);
846 1.2 riastrad if (ret)
847 1.2 riastrad goto err_minors;
848 1.2 riastrad
849 1.2 riastrad ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
850 1.2 riastrad if (ret)
851 1.2 riastrad goto err_minors;
852 1.2 riastrad
853 1.2 riastrad if (dev->driver->load) {
854 1.2 riastrad ret = dev->driver->load(dev, flags);
855 1.2 riastrad if (ret)
856 1.2 riastrad goto err_minors;
857 1.2 riastrad }
858 1.2 riastrad
859 1.2 riastrad ret = 0;
860 1.2 riastrad goto out_unlock;
861 1.2 riastrad
862 1.2 riastrad err_minors:
863 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_LEGACY);
864 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_RENDER);
865 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_CONTROL);
866 1.2 riastrad out_unlock:
867 1.2 riastrad #ifndef __NetBSD__
868 1.2 riastrad mutex_unlock(&drm_global_mutex);
869 1.2 riastrad #endif
870 1.2 riastrad return ret;
871 1.2 riastrad }
872 1.2 riastrad EXPORT_SYMBOL(drm_dev_register);
873 1.2 riastrad
874 1.2 riastrad /**
875 1.2 riastrad * drm_dev_unregister - Unregister DRM device
876 1.2 riastrad * @dev: Device to unregister
877 1.2 riastrad *
878 1.2 riastrad * Unregister the DRM device from the system. This does the reverse of
879 1.2 riastrad * drm_dev_register() but does not deallocate the device. The caller must call
880 1.2 riastrad * drm_dev_unref() to drop their final reference.
881 1.2 riastrad *
882 1.2 riastrad * This should be called first in the device teardown code to make sure
883 1.2 riastrad * userspace can't access the device instance any more.
884 1.2 riastrad */
885 1.2 riastrad void drm_dev_unregister(struct drm_device *dev)
886 1.2 riastrad {
887 1.2 riastrad struct drm_map_list *r_list, *list_temp;
888 1.2 riastrad
889 1.2 riastrad drm_lastclose(dev);
890 1.2 riastrad
891 1.2 riastrad if (dev->driver->unload)
892 1.2 riastrad dev->driver->unload(dev);
893 1.2 riastrad
894 1.2 riastrad #ifndef __NetBSD__ /* Moved to drm_pci. */
895 1.2 riastrad if (dev->agp)
896 1.2 riastrad drm_pci_agp_destroy(dev);
897 1.2 riastrad #endif
898 1.2 riastrad
899 1.2 riastrad drm_vblank_cleanup(dev);
900 1.1 riastrad
901 1.2 riastrad list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
902 1.2 riastrad drm_legacy_rmmap(dev, r_list->map);
903 1.2 riastrad
904 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_LEGACY);
905 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_RENDER);
906 1.2 riastrad drm_minor_unregister(dev, DRM_MINOR_CONTROL);
907 1.2 riastrad }
908 1.2 riastrad EXPORT_SYMBOL(drm_dev_unregister);
909 1.2 riastrad
910 1.2 riastrad /**
911 1.2 riastrad * drm_dev_set_unique - Set the unique name of a DRM device
912 1.2 riastrad * @dev: device of which to set the unique name
913 1.2 riastrad * @fmt: format string for unique name
914 1.2 riastrad *
915 1.2 riastrad * Sets the unique name of a DRM device using the specified format string and
916 1.2 riastrad * a variable list of arguments. Drivers can use this at driver probe time if
917 1.2 riastrad * the unique name of the devices they drive is static.
918 1.2 riastrad *
919 1.2 riastrad * Return: 0 on success or a negative error code on failure.
920 1.2 riastrad */
921 1.2 riastrad int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...)
922 1.2 riastrad {
923 1.2 riastrad va_list ap;
924 1.2 riastrad
925 1.2 riastrad kfree(dev->unique);
926 1.2 riastrad
927 1.2 riastrad va_start(ap, fmt);
928 1.2 riastrad dev->unique = kvasprintf(GFP_KERNEL, fmt, ap);
929 1.2 riastrad va_end(ap);
930 1.2 riastrad
931 1.2 riastrad return dev->unique ? 0 : -ENOMEM;
932 1.2 riastrad }
933 1.2 riastrad EXPORT_SYMBOL(drm_dev_set_unique);
934 1.2 riastrad
935 1.5 riastrad #ifndef __NetBSD__
936 1.5 riastrad
937 1.2 riastrad /*
938 1.2 riastrad * DRM Core
939 1.2 riastrad * The DRM core module initializes all global DRM objects and makes them
940 1.2 riastrad * available to drivers. Once setup, drivers can probe their respective
941 1.2 riastrad * devices.
942 1.2 riastrad * Currently, core management includes:
943 1.2 riastrad * - The "DRM-Global" key/value database
944 1.2 riastrad * - Global ID management for connectors
945 1.2 riastrad * - DRM major number allocation
946 1.2 riastrad * - DRM minor management
947 1.2 riastrad * - DRM sysfs class
948 1.2 riastrad * - DRM debugfs root
949 1.2 riastrad *
950 1.2 riastrad * Furthermore, the DRM core provides dynamic char-dev lookups. For each
951 1.2 riastrad * interface registered on a DRM device, you can request minor numbers from DRM
952 1.2 riastrad * core. DRM core takes care of major-number management and char-dev
953 1.2 riastrad * registration. A stub ->open() callback forwards any open() requests to the
954 1.2 riastrad * registered minor.
955 1.2 riastrad */
956 1.2 riastrad
957 1.2 riastrad static int drm_stub_open(struct inode *inode, struct file *filp)
958 1.2 riastrad {
959 1.2 riastrad const struct file_operations *new_fops;
960 1.2 riastrad struct drm_minor *minor;
961 1.2 riastrad int err;
962 1.2 riastrad
963 1.2 riastrad DRM_DEBUG("\n");
964 1.2 riastrad
965 1.2 riastrad mutex_lock(&drm_global_mutex);
966 1.2 riastrad minor = drm_minor_acquire(iminor(inode));
967 1.2 riastrad if (IS_ERR(minor)) {
968 1.2 riastrad err = PTR_ERR(minor);
969 1.2 riastrad goto out_unlock;
970 1.2 riastrad }
971 1.2 riastrad
972 1.2 riastrad new_fops = fops_get(minor->dev->driver->fops);
973 1.2 riastrad if (!new_fops) {
974 1.2 riastrad err = -ENODEV;
975 1.2 riastrad goto out_release;
976 1.2 riastrad }
977 1.1 riastrad
978 1.2 riastrad replace_fops(filp, new_fops);
979 1.2 riastrad if (filp->f_op->open)
980 1.2 riastrad err = filp->f_op->open(inode, filp);
981 1.2 riastrad else
982 1.2 riastrad err = 0;
983 1.2 riastrad
984 1.2 riastrad out_release:
985 1.2 riastrad drm_minor_release(minor);
986 1.2 riastrad out_unlock:
987 1.2 riastrad mutex_unlock(&drm_global_mutex);
988 1.2 riastrad return err;
989 1.1 riastrad }
990 1.1 riastrad
991 1.1 riastrad static const struct file_operations drm_stub_fops = {
992 1.1 riastrad .owner = THIS_MODULE,
993 1.1 riastrad .open = drm_stub_open,
994 1.1 riastrad .llseek = noop_llseek,
995 1.1 riastrad };
996 1.1 riastrad
997 1.1 riastrad static int __init drm_core_init(void)
998 1.1 riastrad {
999 1.1 riastrad int ret = -ENOMEM;
1000 1.1 riastrad
1001 1.1 riastrad drm_global_init();
1002 1.2 riastrad drm_connector_ida_init();
1003 1.1 riastrad idr_init(&drm_minors_idr);
1004 1.1 riastrad
1005 1.1 riastrad if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
1006 1.1 riastrad goto err_p1;
1007 1.1 riastrad
1008 1.2 riastrad ret = drm_sysfs_init();
1009 1.2 riastrad if (ret < 0) {
1010 1.1 riastrad printk(KERN_ERR "DRM: Error creating drm class.\n");
1011 1.1 riastrad goto err_p2;
1012 1.1 riastrad }
1013 1.1 riastrad
1014 1.1 riastrad drm_debugfs_root = debugfs_create_dir("dri", NULL);
1015 1.1 riastrad if (!drm_debugfs_root) {
1016 1.1 riastrad DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
1017 1.1 riastrad ret = -1;
1018 1.1 riastrad goto err_p3;
1019 1.1 riastrad }
1020 1.1 riastrad
1021 1.1 riastrad DRM_INFO("Initialized %s %d.%d.%d %s\n",
1022 1.1 riastrad CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
1023 1.1 riastrad return 0;
1024 1.1 riastrad err_p3:
1025 1.1 riastrad drm_sysfs_destroy();
1026 1.1 riastrad err_p2:
1027 1.1 riastrad unregister_chrdev(DRM_MAJOR, "drm");
1028 1.1 riastrad
1029 1.1 riastrad idr_destroy(&drm_minors_idr);
1030 1.1 riastrad err_p1:
1031 1.1 riastrad return ret;
1032 1.1 riastrad }
1033 1.1 riastrad
1034 1.1 riastrad static void __exit drm_core_exit(void)
1035 1.1 riastrad {
1036 1.1 riastrad debugfs_remove(drm_debugfs_root);
1037 1.1 riastrad drm_sysfs_destroy();
1038 1.1 riastrad
1039 1.1 riastrad unregister_chrdev(DRM_MAJOR, "drm");
1040 1.1 riastrad
1041 1.2 riastrad drm_connector_ida_destroy();
1042 1.1 riastrad idr_destroy(&drm_minors_idr);
1043 1.1 riastrad }
1044 1.1 riastrad
1045 1.1 riastrad module_init(drm_core_init);
1046 1.1 riastrad module_exit(drm_core_exit);
1047 1.5 riastrad
1048 1.5 riastrad #endif
1049