dd03fd931bb793c51ec390fa1d941d83c3199db0
[linux-2.6-block.git] / arch / arc / include / asm / bitops.h
1 /*
2  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
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
22 /*
23  * Hardware assisted read-modify-write using ARC700 LLOCK/SCOND insns.
24  * The Kconfig glue ensures that in SMP, this is only set if the container
25  * SoC/platform has cross-core coherent LLOCK/SCOND
26  */
27 #if defined(CONFIG_ARC_HAS_LLSC)
28
29 static inline void set_bit(unsigned long nr, volatile unsigned long *m)
30 {
31         unsigned int temp;
32
33         m += nr >> 5;
34
35         /*
36          * ARC ISA micro-optimization:
37          *
38          * Instructions dealing with bitpos only consider lower 5 bits (0-31)
39          * e.g (x << 33) is handled like (x << 1) by ASL instruction
40          *  (mem pointer still needs adjustment to point to next word)
41          *
42          * Hence the masking to clamp @nr arg can be elided in general.
43          *
44          * However if @nr is a constant (above assumed it in a register),
45          * and greater than 31, gcc can optimize away (x << 33) to 0,
46          * as overflow, given the 32-bit ISA. Thus masking needs to be done
47          * for constant @nr, but no code is generated due to const prop.
48          */
49         if (__builtin_constant_p(nr))
50                 nr &= 0x1f;
51
52         __asm__ __volatile__(
53         "1:     llock   %0, [%1]        \n"
54         "       bset    %0, %0, %2      \n"
55         "       scond   %0, [%1]        \n"
56         "       bnz     1b      \n"
57         : "=&r"(temp)
58         : "r"(m), "ir"(nr)
59         : "cc");
60 }
61
62 static inline void clear_bit(unsigned long nr, volatile unsigned long *m)
63 {
64         unsigned int temp;
65
66         m += nr >> 5;
67
68         if (__builtin_constant_p(nr))
69                 nr &= 0x1f;
70
71         __asm__ __volatile__(
72         "1:     llock   %0, [%1]        \n"
73         "       bclr    %0, %0, %2      \n"
74         "       scond   %0, [%1]        \n"
75         "       bnz     1b      \n"
76         : "=&r"(temp)
77         : "r"(m), "ir"(nr)
78         : "cc");
79 }
80
81 static inline void change_bit(unsigned long nr, volatile unsigned long *m)
82 {
83         unsigned int temp;
84
85         m += nr >> 5;
86
87         if (__builtin_constant_p(nr))
88                 nr &= 0x1f;
89
90         __asm__ __volatile__(
91         "1:     llock   %0, [%1]        \n"
92         "       bxor    %0, %0, %2      \n"
93         "       scond   %0, [%1]        \n"
94         "       bnz     1b              \n"
95         : "=&r"(temp)
96         : "r"(m), "ir"(nr)
97         : "cc");
98 }
99
100 /*
101  * Semantically:
102  *    Test the bit
103  *    if clear
104  *        set it and return 0 (old value)
105  *    else
106  *        return 1 (old value).
107  *
108  * Since ARC lacks a equivalent h/w primitive, the bit is set unconditionally
109  * and the old value of bit is returned
110  */
111 static inline int test_and_set_bit(unsigned long nr, volatile unsigned long *m)
112 {
113         unsigned long old, temp;
114
115         m += nr >> 5;
116
117         if (__builtin_constant_p(nr))
118                 nr &= 0x1f;
119
120         /*
121          * Explicit full memory barrier needed before/after as
122          * LLOCK/SCOND themselves don't provide any such semantics
123          */
124         smp_mb();
125
126         __asm__ __volatile__(
127         "1:     llock   %0, [%2]        \n"
128         "       bset    %1, %0, %3      \n"
129         "       scond   %1, [%2]        \n"
130         "       bnz     1b              \n"
131         : "=&r"(old), "=&r"(temp)
132         : "r"(m), "ir"(nr)
133         : "cc");
134
135         smp_mb();
136
137         return (old & (1 << nr)) != 0;
138 }
139
140 static inline int
141 test_and_clear_bit(unsigned long nr, volatile unsigned long *m)
142 {
143         unsigned int old, temp;
144
145         m += nr >> 5;
146
147         if (__builtin_constant_p(nr))
148                 nr &= 0x1f;
149
150         smp_mb();
151
152         __asm__ __volatile__(
153         "1:     llock   %0, [%2]        \n"
154         "       bclr    %1, %0, %3      \n"
155         "       scond   %1, [%2]        \n"
156         "       bnz     1b              \n"
157         : "=&r"(old), "=&r"(temp)
158         : "r"(m), "ir"(nr)
159         : "cc");
160
161         smp_mb();
162
163         return (old & (1 << nr)) != 0;
164 }
165
166 static inline int
167 test_and_change_bit(unsigned long nr, volatile unsigned long *m)
168 {
169         unsigned int old, temp;
170
171         m += nr >> 5;
172
173         if (__builtin_constant_p(nr))
174                 nr &= 0x1f;
175
176         smp_mb();
177
178         __asm__ __volatile__(
179         "1:     llock   %0, [%2]        \n"
180         "       bxor    %1, %0, %3      \n"
181         "       scond   %1, [%2]        \n"
182         "       bnz     1b              \n"
183         : "=&r"(old), "=&r"(temp)
184         : "r"(m), "ir"(nr)
185         : "cc");
186
187         smp_mb();
188
189         return (old & (1 << nr)) != 0;
190 }
191
192 #else   /* !CONFIG_ARC_HAS_LLSC */
193
194 #include <asm/smp.h>
195
196 /*
197  * Non hardware assisted Atomic-R-M-W
198  * Locking would change to irq-disabling only (UP) and spinlocks (SMP)
199  *
200  * There's "significant" micro-optimization in writing our own variants of
201  * bitops (over generic variants)
202  *
203  * (1) The generic APIs have "signed" @nr while we have it "unsigned"
204  *     This avoids extra code to be generated for pointer arithmatic, since
205  *     is "not sure" that index is NOT -ve
206  * (2) Utilize the fact that ARCompact bit fidding insn (BSET/BCLR/ASL) etc
207  *     only consider bottom 5 bits of @nr, so NO need to mask them off.
208  *     (GCC Quirk: however for constant @nr we still need to do the masking
209  *             at compile time)
210  */
211
212 static inline void set_bit(unsigned long nr, volatile unsigned long *m)
213 {
214         unsigned long temp, flags;
215         m += nr >> 5;
216
217         if (__builtin_constant_p(nr))
218                 nr &= 0x1f;
219
220         bitops_lock(flags);
221
222         temp = *m;
223         *m = temp | (1UL << nr);
224
225         bitops_unlock(flags);
226 }
227
228 static inline void clear_bit(unsigned long nr, volatile unsigned long *m)
229 {
230         unsigned long temp, flags;
231         m += nr >> 5;
232
233         if (__builtin_constant_p(nr))
234                 nr &= 0x1f;
235
236         bitops_lock(flags);
237
238         temp = *m;
239         *m = temp & ~(1UL << nr);
240
241         bitops_unlock(flags);
242 }
243
244 static inline void change_bit(unsigned long nr, volatile unsigned long *m)
245 {
246         unsigned long temp, flags;
247         m += nr >> 5;
248
249         if (__builtin_constant_p(nr))
250                 nr &= 0x1f;
251
252         bitops_lock(flags);
253
254         temp = *m;
255         *m = temp ^ (1UL << nr);
256
257         bitops_unlock(flags);
258 }
259
260 static inline int test_and_set_bit(unsigned long nr, volatile unsigned long *m)
261 {
262         unsigned long old, flags;
263         m += nr >> 5;
264
265         if (__builtin_constant_p(nr))
266                 nr &= 0x1f;
267
268         /*
269          * spin lock/unlock provide the needed smp_mb() before/after
270          */
271         bitops_lock(flags);
272
273         old = *m;
274         *m = old | (1 << nr);
275
276         bitops_unlock(flags);
277
278         return (old & (1 << nr)) != 0;
279 }
280
281 static inline int
282 test_and_clear_bit(unsigned long nr, volatile unsigned long *m)
283 {
284         unsigned long old, flags;
285         m += nr >> 5;
286
287         if (__builtin_constant_p(nr))
288                 nr &= 0x1f;
289
290         bitops_lock(flags);
291
292         old = *m;
293         *m = old & ~(1 << nr);
294
295         bitops_unlock(flags);
296
297         return (old & (1 << nr)) != 0;
298 }
299
300 static inline int
301 test_and_change_bit(unsigned long nr, volatile unsigned long *m)
302 {
303         unsigned long old, flags;
304         m += nr >> 5;
305
306         if (__builtin_constant_p(nr))
307                 nr &= 0x1f;
308
309         bitops_lock(flags);
310
311         old = *m;
312         *m = old ^ (1 << nr);
313
314         bitops_unlock(flags);
315
316         return (old & (1 << nr)) != 0;
317 }
318
319 #endif /* CONFIG_ARC_HAS_LLSC */
320
321 /***************************************
322  * Non atomic variants
323  **************************************/
324
325 static inline void __set_bit(unsigned long nr, volatile unsigned long *m)
326 {
327         unsigned long temp;
328         m += nr >> 5;
329
330         if (__builtin_constant_p(nr))
331                 nr &= 0x1f;
332
333         temp = *m;
334         *m = temp | (1UL << nr);
335 }
336
337 static inline void __clear_bit(unsigned long nr, volatile unsigned long *m)
338 {
339         unsigned long temp;
340         m += nr >> 5;
341
342         if (__builtin_constant_p(nr))
343                 nr &= 0x1f;
344
345         temp = *m;
346         *m = temp & ~(1UL << nr);
347 }
348
349 static inline void __change_bit(unsigned long nr, volatile unsigned long *m)
350 {
351         unsigned long temp;
352         m += nr >> 5;
353
354         if (__builtin_constant_p(nr))
355                 nr &= 0x1f;
356
357         temp = *m;
358         *m = temp ^ (1UL << nr);
359 }
360
361 static inline int
362 __test_and_set_bit(unsigned long nr, volatile unsigned long *m)
363 {
364         unsigned long old;
365         m += nr >> 5;
366
367         if (__builtin_constant_p(nr))
368                 nr &= 0x1f;
369
370         old = *m;
371         *m = old | (1 << nr);
372
373         return (old & (1 << nr)) != 0;
374 }
375
376 static inline int
377 __test_and_clear_bit(unsigned long nr, volatile unsigned long *m)
378 {
379         unsigned long old;
380         m += nr >> 5;
381
382         if (__builtin_constant_p(nr))
383                 nr &= 0x1f;
384
385         old = *m;
386         *m = old & ~(1 << nr);
387
388         return (old & (1 << nr)) != 0;
389 }
390
391 static inline int
392 __test_and_change_bit(unsigned long nr, volatile unsigned long *m)
393 {
394         unsigned long old;
395         m += nr >> 5;
396
397         if (__builtin_constant_p(nr))
398                 nr &= 0x1f;
399
400         old = *m;
401         *m = old ^ (1 << nr);
402
403         return (old & (1 << nr)) != 0;
404 }
405
406 /*
407  * This routine doesn't need to be atomic.
408  */
409 static inline int
410 test_bit(unsigned int nr, const volatile unsigned long *addr)
411 {
412         unsigned long mask;
413
414         addr += nr >> 5;
415
416         if (__builtin_constant_p(nr))
417                 nr &= 0x1f;
418
419         mask = 1 << nr;
420
421         return ((mask & *addr) != 0);
422 }
423
424 #ifdef CONFIG_ISA_ARCOMPACT
425
426 /*
427  * Count the number of zeros, starting from MSB
428  * Helper for fls( ) friends
429  * This is a pure count, so (1-32) or (0-31) doesn't apply
430  * It could be 0 to 32, based on num of 0's in there
431  * clz(0x8000_0000) = 0, clz(0xFFFF_FFFF)=0, clz(0) = 32, clz(1) = 31
432  */
433 static inline __attribute__ ((const)) int clz(unsigned int x)
434 {
435         unsigned int res;
436
437         __asm__ __volatile__(
438         "       norm.f  %0, %1          \n"
439         "       mov.n   %0, 0           \n"
440         "       add.p   %0, %0, 1       \n"
441         : "=r"(res)
442         : "r"(x)
443         : "cc");
444
445         return res;
446 }
447
448 static inline int constant_fls(int x)
449 {
450         int r = 32;
451
452         if (!x)
453                 return 0;
454         if (!(x & 0xffff0000u)) {
455                 x <<= 16;
456                 r -= 16;
457         }
458         if (!(x & 0xff000000u)) {
459                 x <<= 8;
460                 r -= 8;
461         }
462         if (!(x & 0xf0000000u)) {
463                 x <<= 4;
464                 r -= 4;
465         }
466         if (!(x & 0xc0000000u)) {
467                 x <<= 2;
468                 r -= 2;
469         }
470         if (!(x & 0x80000000u)) {
471                 x <<= 1;
472                 r -= 1;
473         }
474         return r;
475 }
476
477 /*
478  * fls = Find Last Set in word
479  * @result: [1-32]
480  * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
481  */
482 static inline __attribute__ ((const)) int fls(unsigned long x)
483 {
484         if (__builtin_constant_p(x))
485                return constant_fls(x);
486
487         return 32 - clz(x);
488 }
489
490 /*
491  * __fls: Similar to fls, but zero based (0-31)
492  */
493 static inline __attribute__ ((const)) int __fls(unsigned long x)
494 {
495         if (!x)
496                 return 0;
497         else
498                 return fls(x) - 1;
499 }
500
501 /*
502  * ffs = Find First Set in word (LSB to MSB)
503  * @result: [1-32], 0 if all 0's
504  */
505 #define ffs(x)  ({ unsigned long __t = (x); fls(__t & -__t); })
506
507 /*
508  * __ffs: Similar to ffs, but zero based (0-31)
509  */
510 static inline __attribute__ ((const)) int __ffs(unsigned long word)
511 {
512         if (!word)
513                 return word;
514
515         return ffs(word) - 1;
516 }
517
518 #else   /* CONFIG_ISA_ARCV2 */
519
520 /*
521  * fls = Find Last Set in word
522  * @result: [1-32]
523  * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
524  */
525 static inline __attribute__ ((const)) int fls(unsigned long x)
526 {
527         int n;
528
529         asm volatile(
530         "       fls.f   %0, %1          \n"  /* 0:31; 0(Z) if src 0 */
531         "       add.nz  %0, %0, 1       \n"  /* 0:31 -> 1:32 */
532         : "=r"(n)       /* Early clobber not needed */
533         : "r"(x)
534         : "cc");
535
536         return n;
537 }
538
539 /*
540  * __fls: Similar to fls, but zero based (0-31). Also 0 if no bit set
541  */
542 static inline __attribute__ ((const)) int __fls(unsigned long x)
543 {
544         /* FLS insn has exactly same semantics as the API */
545         return  __builtin_arc_fls(x);
546 }
547
548 /*
549  * ffs = Find First Set in word (LSB to MSB)
550  * @result: [1-32], 0 if all 0's
551  */
552 static inline __attribute__ ((const)) int ffs(unsigned long x)
553 {
554         int n;
555
556         asm volatile(
557         "       ffs.f   %0, %1          \n"  /* 0:31; 31(Z) if src 0 */
558         "       add.nz  %0, %0, 1       \n"  /* 0:31 -> 1:32 */
559         "       mov.z   %0, 0           \n"  /* 31(Z)-> 0 */
560         : "=r"(n)       /* Early clobber not needed */
561         : "r"(x)
562         : "cc");
563
564         return n;
565 }
566
567 /*
568  * __ffs: Similar to ffs, but zero based (0-31)
569  */
570 static inline __attribute__ ((const)) int __ffs(unsigned long x)
571 {
572         int n;
573
574         asm volatile(
575         "       ffs.f   %0, %1          \n"  /* 0:31; 31(Z) if src 0 */
576         "       mov.z   %0, 0           \n"  /* 31(Z)-> 0 */
577         : "=r"(n)
578         : "r"(x)
579         : "cc");
580
581         return n;
582
583 }
584
585 #endif  /* CONFIG_ISA_ARCOMPACT */
586
587 /*
588  * ffz = Find First Zero in word.
589  * @return:[0-31], 32 if all 1's
590  */
591 #define ffz(x)  __ffs(~(x))
592
593 #include <asm-generic/bitops/hweight.h>
594 #include <asm-generic/bitops/fls64.h>
595 #include <asm-generic/bitops/sched.h>
596 #include <asm-generic/bitops/lock.h>
597
598 #include <asm-generic/bitops/find.h>
599 #include <asm-generic/bitops/le.h>
600 #include <asm-generic/bitops/ext2-atomic-setbit.h>
601
602 #endif /* !__ASSEMBLY__ */
603
604 #endif