bitops.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/config.h>
  17. #include <linux/compiler.h>
  18. #include <asm/byteorder.h>
  19. #include <asm/system.h>
  20. #include <asm/atomic.h>
  21. #ifdef __KERNEL__
  22. #include <asm-generic/bitops/ffz.h>
  23. /*
  24. * clear_bit() doesn't provide any barrier for the compiler.
  25. */
  26. #define smp_mb__before_clear_bit() barrier()
  27. #define smp_mb__after_clear_bit() barrier()
  28. static inline int test_and_clear_bit(int nr, volatile void *addr)
  29. {
  30. volatile unsigned long *ptr = addr;
  31. unsigned long mask = 1UL << (nr & 31);
  32. ptr += nr >> 5;
  33. return (atomic_test_and_ANDNOT_mask(mask, ptr) & mask) != 0;
  34. }
  35. static inline int test_and_set_bit(int nr, volatile void *addr)
  36. {
  37. volatile unsigned long *ptr = addr;
  38. unsigned long mask = 1UL << (nr & 31);
  39. ptr += nr >> 5;
  40. return (atomic_test_and_OR_mask(mask, ptr) & mask) != 0;
  41. }
  42. static inline int test_and_change_bit(int nr, volatile void *addr)
  43. {
  44. volatile unsigned long *ptr = addr;
  45. unsigned long mask = 1UL << (nr & 31);
  46. ptr += nr >> 5;
  47. return (atomic_test_and_XOR_mask(mask, ptr) & mask) != 0;
  48. }
  49. static inline void clear_bit(int nr, volatile void *addr)
  50. {
  51. test_and_clear_bit(nr, addr);
  52. }
  53. static inline void set_bit(int nr, volatile void *addr)
  54. {
  55. test_and_set_bit(nr, addr);
  56. }
  57. static inline void change_bit(int nr, volatile void * addr)
  58. {
  59. test_and_change_bit(nr, addr);
  60. }
  61. static inline void __clear_bit(int nr, volatile void * addr)
  62. {
  63. volatile unsigned long *a = addr;
  64. int mask;
  65. a += nr >> 5;
  66. mask = 1 << (nr & 31);
  67. *a &= ~mask;
  68. }
  69. static inline void __set_bit(int nr, volatile void * addr)
  70. {
  71. volatile unsigned long *a = addr;
  72. int mask;
  73. a += nr >> 5;
  74. mask = 1 << (nr & 31);
  75. *a |= mask;
  76. }
  77. static inline void __change_bit(int nr, volatile void *addr)
  78. {
  79. volatile unsigned long *a = addr;
  80. int mask;
  81. a += nr >> 5;
  82. mask = 1 << (nr & 31);
  83. *a ^= mask;
  84. }
  85. static inline int __test_and_clear_bit(int nr, volatile void * addr)
  86. {
  87. volatile unsigned long *a = addr;
  88. int mask, retval;
  89. a += nr >> 5;
  90. mask = 1 << (nr & 31);
  91. retval = (mask & *a) != 0;
  92. *a &= ~mask;
  93. return retval;
  94. }
  95. static inline int __test_and_set_bit(int nr, volatile void * addr)
  96. {
  97. volatile unsigned long *a = addr;
  98. int mask, retval;
  99. a += nr >> 5;
  100. mask = 1 << (nr & 31);
  101. retval = (mask & *a) != 0;
  102. *a |= mask;
  103. return retval;
  104. }
  105. static inline int __test_and_change_bit(int nr, volatile void * addr)
  106. {
  107. volatile unsigned long *a = addr;
  108. int mask, retval;
  109. a += nr >> 5;
  110. mask = 1 << (nr & 31);
  111. retval = (mask & *a) != 0;
  112. *a ^= mask;
  113. return retval;
  114. }
  115. /*
  116. * This routine doesn't need to be atomic.
  117. */
  118. static inline int __constant_test_bit(int nr, const volatile void * addr)
  119. {
  120. return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
  121. }
  122. static inline int __test_bit(int nr, const volatile void * addr)
  123. {
  124. int * a = (int *) addr;
  125. int mask;
  126. a += nr >> 5;
  127. mask = 1 << (nr & 0x1f);
  128. return ((mask & *a) != 0);
  129. }
  130. #define test_bit(nr,addr) \
  131. (__builtin_constant_p(nr) ? \
  132. __constant_test_bit((nr),(addr)) : \
  133. __test_bit((nr),(addr)))
  134. #include <asm-generic/bitops/ffs.h>
  135. #include <asm-generic/bitops/__ffs.h>
  136. #include <asm-generic/bitops/find.h>
  137. /*
  138. * fls: find last bit set.
  139. */
  140. #define fls(x) \
  141. ({ \
  142. int bit; \
  143. \
  144. asm("scan %1,gr0,%0" : "=r"(bit) : "r"(x)); \
  145. \
  146. bit ? 33 - bit : bit; \
  147. })
  148. #include <asm-generic/bitops/fls64.h>
  149. #include <asm-generic/bitops/sched.h>
  150. #include <asm-generic/bitops/hweight.h>
  151. #include <asm-generic/bitops/ext2-non-atomic.h>
  152. #define ext2_set_bit_atomic(lock,nr,addr) test_and_set_bit ((nr) ^ 0x18, (addr))
  153. #define ext2_clear_bit_atomic(lock,nr,addr) test_and_clear_bit((nr) ^ 0x18, (addr))
  154. #include <asm-generic/bitops/minix-le.h>
  155. #endif /* __KERNEL__ */
  156. #endif /* _ASM_BITOPS_H */