blob: daa2ff12101ba366d82be5b550ba5cf5738a6ff2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_lock.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * IOCTLs for locking
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
11 *
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
Dave Airlie5c2a5ce2011-12-22 19:09:01 +000036#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/drmP.h>
David Herrmanne7b960702014-07-24 12:10:04 +020038#include "drm_legacy.h"
Daniel Vetter44af3f52014-09-10 12:43:54 +020039#include "drm_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Daniel Vetter4ac5ec42010-08-23 22:53:34 +020041static int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);
42
Dave Airlieb5e89ed2005-09-25 14:28:13 +100043/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * Lock ioctl.
45 *
46 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +100047 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * \param cmd command.
49 * \param arg user argument, pointing to a drm_lock structure.
50 * \return zero on success or negative number on failure.
51 *
52 * Add the current task to the lock wait queue, and attempt to take to lock.
53 */
David Herrmannbb6d8222014-08-29 12:12:46 +020054int drm_legacy_lock(struct drm_device *dev, void *data,
55 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100057 DECLARE_WAITQUEUE(entry, current);
Eric Anholtc153f452007-09-03 12:06:45 +100058 struct drm_lock *lock = data;
Dave Airlie7c1c2872008-11-28 14:22:24 +100059 struct drm_master *master = file_priv->master;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100060 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Daniel Vetterda168d82015-06-23 11:34:21 +020062 if (drm_core_check_feature(dev, DRIVER_MODESET))
63 return -EINVAL;
64
Eric Anholt6c340ea2007-08-25 20:23:09 +100065 ++file_priv->lock_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Eric Anholtc153f452007-09-03 12:06:45 +100067 if (lock->context == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100068 DRM_ERROR("Process %d using kernel context %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -070069 task_pid_nr(current), lock->context);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100070 return -EINVAL;
71 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Dave Airlieb5e89ed2005-09-25 14:28:13 +100073 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -070074 lock->context, task_pid_nr(current),
Dave Airlie7c1c2872008-11-28 14:22:24 +100075 master->lock.hw_lock->lock, lock->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Dave Airlie7c1c2872008-11-28 14:22:24 +100077 add_wait_queue(&master->lock.lock_queue, &entry);
78 spin_lock_bh(&master->lock.spinlock);
79 master->lock.user_waiters++;
80 spin_unlock_bh(&master->lock.spinlock);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 for (;;) {
83 __set_current_state(TASK_INTERRUPTIBLE);
Dave Airlie7c1c2872008-11-28 14:22:24 +100084 if (!master->lock.hw_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 /* Device has been unregistered */
Thomas Hellstromfda714c2009-03-02 11:10:56 +010086 send_sig(SIGTERM, current, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 ret = -EINTR;
88 break;
89 }
Dave Airlie7c1c2872008-11-28 14:22:24 +100090 if (drm_lock_take(&master->lock, lock->context)) {
91 master->lock.file_priv = file_priv;
92 master->lock.lock_time = jiffies;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100093 break; /* Got lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 /* Contention */
Arnd Bergmann08f2e662010-08-27 08:55:28 +100097 mutex_unlock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 schedule();
Arnd Bergmann08f2e662010-08-27 08:55:28 +100099 mutex_lock(&drm_global_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000100 if (signal_pending(current)) {
Thomas Hellstrom4d77c882009-03-02 11:10:54 +0100101 ret = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 break;
103 }
104 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000105 spin_lock_bh(&master->lock.spinlock);
106 master->lock.user_waiters--;
107 spin_unlock_bh(&master->lock.spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 __set_current_state(TASK_RUNNING);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000109 remove_wait_queue(&master->lock.lock_queue, &entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Eric Anholtc153f452007-09-03 12:06:45 +1000111 DRM_DEBUG("%d %s\n", lock->context,
112 ret ? "interrupted" : "has lock");
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100113 if (ret) return ret;
Dave Airlied985c102006-01-02 21:32:48 +1100114
Dave Airlie3e5fc802008-08-24 17:02:26 +1000115 /* don't set the block all signals on the master process for now
116 * really probably not the correct answer but lets us debug xkb
117 * xserver for now */
Dave Airlie7963e9d2014-08-08 07:30:53 +1000118 if (!file_priv->is_master) {
Dave Airlie3e5fc802008-08-24 17:02:26 +1000119 dev->sigdata.context = lock->context;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000120 dev->sigdata.lock = master->lock.hw_lock;
Dave Airlie3e5fc802008-08-24 17:02:26 +1000121 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000122
Eric Anholtc153f452007-09-03 12:06:45 +1000123 if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT))
124 {
Dave Airlied985c102006-01-02 21:32:48 +1100125 if (dev->driver->dma_quiescent(dev)) {
Eric Anholtc153f452007-09-03 12:06:45 +1000126 DRM_DEBUG("%d waiting for DMA quiescent\n",
127 lock->context);
Eric Anholt20caafa2007-08-25 19:22:43 +1000128 return -EBUSY;
Dave Airlied985c102006-01-02 21:32:48 +1100129 }
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Dave Airlied985c102006-01-02 21:32:48 +1100132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000135/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * Unlock ioctl.
137 *
138 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000139 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * \param cmd command.
141 * \param arg user argument, pointing to a drm_lock structure.
142 * \return zero on success or negative number on failure.
143 *
144 * Transfer and free the lock.
145 */
David Herrmannbb6d8222014-08-29 12:12:46 +0200146int drm_legacy_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Eric Anholtc153f452007-09-03 12:06:45 +1000148 struct drm_lock *lock = data;
Daniel Vetter45ff46c2010-09-26 00:24:48 +0200149 struct drm_master *master = file_priv->master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Daniel Vetterda168d82015-06-23 11:34:21 +0200151 if (drm_core_check_feature(dev, DRIVER_MODESET))
152 return -EINVAL;
153
Eric Anholtc153f452007-09-03 12:06:45 +1000154 if (lock->context == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000155 DRM_ERROR("Process %d using kernel context %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700156 task_pid_nr(current), lock->context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -EINVAL;
158 }
159
David Herrmannbb6d8222014-08-29 12:12:46 +0200160 if (drm_legacy_lock_free(&master->lock, lock->context)) {
Daniel Vetter45ff46c2010-09-26 00:24:48 +0200161 /* FIXME: Should really bail out here. */
162 }
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 0;
165}
166
167/**
168 * Take the heavyweight lock.
169 *
170 * \param lock lock pointer.
171 * \param context locking context.
172 * \return one if the lock is held, or zero otherwise.
173 *
174 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
175 */
Daniel Vetter4ac5ec42010-08-23 22:53:34 +0200176static
Dave Airlie55910512007-07-11 16:53:40 +1000177int drm_lock_take(struct drm_lock_data *lock_data,
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100178 unsigned int context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 unsigned int old, new, prev;
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100181 volatile unsigned int *lock = &lock_data->hw_lock->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000183 spin_lock_bh(&lock_data->spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 do {
185 old = *lock;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000186 if (old & _DRM_LOCK_HELD)
187 new = old | _DRM_LOCK_CONT;
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100188 else {
189 new = context | _DRM_LOCK_HELD |
190 ((lock_data->user_waiters + lock_data->kernel_waiters > 1) ?
191 _DRM_LOCK_CONT : 0);
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 prev = cmpxchg(lock, old, new);
194 } while (prev != old);
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000195 spin_unlock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (_DRM_LOCKING_CONTEXT(old) == context) {
198 if (old & _DRM_LOCK_HELD) {
199 if (context != DRM_KERNEL_CONTEXT) {
200 DRM_ERROR("%d holds heavyweight lock\n",
201 context);
202 }
203 return 0;
204 }
205 }
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100206
207 if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000208 /* Have lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return 1;
210 }
211 return 0;
212}
213
214/**
215 * This takes a lock forcibly and hands it to context. Should ONLY be used
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000216 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
217 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * \param dev DRM device.
219 * \param lock lock pointer.
220 * \param context locking context.
221 * \return always one.
222 *
223 * Resets the lock file pointer.
224 * Marks the lock as held by the given context, via the \p cmpxchg instruction.
225 */
Dave Airlie55910512007-07-11 16:53:40 +1000226static int drm_lock_transfer(struct drm_lock_data *lock_data,
Dave Airliec94f7022005-07-07 21:03:38 +1000227 unsigned int context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 unsigned int old, new, prev;
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100230 volatile unsigned int *lock = &lock_data->hw_lock->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Eric Anholt6c340ea2007-08-25 20:23:09 +1000232 lock_data->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000234 old = *lock;
235 new = context | _DRM_LOCK_HELD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 prev = cmpxchg(lock, old, new);
237 } while (prev != old);
238 return 1;
239}
240
241/**
242 * Free lock.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * \param dev DRM device.
245 * \param lock lock.
246 * \param context context.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000247 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * Resets the lock file pointer.
249 * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
250 * waiting on the lock queue.
251 */
David Herrmannbb6d8222014-08-29 12:12:46 +0200252int drm_legacy_lock_free(struct drm_lock_data *lock_data, unsigned int context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 unsigned int old, new, prev;
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100255 volatile unsigned int *lock = &lock_data->hw_lock->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000257 spin_lock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100258 if (lock_data->kernel_waiters != 0) {
259 drm_lock_transfer(lock_data, 0);
260 lock_data->idle_has_lock = 1;
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000261 spin_unlock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100262 return 1;
263 }
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000264 spin_unlock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000267 old = *lock;
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100268 new = _DRM_LOCKING_CONTEXT(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 prev = cmpxchg(lock, old, new);
270 } while (prev != old);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
273 DRM_ERROR("%d freed heavyweight lock held by %d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274 context, _DRM_LOCKING_CONTEXT(old));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 1;
276 }
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100277 wake_up_interruptible(&lock_data->lock_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279}
280
281/**
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100282 * This function returns immediately and takes the hw lock
283 * with the kernel context if it is free, otherwise it gets the highest priority when and if
284 * it is eventually released.
285 *
286 * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held
287 * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause
288 * a deadlock, which is why the "idlelock" was invented).
289 *
290 * This should be sufficient to wait for GPU idle without
291 * having to worry about starvation.
292 */
293
David Herrmannbb6d8222014-08-29 12:12:46 +0200294void drm_legacy_idlelock_take(struct drm_lock_data *lock_data)
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100295{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200296 int ret;
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100297
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000298 spin_lock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100299 lock_data->kernel_waiters++;
300 if (!lock_data->idle_has_lock) {
301
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000302 spin_unlock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100303 ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000304 spin_lock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100305
306 if (ret == 1)
307 lock_data->idle_has_lock = 1;
308 }
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000309 spin_unlock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100310}
David Herrmannbb6d8222014-08-29 12:12:46 +0200311EXPORT_SYMBOL(drm_legacy_idlelock_take);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100312
David Herrmannbb6d8222014-08-29 12:12:46 +0200313void drm_legacy_idlelock_release(struct drm_lock_data *lock_data)
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100314{
315 unsigned int old, prev;
316 volatile unsigned int *lock = &lock_data->hw_lock->lock;
317
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000318 spin_lock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100319 if (--lock_data->kernel_waiters == 0) {
320 if (lock_data->idle_has_lock) {
321 do {
322 old = *lock;
323 prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT);
324 } while (prev != old);
325 wake_up_interruptible(&lock_data->lock_queue);
326 lock_data->idle_has_lock = 0;
327 }
328 }
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000329 spin_unlock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100330}
David Herrmannbb6d8222014-08-29 12:12:46 +0200331EXPORT_SYMBOL(drm_legacy_idlelock_release);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100332
David Herrmannbb6d8222014-08-29 12:12:46 +0200333int drm_legacy_i_have_hw_lock(struct drm_device *dev,
334 struct drm_file *file_priv)
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100335{
Dave Airlie7c1c2872008-11-28 14:22:24 +1000336 struct drm_master *master = file_priv->master;
337 return (file_priv->lock_count && master->lock.hw_lock &&
338 _DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) &&
339 master->lock.file_priv == file_priv);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100340}