5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
9 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
11 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
37 #include <drm/drm_core.h>
38 #include <linux/slab.h>
40 unsigned int drm_debug
= 0; /* 1 to enable debug output */
41 EXPORT_SYMBOL(drm_debug
);
43 unsigned int drm_rnodes
= 0; /* 1 to enable experimental render nodes API */
44 EXPORT_SYMBOL(drm_rnodes
);
46 /* 1 to allow user space to request universal planes (experimental) */
47 unsigned int drm_universal_planes
= 0;
48 EXPORT_SYMBOL(drm_universal_planes
);
50 unsigned int drm_vblank_offdelay
= 5000; /* Default to 5000 msecs. */
51 EXPORT_SYMBOL(drm_vblank_offdelay
);
53 unsigned int drm_timestamp_precision
= 20; /* Default to 20 usecs. */
54 EXPORT_SYMBOL(drm_timestamp_precision
);
57 * Default to use monotonic timestamps for wait-for-vblank and page-flip
60 unsigned int drm_timestamp_monotonic
= 1;
62 MODULE_AUTHOR(CORE_AUTHOR
);
63 MODULE_DESCRIPTION(CORE_DESC
);
64 MODULE_PARM_DESC(debug
, "Enable debug output");
65 MODULE_PARM_DESC(rnodes
, "Enable experimental render nodes API");
66 MODULE_PARM_DESC(vblankoffdelay
, "Delay until vblank irq auto-disable [msecs]");
67 MODULE_PARM_DESC(timestamp_precision_usec
, "Max. error on timestamps [usecs]");
68 MODULE_PARM_DESC(timestamp_monotonic
, "Use monotonic timestamps");
70 module_param_named(debug
, drm_debug
, int, 0600);
71 module_param_named(rnodes
, drm_rnodes
, int, 0600);
72 module_param_named(universal_planes
, drm_universal_planes
, int, 0600);
73 module_param_named(vblankoffdelay
, drm_vblank_offdelay
, int, 0600);
74 module_param_named(timestamp_precision_usec
, drm_timestamp_precision
, int, 0600);
75 module_param_named(timestamp_monotonic
, drm_timestamp_monotonic
, int, 0600);
77 struct idr drm_minors_idr
;
79 struct class *drm_class
;
80 struct dentry
*drm_debugfs_root
;
82 int drm_err(const char *func
, const char *format
, ...)
89 va_start(args
, format
);
94 r
= printk(KERN_ERR
"[" DRM_NAME
":%s] *ERROR* %pV", func
, &vaf
);
102 EXPORT_SYMBOL(drm_err
);
104 void drm_ut_debug_printk(unsigned int request_level
,
106 const char *function_name
,
107 const char *format
, ...)
110 struct va_format vaf
;
113 if (drm_debug
& request_level
) {
114 va_start(args
, format
);
119 printk(KERN_DEBUG
"[%s:%s], %pV", prefix
,
120 function_name
, &vaf
);
122 printk(KERN_DEBUG
"%pV", &vaf
);
127 EXPORT_SYMBOL(drm_ut_debug_printk
);
130 static int drm_minor_get_id(struct drm_device
*dev
, int type
)
133 int base
= 0, limit
= 63;
135 if (type
== DRM_MINOR_CONTROL
) {
138 } else if (type
== DRM_MINOR_RENDER
) {
143 mutex_lock(&dev
->struct_mutex
);
144 ret
= idr_alloc(&drm_minors_idr
, NULL
, base
, limit
, GFP_KERNEL
);
145 mutex_unlock(&dev
->struct_mutex
);
147 return ret
== -ENOSPC
? -EINVAL
: ret
;
150 struct drm_master
*drm_master_create(struct drm_minor
*minor
)
152 struct drm_master
*master
;
154 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
158 kref_init(&master
->refcount
);
159 spin_lock_init(&master
->lock
.spinlock
);
160 init_waitqueue_head(&master
->lock
.lock_queue
);
161 drm_ht_create(&master
->magiclist
, DRM_MAGIC_HASH_ORDER
);
162 INIT_LIST_HEAD(&master
->magicfree
);
163 master
->minor
= minor
;
165 list_add_tail(&master
->head
, &minor
->master_list
);
170 struct drm_master
*drm_master_get(struct drm_master
*master
)
172 kref_get(&master
->refcount
);
175 EXPORT_SYMBOL(drm_master_get
);
177 static void drm_master_destroy(struct kref
*kref
)
179 struct drm_master
*master
= container_of(kref
, struct drm_master
, refcount
);
180 struct drm_magic_entry
*pt
, *next
;
181 struct drm_device
*dev
= master
->minor
->dev
;
182 struct drm_map_list
*r_list
, *list_temp
;
184 list_del(&master
->head
);
186 if (dev
->driver
->master_destroy
)
187 dev
->driver
->master_destroy(dev
, master
);
189 list_for_each_entry_safe(r_list
, list_temp
, &dev
->maplist
, head
) {
190 if (r_list
->master
== master
) {
191 drm_rmmap_locked(dev
, r_list
->map
);
196 if (master
->unique
) {
197 kfree(master
->unique
);
198 master
->unique
= NULL
;
199 master
->unique_len
= 0;
205 list_for_each_entry_safe(pt
, next
, &master
->magicfree
, head
) {
207 drm_ht_remove_item(&master
->magiclist
, &pt
->hash_item
);
211 drm_ht_remove(&master
->magiclist
);
216 void drm_master_put(struct drm_master
**master
)
218 kref_put(&(*master
)->refcount
, drm_master_destroy
);
221 EXPORT_SYMBOL(drm_master_put
);
224 int drm_setmaster_ioctl(struct drm_device
*dev
, void *data
,
225 struct drm_file
*file_priv
)
227 DRM_DEBUG("setmaster\n");
229 if (file_priv
->master
!= 0)
235 int drm_dropmaster_ioctl(struct drm_device
*dev
, void *data
,
236 struct drm_file
*file_priv
)
238 DRM_DEBUG("dropmaster\n");
239 if (file_priv
->master
!= 0)
246 * drm_get_minor - Allocate and register new DRM minor
248 * @minor: Pointer to where new minor is stored
249 * @type: Type of minor
251 * Allocate a new minor of the given type and register it. A pointer to the new
252 * minor is returned in @minor.
253 * Caller must hold the global DRM mutex.
256 * 0 on success, negative error code on failure.
258 static int drm_get_minor(struct drm_device
*dev
, struct drm_minor
**minor
,
261 struct drm_minor
*new_minor
;
267 minor_id
= drm_minor_get_id(dev
, type
);
271 new_minor
= kzalloc(sizeof(struct drm_minor
), GFP_KERNEL
);
277 new_minor
->type
= type
;
278 new_minor
->device
= MKDEV(DRM_MAJOR
, minor_id
);
279 new_minor
->dev
= dev
;
280 new_minor
->index
= minor_id
;
281 INIT_LIST_HEAD(&new_minor
->master_list
);
283 idr_replace(&drm_minors_idr
, new_minor
, minor_id
);
285 #if defined(CONFIG_DEBUG_FS)
286 ret
= drm_debugfs_init(new_minor
, minor_id
, drm_debugfs_root
);
288 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
293 ret
= drm_sysfs_device_add(new_minor
);
296 "DRM: Error sysfs_device_add.\n");
301 DRM_DEBUG("new minor assigned %d\n", minor_id
);
306 #if defined(CONFIG_DEBUG_FS)
307 drm_debugfs_cleanup(new_minor
);
312 idr_remove(&drm_minors_idr
, minor_id
);
318 * drm_unplug_minor - Unplug DRM minor
319 * @minor: Minor to unplug
321 * Unplugs the given DRM minor but keeps the object. So after this returns,
322 * minor->dev is still valid so existing open-files can still access it to get
323 * device information from their drm_file ojects.
324 * If the minor is already unplugged or if @minor is NULL, nothing is done.
325 * The global DRM mutex must be held by the caller.
327 static void drm_unplug_minor(struct drm_minor
*minor
)
329 if (!minor
|| !minor
->kdev
)
332 #if defined(CONFIG_DEBUG_FS)
333 drm_debugfs_cleanup(minor
);
336 drm_sysfs_device_remove(minor
);
337 idr_remove(&drm_minors_idr
, minor
->index
);
341 * drm_put_minor - Destroy DRM minor
342 * @minor: Minor to destroy
344 * This calls drm_unplug_minor() on the given minor and then frees it. Nothing
345 * is done if @minor is NULL. It is fine to call this on already unplugged
347 * The global DRM mutex must be held by the caller.
349 static void drm_put_minor(struct drm_minor
*minor
)
354 DRM_DEBUG("release secondary minor %d\n", minor
->index
);
356 drm_unplug_minor(minor
);
361 * Called via drm_exit() at module unload time or when pci device is
364 * Cleans up all DRM device, calling drm_lastclose().
367 void drm_put_dev(struct drm_device
*dev
)
372 DRM_ERROR("cleanup called no dev\n");
376 drm_dev_unregister(dev
);
379 EXPORT_SYMBOL(drm_put_dev
);
381 void drm_unplug_dev(struct drm_device
*dev
)
383 /* for a USB device */
384 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
385 drm_unplug_minor(dev
->control
);
387 drm_unplug_minor(dev
->render
);
388 drm_unplug_minor(dev
->primary
);
390 mutex_lock(&drm_global_mutex
);
392 drm_device_set_unplugged(dev
);
394 if (dev
->open_count
== 0) {
397 mutex_unlock(&drm_global_mutex
);
399 EXPORT_SYMBOL(drm_unplug_dev
);
405 * We want to be able to allocate our own "struct address_space" to control
406 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
407 * stand-alone address_space objects, so we need an underlying inode. As there
408 * is no way to allocate an independent inode easily, we need a fake internal
411 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
412 * frees it again. You are allowed to use iget() and iput() to get references to
413 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
414 * drm_fs_inode_free() call (which does not have to be the last iput()).
415 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
416 * between multiple inode-users. You could, technically, call
417 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
418 * iput(), but this way you'd end up with a new vfsmount for each inode.
421 static int drm_fs_cnt
;
422 static struct vfsmount
*drm_fs_mnt
;
424 static const struct dentry_operations drm_fs_dops
= {
425 .d_dname
= simple_dname
,
428 static const struct super_operations drm_fs_sops
= {
429 .statfs
= simple_statfs
,
432 static struct dentry
*drm_fs_mount(struct file_system_type
*fs_type
, int flags
,
433 const char *dev_name
, void *data
)
435 return mount_pseudo(fs_type
,
442 static struct file_system_type drm_fs_type
= {
444 .owner
= THIS_MODULE
,
445 .mount
= drm_fs_mount
,
446 .kill_sb
= kill_anon_super
,
449 static struct inode
*drm_fs_inode_new(void)
454 r
= simple_pin_fs(&drm_fs_type
, &drm_fs_mnt
, &drm_fs_cnt
);
456 DRM_ERROR("Cannot mount pseudo fs: %d\n", r
);
460 inode
= alloc_anon_inode(drm_fs_mnt
->mnt_sb
);
462 simple_release_fs(&drm_fs_mnt
, &drm_fs_cnt
);
467 static void drm_fs_inode_free(struct inode
*inode
)
471 simple_release_fs(&drm_fs_mnt
, &drm_fs_cnt
);
476 * drm_dev_alloc - Allocate new drm device
477 * @driver: DRM driver to allocate device for
478 * @parent: Parent device object
480 * Allocate and initialize a new DRM device. No device registration is done.
481 * Call drm_dev_register() to advertice the device to user space and register it
482 * with other core subsystems.
485 * Pointer to new DRM device, or NULL if out of memory.
487 struct drm_device
*drm_dev_alloc(struct drm_driver
*driver
,
488 struct device
*parent
)
490 struct drm_device
*dev
;
493 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
498 dev
->driver
= driver
;
500 INIT_LIST_HEAD(&dev
->filelist
);
501 INIT_LIST_HEAD(&dev
->ctxlist
);
502 INIT_LIST_HEAD(&dev
->vmalist
);
503 INIT_LIST_HEAD(&dev
->maplist
);
504 INIT_LIST_HEAD(&dev
->vblank_event_list
);
506 spin_lock_init(&dev
->count_lock
);
507 spin_lock_init(&dev
->event_lock
);
508 mutex_init(&dev
->struct_mutex
);
509 mutex_init(&dev
->ctxlist_mutex
);
511 if (drm_ht_create(&dev
->map_hash
, 12))
514 ret
= drm_ctxbitmap_init(dev
);
516 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
520 if (driver
->driver_features
& DRIVER_GEM
) {
521 ret
= drm_gem_init(dev
);
523 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
531 drm_ctxbitmap_cleanup(dev
);
533 drm_ht_remove(&dev
->map_hash
);
538 EXPORT_SYMBOL(drm_dev_alloc
);
541 * drm_dev_free - Free DRM device
542 * @dev: DRM device to free
544 * Free a DRM device that has previously been allocated via drm_dev_alloc().
545 * You must not use kfree() instead or you will leak memory.
547 * This must not be called once the device got registered. Use drm_put_dev()
548 * instead, which then calls drm_dev_free().
550 void drm_dev_free(struct drm_device
*dev
)
552 drm_put_minor(dev
->control
);
553 drm_put_minor(dev
->render
);
554 drm_put_minor(dev
->primary
);
556 if (dev
->driver
->driver_features
& DRIVER_GEM
)
557 drm_gem_destroy(dev
);
559 drm_ctxbitmap_cleanup(dev
);
560 drm_ht_remove(&dev
->map_hash
);
565 EXPORT_SYMBOL(drm_dev_free
);
568 * drm_dev_register - Register DRM device
569 * @dev: Device to register
571 * Register the DRM device @dev with the system, advertise device to user-space
572 * and start normal device operation. @dev must be allocated via drm_dev_alloc()
575 * Never call this twice on any device!
578 * 0 on success, negative error code on failure.
580 int drm_dev_register(struct drm_device
*dev
, unsigned long flags
)
584 mutex_lock(&drm_global_mutex
);
586 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
587 ret
= drm_get_minor(dev
, &dev
->control
, DRM_MINOR_CONTROL
);
592 if (drm_core_check_feature(dev
, DRIVER_RENDER
) && drm_rnodes
) {
593 ret
= drm_get_minor(dev
, &dev
->render
, DRM_MINOR_RENDER
);
595 goto err_control_node
;
598 ret
= drm_get_minor(dev
, &dev
->primary
, DRM_MINOR_LEGACY
);
600 goto err_render_node
;
602 if (dev
->driver
->load
) {
603 ret
= dev
->driver
->load(dev
, flags
);
605 goto err_primary_node
;
608 /* setup grouping for legacy outputs */
609 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
610 ret
= drm_mode_group_init_legacy_group(dev
,
611 &dev
->primary
->mode_group
);
620 if (dev
->driver
->unload
)
621 dev
->driver
->unload(dev
);
623 drm_unplug_minor(dev
->primary
);
625 drm_unplug_minor(dev
->render
);
627 drm_unplug_minor(dev
->control
);
629 mutex_unlock(&drm_global_mutex
);
632 EXPORT_SYMBOL(drm_dev_register
);
635 * drm_dev_unregister - Unregister DRM device
636 * @dev: Device to unregister
638 * Unregister the DRM device from the system. This does the reverse of
639 * drm_dev_register() but does not deallocate the device. The caller must call
640 * drm_dev_free() to free all resources.
642 void drm_dev_unregister(struct drm_device
*dev
)
644 struct drm_map_list
*r_list
, *list_temp
;
648 if (dev
->driver
->unload
)
649 dev
->driver
->unload(dev
);
652 drm_pci_agp_destroy(dev
);
654 drm_vblank_cleanup(dev
);
656 list_for_each_entry_safe(r_list
, list_temp
, &dev
->maplist
, head
)
657 drm_rmmap(dev
, r_list
->map
);
659 drm_unplug_minor(dev
->control
);
660 drm_unplug_minor(dev
->render
);
661 drm_unplug_minor(dev
->primary
);
663 EXPORT_SYMBOL(drm_dev_unregister
);