Full support for Ginger Console
[linux-ginger.git] / arch / s390 / include / asm / spinlock.h
blob41ce6861174eea7e2b15579e7d80b1ef34e9432a
1 /*
2 * include/asm-s390/spinlock.h
4 * S390 version
5 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
8 * Derived from "include/asm-i386/spinlock.h"
9 */
11 #ifndef __ASM_SPINLOCK_H
12 #define __ASM_SPINLOCK_H
14 #include <linux/smp.h>
16 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
18 static inline int
19 _raw_compare_and_swap(volatile unsigned int *lock,
20 unsigned int old, unsigned int new)
22 asm volatile(
23 " cs %0,%3,%1"
24 : "=d" (old), "=Q" (*lock)
25 : "0" (old), "d" (new), "Q" (*lock)
26 : "cc", "memory" );
27 return old;
30 #else /* __GNUC__ */
32 static inline int
33 _raw_compare_and_swap(volatile unsigned int *lock,
34 unsigned int old, unsigned int new)
36 asm volatile(
37 " cs %0,%3,0(%4)"
38 : "=d" (old), "=m" (*lock)
39 : "0" (old), "d" (new), "a" (lock), "m" (*lock)
40 : "cc", "memory" );
41 return old;
44 #endif /* __GNUC__ */
47 * Simple spin lock operations. There are two variants, one clears IRQ's
48 * on the local processor, one does not.
50 * We make no fairness assumptions. They have a cost.
52 * (the type definitions are in asm/spinlock_types.h)
55 #define __raw_spin_is_locked(x) ((x)->owner_cpu != 0)
56 #define __raw_spin_unlock_wait(lock) \
57 do { while (__raw_spin_is_locked(lock)) \
58 _raw_spin_relax(lock); } while (0)
60 extern void _raw_spin_lock_wait(raw_spinlock_t *);
61 extern void _raw_spin_lock_wait_flags(raw_spinlock_t *, unsigned long flags);
62 extern int _raw_spin_trylock_retry(raw_spinlock_t *);
63 extern void _raw_spin_relax(raw_spinlock_t *lock);
65 static inline void __raw_spin_lock(raw_spinlock_t *lp)
67 int old;
69 old = _raw_compare_and_swap(&lp->owner_cpu, 0, ~smp_processor_id());
70 if (likely(old == 0))
71 return;
72 _raw_spin_lock_wait(lp);
75 static inline void __raw_spin_lock_flags(raw_spinlock_t *lp,
76 unsigned long flags)
78 int old;
80 old = _raw_compare_and_swap(&lp->owner_cpu, 0, ~smp_processor_id());
81 if (likely(old == 0))
82 return;
83 _raw_spin_lock_wait_flags(lp, flags);
86 static inline int __raw_spin_trylock(raw_spinlock_t *lp)
88 int old;
90 old = _raw_compare_and_swap(&lp->owner_cpu, 0, ~smp_processor_id());
91 if (likely(old == 0))
92 return 1;
93 return _raw_spin_trylock_retry(lp);
96 static inline void __raw_spin_unlock(raw_spinlock_t *lp)
98 _raw_compare_and_swap(&lp->owner_cpu, lp->owner_cpu, 0);
102 * Read-write spinlocks, allowing multiple readers
103 * but only one writer.
105 * NOTE! it is quite common to have readers in interrupts
106 * but no interrupt writers. For those circumstances we
107 * can "mix" irq-safe locks - any writer needs to get a
108 * irq-safe write-lock, but readers can get non-irqsafe
109 * read-locks.
113 * read_can_lock - would read_trylock() succeed?
114 * @lock: the rwlock in question.
116 #define __raw_read_can_lock(x) ((int)(x)->lock >= 0)
119 * write_can_lock - would write_trylock() succeed?
120 * @lock: the rwlock in question.
122 #define __raw_write_can_lock(x) ((x)->lock == 0)
124 extern void _raw_read_lock_wait(raw_rwlock_t *lp);
125 extern void _raw_read_lock_wait_flags(raw_rwlock_t *lp, unsigned long flags);
126 extern int _raw_read_trylock_retry(raw_rwlock_t *lp);
127 extern void _raw_write_lock_wait(raw_rwlock_t *lp);
128 extern void _raw_write_lock_wait_flags(raw_rwlock_t *lp, unsigned long flags);
129 extern int _raw_write_trylock_retry(raw_rwlock_t *lp);
131 static inline void __raw_read_lock(raw_rwlock_t *rw)
133 unsigned int old;
134 old = rw->lock & 0x7fffffffU;
135 if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old)
136 _raw_read_lock_wait(rw);
139 static inline void __raw_read_lock_flags(raw_rwlock_t *rw, unsigned long flags)
141 unsigned int old;
142 old = rw->lock & 0x7fffffffU;
143 if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old)
144 _raw_read_lock_wait_flags(rw, flags);
147 static inline void __raw_read_unlock(raw_rwlock_t *rw)
149 unsigned int old, cmp;
151 old = rw->lock;
152 do {
153 cmp = old;
154 old = _raw_compare_and_swap(&rw->lock, old, old - 1);
155 } while (cmp != old);
158 static inline void __raw_write_lock(raw_rwlock_t *rw)
160 if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0))
161 _raw_write_lock_wait(rw);
164 static inline void __raw_write_lock_flags(raw_rwlock_t *rw, unsigned long flags)
166 if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0))
167 _raw_write_lock_wait_flags(rw, flags);
170 static inline void __raw_write_unlock(raw_rwlock_t *rw)
172 _raw_compare_and_swap(&rw->lock, 0x80000000, 0);
175 static inline int __raw_read_trylock(raw_rwlock_t *rw)
177 unsigned int old;
178 old = rw->lock & 0x7fffffffU;
179 if (likely(_raw_compare_and_swap(&rw->lock, old, old + 1) == old))
180 return 1;
181 return _raw_read_trylock_retry(rw);
184 static inline int __raw_write_trylock(raw_rwlock_t *rw)
186 if (likely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0))
187 return 1;
188 return _raw_write_trylock_retry(rw);
191 #define _raw_read_relax(lock) cpu_relax()
192 #define _raw_write_relax(lock) cpu_relax()
194 #define __always_inline__spin_lock
195 #define __always_inline__read_lock
196 #define __always_inline__write_lock
197 #define __always_inline__spin_lock_bh
198 #define __always_inline__read_lock_bh
199 #define __always_inline__write_lock_bh
200 #define __always_inline__spin_lock_irq
201 #define __always_inline__read_lock_irq
202 #define __always_inline__write_lock_irq
203 #define __always_inline__spin_lock_irqsave
204 #define __always_inline__read_lock_irqsave
205 #define __always_inline__write_lock_irqsave
206 #define __always_inline__spin_trylock
207 #define __always_inline__read_trylock
208 #define __always_inline__write_trylock
209 #define __always_inline__spin_trylock_bh
210 #define __always_inline__spin_unlock
211 #define __always_inline__read_unlock
212 #define __always_inline__write_unlock
213 #define __always_inline__spin_unlock_bh
214 #define __always_inline__read_unlock_bh
215 #define __always_inline__write_unlock_bh
216 #define __always_inline__spin_unlock_irq
217 #define __always_inline__read_unlock_irq
218 #define __always_inline__write_unlock_irq
219 #define __always_inline__spin_unlock_irqrestore
220 #define __always_inline__read_unlock_irqrestore
221 #define __always_inline__write_unlock_irqrestore
223 #endif /* __ASM_SPINLOCK_H */