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