Linux 2.6.25.3
[linux/fpc-iii.git] / include / asm-frv / bitops.h
blob39456ba0ec17c7156c5a7fef5baed6cbf60f74e2
1 /* bitops.h: bit operations for the Fujitsu FR-V CPUs
3 * For an explanation of how atomic ops work in this arch, see:
4 * Documentation/frv/atomic-ops.txt
6 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 #ifndef _ASM_BITOPS_H
15 #define _ASM_BITOPS_H
17 #include <linux/compiler.h>
18 #include <asm/byteorder.h>
20 #ifdef __KERNEL__
22 #ifndef _LINUX_BITOPS_H
23 #error only <linux/bitops.h> can be included directly
24 #endif
26 #include <asm-generic/bitops/ffz.h>
29 * clear_bit() doesn't provide any barrier for the compiler.
31 #define smp_mb__before_clear_bit() barrier()
32 #define smp_mb__after_clear_bit() barrier()
34 #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
35 static inline
36 unsigned long atomic_test_and_ANDNOT_mask(unsigned long mask, volatile unsigned long *v)
38 unsigned long old, tmp;
40 asm volatile(
41 "0: \n"
42 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
43 " ckeq icc3,cc7 \n"
44 " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
45 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
46 " and%I3 %1,%3,%2 \n"
47 " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
48 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
49 " beq icc3,#0,0b \n"
50 : "+U"(*v), "=&r"(old), "=r"(tmp)
51 : "NPr"(~mask)
52 : "memory", "cc7", "cc3", "icc3"
55 return old;
58 static inline
59 unsigned long atomic_test_and_OR_mask(unsigned long mask, volatile unsigned long *v)
61 unsigned long old, tmp;
63 asm volatile(
64 "0: \n"
65 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
66 " ckeq icc3,cc7 \n"
67 " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
68 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
69 " or%I3 %1,%3,%2 \n"
70 " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
71 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
72 " beq icc3,#0,0b \n"
73 : "+U"(*v), "=&r"(old), "=r"(tmp)
74 : "NPr"(mask)
75 : "memory", "cc7", "cc3", "icc3"
78 return old;
81 static inline
82 unsigned long atomic_test_and_XOR_mask(unsigned long mask, volatile unsigned long *v)
84 unsigned long old, tmp;
86 asm volatile(
87 "0: \n"
88 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
89 " ckeq icc3,cc7 \n"
90 " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
91 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
92 " xor%I3 %1,%3,%2 \n"
93 " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
94 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
95 " beq icc3,#0,0b \n"
96 : "+U"(*v), "=&r"(old), "=r"(tmp)
97 : "NPr"(mask)
98 : "memory", "cc7", "cc3", "icc3"
101 return old;
104 #else
106 extern unsigned long atomic_test_and_ANDNOT_mask(unsigned long mask, volatile unsigned long *v);
107 extern unsigned long atomic_test_and_OR_mask(unsigned long mask, volatile unsigned long *v);
108 extern unsigned long atomic_test_and_XOR_mask(unsigned long mask, volatile unsigned long *v);
110 #endif
112 #define atomic_clear_mask(mask, v) atomic_test_and_ANDNOT_mask((mask), (v))
113 #define atomic_set_mask(mask, v) atomic_test_and_OR_mask((mask), (v))
115 static inline int test_and_clear_bit(int nr, volatile void *addr)
117 volatile unsigned long *ptr = addr;
118 unsigned long mask = 1UL << (nr & 31);
119 ptr += nr >> 5;
120 return (atomic_test_and_ANDNOT_mask(mask, ptr) & mask) != 0;
123 static inline int test_and_set_bit(int nr, volatile void *addr)
125 volatile unsigned long *ptr = addr;
126 unsigned long mask = 1UL << (nr & 31);
127 ptr += nr >> 5;
128 return (atomic_test_and_OR_mask(mask, ptr) & mask) != 0;
131 static inline int test_and_change_bit(int nr, volatile void *addr)
133 volatile unsigned long *ptr = addr;
134 unsigned long mask = 1UL << (nr & 31);
135 ptr += nr >> 5;
136 return (atomic_test_and_XOR_mask(mask, ptr) & mask) != 0;
139 static inline void clear_bit(int nr, volatile void *addr)
141 test_and_clear_bit(nr, addr);
144 static inline void set_bit(int nr, volatile void *addr)
146 test_and_set_bit(nr, addr);
149 static inline void change_bit(int nr, volatile void * addr)
151 test_and_change_bit(nr, addr);
154 static inline void __clear_bit(int nr, volatile void * addr)
156 volatile unsigned long *a = addr;
157 int mask;
159 a += nr >> 5;
160 mask = 1 << (nr & 31);
161 *a &= ~mask;
164 static inline void __set_bit(int nr, volatile void * addr)
166 volatile unsigned long *a = addr;
167 int mask;
169 a += nr >> 5;
170 mask = 1 << (nr & 31);
171 *a |= mask;
174 static inline void __change_bit(int nr, volatile void *addr)
176 volatile unsigned long *a = addr;
177 int mask;
179 a += nr >> 5;
180 mask = 1 << (nr & 31);
181 *a ^= mask;
184 static inline int __test_and_clear_bit(int nr, volatile void * addr)
186 volatile unsigned long *a = addr;
187 int mask, retval;
189 a += nr >> 5;
190 mask = 1 << (nr & 31);
191 retval = (mask & *a) != 0;
192 *a &= ~mask;
193 return retval;
196 static inline int __test_and_set_bit(int nr, volatile void * addr)
198 volatile unsigned long *a = addr;
199 int mask, retval;
201 a += nr >> 5;
202 mask = 1 << (nr & 31);
203 retval = (mask & *a) != 0;
204 *a |= mask;
205 return retval;
208 static inline int __test_and_change_bit(int nr, volatile void * addr)
210 volatile unsigned long *a = addr;
211 int mask, retval;
213 a += nr >> 5;
214 mask = 1 << (nr & 31);
215 retval = (mask & *a) != 0;
216 *a ^= mask;
217 return retval;
221 * This routine doesn't need to be atomic.
223 static inline int __constant_test_bit(int nr, const volatile void * addr)
225 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
228 static inline int __test_bit(int nr, const volatile void * addr)
230 int * a = (int *) addr;
231 int mask;
233 a += nr >> 5;
234 mask = 1 << (nr & 0x1f);
235 return ((mask & *a) != 0);
238 #define test_bit(nr,addr) \
239 (__builtin_constant_p(nr) ? \
240 __constant_test_bit((nr),(addr)) : \
241 __test_bit((nr),(addr)))
243 #include <asm-generic/bitops/find.h>
246 * fls - find last bit set
247 * @x: the word to search
249 * This is defined the same way as ffs:
250 * - return 32..1 to indicate bit 31..0 most significant bit set
251 * - return 0 to indicate no bits set
253 #define fls(x) \
254 ({ \
255 int bit; \
257 asm(" subcc %1,gr0,gr0,icc0 \n" \
258 " ckne icc0,cc4 \n" \
259 " cscan.p %1,gr0,%0 ,cc4,#1 \n" \
260 " csub %0,%0,%0 ,cc4,#0 \n" \
261 " csub %2,%0,%0 ,cc4,#1 \n" \
262 : "=&r"(bit) \
263 : "r"(x), "r"(32) \
264 : "icc0", "cc4" \
265 ); \
267 bit; \
271 * fls64 - find last bit set in a 64-bit value
272 * @n: the value to search
274 * This is defined the same way as ffs:
275 * - return 64..1 to indicate bit 63..0 most significant bit set
276 * - return 0 to indicate no bits set
278 static inline __attribute__((const))
279 int fls64(u64 n)
281 union {
282 u64 ll;
283 struct { u32 h, l; };
284 } _;
285 int bit, x, y;
287 _.ll = n;
289 asm(" subcc.p %3,gr0,gr0,icc0 \n"
290 " subcc %4,gr0,gr0,icc1 \n"
291 " ckne icc0,cc4 \n"
292 " ckne icc1,cc5 \n"
293 " norcr cc4,cc5,cc6 \n"
294 " csub.p %0,%0,%0 ,cc6,1 \n"
295 " orcr cc5,cc4,cc4 \n"
296 " andcr cc4,cc5,cc4 \n"
297 " cscan.p %3,gr0,%0 ,cc4,0 \n"
298 " setlos #64,%1 \n"
299 " cscan.p %4,gr0,%0 ,cc4,1 \n"
300 " setlos #32,%2 \n"
301 " csub.p %1,%0,%0 ,cc4,0 \n"
302 " csub %2,%0,%0 ,cc4,1 \n"
303 : "=&r"(bit), "=r"(x), "=r"(y)
304 : "0r"(_.h), "r"(_.l)
305 : "icc0", "icc1", "cc4", "cc5", "cc6"
307 return bit;
312 * ffs - find first bit set
313 * @x: the word to search
315 * - return 32..1 to indicate bit 31..0 most least significant bit set
316 * - return 0 to indicate no bits set
318 static inline __attribute__((const))
319 int ffs(int x)
321 /* Note: (x & -x) gives us a mask that is the least significant
322 * (rightmost) 1-bit of the value in x.
324 return fls(x & -x);
328 * __ffs - find first bit set
329 * @x: the word to search
331 * - return 31..0 to indicate bit 31..0 most least significant bit set
332 * - if no bits are set in x, the result is undefined
334 static inline __attribute__((const))
335 int __ffs(unsigned long x)
337 int bit;
338 asm("scan %1,gr0,%0" : "=r"(bit) : "r"(x & -x));
339 return 31 - bit;
343 * special slimline version of fls() for calculating ilog2_u32()
344 * - note: no protection against n == 0
346 #define ARCH_HAS_ILOG2_U32
347 static inline __attribute__((const))
348 int __ilog2_u32(u32 n)
350 int bit;
351 asm("scan %1,gr0,%0" : "=r"(bit) : "r"(n));
352 return 31 - bit;
356 * special slimline version of fls64() for calculating ilog2_u64()
357 * - note: no protection against n == 0
359 #define ARCH_HAS_ILOG2_U64
360 static inline __attribute__((const))
361 int __ilog2_u64(u64 n)
363 union {
364 u64 ll;
365 struct { u32 h, l; };
366 } _;
367 int bit, x, y;
369 _.ll = n;
371 asm(" subcc %3,gr0,gr0,icc0 \n"
372 " ckeq icc0,cc4 \n"
373 " cscan.p %3,gr0,%0 ,cc4,0 \n"
374 " setlos #63,%1 \n"
375 " cscan.p %4,gr0,%0 ,cc4,1 \n"
376 " setlos #31,%2 \n"
377 " csub.p %1,%0,%0 ,cc4,0 \n"
378 " csub %2,%0,%0 ,cc4,1 \n"
379 : "=&r"(bit), "=r"(x), "=r"(y)
380 : "0r"(_.h), "r"(_.l)
381 : "icc0", "cc4"
383 return bit;
386 #include <asm-generic/bitops/sched.h>
387 #include <asm-generic/bitops/hweight.h>
388 #include <asm-generic/bitops/lock.h>
390 #include <asm-generic/bitops/ext2-non-atomic.h>
392 #define ext2_set_bit_atomic(lock,nr,addr) test_and_set_bit ((nr) ^ 0x18, (addr))
393 #define ext2_clear_bit_atomic(lock,nr,addr) test_and_clear_bit((nr) ^ 0x18, (addr))
395 #include <asm-generic/bitops/minix-le.h>
397 #endif /* __KERNEL__ */
399 #endif /* _ASM_BITOPS_H */