bitops.h 7.5 KB

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