blob: 8959da245cfbb751d77b2186870a98ca1816301a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Ralf Baechle102fa152007-02-16 17:18:50 +00006 * Copyright (c) 1994 - 1997, 99, 2000, 06, 07 Ralf Baechle (ralf@linux-mips.org)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (c) 1999, 2000 Silicon Graphics, Inc.
8 */
9#ifndef _ASM_BITOPS_H
10#define _ASM_BITOPS_H
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/compiler.h>
Ralf Baechle4ffd8b32006-11-30 01:14:50 +000013#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/types.h>
Ralf Baechle0004a9d2006-10-31 03:45:07 +000015#include <asm/barrier.h>
Ralf Baechleec917c2c2005-10-07 16:58:15 +010016#include <asm/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/byteorder.h> /* sigh ... */
18#include <asm/cpu-features.h>
Ralf Baechle4ffd8b32006-11-30 01:14:50 +000019#include <asm/sgidefs.h>
20#include <asm/war.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#if (_MIPS_SZLONG == 32)
23#define SZLONG_LOG 5
24#define SZLONG_MASK 31UL
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000025#define __LL "ll "
26#define __SC "sc "
Ralf Baechle102fa152007-02-16 17:18:50 +000027#define __INS "ins "
28#define __EXT "ext "
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#elif (_MIPS_SZLONG == 64)
30#define SZLONG_LOG 6
31#define SZLONG_MASK 63UL
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000032#define __LL "lld "
33#define __SC "scd "
Ralf Baechle102fa152007-02-16 17:18:50 +000034#define __INS "dins "
35#define __EXT "dext "
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#endif
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * clear_bit() doesn't provide any barrier for the compiler.
40 */
41#define smp_mb__before_clear_bit() smp_mb()
42#define smp_mb__after_clear_bit() smp_mb()
43
44/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * set_bit - Atomically set a bit in memory
46 * @nr: the bit to set
47 * @addr: the address to start counting from
48 *
49 * This function is atomic and may not be reordered. See __set_bit()
50 * if you do not require the atomic guarantees.
51 * Note that @nr may be almost arbitrarily large; this function is not
52 * restricted to acting on a single-word quantity.
53 */
54static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
55{
56 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleb9611532007-03-05 00:56:15 +000057 unsigned short bit = nr & SZLONG_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 unsigned long temp;
59
60 if (cpu_has_llsc && R10000_LLSC_WAR) {
61 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +000062 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 "1: " __LL "%0, %1 # set_bit \n"
64 " or %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000065 " " __SC "%0, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000067 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 : "=&r" (temp), "=m" (*m)
Ralf Baechleb9611532007-03-05 00:56:15 +000069 : "ir" (1UL << bit), "m" (*m));
Ralf Baechle102fa152007-02-16 17:18:50 +000070#ifdef CONFIG_CPU_MIPSR2
Ralf Baechleb9611532007-03-05 00:56:15 +000071 } else if (__builtin_constant_p(bit)) {
Ralf Baechle102fa152007-02-16 17:18:50 +000072 __asm__ __volatile__(
73 "1: " __LL "%0, %1 # set_bit \n"
74 " " __INS "%0, %4, %2, 1 \n"
75 " " __SC "%0, %1 \n"
76 " beqz %0, 2f \n"
77 " .subsection 2 \n"
78 "2: b 1b \n"
79 " .previous \n"
80 : "=&r" (temp), "=m" (*m)
Ralf Baechleb9611532007-03-05 00:56:15 +000081 : "ir" (bit), "m" (*m), "r" (~0));
Ralf Baechle102fa152007-02-16 17:18:50 +000082#endif /* CONFIG_CPU_MIPSR2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 } else if (cpu_has_llsc) {
84 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +000085 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 "1: " __LL "%0, %1 # set_bit \n"
87 " or %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000088 " " __SC "%0, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +010089 " beqz %0, 2f \n"
90 " .subsection 2 \n"
91 "2: b 1b \n"
92 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000093 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 : "=&r" (temp), "=m" (*m)
Ralf Baechleb9611532007-03-05 00:56:15 +000095 : "ir" (1UL << bit), "m" (*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 } else {
97 volatile unsigned long *a = addr;
98 unsigned long mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +000099 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 a += nr >> SZLONG_LOG;
Ralf Baechleb9611532007-03-05 00:56:15 +0000102 mask = 1UL << bit;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000103 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 *a |= mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000105 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107}
108
109/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * clear_bit - Clears a bit in memory
111 * @nr: Bit to clear
112 * @addr: Address to start counting from
113 *
114 * clear_bit() is atomic and may not be reordered. However, it does
115 * not contain a memory barrier, so if it is used for locking purposes,
116 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
117 * in order to ensure changes are visible on other processors.
118 */
119static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
120{
121 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleb9611532007-03-05 00:56:15 +0000122 unsigned short bit = nr & SZLONG_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 unsigned long temp;
124
125 if (cpu_has_llsc && R10000_LLSC_WAR) {
126 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000127 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 "1: " __LL "%0, %1 # clear_bit \n"
129 " and %0, %2 \n"
130 " " __SC "%0, %1 \n"
131 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000132 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 : "=&r" (temp), "=m" (*m)
Ralf Baechleb9611532007-03-05 00:56:15 +0000134 : "ir" (~(1UL << bit)), "m" (*m));
Ralf Baechle102fa152007-02-16 17:18:50 +0000135#ifdef CONFIG_CPU_MIPSR2
Ralf Baechleb9611532007-03-05 00:56:15 +0000136 } else if (__builtin_constant_p(bit)) {
Ralf Baechle102fa152007-02-16 17:18:50 +0000137 __asm__ __volatile__(
138 "1: " __LL "%0, %1 # clear_bit \n"
139 " " __INS "%0, $0, %2, 1 \n"
140 " " __SC "%0, %1 \n"
141 " beqz %0, 2f \n"
142 " .subsection 2 \n"
143 "2: b 1b \n"
144 " .previous \n"
145 : "=&r" (temp), "=m" (*m)
Ralf Baechleb9611532007-03-05 00:56:15 +0000146 : "ir" (bit), "m" (*m));
Ralf Baechle102fa152007-02-16 17:18:50 +0000147#endif /* CONFIG_CPU_MIPSR2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 } else if (cpu_has_llsc) {
149 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000150 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 "1: " __LL "%0, %1 # clear_bit \n"
152 " and %0, %2 \n"
153 " " __SC "%0, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100154 " beqz %0, 2f \n"
155 " .subsection 2 \n"
156 "2: b 1b \n"
157 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000158 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 : "=&r" (temp), "=m" (*m)
Ralf Baechleb9611532007-03-05 00:56:15 +0000160 : "ir" (~(1UL << bit)), "m" (*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 } else {
162 volatile unsigned long *a = addr;
163 unsigned long mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000164 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 a += nr >> SZLONG_LOG;
Ralf Baechleb9611532007-03-05 00:56:15 +0000167 mask = 1UL << bit;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000168 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *a &= ~mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000170 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172}
173
174/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * change_bit - Toggle a bit in memory
176 * @nr: Bit to change
177 * @addr: Address to start counting from
178 *
179 * change_bit() is atomic and may not be reordered.
180 * Note that @nr may be almost arbitrarily large; this function is not
181 * restricted to acting on a single-word quantity.
182 */
183static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
184{
Ralf Baechleb9611532007-03-05 00:56:15 +0000185 unsigned short bit = nr & SZLONG_MASK;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (cpu_has_llsc && R10000_LLSC_WAR) {
188 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
189 unsigned long temp;
190
191 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000192 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 "1: " __LL "%0, %1 # change_bit \n"
194 " xor %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000195 " " __SC "%0, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000197 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 : "=&r" (temp), "=m" (*m)
Ralf Baechleb9611532007-03-05 00:56:15 +0000199 : "ir" (1UL << bit), "m" (*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 } else if (cpu_has_llsc) {
201 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
202 unsigned long temp;
203
204 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000205 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 "1: " __LL "%0, %1 # change_bit \n"
207 " xor %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000208 " " __SC "%0, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100209 " beqz %0, 2f \n"
210 " .subsection 2 \n"
211 "2: b 1b \n"
212 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000213 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 : "=&r" (temp), "=m" (*m)
Ralf Baechleb9611532007-03-05 00:56:15 +0000215 : "ir" (1UL << bit), "m" (*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 } else {
217 volatile unsigned long *a = addr;
218 unsigned long mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000219 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 a += nr >> SZLONG_LOG;
Ralf Baechleb9611532007-03-05 00:56:15 +0000222 mask = 1UL << bit;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000223 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 *a ^= mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000225 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227}
228
229/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * test_and_set_bit - Set a bit and return its old value
231 * @nr: Bit to set
232 * @addr: Address to count from
233 *
234 * This operation is atomic and cannot be reordered.
235 * It also implies a memory barrier.
236 */
237static inline int test_and_set_bit(unsigned long nr,
238 volatile unsigned long *addr)
239{
Ralf Baechleb9611532007-03-05 00:56:15 +0000240 unsigned short bit = nr & SZLONG_MASK;
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (cpu_has_llsc && R10000_LLSC_WAR) {
243 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
244 unsigned long temp, res;
245
246 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000247 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 "1: " __LL "%0, %1 # test_and_set_bit \n"
249 " or %2, %0, %3 \n"
250 " " __SC "%2, %1 \n"
251 " beqzl %2, 1b \n"
252 " and %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000253 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000255 : "r" (1UL << bit), "m" (*m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 : "memory");
257
258 return res != 0;
259 } else if (cpu_has_llsc) {
260 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
261 unsigned long temp, res;
262
263 __asm__ __volatile__(
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000264 " .set push \n"
265 " .set noreorder \n"
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000266 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000267 "1: " __LL "%0, %1 # test_and_set_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 " or %2, %0, %3 \n"
269 " " __SC "%2, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100270 " beqz %2, 2f \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 " and %2, %0, %3 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100272 " .subsection 2 \n"
273 "2: b 1b \n"
274 " nop \n"
275 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000276 " .set pop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000278 : "r" (1UL << bit), "m" (*m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 : "memory");
280
281 return res != 0;
282 } else {
283 volatile unsigned long *a = addr;
284 unsigned long mask;
285 int retval;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000286 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 a += nr >> SZLONG_LOG;
Ralf Baechleb9611532007-03-05 00:56:15 +0000289 mask = 1UL << bit;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000290 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 retval = (mask & *a) != 0;
292 *a |= mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000293 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 return retval;
296 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000297
298 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * test_and_clear_bit - Clear a bit and return its old value
303 * @nr: Bit to clear
304 * @addr: Address to count from
305 *
306 * This operation is atomic and cannot be reordered.
307 * It also implies a memory barrier.
308 */
309static inline int test_and_clear_bit(unsigned long nr,
310 volatile unsigned long *addr)
311{
Ralf Baechleb9611532007-03-05 00:56:15 +0000312 unsigned short bit = nr & SZLONG_MASK;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (cpu_has_llsc && R10000_LLSC_WAR) {
315 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
316 unsigned long temp, res;
317
318 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000319 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 "1: " __LL "%0, %1 # test_and_clear_bit \n"
321 " or %2, %0, %3 \n"
322 " xor %2, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000323 " " __SC "%2, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 " beqzl %2, 1b \n"
325 " and %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000326 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000328 : "r" (1UL << bit), "m" (*m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 : "memory");
330
331 return res != 0;
Ralf Baechle102fa152007-02-16 17:18:50 +0000332#ifdef CONFIG_CPU_MIPSR2
333 } else if (__builtin_constant_p(nr)) {
334 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
335 unsigned long temp, res;
336
337 __asm__ __volatile__(
338 "1: " __LL "%0, %1 # test_and_clear_bit \n"
339 " " __EXT "%2, %0, %3, 1 \n"
340 " " __INS "%0, $0, %3, 1 \n"
341 " " __SC "%0, %1 \n"
342 " beqz %0, 2f \n"
343 " .subsection 2 \n"
344 "2: b 1b \n"
345 " .previous \n"
346 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000347 : "ri" (bit), "m" (*m)
Ralf Baechle102fa152007-02-16 17:18:50 +0000348 : "memory");
349
350 return res;
351#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 } else if (cpu_has_llsc) {
353 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
354 unsigned long temp, res;
355
356 __asm__ __volatile__(
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000357 " .set push \n"
358 " .set noreorder \n"
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000359 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000360 "1: " __LL "%0, %1 # test_and_clear_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 " or %2, %0, %3 \n"
362 " xor %2, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000363 " " __SC "%2, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100364 " beqz %2, 2f \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 " and %2, %0, %3 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100366 " .subsection 2 \n"
367 "2: b 1b \n"
368 " nop \n"
369 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000370 " .set pop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000372 : "r" (1UL << bit), "m" (*m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 : "memory");
374
375 return res != 0;
376 } else {
377 volatile unsigned long *a = addr;
378 unsigned long mask;
379 int retval;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000380 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 a += nr >> SZLONG_LOG;
Ralf Baechleb9611532007-03-05 00:56:15 +0000383 mask = 1UL << bit;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000384 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 retval = (mask & *a) != 0;
386 *a &= ~mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000387 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 return retval;
390 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000391
392 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * test_and_change_bit - Change a bit and return its old value
397 * @nr: Bit to change
398 * @addr: Address to count from
399 *
400 * This operation is atomic and cannot be reordered.
401 * It also implies a memory barrier.
402 */
403static inline int test_and_change_bit(unsigned long nr,
404 volatile unsigned long *addr)
405{
Ralf Baechleb9611532007-03-05 00:56:15 +0000406 unsigned short bit = nr & SZLONG_MASK;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (cpu_has_llsc && R10000_LLSC_WAR) {
409 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
410 unsigned long temp, res;
411
412 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000413 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000414 "1: " __LL "%0, %1 # test_and_change_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 " xor %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000416 " " __SC "%2, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 " beqzl %2, 1b \n"
418 " and %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000419 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000421 : "r" (1UL << bit), "m" (*m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 : "memory");
423
424 return res != 0;
425 } else if (cpu_has_llsc) {
426 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
427 unsigned long temp, res;
428
429 __asm__ __volatile__(
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000430 " .set push \n"
431 " .set noreorder \n"
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000432 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000433 "1: " __LL "%0, %1 # test_and_change_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 " xor %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000435 " " __SC "\t%2, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100436 " beqz %2, 2f \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 " and %2, %0, %3 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100438 " .subsection 2 \n"
439 "2: b 1b \n"
440 " nop \n"
441 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000442 " .set pop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000444 : "r" (1UL << bit), "m" (*m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 : "memory");
446
447 return res != 0;
448 } else {
449 volatile unsigned long *a = addr;
450 unsigned long mask, retval;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000451 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 a += nr >> SZLONG_LOG;
Ralf Baechleb9611532007-03-05 00:56:15 +0000454 mask = 1UL << bit;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000455 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 retval = (mask & *a) != 0;
457 *a ^= mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000458 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 return retval;
461 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000462
463 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800466#include <asm-generic/bitops/non-atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Ralf Baechle65903262005-07-12 12:50:30 +0000468/*
469 * Return the bit position (0..63) of the most significant 1 bit in a word
470 * Returns -1 if no 1 bit exists
471 */
Ralf Baechleec917c2c2005-10-07 16:58:15 +0100472static inline int __ilog2(unsigned long x)
Ralf Baechle65903262005-07-12 12:50:30 +0000473{
474 int lz;
475
Ralf Baechleec917c2c2005-10-07 16:58:15 +0100476 if (sizeof(x) == 4) {
477 __asm__ (
478 " .set push \n"
479 " .set mips32 \n"
480 " clz %0, %1 \n"
481 " .set pop \n"
482 : "=r" (lz)
483 : "r" (x));
484
485 return 31 - lz;
486 }
487
488 BUG_ON(sizeof(x) != 8);
489
Ralf Baechle65903262005-07-12 12:50:30 +0000490 __asm__ (
491 " .set push \n"
492 " .set mips64 \n"
493 " dclz %0, %1 \n"
494 " .set pop \n"
495 : "=r" (lz)
496 : "r" (x));
497
498 return 63 - lz;
499}
Ralf Baechle65903262005-07-12 12:50:30 +0000500
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800501#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
502
Ralf Baechle65903262005-07-12 12:50:30 +0000503/*
504 * __ffs - find first bit in word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 * @word: The word to search
506 *
Ralf Baechle65903262005-07-12 12:50:30 +0000507 * Returns 0..SZLONG-1
508 * Undefined if no bit exists, so code should check against 0 first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
Ralf Baechle65903262005-07-12 12:50:30 +0000510static inline unsigned long __ffs(unsigned long word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Ralf Baechle65903262005-07-12 12:50:30 +0000512 return __ilog2(word & -word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515/*
Atsushi Nemotobc818242006-04-17 21:19:12 +0900516 * fls - find last bit set.
517 * @word: The word to search
518 *
519 * This is defined the same way as ffs.
520 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
521 */
522static inline int fls(int word)
523{
524 __asm__ ("clz %0, %1" : "=r" (word) : "r" (word));
525
526 return 32 - word;
527}
528
529#if defined(CONFIG_64BIT) && defined(CONFIG_CPU_MIPS64)
530static inline int fls64(__u64 word)
531{
532 __asm__ ("dclz %0, %1" : "=r" (word) : "r" (word));
533
534 return 64 - word;
535}
536#else
537#include <asm-generic/bitops/fls64.h>
538#endif
539
540/*
Ralf Baechle65903262005-07-12 12:50:30 +0000541 * ffs - find first bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * @word: The word to search
543 *
Atsushi Nemotobc818242006-04-17 21:19:12 +0900544 * This is defined the same way as
545 * the libc and compiler builtin ffs routines, therefore
546 * differs in spirit from the above ffz (man ffs).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
Atsushi Nemotobc818242006-04-17 21:19:12 +0900548static inline int ffs(int word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Ralf Baechle65903262005-07-12 12:50:30 +0000550 if (!word)
551 return 0;
552
Atsushi Nemotobc818242006-04-17 21:19:12 +0900553 return fls(word & -word);
Ralf Baechle65903262005-07-12 12:50:30 +0000554}
Ralf Baechle2caf1902006-01-30 17:14:41 +0000555
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800556#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800558#include <asm-generic/bitops/__ffs.h>
559#include <asm-generic/bitops/ffs.h>
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800560#include <asm-generic/bitops/fls.h>
Atsushi Nemotobc818242006-04-17 21:19:12 +0900561#include <asm-generic/bitops/fls64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800563#endif /*defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Atsushi Nemotobc818242006-04-17 21:19:12 +0900565#include <asm-generic/bitops/ffz.h>
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800566#include <asm-generic/bitops/find.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568#ifdef __KERNEL__
569
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800570#include <asm-generic/bitops/sched.h>
571#include <asm-generic/bitops/hweight.h>
572#include <asm-generic/bitops/ext2-non-atomic.h>
573#include <asm-generic/bitops/ext2-atomic.h>
574#include <asm-generic/bitops/minix.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576#endif /* __KERNEL__ */
577
578#endif /* _ASM_BITOPS_H */