system.h 11 KB

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