atomic.h 10 KB

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