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/drm_core.h>
33 #include "drm_legacy.h"
34 #include "drm_internal.h"
36 unsigned int drm_debug = 0; /* 1 to enable debug output */
37 EXPORT_SYMBOL(drm_debug);
39 MODULE_AUTHOR(CORE_AUTHOR);
40 MODULE_DESCRIPTION(CORE_DESC);
41 MODULE_PARM_DESC(debug, "Enable debug output");
42 MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
43 MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
44 MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
46 module_param_named(debug, drm_debug, int, 0600);
49 static DEFINE_SPINLOCK(drm_minor_lock);
50 static struct idr drm_minors_idr;
53 struct class *drm_class;
55 static struct dentry *drm_debugfs_root;
58 void drm_err(const char *format, ...)
65 va_start(args, format);
70 printk(KERN_ERR "[" DRM_NAME ":%pf] *ERROR* %pV",
71 __builtin_return_address(0), &vaf);
78 EXPORT_SYMBOL(drm_err);
80 void drm_ut_debug_printk(const char *function_name, const char *format, ...)
86 va_start(args, format);
90 printk(KERN_DEBUG "[" DRM_NAME ":%s] %pV", function_name, &vaf);
95 EXPORT_SYMBOL(drm_ut_debug_printk);
98 struct drm_master *drm_master_create(struct drm_minor *minor)
100 struct drm_master *master;
102 master = kzalloc(sizeof(*master), GFP_KERNEL);
106 kref_init(&master->refcount);
107 spin_lock_init(&master->lock.spinlock);
108 init_waitqueue_head(&master->lock.lock_queue);
109 if (drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER)) {
113 master->minor = minor;
118 struct drm_master *drm_master_get(struct drm_master *master)
120 kref_get(&master->refcount);
123 EXPORT_SYMBOL(drm_master_get);
125 static void drm_master_destroy(struct kref *kref)
127 struct drm_master *master = container_of(kref, struct drm_master, refcount);
128 struct drm_device *dev = master->minor->dev;
129 struct drm_map_list *r_list, *list_temp;
131 mutex_lock(&dev->struct_mutex);
132 if (dev->driver->master_destroy)
133 dev->driver->master_destroy(dev, master);
135 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
136 if (r_list->master == master) {
137 drm_legacy_rmmap_locked(dev, r_list->map);
142 if (master->unique) {
143 kfree(master->unique);
144 master->unique = NULL;
145 master->unique_len = 0;
148 drm_ht_remove(&master->magiclist);
150 mutex_unlock(&dev->struct_mutex);
154 void drm_master_put(struct drm_master **master)
156 kref_put(&(*master)->refcount, drm_master_destroy);
159 EXPORT_SYMBOL(drm_master_put);
162 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
163 struct drm_file *file_priv)
165 DRM_DEBUG("setmaster\n");
167 if (file_priv->master != 0)
173 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
174 struct drm_file *file_priv)
176 DRM_DEBUG("dropmaster\n");
177 if (file_priv->master != 0)
185 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
186 * of them is represented by a drm_minor object. Depending on the capabilities
187 * of the device-driver, different interfaces are registered.
189 * Minors can be accessed via dev->$minor_name. This pointer is either
190 * NULL or a valid drm_minor pointer and stays valid as long as the device is
191 * valid. This means, DRM minors have the same life-time as the underlying
192 * device. However, this doesn't mean that the minor is active. Minors are
193 * registered and unregistered dynamically according to device-state.
196 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
200 case DRM_MINOR_LEGACY:
201 return &dev->primary;
202 case DRM_MINOR_RENDER:
204 case DRM_MINOR_CONTROL:
205 return &dev->control;
211 static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
213 struct drm_minor *minor;
217 minor = kzalloc(sizeof(*minor), GFP_KERNEL);
224 idr_preload(GFP_KERNEL);
225 spin_lock_irqsave(&drm_minor_lock, flags);
226 r = idr_alloc(&drm_minors_idr,
231 spin_unlock_irqrestore(&drm_minor_lock, flags);
239 minor->kdev = drm_sysfs_minor_alloc(minor);
240 if (IS_ERR(minor->kdev)) {
241 r = PTR_ERR(minor->kdev);
245 *drm_minor_get_slot(dev, type) = minor;
249 spin_lock_irqsave(&drm_minor_lock, flags);
250 idr_remove(&drm_minors_idr, minor->index);
251 spin_unlock_irqrestore(&drm_minor_lock, flags);
257 static void drm_minor_free(struct drm_device *dev, unsigned int type)
259 struct drm_minor **slot, *minor;
262 slot = drm_minor_get_slot(dev, type);
267 drm_mode_group_destroy(&minor->mode_group);
268 put_device(minor->kdev);
270 spin_lock_irqsave(&drm_minor_lock, flags);
271 idr_remove(&drm_minors_idr, minor->index);
272 spin_unlock_irqrestore(&drm_minor_lock, flags);
278 static int drm_minor_register(struct drm_device *dev, unsigned int type)
280 struct drm_minor *minor;
286 minor = *drm_minor_get_slot(dev, type);
290 ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
292 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
296 ret = device_add(minor->kdev);
300 /* replace NULL with @minor so lookups will succeed from now on */
301 spin_lock_irqsave(&drm_minor_lock, flags);
302 idr_replace(&drm_minors_idr, minor, minor->index);
303 spin_unlock_irqrestore(&drm_minor_lock, flags);
305 DRM_DEBUG("new minor registered %d\n", minor->index);
309 drm_debugfs_cleanup(minor);
313 static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
315 struct drm_minor *minor;
318 minor = *drm_minor_get_slot(dev, type);
319 if (!minor || !device_is_registered(minor->kdev))
322 /* replace @minor with NULL so lookups will fail from now on */
323 spin_lock_irqsave(&drm_minor_lock, flags);
324 idr_replace(&drm_minors_idr, NULL, minor->index);
325 spin_unlock_irqrestore(&drm_minor_lock, flags);
327 device_del(minor->kdev);
328 dev_set_drvdata(minor->kdev, NULL); /* safety belt */
329 drm_debugfs_cleanup(minor);
333 * drm_minor_acquire - Acquire a DRM minor
334 * @minor_id: Minor ID of the DRM-minor
336 * Looks up the given minor-ID and returns the respective DRM-minor object. The
337 * refence-count of the underlying device is increased so you must release this
338 * object with drm_minor_release().
340 * As long as you hold this minor, it is guaranteed that the object and the
341 * minor->dev pointer will stay valid! However, the device may get unplugged and
342 * unregistered while you hold the minor.
345 * Pointer to minor-object with increased device-refcount, or PTR_ERR on
348 struct drm_minor *drm_minor_acquire(unsigned int minor_id)
350 struct drm_minor *minor;
353 spin_lock_irqsave(&drm_minor_lock, flags);
354 minor = idr_find(&drm_minors_idr, minor_id);
356 drm_dev_ref(minor->dev);
357 spin_unlock_irqrestore(&drm_minor_lock, flags);
360 return ERR_PTR(-ENODEV);
361 } else if (drm_device_is_unplugged(minor->dev)) {
362 drm_dev_unref(minor->dev);
363 return ERR_PTR(-ENODEV);
370 * drm_minor_release - Release DRM minor
371 * @minor: Pointer to DRM minor object
373 * Release a minor that was previously acquired via drm_minor_acquire().
375 void drm_minor_release(struct drm_minor *minor)
377 drm_dev_unref(minor->dev);
381 * drm_put_dev - Unregister and release a DRM device
384 * Called at module unload time or when a PCI device is unplugged.
386 * Use of this function is discouraged. It will eventually go away completely.
387 * Please use drm_dev_unregister() and drm_dev_unref() explicitly instead.
389 * Cleans up all DRM device, calling drm_lastclose().
391 void drm_put_dev(struct drm_device *dev)
396 DRM_ERROR("cleanup called no dev\n");
400 drm_dev_unregister(dev);
403 EXPORT_SYMBOL(drm_put_dev);
405 void drm_unplug_dev(struct drm_device *dev)
407 /* for a USB device */
408 drm_minor_unregister(dev, DRM_MINOR_LEGACY);
409 drm_minor_unregister(dev, DRM_MINOR_RENDER);
410 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
412 mutex_lock(&drm_global_mutex);
414 drm_device_set_unplugged(dev);
416 if (dev->open_count == 0) {
419 mutex_unlock(&drm_global_mutex);
421 EXPORT_SYMBOL(drm_unplug_dev);
425 * We want to be able to allocate our own "struct address_space" to control
426 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
427 * stand-alone address_space objects, so we need an underlying inode. As there
428 * is no way to allocate an independent inode easily, we need a fake internal
431 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
432 * frees it again. You are allowed to use iget() and iput() to get references to
433 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
434 * drm_fs_inode_free() call (which does not have to be the last iput()).
435 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
436 * between multiple inode-users. You could, technically, call
437 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
438 * iput(), but this way you'd end up with a new vfsmount for each inode.
441 static int drm_fs_cnt;
442 static struct vfsmount *drm_fs_mnt;
444 static const struct dentry_operations drm_fs_dops = {
445 .d_dname = simple_dname,
448 static const struct super_operations drm_fs_sops = {
449 .statfs = simple_statfs,
452 static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
453 const char *dev_name, void *data)
455 return mount_pseudo(fs_type,
462 static struct file_system_type drm_fs_type = {
464 .owner = THIS_MODULE,
465 .mount = drm_fs_mount,
466 .kill_sb = kill_anon_super,
469 static struct inode *drm_fs_inode_new(void)
474 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
476 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
480 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
482 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
487 static void drm_fs_inode_free(struct inode *inode)
491 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
496 * drm_dev_alloc - Allocate new DRM device
497 * @driver: DRM driver to allocate device for
498 * @parent: Parent device object
500 * Allocate and initialize a new DRM device. No device registration is done.
501 * Call drm_dev_register() to advertice the device to user space and register it
502 * with other core subsystems.
504 * The initial ref-count of the object is 1. Use drm_dev_ref() and
505 * drm_dev_unref() to take and drop further ref-counts.
507 * Note that for purely virtual devices @parent can be NULL.
510 * Pointer to new DRM device, or NULL if out of memory.
512 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
513 struct device *parent)
515 struct drm_device *dev;
518 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
522 kref_init(&dev->ref);
524 dev->driver = driver;
526 INIT_LIST_HEAD(&dev->filelist);
527 INIT_LIST_HEAD(&dev->ctxlist);
528 INIT_LIST_HEAD(&dev->vmalist);
529 INIT_LIST_HEAD(&dev->maplist);
530 INIT_LIST_HEAD(&dev->vblank_event_list);
532 spin_lock_init(&dev->buf_lock);
533 spin_lock_init(&dev->event_lock);
534 mutex_init(&dev->struct_mutex);
535 mutex_init(&dev->ctxlist_mutex);
536 mutex_init(&dev->master_mutex);
538 dev->anon_inode = drm_fs_inode_new();
539 if (IS_ERR(dev->anon_inode)) {
540 ret = PTR_ERR(dev->anon_inode);
541 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
545 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
546 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
551 if (drm_core_check_feature(dev, DRIVER_RENDER)) {
552 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
557 ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
561 if (drm_ht_create(&dev->map_hash, 12))
564 ret = drm_legacy_ctxbitmap_init(dev);
566 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
570 if (drm_core_check_feature(dev, DRIVER_GEM)) {
571 ret = drm_gem_init(dev);
573 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
581 drm_legacy_ctxbitmap_cleanup(dev);
583 drm_ht_remove(&dev->map_hash);
585 drm_minor_free(dev, DRM_MINOR_LEGACY);
586 drm_minor_free(dev, DRM_MINOR_RENDER);
587 drm_minor_free(dev, DRM_MINOR_CONTROL);
588 drm_fs_inode_free(dev->anon_inode);
590 mutex_destroy(&dev->master_mutex);
594 EXPORT_SYMBOL(drm_dev_alloc);
596 static void drm_dev_release(struct kref *ref)
598 struct drm_device *dev = container_of(ref, struct drm_device, ref);
600 if (drm_core_check_feature(dev, DRIVER_GEM))
601 drm_gem_destroy(dev);
603 drm_legacy_ctxbitmap_cleanup(dev);
604 drm_ht_remove(&dev->map_hash);
605 drm_fs_inode_free(dev->anon_inode);
607 drm_minor_free(dev, DRM_MINOR_LEGACY);
608 drm_minor_free(dev, DRM_MINOR_RENDER);
609 drm_minor_free(dev, DRM_MINOR_CONTROL);
611 mutex_destroy(&dev->master_mutex);
617 * drm_dev_ref - Take reference of a DRM device
618 * @dev: device to take reference of or NULL
620 * This increases the ref-count of @dev by one. You *must* already own a
621 * reference when calling this. Use drm_dev_unref() to drop this reference
624 * This function never fails. However, this function does not provide *any*
625 * guarantee whether the device is alive or running. It only provides a
626 * reference to the object and the memory associated with it.
628 void drm_dev_ref(struct drm_device *dev)
633 EXPORT_SYMBOL(drm_dev_ref);
636 * drm_dev_unref - Drop reference of a DRM device
637 * @dev: device to drop reference of or NULL
639 * This decreases the ref-count of @dev by one. The device is destroyed if the
640 * ref-count drops to zero.
642 void drm_dev_unref(struct drm_device *dev)
645 kref_put(&dev->ref, drm_dev_release);
647 EXPORT_SYMBOL(drm_dev_unref);
650 * drm_dev_register - Register DRM device
651 * @dev: Device to register
652 * @flags: Flags passed to the driver's .load() function
654 * Register the DRM device @dev with the system, advertise device to user-space
655 * and start normal device operation. @dev must be allocated via drm_dev_alloc()
658 * Never call this twice on any device!
661 * 0 on success, negative error code on failure.
663 int drm_dev_register(struct drm_device *dev, unsigned long flags)
667 mutex_lock(&drm_global_mutex);
669 ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
673 ret = drm_minor_register(dev, DRM_MINOR_RENDER);
677 ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
681 if (dev->driver->load) {
682 ret = dev->driver->load(dev, flags);
687 /* setup grouping for legacy outputs */
688 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
689 ret = drm_mode_group_init_legacy_group(dev,
690 &dev->primary->mode_group);
699 if (dev->driver->unload)
700 dev->driver->unload(dev);
702 drm_minor_unregister(dev, DRM_MINOR_LEGACY);
703 drm_minor_unregister(dev, DRM_MINOR_RENDER);
704 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
706 mutex_unlock(&drm_global_mutex);
709 EXPORT_SYMBOL(drm_dev_register);
712 * drm_dev_unregister - Unregister DRM device
713 * @dev: Device to unregister
715 * Unregister the DRM device from the system. This does the reverse of
716 * drm_dev_register() but does not deallocate the device. The caller must call
717 * drm_dev_unref() to drop their final reference.
719 void drm_dev_unregister(struct drm_device *dev)
721 struct drm_map_list *r_list, *list_temp;
725 if (dev->driver->unload)
726 dev->driver->unload(dev);
729 drm_pci_agp_destroy(dev);
731 drm_vblank_cleanup(dev);
733 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
734 drm_legacy_rmmap(dev, r_list->map);
736 drm_minor_unregister(dev, DRM_MINOR_LEGACY);
737 drm_minor_unregister(dev, DRM_MINOR_RENDER);
738 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
740 EXPORT_SYMBOL(drm_dev_unregister);
743 * drm_dev_set_unique - Set the unique name of a DRM device
744 * @dev: device of which to set the unique name
745 * @fmt: format string for unique name
747 * Sets the unique name of a DRM device using the specified format string and
748 * a variable list of arguments. Drivers can use this at driver probe time if
749 * the unique name of the devices they drive is static.
751 * Return: 0 on success or a negative error code on failure.
753 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...)
760 dev->unique = kvasprintf(GFP_KERNEL, fmt, ap);
763 return dev->unique ? 0 : -ENOMEM;
765 EXPORT_SYMBOL(drm_dev_set_unique);
770 * The DRM core module initializes all global DRM objects and makes them
771 * available to drivers. Once setup, drivers can probe their respective
773 * Currently, core management includes:
774 * - The "DRM-Global" key/value database
775 * - Global ID management for connectors
776 * - DRM major number allocation
777 * - DRM minor management
781 * Furthermore, the DRM core provides dynamic char-dev lookups. For each
782 * interface registered on a DRM device, you can request minor numbers from DRM
783 * core. DRM core takes care of major-number management and char-dev
784 * registration. A stub ->open() callback forwards any open() requests to the
789 static int drm_stub_open(struct inode *inode, struct file *filp)
791 const struct file_operations *new_fops;
792 struct drm_minor *minor;
797 mutex_lock(&drm_global_mutex);
798 minor = drm_minor_acquire(iminor(inode));
800 err = PTR_ERR(minor);
804 new_fops = fops_get(minor->dev->driver->fops);
810 replace_fops(filp, new_fops);
811 if (filp->f_op->open)
812 err = filp->f_op->open(inode, filp);
817 drm_minor_release(minor);
819 mutex_unlock(&drm_global_mutex);
823 static const struct file_operations drm_stub_fops = {
824 .owner = THIS_MODULE,
825 .open = drm_stub_open,
826 .llseek = noop_llseek,
829 static int __init drm_core_init(void)
834 drm_connector_ida_init();
835 idr_init(&drm_minors_idr);
837 if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
840 drm_class = drm_sysfs_create(THIS_MODULE, "drm");
841 if (IS_ERR(drm_class)) {
842 printk(KERN_ERR "DRM: Error creating drm class.\n");
843 ret = PTR_ERR(drm_class);
847 drm_debugfs_root = debugfs_create_dir("dri", NULL);
848 if (!drm_debugfs_root) {
849 DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
854 DRM_INFO("Initialized %s %d.%d.%d %s\n",
855 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
860 unregister_chrdev(DRM_MAJOR, "drm");
862 idr_destroy(&drm_minors_idr);
867 static void __exit drm_core_exit(void)
869 debugfs_remove(drm_debugfs_root);
872 unregister_chrdev(DRM_MAJOR, "drm");
874 drm_connector_ida_destroy();
875 idr_destroy(&drm_minors_idr);
878 module_init(drm_core_init);
879 module_exit(drm_core_exit);
882 #include <sys/devfs.h>
884 #include <linux/export.h>
885 #include <linux/dmi.h>
886 #include <drm/drmP.h>
887 #include <drm/drm_core.h>
889 #if DRM_DEBUG_DEFAULT_ON == 1
890 #define DRM_DEBUGBITS_ON (DRM_DEBUGBITS_DEBUG | DRM_DEBUGBITS_KMS | \
891 DRM_DEBUGBITS_FAILED_IOCTL)
892 #elif DRM_DEBUG_DEFAULT_ON == 2
893 #define DRM_DEBUGBITS_ON (DRM_DEBUGBITS_DEBUG | DRM_DEBUGBITS_KMS | \
894 DRM_DEBUGBITS_FAILED_IOCTL | DRM_DEBUGBITS_VERBOSE)
896 #define DRM_DEBUGBITS_ON (0x0)
899 int drm_notyet_flag = 0;
901 static int drm_load(struct drm_device *dev);
902 drm_pci_id_list_t *drm_find_description(int vendor, int device,
903 drm_pci_id_list_t *idlist);
905 #define DRIVER_SOFTC(unit) \
906 ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
909 drm_modevent(module_t mod, int type, void *data)
914 TUNABLE_INT_FETCH("drm.debug", &drm_debug);
915 TUNABLE_INT_FETCH("drm.notyet", &drm_notyet_flag);
921 static moduledata_t drm_mod = {
926 DECLARE_MODULE(drm, drm_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
927 MODULE_VERSION(drm, 1);
928 MODULE_DEPEND(drm, agp, 1, 1, 1);
929 MODULE_DEPEND(drm, pci, 1, 1, 1);
930 MODULE_DEPEND(drm, iicbus, 1, 1, 1);
932 static struct dev_ops drm_cdevsw = {
933 { "drm", 0, D_TRACKCLOSE | D_MPSAFE },
935 .d_close = drm_close,
937 .d_ioctl = drm_ioctl,
938 .d_kqfilter = drm_kqfilter,
940 .d_mmap_single = drm_mmap_single,
943 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device");
944 SYSCTL_INT(_hw_drm, OID_AUTO, debug, CTLFLAG_RW, &drm_debug, 0,
947 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist)
949 drm_pci_id_list_t *id_entry;
952 vendor = pci_get_vendor(kdev);
953 device = pci_get_device(kdev);
955 if (pci_get_class(kdev) != PCIC_DISPLAY)
958 id_entry = drm_find_description(vendor, device, idlist);
959 if (id_entry != NULL) {
960 if (!device_get_desc(kdev)) {
961 DRM_DEBUG("desc : %s\n", device_get_desc(kdev));
962 device_set_desc(kdev, id_entry->name);
970 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist)
972 struct drm_device *dev;
973 drm_pci_id_list_t *id_entry;
978 unit = device_get_unit(kdev);
979 dev = device_get_softc(kdev);
981 if (!strcmp(device_get_name(kdev), "drmsub"))
982 dev->dev = device_get_parent(kdev);
986 dev->pci_domain = pci_get_domain(dev->dev);
987 dev->pci_bus = pci_get_bus(dev->dev);
988 dev->pci_slot = pci_get_slot(dev->dev);
989 dev->pci_func = pci_get_function(dev->dev);
990 drm_init_pdev(dev->dev, &dev->pdev);
992 id_entry = drm_find_description(dev->pdev->vendor,
993 dev->pdev->device, idlist);
994 dev->id_entry = id_entry;
996 if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) {
999 dev->irq_type = pci_alloc_1intr(dev->dev, msi_enable,
1000 &dev->irqrid, &irq_flags);
1002 dev->irqr = bus_alloc_resource_any(dev->dev, SYS_RES_IRQ,
1003 &dev->irqrid, irq_flags);
1009 dev->irq = (int) rman_get_start(dev->irqr);
1012 lockinit(&dev->dev_lock, "drmdev", 0, LK_CANRECURSE);
1013 lwkt_serialize_init(&dev->irq_lock);
1014 lockinit(&dev->event_lock, "drmev", 0, LK_CANRECURSE);
1015 lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE);
1017 error = drm_load(dev);
1021 error = drm_create_cdevs(kdev);
1028 bus_release_resource(dev->dev, SYS_RES_IRQ,
1029 dev->irqrid, dev->irqr);
1031 if (dev->irq_type == PCI_INTR_TYPE_MSI) {
1032 pci_release_msi(dev->dev);
1038 drm_create_cdevs(device_t kdev)
1040 struct drm_device *dev;
1043 unit = device_get_unit(kdev);
1044 dev = device_get_softc(kdev);
1046 dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID,
1047 DRM_DEV_MODE, "dri/card%d", unit);
1050 dev->devnode->si_drv1 = dev;
1054 #ifndef DRM_DEV_NAME
1055 #define DRM_DEV_NAME "drm"
1058 devclass_t drm_devclass;
1060 drm_pci_id_list_t *drm_find_description(int vendor, int device,
1061 drm_pci_id_list_t *idlist)
1065 for (i = 0; idlist[i].vendor != 0; i++) {
1066 if ((idlist[i].vendor == vendor) &&
1067 ((idlist[i].device == device) ||
1068 (idlist[i].device == 0))) {
1075 static int drm_load(struct drm_device *dev)
1081 INIT_LIST_HEAD(&dev->maplist);
1083 drm_sysctl_init(dev);
1084 INIT_LIST_HEAD(&dev->filelist);
1087 dev->types[0] = _DRM_STAT_LOCK;
1088 dev->types[1] = _DRM_STAT_OPENS;
1089 dev->types[2] = _DRM_STAT_CLOSES;
1090 dev->types[3] = _DRM_STAT_IOCTLS;
1091 dev->types[4] = _DRM_STAT_LOCKS;
1092 dev->types[5] = _DRM_STAT_UNLOCKS;
1094 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
1095 atomic_set(&dev->counts[i], 0);
1097 INIT_LIST_HEAD(&dev->vblank_event_list);
1099 if (drm_core_has_AGP(dev)) {
1100 if (drm_device_is_agp(dev))
1101 dev->agp = drm_agp_init();
1102 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP) &&
1104 DRM_ERROR("Card isn't AGP, or couldn't initialize "
1109 if (dev->agp != NULL && dev->agp->agp_info.ai_aperture_base != 0) {
1110 if (drm_mtrr_add(dev->agp->agp_info.ai_aperture_base,
1111 dev->agp->agp_info.ai_aperture_size, DRM_MTRR_WC) == 0)
1112 dev->agp->agp_mtrr = 1;
1116 if (dev->driver->driver_features & DRIVER_GEM) {
1117 retcode = drm_gem_init(dev);
1119 DRM_ERROR("Cannot initialize graphics execution "
1125 if (dev->driver->load != NULL) {
1127 /* Shared code returns -errno. */
1128 retcode = -dev->driver->load(dev,
1129 dev->id_entry->driver_private);
1130 if (pci_enable_busmaster(dev->dev))
1131 DRM_ERROR("Request to enable bus-master failed.\n");
1137 DRM_INFO("Initialized %s %d.%d.%d %s\n",
1141 dev->driver->patchlevel,
1147 drm_gem_destroy(dev);
1149 drm_sysctl_cleanup(dev);
1153 if (dev->devnode != NULL)
1154 destroy_dev(dev->devnode);
1156 lockuninit(&dev->vbl_lock);
1157 lockuninit(&dev->dev_lock);
1158 lockuninit(&dev->event_lock);
1159 lockuninit(&dev->struct_mutex);
1165 * Stub is needed for devfs
1167 int drm_close(struct dev_close_args *ap)
1172 void drm_cdevpriv_dtor(void *cd)
1174 struct drm_file *file_priv = cd;
1175 struct drm_device *dev = file_priv->dev;
1178 DRM_DEBUG("open_count = %d\n", dev->open_count);
1182 if (dev->driver->preclose != NULL)
1183 dev->driver->preclose(dev, file_priv);
1185 /* ========================================================
1186 * Begin inline drm_release
1189 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
1190 DRM_CURRENTPID, (long)dev->dev, dev->open_count);
1192 if (dev->driver->driver_features & DRIVER_GEM)
1193 drm_gem_release(dev, file_priv);
1195 if (dev->lock.hw_lock && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
1196 && dev->lock.file_priv == file_priv) {
1197 DRM_DEBUG("Process %d dead, freeing lock for context %d\n",
1199 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
1200 if (dev->driver->reclaim_buffers_locked != NULL)
1201 dev->driver->reclaim_buffers_locked(dev, file_priv);
1203 drm_lock_free(&dev->lock,
1204 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
1206 /* FIXME: may require heavy-handed reset of
1207 hardware at this point, possibly
1208 processed via a callback to the X
1210 } else if (dev->driver->reclaim_buffers_locked != NULL &&
1211 dev->lock.hw_lock != NULL) {
1212 /* The lock is required to reclaim buffers */
1214 if (!dev->lock.hw_lock) {
1215 /* Device has been unregistered */
1220 retcode = DRM_LOCK_SLEEP(dev, &dev->lock.lock_queue,
1221 PCATCH, "drmlk2", 0);
1226 dev->driver->reclaim_buffers_locked(dev, file_priv);
1230 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
1231 !dev->driver->reclaim_buffers_locked)
1232 drm_legacy_reclaim_buffers(dev, file_priv);
1234 funsetown(&dev->buf_sigio);
1236 if (dev->driver->postclose != NULL)
1237 dev->driver->postclose(dev, file_priv);
1238 list_del(&file_priv->lhead);
1241 /* ========================================================
1242 * End inline drm_release
1245 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
1246 device_unbusy(dev->dev);
1247 if (--dev->open_count == 0) {
1248 retcode = drm_lastclose(dev);
1255 drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx,
1256 struct sysctl_oid *top)
1258 struct sysctl_oid *oid;
1260 ksnprintf(dev->busid_str, sizeof(dev->busid_str),
1261 "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus,
1262 dev->pci_slot, dev->pci_func);
1263 oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid",
1264 CTLFLAG_RD, dev->busid_str, 0, NULL);
1267 dev->modesetting = (dev->driver->driver_features & DRIVER_MODESET) != 0;
1268 oid = SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(top), OID_AUTO,
1269 "modesetting", CTLFLAG_RD, &dev->modesetting, 0, NULL);
1277 drm_mmap_single(struct dev_mmap_single_args *ap)
1279 struct drm_device *dev;
1280 struct cdev *kdev = ap->a_head.a_dev;
1281 vm_ooffset_t *offset = ap->a_offset;
1282 vm_size_t size = ap->a_size;
1283 struct vm_object **obj_res = ap->a_object;
1284 int nprot = ap->a_nprot;
1286 dev = drm_get_device_from_kdev(kdev);
1287 if (dev->drm_ttm_bdev != NULL) {
1288 return (ttm_bo_mmap_single(dev->drm_ttm_bdev, offset, size,
1290 } else if ((dev->driver->driver_features & DRIVER_GEM) != 0) {
1291 return (drm_gem_mmap_single(dev, offset, size, obj_res, nprot));
1299 #include <sys/sysproto.h>
1301 MODULE_DEPEND(DRIVER_NAME, linux, 1, 1, 1);
1303 #define LINUX_IOCTL_DRM_MIN 0x6400
1304 #define LINUX_IOCTL_DRM_MAX 0x64ff
1306 static linux_ioctl_function_t drm_linux_ioctl;
1307 static struct linux_ioctl_handler drm_handler = {drm_linux_ioctl,
1308 LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX};
1310 /* The bits for in/out are switched on Linux */
1311 #define LINUX_IOC_IN IOC_OUT
1312 #define LINUX_IOC_OUT IOC_IN
1315 drm_linux_ioctl(DRM_STRUCTPROC *p, struct linux_ioctl_args* args)
1318 int cmd = args->cmd;
1320 args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
1321 if (cmd & LINUX_IOC_IN)
1322 args->cmd |= IOC_IN;
1323 if (cmd & LINUX_IOC_OUT)
1324 args->cmd |= IOC_OUT;
1326 error = ioctl(p, (struct ioctl_args *)args);
1330 #endif /* DRM_LINUX */
1333 drm_core_init(void *arg)
1339 linux_ioctl_register_handler(&drm_handler);
1340 #endif /* DRM_LINUX */
1342 DRM_INFO("Initialized %s %d.%d.%d %s\n",
1343 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
1348 drm_core_exit(void *arg)
1352 linux_ioctl_unregister_handler(&drm_handler);
1353 #endif /* DRM_LINUX */
1355 drm_global_release();
1358 SYSINIT(drm_register, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
1359 drm_core_init, NULL);
1360 SYSUNINIT(drm_unregister, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
1361 drm_core_exit, NULL);
1364 #include <linux/dmi.h>
1367 * Check if dmi_system_id structure matches system DMI data
1370 dmi_found(const struct dmi_system_id *dsi)
1374 char *sys_vendor, *board_vendor, *product_name, *board_name;
1376 sys_vendor = kgetenv("smbios.system.maker");
1377 board_vendor = kgetenv("smbios.planar.maker");
1378 product_name = kgetenv("smbios.system.product");
1379 board_name = kgetenv("smbios.planar.product");
1381 for (i = 0; i < NELEM(dsi->matches); i++) {
1382 slot = dsi->matches[i].slot;
1386 case DMI_SYS_VENDOR:
1387 if (sys_vendor != NULL &&
1388 !strcmp(sys_vendor, dsi->matches[i].substr))
1392 case DMI_BOARD_VENDOR:
1393 if (board_vendor != NULL &&
1394 !strcmp(board_vendor, dsi->matches[i].substr))
1398 case DMI_PRODUCT_NAME:
1399 if (product_name != NULL &&
1400 !strcmp(product_name, dsi->matches[i].substr))
1404 case DMI_BOARD_NAME:
1405 if (board_name != NULL &&
1406 !strcmp(board_name, dsi->matches[i].substr))
1417 if (sys_vendor != NULL)
1418 kfreeenv(sys_vendor);
1419 if (board_vendor != NULL)
1420 kfreeenv(board_vendor);
1421 if (product_name != NULL)
1422 kfreeenv(product_name);
1423 if (board_name != NULL)
1424 kfreeenv(board_name);
1429 int dmi_check_system(const struct dmi_system_id *sysid)
1431 const struct dmi_system_id *dsi;
1434 for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) {
1435 if (dmi_found(dsi)) {
1437 if (dsi->callback && dsi->callback(dsi))