Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* asm/bitops.h for Linux/CRIS |
| 2 | * |
| 3 | * TODO: asm versions if speed is needed |
| 4 | * |
| 5 | * All bit operations return 0 if the bit was cleared before the |
| 6 | * operation and != 0 if it was not. |
| 7 | * |
| 8 | * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1). |
| 9 | */ |
| 10 | |
| 11 | #ifndef _CRIS_BITOPS_H |
| 12 | #define _CRIS_BITOPS_H |
| 13 | |
| 14 | /* Currently this is unsuitable for consumption outside the kernel. */ |
| 15 | #ifdef __KERNEL__ |
| 16 | |
| 17 | #include <asm/arch/bitops.h> |
| 18 | #include <asm/system.h> |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 19 | #include <asm/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/compiler.h> |
| 21 | |
| 22 | /* |
| 23 | * Some hacks to defeat gcc over-optimizations.. |
| 24 | */ |
| 25 | struct __dummy { unsigned long a[100]; }; |
| 26 | #define ADDR (*(struct __dummy *) addr) |
| 27 | #define CONST_ADDR (*(const struct __dummy *) addr) |
| 28 | |
| 29 | /* |
| 30 | * set_bit - Atomically set a bit in memory |
| 31 | * @nr: the bit to set |
| 32 | * @addr: the address to start counting from |
| 33 | * |
| 34 | * This function is atomic and may not be reordered. See __set_bit() |
| 35 | * if you do not require the atomic guarantees. |
| 36 | * Note that @nr may be almost arbitrarily large; this function is not |
| 37 | * restricted to acting on a single-word quantity. |
| 38 | */ |
| 39 | |
| 40 | #define set_bit(nr, addr) (void)test_and_set_bit(nr, addr) |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | /* |
| 43 | * clear_bit - Clears a bit in memory |
| 44 | * @nr: Bit to clear |
| 45 | * @addr: Address to start counting from |
| 46 | * |
| 47 | * clear_bit() is atomic and may not be reordered. However, it does |
| 48 | * not contain a memory barrier, so if it is used for locking purposes, |
| 49 | * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit() |
| 50 | * in order to ensure changes are visible on other processors. |
| 51 | */ |
| 52 | |
| 53 | #define clear_bit(nr, addr) (void)test_and_clear_bit(nr, addr) |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* |
| 56 | * change_bit - Toggle a bit in memory |
| 57 | * @nr: Bit to change |
| 58 | * @addr: Address to start counting from |
| 59 | * |
| 60 | * change_bit() is atomic and may not be reordered. |
| 61 | * Note that @nr may be almost arbitrarily large; this function is not |
| 62 | * restricted to acting on a single-word quantity. |
| 63 | */ |
| 64 | |
| 65 | #define change_bit(nr, addr) (void)test_and_change_bit(nr, addr) |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /** |
| 68 | * test_and_set_bit - Set a bit and return its old value |
| 69 | * @nr: Bit to set |
| 70 | * @addr: Address to count from |
| 71 | * |
| 72 | * This operation is atomic and cannot be reordered. |
| 73 | * It also implies a memory barrier. |
| 74 | */ |
| 75 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 76 | static inline int test_and_set_bit(int nr, volatile unsigned long *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
| 78 | unsigned int mask, retval; |
| 79 | unsigned long flags; |
| 80 | unsigned int *adr = (unsigned int *)addr; |
| 81 | |
| 82 | adr += nr >> 5; |
| 83 | mask = 1 << (nr & 0x1f); |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 84 | cris_atomic_save(addr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | retval = (mask & *adr) != 0; |
| 86 | *adr |= mask; |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 87 | cris_atomic_restore(addr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | return retval; |
| 89 | } |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | /* |
| 92 | * clear_bit() doesn't provide any barrier for the compiler. |
| 93 | */ |
| 94 | #define smp_mb__before_clear_bit() barrier() |
| 95 | #define smp_mb__after_clear_bit() barrier() |
| 96 | |
| 97 | /** |
| 98 | * test_and_clear_bit - Clear a bit and return its old value |
| 99 | * @nr: Bit to clear |
| 100 | * @addr: Address to count from |
| 101 | * |
| 102 | * This operation is atomic and cannot be reordered. |
| 103 | * It also implies a memory barrier. |
| 104 | */ |
| 105 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 106 | static inline int test_and_clear_bit(int nr, volatile unsigned long *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | unsigned int mask, retval; |
| 109 | unsigned long flags; |
| 110 | unsigned int *adr = (unsigned int *)addr; |
| 111 | |
| 112 | adr += nr >> 5; |
| 113 | mask = 1 << (nr & 0x1f); |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 114 | cris_atomic_save(addr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | retval = (mask & *adr) != 0; |
| 116 | *adr &= ~mask; |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 117 | cris_atomic_restore(addr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return retval; |
| 119 | } |
| 120 | |
| 121 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | * test_and_change_bit - Change a bit and return its old value |
| 123 | * @nr: Bit to change |
| 124 | * @addr: Address to count from |
| 125 | * |
| 126 | * This operation is atomic and cannot be reordered. |
| 127 | * It also implies a memory barrier. |
| 128 | */ |
| 129 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 130 | static inline int test_and_change_bit(int nr, volatile unsigned long *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | unsigned int mask, retval; |
| 133 | unsigned long flags; |
| 134 | unsigned int *adr = (unsigned int *)addr; |
| 135 | adr += nr >> 5; |
| 136 | mask = 1 << (nr & 0x1f); |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 137 | cris_atomic_save(addr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | retval = (mask & *adr) != 0; |
| 139 | *adr ^= mask; |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 140 | cris_atomic_restore(addr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return retval; |
| 142 | } |
| 143 | |
Akinobu Mita | e9f26df | 2006-03-26 01:39:21 -0800 | [diff] [blame] | 144 | #include <asm-generic/bitops/non-atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | /* |
| 147 | * Since we define it "external", it collides with the built-in |
| 148 | * definition, which doesn't have the same semantics. We don't want to |
| 149 | * use -fno-builtin, so just hide the name ffs. |
| 150 | */ |
| 151 | #define ffs kernel_ffs |
| 152 | |
Akinobu Mita | e9f26df | 2006-03-26 01:39:21 -0800 | [diff] [blame] | 153 | #include <asm-generic/bitops/fls.h> |
| 154 | #include <asm-generic/bitops/fls64.h> |
| 155 | #include <asm-generic/bitops/hweight.h> |
| 156 | #include <asm-generic/bitops/find.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Akinobu Mita | e9f26df | 2006-03-26 01:39:21 -0800 | [diff] [blame] | 158 | #include <asm-generic/bitops/ext2-non-atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | #define ext2_set_bit_atomic(l,n,a) test_and_set_bit(n,a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | #define ext2_clear_bit_atomic(l,n,a) test_and_clear_bit(n,a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Akinobu Mita | e9f26df | 2006-03-26 01:39:21 -0800 | [diff] [blame] | 163 | #include <asm-generic/bitops/minix.h> |
| 164 | #include <asm-generic/bitops/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | #endif /* __KERNEL__ */ |
| 167 | |
| 168 | #endif /* _CRIS_BITOPS_H */ |