atomic.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #ifndef _ASM_POWERPC_ATOMIC_H_
  2. #define _ASM_POWERPC_ATOMIC_H_
  3. /*
  4. * PowerPC atomic operations
  5. */
  6. #ifdef __KERNEL__
  7. #include <linux/types.h>
  8. #include <asm/cmpxchg.h>
  9. #include <asm/barrier.h>
  10. #define ATOMIC_INIT(i) { (i) }
  11. static __inline__ int atomic_read(const atomic_t *v)
  12. {
  13. int t;
  14. __asm__ __volatile__("lwz%U1%X1 %0,%1" : "=r"(t) : "m"(v->counter));
  15. return t;
  16. }
  17. static __inline__ void atomic_set(atomic_t *v, int i)
  18. {
  19. __asm__ __volatile__("stw%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i));
  20. }
  21. #define ATOMIC_OP(op, asm_op) \
  22. static __inline__ void atomic_##op(int a, atomic_t *v) \
  23. { \
  24. int t; \
  25. \
  26. __asm__ __volatile__( \
  27. "1: lwarx %0,0,%3 # atomic_" #op "\n" \
  28. #asm_op " %0,%2,%0\n" \
  29. PPC405_ERR77(0,%3) \
  30. " stwcx. %0,0,%3 \n" \
  31. " bne- 1b\n" \
  32. : "=&r" (t), "+m" (v->counter) \
  33. : "r" (a), "r" (&v->counter) \
  34. : "cc"); \
  35. } \
  36. #define ATOMIC_OP_RETURN(op, asm_op) \
  37. static __inline__ int atomic_##op##_return(int a, atomic_t *v) \
  38. { \
  39. int t; \
  40. \
  41. __asm__ __volatile__( \
  42. PPC_ATOMIC_ENTRY_BARRIER \
  43. "1: lwarx %0,0,%2 # atomic_" #op "_return\n" \
  44. #asm_op " %0,%1,%0\n" \
  45. PPC405_ERR77(0,%2) \
  46. " stwcx. %0,0,%2 \n" \
  47. " bne- 1b\n" \
  48. PPC_ATOMIC_EXIT_BARRIER \
  49. : "=&r" (t) \
  50. : "r" (a), "r" (&v->counter) \
  51. : "cc", "memory"); \
  52. \
  53. return t; \
  54. }
  55. #define ATOMIC_OPS(op, asm_op) ATOMIC_OP(op, asm_op) ATOMIC_OP_RETURN(op, asm_op)
  56. ATOMIC_OPS(add, add)
  57. ATOMIC_OPS(sub, subf)
  58. #undef ATOMIC_OPS
  59. #undef ATOMIC_OP_RETURN
  60. #undef ATOMIC_OP
  61. #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
  62. static __inline__ void atomic_inc(atomic_t *v)
  63. {
  64. int t;
  65. __asm__ __volatile__(
  66. "1: lwarx %0,0,%2 # atomic_inc\n\
  67. addic %0,%0,1\n"
  68. PPC405_ERR77(0,%2)
  69. " stwcx. %0,0,%2 \n\
  70. bne- 1b"
  71. : "=&r" (t), "+m" (v->counter)
  72. : "r" (&v->counter)
  73. : "cc", "xer");
  74. }
  75. static __inline__ int atomic_inc_return(atomic_t *v)
  76. {
  77. int t;
  78. __asm__ __volatile__(
  79. PPC_ATOMIC_ENTRY_BARRIER
  80. "1: lwarx %0,0,%1 # atomic_inc_return\n\
  81. addic %0,%0,1\n"
  82. PPC405_ERR77(0,%1)
  83. " stwcx. %0,0,%1 \n\
  84. bne- 1b"
  85. PPC_ATOMIC_EXIT_BARRIER
  86. : "=&r" (t)
  87. : "r" (&v->counter)
  88. : "cc", "xer", "memory");
  89. return t;
  90. }
  91. /*
  92. * atomic_inc_and_test - increment and test
  93. * @v: pointer of type atomic_t
  94. *
  95. * Atomically increments @v by 1
  96. * and returns true if the result is zero, or false for all
  97. * other cases.
  98. */
  99. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  100. static __inline__ void atomic_dec(atomic_t *v)
  101. {
  102. int t;
  103. __asm__ __volatile__(
  104. "1: lwarx %0,0,%2 # atomic_dec\n\
  105. addic %0,%0,-1\n"
  106. PPC405_ERR77(0,%2)\
  107. " stwcx. %0,0,%2\n\
  108. bne- 1b"
  109. : "=&r" (t), "+m" (v->counter)
  110. : "r" (&v->counter)
  111. : "cc", "xer");
  112. }
  113. static __inline__ int atomic_dec_return(atomic_t *v)
  114. {
  115. int t;
  116. __asm__ __volatile__(
  117. PPC_ATOMIC_ENTRY_BARRIER
  118. "1: lwarx %0,0,%1 # atomic_dec_return\n\
  119. addic %0,%0,-1\n"
  120. PPC405_ERR77(0,%1)
  121. " stwcx. %0,0,%1\n\
  122. bne- 1b"
  123. PPC_ATOMIC_EXIT_BARRIER
  124. : "=&r" (t)
  125. : "r" (&v->counter)
  126. : "cc", "xer", "memory");
  127. return t;
  128. }
  129. #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  130. #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
  131. /**
  132. * __atomic_add_unless - add unless the number is a given value
  133. * @v: pointer of type atomic_t
  134. * @a: the amount to add to v...
  135. * @u: ...unless v is equal to u.
  136. *
  137. * Atomically adds @a to @v, so long as it was not @u.
  138. * Returns the old value of @v.
  139. */
  140. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  141. {
  142. int t;
  143. __asm__ __volatile__ (
  144. PPC_ATOMIC_ENTRY_BARRIER
  145. "1: lwarx %0,0,%1 # __atomic_add_unless\n\
  146. cmpw 0,%0,%3 \n\
  147. beq- 2f \n\
  148. add %0,%2,%0 \n"
  149. PPC405_ERR77(0,%2)
  150. " stwcx. %0,0,%1 \n\
  151. bne- 1b \n"
  152. PPC_ATOMIC_EXIT_BARRIER
  153. " subf %0,%2,%0 \n\
  154. 2:"
  155. : "=&r" (t)
  156. : "r" (&v->counter), "r" (a), "r" (u)
  157. : "cc", "memory");
  158. return t;
  159. }
  160. /**
  161. * atomic_inc_not_zero - increment unless the number is zero
  162. * @v: pointer of type atomic_t
  163. *
  164. * Atomically increments @v by 1, so long as @v is non-zero.
  165. * Returns non-zero if @v was non-zero, and zero otherwise.
  166. */
  167. static __inline__ int atomic_inc_not_zero(atomic_t *v)
  168. {
  169. int t1, t2;
  170. __asm__ __volatile__ (
  171. PPC_ATOMIC_ENTRY_BARRIER
  172. "1: lwarx %0,0,%2 # atomic_inc_not_zero\n\
  173. cmpwi 0,%0,0\n\
  174. beq- 2f\n\
  175. addic %1,%0,1\n"
  176. PPC405_ERR77(0,%2)
  177. " stwcx. %1,0,%2\n\
  178. bne- 1b\n"
  179. PPC_ATOMIC_EXIT_BARRIER
  180. "\n\
  181. 2:"
  182. : "=&r" (t1), "=&r" (t2)
  183. : "r" (&v->counter)
  184. : "cc", "xer", "memory");
  185. return t1;
  186. }
  187. #define atomic_inc_not_zero(v) atomic_inc_not_zero((v))
  188. #define atomic_sub_and_test(a, v) (atomic_sub_return((a), (v)) == 0)
  189. #define atomic_dec_and_test(v) (atomic_dec_return((v)) == 0)
  190. /*
  191. * Atomically test *v and decrement if it is greater than 0.
  192. * The function returns the old value of *v minus 1, even if
  193. * the atomic variable, v, was not decremented.
  194. */
  195. static __inline__ int atomic_dec_if_positive(atomic_t *v)
  196. {
  197. int t;
  198. __asm__ __volatile__(
  199. PPC_ATOMIC_ENTRY_BARRIER
  200. "1: lwarx %0,0,%1 # atomic_dec_if_positive\n\
  201. cmpwi %0,1\n\
  202. addi %0,%0,-1\n\
  203. blt- 2f\n"
  204. PPC405_ERR77(0,%1)
  205. " stwcx. %0,0,%1\n\
  206. bne- 1b"
  207. PPC_ATOMIC_EXIT_BARRIER
  208. "\n\
  209. 2:" : "=&b" (t)
  210. : "r" (&v->counter)
  211. : "cc", "memory");
  212. return t;
  213. }
  214. #define atomic_dec_if_positive atomic_dec_if_positive
  215. #ifdef __powerpc64__
  216. #define ATOMIC64_INIT(i) { (i) }
  217. static __inline__ long atomic64_read(const atomic64_t *v)
  218. {
  219. long t;
  220. __asm__ __volatile__("ld%U1%X1 %0,%1" : "=r"(t) : "m"(v->counter));
  221. return t;
  222. }
  223. static __inline__ void atomic64_set(atomic64_t *v, long i)
  224. {
  225. __asm__ __volatile__("std%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i));
  226. }
  227. #define ATOMIC64_OP(op, asm_op) \
  228. static __inline__ void atomic64_##op(long a, atomic64_t *v) \
  229. { \
  230. long t; \
  231. \
  232. __asm__ __volatile__( \
  233. "1: ldarx %0,0,%3 # atomic64_" #op "\n" \
  234. #asm_op " %0,%2,%0\n" \
  235. " stdcx. %0,0,%3 \n" \
  236. " bne- 1b\n" \
  237. : "=&r" (t), "+m" (v->counter) \
  238. : "r" (a), "r" (&v->counter) \
  239. : "cc"); \
  240. }
  241. #define ATOMIC64_OP_RETURN(op, asm_op) \
  242. static __inline__ long atomic64_##op##_return(long a, atomic64_t *v) \
  243. { \
  244. long t; \
  245. \
  246. __asm__ __volatile__( \
  247. PPC_ATOMIC_ENTRY_BARRIER \
  248. "1: ldarx %0,0,%2 # atomic64_" #op "_return\n" \
  249. #asm_op " %0,%1,%0\n" \
  250. " stdcx. %0,0,%2 \n" \
  251. " bne- 1b\n" \
  252. PPC_ATOMIC_EXIT_BARRIER \
  253. : "=&r" (t) \
  254. : "r" (a), "r" (&v->counter) \
  255. : "cc", "memory"); \
  256. \
  257. return t; \
  258. }
  259. #define ATOMIC64_OPS(op, asm_op) ATOMIC64_OP(op, asm_op) ATOMIC64_OP_RETURN(op, asm_op)
  260. ATOMIC64_OPS(add, add)
  261. ATOMIC64_OPS(sub, subf)
  262. #undef ATOMIC64_OPS
  263. #undef ATOMIC64_OP_RETURN
  264. #undef ATOMIC64_OP
  265. #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
  266. static __inline__ void atomic64_inc(atomic64_t *v)
  267. {
  268. long t;
  269. __asm__ __volatile__(
  270. "1: ldarx %0,0,%2 # atomic64_inc\n\
  271. addic %0,%0,1\n\
  272. stdcx. %0,0,%2 \n\
  273. bne- 1b"
  274. : "=&r" (t), "+m" (v->counter)
  275. : "r" (&v->counter)
  276. : "cc", "xer");
  277. }
  278. static __inline__ long atomic64_inc_return(atomic64_t *v)
  279. {
  280. long t;
  281. __asm__ __volatile__(
  282. PPC_ATOMIC_ENTRY_BARRIER
  283. "1: ldarx %0,0,%1 # atomic64_inc_return\n\
  284. addic %0,%0,1\n\
  285. stdcx. %0,0,%1 \n\
  286. bne- 1b"
  287. PPC_ATOMIC_EXIT_BARRIER
  288. : "=&r" (t)
  289. : "r" (&v->counter)
  290. : "cc", "xer", "memory");
  291. return t;
  292. }
  293. /*
  294. * atomic64_inc_and_test - increment and test
  295. * @v: pointer of type atomic64_t
  296. *
  297. * Atomically increments @v by 1
  298. * and returns true if the result is zero, or false for all
  299. * other cases.
  300. */
  301. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  302. static __inline__ void atomic64_dec(atomic64_t *v)
  303. {
  304. long t;
  305. __asm__ __volatile__(
  306. "1: ldarx %0,0,%2 # atomic64_dec\n\
  307. addic %0,%0,-1\n\
  308. stdcx. %0,0,%2\n\
  309. bne- 1b"
  310. : "=&r" (t), "+m" (v->counter)
  311. : "r" (&v->counter)
  312. : "cc", "xer");
  313. }
  314. static __inline__ long atomic64_dec_return(atomic64_t *v)
  315. {
  316. long t;
  317. __asm__ __volatile__(
  318. PPC_ATOMIC_ENTRY_BARRIER
  319. "1: ldarx %0,0,%1 # atomic64_dec_return\n\
  320. addic %0,%0,-1\n\
  321. stdcx. %0,0,%1\n\
  322. bne- 1b"
  323. PPC_ATOMIC_EXIT_BARRIER
  324. : "=&r" (t)
  325. : "r" (&v->counter)
  326. : "cc", "xer", "memory");
  327. return t;
  328. }
  329. #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
  330. #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
  331. /*
  332. * Atomically test *v and decrement if it is greater than 0.
  333. * The function returns the old value of *v minus 1.
  334. */
  335. static __inline__ long atomic64_dec_if_positive(atomic64_t *v)
  336. {
  337. long t;
  338. __asm__ __volatile__(
  339. PPC_ATOMIC_ENTRY_BARRIER
  340. "1: ldarx %0,0,%1 # atomic64_dec_if_positive\n\
  341. addic. %0,%0,-1\n\
  342. blt- 2f\n\
  343. stdcx. %0,0,%1\n\
  344. bne- 1b"
  345. PPC_ATOMIC_EXIT_BARRIER
  346. "\n\
  347. 2:" : "=&r" (t)
  348. : "r" (&v->counter)
  349. : "cc", "xer", "memory");
  350. return t;
  351. }
  352. #define atomic64_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  353. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  354. /**
  355. * atomic64_add_unless - add unless the number is a given value
  356. * @v: pointer of type atomic64_t
  357. * @a: the amount to add to v...
  358. * @u: ...unless v is equal to u.
  359. *
  360. * Atomically adds @a to @v, so long as it was not @u.
  361. * Returns the old value of @v.
  362. */
  363. static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
  364. {
  365. long t;
  366. __asm__ __volatile__ (
  367. PPC_ATOMIC_ENTRY_BARRIER
  368. "1: ldarx %0,0,%1 # __atomic_add_unless\n\
  369. cmpd 0,%0,%3 \n\
  370. beq- 2f \n\
  371. add %0,%2,%0 \n"
  372. " stdcx. %0,0,%1 \n\
  373. bne- 1b \n"
  374. PPC_ATOMIC_EXIT_BARRIER
  375. " subf %0,%2,%0 \n\
  376. 2:"
  377. : "=&r" (t)
  378. : "r" (&v->counter), "r" (a), "r" (u)
  379. : "cc", "memory");
  380. return t != u;
  381. }
  382. /**
  383. * atomic_inc64_not_zero - increment unless the number is zero
  384. * @v: pointer of type atomic64_t
  385. *
  386. * Atomically increments @v by 1, so long as @v is non-zero.
  387. * Returns non-zero if @v was non-zero, and zero otherwise.
  388. */
  389. static __inline__ long atomic64_inc_not_zero(atomic64_t *v)
  390. {
  391. long t1, t2;
  392. __asm__ __volatile__ (
  393. PPC_ATOMIC_ENTRY_BARRIER
  394. "1: ldarx %0,0,%2 # atomic64_inc_not_zero\n\
  395. cmpdi 0,%0,0\n\
  396. beq- 2f\n\
  397. addic %1,%0,1\n\
  398. stdcx. %1,0,%2\n\
  399. bne- 1b\n"
  400. PPC_ATOMIC_EXIT_BARRIER
  401. "\n\
  402. 2:"
  403. : "=&r" (t1), "=&r" (t2)
  404. : "r" (&v->counter)
  405. : "cc", "xer", "memory");
  406. return t1;
  407. }
  408. #endif /* __powerpc64__ */
  409. #endif /* __KERNEL__ */
  410. #endif /* _ASM_POWERPC_ATOMIC_H_ */