atomic64_32.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 curr_val;
  58. do {
  59. curr_val = __atomic64_read(ptr);
  60. } while (atomic64_cmpxchg(ptr, curr_val, curr_val) != curr_val);
  61. return curr_val;
  62. }
  63. /**
  64. * atomic64_add_return - add and return
  65. * @delta: integer value to add
  66. * @ptr: pointer to type atomic64_t
  67. *
  68. * Atomically adds @delta to @ptr and returns @delta + *@ptr
  69. */
  70. u64 atomic64_add_return(u64 delta, atomic64_t *ptr)
  71. {
  72. u64 old_val, new_val;
  73. do {
  74. old_val = atomic_read(ptr);
  75. new_val = old_val + delta;
  76. } while (atomic64_cmpxchg(ptr, old_val, new_val) != old_val);
  77. return new_val;
  78. }
  79. u64 atomic64_sub_return(u64 delta, atomic64_t *ptr)
  80. {
  81. return atomic64_add_return(-delta, ptr);
  82. }
  83. u64 atomic64_inc_return(atomic64_t *ptr)
  84. {
  85. return atomic64_add_return(1, ptr);
  86. }
  87. u64 atomic64_dec_return(atomic64_t *ptr)
  88. {
  89. return atomic64_sub_return(1, ptr);
  90. }
  91. /**
  92. * atomic64_add - add integer to atomic64 variable
  93. * @delta: integer value to add
  94. * @ptr: pointer to type atomic64_t
  95. *
  96. * Atomically adds @delta to @ptr.
  97. */
  98. void atomic64_add(u64 delta, atomic64_t *ptr)
  99. {
  100. atomic64_add_return(delta, ptr);
  101. }
  102. /**
  103. * atomic64_sub - subtract the atomic64 variable
  104. * @delta: integer value to subtract
  105. * @ptr: pointer to type atomic64_t
  106. *
  107. * Atomically subtracts @delta from @ptr.
  108. */
  109. void atomic64_sub(u64 delta, atomic64_t *ptr)
  110. {
  111. atomic64_add(-delta, ptr);
  112. }
  113. /**
  114. * atomic64_sub_and_test - subtract value from variable and test result
  115. * @delta: integer value to subtract
  116. * @ptr: pointer to type atomic64_t
  117. *
  118. * Atomically subtracts @delta from @ptr and returns
  119. * true if the result is zero, or false for all
  120. * other cases.
  121. */
  122. int atomic64_sub_and_test(u64 delta, atomic64_t *ptr)
  123. {
  124. u64 old_val = atomic64_sub_return(delta, ptr);
  125. return old_val == 0;
  126. }
  127. /**
  128. * atomic64_inc - increment atomic64 variable
  129. * @ptr: pointer to type atomic64_t
  130. *
  131. * Atomically increments @ptr by 1.
  132. */
  133. void atomic64_inc(atomic64_t *ptr)
  134. {
  135. atomic64_add(1, ptr);
  136. }
  137. /**
  138. * atomic64_dec - decrement atomic64 variable
  139. * @ptr: pointer to type atomic64_t
  140. *
  141. * Atomically decrements @ptr by 1.
  142. */
  143. void atomic64_dec(atomic64_t *ptr)
  144. {
  145. atomic64_sub(1, ptr);
  146. }
  147. /**
  148. * atomic64_dec_and_test - decrement and test
  149. * @ptr: pointer to type atomic64_t
  150. *
  151. * Atomically decrements @ptr by 1 and
  152. * returns true if the result is 0, or false for all other
  153. * cases.
  154. */
  155. int atomic64_dec_and_test(atomic64_t *ptr)
  156. {
  157. return atomic64_sub_and_test(1, ptr);
  158. }
  159. /**
  160. * atomic64_inc_and_test - increment and test
  161. * @ptr: pointer to type atomic64_t
  162. *
  163. * Atomically increments @ptr by 1
  164. * and returns true if the result is zero, or false for all
  165. * other cases.
  166. */
  167. int atomic64_inc_and_test(atomic64_t *ptr)
  168. {
  169. return atomic64_sub_and_test(-1, ptr);
  170. }
  171. /**
  172. * atomic64_add_negative - add and test if negative
  173. * @delta: integer value to add
  174. * @ptr: pointer to type atomic64_t
  175. *
  176. * Atomically adds @delta to @ptr and returns true
  177. * if the result is negative, or false when
  178. * result is greater than or equal to zero.
  179. */
  180. int atomic64_add_negative(u64 delta, atomic64_t *ptr)
  181. {
  182. long long old_val = atomic64_add_return(delta, ptr);
  183. return old_val < 0;
  184. }