atomic.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #ifndef _ASM_ARC_ATOMIC_H
  9. #define _ASM_ARC_ATOMIC_H
  10. #ifndef __ASSEMBLY__
  11. #include <linux/types.h>
  12. #include <linux/compiler.h>
  13. #include <asm/cmpxchg.h>
  14. #include <asm/barrier.h>
  15. #include <asm/smp.h>
  16. #define atomic_read(v) ((v)->counter)
  17. #ifdef CONFIG_ARC_HAS_LLSC
  18. #define atomic_set(v, i) (((v)->counter) = (i))
  19. #define ATOMIC_OP(op, c_op, asm_op) \
  20. static inline void atomic_##op(int i, atomic_t *v) \
  21. { \
  22. unsigned int val; \
  23. \
  24. __asm__ __volatile__( \
  25. "1: llock %[val], [%[ctr]] \n" \
  26. " " #asm_op " %[val], %[val], %[i] \n" \
  27. " scond %[val], [%[ctr]] \n" \
  28. " bnz 1b \n" \
  29. : [val] "=&r" (val) /* Early clobber to prevent reg reuse */ \
  30. : [ctr] "r" (&v->counter), /* Not "m": llock only supports reg direct addr mode */ \
  31. [i] "ir" (i) \
  32. : "cc"); \
  33. } \
  34. #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
  35. static inline int atomic_##op##_return(int i, atomic_t *v) \
  36. { \
  37. unsigned int val; \
  38. \
  39. /* \
  40. * Explicit full memory barrier needed before/after as \
  41. * LLOCK/SCOND thmeselves don't provide any such semantics \
  42. */ \
  43. smp_mb(); \
  44. \
  45. __asm__ __volatile__( \
  46. "1: llock %[val], [%[ctr]] \n" \
  47. " " #asm_op " %[val], %[val], %[i] \n" \
  48. " scond %[val], [%[ctr]] \n" \
  49. " bnz 1b \n" \
  50. : [val] "=&r" (val) \
  51. : [ctr] "r" (&v->counter), \
  52. [i] "ir" (i) \
  53. : "cc"); \
  54. \
  55. smp_mb(); \
  56. \
  57. return val; \
  58. }
  59. #else /* !CONFIG_ARC_HAS_LLSC */
  60. #ifndef CONFIG_SMP
  61. /* violating atomic_xxx API locking protocol in UP for optimization sake */
  62. #define atomic_set(v, i) (((v)->counter) = (i))
  63. #else
  64. static inline void atomic_set(atomic_t *v, int i)
  65. {
  66. /*
  67. * Independent of hardware support, all of the atomic_xxx() APIs need
  68. * to follow the same locking rules to make sure that a "hardware"
  69. * atomic insn (e.g. LD) doesn't clobber an "emulated" atomic insn
  70. * sequence
  71. *
  72. * Thus atomic_set() despite being 1 insn (and seemingly atomic)
  73. * requires the locking.
  74. */
  75. unsigned long flags;
  76. atomic_ops_lock(flags);
  77. v->counter = i;
  78. atomic_ops_unlock(flags);
  79. }
  80. #endif
  81. /*
  82. * Non hardware assisted Atomic-R-M-W
  83. * Locking would change to irq-disabling only (UP) and spinlocks (SMP)
  84. */
  85. #define ATOMIC_OP(op, c_op, asm_op) \
  86. static inline void atomic_##op(int i, atomic_t *v) \
  87. { \
  88. unsigned long flags; \
  89. \
  90. atomic_ops_lock(flags); \
  91. v->counter c_op i; \
  92. atomic_ops_unlock(flags); \
  93. }
  94. #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
  95. static inline int atomic_##op##_return(int i, atomic_t *v) \
  96. { \
  97. unsigned long flags; \
  98. unsigned long temp; \
  99. \
  100. /* \
  101. * spin lock/unlock provides the needed smp_mb() before/after \
  102. */ \
  103. atomic_ops_lock(flags); \
  104. temp = v->counter; \
  105. temp c_op i; \
  106. v->counter = temp; \
  107. atomic_ops_unlock(flags); \
  108. \
  109. return temp; \
  110. }
  111. #endif /* !CONFIG_ARC_HAS_LLSC */
  112. #define ATOMIC_OPS(op, c_op, asm_op) \
  113. ATOMIC_OP(op, c_op, asm_op) \
  114. ATOMIC_OP_RETURN(op, c_op, asm_op)
  115. ATOMIC_OPS(add, +=, add)
  116. ATOMIC_OPS(sub, -=, sub)
  117. ATOMIC_OP(and, &=, and)
  118. #define atomic_clear_mask(mask, v) atomic_and(~(mask), (v))
  119. #undef ATOMIC_OPS
  120. #undef ATOMIC_OP_RETURN
  121. #undef ATOMIC_OP
  122. /**
  123. * __atomic_add_unless - add unless the number is a given value
  124. * @v: pointer of type atomic_t
  125. * @a: the amount to add to v...
  126. * @u: ...unless v is equal to u.
  127. *
  128. * Atomically adds @a to @v, so long as it was not @u.
  129. * Returns the old value of @v
  130. */
  131. #define __atomic_add_unless(v, a, u) \
  132. ({ \
  133. int c, old; \
  134. \
  135. /* \
  136. * Explicit full memory barrier needed before/after as \
  137. * LLOCK/SCOND thmeselves don't provide any such semantics \
  138. */ \
  139. smp_mb(); \
  140. \
  141. c = atomic_read(v); \
  142. while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c)\
  143. c = old; \
  144. \
  145. smp_mb(); \
  146. \
  147. c; \
  148. })
  149. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  150. #define atomic_inc(v) atomic_add(1, v)
  151. #define atomic_dec(v) atomic_sub(1, v)
  152. #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
  153. #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
  154. #define atomic_inc_return(v) atomic_add_return(1, (v))
  155. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  156. #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
  157. #define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
  158. #define ATOMIC_INIT(i) { (i) }
  159. #include <asm-generic/atomic64.h>
  160. #endif
  161. #endif