system.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* $Id: system.h,v 1.69 2002/02/09 19:49:31 davem Exp $ */
  2. #ifndef __SPARC64_SYSTEM_H
  3. #define __SPARC64_SYSTEM_H
  4. #include <linux/config.h>
  5. #include <asm/ptrace.h>
  6. #include <asm/processor.h>
  7. #include <asm/visasm.h>
  8. #ifndef __ASSEMBLY__
  9. /*
  10. * Sparc (general) CPU types
  11. */
  12. enum sparc_cpu {
  13. sun4 = 0x00,
  14. sun4c = 0x01,
  15. sun4m = 0x02,
  16. sun4d = 0x03,
  17. sun4e = 0x04,
  18. sun4u = 0x05, /* V8 ploos ploos */
  19. sun_unknown = 0x06,
  20. ap1000 = 0x07, /* almost a sun4m */
  21. };
  22. #define sparc_cpu_model sun4u
  23. /* This cannot ever be a sun4c nor sun4 :) That's just history. */
  24. #define ARCH_SUN4C_SUN4 0
  25. #define ARCH_SUN4 0
  26. #endif
  27. #define setipl(__new_ipl) \
  28. __asm__ __volatile__("wrpr %0, %%pil" : : "r" (__new_ipl) : "memory")
  29. #define local_irq_disable() \
  30. __asm__ __volatile__("wrpr 15, %%pil" : : : "memory")
  31. #define local_irq_enable() \
  32. __asm__ __volatile__("wrpr 0, %%pil" : : : "memory")
  33. #define getipl() \
  34. ({ unsigned long retval; __asm__ __volatile__("rdpr %%pil, %0" : "=r" (retval)); retval; })
  35. #define swap_pil(__new_pil) \
  36. ({ unsigned long retval; \
  37. __asm__ __volatile__("rdpr %%pil, %0\n\t" \
  38. "wrpr %1, %%pil" \
  39. : "=&r" (retval) \
  40. : "r" (__new_pil) \
  41. : "memory"); \
  42. retval; \
  43. })
  44. #define read_pil_and_cli() \
  45. ({ unsigned long retval; \
  46. __asm__ __volatile__("rdpr %%pil, %0\n\t" \
  47. "wrpr 15, %%pil" \
  48. : "=r" (retval) \
  49. : : "memory"); \
  50. retval; \
  51. })
  52. #define local_save_flags(flags) ((flags) = getipl())
  53. #define local_irq_save(flags) ((flags) = read_pil_and_cli())
  54. #define local_irq_restore(flags) setipl((flags))
  55. /* On sparc64 IRQ flags are the PIL register. A value of zero
  56. * means all interrupt levels are enabled, any other value means
  57. * only IRQ levels greater than that value will be received.
  58. * Consequently this means that the lowest IRQ level is one.
  59. */
  60. #define irqs_disabled() \
  61. ({ unsigned long flags; \
  62. local_save_flags(flags);\
  63. (flags > 0); \
  64. })
  65. #define nop() __asm__ __volatile__ ("nop")
  66. #define membar(type) __asm__ __volatile__ ("membar " type : : : "memory")
  67. #define mb() \
  68. membar("#LoadLoad | #LoadStore | #StoreStore | #StoreLoad")
  69. #define rmb() membar("#LoadLoad")
  70. #define wmb() membar("#StoreStore")
  71. #define read_barrier_depends() do { } while(0)
  72. #define set_mb(__var, __value) \
  73. do { __var = __value; membar("#StoreLoad | #StoreStore"); } while(0)
  74. #define set_wmb(__var, __value) \
  75. do { __var = __value; membar("#StoreStore"); } while(0)
  76. #ifdef CONFIG_SMP
  77. #define smp_mb() mb()
  78. #define smp_rmb() rmb()
  79. #define smp_wmb() wmb()
  80. #define smp_read_barrier_depends() read_barrier_depends()
  81. #else
  82. #define smp_mb() __asm__ __volatile__("":::"memory")
  83. #define smp_rmb() __asm__ __volatile__("":::"memory")
  84. #define smp_wmb() __asm__ __volatile__("":::"memory")
  85. #define smp_read_barrier_depends() do { } while(0)
  86. #endif
  87. #define flushi(addr) __asm__ __volatile__ ("flush %0" : : "r" (addr) : "memory")
  88. #define flushw_all() __asm__ __volatile__("flushw")
  89. /* Performance counter register access. */
  90. #define read_pcr(__p) __asm__ __volatile__("rd %%pcr, %0" : "=r" (__p))
  91. #define write_pcr(__p) __asm__ __volatile__("wr %0, 0x0, %%pcr" : : "r" (__p))
  92. #define read_pic(__p) __asm__ __volatile__("rd %%pic, %0" : "=r" (__p))
  93. /* Blackbird errata workaround. See commentary in
  94. * arch/sparc64/kernel/smp.c:smp_percpu_timer_interrupt()
  95. * for more information.
  96. */
  97. #define reset_pic() \
  98. __asm__ __volatile__("ba,pt %xcc, 99f\n\t" \
  99. ".align 64\n" \
  100. "99:wr %g0, 0x0, %pic\n\t" \
  101. "rd %pic, %g0")
  102. #ifndef __ASSEMBLY__
  103. extern void sun_do_break(void);
  104. extern int serial_console;
  105. extern int stop_a_enabled;
  106. static __inline__ int con_is_present(void)
  107. {
  108. return serial_console ? 0 : 1;
  109. }
  110. extern void synchronize_user_stack(void);
  111. extern void __flushw_user(void);
  112. #define flushw_user() __flushw_user()
  113. #define flush_user_windows flushw_user
  114. #define flush_register_windows flushw_all
  115. /* Don't hold the runqueue lock over context switch */
  116. #define __ARCH_WANT_UNLOCKED_CTXSW
  117. #define prepare_arch_switch(next) \
  118. do { \
  119. flushw_all(); \
  120. } while (0)
  121. /* See what happens when you design the chip correctly?
  122. *
  123. * We tell gcc we clobber all non-fixed-usage registers except
  124. * for l0/l1. It will use one for 'next' and the other to hold
  125. * the output value of 'last'. 'next' is not referenced again
  126. * past the invocation of switch_to in the scheduler, so we need
  127. * not preserve it's value. Hairy, but it lets us remove 2 loads
  128. * and 2 stores in this critical code path. -DaveM
  129. */
  130. #if __GNUC__ >= 3
  131. #define EXTRA_CLOBBER ,"%l1"
  132. #else
  133. #define EXTRA_CLOBBER
  134. #endif
  135. #define switch_to(prev, next, last) \
  136. do { if (test_thread_flag(TIF_PERFCTR)) { \
  137. unsigned long __tmp; \
  138. read_pcr(__tmp); \
  139. current_thread_info()->pcr_reg = __tmp; \
  140. read_pic(__tmp); \
  141. current_thread_info()->kernel_cntd0 += (unsigned int)(__tmp);\
  142. current_thread_info()->kernel_cntd1 += ((__tmp) >> 32); \
  143. } \
  144. flush_tlb_pending(); \
  145. save_and_clear_fpu(); \
  146. /* If you are tempted to conditionalize the following */ \
  147. /* so that ASI is only written if it changes, think again. */ \
  148. __asm__ __volatile__("wr %%g0, %0, %%asi" \
  149. : : "r" (__thread_flag_byte_ptr(next->thread_info)[TI_FLAG_BYTE_CURRENT_DS]));\
  150. __asm__ __volatile__( \
  151. "mov %%g4, %%g7\n\t" \
  152. "wrpr %%g0, 0x95, %%pstate\n\t" \
  153. "stx %%i6, [%%sp + 2047 + 0x70]\n\t" \
  154. "stx %%i7, [%%sp + 2047 + 0x78]\n\t" \
  155. "rdpr %%wstate, %%o5\n\t" \
  156. "stx %%o6, [%%g6 + %3]\n\t" \
  157. "stb %%o5, [%%g6 + %2]\n\t" \
  158. "rdpr %%cwp, %%o5\n\t" \
  159. "stb %%o5, [%%g6 + %5]\n\t" \
  160. "mov %1, %%g6\n\t" \
  161. "ldub [%1 + %5], %%g1\n\t" \
  162. "wrpr %%g1, %%cwp\n\t" \
  163. "ldx [%%g6 + %3], %%o6\n\t" \
  164. "ldub [%%g6 + %2], %%o5\n\t" \
  165. "ldx [%%g6 + %4], %%o7\n\t" \
  166. "mov %%g6, %%l2\n\t" \
  167. "wrpr %%o5, 0x0, %%wstate\n\t" \
  168. "ldx [%%sp + 2047 + 0x70], %%i6\n\t" \
  169. "ldx [%%sp + 2047 + 0x78], %%i7\n\t" \
  170. "wrpr %%g0, 0x94, %%pstate\n\t" \
  171. "mov %%l2, %%g6\n\t" \
  172. "ldx [%%g6 + %7], %%g4\n\t" \
  173. "wrpr %%g0, 0x96, %%pstate\n\t" \
  174. "andcc %%o7, %6, %%g0\n\t" \
  175. "beq,pt %%icc, 1f\n\t" \
  176. " mov %%g7, %0\n\t" \
  177. "b,a ret_from_syscall\n\t" \
  178. "1:\n\t" \
  179. : "=&r" (last) \
  180. : "0" (next->thread_info), \
  181. "i" (TI_WSTATE), "i" (TI_KSP), "i" (TI_FLAGS), "i" (TI_CWP), \
  182. "i" (_TIF_NEWCHILD), "i" (TI_TASK) \
  183. : "cc", \
  184. "g1", "g2", "g3", "g7", \
  185. "l2", "l3", "l4", "l5", "l6", "l7", \
  186. "i0", "i1", "i2", "i3", "i4", "i5", \
  187. "o0", "o1", "o2", "o3", "o4", "o5", "o7" EXTRA_CLOBBER);\
  188. /* If you fuck with this, update ret_from_syscall code too. */ \
  189. if (test_thread_flag(TIF_PERFCTR)) { \
  190. write_pcr(current_thread_info()->pcr_reg); \
  191. reset_pic(); \
  192. } \
  193. } while(0)
  194. static inline unsigned long xchg32(__volatile__ unsigned int *m, unsigned int val)
  195. {
  196. unsigned long tmp1, tmp2;
  197. __asm__ __volatile__(
  198. " membar #StoreLoad | #LoadLoad\n"
  199. " mov %0, %1\n"
  200. "1: lduw [%4], %2\n"
  201. " cas [%4], %2, %0\n"
  202. " cmp %2, %0\n"
  203. " bne,a,pn %%icc, 1b\n"
  204. " mov %1, %0\n"
  205. " membar #StoreLoad | #StoreStore\n"
  206. : "=&r" (val), "=&r" (tmp1), "=&r" (tmp2)
  207. : "0" (val), "r" (m)
  208. : "cc", "memory");
  209. return val;
  210. }
  211. static inline unsigned long xchg64(__volatile__ unsigned long *m, unsigned long val)
  212. {
  213. unsigned long tmp1, tmp2;
  214. __asm__ __volatile__(
  215. " membar #StoreLoad | #LoadLoad\n"
  216. " mov %0, %1\n"
  217. "1: ldx [%4], %2\n"
  218. " casx [%4], %2, %0\n"
  219. " cmp %2, %0\n"
  220. " bne,a,pn %%xcc, 1b\n"
  221. " mov %1, %0\n"
  222. " membar #StoreLoad | #StoreStore\n"
  223. : "=&r" (val), "=&r" (tmp1), "=&r" (tmp2)
  224. : "0" (val), "r" (m)
  225. : "cc", "memory");
  226. return val;
  227. }
  228. #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
  229. #define tas(ptr) (xchg((ptr),1))
  230. extern void __xchg_called_with_bad_pointer(void);
  231. static __inline__ unsigned long __xchg(unsigned long x, __volatile__ void * ptr,
  232. int size)
  233. {
  234. switch (size) {
  235. case 4:
  236. return xchg32(ptr, x);
  237. case 8:
  238. return xchg64(ptr, x);
  239. };
  240. __xchg_called_with_bad_pointer();
  241. return x;
  242. }
  243. extern void die_if_kernel(char *str, struct pt_regs *regs) __attribute__ ((noreturn));
  244. /*
  245. * Atomic compare and exchange. Compare OLD with MEM, if identical,
  246. * store NEW in MEM. Return the initial value in MEM. Success is
  247. * indicated by comparing RETURN with OLD.
  248. */
  249. #define __HAVE_ARCH_CMPXCHG 1
  250. static __inline__ unsigned long
  251. __cmpxchg_u32(volatile int *m, int old, int new)
  252. {
  253. __asm__ __volatile__("membar #StoreLoad | #LoadLoad\n"
  254. "cas [%2], %3, %0\n\t"
  255. "membar #StoreLoad | #StoreStore"
  256. : "=&r" (new)
  257. : "0" (new), "r" (m), "r" (old)
  258. : "memory");
  259. return new;
  260. }
  261. static __inline__ unsigned long
  262. __cmpxchg_u64(volatile long *m, unsigned long old, unsigned long new)
  263. {
  264. __asm__ __volatile__("membar #StoreLoad | #LoadLoad\n"
  265. "casx [%2], %3, %0\n\t"
  266. "membar #StoreLoad | #StoreStore"
  267. : "=&r" (new)
  268. : "0" (new), "r" (m), "r" (old)
  269. : "memory");
  270. return new;
  271. }
  272. /* This function doesn't exist, so you'll get a linker error
  273. if something tries to do an invalid cmpxchg(). */
  274. extern void __cmpxchg_called_with_bad_pointer(void);
  275. static __inline__ unsigned long
  276. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
  277. {
  278. switch (size) {
  279. case 4:
  280. return __cmpxchg_u32(ptr, old, new);
  281. case 8:
  282. return __cmpxchg_u64(ptr, old, new);
  283. }
  284. __cmpxchg_called_with_bad_pointer();
  285. return old;
  286. }
  287. #define cmpxchg(ptr,o,n) \
  288. ({ \
  289. __typeof__(*(ptr)) _o_ = (o); \
  290. __typeof__(*(ptr)) _n_ = (n); \
  291. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  292. (unsigned long)_n_, sizeof(*(ptr))); \
  293. })
  294. #endif /* !(__ASSEMBLY__) */
  295. #define arch_align_stack(x) (x)
  296. #endif /* !(__SPARC64_SYSTEM_H) */