atomic.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Atomic operations that C can't guarantee us. Useful for
  3. * resource counting etc..
  4. *
  5. * But use these as seldom as possible since they are much more slower
  6. * than regular operations.
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. *
  12. * Copyright (C) 1996, 97, 99, 2000, 03, 04, 06 by Ralf Baechle
  13. */
  14. #ifndef _ASM_ATOMIC_H
  15. #define _ASM_ATOMIC_H
  16. #include <linux/irqflags.h>
  17. #include <linux/types.h>
  18. #include <asm/barrier.h>
  19. #include <asm/cpu-features.h>
  20. #include <asm/cmpxchg.h>
  21. #include <asm/war.h>
  22. #define ATOMIC_INIT(i) { (i) }
  23. /*
  24. * atomic_read - read atomic variable
  25. * @v: pointer of type atomic_t
  26. *
  27. * Atomically reads the value of @v.
  28. */
  29. #define atomic_read(v) ACCESS_ONCE((v)->counter)
  30. /*
  31. * atomic_set - set atomic variable
  32. * @v: pointer of type atomic_t
  33. * @i: required value
  34. *
  35. * Atomically sets the value of @v to @i.
  36. */
  37. #define atomic_set(v, i) ((v)->counter = (i))
  38. #define ATOMIC_OP(op, c_op, asm_op) \
  39. static __inline__ void atomic_##op(int i, atomic_t * v) \
  40. { \
  41. if (kernel_uses_llsc && R10000_LLSC_WAR) { \
  42. int temp; \
  43. \
  44. __asm__ __volatile__( \
  45. " .set arch=r4000 \n" \
  46. "1: ll %0, %1 # atomic_" #op " \n" \
  47. " " #asm_op " %0, %2 \n" \
  48. " sc %0, %1 \n" \
  49. " beqzl %0, 1b \n" \
  50. " .set mips0 \n" \
  51. : "=&r" (temp), "+m" (v->counter) \
  52. : "Ir" (i)); \
  53. } else if (kernel_uses_llsc) { \
  54. int temp; \
  55. \
  56. do { \
  57. __asm__ __volatile__( \
  58. " .set arch=r4000 \n" \
  59. " ll %0, %1 # atomic_" #op "\n" \
  60. " " #asm_op " %0, %2 \n" \
  61. " sc %0, %1 \n" \
  62. " .set mips0 \n" \
  63. : "=&r" (temp), "+m" (v->counter) \
  64. : "Ir" (i)); \
  65. } while (unlikely(!temp)); \
  66. } else { \
  67. unsigned long flags; \
  68. \
  69. raw_local_irq_save(flags); \
  70. v->counter c_op i; \
  71. raw_local_irq_restore(flags); \
  72. } \
  73. } \
  74. #define ATOMIC_OP_RETURN(op, c_op, asm_op) \
  75. static __inline__ int atomic_##op##_return(int i, atomic_t * v) \
  76. { \
  77. int result; \
  78. \
  79. smp_mb__before_llsc(); \
  80. \
  81. if (kernel_uses_llsc && R10000_LLSC_WAR) { \
  82. int temp; \
  83. \
  84. __asm__ __volatile__( \
  85. " .set arch=r4000 \n" \
  86. "1: ll %1, %2 # atomic_" #op "_return \n" \
  87. " " #asm_op " %0, %1, %3 \n" \
  88. " sc %0, %2 \n" \
  89. " beqzl %0, 1b \n" \
  90. " " #asm_op " %0, %1, %3 \n" \
  91. " .set mips0 \n" \
  92. : "=&r" (result), "=&r" (temp), "+m" (v->counter) \
  93. : "Ir" (i)); \
  94. } else if (kernel_uses_llsc) { \
  95. int temp; \
  96. \
  97. do { \
  98. __asm__ __volatile__( \
  99. " .set arch=r4000 \n" \
  100. " ll %1, %2 # atomic_" #op "_return \n" \
  101. " " #asm_op " %0, %1, %3 \n" \
  102. " sc %0, %2 \n" \
  103. " .set mips0 \n" \
  104. : "=&r" (result), "=&r" (temp), "+m" (v->counter) \
  105. : "Ir" (i)); \
  106. } while (unlikely(!result)); \
  107. \
  108. result = temp; result c_op i; \
  109. } else { \
  110. unsigned long flags; \
  111. \
  112. raw_local_irq_save(flags); \
  113. result = v->counter; \
  114. result c_op i; \
  115. v->counter = result; \
  116. raw_local_irq_restore(flags); \
  117. } \
  118. \
  119. smp_llsc_mb(); \
  120. \
  121. return result; \
  122. }
  123. #define ATOMIC_OPS(op, c_op, asm_op) \
  124. ATOMIC_OP(op, c_op, asm_op) \
  125. ATOMIC_OP_RETURN(op, c_op, asm_op)
  126. ATOMIC_OPS(add, +=, addu)
  127. ATOMIC_OPS(sub, -=, subu)
  128. #undef ATOMIC_OPS
  129. #undef ATOMIC_OP_RETURN
  130. #undef ATOMIC_OP
  131. /*
  132. * atomic_sub_if_positive - conditionally subtract integer from atomic variable
  133. * @i: integer value to subtract
  134. * @v: pointer of type atomic_t
  135. *
  136. * Atomically test @v and subtract @i if @v is greater or equal than @i.
  137. * The function returns the old value of @v minus @i.
  138. */
  139. static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
  140. {
  141. int result;
  142. smp_mb__before_llsc();
  143. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  144. int temp;
  145. __asm__ __volatile__(
  146. " .set arch=r4000 \n"
  147. "1: ll %1, %2 # atomic_sub_if_positive\n"
  148. " subu %0, %1, %3 \n"
  149. " bltz %0, 1f \n"
  150. " sc %0, %2 \n"
  151. " .set noreorder \n"
  152. " beqzl %0, 1b \n"
  153. " subu %0, %1, %3 \n"
  154. " .set reorder \n"
  155. "1: \n"
  156. " .set mips0 \n"
  157. : "=&r" (result), "=&r" (temp), "+m" (v->counter)
  158. : "Ir" (i), "m" (v->counter)
  159. : "memory");
  160. } else if (kernel_uses_llsc) {
  161. int temp;
  162. __asm__ __volatile__(
  163. " .set arch=r4000 \n"
  164. "1: ll %1, %2 # atomic_sub_if_positive\n"
  165. " subu %0, %1, %3 \n"
  166. " bltz %0, 1f \n"
  167. " sc %0, %2 \n"
  168. " .set noreorder \n"
  169. " beqz %0, 1b \n"
  170. " subu %0, %1, %3 \n"
  171. " .set reorder \n"
  172. "1: \n"
  173. " .set mips0 \n"
  174. : "=&r" (result), "=&r" (temp), "+m" (v->counter)
  175. : "Ir" (i));
  176. } else {
  177. unsigned long flags;
  178. raw_local_irq_save(flags);
  179. result = v->counter;
  180. result -= i;
  181. if (result >= 0)
  182. v->counter = result;
  183. raw_local_irq_restore(flags);
  184. }
  185. smp_llsc_mb();
  186. return result;
  187. }
  188. #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  189. #define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
  190. /**
  191. * __atomic_add_unless - add unless the number is a given value
  192. * @v: pointer of type atomic_t
  193. * @a: the amount to add to v...
  194. * @u: ...unless v is equal to u.
  195. *
  196. * Atomically adds @a to @v, so long as it was not @u.
  197. * Returns the old value of @v.
  198. */
  199. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  200. {
  201. int c, old;
  202. c = atomic_read(v);
  203. for (;;) {
  204. if (unlikely(c == (u)))
  205. break;
  206. old = atomic_cmpxchg((v), c, c + (a));
  207. if (likely(old == c))
  208. break;
  209. c = old;
  210. }
  211. return c;
  212. }
  213. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  214. #define atomic_inc_return(v) atomic_add_return(1, (v))
  215. /*
  216. * atomic_sub_and_test - subtract value from variable and test result
  217. * @i: integer value to subtract
  218. * @v: pointer of type atomic_t
  219. *
  220. * Atomically subtracts @i from @v and returns
  221. * true if the result is zero, or false for all
  222. * other cases.
  223. */
  224. #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
  225. /*
  226. * atomic_inc_and_test - increment and test
  227. * @v: pointer of type atomic_t
  228. *
  229. * Atomically increments @v by 1
  230. * and returns true if the result is zero, or false for all
  231. * other cases.
  232. */
  233. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  234. /*
  235. * atomic_dec_and_test - decrement by 1 and test
  236. * @v: pointer of type atomic_t
  237. *
  238. * Atomically decrements @v by 1 and
  239. * returns true if the result is 0, or false for all other
  240. * cases.
  241. */
  242. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  243. /*
  244. * atomic_dec_if_positive - decrement by 1 if old value positive
  245. * @v: pointer of type atomic_t
  246. */
  247. #define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v)
  248. /*
  249. * atomic_inc - increment atomic variable
  250. * @v: pointer of type atomic_t
  251. *
  252. * Atomically increments @v by 1.
  253. */
  254. #define atomic_inc(v) atomic_add(1, (v))
  255. /*
  256. * atomic_dec - decrement and test
  257. * @v: pointer of type atomic_t
  258. *
  259. * Atomically decrements @v by 1.
  260. */
  261. #define atomic_dec(v) atomic_sub(1, (v))
  262. /*
  263. * atomic_add_negative - add and test if negative
  264. * @v: pointer of type atomic_t
  265. * @i: integer value to add
  266. *
  267. * Atomically adds @i to @v and returns true
  268. * if the result is negative, or false when
  269. * result is greater than or equal to zero.
  270. */
  271. #define atomic_add_negative(i, v) (atomic_add_return(i, (v)) < 0)
  272. #ifdef CONFIG_64BIT
  273. #define ATOMIC64_INIT(i) { (i) }
  274. /*
  275. * atomic64_read - read atomic variable
  276. * @v: pointer of type atomic64_t
  277. *
  278. */
  279. #define atomic64_read(v) ACCESS_ONCE((v)->counter)
  280. /*
  281. * atomic64_set - set atomic variable
  282. * @v: pointer of type atomic64_t
  283. * @i: required value
  284. */
  285. #define atomic64_set(v, i) ((v)->counter = (i))
  286. #define ATOMIC64_OP(op, c_op, asm_op) \
  287. static __inline__ void atomic64_##op(long i, atomic64_t * v) \
  288. { \
  289. if (kernel_uses_llsc && R10000_LLSC_WAR) { \
  290. long temp; \
  291. \
  292. __asm__ __volatile__( \
  293. " .set arch=r4000 \n" \
  294. "1: lld %0, %1 # atomic64_" #op " \n" \
  295. " " #asm_op " %0, %2 \n" \
  296. " scd %0, %1 \n" \
  297. " beqzl %0, 1b \n" \
  298. " .set mips0 \n" \
  299. : "=&r" (temp), "+m" (v->counter) \
  300. : "Ir" (i)); \
  301. } else if (kernel_uses_llsc) { \
  302. long temp; \
  303. \
  304. do { \
  305. __asm__ __volatile__( \
  306. " .set arch=r4000 \n" \
  307. " lld %0, %1 # atomic64_" #op "\n" \
  308. " " #asm_op " %0, %2 \n" \
  309. " scd %0, %1 \n" \
  310. " .set mips0 \n" \
  311. : "=&r" (temp), "+m" (v->counter) \
  312. : "Ir" (i)); \
  313. } while (unlikely(!temp)); \
  314. } else { \
  315. unsigned long flags; \
  316. \
  317. raw_local_irq_save(flags); \
  318. v->counter c_op i; \
  319. raw_local_irq_restore(flags); \
  320. } \
  321. } \
  322. #define ATOMIC64_OP_RETURN(op, c_op, asm_op) \
  323. static __inline__ long atomic64_##op##_return(long i, atomic64_t * v) \
  324. { \
  325. long result; \
  326. \
  327. smp_mb__before_llsc(); \
  328. \
  329. if (kernel_uses_llsc && R10000_LLSC_WAR) { \
  330. long temp; \
  331. \
  332. __asm__ __volatile__( \
  333. " .set arch=r4000 \n" \
  334. "1: lld %1, %2 # atomic64_" #op "_return\n" \
  335. " " #asm_op " %0, %1, %3 \n" \
  336. " scd %0, %2 \n" \
  337. " beqzl %0, 1b \n" \
  338. " " #asm_op " %0, %1, %3 \n" \
  339. " .set mips0 \n" \
  340. : "=&r" (result), "=&r" (temp), "+m" (v->counter) \
  341. : "Ir" (i)); \
  342. } else if (kernel_uses_llsc) { \
  343. long temp; \
  344. \
  345. do { \
  346. __asm__ __volatile__( \
  347. " .set arch=r4000 \n" \
  348. " lld %1, %2 # atomic64_" #op "_return\n" \
  349. " " #asm_op " %0, %1, %3 \n" \
  350. " scd %0, %2 \n" \
  351. " .set mips0 \n" \
  352. : "=&r" (result), "=&r" (temp), "=m" (v->counter) \
  353. : "Ir" (i), "m" (v->counter) \
  354. : "memory"); \
  355. } while (unlikely(!result)); \
  356. \
  357. result = temp; result c_op i; \
  358. } else { \
  359. unsigned long flags; \
  360. \
  361. raw_local_irq_save(flags); \
  362. result = v->counter; \
  363. result c_op i; \
  364. v->counter = result; \
  365. raw_local_irq_restore(flags); \
  366. } \
  367. \
  368. smp_llsc_mb(); \
  369. \
  370. return result; \
  371. }
  372. #define ATOMIC64_OPS(op, c_op, asm_op) \
  373. ATOMIC64_OP(op, c_op, asm_op) \
  374. ATOMIC64_OP_RETURN(op, c_op, asm_op)
  375. ATOMIC64_OPS(add, +=, daddu)
  376. ATOMIC64_OPS(sub, -=, dsubu)
  377. #undef ATOMIC64_OPS
  378. #undef ATOMIC64_OP_RETURN
  379. #undef ATOMIC64_OP
  380. /*
  381. * atomic64_sub_if_positive - conditionally subtract integer from atomic variable
  382. * @i: integer value to subtract
  383. * @v: pointer of type atomic64_t
  384. *
  385. * Atomically test @v and subtract @i if @v is greater or equal than @i.
  386. * The function returns the old value of @v minus @i.
  387. */
  388. static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
  389. {
  390. long result;
  391. smp_mb__before_llsc();
  392. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  393. long temp;
  394. __asm__ __volatile__(
  395. " .set arch=r4000 \n"
  396. "1: lld %1, %2 # atomic64_sub_if_positive\n"
  397. " dsubu %0, %1, %3 \n"
  398. " bltz %0, 1f \n"
  399. " scd %0, %2 \n"
  400. " .set noreorder \n"
  401. " beqzl %0, 1b \n"
  402. " dsubu %0, %1, %3 \n"
  403. " .set reorder \n"
  404. "1: \n"
  405. " .set mips0 \n"
  406. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  407. : "Ir" (i), "m" (v->counter)
  408. : "memory");
  409. } else if (kernel_uses_llsc) {
  410. long temp;
  411. __asm__ __volatile__(
  412. " .set arch=r4000 \n"
  413. "1: lld %1, %2 # atomic64_sub_if_positive\n"
  414. " dsubu %0, %1, %3 \n"
  415. " bltz %0, 1f \n"
  416. " scd %0, %2 \n"
  417. " .set noreorder \n"
  418. " beqz %0, 1b \n"
  419. " dsubu %0, %1, %3 \n"
  420. " .set reorder \n"
  421. "1: \n"
  422. " .set mips0 \n"
  423. : "=&r" (result), "=&r" (temp), "+m" (v->counter)
  424. : "Ir" (i));
  425. } else {
  426. unsigned long flags;
  427. raw_local_irq_save(flags);
  428. result = v->counter;
  429. result -= i;
  430. if (result >= 0)
  431. v->counter = result;
  432. raw_local_irq_restore(flags);
  433. }
  434. smp_llsc_mb();
  435. return result;
  436. }
  437. #define atomic64_cmpxchg(v, o, n) \
  438. ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
  439. #define atomic64_xchg(v, new) (xchg(&((v)->counter), (new)))
  440. /**
  441. * atomic64_add_unless - add unless the number is a given value
  442. * @v: pointer of type atomic64_t
  443. * @a: the amount to add to v...
  444. * @u: ...unless v is equal to u.
  445. *
  446. * Atomically adds @a to @v, so long as it was not @u.
  447. * Returns the old value of @v.
  448. */
  449. static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
  450. {
  451. long c, old;
  452. c = atomic64_read(v);
  453. for (;;) {
  454. if (unlikely(c == (u)))
  455. break;
  456. old = atomic64_cmpxchg((v), c, c + (a));
  457. if (likely(old == c))
  458. break;
  459. c = old;
  460. }
  461. return c != (u);
  462. }
  463. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  464. #define atomic64_dec_return(v) atomic64_sub_return(1, (v))
  465. #define atomic64_inc_return(v) atomic64_add_return(1, (v))
  466. /*
  467. * atomic64_sub_and_test - subtract value from variable and test result
  468. * @i: integer value to subtract
  469. * @v: pointer of type atomic64_t
  470. *
  471. * Atomically subtracts @i from @v and returns
  472. * true if the result is zero, or false for all
  473. * other cases.
  474. */
  475. #define atomic64_sub_and_test(i, v) (atomic64_sub_return((i), (v)) == 0)
  476. /*
  477. * atomic64_inc_and_test - increment and test
  478. * @v: pointer of type atomic64_t
  479. *
  480. * Atomically increments @v by 1
  481. * and returns true if the result is zero, or false for all
  482. * other cases.
  483. */
  484. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  485. /*
  486. * atomic64_dec_and_test - decrement by 1 and test
  487. * @v: pointer of type atomic64_t
  488. *
  489. * Atomically decrements @v by 1 and
  490. * returns true if the result is 0, or false for all other
  491. * cases.
  492. */
  493. #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
  494. /*
  495. * atomic64_dec_if_positive - decrement by 1 if old value positive
  496. * @v: pointer of type atomic64_t
  497. */
  498. #define atomic64_dec_if_positive(v) atomic64_sub_if_positive(1, v)
  499. /*
  500. * atomic64_inc - increment atomic variable
  501. * @v: pointer of type atomic64_t
  502. *
  503. * Atomically increments @v by 1.
  504. */
  505. #define atomic64_inc(v) atomic64_add(1, (v))
  506. /*
  507. * atomic64_dec - decrement and test
  508. * @v: pointer of type atomic64_t
  509. *
  510. * Atomically decrements @v by 1.
  511. */
  512. #define atomic64_dec(v) atomic64_sub(1, (v))
  513. /*
  514. * atomic64_add_negative - add and test if negative
  515. * @v: pointer of type atomic64_t
  516. * @i: integer value to add
  517. *
  518. * Atomically adds @i to @v and returns true
  519. * if the result is negative, or false when
  520. * result is greater than or equal to zero.
  521. */
  522. #define atomic64_add_negative(i, v) (atomic64_add_return(i, (v)) < 0)
  523. #endif /* CONFIG_64BIT */
  524. #endif /* _ASM_ATOMIC_H */