atomic_32.h 11 KB

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