lock.h revision 1.3
1/*	$NetBSD: lock.h,v 1.3 2002/09/16 07:00:43 gmcgarry Exp $	*/
2
3/*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wayne Knowles.
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/*
40 * Machine-dependent spin lock operations for MIPS R4000 Processors.
41 *
42 * Note:  R3000 doesn't have any atomic update instructions
43 */
44
45#ifndef _MIPS_LOCK_H_
46#define	_MIPS_LOCK_H_
47
48typedef	__volatile int		__cpu_simple_lock_t;
49
50#define	__SIMPLELOCK_LOCKED	1
51#define	__SIMPLELOCK_UNLOCKED	0
52
53#ifndef _KERNEL
54
55static __inline void
56__cpu_simple_lock_init(__cpu_simple_lock_t *lp)
57{
58
59	__asm __volatile(
60		"# -- BEGIN __cpu_simple_lock_init\n"
61		"	.set push		\n"
62		"	.set mips2		\n"
63		"	sw	$0, %0		\n"
64		"	sync			\n"
65		"	.set pop		\n"
66		"# -- END __cpu_simple_lock_init\n"
67		: "=m" (*lp));
68}
69
70static __inline void
71__cpu_simple_lock(__cpu_simple_lock_t *lp)
72{
73	unsigned long t0;
74
75	/*
76	 * Note, if we detect that the lock is held when
77	 * we do the initial load-locked, we spin using
78	 * a non-locked load to save the coherency logic
79	 * some work.
80	 */
81
82	__asm __volatile(
83		"# -- BEGIN __cpu_simple_lock	\n"
84		"	.set push		\n"
85		"	.set mips2		\n"
86		"1:	ll	%0, %3		\n"
87		"	bnez	%0, 2f		\n"
88		"	nop	       # BDslot	\n"
89		"	li	%0, %2		\n"
90		"	sc	%0, %1		\n"
91		"	beqz	%0, 1b		\n"
92		"	nop	       # BDslot	\n"
93		"	nop			\n"
94		"	sync			\n"
95		"	j	3f		\n"
96		"	nop			\n"
97		"	nop			\n"
98		"2:	lw	%0, %3		\n"
99		"	bnez	%0, 2b		\n"
100		"	nop	       # BDslot	\n"
101		"	j	1b		\n"
102		"	nop			\n"
103		"3:				\n"
104		"	.set pop		\n"
105		"# -- END __cpu_simple_lock	\n"
106		: "=r" (t0), "+m" (*lp)
107		: "i" (__SIMPLELOCK_LOCKED), "1" (*lp));
108}
109
110static __inline int
111__cpu_simple_lock_try(__cpu_simple_lock_t *lp)
112{
113	unsigned long t0, v0;
114
115	__asm __volatile(
116		"# -- BEGIN __cpu_simple_lock_try\n"
117		"	.set push		\n"
118		"	.set mips2		\n"
119		"1:	ll	%0, %4		\n"
120		"	bnez	%0, 2f		\n"
121		"	nop	       # BDslot	\n"
122		"	li	%0, %3		\n"
123		"	sc	%0, %2		\n"
124		"	beqz	%0, 2f		\n"
125		"	nop	       # BDslot	\n"
126		"	li	%1, 1		\n"
127		"	sync			\n"
128		"	j	3f		\n"
129		"	nop			\n"
130		"	nop			\n"
131		"2:	li	%1, 0		\n"
132		"3:				\n"
133		"	.set pop		\n"
134		"# -- END __cpu_simple_lock_try	\n"
135		: "=r" (t0), "=r" (v0), "+m" (*lp)
136		: "i" (__SIMPLELOCK_LOCKED), "2" (*lp));
137
138	return (v0 != 0);
139}
140
141static __inline void
142__cpu_simple_unlock(__cpu_simple_lock_t *lp)
143{
144
145	__asm __volatile(
146		"# -- BEGIN __cpu_simple_unlock \n"
147		"	.set push		\n"
148		"	.set mips2		\n"
149		"	sync			\n"
150		"	sw	$0, %0		\n"
151		"	.set pop		\n"
152		"# -- END __cpu_simple_unlock	\n"
153		: "=m" (*lp));
154}
155
156#endif
157
158#endif /* _MIPS_LOCK_H_ */
159