Home | History | Annotate | Line # | Download | only in drm
drm_drv.c revision 1.2
      1  1.2  riastrad /*	$NetBSD: drm_drv.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.2  riastrad  * Created: Fri Jan 19 10:48:35 2001 by faith (at) acm.org
      5  1.1  riastrad  *
      6  1.2  riastrad  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
      7  1.1  riastrad  * All Rights Reserved.
      8  1.1  riastrad  *
      9  1.2  riastrad  * Author Rickard E. (Rik) Faith <faith (at) valinux.com>
     10  1.2  riastrad  *
     11  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
     12  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     13  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     14  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     15  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     16  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     17  1.1  riastrad  *
     18  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     19  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     20  1.1  riastrad  * Software.
     21  1.1  riastrad  *
     22  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     23  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     24  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     25  1.2  riastrad  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     26  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     27  1.2  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     28  1.2  riastrad  * DEALINGS IN THE SOFTWARE.
     29  1.1  riastrad  */
     30  1.1  riastrad 
     31  1.2  riastrad #include <sys/cdefs.h>
     32  1.2  riastrad __KERNEL_RCSID(0, "$NetBSD: drm_drv.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $");
     33  1.2  riastrad 
     34  1.2  riastrad #include <linux/err.h>
     35  1.2  riastrad #include <linux/export.h>
     36  1.1  riastrad #include <linux/debugfs.h>
     37  1.2  riastrad #include <linux/fs.h>
     38  1.2  riastrad #include <linux/module.h>
     39  1.2  riastrad #include <linux/moduleparam.h>
     40  1.2  riastrad #include <linux/mount.h>
     41  1.2  riastrad #include <linux/printk.h>
     42  1.1  riastrad #include <linux/slab.h>
     43  1.1  riastrad #include <drm/drmP.h>
     44  1.1  riastrad #include <drm/drm_core.h>
     45  1.2  riastrad #include "drm_legacy.h"
     46  1.2  riastrad #include "drm_internal.h"
     47  1.2  riastrad 
     48  1.2  riastrad unsigned int drm_debug = 0;	/* bitmask of DRM_UT_x */
     49  1.2  riastrad EXPORT_SYMBOL(drm_debug);
     50  1.2  riastrad 
     51  1.2  riastrad MODULE_AUTHOR(CORE_AUTHOR);
     52  1.2  riastrad MODULE_DESCRIPTION(CORE_DESC);
     53  1.2  riastrad MODULE_LICENSE("GPL and additional rights");
     54  1.2  riastrad MODULE_PARM_DESC(debug, "Enable debug output");
     55  1.2  riastrad MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
     56  1.2  riastrad MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
     57  1.2  riastrad MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
     58  1.2  riastrad 
     59  1.2  riastrad module_param_named(debug, drm_debug, int, 0600);
     60  1.2  riastrad 
     61  1.2  riastrad #ifdef __NetBSD__
     62  1.2  riastrad static spinlock_t drm_minor_lock;
     63  1.2  riastrad #else
     64  1.2  riastrad static DEFINE_SPINLOCK(drm_minor_lock);
     65  1.2  riastrad #endif
     66  1.2  riastrad static struct idr drm_minors_idr;
     67  1.2  riastrad 
     68  1.2  riastrad #ifndef __NetBSD__
     69  1.2  riastrad static struct dentry *drm_debugfs_root;
     70  1.2  riastrad #endif
     71  1.2  riastrad 
     72  1.2  riastrad void drm_err(const char *format, ...)
     73  1.2  riastrad {
     74  1.2  riastrad #ifdef __NetBSD__
     75  1.2  riastrad 	va_list args;
     76  1.2  riastrad 
     77  1.2  riastrad 	va_start(args, format);
     78  1.2  riastrad 	printf("DRM error in %s: ", func);
     79  1.2  riastrad 	vprintf(format, args);
     80  1.2  riastrad 	va_end(args);
     81  1.2  riastrad 
     82  1.2  riastrad 	return 0;
     83  1.2  riastrad #else
     84  1.2  riastrad 	struct va_format vaf;
     85  1.2  riastrad 	va_list args;
     86  1.2  riastrad 
     87  1.2  riastrad 	va_start(args, format);
     88  1.2  riastrad 
     89  1.2  riastrad 	vaf.fmt = format;
     90  1.2  riastrad 	vaf.va = &args;
     91  1.2  riastrad 
     92  1.2  riastrad 	printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
     93  1.2  riastrad 	       __builtin_return_address(0), &vaf);
     94  1.2  riastrad 
     95  1.2  riastrad 	va_end(args);
     96  1.2  riastrad #endif
     97  1.2  riastrad }
     98  1.2  riastrad EXPORT_SYMBOL(drm_err);
     99  1.2  riastrad 
    100  1.2  riastrad void drm_ut_debug_printk(const char *function_name, const char *format, ...)
    101  1.2  riastrad {
    102  1.2  riastrad #ifdef __NetBSD__
    103  1.2  riastrad 	va_list args;
    104  1.2  riastrad 
    105  1.2  riastrad 	va_start(args, format);
    106  1.2  riastrad 	printf("DRM debug in %s: ", function_name);
    107  1.2  riastrad 	vprintf(format, args);
    108  1.2  riastrad 	va_end(args);
    109  1.2  riastrad #else
    110  1.2  riastrad 	struct va_format vaf;
    111  1.2  riastrad 	va_list args;
    112  1.2  riastrad 
    113  1.2  riastrad 	va_start(args, format);
    114  1.2  riastrad 	vaf.fmt = format;
    115  1.2  riastrad 	vaf.va = &args;
    116  1.2  riastrad 
    117  1.2  riastrad 	printk(KERN_DEBUG "[" DRM_NAME ":%s] %pV", function_name, &vaf);
    118  1.2  riastrad 
    119  1.2  riastrad 	va_end(args);
    120  1.2  riastrad #endif
    121  1.2  riastrad }
    122  1.2  riastrad EXPORT_SYMBOL(drm_ut_debug_printk);
    123  1.2  riastrad 
    124  1.2  riastrad struct drm_master *drm_master_create(struct drm_minor *minor)
    125  1.2  riastrad {
    126  1.2  riastrad 	struct drm_master *master;
    127  1.2  riastrad 
    128  1.2  riastrad 	master = kzalloc(sizeof(*master), GFP_KERNEL);
    129  1.2  riastrad 	if (!master)
    130  1.2  riastrad 		return NULL;
    131  1.2  riastrad 
    132  1.2  riastrad 	kref_init(&master->refcount);
    133  1.2  riastrad 	spin_lock_init(&master->lock.spinlock);
    134  1.2  riastrad #ifdef __NetBSD__
    135  1.2  riastrad 	DRM_INIT_WAITQUEUE(&master->lock.lock_queue, "drmlockq");
    136  1.2  riastrad #else
    137  1.2  riastrad 	init_waitqueue_head(&master->lock.lock_queue);
    138  1.2  riastrad #endif
    139  1.2  riastrad 	idr_init(&master->magic_map);
    140  1.2  riastrad 	master->minor = minor;
    141  1.2  riastrad 
    142  1.2  riastrad 	return master;
    143  1.2  riastrad }
    144  1.2  riastrad 
    145  1.2  riastrad struct drm_master *drm_master_get(struct drm_master *master)
    146  1.2  riastrad {
    147  1.2  riastrad 	kref_get(&master->refcount);
    148  1.2  riastrad 	return master;
    149  1.2  riastrad }
    150  1.2  riastrad EXPORT_SYMBOL(drm_master_get);
    151  1.2  riastrad 
    152  1.2  riastrad static void drm_master_destroy(struct kref *kref)
    153  1.2  riastrad {
    154  1.2  riastrad 	struct drm_master *master = container_of(kref, struct drm_master, refcount);
    155  1.2  riastrad 	struct drm_device *dev = master->minor->dev;
    156  1.2  riastrad 	struct drm_map_list *r_list, *list_temp;
    157  1.2  riastrad 
    158  1.2  riastrad 	mutex_lock(&dev->struct_mutex);
    159  1.2  riastrad 	if (dev->driver->master_destroy)
    160  1.2  riastrad 		dev->driver->master_destroy(dev, master);
    161  1.2  riastrad 
    162  1.2  riastrad 	list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
    163  1.2  riastrad 		if (r_list->master == master) {
    164  1.2  riastrad 			drm_legacy_rmmap_locked(dev, r_list->map);
    165  1.2  riastrad 			r_list = NULL;
    166  1.2  riastrad 		}
    167  1.2  riastrad 	}
    168  1.2  riastrad 	mutex_unlock(&dev->struct_mutex);
    169  1.2  riastrad 
    170  1.2  riastrad 	idr_destroy(&master->magic_map);
    171  1.2  riastrad #ifdef __NetBSD__
    172  1.2  riastrad 	DRM_DESTROY_WAITQUEUE(&master->lock.lock_queue);
    173  1.2  riastrad 	spin_lock_destroy(&master->lock.spinlock);
    174  1.2  riastrad #endif
    175  1.2  riastrad 	kfree(master->unique);
    176  1.2  riastrad 	kfree(master);
    177  1.2  riastrad }
    178  1.2  riastrad 
    179  1.2  riastrad void drm_master_put(struct drm_master **master)
    180  1.2  riastrad {
    181  1.2  riastrad 	kref_put(&(*master)->refcount, drm_master_destroy);
    182  1.2  riastrad 	*master = NULL;
    183  1.2  riastrad }
    184  1.2  riastrad EXPORT_SYMBOL(drm_master_put);
    185  1.2  riastrad 
    186  1.2  riastrad int drm_setmaster_ioctl(struct drm_device *dev, void *data,
    187  1.2  riastrad 			struct drm_file *file_priv)
    188  1.2  riastrad {
    189  1.2  riastrad 	int ret = 0;
    190  1.2  riastrad 
    191  1.2  riastrad 	mutex_lock(&dev->master_mutex);
    192  1.2  riastrad 	if (file_priv->is_master)
    193  1.2  riastrad 		goto out_unlock;
    194  1.2  riastrad 
    195  1.2  riastrad 	if (file_priv->minor->master) {
    196  1.2  riastrad 		ret = -EINVAL;
    197  1.2  riastrad 		goto out_unlock;
    198  1.2  riastrad 	}
    199  1.2  riastrad 
    200  1.2  riastrad 	if (!file_priv->master) {
    201  1.2  riastrad 		ret = -EINVAL;
    202  1.2  riastrad 		goto out_unlock;
    203  1.2  riastrad 	}
    204  1.2  riastrad 
    205  1.2  riastrad 	if (!file_priv->allowed_master) {
    206  1.2  riastrad 		ret = drm_new_set_master(dev, file_priv);
    207  1.2  riastrad 		goto out_unlock;
    208  1.2  riastrad 	}
    209  1.2  riastrad 
    210  1.2  riastrad 	file_priv->minor->master = drm_master_get(file_priv->master);
    211  1.2  riastrad 	file_priv->is_master = 1;
    212  1.2  riastrad 	if (dev->driver->master_set) {
    213  1.2  riastrad 		ret = dev->driver->master_set(dev, file_priv, false);
    214  1.2  riastrad 		if (unlikely(ret != 0)) {
    215  1.2  riastrad 			file_priv->is_master = 0;
    216  1.2  riastrad 			drm_master_put(&file_priv->minor->master);
    217  1.2  riastrad 		}
    218  1.2  riastrad 	}
    219  1.2  riastrad 
    220  1.2  riastrad out_unlock:
    221  1.2  riastrad 	mutex_unlock(&dev->master_mutex);
    222  1.2  riastrad 	return ret;
    223  1.2  riastrad }
    224  1.2  riastrad 
    225  1.2  riastrad int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
    226  1.2  riastrad 			 struct drm_file *file_priv)
    227  1.2  riastrad {
    228  1.2  riastrad 	int ret = -EINVAL;
    229  1.2  riastrad 
    230  1.2  riastrad 	mutex_lock(&dev->master_mutex);
    231  1.2  riastrad 	if (!file_priv->is_master)
    232  1.2  riastrad 		goto out_unlock;
    233  1.2  riastrad 
    234  1.2  riastrad 	if (!file_priv->minor->master)
    235  1.2  riastrad 		goto out_unlock;
    236  1.2  riastrad 
    237  1.2  riastrad 	ret = 0;
    238  1.2  riastrad 	if (dev->driver->master_drop)
    239  1.2  riastrad 		dev->driver->master_drop(dev, file_priv, false);
    240  1.2  riastrad 	drm_master_put(&file_priv->minor->master);
    241  1.2  riastrad 	file_priv->is_master = 0;
    242  1.2  riastrad 
    243  1.2  riastrad out_unlock:
    244  1.2  riastrad 	mutex_unlock(&dev->master_mutex);
    245  1.2  riastrad 	return ret;
    246  1.2  riastrad }
    247  1.2  riastrad 
    248  1.2  riastrad /*
    249  1.2  riastrad  * DRM Minors
    250  1.2  riastrad  * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
    251  1.2  riastrad  * of them is represented by a drm_minor object. Depending on the capabilities
    252  1.2  riastrad  * of the device-driver, different interfaces are registered.
    253  1.2  riastrad  *
    254  1.2  riastrad  * Minors can be accessed via dev->$minor_name. This pointer is either
    255  1.2  riastrad  * NULL or a valid drm_minor pointer and stays valid as long as the device is
    256  1.2  riastrad  * valid. This means, DRM minors have the same life-time as the underlying
    257  1.2  riastrad  * device. However, this doesn't mean that the minor is active. Minors are
    258  1.2  riastrad  * registered and unregistered dynamically according to device-state.
    259  1.2  riastrad  */
    260  1.1  riastrad 
    261  1.2  riastrad static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
    262  1.2  riastrad 					     unsigned int type)
    263  1.2  riastrad {
    264  1.2  riastrad 	switch (type) {
    265  1.2  riastrad 	case DRM_MINOR_LEGACY:
    266  1.2  riastrad 		return &dev->primary;
    267  1.2  riastrad 	case DRM_MINOR_RENDER:
    268  1.2  riastrad 		return &dev->render;
    269  1.2  riastrad 	case DRM_MINOR_CONTROL:
    270  1.2  riastrad 		return &dev->control;
    271  1.2  riastrad 	default:
    272  1.2  riastrad 		return NULL;
    273  1.2  riastrad 	}
    274  1.2  riastrad }
    275  1.2  riastrad 
    276  1.2  riastrad static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
    277  1.2  riastrad {
    278  1.2  riastrad 	struct drm_minor *minor;
    279  1.2  riastrad 	unsigned long flags;
    280  1.2  riastrad 	int r;
    281  1.2  riastrad 
    282  1.2  riastrad 	minor = kzalloc(sizeof(*minor), GFP_KERNEL);
    283  1.2  riastrad 	if (!minor)
    284  1.2  riastrad 		return -ENOMEM;
    285  1.2  riastrad 
    286  1.2  riastrad 	minor->type = type;
    287  1.2  riastrad 	minor->dev = dev;
    288  1.2  riastrad 
    289  1.2  riastrad 	idr_preload(GFP_KERNEL);
    290  1.2  riastrad 	spin_lock_irqsave(&drm_minor_lock, flags);
    291  1.2  riastrad 	r = idr_alloc(&drm_minors_idr,
    292  1.2  riastrad 		      NULL,
    293  1.2  riastrad 		      64 * type,
    294  1.2  riastrad 		      64 * (type + 1),
    295  1.2  riastrad 		      GFP_NOWAIT);
    296  1.2  riastrad 	spin_unlock_irqrestore(&drm_minor_lock, flags);
    297  1.2  riastrad 	idr_preload_end();
    298  1.2  riastrad 
    299  1.2  riastrad 	if (r < 0)
    300  1.2  riastrad 		goto err_free;
    301  1.2  riastrad 
    302  1.2  riastrad 	minor->index = r;
    303  1.2  riastrad 
    304  1.2  riastrad 	minor->kdev = drm_sysfs_minor_alloc(minor);
    305  1.2  riastrad 	if (IS_ERR(minor->kdev)) {
    306  1.2  riastrad 		r = PTR_ERR(minor->kdev);
    307  1.2  riastrad 		goto err_index;
    308  1.2  riastrad 	}
    309  1.1  riastrad 
    310  1.2  riastrad 	*drm_minor_get_slot(dev, type) = minor;
    311  1.2  riastrad 	return 0;
    312  1.2  riastrad 
    313  1.2  riastrad err_index:
    314  1.2  riastrad 	spin_lock_irqsave(&drm_minor_lock, flags);
    315  1.2  riastrad 	idr_remove(&drm_minors_idr, minor->index);
    316  1.2  riastrad 	spin_unlock_irqrestore(&drm_minor_lock, flags);
    317  1.2  riastrad err_free:
    318  1.2  riastrad 	kfree(minor);
    319  1.2  riastrad 	return r;
    320  1.2  riastrad }
    321  1.2  riastrad 
    322  1.2  riastrad static void drm_minor_free(struct drm_device *dev, unsigned int type)
    323  1.2  riastrad {
    324  1.2  riastrad 	struct drm_minor **slot, *minor;
    325  1.2  riastrad 	unsigned long flags;
    326  1.1  riastrad 
    327  1.2  riastrad 	slot = drm_minor_get_slot(dev, type);
    328  1.2  riastrad 	minor = *slot;
    329  1.2  riastrad 	if (!minor)
    330  1.2  riastrad 		return;
    331  1.2  riastrad 
    332  1.2  riastrad 	put_device(minor->kdev);
    333  1.2  riastrad 
    334  1.2  riastrad 	spin_lock_irqsave(&drm_minor_lock, flags);
    335  1.2  riastrad 	idr_remove(&drm_minors_idr, minor->index);
    336  1.2  riastrad 	spin_unlock_irqrestore(&drm_minor_lock, flags);
    337  1.1  riastrad 
    338  1.2  riastrad 	kfree(minor);
    339  1.2  riastrad 	*slot = NULL;
    340  1.2  riastrad }
    341  1.2  riastrad 
    342  1.2  riastrad static int drm_minor_register(struct drm_device *dev, unsigned int type)
    343  1.2  riastrad {
    344  1.2  riastrad 	struct drm_minor *minor;
    345  1.2  riastrad 	unsigned long flags;
    346  1.2  riastrad #ifndef __NetBSD__
    347  1.2  riastrad 	int ret;
    348  1.2  riastrad #endif
    349  1.2  riastrad 
    350  1.2  riastrad 	DRM_DEBUG("\n");
    351  1.2  riastrad 
    352  1.2  riastrad 	minor = *drm_minor_get_slot(dev, type);
    353  1.2  riastrad 	if (!minor)
    354  1.2  riastrad 		return 0;
    355  1.2  riastrad 
    356  1.2  riastrad #ifndef __NetBSD__
    357  1.2  riastrad 	ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
    358  1.2  riastrad 	if (ret) {
    359  1.2  riastrad 		DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
    360  1.2  riastrad 		goto err_debugfs;
    361  1.2  riastrad 	}
    362  1.2  riastrad 
    363  1.2  riastrad 	ret = device_add(minor->kdev);
    364  1.2  riastrad 	if (ret)
    365  1.2  riastrad 		goto err_debugfs;
    366  1.2  riastrad #endif
    367  1.2  riastrad 
    368  1.2  riastrad 	/* replace NULL with @minor so lookups will succeed from now on */
    369  1.2  riastrad 	spin_lock_irqsave(&drm_minor_lock, flags);
    370  1.2  riastrad 	idr_replace(&drm_minors_idr, minor, minor->index);
    371  1.2  riastrad 	spin_unlock_irqrestore(&drm_minor_lock, flags);
    372  1.2  riastrad 
    373  1.2  riastrad 	DRM_DEBUG("new minor registered %d\n", minor->index);
    374  1.2  riastrad 	return 0;
    375  1.2  riastrad 
    376  1.2  riastrad #ifndef __NetBSD__
    377  1.2  riastrad err_debugfs:
    378  1.2  riastrad 	drm_debugfs_cleanup(minor);
    379  1.2  riastrad 	return ret;
    380  1.2  riastrad #endif
    381  1.2  riastrad }
    382  1.2  riastrad 
    383  1.2  riastrad static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
    384  1.2  riastrad {
    385  1.2  riastrad 	struct drm_minor *minor;
    386  1.2  riastrad 	unsigned long flags;
    387  1.1  riastrad 
    388  1.2  riastrad 	minor = *drm_minor_get_slot(dev, type);
    389  1.2  riastrad 	if (!minor || !device_is_registered(minor->kdev))
    390  1.2  riastrad 		return;
    391  1.2  riastrad 
    392  1.2  riastrad 	/* replace @minor with NULL so lookups will fail from now on */
    393  1.2  riastrad 	spin_lock_irqsave(&drm_minor_lock, flags);
    394  1.2  riastrad 	idr_replace(&drm_minors_idr, NULL, minor->index);
    395  1.2  riastrad 	spin_unlock_irqrestore(&drm_minor_lock, flags);
    396  1.2  riastrad 
    397  1.2  riastrad 	device_del(minor->kdev);
    398  1.2  riastrad 	dev_set_drvdata(minor->kdev, NULL); /* safety belt */
    399  1.2  riastrad 	drm_debugfs_cleanup(minor);
    400  1.2  riastrad }
    401  1.1  riastrad 
    402  1.1  riastrad /**
    403  1.2  riastrad  * drm_minor_acquire - Acquire a DRM minor
    404  1.2  riastrad  * @minor_id: Minor ID of the DRM-minor
    405  1.1  riastrad  *
    406  1.2  riastrad  * Looks up the given minor-ID and returns the respective DRM-minor object. The
    407  1.2  riastrad  * refence-count of the underlying device is increased so you must release this
    408  1.2  riastrad  * object with drm_minor_release().
    409  1.1  riastrad  *
    410  1.2  riastrad  * As long as you hold this minor, it is guaranteed that the object and the
    411  1.2  riastrad  * minor->dev pointer will stay valid! However, the device may get unplugged and
    412  1.2  riastrad  * unregistered while you hold the minor.
    413  1.1  riastrad  *
    414  1.2  riastrad  * Returns:
    415  1.2  riastrad  * Pointer to minor-object with increased device-refcount, or PTR_ERR on
    416  1.2  riastrad  * failure.
    417  1.1  riastrad  */
    418  1.2  riastrad struct drm_minor *drm_minor_acquire(unsigned int minor_id)
    419  1.1  riastrad {
    420  1.2  riastrad 	struct drm_minor *minor;
    421  1.2  riastrad 	unsigned long flags;
    422  1.2  riastrad 
    423  1.2  riastrad 	spin_lock_irqsave(&drm_minor_lock, flags);
    424  1.2  riastrad 	minor = idr_find(&drm_minors_idr, minor_id);
    425  1.2  riastrad 	if (minor)
    426  1.2  riastrad 		drm_dev_ref(minor->dev);
    427  1.2  riastrad 	spin_unlock_irqrestore(&drm_minor_lock, flags);
    428  1.2  riastrad 
    429  1.2  riastrad 	if (!minor) {
    430  1.2  riastrad 		return ERR_PTR(-ENODEV);
    431  1.2  riastrad 	} else if (drm_device_is_unplugged(minor->dev)) {
    432  1.2  riastrad 		drm_dev_unref(minor->dev);
    433  1.2  riastrad 		return ERR_PTR(-ENODEV);
    434  1.2  riastrad 	}
    435  1.2  riastrad 
    436  1.2  riastrad 	return minor;
    437  1.2  riastrad }
    438  1.1  riastrad 
    439  1.2  riastrad /**
    440  1.2  riastrad  * drm_minor_release - Release DRM minor
    441  1.2  riastrad  * @minor: Pointer to DRM minor object
    442  1.2  riastrad  *
    443  1.2  riastrad  * Release a minor that was previously acquired via drm_minor_acquire().
    444  1.2  riastrad  */
    445  1.2  riastrad void drm_minor_release(struct drm_minor *minor)
    446  1.2  riastrad {
    447  1.2  riastrad 	drm_dev_unref(minor->dev);
    448  1.2  riastrad }
    449  1.2  riastrad 
    450  1.2  riastrad /**
    451  1.2  riastrad  * DOC: driver instance overview
    452  1.2  riastrad  *
    453  1.2  riastrad  * A device instance for a drm driver is represented by struct &drm_device. This
    454  1.2  riastrad  * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
    455  1.2  riastrad  * callbacks implemented by the driver. The driver then needs to initialize all
    456  1.2  riastrad  * the various subsystems for the drm device like memory management, vblank
    457  1.2  riastrad  * handling, modesetting support and intial output configuration plus obviously
    458  1.2  riastrad  * initialize all the corresponding hardware bits. An important part of this is
    459  1.2  riastrad  * also calling drm_dev_set_unique() to set the userspace-visible unique name of
    460  1.2  riastrad  * this device instance. Finally when everything is up and running and ready for
    461  1.2  riastrad  * userspace the device instance can be published using drm_dev_register().
    462  1.2  riastrad  *
    463  1.2  riastrad  * There is also deprecated support for initalizing device instances using
    464  1.2  riastrad  * bus-specific helpers and the ->load() callback. But due to
    465  1.2  riastrad  * backwards-compatibility needs the device instance have to be published too
    466  1.2  riastrad  * early, which requires unpretty global locking to make safe and is therefore
    467  1.2  riastrad  * only support for existing drivers not yet converted to the new scheme.
    468  1.2  riastrad  *
    469  1.2  riastrad  * When cleaning up a device instance everything needs to be done in reverse:
    470  1.2  riastrad  * First unpublish the device instance with drm_dev_unregister(). Then clean up
    471  1.2  riastrad  * any other resources allocated at device initialization and drop the driver's
    472  1.2  riastrad  * reference to &drm_device using drm_dev_unref().
    473  1.2  riastrad  *
    474  1.2  riastrad  * Note that the lifetime rules for &drm_device instance has still a lot of
    475  1.2  riastrad  * historical baggage. Hence use the reference counting provided by
    476  1.2  riastrad  * drm_dev_ref() and drm_dev_unref() only carefully.
    477  1.2  riastrad  *
    478  1.2  riastrad  * Also note that embedding of &drm_device is currently not (yet) supported (but
    479  1.2  riastrad  * it would be easy to add). Drivers can store driver-private data in the
    480  1.2  riastrad  * dev_priv field of &drm_device.
    481  1.2  riastrad  */
    482  1.2  riastrad 
    483  1.2  riastrad /**
    484  1.2  riastrad  * drm_put_dev - Unregister and release a DRM device
    485  1.2  riastrad  * @dev: DRM device
    486  1.2  riastrad  *
    487  1.2  riastrad  * Called at module unload time or when a PCI device is unplugged.
    488  1.2  riastrad  *
    489  1.2  riastrad  * Cleans up all DRM device, calling drm_lastclose().
    490  1.2  riastrad  *
    491  1.2  riastrad  * Note: Use of this function is deprecated. It will eventually go away
    492  1.2  riastrad  * completely.  Please use drm_dev_unregister() and drm_dev_unref() explicitly
    493  1.2  riastrad  * instead to make sure that the device isn't userspace accessible any more
    494  1.2  riastrad  * while teardown is in progress, ensuring that userspace can't access an
    495  1.2  riastrad  * inconsistent state.
    496  1.2  riastrad  */
    497  1.2  riastrad void drm_put_dev(struct drm_device *dev)
    498  1.2  riastrad {
    499  1.1  riastrad 	DRM_DEBUG("\n");
    500  1.1  riastrad 
    501  1.2  riastrad 	if (!dev) {
    502  1.2  riastrad 		DRM_ERROR("cleanup called no dev\n");
    503  1.2  riastrad 		return;
    504  1.2  riastrad 	}
    505  1.2  riastrad 
    506  1.2  riastrad 	drm_dev_unregister(dev);
    507  1.2  riastrad 	drm_dev_unref(dev);
    508  1.2  riastrad }
    509  1.2  riastrad EXPORT_SYMBOL(drm_put_dev);
    510  1.2  riastrad 
    511  1.2  riastrad void drm_unplug_dev(struct drm_device *dev)
    512  1.2  riastrad {
    513  1.2  riastrad 	/* for a USB device */
    514  1.2  riastrad 	drm_minor_unregister(dev, DRM_MINOR_LEGACY);
    515  1.2  riastrad 	drm_minor_unregister(dev, DRM_MINOR_RENDER);
    516  1.2  riastrad 	drm_minor_unregister(dev, DRM_MINOR_CONTROL);
    517  1.2  riastrad 
    518  1.2  riastrad 	mutex_lock(&drm_global_mutex);
    519  1.2  riastrad 
    520  1.2  riastrad 	drm_device_set_unplugged(dev);
    521  1.2  riastrad 
    522  1.2  riastrad 	if (dev->open_count == 0) {
    523  1.2  riastrad 		drm_put_dev(dev);
    524  1.2  riastrad 	}
    525  1.2  riastrad 	mutex_unlock(&drm_global_mutex);
    526  1.2  riastrad }
    527  1.2  riastrad EXPORT_SYMBOL(drm_unplug_dev);
    528  1.2  riastrad 
    529  1.2  riastrad #ifdef __NetBSD__
    530  1.2  riastrad 
    531  1.2  riastrad struct inode;
    532  1.2  riastrad 
    533  1.2  riastrad static struct inode *
    534  1.2  riastrad drm_fs_inode_new(void)
    535  1.2  riastrad {
    536  1.2  riastrad 	return NULL;
    537  1.2  riastrad }
    538  1.2  riastrad 
    539  1.2  riastrad static void
    540  1.2  riastrad drm_fs_inode_free(struct inode *inode)
    541  1.2  riastrad {
    542  1.2  riastrad 	KASSERT(inode == NULL);
    543  1.2  riastrad }
    544  1.2  riastrad 
    545  1.2  riastrad #else
    546  1.2  riastrad 
    547  1.2  riastrad /*
    548  1.2  riastrad  * DRM internal mount
    549  1.2  riastrad  * We want to be able to allocate our own "struct address_space" to control
    550  1.2  riastrad  * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
    551  1.2  riastrad  * stand-alone address_space objects, so we need an underlying inode. As there
    552  1.2  riastrad  * is no way to allocate an independent inode easily, we need a fake internal
    553  1.2  riastrad  * VFS mount-point.
    554  1.2  riastrad  *
    555  1.2  riastrad  * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
    556  1.2  riastrad  * frees it again. You are allowed to use iget() and iput() to get references to
    557  1.2  riastrad  * the inode. But each drm_fs_inode_new() call must be paired with exactly one
    558  1.2  riastrad  * drm_fs_inode_free() call (which does not have to be the last iput()).
    559  1.2  riastrad  * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
    560  1.2  riastrad  * between multiple inode-users. You could, technically, call
    561  1.2  riastrad  * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
    562  1.2  riastrad  * iput(), but this way you'd end up with a new vfsmount for each inode.
    563  1.2  riastrad  */
    564  1.2  riastrad 
    565  1.2  riastrad static int drm_fs_cnt;
    566  1.2  riastrad static struct vfsmount *drm_fs_mnt;
    567  1.1  riastrad 
    568  1.2  riastrad static const struct dentry_operations drm_fs_dops = {
    569  1.2  riastrad 	.d_dname	= simple_dname,
    570  1.2  riastrad };
    571  1.2  riastrad 
    572  1.2  riastrad static const struct super_operations drm_fs_sops = {
    573  1.2  riastrad 	.statfs		= simple_statfs,
    574  1.2  riastrad };
    575  1.2  riastrad 
    576  1.2  riastrad static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
    577  1.2  riastrad 				   const char *dev_name, void *data)
    578  1.2  riastrad {
    579  1.2  riastrad 	return mount_pseudo(fs_type,
    580  1.2  riastrad 			    "drm:",
    581  1.2  riastrad 			    &drm_fs_sops,
    582  1.2  riastrad 			    &drm_fs_dops,
    583  1.2  riastrad 			    0x010203ff);
    584  1.2  riastrad }
    585  1.2  riastrad 
    586  1.2  riastrad static struct file_system_type drm_fs_type = {
    587  1.2  riastrad 	.name		= "drm",
    588  1.2  riastrad 	.owner		= THIS_MODULE,
    589  1.2  riastrad 	.mount		= drm_fs_mount,
    590  1.2  riastrad 	.kill_sb	= kill_anon_super,
    591  1.2  riastrad };
    592  1.2  riastrad 
    593  1.2  riastrad static struct inode *drm_fs_inode_new(void)
    594  1.2  riastrad {
    595  1.2  riastrad 	struct inode *inode;
    596  1.2  riastrad 	int r;
    597  1.2  riastrad 
    598  1.2  riastrad 	r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
    599  1.2  riastrad 	if (r < 0) {
    600  1.2  riastrad 		DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
    601  1.2  riastrad 		return ERR_PTR(r);
    602  1.2  riastrad 	}
    603  1.2  riastrad 
    604  1.2  riastrad 	inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
    605  1.2  riastrad 	if (IS_ERR(inode))
    606  1.2  riastrad 		simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
    607  1.2  riastrad 
    608  1.2  riastrad 	return inode;
    609  1.2  riastrad }
    610  1.2  riastrad 
    611  1.2  riastrad static void drm_fs_inode_free(struct inode *inode)
    612  1.2  riastrad {
    613  1.2  riastrad 	if (inode) {
    614  1.2  riastrad 		iput(inode);
    615  1.2  riastrad 		simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
    616  1.2  riastrad 	}
    617  1.2  riastrad }
    618  1.2  riastrad 
    619  1.2  riastrad #endif
    620  1.1  riastrad 
    621  1.2  riastrad /**
    622  1.2  riastrad  * drm_dev_alloc - Allocate new DRM device
    623  1.2  riastrad  * @driver: DRM driver to allocate device for
    624  1.2  riastrad  * @parent: Parent device object
    625  1.2  riastrad  *
    626  1.2  riastrad  * Allocate and initialize a new DRM device. No device registration is done.
    627  1.2  riastrad  * Call drm_dev_register() to advertice the device to user space and register it
    628  1.2  riastrad  * with other core subsystems. This should be done last in the device
    629  1.2  riastrad  * initialization sequence to make sure userspace can't access an inconsistent
    630  1.2  riastrad  * state.
    631  1.2  riastrad  *
    632  1.2  riastrad  * The initial ref-count of the object is 1. Use drm_dev_ref() and
    633  1.2  riastrad  * drm_dev_unref() to take and drop further ref-counts.
    634  1.2  riastrad  *
    635  1.2  riastrad  * Note that for purely virtual devices @parent can be NULL.
    636  1.2  riastrad  *
    637  1.2  riastrad  * RETURNS:
    638  1.2  riastrad  * Pointer to new DRM device, or NULL if out of memory.
    639  1.2  riastrad  */
    640  1.2  riastrad struct drm_device *drm_dev_alloc(struct drm_driver *driver,
    641  1.2  riastrad 				 struct device *parent)
    642  1.2  riastrad {
    643  1.2  riastrad 	struct drm_device *dev;
    644  1.2  riastrad 	int ret;
    645  1.2  riastrad 
    646  1.2  riastrad 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
    647  1.2  riastrad 	if (!dev)
    648  1.2  riastrad 		return NULL;
    649  1.2  riastrad 
    650  1.2  riastrad 	kref_init(&dev->ref);
    651  1.2  riastrad 	dev->dev = parent;
    652  1.2  riastrad 	dev->driver = driver;
    653  1.2  riastrad 
    654  1.2  riastrad 	INIT_LIST_HEAD(&dev->filelist);
    655  1.2  riastrad 	INIT_LIST_HEAD(&dev->ctxlist);
    656  1.2  riastrad 	INIT_LIST_HEAD(&dev->vmalist);
    657  1.2  riastrad 	INIT_LIST_HEAD(&dev->maplist);
    658  1.2  riastrad 	INIT_LIST_HEAD(&dev->vblank_event_list);
    659  1.2  riastrad 
    660  1.2  riastrad 	spin_lock_init(&dev->buf_lock);
    661  1.2  riastrad 	spin_lock_init(&dev->event_lock);
    662  1.2  riastrad #ifdef __NetBSD__
    663  1.2  riastrad 	linux_mutex_init(&dev->struct_mutex);
    664  1.2  riastrad 	linux_mutex_init(&dev->ctxlist_mutex);
    665  1.2  riastrad 	linux_mutex_init(&dev->master_mutex);
    666  1.2  riastrad #else
    667  1.2  riastrad 	mutex_init(&dev->struct_mutex);
    668  1.2  riastrad 	mutex_init(&dev->ctxlist_mutex);
    669  1.2  riastrad 	mutex_init(&dev->master_mutex);
    670  1.2  riastrad #endif
    671  1.1  riastrad 
    672  1.2  riastrad 	dev->anon_inode = drm_fs_inode_new();
    673  1.2  riastrad 	if (IS_ERR(dev->anon_inode)) {
    674  1.2  riastrad 		ret = PTR_ERR(dev->anon_inode);
    675  1.2  riastrad 		DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
    676  1.2  riastrad 		goto err_free;
    677  1.2  riastrad 	}
    678  1.1  riastrad 
    679  1.2  riastrad 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
    680  1.2  riastrad 		ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
    681  1.2  riastrad 		if (ret)
    682  1.2  riastrad 			goto err_minors;
    683  1.1  riastrad 
    684  1.2  riastrad 		WARN_ON(driver->suspend || driver->resume);
    685  1.1  riastrad 	}
    686  1.2  riastrad 
    687  1.2  riastrad 	if (drm_core_check_feature(dev, DRIVER_RENDER)) {
    688  1.2  riastrad 		ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
    689  1.2  riastrad 		if (ret)
    690  1.2  riastrad 			goto err_minors;
    691  1.1  riastrad 	}
    692  1.1  riastrad 
    693  1.2  riastrad 	ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
    694  1.2  riastrad 	if (ret)
    695  1.2  riastrad 		goto err_minors;
    696  1.2  riastrad 
    697  1.2  riastrad 	if (drm_ht_create(&dev->map_hash, 12))
    698  1.2  riastrad 		goto err_minors;
    699  1.2  riastrad 
    700  1.2  riastrad 	drm_legacy_ctxbitmap_init(dev);
    701  1.2  riastrad 
    702  1.2  riastrad 	if (drm_core_check_feature(dev, DRIVER_GEM)) {
    703  1.2  riastrad 		ret = drm_gem_init(dev);
    704  1.2  riastrad 		if (ret) {
    705  1.2  riastrad 			DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
    706  1.2  riastrad 			goto err_ctxbitmap;
    707  1.2  riastrad 		}
    708  1.1  riastrad 	}
    709  1.1  riastrad 
    710  1.2  riastrad 	return dev;
    711  1.2  riastrad 
    712  1.2  riastrad err_ctxbitmap:
    713  1.2  riastrad 	drm_legacy_ctxbitmap_cleanup(dev);
    714  1.2  riastrad 	drm_ht_remove(&dev->map_hash);
    715  1.2  riastrad err_minors:
    716  1.2  riastrad 	drm_minor_free(dev, DRM_MINOR_LEGACY);
    717  1.2  riastrad 	drm_minor_free(dev, DRM_MINOR_RENDER);
    718  1.2  riastrad 	drm_minor_free(dev, DRM_MINOR_CONTROL);
    719  1.2  riastrad 	drm_fs_inode_free(dev->anon_inode);
    720  1.2  riastrad err_free:
    721  1.2  riastrad #ifdef __NetBSD__
    722  1.2  riastrad 	linux_mutex_destroy(&dev->struct_mutex);
    723  1.2  riastrad 	linux_mutex_destroy(&dev->ctxlist_mutex);
    724  1.2  riastrad 	linux_mutex_destroy(&dev->master_mutex);
    725  1.2  riastrad 	spin_lock_destroy(&dev->event_lock);
    726  1.2  riastrad 	spin_lock_destroy(&dev->count_lock);
    727  1.2  riastrad #else
    728  1.2  riastrad 	mutex_destroy(&dev->master_mutex);
    729  1.2  riastrad #endif
    730  1.2  riastrad 	kfree(dev);
    731  1.2  riastrad 	return NULL;
    732  1.2  riastrad }
    733  1.2  riastrad EXPORT_SYMBOL(drm_dev_alloc);
    734  1.2  riastrad 
    735  1.2  riastrad static void drm_dev_release(struct kref *ref)
    736  1.2  riastrad {
    737  1.2  riastrad 	struct drm_device *dev = container_of(ref, struct drm_device, ref);
    738  1.2  riastrad 
    739  1.2  riastrad 	if (drm_core_check_feature(dev, DRIVER_GEM))
    740  1.2  riastrad 		drm_gem_destroy(dev);
    741  1.2  riastrad 
    742  1.2  riastrad 	drm_legacy_ctxbitmap_cleanup(dev);
    743  1.2  riastrad 	drm_ht_remove(&dev->map_hash);
    744  1.2  riastrad 	drm_fs_inode_free(dev->anon_inode);
    745  1.2  riastrad 
    746  1.2  riastrad 	drm_minor_free(dev, DRM_MINOR_LEGACY);
    747  1.2  riastrad 	drm_minor_free(dev, DRM_MINOR_RENDER);
    748  1.2  riastrad 	drm_minor_free(dev, DRM_MINOR_CONTROL);
    749  1.2  riastrad 
    750  1.2  riastrad #ifdef __NetBSD__
    751  1.2  riastrad 	linux_mutex_destroy(&dev->struct_mutex);
    752  1.2  riastrad 	linux_mutex_destroy(&dev->ctxlist_mutex);
    753  1.2  riastrad 	linux_mutex_destroy(&dev->master_mutex);
    754  1.2  riastrad 	spin_lock_destroy(&dev->event_lock);
    755  1.2  riastrad 	spin_lock_destroy(&dev->count_lock);
    756  1.2  riastrad #else
    757  1.2  riastrad 	mutex_destroy(&dev->master_mutex);
    758  1.2  riastrad #endif
    759  1.2  riastrad 	kfree(dev->unique);
    760  1.2  riastrad 	kfree(dev);
    761  1.2  riastrad }
    762  1.2  riastrad 
    763  1.2  riastrad /**
    764  1.2  riastrad  * drm_dev_ref - Take reference of a DRM device
    765  1.2  riastrad  * @dev: device to take reference of or NULL
    766  1.2  riastrad  *
    767  1.2  riastrad  * This increases the ref-count of @dev by one. You *must* already own a
    768  1.2  riastrad  * reference when calling this. Use drm_dev_unref() to drop this reference
    769  1.2  riastrad  * again.
    770  1.2  riastrad  *
    771  1.2  riastrad  * This function never fails. However, this function does not provide *any*
    772  1.2  riastrad  * guarantee whether the device is alive or running. It only provides a
    773  1.2  riastrad  * reference to the object and the memory associated with it.
    774  1.2  riastrad  */
    775  1.2  riastrad void drm_dev_ref(struct drm_device *dev)
    776  1.2  riastrad {
    777  1.2  riastrad 	if (dev)
    778  1.2  riastrad 		kref_get(&dev->ref);
    779  1.2  riastrad }
    780  1.2  riastrad EXPORT_SYMBOL(drm_dev_ref);
    781  1.2  riastrad 
    782  1.2  riastrad /**
    783  1.2  riastrad  * drm_dev_unref - Drop reference of a DRM device
    784  1.2  riastrad  * @dev: device to drop reference of or NULL
    785  1.2  riastrad  *
    786  1.2  riastrad  * This decreases the ref-count of @dev by one. The device is destroyed if the
    787  1.2  riastrad  * ref-count drops to zero.
    788  1.2  riastrad  */
    789  1.2  riastrad void drm_dev_unref(struct drm_device *dev)
    790  1.2  riastrad {
    791  1.2  riastrad 	if (dev)
    792  1.2  riastrad 		kref_put(&dev->ref, drm_dev_release);
    793  1.2  riastrad }
    794  1.2  riastrad EXPORT_SYMBOL(drm_dev_unref);
    795  1.2  riastrad 
    796  1.2  riastrad /**
    797  1.2  riastrad  * drm_dev_register - Register DRM device
    798  1.2  riastrad  * @dev: Device to register
    799  1.2  riastrad  * @flags: Flags passed to the driver's .load() function
    800  1.2  riastrad  *
    801  1.2  riastrad  * Register the DRM device @dev with the system, advertise device to user-space
    802  1.2  riastrad  * and start normal device operation. @dev must be allocated via drm_dev_alloc()
    803  1.2  riastrad  * previously.
    804  1.2  riastrad  *
    805  1.2  riastrad  * Never call this twice on any device!
    806  1.2  riastrad  *
    807  1.2  riastrad  * NOTE: To ensure backward compatibility with existing drivers method this
    808  1.2  riastrad  * function calls the ->load() method after registering the device nodes,
    809  1.2  riastrad  * creating race conditions. Usage of the ->load() methods is therefore
    810  1.2  riastrad  * deprecated, drivers must perform all initialization before calling
    811  1.2  riastrad  * drm_dev_register().
    812  1.2  riastrad  *
    813  1.2  riastrad  * RETURNS:
    814  1.2  riastrad  * 0 on success, negative error code on failure.
    815  1.2  riastrad  */
    816  1.2  riastrad int drm_dev_register(struct drm_device *dev, unsigned long flags)
    817  1.2  riastrad {
    818  1.2  riastrad 	int ret;
    819  1.2  riastrad 
    820  1.2  riastrad #ifndef __NetBSD__
    821  1.2  riastrad 	mutex_lock(&drm_global_mutex);
    822  1.2  riastrad #endif
    823  1.2  riastrad 
    824  1.2  riastrad 	ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
    825  1.2  riastrad 	if (ret)
    826  1.2  riastrad 		goto err_minors;
    827  1.2  riastrad 
    828  1.2  riastrad 	ret = drm_minor_register(dev, DRM_MINOR_RENDER);
    829  1.2  riastrad 	if (ret)
    830  1.2  riastrad 		goto err_minors;
    831  1.2  riastrad 
    832  1.2  riastrad 	ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
    833  1.2  riastrad 	if (ret)
    834  1.2  riastrad 		goto err_minors;
    835  1.2  riastrad 
    836  1.2  riastrad 	if (dev->driver->load) {
    837  1.2  riastrad 		ret = dev->driver->load(dev, flags);
    838  1.2  riastrad 		if (ret)
    839  1.2  riastrad 			goto err_minors;
    840  1.2  riastrad 	}
    841  1.2  riastrad 
    842  1.2  riastrad 	ret = 0;
    843  1.2  riastrad 	goto out_unlock;
    844  1.2  riastrad 
    845  1.2  riastrad err_minors:
    846  1.2  riastrad 	drm_minor_unregister(dev, DRM_MINOR_LEGACY);
    847  1.2  riastrad 	drm_minor_unregister(dev, DRM_MINOR_RENDER);
    848  1.2  riastrad 	drm_minor_unregister(dev, DRM_MINOR_CONTROL);
    849  1.2  riastrad out_unlock:
    850  1.2  riastrad #ifndef __NetBSD__
    851  1.2  riastrad 	mutex_unlock(&drm_global_mutex);
    852  1.2  riastrad #endif
    853  1.2  riastrad 	return ret;
    854  1.2  riastrad }
    855  1.2  riastrad EXPORT_SYMBOL(drm_dev_register);
    856  1.2  riastrad 
    857  1.2  riastrad /**
    858  1.2  riastrad  * drm_dev_unregister - Unregister DRM device
    859  1.2  riastrad  * @dev: Device to unregister
    860  1.2  riastrad  *
    861  1.2  riastrad  * Unregister the DRM device from the system. This does the reverse of
    862  1.2  riastrad  * drm_dev_register() but does not deallocate the device. The caller must call
    863  1.2  riastrad  * drm_dev_unref() to drop their final reference.
    864  1.2  riastrad  *
    865  1.2  riastrad  * This should be called first in the device teardown code to make sure
    866  1.2  riastrad  * userspace can't access the device instance any more.
    867  1.2  riastrad  */
    868  1.2  riastrad void drm_dev_unregister(struct drm_device *dev)
    869  1.2  riastrad {
    870  1.2  riastrad 	struct drm_map_list *r_list, *list_temp;
    871  1.2  riastrad 
    872  1.2  riastrad 	drm_lastclose(dev);
    873  1.2  riastrad 
    874  1.2  riastrad 	if (dev->driver->unload)
    875  1.2  riastrad 		dev->driver->unload(dev);
    876  1.2  riastrad 
    877  1.2  riastrad #ifndef __NetBSD__		/* Moved to drm_pci.  */
    878  1.2  riastrad 	if (dev->agp)
    879  1.2  riastrad 		drm_pci_agp_destroy(dev);
    880  1.2  riastrad #endif
    881  1.2  riastrad 
    882  1.2  riastrad 	drm_vblank_cleanup(dev);
    883  1.1  riastrad 
    884  1.2  riastrad 	list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
    885  1.2  riastrad 		drm_legacy_rmmap(dev, r_list->map);
    886  1.2  riastrad 
    887  1.2  riastrad 	drm_minor_unregister(dev, DRM_MINOR_LEGACY);
    888  1.2  riastrad 	drm_minor_unregister(dev, DRM_MINOR_RENDER);
    889  1.2  riastrad 	drm_minor_unregister(dev, DRM_MINOR_CONTROL);
    890  1.2  riastrad }
    891  1.2  riastrad EXPORT_SYMBOL(drm_dev_unregister);
    892  1.2  riastrad 
    893  1.2  riastrad /**
    894  1.2  riastrad  * drm_dev_set_unique - Set the unique name of a DRM device
    895  1.2  riastrad  * @dev: device of which to set the unique name
    896  1.2  riastrad  * @fmt: format string for unique name
    897  1.2  riastrad  *
    898  1.2  riastrad  * Sets the unique name of a DRM device using the specified format string and
    899  1.2  riastrad  * a variable list of arguments. Drivers can use this at driver probe time if
    900  1.2  riastrad  * the unique name of the devices they drive is static.
    901  1.2  riastrad  *
    902  1.2  riastrad  * Return: 0 on success or a negative error code on failure.
    903  1.2  riastrad  */
    904  1.2  riastrad int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...)
    905  1.2  riastrad {
    906  1.2  riastrad 	va_list ap;
    907  1.2  riastrad 
    908  1.2  riastrad 	kfree(dev->unique);
    909  1.2  riastrad 
    910  1.2  riastrad 	va_start(ap, fmt);
    911  1.2  riastrad 	dev->unique = kvasprintf(GFP_KERNEL, fmt, ap);
    912  1.2  riastrad 	va_end(ap);
    913  1.2  riastrad 
    914  1.2  riastrad 	return dev->unique ? 0 : -ENOMEM;
    915  1.2  riastrad }
    916  1.2  riastrad EXPORT_SYMBOL(drm_dev_set_unique);
    917  1.2  riastrad 
    918  1.2  riastrad /*
    919  1.2  riastrad  * DRM Core
    920  1.2  riastrad  * The DRM core module initializes all global DRM objects and makes them
    921  1.2  riastrad  * available to drivers. Once setup, drivers can probe their respective
    922  1.2  riastrad  * devices.
    923  1.2  riastrad  * Currently, core management includes:
    924  1.2  riastrad  *  - The "DRM-Global" key/value database
    925  1.2  riastrad  *  - Global ID management for connectors
    926  1.2  riastrad  *  - DRM major number allocation
    927  1.2  riastrad  *  - DRM minor management
    928  1.2  riastrad  *  - DRM sysfs class
    929  1.2  riastrad  *  - DRM debugfs root
    930  1.2  riastrad  *
    931  1.2  riastrad  * Furthermore, the DRM core provides dynamic char-dev lookups. For each
    932  1.2  riastrad  * interface registered on a DRM device, you can request minor numbers from DRM
    933  1.2  riastrad  * core. DRM core takes care of major-number management and char-dev
    934  1.2  riastrad  * registration. A stub ->open() callback forwards any open() requests to the
    935  1.2  riastrad  * registered minor.
    936  1.2  riastrad  */
    937  1.2  riastrad 
    938  1.2  riastrad static int drm_stub_open(struct inode *inode, struct file *filp)
    939  1.2  riastrad {
    940  1.2  riastrad 	const struct file_operations *new_fops;
    941  1.2  riastrad 	struct drm_minor *minor;
    942  1.2  riastrad 	int err;
    943  1.2  riastrad 
    944  1.2  riastrad 	DRM_DEBUG("\n");
    945  1.2  riastrad 
    946  1.2  riastrad 	mutex_lock(&drm_global_mutex);
    947  1.2  riastrad 	minor = drm_minor_acquire(iminor(inode));
    948  1.2  riastrad 	if (IS_ERR(minor)) {
    949  1.2  riastrad 		err = PTR_ERR(minor);
    950  1.2  riastrad 		goto out_unlock;
    951  1.2  riastrad 	}
    952  1.2  riastrad 
    953  1.2  riastrad 	new_fops = fops_get(minor->dev->driver->fops);
    954  1.2  riastrad 	if (!new_fops) {
    955  1.2  riastrad 		err = -ENODEV;
    956  1.2  riastrad 		goto out_release;
    957  1.2  riastrad 	}
    958  1.1  riastrad 
    959  1.2  riastrad 	replace_fops(filp, new_fops);
    960  1.2  riastrad 	if (filp->f_op->open)
    961  1.2  riastrad 		err = filp->f_op->open(inode, filp);
    962  1.2  riastrad 	else
    963  1.2  riastrad 		err = 0;
    964  1.2  riastrad 
    965  1.2  riastrad out_release:
    966  1.2  riastrad 	drm_minor_release(minor);
    967  1.2  riastrad out_unlock:
    968  1.2  riastrad 	mutex_unlock(&drm_global_mutex);
    969  1.2  riastrad 	return err;
    970  1.1  riastrad }
    971  1.1  riastrad 
    972  1.1  riastrad static const struct file_operations drm_stub_fops = {
    973  1.1  riastrad 	.owner = THIS_MODULE,
    974  1.1  riastrad 	.open = drm_stub_open,
    975  1.1  riastrad 	.llseek = noop_llseek,
    976  1.1  riastrad };
    977  1.1  riastrad 
    978  1.1  riastrad static int __init drm_core_init(void)
    979  1.1  riastrad {
    980  1.1  riastrad 	int ret = -ENOMEM;
    981  1.1  riastrad 
    982  1.1  riastrad 	drm_global_init();
    983  1.2  riastrad 	drm_connector_ida_init();
    984  1.1  riastrad 	idr_init(&drm_minors_idr);
    985  1.1  riastrad 
    986  1.1  riastrad 	if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
    987  1.1  riastrad 		goto err_p1;
    988  1.1  riastrad 
    989  1.2  riastrad 	ret = drm_sysfs_init();
    990  1.2  riastrad 	if (ret < 0) {
    991  1.1  riastrad 		printk(KERN_ERR "DRM: Error creating drm class.\n");
    992  1.1  riastrad 		goto err_p2;
    993  1.1  riastrad 	}
    994  1.1  riastrad 
    995  1.1  riastrad 	drm_debugfs_root = debugfs_create_dir("dri", NULL);
    996  1.1  riastrad 	if (!drm_debugfs_root) {
    997  1.1  riastrad 		DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
    998  1.1  riastrad 		ret = -1;
    999  1.1  riastrad 		goto err_p3;
   1000  1.1  riastrad 	}
   1001  1.1  riastrad 
   1002  1.1  riastrad 	DRM_INFO("Initialized %s %d.%d.%d %s\n",
   1003  1.1  riastrad 		 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
   1004  1.1  riastrad 	return 0;
   1005  1.1  riastrad err_p3:
   1006  1.1  riastrad 	drm_sysfs_destroy();
   1007  1.1  riastrad err_p2:
   1008  1.1  riastrad 	unregister_chrdev(DRM_MAJOR, "drm");
   1009  1.1  riastrad 
   1010  1.1  riastrad 	idr_destroy(&drm_minors_idr);
   1011  1.1  riastrad err_p1:
   1012  1.1  riastrad 	return ret;
   1013  1.1  riastrad }
   1014  1.1  riastrad 
   1015  1.1  riastrad static void __exit drm_core_exit(void)
   1016  1.1  riastrad {
   1017  1.1  riastrad 	debugfs_remove(drm_debugfs_root);
   1018  1.1  riastrad 	drm_sysfs_destroy();
   1019  1.1  riastrad 
   1020  1.1  riastrad 	unregister_chrdev(DRM_MAJOR, "drm");
   1021  1.1  riastrad 
   1022  1.2  riastrad 	drm_connector_ida_destroy();
   1023  1.1  riastrad 	idr_destroy(&drm_minors_idr);
   1024  1.1  riastrad }
   1025  1.1  riastrad 
   1026  1.1  riastrad module_init(drm_core_init);
   1027  1.1  riastrad module_exit(drm_core_exit);
   1028