atomic.h 9.1 KB

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