2 * arch/ubicom32/include/asm/spinlock.h
3 * Spinlock related definitions for Ubicom32 architecture.
5 * (C) Copyright 2009, Ubicom, Inc.
7 * This file is part of the Ubicom32 Linux Kernel Port.
9 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
10 * it and/or modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation, either version 2 of the
12 * License, or (at your option) any later version.
14 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with the Ubicom32 Linux Kernel Port. If not,
21 * see <http://www.gnu.org/licenses/>.
23 * Ubicom32 implementation derived from (with many thanks):
28 #ifndef _ASM_UBICOM32_SPINLOCK_H
29 #define _ASM_UBICOM32_SPINLOCK_H
31 #include <asm/system.h>
32 #include <asm/processor.h>
33 #include <asm/spinlock_types.h>
39 static inline void __raw_spin_lock(raw_spinlock_t
*x
)
42 "1: bset %0, %0, #0 \n\t"
54 static inline void __raw_spin_unlock(raw_spinlock_t
*x
)
57 " bclr %0, %0, #0 \n\t"
65 * __raw_spin_is_locked()
66 * Test if the lock is locked.
68 static inline int __raw_spin_is_locked(raw_spinlock_t
*x
)
74 * __raw_spin_unlock_wait()
75 * Wait for the lock to be unlocked.
77 * Note: the caller has not guarantee that the lock will not
78 * be acquired before they get to it.
80 static inline void __raw_spin_unlock_wait(raw_spinlock_t
*x
)
84 } while (__raw_spin_is_locked(x
));
88 * __raw_spin_trylock()
89 * Try the lock, return 0 on failure, 1 on success.
91 static inline int __raw_spin_trylock(raw_spinlock_t
*x
)
96 " bset %1, %1, #0 \n\t"
100 : "+r" (ret
), "+U4" (x
->lock
)
109 * __raw_spin_lock_flags()
110 * Spin waiting for the lock (enabling IRQ(s))
112 static inline void __raw_spin_lock_flags(raw_spinlock_t
*x
, unsigned long flags
)
115 while (!__raw_spin_trylock(x
)) {
117 * If the flags from the IRQ are set, interrupts are disabled and we
118 * need to re-enable them.
123 raw_local_irq_enable();
125 raw_local_irq_disable();
132 * Read-write spinlocks, allowing multiple readers but only one writer.
133 * Linux rwlocks are unfair to writers; they can be starved for an indefinite
134 * time by readers. With care, they can also be taken in interrupt context.
136 * In Ubicom32 architecture implementation, we have a spinlock and a counter.
137 * Readers use the lock to serialise their access to the counter (which
138 * records how many readers currently hold the lock).
139 * Writers hold the spinlock, preventing any readers or other writers from
140 * grabbing the rwlock.
145 * Increment the counter in the rwlock.
147 * Note that we have to ensure interrupts are disabled in case we're
148 * interrupted by some other code that wants to grab the same read lock
150 static inline void __raw_read_lock(raw_rwlock_t
*rw
)
153 raw_local_irq_save(flags
);
154 __raw_spin_lock_flags(&rw
->lock
, flags
);
156 __raw_spin_unlock(&rw
->lock
);
157 raw_local_irq_restore(flags
);
161 * __raw_read_unlock()
162 * Decrement the counter.
164 * Note that we have to ensure interrupts are disabled in case we're
165 * interrupted by some other code that wants to grab the same read lock
167 static inline void __raw_read_unlock(raw_rwlock_t
*rw
)
170 raw_local_irq_save(flags
);
171 __raw_spin_lock_flags(&rw
->lock
, flags
);
173 __raw_spin_unlock(&rw
->lock
);
174 raw_local_irq_restore(flags
);
178 * __raw_read_trylock()
179 * Increment the counter if we can.
181 * Note that we have to ensure interrupts are disabled in case we're
182 * interrupted by some other code that wants to grab the same read lock
184 static inline int __raw_read_trylock(raw_rwlock_t
*rw
)
188 raw_local_irq_save(flags
);
189 if (__raw_spin_trylock(&rw
->lock
)) {
191 __raw_spin_unlock(&rw
->lock
);
192 raw_local_irq_restore(flags
);
196 raw_local_irq_restore(flags
);
199 * If write-locked, we fail to acquire the lock
201 if (rw
->counter
< 0) {
206 * Wait until we have a realistic chance at the lock
208 while (__raw_spin_is_locked(&rw
->lock
) && rw
->counter
>= 0) {
218 * Note that we have to ensure interrupts are disabled in case we're
219 * interrupted by some other code that wants to read_trylock() this lock
221 static inline void __raw_write_lock(raw_rwlock_t
*rw
)
225 raw_local_irq_save(flags
);
226 __raw_spin_lock_flags(&rw
->lock
, flags
);
228 if (rw
->counter
!= 0) {
229 __raw_spin_unlock(&rw
->lock
);
230 raw_local_irq_restore(flags
);
232 while (rw
->counter
!= 0)
238 rw
->counter
= -1; /* mark as write-locked */
240 raw_local_irq_restore(flags
);
243 static inline void __raw_write_unlock(raw_rwlock_t
*rw
)
246 __raw_spin_unlock(&rw
->lock
);
249 /* Note that we have to ensure interrupts are disabled in case we're
250 * interrupted by some other code that wants to read_trylock() this lock */
251 static inline int __raw_write_trylock(raw_rwlock_t
*rw
)
256 raw_local_irq_save(flags
);
257 if (__raw_spin_trylock(&rw
->lock
)) {
258 if (rw
->counter
== 0) {
262 /* Read-locked. Oh well. */
263 __raw_spin_unlock(&rw
->lock
);
266 raw_local_irq_restore(flags
);
272 * read_can_lock - would read_trylock() succeed?
273 * @lock: the rwlock in question.
275 static inline int __raw_read_can_lock(raw_rwlock_t
*rw
)
277 return rw
->counter
>= 0;
281 * write_can_lock - would write_trylock() succeed?
282 * @lock: the rwlock in question.
284 static inline int __raw_write_can_lock(raw_rwlock_t
*rw
)
289 #define __raw_read_lock_flags(lock, flags) __raw_read_lock(lock)
290 #define __raw_write_lock_flags(lock, flags) __raw_write_lock(lock)
292 #define _raw_spin_relax(lock) cpu_relax()
293 #define _raw_read_relax(lock) cpu_relax()
294 #define _raw_write_relax(lock) cpu_relax()
296 #endif /* _ASM_UBICOM32_SPINLOCK_H */