atomic.h 5.4 KB

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