Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / arch / s390 / include / asm / spinlock.h
blob56612fc8186ef25251ab235458b187bcda73f2b2
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 static inline int
17 _raw_compare_and_swap(volatile unsigned int *lock,
18 unsigned int old, unsigned int new)
20 asm volatile(
21 " cs %0,%3,%1"
22 : "=d" (old), "=Q" (*lock)
23 : "0" (old), "d" (new), "Q" (*lock)
24 : "cc", "memory" );
25 return old;
29 * Simple spin lock operations. There are two variants, one clears IRQ's
30 * on the local processor, one does not.
32 * We make no fairness assumptions. They have a cost.
34 * (the type definitions are in asm/spinlock_types.h)
37 #define arch_spin_is_locked(x) ((x)->owner_cpu != 0)
38 #define arch_spin_unlock_wait(lock) \
39 do { while (arch_spin_is_locked(lock)) \
40 arch_spin_relax(lock); } while (0)
42 extern void arch_spin_lock_wait(arch_spinlock_t *);
43 extern void arch_spin_lock_wait_flags(arch_spinlock_t *, unsigned long flags);
44 extern int arch_spin_trylock_retry(arch_spinlock_t *);
45 extern void arch_spin_relax(arch_spinlock_t *lock);
47 static inline void arch_spin_lock(arch_spinlock_t *lp)
49 int old;
51 old = _raw_compare_and_swap(&lp->owner_cpu, 0, ~smp_processor_id());
52 if (likely(old == 0))
53 return;
54 arch_spin_lock_wait(lp);
57 static inline void arch_spin_lock_flags(arch_spinlock_t *lp,
58 unsigned long flags)
60 int old;
62 old = _raw_compare_and_swap(&lp->owner_cpu, 0, ~smp_processor_id());
63 if (likely(old == 0))
64 return;
65 arch_spin_lock_wait_flags(lp, flags);
68 static inline int arch_spin_trylock(arch_spinlock_t *lp)
70 int old;
72 old = _raw_compare_and_swap(&lp->owner_cpu, 0, ~smp_processor_id());
73 if (likely(old == 0))
74 return 1;
75 return arch_spin_trylock_retry(lp);
78 static inline void arch_spin_unlock(arch_spinlock_t *lp)
80 _raw_compare_and_swap(&lp->owner_cpu, lp->owner_cpu, 0);
84 * Read-write spinlocks, allowing multiple readers
85 * but only one writer.
87 * NOTE! it is quite common to have readers in interrupts
88 * but no interrupt writers. For those circumstances we
89 * can "mix" irq-safe locks - any writer needs to get a
90 * irq-safe write-lock, but readers can get non-irqsafe
91 * read-locks.
94 /**
95 * read_can_lock - would read_trylock() succeed?
96 * @lock: the rwlock in question.
98 #define arch_read_can_lock(x) ((int)(x)->lock >= 0)
101 * write_can_lock - would write_trylock() succeed?
102 * @lock: the rwlock in question.
104 #define arch_write_can_lock(x) ((x)->lock == 0)
106 extern void _raw_read_lock_wait(arch_rwlock_t *lp);
107 extern void _raw_read_lock_wait_flags(arch_rwlock_t *lp, unsigned long flags);
108 extern int _raw_read_trylock_retry(arch_rwlock_t *lp);
109 extern void _raw_write_lock_wait(arch_rwlock_t *lp);
110 extern void _raw_write_lock_wait_flags(arch_rwlock_t *lp, unsigned long flags);
111 extern int _raw_write_trylock_retry(arch_rwlock_t *lp);
113 static inline void arch_read_lock(arch_rwlock_t *rw)
115 unsigned int old;
116 old = rw->lock & 0x7fffffffU;
117 if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old)
118 _raw_read_lock_wait(rw);
121 static inline void arch_read_lock_flags(arch_rwlock_t *rw, unsigned long flags)
123 unsigned int old;
124 old = rw->lock & 0x7fffffffU;
125 if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old)
126 _raw_read_lock_wait_flags(rw, flags);
129 static inline void arch_read_unlock(arch_rwlock_t *rw)
131 unsigned int old, cmp;
133 old = rw->lock;
134 do {
135 cmp = old;
136 old = _raw_compare_and_swap(&rw->lock, old, old - 1);
137 } while (cmp != old);
140 static inline void arch_write_lock(arch_rwlock_t *rw)
142 if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0))
143 _raw_write_lock_wait(rw);
146 static inline void arch_write_lock_flags(arch_rwlock_t *rw, unsigned long flags)
148 if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0))
149 _raw_write_lock_wait_flags(rw, flags);
152 static inline void arch_write_unlock(arch_rwlock_t *rw)
154 _raw_compare_and_swap(&rw->lock, 0x80000000, 0);
157 static inline int arch_read_trylock(arch_rwlock_t *rw)
159 unsigned int old;
160 old = rw->lock & 0x7fffffffU;
161 if (likely(_raw_compare_and_swap(&rw->lock, old, old + 1) == old))
162 return 1;
163 return _raw_read_trylock_retry(rw);
166 static inline int arch_write_trylock(arch_rwlock_t *rw)
168 if (likely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0))
169 return 1;
170 return _raw_write_trylock_retry(rw);
173 #define arch_read_relax(lock) cpu_relax()
174 #define arch_write_relax(lock) cpu_relax()
176 #endif /* __ASM_SPINLOCK_H */