Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 8 | * Author Rickard E. (Rik) Faith <faith@valinux.com> |
| 9 | * Author Gareth Hughes <gareth@valinux.com> |
| 10 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 12 | * copy of this software and associated documentation files (the "Software"), |
| 13 | * to deal in the Software without restriction, including without limitation |
| 14 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 15 | * and/or sell copies of the Software, and to permit persons to whom the |
| 16 | * Software is furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice (including the next |
| 19 | * paragraph) shall be included in all copies or substantial portions of the |
| 20 | * Software. |
| 21 | * |
| 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 25 | * 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 Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 31 | #include <drm/drmP.h> |
Daniel Vetter | 67d0ec4 | 2014-09-10 12:43:53 +0200 | [diff] [blame] | 32 | #include "drm_internal.h" |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 33 | #include "drm_legacy.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 36 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | * |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 41 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 45 | * Returns: 0 on success, negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 47 | int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 49 | struct drm_auth *auth = data; |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 50 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Daniel Vetter | d2b34ee | 2016-06-17 09:33:21 +0200 | [diff] [blame] | 52 | mutex_lock(&dev->master_mutex); |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 53 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 59 | auth->magic = file_priv->magic; |
Daniel Vetter | d2b34ee | 2016-06-17 09:33:21 +0200 | [diff] [blame] | 60 | mutex_unlock(&dev->master_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 62 | DRM_DEBUG("%u\n", auth->magic); |
| 63 | |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 64 | return ret < 0 ? ret : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 68 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 73 | * This looks up a DRM client by the passed magic and authenticates it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | * |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 75 | * Returns: 0 on success, negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 77 | int drm_authmagic(struct drm_device *dev, void *data, |
| 78 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 80 | struct drm_auth *auth = data; |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 81 | struct drm_file *file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 83 | DRM_DEBUG("%u\n", auth->magic); |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 84 | |
Daniel Vetter | d2b34ee | 2016-06-17 09:33:21 +0200 | [diff] [blame] | 85 | mutex_lock(&dev->master_mutex); |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 86 | file = idr_find(&file_priv->master->magic_map, auth->magic); |
| 87 | if (file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | file->authenticated = 1; |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 89 | idr_replace(&file_priv->master->magic_map, NULL, auth->magic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
Daniel Vetter | d2b34ee | 2016-06-17 09:33:21 +0200 | [diff] [blame] | 91 | mutex_unlock(&dev->master_mutex); |
David Herrmann | 32e7b94 | 2015-05-04 21:01:30 +0200 | [diff] [blame] | 92 | |
| 93 | return file ? 0 : -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 95 | |
| 96 | static 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 | |
| 113 | /* |
| 114 | * drm_new_set_master - Allocate a new master object and become master for the |
| 115 | * associated master realm. |
| 116 | * |
| 117 | * @dev: The associated device. |
| 118 | * @fpriv: File private identifying the client. |
| 119 | * |
Daniel Vetter | d2b34ee | 2016-06-17 09:33:21 +0200 | [diff] [blame] | 120 | * This function must be called with dev::master_mutex held. |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 121 | * Returns negative error code on failure. Zero on success. |
| 122 | */ |
Daniel Vetter | 2cbae7e | 2016-06-14 20:51:00 +0200 | [diff] [blame] | 123 | static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv) |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 124 | { |
| 125 | struct drm_master *old_master; |
| 126 | int ret; |
| 127 | |
| 128 | lockdep_assert_held_once(&dev->master_mutex); |
| 129 | |
| 130 | /* create a new master */ |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame^] | 131 | dev->master = drm_master_create(dev); |
| 132 | if (!dev->master) |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 133 | return -ENOMEM; |
| 134 | |
| 135 | /* take another reference for the copy in the local file priv */ |
| 136 | old_master = fpriv->master; |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame^] | 137 | fpriv->master = drm_master_get(dev->master); |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 138 | |
| 139 | if (dev->driver->master_create) { |
| 140 | ret = dev->driver->master_create(dev, fpriv->master); |
| 141 | if (ret) |
| 142 | goto out_err; |
| 143 | } |
| 144 | if (dev->driver->master_set) { |
| 145 | ret = dev->driver->master_set(dev, fpriv, true); |
| 146 | if (ret) |
| 147 | goto out_err; |
| 148 | } |
| 149 | |
| 150 | fpriv->is_master = 1; |
| 151 | fpriv->allowed_master = 1; |
| 152 | fpriv->authenticated = 1; |
| 153 | if (old_master) |
| 154 | drm_master_put(&old_master); |
| 155 | |
| 156 | return 0; |
| 157 | |
| 158 | out_err: |
| 159 | /* drop both references and restore old master on failure */ |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame^] | 160 | drm_master_put(&dev->master); |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 161 | drm_master_put(&fpriv->master); |
| 162 | fpriv->master = old_master; |
| 163 | |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | int drm_setmaster_ioctl(struct drm_device *dev, void *data, |
| 168 | struct drm_file *file_priv) |
| 169 | { |
| 170 | int ret = 0; |
| 171 | |
| 172 | mutex_lock(&dev->master_mutex); |
| 173 | if (file_priv->is_master) |
| 174 | goto out_unlock; |
| 175 | |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame^] | 176 | if (dev->master) { |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 177 | ret = -EINVAL; |
| 178 | goto out_unlock; |
| 179 | } |
| 180 | |
| 181 | if (!file_priv->master) { |
| 182 | ret = -EINVAL; |
| 183 | goto out_unlock; |
| 184 | } |
| 185 | |
| 186 | if (!file_priv->allowed_master) { |
| 187 | ret = drm_new_set_master(dev, file_priv); |
| 188 | goto out_unlock; |
| 189 | } |
| 190 | |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame^] | 191 | dev->master = drm_master_get(file_priv->master); |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 192 | file_priv->is_master = 1; |
| 193 | if (dev->driver->master_set) { |
| 194 | ret = dev->driver->master_set(dev, file_priv, false); |
| 195 | if (unlikely(ret != 0)) { |
| 196 | file_priv->is_master = 0; |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame^] | 197 | drm_master_put(&dev->master); |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | out_unlock: |
| 202 | mutex_unlock(&dev->master_mutex); |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | int drm_dropmaster_ioctl(struct drm_device *dev, void *data, |
| 207 | struct drm_file *file_priv) |
| 208 | { |
| 209 | int ret = -EINVAL; |
| 210 | |
| 211 | mutex_lock(&dev->master_mutex); |
| 212 | if (!file_priv->is_master) |
| 213 | goto out_unlock; |
| 214 | |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame^] | 215 | if (!dev->master) |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 216 | goto out_unlock; |
| 217 | |
| 218 | ret = 0; |
| 219 | if (dev->driver->master_drop) |
| 220 | dev->driver->master_drop(dev, file_priv, false); |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame^] | 221 | drm_master_put(&dev->master); |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 222 | file_priv->is_master = 0; |
| 223 | |
| 224 | out_unlock: |
| 225 | mutex_unlock(&dev->master_mutex); |
| 226 | return ret; |
| 227 | } |
| 228 | |
Daniel Vetter | 2cbae7e | 2016-06-14 20:51:00 +0200 | [diff] [blame] | 229 | int drm_master_open(struct drm_file *file_priv) |
| 230 | { |
| 231 | struct drm_device *dev = file_priv->minor->dev; |
| 232 | int ret = 0; |
| 233 | |
| 234 | /* if there is no current master make this fd it, but do not create |
| 235 | * any master object for render clients */ |
| 236 | mutex_lock(&dev->master_mutex); |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame^] | 237 | if (!dev->master) |
Daniel Vetter | 2cbae7e | 2016-06-14 20:51:00 +0200 | [diff] [blame] | 238 | ret = drm_new_set_master(dev, file_priv); |
| 239 | else |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame^] | 240 | file_priv->master = drm_master_get(dev->master); |
Daniel Vetter | 2cbae7e | 2016-06-14 20:51:00 +0200 | [diff] [blame] | 241 | mutex_unlock(&dev->master_mutex); |
| 242 | |
| 243 | return ret; |
| 244 | } |
| 245 | |
Daniel Vetter | 14d71eb | 2016-06-14 20:51:01 +0200 | [diff] [blame] | 246 | void drm_master_release(struct drm_file *file_priv) |
| 247 | { |
| 248 | struct drm_device *dev = file_priv->minor->dev; |
Daniel Vetter | 0de4cc9 | 2016-06-17 09:33:19 +0200 | [diff] [blame] | 249 | struct drm_master *master = file_priv->master; |
Daniel Vetter | 14d71eb | 2016-06-14 20:51:01 +0200 | [diff] [blame] | 250 | |
Daniel Vetter | d2b34ee | 2016-06-17 09:33:21 +0200 | [diff] [blame] | 251 | mutex_lock(&dev->master_mutex); |
Daniel Vetter | a77316b | 2016-06-17 09:33:20 +0200 | [diff] [blame] | 252 | if (file_priv->magic) |
| 253 | idr_remove(&file_priv->master->magic_map, file_priv->magic); |
Daniel Vetter | a77316b | 2016-06-17 09:33:20 +0200 | [diff] [blame] | 254 | |
Daniel Vetter | 0de4cc9 | 2016-06-17 09:33:19 +0200 | [diff] [blame] | 255 | if (!file_priv->is_master) |
| 256 | goto out; |
Daniel Vetter | 14d71eb | 2016-06-14 20:51:01 +0200 | [diff] [blame] | 257 | |
Daniel Vetter | 0de4cc9 | 2016-06-17 09:33:19 +0200 | [diff] [blame] | 258 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) { |
Daniel Vetter | 14d71eb | 2016-06-14 20:51:01 +0200 | [diff] [blame] | 259 | /* |
| 260 | * Since the master is disappearing, so is the |
| 261 | * possibility to lock. |
| 262 | */ |
| 263 | mutex_lock(&dev->struct_mutex); |
| 264 | if (master->lock.hw_lock) { |
| 265 | if (dev->sigdata.lock == master->lock.hw_lock) |
| 266 | dev->sigdata.lock = NULL; |
| 267 | master->lock.hw_lock = NULL; |
| 268 | master->lock.file_priv = NULL; |
| 269 | wake_up_interruptible_all(&master->lock.lock_queue); |
| 270 | } |
| 271 | mutex_unlock(&dev->struct_mutex); |
Daniel Vetter | 14d71eb | 2016-06-14 20:51:01 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame^] | 274 | if (dev->master == file_priv->master) { |
Daniel Vetter | 0de4cc9 | 2016-06-17 09:33:19 +0200 | [diff] [blame] | 275 | /* drop the reference held my the minor */ |
| 276 | if (dev->driver->master_drop) |
| 277 | dev->driver->master_drop(dev, file_priv, true); |
Daniel Vetter | 95c081c | 2016-06-21 10:54:12 +0200 | [diff] [blame^] | 278 | drm_master_put(&dev->master); |
Daniel Vetter | 0de4cc9 | 2016-06-17 09:33:19 +0200 | [diff] [blame] | 279 | } |
| 280 | out: |
Daniel Vetter | 14d71eb | 2016-06-14 20:51:01 +0200 | [diff] [blame] | 281 | /* drop the master reference held by the file priv */ |
| 282 | if (file_priv->master) |
| 283 | drm_master_put(&file_priv->master); |
| 284 | file_priv->is_master = 0; |
| 285 | mutex_unlock(&dev->master_mutex); |
| 286 | } |
| 287 | |
Daniel Vetter | 6548f4e | 2016-06-14 20:50:59 +0200 | [diff] [blame] | 288 | struct drm_master *drm_master_get(struct drm_master *master) |
| 289 | { |
| 290 | kref_get(&master->refcount); |
| 291 | return master; |
| 292 | } |
| 293 | EXPORT_SYMBOL(drm_master_get); |
| 294 | |
| 295 | static void drm_master_destroy(struct kref *kref) |
| 296 | { |
| 297 | struct drm_master *master = container_of(kref, struct drm_master, refcount); |
| 298 | struct drm_device *dev = master->dev; |
| 299 | |
| 300 | if (dev->driver->master_destroy) |
| 301 | dev->driver->master_destroy(dev, master); |
| 302 | |
| 303 | drm_legacy_master_rmmaps(dev, master); |
| 304 | |
| 305 | idr_destroy(&master->magic_map); |
| 306 | kfree(master->unique); |
| 307 | kfree(master); |
| 308 | } |
| 309 | |
| 310 | void drm_master_put(struct drm_master **master) |
| 311 | { |
| 312 | kref_put(&(*master)->refcount, drm_master_destroy); |
| 313 | *master = NULL; |
| 314 | } |
| 315 | EXPORT_SYMBOL(drm_master_put); |