Reimplement IPI forwarding of FAST interrupts to the cpu owning the BGL
[dragonfly.git] / sys / platform / pc32 / include / lock.h
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $FreeBSD: src/sys/i386/include/lock.h,v 1.11.2.2 2000/09/30 02:49:34 ps Exp $
35  * $DragonFly: src/sys/platform/pc32/include/lock.h,v 1.13 2005/11/03 04:53:57 dillon Exp $
36  */
37
38 #ifndef _MACHINE_LOCK_H_
39 #define _MACHINE_LOCK_H_
40
41 #ifndef _MACHINE_PSL_H_
42 #include "psl.h"
43 #endif
44
45 /*
46  * MP_FREE_LOCK is used by both assembly and C under SMP.
47  */
48 #ifdef SMP
49 #define MP_FREE_LOCK            0xffffffff      /* value of lock when free */
50 #endif
51
52 #ifdef LOCORE
53
54 /*
55  * Spinlock assembly support.  Note: eax and ecx can be tromped.  No
56  * other register will be.   Note that these routines are sometimes
57  * called with (%edx) as the mem argument.
58  *
59  * Under UP the spinlock routines still serve to disable/restore 
60  * interrupts.
61  */
62
63
64 #ifdef SMP
65
66 #define SPIN_INIT(mem)                                          \
67         movl    $0,mem ;                                        \
68
69 #define SPIN_INIT_NOREG(mem)                                    \
70         SPIN_INIT(mem) ;                                        \
71
72 #define SPIN_LOCK(mem)                                          \
73         pushfl ;                                                \
74         popl    %ecx ;          /* flags */                     \
75         cli ;                                                   \
76         orl     $PSL_C,%ecx ;   /* make sure non-zero */        \
77 7: ;                                                            \
78         movl    $0,%eax ;       /* expected contents of lock */ \
79         lock cmpxchgl %ecx,mem ; /* Z=1 (jz) on success */      \
80         pause ;                                                 \
81         jnz     7b ;                                            \
82
83 #define SPIN_LOCK_PUSH_REGS                                     \
84         subl    $8,%esp ;                                       \
85         movl    %ecx,(%esp) ;                                   \
86         movl    %eax,4(%esp) ;                                  \
87
88 #define SPIN_LOCK_POP_REGS                                      \
89         movl    (%esp),%ecx ;                                   \
90         movl    4(%esp),%eax ;                                  \
91         addl    $8,%esp ;                                       \
92
93 #define SPIN_LOCK_FRAME_SIZE    8
94
95 #define SPIN_LOCK_NOREG(mem)                                    \
96         SPIN_LOCK_PUSH_REGS ;                                   \
97         SPIN_LOCK(mem) ;                                        \
98         SPIN_LOCK_POP_REGS ;                                    \
99
100 #define SPIN_UNLOCK(mem)                                        \
101         pushl   mem ;                                           \
102         movl    $0,mem ;                                        \
103         popfl ;                                                 \
104
105 #define SPIN_UNLOCK_PUSH_REGS
106 #define SPIN_UNLOCK_POP_REGS
107 #define SPIN_UNLOCK_FRAME_SIZE  0
108
109 #define SPIN_UNLOCK_NOREG(mem)                                  \
110         SPIN_UNLOCK(mem) ;                                      \
111
112 #else
113
114 #define SPIN_LOCK(mem)                                          \
115         pushfl ;                                                \
116         cli ;                                                   \
117         orl     $PSL_C,(%esp) ;                                 \
118         popl    mem ;                                           \
119
120 #define SPIN_LOCK_PUSH_RESG
121 #define SPIN_LOCK_POP_REGS
122 #define SPIN_LOCK_FRAME_SIZE    0
123
124 #define SPIN_UNLOCK(mem)                                        \
125         pushl   mem ;                                           \
126         movl    $0,mem ;                                        \
127         popfl ;                                                 \
128
129 #define SPIN_UNLOCK_PUSH_REGS
130 #define SPIN_UNLOCK_POP_REGS
131 #define SPIN_UNLOCK_FRAME_SIZE  0
132
133 #endif  /* SMP */
134
135 #else   /* !LOCORE */
136
137 #ifdef _KERNEL
138
139 /*
140  * Spinlock functions (UP and SMP).  Under UP a spinlock still serves
141  * to disable/restore interrupts even if it doesn't spin.
142  */
143 struct spinlock_deprecated {
144         volatile int    opaque;
145 };
146
147 typedef struct spinlock_deprecated *spinlock_t;
148
149 void    mpintr_lock(void);      /* disables int / spinlock combo */
150 void    mpintr_unlock(void);
151 void    com_lock(void);         /* disables int / spinlock combo */
152 void    com_unlock(void);
153 void    imen_lock(void);        /* disables int / spinlock combo */
154 void    imen_unlock(void);
155 void    clock_lock(void);       /* disables int / spinlock combo */
156 void    clock_unlock(void);
157 void    cons_lock(void);        /* disables int / spinlock combo */
158 void    cons_unlock(void);
159
160 extern struct spinlock_deprecated smp_rv_spinlock;
161
162 void    spin_lock_deprecated(spinlock_t lock);
163 void    spin_unlock_deprecated(spinlock_t lock);
164
165 /*
166  * Inline version of spinlock routines -- overrides assembly.  Only unlock
167  * and init here please.
168  */
169 static __inline void
170 spin_lock_init(spinlock_t lock)
171 {
172         lock->opaque = 0;
173 }
174
175 #endif  /* _KERNEL */
176
177 #if defined(_KERNEL) || defined(_UTHREAD)
178
179 /*
180  * MP LOCK functions for SMP and UP.  Under UP the MP lock does not exist
181  * but we leave a few functions intact as macros for convenience.
182  */
183 #ifdef SMP
184
185 void    get_mplock(void);
186 int     try_mplock(void);
187 void    rel_mplock(void);
188 int     cpu_try_mplock(void);
189 void    cpu_get_initial_mplock(void);
190
191 extern u_int    mp_lock;
192
193 #define MP_LOCK_HELD()   (mp_lock == mycpu->gd_cpuid)
194 #define ASSERT_MP_LOCK_HELD(td)   KASSERT(MP_LOCK_HELD(), ("MP_LOCK_HELD(): not held thread %p", td))
195
196 static __inline void
197 cpu_rel_mplock(void)
198 {
199         mp_lock = MP_FREE_LOCK;
200 }
201
202 static __inline int
203 owner_mplock(void)
204 {
205         return (mp_lock);
206 }
207
208 #else
209
210 #define get_mplock()
211 #define try_mplock()    1
212 #define rel_mplock()
213 #define owner_mplock()  0       /* always cpu 0 */
214 #define ASSERT_MP_LOCK_HELD(td)
215
216 #endif  /* SMP */
217 #endif  /* _KERNEL || _UTHREAD */
218 #endif  /* LOCORE */
219 #endif  /* !_MACHINE_LOCK_H_ */