Home | History | Annotate | Line # | Download | only in include
mutex.h revision 1.6
      1 /*	$NetBSD: mutex.h,v 1.6 2023/07/12 12:50:13 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe and Andrew Doran.
      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 _RISCV_MUTEX_H_
     33 #define	_RISCV_MUTEX_H_
     34 
     35 #include <sys/types.h>
     36 
     37 #ifndef __MUTEX_PRIVATE
     38 
     39 struct kmutex {
     40 	uintptr_t	mtx_pad1;
     41 };
     42 
     43 #else	/* __MUTEX_PRIVATE */
     44 
     45 #include <sys/cdefs.h>
     46 
     47 #include <sys/param.h>
     48 
     49 #include <machine/intr.h>
     50 
     51 struct kmutex {
     52 	volatile uintptr_t	mtx_owner;
     53 };
     54 
     55 #ifdef _LP64
     56 #define MTX_ASMOP_SFX ".d"		// doubleword atomic op
     57 #else
     58 #define MTX_ASMOP_SFX ".w"		// word atomic op
     59 #endif
     60 
     61 #define	MTX_LOCK			__BIT(8)	// just one bit
     62 #define	MTX_IPL				__BITS(7,4)	// only need 4 bits
     63 
     64 #undef MUTEX_SPIN_IPL			// override <sys/mutex.h>
     65 #define	MUTEX_SPIN_IPL(a)		riscv_mutex_spin_ipl(a)
     66 #define	MUTEX_INITIALIZE_SPIN_IPL(a,b)	riscv_mutex_initialize_spin_ipl(a,b)
     67 #define MUTEX_SPINBIT_LOCK_INIT(a)	riscv_mutex_spinbit_lock_init(a)
     68 #define MUTEX_SPINBIT_LOCK_TRY(a)	riscv_mutex_spinbit_lock_try(a)
     69 #define MUTEX_SPINBIT_LOCKED_P(a)	riscv_mutex_spinbit_locked_p(a)
     70 #define MUTEX_SPINBIT_LOCK_UNLOCK(a)	riscv_mutex_spinbit_lock_unlock(a)
     71 
     72 static inline ipl_cookie_t
     73 riscv_mutex_spin_ipl(kmutex_t *__mtx)
     74 {
     75 	return (ipl_cookie_t){._spl = __SHIFTOUT(__mtx->mtx_owner, MTX_IPL)};
     76 }
     77 
     78 static inline void
     79 riscv_mutex_initialize_spin_ipl(kmutex_t *__mtx, int ipl)
     80 {
     81 	__mtx->mtx_owner = (__mtx->mtx_owner & ~MTX_IPL)
     82 	    | __SHIFTIN(ipl, MTX_IPL);
     83 }
     84 
     85 static inline void
     86 riscv_mutex_spinbit_lock_init(kmutex_t *__mtx)
     87 {
     88 	__mtx->mtx_owner &= ~MTX_LOCK;
     89 }
     90 
     91 static inline bool
     92 riscv_mutex_spinbit_locked_p(const kmutex_t *__mtx)
     93 {
     94 	return (__mtx->mtx_owner & MTX_LOCK) != 0;
     95 }
     96 
     97 static inline bool
     98 riscv_mutex_spinbit_lock_try(kmutex_t *__mtx)
     99 {
    100 	uintptr_t __old;
    101 	__asm __volatile(
    102 		"amoor" MTX_ASMOP_SFX ".aq\t%0, %1, (%2)"
    103 	   :	"=r"(__old)
    104 	   :	"r"(MTX_LOCK), "r"(__mtx));
    105 	return (__old & MTX_LOCK) == 0;
    106 }
    107 
    108 static inline void
    109 riscv_mutex_spinbit_lock_unlock(kmutex_t *__mtx)
    110 {
    111 	__asm __volatile(
    112 		"amoand" MTX_ASMOP_SFX ".rl\tx0, %0, (%1)"
    113 	   ::	"r"(~MTX_LOCK), "r"(__mtx));
    114 }
    115 
    116 #if 0
    117 #define	__HAVE_MUTEX_STUBS		1
    118 #define	__HAVE_SPIN_MUTEX_STUBS		1
    119 #endif
    120 #define	__HAVE_SIMPLE_MUTEXES		1
    121 
    122 #endif	/* __MUTEX_PRIVATE */
    123 
    124 #endif /* _RISCV_MUTEX_H_ */
    125