system.h 9.9 KB

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