atomic64_32.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #ifndef _ASM_X86_ATOMIC64_32_H
  2. #define _ASM_X86_ATOMIC64_32_H
  3. #include <linux/compiler.h>
  4. #include <linux/types.h>
  5. #include <asm/processor.h>
  6. //#include <asm/cmpxchg.h>
  7. /* An 64bit atomic type */
  8. typedef struct {
  9. u64 __aligned(8) counter;
  10. } atomic64_t;
  11. #define ATOMIC64_INIT(val) { (val) }
  12. #define __ATOMIC64_DECL(sym) void atomic64_##sym(atomic64_t *, ...)
  13. #ifndef ATOMIC64_EXPORT
  14. #define ATOMIC64_DECL_ONE __ATOMIC64_DECL
  15. #else
  16. #define ATOMIC64_DECL_ONE(sym) __ATOMIC64_DECL(sym); \
  17. ATOMIC64_EXPORT(atomic64_##sym)
  18. #endif
  19. #ifdef CONFIG_X86_CMPXCHG64
  20. #define __alternative_atomic64(f, g, out, in...) \
  21. asm volatile("call %P[func]" \
  22. : out : [func] "i" (atomic64_##g##_cx8), ## in)
  23. #define ATOMIC64_DECL(sym) ATOMIC64_DECL_ONE(sym##_cx8)
  24. #else
  25. #define __alternative_atomic64(f, g, out, in...) \
  26. alternative_call(atomic64_##f##_386, atomic64_##g##_cx8, \
  27. X86_FEATURE_CX8, ASM_OUTPUT2(out), ## in)
  28. #define ATOMIC64_DECL(sym) ATOMIC64_DECL_ONE(sym##_cx8); \
  29. ATOMIC64_DECL_ONE(sym##_386)
  30. ATOMIC64_DECL_ONE(add_386);
  31. ATOMIC64_DECL_ONE(sub_386);
  32. ATOMIC64_DECL_ONE(inc_386);
  33. ATOMIC64_DECL_ONE(dec_386);
  34. #endif
  35. #define alternative_atomic64(f, out, in...) \
  36. __alternative_atomic64(f, f, ASM_OUTPUT2(out), ## in)
  37. ATOMIC64_DECL(read);
  38. ATOMIC64_DECL(set);
  39. ATOMIC64_DECL(xchg);
  40. ATOMIC64_DECL(add_return);
  41. ATOMIC64_DECL(sub_return);
  42. ATOMIC64_DECL(inc_return);
  43. ATOMIC64_DECL(dec_return);
  44. ATOMIC64_DECL(dec_if_positive);
  45. ATOMIC64_DECL(inc_not_zero);
  46. ATOMIC64_DECL(add_unless);
  47. #undef ATOMIC64_DECL
  48. #undef ATOMIC64_DECL_ONE
  49. #undef __ATOMIC64_DECL
  50. #undef ATOMIC64_EXPORT
  51. /**
  52. * atomic64_cmpxchg - cmpxchg atomic64 variable
  53. * @p: pointer to type atomic64_t
  54. * @o: expected value
  55. * @n: new value
  56. *
  57. * Atomically sets @v to @n if it was equal to @o and returns
  58. * the old value.
  59. */
  60. static inline long long atomic64_cmpxchg(atomic64_t *v, long long o, long long n)
  61. {
  62. return cmpxchg64(&v->counter, o, n);
  63. }
  64. /**
  65. * atomic64_xchg - xchg atomic64 variable
  66. * @v: pointer to type atomic64_t
  67. * @n: value to assign
  68. *
  69. * Atomically xchgs the value of @v to @n and returns
  70. * the old value.
  71. */
  72. static inline long long atomic64_xchg(atomic64_t *v, long long n)
  73. {
  74. long long o;
  75. unsigned high = (unsigned)(n >> 32);
  76. unsigned low = (unsigned)n;
  77. alternative_atomic64(xchg, "=&A" (o),
  78. "S" (v), "b" (low), "c" (high)
  79. : "memory");
  80. return o;
  81. }
  82. /**
  83. * atomic64_set - set atomic64 variable
  84. * @v: pointer to type atomic64_t
  85. * @n: value to assign
  86. *
  87. * Atomically sets the value of @v to @n.
  88. */
  89. static inline void atomic64_set(atomic64_t *v, long long i)
  90. {
  91. unsigned high = (unsigned)(i >> 32);
  92. unsigned low = (unsigned)i;
  93. alternative_atomic64(set, /* no output */,
  94. "S" (v), "b" (low), "c" (high)
  95. : "eax", "edx", "memory");
  96. }
  97. /**
  98. * atomic64_read - read atomic64 variable
  99. * @v: pointer to type atomic64_t
  100. *
  101. * Atomically reads the value of @v and returns it.
  102. */
  103. static inline long long atomic64_read(const atomic64_t *v)
  104. {
  105. long long r;
  106. alternative_atomic64(read, "=&A" (r), "c" (v) : "memory");
  107. return r;
  108. }
  109. /**
  110. * atomic64_add_return - add and return
  111. * @i: integer value to add
  112. * @v: pointer to type atomic64_t
  113. *
  114. * Atomically adds @i to @v and returns @i + *@v
  115. */
  116. static inline long long atomic64_add_return(long long i, atomic64_t *v)
  117. {
  118. alternative_atomic64(add_return,
  119. ASM_OUTPUT2("+A" (i), "+c" (v)),
  120. ASM_NO_INPUT_CLOBBER("memory"));
  121. return i;
  122. }
  123. /*
  124. * Other variants with different arithmetic operators:
  125. */
  126. static inline long long atomic64_sub_return(long long i, atomic64_t *v)
  127. {
  128. alternative_atomic64(sub_return,
  129. ASM_OUTPUT2("+A" (i), "+c" (v)),
  130. ASM_NO_INPUT_CLOBBER("memory"));
  131. return i;
  132. }
  133. static inline long long atomic64_inc_return(atomic64_t *v)
  134. {
  135. long long a;
  136. alternative_atomic64(inc_return, "=&A" (a),
  137. "S" (v) : "memory", "ecx");
  138. return a;
  139. }
  140. static inline long long atomic64_dec_return(atomic64_t *v)
  141. {
  142. long long a;
  143. alternative_atomic64(dec_return, "=&A" (a),
  144. "S" (v) : "memory", "ecx");
  145. return a;
  146. }
  147. /**
  148. * atomic64_add - add integer to atomic64 variable
  149. * @i: integer value to add
  150. * @v: pointer to type atomic64_t
  151. *
  152. * Atomically adds @i to @v.
  153. */
  154. static inline long long atomic64_add(long long i, atomic64_t *v)
  155. {
  156. __alternative_atomic64(add, add_return,
  157. ASM_OUTPUT2("+A" (i), "+c" (v)),
  158. ASM_NO_INPUT_CLOBBER("memory"));
  159. return i;
  160. }
  161. /**
  162. * atomic64_sub - subtract the atomic64 variable
  163. * @i: integer value to subtract
  164. * @v: pointer to type atomic64_t
  165. *
  166. * Atomically subtracts @i from @v.
  167. */
  168. static inline long long atomic64_sub(long long i, atomic64_t *v)
  169. {
  170. __alternative_atomic64(sub, sub_return,
  171. ASM_OUTPUT2("+A" (i), "+c" (v)),
  172. ASM_NO_INPUT_CLOBBER("memory"));
  173. return i;
  174. }
  175. /**
  176. * atomic64_sub_and_test - subtract value from variable and test result
  177. * @i: integer value to subtract
  178. * @v: pointer to type atomic64_t
  179. *
  180. * Atomically subtracts @i from @v and returns
  181. * true if the result is zero, or false for all
  182. * other cases.
  183. */
  184. static inline int atomic64_sub_and_test(long long i, atomic64_t *v)
  185. {
  186. return atomic64_sub_return(i, v) == 0;
  187. }
  188. /**
  189. * atomic64_inc - increment atomic64 variable
  190. * @v: pointer to type atomic64_t
  191. *
  192. * Atomically increments @v by 1.
  193. */
  194. static inline void atomic64_inc(atomic64_t *v)
  195. {
  196. __alternative_atomic64(inc, inc_return, /* no output */,
  197. "S" (v) : "memory", "eax", "ecx", "edx");
  198. }
  199. /**
  200. * atomic64_dec - decrement atomic64 variable
  201. * @ptr: pointer to type atomic64_t
  202. *
  203. * Atomically decrements @ptr by 1.
  204. */
  205. static inline void atomic64_dec(atomic64_t *v)
  206. {
  207. __alternative_atomic64(dec, dec_return, /* no output */,
  208. "S" (v) : "memory", "eax", "ecx", "edx");
  209. }
  210. /**
  211. * atomic64_dec_and_test - decrement and test
  212. * @v: pointer to type atomic64_t
  213. *
  214. * Atomically decrements @v by 1 and
  215. * returns true if the result is 0, or false for all other
  216. * cases.
  217. */
  218. static inline int atomic64_dec_and_test(atomic64_t *v)
  219. {
  220. return atomic64_dec_return(v) == 0;
  221. }
  222. /**
  223. * atomic64_inc_and_test - increment and test
  224. * @v: pointer to type atomic64_t
  225. *
  226. * Atomically increments @v by 1
  227. * and returns true if the result is zero, or false for all
  228. * other cases.
  229. */
  230. static inline int atomic64_inc_and_test(atomic64_t *v)
  231. {
  232. return atomic64_inc_return(v) == 0;
  233. }
  234. /**
  235. * atomic64_add_negative - add and test if negative
  236. * @i: integer value to add
  237. * @v: pointer to type atomic64_t
  238. *
  239. * Atomically adds @i to @v and returns true
  240. * if the result is negative, or false when
  241. * result is greater than or equal to zero.
  242. */
  243. static inline int atomic64_add_negative(long long i, atomic64_t *v)
  244. {
  245. return atomic64_add_return(i, v) < 0;
  246. }
  247. /**
  248. * atomic64_add_unless - add unless the number is a given value
  249. * @v: pointer of type atomic64_t
  250. * @a: the amount to add to v...
  251. * @u: ...unless v is equal to u.
  252. *
  253. * Atomically adds @a to @v, so long as it was not @u.
  254. * Returns non-zero if the add was done, zero otherwise.
  255. */
  256. static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
  257. {
  258. unsigned low = (unsigned)u;
  259. unsigned high = (unsigned)(u >> 32);
  260. alternative_atomic64(add_unless,
  261. ASM_OUTPUT2("+A" (a), "+c" (low), "+D" (high)),
  262. "S" (v) : "memory");
  263. return (int)a;
  264. }
  265. static inline int atomic64_inc_not_zero(atomic64_t *v)
  266. {
  267. int r;
  268. alternative_atomic64(inc_not_zero, "=&a" (r),
  269. "S" (v) : "ecx", "edx", "memory");
  270. return r;
  271. }
  272. static inline long long atomic64_dec_if_positive(atomic64_t *v)
  273. {
  274. long long r;
  275. alternative_atomic64(dec_if_positive, "=&A" (r),
  276. "S" (v) : "ecx", "memory");
  277. return r;
  278. }
  279. #undef alternative_atomic64
  280. #undef __alternative_atomic64
  281. #endif /* _ASM_X86_ATOMIC64_32_H */