1/*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * Copyright (c) 1998 Apple Computer, Inc.  All rights reserved.
30 *
31 * HISTORY
32 *
33 */
34
35/* 	Copyright (c) 1992 NeXT Computer, Inc.  All rights reserved.
36 *
37 * EventShmemLock.h -	Shared memory area locks for use between the
38 *			WindowServer and the Event Driver.
39 *
40 * HISTORY
41 * 30 Nov   1992    Ben Fathi (benf@next.com)
42 *      Ported to m98k.
43 *
44 * 29 April 1992    Mike Paquette at NeXT
45 *      Created.
46 *
47 * Multiprocessor locks used within the shared memory area between the
48 * kernel and event system.  These must work in both user and kernel mode.
49 * The locks are defined in an include file so they get exported to the local
50 * include file area.
51 */
52
53
54#ifndef _IOKIT_IOSHAREDLOCKIMP_H
55#define _IOKIT_IOSHAREDLOCKIMP_H
56
57#include <architecture/ppc/asm_help.h>
58#ifdef KERNEL
59#undef END
60#include <mach/ppc/asm.h>
61#endif
62
63/*
64 *	void
65 *	ev_lock(p)
66 *		register int *p;
67 *
68 *	Lock the lock pointed to by p.  Spin (possibly forever) until
69 *		the lock is available.  Test and test and set logic used.
70 */
71	TEXT
72
73#ifndef KERNEL
74LEAF(_ev_lock)
75
76		li		a6,1			// lock value
77
788:		lwz		a7,0(a0)		// Get lock word
79		mr.		a7,a7			// Is it held?
80		bne--	8b				// Yup...
81
829:		lwarx	a7,0,a0			// read the lock
83		mr.		a7,a7			// Is it held?
84		bne--	7f				// yes, kill reservation
85		stwcx.	a6,0,a0			// try to get the lock
86		bne--	9b 				// failed, try again
87		isync
88		blr						// got it, return
89
907:		li		a7,-4			// Point to a spot in the red zone
91		stwcx.	a7,a7,r1		// Kill reservation
92		b		8b				// Go wait some more...
93
94
95END(_ev_lock)
96
97LEAF(_IOSpinLock)
98
99		li		a6,1			// lock value
100
1018:		lwz		a7,0(a0)		// Get lock word
102		mr.		a7,a7			// Is it held?
103		bne--	8b				// Yup...
104
1059:		lwarx	a7,0,a0			// read the lock
106		mr.		a7,a7			// Is it held?
107		bne--	7f				// yes, kill reservation
108		stwcx.	a6,0,a0			// try to get the lock
109		bne--	9b 				// failed, try again
110		isync
111		blr						// got it, return
112
1137:		li		a7,-4			// Point to a spot in the red zone
114		stwcx.	a7,a7,r1		// Kill reservation
115		b		8b				// Go wait some more...
116END(_IOSpinLock)
117#endif
118
119/*
120 *	void
121 *	spin_unlock(p)
122 *		int *p;
123 *
124 *	Unlock the lock pointed to by p.
125 */
126
127LEAF(_ev_unlock)
128	sync
129	li	a7,0
130	stw	a7,0(a0)
131	blr
132END(_ev_unlock)
133
134LEAF(_IOSpinUnlock)
135	sync
136	li	a7,0
137	stw	a7,0(a0)
138	blr
139END(_IOSpinUnlock)
140
141
142/*
143 *	ev_try_lock(p)
144 *		int *p;
145 *
146 *	Try to lock p.  Return TRUE if successful in obtaining lock.
147 */
148
149LEAF(_ev_try_lock)
150		li		a6,1			// lock value
151
152		lwz		a7,0(a0)		// Get lock word
153		mr.		a7,a7			// Is it held?
154		bne--	6f				// Yup...
155
1569:		lwarx	a7,0,a0			// read the lock
157		mr.		a7,a7			// Is it held?
158		bne--	7f				// yes, kill reservation
159		stwcx.	a6,0,a0			// try to get the lock
160		bne--	9b 				// failed, try again
161		li		a0,1			// return TRUE
162		isync
163		blr						// got it, return
164
1657:		li		a7,-4			// Point to a spot in the red zone
166		stwcx.	a7,a7,r1		// Kill reservation
167
1686:
169		li	a0,0				// return FALSE
170		blr
171
172END(_ev_try_lock)
173
174LEAF(_IOTrySpinLock)
175		li		a6,1			// lock value
176
177		lwz		a7,0(a0)		// Get lock word
178		mr.		a7,a7			// Is it held?
179		bne--	6f				// Yup...
180
1819:		lwarx	a7,0,a0			// read the lock
182		mr.		a7,a7			// Is it held?
183		bne--	7f				// yes, kill reservation
184		stwcx.	a6,0,a0			// try to get the lock
185		bne--	9b 				// failed, try again
186		li		a0,1			// return TRUE
187		isync
188		blr						// got it, return
189
1907:		li		a7,-4			// Point to a spot in the red zone
191		stwcx.	a7,a7,r1		// Kill reservation
192
1936:
194		li	a0,0				// return FALSE
195		blr
196
197END(_IOTrySpinLock)
198
199#endif /* ! _IOKIT_IOSHAREDLOCKIMP_H */
200