atomic.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_add(int i, atomic_t *v)
  53. {
  54. atomic_add_return(i, v);
  55. }
  56. static inline void atomic_sub(int i, atomic_t *v)
  57. {
  58. atomic_sub_return(i, v);
  59. }
  60. static inline void atomic_inc(atomic_t *v)
  61. {
  62. atomic_inc_return(v);
  63. }
  64. static inline void atomic_dec(atomic_t *v)
  65. {
  66. atomic_dec_return(v);
  67. }
  68. #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
  69. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  70. #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
  71. /*
  72. * 64-bit atomic ops
  73. */
  74. typedef struct {
  75. long long counter;
  76. } atomic64_t;
  77. #define ATOMIC64_INIT(i) { (i) }
  78. static inline long long atomic64_read(const atomic64_t *v)
  79. {
  80. long long counter;
  81. asm("ldd%I1 %M1,%0"
  82. : "=e"(counter)
  83. : "m"(v->counter));
  84. return counter;
  85. }
  86. static inline void atomic64_set(atomic64_t *v, long long i)
  87. {
  88. asm volatile("std%I0 %1,%M0"
  89. : "=m"(v->counter)
  90. : "e"(i));
  91. }
  92. static inline long long atomic64_inc_return(atomic64_t *v)
  93. {
  94. return __atomic64_add_return(1, &v->counter);
  95. }
  96. static inline long long atomic64_dec_return(atomic64_t *v)
  97. {
  98. return __atomic64_sub_return(1, &v->counter);
  99. }
  100. static inline long long atomic64_add_return(long long i, atomic64_t *v)
  101. {
  102. return __atomic64_add_return(i, &v->counter);
  103. }
  104. static inline long long atomic64_sub_return(long long i, atomic64_t *v)
  105. {
  106. return __atomic64_sub_return(i, &v->counter);
  107. }
  108. static inline long long atomic64_add_negative(long long i, atomic64_t *v)
  109. {
  110. return atomic64_add_return(i, v) < 0;
  111. }
  112. static inline void atomic64_add(long long i, atomic64_t *v)
  113. {
  114. atomic64_add_return(i, v);
  115. }
  116. static inline void atomic64_sub(long long i, atomic64_t *v)
  117. {
  118. atomic64_sub_return(i, v);
  119. }
  120. static inline void atomic64_inc(atomic64_t *v)
  121. {
  122. atomic64_inc_return(v);
  123. }
  124. static inline void atomic64_dec(atomic64_t *v)
  125. {
  126. atomic64_dec_return(v);
  127. }
  128. #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
  129. #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
  130. #define atomic64_inc_and_test(v) (atomic64_inc_return((v)) == 0)
  131. #define atomic_cmpxchg(v, old, new) (cmpxchg(&(v)->counter, old, new))
  132. #define atomic_xchg(v, new) (xchg(&(v)->counter, new))
  133. #define atomic64_cmpxchg(v, old, new) (__cmpxchg_64(old, new, &(v)->counter))
  134. #define atomic64_xchg(v, new) (__xchg_64(new, &(v)->counter))
  135. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  136. {
  137. int c, old;
  138. c = atomic_read(v);
  139. for (;;) {
  140. if (unlikely(c == (u)))
  141. break;
  142. old = atomic_cmpxchg((v), c, c + (a));
  143. if (likely(old == c))
  144. break;
  145. c = old;
  146. }
  147. return c;
  148. }
  149. #define ATOMIC_OP(op) \
  150. static inline void atomic_##op(int i, atomic_t *v) \
  151. { \
  152. (void)__atomic32_fetch_##op(i, &v->counter); \
  153. } \
  154. \
  155. static inline void atomic64_##op(long long i, atomic64_t *v) \
  156. { \
  157. (void)__atomic64_fetch_##op(i, &v->counter); \
  158. }
  159. ATOMIC_OP(or)
  160. ATOMIC_OP(and)
  161. ATOMIC_OP(xor)
  162. #undef ATOMIC_OP
  163. #endif /* _ASM_ATOMIC_H */