atomic.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/spr-regs.h>
  18. #include <asm/cmpxchg.h>
  19. #include <asm/barrier.h>
  20. #ifdef CONFIG_SMP
  21. #error not SMP safe
  22. #endif
  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) ACCESS_ONCE((v)->counter)
  31. #define atomic_set(v, i) (((v)->counter) = (i))
  32. #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
  33. static inline int atomic_add_return(int i, atomic_t *v)
  34. {
  35. unsigned long val;
  36. asm("0: \n"
  37. " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
  38. " ckeq icc3,cc7 \n"
  39. " ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
  40. " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
  41. " add%I2 %1,%2,%1 \n"
  42. " cst.p %1,%M0 ,cc3,#1 \n"
  43. " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
  44. " beq icc3,#0,0b \n"
  45. : "+U"(v->counter), "=&r"(val)
  46. : "NPr"(i)
  47. : "memory", "cc7", "cc3", "icc3"
  48. );
  49. return val;
  50. }
  51. static inline int atomic_sub_return(int i, atomic_t *v)
  52. {
  53. unsigned long val;
  54. asm("0: \n"
  55. " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
  56. " ckeq icc3,cc7 \n"
  57. " ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
  58. " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
  59. " sub%I2 %1,%2,%1 \n"
  60. " cst.p %1,%M0 ,cc3,#1 \n"
  61. " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
  62. " beq icc3,#0,0b \n"
  63. : "+U"(v->counter), "=&r"(val)
  64. : "NPr"(i)
  65. : "memory", "cc7", "cc3", "icc3"
  66. );
  67. return val;
  68. }
  69. #else
  70. extern int atomic_add_return(int i, atomic_t *v);
  71. extern int atomic_sub_return(int i, atomic_t *v);
  72. #endif
  73. static inline int atomic_add_negative(int i, atomic_t *v)
  74. {
  75. return atomic_add_return(i, v) < 0;
  76. }
  77. static inline void atomic_add(int i, atomic_t *v)
  78. {
  79. atomic_add_return(i, v);
  80. }
  81. static inline void atomic_sub(int i, atomic_t *v)
  82. {
  83. atomic_sub_return(i, v);
  84. }
  85. static inline void atomic_inc(atomic_t *v)
  86. {
  87. atomic_add_return(1, v);
  88. }
  89. static inline void atomic_dec(atomic_t *v)
  90. {
  91. atomic_sub_return(1, v);
  92. }
  93. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  94. #define atomic_inc_return(v) atomic_add_return(1, (v))
  95. #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
  96. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  97. #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
  98. /*
  99. * 64-bit atomic ops
  100. */
  101. typedef struct {
  102. volatile long long counter;
  103. } atomic64_t;
  104. #define ATOMIC64_INIT(i) { (i) }
  105. static inline long long atomic64_read(atomic64_t *v)
  106. {
  107. long long counter;
  108. asm("ldd%I1 %M1,%0"
  109. : "=e"(counter)
  110. : "m"(v->counter));
  111. return counter;
  112. }
  113. static inline void atomic64_set(atomic64_t *v, long long i)
  114. {
  115. asm volatile("std%I0 %1,%M0"
  116. : "=m"(v->counter)
  117. : "e"(i));
  118. }
  119. extern long long atomic64_inc_return(atomic64_t *v);
  120. extern long long atomic64_dec_return(atomic64_t *v);
  121. extern long long atomic64_add_return(long long i, atomic64_t *v);
  122. extern long long atomic64_sub_return(long long i, atomic64_t *v);
  123. static inline long long atomic64_add_negative(long long i, atomic64_t *v)
  124. {
  125. return atomic64_add_return(i, v) < 0;
  126. }
  127. static inline void atomic64_add(long long i, atomic64_t *v)
  128. {
  129. atomic64_add_return(i, v);
  130. }
  131. static inline void atomic64_sub(long long i, atomic64_t *v)
  132. {
  133. atomic64_sub_return(i, v);
  134. }
  135. static inline void atomic64_inc(atomic64_t *v)
  136. {
  137. atomic64_inc_return(v);
  138. }
  139. static inline void atomic64_dec(atomic64_t *v)
  140. {
  141. atomic64_dec_return(v);
  142. }
  143. #define atomic64_sub_and_test(i,v) (atomic64_sub_return((i), (v)) == 0)
  144. #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
  145. #define atomic64_inc_and_test(v) (atomic64_inc_return((v)) == 0)
  146. #define atomic_cmpxchg(v, old, new) (cmpxchg(&(v)->counter, old, new))
  147. #define atomic_xchg(v, new) (xchg(&(v)->counter, new))
  148. #define atomic64_cmpxchg(v, old, new) (__cmpxchg_64(old, new, &(v)->counter))
  149. #define atomic64_xchg(v, new) (__xchg_64(new, &(v)->counter))
  150. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  151. {
  152. int c, old;
  153. c = atomic_read(v);
  154. for (;;) {
  155. if (unlikely(c == (u)))
  156. break;
  157. old = atomic_cmpxchg((v), c, c + (a));
  158. if (likely(old == c))
  159. break;
  160. c = old;
  161. }
  162. return c;
  163. }
  164. #endif /* _ASM_ATOMIC_H */