Home | History | Annotate | Line # | Download | only in drm
drm_wait_netbsd.h revision 1.7
      1 /*	$NetBSD: drm_wait_netbsd.h,v 1.7 2015/02/28 03:22:50 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _DRM_DRM_WAIT_NETBSD_H_
     33 #define _DRM_DRM_WAIT_NETBSD_H_
     34 
     35 #include <sys/param.h>
     36 #include <sys/condvar.h>
     37 #if DIAGNOSTIC
     38 #include <sys/cpu.h>		/* cpu_intr_p */
     39 #endif
     40 #include <sys/kernel.h>
     41 #include <sys/mutex.h>
     42 #include <sys/systm.h>
     43 
     44 #include <linux/mutex.h>
     45 #include <linux/spinlock.h>
     46 
     47 typedef kcondvar_t drm_waitqueue_t;
     48 
     49 #define	DRM_HZ	hz		/* XXX Hurk...  */
     50 
     51 #define	DRM_UDELAY	DELAY
     52 
     53 static inline void
     54 DRM_INIT_WAITQUEUE(drm_waitqueue_t *q, const char *name)
     55 {
     56 	cv_init(q, name);
     57 }
     58 
     59 static inline void
     60 DRM_DESTROY_WAITQUEUE(drm_waitqueue_t *q)
     61 {
     62 	cv_destroy(q);
     63 }
     64 
     65 static inline bool
     66 DRM_WAITERS_P(drm_waitqueue_t *q, struct mutex *interlock)
     67 {
     68 	KASSERT(mutex_is_locked(interlock));
     69 	return cv_has_waiters(q);
     70 }
     71 
     72 static inline void
     73 DRM_WAKEUP_ONE(drm_waitqueue_t *q, struct mutex *interlock)
     74 {
     75 	KASSERT(mutex_is_locked(interlock));
     76 	cv_signal(q);
     77 }
     78 
     79 static inline void
     80 DRM_WAKEUP_ALL(drm_waitqueue_t *q, struct mutex *interlock)
     81 {
     82 	KASSERT(mutex_is_locked(interlock));
     83 	cv_broadcast(q);
     84 }
     85 
     86 static inline bool
     87 DRM_SPIN_WAITERS_P(drm_waitqueue_t *q, spinlock_t *interlock)
     88 {
     89 	KASSERT(spin_is_locked(interlock));
     90 	return cv_has_waiters(q);
     91 }
     92 
     93 static inline void
     94 DRM_SPIN_WAKEUP_ONE(drm_waitqueue_t *q, spinlock_t *interlock)
     95 {
     96 	KASSERT(spin_is_locked(interlock));
     97 	cv_signal(q);
     98 }
     99 
    100 static inline void
    101 DRM_SPIN_WAKEUP_ALL(drm_waitqueue_t *q, spinlock_t *interlock)
    102 {
    103 	KASSERT(spin_is_locked(interlock));
    104 	cv_broadcast(q);
    105 }
    106 
    107 /*
    108  * WARNING: These DRM_*WAIT*_UNTIL macros are designed to replace the
    109  * Linux wait_event_* macros.  They have a different return value
    110  * convention from the legacy portability DRM_WAIT_ON macro and a
    111  * different return value convention from cv_*wait*.  Specifically,
    112  * DRM_*WAIT*_UNTIL and Linux wait_event_*
    113  *
    114  * - return negative error code on failure (e.g., interruption),
    115  * - return zero on timeout, and
    116  * - return one on success.
    117  *
    118  * Contrast DRM_WAIT_ON which returns -EINTR/-ERESTART on interruption,
    119  * -EBUSY on timeout, and zero on success; and cv_*wait*, which return
    120  * -EINTR/-ERESTART on interruption, -EWOULDBLOCK on timeout, and zero
    121  * on success.
    122  *
    123  * We don't simply implement DRM_WAIT_ON because, like Linux
    124  * wait_event_*, it lacks an interlock, whereas we require an interlock
    125  * for any waits in order to avoid the standard race conditions
    126  * associated with non-interlocked waits that plague Linux drivers.
    127  */
    128 
    129 #define	_DRM_WAIT_UNTIL(RET, WAIT, Q, INTERLOCK, CONDITION) do		\
    130 {									\
    131 	KASSERT(mutex_is_locked((INTERLOCK)));				\
    132 	ASSERT_SLEEPABLE();						\
    133 	KASSERT(!cold);							\
    134 	for (;;) {							\
    135 		if (CONDITION) {					\
    136 			(RET) = 0;					\
    137 			break;						\
    138 		}							\
    139 		/* XXX errno NetBSD->Linux */				\
    140 		(RET) = -WAIT((Q), &(INTERLOCK)->mtx_lock);		\
    141 		if (RET)						\
    142 			break;						\
    143 	}								\
    144 } while (0)
    145 
    146 #define	cv_wait_nointr(Q, I)	(cv_wait((Q), (I)), 0)
    147 
    148 #define	DRM_WAIT_NOINTR_UNTIL(RET, Q, I, C)				\
    149 	_DRM_WAIT_UNTIL(RET, cv_wait_nointr, Q, I, C)
    150 
    151 #define	DRM_WAIT_UNTIL(RET, Q, I, C)				\
    152 	_DRM_WAIT_UNTIL(RET, cv_wait_sig, Q, I, C)
    153 
    154 /*
    155  * Timed wait.  Return:
    156  *
    157  * - 0 if condition is false after timeout,
    158  * - 1 if condition is true after timeout or one tick before timeout,
    159  * - number of ticks left if condition evaluated to true before timeout, or
    160  * - negative error if failure (e.g., interrupted).
    161  *
    162  * XXX Comments in Linux say it returns -ERESTARTSYS if interrupted.
    163  * What if by a signal without SA_RESTART?  Shouldn't it be -EINTR
    164  * then?  I'm going to leave it as what cv_timedwait returned, which is
    165  * ERESTART for signals with SA_RESTART and EINTR otherwise.
    166  */
    167 #define	_DRM_TIMED_WAIT_UNTIL(RET, WAIT, Q, INTERLOCK, TICKS, CONDITION) do \
    168 {									\
    169 	extern int hardclock_ticks;					\
    170 	const int _dtwu_start = hardclock_ticks;			\
    171 	int _dtwu_ticks = (TICKS);					\
    172 	KASSERT(mutex_is_locked((INTERLOCK)));				\
    173 	ASSERT_SLEEPABLE();						\
    174 	KASSERT(!cold);							\
    175 	for (;;) {							\
    176 		if (CONDITION) {					\
    177 			(RET) = _dtwu_ticks;				\
    178 			break;						\
    179 		}							\
    180 		/* XXX errno NetBSD->Linux */				\
    181 		(RET) = -WAIT((Q), &(INTERLOCK)->mtx_lock,		\
    182 		    _dtwu_ticks);					\
    183 		if (RET) {						\
    184 			if ((RET) == -EWOULDBLOCK)			\
    185 				(RET) = (CONDITION) ? 1 : 0;		\
    186 			break;						\
    187 		}							\
    188 		const int _dtwu_now = hardclock_ticks;			\
    189 		KASSERT(_dtwu_start <= _dtwu_now);			\
    190 		if ((_dtwu_now - _dtwu_start) < _dtwu_ticks) {		\
    191 			_dtwu_ticks -= (_dtwu_now - _dtwu_start);	\
    192 		} else {						\
    193 			(RET) = (CONDITION) ? 1 : 0;			\
    194 			break;						\
    195 		}							\
    196 	}								\
    197 } while (0)
    198 
    199 #define	DRM_TIMED_WAIT_NOINTR_UNTIL(RET, Q, I, T, C)			\
    200 	_DRM_TIMED_WAIT_UNTIL(RET, cv_timedwait, Q, I, T, C)
    201 
    202 #define	DRM_TIMED_WAIT_UNTIL(RET, Q, I, T, C)			\
    203 	_DRM_TIMED_WAIT_UNTIL(RET, cv_timedwait_sig, Q, I, T, C)
    204 
    205 /*
    206  * XXX Can't assert sleepable here because we hold a spin lock.  At
    207  * least we can assert that we're not in (soft) interrupt context, and
    208  * hope that nobody tries to use these with a sometimes quickly
    209  * satisfied condition while holding a different spin lock.
    210  */
    211 
    212 #define	_DRM_SPIN_WAIT_UNTIL(RET, WAIT, Q, INTERLOCK, CONDITION) do	\
    213 {									\
    214 	KASSERT(spin_is_locked((INTERLOCK)));				\
    215 	KASSERT(!cpu_intr_p());						\
    216 	KASSERT(!cpu_softintr_p());					\
    217 	KASSERT(!cold);							\
    218 	(RET) = 0;							\
    219 	while (!(CONDITION)) {						\
    220 		/* XXX errno NetBSD->Linux */				\
    221 		(RET) = -WAIT((Q), &(INTERLOCK)->sl_lock);		\
    222 		if (RET)						\
    223 			break;						\
    224 	}								\
    225 } while (0)
    226 
    227 #define	DRM_SPIN_WAIT_NOINTR_UNTIL(RET, Q, I, C)			\
    228 	_DRM_SPIN_WAIT_UNTIL(RET, cv_wait_nointr, Q, I, C)
    229 
    230 #define	DRM_SPIN_WAIT_UNTIL(RET, Q, I, C)				\
    231 	_DRM_SPIN_WAIT_UNTIL(RET, cv_wait_sig, Q, I, C)
    232 
    233 #define	_DRM_SPIN_TIMED_WAIT_UNTIL(RET, WAIT, Q, INTERLOCK, TICKS, CONDITION) \
    234 	do								\
    235 {									\
    236 	extern int hardclock_ticks;					\
    237 	const int _dstwu_start = hardclock_ticks;			\
    238 	int _dstwu_ticks = (TICKS);					\
    239 	KASSERT(spin_is_locked((INTERLOCK)));				\
    240 	KASSERT(!cpu_intr_p());						\
    241 	KASSERT(!cpu_softintr_p());					\
    242 	KASSERT(!cold);							\
    243 	for (;;) {							\
    244 		if (CONDITION) {					\
    245 			(RET) = _dstwu_ticks;				\
    246 			break;						\
    247 		}							\
    248 		/* XXX errno NetBSD->Linux */				\
    249 		(RET) = -WAIT((Q), &(INTERLOCK)->sl_lock,		\
    250 		    _dstwu_ticks);					\
    251 		if (RET) {						\
    252 			if ((RET) == -EWOULDBLOCK)			\
    253 				(RET) = (CONDITION) ? 1 : 0;		\
    254 			break;						\
    255 		}							\
    256 		const int _dstwu_now = hardclock_ticks;			\
    257 		KASSERT(_dstwu_start <= _dstwu_now);			\
    258 		if ((_dstwu_now - _dstwu_start) < _dstwu_ticks) {	\
    259 			_dstwu_ticks -= (_dstwu_now - _dstwu_start);	\
    260 		} else {						\
    261 			(RET) = (CONDITION) ? 1 : 0;			\
    262 			break;						\
    263 		}							\
    264 	}								\
    265 } while (0)
    266 
    267 #define	DRM_SPIN_TIMED_WAIT_NOINTR_UNTIL(RET, Q, I, T, C)		\
    268 	_DRM_SPIN_TIMED_WAIT_UNTIL(RET, cv_timedwait, Q, I, T, C)
    269 
    270 #define	DRM_SPIN_TIMED_WAIT_UNTIL(RET, Q, I, T, C)			\
    271 	_DRM_SPIN_TIMED_WAIT_UNTIL(RET, cv_timedwait_sig, Q, I, T, C)
    272 
    273 #endif  /* _DRM_DRM_WAIT_NETBSD_H_ */
    274