system.h 9.7 KB

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