2 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
4 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
7 * Author Rickard E. (Rik) Faith <faith@valinux.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
29 #include <linux/debugfs.h>
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/mount.h>
34 #include <linux/slab.h>
36 #include <drm/drm_drv.h>
39 #include "drm_crtc_internal.h"
40 #include "drm_legacy.h"
41 #include "drm_internal.h"
44 * drm_debug: Enable debug output.
45 * Bitmask of DRM_UT_x. See include/drm/drmP.h for details.
48 /* Provides three levels of debug: off, minimal, verbose */
49 #if DRM_DEBUG_DEFAULT_ON == 1
50 #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS | \
51 DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL)
52 #elif DRM_DEBUG_DEFAULT_ON == 2
53 #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS | \
54 DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL | \
55 DRM_UT_PID | DRM_UT_IOCTL )
57 #define DRM_DEBUGBITS_ON (0x0)
59 unsigned int drm_debug
= DRM_DEBUGBITS_ON
; /* defaults to 0 */
61 unsigned int drm_debug
= 0;
62 #endif /* __DragonFly__ */
63 EXPORT_SYMBOL(drm_debug
);
65 MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl");
66 MODULE_DESCRIPTION("DRM shared core routines");
67 MODULE_PARM_DESC(debug
, "Enable debug output, where each bit enables a debug category.\n"
68 "\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n"
69 "\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n"
70 "\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n"
71 "\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n"
72 "\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n"
73 "\t\tBit 5 (0x20) will enable VBL messages (vblank code)\n"
74 "\t\tBit 7 (0x80) will enable LEASE messages (leasing code)");
75 module_param_named(debug
, drm_debug
, int, 0600);
77 static DEFINE_MUTEX(drm_minor_lock
);
78 static struct idr drm_minors_idr
;
81 * If the drm core fails to init for whatever reason,
82 * we should prevent any drivers from registering with it.
83 * It's best to check this at drm_dev_init(), as some drivers
84 * prefer to embed struct drm_device into their own device
85 * structure and call drm_dev_init() themselves.
87 static bool drm_core_init_complete
= false;
90 static struct dentry
*drm_debugfs_root
;
93 void drm_err(const char *func
, const char *format
, ...)
97 kprintf("error: [" DRM_NAME
":pid%d:%s] *ERROR* ", DRM_CURRENTPID
, func
);
99 va_start(args
, format
);
100 kvprintf(format
, args
);
104 void drm_ut_debug_printk(const char *function_name
, const char *format
, ...)
108 if (unlikely(drm_debug
& DRM_UT_PID
)) {
109 kprintf("[" DRM_NAME
":pid%d:%s] ",
110 DRM_CURRENTPID
, function_name
);
112 kprintf("[" DRM_NAME
":%s] ", function_name
);
115 va_start(args
, format
);
116 kvprintf(format
, args
);
120 #define DRM_PRINTK_FMT "[" DRM_NAME ":%s]%s %pV"
121 #define DRM_PRINTK_FMT_DFLY "[" DRM_NAME ":%s]%s "
123 void drm_dev_printk(const struct device
*dev
, const char *level
,
124 unsigned int category
, const char *function_name
,
125 const char *prefix
, const char *format
, ...)
127 struct va_format vaf
;
130 if (category
!= DRM_UT_NONE
&& !(drm_debug
& category
))
133 va_start(args
, format
);
139 dev_printk(level
, dev
, DRM_PRINTK_FMT
, function_name
, prefix
,
142 printk("%s" DRM_PRINTK_FMT
, level
, function_name
, prefix
, &vaf
);
145 kprintf("drm_dev_printk: ");
146 dev_printk(level
, dev
, DRM_PRINTK_FMT_DFLY
, function_name
, prefix
);
147 kprintf(vaf
.fmt
, vaf
.va
);
149 kprintf("drm_dev_printk: ");
150 printk("%s" DRM_PRINTK_FMT_DFLY
, level
, function_name
, prefix
);
151 kprintf(vaf
.fmt
, vaf
.va
);
157 EXPORT_SYMBOL(drm_dev_printk
);
159 void drm_printk(const char *level
, unsigned int category
,
160 const char *format
, ...)
162 struct va_format vaf
;
165 if (category
!= DRM_UT_NONE
&& !(drm_debug
& category
))
168 va_start(args
, format
);
173 printk("%s" "[" DRM_NAME
":%ps]%s %pV",
174 level
, __builtin_return_address(0),
175 strcmp(level
, KERN_ERR
) == 0 ? " *ERROR*" : "", &vaf
);
177 printk("%s" "[" DRM_NAME
":%p]%s ",
178 level
, __builtin_return_address(0),
179 strcmp(level
, KERN_ERR
) == 0 ? " *ERROR*" : "");
180 kprintf(vaf
.fmt
, vaf
.va
);
185 EXPORT_SYMBOL(drm_printk
);
189 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
190 * of them is represented by a drm_minor object. Depending on the capabilities
191 * of the device-driver, different interfaces are registered.
193 * Minors can be accessed via dev->$minor_name. This pointer is either
194 * NULL or a valid drm_minor pointer and stays valid as long as the device is
195 * valid. This means, DRM minors have the same life-time as the underlying
196 * device. However, this doesn't mean that the minor is active. Minors are
197 * registered and unregistered dynamically according to device-state.
200 static struct drm_minor
**drm_minor_get_slot(struct drm_device
*dev
,
204 case DRM_MINOR_PRIMARY
:
205 return &dev
->primary
;
206 case DRM_MINOR_RENDER
:
208 case DRM_MINOR_CONTROL
:
209 return &dev
->control
;
215 static int drm_minor_alloc(struct drm_device
*dev
, unsigned int type
)
217 struct drm_minor
*minor
;
221 minor
= kzalloc(sizeof(*minor
), GFP_KERNEL
);
228 idr_preload(GFP_KERNEL
);
229 spin_lock_irqsave(&drm_minor_lock
, flags
);
230 r
= idr_alloc(&drm_minors_idr
,
235 spin_unlock_irqrestore(&drm_minor_lock
, flags
);
243 minor
->kdev
= drm_sysfs_minor_alloc(minor
);
244 if (IS_ERR(minor
->kdev
)) {
245 r
= PTR_ERR(minor
->kdev
);
249 *drm_minor_get_slot(dev
, type
) = minor
;
253 spin_lock_irqsave(&drm_minor_lock
, flags
);
254 idr_remove(&drm_minors_idr
, minor
->index
);
255 spin_unlock_irqrestore(&drm_minor_lock
, flags
);
261 static void drm_minor_free(struct drm_device
*dev
, unsigned int type
)
263 struct drm_minor
**slot
, *minor
;
266 slot
= drm_minor_get_slot(dev
, type
);
272 put_device(minor
->kdev
);
275 spin_lock_irqsave(&drm_minor_lock
, flags
);
276 idr_remove(&drm_minors_idr
, minor
->index
);
277 spin_unlock_irqrestore(&drm_minor_lock
, flags
);
283 static int drm_minor_register(struct drm_device
*dev
, unsigned int type
)
285 struct drm_minor
*minor
;
291 minor
= *drm_minor_get_slot(dev
, type
);
296 ret
= drm_debugfs_init(minor
, minor
->index
, drm_debugfs_root
);
298 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
302 ret
= device_add(minor
->kdev
);
307 /* replace NULL with @minor so lookups will succeed from now on */
308 spin_lock_irqsave(&drm_minor_lock
, flags
);
309 idr_replace(&drm_minors_idr
, minor
, minor
->index
);
310 spin_unlock_irqrestore(&drm_minor_lock
, flags
);
312 DRM_DEBUG("new minor registered %d\n", minor
->index
);
317 drm_debugfs_cleanup(minor
);
322 static void drm_minor_unregister(struct drm_device
*dev
, unsigned int type
)
324 struct drm_minor
*minor
;
327 minor
= *drm_minor_get_slot(dev
, type
);
329 if (!minor
|| !device_is_registered(minor
->kdev
))
335 /* replace @minor with NULL so lookups will fail from now on */
336 spin_lock_irqsave(&drm_minor_lock
, flags
);
337 idr_replace(&drm_minors_idr
, NULL
, minor
->index
);
338 spin_unlock_irqrestore(&drm_minor_lock
, flags
);
341 device_del(minor
->kdev
);
343 dev_set_drvdata(minor
->kdev
, NULL
); /* safety belt */
344 drm_debugfs_cleanup(minor
);
348 * Looks up the given minor-ID and returns the respective DRM-minor object. The
349 * refence-count of the underlying device is increased so you must release this
350 * object with drm_minor_release().
352 * As long as you hold this minor, it is guaranteed that the object and the
353 * minor->dev pointer will stay valid! However, the device may get unplugged and
354 * unregistered while you hold the minor.
356 struct drm_minor
*drm_minor_acquire(unsigned int minor_id
)
358 struct drm_minor
*minor
;
361 spin_lock_irqsave(&drm_minor_lock
, flags
);
362 minor
= idr_find(&drm_minors_idr
, minor_id
);
364 drm_dev_get(minor
->dev
);
365 spin_unlock_irqrestore(&drm_minor_lock
, flags
);
368 return ERR_PTR(-ENODEV
);
369 } else if (drm_dev_is_unplugged(minor
->dev
)) {
370 drm_dev_put(minor
->dev
);
371 return ERR_PTR(-ENODEV
);
377 void drm_minor_release(struct drm_minor
*minor
)
379 drm_dev_put(minor
->dev
);
383 * DOC: driver instance overview
385 * A device instance for a drm driver is represented by &struct drm_device. This
386 * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
387 * callbacks implemented by the driver. The driver then needs to initialize all
388 * the various subsystems for the drm device like memory management, vblank
389 * handling, modesetting support and intial output configuration plus obviously
390 * initialize all the corresponding hardware bits. An important part of this is
391 * also calling drm_dev_set_unique() to set the userspace-visible unique name of
392 * this device instance. Finally when everything is up and running and ready for
393 * userspace the device instance can be published using drm_dev_register().
395 * There is also deprecated support for initalizing device instances using
396 * bus-specific helpers and the &drm_driver.load callback. But due to
397 * backwards-compatibility needs the device instance have to be published too
398 * early, which requires unpretty global locking to make safe and is therefore
399 * only support for existing drivers not yet converted to the new scheme.
401 * When cleaning up a device instance everything needs to be done in reverse:
402 * First unpublish the device instance with drm_dev_unregister(). Then clean up
403 * any other resources allocated at device initialization and drop the driver's
404 * reference to &drm_device using drm_dev_put().
406 * Note that the lifetime rules for &drm_device instance has still a lot of
407 * historical baggage. Hence use the reference counting provided by
408 * drm_dev_get() and drm_dev_put() only carefully.
410 * It is recommended that drivers embed &struct drm_device into their own device
411 * structure, which is supported through drm_dev_init().
416 * drm_put_dev - Unregister and release a DRM device
419 * Called at module unload time or when a PCI device is unplugged.
421 * Cleans up all DRM device, calling drm_lastclose().
423 * Note: Use of this function is deprecated. It will eventually go away
424 * completely. Please use drm_dev_unregister() and drm_dev_put() explicitly
425 * instead to make sure that the device isn't userspace accessible any more
426 * while teardown is in progress, ensuring that userspace can't access an
427 * inconsistent state.
429 void drm_put_dev(struct drm_device
*dev
)
434 DRM_ERROR("cleanup called no dev\n");
438 drm_dev_unregister(dev
);
441 EXPORT_SYMBOL(drm_put_dev
);
443 static void drm_device_set_unplugged(struct drm_device
*dev
)
446 atomic_set(&dev
->unplugged
, 1);
450 * drm_dev_unplug - unplug a DRM device
453 * This unplugs a hotpluggable DRM device, which makes it inaccessible to
454 * userspace operations. Entry-points can use drm_dev_is_unplugged(). This
455 * essentially unregisters the device like drm_dev_unregister(), but can be
456 * called while there are still open users of @dev.
458 void drm_dev_unplug(struct drm_device
*dev
)
460 drm_dev_unregister(dev
);
462 mutex_lock(&drm_global_mutex
);
463 drm_device_set_unplugged(dev
);
464 if (dev
->open_count
== 0)
466 mutex_unlock(&drm_global_mutex
);
468 EXPORT_SYMBOL(drm_dev_unplug
);
472 * We want to be able to allocate our own "struct address_space" to control
473 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
474 * stand-alone address_space objects, so we need an underlying inode. As there
475 * is no way to allocate an independent inode easily, we need a fake internal
478 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
479 * frees it again. You are allowed to use iget() and iput() to get references to
480 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
481 * drm_fs_inode_free() call (which does not have to be the last iput()).
482 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
483 * between multiple inode-users. You could, technically, call
484 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
485 * iput(), but this way you'd end up with a new vfsmount for each inode.
488 static int drm_fs_cnt
;
489 static struct vfsmount
*drm_fs_mnt
;
491 static const struct dentry_operations drm_fs_dops
= {
492 .d_dname
= simple_dname
,
495 static const struct super_operations drm_fs_sops
= {
496 .statfs
= simple_statfs
,
499 static struct dentry
*drm_fs_mount(struct file_system_type
*fs_type
, int flags
,
500 const char *dev_name
, void *data
)
502 return mount_pseudo(fs_type
,
509 static struct file_system_type drm_fs_type
= {
511 .owner
= THIS_MODULE
,
512 .mount
= drm_fs_mount
,
513 .kill_sb
= kill_anon_super
,
516 static struct inode
*drm_fs_inode_new(void)
521 r
= simple_pin_fs(&drm_fs_type
, &drm_fs_mnt
, &drm_fs_cnt
);
523 DRM_ERROR("Cannot mount pseudo fs: %d\n", r
);
527 inode
= alloc_anon_inode(drm_fs_mnt
->mnt_sb
);
529 simple_release_fs(&drm_fs_mnt
, &drm_fs_cnt
);
534 static void drm_fs_inode_free(struct inode
*inode
)
538 simple_release_fs(&drm_fs_mnt
, &drm_fs_cnt
);
544 * drm_dev_init - Initialise new DRM device
546 * @driver: DRM driver
547 * @parent: Parent device object
549 * Initialize a new DRM device. No device registration is done.
550 * Call drm_dev_register() to advertice the device to user space and register it
551 * with other core subsystems. This should be done last in the device
552 * initialization sequence to make sure userspace can't access an inconsistent
555 * The initial ref-count of the object is 1. Use drm_dev_get() and
556 * drm_dev_put() to take and drop further ref-counts.
558 * Note that for purely virtual devices @parent can be NULL.
560 * Drivers that do not want to allocate their own device struct
561 * embedding &struct drm_device can call drm_dev_alloc() instead. For drivers
562 * that do embed &struct drm_device it must be placed first in the overall
563 * structure, and the overall structure must be allocated using kmalloc(): The
564 * drm core's release function unconditionally calls kfree() on the @dev pointer
565 * when the final reference is released. To override this behaviour, and so
566 * allow embedding of the drm_device inside the driver's device struct at an
567 * arbitrary offset, you must supply a &drm_driver.release callback and control
568 * the finalization explicitly.
571 * 0 on success, or error code on failure.
573 int drm_dev_init(struct drm_device
*dev
,
574 struct drm_driver
*driver
,
575 struct device
*parent
)
579 struct drm_softc
*softc
= device_get_softc(parent
->bsddev
);
581 softc
->drm_driver_data
= dev
;
584 if (!drm_core_init_complete
) {
585 DRM_ERROR("DRM core is not initialized\n");
589 kref_init(&dev
->ref
);
591 dev
->driver
= driver
;
593 INIT_LIST_HEAD(&dev
->filelist
);
594 INIT_LIST_HEAD(&dev
->ctxlist
);
595 INIT_LIST_HEAD(&dev
->vmalist
);
596 INIT_LIST_HEAD(&dev
->maplist
);
597 INIT_LIST_HEAD(&dev
->vblank_event_list
);
599 lockinit(&dev
->buf_lock
, "drmdbl", 0, 0);
600 lockinit(&dev
->event_lock
, "drmev", 0, 0);
601 lockinit(&dev
->struct_mutex
, "drmslk", 0, LK_CANRECURSE
);
602 lockinit(&dev
->filelist_mutex
, "drmflm", 0, LK_CANRECURSE
);
603 lockinit(&dev
->ctxlist_mutex
, "drmclm", 0, LK_CANRECURSE
);
604 lockinit(&dev
->master_mutex
, "drmmm", 0, LK_CANRECURSE
);
606 #ifndef __DragonFly__
607 dev
->anon_inode
= drm_fs_inode_new();
608 if (IS_ERR(dev
->anon_inode
)) {
609 ret
= PTR_ERR(dev
->anon_inode
);
610 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret
);
614 dev
->anon_inode
= NULL
;
615 dev
->pci_domain
= pci_get_domain(dev
->dev
->bsddev
);
616 dev
->pci_bus
= pci_get_bus(dev
->dev
->bsddev
);
617 dev
->pci_slot
= pci_get_slot(dev
->dev
->bsddev
);
618 dev
->pci_func
= pci_get_function(dev
->dev
->bsddev
);
619 drm_sysctl_init(dev
);
622 if (drm_core_check_feature(dev
, DRIVER_RENDER
)) {
623 ret
= drm_minor_alloc(dev
, DRM_MINOR_RENDER
);
628 ret
= drm_minor_alloc(dev
, DRM_MINOR_PRIMARY
);
632 ret
= drm_ht_create(&dev
->map_hash
, 12);
636 drm_legacy_ctxbitmap_init(dev
);
638 if (drm_core_check_feature(dev
, DRIVER_GEM
)) {
639 ret
= drm_gem_init(dev
);
641 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
646 /* Use the parent device name as DRM device unique identifier, but fall
647 * back to the driver name for virtual devices like vgem. */
649 ret
= drm_dev_set_unique(dev
, parent
? dev_name(parent
) : driver
->name
);
658 if (drm_core_check_feature(dev
, DRIVER_GEM
))
659 drm_gem_destroy(dev
);
662 drm_legacy_ctxbitmap_cleanup(dev
);
663 drm_ht_remove(&dev
->map_hash
);
665 drm_minor_free(dev
, DRM_MINOR_PRIMARY
);
666 drm_minor_free(dev
, DRM_MINOR_RENDER
);
667 drm_minor_free(dev
, DRM_MINOR_CONTROL
);
668 #ifndef __DragonFly__
669 drm_fs_inode_free(dev
->anon_inode
);
672 mutex_destroy(&dev
->master_mutex
);
673 mutex_destroy(&dev
->ctxlist_mutex
);
674 mutex_destroy(&dev
->filelist_mutex
);
675 mutex_destroy(&dev
->struct_mutex
);
677 drm_sysctl_cleanup(dev
);
681 EXPORT_SYMBOL(drm_dev_init
);
684 * drm_dev_fini - Finalize a dead DRM device
687 * Finalize a dead DRM device. This is the converse to drm_dev_init() and
688 * frees up all data allocated by it. All driver private data should be
689 * finalized first. Note that this function does not free the @dev, that is
690 * left to the caller.
692 * The ref-count of @dev must be zero, and drm_dev_fini() should only be called
693 * from a &drm_driver.release callback.
695 void drm_dev_fini(struct drm_device
*dev
)
697 drm_vblank_cleanup(dev
);
699 if (drm_core_check_feature(dev
, DRIVER_GEM
))
700 drm_gem_destroy(dev
);
702 drm_legacy_ctxbitmap_cleanup(dev
);
703 drm_ht_remove(&dev
->map_hash
);
705 drm_fs_inode_free(dev
->anon_inode
);
708 drm_minor_free(dev
, DRM_MINOR_PRIMARY
);
709 drm_minor_free(dev
, DRM_MINOR_RENDER
);
710 drm_minor_free(dev
, DRM_MINOR_CONTROL
);
712 mutex_destroy(&dev
->master_mutex
);
713 mutex_destroy(&dev
->ctxlist_mutex
);
714 mutex_destroy(&dev
->filelist_mutex
);
715 mutex_destroy(&dev
->struct_mutex
);
718 EXPORT_SYMBOL(drm_dev_fini
);
721 * drm_dev_alloc - Allocate new DRM device
722 * @driver: DRM driver to allocate device for
723 * @parent: Parent device object
725 * Allocate and initialize a new DRM device. No device registration is done.
726 * Call drm_dev_register() to advertice the device to user space and register it
727 * with other core subsystems. This should be done last in the device
728 * initialization sequence to make sure userspace can't access an inconsistent
731 * The initial ref-count of the object is 1. Use drm_dev_get() and
732 * drm_dev_put() to take and drop further ref-counts.
734 * Note that for purely virtual devices @parent can be NULL.
736 * Drivers that wish to subclass or embed &struct drm_device into their
737 * own struct should look at using drm_dev_init() instead.
740 * Pointer to new DRM device, or ERR_PTR on failure.
742 struct drm_device
*drm_dev_alloc(struct drm_driver
*driver
,
743 struct device
*parent
)
745 struct drm_device
*dev
;
748 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
750 return ERR_PTR(-ENOMEM
);
752 ret
= drm_dev_init(dev
, driver
, parent
);
760 EXPORT_SYMBOL(drm_dev_alloc
);
762 static void drm_dev_release(struct kref
*ref
)
764 struct drm_device
*dev
= container_of(ref
, struct drm_device
, ref
);
766 if (dev
->driver
->release
) {
767 dev
->driver
->release(dev
);
775 * drm_dev_get - Take reference of a DRM device
776 * @dev: device to take reference of or NULL
778 * This increases the ref-count of @dev by one. You *must* already own a
779 * reference when calling this. Use drm_dev_put() to drop this reference
782 * This function never fails. However, this function does not provide *any*
783 * guarantee whether the device is alive or running. It only provides a
784 * reference to the object and the memory associated with it.
786 void drm_dev_get(struct drm_device
*dev
)
791 EXPORT_SYMBOL(drm_dev_get
);
794 * drm_dev_put - Drop reference of a DRM device
795 * @dev: device to drop reference of or NULL
797 * This decreases the ref-count of @dev by one. The device is destroyed if the
798 * ref-count drops to zero.
800 void drm_dev_put(struct drm_device
*dev
)
803 kref_put(&dev
->ref
, drm_dev_release
);
805 EXPORT_SYMBOL(drm_dev_put
);
808 * drm_dev_unref - Drop reference of a DRM device
809 * @dev: device to drop reference of or NULL
811 * This is a compatibility alias for drm_dev_put() and should not be used by new
814 void drm_dev_unref(struct drm_device
*dev
)
818 EXPORT_SYMBOL(drm_dev_unref
);
820 static int create_compat_control_link(struct drm_device
*dev
)
822 struct drm_minor
*minor
;
826 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
829 minor
= *drm_minor_get_slot(dev
, DRM_MINOR_PRIMARY
);
834 * Some existing userspace out there uses the existing of the controlD*
835 * sysfs files to figure out whether it's a modeset driver. It only does
836 * readdir, hence a symlink is sufficient (and the least confusing
837 * option). Otherwise controlD* is entirely unused.
839 * Old controlD chardev have been allocated in the range
842 name
= kasprintf(GFP_KERNEL
, "controlD%d", minor
->index
+ 64);
846 #ifndef __DragonFly__ /* DragonFly's libdrm does not need this */
847 ret
= sysfs_create_link(minor
->kdev
->kobj
.parent
,
859 static void remove_compat_control_link(struct drm_device
*dev
)
861 struct drm_minor
*minor
;
864 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
867 minor
= *drm_minor_get_slot(dev
, DRM_MINOR_PRIMARY
);
871 name
= kasprintf(GFP_KERNEL
, "controlD%d", minor
->index
);
875 #ifndef __DragonFly__
876 sysfs_remove_link(minor
->kdev
->kobj
.parent
, name
);
883 * drm_dev_register - Register DRM device
884 * @dev: Device to register
885 * @flags: Flags passed to the driver's .load() function
887 * Register the DRM device @dev with the system, advertise device to user-space
888 * and start normal device operation. @dev must be allocated via drm_dev_alloc()
891 * Never call this twice on any device!
893 * NOTE: To ensure backward compatibility with existing drivers method this
894 * function calls the &drm_driver.load method after registering the device
895 * nodes, creating race conditions. Usage of the &drm_driver.load methods is
896 * therefore deprecated, drivers must perform all initialization before calling
897 * drm_dev_register().
900 * 0 on success, negative error code on failure.
902 int drm_dev_register(struct drm_device
*dev
, unsigned long flags
)
904 struct drm_driver
*driver
= dev
->driver
;
907 mutex_lock(&drm_global_mutex
);
909 ret
= drm_minor_register(dev
, DRM_MINOR_CONTROL
);
913 ret
= drm_minor_register(dev
, DRM_MINOR_RENDER
);
917 ret
= drm_minor_register(dev
, DRM_MINOR_PRIMARY
);
921 ret
= create_compat_control_link(dev
);
925 dev
->registered
= true;
927 if (dev
->driver
->load
) {
928 ret
= dev
->driver
->load(dev
, flags
);
933 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
934 drm_modeset_register_all(dev
);
938 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
939 driver
->name
, driver
->major
, driver
->minor
,
940 driver
->patchlevel
, driver
->date
,
941 dev
->dev
? dev_name(dev
->dev
) : "virtual device",
942 dev
->primary
->index
);
947 remove_compat_control_link(dev
);
948 drm_minor_unregister(dev
, DRM_MINOR_PRIMARY
);
949 drm_minor_unregister(dev
, DRM_MINOR_RENDER
);
950 drm_minor_unregister(dev
, DRM_MINOR_CONTROL
);
952 mutex_unlock(&drm_global_mutex
);
955 EXPORT_SYMBOL(drm_dev_register
);
958 * drm_dev_unregister - Unregister DRM device
959 * @dev: Device to unregister
961 * Unregister the DRM device from the system. This does the reverse of
962 * drm_dev_register() but does not deallocate the device. The caller must call
963 * drm_dev_put() to drop their final reference.
965 * A special form of unregistering for hotpluggable devices is drm_dev_unplug(),
966 * which can be called while there are still open users of @dev.
968 * This should be called first in the device teardown code to make sure
969 * userspace can't access the device instance any more.
971 void drm_dev_unregister(struct drm_device
*dev
)
973 struct drm_map_list
*r_list
, *list_temp
;
975 if (drm_core_check_feature(dev
, DRIVER_LEGACY
))
978 dev
->registered
= false;
980 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
981 drm_modeset_unregister_all(dev
);
983 if (dev
->driver
->unload
)
984 dev
->driver
->unload(dev
);
988 drm_pci_agp_destroy(dev
);
991 list_for_each_entry_safe(r_list
, list_temp
, &dev
->maplist
, head
)
992 drm_legacy_rmmap(dev
, r_list
->map
);
994 remove_compat_control_link(dev
);
995 drm_minor_unregister(dev
, DRM_MINOR_PRIMARY
);
996 drm_minor_unregister(dev
, DRM_MINOR_RENDER
);
997 drm_minor_unregister(dev
, DRM_MINOR_CONTROL
);
999 EXPORT_SYMBOL(drm_dev_unregister
);
1003 * drm_dev_set_unique - Set the unique name of a DRM device
1004 * @dev: device of which to set the unique name
1005 * @name: unique name
1007 * Sets the unique name of a DRM device using the specified string. Drivers
1008 * can use this at driver probe time if the unique name of the devices they
1011 * Return: 0 on success or a negative error code on failure.
1013 int drm_dev_set_unique(struct drm_device
*dev
, const char *name
)
1016 dev
->unique
= kstrdup(name
, GFP_KERNEL
);
1018 return dev
->unique
? 0 : -ENOMEM
;
1020 EXPORT_SYMBOL(drm_dev_set_unique
);
1024 * The DRM core module initializes all global DRM objects and makes them
1025 * available to drivers. Once setup, drivers can probe their respective
1027 * Currently, core management includes:
1028 * - The "DRM-Global" key/value database
1029 * - Global ID management for connectors
1030 * - DRM major number allocation
1031 * - DRM minor management
1033 * - DRM debugfs root
1035 * Furthermore, the DRM core provides dynamic char-dev lookups. For each
1036 * interface registered on a DRM device, you can request minor numbers from DRM
1037 * core. DRM core takes care of major-number management and char-dev
1038 * registration. A stub ->open() callback forwards any open() requests to the
1042 static int drm_stub_open(struct inode
*inode
, struct file
*filp
)
1044 const struct file_operations
*new_fops
;
1045 struct drm_minor
*minor
;
1050 mutex_lock(&drm_global_mutex
);
1051 minor
= drm_minor_acquire(iminor(inode
));
1052 if (IS_ERR(minor
)) {
1053 err
= PTR_ERR(minor
);
1057 new_fops
= fops_get(minor
->dev
->driver
->fops
);
1063 replace_fops(filp
, new_fops
);
1064 if (filp
->f_op
->open
)
1065 err
= filp
->f_op
->open(inode
, filp
);
1070 drm_minor_release(minor
);
1072 mutex_unlock(&drm_global_mutex
);
1076 static const struct file_operations drm_stub_fops
= {
1077 .owner
= THIS_MODULE
,
1078 .open
= drm_stub_open
,
1079 .llseek
= noop_llseek
,
1083 static void drm_core_exit(void)
1086 unregister_chrdev(DRM_MAJOR
, "drm");
1087 debugfs_remove(drm_debugfs_root
);
1088 drm_sysfs_destroy();
1090 idr_destroy(&drm_minors_idr
);
1091 drm_connector_ida_destroy();
1092 drm_global_release();
1095 static int __init
drm_core_init(void)
1102 drm_connector_ida_init();
1103 idr_init(&drm_minors_idr
);
1106 ret
= drm_sysfs_init();
1108 DRM_ERROR("Cannot create DRM class: %d\n", ret
);
1112 drm_debugfs_root
= debugfs_create_dir("dri", NULL
);
1113 if (!drm_debugfs_root
) {
1115 DRM_ERROR("Cannot create debugfs-root: %d\n", ret
);
1119 ret
= register_chrdev(DRM_MAJOR
, "drm", &drm_stub_fops
);
1124 drm_core_init_complete
= true;
1126 DRM_DEBUG("Initialized\n");
1136 module_init(drm_core_init
);
1137 module_exit(drm_core_exit
);
1139 #include <sys/devfs.h>
1141 #include <linux/export.h>
1142 #include <linux/dmi.h>
1143 #include <drm/drmP.h>
1146 drm_modevent(module_t mod
, int type
, void *data
)
1151 TUNABLE_INT_FETCH("drm.debug", &drm_debug
);
1152 linux_task_drop_callback
= linux_task_drop
;
1153 linux_proc_drop_callback
= linux_proc_drop
;
1156 linux_task_drop_callback
= NULL
;
1157 linux_proc_drop_callback
= NULL
;
1163 static moduledata_t drm_mod
= {
1168 DECLARE_MODULE(drm
, drm_mod
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
);
1169 MODULE_VERSION(drm
, 1);
1170 MODULE_DEPEND(drm
, agp
, 1, 1, 1);
1171 MODULE_DEPEND(drm
, pci
, 1, 1, 1);
1172 MODULE_DEPEND(drm
, iicbus
, 1, 1, 1);
1174 struct dev_ops drm_cdevsw
= {
1175 { "drm", 0, D_TRACKCLOSE
| D_MPSAFE
},
1177 .d_close
= drm_close
,
1179 .d_ioctl
= drm_ioctl
,
1180 .d_kqfilter
= drm_kqfilter
,
1182 .d_mmap_single
= drm_mmap_single
,
1185 SYSCTL_NODE(_hw
, OID_AUTO
, drm
, CTLFLAG_RW
, NULL
, "DRM device");
1186 SYSCTL_INT(_hw_drm
, OID_AUTO
, debug
, CTLFLAG_RW
, &drm_debug
, 0,
1189 SYSCTL_INT(_hw_drm
, OID_AUTO
, vma_debug
, CTLFLAG_RW
, &drm_vma_debug
, 0,
1194 drm_create_cdevs(device_t kdev
)
1196 struct drm_device
*dev
;
1198 #ifdef __DragonFly__
1199 struct drm_softc
*softc
= device_get_softc(kdev
);
1201 dev
= softc
->drm_driver_data
;
1203 unit
= device_get_unit(kdev
);
1205 dev
->devnode
= make_dev(&drm_cdevsw
, unit
, DRM_DEV_UID
, DRM_DEV_GID
,
1206 DRM_DEV_MODE
, "dri/card%d", unit
);
1209 dev
->devnode
->si_drv1
= dev
;
1214 #ifndef DRM_DEV_NAME
1215 #define DRM_DEV_NAME "drm"
1218 devclass_t drm_devclass
;
1220 /* XXX: this is supposed to be drm_release() */
1221 void drm_cdevpriv_dtor(void *cd
)
1223 struct drm_file
*file_priv
= cd
;
1224 struct drm_device
*dev
= file_priv
->dev
;
1226 DRM_DEBUG("open_count = %d\n", dev
->open_count
);
1230 if (dev
->driver
->preclose
!= NULL
)
1231 dev
->driver
->preclose(dev
, file_priv
);
1233 /* ========================================================
1234 * Begin inline drm_release
1237 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
1238 DRM_CURRENTPID
, (long)dev
->dev
, dev
->open_count
);
1240 if (dev
->driver
->driver_features
& DRIVER_GEM
)
1241 drm_gem_release(dev
, file_priv
);
1243 if (drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1244 drm_legacy_reclaim_buffers(dev
, file_priv
);
1246 funsetown(&dev
->buf_sigio
);
1248 if (dev
->driver
->postclose
!= NULL
)
1249 dev
->driver
->postclose(dev
, file_priv
);
1250 list_del(&file_priv
->lhead
);
1253 /* ========================================================
1254 * End inline drm_release
1257 device_unbusy(dev
->dev
->bsddev
);
1258 if (--dev
->open_count
== 0) {
1266 drm_add_busid_modesetting(struct drm_device
*dev
, struct sysctl_ctx_list
*ctx
,
1267 struct sysctl_oid
*top
)
1269 struct sysctl_oid
*oid
;
1271 ksnprintf(dev
->busid_str
, sizeof(dev
->busid_str
),
1272 "pci:%04x:%02x:%02x.%d", dev
->pci_domain
, dev
->pci_bus
,
1273 dev
->pci_slot
, dev
->pci_func
);
1274 oid
= SYSCTL_ADD_STRING(ctx
, SYSCTL_CHILDREN(top
), OID_AUTO
, "busid",
1275 CTLFLAG_RD
, dev
->busid_str
, 0, NULL
);
1283 drm_mmap_single(struct dev_mmap_single_args
*ap
)
1285 struct drm_device
*dev
;
1286 struct cdev
*kdev
= ap
->a_head
.a_dev
;
1287 vm_ooffset_t
*offset
= ap
->a_offset
;
1288 vm_size_t size
= ap
->a_size
;
1289 struct vm_object
**obj_res
= ap
->a_object
;
1290 int nprot
= ap
->a_nprot
;
1292 dev
= drm_get_device_from_kdev(kdev
);
1293 if (dev
->drm_ttm_bdev
!= NULL
) {
1294 return (ttm_bo_mmap_single(ap
->a_fp
, dev
,
1295 offset
, size
, obj_res
, nprot
));
1296 } else if ((dev
->driver
->driver_features
& DRIVER_GEM
) != 0) {
1297 return (drm_gem_mmap_single(dev
, offset
, size
, obj_res
, nprot
));
1303 #include <linux/dmi.h>
1306 * Check if dmi_system_id structure matches system DMI data
1309 dmi_found(const struct dmi_system_id
*dsi
)
1313 char *sys_vendor
, *board_vendor
, *product_name
, *board_name
;
1315 sys_vendor
= kgetenv("smbios.system.maker");
1316 board_vendor
= kgetenv("smbios.planar.maker");
1317 product_name
= kgetenv("smbios.system.product");
1318 board_name
= kgetenv("smbios.planar.product");
1320 for (i
= 0; i
< NELEM(dsi
->matches
); i
++) {
1321 slot
= dsi
->matches
[i
].slot
;
1325 case DMI_SYS_VENDOR
:
1326 if (sys_vendor
!= NULL
&&
1327 !strcmp(sys_vendor
, dsi
->matches
[i
].substr
))
1331 case DMI_BOARD_VENDOR
:
1332 if (board_vendor
!= NULL
&&
1333 !strcmp(board_vendor
, dsi
->matches
[i
].substr
))
1337 case DMI_PRODUCT_NAME
:
1338 if (product_name
!= NULL
&&
1339 !strcmp(product_name
, dsi
->matches
[i
].substr
))
1343 case DMI_BOARD_NAME
:
1344 if (board_name
!= NULL
&&
1345 !strcmp(board_name
, dsi
->matches
[i
].substr
))
1356 if (sys_vendor
!= NULL
)
1357 kfreeenv(sys_vendor
);
1358 if (board_vendor
!= NULL
)
1359 kfreeenv(board_vendor
);
1360 if (product_name
!= NULL
)
1361 kfreeenv(product_name
);
1362 if (board_name
!= NULL
)
1363 kfreeenv(board_name
);
1368 int dmi_check_system(const struct dmi_system_id
*sysid
)
1370 const struct dmi_system_id
*dsi
;
1373 for (dsi
= sysid
; dsi
->matches
[0].slot
!= 0 ; dsi
++) {
1374 if (dmi_found(dsi
)) {
1376 if (dsi
->callback
&& dsi
->callback(dsi
))