bitops.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  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. #ifndef __ASM_AVR32_BITOPS_H
  9. #define __ASM_AVR32_BITOPS_H
  10. #ifndef _LINUX_BITOPS_H
  11. #error only <linux/bitops.h> can be included directly
  12. #endif
  13. #include <asm/byteorder.h>
  14. #include <asm/system.h>
  15. /*
  16. * clear_bit() doesn't provide any barrier for the compiler
  17. */
  18. #define smp_mb__before_clear_bit() barrier()
  19. #define smp_mb__after_clear_bit() barrier()
  20. /*
  21. * set_bit - Atomically set a bit in memory
  22. * @nr: the bit to set
  23. * @addr: the address to start counting from
  24. *
  25. * This function is atomic and may not be reordered. See __set_bit()
  26. * if you do not require the atomic guarantees.
  27. *
  28. * Note that @nr may be almost arbitrarily large; this function is not
  29. * restricted to acting on a single-word quantity.
  30. */
  31. static inline void set_bit(int nr, volatile void * addr)
  32. {
  33. unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
  34. unsigned long tmp;
  35. if (__builtin_constant_p(nr)) {
  36. asm volatile(
  37. "1: ssrf 5\n"
  38. " ld.w %0, %2\n"
  39. " sbr %0, %3\n"
  40. " stcond %1, %0\n"
  41. " brne 1b"
  42. : "=&r"(tmp), "=o"(*p)
  43. : "m"(*p), "i"(nr)
  44. : "cc");
  45. } else {
  46. unsigned long mask = 1UL << (nr % BITS_PER_LONG);
  47. asm volatile(
  48. "1: ssrf 5\n"
  49. " ld.w %0, %2\n"
  50. " or %0, %3\n"
  51. " stcond %1, %0\n"
  52. " brne 1b"
  53. : "=&r"(tmp), "=o"(*p)
  54. : "m"(*p), "r"(mask)
  55. : "cc");
  56. }
  57. }
  58. /*
  59. * clear_bit - Clears a bit in memory
  60. * @nr: Bit to clear
  61. * @addr: Address to start counting from
  62. *
  63. * clear_bit() is atomic and may not be reordered. However, it does
  64. * not contain a memory barrier, so if it is used for locking purposes,
  65. * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
  66. * in order to ensure changes are visible on other processors.
  67. */
  68. static inline void clear_bit(int nr, volatile void * addr)
  69. {
  70. unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
  71. unsigned long tmp;
  72. if (__builtin_constant_p(nr)) {
  73. asm volatile(
  74. "1: ssrf 5\n"
  75. " ld.w %0, %2\n"
  76. " cbr %0, %3\n"
  77. " stcond %1, %0\n"
  78. " brne 1b"
  79. : "=&r"(tmp), "=o"(*p)
  80. : "m"(*p), "i"(nr)
  81. : "cc");
  82. } else {
  83. unsigned long mask = 1UL << (nr % BITS_PER_LONG);
  84. asm volatile(
  85. "1: ssrf 5\n"
  86. " ld.w %0, %2\n"
  87. " andn %0, %3\n"
  88. " stcond %1, %0\n"
  89. " brne 1b"
  90. : "=&r"(tmp), "=o"(*p)
  91. : "m"(*p), "r"(mask)
  92. : "cc");
  93. }
  94. }
  95. /*
  96. * change_bit - Toggle a bit in memory
  97. * @nr: Bit to change
  98. * @addr: Address to start counting from
  99. *
  100. * change_bit() is atomic and may not be reordered.
  101. * Note that @nr may be almost arbitrarily large; this function is not
  102. * restricted to acting on a single-word quantity.
  103. */
  104. static inline void change_bit(int nr, volatile void * addr)
  105. {
  106. unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
  107. unsigned long mask = 1UL << (nr % BITS_PER_LONG);
  108. unsigned long tmp;
  109. asm volatile(
  110. "1: ssrf 5\n"
  111. " ld.w %0, %2\n"
  112. " eor %0, %3\n"
  113. " stcond %1, %0\n"
  114. " brne 1b"
  115. : "=&r"(tmp), "=o"(*p)
  116. : "m"(*p), "r"(mask)
  117. : "cc");
  118. }
  119. /*
  120. * test_and_set_bit - Set a bit and return its old value
  121. * @nr: Bit to set
  122. * @addr: Address to count from
  123. *
  124. * This operation is atomic and cannot be reordered.
  125. * It also implies a memory barrier.
  126. */
  127. static inline int test_and_set_bit(int nr, volatile void * addr)
  128. {
  129. unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
  130. unsigned long mask = 1UL << (nr % BITS_PER_LONG);
  131. unsigned long tmp, old;
  132. if (__builtin_constant_p(nr)) {
  133. asm volatile(
  134. "1: ssrf 5\n"
  135. " ld.w %0, %3\n"
  136. " mov %2, %0\n"
  137. " sbr %0, %4\n"
  138. " stcond %1, %0\n"
  139. " brne 1b"
  140. : "=&r"(tmp), "=o"(*p), "=&r"(old)
  141. : "m"(*p), "i"(nr)
  142. : "memory", "cc");
  143. } else {
  144. asm volatile(
  145. "1: ssrf 5\n"
  146. " ld.w %2, %3\n"
  147. " or %0, %2, %4\n"
  148. " stcond %1, %0\n"
  149. " brne 1b"
  150. : "=&r"(tmp), "=o"(*p), "=&r"(old)
  151. : "m"(*p), "r"(mask)
  152. : "memory", "cc");
  153. }
  154. return (old & mask) != 0;
  155. }
  156. /*
  157. * test_and_clear_bit - Clear a bit and return its old value
  158. * @nr: Bit to clear
  159. * @addr: Address to count from
  160. *
  161. * This operation is atomic and cannot be reordered.
  162. * It also implies a memory barrier.
  163. */
  164. static inline int test_and_clear_bit(int nr, volatile void * addr)
  165. {
  166. unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
  167. unsigned long mask = 1UL << (nr % BITS_PER_LONG);
  168. unsigned long tmp, old;
  169. if (__builtin_constant_p(nr)) {
  170. asm volatile(
  171. "1: ssrf 5\n"
  172. " ld.w %0, %3\n"
  173. " mov %2, %0\n"
  174. " cbr %0, %4\n"
  175. " stcond %1, %0\n"
  176. " brne 1b"
  177. : "=&r"(tmp), "=o"(*p), "=&r"(old)
  178. : "m"(*p), "i"(nr)
  179. : "memory", "cc");
  180. } else {
  181. asm volatile(
  182. "1: ssrf 5\n"
  183. " ld.w %0, %3\n"
  184. " mov %2, %0\n"
  185. " andn %0, %4\n"
  186. " stcond %1, %0\n"
  187. " brne 1b"
  188. : "=&r"(tmp), "=o"(*p), "=&r"(old)
  189. : "m"(*p), "r"(mask)
  190. : "memory", "cc");
  191. }
  192. return (old & mask) != 0;
  193. }
  194. /*
  195. * test_and_change_bit - Change a bit and return its old value
  196. * @nr: Bit to change
  197. * @addr: Address to count from
  198. *
  199. * This operation is atomic and cannot be reordered.
  200. * It also implies a memory barrier.
  201. */
  202. static inline int test_and_change_bit(int nr, volatile void * addr)
  203. {
  204. unsigned long *p = ((unsigned long *)addr) + nr / BITS_PER_LONG;
  205. unsigned long mask = 1UL << (nr % BITS_PER_LONG);
  206. unsigned long tmp, old;
  207. asm volatile(
  208. "1: ssrf 5\n"
  209. " ld.w %2, %3\n"
  210. " eor %0, %2, %4\n"
  211. " stcond %1, %0\n"
  212. " brne 1b"
  213. : "=&r"(tmp), "=o"(*p), "=&r"(old)
  214. : "m"(*p), "r"(mask)
  215. : "memory", "cc");
  216. return (old & mask) != 0;
  217. }
  218. #include <asm-generic/bitops/non-atomic.h>
  219. /* Find First bit Set */
  220. static inline unsigned long __ffs(unsigned long word)
  221. {
  222. unsigned long result;
  223. asm("brev %1\n\t"
  224. "clz %0,%1"
  225. : "=r"(result), "=&r"(word)
  226. : "1"(word));
  227. return result;
  228. }
  229. /* Find First Zero */
  230. static inline unsigned long ffz(unsigned long word)
  231. {
  232. return __ffs(~word);
  233. }
  234. /* Find Last bit Set */
  235. static inline int fls(unsigned long word)
  236. {
  237. unsigned long result;
  238. asm("clz %0,%1" : "=r"(result) : "r"(word));
  239. return 32 - result;
  240. }
  241. unsigned long find_first_zero_bit(const unsigned long *addr,
  242. unsigned long size);
  243. unsigned long find_next_zero_bit(const unsigned long *addr,
  244. unsigned long size,
  245. unsigned long offset);
  246. unsigned long find_first_bit(const unsigned long *addr,
  247. unsigned long size);
  248. unsigned long find_next_bit(const unsigned long *addr,
  249. unsigned long size,
  250. unsigned long offset);
  251. /*
  252. * ffs: find first bit set. This is defined the same way as
  253. * the libc and compiler builtin ffs routines, therefore
  254. * differs in spirit from the above ffz (man ffs).
  255. *
  256. * The difference is that bit numbering starts at 1, and if no bit is set,
  257. * the function returns 0.
  258. */
  259. static inline int ffs(unsigned long word)
  260. {
  261. if(word == 0)
  262. return 0;
  263. return __ffs(word) + 1;
  264. }
  265. #include <asm-generic/bitops/fls64.h>
  266. #include <asm-generic/bitops/sched.h>
  267. #include <asm-generic/bitops/hweight.h>
  268. #include <asm-generic/bitops/lock.h>
  269. #include <asm-generic/bitops/ext2-non-atomic.h>
  270. #include <asm-generic/bitops/ext2-atomic.h>
  271. #include <asm-generic/bitops/minix-le.h>
  272. #endif /* __ASM_AVR32_BITOPS_H */