atomic_32.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. * @ptr: 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. *
  259. * Atomically xchgs the value of @ptr to @new_val and returns
  260. * the old value.
  261. */
  262. static inline unsigned long long
  263. atomic64_xchg(atomic64_t *ptr, unsigned long long new_val)
  264. {
  265. unsigned long long old_val;
  266. do {
  267. old_val = atomic_read(ptr);
  268. } while (atomic64_cmpxchg(ptr, old_val, new_val) != old_val);
  269. return old_val;
  270. }
  271. /**
  272. * atomic64_set - set atomic64 variable
  273. * @ptr: pointer to type atomic64_t
  274. * @new_val: value to assign
  275. *
  276. * Atomically sets the value of @ptr to @new_val.
  277. */
  278. static inline void atomic64_set(atomic64_t *ptr, unsigned long long new_val)
  279. {
  280. atomic64_xchg(ptr, new_val);
  281. }
  282. /**
  283. * atomic64_read - read atomic64 variable
  284. * @ptr: pointer to type atomic64_t
  285. *
  286. * Atomically reads the value of @ptr and returns it.
  287. */
  288. static inline unsigned long long atomic64_read(atomic64_t *ptr)
  289. {
  290. unsigned long long curr_val;
  291. do {
  292. curr_val = __atomic64_read(ptr);
  293. } while (atomic64_cmpxchg(ptr, curr_val, curr_val) != curr_val);
  294. return curr_val;
  295. }
  296. /**
  297. * atomic64_add_return - add and return
  298. * @delta: integer value to add
  299. * @ptr: pointer to type atomic64_t
  300. *
  301. * Atomically adds @delta to @ptr and returns @delta + *@ptr
  302. */
  303. static inline unsigned long long
  304. atomic64_add_return(unsigned long long delta, atomic64_t *ptr)
  305. {
  306. unsigned long long old_val, new_val;
  307. do {
  308. old_val = atomic_read(ptr);
  309. new_val = old_val + delta;
  310. } while (atomic64_cmpxchg(ptr, old_val, new_val) != old_val);
  311. return new_val;
  312. }
  313. static inline long atomic64_sub_return(unsigned long long delta, atomic64_t *ptr)
  314. {
  315. return atomic64_add_return(-delta, ptr);
  316. }
  317. static inline long atomic64_inc_return(atomic64_t *ptr)
  318. {
  319. return atomic64_add_return(1, ptr);
  320. }
  321. static inline long atomic64_dec_return(atomic64_t *ptr)
  322. {
  323. return atomic64_sub_return(1, ptr);
  324. }
  325. /**
  326. * atomic64_add - add integer to atomic64 variable
  327. * @delta: integer value to add
  328. * @ptr: pointer to type atomic64_t
  329. *
  330. * Atomically adds @delta to @ptr.
  331. */
  332. static inline void atomic64_add(unsigned long long delta, atomic64_t *ptr)
  333. {
  334. atomic64_add_return(delta, ptr);
  335. }
  336. /**
  337. * atomic64_sub - subtract the atomic64 variable
  338. * @delta: integer value to subtract
  339. * @ptr: pointer to type atomic64_t
  340. *
  341. * Atomically subtracts @delta from @ptr.
  342. */
  343. static inline void atomic64_sub(unsigned long long delta, atomic64_t *ptr)
  344. {
  345. atomic64_add(-delta, ptr);
  346. }
  347. /**
  348. * atomic64_sub_and_test - subtract value from variable and test result
  349. * @delta: integer value to subtract
  350. * @ptr: pointer to type atomic64_t
  351. *
  352. * Atomically subtracts @delta from @ptr and returns
  353. * true if the result is zero, or false for all
  354. * other cases.
  355. */
  356. static inline int
  357. atomic64_sub_and_test(unsigned long long delta, atomic64_t *ptr)
  358. {
  359. unsigned long long old_val = atomic64_sub_return(delta, ptr);
  360. return old_val == 0;
  361. }
  362. /**
  363. * atomic64_inc - increment atomic64 variable
  364. * @ptr: pointer to type atomic64_t
  365. *
  366. * Atomically increments @ptr by 1.
  367. */
  368. static inline void atomic64_inc(atomic64_t *ptr)
  369. {
  370. atomic64_add(1, ptr);
  371. }
  372. /**
  373. * atomic64_dec - decrement atomic64 variable
  374. * @ptr: pointer to type atomic64_t
  375. *
  376. * Atomically decrements @ptr by 1.
  377. */
  378. static inline void atomic64_dec(atomic64_t *ptr)
  379. {
  380. atomic64_sub(1, ptr);
  381. }
  382. /**
  383. * atomic64_dec_and_test - decrement and test
  384. * @ptr: pointer to type atomic64_t
  385. *
  386. * Atomically decrements @ptr by 1 and
  387. * returns true if the result is 0, or false for all other
  388. * cases.
  389. */
  390. static inline int atomic64_dec_and_test(atomic64_t *ptr)
  391. {
  392. return atomic64_sub_and_test(1, ptr);
  393. }
  394. /**
  395. * atomic64_inc_and_test - increment and test
  396. * @ptr: pointer to type atomic64_t
  397. *
  398. * Atomically increments @ptr by 1
  399. * and returns true if the result is zero, or false for all
  400. * other cases.
  401. */
  402. static inline int atomic64_inc_and_test(atomic64_t *ptr)
  403. {
  404. return atomic64_sub_and_test(-1, ptr);
  405. }
  406. /**
  407. * atomic64_add_negative - add and test if negative
  408. * @delta: integer value to add
  409. * @ptr: pointer to type atomic64_t
  410. *
  411. * Atomically adds @delta to @ptr and returns true
  412. * if the result is negative, or false when
  413. * result is greater than or equal to zero.
  414. */
  415. static inline int
  416. atomic64_add_negative(unsigned long long delta, atomic64_t *ptr)
  417. {
  418. long long old_val = atomic64_add_return(delta, ptr);
  419. return old_val < 0;
  420. }
  421. #include <asm-generic/atomic-long.h>
  422. #endif /* _ASM_X86_ATOMIC_32_H */