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