1885Swollman/*	$NetBSD: lock.h,v 1.3 2008/04/28 20:23:25 martin Exp $	*/
2885Swollman
3885Swollman/*-
4885Swollman * Copyright (c) 2000 The NetBSD Foundation, Inc.
5885Swollman * All rights reserved.
6885Swollman *
7885Swollman * This code is derived from software contributed to The NetBSD Foundation
8885Swollman * by Jason R. Thorpe.
9885Swollman *
10885Swollman * Redistribution and use in source and binary forms, with or without
11885Swollman * modification, are permitted provided that the following conditions
12885Swollman * are met:
13885Swollman * 1. Redistributions of source code must retain the above copyright
14885Swollman *    notice, this list of conditions and the following disclaimer.
15885Swollman * 2. Redistributions in binary form must reproduce the above copyright
16885Swollman *    notice, this list of conditions and the following disclaimer in the
17885Swollman *    documentation and/or other materials provided with the distribution.
18885Swollman *
19885Swollman * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20885Swollman * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21885Swollman * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2210625Sdg * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23885Swollman * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24885Swollman * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25885Swollman * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26885Swollman * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27885Swollman * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28885Swollman * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29885Swollman * POSSIBILITY OF SUCH DAMAGE.
30885Swollman */
3112130Sdg
32885Swollman/*
33885Swollman * Machine-dependent spin lock operations.
342056Swollman */
352056Swollman
3611332Sswallace#ifndef _IA64_LOCK_H_
372056Swollman#define	_IA64_LOCK_H_
381549Srgrimes
392056Swollmanstatic __inline int
402056Swollman__SIMPLELOCK_LOCKED_P(__cpu_simple_lock_t *__ptr)
412056Swollman{
42885Swollman	return *__ptr == __SIMPLELOCK_LOCKED;
43886Swollman}
44885Swollman
45886Swollmanstatic __inline int
46886Swollman__SIMPLELOCK_UNLOCKED_P(__cpu_simple_lock_t *__ptr)
47886Swollman{
48886Swollman	return *__ptr == __SIMPLELOCK_UNLOCKED;
49885Swollman}
50885Swollman
51885Swollmanstatic __inline void
52885Swollman__cpu_simple_lock_set(__cpu_simple_lock_t *__ptr)
5312130Sdg{
54885Swollman
55885Swollman	*__ptr = __SIMPLELOCK_LOCKED;
5612130Sdg}
5712130Sdg
58885Swollmanstatic __inline void
5912130Sdg__cpu_simple_lock_clear(__cpu_simple_lock_t *__ptr)
60885Swollman{
61885Swollman
62885Swollman	*__ptr = __SIMPLELOCK_UNLOCKED;
63885Swollman}
64885Swollman
65885Swollman#ifdef _KERNEL
66885Swollman
67885Swollman#define	SPINLOCK_SPIN_HOOK	/* nothing */
68885Swollman#define	SPINLOCK_BACKOFF_HOOK	/* XXX(kochi): hint@pause */
69885Swollman
70885Swollman#endif
7112130Sdg
72885Swollmanstatic __inline void __cpu_simple_lock_init(__cpu_simple_lock_t *)
73885Swollman	__unused;
7412130Sdgstatic __inline void __cpu_simple_lock(__cpu_simple_lock_t *)
75885Swollman	__unused;
76885Swollmanstatic __inline int __cpu_simple_lock_try(__cpu_simple_lock_t *)
77885Swollman	__unused;
78885Swollmanstatic __inline void __cpu_simple_unlock(__cpu_simple_lock_t *)
79885Swollman	__unused;
80885Swollman
81885Swollmanstatic __inline void
82885Swollman__cpu_simple_lock_init(__cpu_simple_lock_t *lockp)
83885Swollman{
84885Swollman
85885Swollman	*lockp = __SIMPLELOCK_UNLOCKED;
86885Swollman	__insn_barrier();
87885Swollman}
88885Swollman
89885Swollmanstatic __inline int
90885Swollman__cpu_simple_lock_try(__cpu_simple_lock_t *lockp)
91885Swollman{
92885Swollman	uint8_t val;
93885Swollman
94885Swollman	val = __SIMPLELOCK_LOCKED;
95885Swollman	__asm volatile ("xchg1 %0=[%1],%2" :
96885Swollman	    "=r" (val)
9712130Sdg	    :"r" (lockp), "r" (val)
98885Swollman	    :"memory");
99885Swollman	return val == __SIMPLELOCK_UNLOCKED;
100885Swollman}
101885Swollman
102885Swollmanstatic __inline void
10312130Sdg__cpu_simple_lock(__cpu_simple_lock_t *lockp)
104885Swollman{
105885Swollman
106885Swollman	while (!__cpu_simple_lock_try(lockp))
107885Swollman		/* nothing */;
108885Swollman	__insn_barrier();
109885Swollman}
110885Swollman
111885Swollmanstatic __inline void
112885Swollman__cpu_simple_unlock(__cpu_simple_lock_t *lockp)
113885Swollman{
114885Swollman
115885Swollman	__insn_barrier();
116885Swollman	*lockp = __SIMPLELOCK_UNLOCKED;
117885Swollman}
118885Swollman
119885Swollman#endif /* _IA64_LOCK_H_ */
120885Swollman