blob: df84ae96915f5342b4f86a76d0ac09651e12c4bd [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 Schwidefsky3c1fcfe2006-09-30 23:27:45 -070014#include <linux/smp.h>
15
Martin Schwidefsky42e47ee2006-10-04 20:02:12 +020016#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
17
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070018static inline int
19_raw_compare_and_swap(volatile unsigned int *lock,
20 unsigned int old, unsigned int new)
21{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020022 asm volatile(
23 " cs %0,%3,%1"
24 : "=d" (old), "=Q" (*lock)
25 : "0" (old), "d" (new), "Q" (*lock)
26 : "cc", "memory" );
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070027 return old;
28}
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020030#else /* __GNUC__ */
31
32static inline int
33_raw_compare_and_swap(volatile unsigned int *lock,
34 unsigned int old, unsigned int new)
35{
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;
42}
43
44#endif /* __GNUC__ */
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * Simple spin lock operations. There are two variants, one clears IRQ's
48 * on the local processor, one does not.
49 *
50 * We make no fairness assumptions. They have a cost.
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070051 *
52 * (the type definitions are in asm/spinlock_types.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
54
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070055#define __raw_spin_is_locked(x) ((x)->owner_cpu != 0)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070056#define __raw_spin_unlock_wait(lock) \
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070057 do { while (__raw_spin_is_locked(lock)) \
58 _raw_spin_relax(lock); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Heiko Carstens3b4beb32008-01-26 14:11:03 +010060extern void _raw_spin_lock_wait(raw_spinlock_t *);
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010061extern void _raw_spin_lock_wait_flags(raw_spinlock_t *, unsigned long flags);
Heiko Carstens3b4beb32008-01-26 14:11:03 +010062extern int _raw_spin_trylock_retry(raw_spinlock_t *);
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070063extern void _raw_spin_relax(raw_spinlock_t *lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070065static inline void __raw_spin_lock(raw_spinlock_t *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070067 int old;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070068
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070069 old = _raw_compare_and_swap(&lp->owner_cpu, 0, ~smp_processor_id());
Heiko Carstens3b4beb32008-01-26 14:11:03 +010070 if (likely(old == 0))
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070071 return;
Heiko Carstens3b4beb32008-01-26 14:11:03 +010072 _raw_spin_lock_wait(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010075static inline void __raw_spin_lock_flags(raw_spinlock_t *lp,
76 unsigned long flags)
77{
78 int old;
79
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);
84}
85
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070086static inline int __raw_spin_trylock(raw_spinlock_t *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070088 int old;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070090 old = _raw_compare_and_swap(&lp->owner_cpu, 0, ~smp_processor_id());
Heiko Carstens3b4beb32008-01-26 14:11:03 +010091 if (likely(old == 0))
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070092 return 1;
Heiko Carstens3b4beb32008-01-26 14:11:03 +010093 return _raw_spin_trylock_retry(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070096static inline void __raw_spin_unlock(raw_spinlock_t *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070098 _raw_compare_and_swap(&lp->owner_cpu, lp->owner_cpu, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101/*
102 * Read-write spinlocks, allowing multiple readers
103 * but only one writer.
104 *
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.
110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/**
113 * read_can_lock - would read_trylock() succeed?
114 * @lock: the rwlock in question.
115 */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700116#define __raw_read_can_lock(x) ((int)(x)->lock >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/**
119 * write_can_lock - would write_trylock() succeed?
120 * @lock: the rwlock in question.
121 */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700122#define __raw_write_can_lock(x) ((x)->lock == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700124extern void _raw_read_lock_wait(raw_rwlock_t *lp);
125extern int _raw_read_trylock_retry(raw_rwlock_t *lp);
126extern void _raw_write_lock_wait(raw_rwlock_t *lp);
127extern int _raw_write_trylock_retry(raw_rwlock_t *lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700129static inline void __raw_read_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700131 unsigned int old;
132 old = rw->lock & 0x7fffffffU;
133 if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old)
134 _raw_read_lock_wait(rw);
135}
136
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700137static inline void __raw_read_unlock(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700138{
139 unsigned int old, cmp;
140
141 old = rw->lock;
142 do {
143 cmp = old;
144 old = _raw_compare_and_swap(&rw->lock, old, old - 1);
145 } while (cmp != old);
146}
147
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700148static inline void __raw_write_lock(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700149{
150 if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0))
151 _raw_write_lock_wait(rw);
152}
153
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700154static inline void __raw_write_unlock(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700155{
156 _raw_compare_and_swap(&rw->lock, 0x80000000, 0);
157}
158
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700159static inline int __raw_read_trylock(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700160{
161 unsigned int old;
162 old = rw->lock & 0x7fffffffU;
163 if (likely(_raw_compare_and_swap(&rw->lock, old, old + 1) == old))
164 return 1;
165 return _raw_read_trylock_retry(rw);
166}
167
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700168static inline int __raw_write_trylock(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700169{
170 if (likely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0))
171 return 1;
172 return _raw_write_trylock_retry(rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Martin Schwidefskyef6edc92006-09-30 23:27:43 -0700175#define _raw_read_relax(lock) cpu_relax()
176#define _raw_write_relax(lock) cpu_relax()
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#endif /* __ASM_SPINLOCK_H */