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