Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / vmwgfx / ttm_lock.c
blob5971c72e6d10cbc32e4da9ffe6dd35b53f90f0f0
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
5 * All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
32 #include <linux/atomic.h>
33 #include <linux/errno.h>
34 #include <linux/wait.h>
35 #include <linux/sched/signal.h>
36 #include "ttm_lock.h"
37 #include "ttm_object.h"
39 #define TTM_WRITE_LOCK_PENDING (1 << 0)
40 #define TTM_VT_LOCK_PENDING (1 << 1)
41 #define TTM_SUSPEND_LOCK_PENDING (1 << 2)
42 #define TTM_VT_LOCK (1 << 3)
43 #define TTM_SUSPEND_LOCK (1 << 4)
45 void ttm_lock_init(struct ttm_lock *lock)
47 spin_lock_init(&lock->lock);
48 init_waitqueue_head(&lock->queue);
49 lock->rw = 0;
50 lock->flags = 0;
53 void ttm_read_unlock(struct ttm_lock *lock)
55 spin_lock(&lock->lock);
56 if (--lock->rw == 0)
57 wake_up_all(&lock->queue);
58 spin_unlock(&lock->lock);
61 static bool __ttm_read_lock(struct ttm_lock *lock)
63 bool locked = false;
65 spin_lock(&lock->lock);
66 if (lock->rw >= 0 && lock->flags == 0) {
67 ++lock->rw;
68 locked = true;
70 spin_unlock(&lock->lock);
71 return locked;
74 int ttm_read_lock(struct ttm_lock *lock, bool interruptible)
76 int ret = 0;
78 if (interruptible)
79 ret = wait_event_interruptible(lock->queue,
80 __ttm_read_lock(lock));
81 else
82 wait_event(lock->queue, __ttm_read_lock(lock));
83 return ret;
86 static bool __ttm_read_trylock(struct ttm_lock *lock, bool *locked)
88 bool block = true;
90 *locked = false;
92 spin_lock(&lock->lock);
93 if (lock->rw >= 0 && lock->flags == 0) {
94 ++lock->rw;
95 block = false;
96 *locked = true;
97 } else if (lock->flags == 0) {
98 block = false;
100 spin_unlock(&lock->lock);
102 return !block;
105 int ttm_read_trylock(struct ttm_lock *lock, bool interruptible)
107 int ret = 0;
108 bool locked;
110 if (interruptible)
111 ret = wait_event_interruptible
112 (lock->queue, __ttm_read_trylock(lock, &locked));
113 else
114 wait_event(lock->queue, __ttm_read_trylock(lock, &locked));
116 if (unlikely(ret != 0)) {
117 BUG_ON(locked);
118 return ret;
121 return (locked) ? 0 : -EBUSY;
124 void ttm_write_unlock(struct ttm_lock *lock)
126 spin_lock(&lock->lock);
127 lock->rw = 0;
128 wake_up_all(&lock->queue);
129 spin_unlock(&lock->lock);
132 static bool __ttm_write_lock(struct ttm_lock *lock)
134 bool locked = false;
136 spin_lock(&lock->lock);
137 if (lock->rw == 0 && ((lock->flags & ~TTM_WRITE_LOCK_PENDING) == 0)) {
138 lock->rw = -1;
139 lock->flags &= ~TTM_WRITE_LOCK_PENDING;
140 locked = true;
141 } else {
142 lock->flags |= TTM_WRITE_LOCK_PENDING;
144 spin_unlock(&lock->lock);
145 return locked;
148 int ttm_write_lock(struct ttm_lock *lock, bool interruptible)
150 int ret = 0;
152 if (interruptible) {
153 ret = wait_event_interruptible(lock->queue,
154 __ttm_write_lock(lock));
155 if (unlikely(ret != 0)) {
156 spin_lock(&lock->lock);
157 lock->flags &= ~TTM_WRITE_LOCK_PENDING;
158 wake_up_all(&lock->queue);
159 spin_unlock(&lock->lock);
161 } else
162 wait_event(lock->queue, __ttm_write_lock(lock));
164 return ret;
167 void ttm_suspend_unlock(struct ttm_lock *lock)
169 spin_lock(&lock->lock);
170 lock->flags &= ~TTM_SUSPEND_LOCK;
171 wake_up_all(&lock->queue);
172 spin_unlock(&lock->lock);
175 static bool __ttm_suspend_lock(struct ttm_lock *lock)
177 bool locked = false;
179 spin_lock(&lock->lock);
180 if (lock->rw == 0) {
181 lock->flags &= ~TTM_SUSPEND_LOCK_PENDING;
182 lock->flags |= TTM_SUSPEND_LOCK;
183 locked = true;
184 } else {
185 lock->flags |= TTM_SUSPEND_LOCK_PENDING;
187 spin_unlock(&lock->lock);
188 return locked;
191 void ttm_suspend_lock(struct ttm_lock *lock)
193 wait_event(lock->queue, __ttm_suspend_lock(lock));