system.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. #ifdef CONFIG_MIPS_MT_FPAFF
  140. /*
  141. * Handle the scheduler resume end of FPU affinity management. We do this
  142. * inline to try to keep the overhead down. If we have been forced to run on
  143. * a "CPU" with an FPU because of a previous high level of FP computation,
  144. * but did not actually use the FPU during the most recent time-slice (CU1
  145. * isn't set), we undo the restriction on cpus_allowed.
  146. *
  147. * We're not calling set_cpus_allowed() here, because we have no need to
  148. * force prompt migration - we're already switching the current CPU to a
  149. * different thread.
  150. */
  151. #define switch_to(prev,next,last) \
  152. do { \
  153. if (cpu_has_fpu && \
  154. (prev->thread.mflags & MF_FPUBOUND) && \
  155. (!(KSTK_STATUS(prev) & ST0_CU1))) { \
  156. prev->thread.mflags &= ~MF_FPUBOUND; \
  157. prev->cpus_allowed = prev->thread.user_cpus_allowed; \
  158. } \
  159. if (cpu_has_dsp) \
  160. __save_dsp(prev); \
  161. next->thread.emulated_fp = 0; \
  162. (last) = resume(prev, next, next->thread_info); \
  163. if (cpu_has_dsp) \
  164. __restore_dsp(current); \
  165. } while(0)
  166. #else
  167. #define switch_to(prev,next,last) \
  168. do { \
  169. if (cpu_has_dsp) \
  170. __save_dsp(prev); \
  171. (last) = resume(prev, next, task_thread_info(next)); \
  172. if (cpu_has_dsp) \
  173. __restore_dsp(current); \
  174. } while(0)
  175. #endif
  176. /*
  177. * On SMP systems, when the scheduler does migration-cost autodetection,
  178. * it needs a way to flush as much of the CPU's caches as possible.
  179. *
  180. * TODO: fill this in!
  181. */
  182. static inline void sched_cacheflush(void)
  183. {
  184. }
  185. static inline unsigned long __xchg_u32(volatile int * m, unsigned int val)
  186. {
  187. __u32 retval;
  188. if (cpu_has_llsc && R10000_LLSC_WAR) {
  189. unsigned long dummy;
  190. __asm__ __volatile__(
  191. " .set mips3 \n"
  192. "1: ll %0, %3 # xchg_u32 \n"
  193. " .set mips0 \n"
  194. " move %2, %z4 \n"
  195. " .set mips3 \n"
  196. " sc %2, %1 \n"
  197. " beqzl %2, 1b \n"
  198. #ifdef CONFIG_SMP
  199. " sync \n"
  200. #endif
  201. " .set mips0 \n"
  202. : "=&r" (retval), "=m" (*m), "=&r" (dummy)
  203. : "R" (*m), "Jr" (val)
  204. : "memory");
  205. } else if (cpu_has_llsc) {
  206. unsigned long dummy;
  207. __asm__ __volatile__(
  208. " .set mips3 \n"
  209. "1: ll %0, %3 # xchg_u32 \n"
  210. " .set mips0 \n"
  211. " move %2, %z4 \n"
  212. " .set mips3 \n"
  213. " sc %2, %1 \n"
  214. " beqz %2, 1b \n"
  215. #ifdef CONFIG_SMP
  216. " sync \n"
  217. #endif
  218. " .set mips0 \n"
  219. : "=&r" (retval), "=m" (*m), "=&r" (dummy)
  220. : "R" (*m), "Jr" (val)
  221. : "memory");
  222. } else {
  223. unsigned long flags;
  224. local_irq_save(flags);
  225. retval = *m;
  226. *m = val;
  227. local_irq_restore(flags); /* implies memory barrier */
  228. }
  229. return retval;
  230. }
  231. #ifdef CONFIG_64BIT
  232. static inline __u64 __xchg_u64(volatile __u64 * m, __u64 val)
  233. {
  234. __u64 retval;
  235. if (cpu_has_llsc && R10000_LLSC_WAR) {
  236. unsigned long dummy;
  237. __asm__ __volatile__(
  238. " .set mips3 \n"
  239. "1: lld %0, %3 # xchg_u64 \n"
  240. " move %2, %z4 \n"
  241. " scd %2, %1 \n"
  242. " beqzl %2, 1b \n"
  243. #ifdef CONFIG_SMP
  244. " sync \n"
  245. #endif
  246. " .set mips0 \n"
  247. : "=&r" (retval), "=m" (*m), "=&r" (dummy)
  248. : "R" (*m), "Jr" (val)
  249. : "memory");
  250. } else if (cpu_has_llsc) {
  251. unsigned long dummy;
  252. __asm__ __volatile__(
  253. " .set mips3 \n"
  254. "1: lld %0, %3 # xchg_u64 \n"
  255. " move %2, %z4 \n"
  256. " scd %2, %1 \n"
  257. " beqz %2, 1b \n"
  258. #ifdef CONFIG_SMP
  259. " sync \n"
  260. #endif
  261. " .set mips0 \n"
  262. : "=&r" (retval), "=m" (*m), "=&r" (dummy)
  263. : "R" (*m), "Jr" (val)
  264. : "memory");
  265. } else {
  266. unsigned long flags;
  267. local_irq_save(flags);
  268. retval = *m;
  269. *m = val;
  270. local_irq_restore(flags); /* implies memory barrier */
  271. }
  272. return retval;
  273. }
  274. #else
  275. extern __u64 __xchg_u64_unsupported_on_32bit_kernels(volatile __u64 * m, __u64 val);
  276. #define __xchg_u64 __xchg_u64_unsupported_on_32bit_kernels
  277. #endif
  278. /* This function doesn't exist, so you'll get a linker error
  279. if something tries to do an invalid xchg(). */
  280. extern void __xchg_called_with_bad_pointer(void);
  281. static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
  282. {
  283. switch (size) {
  284. case 4:
  285. return __xchg_u32(ptr, x);
  286. case 8:
  287. return __xchg_u64(ptr, x);
  288. }
  289. __xchg_called_with_bad_pointer();
  290. return x;
  291. }
  292. #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  293. #define tas(ptr) (xchg((ptr),1))
  294. #define __HAVE_ARCH_CMPXCHG 1
  295. static inline unsigned long __cmpxchg_u32(volatile int * m, unsigned long old,
  296. unsigned long new)
  297. {
  298. __u32 retval;
  299. if (cpu_has_llsc && R10000_LLSC_WAR) {
  300. __asm__ __volatile__(
  301. " .set push \n"
  302. " .set noat \n"
  303. " .set mips3 \n"
  304. "1: ll %0, %2 # __cmpxchg_u32 \n"
  305. " bne %0, %z3, 2f \n"
  306. " .set mips0 \n"
  307. " move $1, %z4 \n"
  308. " .set mips3 \n"
  309. " sc $1, %1 \n"
  310. " beqzl $1, 1b \n"
  311. #ifdef CONFIG_SMP
  312. " sync \n"
  313. #endif
  314. "2: \n"
  315. " .set pop \n"
  316. : "=&r" (retval), "=R" (*m)
  317. : "R" (*m), "Jr" (old), "Jr" (new)
  318. : "memory");
  319. } else if (cpu_has_llsc) {
  320. __asm__ __volatile__(
  321. " .set push \n"
  322. " .set noat \n"
  323. " .set mips3 \n"
  324. "1: ll %0, %2 # __cmpxchg_u32 \n"
  325. " bne %0, %z3, 2f \n"
  326. " .set mips0 \n"
  327. " move $1, %z4 \n"
  328. " .set mips3 \n"
  329. " sc $1, %1 \n"
  330. " beqz $1, 1b \n"
  331. #ifdef CONFIG_SMP
  332. " sync \n"
  333. #endif
  334. "2: \n"
  335. " .set pop \n"
  336. : "=&r" (retval), "=R" (*m)
  337. : "R" (*m), "Jr" (old), "Jr" (new)
  338. : "memory");
  339. } else {
  340. unsigned long flags;
  341. local_irq_save(flags);
  342. retval = *m;
  343. if (retval == old)
  344. *m = new;
  345. local_irq_restore(flags); /* implies memory barrier */
  346. }
  347. return retval;
  348. }
  349. #ifdef CONFIG_64BIT
  350. static inline unsigned long __cmpxchg_u64(volatile int * m, unsigned long old,
  351. unsigned long new)
  352. {
  353. __u64 retval;
  354. if (cpu_has_llsc) {
  355. __asm__ __volatile__(
  356. " .set push \n"
  357. " .set noat \n"
  358. " .set mips3 \n"
  359. "1: lld %0, %2 # __cmpxchg_u64 \n"
  360. " bne %0, %z3, 2f \n"
  361. " move $1, %z4 \n"
  362. " scd $1, %1 \n"
  363. " beqzl $1, 1b \n"
  364. #ifdef CONFIG_SMP
  365. " sync \n"
  366. #endif
  367. "2: \n"
  368. " .set pop \n"
  369. : "=&r" (retval), "=R" (*m)
  370. : "R" (*m), "Jr" (old), "Jr" (new)
  371. : "memory");
  372. } else if (cpu_has_llsc) {
  373. __asm__ __volatile__(
  374. " .set push \n"
  375. " .set noat \n"
  376. " .set mips3 \n"
  377. "1: lld %0, %2 # __cmpxchg_u64 \n"
  378. " bne %0, %z3, 2f \n"
  379. " move $1, %z4 \n"
  380. " scd $1, %1 \n"
  381. " beqz $1, 1b \n"
  382. #ifdef CONFIG_SMP
  383. " sync \n"
  384. #endif
  385. "2: \n"
  386. " .set pop \n"
  387. : "=&r" (retval), "=R" (*m)
  388. : "R" (*m), "Jr" (old), "Jr" (new)
  389. : "memory");
  390. } else {
  391. unsigned long flags;
  392. local_irq_save(flags);
  393. retval = *m;
  394. if (retval == old)
  395. *m = new;
  396. local_irq_restore(flags); /* implies memory barrier */
  397. }
  398. return retval;
  399. }
  400. #else
  401. extern unsigned long __cmpxchg_u64_unsupported_on_32bit_kernels(
  402. volatile int * m, unsigned long old, unsigned long new);
  403. #define __cmpxchg_u64 __cmpxchg_u64_unsupported_on_32bit_kernels
  404. #endif
  405. /* This function doesn't exist, so you'll get a linker error
  406. if something tries to do an invalid cmpxchg(). */
  407. extern void __cmpxchg_called_with_bad_pointer(void);
  408. static inline unsigned long __cmpxchg(volatile void * ptr, unsigned long old,
  409. unsigned long new, int size)
  410. {
  411. switch (size) {
  412. case 4:
  413. return __cmpxchg_u32(ptr, old, new);
  414. case 8:
  415. return __cmpxchg_u64(ptr, old, new);
  416. }
  417. __cmpxchg_called_with_bad_pointer();
  418. return old;
  419. }
  420. #define cmpxchg(ptr,old,new) ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(old), (unsigned long)(new),sizeof(*(ptr))))
  421. extern void set_handler (unsigned long offset, void *addr, unsigned long len);
  422. extern void set_uncached_handler (unsigned long offset, void *addr, unsigned long len);
  423. extern void *set_vi_handler (int n, void *addr);
  424. extern void *set_except_vector(int n, void *addr);
  425. extern unsigned long ebase;
  426. extern void per_cpu_trap_init(void);
  427. extern NORET_TYPE void die(const char *, struct pt_regs *);
  428. static inline void die_if_kernel(const char *str, struct pt_regs *regs)
  429. {
  430. if (unlikely(!user_mode(regs)))
  431. die(str, regs);
  432. }
  433. extern int stop_a_enabled;
  434. /*
  435. * See include/asm-ia64/system.h; prevents deadlock on SMP
  436. * systems.
  437. */
  438. #define __ARCH_WANT_UNLOCKED_CTXSW
  439. #define arch_align_stack(x) (x)
  440. #endif /* _ASM_SYSTEM_H */