system_64.h 9.6 KB

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