drm_lock.c revision 1.2 1 /* $NetBSD: drm_lock.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $ */
2
3 /**
4 * \file drm_lock.c
5 * IOCTLs for locking
6 *
7 * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
8 * \author Gareth Hughes <gareth (at) valinux.com>
9 */
10
11 /*
12 * Created: Tue Feb 2 08:37:54 1999 by faith (at) valinux.com
13 *
14 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
15 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
16 * All Rights Reserved.
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a
19 * copy of this software and associated documentation files (the "Software"),
20 * to deal in the Software without restriction, including without limitation
21 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
22 * and/or sell copies of the Software, and to permit persons to whom the
23 * Software is furnished to do so, subject to the following conditions:
24 *
25 * The above copyright notice and this permission notice (including the next
26 * paragraph) shall be included in all copies or substantial portions of the
27 * Software.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
33 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
34 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35 * OTHER DEALINGS IN THE SOFTWARE.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: drm_lock.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $");
40
41 #include <linux/export.h>
42 #include <drm/drmP.h>
43 #include "drm_legacy.h"
44 #include "drm_internal.h"
45
46 static int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);
47
48 /**
49 * Lock ioctl.
50 *
51 * \param inode device inode.
52 * \param file_priv DRM file private.
53 * \param cmd command.
54 * \param arg user argument, pointing to a drm_lock structure.
55 * \return zero on success or negative number on failure.
56 *
57 * Add the current task to the lock wait queue, and attempt to take to lock.
58 */
59 int drm_legacy_lock(struct drm_device *dev, void *data,
60 struct drm_file *file_priv)
61 {
62 DECLARE_WAITQUEUE(entry, current);
63 struct drm_lock *lock = data;
64 struct drm_master *master = file_priv->master;
65 int ret = 0;
66
67 if (drm_core_check_feature(dev, DRIVER_MODESET))
68 return -EINVAL;
69
70 ++file_priv->lock_count;
71
72 if (lock->context == DRM_KERNEL_CONTEXT) {
73 DRM_ERROR("Process %d using kernel context %d\n",
74 task_pid_nr(current), lock->context);
75 return -EINVAL;
76 }
77
78 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
79 lock->context, task_pid_nr(current),
80 master->lock.hw_lock->lock, lock->flags);
81
82 add_wait_queue(&master->lock.lock_queue, &entry);
83 spin_lock_bh(&master->lock.spinlock);
84 master->lock.user_waiters++;
85 spin_unlock_bh(&master->lock.spinlock);
86
87 for (;;) {
88 __set_current_state(TASK_INTERRUPTIBLE);
89 if (!master->lock.hw_lock) {
90 /* Device has been unregistered */
91 send_sig(SIGTERM, current, 0);
92 ret = -EINTR;
93 break;
94 }
95 if (drm_lock_take(&master->lock, lock->context)) {
96 master->lock.file_priv = file_priv;
97 master->lock.lock_time = jiffies;
98 break; /* Got lock */
99 }
100
101 /* Contention */
102 mutex_unlock(&drm_global_mutex);
103 schedule();
104 mutex_lock(&drm_global_mutex);
105 if (signal_pending(current)) {
106 ret = -EINTR;
107 break;
108 }
109 }
110 spin_lock_bh(&master->lock.spinlock);
111 master->lock.user_waiters--;
112 spin_unlock_bh(&master->lock.spinlock);
113 __set_current_state(TASK_RUNNING);
114 remove_wait_queue(&master->lock.lock_queue, &entry);
115
116 DRM_DEBUG("%d %s\n", lock->context,
117 ret ? "interrupted" : "has lock");
118 if (ret) return ret;
119
120 /* don't set the block all signals on the master process for now
121 * really probably not the correct answer but lets us debug xkb
122 * xserver for now */
123 if (!file_priv->is_master) {
124 dev->sigdata.context = lock->context;
125 dev->sigdata.lock = master->lock.hw_lock;
126 }
127
128 if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT))
129 {
130 if (dev->driver->dma_quiescent(dev)) {
131 DRM_DEBUG("%d waiting for DMA quiescent\n",
132 lock->context);
133 return -EBUSY;
134 }
135 }
136
137 return 0;
138 }
139
140 /**
141 * Unlock ioctl.
142 *
143 * \param inode device inode.
144 * \param file_priv DRM file private.
145 * \param cmd command.
146 * \param arg user argument, pointing to a drm_lock structure.
147 * \return zero on success or negative number on failure.
148 *
149 * Transfer and free the lock.
150 */
151 int drm_legacy_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
152 {
153 struct drm_lock *lock = data;
154 struct drm_master *master = file_priv->master;
155
156 if (drm_core_check_feature(dev, DRIVER_MODESET))
157 return -EINVAL;
158
159 if (lock->context == DRM_KERNEL_CONTEXT) {
160 DRM_ERROR("Process %d using kernel context %d\n",
161 task_pid_nr(current), lock->context);
162 return -EINVAL;
163 }
164
165 if (drm_legacy_lock_free(&master->lock, lock->context)) {
166 /* FIXME: Should really bail out here. */
167 }
168
169 return 0;
170 }
171
172 /**
173 * Take the heavyweight lock.
174 *
175 * \param lock lock pointer.
176 * \param context locking context.
177 * \return one if the lock is held, or zero otherwise.
178 *
179 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
180 */
181 static
182 int drm_lock_take(struct drm_lock_data *lock_data,
183 unsigned int context)
184 {
185 unsigned int old, new, prev;
186 volatile unsigned int *lock = &lock_data->hw_lock->lock;
187
188 spin_lock_bh(&lock_data->spinlock);
189 do {
190 old = *lock;
191 if (old & _DRM_LOCK_HELD)
192 new = old | _DRM_LOCK_CONT;
193 else {
194 new = context | _DRM_LOCK_HELD |
195 ((lock_data->user_waiters + lock_data->kernel_waiters > 1) ?
196 _DRM_LOCK_CONT : 0);
197 }
198 prev = cmpxchg(lock, old, new);
199 } while (prev != old);
200 spin_unlock_bh(&lock_data->spinlock);
201
202 if (_DRM_LOCKING_CONTEXT(old) == context) {
203 if (old & _DRM_LOCK_HELD) {
204 if (context != DRM_KERNEL_CONTEXT) {
205 DRM_ERROR("%d holds heavyweight lock\n",
206 context);
207 }
208 return 0;
209 }
210 }
211
212 if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) {
213 /* Have lock */
214 return 1;
215 }
216 return 0;
217 }
218
219 /**
220 * This takes a lock forcibly and hands it to context. Should ONLY be used
221 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
222 *
223 * \param dev DRM device.
224 * \param lock lock pointer.
225 * \param context locking context.
226 * \return always one.
227 *
228 * Resets the lock file pointer.
229 * Marks the lock as held by the given context, via the \p cmpxchg instruction.
230 */
231 static int drm_lock_transfer(struct drm_lock_data *lock_data,
232 unsigned int context)
233 {
234 unsigned int old, new, prev;
235 volatile unsigned int *lock = &lock_data->hw_lock->lock;
236
237 lock_data->file_priv = NULL;
238 do {
239 old = *lock;
240 new = context | _DRM_LOCK_HELD;
241 prev = cmpxchg(lock, old, new);
242 } while (prev != old);
243 return 1;
244 }
245
246 /**
247 * Free lock.
248 *
249 * \param dev DRM device.
250 * \param lock lock.
251 * \param context context.
252 *
253 * Resets the lock file pointer.
254 * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
255 * waiting on the lock queue.
256 */
257 int drm_legacy_lock_free(struct drm_lock_data *lock_data, unsigned int context)
258 {
259 unsigned int old, new, prev;
260 volatile unsigned int *lock = &lock_data->hw_lock->lock;
261
262 spin_lock_bh(&lock_data->spinlock);
263 if (lock_data->kernel_waiters != 0) {
264 drm_lock_transfer(lock_data, 0);
265 lock_data->idle_has_lock = 1;
266 spin_unlock_bh(&lock_data->spinlock);
267 return 1;
268 }
269 spin_unlock_bh(&lock_data->spinlock);
270
271 do {
272 old = *lock;
273 new = _DRM_LOCKING_CONTEXT(old);
274 prev = cmpxchg(lock, old, new);
275 } while (prev != old);
276
277 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
278 DRM_ERROR("%d freed heavyweight lock held by %d\n",
279 context, _DRM_LOCKING_CONTEXT(old));
280 return 1;
281 }
282 wake_up_interruptible(&lock_data->lock_queue);
283 return 0;
284 }
285
286 /**
287 * This function returns immediately and takes the hw lock
288 * with the kernel context if it is free, otherwise it gets the highest priority when and if
289 * it is eventually released.
290 *
291 * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held
292 * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause
293 * a deadlock, which is why the "idlelock" was invented).
294 *
295 * This should be sufficient to wait for GPU idle without
296 * having to worry about starvation.
297 */
298
299 void drm_legacy_idlelock_take(struct drm_lock_data *lock_data)
300 {
301 int ret;
302
303 spin_lock_bh(&lock_data->spinlock);
304 lock_data->kernel_waiters++;
305 if (!lock_data->idle_has_lock) {
306
307 spin_unlock_bh(&lock_data->spinlock);
308 ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
309 spin_lock_bh(&lock_data->spinlock);
310
311 if (ret == 1)
312 lock_data->idle_has_lock = 1;
313 }
314 spin_unlock_bh(&lock_data->spinlock);
315 }
316 EXPORT_SYMBOL(drm_legacy_idlelock_take);
317
318 void drm_legacy_idlelock_release(struct drm_lock_data *lock_data)
319 {
320 unsigned int old, prev;
321 volatile unsigned int *lock = &lock_data->hw_lock->lock;
322
323 spin_lock_bh(&lock_data->spinlock);
324 if (--lock_data->kernel_waiters == 0) {
325 if (lock_data->idle_has_lock) {
326 do {
327 old = *lock;
328 prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT);
329 } while (prev != old);
330 wake_up_interruptible(&lock_data->lock_queue);
331 lock_data->idle_has_lock = 0;
332 }
333 }
334 spin_unlock_bh(&lock_data->spinlock);
335 }
336 EXPORT_SYMBOL(drm_legacy_idlelock_release);
337
338 int drm_legacy_i_have_hw_lock(struct drm_device *dev,
339 struct drm_file *file_priv)
340 {
341 struct drm_master *master = file_priv->master;
342 return (file_priv->lock_count && master->lock.hw_lock &&
343 _DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) &&
344 master->lock.file_priv == file_priv);
345 }
346