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