blob: fa8548624a049979f4d8895375cfb1a3d090f488 [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>
Simon Glass4dcacfc2020-05-10 11:40:13 -060019#include <asm-generic/bitops/__fls.h>
20#include <asm-generic/bitops/fls.h>
21#include <asm-generic/bitops/fls64.h>
Grygorii Strashkoced66ce2018-06-28 14:25:52 -050022
wdenkc6097192002-11-03 00:24:07 +000023#ifdef __KERNEL__
24
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#ifndef __ASSEMBLY__
26#include <linux/bitops.h>
27#endif
Vasili Galka8e53b1e2014-06-10 16:16:14 +030028#include <asm/proc-armv/system.h>
Simon Kagstrom209a72e2009-08-24 09:10:16 +020029
wdenkc6097192002-11-03 00:24:07 +000030#define smp_mb__before_clear_bit() do { } while (0)
31#define smp_mb__after_clear_bit() do { } while (0)
32
33/*
34 * Function prototypes to keep gcc -Wall happy.
35 */
36extern void set_bit(int nr, volatile void * addr);
37
wdenkc6097192002-11-03 00:24:07 +000038extern void clear_bit(int nr, volatile void * addr);
39
wdenkc6097192002-11-03 00:24:07 +000040extern void change_bit(int nr, volatile void * addr);
41
42static inline void __change_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
47 *p ^= mask;
wdenkc6097192002-11-03 00:24:07 +000048}
49
wdenkc6097192002-11-03 00:24:07 +000050static inline int __test_and_set_bit(int nr, volatile void *addr)
51{
Simon Kagstrom6fc58222009-08-24 09:10:03 +020052 unsigned long mask = BIT_MASK(nr);
53 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
54 unsigned long old = *p;
wdenkc6097192002-11-03 00:24:07 +000055
Simon Kagstrom6fc58222009-08-24 09:10:03 +020056 *p = old | mask;
57 return (old & mask) != 0;
wdenkc6097192002-11-03 00:24:07 +000058}
59
Simon Kagstrom209a72e2009-08-24 09:10:16 +020060static inline int test_and_set_bit(int nr, volatile void * addr)
61{
Heiko Schocher3c6392f2015-11-30 08:47:42 +010062 unsigned long flags = 0;
Simon Kagstrom209a72e2009-08-24 09:10:16 +020063 int out;
64
65 local_irq_save(flags);
66 out = __test_and_set_bit(nr, addr);
67 local_irq_restore(flags);
68
69 return out;
70}
wdenkc6097192002-11-03 00:24:07 +000071
72static inline int __test_and_clear_bit(int nr, volatile void *addr)
73{
Simon Kagstrom6fc58222009-08-24 09:10:03 +020074 unsigned long mask = BIT_MASK(nr);
75 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
76 unsigned long old = *p;
wdenkc6097192002-11-03 00:24:07 +000077
Simon Kagstrom6fc58222009-08-24 09:10:03 +020078 *p = old & ~mask;
79 return (old & mask) != 0;
wdenkc6097192002-11-03 00:24:07 +000080}
81
Simon Kagstrom209a72e2009-08-24 09:10:16 +020082static inline int test_and_clear_bit(int nr, volatile void * addr)
83{
Heiko Schocher3c6392f2015-11-30 08:47:42 +010084 unsigned long flags = 0;
Simon Kagstrom209a72e2009-08-24 09:10:16 +020085 int out;
86
87 local_irq_save(flags);
88 out = __test_and_clear_bit(nr, addr);
89 local_irq_restore(flags);
90
91 return out;
92}
93
wdenkc6097192002-11-03 00:24:07 +000094extern int test_and_change_bit(int nr, volatile void * addr);
95
96static inline int __test_and_change_bit(int nr, volatile void *addr)
97{
Simon Kagstrom6fc58222009-08-24 09:10:03 +020098 unsigned long mask = BIT_MASK(nr);
99 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
100 unsigned long old = *p;
wdenkc6097192002-11-03 00:24:07 +0000101
Simon Kagstrom6fc58222009-08-24 09:10:03 +0200102 *p = old ^ mask;
103 return (old & mask) != 0;
wdenkc6097192002-11-03 00:24:07 +0000104}
105
wdenkc6097192002-11-03 00:24:07 +0000106/*
107 * This routine doesn't need to be atomic.
108 */
109static inline int test_bit(int nr, const void * addr)
110{
111 return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
112}
113
Rob Herringa6357cb2011-07-05 04:38:51 +0000114static inline int __ilog2(unsigned int x)
115{
116 return generic_fls(x) - 1;
117}
118
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500119#define ffz(x) __ffs(~(x))
wdenkc6097192002-11-03 00:24:07 +0000120
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500121static inline int find_next_zero_bit(void *addr, int size, int offset)
122{
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500123 unsigned long *p = ((unsigned long *)addr) + (offset / BITS_PER_LONG);
124 unsigned long result = offset & ~(BITS_PER_LONG - 1);
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500125 unsigned long tmp;
126
127 if (offset >= size)
128 return size;
129 size -= result;
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500130 offset &= (BITS_PER_LONG - 1);
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500131 if (offset) {
132 tmp = *(p++);
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500133 tmp |= ~0UL >> (BITS_PER_LONG - offset);
134 if (size < BITS_PER_LONG)
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500135 goto found_first;
136 if (~tmp)
137 goto found_middle;
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500138 size -= BITS_PER_LONG;
139 result += BITS_PER_LONG;
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500140 }
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500141 while (size & ~(BITS_PER_LONG - 1)) {
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500142 tmp = *(p++);
143 if (~tmp)
144 goto found_middle;
Grygorii Strashkoced66ce2018-06-28 14:25:52 -0500145 result += BITS_PER_LONG;
146 size -= BITS_PER_LONG;
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500147 }
148 if (!size)
149 return result;
150 tmp = *p;
151
152found_first:
Grygorii Strashkocdcda672018-04-27 19:58:49 -0500153 tmp |= ~0UL << size;
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500154found_middle:
155 return result + ffz(tmp);
156}
157
wdenkc6097192002-11-03 00:24:07 +0000158/*
wdenkc6097192002-11-03 00:24:07 +0000159 * hweightN: returns the hamming weight (i.e. the number
160 * of bits set) of a N-bit word
161 */
162
163#define hweight32(x) generic_hweight32(x)
164#define hweight16(x) generic_hweight16(x)
165#define hweight8(x) generic_hweight8(x)
166
Vitaly Andrianov3bd82c52015-02-05 11:24:46 -0500167#define find_first_zero_bit(addr, size) \
168 find_next_zero_bit((addr), (size), 0)
169
wdenkc6097192002-11-03 00:24:07 +0000170#define ext2_set_bit test_and_set_bit
171#define ext2_clear_bit test_and_clear_bit
172#define ext2_test_bit test_bit
173#define ext2_find_first_zero_bit find_first_zero_bit
174#define ext2_find_next_zero_bit find_next_zero_bit
175
176/* Bitmap functions for the minix filesystem. */
177#define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
178#define minix_set_bit(nr,addr) set_bit(nr,addr)
179#define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
180#define minix_test_bit(nr,addr) test_bit(nr,addr)
181#define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
182
183#endif /* __KERNEL__ */
184
wdenkc6097192002-11-03 00:24:07 +0000185#endif /* _ARM_BITOPS_H */