blob: 0f08ba5cfb3309d01f59cd891acae724f596baa6 [file] [log] [blame]
Catalin Marinas08e875c2012-03-05 11:49:30 +00001/*
2 * Copyright (C) 2012 ARM Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#ifndef __ASM_SPINLOCK_H
17#define __ASM_SPINLOCK_H
18
19#include <asm/spinlock_types.h>
20#include <asm/processor.h>
21
22/*
23 * Spinlock implementation.
24 *
Catalin Marinas08e875c2012-03-05 11:49:30 +000025 * The memory barriers are implicit with the load-acquire and store-release
26 * instructions.
Catalin Marinas08e875c2012-03-05 11:49:30 +000027 */
28
Catalin Marinas08e875c2012-03-05 11:49:30 +000029#define arch_spin_unlock_wait(lock) \
30 do { while (arch_spin_is_locked(lock)) cpu_relax(); } while (0)
31
32#define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
33
34static inline void arch_spin_lock(arch_spinlock_t *lock)
35{
36 unsigned int tmp;
Will Deacon52ea2a52013-10-09 15:54:26 +010037 arch_spinlock_t lockval, newval;
Catalin Marinas08e875c2012-03-05 11:49:30 +000038
39 asm volatile(
Will Deacon52ea2a52013-10-09 15:54:26 +010040 /* Atomically increment the next ticket. */
41" prfm pstl1strm, %3\n"
42"1: ldaxr %w0, %3\n"
43" add %w1, %w0, %w5\n"
44" stxr %w2, %w1, %3\n"
45" cbnz %w2, 1b\n"
46 /* Did we get the lock? */
47" eor %w1, %w0, %w0, ror #16\n"
48" cbz %w1, 3f\n"
49 /*
50 * No: spin on the owner. Send a local event to avoid missing an
51 * unlock before the exclusive load.
52 */
53" sevl\n"
54"2: wfe\n"
55" ldaxrh %w2, %4\n"
56" eor %w1, %w2, %w0, lsr #16\n"
57" cbnz %w1, 2b\n"
58 /* We got the lock. Critical section starts here. */
59"3:"
60 : "=&r" (lockval), "=&r" (newval), "=&r" (tmp), "+Q" (*lock)
61 : "Q" (lock->owner), "I" (1 << TICKET_SHIFT)
62 : "memory");
Catalin Marinas08e875c2012-03-05 11:49:30 +000063}
64
65static inline int arch_spin_trylock(arch_spinlock_t *lock)
66{
67 unsigned int tmp;
Will Deacon52ea2a52013-10-09 15:54:26 +010068 arch_spinlock_t lockval;
Catalin Marinas08e875c2012-03-05 11:49:30 +000069
70 asm volatile(
Will Deacon52ea2a52013-10-09 15:54:26 +010071" prfm pstl1strm, %2\n"
72"1: ldaxr %w0, %2\n"
73" eor %w1, %w0, %w0, ror #16\n"
74" cbnz %w1, 2f\n"
75" add %w0, %w0, %3\n"
76" stxr %w1, %w0, %2\n"
77" cbnz %w1, 1b\n"
78"2:"
79 : "=&r" (lockval), "=&r" (tmp), "+Q" (*lock)
80 : "I" (1 << TICKET_SHIFT)
81 : "memory");
Catalin Marinas08e875c2012-03-05 11:49:30 +000082
83 return !tmp;
84}
85
86static inline void arch_spin_unlock(arch_spinlock_t *lock)
87{
88 asm volatile(
Will Deacon52ea2a52013-10-09 15:54:26 +010089" stlrh %w1, %0\n"
90 : "=Q" (lock->owner)
91 : "r" (lock->owner + 1)
92 : "memory");
Catalin Marinas08e875c2012-03-05 11:49:30 +000093}
94
Will Deacon5686b062013-10-09 15:54:27 +010095static inline int arch_spin_value_unlocked(arch_spinlock_t lock)
96{
97 return lock.owner == lock.next;
98}
99
Will Deacon52ea2a52013-10-09 15:54:26 +0100100static inline int arch_spin_is_locked(arch_spinlock_t *lock)
101{
Christian Borntraegeraf2e7aa2014-11-24 10:53:11 +0100102 return !arch_spin_value_unlocked(READ_ONCE(*lock));
Will Deacon52ea2a52013-10-09 15:54:26 +0100103}
104
105static inline int arch_spin_is_contended(arch_spinlock_t *lock)
106{
Christian Borntraegeraf2e7aa2014-11-24 10:53:11 +0100107 arch_spinlock_t lockval = READ_ONCE(*lock);
Will Deacon52ea2a52013-10-09 15:54:26 +0100108 return (lockval.next - lockval.owner) > 1;
109}
110#define arch_spin_is_contended arch_spin_is_contended
111
Catalin Marinas08e875c2012-03-05 11:49:30 +0000112/*
113 * Write lock implementation.
114 *
115 * Write locks set bit 31. Unlocking, is done by writing 0 since the lock is
116 * exclusively held.
117 *
118 * The memory barriers are implicit with the load-acquire and store-release
119 * instructions.
120 */
121
122static inline void arch_write_lock(arch_rwlock_t *rw)
123{
124 unsigned int tmp;
125
126 asm volatile(
127 " sevl\n"
128 "1: wfe\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000129 "2: ldaxr %w0, %1\n"
Catalin Marinas08e875c2012-03-05 11:49:30 +0000130 " cbnz %w0, 1b\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000131 " stxr %w0, %w2, %1\n"
Catalin Marinas08e875c2012-03-05 11:49:30 +0000132 " cbnz %w0, 2b\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000133 : "=&r" (tmp), "+Q" (rw->lock)
134 : "r" (0x80000000)
Will Deacon95c41892014-02-04 12:29:13 +0000135 : "memory");
Catalin Marinas08e875c2012-03-05 11:49:30 +0000136}
137
138static inline int arch_write_trylock(arch_rwlock_t *rw)
139{
140 unsigned int tmp;
141
142 asm volatile(
Will Deacon9511ca12015-07-22 18:25:52 +0100143 "1: ldaxr %w0, %1\n"
144 " cbnz %w0, 2f\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000145 " stxr %w0, %w2, %1\n"
Will Deacon9511ca12015-07-22 18:25:52 +0100146 " cbnz %w0, 1b\n"
147 "2:\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000148 : "=&r" (tmp), "+Q" (rw->lock)
149 : "r" (0x80000000)
Will Deacon95c41892014-02-04 12:29:13 +0000150 : "memory");
Catalin Marinas08e875c2012-03-05 11:49:30 +0000151
152 return !tmp;
153}
154
155static inline void arch_write_unlock(arch_rwlock_t *rw)
156{
157 asm volatile(
Will Deacon3a0310e2013-02-04 12:12:33 +0000158 " stlr %w1, %0\n"
159 : "=Q" (rw->lock) : "r" (0) : "memory");
Catalin Marinas08e875c2012-03-05 11:49:30 +0000160}
161
162/* write_can_lock - would write_trylock() succeed? */
163#define arch_write_can_lock(x) ((x)->lock == 0)
164
165/*
166 * Read lock implementation.
167 *
168 * It exclusively loads the lock value, increments it and stores the new value
169 * back if positive and the CPU still exclusively owns the location. If the
170 * value is negative, the lock is already held.
171 *
172 * During unlocking there may be multiple active read locks but no write lock.
173 *
174 * The memory barriers are implicit with the load-acquire and store-release
175 * instructions.
176 */
177static inline void arch_read_lock(arch_rwlock_t *rw)
178{
179 unsigned int tmp, tmp2;
180
181 asm volatile(
182 " sevl\n"
183 "1: wfe\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000184 "2: ldaxr %w0, %2\n"
Catalin Marinas08e875c2012-03-05 11:49:30 +0000185 " add %w0, %w0, #1\n"
186 " tbnz %w0, #31, 1b\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000187 " stxr %w1, %w0, %2\n"
Catalin Marinas08e875c2012-03-05 11:49:30 +0000188 " cbnz %w1, 2b\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000189 : "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
190 :
Will Deacon95c41892014-02-04 12:29:13 +0000191 : "memory");
Catalin Marinas08e875c2012-03-05 11:49:30 +0000192}
193
194static inline void arch_read_unlock(arch_rwlock_t *rw)
195{
196 unsigned int tmp, tmp2;
197
198 asm volatile(
Will Deacon3a0310e2013-02-04 12:12:33 +0000199 "1: ldxr %w0, %2\n"
Catalin Marinas08e875c2012-03-05 11:49:30 +0000200 " sub %w0, %w0, #1\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000201 " stlxr %w1, %w0, %2\n"
Catalin Marinas08e875c2012-03-05 11:49:30 +0000202 " cbnz %w1, 1b\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000203 : "=&r" (tmp), "=&r" (tmp2), "+Q" (rw->lock)
204 :
Will Deacon95c41892014-02-04 12:29:13 +0000205 : "memory");
Catalin Marinas08e875c2012-03-05 11:49:30 +0000206}
207
208static inline int arch_read_trylock(arch_rwlock_t *rw)
209{
210 unsigned int tmp, tmp2 = 1;
211
212 asm volatile(
Will Deacon9511ca12015-07-22 18:25:52 +0100213 "1: ldaxr %w0, %2\n"
Catalin Marinas08e875c2012-03-05 11:49:30 +0000214 " add %w0, %w0, #1\n"
Will Deacon9511ca12015-07-22 18:25:52 +0100215 " tbnz %w0, #31, 2f\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000216 " stxr %w1, %w0, %2\n"
Will Deacon9511ca12015-07-22 18:25:52 +0100217 " cbnz %w1, 1b\n"
218 "2:\n"
Will Deacon3a0310e2013-02-04 12:12:33 +0000219 : "=&r" (tmp), "+r" (tmp2), "+Q" (rw->lock)
220 :
Will Deacon95c41892014-02-04 12:29:13 +0000221 : "memory");
Catalin Marinas08e875c2012-03-05 11:49:30 +0000222
223 return !tmp2;
224}
225
226/* read_can_lock - would read_trylock() succeed? */
227#define arch_read_can_lock(x) ((x)->lock < 0x80000000)
228
229#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
230#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
231
232#define arch_spin_relax(lock) cpu_relax()
233#define arch_read_relax(lock) cpu_relax()
234#define arch_write_relax(lock) cpu_relax()
235
236#endif /* __ASM_SPINLOCK_H */