bitops.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* asm/bitops.h for Linux/CRIS
  2. *
  3. * TODO: asm versions if speed is needed
  4. *
  5. * All bit operations return 0 if the bit was cleared before the
  6. * operation and != 0 if it was not.
  7. *
  8. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  9. */
  10. #ifndef _CRIS_BITOPS_H
  11. #define _CRIS_BITOPS_H
  12. /* Currently this is unsuitable for consumption outside the kernel. */
  13. #ifdef __KERNEL__
  14. #ifndef _LINUX_BITOPS_H
  15. #error only <linux/bitops.h> can be included directly
  16. #endif
  17. #include <arch/bitops.h>
  18. #include <linux/atomic.h>
  19. #include <linux/compiler.h>
  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. * Note that @nr may be almost arbitrarily large; this function is not
  28. * restricted to acting on a single-word quantity.
  29. */
  30. #define set_bit(nr, addr) (void)test_and_set_bit(nr, addr)
  31. /*
  32. * clear_bit - Clears a bit in memory
  33. * @nr: Bit to clear
  34. * @addr: Address to start counting from
  35. *
  36. * clear_bit() is atomic and may not be reordered. However, it does
  37. * not contain a memory barrier, so if it is used for locking purposes,
  38. * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
  39. * in order to ensure changes are visible on other processors.
  40. */
  41. #define clear_bit(nr, addr) (void)test_and_clear_bit(nr, addr)
  42. /*
  43. * change_bit - Toggle a bit in memory
  44. * @nr: Bit to change
  45. * @addr: Address to start counting from
  46. *
  47. * change_bit() is atomic and may not be reordered.
  48. * Note that @nr may be almost arbitrarily large; this function is not
  49. * restricted to acting on a single-word quantity.
  50. */
  51. #define change_bit(nr, addr) (void)test_and_change_bit(nr, addr)
  52. /**
  53. * test_and_set_bit - Set a bit and return its old value
  54. * @nr: Bit to set
  55. * @addr: Address to count from
  56. *
  57. * This operation is atomic and cannot be reordered.
  58. * It also implies a memory barrier.
  59. */
  60. static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
  61. {
  62. unsigned int mask, retval;
  63. unsigned long flags;
  64. unsigned int *adr = (unsigned int *)addr;
  65. adr += nr >> 5;
  66. mask = 1 << (nr & 0x1f);
  67. cris_atomic_save(addr, flags);
  68. retval = (mask & *adr) != 0;
  69. *adr |= mask;
  70. cris_atomic_restore(addr, flags);
  71. return retval;
  72. }
  73. /*
  74. * clear_bit() doesn't provide any barrier for the compiler.
  75. */
  76. #define smp_mb__before_clear_bit() barrier()
  77. #define smp_mb__after_clear_bit() barrier()
  78. /**
  79. * test_and_clear_bit - Clear a bit and return its old value
  80. * @nr: Bit to clear
  81. * @addr: Address to count from
  82. *
  83. * This operation is atomic and cannot be reordered.
  84. * It also implies a memory barrier.
  85. */
  86. static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
  87. {
  88. unsigned int mask, retval;
  89. unsigned long flags;
  90. unsigned int *adr = (unsigned int *)addr;
  91. adr += nr >> 5;
  92. mask = 1 << (nr & 0x1f);
  93. cris_atomic_save(addr, flags);
  94. retval = (mask & *adr) != 0;
  95. *adr &= ~mask;
  96. cris_atomic_restore(addr, flags);
  97. return retval;
  98. }
  99. /**
  100. * test_and_change_bit - Change a bit and return its old value
  101. * @nr: Bit to change
  102. * @addr: Address to count from
  103. *
  104. * This operation is atomic and cannot be reordered.
  105. * It also implies a memory barrier.
  106. */
  107. static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
  108. {
  109. unsigned int mask, retval;
  110. unsigned long flags;
  111. unsigned int *adr = (unsigned int *)addr;
  112. adr += nr >> 5;
  113. mask = 1 << (nr & 0x1f);
  114. cris_atomic_save(addr, flags);
  115. retval = (mask & *adr) != 0;
  116. *adr ^= mask;
  117. cris_atomic_restore(addr, flags);
  118. return retval;
  119. }
  120. #include <asm-generic/bitops/non-atomic.h>
  121. /*
  122. * Since we define it "external", it collides with the built-in
  123. * definition, which doesn't have the same semantics. We don't want to
  124. * use -fno-builtin, so just hide the name ffs.
  125. */
  126. #define ffs kernel_ffs
  127. #include <asm-generic/bitops/fls.h>
  128. #include <asm-generic/bitops/__fls.h>
  129. #include <asm-generic/bitops/fls64.h>
  130. #include <asm-generic/bitops/hweight.h>
  131. #include <asm-generic/bitops/find.h>
  132. #include <asm-generic/bitops/lock.h>
  133. #include <asm-generic/bitops/le.h>
  134. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  135. #include <asm-generic/bitops/sched.h>
  136. #endif /* __KERNEL__ */
  137. #endif /* _CRIS_BITOPS_H */