atomic.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/system.h>
  19. #ifdef CONFIG_SMP
  20. #error not SMP safe
  21. #endif
  22. /*
  23. * Atomic operations that C can't guarantee us. Useful for
  24. * resource counting etc..
  25. *
  26. * We do not have SMP systems, so we don't have to deal with that.
  27. */
  28. /* Atomic operations are already serializing */
  29. #define smp_mb__before_atomic_dec() barrier()
  30. #define smp_mb__after_atomic_dec() barrier()
  31. #define smp_mb__before_atomic_inc() barrier()
  32. #define smp_mb__after_atomic_inc() barrier()
  33. #define ATOMIC_INIT(i) { (i) }
  34. #define atomic_read(v) ((v)->counter)
  35. #define atomic_set(v, i) (((v)->counter) = (i))
  36. #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
  37. static inline int atomic_add_return(int i, atomic_t *v)
  38. {
  39. unsigned long val;
  40. asm("0: \n"
  41. " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
  42. " ckeq icc3,cc7 \n"
  43. " ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
  44. " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
  45. " add%I2 %1,%2,%1 \n"
  46. " cst.p %1,%M0 ,cc3,#1 \n"
  47. " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
  48. " beq icc3,#0,0b \n"
  49. : "+U"(v->counter), "=&r"(val)
  50. : "NPr"(i)
  51. : "memory", "cc7", "cc3", "icc3"
  52. );
  53. return val;
  54. }
  55. static inline int atomic_sub_return(int i, atomic_t *v)
  56. {
  57. unsigned long val;
  58. asm("0: \n"
  59. " orcc gr0,gr0,gr0,icc3 \n" /* set ICC3.Z */
  60. " ckeq icc3,cc7 \n"
  61. " ld.p %M0,%1 \n" /* LD.P/ORCR must be atomic */
  62. " orcr cc7,cc7,cc3 \n" /* set CC3 to true */
  63. " sub%I2 %1,%2,%1 \n"
  64. " cst.p %1,%M0 ,cc3,#1 \n"
  65. " corcc gr29,gr29,gr0 ,cc3,#1 \n" /* clear ICC3.Z if store happens */
  66. " beq icc3,#0,0b \n"
  67. : "+U"(v->counter), "=&r"(val)
  68. : "NPr"(i)
  69. : "memory", "cc7", "cc3", "icc3"
  70. );
  71. return val;
  72. }
  73. #else
  74. extern int atomic_add_return(int i, atomic_t *v);
  75. extern int atomic_sub_return(int i, atomic_t *v);
  76. #endif
  77. static inline int atomic_add_negative(int i, atomic_t *v)
  78. {
  79. return atomic_add_return(i, v) < 0;
  80. }
  81. static inline void atomic_add(int i, atomic_t *v)
  82. {
  83. atomic_add_return(i, v);
  84. }
  85. static inline void atomic_sub(int i, atomic_t *v)
  86. {
  87. atomic_sub_return(i, v);
  88. }
  89. static inline void atomic_inc(atomic_t *v)
  90. {
  91. atomic_add_return(1, v);
  92. }
  93. static inline void atomic_dec(atomic_t *v)
  94. {
  95. atomic_sub_return(1, v);
  96. }
  97. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  98. #define atomic_inc_return(v) atomic_add_return(1, (v))
  99. #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
  100. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  101. #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
  102. /*****************************************************************************/
  103. /*
  104. * exchange value with memory
  105. */
  106. #ifndef CONFIG_FRV_OUTOFLINE_ATOMIC_OPS
  107. #define xchg(ptr, x) \
  108. ({ \
  109. __typeof__(ptr) __xg_ptr = (ptr); \
  110. __typeof__(*(ptr)) __xg_orig; \
  111. \
  112. switch (sizeof(__xg_orig)) { \
  113. case 4: \
  114. asm volatile( \
  115. "swap%I0 %M0,%1" \
  116. : "+m"(*__xg_ptr), "=r"(__xg_orig) \
  117. : "1"(x) \
  118. : "memory" \
  119. ); \
  120. break; \
  121. \
  122. default: \
  123. __xg_orig = (__typeof__(__xg_orig))0; \
  124. asm volatile("break"); \
  125. break; \
  126. } \
  127. \
  128. __xg_orig; \
  129. })
  130. #else
  131. extern uint32_t __xchg_32(uint32_t i, volatile void *v);
  132. #define xchg(ptr, x) \
  133. ({ \
  134. __typeof__(ptr) __xg_ptr = (ptr); \
  135. __typeof__(*(ptr)) __xg_orig; \
  136. \
  137. switch (sizeof(__xg_orig)) { \
  138. case 4: __xg_orig = (__typeof__(*(ptr))) __xchg_32((uint32_t) x, __xg_ptr); break; \
  139. default: \
  140. __xg_orig = (__typeof__(__xg_orig))0; \
  141. asm volatile("break"); \
  142. break; \
  143. } \
  144. __xg_orig; \
  145. })
  146. #endif
  147. #define tas(ptr) (xchg((ptr), 1))
  148. #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), old, new))
  149. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  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 != (u);
  163. }
  164. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  165. #include <asm-generic/atomic.h>
  166. #endif /* _ASM_ATOMIC_H */