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