2 * Copyright (C) 2004-2006 Atmel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 #ifndef __ASM_AVR32_BITOPS_H
9 #define __ASM_AVR32_BITOPS_H
11 #include <asm/byteorder.h>
12 #include <asm/system.h>
15 * clear_bit() doesn't provide any barrier for the compiler
17 #define smp_mb__before_clear_bit() barrier()
18 #define smp_mb__after_clear_bit() barrier()
21 * set_bit - Atomically set a bit in memory
23 * @addr: the address to start counting from
25 * This function is atomic and may not be reordered. See __set_bit()
26 * if you do not require the atomic guarantees.
28 * Note that @nr may be almost arbitrarily large; this function is not
29 * restricted to acting on a single-word quantity.
31 static inline void set_bit(int nr, volatile void * addr)
33 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
36 if (__builtin_constant_p(nr)) {
43 : "=&r"(tmp), "=o"(*p)
47 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
54 : "=&r"(tmp), "=o"(*p)
61 * clear_bit - Clears a bit in memory
63 * @addr: Address to start counting from
65 * clear_bit() is atomic and may not be reordered. However, it does
66 * not contain a memory barrier, so if it is used for locking purposes,
67 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
68 * in order to ensure changes are visible on other processors.
70 static inline void clear_bit(int nr, volatile void * addr)
72 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
75 if (__builtin_constant_p(nr)) {
82 : "=&r"(tmp), "=o"(*p)
86 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
93 : "=&r"(tmp), "=o"(*p)
100 * change_bit - Toggle a bit in memory
102 * @addr: Address to start counting from
104 * change_bit() is atomic and may not be reordered.
105 * Note that @nr may be almost arbitrarily large; this function is not
106 * restricted to acting on a single-word quantity.
108 static inline void change_bit(int nr, volatile void * addr)
110 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
111 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
120 : "=&r"(tmp), "=o"(*p)
126 * test_and_set_bit - Set a bit and return its old value
128 * @addr: Address to count from
130 * This operation is atomic and cannot be reordered.
131 * It also implies a memory barrier.
133 static inline int test_and_set_bit(int nr, volatile void * addr)
135 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
136 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
137 unsigned long tmp, old;
139 if (__builtin_constant_p(nr)) {
147 : "=&r"(tmp), "=o"(*p), "=&r"(old)
157 : "=&r"(tmp), "=o"(*p), "=&r"(old)
162 return (old & mask) != 0;
166 * test_and_clear_bit - Clear a bit and return its old value
168 * @addr: Address to count from
170 * This operation is atomic and cannot be reordered.
171 * It also implies a memory barrier.
173 static inline int test_and_clear_bit(int nr, volatile void * addr)
175 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
176 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
177 unsigned long tmp, old;
179 if (__builtin_constant_p(nr)) {
187 : "=&r"(tmp), "=o"(*p), "=&r"(old)
198 : "=&r"(tmp), "=o"(*p), "=&r"(old)
203 return (old & mask) != 0;
207 * test_and_change_bit - Change a bit and return its old value
209 * @addr: Address to count from
211 * This operation is atomic and cannot be reordered.
212 * It also implies a memory barrier.
214 static inline int test_and_change_bit(int nr, volatile void * addr)
216 unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
217 unsigned long mask = 1UL << (nr % BITS_PER_LONG);
218 unsigned long tmp, old;
226 : "=&r"(tmp), "=o"(*p), "=&r"(old)
230 return (old & mask) != 0;
233 #include <asm-generic/bitops/non-atomic.h>
235 /* Find First bit Set */
236 static inline unsigned long __ffs(unsigned long word)
238 unsigned long result;
242 : "=r"(result), "=&r"(word)
247 /* Find First Zero */
248 static inline unsigned long ffz(unsigned long word)
253 /* Find Last bit Set */
254 static inline int fls(unsigned long word)
256 unsigned long result;
258 asm("clz %0,%1" : "=r"(result) : "r"(word));
262 unsigned long find_first_zero_bit(const unsigned long *addr,
264 unsigned long find_next_zero_bit(const unsigned long *addr,
266 unsigned long offset);
267 unsigned long find_first_bit(const unsigned long *addr,
269 unsigned long find_next_bit(const unsigned long *addr,
271 unsigned long offset);
274 * ffs: find first bit set. This is defined the same way as
275 * the libc and compiler builtin ffs routines, therefore
276 * differs in spirit from the above ffz (man ffs).
278 * The difference is that bit numbering starts at 1, and if no bit is set,
279 * the function returns 0.
281 static inline int ffs(unsigned long word)
285 return __ffs(word) + 1;
288 #include <asm-generic/bitops/fls64.h>
289 #include <asm-generic/bitops/sched.h>
290 #include <asm-generic/bitops/hweight.h>
292 #include <asm-generic/bitops/ext2-non-atomic.h>
293 #include <asm-generic/bitops/ext2-atomic.h>
294 #include <asm-generic/bitops/minix-le.h>
296 #endif /* __ASM_AVR32_BITOPS_H */