Home | History | Annotate | Line # | Download | only in drm
drm_wait_netbsd.h revision 1.14.18.1
      1 /*	$NetBSD: drm_wait_netbsd.h,v 1.14.18.1 2019/06/10 22:08:31 christos 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 #include <sys/cpu.h>		/* cpu_intr_p */
     38 #include <sys/kernel.h>
     39 #include <sys/mutex.h>
     40 #include <sys/systm.h>
     41 
     42 #include <linux/mutex.h>
     43 #include <linux/spinlock.h>
     44 
     45 typedef kcondvar_t drm_waitqueue_t;
     46 
     47 #define	DRM_HZ	hz		/* XXX Hurk...  */
     48 
     49 #define	DRM_UDELAY	DELAY
     50 
     51 static inline void
     52 DRM_INIT_WAITQUEUE(drm_waitqueue_t *q, const char *name)
     53 {
     54 	cv_init(q, name);
     55 }
     56 
     57 static inline void
     58 DRM_DESTROY_WAITQUEUE(drm_waitqueue_t *q)
     59 {
     60 	cv_destroy(q);
     61 }
     62 
     63 static inline bool
     64 DRM_WAITERS_P(drm_waitqueue_t *q, struct mutex *interlock)
     65 {
     66 	KASSERT(mutex_is_locked(interlock));
     67 	return cv_has_waiters(q);
     68 }
     69 
     70 static inline void
     71 DRM_WAKEUP_ONE(drm_waitqueue_t *q, struct mutex *interlock)
     72 {
     73 	KASSERT(mutex_is_locked(interlock));
     74 	cv_signal(q);
     75 }
     76 
     77 static inline void
     78 DRM_WAKEUP_ALL(drm_waitqueue_t *q, struct mutex *interlock)
     79 {
     80 	KASSERT(mutex_is_locked(interlock));
     81 	cv_broadcast(q);
     82 }
     83 
     84 static inline bool
     85 DRM_SPIN_WAITERS_P(drm_waitqueue_t *q, spinlock_t *interlock)
     86 {
     87 	KASSERT(spin_is_locked(interlock));
     88 	return cv_has_waiters(q);
     89 }
     90 
     91 static inline void
     92 DRM_SPIN_WAKEUP_ONE(drm_waitqueue_t *q, spinlock_t *interlock)
     93 {
     94 	KASSERT(spin_is_locked(interlock));
     95 	cv_signal(q);
     96 }
     97 
     98 static inline void
     99 DRM_SPIN_WAKEUP_ALL(drm_waitqueue_t *q, spinlock_t *interlock)
    100 {
    101 	KASSERT(spin_is_locked(interlock));
    102 	cv_broadcast(q);
    103 }
    104 
    105 /*
    106  * DRM_SPIN_WAIT_ON is a replacement for the legacy DRM_WAIT_ON
    107  * portability macro.  It requires a spin interlock, which may require
    108  * changes to the surrounding code so that the waits actually are
    109  * interlocked by a spin lock.  It also polls the condition at every
    110  * tick, which masks missing wakeups.  Since DRM_WAIT_ON is going away,
    111  * in favour of Linux's native wait_event* API, waits in new code
    112  * should be written to use the DRM_*WAIT*_UNTIL macros below.
    113  *
    114  * Like the legacy DRM_WAIT_ON, DRM_SPIN_WAIT_ON returns
    115  *
    116  * . -EBUSY if timed out (yes, -EBUSY, not -ETIMEDOUT or -EWOULDBLOCK),
    117  * . -EINTR/-ERESTARTSYS if interrupted by a signal, or
    118  * . 0 if the condition was true before or just after the timeout.
    119  *
    120  * Note that cv_timedwait* return -EWOULDBLOCK, not -EBUSY, on timeout.
    121  *
    122  * Note that ERESTARTSYS is actually ELAST+1 and only used in Linux
    123  * code and must be converted for use in NetBSD code (user or kernel.)
    124  */
    125 
    126 #define	DRM_SPIN_WAIT_ON(RET, Q, INTERLOCK, TICKS, CONDITION)	do	      \
    127 {									      \
    128 	unsigned _dswo_ticks = (TICKS);					      \
    129 	unsigned _dswo_start, _dswo_end;				      \
    130 									      \
    131 	KASSERT(spin_is_locked((INTERLOCK)));				      \
    132 	KASSERT(!cpu_intr_p());						      \
    133 	KASSERT(!cpu_softintr_p());					      \
    134 	KASSERT(!cold);							      \
    135 									      \
    136 	for (;;) {							      \
    137 		if (CONDITION) {					      \
    138 			(RET) = 0;					      \
    139 			break;						      \
    140 		}							      \
    141 		if (_dswo_ticks == 0) {					      \
    142 			(RET) = -EBUSY;		/* Match Linux...  */	      \
    143 			break;						      \
    144 		}							      \
    145 		_dswo_start = hardclock_ticks;				      \
    146 		/* XXX errno NetBSD->Linux */				      \
    147 		(RET) = -cv_timedwait_sig((Q), &(INTERLOCK)->sl_lock, 1);     \
    148 		_dswo_end = hardclock_ticks;				      \
    149 		if (_dswo_end - _dswo_start < _dswo_ticks)		      \
    150 			_dswo_ticks -= _dswo_end - _dswo_start;		      \
    151 		else							      \
    152 			_dswo_ticks = 0;				      \
    153 		if (RET) {						      \
    154 			if ((RET) == -ERESTART)				      \
    155 				(RET) = -ERESTARTSYS;			      \
    156 			if ((RET) == -EWOULDBLOCK)			      \
    157 				/* Waited only one tick.  */		      \
    158 				continue;				      \
    159 			break;						      \
    160 		}							      \
    161 	}								      \
    162 } while (0)
    163 
    164 /*
    165  * The DRM_*WAIT*_UNTIL macros are replacements for the Linux
    166  * wait_event* macros.  Like DRM_SPIN_WAIT_ON, they add an interlock,
    167  * and so may require some changes to the surrounding code.  They have
    168  * a different return value convention from DRM_SPIN_WAIT_ON and a
    169  * different return value convention from cv_*wait*.
    170  *
    171  * The untimed DRM_*WAIT*_UNTIL macros return
    172  *
    173  * . -EINTR/-ERESTARTSYS if interrupted by a signal, or
    174  * . zero if the condition evaluated
    175  *
    176  * The timed DRM_*TIMED_WAIT*_UNTIL macros return
    177  *
    178  * . -EINTR/-ERESTARTSYS if interrupted by a signal,
    179  * . 0 if the condition was false after the timeout,
    180  * . 1 if the condition was true just after the timeout, or
    181  * . the number of ticks remaining if the condition was true before the
    182  * timeout.
    183  *
    184  * Contrast DRM_SPIN_WAIT_ON which returns -EINTR/-ERESTARTSYS on signal,
    185  * -EBUSY on timeout, and zero on success; and cv_*wait*, which return
    186  * -EINTR/-ERESTARTSYS on signal, -EWOULDBLOCK on timeout, and zero on
    187  * success.
    188  *
    189  * XXX In retrospect, giving the timed and untimed macros a different
    190  * return convention from one another to match Linux may have been a
    191  * bad idea.  All of this inconsistent timeout return convention logic
    192  * has been a consistent source of bugs.
    193  *
    194  * Note that ERESTARTSYS is actually ELAST+1 and only used in Linux
    195  * code and must be converted for use in NetBSD code (user or kernel.)
    196  */
    197 
    198 #define	_DRM_WAIT_UNTIL(RET, WAIT, Q, INTERLOCK, CONDITION) do		\
    199 {									\
    200 	KASSERT(mutex_is_locked((INTERLOCK)));				\
    201 	ASSERT_SLEEPABLE();						\
    202 	KASSERT(!cold);							\
    203 	for (;;) {							\
    204 		if (CONDITION) {					\
    205 			(RET) = 0;					\
    206 			break;						\
    207 		}							\
    208 		/* XXX errno NetBSD->Linux */				\
    209 		(RET) = -WAIT((Q), &(INTERLOCK)->mtx_lock);		\
    210 		if (RET) {						\
    211 			if ((RET) == -ERESTART)				\
    212 				(RET) = -ERESTARTSYS;			\
    213 			break;						\
    214 		}							\
    215 	}								\
    216 } while (0)
    217 
    218 #define	cv_wait_nointr(Q, I)	(cv_wait((Q), (I)), 0)
    219 
    220 #define	DRM_WAIT_NOINTR_UNTIL(RET, Q, I, C)				\
    221 	_DRM_WAIT_UNTIL(RET, cv_wait_nointr, Q, I, C)
    222 
    223 #define	DRM_WAIT_UNTIL(RET, Q, I, C)				\
    224 	_DRM_WAIT_UNTIL(RET, cv_wait_sig, Q, I, C)
    225 
    226 #define	_DRM_TIMED_WAIT_UNTIL(RET, WAIT, Q, INTERLOCK, TICKS, CONDITION) do \
    227 {									\
    228 	unsigned _dtwu_ticks = (TICKS);					\
    229 	unsigned _dtwu_start, _dtwu_end;				\
    230 									\
    231 	KASSERT(mutex_is_locked((INTERLOCK)));				\
    232 	ASSERT_SLEEPABLE();						\
    233 	KASSERT(!cold);							\
    234 									\
    235 	for (;;) {							\
    236 		if (CONDITION) {					\
    237 			(RET) = MAX(_dtwu_ticks, 1);			\
    238 			break;						\
    239 		}							\
    240 		if (_dtwu_ticks == 0) {					\
    241 			(RET) = 0;					\
    242 			break;						\
    243 		}							\
    244 		_dtwu_start = hardclock_ticks;				\
    245 		/* XXX errno NetBSD->Linux */				\
    246 		(RET) = -WAIT((Q), &(INTERLOCK)->mtx_lock,		\
    247 		    MIN(_dtwu_ticks, INT_MAX/2));			\
    248 		_dtwu_end = hardclock_ticks;				\
    249 		if ((_dtwu_end - _dtwu_start) < _dtwu_ticks)		\
    250 			_dtwu_ticks -= _dtwu_end - _dtwu_start;		\
    251 		else							\
    252 			_dtwu_ticks = 0;				\
    253 		if (RET) {						\
    254 			if ((RET) == -ERESTART)				\
    255 				(RET) = -ERESTARTSYS;			\
    256 			if ((RET) == -EWOULDBLOCK)			\
    257 				(RET) = (CONDITION) ? 1 : 0;		\
    258 			break;						\
    259 		}							\
    260 	}								\
    261 } while (0)
    262 
    263 #define	DRM_TIMED_WAIT_NOINTR_UNTIL(RET, Q, I, T, C)			\
    264 	_DRM_TIMED_WAIT_UNTIL(RET, cv_timedwait, Q, I, T, C)
    265 
    266 #define	DRM_TIMED_WAIT_UNTIL(RET, Q, I, T, C)			\
    267 	_DRM_TIMED_WAIT_UNTIL(RET, cv_timedwait_sig, Q, I, T, C)
    268 
    269 /*
    270  * XXX Can't assert sleepable here because we hold a spin lock.  At
    271  * least we can assert that we're not in (soft) interrupt context, and
    272  * hope that nobody tries to use these with a sometimes quickly
    273  * satisfied condition while holding a different spin lock.
    274  */
    275 
    276 #define	_DRM_SPIN_WAIT_UNTIL(RET, WAIT, Q, INTERLOCK, CONDITION) do	\
    277 {									\
    278 	KASSERT(spin_is_locked((INTERLOCK)));				\
    279 	KASSERT(!cpu_intr_p());						\
    280 	KASSERT(!cpu_softintr_p());					\
    281 	KASSERT(!cold);							\
    282 	(RET) = 0;							\
    283 	while (!(CONDITION)) {						\
    284 		/* XXX errno NetBSD->Linux */				\
    285 		(RET) = -WAIT((Q), &(INTERLOCK)->sl_lock);		\
    286 		if ((RET) == -ERESTART)					\
    287 			(RET) = -ERESTARTSYS;				\
    288 		if (RET)						\
    289 			break;						\
    290 	}								\
    291 } while (0)
    292 
    293 #define	DRM_SPIN_WAIT_NOINTR_UNTIL(RET, Q, I, C)			\
    294 	_DRM_SPIN_WAIT_UNTIL(RET, cv_wait_nointr, Q, I, C)
    295 
    296 #define	DRM_SPIN_WAIT_UNTIL(RET, Q, I, C)				\
    297 	_DRM_SPIN_WAIT_UNTIL(RET, cv_wait_sig, Q, I, C)
    298 
    299 #define	_DRM_SPIN_TIMED_WAIT_UNTIL(RET, WAIT, Q, INTERLOCK, TICKS, CONDITION) \
    300 	do								\
    301 {									\
    302 	unsigned _dstwu_ticks = (TICKS);				\
    303 	unsigned _dstwu_start, _dstwu_end;				\
    304 									\
    305 	KASSERT(spin_is_locked((INTERLOCK)));				\
    306 	KASSERT(!cpu_intr_p());						\
    307 	KASSERT(!cpu_softintr_p());					\
    308 	KASSERT(!cold);							\
    309 									\
    310 	for (;;) {							\
    311 		if (CONDITION) {					\
    312 			(RET) = MAX(_dstwu_ticks, 1);			\
    313 			break;						\
    314 		}							\
    315 		if (_dstwu_ticks == 0) {				\
    316 			(RET) = 0;					\
    317 			break;						\
    318 		}							\
    319 		_dstwu_start = hardclock_ticks;				\
    320 		/* XXX errno NetBSD->Linux */				\
    321 		(RET) = -WAIT((Q), &(INTERLOCK)->sl_lock,		\
    322 		    MIN(_dstwu_ticks, INT_MAX/2));			\
    323 		_dstwu_end = hardclock_ticks;				\
    324 		if ((_dstwu_end - _dstwu_start) < _dstwu_ticks)		\
    325 			_dstwu_ticks -= _dstwu_end - _dstwu_start;	\
    326 		else							\
    327 			_dstwu_ticks = 0;				\
    328 		if (RET) {						\
    329 			if ((RET) == -ERESTART)				\
    330 				(RET) = -ERESTARTSYS;			\
    331 			if ((RET) == -EWOULDBLOCK)			\
    332 				(RET) = (CONDITION) ? 1 : 0;		\
    333 			break;						\
    334 		}							\
    335 	}								\
    336 } while (0)
    337 
    338 #define	DRM_SPIN_TIMED_WAIT_NOINTR_UNTIL(RET, Q, I, T, C)		\
    339 	_DRM_SPIN_TIMED_WAIT_UNTIL(RET, cv_timedwait, Q, I, T, C)
    340 
    341 #define	DRM_SPIN_TIMED_WAIT_UNTIL(RET, Q, I, T, C)			\
    342 	_DRM_SPIN_TIMED_WAIT_UNTIL(RET, cv_timedwait_sig, Q, I, T, C)
    343 
    344 #endif  /* _DRM_DRM_WAIT_NETBSD_H_ */
    345