system.h 9.4 KB

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