lock.h revision 1.10
11.10Sskrll/*	$NetBSD: lock.h,v 1.10 2007/09/10 11:34:09 skrll Exp $	*/
21.1Sthorpej
31.1Sthorpej/*-
41.9Sad * Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
51.1Sthorpej * All rights reserved.
61.1Sthorpej *
71.1Sthorpej * This code is derived from software contributed to The NetBSD Foundation
81.9Sad * by Jason R. Thorpe and Andrew Doran.
91.1Sthorpej *
101.1Sthorpej * Redistribution and use in source and binary forms, with or without
111.1Sthorpej * modification, are permitted provided that the following conditions
121.1Sthorpej * are met:
131.1Sthorpej * 1. Redistributions of source code must retain the above copyright
141.1Sthorpej *    notice, this list of conditions and the following disclaimer.
151.1Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
161.1Sthorpej *    notice, this list of conditions and the following disclaimer in the
171.1Sthorpej *    documentation and/or other materials provided with the distribution.
181.1Sthorpej * 3. All advertising materials mentioning features or use of this software
191.1Sthorpej *    must display the following acknowledgement:
201.1Sthorpej *	This product includes software developed by the NetBSD
211.1Sthorpej *	Foundation, Inc. and its contributors.
221.1Sthorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
231.1Sthorpej *    contributors may be used to endorse or promote products derived
241.1Sthorpej *    from this software without specific prior written permission.
251.1Sthorpej *
261.1Sthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.1Sthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.1Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.1Sthorpej * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.1Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.1Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.1Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.1Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.1Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.1Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.1Sthorpej * POSSIBILITY OF SUCH DAMAGE.
371.1Sthorpej */
381.1Sthorpej
391.1Sthorpej/*
401.1Sthorpej * Machine-dependent spin lock operations.
411.1Sthorpej */
421.1Sthorpej
431.1Sthorpej#ifndef _POWERPC_LOCK_H_
441.3Stsubai#define _POWERPC_LOCK_H_
451.3Stsubai
461.10Sskrllstatic __inline int
471.10Sskrll__SIMPLELOCK_LOCKED_P(__cpu_simple_lock_t *__ptr)
481.10Sskrll{
491.10Sskrll	return *__ptr == __SIMPLELOCK_LOCKED;
501.10Sskrll}
511.10Sskrll
521.10Sskrllstatic __inline int
531.10Sskrll__SIMPLELOCK_UNLOCKED_P(__cpu_simple_lock_t *__ptr)
541.10Sskrll{
551.10Sskrll	return *__ptr == __SIMPLELOCK_UNLOCKED;
561.10Sskrll}
571.10Sskrll
581.10Sskrllstatic __inline void
591.10Sskrll__cpu_simple_lock_clear(__cpu_simple_lock_t *__ptr)
601.10Sskrll{
611.10Sskrll	*__ptr = __SIMPLELOCK_UNLOCKED;
621.10Sskrll}
631.10Sskrll
641.10Sskrllstatic __inline void
651.10Sskrll__cpu_simple_lock_set(__cpu_simple_lock_t *__ptr)
661.10Sskrll{
671.10Sskrll	*__ptr = __SIMPLELOCK_LOCKED;
681.10Sskrll}
691.10Sskrll
701.8Sperrystatic __inline void
711.3Stsubai__cpu_simple_lock_init(__cpu_simple_lock_t *alp)
721.3Stsubai{
731.3Stsubai	*alp = __SIMPLELOCK_UNLOCKED;
741.7Sperry	__asm volatile ("sync");
751.3Stsubai}
761.3Stsubai
771.8Sperrystatic __inline void
781.3Stsubai__cpu_simple_lock(__cpu_simple_lock_t *alp)
791.3Stsubai{
801.3Stsubai	int old;
811.3Stsubai
821.7Sperry	__asm volatile ("	\
831.3Stsubai				\n\
841.3Stsubai1:	lwarx	%0,0,%1		\n\
851.3Stsubai	cmpwi	%0,%2		\n\
861.3Stsubai	beq+	3f		\n\
871.4Stsubai2:	lwzx	%0,0,%1		\n\
881.3Stsubai	cmpwi	%0,%2		\n\
891.3Stsubai	beq+	1b		\n\
901.3Stsubai	b	2b		\n\
911.3Stsubai3:	stwcx.	%3,0,%1		\n\
921.3Stsubai	bne-	1b		\n\
931.3Stsubai	isync			\n\
941.3Stsubai				\n"
951.3Stsubai	: "=&r"(old)
961.3Stsubai	: "r"(alp), "I"(__SIMPLELOCK_UNLOCKED), "r"(__SIMPLELOCK_LOCKED)
971.3Stsubai	: "memory");
981.3Stsubai}
991.3Stsubai
1001.8Sperrystatic __inline int
1011.3Stsubai__cpu_simple_lock_try(__cpu_simple_lock_t *alp)
1021.3Stsubai{
1031.4Stsubai	int old, dummy;
1041.3Stsubai
1051.7Sperry	__asm volatile ("	\
1061.3Stsubai				\n\
1071.3Stsubai1:	lwarx	%0,0,%1		\n\
1081.3Stsubai	cmpwi	%0,%2		\n\
1091.3Stsubai	bne	2f		\n\
1101.3Stsubai	stwcx.	%3,0,%1		\n\
1111.3Stsubai	bne-	1b		\n\
1121.4Stsubai2:	stwcx.	%3,0,%4		\n\
1131.3Stsubai	isync			\n\
1141.4Stsubai				\n"
1151.3Stsubai	: "=&r"(old)
1161.4Stsubai	: "r"(alp), "I"(__SIMPLELOCK_UNLOCKED), "r"(__SIMPLELOCK_LOCKED),
1171.4Stsubai	  "r"(&dummy)
1181.3Stsubai	: "memory");
1191.3Stsubai
1201.3Stsubai	return (old == __SIMPLELOCK_UNLOCKED);
1211.3Stsubai}
1221.3Stsubai
1231.8Sperrystatic __inline void
1241.3Stsubai__cpu_simple_unlock(__cpu_simple_lock_t *alp)
1251.3Stsubai{
1261.7Sperry	__asm volatile ("sync");
1271.3Stsubai	*alp = __SIMPLELOCK_UNLOCKED;
1281.3Stsubai}
1291.1Sthorpej
1301.9Sadstatic __inline void
1311.9Sadmb_read(void)
1321.9Sad{
1331.9Sad	__asm volatile ("isync" ::: "memory");
1341.9Sad}
1351.9Sad
1361.9Sadstatic __inline void
1371.9Sadmb_write(void)
1381.9Sad{
1391.9Sad	__asm volatile ("sync" ::: "memory");
1401.9Sad}
1411.9Sad
1421.9Sadstatic __inline void
1431.9Sadmb_memory(void)
1441.9Sad{
1451.9Sad	__asm volatile ("sync" ::: "memory");
1461.9Sad}
1471.9Sad
1481.1Sthorpej#endif /* _POWERPC_LOCK_H_ */
149