system.h 12 KB

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