atomic.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifndef _ASM_GENERIC_BITOPS_ATOMIC_H_
  2. #define _ASM_GENERIC_BITOPS_ATOMIC_H_
  3. #include <asm/types.h>
  4. #define BITOP_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  5. #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
  6. #ifdef CONFIG_SMP
  7. #include <asm/spinlock.h>
  8. #include <asm/cache.h> /* we use L1_CACHE_BYTES */
  9. /* Use an array of spinlocks for our atomic_ts.
  10. * Hash function to index into a different SPINLOCK.
  11. * Since "a" is usually an address, use one spinlock per cacheline.
  12. */
  13. # define ATOMIC_HASH_SIZE 4
  14. # define ATOMIC_HASH(a) (&(__atomic_hash[ (((unsigned long) a)/L1_CACHE_BYTES) & (ATOMIC_HASH_SIZE-1) ]))
  15. extern raw_spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] __lock_aligned;
  16. /* Can't use raw_spin_lock_irq because of #include problems, so
  17. * this is the substitute */
  18. #define _atomic_spin_lock_irqsave(l,f) do { \
  19. raw_spinlock_t *s = ATOMIC_HASH(l); \
  20. local_irq_save(f); \
  21. __raw_spin_lock(s); \
  22. } while(0)
  23. #define _atomic_spin_unlock_irqrestore(l,f) do { \
  24. raw_spinlock_t *s = ATOMIC_HASH(l); \
  25. __raw_spin_unlock(s); \
  26. local_irq_restore(f); \
  27. } while(0)
  28. #else
  29. # define _atomic_spin_lock_irqsave(l,f) do { local_irq_save(f); } while (0)
  30. # define _atomic_spin_unlock_irqrestore(l,f) do { local_irq_restore(f); } while (0)
  31. #endif
  32. /*
  33. * NMI events can occur at any time, including when interrupts have been
  34. * disabled by *_irqsave(). So you can get NMI events occurring while a
  35. * *_bit function is holding a spin lock. If the NMI handler also wants
  36. * to do bit manipulation (and they do) then you can get a deadlock
  37. * between the original caller of *_bit() and the NMI handler.
  38. *
  39. * by Keith Owens
  40. */
  41. /**
  42. * set_bit - Atomically set a bit in memory
  43. * @nr: the bit to set
  44. * @addr: the address to start counting from
  45. *
  46. * This function is atomic and may not be reordered. See __set_bit()
  47. * if you do not require the atomic guarantees.
  48. *
  49. * Note: there are no guarantees that this function will not be reordered
  50. * on non x86 architectures, so if you are writting portable code,
  51. * make sure not to rely on its reordering guarantees.
  52. *
  53. * Note that @nr may be almost arbitrarily large; this function is not
  54. * restricted to acting on a single-word quantity.
  55. */
  56. static inline void set_bit(int nr, volatile unsigned long *addr)
  57. {
  58. unsigned long mask = BITOP_MASK(nr);
  59. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  60. unsigned long flags;
  61. _atomic_spin_lock_irqsave(p, flags);
  62. *p |= mask;
  63. _atomic_spin_unlock_irqrestore(p, flags);
  64. }
  65. /**
  66. * clear_bit - Clears a bit in memory
  67. * @nr: Bit to clear
  68. * @addr: Address to start counting from
  69. *
  70. * clear_bit() is atomic and may not be reordered. However, it does
  71. * not contain a memory barrier, so if it is used for locking purposes,
  72. * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
  73. * in order to ensure changes are visible on other processors.
  74. */
  75. static inline void clear_bit(int nr, volatile unsigned long *addr)
  76. {
  77. unsigned long mask = BITOP_MASK(nr);
  78. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  79. unsigned long flags;
  80. _atomic_spin_lock_irqsave(p, flags);
  81. *p &= ~mask;
  82. _atomic_spin_unlock_irqrestore(p, flags);
  83. }
  84. /**
  85. * change_bit - Toggle a bit in memory
  86. * @nr: Bit to change
  87. * @addr: Address to start counting from
  88. *
  89. * change_bit() is atomic and may not be reordered. It may be
  90. * reordered on other architectures than x86.
  91. * Note that @nr may be almost arbitrarily large; this function is not
  92. * restricted to acting on a single-word quantity.
  93. */
  94. static inline void change_bit(int nr, volatile unsigned long *addr)
  95. {
  96. unsigned long mask = BITOP_MASK(nr);
  97. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  98. unsigned long flags;
  99. _atomic_spin_lock_irqsave(p, flags);
  100. *p ^= mask;
  101. _atomic_spin_unlock_irqrestore(p, flags);
  102. }
  103. /**
  104. * test_and_set_bit - Set a bit and return its old value
  105. * @nr: Bit to set
  106. * @addr: Address to count from
  107. *
  108. * This operation is atomic and cannot be reordered.
  109. * It may be reordered on other architectures than x86.
  110. * It also implies a memory barrier.
  111. */
  112. static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
  113. {
  114. unsigned long mask = BITOP_MASK(nr);
  115. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  116. unsigned long old;
  117. unsigned long flags;
  118. _atomic_spin_lock_irqsave(p, flags);
  119. old = *p;
  120. *p = old | mask;
  121. _atomic_spin_unlock_irqrestore(p, flags);
  122. return (old & mask) != 0;
  123. }
  124. /**
  125. * test_and_clear_bit - Clear a bit and return its old value
  126. * @nr: Bit to clear
  127. * @addr: Address to count from
  128. *
  129. * This operation is atomic and cannot be reordered.
  130. * It can be reorderdered on other architectures other than x86.
  131. * It also implies a memory barrier.
  132. */
  133. static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
  134. {
  135. unsigned long mask = BITOP_MASK(nr);
  136. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  137. unsigned long old;
  138. unsigned long flags;
  139. _atomic_spin_lock_irqsave(p, flags);
  140. old = *p;
  141. *p = old & ~mask;
  142. _atomic_spin_unlock_irqrestore(p, flags);
  143. return (old & mask) != 0;
  144. }
  145. /**
  146. * test_and_change_bit - Change a bit and return its old value
  147. * @nr: Bit to change
  148. * @addr: Address to count from
  149. *
  150. * This operation is atomic and cannot be reordered.
  151. * It also implies a memory barrier.
  152. */
  153. static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
  154. {
  155. unsigned long mask = BITOP_MASK(nr);
  156. unsigned long *p = ((unsigned long *)addr) + BITOP_WORD(nr);
  157. unsigned long old;
  158. unsigned long flags;
  159. _atomic_spin_lock_irqsave(p, flags);
  160. old = *p;
  161. *p = old ^ mask;
  162. _atomic_spin_unlock_irqrestore(p, flags);
  163. return (old & mask) != 0;
  164. }
  165. #endif /* _ASM_GENERIC_BITOPS_ATOMIC_H */