blob: ce3edf6d63b3bdf4bac03a982d00151577843d4c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * include/asm-s390/spinlock.h
3 *
4 * S390 version
5 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7 *
8 * Derived from "include/asm-i386/spinlock.h"
9 */
10
11#ifndef __ASM_SPINLOCK_H
12#define __ASM_SPINLOCK_H
13
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020014#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
15
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070016static inline int
17_raw_compare_and_swap(volatile unsigned int *lock,
18 unsigned int old, unsigned int new)
19{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020020 asm volatile(
21 " cs %0,%3,%1"
22 : "=d" (old), "=Q" (*lock)
23 : "0" (old), "d" (new), "Q" (*lock)
24 : "cc", "memory" );
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070025 return old;
26}
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020028#else /* __GNUC__ */
29
30static inline int
31_raw_compare_and_swap(volatile unsigned int *lock,
32 unsigned int old, unsigned int new)
33{
34 asm volatile(
35 " cs %0,%3,0(%4)"
36 : "=d" (old), "=m" (*lock)
37 : "0" (old), "d" (new), "a" (lock), "m" (*lock)
38 : "cc", "memory" );
39 return old;
40}
41
42#endif /* __GNUC__ */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * Simple spin lock operations. There are two variants, one clears IRQ's
46 * on the local processor, one does not.
47 *
48 * We make no fairness assumptions. They have a cost.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070049 *
50 * (the type definitions are in asm/spinlock_types.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
52
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070053#define __raw_spin_is_locked(x) ((x)->lock != 0)
54#define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
55#define __raw_spin_unlock_wait(lock) \
56 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070058extern void _raw_spin_lock_wait(raw_spinlock_t *lp, unsigned int pc);
59extern int _raw_spin_trylock_retry(raw_spinlock_t *lp, unsigned int pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070061static inline void __raw_spin_lock(raw_spinlock_t *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Heiko Carstens9513e5e2005-09-03 15:58:05 -070063 unsigned long pc = 1 | (unsigned long) __builtin_return_address(0);
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070064
65 if (unlikely(_raw_compare_and_swap(&lp->lock, 0, pc) != 0))
66 _raw_spin_lock_wait(lp, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070069static inline int __raw_spin_trylock(raw_spinlock_t *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Heiko Carstens9513e5e2005-09-03 15:58:05 -070071 unsigned long pc = 1 | (unsigned long) __builtin_return_address(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070073 if (likely(_raw_compare_and_swap(&lp->lock, 0, pc) == 0))
74 return 1;
75 return _raw_spin_trylock_retry(lp, pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070078static inline void __raw_spin_unlock(raw_spinlock_t *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070080 _raw_compare_and_swap(&lp->lock, lp->lock, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83/*
84 * Read-write spinlocks, allowing multiple readers
85 * but only one writer.
86 *
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.
92 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/**
95 * read_can_lock - would read_trylock() succeed?
96 * @lock: the rwlock in question.
97 */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070098#define __raw_read_can_lock(x) ((int)(x)->lock >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/**
101 * write_can_lock - would write_trylock() succeed?
102 * @lock: the rwlock in question.
103 */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700104#define __raw_write_can_lock(x) ((x)->lock == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700106extern void _raw_read_lock_wait(raw_rwlock_t *lp);
107extern int _raw_read_trylock_retry(raw_rwlock_t *lp);
108extern void _raw_write_lock_wait(raw_rwlock_t *lp);
109extern int _raw_write_trylock_retry(raw_rwlock_t *lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700111static inline void __raw_read_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700113 unsigned int old;
114 old = rw->lock & 0x7fffffffU;
115 if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old)
116 _raw_read_lock_wait(rw);
117}
118
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700119static inline void __raw_read_unlock(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700120{
121 unsigned int old, cmp;
122
123 old = rw->lock;
124 do {
125 cmp = old;
126 old = _raw_compare_and_swap(&rw->lock, old, old - 1);
127 } while (cmp != old);
128}
129
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700130static inline void __raw_write_lock(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700131{
132 if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0))
133 _raw_write_lock_wait(rw);
134}
135
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700136static inline void __raw_write_unlock(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700137{
138 _raw_compare_and_swap(&rw->lock, 0x80000000, 0);
139}
140
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700141static inline int __raw_read_trylock(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700142{
143 unsigned int old;
144 old = rw->lock & 0x7fffffffU;
145 if (likely(_raw_compare_and_swap(&rw->lock, old, old + 1) == old))
146 return 1;
147 return _raw_read_trylock_retry(rw);
148}
149
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700150static inline int __raw_write_trylock(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700151{
152 if (likely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0))
153 return 1;
154 return _raw_write_trylock_retry(rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157#endif /* __ASM_SPINLOCK_H */