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