system.h 11 KB

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