blob: 77ed0c79830b6335dbb1c86fb4244091d63e7608 [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
Ralf Baechle49a89ef2007-10-11 23:46:15 +010022#if _MIPS_SZLONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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 "
Ralf Baechle49a89ef2007-10-11 23:46:15 +010029#elif _MIPS_SZLONG == 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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 */
Ralf Baechle17099b12007-07-14 13:24:05 +010041#define smp_mb__before_clear_bit() smp_llsc_mb()
42#define smp_mb__after_clear_bit() smp_llsc_mb()
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
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 Baechle49edd092007-03-16 16:10:36 +0000103 raw_local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 *a |= mask;
Ralf Baechle49edd092007-03-16 16:10:36 +0000105 raw_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 Baechle49edd092007-03-16 16:10:36 +0000168 raw_local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *a &= ~mask;
Ralf Baechle49edd092007-03-16 16:10:36 +0000170 raw_local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172}
173
174/*
Nick Piggin728697c2007-10-18 03:06:53 -0700175 * clear_bit_unlock - Clears a bit in memory
176 * @nr: Bit to clear
177 * @addr: Address to start counting from
178 *
179 * clear_bit() is atomic and implies release semantics before the memory
180 * operation. It can be used for an unlock.
181 */
182static inline void clear_bit_unlock(unsigned long nr, volatile unsigned long *addr)
183{
184 smp_mb__before_clear_bit();
185 clear_bit(nr, addr);
186}
187
188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * change_bit - Toggle a bit in memory
190 * @nr: Bit to change
191 * @addr: Address to start counting from
192 *
193 * change_bit() is atomic and may not be reordered.
194 * Note that @nr may be almost arbitrarily large; this function is not
195 * restricted to acting on a single-word quantity.
196 */
197static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
198{
Ralf Baechleb9611532007-03-05 00:56:15 +0000199 unsigned short bit = nr & SZLONG_MASK;
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (cpu_has_llsc && R10000_LLSC_WAR) {
202 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
203 unsigned long temp;
204
205 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000206 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 "1: " __LL "%0, %1 # change_bit \n"
208 " xor %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000209 " " __SC "%0, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000211 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 : "=&r" (temp), "=m" (*m)
Ralf Baechleb9611532007-03-05 00:56:15 +0000213 : "ir" (1UL << bit), "m" (*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 } else if (cpu_has_llsc) {
215 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
216 unsigned long temp;
217
218 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000219 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 "1: " __LL "%0, %1 # change_bit \n"
221 " xor %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000222 " " __SC "%0, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100223 " beqz %0, 2f \n"
224 " .subsection 2 \n"
225 "2: b 1b \n"
226 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000227 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 : "=&r" (temp), "=m" (*m)
Ralf Baechleb9611532007-03-05 00:56:15 +0000229 : "ir" (1UL << bit), "m" (*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 } else {
231 volatile unsigned long *a = addr;
232 unsigned long mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000233 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 a += nr >> SZLONG_LOG;
Ralf Baechleb9611532007-03-05 00:56:15 +0000236 mask = 1UL << bit;
Ralf Baechle49edd092007-03-16 16:10:36 +0000237 raw_local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 *a ^= mask;
Ralf Baechle49edd092007-03-16 16:10:36 +0000239 raw_local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241}
242
243/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * test_and_set_bit - Set a bit and return its old value
245 * @nr: Bit to set
246 * @addr: Address to count from
247 *
248 * This operation is atomic and cannot be reordered.
249 * It also implies a memory barrier.
250 */
251static inline int test_and_set_bit(unsigned long nr,
252 volatile unsigned long *addr)
253{
Ralf Baechleb9611532007-03-05 00:56:15 +0000254 unsigned short bit = nr & SZLONG_MASK;
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100255 unsigned long res;
Ralf Baechleb9611532007-03-05 00:56:15 +0000256
Nick Pigginc8f30ae2007-10-18 03:06:52 -0700257 smp_llsc_mb();
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (cpu_has_llsc && R10000_LLSC_WAR) {
260 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100261 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000264 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 "1: " __LL "%0, %1 # test_and_set_bit \n"
266 " or %2, %0, %3 \n"
267 " " __SC "%2, %1 \n"
268 " beqzl %2, 1b \n"
269 " and %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000270 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000272 : "r" (1UL << bit), "m" (*m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 } else if (cpu_has_llsc) {
275 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100276 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 __asm__ __volatile__(
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000279 " .set push \n"
280 " .set noreorder \n"
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000281 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000282 "1: " __LL "%0, %1 # test_and_set_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 " or %2, %0, %3 \n"
284 " " __SC "%2, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100285 " beqz %2, 2f \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 " and %2, %0, %3 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100287 " .subsection 2 \n"
288 "2: b 1b \n"
289 " nop \n"
290 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000291 " .set pop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000293 : "r" (1UL << bit), "m" (*m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 } else {
296 volatile unsigned long *a = addr;
297 unsigned long mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000298 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 a += nr >> SZLONG_LOG;
Ralf Baechleb9611532007-03-05 00:56:15 +0000301 mask = 1UL << bit;
Ralf Baechle49edd092007-03-16 16:10:36 +0000302 raw_local_irq_save(flags);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100303 res = (mask & *a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 *a |= mask;
Ralf Baechle49edd092007-03-16 16:10:36 +0000305 raw_local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000307
Ralf Baechle17099b12007-07-14 13:24:05 +0100308 smp_llsc_mb();
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100309
310 return res != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/*
Nick Piggin728697c2007-10-18 03:06:53 -0700314 * test_and_set_bit_lock - Set a bit and return its old value
315 * @nr: Bit to set
316 * @addr: Address to count from
317 *
318 * This operation is atomic and implies acquire ordering semantics
319 * after the memory operation.
320 */
321static inline int test_and_set_bit_lock(unsigned long nr,
322 volatile unsigned long *addr)
323{
324 unsigned short bit = nr & SZLONG_MASK;
325 unsigned long res;
326
327 if (cpu_has_llsc && R10000_LLSC_WAR) {
328 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
329 unsigned long temp;
330
331 __asm__ __volatile__(
332 " .set mips3 \n"
333 "1: " __LL "%0, %1 # test_and_set_bit \n"
334 " or %2, %0, %3 \n"
335 " " __SC "%2, %1 \n"
336 " beqzl %2, 1b \n"
337 " and %2, %0, %3 \n"
338 " .set mips0 \n"
339 : "=&r" (temp), "=m" (*m), "=&r" (res)
340 : "r" (1UL << bit), "m" (*m)
341 : "memory");
342 } else if (cpu_has_llsc) {
343 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
344 unsigned long temp;
345
346 __asm__ __volatile__(
347 " .set push \n"
348 " .set noreorder \n"
349 " .set mips3 \n"
350 "1: " __LL "%0, %1 # test_and_set_bit \n"
351 " or %2, %0, %3 \n"
352 " " __SC "%2, %1 \n"
353 " beqz %2, 2f \n"
354 " and %2, %0, %3 \n"
355 " .subsection 2 \n"
356 "2: b 1b \n"
357 " nop \n"
358 " .previous \n"
359 " .set pop \n"
360 : "=&r" (temp), "=m" (*m), "=&r" (res)
361 : "r" (1UL << bit), "m" (*m)
362 : "memory");
363 } else {
364 volatile unsigned long *a = addr;
365 unsigned long mask;
366 unsigned long flags;
367
368 a += nr >> SZLONG_LOG;
369 mask = 1UL << bit;
370 raw_local_irq_save(flags);
371 res = (mask & *a);
372 *a |= mask;
373 raw_local_irq_restore(flags);
374 }
375
376 smp_llsc_mb();
377
378 return res != 0;
379}
380/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * test_and_clear_bit - Clear a bit and return its old value
382 * @nr: Bit to clear
383 * @addr: Address to count from
384 *
385 * This operation is atomic and cannot be reordered.
386 * It also implies a memory barrier.
387 */
388static inline int test_and_clear_bit(unsigned long nr,
389 volatile unsigned long *addr)
390{
Ralf Baechleb9611532007-03-05 00:56:15 +0000391 unsigned short bit = nr & SZLONG_MASK;
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100392 unsigned long res;
Ralf Baechleb9611532007-03-05 00:56:15 +0000393
Nick Pigginc8f30ae2007-10-18 03:06:52 -0700394 smp_llsc_mb();
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (cpu_has_llsc && R10000_LLSC_WAR) {
397 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Atsushi Nemoto8e09ffb2007-06-14 00:56:31 +0900398 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000401 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 "1: " __LL "%0, %1 # test_and_clear_bit \n"
403 " or %2, %0, %3 \n"
404 " xor %2, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000405 " " __SC "%2, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 " beqzl %2, 1b \n"
407 " and %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000408 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000410 : "r" (1UL << bit), "m" (*m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 : "memory");
Ralf Baechle102fa152007-02-16 17:18:50 +0000412#ifdef CONFIG_CPU_MIPSR2
413 } else if (__builtin_constant_p(nr)) {
414 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100415 unsigned long temp;
Ralf Baechle102fa152007-02-16 17:18:50 +0000416
417 __asm__ __volatile__(
418 "1: " __LL "%0, %1 # test_and_clear_bit \n"
419 " " __EXT "%2, %0, %3, 1 \n"
420 " " __INS "%0, $0, %3, 1 \n"
421 " " __SC "%0, %1 \n"
422 " beqz %0, 2f \n"
423 " .subsection 2 \n"
424 "2: b 1b \n"
425 " .previous \n"
426 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000427 : "ri" (bit), "m" (*m)
Ralf Baechle102fa152007-02-16 17:18:50 +0000428 : "memory");
Ralf Baechle102fa152007-02-16 17:18:50 +0000429#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 } else if (cpu_has_llsc) {
431 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100432 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 __asm__ __volatile__(
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000435 " .set push \n"
436 " .set noreorder \n"
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000437 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000438 "1: " __LL "%0, %1 # test_and_clear_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 " or %2, %0, %3 \n"
440 " xor %2, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000441 " " __SC "%2, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100442 " beqz %2, 2f \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 " and %2, %0, %3 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100444 " .subsection 2 \n"
445 "2: b 1b \n"
446 " nop \n"
447 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000448 " .set pop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000450 : "r" (1UL << bit), "m" (*m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 } else {
453 volatile unsigned long *a = addr;
454 unsigned long mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000455 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 a += nr >> SZLONG_LOG;
Ralf Baechleb9611532007-03-05 00:56:15 +0000458 mask = 1UL << bit;
Ralf Baechle49edd092007-03-16 16:10:36 +0000459 raw_local_irq_save(flags);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100460 res = (mask & *a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 *a &= ~mask;
Ralf Baechle49edd092007-03-16 16:10:36 +0000462 raw_local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000464
Ralf Baechle17099b12007-07-14 13:24:05 +0100465 smp_llsc_mb();
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100466
467 return res != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 * test_and_change_bit - Change a bit and return its old value
472 * @nr: Bit to change
473 * @addr: Address to count from
474 *
475 * This operation is atomic and cannot be reordered.
476 * It also implies a memory barrier.
477 */
478static inline int test_and_change_bit(unsigned long nr,
479 volatile unsigned long *addr)
480{
Ralf Baechleb9611532007-03-05 00:56:15 +0000481 unsigned short bit = nr & SZLONG_MASK;
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100482 unsigned long res;
Ralf Baechleb9611532007-03-05 00:56:15 +0000483
Nick Pigginc8f30ae2007-10-18 03:06:52 -0700484 smp_llsc_mb();
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (cpu_has_llsc && R10000_LLSC_WAR) {
487 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100488 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000491 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000492 "1: " __LL "%0, %1 # test_and_change_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 " xor %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000494 " " __SC "%2, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 " beqzl %2, 1b \n"
496 " and %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000497 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000499 : "r" (1UL << bit), "m" (*m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 } else if (cpu_has_llsc) {
502 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100503 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 __asm__ __volatile__(
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000506 " .set push \n"
507 " .set noreorder \n"
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000508 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000509 "1: " __LL "%0, %1 # test_and_change_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 " xor %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000511 " " __SC "\t%2, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100512 " beqz %2, 2f \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 " and %2, %0, %3 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100514 " .subsection 2 \n"
515 "2: b 1b \n"
516 " nop \n"
517 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000518 " .set pop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 : "=&r" (temp), "=m" (*m), "=&r" (res)
Ralf Baechleb9611532007-03-05 00:56:15 +0000520 : "r" (1UL << bit), "m" (*m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 } else {
523 volatile unsigned long *a = addr;
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100524 unsigned long mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000525 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 a += nr >> SZLONG_LOG;
Ralf Baechleb9611532007-03-05 00:56:15 +0000528 mask = 1UL << bit;
Ralf Baechle49edd092007-03-16 16:10:36 +0000529 raw_local_irq_save(flags);
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100530 res = (mask & *a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 *a ^= mask;
Ralf Baechle49edd092007-03-16 16:10:36 +0000532 raw_local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000534
Ralf Baechle17099b12007-07-14 13:24:05 +0100535 smp_llsc_mb();
Ralf Baechleff72b7a2007-06-07 13:17:30 +0100536
537 return res != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800540#include <asm-generic/bitops/non-atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Ralf Baechle65903262005-07-12 12:50:30 +0000542/*
Nick Piggin728697c2007-10-18 03:06:53 -0700543 * __clear_bit_unlock - Clears a bit in memory
544 * @nr: Bit to clear
545 * @addr: Address to start counting from
546 *
547 * __clear_bit() is non-atomic and implies release semantics before the memory
548 * operation. It can be used for an unlock if no other CPUs can concurrently
549 * modify other bits in the word.
550 */
551static inline void __clear_bit_unlock(unsigned long nr, volatile unsigned long *addr)
552{
553 smp_mb();
554 __clear_bit(nr, addr);
555}
556
557/*
Ralf Baechle65903262005-07-12 12:50:30 +0000558 * Return the bit position (0..63) of the most significant 1 bit in a word
559 * Returns -1 if no 1 bit exists
560 */
Ralf Baechleec917c2c2005-10-07 16:58:15 +0100561static inline int __ilog2(unsigned long x)
Ralf Baechle65903262005-07-12 12:50:30 +0000562{
563 int lz;
564
Ralf Baechleec917c2c2005-10-07 16:58:15 +0100565 if (sizeof(x) == 4) {
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100566 __asm__(
Ralf Baechleec917c2c2005-10-07 16:58:15 +0100567 " .set push \n"
568 " .set mips32 \n"
569 " clz %0, %1 \n"
570 " .set pop \n"
571 : "=r" (lz)
572 : "r" (x));
573
574 return 31 - lz;
575 }
576
577 BUG_ON(sizeof(x) != 8);
578
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100579 __asm__(
Ralf Baechle65903262005-07-12 12:50:30 +0000580 " .set push \n"
581 " .set mips64 \n"
582 " dclz %0, %1 \n"
583 " .set pop \n"
584 : "=r" (lz)
585 : "r" (x));
586
587 return 63 - lz;
588}
Ralf Baechle65903262005-07-12 12:50:30 +0000589
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800590#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
591
Ralf Baechle65903262005-07-12 12:50:30 +0000592/*
593 * __ffs - find first bit in word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * @word: The word to search
595 *
Ralf Baechle65903262005-07-12 12:50:30 +0000596 * Returns 0..SZLONG-1
597 * Undefined if no bit exists, so code should check against 0 first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
Ralf Baechle65903262005-07-12 12:50:30 +0000599static inline unsigned long __ffs(unsigned long word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Ralf Baechle65903262005-07-12 12:50:30 +0000601 return __ilog2(word & -word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604/*
Atsushi Nemotobc818242006-04-17 21:19:12 +0900605 * fls - find last bit set.
606 * @word: The word to search
607 *
608 * This is defined the same way as ffs.
609 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
610 */
611static inline int fls(int word)
612{
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100613 __asm__("clz %0, %1" : "=r" (word) : "r" (word));
Atsushi Nemotobc818242006-04-17 21:19:12 +0900614
615 return 32 - word;
616}
617
618#if defined(CONFIG_64BIT) && defined(CONFIG_CPU_MIPS64)
619static inline int fls64(__u64 word)
620{
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100621 __asm__("dclz %0, %1" : "=r" (word) : "r" (word));
Atsushi Nemotobc818242006-04-17 21:19:12 +0900622
623 return 64 - word;
624}
625#else
626#include <asm-generic/bitops/fls64.h>
627#endif
628
629/*
Ralf Baechle65903262005-07-12 12:50:30 +0000630 * ffs - find first bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * @word: The word to search
632 *
Atsushi Nemotobc818242006-04-17 21:19:12 +0900633 * This is defined the same way as
634 * the libc and compiler builtin ffs routines, therefore
635 * differs in spirit from the above ffz (man ffs).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
Atsushi Nemotobc818242006-04-17 21:19:12 +0900637static inline int ffs(int word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Ralf Baechle65903262005-07-12 12:50:30 +0000639 if (!word)
640 return 0;
641
Atsushi Nemotobc818242006-04-17 21:19:12 +0900642 return fls(word & -word);
Ralf Baechle65903262005-07-12 12:50:30 +0000643}
Ralf Baechle2caf1902006-01-30 17:14:41 +0000644
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800645#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800647#include <asm-generic/bitops/__ffs.h>
648#include <asm-generic/bitops/ffs.h>
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800649#include <asm-generic/bitops/fls.h>
Atsushi Nemotobc818242006-04-17 21:19:12 +0900650#include <asm-generic/bitops/fls64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800652#endif /*defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Atsushi Nemotobc818242006-04-17 21:19:12 +0900654#include <asm-generic/bitops/ffz.h>
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800655#include <asm-generic/bitops/find.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657#ifdef __KERNEL__
658
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800659#include <asm-generic/bitops/sched.h>
660#include <asm-generic/bitops/hweight.h>
661#include <asm-generic/bitops/ext2-non-atomic.h>
662#include <asm-generic/bitops/ext2-atomic.h>
663#include <asm-generic/bitops/minix.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665#endif /* __KERNEL__ */
666
667#endif /* _ASM_BITOPS_H */