blob: 8a63515f03bfe3931930d094a479060815832fe6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __ASM_SPINLOCK_H
3#define __ASM_SPINLOCK_H
4
Rolf Eike Beer1cab4202012-05-10 22:57:11 +02005#include <asm/barrier.h>
6#include <asm/ldcw.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -07007#include <asm/processor.h>
8#include <asm/spinlock_types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010010static inline int arch_spin_is_locked(arch_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011{
12 volatile unsigned int *a = __ldcw_align(x);
13 return *a == 0;
14}
15
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010016#define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0)
Peter Zijlstra726328d2016-05-26 10:35:03 +020017
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 a = __ldcw_align(x);
24 while (__ldcw(a) == 0)
James Bottomley08dc2ca2005-11-17 16:35:09 -050025 while (*a == 0)
26 if (flags & PSW_SM_I) {
27 local_irq_enable();
28 cpu_relax();
29 local_irq_disable();
30 } else
31 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
Will Deacona4c18872017-10-03 19:25:29 +010033#define arch_spin_lock_flags arch_spin_lock_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010035static inline void arch_spin_unlock(arch_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 volatile unsigned int *a;
John David Anglin3b885ac2018-08-12 16:31:17 -040038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 a = __ldcw_align(x);
John David Anglin86d4d062018-11-06 12:00:01 +010040 mb();
41 *a = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010044static inline int arch_spin_trylock(arch_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 volatile unsigned int *a;
47 int ret;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 a = __ldcw_align(x);
50 ret = __ldcw(a) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 return ret;
53}
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
Matthew Wilcox6e071852006-09-02 07:54:58 -060056 * Read-write spinlocks, allowing multiple readers but only one writer.
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -060057 * Linux rwlocks are unfair to writers; they can be starved for an indefinite
58 * time by readers. With care, they can also be taken in interrupt context.
59 *
60 * In the PA-RISC implementation, we have a spinlock and a counter.
61 * Readers use the lock to serialise their access to the counter (which
62 * records how many readers currently hold the lock).
63 * Writers hold the spinlock, preventing any readers or other writers from
64 * grabbing the rwlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -060067/* Note that we have to ensure interrupts are disabled in case we're
68 * interrupted by some other code that wants to grab the same read lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +010069static __inline__ void arch_read_lock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Matthew Wilcox6e071852006-09-02 07:54:58 -060071 unsigned long flags;
72 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010073 arch_spin_lock_flags(&rw->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 rw->counter++;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010075 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -060076 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -060079/* Note that we have to ensure interrupts are disabled in case we're
80 * interrupted by some other code that wants to grab the same read lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +010081static __inline__ void arch_read_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Matthew Wilcox6e071852006-09-02 07:54:58 -060083 unsigned long flags;
84 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010085 arch_spin_lock_flags(&rw->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 rw->counter--;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010087 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -060088 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -060091/* Note that we have to ensure interrupts are disabled in case we're
92 * interrupted by some other code that wants to grab the same read lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +010093static __inline__ int arch_read_trylock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Matthew Wilcox6e071852006-09-02 07:54:58 -060095 unsigned long flags;
96 retry:
97 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010098 if (arch_spin_trylock(&rw->lock)) {
Matthew Wilcox6e071852006-09-02 07:54:58 -060099 rw->counter++;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100100 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -0600101 local_irq_restore(flags);
102 return 1;
103 }
104
105 local_irq_restore(flags);
106 /* If write-locked, we fail to acquire the lock */
107 if (rw->counter < 0)
108 return 0;
109
110 /* Wait until we have a realistic chance at the lock */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100111 while (arch_spin_is_locked(&rw->lock) && rw->counter >= 0)
Matthew Wilcox6e071852006-09-02 07:54:58 -0600112 cpu_relax();
113
114 goto retry;
115}
116
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -0600117/* Note that we have to ensure interrupts are disabled in case we're
118 * interrupted by some other code that wants to read_trylock() this lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100119static __inline__ void arch_write_lock(arch_rwlock_t *rw)
Matthew Wilcox6e071852006-09-02 07:54:58 -0600120{
121 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122retry:
Matthew Wilcox6e071852006-09-02 07:54:58 -0600123 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100124 arch_spin_lock_flags(&rw->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Matthew Wilcox6e071852006-09-02 07:54:58 -0600126 if (rw->counter != 0) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100127 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -0600128 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700130 while (rw->counter != 0)
131 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 goto retry;
134 }
135
Matthew Wilcox6e071852006-09-02 07:54:58 -0600136 rw->counter = -1; /* mark as write-locked */
137 mb();
138 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Thomas Gleixnere5931942009-12-03 20:08:46 +0100141static __inline__ void arch_write_unlock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 rw->counter = 0;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100144 arch_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Matthew Wilcox65ee8f0a2006-09-08 05:43:44 -0600147/* Note that we have to ensure interrupts are disabled in case we're
148 * interrupted by some other code that wants to read_trylock() this lock */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100149static __inline__ int arch_write_trylock(arch_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Matthew Wilcox6e071852006-09-02 07:54:58 -0600151 unsigned long flags;
152 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Matthew Wilcox6e071852006-09-02 07:54:58 -0600154 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100155 if (arch_spin_trylock(&rw->lock)) {
Matthew Wilcox6e071852006-09-02 07:54:58 -0600156 if (rw->counter == 0) {
157 rw->counter = -1;
158 result = 1;
159 } else {
160 /* Read-locked. Oh well. */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100161 arch_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -0600162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Matthew Wilcox6e071852006-09-02 07:54:58 -0600164 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Matthew Wilcox6e071852006-09-02 07:54:58 -0600166 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#endif /* __ASM_SPINLOCK_H */