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