atomic.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  3. * Copyright (C) 2006 Kyle McMartin <kyle@parisc-linux.org>
  4. */
  5. #ifndef _ASM_PARISC_ATOMIC_H_
  6. #define _ASM_PARISC_ATOMIC_H_
  7. #include <linux/types.h>
  8. #include <asm/cmpxchg.h>
  9. #include <asm/barrier.h>
  10. /*
  11. * Atomic operations that C can't guarantee us. Useful for
  12. * resource counting etc..
  13. *
  14. * And probably incredibly slow on parisc. OTOH, we don't
  15. * have to write any serious assembly. prumpf
  16. */
  17. #ifdef CONFIG_SMP
  18. #include <asm/spinlock.h>
  19. #include <asm/cache.h> /* we use L1_CACHE_BYTES */
  20. /* Use an array of spinlocks for our atomic_ts.
  21. * Hash function to index into a different SPINLOCK.
  22. * Since "a" is usually an address, use one spinlock per cacheline.
  23. */
  24. # define ATOMIC_HASH_SIZE 4
  25. # define ATOMIC_HASH(a) (&(__atomic_hash[ (((unsigned long) (a))/L1_CACHE_BYTES) & (ATOMIC_HASH_SIZE-1) ]))
  26. extern arch_spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] __lock_aligned;
  27. /* Can't use raw_spin_lock_irq because of #include problems, so
  28. * this is the substitute */
  29. #define _atomic_spin_lock_irqsave(l,f) do { \
  30. arch_spinlock_t *s = ATOMIC_HASH(l); \
  31. local_irq_save(f); \
  32. arch_spin_lock(s); \
  33. } while(0)
  34. #define _atomic_spin_unlock_irqrestore(l,f) do { \
  35. arch_spinlock_t *s = ATOMIC_HASH(l); \
  36. arch_spin_unlock(s); \
  37. local_irq_restore(f); \
  38. } while(0)
  39. #else
  40. # define _atomic_spin_lock_irqsave(l,f) do { local_irq_save(f); } while (0)
  41. # define _atomic_spin_unlock_irqrestore(l,f) do { local_irq_restore(f); } while (0)
  42. #endif
  43. /*
  44. * Note that we need not lock read accesses - aligned word writes/reads
  45. * are atomic, so a reader never sees inconsistent values.
  46. */
  47. static __inline__ void arch_atomic_set(atomic_t *v, int i)
  48. {
  49. unsigned long flags;
  50. _atomic_spin_lock_irqsave(v, flags);
  51. v->counter = i;
  52. _atomic_spin_unlock_irqrestore(v, flags);
  53. }
  54. #define arch_atomic_set_release(v, i) arch_atomic_set((v), (i))
  55. static __inline__ int arch_atomic_read(const atomic_t *v)
  56. {
  57. return READ_ONCE((v)->counter);
  58. }
  59. #define ATOMIC_OP(op, c_op) \
  60. static __inline__ void arch_atomic_##op(int i, atomic_t *v) \
  61. { \
  62. unsigned long flags; \
  63. \
  64. _atomic_spin_lock_irqsave(v, flags); \
  65. v->counter c_op i; \
  66. _atomic_spin_unlock_irqrestore(v, flags); \
  67. }
  68. #define ATOMIC_OP_RETURN(op, c_op) \
  69. static __inline__ int arch_atomic_##op##_return(int i, atomic_t *v) \
  70. { \
  71. unsigned long flags; \
  72. int ret; \
  73. \
  74. _atomic_spin_lock_irqsave(v, flags); \
  75. ret = (v->counter c_op i); \
  76. _atomic_spin_unlock_irqrestore(v, flags); \
  77. \
  78. return ret; \
  79. }
  80. #define ATOMIC_FETCH_OP(op, c_op) \
  81. static __inline__ int arch_atomic_fetch_##op(int i, atomic_t *v) \
  82. { \
  83. unsigned long flags; \
  84. int ret; \
  85. \
  86. _atomic_spin_lock_irqsave(v, flags); \
  87. ret = v->counter; \
  88. v->counter c_op i; \
  89. _atomic_spin_unlock_irqrestore(v, flags); \
  90. \
  91. return ret; \
  92. }
  93. #define ATOMIC_OPS(op, c_op) \
  94. ATOMIC_OP(op, c_op) \
  95. ATOMIC_OP_RETURN(op, c_op) \
  96. ATOMIC_FETCH_OP(op, c_op)
  97. ATOMIC_OPS(add, +=)
  98. ATOMIC_OPS(sub, -=)
  99. #define arch_atomic_add_return arch_atomic_add_return
  100. #define arch_atomic_sub_return arch_atomic_sub_return
  101. #define arch_atomic_fetch_add arch_atomic_fetch_add
  102. #define arch_atomic_fetch_sub arch_atomic_fetch_sub
  103. #undef ATOMIC_OPS
  104. #define ATOMIC_OPS(op, c_op) \
  105. ATOMIC_OP(op, c_op) \
  106. ATOMIC_FETCH_OP(op, c_op)
  107. ATOMIC_OPS(and, &=)
  108. ATOMIC_OPS(or, |=)
  109. ATOMIC_OPS(xor, ^=)
  110. #define arch_atomic_fetch_and arch_atomic_fetch_and
  111. #define arch_atomic_fetch_or arch_atomic_fetch_or
  112. #define arch_atomic_fetch_xor arch_atomic_fetch_xor
  113. #undef ATOMIC_OPS
  114. #undef ATOMIC_FETCH_OP
  115. #undef ATOMIC_OP_RETURN
  116. #undef ATOMIC_OP
  117. #ifdef CONFIG_64BIT
  118. #define ATOMIC64_INIT(i) { (i) }
  119. #define ATOMIC64_OP(op, c_op) \
  120. static __inline__ void arch_atomic64_##op(s64 i, atomic64_t *v) \
  121. { \
  122. unsigned long flags; \
  123. \
  124. _atomic_spin_lock_irqsave(v, flags); \
  125. v->counter c_op i; \
  126. _atomic_spin_unlock_irqrestore(v, flags); \
  127. }
  128. #define ATOMIC64_OP_RETURN(op, c_op) \
  129. static __inline__ s64 arch_atomic64_##op##_return(s64 i, atomic64_t *v) \
  130. { \
  131. unsigned long flags; \
  132. s64 ret; \
  133. \
  134. _atomic_spin_lock_irqsave(v, flags); \
  135. ret = (v->counter c_op i); \
  136. _atomic_spin_unlock_irqrestore(v, flags); \
  137. \
  138. return ret; \
  139. }
  140. #define ATOMIC64_FETCH_OP(op, c_op) \
  141. static __inline__ s64 arch_atomic64_fetch_##op(s64 i, atomic64_t *v) \
  142. { \
  143. unsigned long flags; \
  144. s64 ret; \
  145. \
  146. _atomic_spin_lock_irqsave(v, flags); \
  147. ret = v->counter; \
  148. v->counter c_op i; \
  149. _atomic_spin_unlock_irqrestore(v, flags); \
  150. \
  151. return ret; \
  152. }
  153. #define ATOMIC64_OPS(op, c_op) \
  154. ATOMIC64_OP(op, c_op) \
  155. ATOMIC64_OP_RETURN(op, c_op) \
  156. ATOMIC64_FETCH_OP(op, c_op)
  157. ATOMIC64_OPS(add, +=)
  158. ATOMIC64_OPS(sub, -=)
  159. #define arch_atomic64_add_return arch_atomic64_add_return
  160. #define arch_atomic64_sub_return arch_atomic64_sub_return
  161. #define arch_atomic64_fetch_add arch_atomic64_fetch_add
  162. #define arch_atomic64_fetch_sub arch_atomic64_fetch_sub
  163. #undef ATOMIC64_OPS
  164. #define ATOMIC64_OPS(op, c_op) \
  165. ATOMIC64_OP(op, c_op) \
  166. ATOMIC64_FETCH_OP(op, c_op)
  167. ATOMIC64_OPS(and, &=)
  168. ATOMIC64_OPS(or, |=)
  169. ATOMIC64_OPS(xor, ^=)
  170. #define arch_atomic64_fetch_and arch_atomic64_fetch_and
  171. #define arch_atomic64_fetch_or arch_atomic64_fetch_or
  172. #define arch_atomic64_fetch_xor arch_atomic64_fetch_xor
  173. #undef ATOMIC64_OPS
  174. #undef ATOMIC64_FETCH_OP
  175. #undef ATOMIC64_OP_RETURN
  176. #undef ATOMIC64_OP
  177. static __inline__ void
  178. arch_atomic64_set(atomic64_t *v, s64 i)
  179. {
  180. unsigned long flags;
  181. _atomic_spin_lock_irqsave(v, flags);
  182. v->counter = i;
  183. _atomic_spin_unlock_irqrestore(v, flags);
  184. }
  185. #define arch_atomic64_set_release(v, i) arch_atomic64_set((v), (i))
  186. static __inline__ s64
  187. arch_atomic64_read(const atomic64_t *v)
  188. {
  189. return READ_ONCE((v)->counter);
  190. }
  191. #endif /* !CONFIG_64BIT */
  192. #endif /* _ASM_PARISC_ATOMIC_H_ */