1 /* 2 * Copyright (c) 2011 The Chromium OS Authors. 3 * 4 * Copyright 1995, Russell King. 5 * Various bits and pieces copyrights include: 6 * Linus Torvalds (test_bit). 7 * 8 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1). 9 * 10 * Please note that the code in this file should never be included 11 * from user space. Many of these are not implemented in assembler 12 * since they would be too costly. Also, they require priviledged 13 * instructions (which are not available from user mode) to ensure 14 * that they are atomic. 15 */ 16 17 #ifndef __ASM_SANDBOX_BITOPS_H 18 #define __ASM_SANDBOX_BITOPS_H 19 20 #include <linux/compiler.h> 21 #include <asm/system.h> 22 23 #ifdef __KERNEL__ 24 25 #define smp_mb__before_clear_bit() do { } while (0) 26 #define smp_mb__after_clear_bit() do { } while (0) 27 28 /* 29 * Function prototypes to keep gcc -Wall happy. 30 */ 31 extern void set_bit(int nr, void *addr); 32 33 extern void clear_bit(int nr, void *addr); 34 35 extern void change_bit(int nr, void *addr); 36 37 static inline void __change_bit(int nr, void *addr) 38 { 39 unsigned long mask = BIT_MASK(nr); 40 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); 41 42 *p ^= mask; 43 } 44 45 static inline int __test_and_set_bit(int nr, void *addr) 46 { 47 unsigned long mask = BIT_MASK(nr); 48 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); 49 unsigned long old = *p; 50 51 *p = old | mask; 52 return (old & mask) != 0; 53 } 54 55 static inline int test_and_set_bit(int nr, void *addr) 56 { 57 unsigned long __always_unused flags; 58 int out; 59 60 local_irq_save(flags); 61 out = __test_and_set_bit(nr, addr); 62 local_irq_restore(flags); 63 64 return out; 65 } 66 67 static inline int __test_and_clear_bit(int nr, void *addr) 68 { 69 unsigned long mask = BIT_MASK(nr); 70 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); 71 unsigned long old = *p; 72 73 *p = old & ~mask; 74 return (old & mask) != 0; 75 } 76 77 static inline int test_and_clear_bit(int nr, void *addr) 78 { 79 unsigned long __always_unused flags; 80 int out; 81 82 local_irq_save(flags); 83 out = __test_and_clear_bit(nr, addr); 84 local_irq_restore(flags); 85 86 return out; 87 } 88 89 extern int test_and_change_bit(int nr, void *addr); 90 91 static inline int __test_and_change_bit(int nr, void *addr) 92 { 93 unsigned long mask = BIT_MASK(nr); 94 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); 95 unsigned long old = *p; 96 97 *p = old ^ mask; 98 return (old & mask) != 0; 99 } 100 101 extern int find_first_zero_bit(void *addr, unsigned size); 102 extern int find_next_zero_bit(void *addr, int size, int offset); 103 104 /* 105 * This routine doesn't need to be atomic. 106 */ 107 static inline int test_bit(int nr, const void *addr) 108 { 109 return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7)); 110 } 111 112 /* 113 * ffz = Find First Zero in word. Undefined if no zero exists, 114 * so code should check against ~0UL first.. 115 */ 116 static inline unsigned long ffz(unsigned long word) 117 { 118 int k; 119 120 word = ~word; 121 k = 31; 122 if (word & 0x0000ffff) { 123 k -= 16; word <<= 16; 124 } 125 if (word & 0x00ff0000) { 126 k -= 8; word <<= 8; 127 } 128 if (word & 0x0f000000) { 129 k -= 4; word <<= 4; 130 } 131 if (word & 0x30000000) { 132 k -= 2; word <<= 2; 133 } 134 if (word & 0x40000000) 135 k -= 1; 136 return k; 137 } 138 139 /* 140 * hweightN: returns the hamming weight (i.e. the number 141 * of bits set) of a N-bit word 142 */ 143 144 #define hweight32(x) generic_hweight32(x) 145 #define hweight16(x) generic_hweight16(x) 146 #define hweight8(x) generic_hweight8(x) 147 148 #define ext2_set_bit test_and_set_bit 149 #define ext2_clear_bit test_and_clear_bit 150 #define ext2_test_bit test_bit 151 #define ext2_find_first_zero_bit find_first_zero_bit 152 #define ext2_find_next_zero_bit find_next_zero_bit 153 154 /* Bitmap functions for the minix filesystem. */ 155 #define minix_test_and_set_bit(nr, addr) test_and_set_bit(nr, addr) 156 #define minix_set_bit(nr, addr) set_bit(nr, addr) 157 #define minix_test_and_clear_bit(nr, addr) test_and_clear_bit(nr, addr) 158 #define minix_test_bit(nr, addr) test_bit(nr, addr) 159 #define minix_find_first_zero_bit(addr, size) find_first_zero_bit(addr, size) 160 161 #endif /* __KERNEL__ */ 162 163 #endif /* _ARM_BITOPS_H */ 164