blob: 270f163eee3fb5ebe190cec4352c19901361eba9 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * Copyright 1995, Russell King.
3 * Various bits and pieces copyrights include:
4 * Linus Torvalds (test_bit).
5 *
6 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
7 *
8 * Please note that the code in this file should never be included
9 * from user space. Many of these are not implemented in assembler
10 * since they would be too costly. Also, they require priviledged
11 * instructions (which are not available from user mode) to ensure
12 * that they are atomic.
13 */
14
15#ifndef __ASM_ARM_BITOPS_H
16#define __ASM_ARM_BITOPS_H
17
18#ifdef __KERNEL__
19
Simon Kagstrom209a72e2009-08-24 09:10:16 +020020#include <asm/proc/system.h>
21
wdenkc6097192002-11-03 00:24:07 +000022#define smp_mb__before_clear_bit() do { } while (0)
23#define smp_mb__after_clear_bit() do { } while (0)
24
25/*
26 * Function prototypes to keep gcc -Wall happy.
27 */
28extern void set_bit(int nr, volatile void * addr);
29
wdenkc6097192002-11-03 00:24:07 +000030extern void clear_bit(int nr, volatile void * addr);
31
wdenkc6097192002-11-03 00:24:07 +000032extern void change_bit(int nr, volatile void * addr);
33
34static inline void __change_bit(int nr, volatile void *addr)
35{
Simon Kagstrom6fc58222009-08-24 09:10:03 +020036 unsigned long mask = BIT_MASK(nr);
37 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
38
39 *p ^= mask;
wdenkc6097192002-11-03 00:24:07 +000040}
41
wdenkc6097192002-11-03 00:24:07 +000042static inline int __test_and_set_bit(int nr, volatile void *addr)
43{
Simon Kagstrom6fc58222009-08-24 09:10:03 +020044 unsigned long mask = BIT_MASK(nr);
45 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
46 unsigned long old = *p;
wdenkc6097192002-11-03 00:24:07 +000047
Simon Kagstrom6fc58222009-08-24 09:10:03 +020048 *p = old | mask;
49 return (old & mask) != 0;
wdenkc6097192002-11-03 00:24:07 +000050}
51
Simon Kagstrom209a72e2009-08-24 09:10:16 +020052static inline int test_and_set_bit(int nr, volatile void * addr)
53{
54 unsigned long flags;
55 int out;
56
57 local_irq_save(flags);
58 out = __test_and_set_bit(nr, addr);
59 local_irq_restore(flags);
60
61 return out;
62}
wdenkc6097192002-11-03 00:24:07 +000063
64static inline int __test_and_clear_bit(int nr, volatile void *addr)
65{
Simon Kagstrom6fc58222009-08-24 09:10:03 +020066 unsigned long mask = BIT_MASK(nr);
67 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
68 unsigned long old = *p;
wdenkc6097192002-11-03 00:24:07 +000069
Simon Kagstrom6fc58222009-08-24 09:10:03 +020070 *p = old & ~mask;
71 return (old & mask) != 0;
wdenkc6097192002-11-03 00:24:07 +000072}
73
Simon Kagstrom209a72e2009-08-24 09:10:16 +020074static inline int test_and_clear_bit(int nr, volatile void * addr)
75{
76 unsigned long flags;
77 int out;
78
79 local_irq_save(flags);
80 out = __test_and_clear_bit(nr, addr);
81 local_irq_restore(flags);
82
83 return out;
84}
85
wdenkc6097192002-11-03 00:24:07 +000086extern int test_and_change_bit(int nr, volatile void * addr);
87
88static inline int __test_and_change_bit(int nr, volatile void *addr)
89{
Simon Kagstrom6fc58222009-08-24 09:10:03 +020090 unsigned long mask = BIT_MASK(nr);
91 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
92 unsigned long old = *p;
wdenkc6097192002-11-03 00:24:07 +000093
Simon Kagstrom6fc58222009-08-24 09:10:03 +020094 *p = old ^ mask;
95 return (old & mask) != 0;
wdenkc6097192002-11-03 00:24:07 +000096}
97
98extern int find_first_zero_bit(void * addr, unsigned size);
99extern int find_next_zero_bit(void * addr, int size, int offset);
100
101/*
102 * This routine doesn't need to be atomic.
103 */
104static inline int test_bit(int nr, const void * addr)
105{
106 return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
107}
108
109/*
110 * ffz = Find First Zero in word. Undefined if no zero exists,
111 * so code should check against ~0UL first..
112 */
113static inline unsigned long ffz(unsigned long word)
114{
115 int k;
116
117 word = ~word;
118 k = 31;
119 if (word & 0x0000ffff) { k -= 16; word <<= 16; }
120 if (word & 0x00ff0000) { k -= 8; word <<= 8; }
121 if (word & 0x0f000000) { k -= 4; word <<= 4; }
122 if (word & 0x30000000) { k -= 2; word <<= 2; }
123 if (word & 0x40000000) { k -= 1; }
wdenk57b2d802003-06-27 21:31:46 +0000124 return k;
wdenkc6097192002-11-03 00:24:07 +0000125}
126
127/*
wdenkc6097192002-11-03 00:24:07 +0000128 * hweightN: returns the hamming weight (i.e. the number
129 * of bits set) of a N-bit word
130 */
131
132#define hweight32(x) generic_hweight32(x)
133#define hweight16(x) generic_hweight16(x)
134#define hweight8(x) generic_hweight8(x)
135
136#define ext2_set_bit test_and_set_bit
137#define ext2_clear_bit test_and_clear_bit
138#define ext2_test_bit test_bit
139#define ext2_find_first_zero_bit find_first_zero_bit
140#define ext2_find_next_zero_bit find_next_zero_bit
141
142/* Bitmap functions for the minix filesystem. */
143#define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
144#define minix_set_bit(nr,addr) set_bit(nr,addr)
145#define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
146#define minix_test_bit(nr,addr) test_bit(nr,addr)
147#define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
148
149#endif /* __KERNEL__ */
150
151#endif /* _ARM_BITOPS_H */