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 | |
Jiri Slaby | 0624517 | 2007-10-18 23:40:26 -0700 | [diff] [blame] | 17 | #ifndef _LINUX_BITOPS_H |
| 18 | #error only <linux/bitops.h> can be included directly |
| 19 | #endif |
| 20 | |
Jesper Nilsson | 556dcee | 2008-10-21 17:45:58 +0200 | [diff] [blame] | 21 | #include <arch/bitops.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 22 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/compiler.h> |
| 24 | |
| 25 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | * set_bit - Atomically set a bit in memory |
| 27 | * @nr: the bit to set |
| 28 | * @addr: the address to start counting from |
| 29 | * |
| 30 | * This function is atomic and may not be reordered. See __set_bit() |
| 31 | * if you do not require the atomic guarantees. |
| 32 | * Note that @nr may be almost arbitrarily large; this function is not |
| 33 | * restricted to acting on a single-word quantity. |
| 34 | */ |
| 35 | |
| 36 | #define set_bit(nr, addr) (void)test_and_set_bit(nr, addr) |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | /* |
| 39 | * clear_bit - Clears a bit in memory |
| 40 | * @nr: Bit to clear |
| 41 | * @addr: Address to start counting from |
| 42 | * |
| 43 | * clear_bit() is atomic and may not be reordered. However, it does |
| 44 | * not contain a memory barrier, so if it is used for locking purposes, |
| 45 | * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit() |
| 46 | * in order to ensure changes are visible on other processors. |
| 47 | */ |
| 48 | |
| 49 | #define clear_bit(nr, addr) (void)test_and_clear_bit(nr, addr) |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /* |
| 52 | * change_bit - Toggle a bit in memory |
| 53 | * @nr: Bit to change |
| 54 | * @addr: Address to start counting from |
| 55 | * |
| 56 | * change_bit() is atomic and may not be reordered. |
| 57 | * Note that @nr may be almost arbitrarily large; this function is not |
| 58 | * restricted to acting on a single-word quantity. |
| 59 | */ |
| 60 | |
| 61 | #define change_bit(nr, addr) (void)test_and_change_bit(nr, addr) |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | /** |
| 64 | * test_and_set_bit - Set a bit and return its old value |
| 65 | * @nr: Bit to set |
| 66 | * @addr: Address to count from |
| 67 | * |
| 68 | * This operation is atomic and cannot be reordered. |
| 69 | * It also implies a memory barrier. |
| 70 | */ |
| 71 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 72 | 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] | 73 | { |
| 74 | unsigned int mask, retval; |
| 75 | unsigned long flags; |
| 76 | unsigned int *adr = (unsigned int *)addr; |
| 77 | |
| 78 | adr += nr >> 5; |
| 79 | mask = 1 << (nr & 0x1f); |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 80 | cris_atomic_save(addr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | retval = (mask & *adr) != 0; |
| 82 | *adr |= mask; |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 83 | cris_atomic_restore(addr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return retval; |
| 85 | } |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* |
| 88 | * clear_bit() doesn't provide any barrier for the compiler. |
| 89 | */ |
| 90 | #define smp_mb__before_clear_bit() barrier() |
| 91 | #define smp_mb__after_clear_bit() barrier() |
| 92 | |
| 93 | /** |
| 94 | * test_and_clear_bit - Clear a bit and return its old value |
| 95 | * @nr: Bit to clear |
| 96 | * @addr: Address to count from |
| 97 | * |
| 98 | * This operation is atomic and cannot be reordered. |
| 99 | * It also implies a memory barrier. |
| 100 | */ |
| 101 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 102 | 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] | 103 | { |
| 104 | unsigned int mask, retval; |
| 105 | unsigned long flags; |
| 106 | unsigned int *adr = (unsigned int *)addr; |
| 107 | |
| 108 | adr += nr >> 5; |
| 109 | mask = 1 << (nr & 0x1f); |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 110 | cris_atomic_save(addr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | retval = (mask & *adr) != 0; |
| 112 | *adr &= ~mask; |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 113 | cris_atomic_restore(addr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return retval; |
| 115 | } |
| 116 | |
| 117 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | * test_and_change_bit - Change a bit and return its old value |
| 119 | * @nr: Bit to change |
| 120 | * @addr: Address to count from |
| 121 | * |
| 122 | * This operation is atomic and cannot be reordered. |
| 123 | * It also implies a memory barrier. |
| 124 | */ |
| 125 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 126 | 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] | 127 | { |
| 128 | unsigned int mask, retval; |
| 129 | unsigned long flags; |
| 130 | unsigned int *adr = (unsigned int *)addr; |
| 131 | adr += nr >> 5; |
| 132 | mask = 1 << (nr & 0x1f); |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 133 | cris_atomic_save(addr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | retval = (mask & *adr) != 0; |
| 135 | *adr ^= mask; |
Mikael Starvik | 5d01e6c | 2005-07-27 11:44:43 -0700 | [diff] [blame] | 136 | cris_atomic_restore(addr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return retval; |
| 138 | } |
| 139 | |
Akinobu Mita | e9f26df | 2006-03-26 01:39:21 -0800 | [diff] [blame] | 140 | #include <asm-generic/bitops/non-atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Since we define it "external", it collides with the built-in |
| 144 | * definition, which doesn't have the same semantics. We don't want to |
| 145 | * use -fno-builtin, so just hide the name ffs. |
| 146 | */ |
| 147 | #define ffs kernel_ffs |
| 148 | |
Akinobu Mita | e9f26df | 2006-03-26 01:39:21 -0800 | [diff] [blame] | 149 | #include <asm-generic/bitops/fls.h> |
Rusty Russell | 0999769 | 2009-01-03 15:37:14 +1030 | [diff] [blame] | 150 | #include <asm-generic/bitops/__fls.h> |
Akinobu Mita | e9f26df | 2006-03-26 01:39:21 -0800 | [diff] [blame] | 151 | #include <asm-generic/bitops/fls64.h> |
| 152 | #include <asm-generic/bitops/hweight.h> |
| 153 | #include <asm-generic/bitops/find.h> |
Nick Piggin | 2633357 | 2007-10-18 03:06:39 -0700 | [diff] [blame] | 154 | #include <asm-generic/bitops/lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Akinobu Mita | 861b5ae | 2011-03-23 16:42:02 -0700 | [diff] [blame] | 156 | #include <asm-generic/bitops/le.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Akinobu Mita | 148817b | 2011-07-26 16:09:04 -0700 | [diff] [blame] | 158 | #include <asm-generic/bitops/ext2-atomic-setbit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Akinobu Mita | e9f26df | 2006-03-26 01:39:21 -0800 | [diff] [blame] | 160 | #include <asm-generic/bitops/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | #endif /* __KERNEL__ */ |
| 163 | |
| 164 | #endif /* _CRIS_BITOPS_H */ |