bitops.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* bitops.h: bit operations for the Fujitsu FR-V CPUs
  2. *
  3. * For an explanation of how atomic ops work in this arch, see:
  4. * Documentation/fujitsu/frv/atomic-ops.txt
  5. *
  6. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  7. * Written by David Howells (dhowells@redhat.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #ifndef _ASM_BITOPS_H
  15. #define _ASM_BITOPS_H
  16. #include <linux/compiler.h>
  17. #include <asm/byteorder.h>
  18. #include <asm/system.h>
  19. #include <asm/atomic.h>
  20. #ifdef __KERNEL__
  21. #include <asm-generic/bitops/ffz.h>
  22. /*
  23. * clear_bit() doesn't provide any barrier for the compiler.
  24. */
  25. #define smp_mb__before_clear_bit() barrier()
  26. #define smp_mb__after_clear_bit() barrier()
  27. static inline int test_and_clear_bit(int nr, volatile void *addr)
  28. {
  29. volatile unsigned long *ptr = addr;
  30. unsigned long mask = 1UL << (nr & 31);
  31. ptr += nr >> 5;
  32. return (atomic_test_and_ANDNOT_mask(mask, ptr) & mask) != 0;
  33. }
  34. static inline int test_and_set_bit(int nr, volatile void *addr)
  35. {
  36. volatile unsigned long *ptr = addr;
  37. unsigned long mask = 1UL << (nr & 31);
  38. ptr += nr >> 5;
  39. return (atomic_test_and_OR_mask(mask, ptr) & mask) != 0;
  40. }
  41. static inline int test_and_change_bit(int nr, volatile void *addr)
  42. {
  43. volatile unsigned long *ptr = addr;
  44. unsigned long mask = 1UL << (nr & 31);
  45. ptr += nr >> 5;
  46. return (atomic_test_and_XOR_mask(mask, ptr) & mask) != 0;
  47. }
  48. static inline void clear_bit(int nr, volatile void *addr)
  49. {
  50. test_and_clear_bit(nr, addr);
  51. }
  52. static inline void set_bit(int nr, volatile void *addr)
  53. {
  54. test_and_set_bit(nr, addr);
  55. }
  56. static inline void change_bit(int nr, volatile void * addr)
  57. {
  58. test_and_change_bit(nr, addr);
  59. }
  60. static inline void __clear_bit(int nr, volatile void * addr)
  61. {
  62. volatile unsigned long *a = addr;
  63. int mask;
  64. a += nr >> 5;
  65. mask = 1 << (nr & 31);
  66. *a &= ~mask;
  67. }
  68. static inline void __set_bit(int nr, volatile void * addr)
  69. {
  70. volatile unsigned long *a = addr;
  71. int mask;
  72. a += nr >> 5;
  73. mask = 1 << (nr & 31);
  74. *a |= mask;
  75. }
  76. static inline void __change_bit(int nr, volatile void *addr)
  77. {
  78. volatile unsigned long *a = addr;
  79. int mask;
  80. a += nr >> 5;
  81. mask = 1 << (nr & 31);
  82. *a ^= mask;
  83. }
  84. static inline int __test_and_clear_bit(int nr, volatile void * addr)
  85. {
  86. volatile unsigned long *a = addr;
  87. int mask, retval;
  88. a += nr >> 5;
  89. mask = 1 << (nr & 31);
  90. retval = (mask & *a) != 0;
  91. *a &= ~mask;
  92. return retval;
  93. }
  94. static inline int __test_and_set_bit(int nr, volatile void * addr)
  95. {
  96. volatile unsigned long *a = addr;
  97. int mask, retval;
  98. a += nr >> 5;
  99. mask = 1 << (nr & 31);
  100. retval = (mask & *a) != 0;
  101. *a |= mask;
  102. return retval;
  103. }
  104. static inline int __test_and_change_bit(int nr, volatile void * addr)
  105. {
  106. volatile unsigned long *a = addr;
  107. int mask, retval;
  108. a += nr >> 5;
  109. mask = 1 << (nr & 31);
  110. retval = (mask & *a) != 0;
  111. *a ^= mask;
  112. return retval;
  113. }
  114. /*
  115. * This routine doesn't need to be atomic.
  116. */
  117. static inline int __constant_test_bit(int nr, const volatile void * addr)
  118. {
  119. return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
  120. }
  121. static inline int __test_bit(int nr, const volatile void * addr)
  122. {
  123. int * a = (int *) addr;
  124. int mask;
  125. a += nr >> 5;
  126. mask = 1 << (nr & 0x1f);
  127. return ((mask & *a) != 0);
  128. }
  129. #define test_bit(nr,addr) \
  130. (__builtin_constant_p(nr) ? \
  131. __constant_test_bit((nr),(addr)) : \
  132. __test_bit((nr),(addr)))
  133. #include <asm-generic/bitops/ffs.h>
  134. #include <asm-generic/bitops/__ffs.h>
  135. #include <asm-generic/bitops/find.h>
  136. /*
  137. * fls: find last bit set.
  138. */
  139. #define fls(x) \
  140. ({ \
  141. int bit; \
  142. \
  143. asm("scan %1,gr0,%0" : "=r"(bit) : "r"(x)); \
  144. \
  145. bit ? 33 - bit : bit; \
  146. })
  147. #include <asm-generic/bitops/fls64.h>
  148. #include <asm-generic/bitops/sched.h>
  149. #include <asm-generic/bitops/hweight.h>
  150. #include <asm-generic/bitops/ext2-non-atomic.h>
  151. #define ext2_set_bit_atomic(lock,nr,addr) test_and_set_bit ((nr) ^ 0x18, (addr))
  152. #define ext2_clear_bit_atomic(lock,nr,addr) test_and_clear_bit((nr) ^ 0x18, (addr))
  153. #include <asm-generic/bitops/minix-le.h>
  154. #endif /* __KERNEL__ */
  155. #endif /* _ASM_BITOPS_H */