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 atomic_dec_if_positive atomic_dec_if_positive
  232. #define smp_mb__before_atomic_dec() smp_mb()
  233. #define smp_mb__after_atomic_dec() smp_mb()
  234. #define smp_mb__before_atomic_inc() smp_mb()
  235. #define smp_mb__after_atomic_inc() smp_mb()
  236. #ifdef __powerpc64__
  237. #define ATOMIC64_INIT(i) { (i) }
  238. static __inline__ long atomic64_read(const atomic64_t *v)
  239. {
  240. long t;
  241. __asm__ __volatile__("ld%U1%X1 %0,%1" : "=r"(t) : "m"(v->counter));
  242. return t;
  243. }
  244. static __inline__ void atomic64_set(atomic64_t *v, long i)
  245. {
  246. __asm__ __volatile__("std%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i));
  247. }
  248. static __inline__ void atomic64_add(long a, atomic64_t *v)
  249. {
  250. long t;
  251. __asm__ __volatile__(
  252. "1: ldarx %0,0,%3 # atomic64_add\n\
  253. add %0,%2,%0\n\
  254. stdcx. %0,0,%3 \n\
  255. bne- 1b"
  256. : "=&r" (t), "+m" (v->counter)
  257. : "r" (a), "r" (&v->counter)
  258. : "cc");
  259. }
  260. static __inline__ long atomic64_add_return(long a, atomic64_t *v)
  261. {
  262. long t;
  263. __asm__ __volatile__(
  264. PPC_ATOMIC_ENTRY_BARRIER
  265. "1: ldarx %0,0,%2 # atomic64_add_return\n\
  266. add %0,%1,%0\n\
  267. stdcx. %0,0,%2 \n\
  268. bne- 1b"
  269. PPC_ATOMIC_EXIT_BARRIER
  270. : "=&r" (t)
  271. : "r" (a), "r" (&v->counter)
  272. : "cc", "memory");
  273. return t;
  274. }
  275. #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
  276. static __inline__ void atomic64_sub(long a, atomic64_t *v)
  277. {
  278. long t;
  279. __asm__ __volatile__(
  280. "1: ldarx %0,0,%3 # atomic64_sub\n\
  281. subf %0,%2,%0\n\
  282. stdcx. %0,0,%3 \n\
  283. bne- 1b"
  284. : "=&r" (t), "+m" (v->counter)
  285. : "r" (a), "r" (&v->counter)
  286. : "cc");
  287. }
  288. static __inline__ long atomic64_sub_return(long a, atomic64_t *v)
  289. {
  290. long t;
  291. __asm__ __volatile__(
  292. PPC_ATOMIC_ENTRY_BARRIER
  293. "1: ldarx %0,0,%2 # atomic64_sub_return\n\
  294. subf %0,%1,%0\n\
  295. stdcx. %0,0,%2 \n\
  296. bne- 1b"
  297. PPC_ATOMIC_EXIT_BARRIER
  298. : "=&r" (t)
  299. : "r" (a), "r" (&v->counter)
  300. : "cc", "memory");
  301. return t;
  302. }
  303. static __inline__ void atomic64_inc(atomic64_t *v)
  304. {
  305. long t;
  306. __asm__ __volatile__(
  307. "1: ldarx %0,0,%2 # atomic64_inc\n\
  308. addic %0,%0,1\n\
  309. stdcx. %0,0,%2 \n\
  310. bne- 1b"
  311. : "=&r" (t), "+m" (v->counter)
  312. : "r" (&v->counter)
  313. : "cc", "xer");
  314. }
  315. static __inline__ long atomic64_inc_return(atomic64_t *v)
  316. {
  317. long t;
  318. __asm__ __volatile__(
  319. PPC_ATOMIC_ENTRY_BARRIER
  320. "1: ldarx %0,0,%1 # atomic64_inc_return\n\
  321. addic %0,%0,1\n\
  322. stdcx. %0,0,%1 \n\
  323. bne- 1b"
  324. PPC_ATOMIC_EXIT_BARRIER
  325. : "=&r" (t)
  326. : "r" (&v->counter)
  327. : "cc", "xer", "memory");
  328. return t;
  329. }
  330. /*
  331. * atomic64_inc_and_test - increment and test
  332. * @v: pointer of type atomic64_t
  333. *
  334. * Atomically increments @v by 1
  335. * and returns true if the result is zero, or false for all
  336. * other cases.
  337. */
  338. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  339. static __inline__ void atomic64_dec(atomic64_t *v)
  340. {
  341. long t;
  342. __asm__ __volatile__(
  343. "1: ldarx %0,0,%2 # atomic64_dec\n\
  344. addic %0,%0,-1\n\
  345. stdcx. %0,0,%2\n\
  346. bne- 1b"
  347. : "=&r" (t), "+m" (v->counter)
  348. : "r" (&v->counter)
  349. : "cc", "xer");
  350. }
  351. static __inline__ long atomic64_dec_return(atomic64_t *v)
  352. {
  353. long t;
  354. __asm__ __volatile__(
  355. PPC_ATOMIC_ENTRY_BARRIER
  356. "1: ldarx %0,0,%1 # atomic64_dec_return\n\
  357. addic %0,%0,-1\n\
  358. stdcx. %0,0,%1\n\
  359. bne- 1b"
  360. PPC_ATOMIC_EXIT_BARRIER
  361. : "=&r" (t)
  362. : "r" (&v->counter)
  363. : "cc", "xer", "memory");
  364. return t;
  365. }
  366. #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
  367. #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
  368. /*
  369. * Atomically test *v and decrement if it is greater than 0.
  370. * The function returns the old value of *v minus 1.
  371. */
  372. static __inline__ long atomic64_dec_if_positive(atomic64_t *v)
  373. {
  374. long t;
  375. __asm__ __volatile__(
  376. PPC_ATOMIC_ENTRY_BARRIER
  377. "1: ldarx %0,0,%1 # atomic64_dec_if_positive\n\
  378. addic. %0,%0,-1\n\
  379. blt- 2f\n\
  380. stdcx. %0,0,%1\n\
  381. bne- 1b"
  382. PPC_ATOMIC_EXIT_BARRIER
  383. "\n\
  384. 2:" : "=&r" (t)
  385. : "r" (&v->counter)
  386. : "cc", "xer", "memory");
  387. return t;
  388. }
  389. #define atomic64_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  390. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  391. /**
  392. * atomic64_add_unless - add unless the number is a given value
  393. * @v: pointer of type atomic64_t
  394. * @a: the amount to add to v...
  395. * @u: ...unless v is equal to u.
  396. *
  397. * Atomically adds @a to @v, so long as it was not @u.
  398. * Returns the old value of @v.
  399. */
  400. static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
  401. {
  402. long t;
  403. __asm__ __volatile__ (
  404. PPC_ATOMIC_ENTRY_BARRIER
  405. "1: ldarx %0,0,%1 # __atomic_add_unless\n\
  406. cmpd 0,%0,%3 \n\
  407. beq- 2f \n\
  408. add %0,%2,%0 \n"
  409. " stdcx. %0,0,%1 \n\
  410. bne- 1b \n"
  411. PPC_ATOMIC_EXIT_BARRIER
  412. " subf %0,%2,%0 \n\
  413. 2:"
  414. : "=&r" (t)
  415. : "r" (&v->counter), "r" (a), "r" (u)
  416. : "cc", "memory");
  417. return t != u;
  418. }
  419. /**
  420. * atomic_inc64_not_zero - increment unless the number is zero
  421. * @v: pointer of type atomic64_t
  422. *
  423. * Atomically increments @v by 1, so long as @v is non-zero.
  424. * Returns non-zero if @v was non-zero, and zero otherwise.
  425. */
  426. static __inline__ long atomic64_inc_not_zero(atomic64_t *v)
  427. {
  428. long t1, t2;
  429. __asm__ __volatile__ (
  430. PPC_ATOMIC_ENTRY_BARRIER
  431. "1: ldarx %0,0,%2 # atomic64_inc_not_zero\n\
  432. cmpdi 0,%0,0\n\
  433. beq- 2f\n\
  434. addic %1,%0,1\n\
  435. stdcx. %1,0,%2\n\
  436. bne- 1b\n"
  437. PPC_ATOMIC_EXIT_BARRIER
  438. "\n\
  439. 2:"
  440. : "=&r" (t1), "=&r" (t2)
  441. : "r" (&v->counter)
  442. : "cc", "xer", "memory");
  443. return t1;
  444. }
  445. #endif /* __powerpc64__ */
  446. #endif /* __KERNEL__ */
  447. #endif /* _ASM_POWERPC_ATOMIC_H_ */