atomic.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 temp; \
  23. \
  24. __asm__ __volatile__( \
  25. "1: llock %0, [%1] \n" \
  26. " " #asm_op " %0, %0, %2 \n" \
  27. " scond %0, [%1] \n" \
  28. " bnz 1b \n" \
  29. : "=&r"(temp) /* Early clobber, to prevent reg reuse */ \
  30. : "r"(&v->counter), "ir"(i) \
  31. : "cc"); \
  32. } \
  33. #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
  34. static inline int atomic_##op##_return(int i, atomic_t *v) \
  35. { \
  36. unsigned int temp; \
  37. \
  38. __asm__ __volatile__( \
  39. "1: llock %0, [%1] \n" \
  40. " " #asm_op " %0, %0, %2 \n" \
  41. " scond %0, [%1] \n" \
  42. " bnz 1b \n" \
  43. : "=&r"(temp) \
  44. : "r"(&v->counter), "ir"(i) \
  45. : "cc"); \
  46. \
  47. return temp; \
  48. }
  49. #else /* !CONFIG_ARC_HAS_LLSC */
  50. #ifndef CONFIG_SMP
  51. /* violating atomic_xxx API locking protocol in UP for optimization sake */
  52. #define atomic_set(v, i) (((v)->counter) = (i))
  53. #else
  54. static inline void atomic_set(atomic_t *v, int i)
  55. {
  56. /*
  57. * Independent of hardware support, all of the atomic_xxx() APIs need
  58. * to follow the same locking rules to make sure that a "hardware"
  59. * atomic insn (e.g. LD) doesn't clobber an "emulated" atomic insn
  60. * sequence
  61. *
  62. * Thus atomic_set() despite being 1 insn (and seemingly atomic)
  63. * requires the locking.
  64. */
  65. unsigned long flags;
  66. atomic_ops_lock(flags);
  67. v->counter = i;
  68. atomic_ops_unlock(flags);
  69. }
  70. #endif
  71. /*
  72. * Non hardware assisted Atomic-R-M-W
  73. * Locking would change to irq-disabling only (UP) and spinlocks (SMP)
  74. */
  75. #define ATOMIC_OP(op, c_op, asm_op) \
  76. static inline void atomic_##op(int i, atomic_t *v) \
  77. { \
  78. unsigned long flags; \
  79. \
  80. atomic_ops_lock(flags); \
  81. v->counter c_op i; \
  82. atomic_ops_unlock(flags); \
  83. }
  84. #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
  85. static inline int atomic_##op##_return(int i, atomic_t *v) \
  86. { \
  87. unsigned long flags; \
  88. unsigned long temp; \
  89. \
  90. atomic_ops_lock(flags); \
  91. temp = v->counter; \
  92. temp c_op i; \
  93. v->counter = temp; \
  94. atomic_ops_unlock(flags); \
  95. \
  96. return temp; \
  97. }
  98. #endif /* !CONFIG_ARC_HAS_LLSC */
  99. #define ATOMIC_OPS(op, c_op, asm_op) \
  100. ATOMIC_OP(op, c_op, asm_op) \
  101. ATOMIC_OP_RETURN(op, c_op, asm_op)
  102. ATOMIC_OPS(add, +=, add)
  103. ATOMIC_OPS(sub, -=, sub)
  104. ATOMIC_OP(and, &=, and)
  105. #define atomic_clear_mask(mask, v) atomic_and(~(mask), (v))
  106. #undef ATOMIC_OPS
  107. #undef ATOMIC_OP_RETURN
  108. #undef ATOMIC_OP
  109. /**
  110. * __atomic_add_unless - add unless the number is a given value
  111. * @v: pointer of type atomic_t
  112. * @a: the amount to add to v...
  113. * @u: ...unless v is equal to u.
  114. *
  115. * Atomically adds @a to @v, so long as it was not @u.
  116. * Returns the old value of @v
  117. */
  118. #define __atomic_add_unless(v, a, u) \
  119. ({ \
  120. int c, old; \
  121. c = atomic_read(v); \
  122. while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c)\
  123. c = old; \
  124. c; \
  125. })
  126. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  127. #define atomic_inc(v) atomic_add(1, v)
  128. #define atomic_dec(v) atomic_sub(1, v)
  129. #define atomic_inc_and_test(v) (atomic_add_return(1, v) == 0)
  130. #define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
  131. #define atomic_inc_return(v) atomic_add_return(1, (v))
  132. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  133. #define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
  134. #define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
  135. #define ATOMIC_INIT(i) { (i) }
  136. #include <asm-generic/atomic64.h>
  137. #endif
  138. #endif