atomic64_32.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <linux/compiler.h>
  2. #include <linux/types.h>
  3. #include <asm/processor.h>
  4. #include <asm/cmpxchg.h>
  5. #include <asm/atomic.h>
  6. static inline u64 cmpxchg8b(u64 *ptr, u64 old, u64 new)
  7. {
  8. asm volatile(
  9. LOCK_PREFIX "cmpxchg8b (%[ptr])\n"
  10. : "=A" (old)
  11. : [ptr] "D" (ptr),
  12. "A" (old),
  13. "b" (ll_low(new)),
  14. "c" (ll_high(new))
  15. : "memory");
  16. return old;
  17. }
  18. u64 atomic64_cmpxchg(atomic64_t *ptr, u64 old_val, u64 new_val)
  19. {
  20. return cmpxchg8b(&ptr->counter, old_val, new_val);
  21. }
  22. /**
  23. * atomic64_xchg - xchg atomic64 variable
  24. * @ptr: pointer to type atomic64_t
  25. * @new_val: value to assign
  26. *
  27. * Atomically xchgs the value of @ptr to @new_val and returns
  28. * the old value.
  29. */
  30. u64 atomic64_xchg(atomic64_t *ptr, u64 new_val)
  31. {
  32. u64 old_val;
  33. do {
  34. old_val = atomic_read(ptr);
  35. } while (atomic64_cmpxchg(ptr, old_val, new_val) != old_val);
  36. return old_val;
  37. }
  38. /**
  39. * atomic64_set - set atomic64 variable
  40. * @ptr: pointer to type atomic64_t
  41. * @new_val: value to assign
  42. *
  43. * Atomically sets the value of @ptr to @new_val.
  44. */
  45. void atomic64_set(atomic64_t *ptr, u64 new_val)
  46. {
  47. atomic64_xchg(ptr, new_val);
  48. }
  49. /**
  50. * atomic64_read - read atomic64 variable
  51. * @ptr: pointer to type atomic64_t
  52. *
  53. * Atomically reads the value of @ptr and returns it.
  54. */
  55. u64 atomic64_read(atomic64_t *ptr)
  56. {
  57. u64 old = 1LL << 32;
  58. return cmpxchg8b(&ptr->counter, old, old);
  59. }
  60. /**
  61. * atomic64_add_return - add and return
  62. * @delta: integer value to add
  63. * @ptr: pointer to type atomic64_t
  64. *
  65. * Atomically adds @delta to @ptr and returns @delta + *@ptr
  66. */
  67. u64 atomic64_add_return(u64 delta, atomic64_t *ptr)
  68. {
  69. u64 old_val, new_val;
  70. do {
  71. old_val = atomic_read(ptr);
  72. new_val = old_val + delta;
  73. } while (atomic64_cmpxchg(ptr, old_val, new_val) != old_val);
  74. return new_val;
  75. }
  76. u64 atomic64_sub_return(u64 delta, atomic64_t *ptr)
  77. {
  78. return atomic64_add_return(-delta, ptr);
  79. }
  80. u64 atomic64_inc_return(atomic64_t *ptr)
  81. {
  82. return atomic64_add_return(1, ptr);
  83. }
  84. u64 atomic64_dec_return(atomic64_t *ptr)
  85. {
  86. return atomic64_sub_return(1, ptr);
  87. }
  88. /**
  89. * atomic64_add - add integer to atomic64 variable
  90. * @delta: integer value to add
  91. * @ptr: pointer to type atomic64_t
  92. *
  93. * Atomically adds @delta to @ptr.
  94. */
  95. void atomic64_add(u64 delta, atomic64_t *ptr)
  96. {
  97. atomic64_add_return(delta, ptr);
  98. }
  99. /**
  100. * atomic64_sub - subtract the atomic64 variable
  101. * @delta: integer value to subtract
  102. * @ptr: pointer to type atomic64_t
  103. *
  104. * Atomically subtracts @delta from @ptr.
  105. */
  106. void atomic64_sub(u64 delta, atomic64_t *ptr)
  107. {
  108. atomic64_add(-delta, ptr);
  109. }
  110. /**
  111. * atomic64_sub_and_test - subtract value from variable and test result
  112. * @delta: integer value to subtract
  113. * @ptr: pointer to type atomic64_t
  114. *
  115. * Atomically subtracts @delta from @ptr and returns
  116. * true if the result is zero, or false for all
  117. * other cases.
  118. */
  119. int atomic64_sub_and_test(u64 delta, atomic64_t *ptr)
  120. {
  121. u64 old_val = atomic64_sub_return(delta, ptr);
  122. return old_val == 0;
  123. }
  124. /**
  125. * atomic64_inc - increment atomic64 variable
  126. * @ptr: pointer to type atomic64_t
  127. *
  128. * Atomically increments @ptr by 1.
  129. */
  130. void atomic64_inc(atomic64_t *ptr)
  131. {
  132. atomic64_add(1, ptr);
  133. }
  134. /**
  135. * atomic64_dec - decrement atomic64 variable
  136. * @ptr: pointer to type atomic64_t
  137. *
  138. * Atomically decrements @ptr by 1.
  139. */
  140. void atomic64_dec(atomic64_t *ptr)
  141. {
  142. atomic64_sub(1, ptr);
  143. }
  144. /**
  145. * atomic64_dec_and_test - decrement and test
  146. * @ptr: pointer to type atomic64_t
  147. *
  148. * Atomically decrements @ptr by 1 and
  149. * returns true if the result is 0, or false for all other
  150. * cases.
  151. */
  152. int atomic64_dec_and_test(atomic64_t *ptr)
  153. {
  154. return atomic64_sub_and_test(1, ptr);
  155. }
  156. /**
  157. * atomic64_inc_and_test - increment and test
  158. * @ptr: pointer to type atomic64_t
  159. *
  160. * Atomically increments @ptr by 1
  161. * and returns true if the result is zero, or false for all
  162. * other cases.
  163. */
  164. int atomic64_inc_and_test(atomic64_t *ptr)
  165. {
  166. return atomic64_sub_and_test(-1, ptr);
  167. }
  168. /**
  169. * atomic64_add_negative - add and test if negative
  170. * @delta: integer value to add
  171. * @ptr: pointer to type atomic64_t
  172. *
  173. * Atomically adds @delta to @ptr and returns true
  174. * if the result is negative, or false when
  175. * result is greater than or equal to zero.
  176. */
  177. int atomic64_add_negative(u64 delta, atomic64_t *ptr)
  178. {
  179. long long old_val = atomic64_add_return(delta, ptr);
  180. return old_val < 0;
  181. }