atomic.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* atomic.h: atomic operation emulation for FR-V
  2. *
  3. * For an explanation of how atomic ops work in this arch, see:
  4. * Documentation/frv/atomic-ops.txt
  5. *
  6. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  7. * Written by David Howells (dhowells@redhat.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #ifndef _ASM_ATOMIC_H
  15. #define _ASM_ATOMIC_H
  16. #include <linux/types.h>
  17. #include <asm/cmpxchg.h>
  18. #include <asm/barrier.h>
  19. #ifdef CONFIG_SMP
  20. #error not SMP safe
  21. #endif
  22. #include <asm/atomic_defs.h>
  23. /*
  24. * Atomic operations that C can't guarantee us. Useful for
  25. * resource counting etc..
  26. *
  27. * We do not have SMP systems, so we don't have to deal with that.
  28. */
  29. #define ATOMIC_INIT(i) { (i) }
  30. #define atomic_read(v) READ_ONCE((v)->counter)
  31. #define atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
  32. static inline int atomic_inc_return(atomic_t *v)
  33. {
  34. return __atomic_add_return(1, &v->counter);
  35. }
  36. static inline int atomic_dec_return(atomic_t *v)
  37. {
  38. return __atomic_sub_return(1, &v->counter);
  39. }
  40. static inline int atomic_add_return(int i, atomic_t *v)
  41. {
  42. return __atomic_add_return(i, &v->counter);
  43. }
  44. static inline int atomic_sub_return(int i, atomic_t *v)
  45. {
  46. return __atomic_sub_return(i, &v->counter);
  47. }
  48. static inline int atomic_add_negative(int i, atomic_t *v)
  49. {
  50. return atomic_add_return(i, v) < 0;
  51. }
  52. static inline void atomic_inc(atomic_t *v)
  53. {
  54. atomic_inc_return(v);
  55. }
  56. static inline void atomic_dec(atomic_t *v)
  57. {
  58. atomic_dec_return(v);
  59. }
  60. #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
  61. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  62. #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
  63. /*
  64. * 64-bit atomic ops
  65. */
  66. typedef struct {
  67. long long counter;
  68. } atomic64_t;
  69. #define ATOMIC64_INIT(i) { (i) }
  70. static inline long long atomic64_read(const atomic64_t *v)
  71. {
  72. long long counter;
  73. asm("ldd%I1 %M1,%0"
  74. : "=e"(counter)
  75. : "m"(v->counter));
  76. return counter;
  77. }
  78. static inline void atomic64_set(atomic64_t *v, long long i)
  79. {
  80. asm volatile("std%I0 %1,%M0"
  81. : "=m"(v->counter)
  82. : "e"(i));
  83. }
  84. static inline long long atomic64_inc_return(atomic64_t *v)
  85. {
  86. return __atomic64_add_return(1, &v->counter);
  87. }
  88. static inline long long atomic64_dec_return(atomic64_t *v)
  89. {
  90. return __atomic64_sub_return(1, &v->counter);
  91. }
  92. static inline long long atomic64_add_return(long long i, atomic64_t *v)
  93. {
  94. return __atomic64_add_return(i, &v->counter);
  95. }
  96. static inline long long atomic64_sub_return(long long i, atomic64_t *v)
  97. {
  98. return __atomic64_sub_return(i, &v->counter);
  99. }
  100. static inline long long atomic64_add_negative(long long i, atomic64_t *v)
  101. {
  102. return atomic64_add_return(i, v) < 0;
  103. }
  104. static inline void atomic64_inc(atomic64_t *v)
  105. {
  106. atomic64_inc_return(v);
  107. }
  108. static inline void atomic64_dec(atomic64_t *v)
  109. {
  110. atomic64_dec_return(v);
  111. }
  112. #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
  113. #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
  114. #define atomic64_inc_and_test(v) (atomic64_inc_return((v)) == 0)
  115. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  116. #define atomic_cmpxchg(v, old, new) (cmpxchg(&(v)->counter, old, new))
  117. #define atomic_xchg(v, new) (xchg(&(v)->counter, new))
  118. #define atomic64_cmpxchg(v, old, new) (__cmpxchg_64(old, new, &(v)->counter))
  119. #define atomic64_xchg(v, new) (__xchg_64(new, &(v)->counter))
  120. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  121. {
  122. int c, old;
  123. c = atomic_read(v);
  124. for (;;) {
  125. if (unlikely(c == (u)))
  126. break;
  127. old = atomic_cmpxchg((v), c, c + (a));
  128. if (likely(old == c))
  129. break;
  130. c = old;
  131. }
  132. return c;
  133. }
  134. static inline int atomic64_add_unless(atomic64_t *v, long long i, long long u)
  135. {
  136. long long c, old;
  137. c = atomic64_read(v);
  138. for (;;) {
  139. if (unlikely(c == u))
  140. break;
  141. old = atomic64_cmpxchg(v, c, c + i);
  142. if (likely(old == c))
  143. break;
  144. c = old;
  145. }
  146. return c != u;
  147. }
  148. static inline long long atomic64_dec_if_positive(atomic64_t *v)
  149. {
  150. long long c, old, dec;
  151. c = atomic64_read(v);
  152. for (;;) {
  153. dec = c - 1;
  154. if (unlikely(dec < 0))
  155. break;
  156. old = atomic64_cmpxchg((v), c, dec);
  157. if (likely(old == c))
  158. break;
  159. c = old;
  160. }
  161. return dec;
  162. }
  163. #define ATOMIC_OP(op) \
  164. static inline int atomic_fetch_##op(int i, atomic_t *v) \
  165. { \
  166. return __atomic32_fetch_##op(i, &v->counter); \
  167. } \
  168. static inline void atomic_##op(int i, atomic_t *v) \
  169. { \
  170. (void)__atomic32_fetch_##op(i, &v->counter); \
  171. } \
  172. \
  173. static inline long long atomic64_fetch_##op(long long i, atomic64_t *v) \
  174. { \
  175. return __atomic64_fetch_##op(i, &v->counter); \
  176. } \
  177. static inline void atomic64_##op(long long i, atomic64_t *v) \
  178. { \
  179. (void)__atomic64_fetch_##op(i, &v->counter); \
  180. }
  181. ATOMIC_OP(or)
  182. ATOMIC_OP(and)
  183. ATOMIC_OP(xor)
  184. ATOMIC_OP(add)
  185. ATOMIC_OP(sub)
  186. #undef ATOMIC_OP
  187. #endif /* _ASM_ATOMIC_H */