atomic.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #ifndef _ASM_POWERPC_ATOMIC_H_
  2. #define _ASM_POWERPC_ATOMIC_H_
  3. /*
  4. * PowerPC atomic operations
  5. */
  6. typedef struct { int counter; } atomic_t;
  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");
  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", "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");
  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", "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. typedef struct { long counter; } atomic64_t;
  213. #define ATOMIC64_INIT(i) { (i) }
  214. static __inline__ long atomic64_read(const atomic64_t *v)
  215. {
  216. long t;
  217. __asm__ __volatile__("ld%U1%X1 %0,%1" : "=r"(t) : "m"(v->counter));
  218. return t;
  219. }
  220. static __inline__ void atomic64_set(atomic64_t *v, long i)
  221. {
  222. __asm__ __volatile__("std%U0%X0 %1,%0" : "=m"(v->counter) : "r"(i));
  223. }
  224. static __inline__ void atomic64_add(long a, atomic64_t *v)
  225. {
  226. long t;
  227. __asm__ __volatile__(
  228. "1: ldarx %0,0,%3 # atomic64_add\n\
  229. add %0,%2,%0\n\
  230. stdcx. %0,0,%3 \n\
  231. bne- 1b"
  232. : "=&r" (t), "+m" (v->counter)
  233. : "r" (a), "r" (&v->counter)
  234. : "cc");
  235. }
  236. static __inline__ long atomic64_add_return(long a, atomic64_t *v)
  237. {
  238. long t;
  239. __asm__ __volatile__(
  240. LWSYNC_ON_SMP
  241. "1: ldarx %0,0,%2 # atomic64_add_return\n\
  242. add %0,%1,%0\n\
  243. stdcx. %0,0,%2 \n\
  244. bne- 1b"
  245. ISYNC_ON_SMP
  246. : "=&r" (t)
  247. : "r" (a), "r" (&v->counter)
  248. : "cc", "memory");
  249. return t;
  250. }
  251. #define atomic64_add_negative(a, v) (atomic64_add_return((a), (v)) < 0)
  252. static __inline__ void atomic64_sub(long a, atomic64_t *v)
  253. {
  254. long t;
  255. __asm__ __volatile__(
  256. "1: ldarx %0,0,%3 # atomic64_sub\n\
  257. subf %0,%2,%0\n\
  258. stdcx. %0,0,%3 \n\
  259. bne- 1b"
  260. : "=&r" (t), "+m" (v->counter)
  261. : "r" (a), "r" (&v->counter)
  262. : "cc");
  263. }
  264. static __inline__ long atomic64_sub_return(long a, atomic64_t *v)
  265. {
  266. long t;
  267. __asm__ __volatile__(
  268. LWSYNC_ON_SMP
  269. "1: ldarx %0,0,%2 # atomic64_sub_return\n\
  270. subf %0,%1,%0\n\
  271. stdcx. %0,0,%2 \n\
  272. bne- 1b"
  273. ISYNC_ON_SMP
  274. : "=&r" (t)
  275. : "r" (a), "r" (&v->counter)
  276. : "cc", "memory");
  277. return t;
  278. }
  279. static __inline__ void atomic64_inc(atomic64_t *v)
  280. {
  281. long t;
  282. __asm__ __volatile__(
  283. "1: ldarx %0,0,%2 # atomic64_inc\n\
  284. addic %0,%0,1\n\
  285. stdcx. %0,0,%2 \n\
  286. bne- 1b"
  287. : "=&r" (t), "+m" (v->counter)
  288. : "r" (&v->counter)
  289. : "cc");
  290. }
  291. static __inline__ long atomic64_inc_return(atomic64_t *v)
  292. {
  293. long t;
  294. __asm__ __volatile__(
  295. LWSYNC_ON_SMP
  296. "1: ldarx %0,0,%1 # atomic64_inc_return\n\
  297. addic %0,%0,1\n\
  298. stdcx. %0,0,%1 \n\
  299. bne- 1b"
  300. ISYNC_ON_SMP
  301. : "=&r" (t)
  302. : "r" (&v->counter)
  303. : "cc", "memory");
  304. return t;
  305. }
  306. /*
  307. * atomic64_inc_and_test - increment and test
  308. * @v: pointer of type atomic64_t
  309. *
  310. * Atomically increments @v by 1
  311. * and returns true if the result is zero, or false for all
  312. * other cases.
  313. */
  314. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  315. static __inline__ void atomic64_dec(atomic64_t *v)
  316. {
  317. long t;
  318. __asm__ __volatile__(
  319. "1: ldarx %0,0,%2 # atomic64_dec\n\
  320. addic %0,%0,-1\n\
  321. stdcx. %0,0,%2\n\
  322. bne- 1b"
  323. : "=&r" (t), "+m" (v->counter)
  324. : "r" (&v->counter)
  325. : "cc");
  326. }
  327. static __inline__ long atomic64_dec_return(atomic64_t *v)
  328. {
  329. long t;
  330. __asm__ __volatile__(
  331. LWSYNC_ON_SMP
  332. "1: ldarx %0,0,%1 # atomic64_dec_return\n\
  333. addic %0,%0,-1\n\
  334. stdcx. %0,0,%1\n\
  335. bne- 1b"
  336. ISYNC_ON_SMP
  337. : "=&r" (t)
  338. : "r" (&v->counter)
  339. : "cc", "memory");
  340. return t;
  341. }
  342. #define atomic64_sub_and_test(a, v) (atomic64_sub_return((a), (v)) == 0)
  343. #define atomic64_dec_and_test(v) (atomic64_dec_return((v)) == 0)
  344. /*
  345. * Atomically test *v and decrement if it is greater than 0.
  346. * The function returns the old value of *v minus 1.
  347. */
  348. static __inline__ long atomic64_dec_if_positive(atomic64_t *v)
  349. {
  350. long t;
  351. __asm__ __volatile__(
  352. LWSYNC_ON_SMP
  353. "1: ldarx %0,0,%1 # atomic64_dec_if_positive\n\
  354. addic. %0,%0,-1\n\
  355. blt- 2f\n\
  356. stdcx. %0,0,%1\n\
  357. bne- 1b"
  358. ISYNC_ON_SMP
  359. "\n\
  360. 2:" : "=&r" (t)
  361. : "r" (&v->counter)
  362. : "cc", "memory");
  363. return t;
  364. }
  365. #define atomic64_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  366. #define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
  367. /**
  368. * atomic64_add_unless - add unless the number is a given value
  369. * @v: pointer of type atomic64_t
  370. * @a: the amount to add to v...
  371. * @u: ...unless v is equal to u.
  372. *
  373. * Atomically adds @a to @v, so long as it was not @u.
  374. * Returns non-zero if @v was not @u, and zero otherwise.
  375. */
  376. static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
  377. {
  378. long t;
  379. __asm__ __volatile__ (
  380. LWSYNC_ON_SMP
  381. "1: ldarx %0,0,%1 # atomic_add_unless\n\
  382. cmpd 0,%0,%3 \n\
  383. beq- 2f \n\
  384. add %0,%2,%0 \n"
  385. " stdcx. %0,0,%1 \n\
  386. bne- 1b \n"
  387. ISYNC_ON_SMP
  388. " subf %0,%2,%0 \n\
  389. 2:"
  390. : "=&r" (t)
  391. : "r" (&v->counter), "r" (a), "r" (u)
  392. : "cc", "memory");
  393. return t != u;
  394. }
  395. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  396. #endif /* __powerpc64__ */
  397. #include <asm-generic/atomic.h>
  398. #endif /* __KERNEL__ */
  399. #endif /* _ASM_POWERPC_ATOMIC_H_ */