blob: 74036f436a3b51e9f6c6f1b55ba1f56502aa07f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ASM_SPINLOCK_H
2#define __ASM_SPINLOCK_H
3
4#include <asm/system.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -07005#include <asm/processor.h>
6#include <asm/spinlock_types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01008static inline int arch_spin_is_locked(arch_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009{
10 volatile unsigned int *a = __ldcw_align(x);
11 return *a == 0;
12}
13
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010014#define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0)
15#define arch_spin_unlock_wait(x) \
16 do { cpu_relax(); } while (arch_spin_is_locked(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010018static inline void arch_spin_lock_flags(arch_spinlock_t *x,
James Bottomley08dc2ca2005-11-17 16:35:09 -050019 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
21 volatile unsigned int *a;
22
23 mb();
24 a = __ldcw_align(x);
25 while (__ldcw(a) == 0)
James Bottomley08dc2ca2005-11-17 16:35:09 -050026 while (*a == 0)
27 if (flags & PSW_SM_I) {
28 local_irq_enable();
29 cpu_relax();
30 local_irq_disable();
31 } else
32 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 mb();
34}
35
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010036static inline void arch_spin_unlock(arch_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 volatile unsigned int *a;
39 mb();
40 a = __ldcw_align(x);
41 *a = 1;
42 mb();
43}
44
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010045static inline int arch_spin_trylock(arch_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 volatile unsigned int *a;
48 int ret;
49
50 mb();
51 a = __ldcw_align(x);
52 ret = __ldcw(a) != 0;
53 mb();
54
55 return ret;
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*
Matthew Wilcox6e071852006-09-02 07:54:58 -060059 * Read-write spinlocks, allowing multiple readers but only one writer.
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -060060 * Linux rwlocks are unfair to writers; they can be starved for an indefinite
61 * time by readers. With care, they can also be taken in interrupt context.
62 *
63 * In the PA-RISC implementation, we have a spinlock and a counter.
64 * Readers use the lock to serialise their access to the counter (which
65 * records how many readers currently hold the lock).
66 * Writers hold the spinlock, preventing any readers or other writers from
67 * grabbing the rwlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -060070/* Note that we have to ensure interrupts are disabled in case we're
71 * interrupted by some other code that wants to grab the same read lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +010072static __inline__ void arch_read_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Matthew Wilcox6e071852006-09-02 07:54:58 -060074 unsigned long flags;
75 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010076 arch_spin_lock_flags(&rw->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 rw->counter++;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010078 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -060079 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -060082/* Note that we have to ensure interrupts are disabled in case we're
83 * interrupted by some other code that wants to grab the same read lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +010084static __inline__ void arch_read_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Matthew Wilcox6e071852006-09-02 07:54:58 -060086 unsigned long flags;
87 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010088 arch_spin_lock_flags(&rw->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 rw->counter--;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010090 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -060091 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -060094/* Note that we have to ensure interrupts are disabled in case we're
95 * interrupted by some other code that wants to grab the same read lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +010096static __inline__ int arch_read_trylock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Matthew Wilcox6e071852006-09-02 07:54:58 -060098 unsigned long flags;
99 retry:
100 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100101 if (arch_spin_trylock(&rw->lock)) {
Matthew Wilcox6e071852006-09-02 07:54:58 -0600102 rw->counter++;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100103 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -0600104 local_irq_restore(flags);
105 return 1;
106 }
107
108 local_irq_restore(flags);
109 /* If write-locked, we fail to acquire the lock */
110 if (rw->counter < 0)
111 return 0;
112
113 /* Wait until we have a realistic chance at the lock */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100114 while (arch_spin_is_locked(&rw->lock) && rw->counter >= 0)
Matthew Wilcox6e071852006-09-02 07:54:58 -0600115 cpu_relax();
116
117 goto retry;
118}
119
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -0600120/* Note that we have to ensure interrupts are disabled in case we're
121 * interrupted by some other code that wants to read_trylock() this lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100122static __inline__ void arch_write_lock(arch_rwlock_t *rw)
Matthew Wilcox6e071852006-09-02 07:54:58 -0600123{
124 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125retry:
Matthew Wilcox6e071852006-09-02 07:54:58 -0600126 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100127 arch_spin_lock_flags(&rw->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Matthew Wilcox6e071852006-09-02 07:54:58 -0600129 if (rw->counter != 0) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100130 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -0600131 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700133 while (rw->counter != 0)
134 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 goto retry;
137 }
138
Matthew Wilcox6e071852006-09-02 07:54:58 -0600139 rw->counter = -1; /* mark as write-locked */
140 mb();
141 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Thomas Gleixnere5931942009-12-03 20:08:46 +0100144static __inline__ void arch_write_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 rw->counter = 0;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100147 arch_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -0600150/* Note that we have to ensure interrupts are disabled in case we're
151 * interrupted by some other code that wants to read_trylock() this lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100152static __inline__ int arch_write_trylock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Matthew Wilcox6e071852006-09-02 07:54:58 -0600154 unsigned long flags;
155 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Matthew Wilcox6e071852006-09-02 07:54:58 -0600157 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100158 if (arch_spin_trylock(&rw->lock)) {
Matthew Wilcox6e071852006-09-02 07:54:58 -0600159 if (rw->counter == 0) {
160 rw->counter = -1;
161 result = 1;
162 } else {
163 /* Read-locked. Oh well. */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100164 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -0600165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
Matthew Wilcox6e071852006-09-02 07:54:58 -0600167 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Matthew Wilcox6e071852006-09-02 07:54:58 -0600169 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700172/*
173 * read_can_lock - would read_trylock() succeed?
174 * @lock: the rwlock in question.
175 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100176static __inline__ int arch_read_can_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700178 return rw->counter >= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700181/*
182 * write_can_lock - would write_trylock() succeed?
183 * @lock: the rwlock in question.
184 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100185static __inline__ int arch_write_can_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700187 return !rw->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Thomas Gleixnere5931942009-12-03 20:08:46 +0100190#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
191#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
Robin Holtf5f7eac2009-04-02 16:59:46 -0700192
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100193#define arch_spin_relax(lock) cpu_relax()
194#define arch_read_relax(lock) cpu_relax()
195#define arch_write_relax(lock) cpu_relax()
Martin Schwidefskyef6edc92006-09-30 23:27:43 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#endif /* __ASM_SPINLOCK_H */