atomic.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Low-level functions for atomic operations. Mips version.
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _MIPS_BITS_ATOMIC_H
  17. #define _MIPS_BITS_ATOMIC_H 1
  18. #include <inttypes.h>
  19. #include <sgidefs.h>
  20. typedef int32_t atomic32_t;
  21. typedef uint32_t uatomic32_t;
  22. typedef int_fast32_t atomic_fast32_t;
  23. typedef uint_fast32_t uatomic_fast32_t;
  24. typedef int64_t atomic64_t;
  25. typedef uint64_t uatomic64_t;
  26. typedef int_fast64_t atomic_fast64_t;
  27. typedef uint_fast64_t uatomic_fast64_t;
  28. typedef intptr_t atomicptr_t;
  29. typedef uintptr_t uatomicptr_t;
  30. typedef intmax_t atomic_max_t;
  31. typedef uintmax_t uatomic_max_t;
  32. #if _MIPS_SIM == _ABIO32
  33. #define MIPS_PUSH_MIPS2 ".set mips2\n\t"
  34. #else
  35. #define MIPS_PUSH_MIPS2
  36. #endif
  37. /* See the comments in <sys/asm.h> about the use of the sync instruction. */
  38. #ifndef MIPS_SYNC
  39. # define MIPS_SYNC sync
  40. #endif
  41. /* Certain revisions of the R10000 Processor need an LL/SC Workaround
  42. enabled. Revisions before 3.0 misbehave on atomic operations, and
  43. Revs 2.6 and lower deadlock after several seconds due to other errata.
  44. To quote the R10K Errata:
  45. Workaround: The basic idea is to inhibit the four instructions
  46. from simultaneously becoming active in R10000. Padding all
  47. ll/sc sequences with nops or changing the looping branch in the
  48. routines to a branch likely (which is always predicted taken
  49. by R10000) will work. The nops should go after the loop, and the
  50. number of them should be 28. This number could be decremented for
  51. each additional instruction in the ll/sc loop such as the lock
  52. modifier(s) between the ll and sc, the looping branch and its
  53. delay slot. For typical short routines with one ll/sc loop, any
  54. instructions after the loop could also count as a decrement. The
  55. nop workaround pollutes the cache more but would be a few cycles
  56. faster if all the code is in the cache and the looping branch
  57. is predicted not taken. */
  58. #ifdef _MIPS_ARCH_R10000
  59. #define R10K_BEQZ_INSN "beqzl"
  60. #else
  61. #define R10K_BEQZ_INSN "beqz"
  62. #endif
  63. #define MIPS_SYNC_STR_2(X) #X
  64. #define MIPS_SYNC_STR_1(X) MIPS_SYNC_STR_2(X)
  65. #define MIPS_SYNC_STR MIPS_SYNC_STR_1(MIPS_SYNC)
  66. /* Compare and exchange. For all of the "xxx" routines, we expect a
  67. "__prev" and a "__cmp" variable to be provided by the enclosing scope,
  68. in which values are returned. */
  69. #define __arch_compare_and_exchange_xxx_8_int(mem, newval, oldval, rel, acq) \
  70. (abort (), __prev = __cmp = 0)
  71. #define __arch_compare_and_exchange_xxx_16_int(mem, newval, oldval, rel, acq) \
  72. (abort (), __prev = __cmp = 0)
  73. #define __arch_compare_and_exchange_xxx_32_int(mem, newval, oldval, rel, acq) \
  74. __asm__ __volatile__ ( \
  75. ".set push\n\t" \
  76. MIPS_PUSH_MIPS2 \
  77. rel "\n" \
  78. "1:\t" \
  79. "ll %0,%5\n\t" \
  80. "move %1,$0\n\t" \
  81. "bne %0,%3,2f\n\t" \
  82. "move %1,%4\n\t" \
  83. "sc %1,%2\n\t" \
  84. R10K_BEQZ_INSN" %1,1b\n" \
  85. acq "\n\t" \
  86. ".set pop\n" \
  87. "2:\n\t" \
  88. : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
  89. : "r" (oldval), "r" (newval), "m" (*mem) \
  90. : "memory")
  91. #if _MIPS_SIM == _ABIO32
  92. /* We can't do an atomic 64-bit operation in O32. */
  93. #define __arch_compare_and_exchange_xxx_64_int(mem, newval, oldval, rel, acq) \
  94. (abort (), __prev = __cmp = 0)
  95. #else
  96. #define __arch_compare_and_exchange_xxx_64_int(mem, newval, oldval, rel, acq) \
  97. __asm__ __volatile__ ("\n" \
  98. ".set push\n\t" \
  99. MIPS_PUSH_MIPS2 \
  100. rel "\n" \
  101. "1:\t" \
  102. "lld %0,%5\n\t" \
  103. "move %1,$0\n\t" \
  104. "bne %0,%3,2f\n\t" \
  105. "move %1,%4\n\t" \
  106. "scd %1,%2\n\t" \
  107. R10K_BEQZ_INSN" %1,1b\n" \
  108. acq "\n\t" \
  109. ".set pop\n" \
  110. "2:\n\t" \
  111. : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
  112. : "r" (oldval), "r" (newval), "m" (*mem) \
  113. : "memory")
  114. #endif
  115. /* For all "bool" routines, we return FALSE if exchange succesful. */
  116. #define __arch_compare_and_exchange_bool_8_int(mem, new, old, rel, acq) \
  117. ({ __typeof (*mem) __prev; int __cmp; \
  118. __arch_compare_and_exchange_xxx_8_int(mem, new, old, rel, acq); \
  119. !__cmp; })
  120. #define __arch_compare_and_exchange_bool_16_int(mem, new, old, rel, acq) \
  121. ({ __typeof (*mem) __prev; int __cmp; \
  122. __arch_compare_and_exchange_xxx_16_int(mem, new, old, rel, acq); \
  123. !__cmp; })
  124. #define __arch_compare_and_exchange_bool_32_int(mem, new, old, rel, acq) \
  125. ({ __typeof (*mem) __prev; int __cmp; \
  126. __arch_compare_and_exchange_xxx_32_int(mem, new, old, rel, acq); \
  127. !__cmp; })
  128. #define __arch_compare_and_exchange_bool_64_int(mem, new, old, rel, acq) \
  129. ({ __typeof (*mem) __prev; int __cmp; \
  130. __arch_compare_and_exchange_xxx_64_int(mem, new, old, rel, acq); \
  131. !__cmp; })
  132. /* For all "val" routines, return the old value whether exchange
  133. successful or not. */
  134. #define __arch_compare_and_exchange_val_8_int(mem, new, old, rel, acq) \
  135. ({ __typeof (*mem) __prev; int __cmp; \
  136. __arch_compare_and_exchange_xxx_8_int(mem, new, old, rel, acq); \
  137. (__typeof (*mem))__prev; })
  138. #define __arch_compare_and_exchange_val_16_int(mem, new, old, rel, acq) \
  139. ({ __typeof (*mem) __prev; int __cmp; \
  140. __arch_compare_and_exchange_xxx_16_int(mem, new, old, rel, acq); \
  141. (__typeof (*mem))__prev; })
  142. #define __arch_compare_and_exchange_val_32_int(mem, new, old, rel, acq) \
  143. ({ __typeof (*mem) __prev; int __cmp; \
  144. __arch_compare_and_exchange_xxx_32_int(mem, new, old, rel, acq); \
  145. (__typeof (*mem))__prev; })
  146. #define __arch_compare_and_exchange_val_64_int(mem, new, old, rel, acq) \
  147. ({ __typeof (*mem) __prev; int __cmp; \
  148. __arch_compare_and_exchange_xxx_64_int(mem, new, old, rel, acq); \
  149. (__typeof (*mem))__prev; })
  150. /* Compare and exchange with "acquire" semantics, ie barrier after. */
  151. #define atomic_compare_and_exchange_bool_acq(mem, new, old) \
  152. __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
  153. mem, new, old, "", MIPS_SYNC_STR)
  154. #define atomic_compare_and_exchange_val_acq(mem, new, old) \
  155. __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
  156. mem, new, old, "", MIPS_SYNC_STR)
  157. /* Compare and exchange with "release" semantics, ie barrier before. */
  158. #define atomic_compare_and_exchange_bool_rel(mem, new, old) \
  159. __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
  160. mem, new, old, MIPS_SYNC_STR, "")
  161. #define atomic_compare_and_exchange_val_rel(mem, new, old) \
  162. __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
  163. mem, new, old, MIPS_SYNC_STR, "")
  164. /* Atomic exchange (without compare). */
  165. #define __arch_exchange_xxx_8_int(mem, newval, rel, acq) \
  166. (abort (), 0)
  167. #define __arch_exchange_xxx_16_int(mem, newval, rel, acq) \
  168. (abort (), 0)
  169. #define __arch_exchange_xxx_32_int(mem, newval, rel, acq) \
  170. ({ __typeof (*mem) __prev; int __cmp; \
  171. __asm__ __volatile__ ("\n" \
  172. ".set push\n\t" \
  173. MIPS_PUSH_MIPS2 \
  174. rel "\n" \
  175. "1:\t" \
  176. "ll %0,%4\n\t" \
  177. "move %1,%3\n\t" \
  178. "sc %1,%2\n\t" \
  179. R10K_BEQZ_INSN" %1,1b\n" \
  180. acq "\n\t" \
  181. ".set pop\n" \
  182. "2:\n\t" \
  183. : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
  184. : "r" (newval), "m" (*mem) \
  185. : "memory"); \
  186. __prev; })
  187. #if _MIPS_SIM == _ABIO32
  188. /* We can't do an atomic 64-bit operation in O32. */
  189. #define __arch_exchange_xxx_64_int(mem, newval, rel, acq) \
  190. (abort (), 0)
  191. #else
  192. #define __arch_exchange_xxx_64_int(mem, newval, rel, acq) \
  193. ({ __typeof (*mem) __prev; int __cmp; \
  194. __asm__ __volatile__ ("\n" \
  195. ".set push\n\t" \
  196. MIPS_PUSH_MIPS2 \
  197. rel "\n" \
  198. "1:\n" \
  199. "lld %0,%4\n\t" \
  200. "move %1,%3\n\t" \
  201. "scd %1,%2\n\t" \
  202. R10K_BEQZ_INSN" %1,1b\n" \
  203. acq "\n\t" \
  204. ".set pop\n" \
  205. "2:\n\t" \
  206. : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
  207. : "r" (newval), "m" (*mem) \
  208. : "memory"); \
  209. __prev; })
  210. #endif
  211. #define atomic_exchange_acq(mem, value) \
  212. __atomic_val_bysize (__arch_exchange_xxx, int, mem, value, "", MIPS_SYNC_STR)
  213. #define atomic_exchange_rel(mem, value) \
  214. __atomic_val_bysize (__arch_exchange_xxx, int, mem, value, MIPS_SYNC_STR, "")
  215. /* Atomically add value and return the previous (unincremented) value. */
  216. #define __arch_exchange_and_add_8_int(mem, newval, rel, acq) \
  217. (abort (), (__typeof(*mem)) 0)
  218. #define __arch_exchange_and_add_16_int(mem, newval, rel, acq) \
  219. (abort (), (__typeof(*mem)) 0)
  220. #define __arch_exchange_and_add_32_int(mem, value, rel, acq) \
  221. ({ __typeof (*mem) __prev; int __cmp; \
  222. __asm__ __volatile__ ("\n" \
  223. ".set push\n\t" \
  224. MIPS_PUSH_MIPS2 \
  225. rel "\n" \
  226. "1:\t" \
  227. "ll %0,%4\n\t" \
  228. "addu %1,%0,%3\n\t" \
  229. "sc %1,%2\n\t" \
  230. R10K_BEQZ_INSN" %1,1b\n" \
  231. acq "\n\t" \
  232. ".set pop\n" \
  233. "2:\n\t" \
  234. : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
  235. : "r" (value), "m" (*mem) \
  236. : "memory"); \
  237. __prev; })
  238. #if _MIPS_SIM == _ABIO32
  239. /* We can't do an atomic 64-bit operation in O32. */
  240. #define __arch_exchange_and_add_64_int(mem, value, rel, acq) \
  241. (abort (), (__typeof(*mem)) 0)
  242. #else
  243. #define __arch_exchange_and_add_64_int(mem, value, rel, acq) \
  244. ({ __typeof (*mem) __prev; int __cmp; \
  245. __asm__ __volatile__ ( \
  246. ".set push\n\t" \
  247. MIPS_PUSH_MIPS2 \
  248. rel "\n" \
  249. "1:\t" \
  250. "lld %0,%4\n\t" \
  251. "daddu %1,%0,%3\n\t" \
  252. "scd %1,%2\n\t" \
  253. R10K_BEQZ_INSN" %1,1b\n" \
  254. acq "\n\t" \
  255. ".set pop\n" \
  256. "2:\n\t" \
  257. : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
  258. : "r" (value), "m" (*mem) \
  259. : "memory"); \
  260. __prev; })
  261. #endif
  262. /* ??? Barrier semantics for atomic_exchange_and_add appear to be
  263. undefined. Use full barrier for now, as that's safe. */
  264. #define atomic_exchange_and_add(mem, value) \
  265. __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
  266. MIPS_SYNC_STR, MIPS_SYNC_STR)
  267. /* TODO: More atomic operations could be implemented efficiently; only the
  268. basic requirements are done. */
  269. #define atomic_full_barrier() \
  270. __asm__ __volatile__ (".set push\n\t" \
  271. MIPS_PUSH_MIPS2 \
  272. MIPS_SYNC_STR "\n\t" \
  273. ".set pop" : : : "memory")
  274. #endif /* bits/atomic.h */