blob: f2903d7e3d8e50dc4f2f87544f1879e23f0f8a95 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * \file drm_stub.h
3 * Stub support
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 */
7
8/*
9 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
10 *
11 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
23 * Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
32 */
33
David Herrmann31bbe162014-01-03 14:09:47 +010034#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/moduleparam.h>
David Herrmann31bbe162014-01-03 14:09:47 +010037#include <linux/mount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Howells760285e2012-10-02 18:01:07 +010039#include <drm/drmP.h>
40#include <drm/drm_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Dave Airlieb5e89ed2005-09-25 14:28:13 +100042unsigned int drm_debug = 0; /* 1 to enable debug output */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043EXPORT_SYMBOL(drm_debug);
44
David Herrmann17931262013-08-25 18:29:00 +020045unsigned int drm_rnodes = 0; /* 1 to enable experimental render nodes API */
46EXPORT_SYMBOL(drm_rnodes);
47
Mario Kleiner27641c32010-10-23 04:20:23 +020048unsigned int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
49EXPORT_SYMBOL(drm_vblank_offdelay);
50
51unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
52EXPORT_SYMBOL(drm_timestamp_precision);
53
Imre Deakc61eef72012-10-23 18:53:26 +000054/*
55 * Default to use monotonic timestamps for wait-for-vblank and page-flip
56 * complete events.
57 */
58unsigned int drm_timestamp_monotonic = 1;
59
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060MODULE_AUTHOR(CORE_AUTHOR);
61MODULE_DESCRIPTION(CORE_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062MODULE_LICENSE("GPL and additional rights");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063MODULE_PARM_DESC(debug, "Enable debug output");
David Herrmann17931262013-08-25 18:29:00 +020064MODULE_PARM_DESC(rnodes, "Enable experimental render nodes API");
Mario Kleiner27641c32010-10-23 04:20:23 +020065MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs]");
66MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
Imre Deakc61eef72012-10-23 18:53:26 +000067MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Dave Jonesc0758142005-10-03 15:02:20 -040069module_param_named(debug, drm_debug, int, 0600);
David Herrmann17931262013-08-25 18:29:00 +020070module_param_named(rnodes, drm_rnodes, int, 0600);
Mario Kleiner27641c32010-10-23 04:20:23 +020071module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
72module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
Imre Deakc61eef72012-10-23 18:53:26 +000073module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Dave Airlie2c14f282008-04-21 16:47:32 +100075struct idr drm_minors_idr;
76
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -080077struct class *drm_class;
Ben Gamari955b12d2009-02-17 20:08:49 -050078struct dentry *drm_debugfs_root;
Joe Perches5ad3d882011-04-17 20:35:51 -070079
80int drm_err(const char *func, const char *format, ...)
81{
82 struct va_format vaf;
83 va_list args;
84 int r;
85
86 va_start(args, format);
87
88 vaf.fmt = format;
89 vaf.va = &args;
90
91 r = printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* %pV", func, &vaf);
92
93 va_end(args);
94
95 return r;
96}
97EXPORT_SYMBOL(drm_err);
98
yakui_zhao4fefcb22009-06-02 14:09:47 +080099void drm_ut_debug_printk(unsigned int request_level,
100 const char *prefix,
101 const char *function_name,
102 const char *format, ...)
103{
Daniel Vetterfffb9062013-11-17 22:25:02 +0100104 struct va_format vaf;
yakui_zhao4fefcb22009-06-02 14:09:47 +0800105 va_list args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
yakui_zhao4fefcb22009-06-02 14:09:47 +0800107 if (drm_debug & request_level) {
yakui_zhao4fefcb22009-06-02 14:09:47 +0800108 va_start(args, format);
Daniel Vetterfffb9062013-11-17 22:25:02 +0100109 vaf.fmt = format;
110 vaf.va = &args;
111
112 if (function_name)
113 printk(KERN_DEBUG "[%s:%s], %pV", prefix,
114 function_name, &vaf);
115 else
116 printk(KERN_DEBUG "%pV", &vaf);
yakui_zhao4fefcb22009-06-02 14:09:47 +0800117 va_end(args);
118 }
119}
120EXPORT_SYMBOL(drm_ut_debug_printk);
Joe Perches5ad3d882011-04-17 20:35:51 -0700121
Dave Airlie2c14f282008-04-21 16:47:32 +1000122static int drm_minor_get_id(struct drm_device *dev, int type)
123{
Dave Airlie2c14f282008-04-21 16:47:32 +1000124 int ret;
125 int base = 0, limit = 63;
126
Dave Airlief453ba02008-11-07 14:05:41 -0800127 if (type == DRM_MINOR_CONTROL) {
Kristian Høgsberg24f40032013-08-08 19:10:21 +0200128 base += 64;
129 limit = base + 63;
130 } else if (type == DRM_MINOR_RENDER) {
131 base += 128;
132 limit = base + 63;
133 }
Dave Airlief453ba02008-11-07 14:05:41 -0800134
Dave Airlie2c14f282008-04-21 16:47:32 +1000135 mutex_lock(&dev->struct_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800136 ret = idr_alloc(&drm_minors_idr, NULL, base, limit, GFP_KERNEL);
Dave Airlie2c14f282008-04-21 16:47:32 +1000137 mutex_unlock(&dev->struct_mutex);
Dave Airlie2c14f282008-04-21 16:47:32 +1000138
Tejun Heo2e928812013-02-27 17:04:08 -0800139 return ret == -ENOSPC ? -EINVAL : ret;
Dave Airlie2c14f282008-04-21 16:47:32 +1000140}
141
Dave Airlie7c1c2872008-11-28 14:22:24 +1000142struct drm_master *drm_master_create(struct drm_minor *minor)
143{
144 struct drm_master *master;
145
Eric Anholt9a298b22009-03-24 12:23:04 -0700146 master = kzalloc(sizeof(*master), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000147 if (!master)
148 return NULL;
149
150 kref_init(&master->refcount);
151 spin_lock_init(&master->lock.spinlock);
152 init_waitqueue_head(&master->lock.lock_queue);
153 drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
154 INIT_LIST_HEAD(&master->magicfree);
155 master->minor = minor;
156
157 list_add_tail(&master->head, &minor->master_list);
158
159 return master;
160}
161
162struct drm_master *drm_master_get(struct drm_master *master)
163{
164 kref_get(&master->refcount);
165 return master;
166}
Thomas Hellstrom85bb0c32009-12-06 21:46:28 +0100167EXPORT_SYMBOL(drm_master_get);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000168
169static void drm_master_destroy(struct kref *kref)
170{
171 struct drm_master *master = container_of(kref, struct drm_master, refcount);
172 struct drm_magic_entry *pt, *next;
173 struct drm_device *dev = master->minor->dev;
Dave Airliec1ff85d2009-01-19 17:17:58 +1000174 struct drm_map_list *r_list, *list_temp;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000175
176 list_del(&master->head);
177
178 if (dev->driver->master_destroy)
179 dev->driver->master_destroy(dev, master);
180
Dave Airliec1ff85d2009-01-19 17:17:58 +1000181 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
182 if (r_list->master == master) {
183 drm_rmmap_locked(dev, r_list->map);
184 r_list = NULL;
185 }
186 }
187
Dave Airlie7c1c2872008-11-28 14:22:24 +1000188 if (master->unique) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700189 kfree(master->unique);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000190 master->unique = NULL;
191 master->unique_len = 0;
192 }
193
Chris Wilson6e350232010-07-24 18:29:36 +0100194 kfree(dev->devname);
195 dev->devname = NULL;
196
Dave Airlie7c1c2872008-11-28 14:22:24 +1000197 list_for_each_entry_safe(pt, next, &master->magicfree, head) {
198 list_del(&pt->head);
199 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
Eric Anholt9a298b22009-03-24 12:23:04 -0700200 kfree(pt);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000201 }
202
203 drm_ht_remove(&master->magiclist);
204
Eric Anholt9a298b22009-03-24 12:23:04 -0700205 kfree(master);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000206}
207
208void drm_master_put(struct drm_master **master)
209{
210 kref_put(&(*master)->refcount, drm_master_destroy);
211 *master = NULL;
212}
Thomas Hellstrom85bb0c32009-12-06 21:46:28 +0100213EXPORT_SYMBOL(drm_master_put);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000214
215int drm_setmaster_ioctl(struct drm_device *dev, void *data,
216 struct drm_file *file_priv)
217{
Benjamin Gaignard53ef1602013-06-26 17:58:59 +0200218 int ret = 0;
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000219
Jonas Bonn6b008422009-04-16 09:00:02 +0200220 if (file_priv->is_master)
221 return 0;
222
Dave Airlie7c1c2872008-11-28 14:22:24 +1000223 if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
224 return -EINVAL;
225
226 if (!file_priv->master)
227 return -EINVAL;
228
David Herrmann08bec5b2012-11-15 13:04:37 +0000229 if (file_priv->minor->master)
230 return -EINVAL;
231
232 mutex_lock(&dev->struct_mutex);
233 file_priv->minor->master = drm_master_get(file_priv->master);
234 file_priv->is_master = 1;
235 if (dev->driver->master_set) {
236 ret = dev->driver->master_set(dev, file_priv, false);
237 if (unlikely(ret != 0)) {
238 file_priv->is_master = 0;
239 drm_master_put(&file_priv->minor->master);
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000240 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000241 }
David Herrmann08bec5b2012-11-15 13:04:37 +0000242 mutex_unlock(&dev->struct_mutex);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000243
Benjamin Gaignard53ef1602013-06-26 17:58:59 +0200244 return ret;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000245}
246
247int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
248 struct drm_file *file_priv)
249{
Jonas Bonn6b008422009-04-16 09:00:02 +0200250 if (!file_priv->is_master)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000251 return -EINVAL;
Jonas Bonn6b008422009-04-16 09:00:02 +0200252
Dave Airlie07f1c7a2009-04-20 09:32:50 +1000253 if (!file_priv->minor->master)
254 return -EINVAL;
255
Dave Airlie7c1c2872008-11-28 14:22:24 +1000256 mutex_lock(&dev->struct_mutex);
Thomas Hellstrom862302f2009-12-02 18:15:25 +0000257 if (dev->driver->master_drop)
258 dev->driver->master_drop(dev, file_priv, false);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000259 drm_master_put(&file_priv->minor->master);
Jonas Bonn6b008422009-04-16 09:00:02 +0200260 file_priv->is_master = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000261 mutex_unlock(&dev->struct_mutex);
262 return 0;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/**
David Herrmanna99ee452013-10-20 18:55:42 +0200266 * drm_get_minor - Allocate and register new DRM minor
267 * @dev: DRM device
268 * @minor: Pointer to where new minor is stored
269 * @type: Type of minor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
David Herrmanna99ee452013-10-20 18:55:42 +0200271 * Allocate a new minor of the given type and register it. A pointer to the new
272 * minor is returned in @minor.
273 * Caller must hold the global DRM mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 *
David Herrmanna99ee452013-10-20 18:55:42 +0200275 * RETURNS:
276 * 0 on success, negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
David Herrmanna99ee452013-10-20 18:55:42 +0200278static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor,
279 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Dave Airlie2c14f282008-04-21 16:47:32 +1000281 struct drm_minor *new_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int ret;
Dave Airlie2c14f282008-04-21 16:47:32 +1000283 int minor_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 DRM_DEBUG("\n");
286
Dave Airlie2c14f282008-04-21 16:47:32 +1000287 minor_id = drm_minor_get_id(dev, type);
288 if (minor_id < 0)
289 return minor_id;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290
Dave Airlie2c14f282008-04-21 16:47:32 +1000291 new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
292 if (!new_minor) {
293 ret = -ENOMEM;
294 goto err_idr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Dave Airlie2c14f282008-04-21 16:47:32 +1000296
297 new_minor->type = type;
298 new_minor->device = MKDEV(DRM_MAJOR, minor_id);
299 new_minor->dev = dev;
300 new_minor->index = minor_id;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000301 INIT_LIST_HEAD(&new_minor->master_list);
Dave Airlie2c14f282008-04-21 16:47:32 +1000302
303 idr_replace(&drm_minors_idr, new_minor, minor_id);
304
Ben Gamari955b12d2009-02-17 20:08:49 -0500305#if defined(CONFIG_DEBUG_FS)
306 ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
307 if (ret) {
GeunSik Lim156f5a72009-06-02 15:01:37 +0900308 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
Daniel Vettercb6458f2013-08-08 15:41:34 +0200309 goto err_mem;
Ben Gamari955b12d2009-02-17 20:08:49 -0500310 }
311#endif
Dave Airlie2c14f282008-04-21 16:47:32 +1000312
313 ret = drm_sysfs_device_add(new_minor);
314 if (ret) {
315 printk(KERN_ERR
316 "DRM: Error sysfs_device_add.\n");
Daniel Vettercb6458f2013-08-08 15:41:34 +0200317 goto err_debugfs;
Dave Airlie2c14f282008-04-21 16:47:32 +1000318 }
319 *minor = new_minor;
320
321 DRM_DEBUG("new minor assigned %d\n", minor_id);
322 return 0;
323
324
Daniel Vettercb6458f2013-08-08 15:41:34 +0200325err_debugfs:
326#if defined(CONFIG_DEBUG_FS)
327 drm_debugfs_cleanup(new_minor);
Dave Airlie2c14f282008-04-21 16:47:32 +1000328err_mem:
Daniel Vettercb6458f2013-08-08 15:41:34 +0200329#endif
Dave Airlie2c14f282008-04-21 16:47:32 +1000330 kfree(new_minor);
331err_idr:
332 idr_remove(&drm_minors_idr, minor_id);
333 *minor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return ret;
335}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336
Dave Airliec94f7022005-07-07 21:03:38 +1000337/**
David Herrmannf73aca52013-10-20 18:55:40 +0200338 * drm_unplug_minor - Unplug DRM minor
339 * @minor: Minor to unplug
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 *
David Herrmannf73aca52013-10-20 18:55:40 +0200341 * Unplugs the given DRM minor but keeps the object. So after this returns,
342 * minor->dev is still valid so existing open-files can still access it to get
343 * device information from their drm_file ojects.
344 * If the minor is already unplugged or if @minor is NULL, nothing is done.
345 * The global DRM mutex must be held by the caller.
346 */
347static void drm_unplug_minor(struct drm_minor *minor)
348{
David Herrmanna3483352013-11-13 11:42:26 +0100349 if (!minor || !minor->kdev)
David Herrmannf73aca52013-10-20 18:55:40 +0200350 return;
351
David Herrmann865fb472013-10-20 18:55:43 +0200352#if defined(CONFIG_DEBUG_FS)
353 drm_debugfs_cleanup(minor);
354#endif
355
David Herrmannf73aca52013-10-20 18:55:40 +0200356 drm_sysfs_device_remove(minor);
David Herrmannf67e9462013-10-20 18:55:44 +0200357 idr_remove(&drm_minors_idr, minor->index);
David Herrmannf73aca52013-10-20 18:55:40 +0200358}
359
360/**
361 * drm_put_minor - Destroy DRM minor
David Herrmann4ac387f2013-10-20 18:55:41 +0200362 * @minor: Minor to destroy
David Herrmannf73aca52013-10-20 18:55:40 +0200363 *
David Herrmann4ac387f2013-10-20 18:55:41 +0200364 * This calls drm_unplug_minor() on the given minor and then frees it. Nothing
365 * is done if @minor is NULL. It is fine to call this on already unplugged
366 * minors.
David Herrmannf73aca52013-10-20 18:55:40 +0200367 * The global DRM mutex must be held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
David Herrmann4ac387f2013-10-20 18:55:41 +0200369static void drm_put_minor(struct drm_minor *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
David Herrmann4ac387f2013-10-20 18:55:41 +0200371 if (!minor)
372 return;
Eric Anholt673a3942008-07-30 12:06:12 -0700373
Dave Airlie2c14f282008-04-21 16:47:32 +1000374 DRM_DEBUG("release secondary minor %d\n", minor->index);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000375
David Herrmannf73aca52013-10-20 18:55:40 +0200376 drm_unplug_minor(minor);
Dave Airlie2c14f282008-04-21 16:47:32 +1000377 kfree(minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500379
380/**
381 * Called via drm_exit() at module unload time or when pci device is
382 * unplugged.
383 *
384 * Cleans up all DRM device, calling drm_lastclose().
385 *
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500386 */
387void drm_put_dev(struct drm_device *dev)
388{
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500389 DRM_DEBUG("\n");
390
391 if (!dev) {
392 DRM_ERROR("cleanup called no dev\n");
393 return;
394 }
395
David Herrmannc3a49732013-10-02 11:23:38 +0200396 drm_dev_unregister(dev);
David Herrmann0dc8fe52013-10-02 11:23:37 +0200397 drm_dev_free(dev);
Kristian Høgsberg112b7152009-01-04 16:55:33 -0500398}
399EXPORT_SYMBOL(drm_put_dev);
Dave Airlie2c07a212012-02-20 14:18:07 +0000400
401void drm_unplug_dev(struct drm_device *dev)
402{
403 /* for a USB device */
404 if (drm_core_check_feature(dev, DRIVER_MODESET))
405 drm_unplug_minor(dev->control);
David Herrmann17931262013-08-25 18:29:00 +0200406 if (dev->render)
407 drm_unplug_minor(dev->render);
Dave Airlie2c07a212012-02-20 14:18:07 +0000408 drm_unplug_minor(dev->primary);
409
410 mutex_lock(&drm_global_mutex);
411
412 drm_device_set_unplugged(dev);
413
414 if (dev->open_count == 0) {
415 drm_put_dev(dev);
416 }
417 mutex_unlock(&drm_global_mutex);
418}
419EXPORT_SYMBOL(drm_unplug_dev);
David Herrmann1bb72532013-10-02 11:23:34 +0200420
David Herrmann31bbe162014-01-03 14:09:47 +0100421/*
422 * DRM internal mount
423 * We want to be able to allocate our own "struct address_space" to control
424 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
425 * stand-alone address_space objects, so we need an underlying inode. As there
426 * is no way to allocate an independent inode easily, we need a fake internal
427 * VFS mount-point.
428 *
429 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
430 * frees it again. You are allowed to use iget() and iput() to get references to
431 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
432 * drm_fs_inode_free() call (which does not have to be the last iput()).
433 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
434 * between multiple inode-users. You could, technically, call
435 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
436 * iput(), but this way you'd end up with a new vfsmount for each inode.
437 */
438
439static int drm_fs_cnt;
440static struct vfsmount *drm_fs_mnt;
441
442static const struct dentry_operations drm_fs_dops = {
443 .d_dname = simple_dname,
444};
445
446static const struct super_operations drm_fs_sops = {
447 .statfs = simple_statfs,
448};
449
450static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
451 const char *dev_name, void *data)
452{
453 return mount_pseudo(fs_type,
454 "drm:",
455 &drm_fs_sops,
456 &drm_fs_dops,
457 0x010203ff);
458}
459
460static struct file_system_type drm_fs_type = {
461 .name = "drm",
462 .owner = THIS_MODULE,
463 .mount = drm_fs_mount,
464 .kill_sb = kill_anon_super,
465};
466
467static struct inode *drm_fs_inode_new(void)
468{
469 struct inode *inode;
470 int r;
471
472 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
473 if (r < 0) {
474 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
475 return ERR_PTR(r);
476 }
477
478 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
479 if (IS_ERR(inode))
480 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
481
482 return inode;
483}
484
485static void drm_fs_inode_free(struct inode *inode)
486{
487 if (inode) {
488 iput(inode);
489 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
490 }
491}
492
David Herrmann1bb72532013-10-02 11:23:34 +0200493/**
494 * drm_dev_alloc - Allocate new drm device
495 * @driver: DRM driver to allocate device for
496 * @parent: Parent device object
497 *
498 * Allocate and initialize a new DRM device. No device registration is done.
David Herrmannc22f0ac2013-10-02 11:23:35 +0200499 * Call drm_dev_register() to advertice the device to user space and register it
500 * with other core subsystems.
David Herrmann1bb72532013-10-02 11:23:34 +0200501 *
502 * RETURNS:
503 * Pointer to new DRM device, or NULL if out of memory.
504 */
505struct drm_device *drm_dev_alloc(struct drm_driver *driver,
506 struct device *parent)
507{
508 struct drm_device *dev;
509 int ret;
510
511 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
512 if (!dev)
513 return NULL;
514
515 dev->dev = parent;
516 dev->driver = driver;
517
518 INIT_LIST_HEAD(&dev->filelist);
519 INIT_LIST_HEAD(&dev->ctxlist);
520 INIT_LIST_HEAD(&dev->vmalist);
521 INIT_LIST_HEAD(&dev->maplist);
522 INIT_LIST_HEAD(&dev->vblank_event_list);
523
524 spin_lock_init(&dev->count_lock);
525 spin_lock_init(&dev->event_lock);
526 mutex_init(&dev->struct_mutex);
527 mutex_init(&dev->ctxlist_mutex);
528
David Herrmann1bb72532013-10-02 11:23:34 +0200529 if (drm_ht_create(&dev->map_hash, 12))
530 goto err_free;
531
532 ret = drm_ctxbitmap_init(dev);
533 if (ret) {
534 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
535 goto err_ht;
536 }
537
538 if (driver->driver_features & DRIVER_GEM) {
539 ret = drm_gem_init(dev);
540 if (ret) {
541 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
542 goto err_ctxbitmap;
543 }
544 }
545
546 return dev;
547
548err_ctxbitmap:
549 drm_ctxbitmap_cleanup(dev);
550err_ht:
551 drm_ht_remove(&dev->map_hash);
552err_free:
553 kfree(dev);
554 return NULL;
555}
556EXPORT_SYMBOL(drm_dev_alloc);
David Herrmannc22f0ac2013-10-02 11:23:35 +0200557
558/**
David Herrmann0dc8fe52013-10-02 11:23:37 +0200559 * drm_dev_free - Free DRM device
560 * @dev: DRM device to free
561 *
562 * Free a DRM device that has previously been allocated via drm_dev_alloc().
563 * You must not use kfree() instead or you will leak memory.
564 *
565 * This must not be called once the device got registered. Use drm_put_dev()
566 * instead, which then calls drm_dev_free().
567 */
568void drm_dev_free(struct drm_device *dev)
569{
David Herrmann8f6599d2013-10-20 18:55:45 +0200570 drm_put_minor(dev->control);
571 drm_put_minor(dev->render);
572 drm_put_minor(dev->primary);
573
David Herrmann0dc8fe52013-10-02 11:23:37 +0200574 if (dev->driver->driver_features & DRIVER_GEM)
575 drm_gem_destroy(dev);
576
577 drm_ctxbitmap_cleanup(dev);
578 drm_ht_remove(&dev->map_hash);
579
580 kfree(dev->devname);
581 kfree(dev);
582}
583EXPORT_SYMBOL(drm_dev_free);
584
585/**
David Herrmannc22f0ac2013-10-02 11:23:35 +0200586 * drm_dev_register - Register DRM device
587 * @dev: Device to register
588 *
589 * Register the DRM device @dev with the system, advertise device to user-space
590 * and start normal device operation. @dev must be allocated via drm_dev_alloc()
591 * previously.
592 *
593 * Never call this twice on any device!
594 *
595 * RETURNS:
596 * 0 on success, negative error code on failure.
597 */
598int drm_dev_register(struct drm_device *dev, unsigned long flags)
599{
600 int ret;
601
602 mutex_lock(&drm_global_mutex);
603
David Herrmannc22f0ac2013-10-02 11:23:35 +0200604 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
605 ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
606 if (ret)
Daniel Vetter2c695fa2013-12-11 11:34:36 +0100607 goto out_unlock;
David Herrmannc22f0ac2013-10-02 11:23:35 +0200608 }
609
610 if (drm_core_check_feature(dev, DRIVER_RENDER) && drm_rnodes) {
611 ret = drm_get_minor(dev, &dev->render, DRM_MINOR_RENDER);
612 if (ret)
613 goto err_control_node;
614 }
615
616 ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY);
617 if (ret)
618 goto err_render_node;
619
620 if (dev->driver->load) {
621 ret = dev->driver->load(dev, flags);
622 if (ret)
623 goto err_primary_node;
624 }
625
626 /* setup grouping for legacy outputs */
627 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
628 ret = drm_mode_group_init_legacy_group(dev,
629 &dev->primary->mode_group);
630 if (ret)
631 goto err_unload;
632 }
633
David Herrmannc22f0ac2013-10-02 11:23:35 +0200634 ret = 0;
635 goto out_unlock;
636
637err_unload:
638 if (dev->driver->unload)
639 dev->driver->unload(dev);
640err_primary_node:
Ilia Mirkin0f584112013-12-05 09:42:49 -0500641 drm_unplug_minor(dev->primary);
David Herrmannc22f0ac2013-10-02 11:23:35 +0200642err_render_node:
Ilia Mirkin0f584112013-12-05 09:42:49 -0500643 drm_unplug_minor(dev->render);
David Herrmannc22f0ac2013-10-02 11:23:35 +0200644err_control_node:
Ilia Mirkin0f584112013-12-05 09:42:49 -0500645 drm_unplug_minor(dev->control);
David Herrmannc22f0ac2013-10-02 11:23:35 +0200646out_unlock:
647 mutex_unlock(&drm_global_mutex);
648 return ret;
649}
650EXPORT_SYMBOL(drm_dev_register);
David Herrmannc3a49732013-10-02 11:23:38 +0200651
652/**
653 * drm_dev_unregister - Unregister DRM device
654 * @dev: Device to unregister
655 *
656 * Unregister the DRM device from the system. This does the reverse of
657 * drm_dev_register() but does not deallocate the device. The caller must call
658 * drm_dev_free() to free all resources.
659 */
660void drm_dev_unregister(struct drm_device *dev)
661{
662 struct drm_map_list *r_list, *list_temp;
663
664 drm_lastclose(dev);
665
666 if (dev->driver->unload)
667 dev->driver->unload(dev);
668
Daniel Vetter4efafeb2013-12-11 11:34:38 +0100669 if (dev->agp)
670 drm_pci_agp_destroy(dev);
David Herrmannc3a49732013-10-02 11:23:38 +0200671
672 drm_vblank_cleanup(dev);
673
674 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
675 drm_rmmap(dev, r_list->map);
676
David Herrmann8f6599d2013-10-20 18:55:45 +0200677 drm_unplug_minor(dev->control);
678 drm_unplug_minor(dev->render);
679 drm_unplug_minor(dev->primary);
David Herrmannc3a49732013-10-02 11:23:38 +0200680}
681EXPORT_SYMBOL(drm_dev_unregister);