bitops.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * U-boot - bitops.h Routines for bit operations
  3. *
  4. * Copyright (c) 2005 blackfin.uclinux.org
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  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 as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #ifndef _BLACKFIN_BITOPS_H
  25. #define _BLACKFIN_BITOPS_H
  26. /*
  27. * Copyright 1992, Linus Torvalds.
  28. */
  29. #include <linux/config.h>
  30. #include <asm/byteorder.h>
  31. #include <asm/system.h>
  32. #ifdef __KERNEL__
  33. /*
  34. * Function prototypes to keep gcc -Wall happy
  35. */
  36. /*
  37. * The __ functions are not atomic
  38. */
  39. /*
  40. * ffz = Find First Zero in word. Undefined if no zero exists,
  41. * so code should check against ~0UL first..
  42. */
  43. static __inline__ unsigned long ffz(unsigned long word)
  44. {
  45. unsigned long result = 0;
  46. while (word & 1) {
  47. result++;
  48. word >>= 1;
  49. }
  50. return result;
  51. }
  52. static __inline__ void set_bit(int nr, volatile void *addr)
  53. {
  54. int *a = (int *) addr;
  55. int mask;
  56. unsigned long flags;
  57. a += nr >> 5;
  58. mask = 1 << (nr & 0x1f);
  59. save_and_cli(flags);
  60. *a |= mask;
  61. restore_flags(flags);
  62. }
  63. static __inline__ void __set_bit(int nr, volatile void *addr)
  64. {
  65. int *a = (int *) addr;
  66. int mask;
  67. a += nr >> 5;
  68. mask = 1 << (nr & 0x1f);
  69. *a |= mask;
  70. }
  71. /*
  72. * clear_bit() doesn't provide any barrier for the compiler.
  73. */
  74. #define smp_mb__before_clear_bit() barrier()
  75. #define smp_mb__after_clear_bit() barrier()
  76. static __inline__ void clear_bit(int nr, volatile void *addr)
  77. {
  78. int *a = (int *) addr;
  79. int mask;
  80. unsigned long flags;
  81. a += nr >> 5;
  82. mask = 1 << (nr & 0x1f);
  83. save_and_cli(flags);
  84. *a &= ~mask;
  85. restore_flags(flags);
  86. }
  87. static __inline__ void change_bit(int nr, volatile void *addr)
  88. {
  89. int mask, flags;
  90. unsigned long *ADDR = (unsigned long *) addr;
  91. ADDR += nr >> 5;
  92. mask = 1 << (nr & 31);
  93. save_and_cli(flags);
  94. *ADDR ^= mask;
  95. restore_flags(flags);
  96. }
  97. static __inline__ void __change_bit(int nr, volatile void *addr)
  98. {
  99. int mask;
  100. unsigned long *ADDR = (unsigned long *) addr;
  101. ADDR += nr >> 5;
  102. mask = 1 << (nr & 31);
  103. *ADDR ^= mask;
  104. }
  105. static __inline__ int test_and_set_bit(int nr, volatile void *addr)
  106. {
  107. int mask, retval;
  108. volatile unsigned int *a = (volatile unsigned int *) addr;
  109. unsigned long flags;
  110. a += nr >> 5;
  111. mask = 1 << (nr & 0x1f);
  112. save_and_cli(flags);
  113. retval = (mask & *a) != 0;
  114. *a |= mask;
  115. restore_flags(flags);
  116. return retval;
  117. }
  118. static __inline__ int __test_and_set_bit(int nr, volatile void *addr)
  119. {
  120. int mask, retval;
  121. volatile unsigned int *a = (volatile unsigned int *) addr;
  122. a += nr >> 5;
  123. mask = 1 << (nr & 0x1f);
  124. retval = (mask & *a) != 0;
  125. *a |= mask;
  126. return retval;
  127. }
  128. static __inline__ int test_and_clear_bit(int nr, volatile void *addr)
  129. {
  130. int mask, retval;
  131. volatile unsigned int *a = (volatile unsigned int *) addr;
  132. unsigned long flags;
  133. a += nr >> 5;
  134. mask = 1 << (nr & 0x1f);
  135. save_and_cli(flags);
  136. retval = (mask & *a) != 0;
  137. *a &= ~mask;
  138. restore_flags(flags);
  139. return retval;
  140. }
  141. static __inline__ int __test_and_clear_bit(int nr, volatile void *addr)
  142. {
  143. int mask, retval;
  144. volatile unsigned int *a = (volatile unsigned int *) addr;
  145. a += nr >> 5;
  146. mask = 1 << (nr & 0x1f);
  147. retval = (mask & *a) != 0;
  148. *a &= ~mask;
  149. return retval;
  150. }
  151. static __inline__ int test_and_change_bit(int nr, volatile void *addr)
  152. {
  153. int mask, retval;
  154. volatile unsigned int *a = (volatile unsigned int *) addr;
  155. unsigned long flags;
  156. a += nr >> 5;
  157. mask = 1 << (nr & 0x1f);
  158. save_and_cli(flags);
  159. retval = (mask & *a) != 0;
  160. *a ^= mask;
  161. restore_flags(flags);
  162. return retval;
  163. }
  164. static __inline__ int __test_and_change_bit(int nr, volatile void *addr)
  165. {
  166. int mask, retval;
  167. volatile unsigned int *a = (volatile unsigned int *) addr;
  168. a += nr >> 5;
  169. mask = 1 << (nr & 0x1f);
  170. retval = (mask & *a) != 0;
  171. *a ^= mask;
  172. return retval;
  173. }
  174. /*
  175. * This routine doesn't need to be atomic.
  176. */
  177. static __inline__ int __constant_test_bit(int nr,
  178. const volatile void *addr)
  179. {
  180. return ((1UL << (nr & 31)) &
  181. (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
  182. }
  183. static __inline__ int __test_bit(int nr, volatile void *addr)
  184. {
  185. int *a = (int *) addr;
  186. int mask;
  187. a += nr >> 5;
  188. mask = 1 << (nr & 0x1f);
  189. return ((mask & *a) != 0);
  190. }
  191. #define test_bit(nr,addr) \
  192. (__builtin_constant_p(nr) ? \
  193. __constant_test_bit((nr),(addr)) : \
  194. __test_bit((nr),(addr)))
  195. #define find_first_zero_bit(addr, size) \
  196. find_next_zero_bit((addr), (size), 0)
  197. static __inline__ int find_next_zero_bit(void *addr, int size, int offset)
  198. {
  199. unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
  200. unsigned long result = offset & ~31UL;
  201. unsigned long tmp;
  202. if (offset >= size)
  203. return size;
  204. size -= result;
  205. offset &= 31UL;
  206. if (offset) {
  207. tmp = *(p++);
  208. tmp |= ~0UL >> (32 - offset);
  209. if (size < 32)
  210. goto found_first;
  211. if (~tmp)
  212. goto found_middle;
  213. size -= 32;
  214. result += 32;
  215. }
  216. while (size & ~31UL) {
  217. if (~(tmp = *(p++)))
  218. goto found_middle;
  219. result += 32;
  220. size -= 32;
  221. }
  222. if (!size)
  223. return result;
  224. tmp = *p;
  225. found_first:
  226. tmp |= ~0UL >> size;
  227. found_middle:
  228. return result + ffz(tmp);
  229. }
  230. /*
  231. * ffs: find first bit set. This is defined the same way as
  232. * the libc and compiler builtin ffs routines, therefore
  233. * differs in spirit from the above ffz (man ffs).
  234. */
  235. #define ffs(x) generic_ffs(x)
  236. /*
  237. * hweightN: returns the hamming weight (i.e. the number
  238. * of bits set) of a N-bit word
  239. */
  240. #define hweight32(x) generic_hweight32(x)
  241. #define hweight16(x) generic_hweight16(x)
  242. #define hweight8(x) generic_hweight8(x)
  243. static __inline__ int ext2_set_bit(int nr, volatile void *addr)
  244. {
  245. int mask, retval;
  246. unsigned long flags;
  247. volatile unsigned char *ADDR = (unsigned char *) addr;
  248. ADDR += nr >> 3;
  249. mask = 1 << (nr & 0x07);
  250. save_and_cli(flags);
  251. retval = (mask & *ADDR) != 0;
  252. *ADDR |= mask;
  253. restore_flags(flags);
  254. return retval;
  255. }
  256. static __inline__ int ext2_clear_bit(int nr, volatile void *addr)
  257. {
  258. int mask, retval;
  259. unsigned long flags;
  260. volatile unsigned char *ADDR = (unsigned char *) addr;
  261. ADDR += nr >> 3;
  262. mask = 1 << (nr & 0x07);
  263. save_and_cli(flags);
  264. retval = (mask & *ADDR) != 0;
  265. *ADDR &= ~mask;
  266. restore_flags(flags);
  267. return retval;
  268. }
  269. static __inline__ int ext2_test_bit(int nr, const volatile void *addr)
  270. {
  271. int mask;
  272. const volatile unsigned char *ADDR = (const unsigned char *) addr;
  273. ADDR += nr >> 3;
  274. mask = 1 << (nr & 0x07);
  275. return ((mask & *ADDR) != 0);
  276. }
  277. #define ext2_find_first_zero_bit(addr, size) \
  278. ext2_find_next_zero_bit((addr), (size), 0)
  279. static __inline__ unsigned long ext2_find_next_zero_bit(void *addr,
  280. unsigned long size,
  281. unsigned long
  282. offset)
  283. {
  284. unsigned long *p = ((unsigned long *) addr) + (offset >> 5);
  285. unsigned long result = offset & ~31UL;
  286. unsigned long tmp;
  287. if (offset >= size)
  288. return size;
  289. size -= result;
  290. offset &= 31UL;
  291. if (offset) {
  292. tmp = *(p++);
  293. tmp |= ~0UL >> (32 - offset);
  294. if (size < 32)
  295. goto found_first;
  296. if (~tmp)
  297. goto found_middle;
  298. size -= 32;
  299. result += 32;
  300. }
  301. while (size & ~31UL) {
  302. if (~(tmp = *(p++)))
  303. goto found_middle;
  304. result += 32;
  305. size -= 32;
  306. }
  307. if (!size)
  308. return result;
  309. tmp = *p;
  310. found_first:
  311. tmp |= ~0UL >> size;
  312. found_middle:
  313. return result + ffz(tmp);
  314. }
  315. /* Bitmap functions for the minix filesystem. */
  316. #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
  317. #define minix_set_bit(nr,addr) set_bit(nr,addr)
  318. #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
  319. #define minix_test_bit(nr,addr) test_bit(nr,addr)
  320. #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
  321. #endif
  322. #endif