blob: 1efbd5389d89301718c831ffdf8ab80eb4682906 [file] [log] [blame]
Qian Cai45b2fda2019-07-15 09:42:53 -04001/*
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
Sam Ravnborg0500c042019-05-26 19:35:35 +020039#include <drm/drm.h>
40#include <drm/drm_drv.h>
41#include <drm/drm_file.h>
42#include <drm/drm_print.h>
43
Daniel Vetter44af3f52014-09-10 12:43:54 +020044#include "drm_internal.h"
Sam Ravnborg0500c042019-05-26 19:35:35 +020045#include "drm_legacy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Daniel Vetter4ac5ec42010-08-23 22:53:34 +020047static int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);
48
Benjamin Gaignardbd50d4a2020-03-06 11:29:35 +010049/*
Daniel Vetter1a75a222016-06-14 20:50:57 +020050 * Take the heavyweight lock.
51 *
52 * \param lock lock pointer.
53 * \param context locking context.
54 * \return one if the lock is held, or zero otherwise.
55 *
56 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
57 */
58static
59int drm_lock_take(struct drm_lock_data *lock_data,
60 unsigned int context)
61{
62 unsigned int old, new, prev;
63 volatile unsigned int *lock = &lock_data->hw_lock->lock;
64
65 spin_lock_bh(&lock_data->spinlock);
66 do {
67 old = *lock;
68 if (old & _DRM_LOCK_HELD)
69 new = old | _DRM_LOCK_CONT;
70 else {
71 new = context | _DRM_LOCK_HELD |
72 ((lock_data->user_waiters + lock_data->kernel_waiters > 1) ?
73 _DRM_LOCK_CONT : 0);
74 }
75 prev = cmpxchg(lock, old, new);
76 } while (prev != old);
77 spin_unlock_bh(&lock_data->spinlock);
78
79 if (_DRM_LOCKING_CONTEXT(old) == context) {
80 if (old & _DRM_LOCK_HELD) {
81 if (context != DRM_KERNEL_CONTEXT) {
82 DRM_ERROR("%d holds heavyweight lock\n",
83 context);
84 }
85 return 0;
86 }
87 }
88
89 if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) {
90 /* Have lock */
91 return 1;
92 }
93 return 0;
94}
95
Benjamin Gaignardbd50d4a2020-03-06 11:29:35 +010096/*
Daniel Vetter1a75a222016-06-14 20:50:57 +020097 * This takes a lock forcibly and hands it to context. Should ONLY be used
98 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
99 *
100 * \param dev DRM device.
101 * \param lock lock pointer.
102 * \param context locking context.
103 * \return always one.
104 *
105 * Resets the lock file pointer.
106 * Marks the lock as held by the given context, via the \p cmpxchg instruction.
107 */
108static int drm_lock_transfer(struct drm_lock_data *lock_data,
109 unsigned int context)
110{
111 unsigned int old, new, prev;
112 volatile unsigned int *lock = &lock_data->hw_lock->lock;
113
114 lock_data->file_priv = NULL;
115 do {
116 old = *lock;
117 new = context | _DRM_LOCK_HELD;
118 prev = cmpxchg(lock, old, new);
119 } while (prev != old);
120 return 1;
121}
122
123static int drm_legacy_lock_free(struct drm_lock_data *lock_data,
124 unsigned int context)
125{
126 unsigned int old, new, prev;
127 volatile unsigned int *lock = &lock_data->hw_lock->lock;
128
129 spin_lock_bh(&lock_data->spinlock);
130 if (lock_data->kernel_waiters != 0) {
131 drm_lock_transfer(lock_data, 0);
132 lock_data->idle_has_lock = 1;
133 spin_unlock_bh(&lock_data->spinlock);
134 return 1;
135 }
136 spin_unlock_bh(&lock_data->spinlock);
137
138 do {
139 old = *lock;
140 new = _DRM_LOCKING_CONTEXT(old);
141 prev = cmpxchg(lock, old, new);
142 } while (prev != old);
143
144 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
145 DRM_ERROR("%d freed heavyweight lock held by %d\n",
146 context, _DRM_LOCKING_CONTEXT(old));
147 return 1;
148 }
149 wake_up_interruptible(&lock_data->lock_queue);
150 return 0;
151}
152
Benjamin Gaignardbd50d4a2020-03-06 11:29:35 +0100153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * Lock ioctl.
155 *
156 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000157 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * \param cmd command.
159 * \param arg user argument, pointing to a drm_lock structure.
160 * \return zero on success or negative number on failure.
161 *
162 * Add the current task to the lock wait queue, and attempt to take to lock.
163 */
David Herrmannbb6d8222014-08-29 12:12:46 +0200164int drm_legacy_lock(struct drm_device *dev, void *data,
165 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000167 DECLARE_WAITQUEUE(entry, current);
Eric Anholtc153f452007-09-03 12:06:45 +1000168 struct drm_lock *lock = data;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000169 struct drm_master *master = file_priv->master;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000170 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Daniel Vetterfa538642016-08-03 21:11:10 +0200172 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100173 return -EOPNOTSUPP;
Daniel Vetterda168d82015-06-23 11:34:21 +0200174
Eric Anholt6c340ea2007-08-25 20:23:09 +1000175 ++file_priv->lock_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Eric Anholtc153f452007-09-03 12:06:45 +1000177 if (lock->context == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000178 DRM_ERROR("Process %d using kernel context %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700179 task_pid_nr(current), lock->context);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000180 return -EINVAL;
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000183 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700184 lock->context, task_pid_nr(current),
Chris Wilsona2ec1c12016-11-27 17:09:09 +0000185 master->lock.hw_lock ? master->lock.hw_lock->lock : -1,
186 lock->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Dave Airlie7c1c2872008-11-28 14:22:24 +1000188 add_wait_queue(&master->lock.lock_queue, &entry);
189 spin_lock_bh(&master->lock.spinlock);
190 master->lock.user_waiters++;
191 spin_unlock_bh(&master->lock.spinlock);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 for (;;) {
194 __set_current_state(TASK_INTERRUPTIBLE);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000195 if (!master->lock.hw_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Device has been unregistered */
Thomas Hellstromfda714c2009-03-02 11:10:56 +0100197 send_sig(SIGTERM, current, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ret = -EINTR;
199 break;
200 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000201 if (drm_lock_take(&master->lock, lock->context)) {
202 master->lock.file_priv = file_priv;
203 master->lock.lock_time = jiffies;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000204 break; /* Got lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Contention */
Arnd Bergmann08f2e662010-08-27 08:55:28 +1000208 mutex_unlock(&drm_global_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 schedule();
Arnd Bergmann08f2e662010-08-27 08:55:28 +1000210 mutex_lock(&drm_global_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 if (signal_pending(current)) {
Thomas Hellstrom4d77c882009-03-02 11:10:54 +0100212 ret = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 break;
214 }
215 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000216 spin_lock_bh(&master->lock.spinlock);
217 master->lock.user_waiters--;
218 spin_unlock_bh(&master->lock.spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 __set_current_state(TASK_RUNNING);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000220 remove_wait_queue(&master->lock.lock_queue, &entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Eric Anholtc153f452007-09-03 12:06:45 +1000222 DRM_DEBUG("%d %s\n", lock->context,
223 ret ? "interrupted" : "has lock");
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100224 if (ret) return ret;
Dave Airlied985c102006-01-02 21:32:48 +1100225
Dave Airlie3e5fc802008-08-24 17:02:26 +1000226 /* don't set the block all signals on the master process for now
227 * really probably not the correct answer but lets us debug xkb
228 * xserver for now */
Daniel Vetterb3ac9f22016-06-21 10:54:20 +0200229 if (!drm_is_current_master(file_priv)) {
Dave Airlie3e5fc802008-08-24 17:02:26 +1000230 dev->sigdata.context = lock->context;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000231 dev->sigdata.lock = master->lock.hw_lock;
Dave Airlie3e5fc802008-08-24 17:02:26 +1000232 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000233
Eric Anholtc153f452007-09-03 12:06:45 +1000234 if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT))
235 {
Dave Airlied985c102006-01-02 21:32:48 +1100236 if (dev->driver->dma_quiescent(dev)) {
Eric Anholtc153f452007-09-03 12:06:45 +1000237 DRM_DEBUG("%d waiting for DMA quiescent\n",
238 lock->context);
Eric Anholt20caafa2007-08-25 19:22:43 +1000239 return -EBUSY;
Dave Airlied985c102006-01-02 21:32:48 +1100240 }
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Dave Airlied985c102006-01-02 21:32:48 +1100243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Benjamin Gaignardbd50d4a2020-03-06 11:29:35 +0100246/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * Unlock ioctl.
248 *
249 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000250 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * \param cmd command.
252 * \param arg user argument, pointing to a drm_lock structure.
253 * \return zero on success or negative number on failure.
254 *
255 * Transfer and free the lock.
256 */
David Herrmannbb6d8222014-08-29 12:12:46 +0200257int drm_legacy_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Eric Anholtc153f452007-09-03 12:06:45 +1000259 struct drm_lock *lock = data;
Daniel Vetter45ff46c2010-09-26 00:24:48 +0200260 struct drm_master *master = file_priv->master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Daniel Vetterfa538642016-08-03 21:11:10 +0200262 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Chris Wilson69fdf422018-09-13 20:20:50 +0100263 return -EOPNOTSUPP;
Daniel Vetterda168d82015-06-23 11:34:21 +0200264
Eric Anholtc153f452007-09-03 12:06:45 +1000265 if (lock->context == DRM_KERNEL_CONTEXT) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000266 DRM_ERROR("Process %d using kernel context %d\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700267 task_pid_nr(current), lock->context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return -EINVAL;
269 }
270
David Herrmannbb6d8222014-08-29 12:12:46 +0200271 if (drm_legacy_lock_free(&master->lock, lock->context)) {
Daniel Vetter45ff46c2010-09-26 00:24:48 +0200272 /* FIXME: Should really bail out here. */
273 }
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 0;
276}
277
Benjamin Gaignardbd50d4a2020-03-06 11:29:35 +0100278/*
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100279 * This function returns immediately and takes the hw lock
280 * with the kernel context if it is free, otherwise it gets the highest priority when and if
281 * it is eventually released.
282 *
283 * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held
284 * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause
285 * a deadlock, which is why the "idlelock" was invented).
286 *
287 * This should be sufficient to wait for GPU idle without
288 * having to worry about starvation.
289 */
David Herrmannbb6d8222014-08-29 12:12:46 +0200290void drm_legacy_idlelock_take(struct drm_lock_data *lock_data)
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100291{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200292 int ret;
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100293
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000294 spin_lock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100295 lock_data->kernel_waiters++;
296 if (!lock_data->idle_has_lock) {
297
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000298 spin_unlock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100299 ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000300 spin_lock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100301
302 if (ret == 1)
303 lock_data->idle_has_lock = 1;
304 }
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000305 spin_unlock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100306}
David Herrmannbb6d8222014-08-29 12:12:46 +0200307EXPORT_SYMBOL(drm_legacy_idlelock_take);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100308
David Herrmannbb6d8222014-08-29 12:12:46 +0200309void drm_legacy_idlelock_release(struct drm_lock_data *lock_data)
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100310{
311 unsigned int old, prev;
312 volatile unsigned int *lock = &lock_data->hw_lock->lock;
313
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000314 spin_lock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100315 if (--lock_data->kernel_waiters == 0) {
316 if (lock_data->idle_has_lock) {
317 do {
318 old = *lock;
319 prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT);
320 } while (prev != old);
321 wake_up_interruptible(&lock_data->lock_queue);
322 lock_data->idle_has_lock = 0;
323 }
324 }
Thomas Hellstromf116cc52008-05-07 12:22:39 +1000325 spin_unlock_bh(&lock_data->spinlock);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100326}
David Herrmannbb6d8222014-08-29 12:12:46 +0200327EXPORT_SYMBOL(drm_legacy_idlelock_release);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100328
Daniel Vetter1a75a222016-06-14 20:50:57 +0200329static int drm_legacy_i_have_hw_lock(struct drm_device *dev,
330 struct drm_file *file_priv)
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100331{
Dave Airlie7c1c2872008-11-28 14:22:24 +1000332 struct drm_master *master = file_priv->master;
Suraj Upadhyay948de8422020-07-02 18:53:32 +0530333
Dave Airlie7c1c2872008-11-28 14:22:24 +1000334 return (file_priv->lock_count && master->lock.hw_lock &&
335 _DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) &&
336 master->lock.file_priv == file_priv);
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100337}
Daniel Vetter1a75a222016-06-14 20:50:57 +0200338
339void drm_legacy_lock_release(struct drm_device *dev, struct file *filp)
340{
341 struct drm_file *file_priv = filp->private_data;
342
343 /* if the master has gone away we can't do anything with the lock */
Daniel Vetter95c081c2016-06-21 10:54:12 +0200344 if (!dev->master)
Daniel Vetter1a75a222016-06-14 20:50:57 +0200345 return;
346
347 if (drm_legacy_i_have_hw_lock(dev, file_priv)) {
348 DRM_DEBUG("File %p released, freeing lock for context %d\n",
349 filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
350 drm_legacy_lock_free(&file_priv->master->lock,
351 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
352 }
353}
Dave Airlie058ca502019-04-23 09:36:54 +1000354
355void drm_legacy_lock_master_cleanup(struct drm_device *dev, struct drm_master *master)
356{
357 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
358 return;
359
360 /*
361 * Since the master is disappearing, so is the
362 * possibility to lock.
Dan Carpenter5b99cad2020-01-08 08:43:12 +0300363 */
364 mutex_lock(&dev->struct_mutex);
Dave Airlie058ca502019-04-23 09:36:54 +1000365 if (master->lock.hw_lock) {
366 if (dev->sigdata.lock == master->lock.hw_lock)
367 dev->sigdata.lock = NULL;
368 master->lock.hw_lock = NULL;
369 master->lock.file_priv = NULL;
370 wake_up_interruptible_all(&master->lock.lock_queue);
371 }
372 mutex_unlock(&dev->struct_mutex);
373}