blob: 64f2992e439fcd81811e7a44d322bdc5aafd9ff1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ASM_SPINLOCK_H
2#define __ASM_SPINLOCK_H
3
Rolf Eike Beer1cab4202012-05-10 22:57:11 +02004#include <asm/barrier.h>
5#include <asm/ldcw.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -07006#include <asm/processor.h>
7#include <asm/spinlock_types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01009static inline int arch_spin_is_locked(arch_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010{
11 volatile unsigned int *a = __ldcw_align(x);
12 return *a == 0;
13}
14
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010015#define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0)
16#define arch_spin_unlock_wait(x) \
17 do { cpu_relax(); } while (arch_spin_is_locked(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010019static inline void arch_spin_lock_flags(arch_spinlock_t *x,
James Bottomley08dc2ca2005-11-17 16:35:09 -050020 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
22 volatile unsigned int *a;
23
24 mb();
25 a = __ldcw_align(x);
26 while (__ldcw(a) == 0)
James Bottomley08dc2ca2005-11-17 16:35:09 -050027 while (*a == 0)
28 if (flags & PSW_SM_I) {
29 local_irq_enable();
30 cpu_relax();
31 local_irq_disable();
32 } else
33 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 mb();
35}
36
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010037static inline void arch_spin_unlock(arch_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 volatile unsigned int *a;
40 mb();
41 a = __ldcw_align(x);
42 *a = 1;
43 mb();
44}
45
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010046static inline int arch_spin_trylock(arch_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 volatile unsigned int *a;
49 int ret;
50
51 mb();
52 a = __ldcw_align(x);
53 ret = __ldcw(a) != 0;
54 mb();
55
56 return ret;
57}
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/*
Matthew Wilcox6e071852006-09-02 07:54:58 -060060 * Read-write spinlocks, allowing multiple readers but only one writer.
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -060061 * Linux rwlocks are unfair to writers; they can be starved for an indefinite
62 * time by readers. With care, they can also be taken in interrupt context.
63 *
64 * In the PA-RISC implementation, we have a spinlock and a counter.
65 * Readers use the lock to serialise their access to the counter (which
66 * records how many readers currently hold the lock).
67 * Writers hold the spinlock, preventing any readers or other writers from
68 * grabbing the rwlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -060071/* Note that we have to ensure interrupts are disabled in case we're
72 * interrupted by some other code that wants to grab the same read lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +010073static __inline__ void arch_read_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Matthew Wilcox6e071852006-09-02 07:54:58 -060075 unsigned long flags;
76 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010077 arch_spin_lock_flags(&rw->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 rw->counter++;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010079 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -060080 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -060083/* Note that we have to ensure interrupts are disabled in case we're
84 * interrupted by some other code that wants to grab the same read lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +010085static __inline__ void arch_read_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Matthew Wilcox6e071852006-09-02 07:54:58 -060087 unsigned long flags;
88 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010089 arch_spin_lock_flags(&rw->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 rw->counter--;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010091 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -060092 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -060095/* Note that we have to ensure interrupts are disabled in case we're
96 * interrupted by some other code that wants to grab the same read lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +010097static __inline__ int arch_read_trylock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Matthew Wilcox6e071852006-09-02 07:54:58 -060099 unsigned long flags;
100 retry:
101 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100102 if (arch_spin_trylock(&rw->lock)) {
Matthew Wilcox6e071852006-09-02 07:54:58 -0600103 rw->counter++;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100104 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -0600105 local_irq_restore(flags);
106 return 1;
107 }
108
109 local_irq_restore(flags);
110 /* If write-locked, we fail to acquire the lock */
111 if (rw->counter < 0)
112 return 0;
113
114 /* Wait until we have a realistic chance at the lock */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100115 while (arch_spin_is_locked(&rw->lock) && rw->counter >= 0)
Matthew Wilcox6e071852006-09-02 07:54:58 -0600116 cpu_relax();
117
118 goto retry;
119}
120
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -0600121/* Note that we have to ensure interrupts are disabled in case we're
122 * interrupted by some other code that wants to read_trylock() this lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100123static __inline__ void arch_write_lock(arch_rwlock_t *rw)
Matthew Wilcox6e071852006-09-02 07:54:58 -0600124{
125 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126retry:
Matthew Wilcox6e071852006-09-02 07:54:58 -0600127 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100128 arch_spin_lock_flags(&rw->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Matthew Wilcox6e071852006-09-02 07:54:58 -0600130 if (rw->counter != 0) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100131 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -0600132 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700134 while (rw->counter != 0)
135 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 goto retry;
138 }
139
Matthew Wilcox6e071852006-09-02 07:54:58 -0600140 rw->counter = -1; /* mark as write-locked */
141 mb();
142 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Thomas Gleixnere5931942009-12-03 20:08:46 +0100145static __inline__ void arch_write_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 rw->counter = 0;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100148 arch_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -0600151/* Note that we have to ensure interrupts are disabled in case we're
152 * interrupted by some other code that wants to read_trylock() this lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100153static __inline__ int arch_write_trylock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Matthew Wilcox6e071852006-09-02 07:54:58 -0600155 unsigned long flags;
156 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Matthew Wilcox6e071852006-09-02 07:54:58 -0600158 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100159 if (arch_spin_trylock(&rw->lock)) {
Matthew Wilcox6e071852006-09-02 07:54:58 -0600160 if (rw->counter == 0) {
161 rw->counter = -1;
162 result = 1;
163 } else {
164 /* Read-locked. Oh well. */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100165 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -0600166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Matthew Wilcox6e071852006-09-02 07:54:58 -0600168 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Matthew Wilcox6e071852006-09-02 07:54:58 -0600170 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700173/*
174 * read_can_lock - would read_trylock() succeed?
175 * @lock: the rwlock in question.
176 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100177static __inline__ int arch_read_can_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700179 return rw->counter >= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700182/*
183 * write_can_lock - would write_trylock() succeed?
184 * @lock: the rwlock in question.
185 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100186static __inline__ int arch_write_can_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700188 return !rw->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Thomas Gleixnere5931942009-12-03 20:08:46 +0100191#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
192#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
Robin Holtf5f7eac2009-04-02 16:59:46 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#endif /* __ASM_SPINLOCK_H */