blob: 0a23b32e627c94bca30c7ae8f8c7ec3ad9b7d62c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
David Herrmann32e7b942015-05-04 21:01:30 +02008 * Author Rickard E. (Rik) Faith <faith@valinux.com>
9 * Author Gareth Hughes <gareth@valinux.com>
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice (including the next
19 * paragraph) shall be included in all copies or substantial portions of the
20 * Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
Daniel Vetter67d0ec42014-09-10 12:43:53 +020032#include "drm_internal.h"
Daniel Vetter6548f4e2016-06-14 20:50:59 +020033#include "drm_legacy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/**
David Herrmann32e7b942015-05-04 21:01:30 +020036 * drm_getmagic - Get unique magic of a client
37 * @dev: DRM device to operate on
38 * @data: ioctl data containing the drm_auth object
39 * @file_priv: DRM file that performs the operation
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 *
David Herrmann32e7b942015-05-04 21:01:30 +020041 * This looks up the unique magic of the passed client and returns it. If the
42 * client did not have a magic assigned, yet, a new one is registered. The magic
43 * is stored in the passed drm_auth object.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
David Herrmann32e7b942015-05-04 21:01:30 +020045 * Returns: 0 on success, negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
Eric Anholtc153f452007-09-03 12:06:45 +100047int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Eric Anholtc153f452007-09-03 12:06:45 +100049 struct drm_auth *auth = data;
David Herrmann32e7b942015-05-04 21:01:30 +020050 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Daniel Vetterd2b34ee2016-06-17 09:33:21 +020052 mutex_lock(&dev->master_mutex);
David Herrmann32e7b942015-05-04 21:01:30 +020053 if (!file_priv->magic) {
54 ret = idr_alloc(&file_priv->master->magic_map, file_priv,
55 1, 0, GFP_KERNEL);
56 if (ret >= 0)
57 file_priv->magic = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
David Herrmann32e7b942015-05-04 21:01:30 +020059 auth->magic = file_priv->magic;
Daniel Vetterd2b34ee2016-06-17 09:33:21 +020060 mutex_unlock(&dev->master_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Eric Anholtc153f452007-09-03 12:06:45 +100062 DRM_DEBUG("%u\n", auth->magic);
63
David Herrmann32e7b942015-05-04 21:01:30 +020064 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67/**
David Herrmann32e7b942015-05-04 21:01:30 +020068 * drm_authmagic - Authenticate client with a magic
69 * @dev: DRM device to operate on
70 * @data: ioctl data containing the drm_auth object
71 * @file_priv: DRM file that performs the operation
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
David Herrmann32e7b942015-05-04 21:01:30 +020073 * This looks up a DRM client by the passed magic and authenticates it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
David Herrmann32e7b942015-05-04 21:01:30 +020075 * Returns: 0 on success, negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 */
Eric Anholtc153f452007-09-03 12:06:45 +100077int drm_authmagic(struct drm_device *dev, void *data,
78 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Eric Anholtc153f452007-09-03 12:06:45 +100080 struct drm_auth *auth = data;
Dave Airlie84b1fd12007-07-11 15:53:27 +100081 struct drm_file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Eric Anholtc153f452007-09-03 12:06:45 +100083 DRM_DEBUG("%u\n", auth->magic);
David Herrmann32e7b942015-05-04 21:01:30 +020084
Daniel Vetterd2b34ee2016-06-17 09:33:21 +020085 mutex_lock(&dev->master_mutex);
David Herrmann32e7b942015-05-04 21:01:30 +020086 file = idr_find(&file_priv->master->magic_map, auth->magic);
87 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 file->authenticated = 1;
David Herrmann32e7b942015-05-04 21:01:30 +020089 idr_replace(&file_priv->master->magic_map, NULL, auth->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Daniel Vetterd2b34ee2016-06-17 09:33:21 +020091 mutex_unlock(&dev->master_mutex);
David Herrmann32e7b942015-05-04 21:01:30 +020092
93 return file ? 0 : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
Daniel Vetter6548f4e2016-06-14 20:50:59 +020095
96static struct drm_master *drm_master_create(struct drm_device *dev)
97{
98 struct drm_master *master;
99
100 master = kzalloc(sizeof(*master), GFP_KERNEL);
101 if (!master)
102 return NULL;
103
104 kref_init(&master->refcount);
105 spin_lock_init(&master->lock.spinlock);
106 init_waitqueue_head(&master->lock.lock_queue);
107 idr_init(&master->magic_map);
108 master->dev = dev;
109
110 return master;
111}
112
Daniel Vetterd6ed6822016-06-21 14:20:38 +0200113static int drm_set_master(struct drm_device *dev, struct drm_file *fpriv,
114 bool new_master)
115{
116 int ret = 0;
117
118 dev->master = drm_master_get(fpriv->master);
Daniel Vetterd6ed6822016-06-21 14:20:38 +0200119 if (dev->driver->master_set) {
120 ret = dev->driver->master_set(dev, fpriv, new_master);
121 if (unlikely(ret != 0)) {
Daniel Vetterd6ed6822016-06-21 14:20:38 +0200122 drm_master_put(&dev->master);
123 }
124 }
125
126 return ret;
127}
128
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200129/*
130 * drm_new_set_master - Allocate a new master object and become master for the
131 * associated master realm.
132 *
133 * @dev: The associated device.
134 * @fpriv: File private identifying the client.
135 *
Daniel Vetterd2b34ee2016-06-17 09:33:21 +0200136 * This function must be called with dev::master_mutex held.
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200137 * Returns negative error code on failure. Zero on success.
138 */
Daniel Vetter2cbae7e2016-06-14 20:51:00 +0200139static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200140{
141 struct drm_master *old_master;
142 int ret;
143
144 lockdep_assert_held_once(&dev->master_mutex);
145
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200146 old_master = fpriv->master;
Daniel Vetterd6ed6822016-06-21 14:20:38 +0200147 fpriv->master = drm_master_create(dev);
148 if (!fpriv->master) {
149 fpriv->master = old_master;
150 return -ENOMEM;
151 }
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200152
153 if (dev->driver->master_create) {
154 ret = dev->driver->master_create(dev, fpriv->master);
155 if (ret)
156 goto out_err;
157 }
Daniel Vetter0aae5922016-06-21 10:54:21 +0200158 fpriv->is_master = 1;
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200159 fpriv->authenticated = 1;
Daniel Vetterd6ed6822016-06-21 14:20:38 +0200160
161 ret = drm_set_master(dev, fpriv, true);
162 if (ret)
163 goto out_err;
164
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200165 if (old_master)
166 drm_master_put(&old_master);
167
168 return 0;
169
170out_err:
Daniel Vetterd6ed6822016-06-21 14:20:38 +0200171 /* drop references and restore old master on failure */
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200172 drm_master_put(&fpriv->master);
173 fpriv->master = old_master;
174
175 return ret;
176}
177
178int drm_setmaster_ioctl(struct drm_device *dev, void *data,
179 struct drm_file *file_priv)
180{
181 int ret = 0;
182
183 mutex_lock(&dev->master_mutex);
Daniel Vetterb3ac9f22016-06-21 10:54:20 +0200184 if (drm_is_current_master(file_priv))
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200185 goto out_unlock;
186
Daniel Vetter95c081c2016-06-21 10:54:12 +0200187 if (dev->master) {
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200188 ret = -EINVAL;
189 goto out_unlock;
190 }
191
192 if (!file_priv->master) {
193 ret = -EINVAL;
194 goto out_unlock;
195 }
196
Daniel Vetter0aae5922016-06-21 10:54:21 +0200197 if (!file_priv->is_master) {
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200198 ret = drm_new_set_master(dev, file_priv);
199 goto out_unlock;
200 }
201
Daniel Vetterd6ed6822016-06-21 14:20:38 +0200202 ret = drm_set_master(dev, file_priv, false);
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200203out_unlock:
204 mutex_unlock(&dev->master_mutex);
205 return ret;
206}
207
Daniel Vetterd6ed6822016-06-21 14:20:38 +0200208static void drm_drop_master(struct drm_device *dev,
209 struct drm_file *fpriv)
210{
211 if (dev->driver->master_drop)
212 dev->driver->master_drop(dev, fpriv);
213 drm_master_put(&dev->master);
Daniel Vetterd6ed6822016-06-21 14:20:38 +0200214}
215
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200216int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
217 struct drm_file *file_priv)
218{
219 int ret = -EINVAL;
220
221 mutex_lock(&dev->master_mutex);
Daniel Vetterb3ac9f22016-06-21 10:54:20 +0200222 if (!drm_is_current_master(file_priv))
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200223 goto out_unlock;
224
Daniel Vetter95c081c2016-06-21 10:54:12 +0200225 if (!dev->master)
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200226 goto out_unlock;
227
228 ret = 0;
Daniel Vetterd6ed6822016-06-21 14:20:38 +0200229 drm_drop_master(dev, file_priv);
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200230out_unlock:
231 mutex_unlock(&dev->master_mutex);
232 return ret;
233}
234
Daniel Vetter2cbae7e2016-06-14 20:51:00 +0200235int drm_master_open(struct drm_file *file_priv)
236{
237 struct drm_device *dev = file_priv->minor->dev;
238 int ret = 0;
239
240 /* if there is no current master make this fd it, but do not create
241 * any master object for render clients */
242 mutex_lock(&dev->master_mutex);
Daniel Vetter95c081c2016-06-21 10:54:12 +0200243 if (!dev->master)
Daniel Vetter2cbae7e2016-06-14 20:51:00 +0200244 ret = drm_new_set_master(dev, file_priv);
245 else
Daniel Vetter95c081c2016-06-21 10:54:12 +0200246 file_priv->master = drm_master_get(dev->master);
Daniel Vetter2cbae7e2016-06-14 20:51:00 +0200247 mutex_unlock(&dev->master_mutex);
248
249 return ret;
250}
251
Daniel Vetter14d71eb2016-06-14 20:51:01 +0200252void drm_master_release(struct drm_file *file_priv)
253{
254 struct drm_device *dev = file_priv->minor->dev;
Daniel Vetter0de4cc92016-06-17 09:33:19 +0200255 struct drm_master *master = file_priv->master;
Daniel Vetter14d71eb2016-06-14 20:51:01 +0200256
Daniel Vetterd2b34ee2016-06-17 09:33:21 +0200257 mutex_lock(&dev->master_mutex);
Daniel Vettera77316b2016-06-17 09:33:20 +0200258 if (file_priv->magic)
259 idr_remove(&file_priv->master->magic_map, file_priv->magic);
Daniel Vettera77316b2016-06-17 09:33:20 +0200260
Daniel Vetterb3ac9f22016-06-21 10:54:20 +0200261 if (!drm_is_current_master(file_priv))
Daniel Vetter0de4cc92016-06-17 09:33:19 +0200262 goto out;
Daniel Vetter14d71eb2016-06-14 20:51:01 +0200263
Daniel Vetter0de4cc92016-06-17 09:33:19 +0200264 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter14d71eb2016-06-14 20:51:01 +0200265 /*
266 * Since the master is disappearing, so is the
267 * possibility to lock.
268 */
269 mutex_lock(&dev->struct_mutex);
270 if (master->lock.hw_lock) {
271 if (dev->sigdata.lock == master->lock.hw_lock)
272 dev->sigdata.lock = NULL;
273 master->lock.hw_lock = NULL;
274 master->lock.file_priv = NULL;
275 wake_up_interruptible_all(&master->lock.lock_queue);
276 }
277 mutex_unlock(&dev->struct_mutex);
Daniel Vetter14d71eb2016-06-14 20:51:01 +0200278 }
279
Daniel Vetterd6ed6822016-06-21 14:20:38 +0200280 if (dev->master == file_priv->master)
281 drm_drop_master(dev, file_priv);
Daniel Vetter0de4cc92016-06-17 09:33:19 +0200282out:
Daniel Vetter14d71eb2016-06-14 20:51:01 +0200283 /* drop the master reference held by the file priv */
284 if (file_priv->master)
285 drm_master_put(&file_priv->master);
Daniel Vetter14d71eb2016-06-14 20:51:01 +0200286 mutex_unlock(&dev->master_mutex);
287}
288
Daniel Vetterb3ac9f22016-06-21 10:54:20 +0200289bool drm_is_current_master(struct drm_file *fpriv)
290{
Daniel Vetter0aae5922016-06-21 10:54:21 +0200291 return fpriv->is_master && fpriv->master == fpriv->minor->dev->master;
Daniel Vetterb3ac9f22016-06-21 10:54:20 +0200292}
293EXPORT_SYMBOL(drm_is_current_master);
294
Daniel Vetter6548f4e2016-06-14 20:50:59 +0200295struct drm_master *drm_master_get(struct drm_master *master)
296{
297 kref_get(&master->refcount);
298 return master;
299}
300EXPORT_SYMBOL(drm_master_get);
301
302static void drm_master_destroy(struct kref *kref)
303{
304 struct drm_master *master = container_of(kref, struct drm_master, refcount);
305 struct drm_device *dev = master->dev;
306
307 if (dev->driver->master_destroy)
308 dev->driver->master_destroy(dev, master);
309
310 drm_legacy_master_rmmaps(dev, master);
311
312 idr_destroy(&master->magic_map);
313 kfree(master->unique);
314 kfree(master);
315}
316
317void drm_master_put(struct drm_master **master)
318{
319 kref_put(&(*master)->refcount, drm_master_destroy);
320 *master = NULL;
321}
322EXPORT_SYMBOL(drm_master_put);