atomic64_32.c 4.2 KB

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