blob: ecd003266432cae99c7f8934e6066b3b1eec1ce6 [file] [log] [blame]
Macpaul Lin0b9b59a2011-10-11 22:33:15 +00001/*
2 * Copyright 1995, Russell King.
3 * Various bits and pieces copyrights include:
4 * Linus Torvalds (test_bit).
5 *
6 * Copyright (C) 2011 Andes Technology Corporation
7 * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
8 *
9 * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
10 *
11 * Please note that the code in this file should never be included
12 * from user space. Many of these are not implemented in assembler
13 * since they would be too costly. Also, they require priviledged
14 * instructions (which are not available from user mode) to ensure
15 * that they are atomic.
16 */
17
18#ifndef __ASM_NDS_BITOPS_H
19#define __ASM_NDS_BITOPS_H
20
21#ifdef __KERNEL__
22
23#include <asm/system.h>
Fabio Estevam4a779da2015-11-05 12:43:33 -020024#include <asm-generic/bitops/fls.h>
25#include <asm-generic/bitops/__fls.h>
26#include <asm-generic/bitops/fls64.h>
27#include <asm-generic/bitops/__ffs.h>
Macpaul Lin0b9b59a2011-10-11 22:33:15 +000028
29#define smp_mb__before_clear_bit() do { } while (0)
30#define smp_mb__after_clear_bit() do { } while (0)
31
32/*
33 * Function prototypes to keep gcc -Wall happy.
34 */
35extern void set_bit(int nr, void *addr);
36
37static inline void __set_bit(int nr, void *addr)
38{
39 int *a = (int *)addr;
40 int mask;
41
42 a += nr >> 5;
43 mask = 1 << (nr & 0x1f);
44 *a |= mask;
45}
46
Bryan O'Donoghue2725e0b2018-04-30 15:56:07 +010047#define PLATFORM__SET_BIT
48
Macpaul Lin0b9b59a2011-10-11 22:33:15 +000049extern void clear_bit(int nr, void *addr);
50
51static inline void __clear_bit(int nr, void *addr)
52{
53 int *a = (int *)addr;
54 int mask;
55 unsigned long flags;
56
57 a += nr >> 5;
58 mask = 1 << (nr & 0x1f);
59 local_irq_save(flags);
60 *a &= ~mask;
61 local_irq_restore(flags);
62}
63
64extern void change_bit(int nr, void *addr);
65
66static inline void __change_bit(int nr, void *addr)
67{
68 int mask;
69 unsigned long *ADDR = (unsigned long *)addr;
70
71 ADDR += nr >> 5;
72 mask = 1 << (nr & 31);
73 *ADDR ^= mask;
74}
75
76extern int test_and_set_bit(int nr, void *addr);
77
78static inline int __test_and_set_bit(int nr, void *addr)
79{
80 int mask, retval;
81 unsigned int *a = (unsigned int *)addr;
82
83 a += nr >> 5;
84 mask = 1 << (nr & 0x1f);
85 retval = (mask & *a) != 0;
86 *a |= mask;
87 return retval;
88}
89
90extern int test_and_clear_bit(int nr, void *addr);
91
92static inline int __test_and_clear_bit(int nr, void *addr)
93{
94 int mask, retval;
95 unsigned int *a = (unsigned int *)addr;
96
97 a += nr >> 5;
98 mask = 1 << (nr & 0x1f);
99 retval = (mask & *a) != 0;
100 *a &= ~mask;
101 return retval;
102}
103
104extern int test_and_change_bit(int nr, void *addr);
105
106static inline int __test_and_change_bit(int nr, void *addr)
107{
108 int mask, retval;
109 unsigned int *a = (unsigned int *)addr;
110
111 a += nr >> 5;
112 mask = 1 << (nr & 0x1f);
113 retval = (mask & *a) != 0;
114 *a ^= mask;
115 return retval;
116}
117
118extern int find_first_zero_bit(void *addr, unsigned size);
119extern int find_next_zero_bit(void *addr, int size, int offset);
120
121/*
122 * This routine doesn't need to be atomic.
123 */
124static inline int test_bit(int nr, const void *addr)
125{
126 return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
127}
128
129/*
130 * ffz = Find First Zero in word. Undefined if no zero exists,
131 * so code should check against ~0UL first..
132 */
133static inline unsigned long ffz(unsigned long word)
134{
135 int k;
136
137 word = ~word;
138 k = 31;
139 if (word & 0x0000ffff) {
140 k -= 16; word <<= 16;
141 }
142 if (word & 0x00ff0000) {
143 k -= 8; word <<= 8;
144 }
145 if (word & 0x0f000000) {
146 k -= 4; word <<= 4;
147 }
148 if (word & 0x30000000) {
149 k -= 2; word <<= 2;
150 }
151 if (word & 0x40000000)
152 k -= 1;
153
154 return k;
155}
156
157/*
158 * ffs: find first bit set. This is defined the same way as
159 * the libc and compiler builtin ffs routines, therefore
160 * differs in spirit from the above ffz (man ffs).
161 */
162
163/*
164 * redefined in include/linux/bitops.h
165 * #define ffs(x) generic_ffs(x)
166 */
167
168/*
169 * hweightN: returns the hamming weight (i.e. the number
170 * of bits set) of a N-bit word
171 */
172
173#define hweight32(x) generic_hweight32(x)
174#define hweight16(x) generic_hweight16(x)
175#define hweight8(x) generic_hweight8(x)
176
177#define ext2_set_bit test_and_set_bit
178#define ext2_clear_bit test_and_clear_bit
179#define ext2_test_bit test_bit
180#define ext2_find_first_zero_bit find_first_zero_bit
181#define ext2_find_next_zero_bit find_next_zero_bit
182
183/* Bitmap functions for the minix filesystem. */
184#define minix_test_and_set_bit(nr, addr) test_and_set_bit(nr, addr)
185#define minix_set_bit(nr, addr) set_bit(nr, addr)
186#define minix_test_and_clear_bit(nr, addr) test_and_clear_bit(nr, addr)
187#define minix_test_bit(nr, addr) test_bit(nr, addr)
188#define minix_find_first_zero_bit(addr, size) find_first_zero_bit(addr, size)
189
190#endif /* __KERNEL__ */
191
192#endif /* __ASM_NDS_BITOPS_H */