Initial import from FreeBSD RELENG_4:
[games.git] / sys / i386 / include / lock.h
1 /*
2  * Copyright (c) 1997, by Steve Passe
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. The name of the developer may NOT be used to endorse or promote products
11  *    derived from this software without specific prior written permission.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: src/sys/i386/include/lock.h,v 1.11.2.2 2000/09/30 02:49:34 ps Exp $
26  */
27
28
29 #ifndef _MACHINE_LOCK_H_
30 #define _MACHINE_LOCK_H_
31
32
33 #ifdef LOCORE
34
35 #ifdef SMP
36
37 #define MPLOCKED        lock ;
38
39 /*
40  * Some handy macros to allow logical organization.
41  */
42
43 #define MP_LOCK         call    _get_mplock
44
45 #define MP_TRYLOCK                                                      \
46         pushl   $_mp_lock ;                     /* GIANT_LOCK */        \
47         call    _MPtrylock ;                    /* try to get lock */   \
48         add     $4, %esp
49
50 #define MP_RELLOCK                                                      \
51         movl    $_mp_lock,%edx ;                /* GIANT_LOCK */        \
52         call    _MPrellock_edx
53
54 /*
55  * Protects the IO APIC and apic_imen as a critical region.
56  */
57 #define IMASK_LOCK                                                      \
58         pushl   $_imen_lock ;                   /* address of lock */   \
59         call    _s_lock ;                       /* MP-safe */           \
60         addl    $4, %esp
61
62 #define IMASK_UNLOCK                                                    \
63         movl    $0, _imen_lock
64
65 #else  /* SMP */
66
67 #define MPLOCKED                                /* NOP */
68
69 #define MP_LOCK                                 /* NOP */
70
71 #endif /* SMP */
72
73 #else /* LOCORE */
74
75 #ifdef SMP
76
77 #include <machine/smptests.h>                   /** xxx_LOCK */
78
79 /*
80  * Locks regions protected in UP kernel via cli/sti.
81  */
82 #ifdef USE_MPINTRLOCK
83 #define MPINTR_LOCK()   s_lock(&mpintr_lock)
84 #define MPINTR_UNLOCK() s_unlock(&mpintr_lock)
85 #else
86 #define MPINTR_LOCK()
87 #define MPINTR_UNLOCK()
88 #endif /* USE_MPINTRLOCK */
89
90 /*
91  * sio/cy lock.
92  * XXX should rc (RISCom/8) use this?
93  */
94 #ifdef USE_COMLOCK
95 #define COM_LOCK()      s_lock(&com_lock)
96 #define COM_UNLOCK()    s_unlock(&com_lock)
97 #define COM_DISABLE_INTR() \
98                 { __asm __volatile("cli" : : : "memory"); COM_LOCK(); }
99 #define COM_ENABLE_INTR() \
100                 { COM_UNLOCK(); __asm __volatile("sti"); }
101 #else
102 #define COM_LOCK()
103 #define COM_UNLOCK()
104 #define COM_DISABLE_INTR()      disable_intr()
105 #define COM_ENABLE_INTR()       enable_intr()
106 #endif /* USE_COMLOCK */
107
108 /* 
109  * Clock hardware/struct lock.
110  * XXX pcaudio and friends still need this lock installed.
111  */
112 #ifdef USE_CLOCKLOCK
113 #define CLOCK_LOCK()    s_lock(&clock_lock)
114 #define CLOCK_UNLOCK()  s_unlock(&clock_lock)
115 #define CLOCK_DISABLE_INTR() \
116                 { __asm __volatile("cli" : : : "memory"); CLOCK_LOCK(); }
117 #define CLOCK_ENABLE_INTR() \
118                 { CLOCK_UNLOCK(); __asm __volatile("sti"); }
119 #else
120 #define CLOCK_LOCK()
121 #define CLOCK_UNLOCK()
122 #define CLOCK_DISABLE_INTR()    disable_intr()
123 #define CLOCK_ENABLE_INTR()     enable_intr()
124 #endif /* USE_CLOCKLOCK */
125
126 #else /* SMP */
127
128 #define MPINTR_LOCK()
129 #define MPINTR_UNLOCK()
130
131 #define COM_LOCK()
132 #define COM_UNLOCK()
133 #define CLOCK_LOCK()
134 #define CLOCK_UNLOCK()
135
136 #endif /* SMP */
137
138 /*
139  * Simple spin lock.
140  * It is an error to hold one of these locks while a process is sleeping.
141  */
142 struct simplelock {
143         volatile int    lock_data;
144 };
145
146 /* functions in simplelock.s */
147 void    s_lock_init             __P((struct simplelock *));
148 void    s_lock                  __P((struct simplelock *));
149 int     s_lock_try              __P((struct simplelock *));
150 void    ss_lock                 __P((struct simplelock *));
151 void    ss_unlock               __P((struct simplelock *));
152 void    s_lock_np               __P((struct simplelock *));
153 void    s_unlock_np             __P((struct simplelock *));
154
155 /* inline simplelock functions */
156 static __inline void
157 s_unlock(struct simplelock *lkp)
158 {
159         lkp->lock_data = 0;
160 }
161
162 /* global data in mp_machdep.c */
163 extern struct simplelock        imen_lock;
164 extern struct simplelock        cpl_lock;
165 extern struct simplelock        fast_intr_lock;
166 extern struct simplelock        intr_lock;
167 extern struct simplelock        clock_lock;
168 extern struct simplelock        com_lock;
169 extern struct simplelock        mpintr_lock;
170 extern struct simplelock        mcount_lock;
171
172 #if !defined(SIMPLELOCK_DEBUG) && MAXCPU > 1
173 /*
174  * This set of defines turns on the real functions in i386/isa/apic_ipl.s.
175  */
176 #define simple_lock_init(alp)   s_lock_init(alp)
177 #define simple_lock(alp)        s_lock(alp)
178 #define simple_lock_try(alp)    s_lock_try(alp)
179 #define simple_unlock(alp)      s_unlock(alp)
180
181 #endif /* !SIMPLELOCK_DEBUG && MAXCPU > 1 */
182
183 #endif /* LOCORE */
184
185 #endif /* !_MACHINE_LOCK_H_ */