| 1 | /*
|
|---|
| 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
|---|
| 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 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup sync
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef KERN_SPINLOCK_H_
|
|---|
| 36 | #define KERN_SPINLOCK_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <typedefs.h>
|
|---|
| 39 | #include <arch/barrier.h>
|
|---|
| 40 | #include <preemption.h>
|
|---|
| 41 | #include <atomic.h>
|
|---|
| 42 | #include <debug.h>
|
|---|
| 43 | #include <arch/asm.h>
|
|---|
| 44 |
|
|---|
| 45 | #ifdef CONFIG_SMP
|
|---|
| 46 |
|
|---|
| 47 | typedef struct {
|
|---|
| 48 | atomic_t val;
|
|---|
| 49 |
|
|---|
| 50 | #ifdef CONFIG_DEBUG_SPINLOCK
|
|---|
| 51 | const char *name;
|
|---|
| 52 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| 53 | } spinlock_t;
|
|---|
| 54 |
|
|---|
| 55 | /*
|
|---|
| 56 | * SPINLOCK_DECLARE is to be used for dynamically allocated spinlocks,
|
|---|
| 57 | * where the lock gets initialized in run time.
|
|---|
| 58 | */
|
|---|
| 59 | #define SPINLOCK_DECLARE(lock_name) spinlock_t lock_name
|
|---|
| 60 | #define SPINLOCK_EXTERN(lock_name) extern spinlock_t lock_name
|
|---|
| 61 |
|
|---|
| 62 | /*
|
|---|
| 63 | * SPINLOCK_INITIALIZE and SPINLOCK_STATIC_INITIALIZE are to be used
|
|---|
| 64 | * for statically allocated spinlocks. They declare (either as global
|
|---|
| 65 | * or static) symbol and initialize the lock.
|
|---|
| 66 | */
|
|---|
| 67 | #ifdef CONFIG_DEBUG_SPINLOCK
|
|---|
| 68 |
|
|---|
| 69 | #define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 70 | spinlock_t lock_name = { \
|
|---|
| 71 | .name = desc_name, \
|
|---|
| 72 | .val = { 0 } \
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | #define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 76 | static spinlock_t lock_name = { \
|
|---|
| 77 | .name = desc_name, \
|
|---|
| 78 | .val = { 0 } \
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | #define ASSERT_SPINLOCK(expr, lock) \
|
|---|
| 82 | ASSERT_VERBOSE(expr, (lock)->name)
|
|---|
| 83 |
|
|---|
| 84 | #define spinlock_lock(lock) spinlock_lock_debug((lock))
|
|---|
| 85 | #define spinlock_unlock(lock) spinlock_unlock_debug((lock))
|
|---|
| 86 |
|
|---|
| 87 | #else /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| 88 |
|
|---|
| 89 | #define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 90 | spinlock_t lock_name = { \
|
|---|
| 91 | .val = { 0 } \
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | #define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 95 | static spinlock_t lock_name = { \
|
|---|
| 96 | .val = { 0 } \
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | #define ASSERT_SPINLOCK(expr, lock) \
|
|---|
| 100 | ASSERT(expr)
|
|---|
| 101 |
|
|---|
| 102 | #define spinlock_lock(lock) atomic_lock_arch(&(lock)->val)
|
|---|
| 103 | #define spinlock_unlock(lock) spinlock_unlock_nondebug((lock))
|
|---|
| 104 |
|
|---|
| 105 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| 106 |
|
|---|
| 107 | #define SPINLOCK_INITIALIZE(lock_name) \
|
|---|
| 108 | SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
|
|---|
| 109 |
|
|---|
| 110 | #define SPINLOCK_STATIC_INITIALIZE(lock_name) \
|
|---|
| 111 | SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
|
|---|
| 112 |
|
|---|
| 113 | extern void spinlock_initialize(spinlock_t *, const char *);
|
|---|
| 114 | extern int spinlock_trylock(spinlock_t *);
|
|---|
| 115 | extern void spinlock_lock_debug(spinlock_t *);
|
|---|
| 116 | extern void spinlock_unlock_debug(spinlock_t *);
|
|---|
| 117 | extern bool spinlock_locked(spinlock_t *);
|
|---|
| 118 |
|
|---|
| 119 | /** Unlock spinlock
|
|---|
| 120 | *
|
|---|
| 121 | * Unlock spinlock for non-debug kernels.
|
|---|
| 122 | *
|
|---|
| 123 | * @param sl Pointer to spinlock_t structure.
|
|---|
| 124 | *
|
|---|
| 125 | */
|
|---|
| 126 | NO_TRACE static inline void spinlock_unlock_nondebug(spinlock_t *lock)
|
|---|
| 127 | {
|
|---|
| 128 | /*
|
|---|
| 129 | * Prevent critical section code from bleeding out this way down.
|
|---|
| 130 | */
|
|---|
| 131 | CS_LEAVE_BARRIER();
|
|---|
| 132 |
|
|---|
| 133 | atomic_set(&lock->val, 0);
|
|---|
| 134 | preemption_enable();
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | #ifdef CONFIG_DEBUG_SPINLOCK
|
|---|
| 138 |
|
|---|
| 139 | #include <print.h>
|
|---|
| 140 |
|
|---|
| 141 | #define DEADLOCK_THRESHOLD 100000000
|
|---|
| 142 |
|
|---|
| 143 | #define DEADLOCK_PROBE_INIT(pname) size_t pname = 0
|
|---|
| 144 |
|
|---|
| 145 | #define DEADLOCK_PROBE(pname, value) \
|
|---|
| 146 | if ((pname)++ > (value)) { \
|
|---|
| 147 | (pname) = 0; \
|
|---|
| 148 | printf("Deadlock probe %s: exceeded threshold %u\n" \
|
|---|
| 149 | "cpu%u: function=%s, line=%u\n", \
|
|---|
| 150 | #pname, (value), CPU->id, __func__, __LINE__); \
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | #else /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| 154 |
|
|---|
| 155 | #define DEADLOCK_PROBE_INIT(pname)
|
|---|
| 156 | #define DEADLOCK_PROBE(pname, value)
|
|---|
| 157 |
|
|---|
| 158 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| 159 |
|
|---|
| 160 | #else /* CONFIG_SMP */
|
|---|
| 161 |
|
|---|
| 162 | /* On UP systems, spinlocks are effectively left out. */
|
|---|
| 163 |
|
|---|
| 164 | #define SPINLOCK_DECLARE(name)
|
|---|
| 165 | #define SPINLOCK_EXTERN(name)
|
|---|
| 166 |
|
|---|
| 167 | #define SPINLOCK_INITIALIZE(name)
|
|---|
| 168 | #define SPINLOCK_STATIC_INITIALIZE(name)
|
|---|
| 169 |
|
|---|
| 170 | #define SPINLOCK_INITIALIZE_NAME(name, desc_name)
|
|---|
| 171 | #define SPINLOCK_STATIC_INITIALIZE_NAME(name, desc_name)
|
|---|
| 172 |
|
|---|
| 173 | #define ASSERT_SPINLOCK(expr, lock) ASSERT(expr)
|
|---|
| 174 |
|
|---|
| 175 | #define spinlock_initialize(lock, name)
|
|---|
| 176 |
|
|---|
| 177 | #define spinlock_lock(lock) preemption_disable()
|
|---|
| 178 | #define spinlock_trylock(lock) (preemption_disable(), 1)
|
|---|
| 179 | #define spinlock_unlock(lock) preemption_enable()
|
|---|
| 180 | #define spinlock_locked(lock) 1
|
|---|
| 181 | #define spinlock_unlocked(lock) 1
|
|---|
| 182 |
|
|---|
| 183 | #define DEADLOCK_PROBE_INIT(pname)
|
|---|
| 184 | #define DEADLOCK_PROBE(pname, value)
|
|---|
| 185 |
|
|---|
| 186 | #endif /* CONFIG_SMP */
|
|---|
| 187 |
|
|---|
| 188 | typedef struct {
|
|---|
| 189 | SPINLOCK_DECLARE(lock); /**< Spinlock */
|
|---|
| 190 | bool guard; /**< Flag whether ipl is valid */
|
|---|
| 191 | ipl_t ipl; /**< Original interrupt level */
|
|---|
| 192 | } irq_spinlock_t;
|
|---|
| 193 |
|
|---|
| 194 | #define IRQ_SPINLOCK_DECLARE(lock_name) irq_spinlock_t lock_name
|
|---|
| 195 | #define IRQ_SPINLOCK_EXTERN(lock_name) extern irq_spinlock_t lock_name
|
|---|
| 196 |
|
|---|
| 197 | #ifdef CONFIG_SMP
|
|---|
| 198 |
|
|---|
| 199 | #define ASSERT_IRQ_SPINLOCK(expr, irq_lock) \
|
|---|
| 200 | ASSERT_SPINLOCK(expr, &((irq_lock)->lock))
|
|---|
| 201 |
|
|---|
| 202 | /*
|
|---|
| 203 | * IRQ_SPINLOCK_INITIALIZE and IRQ_SPINLOCK_STATIC_INITIALIZE are to be used
|
|---|
| 204 | * for statically allocated interrupts-disabled spinlocks. They declare (either
|
|---|
| 205 | * as global or static symbol) and initialize the lock.
|
|---|
| 206 | */
|
|---|
| 207 | #ifdef CONFIG_DEBUG_SPINLOCK
|
|---|
| 208 |
|
|---|
| 209 | #define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 210 | irq_spinlock_t lock_name = { \
|
|---|
| 211 | .lock = { \
|
|---|
| 212 | .name = desc_name, \
|
|---|
| 213 | .val = { 0 } \
|
|---|
| 214 | }, \
|
|---|
| 215 | .guard = false, \
|
|---|
| 216 | .ipl = 0 \
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | #define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 220 | static irq_spinlock_t lock_name = { \
|
|---|
| 221 | .lock = { \
|
|---|
| 222 | .name = desc_name, \
|
|---|
| 223 | .val = { 0 } \
|
|---|
| 224 | }, \
|
|---|
| 225 | .guard = false, \
|
|---|
| 226 | .ipl = 0 \
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | #else /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| 230 |
|
|---|
| 231 | #define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 232 | irq_spinlock_t lock_name = { \
|
|---|
| 233 | .lock = { \
|
|---|
| 234 | .val = { 0 } \
|
|---|
| 235 | }, \
|
|---|
| 236 | .guard = false, \
|
|---|
| 237 | .ipl = 0 \
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | #define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 241 | static irq_spinlock_t lock_name = { \
|
|---|
| 242 | .lock = { \
|
|---|
| 243 | .val = { 0 } \
|
|---|
| 244 | }, \
|
|---|
| 245 | .guard = false, \
|
|---|
| 246 | .ipl = 0 \
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | #endif /* CONFIG_DEBUG_SPINLOCK */
|
|---|
| 250 |
|
|---|
| 251 | #else /* CONFIG_SMP */
|
|---|
| 252 |
|
|---|
| 253 | /*
|
|---|
| 254 | * Since the spinlocks are void on UP systems, we also need
|
|---|
| 255 | * to have a special variant of interrupts-disabled spinlock
|
|---|
| 256 | * macros which take this into account.
|
|---|
| 257 | */
|
|---|
| 258 |
|
|---|
| 259 | #define ASSERT_IRQ_SPINLOCK(expr, irq_lock) \
|
|---|
| 260 | ASSERT_SPINLOCK(expr, NULL)
|
|---|
| 261 |
|
|---|
| 262 | #define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 263 | irq_spinlock_t lock_name = { \
|
|---|
| 264 | .guard = false, \
|
|---|
| 265 | .ipl = 0 \
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | #define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
|
|---|
| 269 | static irq_spinlock_t lock_name = { \
|
|---|
| 270 | .guard = false, \
|
|---|
| 271 | .ipl = 0 \
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | #endif /* CONFIG_SMP */
|
|---|
| 275 |
|
|---|
| 276 | #define IRQ_SPINLOCK_INITIALIZE(lock_name) \
|
|---|
| 277 | IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
|
|---|
| 278 |
|
|---|
| 279 | #define IRQ_SPINLOCK_STATIC_INITIALIZE(lock_name) \
|
|---|
| 280 | IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
|
|---|
| 281 |
|
|---|
| 282 | extern void irq_spinlock_initialize(irq_spinlock_t *, const char *);
|
|---|
| 283 | extern void irq_spinlock_lock(irq_spinlock_t *, bool);
|
|---|
| 284 | extern void irq_spinlock_unlock(irq_spinlock_t *, bool);
|
|---|
| 285 | extern int irq_spinlock_trylock(irq_spinlock_t *);
|
|---|
| 286 | extern void irq_spinlock_pass(irq_spinlock_t *, irq_spinlock_t *);
|
|---|
| 287 | extern void irq_spinlock_exchange(irq_spinlock_t *, irq_spinlock_t *);
|
|---|
| 288 | extern bool irq_spinlock_locked(irq_spinlock_t *);
|
|---|
| 289 |
|
|---|
| 290 | #endif
|
|---|
| 291 |
|
|---|
| 292 | /** @}
|
|---|
| 293 | */
|
|---|