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