4 * Copyright 1992, Linus Torvalds.
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive
11 #ifndef _LINUX_BITOPS_H
12 #error only <linux/bitops.h> can be included directly
15 #include <linux/compiler.h>
16 #include <asm/barrier.h>
19 * Bit access functions vary across the ColdFire and 68k families.
20 * So we will break them out here, and then macro in the ones we want.
22 * ColdFire - supports standard bset/bclr/bchg with register operand only
23 * 68000 - supports standard bset/bclr/bchg with memory operand
24 * >= 68020 - also supports the bfset/bfclr/bfchg instructions
26 * Although it is possible to use only the bset/bclr/bchg with register
27 * operands on all platforms you end up with larger generated code.
28 * So we use the best form possible on a given platform.
31 static inline void bset_reg_set_bit(int nr, volatile unsigned long *vaddr)
33 char *p = (char *)vaddr + (nr ^ 31) / 8;
35 __asm__ __volatile__ ("bset %1,(%0)"
37 : "a" (p), "di" (nr & 7)
41 static inline void bset_mem_set_bit(int nr, volatile unsigned long *vaddr)
43 char *p = (char *)vaddr + (nr ^ 31) / 8;
45 __asm__ __volatile__ ("bset %1,%0"
50 static inline void bfset_mem_set_bit(int nr, volatile unsigned long *vaddr)
52 __asm__ __volatile__ ("bfset %1{%0:#1}"
54 : "d" (nr ^ 31), "o" (*vaddr)
58 #if defined(CONFIG_COLDFIRE)
59 #define set_bit(nr, vaddr) bset_reg_set_bit(nr, vaddr)
60 #elif defined(CONFIG_CPU_HAS_NO_BITFIELDS)
61 #define set_bit(nr, vaddr) bset_mem_set_bit(nr, vaddr)
63 #define set_bit(nr, vaddr) (__builtin_constant_p(nr) ? \
64 bset_mem_set_bit(nr, vaddr) : \
65 bfset_mem_set_bit(nr, vaddr))
68 static __always_inline void
69 arch___set_bit(unsigned long nr, volatile unsigned long *addr)
74 static inline void bclr_reg_clear_bit(int nr, volatile unsigned long *vaddr)
76 char *p = (char *)vaddr + (nr ^ 31) / 8;
78 __asm__ __volatile__ ("bclr %1,(%0)"
80 : "a" (p), "di" (nr & 7)
84 static inline void bclr_mem_clear_bit(int nr, volatile unsigned long *vaddr)
86 char *p = (char *)vaddr + (nr ^ 31) / 8;
88 __asm__ __volatile__ ("bclr %1,%0"
93 static inline void bfclr_mem_clear_bit(int nr, volatile unsigned long *vaddr)
95 __asm__ __volatile__ ("bfclr %1{%0:#1}"
97 : "d" (nr ^ 31), "o" (*vaddr)
101 #if defined(CONFIG_COLDFIRE)
102 #define clear_bit(nr, vaddr) bclr_reg_clear_bit(nr, vaddr)
103 #elif defined(CONFIG_CPU_HAS_NO_BITFIELDS)
104 #define clear_bit(nr, vaddr) bclr_mem_clear_bit(nr, vaddr)
106 #define clear_bit(nr, vaddr) (__builtin_constant_p(nr) ? \
107 bclr_mem_clear_bit(nr, vaddr) : \
108 bfclr_mem_clear_bit(nr, vaddr))
111 static __always_inline void
112 arch___clear_bit(unsigned long nr, volatile unsigned long *addr)
117 static inline void bchg_reg_change_bit(int nr, volatile unsigned long *vaddr)
119 char *p = (char *)vaddr + (nr ^ 31) / 8;
121 __asm__ __volatile__ ("bchg %1,(%0)"
123 : "a" (p), "di" (nr & 7)
127 static inline void bchg_mem_change_bit(int nr, volatile unsigned long *vaddr)
129 char *p = (char *)vaddr + (nr ^ 31) / 8;
131 __asm__ __volatile__ ("bchg %1,%0"
136 static inline void bfchg_mem_change_bit(int nr, volatile unsigned long *vaddr)
138 __asm__ __volatile__ ("bfchg %1{%0:#1}"
140 : "d" (nr ^ 31), "o" (*vaddr)
144 #if defined(CONFIG_COLDFIRE)
145 #define change_bit(nr, vaddr) bchg_reg_change_bit(nr, vaddr)
146 #elif defined(CONFIG_CPU_HAS_NO_BITFIELDS)
147 #define change_bit(nr, vaddr) bchg_mem_change_bit(nr, vaddr)
149 #define change_bit(nr, vaddr) (__builtin_constant_p(nr) ? \
150 bchg_mem_change_bit(nr, vaddr) : \
151 bfchg_mem_change_bit(nr, vaddr))
154 static __always_inline void
155 arch___change_bit(unsigned long nr, volatile unsigned long *addr)
157 change_bit(nr, addr);
160 static __always_inline bool
161 arch_test_bit(unsigned long nr, const volatile unsigned long *addr)
163 return (addr[nr >> 5] & (1UL << (nr & 31))) != 0;
166 static inline int bset_reg_test_and_set_bit(int nr,
167 volatile unsigned long *vaddr)
169 char *p = (char *)vaddr + (nr ^ 31) / 8;
172 __asm__ __volatile__ ("bset %2,(%1); sne %0"
174 : "a" (p), "di" (nr & 7)
179 static inline int bset_mem_test_and_set_bit(int nr,
180 volatile unsigned long *vaddr)
182 char *p = (char *)vaddr + (nr ^ 31) / 8;
185 __asm__ __volatile__ ("bset %2,%1; sne %0"
186 : "=d" (retval), "+m" (*p)
191 static inline int bfset_mem_test_and_set_bit(int nr,
192 volatile unsigned long *vaddr)
196 __asm__ __volatile__ ("bfset %2{%1:#1}; sne %0"
198 : "d" (nr ^ 31), "o" (*vaddr)
203 #if defined(CONFIG_COLDFIRE)
204 #define test_and_set_bit(nr, vaddr) bset_reg_test_and_set_bit(nr, vaddr)
205 #elif defined(CONFIG_CPU_HAS_NO_BITFIELDS)
206 #define test_and_set_bit(nr, vaddr) bset_mem_test_and_set_bit(nr, vaddr)
208 #define test_and_set_bit(nr, vaddr) (__builtin_constant_p(nr) ? \
209 bset_mem_test_and_set_bit(nr, vaddr) : \
210 bfset_mem_test_and_set_bit(nr, vaddr))
213 static __always_inline bool
214 arch___test_and_set_bit(unsigned long nr, volatile unsigned long *addr)
216 return test_and_set_bit(nr, addr);
219 static inline int bclr_reg_test_and_clear_bit(int nr,
220 volatile unsigned long *vaddr)
222 char *p = (char *)vaddr + (nr ^ 31) / 8;
225 __asm__ __volatile__ ("bclr %2,(%1); sne %0"
227 : "a" (p), "di" (nr & 7)
232 static inline int bclr_mem_test_and_clear_bit(int nr,
233 volatile unsigned long *vaddr)
235 char *p = (char *)vaddr + (nr ^ 31) / 8;
238 __asm__ __volatile__ ("bclr %2,%1; sne %0"
239 : "=d" (retval), "+m" (*p)
244 static inline int bfclr_mem_test_and_clear_bit(int nr,
245 volatile unsigned long *vaddr)
249 __asm__ __volatile__ ("bfclr %2{%1:#1}; sne %0"
251 : "d" (nr ^ 31), "o" (*vaddr)
256 #if defined(CONFIG_COLDFIRE)
257 #define test_and_clear_bit(nr, vaddr) bclr_reg_test_and_clear_bit(nr, vaddr)
258 #elif defined(CONFIG_CPU_HAS_NO_BITFIELDS)
259 #define test_and_clear_bit(nr, vaddr) bclr_mem_test_and_clear_bit(nr, vaddr)
261 #define test_and_clear_bit(nr, vaddr) (__builtin_constant_p(nr) ? \
262 bclr_mem_test_and_clear_bit(nr, vaddr) : \
263 bfclr_mem_test_and_clear_bit(nr, vaddr))
266 static __always_inline bool
267 arch___test_and_clear_bit(unsigned long nr, volatile unsigned long *addr)
269 return test_and_clear_bit(nr, addr);
272 static inline int bchg_reg_test_and_change_bit(int nr,
273 volatile unsigned long *vaddr)
275 char *p = (char *)vaddr + (nr ^ 31) / 8;
278 __asm__ __volatile__ ("bchg %2,(%1); sne %0"
280 : "a" (p), "di" (nr & 7)
285 static inline int bchg_mem_test_and_change_bit(int nr,
286 volatile unsigned long *vaddr)
288 char *p = (char *)vaddr + (nr ^ 31) / 8;
291 __asm__ __volatile__ ("bchg %2,%1; sne %0"
292 : "=d" (retval), "+m" (*p)
297 static inline int bfchg_mem_test_and_change_bit(int nr,
298 volatile unsigned long *vaddr)
302 __asm__ __volatile__ ("bfchg %2{%1:#1}; sne %0"
304 : "d" (nr ^ 31), "o" (*vaddr)
309 #if defined(CONFIG_COLDFIRE)
310 #define test_and_change_bit(nr, vaddr) bchg_reg_test_and_change_bit(nr, vaddr)
311 #elif defined(CONFIG_CPU_HAS_NO_BITFIELDS)
312 #define test_and_change_bit(nr, vaddr) bchg_mem_test_and_change_bit(nr, vaddr)
314 #define test_and_change_bit(nr, vaddr) (__builtin_constant_p(nr) ? \
315 bchg_mem_test_and_change_bit(nr, vaddr) : \
316 bfchg_mem_test_and_change_bit(nr, vaddr))
319 static __always_inline bool
320 arch___test_and_change_bit(unsigned long nr, volatile unsigned long *addr)
322 return test_and_change_bit(nr, addr);
326 * The true 68020 and more advanced processors support the "bfffo"
327 * instruction for finding bits. ColdFire and simple 68000 parts
328 * (including CPU32) do not support this. They simply use the generic
331 #if defined(CONFIG_CPU_HAS_NO_BITFIELDS)
332 #include <asm-generic/bitops/ffz.h>
335 static inline int find_first_zero_bit(const unsigned long *vaddr,
338 const unsigned long *p = vaddr;
346 words = (size + 31) >> 5;
347 while (!(num = ~*p++)) {
352 __asm__ __volatile__ ("bfffo %1{#0,#0},%0"
353 : "=d" (res) : "d" (num & -num));
356 res += ((long)p - (long)vaddr - 4) * 8;
357 return res < size ? res : size;
359 #define find_first_zero_bit find_first_zero_bit
361 static inline int find_next_zero_bit(const unsigned long *vaddr, int size,
364 const unsigned long *p = vaddr + (offset >> 5);
365 int bit = offset & 31UL, res;
371 unsigned long num = ~*p++ & (~0UL << bit);
374 /* Look for zero in first longword */
375 __asm__ __volatile__ ("bfffo %1{#0,#0},%0"
376 : "=d" (res) : "d" (num & -num));
379 return offset < size ? offset : size;
386 /* No zero yet, search remaining full bytes for a zero */
387 return offset + find_first_zero_bit(p, size - offset);
389 #define find_next_zero_bit find_next_zero_bit
391 static inline int find_first_bit(const unsigned long *vaddr, unsigned size)
393 const unsigned long *p = vaddr;
401 words = (size + 31) >> 5;
402 while (!(num = *p++)) {
407 __asm__ __volatile__ ("bfffo %1{#0,#0},%0"
408 : "=d" (res) : "d" (num & -num));
411 res += ((long)p - (long)vaddr - 4) * 8;
412 return res < size ? res : size;
414 #define find_first_bit find_first_bit
416 static inline int find_next_bit(const unsigned long *vaddr, int size,
419 const unsigned long *p = vaddr + (offset >> 5);
420 int bit = offset & 31UL, res;
426 unsigned long num = *p++ & (~0UL << bit);
429 /* Look for one in first longword */
430 __asm__ __volatile__ ("bfffo %1{#0,#0},%0"
431 : "=d" (res) : "d" (num & -num));
434 return offset < size ? offset : size;
441 /* No one yet, search remaining full bytes for a one */
442 return offset + find_first_bit(p, size - offset);
444 #define find_next_bit find_next_bit
447 * ffz = Find First Zero in word. Undefined if no zero exists,
448 * so code should check against ~0UL first..
450 static inline unsigned long ffz(unsigned long word)
454 __asm__ __volatile__ ("bfffo %1{#0,#0},%0"
455 : "=d" (res) : "d" (~word & -~word));
463 #if defined(CONFIG_CPU_HAS_NO_BITFIELDS)
466 * The newer ColdFire family members support a "bitrev" instruction
467 * and we can use that to implement a fast ffs. Older Coldfire parts,
468 * and normal 68000 parts don't have anything special, so we use the
469 * generic functions for those.
471 #if (defined(__mcfisaaplus__) || defined(__mcfisac__)) && \
472 !defined(CONFIG_M68000)
473 static inline unsigned long __ffs(unsigned long x)
475 __asm__ __volatile__ ("bitrev %0; ff1 %0"
481 static inline int ffs(int x)
489 #include <asm-generic/bitops/ffs.h>
490 #include <asm-generic/bitops/__ffs.h>
493 #include <asm-generic/bitops/fls.h>
494 #include <asm-generic/bitops/__fls.h>
499 * ffs: find first bit set. This is defined the same way as
500 * the libc and compiler builtin ffs routines, therefore
501 * differs in spirit from the above ffz (man ffs).
503 static inline int ffs(int x)
507 __asm__ ("bfffo %1{#0:#0},%0"
513 static inline unsigned long __ffs(unsigned long x)
519 * fls: find last bit set.
521 static inline int fls(unsigned int x)
525 __asm__ ("bfffo %1{#0,#0},%0"
531 static inline unsigned long __fls(unsigned long x)
538 /* Simple test-and-set bit locks */
539 #define test_and_set_bit_lock test_and_set_bit
540 #define clear_bit_unlock clear_bit
541 #define __clear_bit_unlock clear_bit_unlock
543 #include <asm-generic/bitops/non-instrumented-non-atomic.h>
544 #include <asm-generic/bitops/ext2-atomic.h>
545 #include <asm-generic/bitops/fls64.h>
546 #include <asm-generic/bitops/sched.h>
547 #include <asm-generic/bitops/hweight.h>
548 #include <asm-generic/bitops/le.h>
549 #endif /* __KERNEL__ */
551 #endif /* _M68K_BITOPS_H */