1
2
3
4
5
6
7
8
9#ifndef _ASM_BITOPS_H
10#define _ASM_BITOPS_H
11
12#ifndef _LINUX_BITOPS_H
13#error only <linux/bitops.h> can be included directly
14#endif
15
16#ifndef __ASSEMBLY__
17
18#include <linux/types.h>
19#include <linux/compiler.h>
20#include <asm/barrier.h>
21#ifndef CONFIG_ARC_HAS_LLSC
22#include <asm/smp.h>
23#endif
24
25#ifdef CONFIG_ARC_HAS_LLSC
26
27
28
29
30
31#define BIT_OP(op, c_op, asm_op) \
32static inline void op##_bit(unsigned long nr, volatile unsigned long *m)\
33{ \
34 unsigned int temp; \
35 \
36 m += nr >> 5; \
37 \
38 nr &= 0x1f; \
39 \
40 __asm__ __volatile__( \
41 "1: llock %0, [%1] \n" \
42 " " #asm_op " %0, %0, %2 \n" \
43 " scond %0, [%1] \n" \
44 " bnz 1b \n" \
45 : "=&r"(temp) \
46 : "r"(m), \
47 "ir"(nr) \
48 : "cc"); \
49}
50
51
52
53
54
55
56
57
58
59
60
61
62#define TEST_N_BIT_OP(op, c_op, asm_op) \
63static inline int test_and_##op##_bit(unsigned long nr, volatile unsigned long *m)\
64{ \
65 unsigned long old, temp; \
66 \
67 m += nr >> 5; \
68 \
69 nr &= 0x1f; \
70 \
71
72
73
74 \
75 smp_mb(); \
76 \
77 __asm__ __volatile__( \
78 "1: llock %0, [%2] \n" \
79 " " #asm_op " %1, %0, %3 \n" \
80 " scond %1, [%2] \n" \
81 " bnz 1b \n" \
82 : "=&r"(old), "=&r"(temp) \
83 : "r"(m), "ir"(nr) \
84 : "cc"); \
85 \
86 smp_mb(); \
87 \
88 return (old & (1 << nr)) != 0; \
89}
90
91#elif !defined(CONFIG_ARC_PLAT_EZNPS)
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109#define BIT_OP(op, c_op, asm_op) \
110static inline void op##_bit(unsigned long nr, volatile unsigned long *m)\
111{ \
112 unsigned long temp, flags; \
113 m += nr >> 5; \
114 \
115
116
117 \
118 bitops_lock(flags); \
119 \
120 temp = *m; \
121 *m = temp c_op (1UL << (nr & 0x1f)); \
122 \
123 bitops_unlock(flags); \
124}
125
126#define TEST_N_BIT_OP(op, c_op, asm_op) \
127static inline int test_and_##op##_bit(unsigned long nr, volatile unsigned long *m)\
128{ \
129 unsigned long old, flags; \
130 m += nr >> 5; \
131 \
132 bitops_lock(flags); \
133 \
134 old = *m; \
135 *m = old c_op (1UL << (nr & 0x1f)); \
136 \
137 bitops_unlock(flags); \
138 \
139 return (old & (1UL << (nr & 0x1f))) != 0; \
140}
141
142#else
143
144#define BIT_OP(op, c_op, asm_op) \
145static inline void op##_bit(unsigned long nr, volatile unsigned long *m)\
146{ \
147 m += nr >> 5; \
148 \
149 nr = (1UL << (nr & 0x1f)); \
150 if (asm_op == CTOP_INST_AAND_DI_R2_R2_R3) \
151 nr = ~nr; \
152 \
153 __asm__ __volatile__( \
154 " mov r2, %0\n" \
155 " mov r3, %1\n" \
156 " .word %2\n" \
157 : \
158 : "r"(nr), "r"(m), "i"(asm_op) \
159 : "r2", "r3", "memory"); \
160}
161
162#define TEST_N_BIT_OP(op, c_op, asm_op) \
163static inline int test_and_##op##_bit(unsigned long nr, volatile unsigned long *m)\
164{ \
165 unsigned long old; \
166 \
167 m += nr >> 5; \
168 \
169 nr = old = (1UL << (nr & 0x1f)); \
170 if (asm_op == CTOP_INST_AAND_DI_R2_R2_R3) \
171 old = ~old; \
172 \
173 \
174 smp_mb(); \
175 \
176 __asm__ __volatile__( \
177 " mov r2, %0\n" \
178 " mov r3, %1\n" \
179 " .word %2\n" \
180 " mov %0, r2" \
181 : "+r"(old) \
182 : "r"(m), "i"(asm_op) \
183 : "r2", "r3", "memory"); \
184 \
185 smp_mb(); \
186 \
187 return (old & nr) != 0; \
188}
189
190#endif
191
192
193
194
195
196#define __BIT_OP(op, c_op, asm_op) \
197static inline void __##op##_bit(unsigned long nr, volatile unsigned long *m) \
198{ \
199 unsigned long temp; \
200 m += nr >> 5; \
201 \
202 temp = *m; \
203 *m = temp c_op (1UL << (nr & 0x1f)); \
204}
205
206#define __TEST_N_BIT_OP(op, c_op, asm_op) \
207static inline int __test_and_##op##_bit(unsigned long nr, volatile unsigned long *m)\
208{ \
209 unsigned long old; \
210 m += nr >> 5; \
211 \
212 old = *m; \
213 *m = old c_op (1UL << (nr & 0x1f)); \
214 \
215 return (old & (1UL << (nr & 0x1f))) != 0; \
216}
217
218#define BIT_OPS(op, c_op, asm_op) \
219 \
220 \
221 BIT_OP(op, c_op, asm_op) \
222 \
223 \
224 TEST_N_BIT_OP(op, c_op, asm_op) \
225 \
226 \
227 __BIT_OP(op, c_op, asm_op) \
228 \
229 \
230 __TEST_N_BIT_OP(op, c_op, asm_op)
231
232#ifndef CONFIG_ARC_PLAT_EZNPS
233BIT_OPS(set, |, bset)
234BIT_OPS(clear, & ~, bclr)
235BIT_OPS(change, ^, bxor)
236#else
237BIT_OPS(set, |, CTOP_INST_AOR_DI_R2_R2_R3)
238BIT_OPS(clear, & ~, CTOP_INST_AAND_DI_R2_R2_R3)
239BIT_OPS(change, ^, CTOP_INST_AXOR_DI_R2_R2_R3)
240#endif
241
242
243
244
245static inline int
246test_bit(unsigned int nr, const volatile unsigned long *addr)
247{
248 unsigned long mask;
249
250 addr += nr >> 5;
251
252 mask = 1UL << (nr & 0x1f);
253
254 return ((mask & *addr) != 0);
255}
256
257#ifdef CONFIG_ISA_ARCOMPACT
258
259
260
261
262
263
264
265
266static inline __attribute__ ((const)) int clz(unsigned int x)
267{
268 unsigned int res;
269
270 __asm__ __volatile__(
271 " norm.f %0, %1 \n"
272 " mov.n %0, 0 \n"
273 " add.p %0, %0, 1 \n"
274 : "=r"(res)
275 : "r"(x)
276 : "cc");
277
278 return res;
279}
280
281static inline int constant_fls(int x)
282{
283 int r = 32;
284
285 if (!x)
286 return 0;
287 if (!(x & 0xffff0000u)) {
288 x <<= 16;
289 r -= 16;
290 }
291 if (!(x & 0xff000000u)) {
292 x <<= 8;
293 r -= 8;
294 }
295 if (!(x & 0xf0000000u)) {
296 x <<= 4;
297 r -= 4;
298 }
299 if (!(x & 0xc0000000u)) {
300 x <<= 2;
301 r -= 2;
302 }
303 if (!(x & 0x80000000u)) {
304 x <<= 1;
305 r -= 1;
306 }
307 return r;
308}
309
310
311
312
313
314
315static inline __attribute__ ((const)) int fls(unsigned long x)
316{
317 if (__builtin_constant_p(x))
318 return constant_fls(x);
319
320 return 32 - clz(x);
321}
322
323
324
325
326static inline __attribute__ ((const)) int __fls(unsigned long x)
327{
328 if (!x)
329 return 0;
330 else
331 return fls(x) - 1;
332}
333
334
335
336
337
338#define ffs(x) ({ unsigned long __t = (x); fls(__t & -__t); })
339
340
341
342
343static inline __attribute__ ((const)) int __ffs(unsigned long word)
344{
345 if (!word)
346 return word;
347
348 return ffs(word) - 1;
349}
350
351#else
352
353
354
355
356
357
358static inline __attribute__ ((const)) int fls(unsigned long x)
359{
360 int n;
361
362 asm volatile(
363 " fls.f %0, %1 \n"
364 " add.nz %0, %0, 1 \n"
365 : "=r"(n)
366 : "r"(x)
367 : "cc");
368
369 return n;
370}
371
372
373
374
375static inline __attribute__ ((const)) int __fls(unsigned long x)
376{
377
378 return __builtin_arc_fls(x);
379}
380
381
382
383
384
385static inline __attribute__ ((const)) int ffs(unsigned long x)
386{
387 int n;
388
389 asm volatile(
390 " ffs.f %0, %1 \n"
391 " add.nz %0, %0, 1 \n"
392 " mov.z %0, 0 \n"
393 : "=r"(n)
394 : "r"(x)
395 : "cc");
396
397 return n;
398}
399
400
401
402
403static inline __attribute__ ((const)) int __ffs(unsigned long x)
404{
405 int n;
406
407 asm volatile(
408 " ffs.f %0, %1 \n"
409 " mov.z %0, 0 \n"
410 : "=r"(n)
411 : "r"(x)
412 : "cc");
413
414 return n;
415
416}
417
418#endif
419
420
421
422
423
424#define ffz(x) __ffs(~(x))
425
426#include <asm-generic/bitops/hweight.h>
427#include <asm-generic/bitops/fls64.h>
428#include <asm-generic/bitops/sched.h>
429#include <asm-generic/bitops/lock.h>
430
431#include <asm-generic/bitops/find.h>
432#include <asm-generic/bitops/le.h>
433#include <asm-generic/bitops/ext2-atomic-setbit.h>
434
435#endif
436
437#endif
438