system_64.h 10 KB

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