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/module.h>
30 #include <linux/moduleparam.h>
32 #include "drm_crtc_internal.h"
33 #include "drm_legacy.h"
34 #include "drm_internal.h"
37 * drm_debug: Enable debug output.
38 * Bitmask of DRM_UT_x. See include/drm/drmP.h for details.
41 /* Provides three levels of debug: off, minimal, verbose */
42 #if DRM_DEBUG_DEFAULT_ON == 1
43 #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS | \
44 DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL)
45 #elif DRM_DEBUG_DEFAULT_ON == 2
46 #define DRM_DEBUGBITS_ON (DRM_UT_CORE | DRM_UT_DRIVER | DRM_UT_KMS | \
47 DRM_UT_PRIME| DRM_UT_ATOMIC | DRM_UT_FIOCTL | \
48 DRM_UT_PID | DRM_UT_IOCTL | DRM_UT_VBLANK)
50 #define DRM_DEBUGBITS_ON (0x0)
52 unsigned int drm_debug = DRM_DEBUGBITS_ON; /* defaults to 0 */
54 unsigned int drm_debug = 0;
55 #endif /* __DragonFly__ */
56 EXPORT_SYMBOL(drm_debug);
58 MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl");
59 MODULE_DESCRIPTION("DRM shared core routines");
60 MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n"
61 "\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n"
62 "\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n"
63 "\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n"
64 "\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n"
65 "\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n"
66 "\t\tBit 5 (0x20) will enable VBL messages (vblank code)");
67 module_param_named(debug, drm_debug, int, 0600);
69 static DEFINE_MUTEX(drm_minor_lock);
70 static struct idr drm_minors_idr;
73 static struct dentry *drm_debugfs_root;
76 void drm_ut_debug_printk(const char *function_name, const char *format, ...)
80 if (unlikely(drm_debug & DRM_UT_PID)) {
81 kprintf("[" DRM_NAME ":pid%d:%s] ",
82 DRM_CURRENTPID, function_name);
84 kprintf("[" DRM_NAME ":%s] ", function_name);
87 va_start(args, format);
88 kvprintf(format, args);
92 #define DRM_PRINTK_FMT "[" DRM_NAME ":%s]%s %pV"
94 void drm_dev_printk(const struct device *dev, const char *level,
95 unsigned int category, const char *function_name,
96 const char *prefix, const char *format, ...)
101 if (category != DRM_UT_NONE && !(drm_debug & category))
104 va_start(args, format);
109 dev_printk(level, dev, DRM_PRINTK_FMT, function_name, prefix,
112 printk("%s" DRM_PRINTK_FMT, level, function_name, prefix, &vaf);
116 EXPORT_SYMBOL(drm_dev_printk);
118 void drm_printk(const char *level, unsigned int category,
119 const char *format, ...)
121 struct va_format vaf;
124 if (category != DRM_UT_NONE && !(drm_debug & category))
127 va_start(args, format);
131 printk("%s" "[" DRM_NAME ":%ps]%s %pV",
132 level, __builtin_return_address(0),
133 strcmp(level, KERN_ERR) == 0 ? " *ERROR*" : "", &vaf);
137 EXPORT_SYMBOL(drm_printk);
141 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
142 * of them is represented by a drm_minor object. Depending on the capabilities
143 * of the device-driver, different interfaces are registered.
145 * Minors can be accessed via dev->$minor_name. This pointer is either
146 * NULL or a valid drm_minor pointer and stays valid as long as the device is
147 * valid. This means, DRM minors have the same life-time as the underlying
148 * device. However, this doesn't mean that the minor is active. Minors are
149 * registered and unregistered dynamically according to device-state.
152 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
156 case DRM_MINOR_PRIMARY:
157 return &dev->primary;
158 case DRM_MINOR_RENDER:
160 case DRM_MINOR_CONTROL:
161 return &dev->control;
167 static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
169 struct drm_minor *minor;
173 minor = kzalloc(sizeof(*minor), GFP_KERNEL);
180 idr_preload(GFP_KERNEL);
181 spin_lock_irqsave(&drm_minor_lock, flags);
182 r = idr_alloc(&drm_minors_idr,
187 spin_unlock_irqrestore(&drm_minor_lock, flags);
196 minor->kdev = drm_sysfs_minor_alloc(minor);
197 if (IS_ERR(minor->kdev)) {
198 r = PTR_ERR(minor->kdev);
203 *drm_minor_get_slot(dev, type) = minor;
208 spin_lock_irqsave(&drm_minor_lock, flags);
209 idr_remove(&drm_minors_idr, minor->index);
210 spin_unlock_irqrestore(&drm_minor_lock, flags);
217 static void drm_minor_free(struct drm_device *dev, unsigned int type)
219 struct drm_minor **slot, *minor;
222 slot = drm_minor_get_slot(dev, type);
228 put_device(minor->kdev);
231 spin_lock_irqsave(&drm_minor_lock, flags);
232 idr_remove(&drm_minors_idr, minor->index);
233 spin_unlock_irqrestore(&drm_minor_lock, flags);
239 static int drm_minor_register(struct drm_device *dev, unsigned int type)
241 struct drm_minor *minor;
249 minor = *drm_minor_get_slot(dev, type);
254 ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
256 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
260 ret = device_add(minor->kdev);
265 /* replace NULL with @minor so lookups will succeed from now on */
266 spin_lock_irqsave(&drm_minor_lock, flags);
267 idr_replace(&drm_minors_idr, minor, minor->index);
268 spin_unlock_irqrestore(&drm_minor_lock, flags);
270 DRM_DEBUG("new minor registered %d\n", minor->index);
275 drm_debugfs_cleanup(minor);
280 static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
282 struct drm_minor *minor;
285 minor = *drm_minor_get_slot(dev, type);
287 if (!minor || !device_is_registered(minor->kdev))
293 /* replace @minor with NULL so lookups will fail from now on */
294 spin_lock_irqsave(&drm_minor_lock, flags);
295 idr_replace(&drm_minors_idr, NULL, minor->index);
296 spin_unlock_irqrestore(&drm_minor_lock, flags);
299 device_del(minor->kdev);
300 dev_set_drvdata(minor->kdev, NULL); /* safety belt */
302 drm_debugfs_cleanup(minor);
306 * drm_minor_acquire - Acquire a DRM minor
307 * @minor_id: Minor ID of the DRM-minor
309 * Looks up the given minor-ID and returns the respective DRM-minor object. The
310 * refence-count of the underlying device is increased so you must release this
311 * object with drm_minor_release().
313 * As long as you hold this minor, it is guaranteed that the object and the
314 * minor->dev pointer will stay valid! However, the device may get unplugged and
315 * unregistered while you hold the minor.
318 * Pointer to minor-object with increased device-refcount, or PTR_ERR on
321 struct drm_minor *drm_minor_acquire(unsigned int minor_id)
323 struct drm_minor *minor;
326 spin_lock_irqsave(&drm_minor_lock, flags);
327 minor = idr_find(&drm_minors_idr, minor_id);
329 drm_dev_ref(minor->dev);
330 spin_unlock_irqrestore(&drm_minor_lock, flags);
333 return ERR_PTR(-ENODEV);
334 } else if (drm_device_is_unplugged(minor->dev)) {
335 drm_dev_unref(minor->dev);
336 return ERR_PTR(-ENODEV);
343 * drm_minor_release - Release DRM minor
344 * @minor: Pointer to DRM minor object
346 * Release a minor that was previously acquired via drm_minor_acquire().
348 void drm_minor_release(struct drm_minor *minor)
350 drm_dev_unref(minor->dev);
355 * DOC: driver instance overview
357 * A device instance for a drm driver is represented by struct &drm_device. This
358 * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
359 * callbacks implemented by the driver. The driver then needs to initialize all
360 * the various subsystems for the drm device like memory management, vblank
361 * handling, modesetting support and intial output configuration plus obviously
362 * initialize all the corresponding hardware bits. Finally when everything is up
363 * and running and ready for userspace the device instance can be published
364 * using drm_dev_register().
366 * There is also deprecated support for initalizing device instances using
367 * bus-specific helpers and the ->load() callback. But due to
368 * backwards-compatibility needs the device instance have to be published too
369 * early, which requires unpretty global locking to make safe and is therefore
370 * only support for existing drivers not yet converted to the new scheme.
372 * When cleaning up a device instance everything needs to be done in reverse:
373 * First unpublish the device instance with drm_dev_unregister(). Then clean up
374 * any other resources allocated at device initialization and drop the driver's
375 * reference to &drm_device using drm_dev_unref().
377 * Note that the lifetime rules for &drm_device instance has still a lot of
378 * historical baggage. Hence use the reference counting provided by
379 * drm_dev_ref() and drm_dev_unref() only carefully.
381 * Also note that embedding of &drm_device is currently not (yet) supported (but
382 * it would be easy to add). Drivers can store driver-private data in the
383 * dev_priv field of &drm_device.
386 static int drm_dev_set_unique(struct drm_device *dev, const char *name)
392 dev->unique = kstrdup(name, GFP_KERNEL);
394 return dev->unique ? 0 : -ENOMEM;
398 * drm_put_dev - Unregister and release a DRM device
401 * Called at module unload time or when a PCI device is unplugged.
403 * Cleans up all DRM device, calling drm_lastclose().
405 * Note: Use of this function is deprecated. It will eventually go away
406 * completely. Please use drm_dev_unregister() and drm_dev_unref() explicitly
407 * instead to make sure that the device isn't userspace accessible any more
408 * while teardown is in progress, ensuring that userspace can't access an
409 * inconsistent state.
411 void drm_put_dev(struct drm_device *dev)
416 DRM_ERROR("cleanup called no dev\n");
420 drm_dev_unregister(dev);
423 EXPORT_SYMBOL(drm_put_dev);
425 void drm_unplug_dev(struct drm_device *dev)
427 /* for a USB device */
428 drm_dev_unregister(dev);
430 mutex_lock(&drm_global_mutex);
432 drm_device_set_unplugged(dev);
434 if (dev->open_count == 0) {
437 mutex_unlock(&drm_global_mutex);
439 EXPORT_SYMBOL(drm_unplug_dev);
443 * We want to be able to allocate our own "struct address_space" to control
444 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
445 * stand-alone address_space objects, so we need an underlying inode. As there
446 * is no way to allocate an independent inode easily, we need a fake internal
449 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
450 * frees it again. You are allowed to use iget() and iput() to get references to
451 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
452 * drm_fs_inode_free() call (which does not have to be the last iput()).
453 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
454 * between multiple inode-users. You could, technically, call
455 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
456 * iput(), but this way you'd end up with a new vfsmount for each inode.
459 static int drm_fs_cnt;
460 static struct vfsmount *drm_fs_mnt;
462 static const struct dentry_operations drm_fs_dops = {
463 .d_dname = simple_dname,
466 static const struct super_operations drm_fs_sops = {
467 .statfs = simple_statfs,
470 static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
471 const char *dev_name, void *data)
473 return mount_pseudo(fs_type,
480 static struct file_system_type drm_fs_type = {
482 .owner = THIS_MODULE,
483 .mount = drm_fs_mount,
484 .kill_sb = kill_anon_super,
487 static struct inode *drm_fs_inode_new(void)
492 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
494 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
498 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
500 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
505 static void drm_fs_inode_free(struct inode *inode)
509 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
515 * drm_dev_init - Initialise new DRM device
517 * @driver: DRM driver
518 * @parent: Parent device object
520 * Initialize a new DRM device. No device registration is done.
521 * Call drm_dev_register() to advertice the device to user space and register it
522 * with other core subsystems. This should be done last in the device
523 * initialization sequence to make sure userspace can't access an inconsistent
526 * The initial ref-count of the object is 1. Use drm_dev_ref() and
527 * drm_dev_unref() to take and drop further ref-counts.
529 * Note that for purely virtual devices @parent can be NULL.
531 * Drivers that do not want to allocate their own device struct
532 * embedding struct &drm_device can call drm_dev_alloc() instead.
535 * 0 on success, or error code on failure.
537 int drm_dev_init(struct drm_device *dev,
538 struct drm_driver *driver,
539 struct device *parent)
543 kref_init(&dev->ref);
545 dev->driver = driver;
547 INIT_LIST_HEAD(&dev->filelist);
548 INIT_LIST_HEAD(&dev->ctxlist);
549 INIT_LIST_HEAD(&dev->vmalist);
550 INIT_LIST_HEAD(&dev->maplist);
551 INIT_LIST_HEAD(&dev->vblank_event_list);
553 spin_init(&dev->buf_lock, "drmdbl");
554 lockinit(&dev->event_lock, "drmev", 0, LK_CANRECURSE);
555 lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE);
556 lockinit(&dev->filelist_mutex, "drmflm", 0, LK_CANRECURSE);
557 lockinit(&dev->ctxlist_mutex, "drmclm", 0, LK_CANRECURSE);
558 lockinit(&dev->master_mutex, "drmmm", 0, LK_CANRECURSE);
560 #ifndef __DragonFly__
561 dev->anon_inode = drm_fs_inode_new();
562 if (IS_ERR(dev->anon_inode)) {
563 ret = PTR_ERR(dev->anon_inode);
564 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
568 dev->anon_inode = NULL;
571 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
572 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
577 if (drm_core_check_feature(dev, DRIVER_RENDER)) {
578 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
583 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY);
587 ret = drm_ht_create(&dev->map_hash, 12);
591 drm_legacy_ctxbitmap_init(dev);
593 if (drm_core_check_feature(dev, DRIVER_GEM)) {
594 ret = drm_gem_init(dev);
596 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
603 ret = drm_dev_set_unique(dev, dev_name(parent));
613 if (drm_core_check_feature(dev, DRIVER_GEM))
614 drm_gem_destroy(dev);
617 drm_legacy_ctxbitmap_cleanup(dev);
618 drm_ht_remove(&dev->map_hash);
620 drm_minor_free(dev, DRM_MINOR_PRIMARY);
621 drm_minor_free(dev, DRM_MINOR_RENDER);
622 drm_minor_free(dev, DRM_MINOR_CONTROL);
623 #ifndef __DragonFly__
624 drm_fs_inode_free(dev->anon_inode);
627 mutex_destroy(&dev->master_mutex);
630 EXPORT_SYMBOL(drm_dev_init);
633 * drm_dev_alloc - Allocate new DRM device
634 * @driver: DRM driver to allocate device for
635 * @parent: Parent device object
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
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.
646 * Note that for purely virtual devices @parent can be NULL.
648 * Drivers that wish to subclass or embed struct &drm_device into their
649 * own struct should look at using drm_dev_init() instead.
652 * Pointer to new DRM device, or ERR_PTR on failure.
654 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
655 struct device *parent)
657 struct drm_device *dev;
660 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
662 return ERR_PTR(-ENOMEM);
664 ret = drm_dev_init(dev, driver, parent);
672 EXPORT_SYMBOL(drm_dev_alloc);
675 static void drm_dev_release(struct kref *ref)
677 struct drm_device *dev = container_of(ref, struct drm_device, ref);
679 if (drm_core_check_feature(dev, DRIVER_GEM))
680 drm_gem_destroy(dev);
682 drm_legacy_ctxbitmap_cleanup(dev);
683 drm_ht_remove(&dev->map_hash);
684 drm_fs_inode_free(dev->anon_inode);
686 drm_minor_free(dev, DRM_MINOR_PRIMARY);
687 drm_minor_free(dev, DRM_MINOR_RENDER);
688 drm_minor_free(dev, DRM_MINOR_CONTROL);
690 mutex_destroy(&dev->master_mutex);
697 * drm_dev_ref - Take reference of a DRM device
698 * @dev: device to take reference of or NULL
700 * This increases the ref-count of @dev by one. You *must* already own a
701 * reference when calling this. Use drm_dev_unref() to drop this reference
704 * This function never fails. However, this function does not provide *any*
705 * guarantee whether the device is alive or running. It only provides a
706 * reference to the object and the memory associated with it.
708 void drm_dev_ref(struct drm_device *dev)
713 EXPORT_SYMBOL(drm_dev_ref);
716 * drm_dev_unref - Drop reference of a DRM device
717 * @dev: device to drop reference of or NULL
719 * This decreases the ref-count of @dev by one. The device is destroyed if the
720 * ref-count drops to zero.
722 void drm_dev_unref(struct drm_device *dev)
726 kref_put(&dev->ref, drm_dev_release);
729 EXPORT_SYMBOL(drm_dev_unref);
732 * drm_dev_register - Register DRM device
733 * @dev: Device to register
734 * @flags: Flags passed to the driver's .load() function
736 * Register the DRM device @dev with the system, advertise device to user-space
737 * and start normal device operation. @dev must be allocated via drm_dev_alloc()
740 * Never call this twice on any device!
742 * NOTE: To ensure backward compatibility with existing drivers method this
743 * function calls the ->load() method after registering the device nodes,
744 * creating race conditions. Usage of the ->load() methods is therefore
745 * deprecated, drivers must perform all initialization before calling
746 * drm_dev_register().
749 * 0 on success, negative error code on failure.
751 int drm_dev_register(struct drm_device *dev, unsigned long flags)
755 mutex_lock(&drm_global_mutex);
757 ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
761 ret = drm_minor_register(dev, DRM_MINOR_RENDER);
765 ret = drm_minor_register(dev, DRM_MINOR_PRIMARY);
769 if (dev->driver->load) {
770 ret = dev->driver->load(dev, flags);
775 if (drm_core_check_feature(dev, DRIVER_MODESET))
776 drm_modeset_register_all(dev);
782 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
783 drm_minor_unregister(dev, DRM_MINOR_RENDER);
784 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
786 mutex_unlock(&drm_global_mutex);
789 EXPORT_SYMBOL(drm_dev_register);
792 * drm_dev_unregister - Unregister DRM device
793 * @dev: Device to unregister
795 * Unregister the DRM device from the system. This does the reverse of
796 * drm_dev_register() but does not deallocate the device. The caller must call
797 * drm_dev_unref() to drop their final reference.
799 * This should be called first in the device teardown code to make sure
800 * userspace can't access the device instance any more.
802 void drm_dev_unregister(struct drm_device *dev)
804 struct drm_map_list *r_list, *list_temp;
808 if (drm_core_check_feature(dev, DRIVER_MODESET))
809 drm_modeset_unregister_all(dev);
811 if (dev->driver->unload)
812 dev->driver->unload(dev);
816 drm_pci_agp_destroy(dev);
819 drm_vblank_cleanup(dev);
821 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
822 drm_legacy_rmmap(dev, r_list->map);
824 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
825 drm_minor_unregister(dev, DRM_MINOR_RENDER);
826 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
828 EXPORT_SYMBOL(drm_dev_unregister);
832 * The DRM core module initializes all global DRM objects and makes them
833 * available to drivers. Once setup, drivers can probe their respective
835 * Currently, core management includes:
836 * - The "DRM-Global" key/value database
837 * - Global ID management for connectors
838 * - DRM major number allocation
839 * - DRM minor management
843 * Furthermore, the DRM core provides dynamic char-dev lookups. For each
844 * interface registered on a DRM device, you can request minor numbers from DRM
845 * core. DRM core takes care of major-number management and char-dev
846 * registration. A stub ->open() callback forwards any open() requests to the
851 static int drm_stub_open(struct inode *inode, struct file *filp)
853 const struct file_operations *new_fops;
854 struct drm_minor *minor;
859 mutex_lock(&drm_global_mutex);
860 minor = drm_minor_acquire(iminor(inode));
862 err = PTR_ERR(minor);
866 new_fops = fops_get(minor->dev->driver->fops);
872 replace_fops(filp, new_fops);
873 if (filp->f_op->open)
874 err = filp->f_op->open(inode, filp);
879 drm_minor_release(minor);
881 mutex_unlock(&drm_global_mutex);
885 static const struct file_operations drm_stub_fops = {
886 .owner = THIS_MODULE,
887 .open = drm_stub_open,
888 .llseek = noop_llseek,
892 static void drm_core_exit(void)
895 unregister_chrdev(DRM_MAJOR, "drm");
896 debugfs_remove(drm_debugfs_root);
899 idr_destroy(&drm_minors_idr);
900 drm_connector_ida_destroy();
901 drm_global_release();
904 static int __init drm_core_init(void)
911 drm_connector_ida_init();
912 idr_init(&drm_minors_idr);
915 ret = drm_sysfs_init();
917 DRM_ERROR("Cannot create DRM class: %d\n", ret);
921 drm_debugfs_root = debugfs_create_dir("dri", NULL);
922 if (!drm_debugfs_root) {
924 DRM_ERROR("Cannot create debugfs-root: %d\n", ret);
928 ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
933 DRM_INFO("Initialized\n");
943 module_init(drm_core_init);
944 module_exit(drm_core_exit);
946 #include <sys/devfs.h>
948 #include <linux/export.h>
949 #include <linux/dmi.h>
950 #include <drm/drmP.h>
952 static int drm_load(struct drm_device *dev);
953 drm_pci_id_list_t *drm_find_description(int vendor, int device,
954 drm_pci_id_list_t *idlist);
956 #define DRIVER_SOFTC(unit) \
957 ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
960 drm_modevent(module_t mod, int type, void *data)
965 TUNABLE_INT_FETCH("drm.debug", &drm_debug);
966 linux_task_drop_callback = linux_task_drop;
967 linux_proc_drop_callback = linux_proc_drop;
970 linux_task_drop_callback = NULL;
971 linux_proc_drop_callback = NULL;
977 static moduledata_t drm_mod = {
982 DECLARE_MODULE(drm, drm_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
983 MODULE_VERSION(drm, 1);
984 MODULE_DEPEND(drm, agp, 1, 1, 1);
985 MODULE_DEPEND(drm, pci, 1, 1, 1);
986 MODULE_DEPEND(drm, iicbus, 1, 1, 1);
988 static struct dev_ops drm_cdevsw = {
989 { "drm", 0, D_TRACKCLOSE | D_MPSAFE },
991 .d_close = drm_close,
993 .d_ioctl = drm_ioctl,
994 .d_kqfilter = drm_kqfilter,
996 .d_mmap_single = drm_mmap_single,
999 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device");
1000 SYSCTL_INT(_hw_drm, OID_AUTO, debug, CTLFLAG_RW, &drm_debug, 0,
1003 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist)
1005 drm_pci_id_list_t *id_entry;
1008 vendor = pci_get_vendor(kdev);
1009 device = pci_get_device(kdev);
1011 if (pci_get_class(kdev) != PCIC_DISPLAY)
1014 id_entry = drm_find_description(vendor, device, idlist);
1015 if (id_entry != NULL) {
1016 if (!device_get_desc(kdev)) {
1017 device_set_desc(kdev, id_entry->name);
1018 DRM_DEBUG("desc : %s\n", device_get_desc(kdev));
1026 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist)
1028 struct drm_device *dev;
1029 drm_pci_id_list_t *id_entry;
1032 unit = device_get_unit(kdev);
1033 dev = device_get_softc(kdev);
1035 /* Initialize Linux struct device */
1036 dev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1038 if (!strcmp(device_get_name(kdev), "drmsub"))
1039 dev->dev->bsddev = device_get_parent(kdev);
1041 dev->dev->bsddev = kdev;
1043 dev->pci_domain = pci_get_domain(dev->dev->bsddev);
1044 dev->pci_bus = pci_get_bus(dev->dev->bsddev);
1045 dev->pci_slot = pci_get_slot(dev->dev->bsddev);
1046 dev->pci_func = pci_get_function(dev->dev->bsddev);
1047 drm_init_pdev(dev->dev->bsddev, &dev->pdev);
1049 id_entry = drm_find_description(dev->pdev->vendor,
1050 dev->pdev->device, idlist);
1051 dev->id_entry = id_entry;
1053 /* Print the contents of pdev struct. */
1054 drm_print_pdev(dev->pdev);
1056 lwkt_serialize_init(&dev->irq_lock);
1057 lockinit(&dev->event_lock, "drmev", 0, LK_CANRECURSE);
1058 lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE);
1060 error = drm_load(dev);
1064 error = drm_create_cdevs(kdev);
1071 drm_create_cdevs(device_t kdev)
1073 struct drm_device *dev;
1076 unit = device_get_unit(kdev);
1077 dev = device_get_softc(kdev);
1079 dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID,
1080 DRM_DEV_MODE, "dri/card%d", unit);
1083 dev->devnode->si_drv1 = dev;
1087 #ifndef DRM_DEV_NAME
1088 #define DRM_DEV_NAME "drm"
1091 devclass_t drm_devclass;
1093 drm_pci_id_list_t *drm_find_description(int vendor, int device,
1094 drm_pci_id_list_t *idlist)
1098 for (i = 0; idlist[i].vendor != 0; i++) {
1099 if ((idlist[i].vendor == vendor) &&
1100 ((idlist[i].device == device) ||
1101 (idlist[i].device == 0))) {
1108 static int drm_load(struct drm_device *dev)
1114 INIT_LIST_HEAD(&dev->maplist);
1116 drm_sysctl_init(dev);
1117 INIT_LIST_HEAD(&dev->filelist);
1119 INIT_LIST_HEAD(&dev->vblank_event_list);
1121 if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
1122 if (drm_pci_device_is_agp(dev))
1123 dev->agp = drm_agp_init(dev);
1126 if (dev->driver->driver_features & DRIVER_GEM) {
1127 retcode = drm_gem_init(dev);
1129 DRM_ERROR("Cannot initialize graphics execution "
1135 if (dev->driver->load != NULL) {
1137 /* Shared code returns -errno. */
1138 retcode = -dev->driver->load(dev,
1139 dev->id_entry->driver_private);
1140 if (pci_enable_busmaster(dev->dev->bsddev))
1141 DRM_ERROR("Request to enable bus-master failed.\n");
1147 DRM_INFO("Initialized %s %d.%d.%d %s\n",
1151 dev->driver->patchlevel,
1157 drm_gem_destroy(dev);
1158 drm_sysctl_cleanup(dev);
1162 if (dev->devnode != NULL)
1163 destroy_dev(dev->devnode);
1165 lockuninit(&dev->vbl_lock);
1166 lockuninit(&dev->event_lock);
1167 lockuninit(&dev->struct_mutex);
1173 * Stub is needed for devfs
1175 int drm_close(struct dev_close_args *ap)
1180 /* XXX: this is supposed to be drm_release() */
1181 void drm_cdevpriv_dtor(void *cd)
1183 struct drm_file *file_priv = cd;
1184 struct drm_device *dev = file_priv->dev;
1186 DRM_DEBUG("open_count = %d\n", dev->open_count);
1190 if (dev->driver->preclose != NULL)
1191 dev->driver->preclose(dev, file_priv);
1193 /* ========================================================
1194 * Begin inline drm_release
1197 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
1198 DRM_CURRENTPID, (long)dev->dev, dev->open_count);
1200 if (dev->driver->driver_features & DRIVER_GEM)
1201 drm_gem_release(dev, file_priv);
1203 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1204 drm_legacy_reclaim_buffers(dev, file_priv);
1206 funsetown(&dev->buf_sigio);
1208 if (dev->driver->postclose != NULL)
1209 dev->driver->postclose(dev, file_priv);
1210 list_del(&file_priv->lhead);
1213 /* ========================================================
1214 * End inline drm_release
1217 device_unbusy(dev->dev->bsddev);
1218 if (--dev->open_count == 0) {
1226 drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx,
1227 struct sysctl_oid *top)
1229 struct sysctl_oid *oid;
1231 ksnprintf(dev->busid_str, sizeof(dev->busid_str),
1232 "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus,
1233 dev->pci_slot, dev->pci_func);
1234 oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid",
1235 CTLFLAG_RD, dev->busid_str, 0, NULL);
1238 dev->modesetting = (dev->driver->driver_features & DRIVER_MODESET) != 0;
1239 oid = SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(top), OID_AUTO,
1240 "modesetting", CTLFLAG_RD, &dev->modesetting, 0, NULL);
1248 drm_mmap_single(struct dev_mmap_single_args *ap)
1250 struct drm_device *dev;
1251 struct cdev *kdev = ap->a_head.a_dev;
1252 vm_ooffset_t *offset = ap->a_offset;
1253 vm_size_t size = ap->a_size;
1254 struct vm_object **obj_res = ap->a_object;
1255 int nprot = ap->a_nprot;
1257 dev = drm_get_device_from_kdev(kdev);
1258 if (dev->drm_ttm_bdev != NULL) {
1259 return (ttm_bo_mmap_single(dev, offset, size, obj_res, nprot));
1260 } else if ((dev->driver->driver_features & DRIVER_GEM) != 0) {
1261 return (drm_gem_mmap_single(dev, offset, size, obj_res, nprot));
1267 #include <linux/dmi.h>
1270 * Check if dmi_system_id structure matches system DMI data
1273 dmi_found(const struct dmi_system_id *dsi)
1277 char *sys_vendor, *board_vendor, *product_name, *board_name;
1279 sys_vendor = kgetenv("smbios.system.maker");
1280 board_vendor = kgetenv("smbios.planar.maker");
1281 product_name = kgetenv("smbios.system.product");
1282 board_name = kgetenv("smbios.planar.product");
1284 for (i = 0; i < NELEM(dsi->matches); i++) {
1285 slot = dsi->matches[i].slot;
1289 case DMI_SYS_VENDOR:
1290 if (sys_vendor != NULL &&
1291 !strcmp(sys_vendor, dsi->matches[i].substr))
1295 case DMI_BOARD_VENDOR:
1296 if (board_vendor != NULL &&
1297 !strcmp(board_vendor, dsi->matches[i].substr))
1301 case DMI_PRODUCT_NAME:
1302 if (product_name != NULL &&
1303 !strcmp(product_name, dsi->matches[i].substr))
1307 case DMI_BOARD_NAME:
1308 if (board_name != NULL &&
1309 !strcmp(board_name, dsi->matches[i].substr))
1320 if (sys_vendor != NULL)
1321 kfreeenv(sys_vendor);
1322 if (board_vendor != NULL)
1323 kfreeenv(board_vendor);
1324 if (product_name != NULL)
1325 kfreeenv(product_name);
1326 if (board_name != NULL)
1327 kfreeenv(board_name);
1332 int dmi_check_system(const struct dmi_system_id *sysid)
1334 const struct dmi_system_id *dsi;
1337 for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) {
1338 if (dmi_found(dsi)) {
1340 if (dsi->callback && dsi->callback(dsi))