Linux 4.1.18
[linux/fpc-iii.git] / arch / frv / include / asm / bitops.h
blob96de220ef131ed84d4a0499e3fec9fbc0861a482
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>
28 #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
29 static inline
30 unsigned long atomic_test_and_ANDNOT_mask(unsigned long mask, volatile unsigned long *v)
32 unsigned long old, tmp;
34 asm volatile(
35 "0: \n"
36 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
37 " ckeq icc3,cc7 \n"
38 " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
39 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
40 " and%I3 %1,%3,%2 \n"
41 " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
42 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
43 " beq icc3,#0,0b \n"
44 : "+U"(*v), "=&r"(old), "=r"(tmp)
45 : "NPr"(~mask)
46 : "memory", "cc7", "cc3", "icc3"
49 return old;
52 static inline
53 unsigned long atomic_test_and_OR_mask(unsigned long mask, volatile unsigned long *v)
55 unsigned long old, tmp;
57 asm volatile(
58 "0: \n"
59 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
60 " ckeq icc3,cc7 \n"
61 " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
62 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
63 " or%I3 %1,%3,%2 \n"
64 " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
65 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
66 " beq icc3,#0,0b \n"
67 : "+U"(*v), "=&r"(old), "=r"(tmp)
68 : "NPr"(mask)
69 : "memory", "cc7", "cc3", "icc3"
72 return old;
75 static inline
76 unsigned long atomic_test_and_XOR_mask(unsigned long mask, volatile unsigned long *v)
78 unsigned long old, tmp;
80 asm volatile(
81 "0: \n"
82 " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
83 " ckeq icc3,cc7 \n"
84 " ld.p %M0,%1 \n" /* LD.P/ORCR are atomic */
85 " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
86 " xor%I3 %1,%3,%2 \n"
87 " cst.p %2,%M0 ,cc3,#1 \n" /* if store happens... */
88 " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* ... clear ICC3.Z */
89 " beq icc3,#0,0b \n"
90 : "+U"(*v), "=&r"(old), "=r"(tmp)
91 : "NPr"(mask)
92 : "memory", "cc7", "cc3", "icc3"
95 return old;
98 #else
100 extern unsigned long atomic_test_and_ANDNOT_mask(unsigned long mask, volatile unsigned long *v);
101 extern unsigned long atomic_test_and_OR_mask(unsigned long mask, volatile unsigned long *v);
102 extern unsigned long atomic_test_and_XOR_mask(unsigned long mask, volatile unsigned long *v);
104 #endif
106 #define atomic_clear_mask(mask, v) atomic_test_and_ANDNOT_mask((mask), (v))
107 #define atomic_set_mask(mask, v) atomic_test_and_OR_mask((mask), (v))
109 static inline int test_and_clear_bit(unsigned long nr, volatile void *addr)
111 volatile unsigned long *ptr = addr;
112 unsigned long mask = 1UL << (nr & 31);
113 ptr += nr >> 5;
114 return (atomic_test_and_ANDNOT_mask(mask, ptr) & mask) != 0;
117 static inline int test_and_set_bit(unsigned long nr, volatile void *addr)
119 volatile unsigned long *ptr = addr;
120 unsigned long mask = 1UL << (nr & 31);
121 ptr += nr >> 5;
122 return (atomic_test_and_OR_mask(mask, ptr) & mask) != 0;
125 static inline int test_and_change_bit(unsigned long nr, volatile void *addr)
127 volatile unsigned long *ptr = addr;
128 unsigned long mask = 1UL << (nr & 31);
129 ptr += nr >> 5;
130 return (atomic_test_and_XOR_mask(mask, ptr) & mask) != 0;
133 static inline void clear_bit(unsigned long nr, volatile void *addr)
135 test_and_clear_bit(nr, addr);
138 static inline void set_bit(unsigned long nr, volatile void *addr)
140 test_and_set_bit(nr, addr);
143 static inline void change_bit(unsigned long nr, volatile void *addr)
145 test_and_change_bit(nr, addr);
148 static inline void __clear_bit(unsigned long nr, volatile void *addr)
150 volatile unsigned long *a = addr;
151 int mask;
153 a += nr >> 5;
154 mask = 1 << (nr & 31);
155 *a &= ~mask;
158 static inline void __set_bit(unsigned long nr, volatile void *addr)
160 volatile unsigned long *a = addr;
161 int mask;
163 a += nr >> 5;
164 mask = 1 << (nr & 31);
165 *a |= mask;
168 static inline void __change_bit(unsigned long nr, volatile void *addr)
170 volatile unsigned long *a = addr;
171 int mask;
173 a += nr >> 5;
174 mask = 1 << (nr & 31);
175 *a ^= mask;
178 static inline int __test_and_clear_bit(unsigned long nr, volatile void *addr)
180 volatile unsigned long *a = addr;
181 int mask, retval;
183 a += nr >> 5;
184 mask = 1 << (nr & 31);
185 retval = (mask & *a) != 0;
186 *a &= ~mask;
187 return retval;
190 static inline int __test_and_set_bit(unsigned long nr, volatile void *addr)
192 volatile unsigned long *a = addr;
193 int mask, retval;
195 a += nr >> 5;
196 mask = 1 << (nr & 31);
197 retval = (mask & *a) != 0;
198 *a |= mask;
199 return retval;
202 static inline int __test_and_change_bit(unsigned long nr, volatile void *addr)
204 volatile unsigned long *a = addr;
205 int mask, retval;
207 a += nr >> 5;
208 mask = 1 << (nr & 31);
209 retval = (mask & *a) != 0;
210 *a ^= mask;
211 return retval;
215 * This routine doesn't need to be atomic.
217 static inline int
218 __constant_test_bit(unsigned long nr, const volatile void *addr)
220 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
223 static inline int __test_bit(unsigned long nr, const volatile void *addr)
225 int * a = (int *) addr;
226 int mask;
228 a += nr >> 5;
229 mask = 1 << (nr & 0x1f);
230 return ((mask & *a) != 0);
233 #define test_bit(nr,addr) \
234 (__builtin_constant_p(nr) ? \
235 __constant_test_bit((nr),(addr)) : \
236 __test_bit((nr),(addr)))
238 #include <asm-generic/bitops/find.h>
241 * fls - find last bit set
242 * @x: the word to search
244 * This is defined the same way as ffs:
245 * - return 32..1 to indicate bit 31..0 most significant bit set
246 * - return 0 to indicate no bits set
248 #define fls(x) \
249 ({ \
250 int bit; \
252 asm(" subcc %1,gr0,gr0,icc0 \n" \
253 " ckne icc0,cc4 \n" \
254 " cscan.p %1,gr0,%0 ,cc4,#1 \n" \
255 " csub %0,%0,%0 ,cc4,#0 \n" \
256 " csub %2,%0,%0 ,cc4,#1 \n" \
257 : "=&r"(bit) \
258 : "r"(x), "r"(32) \
259 : "icc0", "cc4" \
260 ); \
262 bit; \
266 * fls64 - find last bit set in a 64-bit value
267 * @n: the value to search
269 * This is defined the same way as ffs:
270 * - return 64..1 to indicate bit 63..0 most significant bit set
271 * - return 0 to indicate no bits set
273 static inline __attribute__((const))
274 int fls64(u64 n)
276 union {
277 u64 ll;
278 struct { u32 h, l; };
279 } _;
280 int bit, x, y;
282 _.ll = n;
284 asm(" subcc.p %3,gr0,gr0,icc0 \n"
285 " subcc %4,gr0,gr0,icc1 \n"
286 " ckne icc0,cc4 \n"
287 " ckne icc1,cc5 \n"
288 " norcr cc4,cc5,cc6 \n"
289 " csub.p %0,%0,%0 ,cc6,1 \n"
290 " orcr cc5,cc4,cc4 \n"
291 " andcr cc4,cc5,cc4 \n"
292 " cscan.p %3,gr0,%0 ,cc4,0 \n"
293 " setlos #64,%1 \n"
294 " cscan.p %4,gr0,%0 ,cc4,1 \n"
295 " setlos #32,%2 \n"
296 " csub.p %1,%0,%0 ,cc4,0 \n"
297 " csub %2,%0,%0 ,cc4,1 \n"
298 : "=&r"(bit), "=r"(x), "=r"(y)
299 : "0r"(_.h), "r"(_.l)
300 : "icc0", "icc1", "cc4", "cc5", "cc6"
302 return bit;
307 * ffs - find first bit set
308 * @x: the word to search
310 * - return 32..1 to indicate bit 31..0 most least significant bit set
311 * - return 0 to indicate no bits set
313 static inline __attribute__((const))
314 int ffs(int x)
316 /* Note: (x & -x) gives us a mask that is the least significant
317 * (rightmost) 1-bit of the value in x.
319 return fls(x & -x);
323 * __ffs - find first bit set
324 * @x: the word to search
326 * - return 31..0 to indicate bit 31..0 most least significant bit set
327 * - if no bits are set in x, the result is undefined
329 static inline __attribute__((const))
330 int __ffs(unsigned long x)
332 int bit;
333 asm("scan %1,gr0,%0" : "=r"(bit) : "r"(x & -x));
334 return 31 - bit;
338 * __fls - find last (most-significant) set bit in a long word
339 * @word: the word to search
341 * Undefined if no set bit exists, so code should check against 0 first.
343 static inline unsigned long __fls(unsigned long word)
345 unsigned long bit;
346 asm("scan %1,gr0,%0" : "=r"(bit) : "r"(word));
347 return bit;
351 * special slimline version of fls() for calculating ilog2_u32()
352 * - note: no protection against n == 0
354 #define ARCH_HAS_ILOG2_U32
355 static inline __attribute__((const))
356 int __ilog2_u32(u32 n)
358 int bit;
359 asm("scan %1,gr0,%0" : "=r"(bit) : "r"(n));
360 return 31 - bit;
364 * special slimline version of fls64() for calculating ilog2_u64()
365 * - note: no protection against n == 0
367 #define ARCH_HAS_ILOG2_U64
368 static inline __attribute__((const))
369 int __ilog2_u64(u64 n)
371 union {
372 u64 ll;
373 struct { u32 h, l; };
374 } _;
375 int bit, x, y;
377 _.ll = n;
379 asm(" subcc %3,gr0,gr0,icc0 \n"
380 " ckeq icc0,cc4 \n"
381 " cscan.p %3,gr0,%0 ,cc4,0 \n"
382 " setlos #63,%1 \n"
383 " cscan.p %4,gr0,%0 ,cc4,1 \n"
384 " setlos #31,%2 \n"
385 " csub.p %1,%0,%0 ,cc4,0 \n"
386 " csub %2,%0,%0 ,cc4,1 \n"
387 : "=&r"(bit), "=r"(x), "=r"(y)
388 : "0r"(_.h), "r"(_.l)
389 : "icc0", "cc4"
391 return bit;
394 #include <asm-generic/bitops/sched.h>
395 #include <asm-generic/bitops/hweight.h>
396 #include <asm-generic/bitops/lock.h>
398 #include <asm-generic/bitops/le.h>
400 #include <asm-generic/bitops/ext2-atomic-setbit.h>
402 #endif /* __KERNEL__ */
404 #endif /* _ASM_BITOPS_H */