blob: a7f6282983656817ac860546422f2ca0973774c9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
3 *
4 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
5 * All Rights Reserved.
6 *
Thierry Redingc6a1af8a2014-05-19 13:39:07 +02007 * Author Rickard E. (Rik) Faith <faith@valinux.com>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 */
28
David Herrmann1b7199f2014-07-23 12:29:56 +020029#include <linux/debugfs.h>
David Herrmann31bbe162014-01-03 14:09:47 +010030#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/moduleparam.h>
David Herrmann31bbe162014-01-03 14:09:47 +010033#include <linux/mount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
36#include <drm/drm_core.h>
Benjamin Gaignard79190ea2016-06-21 16:37:09 +020037#include "drm_crtc_internal.h"
David Herrmanne7b960702014-07-24 12:10:04 +020038#include "drm_legacy.h"
Daniel Vetter67d0ec42014-09-10 12:43:53 +020039#include "drm_internal.h"
Daniel Vetter81065542016-06-21 10:54:13 +020040#include "drm_crtc_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ezequiel Garcia6dc3e222016-04-20 13:45:03 -030042/*
43 * drm_debug: Enable debug output.
44 * Bitmask of DRM_UT_x. See include/drm/drmP.h for details.
45 */
46unsigned int drm_debug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047EXPORT_SYMBOL(drm_debug);
48
Dave Airlieb5e89ed2005-09-25 14:28:13 +100049MODULE_AUTHOR(CORE_AUTHOR);
50MODULE_DESCRIPTION(CORE_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051MODULE_LICENSE("GPL and additional rights");
Ezequiel Garcia6dc3e222016-04-20 13:45:03 -030052MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n"
53"\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n"
54"\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n"
55"\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n"
56"\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n"
57"\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n"
58"\t\tBit 5 (0x20) will enable VBL messages (vblank code)");
Dave Jonesc0758142005-10-03 15:02:20 -040059module_param_named(debug, drm_debug, int, 0600);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
David Herrmann0d639882014-02-24 15:53:25 +010061static DEFINE_SPINLOCK(drm_minor_lock);
David Herrmann1b7199f2014-07-23 12:29:56 +020062static struct idr drm_minors_idr;
Dave Airlie2c14f282008-04-21 16:47:32 +100063
David Herrmann1b7199f2014-07-23 12:29:56 +020064static struct dentry *drm_debugfs_root;
Joe Perches5ad3d882011-04-17 20:35:51 -070065
Sean Paulc4e68a52016-08-15 16:18:04 -070066#define DRM_PRINTK_FMT "[" DRM_NAME ":%s]%s %pV"
67
68void drm_dev_printk(const struct device *dev, const char *level,
69 unsigned int category, const char *function_name,
70 const char *prefix, const char *format, ...)
Joe Perches5ad3d882011-04-17 20:35:51 -070071{
72 struct va_format vaf;
73 va_list args;
Joe Perches5ad3d882011-04-17 20:35:51 -070074
Sean Paulc4e68a52016-08-15 16:18:04 -070075 if (category != DRM_UT_NONE && !(drm_debug & category))
76 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Lespiau, Damiena73d4e92014-03-24 15:53:15 +000078 va_start(args, format);
79 vaf.fmt = format;
80 vaf.va = &args;
Daniel Vetterfffb9062013-11-17 22:25:02 +010081
Sean Paulc4e68a52016-08-15 16:18:04 -070082 dev_printk(level, dev, DRM_PRINTK_FMT, function_name, prefix,
83 &vaf);
Lespiau, Damiena73d4e92014-03-24 15:53:15 +000084
85 va_end(args);
yakui_zhao4fefcb22009-06-02 14:09:47 +080086}
Sean Paulc4e68a52016-08-15 16:18:04 -070087EXPORT_SYMBOL(drm_dev_printk);
88
89void drm_printk(const char *level, unsigned int category,
90 const char *function_name, const char *prefix,
91 const char *format, ...)
92{
93 struct va_format vaf;
94 va_list args;
95
96 if (category != DRM_UT_NONE && !(drm_debug & category))
97 return;
98
99 va_start(args, format);
100 vaf.fmt = format;
101 vaf.va = &args;
102
103 printk("%s" DRM_PRINTK_FMT, level, function_name, prefix, &vaf);
104
105 va_end(args);
106}
107EXPORT_SYMBOL(drm_printk);
Joe Perches5ad3d882011-04-17 20:35:51 -0700108
David Herrmann0d639882014-02-24 15:53:25 +0100109/*
110 * DRM Minors
111 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
112 * of them is represented by a drm_minor object. Depending on the capabilities
113 * of the device-driver, different interfaces are registered.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
David Herrmann0d639882014-02-24 15:53:25 +0100115 * Minors can be accessed via dev->$minor_name. This pointer is either
116 * NULL or a valid drm_minor pointer and stays valid as long as the device is
117 * valid. This means, DRM minors have the same life-time as the underlying
118 * device. However, this doesn't mean that the minor is active. Minors are
119 * registered and unregistered dynamically according to device-state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
David Herrmann0d639882014-02-24 15:53:25 +0100121
David Herrmann05b701f2014-01-29 12:43:56 +0100122static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
123 unsigned int type)
124{
125 switch (type) {
David Herrmanna3ccc462016-08-03 20:04:25 +0200126 case DRM_MINOR_PRIMARY:
David Herrmann05b701f2014-01-29 12:43:56 +0100127 return &dev->primary;
128 case DRM_MINOR_RENDER:
129 return &dev->render;
130 case DRM_MINOR_CONTROL:
131 return &dev->control;
132 default:
133 return NULL;
134 }
135}
136
137static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
138{
139 struct drm_minor *minor;
David Herrmannf1b85962014-07-23 10:34:52 +0200140 unsigned long flags;
141 int r;
David Herrmann05b701f2014-01-29 12:43:56 +0100142
143 minor = kzalloc(sizeof(*minor), GFP_KERNEL);
144 if (!minor)
145 return -ENOMEM;
146
147 minor->type = type;
148 minor->dev = dev;
David Herrmann05b701f2014-01-29 12:43:56 +0100149
David Herrmannf1b85962014-07-23 10:34:52 +0200150 idr_preload(GFP_KERNEL);
151 spin_lock_irqsave(&drm_minor_lock, flags);
152 r = idr_alloc(&drm_minors_idr,
153 NULL,
154 64 * type,
155 64 * (type + 1),
156 GFP_NOWAIT);
157 spin_unlock_irqrestore(&drm_minor_lock, flags);
158 idr_preload_end();
159
160 if (r < 0)
161 goto err_free;
162
163 minor->index = r;
164
David Herrmanne1728072014-07-23 11:38:38 +0200165 minor->kdev = drm_sysfs_minor_alloc(minor);
166 if (IS_ERR(minor->kdev)) {
167 r = PTR_ERR(minor->kdev);
168 goto err_index;
169 }
170
David Herrmann05b701f2014-01-29 12:43:56 +0100171 *drm_minor_get_slot(dev, type) = minor;
172 return 0;
David Herrmannf1b85962014-07-23 10:34:52 +0200173
David Herrmanne1728072014-07-23 11:38:38 +0200174err_index:
175 spin_lock_irqsave(&drm_minor_lock, flags);
176 idr_remove(&drm_minors_idr, minor->index);
177 spin_unlock_irqrestore(&drm_minor_lock, flags);
David Herrmannf1b85962014-07-23 10:34:52 +0200178err_free:
179 kfree(minor);
180 return r;
David Herrmann05b701f2014-01-29 12:43:56 +0100181}
182
David Herrmannbd9dfa92014-01-29 12:55:48 +0100183static void drm_minor_free(struct drm_device *dev, unsigned int type)
184{
David Herrmannf1b85962014-07-23 10:34:52 +0200185 struct drm_minor **slot, *minor;
186 unsigned long flags;
David Herrmannbd9dfa92014-01-29 12:55:48 +0100187
188 slot = drm_minor_get_slot(dev, type);
David Herrmannf1b85962014-07-23 10:34:52 +0200189 minor = *slot;
190 if (!minor)
191 return;
192
David Herrmanne1728072014-07-23 11:38:38 +0200193 put_device(minor->kdev);
David Herrmannf1b85962014-07-23 10:34:52 +0200194
195 spin_lock_irqsave(&drm_minor_lock, flags);
196 idr_remove(&drm_minors_idr, minor->index);
197 spin_unlock_irqrestore(&drm_minor_lock, flags);
198
199 kfree(minor);
200 *slot = NULL;
David Herrmannbd9dfa92014-01-29 12:55:48 +0100201}
202
David Herrmannafcdbc82014-01-29 12:57:05 +0100203static int drm_minor_register(struct drm_device *dev, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
David Herrmannf1b85962014-07-23 10:34:52 +0200205 struct drm_minor *minor;
David Herrmann0d639882014-02-24 15:53:25 +0100206 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 DRM_DEBUG("\n");
210
David Herrmannf1b85962014-07-23 10:34:52 +0200211 minor = *drm_minor_get_slot(dev, type);
212 if (!minor)
David Herrmann05b701f2014-01-29 12:43:56 +0100213 return 0;
214
David Herrmannf1b85962014-07-23 10:34:52 +0200215 ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
Ben Gamari955b12d2009-02-17 20:08:49 -0500216 if (ret) {
GeunSik Lim156f5a72009-06-02 15:01:37 +0900217 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
David Herrmannf1b85962014-07-23 10:34:52 +0200218 return ret;
Ben Gamari955b12d2009-02-17 20:08:49 -0500219 }
Dave Airlie2c14f282008-04-21 16:47:32 +1000220
David Herrmanne1728072014-07-23 11:38:38 +0200221 ret = device_add(minor->kdev);
222 if (ret)
Daniel Vettercb6458f2013-08-08 15:41:34 +0200223 goto err_debugfs;
Dave Airlie2c14f282008-04-21 16:47:32 +1000224
David Herrmann0d639882014-02-24 15:53:25 +0100225 /* replace NULL with @minor so lookups will succeed from now on */
226 spin_lock_irqsave(&drm_minor_lock, flags);
David Herrmannf1b85962014-07-23 10:34:52 +0200227 idr_replace(&drm_minors_idr, minor, minor->index);
David Herrmann0d639882014-02-24 15:53:25 +0100228 spin_unlock_irqrestore(&drm_minor_lock, flags);
Dave Airlie2c14f282008-04-21 16:47:32 +1000229
David Herrmannf1b85962014-07-23 10:34:52 +0200230 DRM_DEBUG("new minor registered %d\n", minor->index);
Dave Airlie2c14f282008-04-21 16:47:32 +1000231 return 0;
232
Daniel Vettercb6458f2013-08-08 15:41:34 +0200233err_debugfs:
David Herrmannf1b85962014-07-23 10:34:52 +0200234 drm_debugfs_cleanup(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return ret;
236}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000237
David Herrmannafcdbc82014-01-29 12:57:05 +0100238static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
David Herrmannf73aca52013-10-20 18:55:40 +0200239{
David Herrmannafcdbc82014-01-29 12:57:05 +0100240 struct drm_minor *minor;
David Herrmann0d639882014-02-24 15:53:25 +0100241 unsigned long flags;
David Herrmannafcdbc82014-01-29 12:57:05 +0100242
243 minor = *drm_minor_get_slot(dev, type);
David Herrmanne1728072014-07-23 11:38:38 +0200244 if (!minor || !device_is_registered(minor->kdev))
David Herrmannf73aca52013-10-20 18:55:40 +0200245 return;
246
David Herrmannf1b85962014-07-23 10:34:52 +0200247 /* replace @minor with NULL so lookups will fail from now on */
David Herrmann0d639882014-02-24 15:53:25 +0100248 spin_lock_irqsave(&drm_minor_lock, flags);
David Herrmannf1b85962014-07-23 10:34:52 +0200249 idr_replace(&drm_minors_idr, NULL, minor->index);
David Herrmann0d639882014-02-24 15:53:25 +0100250 spin_unlock_irqrestore(&drm_minor_lock, flags);
David Herrmann0d639882014-02-24 15:53:25 +0100251
David Herrmanne1728072014-07-23 11:38:38 +0200252 device_del(minor->kdev);
253 dev_set_drvdata(minor->kdev, NULL); /* safety belt */
David Herrmann865fb472013-10-20 18:55:43 +0200254 drm_debugfs_cleanup(minor);
David Herrmannf73aca52013-10-20 18:55:40 +0200255}
256
257/**
David Herrmann1616c522014-01-29 10:49:19 +0100258 * drm_minor_acquire - Acquire a DRM minor
259 * @minor_id: Minor ID of the DRM-minor
David Herrmannf73aca52013-10-20 18:55:40 +0200260 *
David Herrmann1616c522014-01-29 10:49:19 +0100261 * Looks up the given minor-ID and returns the respective DRM-minor object. The
262 * refence-count of the underlying device is increased so you must release this
263 * object with drm_minor_release().
264 *
265 * As long as you hold this minor, it is guaranteed that the object and the
266 * minor->dev pointer will stay valid! However, the device may get unplugged and
267 * unregistered while you hold the minor.
268 *
269 * Returns:
270 * Pointer to minor-object with increased device-refcount, or PTR_ERR on
271 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
David Herrmann1616c522014-01-29 10:49:19 +0100273struct drm_minor *drm_minor_acquire(unsigned int minor_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
David Herrmann1616c522014-01-29 10:49:19 +0100275 struct drm_minor *minor;
David Herrmann0d639882014-02-24 15:53:25 +0100276 unsigned long flags;
Eric Anholt673a3942008-07-30 12:06:12 -0700277
David Herrmann0d639882014-02-24 15:53:25 +0100278 spin_lock_irqsave(&drm_minor_lock, flags);
David Herrmann1616c522014-01-29 10:49:19 +0100279 minor = idr_find(&drm_minors_idr, minor_id);
David Herrmann0d639882014-02-24 15:53:25 +0100280 if (minor)
281 drm_dev_ref(minor->dev);
282 spin_unlock_irqrestore(&drm_minor_lock, flags);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000283
David Herrmann0d639882014-02-24 15:53:25 +0100284 if (!minor) {
285 return ERR_PTR(-ENODEV);
286 } else if (drm_device_is_unplugged(minor->dev)) {
287 drm_dev_unref(minor->dev);
288 return ERR_PTR(-ENODEV);
289 }
290
David Herrmann1616c522014-01-29 10:49:19 +0100291 return minor;
292}
293
294/**
295 * drm_minor_release - Release DRM minor
296 * @minor: Pointer to DRM minor object
297 *
298 * Release a minor that was previously acquired via drm_minor_acquire().
299 */
300void drm_minor_release(struct drm_minor *minor)
301{
302 drm_dev_unref(minor->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500304
305/**
Daniel Vetter6e3f7972015-09-28 21:46:35 +0200306 * DOC: driver instance overview
307 *
308 * A device instance for a drm driver is represented by struct &drm_device. This
309 * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
310 * callbacks implemented by the driver. The driver then needs to initialize all
311 * the various subsystems for the drm device like memory management, vblank
312 * handling, modesetting support and intial output configuration plus obviously
Daniel Vettera7429462016-06-21 10:54:16 +0200313 * initialize all the corresponding hardware bits. Finally when everything is up
314 * and running and ready for userspace the device instance can be published
315 * using drm_dev_register().
Daniel Vetter6e3f7972015-09-28 21:46:35 +0200316 *
317 * There is also deprecated support for initalizing device instances using
318 * bus-specific helpers and the ->load() callback. But due to
319 * backwards-compatibility needs the device instance have to be published too
320 * early, which requires unpretty global locking to make safe and is therefore
321 * only support for existing drivers not yet converted to the new scheme.
322 *
323 * When cleaning up a device instance everything needs to be done in reverse:
324 * First unpublish the device instance with drm_dev_unregister(). Then clean up
325 * any other resources allocated at device initialization and drop the driver's
326 * reference to &drm_device using drm_dev_unref().
327 *
328 * Note that the lifetime rules for &drm_device instance has still a lot of
329 * historical baggage. Hence use the reference counting provided by
330 * drm_dev_ref() and drm_dev_unref() only carefully.
331 *
332 * Also note that embedding of &drm_device is currently not (yet) supported (but
333 * it would be easy to add). Drivers can store driver-private data in the
334 * dev_priv field of &drm_device.
335 */
336
Daniel Vettera7429462016-06-21 10:54:16 +0200337static int drm_dev_set_unique(struct drm_device *dev, const char *name)
338{
339 kfree(dev->unique);
340 dev->unique = kstrdup(name, GFP_KERNEL);
341
342 return dev->unique ? 0 : -ENOMEM;
343}
344
Daniel Vetter6e3f7972015-09-28 21:46:35 +0200345/**
Thierry Redingc6a1af8a2014-05-19 13:39:07 +0200346 * drm_put_dev - Unregister and release a DRM device
347 * @dev: DRM device
348 *
349 * Called at module unload time or when a PCI device is unplugged.
350 *
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500351 * Cleans up all DRM device, calling drm_lastclose().
Daniel Vetter6e3f7972015-09-28 21:46:35 +0200352 *
353 * Note: Use of this function is deprecated. It will eventually go away
354 * completely. Please use drm_dev_unregister() and drm_dev_unref() explicitly
355 * instead to make sure that the device isn't userspace accessible any more
356 * while teardown is in progress, ensuring that userspace can't access an
357 * inconsistent state.
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500358 */
359void drm_put_dev(struct drm_device *dev)
360{
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500361 DRM_DEBUG("\n");
362
363 if (!dev) {
364 DRM_ERROR("cleanup called no dev\n");
365 return;
366 }
367
David Herrmannc3a49732013-10-02 11:23:38 +0200368 drm_dev_unregister(dev);
David Herrmann099d1c22014-01-29 10:21:36 +0100369 drm_dev_unref(dev);
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500370}
371EXPORT_SYMBOL(drm_put_dev);
Dave Airlie2c07a212012-02-20 14:18:07 +0000372
373void drm_unplug_dev(struct drm_device *dev)
374{
375 /* for a USB device */
Chris Wilsona39be602016-06-24 15:36:20 +0100376 drm_dev_unregister(dev);
Dave Airlie2c07a212012-02-20 14:18:07 +0000377
378 mutex_lock(&drm_global_mutex);
379
380 drm_device_set_unplugged(dev);
381
382 if (dev->open_count == 0) {
383 drm_put_dev(dev);
384 }
385 mutex_unlock(&drm_global_mutex);
386}
387EXPORT_SYMBOL(drm_unplug_dev);
David Herrmann1bb72532013-10-02 11:23:34 +0200388
David Herrmann31bbe162014-01-03 14:09:47 +0100389/*
390 * DRM internal mount
391 * We want to be able to allocate our own "struct address_space" to control
392 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
393 * stand-alone address_space objects, so we need an underlying inode. As there
394 * is no way to allocate an independent inode easily, we need a fake internal
395 * VFS mount-point.
396 *
397 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
398 * frees it again. You are allowed to use iget() and iput() to get references to
399 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
400 * drm_fs_inode_free() call (which does not have to be the last iput()).
401 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
402 * between multiple inode-users. You could, technically, call
403 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
404 * iput(), but this way you'd end up with a new vfsmount for each inode.
405 */
406
407static int drm_fs_cnt;
408static struct vfsmount *drm_fs_mnt;
409
410static const struct dentry_operations drm_fs_dops = {
411 .d_dname = simple_dname,
412};
413
414static const struct super_operations drm_fs_sops = {
415 .statfs = simple_statfs,
416};
417
418static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
419 const char *dev_name, void *data)
420{
421 return mount_pseudo(fs_type,
422 "drm:",
423 &drm_fs_sops,
424 &drm_fs_dops,
425 0x010203ff);
426}
427
428static struct file_system_type drm_fs_type = {
429 .name = "drm",
430 .owner = THIS_MODULE,
431 .mount = drm_fs_mount,
432 .kill_sb = kill_anon_super,
433};
434
435static struct inode *drm_fs_inode_new(void)
436{
437 struct inode *inode;
438 int r;
439
440 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
441 if (r < 0) {
442 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
443 return ERR_PTR(r);
444 }
445
446 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
447 if (IS_ERR(inode))
448 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
449
450 return inode;
451}
452
453static void drm_fs_inode_free(struct inode *inode)
454{
455 if (inode) {
456 iput(inode);
457 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
458 }
459}
460
David Herrmann1bb72532013-10-02 11:23:34 +0200461/**
Chris Wilsonb209aca2016-06-15 13:17:46 +0100462 * drm_dev_init - Initialise new DRM device
463 * @dev: DRM device
464 * @driver: DRM driver
David Herrmann1bb72532013-10-02 11:23:34 +0200465 * @parent: Parent device object
466 *
Chris Wilsonb209aca2016-06-15 13:17:46 +0100467 * Initialize a new DRM device. No device registration is done.
David Herrmannc22f0ac2013-10-02 11:23:35 +0200468 * Call drm_dev_register() to advertice the device to user space and register it
Daniel Vetter6e3f7972015-09-28 21:46:35 +0200469 * with other core subsystems. This should be done last in the device
470 * initialization sequence to make sure userspace can't access an inconsistent
471 * state.
David Herrmann1bb72532013-10-02 11:23:34 +0200472 *
David Herrmann099d1c22014-01-29 10:21:36 +0100473 * The initial ref-count of the object is 1. Use drm_dev_ref() and
474 * drm_dev_unref() to take and drop further ref-counts.
475 *
Daniel Vetterb0ff4b92014-11-24 20:01:58 +0100476 * Note that for purely virtual devices @parent can be NULL.
477 *
Chris Wilsonb209aca2016-06-15 13:17:46 +0100478 * Drivers that do not want to allocate their own device struct
479 * embedding struct &drm_device can call drm_dev_alloc() instead.
480 *
David Herrmann1bb72532013-10-02 11:23:34 +0200481 * RETURNS:
Chris Wilsonb209aca2016-06-15 13:17:46 +0100482 * 0 on success, or error code on failure.
David Herrmann1bb72532013-10-02 11:23:34 +0200483 */
Chris Wilsonb209aca2016-06-15 13:17:46 +0100484int drm_dev_init(struct drm_device *dev,
485 struct drm_driver *driver,
486 struct device *parent)
David Herrmann1bb72532013-10-02 11:23:34 +0200487{
David Herrmann1bb72532013-10-02 11:23:34 +0200488 int ret;
489
David Herrmann099d1c22014-01-29 10:21:36 +0100490 kref_init(&dev->ref);
David Herrmann1bb72532013-10-02 11:23:34 +0200491 dev->dev = parent;
492 dev->driver = driver;
493
494 INIT_LIST_HEAD(&dev->filelist);
495 INIT_LIST_HEAD(&dev->ctxlist);
496 INIT_LIST_HEAD(&dev->vmalist);
497 INIT_LIST_HEAD(&dev->maplist);
498 INIT_LIST_HEAD(&dev->vblank_event_list);
499
Daniel Vetter2177a212013-12-16 11:21:06 +0100500 spin_lock_init(&dev->buf_lock);
David Herrmann1bb72532013-10-02 11:23:34 +0200501 spin_lock_init(&dev->event_lock);
502 mutex_init(&dev->struct_mutex);
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200503 mutex_init(&dev->filelist_mutex);
David Herrmann1bb72532013-10-02 11:23:34 +0200504 mutex_init(&dev->ctxlist_mutex);
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100505 mutex_init(&dev->master_mutex);
David Herrmann1bb72532013-10-02 11:23:34 +0200506
David Herrmann6796cb12014-01-03 14:24:19 +0100507 dev->anon_inode = drm_fs_inode_new();
508 if (IS_ERR(dev->anon_inode)) {
509 ret = PTR_ERR(dev->anon_inode);
510 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
David Herrmann1bb72532013-10-02 11:23:34 +0200511 goto err_free;
David Herrmann6796cb12014-01-03 14:24:19 +0100512 }
513
David Herrmann05b701f2014-01-29 12:43:56 +0100514 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
515 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
516 if (ret)
517 goto err_minors;
518 }
519
David Herrmann6d6dfcf2014-03-16 14:38:40 +0100520 if (drm_core_check_feature(dev, DRIVER_RENDER)) {
David Herrmann05b701f2014-01-29 12:43:56 +0100521 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
522 if (ret)
523 goto err_minors;
524 }
525
David Herrmanna3ccc462016-08-03 20:04:25 +0200526 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY);
David Herrmann05b701f2014-01-29 12:43:56 +0100527 if (ret)
528 goto err_minors;
529
Chris Wilsonb209aca2016-06-15 13:17:46 +0100530 ret = drm_ht_create(&dev->map_hash, 12);
531 if (ret)
David Herrmann05b701f2014-01-29 12:43:56 +0100532 goto err_minors;
David Herrmann1bb72532013-10-02 11:23:34 +0200533
Daniel Vetterba6976c2015-06-23 11:22:36 +0200534 drm_legacy_ctxbitmap_init(dev);
David Herrmann1bb72532013-10-02 11:23:34 +0200535
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200536 if (drm_core_check_feature(dev, DRIVER_GEM)) {
David Herrmann1bb72532013-10-02 11:23:34 +0200537 ret = drm_gem_init(dev);
538 if (ret) {
539 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
540 goto err_ctxbitmap;
541 }
542 }
543
Daniel Vetter5079c462016-06-21 10:54:14 +0200544 /* Use the parent device name as DRM device unique identifier, but fall
545 * back to the driver name for virtual devices like vgem. */
546 ret = drm_dev_set_unique(dev, parent ? dev_name(parent) : driver->name);
547 if (ret)
548 goto err_setunique;
Nicolas Ioosse112e592015-12-11 11:20:28 +0100549
Chris Wilsonb209aca2016-06-15 13:17:46 +0100550 return 0;
David Herrmann1bb72532013-10-02 11:23:34 +0200551
Nicolas Ioosse112e592015-12-11 11:20:28 +0100552err_setunique:
553 if (drm_core_check_feature(dev, DRIVER_GEM))
554 drm_gem_destroy(dev);
David Herrmann1bb72532013-10-02 11:23:34 +0200555err_ctxbitmap:
David Herrmanne7b960702014-07-24 12:10:04 +0200556 drm_legacy_ctxbitmap_cleanup(dev);
David Herrmann1bb72532013-10-02 11:23:34 +0200557 drm_ht_remove(&dev->map_hash);
David Herrmann05b701f2014-01-29 12:43:56 +0100558err_minors:
David Herrmanna3ccc462016-08-03 20:04:25 +0200559 drm_minor_free(dev, DRM_MINOR_PRIMARY);
David Herrmannbd9dfa92014-01-29 12:55:48 +0100560 drm_minor_free(dev, DRM_MINOR_RENDER);
561 drm_minor_free(dev, DRM_MINOR_CONTROL);
David Herrmann6796cb12014-01-03 14:24:19 +0100562 drm_fs_inode_free(dev->anon_inode);
David Herrmann1bb72532013-10-02 11:23:34 +0200563err_free:
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100564 mutex_destroy(&dev->master_mutex);
Chris Wilsonb209aca2016-06-15 13:17:46 +0100565 return ret;
566}
567EXPORT_SYMBOL(drm_dev_init);
568
569/**
570 * drm_dev_alloc - Allocate new DRM device
571 * @driver: DRM driver to allocate device for
572 * @parent: Parent device object
573 *
574 * Allocate and initialize a new DRM device. No device registration is done.
575 * Call drm_dev_register() to advertice the device to user space and register it
576 * with other core subsystems. This should be done last in the device
577 * initialization sequence to make sure userspace can't access an inconsistent
578 * state.
579 *
580 * The initial ref-count of the object is 1. Use drm_dev_ref() and
581 * drm_dev_unref() to take and drop further ref-counts.
582 *
583 * Note that for purely virtual devices @parent can be NULL.
584 *
585 * Drivers that wish to subclass or embed struct &drm_device into their
586 * own struct should look at using drm_dev_init() instead.
587 *
588 * RETURNS:
589 * Pointer to new DRM device, or NULL if out of memory.
590 */
591struct drm_device *drm_dev_alloc(struct drm_driver *driver,
592 struct device *parent)
593{
594 struct drm_device *dev;
595 int ret;
596
597 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
598 if (!dev)
599 return NULL;
600
601 ret = drm_dev_init(dev, driver, parent);
602 if (ret) {
603 kfree(dev);
604 return NULL;
605 }
606
607 return dev;
David Herrmann1bb72532013-10-02 11:23:34 +0200608}
609EXPORT_SYMBOL(drm_dev_alloc);
David Herrmannc22f0ac2013-10-02 11:23:35 +0200610
David Herrmann099d1c22014-01-29 10:21:36 +0100611static void drm_dev_release(struct kref *ref)
David Herrmann0dc8fe52013-10-02 11:23:37 +0200612{
David Herrmann099d1c22014-01-29 10:21:36 +0100613 struct drm_device *dev = container_of(ref, struct drm_device, ref);
David Herrmann8f6599d2013-10-20 18:55:45 +0200614
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200615 if (drm_core_check_feature(dev, DRIVER_GEM))
David Herrmann0dc8fe52013-10-02 11:23:37 +0200616 drm_gem_destroy(dev);
617
David Herrmanne7b960702014-07-24 12:10:04 +0200618 drm_legacy_ctxbitmap_cleanup(dev);
David Herrmann0dc8fe52013-10-02 11:23:37 +0200619 drm_ht_remove(&dev->map_hash);
David Herrmann6796cb12014-01-03 14:24:19 +0100620 drm_fs_inode_free(dev->anon_inode);
David Herrmann0dc8fe52013-10-02 11:23:37 +0200621
David Herrmanna3ccc462016-08-03 20:04:25 +0200622 drm_minor_free(dev, DRM_MINOR_PRIMARY);
David Herrmannbd9dfa92014-01-29 12:55:48 +0100623 drm_minor_free(dev, DRM_MINOR_RENDER);
624 drm_minor_free(dev, DRM_MINOR_CONTROL);
625
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100626 mutex_destroy(&dev->master_mutex);
Thierry Redingca8e2ad2014-04-11 15:23:00 +0200627 kfree(dev->unique);
David Herrmann0dc8fe52013-10-02 11:23:37 +0200628 kfree(dev);
629}
David Herrmann099d1c22014-01-29 10:21:36 +0100630
631/**
632 * drm_dev_ref - Take reference of a DRM device
633 * @dev: device to take reference of or NULL
634 *
635 * This increases the ref-count of @dev by one. You *must* already own a
636 * reference when calling this. Use drm_dev_unref() to drop this reference
637 * again.
638 *
639 * This function never fails. However, this function does not provide *any*
640 * guarantee whether the device is alive or running. It only provides a
641 * reference to the object and the memory associated with it.
642 */
643void drm_dev_ref(struct drm_device *dev)
644{
645 if (dev)
646 kref_get(&dev->ref);
647}
648EXPORT_SYMBOL(drm_dev_ref);
649
650/**
651 * drm_dev_unref - Drop reference of a DRM device
652 * @dev: device to drop reference of or NULL
653 *
654 * This decreases the ref-count of @dev by one. The device is destroyed if the
655 * ref-count drops to zero.
656 */
657void drm_dev_unref(struct drm_device *dev)
658{
659 if (dev)
660 kref_put(&dev->ref, drm_dev_release);
661}
662EXPORT_SYMBOL(drm_dev_unref);
David Herrmann0dc8fe52013-10-02 11:23:37 +0200663
664/**
David Herrmannc22f0ac2013-10-02 11:23:35 +0200665 * drm_dev_register - Register DRM device
666 * @dev: Device to register
Thierry Redingc6a1af8a2014-05-19 13:39:07 +0200667 * @flags: Flags passed to the driver's .load() function
David Herrmannc22f0ac2013-10-02 11:23:35 +0200668 *
669 * Register the DRM device @dev with the system, advertise device to user-space
670 * and start normal device operation. @dev must be allocated via drm_dev_alloc()
Chris Wilsone28cd4d2016-06-17 09:25:17 +0100671 * previously.
David Herrmannc22f0ac2013-10-02 11:23:35 +0200672 *
673 * Never call this twice on any device!
674 *
Daniel Vetter6e3f7972015-09-28 21:46:35 +0200675 * NOTE: To ensure backward compatibility with existing drivers method this
676 * function calls the ->load() method after registering the device nodes,
677 * creating race conditions. Usage of the ->load() methods is therefore
678 * deprecated, drivers must perform all initialization before calling
679 * drm_dev_register().
680 *
David Herrmannc22f0ac2013-10-02 11:23:35 +0200681 * RETURNS:
682 * 0 on success, negative error code on failure.
683 */
684int drm_dev_register(struct drm_device *dev, unsigned long flags)
685{
686 int ret;
687
688 mutex_lock(&drm_global_mutex);
689
David Herrmannafcdbc82014-01-29 12:57:05 +0100690 ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
David Herrmannc22f0ac2013-10-02 11:23:35 +0200691 if (ret)
David Herrmann05b701f2014-01-29 12:43:56 +0100692 goto err_minors;
693
David Herrmannafcdbc82014-01-29 12:57:05 +0100694 ret = drm_minor_register(dev, DRM_MINOR_RENDER);
David Herrmann05b701f2014-01-29 12:43:56 +0100695 if (ret)
696 goto err_minors;
697
David Herrmanna3ccc462016-08-03 20:04:25 +0200698 ret = drm_minor_register(dev, DRM_MINOR_PRIMARY);
David Herrmann05b701f2014-01-29 12:43:56 +0100699 if (ret)
700 goto err_minors;
David Herrmannc22f0ac2013-10-02 11:23:35 +0200701
702 if (dev->driver->load) {
703 ret = dev->driver->load(dev, flags);
704 if (ret)
David Herrmann05b701f2014-01-29 12:43:56 +0100705 goto err_minors;
David Herrmannc22f0ac2013-10-02 11:23:35 +0200706 }
707
Chris Wilsonbee7fb12016-06-18 14:46:41 +0100708 if (drm_core_check_feature(dev, DRIVER_MODESET))
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200709 drm_modeset_register_all(dev);
Chris Wilsone28cd4d2016-06-17 09:25:17 +0100710
David Herrmannc22f0ac2013-10-02 11:23:35 +0200711 ret = 0;
712 goto out_unlock;
713
David Herrmann05b701f2014-01-29 12:43:56 +0100714err_minors:
David Herrmanna3ccc462016-08-03 20:04:25 +0200715 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
David Herrmannafcdbc82014-01-29 12:57:05 +0100716 drm_minor_unregister(dev, DRM_MINOR_RENDER);
717 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
David Herrmannc22f0ac2013-10-02 11:23:35 +0200718out_unlock:
719 mutex_unlock(&drm_global_mutex);
720 return ret;
721}
722EXPORT_SYMBOL(drm_dev_register);
David Herrmannc3a49732013-10-02 11:23:38 +0200723
724/**
725 * drm_dev_unregister - Unregister DRM device
726 * @dev: Device to unregister
727 *
728 * Unregister the DRM device from the system. This does the reverse of
729 * drm_dev_register() but does not deallocate the device. The caller must call
David Herrmann099d1c22014-01-29 10:21:36 +0100730 * drm_dev_unref() to drop their final reference.
Daniel Vetter6e3f7972015-09-28 21:46:35 +0200731 *
732 * This should be called first in the device teardown code to make sure
733 * userspace can't access the device instance any more.
David Herrmannc3a49732013-10-02 11:23:38 +0200734 */
735void drm_dev_unregister(struct drm_device *dev)
736{
737 struct drm_map_list *r_list, *list_temp;
738
739 drm_lastclose(dev);
740
Chris Wilsonbee7fb12016-06-18 14:46:41 +0100741 if (drm_core_check_feature(dev, DRIVER_MODESET))
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200742 drm_modeset_unregister_all(dev);
Chris Wilsone28cd4d2016-06-17 09:25:17 +0100743
David Herrmannc3a49732013-10-02 11:23:38 +0200744 if (dev->driver->unload)
745 dev->driver->unload(dev);
746
Daniel Vetter4efafeb2013-12-11 11:34:38 +0100747 if (dev->agp)
748 drm_pci_agp_destroy(dev);
David Herrmannc3a49732013-10-02 11:23:38 +0200749
750 drm_vblank_cleanup(dev);
751
752 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
David Herrmann9fc5cde2014-08-29 12:12:28 +0200753 drm_legacy_rmmap(dev, r_list->map);
David Herrmannc3a49732013-10-02 11:23:38 +0200754
David Herrmanna3ccc462016-08-03 20:04:25 +0200755 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
David Herrmannafcdbc82014-01-29 12:57:05 +0100756 drm_minor_unregister(dev, DRM_MINOR_RENDER);
757 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
David Herrmannc3a49732013-10-02 11:23:38 +0200758}
759EXPORT_SYMBOL(drm_dev_unregister);
Thierry Redingca8e2ad2014-04-11 15:23:00 +0200760
David Herrmann1b7199f2014-07-23 12:29:56 +0200761/*
762 * DRM Core
763 * The DRM core module initializes all global DRM objects and makes them
764 * available to drivers. Once setup, drivers can probe their respective
765 * devices.
766 * Currently, core management includes:
767 * - The "DRM-Global" key/value database
768 * - Global ID management for connectors
769 * - DRM major number allocation
770 * - DRM minor management
771 * - DRM sysfs class
772 * - DRM debugfs root
773 *
774 * Furthermore, the DRM core provides dynamic char-dev lookups. For each
775 * interface registered on a DRM device, you can request minor numbers from DRM
776 * core. DRM core takes care of major-number management and char-dev
777 * registration. A stub ->open() callback forwards any open() requests to the
778 * registered minor.
779 */
780
781static int drm_stub_open(struct inode *inode, struct file *filp)
782{
783 const struct file_operations *new_fops;
784 struct drm_minor *minor;
785 int err;
786
787 DRM_DEBUG("\n");
788
789 mutex_lock(&drm_global_mutex);
790 minor = drm_minor_acquire(iminor(inode));
791 if (IS_ERR(minor)) {
792 err = PTR_ERR(minor);
793 goto out_unlock;
794 }
795
796 new_fops = fops_get(minor->dev->driver->fops);
797 if (!new_fops) {
798 err = -ENODEV;
799 goto out_release;
800 }
801
802 replace_fops(filp, new_fops);
803 if (filp->f_op->open)
804 err = filp->f_op->open(inode, filp);
805 else
806 err = 0;
807
808out_release:
809 drm_minor_release(minor);
810out_unlock:
811 mutex_unlock(&drm_global_mutex);
812 return err;
813}
814
815static const struct file_operations drm_stub_fops = {
816 .owner = THIS_MODULE,
817 .open = drm_stub_open,
818 .llseek = noop_llseek,
819};
820
821static int __init drm_core_init(void)
822{
823 int ret = -ENOMEM;
824
825 drm_global_init();
826 drm_connector_ida_init();
827 idr_init(&drm_minors_idr);
828
829 if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
830 goto err_p1;
831
David Herrmannfcc90212015-09-09 14:21:30 +0200832 ret = drm_sysfs_init();
833 if (ret < 0) {
David Herrmann1b7199f2014-07-23 12:29:56 +0200834 printk(KERN_ERR "DRM: Error creating drm class.\n");
David Herrmann1b7199f2014-07-23 12:29:56 +0200835 goto err_p2;
836 }
837
838 drm_debugfs_root = debugfs_create_dir("dri", NULL);
839 if (!drm_debugfs_root) {
840 DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
841 ret = -1;
842 goto err_p3;
843 }
844
845 DRM_INFO("Initialized %s %d.%d.%d %s\n",
846 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
847 return 0;
848err_p3:
849 drm_sysfs_destroy();
850err_p2:
851 unregister_chrdev(DRM_MAJOR, "drm");
852
853 idr_destroy(&drm_minors_idr);
854err_p1:
855 return ret;
856}
857
858static void __exit drm_core_exit(void)
859{
860 debugfs_remove(drm_debugfs_root);
861 drm_sysfs_destroy();
862
863 unregister_chrdev(DRM_MAJOR, "drm");
864
865 drm_connector_ida_destroy();
866 idr_destroy(&drm_minors_idr);
867}
868
869module_init(drm_core_init);
870module_exit(drm_core_exit);