atomic.h 5.9 KB

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