blob: 66456698447ea84dd663cd22b3e5daf9435b7c7c [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>
David Herrmanne7b960702014-07-24 12:10:04 +020037#include "drm_legacy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Dave Airlieb5e89ed2005-09-25 14:28:13 +100039unsigned int drm_debug = 0; /* 1 to enable debug output */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040EXPORT_SYMBOL(drm_debug);
41
Mario Kleiner27641c32010-10-23 04:20:23 +020042unsigned int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
Mario Kleiner27641c32010-10-23 04:20:23 +020043
44unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
Mario Kleiner27641c32010-10-23 04:20:23 +020045
Imre Deakc61eef72012-10-23 18:53:26 +000046/*
47 * Default to use monotonic timestamps for wait-for-vblank and page-flip
48 * complete events.
49 */
50unsigned int drm_timestamp_monotonic = 1;
51
Dave Airlieb5e89ed2005-09-25 14:28:13 +100052MODULE_AUTHOR(CORE_AUTHOR);
53MODULE_DESCRIPTION(CORE_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054MODULE_LICENSE("GPL and additional rights");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_PARM_DESC(debug, "Enable debug output");
Mario Kleiner27641c32010-10-23 04:20:23 +020056MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs]");
57MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
Imre Deakc61eef72012-10-23 18:53:26 +000058MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Dave Jonesc0758142005-10-03 15:02:20 -040060module_param_named(debug, drm_debug, int, 0600);
Mario Kleiner27641c32010-10-23 04:20:23 +020061module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
62module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
Imre Deakc61eef72012-10-23 18:53:26 +000063module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
David Herrmann0d639882014-02-24 15:53:25 +010065static DEFINE_SPINLOCK(drm_minor_lock);
David Herrmann1b7199f2014-07-23 12:29:56 +020066static struct idr drm_minors_idr;
Dave Airlie2c14f282008-04-21 16:47:32 +100067
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -080068struct class *drm_class;
David Herrmann1b7199f2014-07-23 12:29:56 +020069static struct dentry *drm_debugfs_root;
Joe Perches5ad3d882011-04-17 20:35:51 -070070
71int drm_err(const char *func, const char *format, ...)
72{
73 struct va_format vaf;
74 va_list args;
75 int r;
76
77 va_start(args, format);
78
79 vaf.fmt = format;
80 vaf.va = &args;
81
82 r = printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* %pV", func, &vaf);
83
84 va_end(args);
85
86 return r;
87}
88EXPORT_SYMBOL(drm_err);
89
Lespiau, Damien1287aa92014-03-24 15:53:17 +000090void drm_ut_debug_printk(const char *function_name, const char *format, ...)
yakui_zhao4fefcb22009-06-02 14:09:47 +080091{
Daniel Vetterfffb9062013-11-17 22:25:02 +010092 struct va_format vaf;
yakui_zhao4fefcb22009-06-02 14:09:47 +080093 va_list args;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Lespiau, Damiena73d4e92014-03-24 15:53:15 +000095 va_start(args, format);
96 vaf.fmt = format;
97 vaf.va = &args;
Daniel Vetterfffb9062013-11-17 22:25:02 +010098
Lespiau, Damien0ed02982014-03-24 15:53:18 +000099 printk(KERN_DEBUG "[" DRM_NAME ":%s] %pV", function_name, &vaf);
Lespiau, Damiena73d4e92014-03-24 15:53:15 +0000100
101 va_end(args);
yakui_zhao4fefcb22009-06-02 14:09:47 +0800102}
103EXPORT_SYMBOL(drm_ut_debug_printk);
Joe Perches5ad3d882011-04-17 20:35:51 -0700104
Dave Airlie7c1c2872008-11-28 14:22:24 +1000105struct drm_master *drm_master_create(struct drm_minor *minor)
106{
107 struct drm_master *master;
108
Eric Anholt9a298b22009-03-24 12:23:04 -0700109 master = kzalloc(sizeof(*master), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000110 if (!master)
111 return NULL;
112
113 kref_init(&master->refcount);
114 spin_lock_init(&master->lock.spinlock);
115 init_waitqueue_head(&master->lock.lock_queue);
Daniel Vetter10e68562014-04-05 11:12:04 +0200116 if (drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER)) {
117 kfree(master);
118 return NULL;
119 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000120 INIT_LIST_HEAD(&master->magicfree);
121 master->minor = minor;
122
Dave Airlie7c1c2872008-11-28 14:22:24 +1000123 return master;
124}
125
126struct drm_master *drm_master_get(struct drm_master *master)
127{
128 kref_get(&master->refcount);
129 return master;
130}
Thomas Hellstrom85bb0c32009-12-06 21:46:28 +0100131EXPORT_SYMBOL(drm_master_get);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000132
133static void drm_master_destroy(struct kref *kref)
134{
135 struct drm_master *master = container_of(kref, struct drm_master, refcount);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000136 struct drm_device *dev = master->minor->dev;
Dave Airliec1ff85d2009-01-19 17:17:58 +1000137 struct drm_map_list *r_list, *list_temp;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000138
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100139 mutex_lock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000140 if (dev->driver->master_destroy)
141 dev->driver->master_destroy(dev, master);
142
Dave Airliec1ff85d2009-01-19 17:17:58 +1000143 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
144 if (r_list->master == master) {
David Herrmann9fc5cde2014-08-29 12:12:28 +0200145 drm_legacy_rmmap_locked(dev, r_list->map);
Dave Airliec1ff85d2009-01-19 17:17:58 +1000146 r_list = NULL;
147 }
148 }
149
Dave Airlie7c1c2872008-11-28 14:22:24 +1000150 if (master->unique) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700151 kfree(master->unique);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000152 master->unique = NULL;
153 master->unique_len = 0;
154 }
155
Dave Airlie7c1c2872008-11-28 14:22:24 +1000156 drm_ht_remove(&master->magiclist);
157
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100158 mutex_unlock(&dev->struct_mutex);
Eric Anholt9a298b22009-03-24 12:23:04 -0700159 kfree(master);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000160}
161
162void drm_master_put(struct drm_master **master)
163{
164 kref_put(&(*master)->refcount, drm_master_destroy);
165 *master = NULL;
166}
Thomas Hellstrom85bb0c32009-12-06 21:46:28 +0100167EXPORT_SYMBOL(drm_master_put);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000168
169int drm_setmaster_ioctl(struct drm_device *dev, void *data,
170 struct drm_file *file_priv)
171{
Benjamin Gaignard53ef1602013-06-26 17:58:59 +0200172 int ret = 0;
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000173
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100174 mutex_lock(&dev->master_mutex);
Dave Airlie7963e9d2014-08-08 07:30:53 +1000175 if (file_priv->is_master)
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100176 goto out_unlock;
Jonas Bonn6b008422009-04-16 09:00:02 +0200177
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100178 if (file_priv->minor->master) {
179 ret = -EINVAL;
180 goto out_unlock;
181 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000182
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100183 if (!file_priv->master) {
184 ret = -EINVAL;
185 goto out_unlock;
186 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000187
David Herrmann08bec5b2012-11-15 13:04:37 +0000188 file_priv->minor->master = drm_master_get(file_priv->master);
Dave Airlie7963e9d2014-08-08 07:30:53 +1000189 file_priv->is_master = 1;
David Herrmann08bec5b2012-11-15 13:04:37 +0000190 if (dev->driver->master_set) {
191 ret = dev->driver->master_set(dev, file_priv, false);
Dave Airlie7963e9d2014-08-08 07:30:53 +1000192 if (unlikely(ret != 0)) {
193 file_priv->is_master = 0;
David Herrmann08bec5b2012-11-15 13:04:37 +0000194 drm_master_put(&file_priv->minor->master);
Dave Airlie7963e9d2014-08-08 07:30:53 +1000195 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000196 }
197
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100198out_unlock:
199 mutex_unlock(&dev->master_mutex);
Benjamin Gaignard53ef1602013-06-26 17:58:59 +0200200 return ret;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000201}
202
203int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
204 struct drm_file *file_priv)
205{
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100206 int ret = -EINVAL;
207
208 mutex_lock(&dev->master_mutex);
Dave Airlie7963e9d2014-08-08 07:30:53 +1000209 if (!file_priv->is_master)
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100210 goto out_unlock;
Jonas Bonn6b008422009-04-16 09:00:02 +0200211
Dave Airlie07f1c7a2009-04-20 09:32:50 +1000212 if (!file_priv->minor->master)
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100213 goto out_unlock;
Dave Airlie07f1c7a2009-04-20 09:32:50 +1000214
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100215 ret = 0;
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000216 if (dev->driver->master_drop)
217 dev->driver->master_drop(dev, file_priv, false);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000218 drm_master_put(&file_priv->minor->master);
Dave Airlie7963e9d2014-08-08 07:30:53 +1000219 file_priv->is_master = 0;
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100220
221out_unlock:
222 mutex_unlock(&dev->master_mutex);
223 return ret;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000224}
225
David Herrmann0d639882014-02-24 15:53:25 +0100226/*
227 * DRM Minors
228 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
229 * of them is represented by a drm_minor object. Depending on the capabilities
230 * of the device-driver, different interfaces are registered.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 *
David Herrmann0d639882014-02-24 15:53:25 +0100232 * Minors can be accessed via dev->$minor_name. This pointer is either
233 * NULL or a valid drm_minor pointer and stays valid as long as the device is
234 * valid. This means, DRM minors have the same life-time as the underlying
235 * device. However, this doesn't mean that the minor is active. Minors are
236 * registered and unregistered dynamically according to device-state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
David Herrmann0d639882014-02-24 15:53:25 +0100238
David Herrmann05b701f2014-01-29 12:43:56 +0100239static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
240 unsigned int type)
241{
242 switch (type) {
243 case DRM_MINOR_LEGACY:
244 return &dev->primary;
245 case DRM_MINOR_RENDER:
246 return &dev->render;
247 case DRM_MINOR_CONTROL:
248 return &dev->control;
249 default:
250 return NULL;
251 }
252}
253
254static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
255{
256 struct drm_minor *minor;
David Herrmannf1b85962014-07-23 10:34:52 +0200257 unsigned long flags;
258 int r;
David Herrmann05b701f2014-01-29 12:43:56 +0100259
260 minor = kzalloc(sizeof(*minor), GFP_KERNEL);
261 if (!minor)
262 return -ENOMEM;
263
264 minor->type = type;
265 minor->dev = dev;
David Herrmann05b701f2014-01-29 12:43:56 +0100266
David Herrmannf1b85962014-07-23 10:34:52 +0200267 idr_preload(GFP_KERNEL);
268 spin_lock_irqsave(&drm_minor_lock, flags);
269 r = idr_alloc(&drm_minors_idr,
270 NULL,
271 64 * type,
272 64 * (type + 1),
273 GFP_NOWAIT);
274 spin_unlock_irqrestore(&drm_minor_lock, flags);
275 idr_preload_end();
276
277 if (r < 0)
278 goto err_free;
279
280 minor->index = r;
281
David Herrmanne1728072014-07-23 11:38:38 +0200282 minor->kdev = drm_sysfs_minor_alloc(minor);
283 if (IS_ERR(minor->kdev)) {
284 r = PTR_ERR(minor->kdev);
285 goto err_index;
286 }
287
David Herrmann05b701f2014-01-29 12:43:56 +0100288 *drm_minor_get_slot(dev, type) = minor;
289 return 0;
David Herrmannf1b85962014-07-23 10:34:52 +0200290
David Herrmanne1728072014-07-23 11:38:38 +0200291err_index:
292 spin_lock_irqsave(&drm_minor_lock, flags);
293 idr_remove(&drm_minors_idr, minor->index);
294 spin_unlock_irqrestore(&drm_minor_lock, flags);
David Herrmannf1b85962014-07-23 10:34:52 +0200295err_free:
296 kfree(minor);
297 return r;
David Herrmann05b701f2014-01-29 12:43:56 +0100298}
299
David Herrmannbd9dfa92014-01-29 12:55:48 +0100300static void drm_minor_free(struct drm_device *dev, unsigned int type)
301{
David Herrmannf1b85962014-07-23 10:34:52 +0200302 struct drm_minor **slot, *minor;
303 unsigned long flags;
David Herrmannbd9dfa92014-01-29 12:55:48 +0100304
305 slot = drm_minor_get_slot(dev, type);
David Herrmannf1b85962014-07-23 10:34:52 +0200306 minor = *slot;
307 if (!minor)
308 return;
309
310 drm_mode_group_destroy(&minor->mode_group);
David Herrmanne1728072014-07-23 11:38:38 +0200311 put_device(minor->kdev);
David Herrmannf1b85962014-07-23 10:34:52 +0200312
313 spin_lock_irqsave(&drm_minor_lock, flags);
314 idr_remove(&drm_minors_idr, minor->index);
315 spin_unlock_irqrestore(&drm_minor_lock, flags);
316
317 kfree(minor);
318 *slot = NULL;
David Herrmannbd9dfa92014-01-29 12:55:48 +0100319}
320
David Herrmannafcdbc82014-01-29 12:57:05 +0100321static int drm_minor_register(struct drm_device *dev, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
David Herrmannf1b85962014-07-23 10:34:52 +0200323 struct drm_minor *minor;
David Herrmann0d639882014-02-24 15:53:25 +0100324 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 DRM_DEBUG("\n");
328
David Herrmannf1b85962014-07-23 10:34:52 +0200329 minor = *drm_minor_get_slot(dev, type);
330 if (!minor)
David Herrmann05b701f2014-01-29 12:43:56 +0100331 return 0;
332
David Herrmannf1b85962014-07-23 10:34:52 +0200333 ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
Ben Gamari955b12d2009-02-17 20:08:49 -0500334 if (ret) {
GeunSik Lim156f5a72009-06-02 15:01:37 +0900335 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
David Herrmannf1b85962014-07-23 10:34:52 +0200336 return ret;
Ben Gamari955b12d2009-02-17 20:08:49 -0500337 }
Dave Airlie2c14f282008-04-21 16:47:32 +1000338
David Herrmanne1728072014-07-23 11:38:38 +0200339 ret = device_add(minor->kdev);
340 if (ret)
Daniel Vettercb6458f2013-08-08 15:41:34 +0200341 goto err_debugfs;
Dave Airlie2c14f282008-04-21 16:47:32 +1000342
David Herrmann0d639882014-02-24 15:53:25 +0100343 /* replace NULL with @minor so lookups will succeed from now on */
344 spin_lock_irqsave(&drm_minor_lock, flags);
David Herrmannf1b85962014-07-23 10:34:52 +0200345 idr_replace(&drm_minors_idr, minor, minor->index);
David Herrmann0d639882014-02-24 15:53:25 +0100346 spin_unlock_irqrestore(&drm_minor_lock, flags);
Dave Airlie2c14f282008-04-21 16:47:32 +1000347
David Herrmannf1b85962014-07-23 10:34:52 +0200348 DRM_DEBUG("new minor registered %d\n", minor->index);
Dave Airlie2c14f282008-04-21 16:47:32 +1000349 return 0;
350
Daniel Vettercb6458f2013-08-08 15:41:34 +0200351err_debugfs:
David Herrmannf1b85962014-07-23 10:34:52 +0200352 drm_debugfs_cleanup(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return ret;
354}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000355
David Herrmannafcdbc82014-01-29 12:57:05 +0100356static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
David Herrmannf73aca52013-10-20 18:55:40 +0200357{
David Herrmannafcdbc82014-01-29 12:57:05 +0100358 struct drm_minor *minor;
David Herrmann0d639882014-02-24 15:53:25 +0100359 unsigned long flags;
David Herrmannafcdbc82014-01-29 12:57:05 +0100360
361 minor = *drm_minor_get_slot(dev, type);
David Herrmanne1728072014-07-23 11:38:38 +0200362 if (!minor || !device_is_registered(minor->kdev))
David Herrmannf73aca52013-10-20 18:55:40 +0200363 return;
364
David Herrmannf1b85962014-07-23 10:34:52 +0200365 /* replace @minor with NULL so lookups will fail from now on */
David Herrmann0d639882014-02-24 15:53:25 +0100366 spin_lock_irqsave(&drm_minor_lock, flags);
David Herrmannf1b85962014-07-23 10:34:52 +0200367 idr_replace(&drm_minors_idr, NULL, minor->index);
David Herrmann0d639882014-02-24 15:53:25 +0100368 spin_unlock_irqrestore(&drm_minor_lock, flags);
David Herrmann0d639882014-02-24 15:53:25 +0100369
David Herrmanne1728072014-07-23 11:38:38 +0200370 device_del(minor->kdev);
371 dev_set_drvdata(minor->kdev, NULL); /* safety belt */
David Herrmann865fb472013-10-20 18:55:43 +0200372 drm_debugfs_cleanup(minor);
David Herrmannf73aca52013-10-20 18:55:40 +0200373}
374
375/**
David Herrmann1616c522014-01-29 10:49:19 +0100376 * drm_minor_acquire - Acquire a DRM minor
377 * @minor_id: Minor ID of the DRM-minor
David Herrmannf73aca52013-10-20 18:55:40 +0200378 *
David Herrmann1616c522014-01-29 10:49:19 +0100379 * Looks up the given minor-ID and returns the respective DRM-minor object. The
380 * refence-count of the underlying device is increased so you must release this
381 * object with drm_minor_release().
382 *
383 * As long as you hold this minor, it is guaranteed that the object and the
384 * minor->dev pointer will stay valid! However, the device may get unplugged and
385 * unregistered while you hold the minor.
386 *
387 * Returns:
388 * Pointer to minor-object with increased device-refcount, or PTR_ERR on
389 * failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
David Herrmann1616c522014-01-29 10:49:19 +0100391struct drm_minor *drm_minor_acquire(unsigned int minor_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
David Herrmann1616c522014-01-29 10:49:19 +0100393 struct drm_minor *minor;
David Herrmann0d639882014-02-24 15:53:25 +0100394 unsigned long flags;
Eric Anholt673a3942008-07-30 12:06:12 -0700395
David Herrmann0d639882014-02-24 15:53:25 +0100396 spin_lock_irqsave(&drm_minor_lock, flags);
David Herrmann1616c522014-01-29 10:49:19 +0100397 minor = idr_find(&drm_minors_idr, minor_id);
David Herrmann0d639882014-02-24 15:53:25 +0100398 if (minor)
399 drm_dev_ref(minor->dev);
400 spin_unlock_irqrestore(&drm_minor_lock, flags);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000401
David Herrmann0d639882014-02-24 15:53:25 +0100402 if (!minor) {
403 return ERR_PTR(-ENODEV);
404 } else if (drm_device_is_unplugged(minor->dev)) {
405 drm_dev_unref(minor->dev);
406 return ERR_PTR(-ENODEV);
407 }
408
David Herrmann1616c522014-01-29 10:49:19 +0100409 return minor;
410}
411
412/**
413 * drm_minor_release - Release DRM minor
414 * @minor: Pointer to DRM minor object
415 *
416 * Release a minor that was previously acquired via drm_minor_acquire().
417 */
418void drm_minor_release(struct drm_minor *minor)
419{
420 drm_dev_unref(minor->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500422
423/**
Thierry Redingc6a1af8a2014-05-19 13:39:07 +0200424 * drm_put_dev - Unregister and release a DRM device
425 * @dev: DRM device
426 *
427 * Called at module unload time or when a PCI device is unplugged.
428 *
429 * Use of this function is discouraged. It will eventually go away completely.
430 * Please use drm_dev_unregister() and drm_dev_unref() explicitly instead.
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500431 *
432 * Cleans up all DRM device, calling drm_lastclose().
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500433 */
434void drm_put_dev(struct drm_device *dev)
435{
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500436 DRM_DEBUG("\n");
437
438 if (!dev) {
439 DRM_ERROR("cleanup called no dev\n");
440 return;
441 }
442
David Herrmannc3a49732013-10-02 11:23:38 +0200443 drm_dev_unregister(dev);
David Herrmann099d1c22014-01-29 10:21:36 +0100444 drm_dev_unref(dev);
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500445}
446EXPORT_SYMBOL(drm_put_dev);
Dave Airlie2c07a212012-02-20 14:18:07 +0000447
448void drm_unplug_dev(struct drm_device *dev)
449{
450 /* for a USB device */
David Herrmannafcdbc82014-01-29 12:57:05 +0100451 drm_minor_unregister(dev, DRM_MINOR_LEGACY);
452 drm_minor_unregister(dev, DRM_MINOR_RENDER);
453 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
Dave Airlie2c07a212012-02-20 14:18:07 +0000454
455 mutex_lock(&drm_global_mutex);
456
457 drm_device_set_unplugged(dev);
458
459 if (dev->open_count == 0) {
460 drm_put_dev(dev);
461 }
462 mutex_unlock(&drm_global_mutex);
463}
464EXPORT_SYMBOL(drm_unplug_dev);
David Herrmann1bb72532013-10-02 11:23:34 +0200465
David Herrmann31bbe162014-01-03 14:09:47 +0100466/*
467 * DRM internal mount
468 * We want to be able to allocate our own "struct address_space" to control
469 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
470 * stand-alone address_space objects, so we need an underlying inode. As there
471 * is no way to allocate an independent inode easily, we need a fake internal
472 * VFS mount-point.
473 *
474 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
475 * frees it again. You are allowed to use iget() and iput() to get references to
476 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
477 * drm_fs_inode_free() call (which does not have to be the last iput()).
478 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
479 * between multiple inode-users. You could, technically, call
480 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
481 * iput(), but this way you'd end up with a new vfsmount for each inode.
482 */
483
484static int drm_fs_cnt;
485static struct vfsmount *drm_fs_mnt;
486
487static const struct dentry_operations drm_fs_dops = {
488 .d_dname = simple_dname,
489};
490
491static const struct super_operations drm_fs_sops = {
492 .statfs = simple_statfs,
493};
494
495static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
496 const char *dev_name, void *data)
497{
498 return mount_pseudo(fs_type,
499 "drm:",
500 &drm_fs_sops,
501 &drm_fs_dops,
502 0x010203ff);
503}
504
505static struct file_system_type drm_fs_type = {
506 .name = "drm",
507 .owner = THIS_MODULE,
508 .mount = drm_fs_mount,
509 .kill_sb = kill_anon_super,
510};
511
512static struct inode *drm_fs_inode_new(void)
513{
514 struct inode *inode;
515 int r;
516
517 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
518 if (r < 0) {
519 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
520 return ERR_PTR(r);
521 }
522
523 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
524 if (IS_ERR(inode))
525 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
526
527 return inode;
528}
529
530static void drm_fs_inode_free(struct inode *inode)
531{
532 if (inode) {
533 iput(inode);
534 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
535 }
536}
537
David Herrmann1bb72532013-10-02 11:23:34 +0200538/**
Thierry Redingc6a1af8a2014-05-19 13:39:07 +0200539 * drm_dev_alloc - Allocate new DRM device
David Herrmann1bb72532013-10-02 11:23:34 +0200540 * @driver: DRM driver to allocate device for
541 * @parent: Parent device object
542 *
543 * Allocate and initialize a new DRM device. No device registration is done.
David Herrmannc22f0ac2013-10-02 11:23:35 +0200544 * Call drm_dev_register() to advertice the device to user space and register it
545 * with other core subsystems.
David Herrmann1bb72532013-10-02 11:23:34 +0200546 *
David Herrmann099d1c22014-01-29 10:21:36 +0100547 * The initial ref-count of the object is 1. Use drm_dev_ref() and
548 * drm_dev_unref() to take and drop further ref-counts.
549 *
David Herrmann1bb72532013-10-02 11:23:34 +0200550 * RETURNS:
551 * Pointer to new DRM device, or NULL if out of memory.
552 */
553struct drm_device *drm_dev_alloc(struct drm_driver *driver,
554 struct device *parent)
555{
556 struct drm_device *dev;
557 int ret;
558
559 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
560 if (!dev)
561 return NULL;
562
David Herrmann099d1c22014-01-29 10:21:36 +0100563 kref_init(&dev->ref);
David Herrmann1bb72532013-10-02 11:23:34 +0200564 dev->dev = parent;
565 dev->driver = driver;
566
567 INIT_LIST_HEAD(&dev->filelist);
568 INIT_LIST_HEAD(&dev->ctxlist);
569 INIT_LIST_HEAD(&dev->vmalist);
570 INIT_LIST_HEAD(&dev->maplist);
571 INIT_LIST_HEAD(&dev->vblank_event_list);
572
Daniel Vetter2177a212013-12-16 11:21:06 +0100573 spin_lock_init(&dev->buf_lock);
David Herrmann1bb72532013-10-02 11:23:34 +0200574 spin_lock_init(&dev->event_lock);
575 mutex_init(&dev->struct_mutex);
576 mutex_init(&dev->ctxlist_mutex);
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100577 mutex_init(&dev->master_mutex);
David Herrmann1bb72532013-10-02 11:23:34 +0200578
David Herrmann6796cb12014-01-03 14:24:19 +0100579 dev->anon_inode = drm_fs_inode_new();
580 if (IS_ERR(dev->anon_inode)) {
581 ret = PTR_ERR(dev->anon_inode);
582 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
David Herrmann1bb72532013-10-02 11:23:34 +0200583 goto err_free;
David Herrmann6796cb12014-01-03 14:24:19 +0100584 }
585
David Herrmann05b701f2014-01-29 12:43:56 +0100586 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
587 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
588 if (ret)
589 goto err_minors;
590 }
591
David Herrmann6d6dfcf2014-03-16 14:38:40 +0100592 if (drm_core_check_feature(dev, DRIVER_RENDER)) {
David Herrmann05b701f2014-01-29 12:43:56 +0100593 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
594 if (ret)
595 goto err_minors;
596 }
597
598 ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
599 if (ret)
600 goto err_minors;
601
David Herrmann6796cb12014-01-03 14:24:19 +0100602 if (drm_ht_create(&dev->map_hash, 12))
David Herrmann05b701f2014-01-29 12:43:56 +0100603 goto err_minors;
David Herrmann1bb72532013-10-02 11:23:34 +0200604
David Herrmanne7b960702014-07-24 12:10:04 +0200605 ret = drm_legacy_ctxbitmap_init(dev);
David Herrmann1bb72532013-10-02 11:23:34 +0200606 if (ret) {
607 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
608 goto err_ht;
609 }
610
611 if (driver->driver_features & DRIVER_GEM) {
612 ret = drm_gem_init(dev);
613 if (ret) {
614 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
615 goto err_ctxbitmap;
616 }
617 }
618
619 return dev;
620
621err_ctxbitmap:
David Herrmanne7b960702014-07-24 12:10:04 +0200622 drm_legacy_ctxbitmap_cleanup(dev);
David Herrmann1bb72532013-10-02 11:23:34 +0200623err_ht:
624 drm_ht_remove(&dev->map_hash);
David Herrmann05b701f2014-01-29 12:43:56 +0100625err_minors:
David Herrmannbd9dfa92014-01-29 12:55:48 +0100626 drm_minor_free(dev, DRM_MINOR_LEGACY);
627 drm_minor_free(dev, DRM_MINOR_RENDER);
628 drm_minor_free(dev, DRM_MINOR_CONTROL);
David Herrmann6796cb12014-01-03 14:24:19 +0100629 drm_fs_inode_free(dev->anon_inode);
David Herrmann1bb72532013-10-02 11:23:34 +0200630err_free:
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100631 mutex_destroy(&dev->master_mutex);
David Herrmann1bb72532013-10-02 11:23:34 +0200632 kfree(dev);
633 return NULL;
634}
635EXPORT_SYMBOL(drm_dev_alloc);
David Herrmannc22f0ac2013-10-02 11:23:35 +0200636
David Herrmann099d1c22014-01-29 10:21:36 +0100637static void drm_dev_release(struct kref *ref)
David Herrmann0dc8fe52013-10-02 11:23:37 +0200638{
David Herrmann099d1c22014-01-29 10:21:36 +0100639 struct drm_device *dev = container_of(ref, struct drm_device, ref);
David Herrmann8f6599d2013-10-20 18:55:45 +0200640
David Herrmann0dc8fe52013-10-02 11:23:37 +0200641 if (dev->driver->driver_features & DRIVER_GEM)
642 drm_gem_destroy(dev);
643
David Herrmanne7b960702014-07-24 12:10:04 +0200644 drm_legacy_ctxbitmap_cleanup(dev);
David Herrmann0dc8fe52013-10-02 11:23:37 +0200645 drm_ht_remove(&dev->map_hash);
David Herrmann6796cb12014-01-03 14:24:19 +0100646 drm_fs_inode_free(dev->anon_inode);
David Herrmann0dc8fe52013-10-02 11:23:37 +0200647
David Herrmannbd9dfa92014-01-29 12:55:48 +0100648 drm_minor_free(dev, DRM_MINOR_LEGACY);
649 drm_minor_free(dev, DRM_MINOR_RENDER);
650 drm_minor_free(dev, DRM_MINOR_CONTROL);
651
Thomas Hellstromc996fd02014-02-25 19:57:44 +0100652 mutex_destroy(&dev->master_mutex);
Thierry Redingca8e2ad2014-04-11 15:23:00 +0200653 kfree(dev->unique);
David Herrmann0dc8fe52013-10-02 11:23:37 +0200654 kfree(dev);
655}
David Herrmann099d1c22014-01-29 10:21:36 +0100656
657/**
658 * drm_dev_ref - Take reference of a DRM device
659 * @dev: device to take reference of or NULL
660 *
661 * This increases the ref-count of @dev by one. You *must* already own a
662 * reference when calling this. Use drm_dev_unref() to drop this reference
663 * again.
664 *
665 * This function never fails. However, this function does not provide *any*
666 * guarantee whether the device is alive or running. It only provides a
667 * reference to the object and the memory associated with it.
668 */
669void drm_dev_ref(struct drm_device *dev)
670{
671 if (dev)
672 kref_get(&dev->ref);
673}
674EXPORT_SYMBOL(drm_dev_ref);
675
676/**
677 * drm_dev_unref - Drop reference of a DRM device
678 * @dev: device to drop reference of or NULL
679 *
680 * This decreases the ref-count of @dev by one. The device is destroyed if the
681 * ref-count drops to zero.
682 */
683void drm_dev_unref(struct drm_device *dev)
684{
685 if (dev)
686 kref_put(&dev->ref, drm_dev_release);
687}
688EXPORT_SYMBOL(drm_dev_unref);
David Herrmann0dc8fe52013-10-02 11:23:37 +0200689
690/**
David Herrmannc22f0ac2013-10-02 11:23:35 +0200691 * drm_dev_register - Register DRM device
692 * @dev: Device to register
Thierry Redingc6a1af8a2014-05-19 13:39:07 +0200693 * @flags: Flags passed to the driver's .load() function
David Herrmannc22f0ac2013-10-02 11:23:35 +0200694 *
695 * Register the DRM device @dev with the system, advertise device to user-space
696 * and start normal device operation. @dev must be allocated via drm_dev_alloc()
697 * previously.
698 *
699 * Never call this twice on any device!
700 *
701 * RETURNS:
702 * 0 on success, negative error code on failure.
703 */
704int drm_dev_register(struct drm_device *dev, unsigned long flags)
705{
706 int ret;
707
708 mutex_lock(&drm_global_mutex);
709
David Herrmannafcdbc82014-01-29 12:57:05 +0100710 ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
David Herrmannc22f0ac2013-10-02 11:23:35 +0200711 if (ret)
David Herrmann05b701f2014-01-29 12:43:56 +0100712 goto err_minors;
713
David Herrmannafcdbc82014-01-29 12:57:05 +0100714 ret = drm_minor_register(dev, DRM_MINOR_RENDER);
David Herrmann05b701f2014-01-29 12:43:56 +0100715 if (ret)
716 goto err_minors;
717
David Herrmannafcdbc82014-01-29 12:57:05 +0100718 ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
David Herrmann05b701f2014-01-29 12:43:56 +0100719 if (ret)
720 goto err_minors;
David Herrmannc22f0ac2013-10-02 11:23:35 +0200721
722 if (dev->driver->load) {
723 ret = dev->driver->load(dev, flags);
724 if (ret)
David Herrmann05b701f2014-01-29 12:43:56 +0100725 goto err_minors;
David Herrmannc22f0ac2013-10-02 11:23:35 +0200726 }
727
728 /* setup grouping for legacy outputs */
729 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
730 ret = drm_mode_group_init_legacy_group(dev,
731 &dev->primary->mode_group);
732 if (ret)
733 goto err_unload;
734 }
735
David Herrmannc22f0ac2013-10-02 11:23:35 +0200736 ret = 0;
737 goto out_unlock;
738
739err_unload:
740 if (dev->driver->unload)
741 dev->driver->unload(dev);
David Herrmann05b701f2014-01-29 12:43:56 +0100742err_minors:
David Herrmannafcdbc82014-01-29 12:57:05 +0100743 drm_minor_unregister(dev, DRM_MINOR_LEGACY);
744 drm_minor_unregister(dev, DRM_MINOR_RENDER);
745 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
David Herrmannc22f0ac2013-10-02 11:23:35 +0200746out_unlock:
747 mutex_unlock(&drm_global_mutex);
748 return ret;
749}
750EXPORT_SYMBOL(drm_dev_register);
David Herrmannc3a49732013-10-02 11:23:38 +0200751
752/**
753 * drm_dev_unregister - Unregister DRM device
754 * @dev: Device to unregister
755 *
756 * Unregister the DRM device from the system. This does the reverse of
757 * drm_dev_register() but does not deallocate the device. The caller must call
David Herrmann099d1c22014-01-29 10:21:36 +0100758 * drm_dev_unref() to drop their final reference.
David Herrmannc3a49732013-10-02 11:23:38 +0200759 */
760void drm_dev_unregister(struct drm_device *dev)
761{
762 struct drm_map_list *r_list, *list_temp;
763
764 drm_lastclose(dev);
765
766 if (dev->driver->unload)
767 dev->driver->unload(dev);
768
Daniel Vetter4efafeb2013-12-11 11:34:38 +0100769 if (dev->agp)
770 drm_pci_agp_destroy(dev);
David Herrmannc3a49732013-10-02 11:23:38 +0200771
772 drm_vblank_cleanup(dev);
773
774 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
David Herrmann9fc5cde2014-08-29 12:12:28 +0200775 drm_legacy_rmmap(dev, r_list->map);
David Herrmannc3a49732013-10-02 11:23:38 +0200776
David Herrmannafcdbc82014-01-29 12:57:05 +0100777 drm_minor_unregister(dev, DRM_MINOR_LEGACY);
778 drm_minor_unregister(dev, DRM_MINOR_RENDER);
779 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
David Herrmannc3a49732013-10-02 11:23:38 +0200780}
781EXPORT_SYMBOL(drm_dev_unregister);
Thierry Redingca8e2ad2014-04-11 15:23:00 +0200782
783/**
784 * drm_dev_set_unique - Set the unique name of a DRM device
785 * @dev: device of which to set the unique name
786 * @fmt: format string for unique name
787 *
788 * Sets the unique name of a DRM device using the specified format string and
789 * a variable list of arguments. Drivers can use this at driver probe time if
790 * the unique name of the devices they drive is static.
791 *
792 * Return: 0 on success or a negative error code on failure.
793 */
794int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...)
795{
796 va_list ap;
797
798 kfree(dev->unique);
799
800 va_start(ap, fmt);
801 dev->unique = kvasprintf(GFP_KERNEL, fmt, ap);
802 va_end(ap);
803
804 return dev->unique ? 0 : -ENOMEM;
805}
806EXPORT_SYMBOL(drm_dev_set_unique);
David Herrmann1b7199f2014-07-23 12:29:56 +0200807
808/*
809 * DRM Core
810 * The DRM core module initializes all global DRM objects and makes them
811 * available to drivers. Once setup, drivers can probe their respective
812 * devices.
813 * Currently, core management includes:
814 * - The "DRM-Global" key/value database
815 * - Global ID management for connectors
816 * - DRM major number allocation
817 * - DRM minor management
818 * - DRM sysfs class
819 * - DRM debugfs root
820 *
821 * Furthermore, the DRM core provides dynamic char-dev lookups. For each
822 * interface registered on a DRM device, you can request minor numbers from DRM
823 * core. DRM core takes care of major-number management and char-dev
824 * registration. A stub ->open() callback forwards any open() requests to the
825 * registered minor.
826 */
827
828static int drm_stub_open(struct inode *inode, struct file *filp)
829{
830 const struct file_operations *new_fops;
831 struct drm_minor *minor;
832 int err;
833
834 DRM_DEBUG("\n");
835
836 mutex_lock(&drm_global_mutex);
837 minor = drm_minor_acquire(iminor(inode));
838 if (IS_ERR(minor)) {
839 err = PTR_ERR(minor);
840 goto out_unlock;
841 }
842
843 new_fops = fops_get(minor->dev->driver->fops);
844 if (!new_fops) {
845 err = -ENODEV;
846 goto out_release;
847 }
848
849 replace_fops(filp, new_fops);
850 if (filp->f_op->open)
851 err = filp->f_op->open(inode, filp);
852 else
853 err = 0;
854
855out_release:
856 drm_minor_release(minor);
857out_unlock:
858 mutex_unlock(&drm_global_mutex);
859 return err;
860}
861
862static const struct file_operations drm_stub_fops = {
863 .owner = THIS_MODULE,
864 .open = drm_stub_open,
865 .llseek = noop_llseek,
866};
867
868static int __init drm_core_init(void)
869{
870 int ret = -ENOMEM;
871
872 drm_global_init();
873 drm_connector_ida_init();
874 idr_init(&drm_minors_idr);
875
876 if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
877 goto err_p1;
878
879 drm_class = drm_sysfs_create(THIS_MODULE, "drm");
880 if (IS_ERR(drm_class)) {
881 printk(KERN_ERR "DRM: Error creating drm class.\n");
882 ret = PTR_ERR(drm_class);
883 goto err_p2;
884 }
885
886 drm_debugfs_root = debugfs_create_dir("dri", NULL);
887 if (!drm_debugfs_root) {
888 DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
889 ret = -1;
890 goto err_p3;
891 }
892
893 DRM_INFO("Initialized %s %d.%d.%d %s\n",
894 CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
895 return 0;
896err_p3:
897 drm_sysfs_destroy();
898err_p2:
899 unregister_chrdev(DRM_MAJOR, "drm");
900
901 idr_destroy(&drm_minors_idr);
902err_p1:
903 return ret;
904}
905
906static void __exit drm_core_exit(void)
907{
908 debugfs_remove(drm_debugfs_root);
909 drm_sysfs_destroy();
910
911 unregister_chrdev(DRM_MAJOR, "drm");
912
913 drm_connector_ida_destroy();
914 idr_destroy(&drm_minors_idr);
915}
916
917module_init(drm_core_init);
918module_exit(drm_core_exit);