atomic.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef __ARCH_S390_ATOMIC__
  2. #define __ARCH_S390_ATOMIC__
  3. /*
  4. * include/asm-s390/atomic.h
  5. *
  6. * S390 version
  7. * Copyright (C) 1999-2005 IBM Deutschland Entwicklung GmbH, IBM Corporation
  8. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  9. * Denis Joseph Barrow,
  10. * Arnd Bergmann (arndb@de.ibm.com)
  11. *
  12. * Derived from "include/asm-i386/bitops.h"
  13. * Copyright (C) 1992, Linus Torvalds
  14. *
  15. */
  16. /*
  17. * Atomic operations that C can't guarantee us. Useful for
  18. * resource counting etc..
  19. * S390 uses 'Compare And Swap' for atomicity in SMP enviroment
  20. */
  21. typedef struct {
  22. volatile int counter;
  23. } __attribute__ ((aligned (4))) atomic_t;
  24. #define ATOMIC_INIT(i) { (i) }
  25. #ifdef __KERNEL__
  26. #define __CS_LOOP(ptr, op_val, op_string) ({ \
  27. typeof(ptr->counter) old_val, new_val; \
  28. __asm__ __volatile__(" l %0,0(%3)\n" \
  29. "0: lr %1,%0\n" \
  30. op_string " %1,%4\n" \
  31. " cs %0,%1,0(%3)\n" \
  32. " jl 0b" \
  33. : "=&d" (old_val), "=&d" (new_val), \
  34. "=m" (((atomic_t *)(ptr))->counter) \
  35. : "a" (ptr), "d" (op_val), \
  36. "m" (((atomic_t *)(ptr))->counter) \
  37. : "cc", "memory" ); \
  38. new_val; \
  39. })
  40. #define atomic_read(v) ((v)->counter)
  41. #define atomic_set(v,i) (((v)->counter) = (i))
  42. static __inline__ int atomic_add_return(int i, atomic_t * v)
  43. {
  44. return __CS_LOOP(v, i, "ar");
  45. }
  46. #define atomic_add(_i, _v) atomic_add_return(_i, _v)
  47. #define atomic_add_negative(_i, _v) (atomic_add_return(_i, _v) < 0)
  48. #define atomic_inc(_v) atomic_add_return(1, _v)
  49. #define atomic_inc_return(_v) atomic_add_return(1, _v)
  50. #define atomic_inc_and_test(_v) (atomic_add_return(1, _v) == 0)
  51. static __inline__ int atomic_sub_return(int i, atomic_t * v)
  52. {
  53. return __CS_LOOP(v, i, "sr");
  54. }
  55. #define atomic_sub(_i, _v) atomic_sub_return(_i, _v)
  56. #define atomic_sub_and_test(_i, _v) (atomic_sub_return(_i, _v) == 0)
  57. #define atomic_dec(_v) atomic_sub_return(1, _v)
  58. #define atomic_dec_return(_v) atomic_sub_return(1, _v)
  59. #define atomic_dec_and_test(_v) (atomic_sub_return(1, _v) == 0)
  60. static __inline__ void atomic_clear_mask(unsigned long mask, atomic_t * v)
  61. {
  62. __CS_LOOP(v, ~mask, "nr");
  63. }
  64. static __inline__ void atomic_set_mask(unsigned long mask, atomic_t * v)
  65. {
  66. __CS_LOOP(v, mask, "or");
  67. }
  68. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  69. static __inline__ int atomic_cmpxchg(atomic_t *v, int old, int new)
  70. {
  71. __asm__ __volatile__(" cs %0,%3,0(%2)\n"
  72. : "+d" (old), "=m" (v->counter)
  73. : "a" (v), "d" (new), "m" (v->counter)
  74. : "cc", "memory" );
  75. return old;
  76. }
  77. static __inline__ int atomic_add_unless(atomic_t *v, int a, int u)
  78. {
  79. int c, old;
  80. c = atomic_read(v);
  81. for (;;) {
  82. if (unlikely(c == u))
  83. break;
  84. old = atomic_cmpxchg(v, c, c + a);
  85. if (likely(old == c))
  86. break;
  87. c = old;
  88. }
  89. return c != u;
  90. }
  91. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  92. #undef __CS_LOOP
  93. #ifdef __s390x__
  94. typedef struct {
  95. volatile long long counter;
  96. } __attribute__ ((aligned (8))) atomic64_t;
  97. #define ATOMIC64_INIT(i) { (i) }
  98. #define __CSG_LOOP(ptr, op_val, op_string) ({ \
  99. typeof(ptr->counter) old_val, new_val; \
  100. __asm__ __volatile__(" lg %0,0(%3)\n" \
  101. "0: lgr %1,%0\n" \
  102. op_string " %1,%4\n" \
  103. " csg %0,%1,0(%3)\n" \
  104. " jl 0b" \
  105. : "=&d" (old_val), "=&d" (new_val), \
  106. "=m" (((atomic_t *)(ptr))->counter) \
  107. : "a" (ptr), "d" (op_val), \
  108. "m" (((atomic_t *)(ptr))->counter) \
  109. : "cc", "memory" ); \
  110. new_val; \
  111. })
  112. #define atomic64_read(v) ((v)->counter)
  113. #define atomic64_set(v,i) (((v)->counter) = (i))
  114. static __inline__ long long atomic64_add_return(long long i, atomic64_t * v)
  115. {
  116. return __CSG_LOOP(v, i, "agr");
  117. }
  118. #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
  119. #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
  120. #define atomic64_inc(_v) atomic64_add_return(1, _v)
  121. #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
  122. #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
  123. static __inline__ long long atomic64_sub_return(long long i, atomic64_t * v)
  124. {
  125. return __CSG_LOOP(v, i, "sgr");
  126. }
  127. #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
  128. #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
  129. #define atomic64_dec(_v) atomic64_sub_return(1, _v)
  130. #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
  131. #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
  132. static __inline__ void atomic64_clear_mask(unsigned long mask, atomic64_t * v)
  133. {
  134. __CSG_LOOP(v, ~mask, "ngr");
  135. }
  136. static __inline__ void atomic64_set_mask(unsigned long mask, atomic64_t * v)
  137. {
  138. __CSG_LOOP(v, mask, "ogr");
  139. }
  140. static __inline__ long long atomic64_cmpxchg(atomic64_t *v,
  141. long long old, long long new)
  142. {
  143. __asm__ __volatile__(" csg %0,%3,0(%2)\n"
  144. : "+d" (old), "=m" (v->counter)
  145. : "a" (v), "d" (new), "m" (v->counter)
  146. : "cc", "memory" );
  147. return old;
  148. }
  149. static __inline__ int atomic64_add_unless(atomic64_t *v,
  150. long long a, long long u)
  151. {
  152. long long c, old;
  153. c = atomic64_read(v);
  154. for (;;) {
  155. if (unlikely(c == u))
  156. break;
  157. old = atomic64_cmpxchg(v, c, c + a);
  158. if (likely(old == c))
  159. break;
  160. c = old;
  161. }
  162. return c != u;
  163. }
  164. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  165. #undef __CSG_LOOP
  166. #endif
  167. #define smp_mb__before_atomic_dec() smp_mb()
  168. #define smp_mb__after_atomic_dec() smp_mb()
  169. #define smp_mb__before_atomic_inc() smp_mb()
  170. #define smp_mb__after_atomic_inc() smp_mb()
  171. #include <asm-generic/atomic.h>
  172. #endif /* __KERNEL__ */
  173. #endif /* __ARCH_S390_ATOMIC__ */