1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#ifndef _ASM_TILE_SPINLOCK_64_H
19#define _ASM_TILE_SPINLOCK_64_H
20
21#include <linux/compiler.h>
22
23
24#define __ARCH_SPIN_CURRENT_SHIFT 17
25#define __ARCH_SPIN_NEXT_MASK 0x7fff
26#define __ARCH_SPIN_NEXT_OVERFLOW 0x8000
27
28
29
30
31
32static inline u32 arch_spin_current(u32 val)
33{
34 return val >> __ARCH_SPIN_CURRENT_SHIFT;
35}
36
37
38
39
40
41static inline u32 arch_spin_next(u32 val)
42{
43 return val & __ARCH_SPIN_NEXT_MASK;
44}
45
46
47static inline int arch_spin_is_locked(arch_spinlock_t *lock)
48{
49
50 u32 val = READ_ONCE(lock->lock);
51 return arch_spin_current(val) != arch_spin_next(val);
52}
53
54
55static inline void arch_spin_unlock(arch_spinlock_t *lock)
56{
57 wmb();
58 __insn_fetchadd4(&lock->lock, 1U << __ARCH_SPIN_CURRENT_SHIFT);
59}
60
61void arch_spin_unlock_wait(arch_spinlock_t *lock);
62
63void arch_spin_lock_slow(arch_spinlock_t *lock, u32 val);
64
65
66
67
68
69static inline void arch_spin_lock(arch_spinlock_t *lock)
70{
71 u32 val = __insn_fetchadd4(&lock->lock, 1);
72 u32 ticket = val & (__ARCH_SPIN_NEXT_MASK | __ARCH_SPIN_NEXT_OVERFLOW);
73 if (unlikely(arch_spin_current(val) != ticket))
74 arch_spin_lock_slow(lock, ticket);
75}
76
77
78int arch_spin_trylock(arch_spinlock_t *lock);
79
80
81#define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
82
83
84
85
86
87
88
89
90
91#define __WRITE_LOCK_BIT (1 << 31)
92
93static inline int arch_write_val_locked(int val)
94{
95 return val < 0;
96}
97
98
99
100
101
102static inline int arch_read_can_lock(arch_rwlock_t *rw)
103{
104 return !arch_write_val_locked(rw->lock);
105}
106
107
108
109
110
111static inline int arch_write_can_lock(arch_rwlock_t *rw)
112{
113 return rw->lock == 0;
114}
115
116extern void __read_lock_failed(arch_rwlock_t *rw);
117
118static inline void arch_read_lock(arch_rwlock_t *rw)
119{
120 u32 val = __insn_fetchaddgez4(&rw->lock, 1);
121 if (unlikely(arch_write_val_locked(val)))
122 __read_lock_failed(rw);
123}
124
125extern void __write_lock_failed(arch_rwlock_t *rw, u32 val);
126
127static inline void arch_write_lock(arch_rwlock_t *rw)
128{
129 u32 val = __insn_fetchor4(&rw->lock, __WRITE_LOCK_BIT);
130 if (unlikely(val != 0))
131 __write_lock_failed(rw, val);
132}
133
134static inline void arch_read_unlock(arch_rwlock_t *rw)
135{
136 __insn_mf();
137 __insn_fetchadd4(&rw->lock, -1);
138}
139
140static inline void arch_write_unlock(arch_rwlock_t *rw)
141{
142 __insn_mf();
143 __insn_exch4(&rw->lock, 0);
144}
145
146static inline int arch_read_trylock(arch_rwlock_t *rw)
147{
148 return !arch_write_val_locked(__insn_fetchaddgez4(&rw->lock, 1));
149}
150
151static inline int arch_write_trylock(arch_rwlock_t *rw)
152{
153 u32 val = __insn_fetchor4(&rw->lock, __WRITE_LOCK_BIT);
154 if (likely(val == 0))
155 return 1;
156 if (!arch_write_val_locked(val))
157 __insn_fetchand4(&rw->lock, ~__WRITE_LOCK_BIT);
158 return 0;
159}
160
161#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
162#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
163
164#endif
165