bitops.h 5.9 KB

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