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