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>
39 unsigned int drm_debug = 0; /* 1 to enable debug output */
40 EXPORT_SYMBOL(drm_debug);
42 unsigned int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
43 EXPORT_SYMBOL(drm_vblank_offdelay);
45 unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
46 EXPORT_SYMBOL(drm_timestamp_precision);
49 * Default to use monotonic timestamps for wait-for-vblank and page-flip
52 unsigned int drm_timestamp_monotonic = 1;
54 MODULE_AUTHOR(CORE_AUTHOR);
55 MODULE_DESCRIPTION(CORE_DESC);
56 MODULE_LICENSE("GPL and additional rights");
57 MODULE_PARM_DESC(debug, "Enable debug output");
58 MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs]");
59 MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
60 MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
62 module_param_named(debug, drm_debug, int, 0600);
63 module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
64 module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
65 module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
67 struct idr drm_minors_idr;
69 struct class *drm_class;
70 struct proc_dir_entry *drm_proc_root;
71 struct dentry *drm_debugfs_root;
73 int drm_err(const char *func, const char *format, ...)
80 va_start(args, format);
85 r = printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* %pV", func, &vaf);
93 EXPORT_SYMBOL(drm_err);
96 void drm_ut_debug_printk(unsigned int request_level,
98 const char *function_name,
99 const char *format, ...)
103 if (drm_debug & request_level) {
105 printk(KERN_DEBUG "[%s:%s], ", prefix, function_name);
106 va_start(args, format);
107 vprintk(format, args);
111 EXPORT_SYMBOL(drm_ut_debug_printk);
114 static int drm_minor_get_id(struct drm_device *dev, int type)
118 int base = 0, limit = 63;
120 if (type == DRM_MINOR_CONTROL) {
123 } else if (type == DRM_MINOR_RENDER) {
129 if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
130 DRM_ERROR("Out of memory expanding drawable idr\n");
133 mutex_lock(&dev->struct_mutex);
134 ret = idr_get_new_above(&drm_minors_idr, NULL,
136 mutex_unlock(&dev->struct_mutex);
143 if (new_id >= limit) {
144 idr_remove(&drm_minors_idr, new_id);
150 struct drm_master *drm_master_create(struct drm_minor *minor)
152 struct drm_master *master;
154 master = kmalloc(sizeof(*master), M_DRM, M_WAITOK | M_ZERO);
158 kref_init(&master->refcount);
159 spin_init(&master->lock.spinlock, "drmmls");
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, M_DRM);
198 master->unique = NULL;
199 master->unique_len = 0;
203 kfree(dev->devname, M_DRM);
207 list_for_each_entry_safe(pt, next, &master->magicfree, head) {
209 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
213 drm_ht_remove(&master->magiclist);
215 kfree(master, M_DRM);
218 void drm_master_put(struct drm_master **master)
220 kref_put(&(*master)->refcount, drm_master_destroy);
223 EXPORT_SYMBOL(drm_master_put);
225 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
226 struct drm_file *file_priv)
230 if (file_priv->is_master)
233 if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
236 if (!file_priv->master)
239 if (file_priv->minor->master)
242 mutex_lock(&dev->struct_mutex);
243 file_priv->minor->master = drm_master_get(file_priv->master);
244 file_priv->is_master = 1;
245 if (dev->driver->master_set) {
246 ret = dev->driver->master_set(dev, file_priv, false);
247 if (unlikely(ret != 0)) {
248 file_priv->is_master = 0;
249 drm_master_put(&file_priv->minor->master);
252 mutex_unlock(&dev->struct_mutex);
257 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
258 struct drm_file *file_priv)
260 if (!file_priv->is_master)
263 if (!file_priv->minor->master)
266 mutex_lock(&dev->struct_mutex);
267 if (dev->driver->master_drop)
268 dev->driver->master_drop(dev, file_priv, false);
269 drm_master_put(&file_priv->minor->master);
270 file_priv->is_master = 0;
271 mutex_unlock(&dev->struct_mutex);
276 int drm_fill_in_dev(struct drm_device *dev,
277 const struct pci_device_id *ent,
278 struct drm_driver *driver)
282 INIT_LIST_HEAD(&dev->filelist);
283 INIT_LIST_HEAD(&dev->ctxlist);
284 INIT_LIST_HEAD(&dev->vmalist);
285 INIT_LIST_HEAD(&dev->maplist);
286 INIT_LIST_HEAD(&dev->vblank_event_list);
288 spin_lock_init(&dev->count_lock);
289 spin_lock_init(&dev->event_lock);
290 mutex_init(&dev->struct_mutex);
291 mutex_init(&dev->ctxlist_mutex);
293 if (drm_ht_create(&dev->map_hash, 12)) {
297 /* the DRM has 6 basic counters */
299 dev->types[0] = _DRM_STAT_LOCK;
300 dev->types[1] = _DRM_STAT_OPENS;
301 dev->types[2] = _DRM_STAT_CLOSES;
302 dev->types[3] = _DRM_STAT_IOCTLS;
303 dev->types[4] = _DRM_STAT_LOCKS;
304 dev->types[5] = _DRM_STAT_UNLOCKS;
306 dev->driver = driver;
308 if (dev->driver->bus->agp_init) {
309 retcode = dev->driver->bus->agp_init(dev);
311 goto error_out_unreg;
316 retcode = drm_ctxbitmap_init(dev);
318 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
319 goto error_out_unreg;
322 if (driver->driver_features & DRIVER_GEM) {
323 retcode = drm_gem_init(dev);
325 DRM_ERROR("Cannot initialize graphics execution "
327 goto error_out_unreg;
337 EXPORT_SYMBOL(drm_fill_in_dev);
340 extern struct dev_ops drm_cdevsw;
343 * Get a secondary minor number.
345 * \param dev device data structure
346 * \param sec-minor structure to hold the assigned minor
347 * \return negative number on failure.
349 * Search an empty entry and initialize it to the given parameters, and
350 * create the proc init entry via proc_init(). This routines assigns
351 * minor numbers to secondary heads of multi-headed cards
353 int drm_get_minor(device_t kdev, struct drm_device *dev, struct drm_minor **minor, int type)
355 struct drm_minor *new_minor;
362 minor_id = drm_minor_get_id(dev, type);
366 new_minor = kmalloc(sizeof(struct drm_minor), M_DRM, M_WAITOK | M_ZERO);
372 new_minor->type = type;
374 new_minor->device = MKDEV(DRM_MAJOR, minor_id);
376 unit = device_get_unit(kdev);
377 new_minor->device = make_dev(&drm_cdevsw, minor_id, DRM_DEV_UID, DRM_DEV_GID,
378 DRM_DEV_MODE, "dri/card%d", unit);
379 new_minor->device->si_drv1 = dev;
382 new_minor->dev = dev;
383 new_minor->index = minor_id;
384 INIT_LIST_HEAD(&new_minor->master_list);
386 idr_replace(&drm_minors_idr, new_minor, minor_id);
389 if (type == DRM_MINOR_LEGACY) {
390 ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
392 DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
396 new_minor->proc_root = NULL;
399 #if defined(CONFIG_DEBUG_FS)
400 ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
402 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
408 ret = drm_sysfs_device_add(new_minor);
411 "DRM: Error sysfs_device_add.\n");
417 DRM_DEBUG("new minor assigned %d\n", minor_id);
423 if (new_minor->type == DRM_MINOR_LEGACY)
424 drm_proc_cleanup(new_minor, drm_proc_root);
426 kfree(new_minor, M_DRM);
429 idr_remove(&drm_minors_idr, minor_id);
433 EXPORT_SYMBOL(drm_get_minor);
437 * Put a secondary minor number.
439 * \param sec_minor - structure to be released
440 * \return always zero
442 * Cleans up the proc resources. Not legal for this to be the
443 * last minor released.
446 int drm_put_minor(struct drm_minor **minor_p)
448 struct drm_minor *minor = *minor_p;
450 DRM_DEBUG("release secondary minor %d\n", minor->index);
452 if (minor->type == DRM_MINOR_LEGACY)
453 drm_proc_cleanup(minor, drm_proc_root);
454 #if defined(CONFIG_DEBUG_FS)
455 drm_debugfs_cleanup(minor);
458 drm_sysfs_device_remove(minor);
460 idr_remove(&drm_minors_idr, minor->index);
466 EXPORT_SYMBOL(drm_put_minor);
470 static void drm_unplug_minor(struct drm_minor *minor)
472 drm_sysfs_device_remove(minor);
476 * Called via drm_exit() at module unload time or when pci device is
479 * Cleans up all DRM device, calling drm_lastclose().
482 void drm_put_dev(struct drm_device *dev)
484 struct drm_driver *driver;
485 struct drm_map_list *r_list, *list_temp;
490 DRM_ERROR("cleanup called no dev\n");
493 driver = dev->driver;
497 if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
498 dev->agp && dev->agp->agp_mtrr >= 0) {
500 retval = mtrr_del(dev->agp->agp_mtrr,
501 dev->agp->agp_info.aper_base,
502 dev->agp->agp_info.aper_size * 1024 * 1024);
503 DRM_DEBUG("mtrr_del=%d\n", retval);
506 if (dev->driver->unload)
507 dev->driver->unload(dev);
509 if (drm_core_has_AGP(dev) && dev->agp) {
510 kfree(dev->agp, M_DRM);
514 drm_vblank_cleanup(dev);
516 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
517 drm_rmmap(dev, r_list->map);
518 drm_ht_remove(&dev->map_hash);
520 drm_ctxbitmap_cleanup(dev);
522 if (drm_core_check_feature(dev, DRIVER_MODESET))
523 drm_put_minor(&dev->control);
525 if (driver->driver_features & DRIVER_GEM)
526 drm_gem_destroy(dev);
528 drm_put_minor(&dev->primary);
530 list_del(&dev->driver_item);
531 kfree(dev->devname, M_DRM);
534 EXPORT_SYMBOL(drm_put_dev);
536 void drm_unplug_dev(struct drm_device *dev)
538 /* for a USB device */
539 if (drm_core_check_feature(dev, DRIVER_MODESET))
540 drm_unplug_minor(dev->control);
541 drm_unplug_minor(dev->primary);
543 mutex_lock(&drm_global_mutex);
545 drm_device_set_unplugged(dev);
547 if (dev->open_count == 0) {
550 mutex_unlock(&drm_global_mutex);
552 EXPORT_SYMBOL(drm_unplug_dev);