blob: b02c454d04c14ae755b38016ad067040be557c0a [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
Grygorii Strashkoced66ce2018-06-28 14:25:52 -050018#include <asm-generic/bitops/__ffs.h>
19
wdenkc6097192002-11-03 00:24:07 +000020#ifdef __KERNEL__
21
Vasili Galka8e53b1e2014-06-10 16:16:14 +030022#include <asm/proc-armv/system.h>
Simon Kagstrom209a72e2009-08-24 09:10:16 +020023
wdenkc6097192002-11-03 00:24:07 +000024#define smp_mb__before_clear_bit() do { } while (0)
25#define smp_mb__after_clear_bit() do { } while (0)
26
27/*
28 * Function prototypes to keep gcc -Wall happy.
29 */
30extern void set_bit(int nr, volatile void * addr);
31
wdenkc6097192002-11-03 00:24:07 +000032extern void clear_bit(int nr, volatile void * addr);
33
wdenkc6097192002-11-03 00:24:07 +000034extern void change_bit(int nr, volatile void * addr);
35
36static inline void __change_bit(int nr, volatile void *addr)
37{
Simon Kagstrom6fc58222009-08-24 09:10:03 +020038 unsigned long mask = BIT_MASK(nr);
39 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
40
41 *p ^= mask;
wdenkc6097192002-11-03 00:24:07 +000042}
43
wdenkc6097192002-11-03 00:24:07 +000044static inline int __test_and_set_bit(int nr, volatile void *addr)
45{
Simon Kagstrom6fc58222009-08-24 09:10:03 +020046 unsigned long mask = BIT_MASK(nr);
47 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
48 unsigned long old = *p;
wdenkc6097192002-11-03 00:24:07 +000049
Simon Kagstrom6fc58222009-08-24 09:10:03 +020050 *p = old | mask;
51 return (old & mask) != 0;
wdenkc6097192002-11-03 00:24:07 +000052}
53
Simon Kagstrom209a72e2009-08-24 09:10:16 +020054static inline int test_and_set_bit(int nr, volatile void * addr)
55{
Heiko Schocher3c6392f2015-11-30 08:47:42 +010056 unsigned long flags = 0;
Simon Kagstrom209a72e2009-08-24 09:10:16 +020057 int out;
58
59 local_irq_save(flags);
60 out = __test_and_set_bit(nr, addr);
61 local_irq_restore(flags);
62
63 return out;
64}
wdenkc6097192002-11-03 00:24:07 +000065
66static inline int __test_and_clear_bit(int nr, volatile void *addr)
67{
Simon Kagstrom6fc58222009-08-24 09:10:03 +020068 unsigned long mask = BIT_MASK(nr);
69 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
70 unsigned long old = *p;
wdenkc6097192002-11-03 00:24:07 +000071
Simon Kagstrom6fc58222009-08-24 09:10:03 +020072 *p = old & ~mask;
73 return (old & mask) != 0;
wdenkc6097192002-11-03 00:24:07 +000074}
75
Simon Kagstrom209a72e2009-08-24 09:10:16 +020076static inline int test_and_clear_bit(int nr, volatile void * addr)
77{
Heiko Schocher3c6392f2015-11-30 08:47:42 +010078 unsigned long flags = 0;
Simon Kagstrom209a72e2009-08-24 09:10:16 +020079 int out;
80
81 local_irq_save(flags);
82 out = __test_and_clear_bit(nr, addr);
83 local_irq_restore(flags);
84
85 return out;
86}
87
wdenkc6097192002-11-03 00:24:07 +000088extern int test_and_change_bit(int nr, volatile void * addr);
89
90static inline int __test_and_change_bit(int nr, volatile void *addr)
91{
Simon Kagstrom6fc58222009-08-24 09:10:03 +020092 unsigned long mask = BIT_MASK(nr);
93 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
94 unsigned long old = *p;
wdenkc6097192002-11-03 00:24:07 +000095
Simon Kagstrom6fc58222009-08-24 09:10:03 +020096 *p = old ^ mask;
97 return (old & mask) != 0;
wdenkc6097192002-11-03 00:24:07 +000098}
99
wdenkc6097192002-11-03 00:24:07 +0000100/*
101 * This routine doesn't need to be atomic.
102 */
103static inline int test_bit(int nr, const void * addr)
104{
105 return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
106}
107
Rob Herringa6357cb2011-07-05 04:38:51 +0000108static inline int __ilog2(unsigned int x)
109{
110 return generic_fls(x) - 1;
111}
112
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500113#define ffz(x) __ffs(~(x))
wdenkc6097192002-11-03 00:24:07 +0000114
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500115static inline int find_next_zero_bit(void *addr, int size, int offset)
116{
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500117 unsigned long *p = ((unsigned long *)addr) + (offset / BITS_PER_LONG);
118 unsigned long result = offset & ~(BITS_PER_LONG - 1);
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500119 unsigned long tmp;
120
121 if (offset >= size)
122 return size;
123 size -= result;
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500124 offset &= (BITS_PER_LONG - 1);
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500125 if (offset) {
126 tmp = *(p++);
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500127 tmp |= ~0UL >> (BITS_PER_LONG - offset);
128 if (size < BITS_PER_LONG)
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500129 goto found_first;
130 if (~tmp)
131 goto found_middle;
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500132 size -= BITS_PER_LONG;
133 result += BITS_PER_LONG;
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500134 }
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500135 while (size & ~(BITS_PER_LONG - 1)) {
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500136 tmp = *(p++);
137 if (~tmp)
138 goto found_middle;
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500139 result += BITS_PER_LONG;
140 size -= BITS_PER_LONG;
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500141 }
142 if (!size)
143 return result;
144 tmp = *p;
145
146found_first:
Grygorii Strashkocdcda672018-04-27 19:58:49 -0500147 tmp |= ~0UL << size;
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500148found_middle:
149 return result + ffz(tmp);
150}
151
wdenkc6097192002-11-03 00:24:07 +0000152/*
wdenkc6097192002-11-03 00:24:07 +0000153 * hweightN: returns the hamming weight (i.e. the number
154 * of bits set) of a N-bit word
155 */
156
157#define hweight32(x) generic_hweight32(x)
158#define hweight16(x) generic_hweight16(x)
159#define hweight8(x) generic_hweight8(x)
160
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500161#define find_first_zero_bit(addr, size) \
162 find_next_zero_bit((addr), (size), 0)
163
wdenkc6097192002-11-03 00:24:07 +0000164#define ext2_set_bit test_and_set_bit
165#define ext2_clear_bit test_and_clear_bit
166#define ext2_test_bit test_bit
167#define ext2_find_first_zero_bit find_first_zero_bit
168#define ext2_find_next_zero_bit find_next_zero_bit
169
170/* Bitmap functions for the minix filesystem. */
171#define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
172#define minix_set_bit(nr,addr) set_bit(nr,addr)
173#define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
174#define minix_test_bit(nr,addr) test_bit(nr,addr)
175#define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
176
177#endif /* __KERNEL__ */
178
Fabio Estevamd24ab142015-11-05 12:43:24 -0200179#include <asm-generic/bitops/__fls.h>
Fabio Estevamd24ab142015-11-05 12:43:24 -0200180#include <asm-generic/bitops/fls.h>
181#include <asm-generic/bitops/fls64.h>
182
wdenkc6097192002-11-03 00:24:07 +0000183#endif /* _ARM_BITOPS_H */