system.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1994, 95, 96, 97, 98, 99, 2003 by Ralf Baechle
  7. * Copyright (C) 1996 by Paul M. Antoine
  8. * Copyright (C) 1999 Silicon Graphics
  9. * Kevin D. Kissell, kevink@mips.org and Carsten Langgaard, carstenl@mips.com
  10. * Copyright (C) 2000 MIPS Technologies, Inc.
  11. */
  12. #ifndef _ASM_SYSTEM_H
  13. #define _ASM_SYSTEM_H
  14. #include <linux/config.h>
  15. #include <linux/types.h>
  16. #include <asm/addrspace.h>
  17. #include <asm/cpu-features.h>
  18. #include <asm/ptrace.h>
  19. #include <asm/war.h>
  20. #include <asm/interrupt.h>
  21. /*
  22. * read_barrier_depends - Flush all pending reads that subsequents reads
  23. * depend on.
  24. *
  25. * No data-dependent reads from memory-like regions are ever reordered
  26. * over this barrier. All reads preceding this primitive are guaranteed
  27. * to access memory (but not necessarily other CPUs' caches) before any
  28. * reads following this primitive that depend on the data return by
  29. * any of the preceding reads. This primitive is much lighter weight than
  30. * rmb() on most CPUs, and is never heavier weight than is
  31. * rmb().
  32. *
  33. * These ordering constraints are respected by both the local CPU
  34. * and the compiler.
  35. *
  36. * Ordering is not guaranteed by anything other than these primitives,
  37. * not even by data dependencies. See the documentation for
  38. * memory_barrier() for examples and URLs to more information.
  39. *
  40. * For example, the following code would force ordering (the initial
  41. * value of "a" is zero, "b" is one, and "p" is "&a"):
  42. *
  43. * <programlisting>
  44. * CPU 0 CPU 1
  45. *
  46. * b = 2;
  47. * memory_barrier();
  48. * p = &b; q = p;
  49. * read_barrier_depends();
  50. * d = *q;
  51. * </programlisting>
  52. *
  53. * because the read of "*q" depends on the read of "p" and these
  54. * two reads are separated by a read_barrier_depends(). However,
  55. * the following code, with the same initial values for "a" and "b":
  56. *
  57. * <programlisting>
  58. * CPU 0 CPU 1
  59. *
  60. * a = 2;
  61. * memory_barrier();
  62. * b = 3; y = b;
  63. * read_barrier_depends();
  64. * x = a;
  65. * </programlisting>
  66. *
  67. * does not enforce ordering, since there is no data dependency between
  68. * the read of "a" and the read of "b". Therefore, on some CPUs, such
  69. * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
  70. * in cases like thiswhere there are no data dependencies.
  71. */
  72. #define read_barrier_depends() do { } while(0)
  73. #ifdef CONFIG_CPU_HAS_SYNC
  74. #define __sync() \
  75. __asm__ __volatile__( \
  76. ".set push\n\t" \
  77. ".set noreorder\n\t" \
  78. ".set mips2\n\t" \
  79. "sync\n\t" \
  80. ".set pop" \
  81. : /* no output */ \
  82. : /* no input */ \
  83. : "memory")
  84. #else
  85. #define __sync() do { } while(0)
  86. #endif
  87. #define __fast_iob() \
  88. __asm__ __volatile__( \
  89. ".set push\n\t" \
  90. ".set noreorder\n\t" \
  91. "lw $0,%0\n\t" \
  92. "nop\n\t" \
  93. ".set pop" \
  94. : /* no output */ \
  95. : "m" (*(int *)CKSEG1) \
  96. : "memory")
  97. #define fast_wmb() __sync()
  98. #define fast_rmb() __sync()
  99. #define fast_mb() __sync()
  100. #define fast_iob() \
  101. do { \
  102. __sync(); \
  103. __fast_iob(); \
  104. } while (0)
  105. #ifdef CONFIG_CPU_HAS_WB
  106. #include <asm/wbflush.h>
  107. #define wmb() fast_wmb()
  108. #define rmb() fast_rmb()
  109. #define mb() wbflush()
  110. #define iob() wbflush()
  111. #else /* !CONFIG_CPU_HAS_WB */
  112. #define wmb() fast_wmb()
  113. #define rmb() fast_rmb()
  114. #define mb() fast_mb()
  115. #define iob() fast_iob()
  116. #endif /* !CONFIG_CPU_HAS_WB */
  117. #ifdef CONFIG_SMP
  118. #define smp_mb() mb()
  119. #define smp_rmb() rmb()
  120. #define smp_wmb() wmb()
  121. #define smp_read_barrier_depends() read_barrier_depends()
  122. #else
  123. #define smp_mb() barrier()
  124. #define smp_rmb() barrier()
  125. #define smp_wmb() barrier()
  126. #define smp_read_barrier_depends() do { } while(0)
  127. #endif
  128. #define set_mb(var, value) \
  129. do { var = value; mb(); } while (0)
  130. #define set_wmb(var, value) \
  131. do { var = value; wmb(); } while (0)
  132. /*
  133. * switch_to(n) should switch tasks to task nr n, first
  134. * checking that n isn't the current task, in which case it does nothing.
  135. */
  136. extern asmlinkage void *resume(void *last, void *next, void *next_ti);
  137. struct task_struct;
  138. #define switch_to(prev,next,last) \
  139. do { \
  140. (last) = resume(prev, next, next->thread_info); \
  141. } while(0)
  142. #define ROT_IN_PIECES \
  143. " .set noreorder \n" \
  144. " .set reorder \n"
  145. static inline unsigned long __xchg_u32(volatile int * m, unsigned int val)
  146. {
  147. __u32 retval;
  148. if (cpu_has_llsc && R10000_LLSC_WAR) {
  149. unsigned long dummy;
  150. __asm__ __volatile__(
  151. "1: ll %0, %3 # xchg_u32 \n"
  152. " move %2, %z4 \n"
  153. " sc %2, %1 \n"
  154. " beqzl %2, 1b \n"
  155. ROT_IN_PIECES
  156. #ifdef CONFIG_SMP
  157. " sync \n"
  158. #endif
  159. : "=&r" (retval), "=m" (*m), "=&r" (dummy)
  160. : "R" (*m), "Jr" (val)
  161. : "memory");
  162. } else if (cpu_has_llsc) {
  163. unsigned long dummy;
  164. __asm__ __volatile__(
  165. "1: ll %0, %3 # xchg_u32 \n"
  166. " move %2, %z4 \n"
  167. " sc %2, %1 \n"
  168. " beqz %2, 1b \n"
  169. #ifdef CONFIG_SMP
  170. " sync \n"
  171. #endif
  172. : "=&r" (retval), "=m" (*m), "=&r" (dummy)
  173. : "R" (*m), "Jr" (val)
  174. : "memory");
  175. } else {
  176. unsigned long flags;
  177. local_irq_save(flags);
  178. retval = *m;
  179. *m = val;
  180. local_irq_restore(flags); /* implies memory barrier */
  181. }
  182. return retval;
  183. }
  184. #ifdef CONFIG_MIPS64
  185. static inline __u64 __xchg_u64(volatile __u64 * m, __u64 val)
  186. {
  187. __u64 retval;
  188. if (cpu_has_llsc && R10000_LLSC_WAR) {
  189. unsigned long dummy;
  190. __asm__ __volatile__(
  191. "1: lld %0, %3 # xchg_u64 \n"
  192. " move %2, %z4 \n"
  193. " scd %2, %1 \n"
  194. " beqzl %2, 1b \n"
  195. ROT_IN_PIECES
  196. #ifdef CONFIG_SMP
  197. " sync \n"
  198. #endif
  199. : "=&r" (retval), "=m" (*m), "=&r" (dummy)
  200. : "R" (*m), "Jr" (val)
  201. : "memory");
  202. } else if (cpu_has_llsc) {
  203. unsigned long dummy;
  204. __asm__ __volatile__(
  205. "1: lld %0, %3 # xchg_u64 \n"
  206. " move %2, %z4 \n"
  207. " scd %2, %1 \n"
  208. " beqz %2, 1b \n"
  209. #ifdef CONFIG_SMP
  210. " sync \n"
  211. #endif
  212. : "=&r" (retval), "=m" (*m), "=&r" (dummy)
  213. : "R" (*m), "Jr" (val)
  214. : "memory");
  215. } else {
  216. unsigned long flags;
  217. local_irq_save(flags);
  218. retval = *m;
  219. *m = val;
  220. local_irq_restore(flags); /* implies memory barrier */
  221. }
  222. return retval;
  223. }
  224. #else
  225. extern __u64 __xchg_u64_unsupported_on_32bit_kernels(volatile __u64 * m, __u64 val);
  226. #define __xchg_u64 __xchg_u64_unsupported_on_32bit_kernels
  227. #endif
  228. /* This function doesn't exist, so you'll get a linker error
  229. if something tries to do an invalid xchg(). */
  230. extern void __xchg_called_with_bad_pointer(void);
  231. static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
  232. {
  233. switch (size) {
  234. case 4:
  235. return __xchg_u32(ptr, x);
  236. case 8:
  237. return __xchg_u64(ptr, x);
  238. }
  239. __xchg_called_with_bad_pointer();
  240. return x;
  241. }
  242. #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  243. #define tas(ptr) (xchg((ptr),1))
  244. #define __HAVE_ARCH_CMPXCHG 1
  245. static inline unsigned long __cmpxchg_u32(volatile int * m, unsigned long old,
  246. unsigned long new)
  247. {
  248. __u32 retval;
  249. if (cpu_has_llsc && R10000_LLSC_WAR) {
  250. __asm__ __volatile__(
  251. " .set noat \n"
  252. "1: ll %0, %2 # __cmpxchg_u32 \n"
  253. " bne %0, %z3, 2f \n"
  254. " move $1, %z4 \n"
  255. " sc $1, %1 \n"
  256. " beqzl $1, 1b \n"
  257. ROT_IN_PIECES
  258. #ifdef CONFIG_SMP
  259. " sync \n"
  260. #endif
  261. "2: \n"
  262. " .set at \n"
  263. : "=&r" (retval), "=m" (*m)
  264. : "R" (*m), "Jr" (old), "Jr" (new)
  265. : "memory");
  266. } else if (cpu_has_llsc) {
  267. __asm__ __volatile__(
  268. " .set noat \n"
  269. "1: ll %0, %2 # __cmpxchg_u32 \n"
  270. " bne %0, %z3, 2f \n"
  271. " move $1, %z4 \n"
  272. " sc $1, %1 \n"
  273. " beqz $1, 1b \n"
  274. #ifdef CONFIG_SMP
  275. " sync \n"
  276. #endif
  277. "2: \n"
  278. " .set at \n"
  279. : "=&r" (retval), "=m" (*m)
  280. : "R" (*m), "Jr" (old), "Jr" (new)
  281. : "memory");
  282. } else {
  283. unsigned long flags;
  284. local_irq_save(flags);
  285. retval = *m;
  286. if (retval == old)
  287. *m = new;
  288. local_irq_restore(flags); /* implies memory barrier */
  289. }
  290. return retval;
  291. }
  292. #ifdef CONFIG_MIPS64
  293. static inline unsigned long __cmpxchg_u64(volatile int * m, unsigned long old,
  294. unsigned long new)
  295. {
  296. __u64 retval;
  297. if (cpu_has_llsc) {
  298. __asm__ __volatile__(
  299. " .set noat \n"
  300. "1: lld %0, %2 # __cmpxchg_u64 \n"
  301. " bne %0, %z3, 2f \n"
  302. " move $1, %z4 \n"
  303. " scd $1, %1 \n"
  304. " beqzl $1, 1b \n"
  305. ROT_IN_PIECES
  306. #ifdef CONFIG_SMP
  307. " sync \n"
  308. #endif
  309. "2: \n"
  310. " .set at \n"
  311. : "=&r" (retval), "=m" (*m)
  312. : "R" (*m), "Jr" (old), "Jr" (new)
  313. : "memory");
  314. } else if (cpu_has_llsc) {
  315. __asm__ __volatile__(
  316. " .set noat \n"
  317. "1: lld %0, %2 # __cmpxchg_u64 \n"
  318. " bne %0, %z3, 2f \n"
  319. " move $1, %z4 \n"
  320. " scd $1, %1 \n"
  321. " beqz $1, 1b \n"
  322. #ifdef CONFIG_SMP
  323. " sync \n"
  324. #endif
  325. "2: \n"
  326. " .set at \n"
  327. : "=&r" (retval), "=m" (*m)
  328. : "R" (*m), "Jr" (old), "Jr" (new)
  329. : "memory");
  330. } else {
  331. unsigned long flags;
  332. local_irq_save(flags);
  333. retval = *m;
  334. if (retval == old)
  335. *m = new;
  336. local_irq_restore(flags); /* implies memory barrier */
  337. }
  338. return retval;
  339. }
  340. #else
  341. extern unsigned long __cmpxchg_u64_unsupported_on_32bit_kernels(
  342. volatile int * m, unsigned long old, unsigned long new);
  343. #define __cmpxchg_u64 __cmpxchg_u64_unsupported_on_32bit_kernels
  344. #endif
  345. /* This function doesn't exist, so you'll get a linker error
  346. if something tries to do an invalid cmpxchg(). */
  347. extern void __cmpxchg_called_with_bad_pointer(void);
  348. static inline unsigned long __cmpxchg(volatile void * ptr, unsigned long old,
  349. unsigned long new, int size)
  350. {
  351. switch (size) {
  352. case 4:
  353. return __cmpxchg_u32(ptr, old, new);
  354. case 8:
  355. return __cmpxchg_u64(ptr, old, new);
  356. }
  357. __cmpxchg_called_with_bad_pointer();
  358. return old;
  359. }
  360. #define cmpxchg(ptr,old,new) ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(old), (unsigned long)(new),sizeof(*(ptr))))
  361. extern void *set_except_vector(int n, void *addr);
  362. extern void per_cpu_trap_init(void);
  363. extern NORET_TYPE void __die(const char *, struct pt_regs *, const char *file,
  364. const char *func, unsigned long line);
  365. extern void __die_if_kernel(const char *, struct pt_regs *, const char *file,
  366. const char *func, unsigned long line);
  367. #define die(msg, regs) \
  368. __die(msg, regs, __FILE__ ":", __FUNCTION__, __LINE__)
  369. #define die_if_kernel(msg, regs) \
  370. __die_if_kernel(msg, regs, __FILE__ ":", __FUNCTION__, __LINE__)
  371. extern int stop_a_enabled;
  372. /*
  373. * See include/asm-ia64/system.h; prevents deadlock on SMP
  374. * systems.
  375. */
  376. #define __ARCH_WANT_UNLOCKED_CTXSW
  377. #define arch_align_stack(x) (x)
  378. #endif /* _ASM_SYSTEM_H */