Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / xtensa / include / asm / spinlock.h
blobc6e1290dcbb7cbb8034437bc47e95d653b968bfb
1 /*
2 * include/asm-xtensa/spinlock.h
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
6 * for more details.
8 * Copyright (C) 2001 - 2005 Tensilica Inc.
9 */
11 #ifndef _XTENSA_SPINLOCK_H
12 #define _XTENSA_SPINLOCK_H
14 #include <asm/barrier.h>
15 #include <asm/processor.h>
18 * spinlock
20 * There is at most one owner of a spinlock. There are not different
21 * types of spinlock owners like there are for rwlocks (see below).
23 * When trying to obtain a spinlock, the function "spins" forever, or busy-
24 * waits, until the lock is obtained. When spinning, presumably some other
25 * owner will soon give up the spinlock making it available to others. Use
26 * the trylock functions to avoid spinning forever.
28 * possible values:
30 * 0 nobody owns the spinlock
31 * 1 somebody owns the spinlock
34 #define arch_spin_is_locked(x) ((x)->slock != 0)
36 static inline void arch_spin_lock(arch_spinlock_t *lock)
38 unsigned long tmp;
40 __asm__ __volatile__(
41 " movi %0, 0\n"
42 " wsr %0, scompare1\n"
43 "1: movi %0, 1\n"
44 " s32c1i %0, %1, 0\n"
45 " bnez %0, 1b\n"
46 : "=&a" (tmp)
47 : "a" (&lock->slock)
48 : "memory");
51 /* Returns 1 if the lock is obtained, 0 otherwise. */
53 static inline int arch_spin_trylock(arch_spinlock_t *lock)
55 unsigned long tmp;
57 __asm__ __volatile__(
58 " movi %0, 0\n"
59 " wsr %0, scompare1\n"
60 " movi %0, 1\n"
61 " s32c1i %0, %1, 0\n"
62 : "=&a" (tmp)
63 : "a" (&lock->slock)
64 : "memory");
66 return tmp == 0 ? 1 : 0;
69 static inline void arch_spin_unlock(arch_spinlock_t *lock)
71 unsigned long tmp;
73 __asm__ __volatile__(
74 " movi %0, 0\n"
75 " s32ri %0, %1, 0\n"
76 : "=&a" (tmp)
77 : "a" (&lock->slock)
78 : "memory");
82 * rwlock
84 * Read-write locks are really a more flexible spinlock. They allow
85 * multiple readers but only one writer. Write ownership is exclusive
86 * (i.e., all other readers and writers are blocked from ownership while
87 * there is a write owner). These rwlocks are unfair to writers. Writers
88 * can be starved for an indefinite time by readers.
90 * possible values:
92 * 0 nobody owns the rwlock
93 * >0 one or more readers own the rwlock
94 * (the positive value is the actual number of readers)
95 * 0x80000000 one writer owns the rwlock, no other writers, no readers
98 static inline void arch_write_lock(arch_rwlock_t *rw)
100 unsigned long tmp;
102 __asm__ __volatile__(
103 " movi %0, 0\n"
104 " wsr %0, scompare1\n"
105 "1: movi %0, 1\n"
106 " slli %0, %0, 31\n"
107 " s32c1i %0, %1, 0\n"
108 " bnez %0, 1b\n"
109 : "=&a" (tmp)
110 : "a" (&rw->lock)
111 : "memory");
114 /* Returns 1 if the lock is obtained, 0 otherwise. */
116 static inline int arch_write_trylock(arch_rwlock_t *rw)
118 unsigned long tmp;
120 __asm__ __volatile__(
121 " movi %0, 0\n"
122 " wsr %0, scompare1\n"
123 " movi %0, 1\n"
124 " slli %0, %0, 31\n"
125 " s32c1i %0, %1, 0\n"
126 : "=&a" (tmp)
127 : "a" (&rw->lock)
128 : "memory");
130 return tmp == 0 ? 1 : 0;
133 static inline void arch_write_unlock(arch_rwlock_t *rw)
135 unsigned long tmp;
137 __asm__ __volatile__(
138 " movi %0, 0\n"
139 " s32ri %0, %1, 0\n"
140 : "=&a" (tmp)
141 : "a" (&rw->lock)
142 : "memory");
145 static inline void arch_read_lock(arch_rwlock_t *rw)
147 unsigned long tmp;
148 unsigned long result;
150 __asm__ __volatile__(
151 "1: l32i %1, %2, 0\n"
152 " bltz %1, 1b\n"
153 " wsr %1, scompare1\n"
154 " addi %0, %1, 1\n"
155 " s32c1i %0, %2, 0\n"
156 " bne %0, %1, 1b\n"
157 : "=&a" (result), "=&a" (tmp)
158 : "a" (&rw->lock)
159 : "memory");
162 /* Returns 1 if the lock is obtained, 0 otherwise. */
164 static inline int arch_read_trylock(arch_rwlock_t *rw)
166 unsigned long result;
167 unsigned long tmp;
169 __asm__ __volatile__(
170 " l32i %1, %2, 0\n"
171 " addi %0, %1, 1\n"
172 " bltz %0, 1f\n"
173 " wsr %1, scompare1\n"
174 " s32c1i %0, %2, 0\n"
175 " sub %0, %0, %1\n"
176 "1:\n"
177 : "=&a" (result), "=&a" (tmp)
178 : "a" (&rw->lock)
179 : "memory");
181 return result == 0;
184 static inline void arch_read_unlock(arch_rwlock_t *rw)
186 unsigned long tmp1, tmp2;
188 __asm__ __volatile__(
189 "1: l32i %1, %2, 0\n"
190 " addi %0, %1, -1\n"
191 " wsr %1, scompare1\n"
192 " s32c1i %0, %2, 0\n"
193 " bne %0, %1, 1b\n"
194 : "=&a" (tmp1), "=&a" (tmp2)
195 : "a" (&rw->lock)
196 : "memory");
199 #endif /* _XTENSA_SPINLOCK_H */