blob: 06c08228a5256cbb3fa9320b102b09eb6e21774d [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 Baechle0004a9d2006-10-31 03:45:07 +00006 * Copyright (c) 1994 - 1997, 1999, 2000, 06 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 Baechleec917c22005-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 "
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#elif (_MIPS_SZLONG == 64)
28#define SZLONG_LOG 6
29#define SZLONG_MASK 63UL
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000030#define __LL "lld "
31#define __SC "scd "
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#endif
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
35 * clear_bit() doesn't provide any barrier for the compiler.
36 */
37#define smp_mb__before_clear_bit() smp_mb()
38#define smp_mb__after_clear_bit() smp_mb()
39
40/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * set_bit - Atomically set a bit in memory
42 * @nr: the bit to set
43 * @addr: the address to start counting from
44 *
45 * This function is atomic and may not be reordered. See __set_bit()
46 * if you do not require the atomic guarantees.
47 * Note that @nr may be almost arbitrarily large; this function is not
48 * restricted to acting on a single-word quantity.
49 */
50static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
51{
52 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
53 unsigned long temp;
54
55 if (cpu_has_llsc && R10000_LLSC_WAR) {
56 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +000057 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 "1: " __LL "%0, %1 # set_bit \n"
59 " or %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000060 " " __SC "%0, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000062 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 : "=&r" (temp), "=m" (*m)
64 : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
65 } else if (cpu_has_llsc) {
66 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +000067 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 "1: " __LL "%0, %1 # set_bit \n"
69 " or %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000070 " " __SC "%0, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +010071 " beqz %0, 2f \n"
72 " .subsection 2 \n"
73 "2: b 1b \n"
74 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000075 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 : "=&r" (temp), "=m" (*m)
77 : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
78 } else {
79 volatile unsigned long *a = addr;
80 unsigned long mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +000081 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 a += nr >> SZLONG_LOG;
84 mask = 1UL << (nr & SZLONG_MASK);
Ralf Baechle4ffd8b32006-11-30 01:14:50 +000085 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *a |= mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +000087 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89}
90
91/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * clear_bit - Clears a bit in memory
93 * @nr: Bit to clear
94 * @addr: Address to start counting from
95 *
96 * clear_bit() is atomic and may not be reordered. However, it does
97 * not contain a memory barrier, so if it is used for locking purposes,
98 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
99 * in order to ensure changes are visible on other processors.
100 */
101static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
102{
103 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
104 unsigned long temp;
105
106 if (cpu_has_llsc && R10000_LLSC_WAR) {
107 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000108 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 "1: " __LL "%0, %1 # clear_bit \n"
110 " and %0, %2 \n"
111 " " __SC "%0, %1 \n"
112 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000113 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 : "=&r" (temp), "=m" (*m)
115 : "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m));
116 } else if (cpu_has_llsc) {
117 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000118 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 "1: " __LL "%0, %1 # clear_bit \n"
120 " and %0, %2 \n"
121 " " __SC "%0, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100122 " beqz %0, 2f \n"
123 " .subsection 2 \n"
124 "2: b 1b \n"
125 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000126 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 : "=&r" (temp), "=m" (*m)
128 : "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m));
129 } else {
130 volatile unsigned long *a = addr;
131 unsigned long mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000132 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 a += nr >> SZLONG_LOG;
135 mask = 1UL << (nr & SZLONG_MASK);
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000136 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *a &= ~mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000138 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140}
141
142/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * change_bit - Toggle a bit in memory
144 * @nr: Bit to change
145 * @addr: Address to start counting from
146 *
147 * change_bit() is atomic and may not be reordered.
148 * Note that @nr may be almost arbitrarily large; this function is not
149 * restricted to acting on a single-word quantity.
150 */
151static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
152{
153 if (cpu_has_llsc && R10000_LLSC_WAR) {
154 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
155 unsigned long temp;
156
157 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000158 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 "1: " __LL "%0, %1 # change_bit \n"
160 " xor %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000161 " " __SC "%0, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000163 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 : "=&r" (temp), "=m" (*m)
165 : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
166 } else if (cpu_has_llsc) {
167 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
168 unsigned long temp;
169
170 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000171 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 "1: " __LL "%0, %1 # change_bit \n"
173 " xor %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000174 " " __SC "%0, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100175 " beqz %0, 2f \n"
176 " .subsection 2 \n"
177 "2: b 1b \n"
178 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000179 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 : "=&r" (temp), "=m" (*m)
181 : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
182 } else {
183 volatile unsigned long *a = addr;
184 unsigned long mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000185 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 a += nr >> SZLONG_LOG;
188 mask = 1UL << (nr & SZLONG_MASK);
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000189 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *a ^= mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000191 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193}
194
195/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * test_and_set_bit - Set a bit and return its old value
197 * @nr: Bit to set
198 * @addr: Address to count from
199 *
200 * This operation is atomic and cannot be reordered.
201 * It also implies a memory barrier.
202 */
203static inline int test_and_set_bit(unsigned long nr,
204 volatile unsigned long *addr)
205{
206 if (cpu_has_llsc && R10000_LLSC_WAR) {
207 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
208 unsigned long temp, res;
209
210 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000211 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 "1: " __LL "%0, %1 # test_and_set_bit \n"
213 " or %2, %0, %3 \n"
214 " " __SC "%2, %1 \n"
215 " beqzl %2, 1b \n"
216 " and %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000217 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 : "=&r" (temp), "=m" (*m), "=&r" (res)
219 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
220 : "memory");
221
222 return res != 0;
223 } else if (cpu_has_llsc) {
224 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
225 unsigned long temp, res;
226
227 __asm__ __volatile__(
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000228 " .set push \n"
229 " .set noreorder \n"
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000230 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000231 "1: " __LL "%0, %1 # test_and_set_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 " or %2, %0, %3 \n"
233 " " __SC "%2, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100234 " beqz %2, 2f \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 " and %2, %0, %3 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100236 " .subsection 2 \n"
237 "2: b 1b \n"
238 " nop \n"
239 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000240 " .set pop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 : "=&r" (temp), "=m" (*m), "=&r" (res)
242 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
243 : "memory");
244
245 return res != 0;
246 } else {
247 volatile unsigned long *a = addr;
248 unsigned long mask;
249 int retval;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000250 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 a += nr >> SZLONG_LOG;
253 mask = 1UL << (nr & SZLONG_MASK);
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000254 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 retval = (mask & *a) != 0;
256 *a |= mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000257 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 return retval;
260 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000261
262 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * test_and_clear_bit - Clear a bit and return its old value
267 * @nr: Bit to clear
268 * @addr: Address to count from
269 *
270 * This operation is atomic and cannot be reordered.
271 * It also implies a memory barrier.
272 */
273static inline int test_and_clear_bit(unsigned long nr,
274 volatile unsigned long *addr)
275{
276 if (cpu_has_llsc && R10000_LLSC_WAR) {
277 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
278 unsigned long temp, res;
279
280 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000281 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 "1: " __LL "%0, %1 # test_and_clear_bit \n"
283 " or %2, %0, %3 \n"
284 " xor %2, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000285 " " __SC "%2, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 " beqzl %2, 1b \n"
287 " and %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000288 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 : "=&r" (temp), "=m" (*m), "=&r" (res)
290 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
291 : "memory");
292
293 return res != 0;
294 } else if (cpu_has_llsc) {
295 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
296 unsigned long temp, res;
297
298 __asm__ __volatile__(
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000299 " .set push \n"
300 " .set noreorder \n"
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000301 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000302 "1: " __LL "%0, %1 # test_and_clear_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 " or %2, %0, %3 \n"
304 " xor %2, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000305 " " __SC "%2, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100306 " beqz %2, 2f \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 " and %2, %0, %3 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100308 " .subsection 2 \n"
309 "2: b 1b \n"
310 " nop \n"
311 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000312 " .set pop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 : "=&r" (temp), "=m" (*m), "=&r" (res)
314 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
315 : "memory");
316
317 return res != 0;
318 } else {
319 volatile unsigned long *a = addr;
320 unsigned long mask;
321 int retval;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000322 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 a += nr >> SZLONG_LOG;
325 mask = 1UL << (nr & SZLONG_MASK);
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000326 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 retval = (mask & *a) != 0;
328 *a &= ~mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000329 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 return retval;
332 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000333
334 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * test_and_change_bit - Change a bit and return its old value
339 * @nr: Bit to change
340 * @addr: Address to count from
341 *
342 * This operation is atomic and cannot be reordered.
343 * It also implies a memory barrier.
344 */
345static inline int test_and_change_bit(unsigned long nr,
346 volatile unsigned long *addr)
347{
348 if (cpu_has_llsc && R10000_LLSC_WAR) {
349 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
350 unsigned long temp, res;
351
352 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000353 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000354 "1: " __LL "%0, %1 # test_and_change_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 " xor %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000356 " " __SC "%2, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 " beqzl %2, 1b \n"
358 " and %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000359 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 : "=&r" (temp), "=m" (*m), "=&r" (res)
361 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
362 : "memory");
363
364 return res != 0;
365 } else if (cpu_has_llsc) {
366 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
367 unsigned long temp, res;
368
369 __asm__ __volatile__(
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000370 " .set push \n"
371 " .set noreorder \n"
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000372 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000373 "1: " __LL "%0, %1 # test_and_change_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 " xor %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000375 " " __SC "\t%2, %1 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100376 " beqz %2, 2f \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 " and %2, %0, %3 \n"
Ralf Baechlef65e4fa2006-09-28 01:45:21 +0100378 " .subsection 2 \n"
379 "2: b 1b \n"
380 " nop \n"
381 " .previous \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000382 " .set pop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 : "=&r" (temp), "=m" (*m), "=&r" (res)
384 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
385 : "memory");
386
387 return res != 0;
388 } else {
389 volatile unsigned long *a = addr;
390 unsigned long mask, retval;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000391 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 a += nr >> SZLONG_LOG;
394 mask = 1UL << (nr & SZLONG_MASK);
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000395 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 retval = (mask & *a) != 0;
397 *a ^= mask;
Ralf Baechle4ffd8b32006-11-30 01:14:50 +0000398 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 return retval;
401 }
Ralf Baechle0004a9d2006-10-31 03:45:07 +0000402
403 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800406#include <asm-generic/bitops/non-atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Ralf Baechle65903262005-07-12 12:50:30 +0000408/*
409 * Return the bit position (0..63) of the most significant 1 bit in a word
410 * Returns -1 if no 1 bit exists
411 */
Ralf Baechleec917c22005-10-07 16:58:15 +0100412static inline int __ilog2(unsigned long x)
Ralf Baechle65903262005-07-12 12:50:30 +0000413{
414 int lz;
415
Ralf Baechleec917c22005-10-07 16:58:15 +0100416 if (sizeof(x) == 4) {
417 __asm__ (
418 " .set push \n"
419 " .set mips32 \n"
420 " clz %0, %1 \n"
421 " .set pop \n"
422 : "=r" (lz)
423 : "r" (x));
424
425 return 31 - lz;
426 }
427
428 BUG_ON(sizeof(x) != 8);
429
Ralf Baechle65903262005-07-12 12:50:30 +0000430 __asm__ (
431 " .set push \n"
432 " .set mips64 \n"
433 " dclz %0, %1 \n"
434 " .set pop \n"
435 : "=r" (lz)
436 : "r" (x));
437
438 return 63 - lz;
439}
Ralf Baechle65903262005-07-12 12:50:30 +0000440
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800441#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
442
Ralf Baechle65903262005-07-12 12:50:30 +0000443/*
444 * __ffs - find first bit in word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * @word: The word to search
446 *
Ralf Baechle65903262005-07-12 12:50:30 +0000447 * Returns 0..SZLONG-1
448 * Undefined if no bit exists, so code should check against 0 first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
Ralf Baechle65903262005-07-12 12:50:30 +0000450static inline unsigned long __ffs(unsigned long word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Ralf Baechle65903262005-07-12 12:50:30 +0000452 return __ilog2(word & -word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455/*
Atsushi Nemotobc818242006-04-17 21:19:12 +0900456 * fls - find last bit set.
457 * @word: The word to search
458 *
459 * This is defined the same way as ffs.
460 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
461 */
462static inline int fls(int word)
463{
464 __asm__ ("clz %0, %1" : "=r" (word) : "r" (word));
465
466 return 32 - word;
467}
468
469#if defined(CONFIG_64BIT) && defined(CONFIG_CPU_MIPS64)
470static inline int fls64(__u64 word)
471{
472 __asm__ ("dclz %0, %1" : "=r" (word) : "r" (word));
473
474 return 64 - word;
475}
476#else
477#include <asm-generic/bitops/fls64.h>
478#endif
479
480/*
Ralf Baechle65903262005-07-12 12:50:30 +0000481 * ffs - find first bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * @word: The word to search
483 *
Atsushi Nemotobc818242006-04-17 21:19:12 +0900484 * This is defined the same way as
485 * the libc and compiler builtin ffs routines, therefore
486 * differs in spirit from the above ffz (man ffs).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
Atsushi Nemotobc818242006-04-17 21:19:12 +0900488static inline int ffs(int word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Ralf Baechle65903262005-07-12 12:50:30 +0000490 if (!word)
491 return 0;
492
Atsushi Nemotobc818242006-04-17 21:19:12 +0900493 return fls(word & -word);
Ralf Baechle65903262005-07-12 12:50:30 +0000494}
Ralf Baechle2caf1902006-01-30 17:14:41 +0000495
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800496#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800498#include <asm-generic/bitops/__ffs.h>
499#include <asm-generic/bitops/ffs.h>
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800500#include <asm-generic/bitops/fls.h>
Atsushi Nemotobc818242006-04-17 21:19:12 +0900501#include <asm-generic/bitops/fls64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800503#endif /*defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Atsushi Nemotobc818242006-04-17 21:19:12 +0900505#include <asm-generic/bitops/ffz.h>
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800506#include <asm-generic/bitops/find.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508#ifdef __KERNEL__
509
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800510#include <asm-generic/bitops/sched.h>
511#include <asm-generic/bitops/hweight.h>
512#include <asm-generic/bitops/ext2-non-atomic.h>
513#include <asm-generic/bitops/ext2-atomic.h>
514#include <asm-generic/bitops/minix.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516#endif /* __KERNEL__ */
517
518#endif /* _ASM_BITOPS_H */