xref: /netbsd-src/sys/arch/sparc/include/mutex.h (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: mutex.h,v 1.5 2007/10/17 19:57:13 garbled 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #ifndef _SPARC_MUTEX_H_
40 #define	_SPARC_MUTEX_H_
41 
42 /*
43  * There sparc mutex implementation is troublesome, because sparc (v7 and
44  * v8) lacks a compare-and-set operation, yet there are many SMP sparc
45  * machines in circulation.  SMP for spin mutexes is easy - we don't need
46  * to know who owns the lock.  For adaptive mutexes, we need an aditional
47  * interlock.
48  *
49  * The locked byte set by the sparc 'ldstub' instruction is 0xff.  sparc
50  * kernels are always loaded above 0xe0000000, and the low 5 bits of any
51  * "struct lwp *" are always zero.  So, to record the lock owner, we only
52  * need 23 bits of space.  mtxa_owner contains the mutex owner's address
53  * shifted right by 5: the top three bits of which will always be 0xe,
54  * overlapping with the interlock at the top byte, which is always 0xff
55  * when the mutex is held.
56  *
57  * For a mutex acquisition, the owner field is set in two steps: first,
58  * acquire the interlock (top byte), and second OR in the owner's address.
59  * Once the owner field is non zero, it will appear that the mutex is held,
60  * by which LWP it does not matter: other LWPs competing for the lock will
61  * fall through to mutex_vector_enter(), and either spin or sleep.
62  *
63  * As a result there is no space for a waiters bit in the owner field.  No
64  * problem, because it would be hard to synchronise using one without a CAS
65  * operation.  Note that in order to do unlocked release of adaptive
66  * mutexes, we need the effect of MUTEX_SET_WAITERS() to be immediatley
67  * visible on the bus.  So, adaptive mutexes share the spin lock byte with
68  * spin mutexes (set with ldstub), but it is not treated as a lock in its
69  * own right, rather as a flag that can be atomically set or cleared.
70  *
71  * When releasing an adaptive mutex, we first clear the owners field, and
72  * then check to see if the waiters byte is set.  This ensures that there
73  * will always be someone to wake any sleeping waiters up (even it the mutex
74  * is acquired immediately after we release it, or if we are preempted
75  * immediatley after clearing the owners field).  The setting or clearing of
76  * the waiters byte is serialized by the turnstile chain lock associated
77  * with the mutex.
78  *
79  * See comments in kern_mutex.c about releasing adaptive mutexes without
80  * an interlocking step.
81  */
82 
83 #ifndef __MUTEX_PRIVATE
84 
85 struct kmutex {
86 	uintptr_t	mtx_pad1;
87 	uint32_t	mtx_pad2;
88 };
89 
90 #else	/* __MUTEX_PRIVATE */
91 
92 struct kmutex {
93 	union {
94 		/* Adaptive mutex */
95 		volatile uintptr_t	mtxu_owner;		/* 0-3 */
96 		__cpu_simple_lock_t	mtxu_interlock;		/* 0 */
97 
98 		/* Spin mutex. */
99 		struct {
100 			uint8_t			mtxs_dummy;	/* 0 */
101 			uint8_t			mtxs_unused1;	/* 1 */
102 			ipl_cookie_t		mtxs_ipl;	/* 2 */
103 			uint8_t			mtxs_unused2;	/* 3 */
104 		} s;
105 	} u;
106 	__cpu_simple_lock_t	mtx_lock;			/* 4 */
107 	uint8_t			mtx_idtype[3];			/* 5-7 */
108 };
109 
110 #define	__HAVE_MUTEX_STUBS	1
111 #if 0 /* does not work for MP yet */
112 #define	__HAVE_SPIN_MUTEX_STUBS	1
113 #endif
114 
115 #define	mtx_owner	u.mtxu_owner
116 #define	mtx_interlock	u.mtxu_interlock
117 #define	mtx_dummy	u.s.mtxs_dummy
118 #define	mtx_ipl		u.s.mtxs_ipl
119 
120 static inline uintptr_t
121 MUTEX_OWNER(uintptr_t owner)
122 {
123 	return owner << 5;
124 }
125 
126 static inline int
127 MUTEX_OWNED(uintptr_t owner)
128 {
129 	return owner != 0;
130 }
131 
132 static inline int
133 MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
134 {
135 	(void)__cpu_simple_lock_try(&mtx->mtx_lock);
136  	return mtx->mtx_owner != 0;
137 }
138 
139 static inline int
140 MUTEX_HAS_WAITERS(volatile kmutex_t *mtx)
141 {
142 	if (mtx->mtx_owner == 0)
143 		return 0;
144 	return mtx->mtx_lock == __SIMPLELOCK_LOCKED;
145 }
146 
147 static inline void
148 MUTEX_INITIALIZE_SPIN(kmutex_t *mtx, u_int id, int ipl)
149 {
150 	mtx->mtx_idtype[0] = (uint8_t)id;
151 	mtx->mtx_idtype[1] = (uint8_t)(id >> 8);
152 	mtx->mtx_idtype[2] = (uint8_t)((id >> 16) | 0x80);
153 	mtx->mtx_ipl = makeiplcookie(ipl);
154 	mtx->mtx_interlock = __SIMPLELOCK_LOCKED;
155 	__cpu_simple_lock_init(&mtx->mtx_lock);
156 }
157 
158 static inline void
159 MUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, u_int id)
160 {
161 	mtx->mtx_idtype[0] = (uint8_t)id;
162 	mtx->mtx_idtype[1] = (uint8_t)(id >> 8);
163 	mtx->mtx_idtype[2] = (uint8_t)(id >> 16);
164 	__cpu_simple_lock_init(&mtx->mtx_lock);
165 }
166 
167 static inline void
168 MUTEX_DESTROY(kmutex_t *mtx)
169 {
170 	mtx->mtx_owner = (uintptr_t)-1L;
171 	mtx->mtx_idtype[0] = 0xff;
172 	mtx->mtx_idtype[1] = 0xff;
173 	mtx->mtx_idtype[2] = 0xff;
174 }
175 
176 static inline u_int
177 MUTEX_GETID(kmutex_t *mtx)
178 {
179 	return (u_int)mtx->mtx_idtype[0] |
180 	    ((u_int)mtx->mtx_idtype[1] << 8) |
181 	    (((u_int)mtx->mtx_idtype[2] & 0x7f) << 16);
182 }
183 
184 static inline int
185 MUTEX_SPIN_P(volatile kmutex_t *mtx)
186 {
187 	return mtx->mtx_idtype[2] & 0x80;
188 }
189 
190 static inline int
191 MUTEX_ADAPTIVE_P(volatile kmutex_t *mtx)
192 {
193 	return (mtx->mtx_idtype[2] & 0x80) == 0;
194 }
195 
196 static inline int
197 MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
198 {
199 	if (!__cpu_simple_lock_try(&mtx->mtx_interlock))
200 		return 0;
201 	mtx->mtx_owner = (curthread >> 5) | 0xf8000000;
202 	return 1;
203 }
204 
205 static inline void
206 MUTEX_RELEASE(kmutex_t *mtx)
207 {
208 	mtx->mtx_owner = 0;
209 	__cpu_simple_unlock(&mtx->mtx_lock);
210 }
211 
212 static inline void
213 MUTEX_CLEAR_WAITERS(kmutex_t *mtx)
214 {
215 	__cpu_simple_unlock(&mtx->mtx_lock);
216 }
217 
218 #endif	/* __MUTEX_PRIVATE */
219 
220 #endif /* _SPARC_MUTEX_H_ */
221