atomic_64.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #ifndef ASM_X86__ATOMIC_64_H
  2. #define ASM_X86__ATOMIC_64_H
  3. #include <asm/alternative.h>
  4. #include <asm/cmpxchg.h>
  5. /* atomic_t should be 32 bit signed type */
  6. /*
  7. * Atomic operations that C can't guarantee us. Useful for
  8. * resource counting etc..
  9. */
  10. /*
  11. * Make sure gcc doesn't try to be clever and move things around
  12. * on us. We need to use _exactly_ the address the user gave us,
  13. * not some alias that contains the same information.
  14. */
  15. typedef struct {
  16. int counter;
  17. } atomic_t;
  18. #define ATOMIC_INIT(i) { (i) }
  19. /**
  20. * atomic_read - read atomic variable
  21. * @v: pointer of type atomic_t
  22. *
  23. * Atomically reads the value of @v.
  24. */
  25. #define atomic_read(v) ((v)->counter)
  26. /**
  27. * atomic_set - set atomic variable
  28. * @v: pointer of type atomic_t
  29. * @i: required value
  30. *
  31. * Atomically sets the value of @v to @i.
  32. */
  33. #define atomic_set(v, i) (((v)->counter) = (i))
  34. /**
  35. * atomic_add - add integer to atomic variable
  36. * @i: integer value to add
  37. * @v: pointer of type atomic_t
  38. *
  39. * Atomically adds @i to @v.
  40. */
  41. static inline void atomic_add(int i, atomic_t *v)
  42. {
  43. asm volatile(LOCK_PREFIX "addl %1,%0"
  44. : "=m" (v->counter)
  45. : "ir" (i), "m" (v->counter));
  46. }
  47. /**
  48. * atomic_sub - subtract the atomic variable
  49. * @i: integer value to subtract
  50. * @v: pointer of type atomic_t
  51. *
  52. * Atomically subtracts @i from @v.
  53. */
  54. static inline void atomic_sub(int i, atomic_t *v)
  55. {
  56. asm volatile(LOCK_PREFIX "subl %1,%0"
  57. : "=m" (v->counter)
  58. : "ir" (i), "m" (v->counter));
  59. }
  60. /**
  61. * atomic_sub_and_test - subtract value from variable and test result
  62. * @i: integer value to subtract
  63. * @v: pointer of type atomic_t
  64. *
  65. * Atomically subtracts @i from @v and returns
  66. * true if the result is zero, or false for all
  67. * other cases.
  68. */
  69. static inline int atomic_sub_and_test(int i, atomic_t *v)
  70. {
  71. unsigned char c;
  72. asm volatile(LOCK_PREFIX "subl %2,%0; sete %1"
  73. : "=m" (v->counter), "=qm" (c)
  74. : "ir" (i), "m" (v->counter) : "memory");
  75. return c;
  76. }
  77. /**
  78. * atomic_inc - increment atomic variable
  79. * @v: pointer of type atomic_t
  80. *
  81. * Atomically increments @v by 1.
  82. */
  83. static inline void atomic_inc(atomic_t *v)
  84. {
  85. asm volatile(LOCK_PREFIX "incl %0"
  86. : "=m" (v->counter)
  87. : "m" (v->counter));
  88. }
  89. /**
  90. * atomic_dec - decrement atomic variable
  91. * @v: pointer of type atomic_t
  92. *
  93. * Atomically decrements @v by 1.
  94. */
  95. static inline void atomic_dec(atomic_t *v)
  96. {
  97. asm volatile(LOCK_PREFIX "decl %0"
  98. : "=m" (v->counter)
  99. : "m" (v->counter));
  100. }
  101. /**
  102. * atomic_dec_and_test - decrement and test
  103. * @v: pointer of type atomic_t
  104. *
  105. * Atomically decrements @v by 1 and
  106. * returns true if the result is 0, or false for all other
  107. * cases.
  108. */
  109. static inline int atomic_dec_and_test(atomic_t *v)
  110. {
  111. unsigned char c;
  112. asm volatile(LOCK_PREFIX "decl %0; sete %1"
  113. : "=m" (v->counter), "=qm" (c)
  114. : "m" (v->counter) : "memory");
  115. return c != 0;
  116. }
  117. /**
  118. * atomic_inc_and_test - increment and test
  119. * @v: pointer of type atomic_t
  120. *
  121. * Atomically increments @v by 1
  122. * and returns true if the result is zero, or false for all
  123. * other cases.
  124. */
  125. static inline int atomic_inc_and_test(atomic_t *v)
  126. {
  127. unsigned char c;
  128. asm volatile(LOCK_PREFIX "incl %0; sete %1"
  129. : "=m" (v->counter), "=qm" (c)
  130. : "m" (v->counter) : "memory");
  131. return c != 0;
  132. }
  133. /**
  134. * atomic_add_negative - add and test if negative
  135. * @i: integer value to add
  136. * @v: pointer of type atomic_t
  137. *
  138. * Atomically adds @i to @v and returns true
  139. * if the result is negative, or false when
  140. * result is greater than or equal to zero.
  141. */
  142. static inline int atomic_add_negative(int i, atomic_t *v)
  143. {
  144. unsigned char c;
  145. asm volatile(LOCK_PREFIX "addl %2,%0; sets %1"
  146. : "=m" (v->counter), "=qm" (c)
  147. : "ir" (i), "m" (v->counter) : "memory");
  148. return c;
  149. }
  150. /**
  151. * atomic_add_return - add and return
  152. * @i: integer value to add
  153. * @v: pointer of type atomic_t
  154. *
  155. * Atomically adds @i to @v and returns @i + @v
  156. */
  157. static inline int atomic_add_return(int i, atomic_t *v)
  158. {
  159. int __i = i;
  160. asm volatile(LOCK_PREFIX "xaddl %0, %1"
  161. : "+r" (i), "+m" (v->counter)
  162. : : "memory");
  163. return i + __i;
  164. }
  165. static inline int atomic_sub_return(int i, atomic_t *v)
  166. {
  167. return atomic_add_return(-i, v);
  168. }
  169. #define atomic_inc_return(v) (atomic_add_return(1, v))
  170. #define atomic_dec_return(v) (atomic_sub_return(1, v))
  171. /* An 64bit atomic type */
  172. typedef struct {
  173. long counter;
  174. } atomic64_t;
  175. #define ATOMIC64_INIT(i) { (i) }
  176. /**
  177. * atomic64_read - read atomic64 variable
  178. * @v: pointer of type atomic64_t
  179. *
  180. * Atomically reads the value of @v.
  181. * Doesn't imply a read memory barrier.
  182. */
  183. #define atomic64_read(v) ((v)->counter)
  184. /**
  185. * atomic64_set - set atomic64 variable
  186. * @v: pointer to type atomic64_t
  187. * @i: required value
  188. *
  189. * Atomically sets the value of @v to @i.
  190. */
  191. #define atomic64_set(v, i) (((v)->counter) = (i))
  192. /**
  193. * atomic64_add - add integer to atomic64 variable
  194. * @i: integer value to add
  195. * @v: pointer to type atomic64_t
  196. *
  197. * Atomically adds @i to @v.
  198. */
  199. static inline void atomic64_add(long i, atomic64_t *v)
  200. {
  201. asm volatile(LOCK_PREFIX "addq %1,%0"
  202. : "=m" (v->counter)
  203. : "er" (i), "m" (v->counter));
  204. }
  205. /**
  206. * atomic64_sub - subtract the atomic64 variable
  207. * @i: integer value to subtract
  208. * @v: pointer to type atomic64_t
  209. *
  210. * Atomically subtracts @i from @v.
  211. */
  212. static inline void atomic64_sub(long i, atomic64_t *v)
  213. {
  214. asm volatile(LOCK_PREFIX "subq %1,%0"
  215. : "=m" (v->counter)
  216. : "er" (i), "m" (v->counter));
  217. }
  218. /**
  219. * atomic64_sub_and_test - subtract value from variable and test result
  220. * @i: integer value to subtract
  221. * @v: pointer to type atomic64_t
  222. *
  223. * Atomically subtracts @i from @v and returns
  224. * true if the result is zero, or false for all
  225. * other cases.
  226. */
  227. static inline int atomic64_sub_and_test(long i, atomic64_t *v)
  228. {
  229. unsigned char c;
  230. asm volatile(LOCK_PREFIX "subq %2,%0; sete %1"
  231. : "=m" (v->counter), "=qm" (c)
  232. : "er" (i), "m" (v->counter) : "memory");
  233. return c;
  234. }
  235. /**
  236. * atomic64_inc - increment atomic64 variable
  237. * @v: pointer to type atomic64_t
  238. *
  239. * Atomically increments @v by 1.
  240. */
  241. static inline void atomic64_inc(atomic64_t *v)
  242. {
  243. asm volatile(LOCK_PREFIX "incq %0"
  244. : "=m" (v->counter)
  245. : "m" (v->counter));
  246. }
  247. /**
  248. * atomic64_dec - decrement atomic64 variable
  249. * @v: pointer to type atomic64_t
  250. *
  251. * Atomically decrements @v by 1.
  252. */
  253. static inline void atomic64_dec(atomic64_t *v)
  254. {
  255. asm volatile(LOCK_PREFIX "decq %0"
  256. : "=m" (v->counter)
  257. : "m" (v->counter));
  258. }
  259. /**
  260. * atomic64_dec_and_test - decrement and test
  261. * @v: pointer to type atomic64_t
  262. *
  263. * Atomically decrements @v by 1 and
  264. * returns true if the result is 0, or false for all other
  265. * cases.
  266. */
  267. static inline int atomic64_dec_and_test(atomic64_t *v)
  268. {
  269. unsigned char c;
  270. asm volatile(LOCK_PREFIX "decq %0; sete %1"
  271. : "=m" (v->counter), "=qm" (c)
  272. : "m" (v->counter) : "memory");
  273. return c != 0;
  274. }
  275. /**
  276. * atomic64_inc_and_test - increment and test
  277. * @v: pointer to type atomic64_t
  278. *
  279. * Atomically increments @v by 1
  280. * and returns true if the result is zero, or false for all
  281. * other cases.
  282. */
  283. static inline int atomic64_inc_and_test(atomic64_t *v)
  284. {
  285. unsigned char c;
  286. asm volatile(LOCK_PREFIX "incq %0; sete %1"
  287. : "=m" (v->counter), "=qm" (c)
  288. : "m" (v->counter) : "memory");
  289. return c != 0;
  290. }
  291. /**
  292. * atomic64_add_negative - add and test if negative
  293. * @i: integer value to add
  294. * @v: pointer to type atomic64_t
  295. *
  296. * Atomically adds @i to @v and returns true
  297. * if the result is negative, or false when
  298. * result is greater than or equal to zero.
  299. */
  300. static inline int atomic64_add_negative(long i, atomic64_t *v)
  301. {
  302. unsigned char c;
  303. asm volatile(LOCK_PREFIX "addq %2,%0; sets %1"
  304. : "=m" (v->counter), "=qm" (c)
  305. : "er" (i), "m" (v->counter) : "memory");
  306. return c;
  307. }
  308. /**
  309. * atomic64_add_return - add and return
  310. * @i: integer value to add
  311. * @v: pointer to type atomic64_t
  312. *
  313. * Atomically adds @i to @v and returns @i + @v
  314. */
  315. static inline long atomic64_add_return(long i, atomic64_t *v)
  316. {
  317. long __i = i;
  318. asm volatile(LOCK_PREFIX "xaddq %0, %1;"
  319. : "+r" (i), "+m" (v->counter)
  320. : : "memory");
  321. return i + __i;
  322. }
  323. static inline long atomic64_sub_return(long i, atomic64_t *v)
  324. {
  325. return atomic64_add_return(-i, v);
  326. }
  327. #define atomic64_inc_return(v) (atomic64_add_return(1, (v)))
  328. #define atomic64_dec_return(v) (atomic64_sub_return(1, (v)))
  329. #define atomic64_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
  330. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  331. #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
  332. #define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
  333. /**
  334. * atomic_add_unless - add unless the number is a given value
  335. * @v: pointer of type atomic_t
  336. * @a: the amount to add to v...
  337. * @u: ...unless v is equal to u.
  338. *
  339. * Atomically adds @a to @v, so long as it was not @u.
  340. * Returns non-zero if @v was not @u, and zero otherwise.
  341. */
  342. static inline int atomic_add_unless(atomic_t *v, int a, int u)
  343. {
  344. int c, old;
  345. c = atomic_read(v);
  346. for (;;) {
  347. if (unlikely(c == (u)))
  348. break;
  349. old = atomic_cmpxchg((v), c, c + (a));
  350. if (likely(old == c))
  351. break;
  352. c = old;
  353. }
  354. return c != (u);
  355. }
  356. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  357. /**
  358. * atomic64_add_unless - add unless the number is a given value
  359. * @v: pointer of type atomic64_t
  360. * @a: the amount to add to v...
  361. * @u: ...unless v is equal to u.
  362. *
  363. * Atomically adds @a to @v, so long as it was not @u.
  364. * Returns non-zero if @v was not @u, and zero otherwise.
  365. */
  366. static inline int atomic64_add_unless(atomic64_t *v, long a, long u)
  367. {
  368. long c, old;
  369. c = atomic64_read(v);
  370. for (;;) {
  371. if (unlikely(c == (u)))
  372. break;
  373. old = atomic64_cmpxchg((v), c, c + (a));
  374. if (likely(old == c))
  375. break;
  376. c = old;
  377. }
  378. return c != (u);
  379. }
  380. /**
  381. * atomic_inc_short - increment of a short integer
  382. * @v: pointer to type int
  383. *
  384. * Atomically adds 1 to @v
  385. * Returns the new value of @u
  386. */
  387. static inline short int atomic_inc_short(short int *v)
  388. {
  389. asm(LOCK_PREFIX "addw $1, %0" : "+m" (*v));
  390. return *v;
  391. }
  392. /**
  393. * atomic_or_long - OR of two long integers
  394. * @v1: pointer to type unsigned long
  395. * @v2: pointer to type unsigned long
  396. *
  397. * Atomically ORs @v1 and @v2
  398. * Returns the result of the OR
  399. */
  400. static inline void atomic_or_long(unsigned long *v1, unsigned long v2)
  401. {
  402. asm(LOCK_PREFIX "orq %1, %0" : "+m" (*v1) : "r" (v2));
  403. }
  404. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  405. /* These are x86-specific, used by some header files */
  406. #define atomic_clear_mask(mask, addr) \
  407. asm volatile(LOCK_PREFIX "andl %0,%1" \
  408. : : "r" (~(mask)), "m" (*(addr)) : "memory")
  409. #define atomic_set_mask(mask, addr) \
  410. asm volatile(LOCK_PREFIX "orl %0,%1" \
  411. : : "r" ((unsigned)(mask)), "m" (*(addr)) \
  412. : "memory")
  413. /* Atomic operations are already serializing on x86 */
  414. #define smp_mb__before_atomic_dec() barrier()
  415. #define smp_mb__after_atomic_dec() barrier()
  416. #define smp_mb__before_atomic_inc() barrier()
  417. #define smp_mb__after_atomic_inc() barrier()
  418. #include <asm-generic/atomic.h>
  419. #endif /* ASM_X86__ATOMIC_64_H */