bitops.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #ifndef _H8300_BITOPS_H
  2. #define _H8300_BITOPS_H
  3. /*
  4. * Copyright 1992, Linus Torvalds.
  5. * Copyright 2002, Yoshinori Sato
  6. */
  7. #include <linux/compiler.h>
  8. #include <asm/system.h>
  9. #ifdef __KERNEL__
  10. /*
  11. * Function prototypes to keep gcc -Wall happy
  12. */
  13. /*
  14. * ffz = Find First Zero in word. Undefined if no zero exists,
  15. * so code should check against ~0UL first..
  16. */
  17. static __inline__ unsigned long ffz(unsigned long word)
  18. {
  19. unsigned long result;
  20. result = -1;
  21. __asm__("1:\n\t"
  22. "shlr.l %2\n\t"
  23. "adds #1,%0\n\t"
  24. "bcs 1b"
  25. : "=r" (result)
  26. : "0" (result),"r" (word));
  27. return result;
  28. }
  29. #define H8300_GEN_BITOP_CONST(OP,BIT) \
  30. case BIT: \
  31. __asm__(OP " #" #BIT ",@%0"::"r"(b_addr):"memory"); \
  32. break;
  33. #define H8300_GEN_BITOP(FNAME,OP) \
  34. static __inline__ void FNAME(int nr, volatile unsigned long* addr) \
  35. { \
  36. volatile unsigned char *b_addr; \
  37. b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3); \
  38. if (__builtin_constant_p(nr)) { \
  39. switch(nr & 7) { \
  40. H8300_GEN_BITOP_CONST(OP,0) \
  41. H8300_GEN_BITOP_CONST(OP,1) \
  42. H8300_GEN_BITOP_CONST(OP,2) \
  43. H8300_GEN_BITOP_CONST(OP,3) \
  44. H8300_GEN_BITOP_CONST(OP,4) \
  45. H8300_GEN_BITOP_CONST(OP,5) \
  46. H8300_GEN_BITOP_CONST(OP,6) \
  47. H8300_GEN_BITOP_CONST(OP,7) \
  48. } \
  49. } else { \
  50. __asm__(OP " %w0,@%1"::"r"(nr),"r"(b_addr):"memory"); \
  51. } \
  52. }
  53. /*
  54. * clear_bit() doesn't provide any barrier for the compiler.
  55. */
  56. #define smp_mb__before_clear_bit() barrier()
  57. #define smp_mb__after_clear_bit() barrier()
  58. H8300_GEN_BITOP(set_bit ,"bset")
  59. H8300_GEN_BITOP(clear_bit ,"bclr")
  60. H8300_GEN_BITOP(change_bit,"bnot")
  61. #define __set_bit(nr,addr) set_bit((nr),(addr))
  62. #define __clear_bit(nr,addr) clear_bit((nr),(addr))
  63. #define __change_bit(nr,addr) change_bit((nr),(addr))
  64. #undef H8300_GEN_BITOP
  65. #undef H8300_GEN_BITOP_CONST
  66. static __inline__ int test_bit(int nr, const unsigned long* addr)
  67. {
  68. return (*((volatile unsigned char *)addr +
  69. ((nr >> 3) ^ 3)) & (1UL << (nr & 7))) != 0;
  70. }
  71. #define __test_bit(nr, addr) test_bit(nr, addr)
  72. #define H8300_GEN_TEST_BITOP_CONST_INT(OP,BIT) \
  73. case BIT: \
  74. __asm__("stc ccr,%w1\n\t" \
  75. "orc #0x80,ccr\n\t" \
  76. "bld #" #BIT ",@%4\n\t" \
  77. OP " #" #BIT ",@%4\n\t" \
  78. "rotxl.l %0\n\t" \
  79. "ldc %w1,ccr" \
  80. : "=r"(retval),"=&r"(ccrsave),"=m"(*b_addr) \
  81. : "0" (retval),"r" (b_addr) \
  82. : "memory"); \
  83. break;
  84. #define H8300_GEN_TEST_BITOP_CONST(OP,BIT) \
  85. case BIT: \
  86. __asm__("bld #" #BIT ",@%3\n\t" \
  87. OP " #" #BIT ",@%3\n\t" \
  88. "rotxl.l %0\n\t" \
  89. : "=r"(retval),"=m"(*b_addr) \
  90. : "0" (retval),"r" (b_addr) \
  91. : "memory"); \
  92. break;
  93. #define H8300_GEN_TEST_BITOP(FNNAME,OP) \
  94. static __inline__ int FNNAME(int nr, volatile void * addr) \
  95. { \
  96. int retval = 0; \
  97. char ccrsave; \
  98. volatile unsigned char *b_addr; \
  99. b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3); \
  100. if (__builtin_constant_p(nr)) { \
  101. switch(nr & 7) { \
  102. H8300_GEN_TEST_BITOP_CONST_INT(OP,0) \
  103. H8300_GEN_TEST_BITOP_CONST_INT(OP,1) \
  104. H8300_GEN_TEST_BITOP_CONST_INT(OP,2) \
  105. H8300_GEN_TEST_BITOP_CONST_INT(OP,3) \
  106. H8300_GEN_TEST_BITOP_CONST_INT(OP,4) \
  107. H8300_GEN_TEST_BITOP_CONST_INT(OP,5) \
  108. H8300_GEN_TEST_BITOP_CONST_INT(OP,6) \
  109. H8300_GEN_TEST_BITOP_CONST_INT(OP,7) \
  110. } \
  111. } else { \
  112. __asm__("stc ccr,%w1\n\t" \
  113. "orc #0x80,ccr\n\t" \
  114. "btst %w5,@%4\n\t" \
  115. OP " %w5,@%4\n\t" \
  116. "beq 1f\n\t" \
  117. "inc.l #1,%0\n" \
  118. "1:\n\t" \
  119. "ldc %w1,ccr" \
  120. : "=r"(retval),"=&r"(ccrsave),"=m"(*b_addr) \
  121. : "0" (retval),"r" (b_addr),"r"(nr) \
  122. : "memory"); \
  123. } \
  124. return retval; \
  125. } \
  126. \
  127. static __inline__ int __ ## FNNAME(int nr, volatile void * addr) \
  128. { \
  129. int retval = 0; \
  130. volatile unsigned char *b_addr; \
  131. b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3); \
  132. if (__builtin_constant_p(nr)) { \
  133. switch(nr & 7) { \
  134. H8300_GEN_TEST_BITOP_CONST(OP,0) \
  135. H8300_GEN_TEST_BITOP_CONST(OP,1) \
  136. H8300_GEN_TEST_BITOP_CONST(OP,2) \
  137. H8300_GEN_TEST_BITOP_CONST(OP,3) \
  138. H8300_GEN_TEST_BITOP_CONST(OP,4) \
  139. H8300_GEN_TEST_BITOP_CONST(OP,5) \
  140. H8300_GEN_TEST_BITOP_CONST(OP,6) \
  141. H8300_GEN_TEST_BITOP_CONST(OP,7) \
  142. } \
  143. } else { \
  144. __asm__("btst %w4,@%3\n\t" \
  145. OP " %w4,@%3\n\t" \
  146. "beq 1f\n\t" \
  147. "inc.l #1,%0\n" \
  148. "1:" \
  149. : "=r"(retval),"=m"(*b_addr) \
  150. : "0" (retval),"r" (b_addr),"r"(nr) \
  151. : "memory"); \
  152. } \
  153. return retval; \
  154. }
  155. H8300_GEN_TEST_BITOP(test_and_set_bit, "bset")
  156. H8300_GEN_TEST_BITOP(test_and_clear_bit, "bclr")
  157. H8300_GEN_TEST_BITOP(test_and_change_bit,"bnot")
  158. #undef H8300_GEN_TEST_BITOP_CONST
  159. #undef H8300_GEN_TEST_BITOP_CONST_INT
  160. #undef H8300_GEN_TEST_BITOP
  161. #include <asm-generic/bitops/ffs.h>
  162. static __inline__ unsigned long __ffs(unsigned long word)
  163. {
  164. unsigned long result;
  165. result = -1;
  166. __asm__("1:\n\t"
  167. "shlr.l %2\n\t"
  168. "adds #1,%0\n\t"
  169. "bcc 1b"
  170. : "=r" (result)
  171. : "0"(result),"r"(word));
  172. return result;
  173. }
  174. #include <asm-generic/bitops/find.h>
  175. #include <asm-generic/bitops/sched.h>
  176. #include <asm-generic/bitops/hweight.h>
  177. #include <asm-generic/bitops/ext2-non-atomic.h>
  178. #include <asm-generic/bitops/ext2-atomic.h>
  179. #include <asm-generic/bitops/minix.h>
  180. #endif /* __KERNEL__ */
  181. #include <asm-generic/bitops/fls.h>
  182. #include <asm-generic/bitops/fls64.h>
  183. #endif /* _H8300_BITOPS_H */