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