atomic_32.h 9.6 KB

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