atomic.h 4.2 KB

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