system.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 1994 - 1999 by Ralf Baechle
  4. * Copyright (C) 1996 by Paul M. Antoine
  5. * Copyright (C) 1994 - 1999 by Ralf Baechle
  6. *
  7. * Changed set_except_vector declaration to allow return of previous
  8. * vector address value - necessary for "borrowing" vectors.
  9. *
  10. * Kevin D. Kissell, kevink@mips.org and Carsten Langgaard, carstenl@mips.com
  11. * Copyright (C) 2000 MIPS Technologies, Inc.
  12. */
  13. #ifndef _ASM_SYSTEM_H
  14. #define _ASM_SYSTEM_H
  15. #include <asm/asm.h>
  16. #include <asm/sgidefs.h>
  17. #include <asm/ptrace.h>
  18. #include <linux/stringify.h>
  19. #if 0
  20. #include <linux/kernel.h>
  21. #endif
  22. static __inline__ void
  23. __sti(void)
  24. {
  25. __asm__ __volatile__(
  26. ".set\tpush\n\t"
  27. ".set\treorder\n\t"
  28. ".set\tnoat\n\t"
  29. "mfc0\t$1,$12\n\t"
  30. "ori\t$1,0x1f\n\t"
  31. "xori\t$1,0x1e\n\t"
  32. "mtc0\t$1,$12\n\t"
  33. ".set\tpop\n\t"
  34. : /* no outputs */
  35. : /* no inputs */
  36. : "$1", "memory");
  37. }
  38. /*
  39. * For cli() we have to insert nops to make shure that the new value
  40. * has actually arrived in the status register before the end of this
  41. * macro.
  42. * R4000/R4400 need three nops, the R4600 two nops and the R10000 needs
  43. * no nops at all.
  44. */
  45. static __inline__ void
  46. __cli(void)
  47. {
  48. __asm__ __volatile__(
  49. ".set\tpush\n\t"
  50. ".set\treorder\n\t"
  51. ".set\tnoat\n\t"
  52. "mfc0\t$1,$12\n\t"
  53. "ori\t$1,1\n\t"
  54. "xori\t$1,1\n\t"
  55. ".set\tnoreorder\n\t"
  56. "mtc0\t$1,$12\n\t"
  57. "nop\n\t"
  58. "nop\n\t"
  59. "nop\n\t"
  60. ".set\tpop\n\t"
  61. : /* no outputs */
  62. : /* no inputs */
  63. : "$1", "memory");
  64. }
  65. #define __save_flags(x) \
  66. __asm__ __volatile__( \
  67. ".set\tpush\n\t" \
  68. ".set\treorder\n\t" \
  69. "mfc0\t%0,$12\n\t" \
  70. ".set\tpop\n\t" \
  71. : "=r" (x))
  72. #define __save_and_cli(x) \
  73. __asm__ __volatile__( \
  74. ".set\tpush\n\t" \
  75. ".set\treorder\n\t" \
  76. ".set\tnoat\n\t" \
  77. "mfc0\t%0,$12\n\t" \
  78. "ori\t$1,%0,1\n\t" \
  79. "xori\t$1,1\n\t" \
  80. ".set\tnoreorder\n\t" \
  81. "mtc0\t$1,$12\n\t" \
  82. "nop\n\t" \
  83. "nop\n\t" \
  84. "nop\n\t" \
  85. ".set\tpop\n\t" \
  86. : "=r" (x) \
  87. : /* no inputs */ \
  88. : "$1", "memory")
  89. #define __restore_flags(flags) \
  90. do { \
  91. unsigned long __tmp1; \
  92. \
  93. __asm__ __volatile__( \
  94. ".set\tnoreorder\t\t\t# __restore_flags\n\t" \
  95. ".set\tnoat\n\t" \
  96. "mfc0\t$1, $12\n\t" \
  97. "andi\t%0, 1\n\t" \
  98. "ori\t$1, 1\n\t" \
  99. "xori\t$1, 1\n\t" \
  100. "or\t%0, $1\n\t" \
  101. "mtc0\t%0, $12\n\t" \
  102. "nop\n\t" \
  103. "nop\n\t" \
  104. "nop\n\t" \
  105. ".set\tat\n\t" \
  106. ".set\treorder" \
  107. : "=r" (__tmp1) \
  108. : "0" (flags) \
  109. : "$1", "memory"); \
  110. } while(0)
  111. #ifdef CONFIG_SMP
  112. extern void __global_sti(void);
  113. extern void __global_cli(void);
  114. extern unsigned long __global_save_flags(void);
  115. extern void __global_restore_flags(unsigned long);
  116. # define sti() __global_sti()
  117. # define cli() __global_cli()
  118. # define save_flags(x) do { x = __global_save_flags(); } while (0)
  119. # define restore_flags(x) __global_restore_flags(x)
  120. # define save_and_cli(x) do { save_flags(x); cli(); } while(0)
  121. #else /* Single processor */
  122. # define sti() __sti()
  123. # define cli() __cli()
  124. # define save_flags(x) __save_flags(x)
  125. # define save_and_cli(x) __save_and_cli(x)
  126. # define restore_flags(x) __restore_flags(x)
  127. #endif /* SMP */
  128. /* For spinlocks etc */
  129. #define local_irq_save(x) __save_and_cli(x);
  130. #define local_irq_restore(x) __restore_flags(x);
  131. #define local_irq_disable() __cli();
  132. #define local_irq_enable() __sti();
  133. /*
  134. * These are probably defined overly paranoid ...
  135. */
  136. #ifdef CONFIG_CPU_HAS_WB
  137. #include <asm/wbflush.h>
  138. #define rmb() do { } while(0)
  139. #define wmb() wbflush()
  140. #define mb() wbflush()
  141. #else /* CONFIG_CPU_HAS_WB */
  142. #define mb() \
  143. __asm__ __volatile__( \
  144. "# prevent instructions being moved around\n\t" \
  145. ".set\tnoreorder\n\t" \
  146. "# 8 nops to fool the R4400 pipeline\n\t" \
  147. "nop;nop;nop;nop;nop;nop;nop;nop\n\t" \
  148. ".set\treorder" \
  149. : /* no output */ \
  150. : /* no input */ \
  151. : "memory")
  152. #define rmb() mb()
  153. #define wmb() mb()
  154. #endif /* CONFIG_CPU_HAS_WB */
  155. #ifdef CONFIG_SMP
  156. #define smp_mb() mb()
  157. #define smp_rmb() rmb()
  158. #define smp_wmb() wmb()
  159. #else
  160. #define smp_mb() barrier()
  161. #define smp_rmb() barrier()
  162. #define smp_wmb() barrier()
  163. #endif
  164. #define set_mb(var, value) \
  165. do { var = value; mb(); } while (0)
  166. #define set_wmb(var, value) \
  167. do { var = value; wmb(); } while (0)
  168. #if !defined (_LANGUAGE_ASSEMBLY)
  169. /*
  170. * switch_to(n) should switch tasks to task nr n, first
  171. * checking that n isn't the current task, in which case it does nothing.
  172. */
  173. #if 0
  174. extern asmlinkage void *resume(void *last, void *next);
  175. #endif
  176. #endif /* !defined (_LANGUAGE_ASSEMBLY) */
  177. #define prepare_to_switch() do { } while(0)
  178. #define switch_to(prev,next,last) \
  179. do { \
  180. (last) = resume(prev, next); \
  181. } while(0)
  182. /*
  183. * For 32 and 64 bit operands we can take advantage of ll and sc.
  184. * FIXME: This doesn't work for R3000 machines.
  185. */
  186. static __inline__ unsigned long xchg_u32(volatile int * m, unsigned long val)
  187. {
  188. #ifdef CONFIG_CPU_HAS_LLSC
  189. unsigned long dummy;
  190. __asm__ __volatile__(
  191. ".set\tnoreorder\t\t\t# xchg_u32\n\t"
  192. ".set\tnoat\n\t"
  193. "ll\t%0, %3\n"
  194. "1:\tmove\t$1, %2\n\t"
  195. "sc\t$1, %1\n\t"
  196. "beqzl\t$1, 1b\n\t"
  197. " ll\t%0, %3\n\t"
  198. ".set\tat\n\t"
  199. ".set\treorder"
  200. : "=r" (val), "=o" (*m), "=r" (dummy)
  201. : "o" (*m), "2" (val)
  202. : "memory");
  203. return val;
  204. #else
  205. unsigned long flags, retval;
  206. save_flags(flags);
  207. cli();
  208. retval = *m;
  209. *m = val;
  210. restore_flags(flags);
  211. return retval;
  212. #endif /* Processor-dependent optimization */
  213. }
  214. #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  215. #define tas(ptr) (xchg((ptr),1))
  216. static __inline__ unsigned long
  217. __xchg(unsigned long x, volatile void * ptr, int size)
  218. {
  219. switch (size) {
  220. case 4:
  221. return xchg_u32(ptr, x);
  222. }
  223. return x;
  224. }
  225. extern void *set_except_vector(int n, void *addr);
  226. extern void __die(const char *, struct pt_regs *, const char *where,
  227. unsigned long line) __attribute__((noreturn));
  228. extern void __die_if_kernel(const char *, struct pt_regs *, const char *where,
  229. unsigned long line);
  230. #define die(msg, regs) \
  231. __die(msg, regs, __FILE__ ":"__FUNCTION__, __LINE__)
  232. #define die_if_kernel(msg, regs) \
  233. __die_if_kernel(msg, regs, __FILE__ ":"__FUNCTION__, __LINE__)
  234. static inline void execution_hazard_barrier(void)
  235. {
  236. __asm__ __volatile__(
  237. ".set noreorder\n"
  238. "ehb\n"
  239. ".set reorder");
  240. }
  241. static inline void instruction_hazard_barrier(void)
  242. {
  243. unsigned long tmp;
  244. asm volatile(
  245. __stringify(PTR_LA) "\t%0, 1f\n"
  246. " jr.hb %0\n"
  247. "1: .insn"
  248. : "=&r"(tmp));
  249. }
  250. #endif /* _ASM_SYSTEM_H */