atomic_32.h 11 KB

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