atomic.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. while (c != u && (old = atomic_cmpxchg(v, c, c + a)) != c)
  82. c = old;
  83. return c != u;
  84. }
  85. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  86. #undef __CS_LOOP
  87. #ifdef __s390x__
  88. typedef struct {
  89. volatile long long counter;
  90. } __attribute__ ((aligned (8))) atomic64_t;
  91. #define ATOMIC64_INIT(i) { (i) }
  92. #define __CSG_LOOP(ptr, op_val, op_string) ({ \
  93. typeof(ptr->counter) old_val, new_val; \
  94. __asm__ __volatile__(" lg %0,0(%3)\n" \
  95. "0: lgr %1,%0\n" \
  96. op_string " %1,%4\n" \
  97. " csg %0,%1,0(%3)\n" \
  98. " jl 0b" \
  99. : "=&d" (old_val), "=&d" (new_val), \
  100. "=m" (((atomic_t *)(ptr))->counter) \
  101. : "a" (ptr), "d" (op_val), \
  102. "m" (((atomic_t *)(ptr))->counter) \
  103. : "cc", "memory" ); \
  104. new_val; \
  105. })
  106. #define atomic64_read(v) ((v)->counter)
  107. #define atomic64_set(v,i) (((v)->counter) = (i))
  108. static __inline__ long long atomic64_add_return(long long i, atomic64_t * v)
  109. {
  110. return __CSG_LOOP(v, i, "agr");
  111. }
  112. #define atomic64_add(_i, _v) atomic64_add_return(_i, _v)
  113. #define atomic64_add_negative(_i, _v) (atomic64_add_return(_i, _v) < 0)
  114. #define atomic64_inc(_v) atomic64_add_return(1, _v)
  115. #define atomic64_inc_return(_v) atomic64_add_return(1, _v)
  116. #define atomic64_inc_and_test(_v) (atomic64_add_return(1, _v) == 0)
  117. static __inline__ long long atomic64_sub_return(long long i, atomic64_t * v)
  118. {
  119. return __CSG_LOOP(v, i, "sgr");
  120. }
  121. #define atomic64_sub(_i, _v) atomic64_sub_return(_i, _v)
  122. #define atomic64_sub_and_test(_i, _v) (atomic64_sub_return(_i, _v) == 0)
  123. #define atomic64_dec(_v) atomic64_sub_return(1, _v)
  124. #define atomic64_dec_return(_v) atomic64_sub_return(1, _v)
  125. #define atomic64_dec_and_test(_v) (atomic64_sub_return(1, _v) == 0)
  126. static __inline__ void atomic64_clear_mask(unsigned long mask, atomic64_t * v)
  127. {
  128. __CSG_LOOP(v, ~mask, "ngr");
  129. }
  130. static __inline__ void atomic64_set_mask(unsigned long mask, atomic64_t * v)
  131. {
  132. __CSG_LOOP(v, mask, "ogr");
  133. }
  134. static __inline__ long long atomic64_cmpxchg(atomic64_t *v,
  135. long long old, long long new)
  136. {
  137. __asm__ __volatile__(" csg %0,%3,0(%2)\n"
  138. : "+d" (old), "=m" (v->counter)
  139. : "a" (v), "d" (new), "m" (v->counter)
  140. : "cc", "memory" );
  141. return old;
  142. }
  143. static __inline__ int atomic64_add_unless(atomic64_t *v,
  144. long long a, long long u)
  145. {
  146. long long c, old;
  147. c = atomic64_read(v);
  148. while (c != u && (old = atomic64_cmpxchg(v, c, c + a)) != c)
  149. c = old;
  150. return c != u;
  151. }
  152. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  153. #undef __CSG_LOOP
  154. #endif
  155. #define smp_mb__before_atomic_dec() smp_mb()
  156. #define smp_mb__after_atomic_dec() smp_mb()
  157. #define smp_mb__before_atomic_inc() smp_mb()
  158. #define smp_mb__after_atomic_inc() smp_mb()
  159. #include <asm-generic/atomic.h>
  160. #endif /* __KERNEL__ */
  161. #endif /* __ARCH_S390_ATOMIC__ */