blob: 96bb6badb818d194f43997d1b427481fe1433ab4 [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010037#include <linux/sched/signal.h>
38
David Howells760285e2012-10-02 18:01:07 +010039#include <drm/drmP.h>
David Herrmanne7b960702014-07-24 12:10:04 +020040#include "drm_legacy.h"
Daniel Vetter44af3f52014-09-10 12:43:54 +020041#include "drm_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Daniel Vetter4ac5ec42010-08-23 22:53:34 +020043static int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);
44
Dave Airlieb5e89ed2005-09-25 14:28:13 +100045/**
Daniel Vetter1a75a222016-06-14 20:50:57 +020046 * Take the heavyweight lock.
47 *
48 * \param lock lock pointer.
49 * \param context locking context.
50 * \return one if the lock is held, or zero otherwise.
51 *
52 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
53 */
54static
55int drm_lock_take(struct drm_lock_data *lock_data,
56 unsigned int context)
57{
58 unsigned int old, new, prev;
59 volatile unsigned int *lock = &lock_data->hw_lock->lock;
60
61 spin_lock_bh(&lock_data->spinlock);
62 do {
63 old = *lock;
64 if (old & _DRM_LOCK_HELD)
65 new = old | _DRM_LOCK_CONT;
66 else {
67 new = context | _DRM_LOCK_HELD |
68 ((lock_data->user_waiters + lock_data->kernel_waiters > 1) ?
69 _DRM_LOCK_CONT : 0);
70 }
71 prev = cmpxchg(lock, old, new);
72 } while (prev != old);
73 spin_unlock_bh(&lock_data->spinlock);
74
75 if (_DRM_LOCKING_CONTEXT(old) == context) {
76 if (old & _DRM_LOCK_HELD) {
77 if (context != DRM_KERNEL_CONTEXT) {
78 DRM_ERROR("%d holds heavyweight lock\n",
79 context);
80 }
81 return 0;
82 }
83 }
84
85 if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) {
86 /* Have lock */
87 return 1;
88 }
89 return 0;
90}
91
92/**
93 * This takes a lock forcibly and hands it to context. Should ONLY be used
94 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
95 *
96 * \param dev DRM device.
97 * \param lock lock pointer.
98 * \param context locking context.
99 * \return always one.
100 *
101 * Resets the lock file pointer.
102 * Marks the lock as held by the given context, via the \p cmpxchg instruction.
103 */
104static int drm_lock_transfer(struct drm_lock_data *lock_data,
105 unsigned int context)
106{
107 unsigned int old, new, prev;
108 volatile unsigned int *lock = &lock_data->hw_lock->lock;
109
110 lock_data->file_priv = NULL;
111 do {
112 old = *lock;
113 new = context | _DRM_LOCK_HELD;
114 prev = cmpxchg(lock, old, new);
115 } while (prev != old);
116 return 1;
117}
118
119static int drm_legacy_lock_free(struct drm_lock_data *lock_data,
120 unsigned int context)
121{
122 unsigned int old, new, prev;
123 volatile unsigned int *lock = &lock_data->hw_lock->lock;
124
125 spin_lock_bh(&lock_data->spinlock);
126 if (lock_data->kernel_waiters != 0) {
127 drm_lock_transfer(lock_data, 0);
128 lock_data->idle_has_lock = 1;
129 spin_unlock_bh(&lock_data->spinlock);
130 return 1;
131 }
132 spin_unlock_bh(&lock_data->spinlock);
133
134 do {
135 old = *lock;
136 new = _DRM_LOCKING_CONTEXT(old);
137 prev = cmpxchg(lock, old, new);
138 } while (prev != old);
139
140 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
141 DRM_ERROR("%d freed heavyweight lock held by %d\n",
142 context, _DRM_LOCKING_CONTEXT(old));
143 return 1;
144 }
145 wake_up_interruptible(&lock_data->lock_queue);
146 return 0;
147}
148
149/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * Lock ioctl.
151 *
152 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000153 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * \param cmd command.
155 * \param arg user argument, pointing to a drm_lock structure.
156 * \return zero on success or negative number on failure.
157 *
158 * Add the current task to the lock wait queue, and attempt to take to lock.
159 */
David Herrmannbb6d8222014-08-29 12:12:46 +0200160int drm_legacy_lock(struct drm_device *dev, void *data,
161 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000163 DECLARE_WAITQUEUE(entry, current);
Eric Anholtc153f452007-09-03 12:06:45 +1000164 struct drm_lock *lock = data;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000165 struct drm_master *master = file_priv->master;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000166 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Daniel Vetterfa538642016-08-03 21:11:10 +0200168 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetterda168d82015-06-23 11:34:21 +0200169 return -EINVAL;
170
Eric Anholt6c340ea2007-08-25 20:23:09 +1000171 ++file_priv->lock_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Eric Anholtc153f452007-09-03 12:06:45 +1000173 if (lock->context == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 DRM_ERROR("Process %d using kernel context %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700175 task_pid_nr(current), lock->context);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 return -EINVAL;
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000179 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700180 lock->context, task_pid_nr(current),
Chris Wilsona2ec1c12016-11-27 17:09:09 +0000181 master->lock.hw_lock ? master->lock.hw_lock->lock : -1,
182 lock->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Dave Airlie7c1c2872008-11-28 14:22:24 +1000184 add_wait_queue(&master->lock.lock_queue, &entry);
185 spin_lock_bh(&master->lock.spinlock);
186 master->lock.user_waiters++;
187 spin_unlock_bh(&master->lock.spinlock);
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 for (;;) {
190 __set_current_state(TASK_INTERRUPTIBLE);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000191 if (!master->lock.hw_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /* Device has been unregistered */
Thomas Hellstromfda714c2009-03-02 11:10:56 +0100193 send_sig(SIGTERM, current, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 ret = -EINTR;
195 break;
196 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000197 if (drm_lock_take(&master->lock, lock->context)) {
198 master->lock.file_priv = file_priv;
199 master->lock.lock_time = jiffies;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000200 break; /* Got lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* Contention */
Arnd Bergmann08f2e662010-08-27 08:55:28 +1000204 mutex_unlock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 schedule();
Arnd Bergmann08f2e662010-08-27 08:55:28 +1000206 mutex_lock(&drm_global_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000207 if (signal_pending(current)) {
Thomas Hellstrom4d77c882009-03-02 11:10:54 +0100208 ret = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 break;
210 }
211 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000212 spin_lock_bh(&master->lock.spinlock);
213 master->lock.user_waiters--;
214 spin_unlock_bh(&master->lock.spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 __set_current_state(TASK_RUNNING);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000216 remove_wait_queue(&master->lock.lock_queue, &entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Eric Anholtc153f452007-09-03 12:06:45 +1000218 DRM_DEBUG("%d %s\n", lock->context,
219 ret ? "interrupted" : "has lock");
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100220 if (ret) return ret;
Dave Airlied985c102006-01-02 21:32:48 +1100221
Dave Airlie3e5fc802008-08-24 17:02:26 +1000222 /* don't set the block all signals on the master process for now
223 * really probably not the correct answer but lets us debug xkb
224 * xserver for now */
Daniel Vetterb3ac9f22016-06-21 10:54:20 +0200225 if (!drm_is_current_master(file_priv)) {
Dave Airlie3e5fc802008-08-24 17:02:26 +1000226 dev->sigdata.context = lock->context;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000227 dev->sigdata.lock = master->lock.hw_lock;
Dave Airlie3e5fc802008-08-24 17:02:26 +1000228 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000229
Eric Anholtc153f452007-09-03 12:06:45 +1000230 if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT))
231 {
Dave Airlied985c102006-01-02 21:32:48 +1100232 if (dev->driver->dma_quiescent(dev)) {
Eric Anholtc153f452007-09-03 12:06:45 +1000233 DRM_DEBUG("%d waiting for DMA quiescent\n",
234 lock->context);
Eric Anholt20caafa2007-08-25 19:22:43 +1000235 return -EBUSY;
Dave Airlied985c102006-01-02 21:32:48 +1100236 }
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Dave Airlied985c102006-01-02 21:32:48 +1100239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000242/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * Unlock ioctl.
244 *
245 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000246 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * \param cmd command.
248 * \param arg user argument, pointing to a drm_lock structure.
249 * \return zero on success or negative number on failure.
250 *
251 * Transfer and free the lock.
252 */
David Herrmannbb6d8222014-08-29 12:12:46 +0200253int drm_legacy_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Eric Anholtc153f452007-09-03 12:06:45 +1000255 struct drm_lock *lock = data;
Daniel Vetter45ff46c2010-09-26 00:24:48 +0200256 struct drm_master *master = file_priv->master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Daniel Vetterfa538642016-08-03 21:11:10 +0200258 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetterda168d82015-06-23 11:34:21 +0200259 return -EINVAL;
260
Eric Anholtc153f452007-09-03 12:06:45 +1000261 if (lock->context == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000262 DRM_ERROR("Process %d using kernel context %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700263 task_pid_nr(current), lock->context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return -EINVAL;
265 }
266
David Herrmannbb6d8222014-08-29 12:12:46 +0200267 if (drm_legacy_lock_free(&master->lock, lock->context)) {
Daniel Vetter45ff46c2010-09-26 00:24:48 +0200268 /* FIXME: Should really bail out here. */
269 }
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 0;
272}
273
274/**
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100275 * This function returns immediately and takes the hw lock
276 * with the kernel context if it is free, otherwise it gets the highest priority when and if
277 * it is eventually released.
278 *
279 * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held
280 * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause
281 * a deadlock, which is why the "idlelock" was invented).
282 *
283 * This should be sufficient to wait for GPU idle without
284 * having to worry about starvation.
285 */
286
David Herrmannbb6d8222014-08-29 12:12:46 +0200287void drm_legacy_idlelock_take(struct drm_lock_data *lock_data)
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100288{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200289 int ret;
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100290
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000291 spin_lock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100292 lock_data->kernel_waiters++;
293 if (!lock_data->idle_has_lock) {
294
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000295 spin_unlock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100296 ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000297 spin_lock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100298
299 if (ret == 1)
300 lock_data->idle_has_lock = 1;
301 }
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000302 spin_unlock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100303}
David Herrmannbb6d8222014-08-29 12:12:46 +0200304EXPORT_SYMBOL(drm_legacy_idlelock_take);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100305
David Herrmannbb6d8222014-08-29 12:12:46 +0200306void drm_legacy_idlelock_release(struct drm_lock_data *lock_data)
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100307{
308 unsigned int old, prev;
309 volatile unsigned int *lock = &lock_data->hw_lock->lock;
310
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000311 spin_lock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100312 if (--lock_data->kernel_waiters == 0) {
313 if (lock_data->idle_has_lock) {
314 do {
315 old = *lock;
316 prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT);
317 } while (prev != old);
318 wake_up_interruptible(&lock_data->lock_queue);
319 lock_data->idle_has_lock = 0;
320 }
321 }
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000322 spin_unlock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100323}
David Herrmannbb6d8222014-08-29 12:12:46 +0200324EXPORT_SYMBOL(drm_legacy_idlelock_release);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100325
Daniel Vetter1a75a222016-06-14 20:50:57 +0200326static int drm_legacy_i_have_hw_lock(struct drm_device *dev,
327 struct drm_file *file_priv)
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100328{
Dave Airlie7c1c2872008-11-28 14:22:24 +1000329 struct drm_master *master = file_priv->master;
330 return (file_priv->lock_count && master->lock.hw_lock &&
331 _DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) &&
332 master->lock.file_priv == file_priv);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100333}
Daniel Vetter1a75a222016-06-14 20:50:57 +0200334
335void drm_legacy_lock_release(struct drm_device *dev, struct file *filp)
336{
337 struct drm_file *file_priv = filp->private_data;
338
339 /* if the master has gone away we can't do anything with the lock */
Daniel Vetter95c081c2016-06-21 10:54:12 +0200340 if (!dev->master)
Daniel Vetter1a75a222016-06-14 20:50:57 +0200341 return;
342
343 if (drm_legacy_i_have_hw_lock(dev, file_priv)) {
344 DRM_DEBUG("File %p released, freeing lock for context %d\n",
345 filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
346 drm_legacy_lock_free(&file_priv->master->lock,
347 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
348 }
349}