1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#ifndef _ASM_POWERPC_BITOPS_H
37#define _ASM_POWERPC_BITOPS_H
38
39#ifdef __KERNEL__
40
41#ifndef _LINUX_BITOPS_H
42#error only <linux/bitops.h> can be included directly
43#endif
44
45#include <linux/compiler.h>
46#include <asm/asm-compat.h>
47#include <asm/synch.h>
48
49
50
51
52#define smp_mb__before_clear_bit() smp_mb()
53#define smp_mb__after_clear_bit() smp_mb()
54
55
56#define DEFINE_BITOP(fn, op, prefix) \
57static __inline__ void fn(unsigned long mask, \
58 volatile unsigned long *_p) \
59{ \
60 unsigned long old; \
61 unsigned long *p = (unsigned long *)_p; \
62 __asm__ __volatile__ ( \
63 prefix \
64"1:" PPC_LLARX(%0,0,%3,0) "\n" \
65 stringify_in_c(op) "%0,%0,%2\n" \
66 PPC405_ERR77(0,%3) \
67 PPC_STLCX "%0,0,%3\n" \
68 "bne- 1b\n" \
69 : "=&r" (old), "+m" (*p) \
70 : "r" (mask), "r" (p) \
71 : "cc", "memory"); \
72}
73
74DEFINE_BITOP(set_bits, or, "")
75DEFINE_BITOP(clear_bits, andc, "")
76DEFINE_BITOP(clear_bits_unlock, andc, PPC_RELEASE_BARRIER)
77DEFINE_BITOP(change_bits, xor, "")
78
79static __inline__ void set_bit(int nr, volatile unsigned long *addr)
80{
81 set_bits(BIT_MASK(nr), addr + BIT_WORD(nr));
82}
83
84static __inline__ void clear_bit(int nr, volatile unsigned long *addr)
85{
86 clear_bits(BIT_MASK(nr), addr + BIT_WORD(nr));
87}
88
89static __inline__ void clear_bit_unlock(int nr, volatile unsigned long *addr)
90{
91 clear_bits_unlock(BIT_MASK(nr), addr + BIT_WORD(nr));
92}
93
94static __inline__ void change_bit(int nr, volatile unsigned long *addr)
95{
96 change_bits(BIT_MASK(nr), addr + BIT_WORD(nr));
97}
98
99
100
101#define DEFINE_TESTOP(fn, op, prefix, postfix, eh) \
102static __inline__ unsigned long fn( \
103 unsigned long mask, \
104 volatile unsigned long *_p) \
105{ \
106 unsigned long old, t; \
107 unsigned long *p = (unsigned long *)_p; \
108 __asm__ __volatile__ ( \
109 prefix \
110"1:" PPC_LLARX(%0,0,%3,eh) "\n" \
111 stringify_in_c(op) "%1,%0,%2\n" \
112 PPC405_ERR77(0,%3) \
113 PPC_STLCX "%1,0,%3\n" \
114 "bne- 1b\n" \
115 postfix \
116 : "=&r" (old), "=&r" (t) \
117 : "r" (mask), "r" (p) \
118 : "cc", "memory"); \
119 return (old & mask); \
120}
121
122DEFINE_TESTOP(test_and_set_bits, or, PPC_ATOMIC_ENTRY_BARRIER,
123 PPC_ATOMIC_EXIT_BARRIER, 0)
124DEFINE_TESTOP(test_and_set_bits_lock, or, "",
125 PPC_ACQUIRE_BARRIER, 1)
126DEFINE_TESTOP(test_and_clear_bits, andc, PPC_ATOMIC_ENTRY_BARRIER,
127 PPC_ATOMIC_EXIT_BARRIER, 0)
128DEFINE_TESTOP(test_and_change_bits, xor, PPC_ATOMIC_ENTRY_BARRIER,
129 PPC_ATOMIC_EXIT_BARRIER, 0)
130
131static __inline__ int test_and_set_bit(unsigned long nr,
132 volatile unsigned long *addr)
133{
134 return test_and_set_bits(BIT_MASK(nr), addr + BIT_WORD(nr)) != 0;
135}
136
137static __inline__ int test_and_set_bit_lock(unsigned long nr,
138 volatile unsigned long *addr)
139{
140 return test_and_set_bits_lock(BIT_MASK(nr),
141 addr + BIT_WORD(nr)) != 0;
142}
143
144static __inline__ int test_and_clear_bit(unsigned long nr,
145 volatile unsigned long *addr)
146{
147 return test_and_clear_bits(BIT_MASK(nr), addr + BIT_WORD(nr)) != 0;
148}
149
150static __inline__ int test_and_change_bit(unsigned long nr,
151 volatile unsigned long *addr)
152{
153 return test_and_change_bits(BIT_MASK(nr), addr + BIT_WORD(nr)) != 0;
154}
155
156#include <asm-generic/bitops/non-atomic.h>
157
158static __inline__ void __clear_bit_unlock(int nr, volatile unsigned long *addr)
159{
160 __asm__ __volatile__(PPC_RELEASE_BARRIER "" ::: "memory");
161 __clear_bit(nr, addr);
162}
163
164
165
166
167
168static __inline__ __attribute__((const))
169int __ilog2(unsigned long x)
170{
171 int lz;
172
173 asm (PPC_CNTLZL "%0,%1" : "=r" (lz) : "r" (x));
174 return BITS_PER_LONG - 1 - lz;
175}
176
177static inline __attribute__((const))
178int __ilog2_u32(u32 n)
179{
180 int bit;
181 asm ("cntlzw %0,%1" : "=r" (bit) : "r" (n));
182 return 31 - bit;
183}
184
185#ifdef __powerpc64__
186static inline __attribute__((const))
187int __ilog2_u64(u64 n)
188{
189 int bit;
190 asm ("cntlzd %0,%1" : "=r" (bit) : "r" (n));
191 return 63 - bit;
192}
193#endif
194
195
196
197
198
199
200static __inline__ unsigned long ffz(unsigned long x)
201{
202
203 if ((x = ~x) == 0)
204 return BITS_PER_LONG;
205
206
207
208
209
210
211
212 return __ilog2(x & -x);
213}
214
215static __inline__ int __ffs(unsigned long x)
216{
217 return __ilog2(x & -x);
218}
219
220
221
222
223
224
225static __inline__ int ffs(int x)
226{
227 unsigned long i = (unsigned long)x;
228 return __ilog2(i & -i) + 1;
229}
230
231
232
233
234
235static __inline__ int fls(unsigned int x)
236{
237 int lz;
238
239 asm ("cntlzw %0,%1" : "=r" (lz) : "r" (x));
240 return 32 - lz;
241}
242
243static __inline__ unsigned long __fls(unsigned long x)
244{
245 return __ilog2(x);
246}
247
248
249
250
251
252
253#ifdef __powerpc64__
254static __inline__ int fls64(__u64 x)
255{
256 int lz;
257
258 asm ("cntlzd %0,%1" : "=r" (lz) : "r" (x));
259 return 64 - lz;
260}
261#else
262#include <asm-generic/bitops/fls64.h>
263#endif
264
265#ifdef CONFIG_PPC64
266unsigned int __arch_hweight8(unsigned int w);
267unsigned int __arch_hweight16(unsigned int w);
268unsigned int __arch_hweight32(unsigned int w);
269unsigned long __arch_hweight64(__u64 w);
270#include <asm-generic/bitops/const_hweight.h>
271#else
272#include <asm-generic/bitops/hweight.h>
273#endif
274
275#include <asm-generic/bitops/find.h>
276
277
278#include <asm-generic/bitops/le.h>
279
280
281
282#include <asm-generic/bitops/ext2-atomic-setbit.h>
283
284#include <asm-generic/bitops/sched.h>
285
286#endif
287
288#endif
289