]> git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blame - arch/parisc/include/asm/spinlock.h
UBUNTU: Ubuntu-5.0.0-29.31
[mirror_ubuntu-disco-kernel.git] / arch / parisc / include / asm / spinlock.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef __ASM_SPINLOCK_H
3#define __ASM_SPINLOCK_H
4
1cab4201
REB
5#include <asm/barrier.h>
6#include <asm/ldcw.h>
fb1c8f93
IM
7#include <asm/processor.h>
8#include <asm/spinlock_types.h>
1da177e4 9
0199c4e6 10static inline int arch_spin_is_locked(arch_spinlock_t *x)
1da177e4
LT
11{
12 volatile unsigned int *a = __ldcw_align(x);
13 return *a == 0;
14}
15
0199c4e6 16#define arch_spin_lock(lock) arch_spin_lock_flags(lock, 0)
726328d9 17
0199c4e6 18static inline void arch_spin_lock_flags(arch_spinlock_t *x,
08dc2ca6 19 unsigned long flags)
1da177e4
LT
20{
21 volatile unsigned int *a;
22
1da177e4
LT
23 a = __ldcw_align(x);
24 while (__ldcw(a) == 0)
08dc2ca6
JB
25 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();
1da177e4 32}
a4c1887d 33#define arch_spin_lock_flags arch_spin_lock_flags
1da177e4 34
0199c4e6 35static inline void arch_spin_unlock(arch_spinlock_t *x)
1da177e4
LT
36{
37 volatile unsigned int *a;
3b885ac1 38
1da177e4 39 a = __ldcw_align(x);
86d4d068
JDA
40 mb();
41 *a = 1;
1da177e4
LT
42}
43
0199c4e6 44static inline int arch_spin_trylock(arch_spinlock_t *x)
1da177e4
LT
45{
46 volatile unsigned int *a;
47 int ret;
48
1da177e4
LT
49 a = __ldcw_align(x);
50 ret = __ldcw(a) != 0;
1da177e4
LT
51
52 return ret;
53}
1da177e4
LT
54
55/*
6e071852 56 * Read-write spinlocks, allowing multiple readers but only one writer.
65ee8f0a
MW
57 * 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.
1da177e4 65 */
1da177e4 66
65ee8f0a
MW
67/* 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 */
e5931943 69static __inline__ void arch_read_lock(arch_rwlock_t *rw)
1da177e4 70{
6e071852
MW
71 unsigned long flags;
72 local_irq_save(flags);
0199c4e6 73 arch_spin_lock_flags(&rw->lock, flags);
1da177e4 74 rw->counter++;
0199c4e6 75 arch_spin_unlock(&rw->lock);
6e071852 76 local_irq_restore(flags);
1da177e4 77}
1da177e4 78
65ee8f0a
MW
79/* 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 */
e5931943 81static __inline__ void arch_read_unlock(arch_rwlock_t *rw)
1da177e4 82{
6e071852
MW
83 unsigned long flags;
84 local_irq_save(flags);
0199c4e6 85 arch_spin_lock_flags(&rw->lock, flags);
1da177e4 86 rw->counter--;
0199c4e6 87 arch_spin_unlock(&rw->lock);
6e071852 88 local_irq_restore(flags);
1da177e4
LT
89}
90
65ee8f0a
MW
91/* 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 */
e5931943 93static __inline__ int arch_read_trylock(arch_rwlock_t *rw)
6e071852
MW
94{
95 unsigned long flags;
96 retry:
97 local_irq_save(flags);
0199c4e6 98 if (arch_spin_trylock(&rw->lock)) {
6e071852 99 rw->counter++;
0199c4e6 100 arch_spin_unlock(&rw->lock);
6e071852
MW
101 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 */
0199c4e6 111 while (arch_spin_is_locked(&rw->lock) && rw->counter >= 0)
6e071852
MW
112 cpu_relax();
113
114 goto retry;
115}
1da177e4 116
65ee8f0a
MW
117/* 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 */
e5931943 119static __inline__ void arch_write_lock(arch_rwlock_t *rw)
1da177e4 120{
6e071852 121 unsigned long flags;
1da177e4 122retry:
6e071852 123 local_irq_save(flags);
0199c4e6 124 arch_spin_lock_flags(&rw->lock, flags);
1da177e4 125
6e071852 126 if (rw->counter != 0) {
0199c4e6 127 arch_spin_unlock(&rw->lock);
6e071852 128 local_irq_restore(flags);
1da177e4 129
fb1c8f93
IM
130 while (rw->counter != 0)
131 cpu_relax();
1da177e4
LT
132
133 goto retry;
134 }
135
6e071852
MW
136 rw->counter = -1; /* mark as write-locked */
137 mb();
138 local_irq_restore(flags);
1da177e4 139}
1da177e4 140
e5931943 141static __inline__ void arch_write_unlock(arch_rwlock_t *rw)
1da177e4
LT
142{
143 rw->counter = 0;
0199c4e6 144 arch_spin_unlock(&rw->lock);
1da177e4
LT
145}
146
65ee8f0a
MW
147/* 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 */
e5931943 149static __inline__ int arch_write_trylock(arch_rwlock_t *rw)
1da177e4 150{
6e071852
MW
151 unsigned long flags;
152 int result = 0;
153
154 local_irq_save(flags);
0199c4e6 155 if (arch_spin_trylock(&rw->lock)) {
6e071852
MW
156 if (rw->counter == 0) {
157 rw->counter = -1;
158 result = 1;
159 } else {
160 /* Read-locked. Oh well. */
0199c4e6 161 arch_spin_unlock(&rw->lock);
6e071852 162 }
1da177e4 163 }
6e071852 164 local_irq_restore(flags);
1da177e4 165
6e071852 166 return result;
1da177e4 167}
1da177e4 168
1da177e4 169#endif /* __ASM_SPINLOCK_H */