drm_lock.c revision 1.2.4.2 1 1.2.4.2 rmind /* $NetBSD: drm_lock.c,v 1.2.4.2 2014/05/18 17:46:00 rmind Exp $ */
2 1.2.4.2 rmind
3 1.2.4.2 rmind /*-
4 1.2.4.2 rmind * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.2.4.2 rmind * All rights reserved.
6 1.2.4.2 rmind *
7 1.2.4.2 rmind * This code is derived from software contributed to The NetBSD Foundation
8 1.2.4.2 rmind * by Taylor R. Campbell.
9 1.2.4.2 rmind *
10 1.2.4.2 rmind * Redistribution and use in source and binary forms, with or without
11 1.2.4.2 rmind * modification, are permitted provided that the following conditions
12 1.2.4.2 rmind * are met:
13 1.2.4.2 rmind * 1. Redistributions of source code must retain the above copyright
14 1.2.4.2 rmind * notice, this list of conditions and the following disclaimer.
15 1.2.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.4.2 rmind * notice, this list of conditions and the following disclaimer in the
17 1.2.4.2 rmind * documentation and/or other materials provided with the distribution.
18 1.2.4.2 rmind *
19 1.2.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.4.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.4.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.4.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.4.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.4.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.4.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.4.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.4.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.4.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.4.2 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.2.4.2 rmind */
31 1.2.4.2 rmind
32 1.2.4.2 rmind /*
33 1.2.4.2 rmind * DRM lock. Each drm master has a heavy-weight lock to provide mutual
34 1.2.4.2 rmind * exclusion for access to the hardware. The lock can be held by the
35 1.2.4.2 rmind * kernel or by a drm file; the kernel takes access only for unusual
36 1.2.4.2 rmind * purposes, with drm_idlelock_take, mainly for idling the GPU when
37 1.2.4.2 rmind * closing down.
38 1.2.4.2 rmind *
39 1.2.4.2 rmind * The physical memory storing the lock state is shared between
40 1.2.4.2 rmind * userland and kernel: the pointer at dev->master->lock->hw_lock is
41 1.2.4.2 rmind * mapped into both userland and kernel address spaces. This way,
42 1.2.4.2 rmind * userland can try to take the hardware lock without a system call,
43 1.2.4.2 rmind * although if it fails then it will use the DRM_LOCK ioctl to block
44 1.2.4.2 rmind * atomically until the lock is available. All this means that the
45 1.2.4.2 rmind * kernel must use atomic_ops to manage the lock state.
46 1.2.4.2 rmind */
47 1.2.4.2 rmind
48 1.2.4.2 rmind #include <sys/cdefs.h>
49 1.2.4.2 rmind __KERNEL_RCSID(0, "$NetBSD: drm_lock.c,v 1.2.4.2 2014/05/18 17:46:00 rmind Exp $");
50 1.2.4.2 rmind
51 1.2.4.2 rmind #include <sys/types.h>
52 1.2.4.2 rmind #include <sys/errno.h>
53 1.2.4.2 rmind #include <sys/systm.h>
54 1.2.4.2 rmind
55 1.2.4.2 rmind #include <drm/drmP.h>
56 1.2.4.2 rmind
57 1.2.4.2 rmind static bool drm_lock_acquire(struct drm_lock_data *, int);
58 1.2.4.2 rmind static void drm_lock_release(struct drm_lock_data *, int);
59 1.2.4.2 rmind static int drm_lock_block_signals(struct drm_device *, struct drm_lock *,
60 1.2.4.2 rmind struct drm_file *);
61 1.2.4.2 rmind static void drm_lock_unblock_signals(struct drm_device *,
62 1.2.4.2 rmind struct drm_lock *, struct drm_file *);
63 1.2.4.2 rmind
64 1.2.4.2 rmind /*
65 1.2.4.2 rmind * Take the lock on behalf of userland.
66 1.2.4.2 rmind */
67 1.2.4.2 rmind int
68 1.2.4.2 rmind drm_lock(struct drm_device *dev, void *data, struct drm_file *file)
69 1.2.4.2 rmind {
70 1.2.4.2 rmind struct drm_lock *lock_request = data;
71 1.2.4.2 rmind struct drm_master *master = file->master;
72 1.2.4.2 rmind int error;
73 1.2.4.2 rmind
74 1.2.4.2 rmind /* Sanitize the drm global mutex bollocks until we get rid of it. */
75 1.2.4.2 rmind KASSERT(mutex_is_locked(&drm_global_mutex));
76 1.2.4.2 rmind mutex_unlock(&drm_global_mutex);
77 1.2.4.2 rmind
78 1.2.4.2 rmind /* Refuse to lock on behalf of the kernel. */
79 1.2.4.2 rmind if (lock_request->context == DRM_KERNEL_CONTEXT) {
80 1.2.4.2 rmind error = -EINVAL;
81 1.2.4.2 rmind goto out0;
82 1.2.4.2 rmind }
83 1.2.4.2 rmind
84 1.2.4.2 rmind /* Refuse to set the magic bits. */
85 1.2.4.2 rmind if (lock_request->context !=
86 1.2.4.2 rmind _DRM_LOCKING_CONTEXT(lock_request->context)) {
87 1.2.4.2 rmind error = -EINVAL;
88 1.2.4.2 rmind goto out0;
89 1.2.4.2 rmind }
90 1.2.4.2 rmind
91 1.2.4.2 rmind /* Count it in the file and device statistics (XXX why here?). */
92 1.2.4.2 rmind file->lock_count++;
93 1.2.4.2 rmind atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
94 1.2.4.2 rmind
95 1.2.4.2 rmind /* Wait until the hardware lock is gone or we can acquire it. */
96 1.2.4.2 rmind spin_lock(&master->lock.spinlock);
97 1.2.4.2 rmind
98 1.2.4.2 rmind if (master->lock.user_waiters == UINT32_MAX) {
99 1.2.4.2 rmind error = -EBUSY;
100 1.2.4.2 rmind goto out1;
101 1.2.4.2 rmind }
102 1.2.4.2 rmind
103 1.2.4.2 rmind master->lock.user_waiters++;
104 1.2.4.2 rmind DRM_SPIN_WAIT_UNTIL(error, &master->lock.lock_queue,
105 1.2.4.2 rmind &master->lock.spinlock,
106 1.2.4.2 rmind ((master->lock.hw_lock == NULL) ||
107 1.2.4.2 rmind drm_lock_acquire(&master->lock, lock_request->context)));
108 1.2.4.2 rmind KASSERT(0 < master->lock.user_waiters);
109 1.2.4.2 rmind master->lock.user_waiters--;
110 1.2.4.2 rmind if (error)
111 1.2.4.2 rmind goto out1;
112 1.2.4.2 rmind
113 1.2.4.2 rmind /* If the lock is gone, give up. */
114 1.2.4.2 rmind if (master->lock.hw_lock == NULL) {
115 1.2.4.2 rmind #if 0 /* XXX Linux sends SIGTERM, but why? */
116 1.2.4.2 rmind mutex_enter(proc_lock);
117 1.2.4.2 rmind psignal(curproc, SIGTERM);
118 1.2.4.2 rmind mutex_exit(proc_lock);
119 1.2.4.2 rmind error = -EINTR;
120 1.2.4.2 rmind #else
121 1.2.4.2 rmind error = -ENXIO;
122 1.2.4.2 rmind #endif
123 1.2.4.2 rmind goto out1;
124 1.2.4.2 rmind }
125 1.2.4.2 rmind
126 1.2.4.2 rmind /* Mark the lock as owned by file. */
127 1.2.4.2 rmind master->lock.file_priv = file;
128 1.2.4.2 rmind master->lock.lock_time = jiffies; /* XXX Unused? */
129 1.2.4.2 rmind
130 1.2.4.2 rmind /* Block signals while the lock is held. */
131 1.2.4.2 rmind error = drm_lock_block_signals(dev, lock_request, file);
132 1.2.4.2 rmind if (error)
133 1.2.4.2 rmind goto fail2;
134 1.2.4.2 rmind
135 1.2.4.2 rmind /* Enter the DMA quiescent state if requested and available. */
136 1.2.4.2 rmind /* XXX Drop the spin lock first... */
137 1.2.4.2 rmind if (ISSET(lock_request->flags, _DRM_LOCK_QUIESCENT) &&
138 1.2.4.2 rmind (dev->driver->dma_quiescent != NULL)) {
139 1.2.4.2 rmind error = (*dev->driver->dma_quiescent)(dev);
140 1.2.4.2 rmind if (error)
141 1.2.4.2 rmind goto fail3;
142 1.2.4.2 rmind }
143 1.2.4.2 rmind
144 1.2.4.2 rmind /* Success! */
145 1.2.4.2 rmind error = 0;
146 1.2.4.2 rmind goto out1;
147 1.2.4.2 rmind
148 1.2.4.2 rmind fail3: drm_lock_unblock_signals(dev, lock_request, file);
149 1.2.4.2 rmind fail2: drm_lock_release(&master->lock, lock_request->context);
150 1.2.4.2 rmind master->lock.file_priv = NULL;
151 1.2.4.2 rmind out1: spin_unlock(&master->lock.spinlock);
152 1.2.4.2 rmind out0: mutex_lock(&drm_global_mutex);
153 1.2.4.2 rmind return error;
154 1.2.4.2 rmind }
155 1.2.4.2 rmind
156 1.2.4.2 rmind /*
157 1.2.4.2 rmind * Try to relinquish a lock that userland thinks it holds, per
158 1.2.4.2 rmind * userland's request. Fail if it doesn't actually hold the lock.
159 1.2.4.2 rmind */
160 1.2.4.2 rmind int
161 1.2.4.2 rmind drm_unlock(struct drm_device *dev, void *data, struct drm_file *file)
162 1.2.4.2 rmind {
163 1.2.4.2 rmind struct drm_lock *lock_request = data;
164 1.2.4.2 rmind struct drm_master *master = file->master;
165 1.2.4.2 rmind int error;
166 1.2.4.2 rmind
167 1.2.4.2 rmind /* Sanitize the drm global mutex bollocks until we get rid of it. */
168 1.2.4.2 rmind KASSERT(mutex_is_locked(&drm_global_mutex));
169 1.2.4.2 rmind mutex_unlock(&drm_global_mutex);
170 1.2.4.2 rmind
171 1.2.4.2 rmind /* Refuse to unlock on behalf of the kernel. */
172 1.2.4.2 rmind if (lock_request->context == DRM_KERNEL_CONTEXT) {
173 1.2.4.2 rmind error = -EINVAL;
174 1.2.4.2 rmind goto out0;
175 1.2.4.2 rmind }
176 1.2.4.2 rmind
177 1.2.4.2 rmind /* Count it in the device statistics. */
178 1.2.4.2 rmind atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
179 1.2.4.2 rmind
180 1.2.4.2 rmind /* Lock the internal spin lock to make changes. */
181 1.2.4.2 rmind spin_lock(&master->lock.spinlock);
182 1.2.4.2 rmind
183 1.2.4.2 rmind /* Make sure it's actually locked. */
184 1.2.4.2 rmind if (!_DRM_LOCK_IS_HELD(master->lock.hw_lock->lock)) {
185 1.2.4.2 rmind error = -EINVAL; /* XXX Right error? */
186 1.2.4.2 rmind goto out1;
187 1.2.4.2 rmind }
188 1.2.4.2 rmind
189 1.2.4.2 rmind /* Make sure it's locked in the right context. */
190 1.2.4.2 rmind if (_DRM_LOCKING_CONTEXT(master->lock.hw_lock->lock) !=
191 1.2.4.2 rmind lock_request->context) {
192 1.2.4.2 rmind error = -EACCES; /* XXX Right error? */
193 1.2.4.2 rmind goto out1;
194 1.2.4.2 rmind }
195 1.2.4.2 rmind
196 1.2.4.2 rmind /* Make sure it's locked by us. */
197 1.2.4.2 rmind if (master->lock.file_priv != file) {
198 1.2.4.2 rmind error = -EACCES; /* XXX Right error? */
199 1.2.4.2 rmind goto out1;
200 1.2.4.2 rmind }
201 1.2.4.2 rmind
202 1.2.4.2 rmind /* Actually release the lock. */
203 1.2.4.2 rmind drm_lock_release(&master->lock, lock_request->context);
204 1.2.4.2 rmind
205 1.2.4.2 rmind /* Clear the lock's file pointer, just in case. */
206 1.2.4.2 rmind master->lock.file_priv = NULL;
207 1.2.4.2 rmind
208 1.2.4.2 rmind /* Unblock the signals we blocked in drm_lock. */
209 1.2.4.2 rmind drm_lock_unblock_signals(dev, lock_request, file);
210 1.2.4.2 rmind
211 1.2.4.2 rmind /* Success! */
212 1.2.4.2 rmind error = 0;
213 1.2.4.2 rmind
214 1.2.4.2 rmind out1: spin_unlock(&master->lock.spinlock);
215 1.2.4.2 rmind out0: mutex_lock(&drm_global_mutex);
216 1.2.4.2 rmind return error;
217 1.2.4.2 rmind }
218 1.2.4.2 rmind
219 1.2.4.2 rmind /*
220 1.2.4.2 rmind * Drop the lock.
221 1.2.4.2 rmind *
222 1.2.4.2 rmind * Return value is an artefact of Linux. Caller must guarantee
223 1.2.4.2 rmind * preconditions; failure is fatal.
224 1.2.4.2 rmind *
225 1.2.4.2 rmind * XXX Should we also unblock signals like drm_unlock does?
226 1.2.4.2 rmind */
227 1.2.4.2 rmind int
228 1.2.4.2 rmind drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
229 1.2.4.2 rmind {
230 1.2.4.2 rmind
231 1.2.4.2 rmind spin_lock(&lock_data->spinlock);
232 1.2.4.2 rmind drm_lock_release(lock_data, context);
233 1.2.4.2 rmind spin_unlock(&lock_data->spinlock);
234 1.2.4.2 rmind
235 1.2.4.2 rmind return 0;
236 1.2.4.2 rmind }
237 1.2.4.2 rmind
238 1.2.4.2 rmind /*
239 1.2.4.2 rmind * Take the lock for the kernel's use.
240 1.2.4.2 rmind *
241 1.2.4.2 rmind * XXX This is unimplemented because it's not clear that the Linux code
242 1.2.4.2 rmind * makes sense at all. Linux's drm_idlelock_take never blocks, but it
243 1.2.4.2 rmind * doesn't guarantee that the kernel holds the lock on return! For
244 1.2.4.2 rmind * now, I'll hope that the code paths relying on this don't matter yet.
245 1.2.4.2 rmind */
246 1.2.4.2 rmind void
247 1.2.4.2 rmind drm_idlelock_take(struct drm_lock_data *lock_data __unused)
248 1.2.4.2 rmind {
249 1.2.4.2 rmind KASSERT(mutex_is_locked(&drm_global_mutex));
250 1.2.4.2 rmind panic("drm_idlelock_take is not yet implemented"); /* XXX */
251 1.2.4.2 rmind }
252 1.2.4.2 rmind
253 1.2.4.2 rmind /*
254 1.2.4.2 rmind * Release the lock from the kernel.
255 1.2.4.2 rmind */
256 1.2.4.2 rmind void
257 1.2.4.2 rmind drm_idlelock_release(struct drm_lock_data *lock_data __unused)
258 1.2.4.2 rmind {
259 1.2.4.2 rmind KASSERT(mutex_is_locked(&drm_global_mutex));
260 1.2.4.2 rmind panic("drm_idlelock_release is not yet implemented"); /* XXX */
261 1.2.4.2 rmind }
262 1.2.4.2 rmind
263 1.2.4.2 rmind /*
264 1.2.4.2 rmind * Does this file hold this drm device's hardware lock?
265 1.2.4.2 rmind *
266 1.2.4.2 rmind * Used to decide whether to release the lock when the file is being
267 1.2.4.2 rmind * closed.
268 1.2.4.2 rmind *
269 1.2.4.2 rmind * XXX I don't think this answers correctly in the case that the
270 1.2.4.2 rmind * userland has taken the lock and it is uncontended. But I don't
271 1.2.4.2 rmind * think we can know what the correct answer is in that case.
272 1.2.4.2 rmind */
273 1.2.4.2 rmind int
274 1.2.4.2 rmind drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file)
275 1.2.4.2 rmind {
276 1.2.4.2 rmind struct drm_lock_data *const lock_data = &file->master->lock;
277 1.2.4.2 rmind int answer = 0;
278 1.2.4.2 rmind
279 1.2.4.2 rmind /* If this file has never locked anything, then no. */
280 1.2.4.2 rmind if (file->lock_count == 0)
281 1.2.4.2 rmind return 0;
282 1.2.4.2 rmind
283 1.2.4.2 rmind spin_lock(&lock_data->spinlock);
284 1.2.4.2 rmind
285 1.2.4.2 rmind /* If there is no lock, then this file doesn't hold it. */
286 1.2.4.2 rmind if (lock_data->hw_lock == NULL)
287 1.2.4.2 rmind goto out;
288 1.2.4.2 rmind
289 1.2.4.2 rmind /* If this lock is not held, then this file doesn't hold it. */
290 1.2.4.2 rmind if (!_DRM_LOCK_IS_HELD(lock_data->hw_lock->lock))
291 1.2.4.2 rmind goto out;
292 1.2.4.2 rmind
293 1.2.4.2 rmind /*
294 1.2.4.2 rmind * Otherwise, it boils down to whether this file is the owner
295 1.2.4.2 rmind * or someone else.
296 1.2.4.2 rmind *
297 1.2.4.2 rmind * XXX This is not reliable! Userland doesn't update this when
298 1.2.4.2 rmind * it takes the lock...
299 1.2.4.2 rmind */
300 1.2.4.2 rmind answer = (file == lock_data->file_priv);
301 1.2.4.2 rmind
302 1.2.4.2 rmind out: spin_unlock(&lock_data->spinlock);
303 1.2.4.2 rmind return answer;
304 1.2.4.2 rmind }
305 1.2.4.2 rmind
306 1.2.4.2 rmind /*
307 1.2.4.2 rmind * Try to acquire the lock. Return true if successful, false if not.
308 1.2.4.2 rmind *
309 1.2.4.2 rmind * This is hairy because it races with userland, and if userland
310 1.2.4.2 rmind * already holds the lock, we must tell it, by marking it
311 1.2.4.2 rmind * _DRM_LOCK_CONT (contended), that it must call ioctl(DRM_UNLOCK) to
312 1.2.4.2 rmind * release the lock so that we can wake waiters.
313 1.2.4.2 rmind *
314 1.2.4.2 rmind * XXX What happens if the process is interrupted?
315 1.2.4.2 rmind */
316 1.2.4.2 rmind static bool
317 1.2.4.2 rmind drm_lock_acquire(struct drm_lock_data *lock_data, int context)
318 1.2.4.2 rmind {
319 1.2.4.2 rmind volatile unsigned int *const lock = &lock_data->hw_lock->lock;
320 1.2.4.2 rmind unsigned int old, new;
321 1.2.4.2 rmind
322 1.2.4.2 rmind KASSERT(spin_is_locked(&lock_data->spinlock));
323 1.2.4.2 rmind
324 1.2.4.2 rmind do {
325 1.2.4.2 rmind old = *lock;
326 1.2.4.2 rmind if (!_DRM_LOCK_IS_HELD(old)) {
327 1.2.4.2 rmind new = (context | _DRM_LOCK_HELD);
328 1.2.4.2 rmind if ((0 < lock_data->user_waiters) ||
329 1.2.4.2 rmind (0 < lock_data->kernel_waiters))
330 1.2.4.2 rmind new |= _DRM_LOCK_CONT;
331 1.2.4.2 rmind } else if (_DRM_LOCKING_CONTEXT(old) != context) {
332 1.2.4.2 rmind new = (old | _DRM_LOCK_CONT);
333 1.2.4.2 rmind } else {
334 1.2.4.2 rmind DRM_ERROR("%d already holds heavyweight lock\n",
335 1.2.4.2 rmind context);
336 1.2.4.2 rmind return false;
337 1.2.4.2 rmind }
338 1.2.4.2 rmind } while (atomic_cas_uint(lock, old, new) != old);
339 1.2.4.2 rmind
340 1.2.4.2 rmind return !_DRM_LOCK_IS_HELD(old);
341 1.2.4.2 rmind }
342 1.2.4.2 rmind
343 1.2.4.2 rmind /*
344 1.2.4.2 rmind * Release the lock held in the given context. Wake any waiters,
345 1.2.4.2 rmind * preferring kernel waiters over userland waiters.
346 1.2.4.2 rmind *
347 1.2.4.2 rmind * Lock's spinlock must be held and lock must be held in this context.
348 1.2.4.2 rmind */
349 1.2.4.2 rmind static void
350 1.2.4.2 rmind drm_lock_release(struct drm_lock_data *lock_data, int context)
351 1.2.4.2 rmind {
352 1.2.4.2 rmind
353 1.2.4.2 rmind (void)context; /* ignore */
354 1.2.4.2 rmind KASSERT(spin_is_locked(&lock_data->spinlock));
355 1.2.4.2 rmind KASSERT(_DRM_LOCK_IS_HELD(lock_data->hw_lock->lock));
356 1.2.4.2 rmind KASSERT(_DRM_LOCKING_CONTEXT(lock_data->hw_lock->lock) == context);
357 1.2.4.2 rmind
358 1.2.4.2 rmind lock_data->hw_lock->lock = 0;
359 1.2.4.2 rmind DRM_SPIN_WAKEUP_ONE(&lock_data->lock_queue, &lock_data->spinlock);
360 1.2.4.2 rmind }
361 1.2.4.2 rmind
362 1.2.4.2 rmind /*
363 1.2.4.2 rmind * Block signals for a process that holds a drm lock.
364 1.2.4.2 rmind *
365 1.2.4.2 rmind * XXX It's not processes but files that hold drm locks, so blocking
366 1.2.4.2 rmind * signals in a process seems wrong, and it's not clear that blocking
367 1.2.4.2 rmind * signals automatically is remotely sensible anyway.
368 1.2.4.2 rmind */
369 1.2.4.2 rmind static int
370 1.2.4.2 rmind drm_lock_block_signals(struct drm_device *dev __unused,
371 1.2.4.2 rmind struct drm_lock *lock_request __unused, struct drm_file *file __unused)
372 1.2.4.2 rmind {
373 1.2.4.2 rmind return 0;
374 1.2.4.2 rmind }
375 1.2.4.2 rmind
376 1.2.4.2 rmind /*
377 1.2.4.2 rmind * Unblock the signals that drm_lock_block_signals blocked.
378 1.2.4.2 rmind */
379 1.2.4.2 rmind static void
380 1.2.4.2 rmind drm_lock_unblock_signals(struct drm_device *dev __unused,
381 1.2.4.2 rmind struct drm_lock *lock_request __unused, struct drm_file *file __unused)
382 1.2.4.2 rmind {
383 1.2.4.2 rmind }
384