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"
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(CORE_AUTHOR);
59 MODULE_DESCRIPTION(CORE_DESC);
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);
70 static DEFINE_SPINLOCK(drm_minor_lock);
71 static struct idr drm_minors_idr;
75 static struct dentry *drm_debugfs_root;
78 void drm_err(const char *func, const char *format, ...)
82 kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* ", DRM_CURRENTPID, func);
84 __va_start(args, format);
85 kvprintf(format, args);
88 EXPORT_SYMBOL(drm_err);
90 void drm_ut_debug_printk(const char *function_name, const char *format, ...)
94 if (unlikely(drm_debug & DRM_UT_PID)) {
95 kprintf("[" DRM_NAME ":pid%d:%s] ",
96 DRM_CURRENTPID, function_name);
98 kprintf("[" DRM_NAME ":%s] ", function_name);
101 __va_start(args, format);
102 kvprintf(format, args);
105 EXPORT_SYMBOL(drm_ut_debug_printk);
108 struct drm_master *drm_master_create(struct drm_minor *minor)
110 struct drm_master *master;
112 master = kzalloc(sizeof(*master), GFP_KERNEL);
116 kref_init(&master->refcount);
117 spin_lock_init(&master->lock.spinlock);
118 init_waitqueue_head(&master->lock.lock_queue);
119 if (drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER)) {
123 master->minor = minor;
128 struct drm_master *drm_master_get(struct drm_master *master)
130 kref_get(&master->refcount);
133 EXPORT_SYMBOL(drm_master_get);
135 static void drm_master_destroy(struct kref *kref)
137 struct drm_master *master = container_of(kref, struct drm_master, refcount);
138 struct drm_device *dev = master->minor->dev;
139 struct drm_map_list *r_list, *list_temp;
141 if (dev->driver->master_destroy)
142 dev->driver->master_destroy(dev, master);
144 mutex_lock(&dev->struct_mutex);
145 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
146 if (r_list->master == master) {
147 drm_legacy_rmmap_locked(dev, r_list->map);
152 if (master->unique) {
153 kfree(master->unique);
154 master->unique = NULL;
155 master->unique_len = 0;
158 drm_ht_remove(&master->magiclist);
160 mutex_unlock(&dev->struct_mutex);
164 void drm_master_put(struct drm_master **master)
166 kref_put(&(*master)->refcount, drm_master_destroy);
169 EXPORT_SYMBOL(drm_master_put);
172 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
173 struct drm_file *file_priv)
175 DRM_DEBUG("setmaster\n");
177 if (file_priv->master != 0)
183 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
184 struct drm_file *file_priv)
186 DRM_DEBUG("dropmaster\n");
187 if (file_priv->master != 0)
195 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
196 * of them is represented by a drm_minor object. Depending on the capabilities
197 * of the device-driver, different interfaces are registered.
199 * Minors can be accessed via dev->$minor_name. This pointer is either
200 * NULL or a valid drm_minor pointer and stays valid as long as the device is
201 * valid. This means, DRM minors have the same life-time as the underlying
202 * device. However, this doesn't mean that the minor is active. Minors are
203 * registered and unregistered dynamically according to device-state.
206 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
210 case DRM_MINOR_LEGACY:
211 return &dev->primary;
212 case DRM_MINOR_RENDER:
214 case DRM_MINOR_CONTROL:
215 return &dev->control;
221 static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
223 struct drm_minor *minor;
227 minor = kzalloc(sizeof(*minor), GFP_KERNEL);
234 idr_preload(GFP_KERNEL);
235 spin_lock_irqsave(&drm_minor_lock, flags);
236 r = idr_alloc(&drm_minors_idr,
241 spin_unlock_irqrestore(&drm_minor_lock, flags);
249 minor->kdev = drm_sysfs_minor_alloc(minor);
250 if (IS_ERR(minor->kdev)) {
251 r = PTR_ERR(minor->kdev);
255 *drm_minor_get_slot(dev, type) = minor;
259 spin_lock_irqsave(&drm_minor_lock, flags);
260 idr_remove(&drm_minors_idr, minor->index);
261 spin_unlock_irqrestore(&drm_minor_lock, flags);
267 static void drm_minor_free(struct drm_device *dev, unsigned int type)
269 struct drm_minor **slot, *minor;
272 slot = drm_minor_get_slot(dev, type);
277 put_device(minor->kdev);
279 spin_lock_irqsave(&drm_minor_lock, flags);
280 idr_remove(&drm_minors_idr, minor->index);
281 spin_unlock_irqrestore(&drm_minor_lock, flags);
287 static int drm_minor_register(struct drm_device *dev, unsigned int type)
289 struct drm_minor *minor;
295 minor = *drm_minor_get_slot(dev, type);
299 ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
301 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
305 ret = device_add(minor->kdev);
309 /* replace NULL with @minor so lookups will succeed from now on */
310 spin_lock_irqsave(&drm_minor_lock, flags);
311 idr_replace(&drm_minors_idr, minor, minor->index);
312 spin_unlock_irqrestore(&drm_minor_lock, flags);
314 DRM_DEBUG("new minor registered %d\n", minor->index);
318 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);
328 if (!minor || !device_is_registered(minor->kdev))
331 /* replace @minor with NULL so lookups will fail from now on */
332 spin_lock_irqsave(&drm_minor_lock, flags);
333 idr_replace(&drm_minors_idr, NULL, minor->index);
334 spin_unlock_irqrestore(&drm_minor_lock, flags);
336 device_del(minor->kdev);
337 dev_set_drvdata(minor->kdev, NULL); /* safety belt */
338 drm_debugfs_cleanup(minor);
342 * drm_minor_acquire - Acquire a DRM minor
343 * @minor_id: Minor ID of the DRM-minor
345 * Looks up the given minor-ID and returns the respective DRM-minor object. The
346 * refence-count of the underlying device is increased so you must release this
347 * object with drm_minor_release().
349 * As long as you hold this minor, it is guaranteed that the object and the
350 * minor->dev pointer will stay valid! However, the device may get unplugged and
351 * unregistered while you hold the minor.
354 * Pointer to minor-object with increased device-refcount, or PTR_ERR on
357 struct drm_minor *drm_minor_acquire(unsigned int minor_id)
359 struct drm_minor *minor;
362 spin_lock_irqsave(&drm_minor_lock, flags);
363 minor = idr_find(&drm_minors_idr, minor_id);
365 drm_dev_ref(minor->dev);
366 spin_unlock_irqrestore(&drm_minor_lock, flags);
369 return ERR_PTR(-ENODEV);
370 } else if (drm_device_is_unplugged(minor->dev)) {
371 drm_dev_unref(minor->dev);
372 return ERR_PTR(-ENODEV);
379 * drm_minor_release - Release DRM minor
380 * @minor: Pointer to DRM minor object
382 * Release a minor that was previously acquired via drm_minor_acquire().
384 void drm_minor_release(struct drm_minor *minor)
386 drm_dev_unref(minor->dev);
390 * DOC: driver instance overview
392 * A device instance for a drm driver is represented by struct &drm_device. This
393 * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
394 * callbacks implemented by the driver. The driver then needs to initialize all
395 * the various subsystems for the drm device like memory management, vblank
396 * handling, modesetting support and intial output configuration plus obviously
397 * initialize all the corresponding hardware bits. An important part of this is
398 * also calling drm_dev_set_unique() to set the userspace-visible unique name of
399 * this device instance. Finally when everything is up and running and ready for
400 * userspace the device instance can be published using drm_dev_register().
402 * There is also deprecated support for initalizing device instances using
403 * bus-specific helpers and the ->load() callback. But due to
404 * backwards-compatibility needs the device instance have to be published too
405 * early, which requires unpretty global locking to make safe and is therefore
406 * only support for existing drivers not yet converted to the new scheme.
408 * When cleaning up a device instance everything needs to be done in reverse:
409 * First unpublish the device instance with drm_dev_unregister(). Then clean up
410 * any other resources allocated at device initialization and drop the driver's
411 * reference to &drm_device using drm_dev_unref().
413 * Note that the lifetime rules for &drm_device instance has still a lot of
414 * historical baggage. Hence use the reference counting provided by
415 * drm_dev_ref() and drm_dev_unref() only carefully.
417 * Also note that embedding of &drm_device is currently not (yet) supported (but
418 * it would be easy to add). Drivers can store driver-private data in the
419 * dev_priv field of &drm_device.
423 * drm_put_dev - Unregister and release a DRM device
426 * Called at module unload time or when a PCI device is unplugged.
428 * Cleans up all DRM device, calling drm_lastclose().
430 * Note: Use of this function is deprecated. It will eventually go away
431 * completely. Please use drm_dev_unregister() and drm_dev_unref() explicitly
432 * instead to make sure that the device isn't userspace accessible any more
433 * while teardown is in progress, ensuring that userspace can't access an
434 * inconsistent state.
436 void drm_put_dev(struct drm_device *dev)
441 DRM_ERROR("cleanup called no dev\n");
445 drm_dev_unregister(dev);
448 EXPORT_SYMBOL(drm_put_dev);
450 void drm_unplug_dev(struct drm_device *dev)
452 /* for a USB device */
453 drm_minor_unregister(dev, DRM_MINOR_LEGACY);
454 drm_minor_unregister(dev, DRM_MINOR_RENDER);
455 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
457 mutex_lock(&drm_global_mutex);
459 drm_device_set_unplugged(dev);
461 if (dev->open_count == 0) {
464 mutex_unlock(&drm_global_mutex);
466 EXPORT_SYMBOL(drm_unplug_dev);
470 * We want to be able to allocate our own "struct address_space" to control
471 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
472 * stand-alone address_space objects, so we need an underlying inode. As there
473 * is no way to allocate an independent inode easily, we need a fake internal
476 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
477 * frees it again. You are allowed to use iget() and iput() to get references to
478 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
479 * drm_fs_inode_free() call (which does not have to be the last iput()).
480 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
481 * between multiple inode-users. You could, technically, call
482 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
483 * iput(), but this way you'd end up with a new vfsmount for each inode.
486 static int drm_fs_cnt;
487 static struct vfsmount *drm_fs_mnt;
489 static const struct dentry_operations drm_fs_dops = {
490 .d_dname = simple_dname,
493 static const struct super_operations drm_fs_sops = {
494 .statfs = simple_statfs,
497 static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
498 const char *dev_name, void *data)
500 return mount_pseudo(fs_type,
507 static struct file_system_type drm_fs_type = {
509 .owner = THIS_MODULE,
510 .mount = drm_fs_mount,
511 .kill_sb = kill_anon_super,
514 static struct inode *drm_fs_inode_new(void)
519 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
521 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
525 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
527 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
532 static void drm_fs_inode_free(struct inode *inode)
536 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
541 * drm_dev_alloc - Allocate new DRM device
542 * @driver: DRM driver to allocate device for
543 * @parent: Parent device object
545 * Allocate and initialize a new DRM device. No device registration is done.
546 * Call drm_dev_register() to advertice the device to user space and register it
547 * with other core subsystems. This should be done last in the device
548 * initialization sequence to make sure userspace can't access an inconsistent
551 * The initial ref-count of the object is 1. Use drm_dev_ref() and
552 * drm_dev_unref() to take and drop further ref-counts.
554 * Note that for purely virtual devices @parent can be NULL.
557 * Pointer to new DRM device, or NULL if out of memory.
559 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
560 struct device *parent)
562 struct drm_device *dev;
565 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
569 kref_init(&dev->ref);
571 dev->driver = driver;
573 INIT_LIST_HEAD(&dev->filelist);
574 INIT_LIST_HEAD(&dev->ctxlist);
575 INIT_LIST_HEAD(&dev->vmalist);
576 INIT_LIST_HEAD(&dev->maplist);
577 INIT_LIST_HEAD(&dev->vblank_event_list);
579 spin_lock_init(&dev->buf_lock);
580 spin_lock_init(&dev->event_lock);
581 mutex_init(&dev->struct_mutex);
582 mutex_init(&dev->filelist_mutex);
583 mutex_init(&dev->ctxlist_mutex);
584 mutex_init(&dev->master_mutex);
586 dev->anon_inode = drm_fs_inode_new();
587 if (IS_ERR(dev->anon_inode)) {
588 ret = PTR_ERR(dev->anon_inode);
589 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
593 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
594 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
598 WARN_ON(driver->suspend || driver->resume);
601 if (drm_core_check_feature(dev, DRIVER_RENDER)) {
602 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
607 ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
611 if (drm_ht_create(&dev->map_hash, 12))
614 drm_legacy_ctxbitmap_init(dev);
616 if (drm_core_check_feature(dev, DRIVER_GEM)) {
617 ret = drm_gem_init(dev);
619 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
625 ret = drm_dev_set_unique(dev, dev_name(parent));
633 if (drm_core_check_feature(dev, DRIVER_GEM))
634 drm_gem_destroy(dev);
636 drm_legacy_ctxbitmap_cleanup(dev);
637 drm_ht_remove(&dev->map_hash);
639 drm_minor_free(dev, DRM_MINOR_LEGACY);
640 drm_minor_free(dev, DRM_MINOR_RENDER);
641 drm_minor_free(dev, DRM_MINOR_CONTROL);
642 drm_fs_inode_free(dev->anon_inode);
644 mutex_destroy(&dev->master_mutex);
648 EXPORT_SYMBOL(drm_dev_alloc);
650 static void drm_dev_release(struct kref *ref)
652 struct drm_device *dev = container_of(ref, struct drm_device, ref);
654 if (drm_core_check_feature(dev, DRIVER_GEM))
655 drm_gem_destroy(dev);
657 drm_legacy_ctxbitmap_cleanup(dev);
658 drm_ht_remove(&dev->map_hash);
659 drm_fs_inode_free(dev->anon_inode);
661 drm_minor_free(dev, DRM_MINOR_LEGACY);
662 drm_minor_free(dev, DRM_MINOR_RENDER);
663 drm_minor_free(dev, DRM_MINOR_CONTROL);
665 mutex_destroy(&dev->master_mutex);
671 * drm_dev_ref - Take reference of a DRM device
672 * @dev: device to take reference of or NULL
674 * This increases the ref-count of @dev by one. You *must* already own a
675 * reference when calling this. Use drm_dev_unref() to drop this reference
678 * This function never fails. However, this function does not provide *any*
679 * guarantee whether the device is alive or running. It only provides a
680 * reference to the object and the memory associated with it.
682 void drm_dev_ref(struct drm_device *dev)
687 EXPORT_SYMBOL(drm_dev_ref);
690 * drm_dev_unref - Drop reference of a DRM device
691 * @dev: device to drop reference of or NULL
693 * This decreases the ref-count of @dev by one. The device is destroyed if the
694 * ref-count drops to zero.
696 void drm_dev_unref(struct drm_device *dev)
699 kref_put(&dev->ref, drm_dev_release);
701 EXPORT_SYMBOL(drm_dev_unref);
704 * drm_dev_register - Register DRM device
705 * @dev: Device to register
706 * @flags: Flags passed to the driver's .load() function
708 * Register the DRM device @dev with the system, advertise device to user-space
709 * and start normal device operation. @dev must be allocated via drm_dev_alloc()
710 * previously. Right after drm_dev_register() the driver should call
711 * drm_connector_register_all() to register all connectors in sysfs. This is
712 * a separate call for backward compatibility with drivers still using
713 * the deprecated ->load() callback, where connectors are registered from within
714 * the ->load() callback.
716 * Never call this twice on any device!
718 * NOTE: To ensure backward compatibility with existing drivers method this
719 * function calls the ->load() method after registering the device nodes,
720 * creating race conditions. Usage of the ->load() methods is therefore
721 * deprecated, drivers must perform all initialization before calling
722 * drm_dev_register().
725 * 0 on success, negative error code on failure.
727 int drm_dev_register(struct drm_device *dev, unsigned long flags)
731 mutex_lock(&drm_global_mutex);
733 ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
737 ret = drm_minor_register(dev, DRM_MINOR_RENDER);
741 ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
745 if (dev->driver->load) {
746 ret = dev->driver->load(dev, flags);
755 drm_minor_unregister(dev, DRM_MINOR_LEGACY);
756 drm_minor_unregister(dev, DRM_MINOR_RENDER);
757 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
759 mutex_unlock(&drm_global_mutex);
762 EXPORT_SYMBOL(drm_dev_register);
765 * drm_dev_unregister - Unregister DRM device
766 * @dev: Device to unregister
768 * Unregister the DRM device from the system. This does the reverse of
769 * drm_dev_register() but does not deallocate the device. The caller must call
770 * drm_dev_unref() to drop their final reference.
772 * This should be called first in the device teardown code to make sure
773 * userspace can't access the device instance any more.
775 void drm_dev_unregister(struct drm_device *dev)
777 struct drm_map_list *r_list, *list_temp;
781 if (dev->driver->unload)
782 dev->driver->unload(dev);
785 drm_pci_agp_destroy(dev);
787 drm_vblank_cleanup(dev);
789 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
790 drm_legacy_rmmap(dev, r_list->map);
792 drm_minor_unregister(dev, DRM_MINOR_LEGACY);
793 drm_minor_unregister(dev, DRM_MINOR_RENDER);
794 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
796 EXPORT_SYMBOL(drm_dev_unregister);
799 * drm_dev_set_unique - Set the unique name of a DRM device
800 * @dev: device of which to set the unique name
803 * Sets the unique name of a DRM device using the specified string. Drivers
804 * can use this at driver probe time if the unique name of the devices they
807 * Return: 0 on success or a negative error code on failure.
809 int drm_dev_set_unique(struct drm_device *dev, const char *name)
812 dev->unique = kstrdup(name, GFP_KERNEL);
814 return dev->unique ? 0 : -ENOMEM;
816 EXPORT_SYMBOL(drm_dev_set_unique);
821 * The DRM core module initializes all global DRM objects and makes them
822 * available to drivers. Once setup, drivers can probe their respective
824 * Currently, core management includes:
825 * - The "DRM-Global" key/value database
826 * - Global ID management for connectors
827 * - DRM major number allocation
828 * - DRM minor management
832 * Furthermore, the DRM core provides dynamic char-dev lookups. For each
833 * interface registered on a DRM device, you can request minor numbers from DRM
834 * core. DRM core takes care of major-number management and char-dev
835 * registration. A stub ->open() callback forwards any open() requests to the
840 static int drm_stub_open(struct inode *inode, struct file *filp)
842 const struct file_operations *new_fops;
843 struct drm_minor *minor;
848 mutex_lock(&drm_global_mutex);
849 minor = drm_minor_acquire(iminor(inode));
851 err = PTR_ERR(minor);
855 new_fops = fops_get(minor->dev->driver->fops);
861 replace_fops(filp, new_fops);
862 if (filp->f_op->open)
863 err = filp->f_op->open(inode, filp);
868 drm_minor_release(minor);
870 mutex_unlock(&drm_global_mutex);
874 static const struct file_operations drm_stub_fops = {
875 .owner = THIS_MODULE,
876 .open = drm_stub_open,
877 .llseek = noop_llseek,
880 static int __init drm_core_init(void)
885 drm_connector_ida_init();
886 idr_init(&drm_minors_idr);
888 if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
891 ret = drm_sysfs_init();
893 printk(KERN_ERR "DRM: Error creating drm class.\n");
897 drm_debugfs_root = debugfs_create_dir("dri", NULL);
898 if (!drm_debugfs_root) {
899 DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
904 DRM_INFO("Initialized %s %d.%d.%d %s\n",
905 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
910 unregister_chrdev(DRM_MAJOR, "drm");
912 idr_destroy(&drm_minors_idr);
917 static void __exit drm_core_exit(void)
919 debugfs_remove(drm_debugfs_root);
922 unregister_chrdev(DRM_MAJOR, "drm");
924 drm_connector_ida_destroy();
925 idr_destroy(&drm_minors_idr);
928 module_init(drm_core_init);
929 module_exit(drm_core_exit);
932 #include <sys/devfs.h>
934 #include <linux/export.h>
935 #include <linux/dmi.h>
936 #include <drm/drmP.h>
937 #include <drm/drm_core.h>
939 static int drm_load(struct drm_device *dev);
940 drm_pci_id_list_t *drm_find_description(int vendor, int device,
941 drm_pci_id_list_t *idlist);
943 #define DRIVER_SOFTC(unit) \
944 ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
947 drm_modevent(module_t mod, int type, void *data)
952 TUNABLE_INT_FETCH("drm.debug", &drm_debug);
958 static moduledata_t drm_mod = {
963 DECLARE_MODULE(drm, drm_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
964 MODULE_VERSION(drm, 1);
965 MODULE_DEPEND(drm, agp, 1, 1, 1);
966 MODULE_DEPEND(drm, pci, 1, 1, 1);
967 MODULE_DEPEND(drm, iicbus, 1, 1, 1);
969 static struct dev_ops drm_cdevsw = {
970 { "drm", 0, D_TRACKCLOSE | D_MPSAFE },
972 .d_close = drm_close,
974 .d_ioctl = drm_ioctl,
975 .d_kqfilter = drm_kqfilter,
977 .d_mmap_single = drm_mmap_single,
980 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device");
981 SYSCTL_INT(_hw_drm, OID_AUTO, debug, CTLFLAG_RW, &drm_debug, 0,
984 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist)
986 drm_pci_id_list_t *id_entry;
989 vendor = pci_get_vendor(kdev);
990 device = pci_get_device(kdev);
992 if (pci_get_class(kdev) != PCIC_DISPLAY)
995 id_entry = drm_find_description(vendor, device, idlist);
996 if (id_entry != NULL) {
997 if (!device_get_desc(kdev)) {
998 device_set_desc(kdev, id_entry->name);
999 DRM_DEBUG("desc : %s\n", device_get_desc(kdev));
1007 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist)
1009 struct drm_device *dev;
1010 drm_pci_id_list_t *id_entry;
1013 unit = device_get_unit(kdev);
1014 dev = device_get_softc(kdev);
1016 /* Initialize Linux struct device */
1017 dev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1019 if (!strcmp(device_get_name(kdev), "drmsub"))
1020 dev->dev->bsddev = device_get_parent(kdev);
1022 dev->dev->bsddev = kdev;
1024 dev->pci_domain = pci_get_domain(dev->dev->bsddev);
1025 dev->pci_bus = pci_get_bus(dev->dev->bsddev);
1026 dev->pci_slot = pci_get_slot(dev->dev->bsddev);
1027 dev->pci_func = pci_get_function(dev->dev->bsddev);
1028 drm_init_pdev(dev->dev->bsddev, &dev->pdev);
1030 id_entry = drm_find_description(dev->pdev->vendor,
1031 dev->pdev->device, idlist);
1032 dev->id_entry = id_entry;
1034 /* Print the contents of pdev struct. */
1035 drm_print_pdev(dev->pdev);
1037 lockinit(&dev->dev_lock, "drmdev", 0, LK_CANRECURSE);
1038 lwkt_serialize_init(&dev->irq_lock);
1039 lockinit(&dev->event_lock, "drmev", 0, LK_CANRECURSE);
1040 lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE);
1042 error = drm_load(dev);
1046 error = drm_create_cdevs(kdev);
1053 drm_create_cdevs(device_t kdev)
1055 struct drm_device *dev;
1058 unit = device_get_unit(kdev);
1059 dev = device_get_softc(kdev);
1061 dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID,
1062 DRM_DEV_MODE, "dri/card%d", unit);
1065 dev->devnode->si_drv1 = dev;
1069 #ifndef DRM_DEV_NAME
1070 #define DRM_DEV_NAME "drm"
1073 devclass_t drm_devclass;
1075 drm_pci_id_list_t *drm_find_description(int vendor, int device,
1076 drm_pci_id_list_t *idlist)
1080 for (i = 0; idlist[i].vendor != 0; i++) {
1081 if ((idlist[i].vendor == vendor) &&
1082 ((idlist[i].device == device) ||
1083 (idlist[i].device == 0))) {
1090 static int drm_load(struct drm_device *dev)
1096 INIT_LIST_HEAD(&dev->maplist);
1098 drm_sysctl_init(dev);
1099 INIT_LIST_HEAD(&dev->filelist);
1102 dev->types[0] = _DRM_STAT_LOCK;
1103 dev->types[1] = _DRM_STAT_OPENS;
1104 dev->types[2] = _DRM_STAT_CLOSES;
1105 dev->types[3] = _DRM_STAT_IOCTLS;
1106 dev->types[4] = _DRM_STAT_LOCKS;
1107 dev->types[5] = _DRM_STAT_UNLOCKS;
1109 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
1110 atomic_set(&dev->counts[i], 0);
1112 INIT_LIST_HEAD(&dev->vblank_event_list);
1114 if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
1115 if (drm_pci_device_is_agp(dev))
1116 dev->agp = drm_agp_init(dev);
1119 if (dev->driver->driver_features & DRIVER_GEM) {
1120 retcode = drm_gem_init(dev);
1122 DRM_ERROR("Cannot initialize graphics execution "
1128 if (dev->driver->load != NULL) {
1130 /* Shared code returns -errno. */
1131 retcode = -dev->driver->load(dev,
1132 dev->id_entry->driver_private);
1133 if (pci_enable_busmaster(dev->dev->bsddev))
1134 DRM_ERROR("Request to enable bus-master failed.\n");
1140 DRM_INFO("Initialized %s %d.%d.%d %s\n",
1144 dev->driver->patchlevel,
1150 drm_gem_destroy(dev);
1151 drm_sysctl_cleanup(dev);
1155 if (dev->devnode != NULL)
1156 destroy_dev(dev->devnode);
1158 lockuninit(&dev->vbl_lock);
1159 lockuninit(&dev->dev_lock);
1160 lockuninit(&dev->event_lock);
1161 lockuninit(&dev->struct_mutex);
1167 * Stub is needed for devfs
1169 int drm_close(struct dev_close_args *ap)
1174 /* XXX: this is supposed to be drm_release() */
1175 void drm_cdevpriv_dtor(void *cd)
1177 struct drm_file *file_priv = cd;
1178 struct drm_device *dev = file_priv->dev;
1180 DRM_DEBUG("open_count = %d\n", dev->open_count);
1184 if (dev->driver->preclose != NULL)
1185 dev->driver->preclose(dev, file_priv);
1187 /* ========================================================
1188 * Begin inline drm_release
1191 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
1192 DRM_CURRENTPID, (long)dev->dev, dev->open_count);
1194 if (dev->driver->driver_features & DRIVER_GEM)
1195 drm_gem_release(dev, file_priv);
1197 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1198 drm_legacy_reclaim_buffers(dev, file_priv);
1200 funsetown(&dev->buf_sigio);
1202 if (dev->driver->postclose != NULL)
1203 dev->driver->postclose(dev, file_priv);
1204 list_del(&file_priv->lhead);
1207 /* ========================================================
1208 * End inline drm_release
1211 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
1212 device_unbusy(dev->dev->bsddev);
1213 if (--dev->open_count == 0) {
1221 drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx,
1222 struct sysctl_oid *top)
1224 struct sysctl_oid *oid;
1226 ksnprintf(dev->busid_str, sizeof(dev->busid_str),
1227 "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus,
1228 dev->pci_slot, dev->pci_func);
1229 oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid",
1230 CTLFLAG_RD, dev->busid_str, 0, NULL);
1233 dev->modesetting = (dev->driver->driver_features & DRIVER_MODESET) != 0;
1234 oid = SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(top), OID_AUTO,
1235 "modesetting", CTLFLAG_RD, &dev->modesetting, 0, NULL);
1243 drm_mmap_single(struct dev_mmap_single_args *ap)
1245 struct drm_device *dev;
1246 struct cdev *kdev = ap->a_head.a_dev;
1247 vm_ooffset_t *offset = ap->a_offset;
1248 vm_size_t size = ap->a_size;
1249 struct vm_object **obj_res = ap->a_object;
1250 int nprot = ap->a_nprot;
1252 dev = drm_get_device_from_kdev(kdev);
1253 if (dev->drm_ttm_bdev != NULL) {
1254 return (ttm_bo_mmap_single(dev->drm_ttm_bdev, offset, size,
1256 } else if ((dev->driver->driver_features & DRIVER_GEM) != 0) {
1257 return (drm_gem_mmap_single(dev, offset, size, obj_res, nprot));
1264 drm_core_init(void *arg)
1269 DRM_INFO("Initialized %s %d.%d.%d %s\n",
1270 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
1275 drm_core_exit(void *arg)
1278 drm_global_release();
1281 SYSINIT(drm_register, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
1282 drm_core_init, NULL);
1283 SYSUNINIT(drm_unregister, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
1284 drm_core_exit, NULL);
1287 #include <linux/dmi.h>
1290 * Check if dmi_system_id structure matches system DMI data
1293 dmi_found(const struct dmi_system_id *dsi)
1297 char *sys_vendor, *board_vendor, *product_name, *board_name;
1299 sys_vendor = kgetenv("smbios.system.maker");
1300 board_vendor = kgetenv("smbios.planar.maker");
1301 product_name = kgetenv("smbios.system.product");
1302 board_name = kgetenv("smbios.planar.product");
1304 for (i = 0; i < NELEM(dsi->matches); i++) {
1305 slot = dsi->matches[i].slot;
1309 case DMI_SYS_VENDOR:
1310 if (sys_vendor != NULL &&
1311 !strcmp(sys_vendor, dsi->matches[i].substr))
1315 case DMI_BOARD_VENDOR:
1316 if (board_vendor != NULL &&
1317 !strcmp(board_vendor, dsi->matches[i].substr))
1321 case DMI_PRODUCT_NAME:
1322 if (product_name != NULL &&
1323 !strcmp(product_name, dsi->matches[i].substr))
1327 case DMI_BOARD_NAME:
1328 if (board_name != NULL &&
1329 !strcmp(board_name, dsi->matches[i].substr))
1340 if (sys_vendor != NULL)
1341 kfreeenv(sys_vendor);
1342 if (board_vendor != NULL)
1343 kfreeenv(board_vendor);
1344 if (product_name != NULL)
1345 kfreeenv(product_name);
1346 if (board_name != NULL)
1347 kfreeenv(board_name);
1352 int dmi_check_system(const struct dmi_system_id *sysid)
1354 const struct dmi_system_id *dsi;
1357 for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) {
1358 if (dmi_found(dsi)) {
1360 if (dsi->callback && dsi->callback(dsi))