Home | History | Annotate | Line # | Download | only in drm
drm_wait_netbsd.h revision 1.5.2.2
      1 /*	$NetBSD: drm_wait_netbsd.h,v 1.5.2.2 2015/06/06 14:40:19 skrll 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  * DRM_SPIN_WAIT_ON is a replacement for the legacy DRM_WAIT_ON
    109  * portability macro.  It requires a spin interlock, which may require
    110  * changes to the surrounding code so that the waits actually are
    111  * interlocked by a spin lock.  It also polls the condition at every
    112  * tick, which masks missing wakeups.  Since DRM_WAIT_ON is going away,
    113  * in favour of Linux's native wait_event* API, waits in new code
    114  * should be written to use the DRM_*WAIT*_UNTIL macros below.
    115  *
    116  * Like the legacy DRM_WAIT_ON, DRM_SPIN_WAIT_ON returns
    117  *
    118  * . -EBUSY if timed out (yes, -EBUSY, not -ETIMEDOUT or -EWOULDBLOCK),
    119  * . -EINTR/-ERESTART if interrupted by a signal, or
    120  * . 0 if the condition was true before or just after the timeout.
    121  *
    122  * Note that cv_timedwait* return -EWOULDBLOCK, not -EBUSY, on timeout.
    123  */
    124 
    125 #define	DRM_SPIN_WAIT_ON(RET, Q, INTERLOCK, TICKS, CONDITION)	do	      \
    126 {									      \
    127 	unsigned _dswo_ticks = (TICKS);					      \
    128 	unsigned _dswo_start, _dswo_end;				      \
    129 									      \
    130 	KASSERT(spin_is_locked((INTERLOCK)));				      \
    131 	KASSERT(!cpu_intr_p());						      \
    132 	KASSERT(!cpu_softintr_p());					      \
    133 	KASSERT(!cold);							      \
    134 									      \
    135 	for (;;) {							      \
    136 		if (CONDITION) {					      \
    137 			(RET) = 0;					      \
    138 			break;						      \
    139 		}							      \
    140 		if (_dswo_ticks == 0) {					      \
    141 			(RET) = -EBUSY;		/* Match Linux...  */	      \
    142 			break;						      \
    143 		}							      \
    144 		_dswo_start = hardclock_ticks;				      \
    145 		/* XXX errno NetBSD->Linux */				      \
    146 		(RET) = -cv_timedwait_sig((Q), &(INTERLOCK)->sl_lock, 1);     \
    147 		_dswo_end = hardclock_ticks;				      \
    148 		if (_dswo_end - _dswo_start < _dswo_ticks)		      \
    149 			_dswo_ticks -= _dswo_end - _dswo_start;		      \
    150 		else							      \
    151 			_dswo_ticks = 0;				      \
    152 		if (RET) {						      \
    153 			if ((RET) == -EWOULDBLOCK)			      \
    154 				/* Waited only one tick.  */		      \
    155 				continue;				      \
    156 			break;						      \
    157 		}							      \
    158 	}								      \
    159 } while (0)
    160 
    161 /*
    162  * The DRM_*WAIT*_UNTIL macros are replacements for the Linux
    163  * wait_event* macros.  Like DRM_SPIN_WAIT_ON, they add an interlock,
    164  * and so may require some changes to the surrounding code.  They have
    165  * a different return value convention from DRM_SPIN_WAIT_ON and a
    166  * different return value convention from cv_*wait*.
    167  *
    168  * The untimed DRM_*WAIT*_UNTIL macros return
    169  *
    170  * . -EINTR/-ERESTART if interrupted by a signal, or
    171  * . zero if the condition evaluated
    172  *
    173  * The timed DRM_*TIMED_WAIT*_UNTIL macros return
    174  *
    175  * . -EINTR/-ERESTART if interrupted by a signal,
    176  * . 0 if the condition was false after the timeout,
    177  * . 1 if the condition was true just after the timeout, or
    178  * . the number of ticks remaining if the condition was true before the
    179  * timeout.
    180  *
    181  * Contrast DRM_SPIN_WAIT_ON which returns -EINTR/-ERESTART on signal,
    182  * -EBUSY on timeout, and zero on success; and cv_*wait*, which return
    183  * -EINTR/-ERESTART on signal, -EWOULDBLOCK on timeout, and zero on
    184  * success.
    185  *
    186  * XXX In retrospect, giving the timed and untimed macros a different
    187  * return convention from one another to match Linux may have been a
    188  * bad idea.  All of this inconsistent timeout return convention logic
    189  * has been a consistent source of bugs.
    190  */
    191 
    192 #define	_DRM_WAIT_UNTIL(RET, WAIT, Q, INTERLOCK, CONDITION) do		\
    193 {									\
    194 	KASSERT(mutex_is_locked((INTERLOCK)));				\
    195 	ASSERT_SLEEPABLE();						\
    196 	KASSERT(!cold);							\
    197 	for (;;) {							\
    198 		if (CONDITION) {					\
    199 			(RET) = 0;					\
    200 			break;						\
    201 		}							\
    202 		/* XXX errno NetBSD->Linux */				\
    203 		(RET) = -WAIT((Q), &(INTERLOCK)->mtx_lock);		\
    204 		if (RET)						\
    205 			break;						\
    206 	}								\
    207 } while (0)
    208 
    209 #define	cv_wait_nointr(Q, I)	(cv_wait((Q), (I)), 0)
    210 
    211 #define	DRM_WAIT_NOINTR_UNTIL(RET, Q, I, C)				\
    212 	_DRM_WAIT_UNTIL(RET, cv_wait_nointr, Q, I, C)
    213 
    214 #define	DRM_WAIT_UNTIL(RET, Q, I, C)				\
    215 	_DRM_WAIT_UNTIL(RET, cv_wait_sig, Q, I, C)
    216 
    217 #define	_DRM_TIMED_WAIT_UNTIL(RET, WAIT, Q, INTERLOCK, TICKS, CONDITION) do \
    218 {									\
    219 	unsigned _dtwu_ticks = (TICKS);					\
    220 	unsigned _dtwu_start, _dtwu_end;				\
    221 									\
    222 	KASSERT(mutex_is_locked((INTERLOCK)));				\
    223 	ASSERT_SLEEPABLE();						\
    224 	KASSERT(!cold);							\
    225 									\
    226 	for (;;) {							\
    227 		if (CONDITION) {					\
    228 			(RET) = MAX(_dtwu_ticks, 1);			\
    229 			break;						\
    230 		}							\
    231 		if (_dtwu_ticks == 0) {					\
    232 			(RET) = 0;					\
    233 			break;						\
    234 		}							\
    235 		_dtwu_start = hardclock_ticks;				\
    236 		/* XXX errno NetBSD->Linux */				\
    237 		(RET) = -WAIT((Q), &(INTERLOCK)->mtx_lock,		\
    238 		    MIN(_dtwu_ticks, INT_MAX/2));			\
    239 		_dtwu_end = hardclock_ticks;				\
    240 		if ((_dtwu_end - _dtwu_start) < _dtwu_ticks)		\
    241 			_dtwu_ticks -= _dtwu_end - _dtwu_start;		\
    242 		else							\
    243 			_dtwu_ticks = 0;				\
    244 		if (RET) {						\
    245 			if ((RET) == -EWOULDBLOCK)			\
    246 				(RET) = (CONDITION) ? 1 : 0;		\
    247 			break;						\
    248 		}							\
    249 	}								\
    250 } while (0)
    251 
    252 #define	DRM_TIMED_WAIT_NOINTR_UNTIL(RET, Q, I, T, C)			\
    253 	_DRM_TIMED_WAIT_UNTIL(RET, cv_timedwait, Q, I, T, C)
    254 
    255 #define	DRM_TIMED_WAIT_UNTIL(RET, Q, I, T, C)			\
    256 	_DRM_TIMED_WAIT_UNTIL(RET, cv_timedwait_sig, Q, I, T, C)
    257 
    258 /*
    259  * XXX Can't assert sleepable here because we hold a spin lock.  At
    260  * least we can assert that we're not in (soft) interrupt context, and
    261  * hope that nobody tries to use these with a sometimes quickly
    262  * satisfied condition while holding a different spin lock.
    263  */
    264 
    265 #define	_DRM_SPIN_WAIT_UNTIL(RET, WAIT, Q, INTERLOCK, CONDITION) do	\
    266 {									\
    267 	KASSERT(spin_is_locked((INTERLOCK)));				\
    268 	KASSERT(!cpu_intr_p());						\
    269 	KASSERT(!cpu_softintr_p());					\
    270 	KASSERT(!cold);							\
    271 	(RET) = 0;							\
    272 	while (!(CONDITION)) {						\
    273 		/* XXX errno NetBSD->Linux */				\
    274 		(RET) = -WAIT((Q), &(INTERLOCK)->sl_lock);		\
    275 		if (RET)						\
    276 			break;						\
    277 	}								\
    278 } while (0)
    279 
    280 #define	DRM_SPIN_WAIT_NOINTR_UNTIL(RET, Q, I, C)			\
    281 	_DRM_SPIN_WAIT_UNTIL(RET, cv_wait_nointr, Q, I, C)
    282 
    283 #define	DRM_SPIN_WAIT_UNTIL(RET, Q, I, C)				\
    284 	_DRM_SPIN_WAIT_UNTIL(RET, cv_wait_sig, Q, I, C)
    285 
    286 #define	_DRM_SPIN_TIMED_WAIT_UNTIL(RET, WAIT, Q, INTERLOCK, TICKS, CONDITION) \
    287 	do								\
    288 {									\
    289 	unsigned _dstwu_ticks = (TICKS);				\
    290 	unsigned _dstwu_start, _dstwu_end;				\
    291 									\
    292 	KASSERT(spin_is_locked((INTERLOCK)));				\
    293 	KASSERT(!cpu_intr_p());						\
    294 	KASSERT(!cpu_softintr_p());					\
    295 	KASSERT(!cold);							\
    296 									\
    297 	for (;;) {							\
    298 		if (CONDITION) {					\
    299 			(RET) = MAX(_dstwu_ticks, 1);			\
    300 			break;						\
    301 		}							\
    302 		if (_dstwu_ticks == 0) {				\
    303 			(RET) = 0;					\
    304 			break;						\
    305 		}							\
    306 		_dstwu_start = hardclock_ticks;				\
    307 		/* XXX errno NetBSD->Linux */				\
    308 		(RET) = -WAIT((Q), &(INTERLOCK)->sl_lock,		\
    309 		    MIN(_dstwu_ticks, INT_MAX/2));			\
    310 		_dstwu_end = hardclock_ticks;				\
    311 		if ((_dstwu_end - _dstwu_start) < _dstwu_ticks)		\
    312 			_dstwu_ticks -= _dstwu_end - _dstwu_start;	\
    313 		else							\
    314 			_dstwu_ticks = 0;				\
    315 		if (RET) {						\
    316 			if ((RET) == -EWOULDBLOCK)			\
    317 				(RET) = (CONDITION) ? 1 : 0;		\
    318 			break;						\
    319 		}							\
    320 	}								\
    321 } while (0)
    322 
    323 #define	DRM_SPIN_TIMED_WAIT_NOINTR_UNTIL(RET, Q, I, T, C)		\
    324 	_DRM_SPIN_TIMED_WAIT_UNTIL(RET, cv_timedwait, Q, I, T, C)
    325 
    326 #define	DRM_SPIN_TIMED_WAIT_UNTIL(RET, Q, I, T, C)			\
    327 	_DRM_SPIN_TIMED_WAIT_UNTIL(RET, cv_timedwait_sig, Q, I, T, C)
    328 
    329 #endif  /* _DRM_DRM_WAIT_NETBSD_H_ */
    330