lock.h revision 1.1.2.2 1 1.1.2.2 jdolecek /* $NetBSD: lock.h,v 1.1.2.2 2002/06/23 17:37:09 jdolecek Exp $ */
2 1.1.2.2 jdolecek
3 1.1.2.2 jdolecek /*-
4 1.1.2.2 jdolecek * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
5 1.1.2.2 jdolecek * All rights reserved.
6 1.1.2.2 jdolecek *
7 1.1.2.2 jdolecek * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 jdolecek * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1.2.2 jdolecek * NASA Ames Research Center, and Matthew Fredette.
10 1.1.2.2 jdolecek *
11 1.1.2.2 jdolecek * Redistribution and use in source and binary forms, with or without
12 1.1.2.2 jdolecek * modification, are permitted provided that the following conditions
13 1.1.2.2 jdolecek * are met:
14 1.1.2.2 jdolecek * 1. Redistributions of source code must retain the above copyright
15 1.1.2.2 jdolecek * notice, this list of conditions and the following disclaimer.
16 1.1.2.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
17 1.1.2.2 jdolecek * notice, this list of conditions and the following disclaimer in the
18 1.1.2.2 jdolecek * documentation and/or other materials provided with the distribution.
19 1.1.2.2 jdolecek * 3. All advertising materials mentioning features or use of this software
20 1.1.2.2 jdolecek * must display the following acknowledgement:
21 1.1.2.2 jdolecek * This product includes software developed by the NetBSD
22 1.1.2.2 jdolecek * Foundation, Inc. and its contributors.
23 1.1.2.2 jdolecek * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1.2.2 jdolecek * contributors may be used to endorse or promote products derived
25 1.1.2.2 jdolecek * from this software without specific prior written permission.
26 1.1.2.2 jdolecek *
27 1.1.2.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1.2.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1.2.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1.2.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1.2.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1.2.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1.2.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1.2.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1.2.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1.2.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1.2.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
38 1.1.2.2 jdolecek */
39 1.1.2.2 jdolecek
40 1.1.2.2 jdolecek /*
41 1.1.2.2 jdolecek * Machine-dependent spin lock operations.
42 1.1.2.2 jdolecek */
43 1.1.2.2 jdolecek
44 1.1.2.2 jdolecek #ifndef _HPPA_LOCK_H_
45 1.1.2.2 jdolecek #define _HPPA_LOCK_H_
46 1.1.2.2 jdolecek
47 1.1.2.2 jdolecek #ifdef MULTIPROCESSOR
48 1.1.2.2 jdolecek
49 1.1.2.2 jdolecek /*
50 1.1.2.2 jdolecek * Semaphores must be aligned on 16-byte boundaries on the PA-RISC.
51 1.1.2.2 jdolecek */
52 1.1.2.2 jdolecek typedef __volatile struct {
53 1.1.2.2 jdolecek int32_t sem __attribute__ ((aligned (16)));
54 1.1.2.2 jdolecek } __cpu_simple_lock_t;
55 1.1.2.2 jdolecek
56 1.1.2.2 jdolecek
57 1.1.2.2 jdolecek static __inline void
58 1.1.2.2 jdolecek __cpu_simple_lock_init(__cpu_simple_lock_t *alp)
59 1.1.2.2 jdolecek {
60 1.1.2.2 jdolecek __asm __volatile(
61 1.1.2.2 jdolecek " ; BEGIN __cpu_simple_lock_init\n"
62 1.1.2.2 jdolecek " stw %1, %0 \n"
63 1.1.2.2 jdolecek " sync \n"
64 1.1.2.2 jdolecek " ; END __cpu_simple_lock_init"
65 1.1.2.2 jdolecek : "=m" (*alp->sem)
66 1.1.2.2 jdolecek : "r" (__SIMPLELOCK_UNLOCKED));
67 1.1.2.2 jdolecek }
68 1.1.2.2 jdolecek
69 1.1.2.2 jdolecek static __inline void
70 1.1.2.2 jdolecek __cpu_simple_lock(__cpu_simple_lock_t *alp)
71 1.1.2.2 jdolecek {
72 1.1.2.2 jdolecek int32_t t0;
73 1.1.2.2 jdolecek
74 1.1.2.2 jdolecek /*
75 1.1.2.2 jdolecek * Note, if we detect that the lock is held when
76 1.1.2.2 jdolecek * we do the initial load-clear-word, we spin using
77 1.1.2.2 jdolecek * a non-locked load to save the coherency logic
78 1.1.2.2 jdolecek * some work.
79 1.1.2.2 jdolecek */
80 1.1.2.2 jdolecek
81 1.1.2.2 jdolecek __asm __volatile(
82 1.1.2.2 jdolecek " ; BEGIN __cpu_simple_lock\n"
83 1.1.2.2 jdolecek " ldcw %1, %0 \n"
84 1.1.2.2 jdolecek " comb,<>,n %%r0,%0, 2f \n"
85 1.1.2.2 jdolecek "1: comb,=,n %%r0,%0, 1b \n"
86 1.1.2.2 jdolecek " ldw %1, %0 \n"
87 1.1.2.2 jdolecek " ldcw %1, %0 \n"
88 1.1.2.2 jdolecek " comb,=,n %%r0,%0, 1b \n"
89 1.1.2.2 jdolecek " ldw %1, %0 \n"
90 1.1.2.2 jdolecek "2: sync \n"
91 1.1.2.2 jdolecek " ; END __cpu_simple_lock\n"
92 1.1.2.2 jdolecek : "=r" (t0), "+m" (*alp->sem));
93 1.1.2.2 jdolecek }
94 1.1.2.2 jdolecek
95 1.1.2.2 jdolecek static __inline int
96 1.1.2.2 jdolecek __cpu_simple_lock_try(__cpu_simple_lock_t *alp)
97 1.1.2.2 jdolecek {
98 1.1.2.2 jdolecek int32_t t0;
99 1.1.2.2 jdolecek
100 1.1.2.2 jdolecek __asm __volatile(
101 1.1.2.2 jdolecek " ; BEGIN __cpu_simple_lock_try\n"
102 1.1.2.2 jdolecek " ldcw %1, %0 \n"
103 1.1.2.2 jdolecek " sync \n"
104 1.1.2.2 jdolecek " ; END __cpu_simple_lock_try"
105 1.1.2.2 jdolecek : "=r" (t0), "+m" (*alp->sem));
106 1.1.2.2 jdolecek return (t0 != 0);
107 1.1.2.2 jdolecek }
108 1.1.2.2 jdolecek
109 1.1.2.2 jdolecek static __inline void
110 1.1.2.2 jdolecek __cpu_simple_unlock(__cpu_simple_lock_t *alp)
111 1.1.2.2 jdolecek {
112 1.1.2.2 jdolecek __asm __volatile(
113 1.1.2.2 jdolecek " ; BEGIN __cpu_simple_unlock\n"
114 1.1.2.2 jdolecek " sync \n"
115 1.1.2.2 jdolecek " stw %1, %0 \n"
116 1.1.2.2 jdolecek " ; END __cpu_simple_unlock"
117 1.1.2.2 jdolecek : "+m" (*alp->sem)
118 1.1.2.2 jdolecek : "r" (__SIMPLELOCK_UNLOCKED));
119 1.1.2.2 jdolecek }
120 1.1.2.2 jdolecek
121 1.1.2.2 jdolecek #else /* !MULTIPROCESSOR */
122 1.1.2.2 jdolecek
123 1.1.2.2 jdolecek typedef __volatile int __cpu_simple_lock_t;
124 1.1.2.2 jdolecek
125 1.1.2.2 jdolecek #define __SIMPLELOCK_LOCKED 1
126 1.1.2.2 jdolecek #define __SIMPLELOCK_UNLOCKED 0
127 1.1.2.2 jdolecek
128 1.1.2.2 jdolecek #endif /* !MULTIPROCESSOR */
129 1.1.2.2 jdolecek
130 1.1.2.2 jdolecek #endif /* _HPPA_LOCK_H_ */
131