Home | History | Annotate | Line # | Download | only in include
      1 /*	$NetBSD: mutex.h,v 1.19 2023/07/13 07:46:43 mrg 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 _HPPA_MUTEX_H_
     33 #define	_HPPA_MUTEX_H_
     34 
     35 /*
     36  * The HPPA mutex implementation is troublesome, because HPPA lacks
     37  * a compare-and-set operation, yet there are many SMP HPPA machines
     38  * in circulation.  SMP for spin mutexes is easy - we don't need to
     39  * know who owns the lock.  For adaptive mutexes, we need an owner
     40  * field and additional interlock
     41  */
     42 
     43 #ifndef __ASSEMBLER__
     44 
     45 #include <sys/types.h>
     46 
     47 #ifdef __MUTEX_PRIVATE
     48 #include <machine/lock.h>
     49 #include <machine/intr.h>
     50 #endif
     51 
     52 struct kmutex {
     53 	union {
     54 		/*
     55 		 * Only the 16 bytes aligned word of __cpu_simple_lock_t will
     56 		 * be used. It's 16 bytes to simplify the allocation.
     57 		 * See hppa/lock.h
     58 		 */
     59 #ifdef __MUTEX_PRIVATE
     60 		struct {
     61 			__cpu_simple_lock_t	mtxu_lock;	/* 0-15 */
     62 			volatile uint32_t	mtxs_owner;	/* 16-19 */
     63 			ipl_cookie_t		mtxs_ipl;	/* 20-23 */
     64 			volatile uint8_t	mtxs_waiters;	/* 24 */
     65 
     66 			/* For LOCKDEBUG */
     67 			uint8_t			mtxs_dodebug;	/* 25 */
     68 		} s;
     69 #endif
     70 		uint8_t			mtxu_pad[32];	/* 0 - 32 */
     71 	} u;
     72 } __aligned (16);
     73 #endif
     74 
     75 #ifdef __MUTEX_PRIVATE
     76 
     77 #define	__HAVE_MUTEX_STUBS	1
     78 
     79 #define	mtx_lock	u.s.mtxu_lock
     80 #define	mtx_owner	u.s.mtxs_owner
     81 #define	mtx_ipl		u.s.mtxs_ipl
     82 #define	mtx_waiters	u.s.mtxs_waiters
     83 #define	mtx_dodebug	u.s.mtxs_dodebug
     84 
     85 /* Magic constants for mtx_owner */
     86 #define	MUTEX_ADAPTIVE_UNOWNED		0xffffff00
     87 #define	MUTEX_SPIN_FLAG			0xffffff10
     88 #define	MUTEX_UNOWNED_OR_SPIN(x)	(((x) & 0xffffffef) == 0xffffff00)
     89 
     90 #if !defined(__ASSEMBLER__) && defined(_KERNEL)
     91 
     92 static inline uintptr_t
     93 MUTEX_OWNER(uintptr_t owner)
     94 {
     95 	return owner;
     96 }
     97 
     98 static inline int
     99 MUTEX_OWNED(uintptr_t owner)
    100 {
    101 	return owner != MUTEX_ADAPTIVE_UNOWNED;
    102 }
    103 
    104 static inline int
    105 MUTEX_SET_WAITERS(struct kmutex *mtx, uintptr_t owner)
    106 {
    107 	__sync();		/* formerly mb_read */
    108 	mtx->mtx_waiters = 1;
    109 	__sync();		/* formerly mb_memory */
    110 	return mtx->mtx_owner != MUTEX_ADAPTIVE_UNOWNED;
    111 }
    112 
    113 static inline int
    114 MUTEX_HAS_WAITERS(const volatile struct kmutex *mtx)
    115 {
    116 	return mtx->mtx_waiters != 0;
    117 }
    118 
    119 static inline void
    120 MUTEX_INITIALIZE_SPIN(struct kmutex *mtx, bool dodebug, int ipl)
    121 {
    122 	mtx->mtx_ipl = makeiplcookie(ipl);
    123 	mtx->mtx_dodebug = dodebug;
    124 	mtx->mtx_owner = MUTEX_SPIN_FLAG;
    125 	__cpu_simple_lock_init(&mtx->mtx_lock);
    126 }
    127 
    128 static inline void
    129 MUTEX_INITIALIZE_ADAPTIVE(struct kmutex *mtx, bool dodebug)
    130 {
    131 	mtx->mtx_dodebug = dodebug;
    132 	mtx->mtx_owner = MUTEX_ADAPTIVE_UNOWNED;
    133 	__cpu_simple_lock_init(&mtx->mtx_lock);
    134 }
    135 
    136 static inline void
    137 MUTEX_DESTROY(struct kmutex *mtx)
    138 {
    139 	mtx->mtx_owner = 0xffffffff;
    140 }
    141 
    142 static inline bool
    143 MUTEX_DEBUG_P(const volatile struct kmutex *mtx)
    144 {
    145 	return mtx->mtx_dodebug != 0;
    146 }
    147 
    148 static inline int
    149 MUTEX_SPIN_P(const uintptr_t owner)
    150 {
    151 	return owner == MUTEX_SPIN_FLAG;
    152 }
    153 
    154 static inline int
    155 MUTEX_ADAPTIVE_P(const uintptr_t owner)
    156 {
    157 	return owner != MUTEX_SPIN_FLAG;
    158 }
    159 
    160 /* Acquire an adaptive mutex */
    161 static inline int
    162 MUTEX_ACQUIRE(struct kmutex *mtx, uintptr_t curthread)
    163 {
    164 	if (!__cpu_simple_lock_try(&mtx->mtx_lock))
    165 		return 0;
    166 	mtx->mtx_owner = curthread;
    167 	return 1;
    168 }
    169 
    170 /* Release an adaptive mutex */
    171 static inline void
    172 MUTEX_RELEASE(struct kmutex *mtx)
    173 {
    174 	mtx->mtx_owner = MUTEX_ADAPTIVE_UNOWNED;
    175 	__cpu_simple_unlock(&mtx->mtx_lock);
    176 	mtx->mtx_waiters = 0;
    177 }
    178 
    179 static inline void
    180 MUTEX_CLEAR_WAITERS(struct kmutex *mtx)
    181 {
    182 	mtx->mtx_waiters = 0;
    183 }
    184 
    185 #endif	/* !__ASSEMBLER__ && _KERNEL */
    186 
    187 #endif	/* __MUTEX_PRIVATE */
    188 
    189 #endif /* _HPPA_MUTEX_H_ */
    190