1 /* $Id: system.h,v 1.68 2001/11/18 00:12:56 davem Exp $ */
2 #ifndef __SPARC64_SYSTEM_H
3 #define __SPARC64_SYSTEM_H
5 #include <linux/config.h>
6 #include <asm/ptrace.h>
7 #include <asm/processor.h>
8 #include <asm/asm_offsets.h>
9 #include <asm/visasm.h>
13 * Sparc (general) CPU types
21 sun4u = 0x05, /* V8 ploos ploos */
23 ap1000 = 0x07, /* almost a sun4m */
26 #define sparc_cpu_model sun4u
28 /* This cannot ever be a sun4c nor sun4 :) That's just history. */
29 #define ARCH_SUN4C_SUN4 0
34 #define setipl(__new_ipl) \
35 __asm__ __volatile__("wrpr %0, %%pil" : : "r" (__new_ipl) : "memory")
38 __asm__ __volatile__("wrpr 15, %%pil" : : : "memory")
41 __asm__ __volatile__("wrpr 0, %%pil" : : : "memory")
44 ({ unsigned long retval; __asm__ __volatile__("rdpr %%pil, %0" : "=r" (retval)); retval; })
46 #define swap_pil(__new_pil) \
47 ({ unsigned long retval; \
48 __asm__ __volatile__("rdpr %%pil, %0\n\t" \
56 #define read_pil_and_cli() \
57 ({ unsigned long retval; \
58 __asm__ __volatile__("rdpr %%pil, %0\n\t" \
65 #define read_pil_and_sti() \
66 ({ unsigned long retval; \
67 __asm__ __volatile__("rdpr %%pil, %0\n\t" \
74 #define __save_flags(flags) ((flags) = getipl())
75 #define __save_and_cli(flags) ((flags) = read_pil_and_cli())
76 #define __save_and_sti(flags) ((flags) = read_pil_and_sti())
77 #define __restore_flags(flags) setipl((flags))
78 #define local_irq_disable() __cli()
79 #define local_irq_enable() __sti()
80 #define local_irq_save(flags) __save_and_cli(flags)
81 #define local_irq_set(flags) __save_and_sti(flags)
82 #define local_irq_restore(flags) __restore_flags(flags)
87 #define save_flags(x) __save_flags(x)
88 #define restore_flags(x) __restore_flags(x)
89 #define save_and_cli(x) __save_and_cli(x)
93 extern void __global_cli(void);
94 extern void __global_sti(void);
95 extern unsigned long __global_save_flags(void);
96 extern void __global_restore_flags(unsigned long flags);
99 #define cli() __global_cli()
100 #define sti() __global_sti()
101 #define save_flags(x) ((x) = __global_save_flags())
102 #define restore_flags(flags) __global_restore_flags(flags)
103 #define save_and_cli(flags) do { save_flags(flags); cli(); } while(0)
107 #define nop() __asm__ __volatile__ ("nop")
109 /* These are here in an effort to more fully work around Spitfire Errata
110 * #51. Essentially, if a memory barrier occurs soon after a mispredicted
111 * branch, the chip can stop executing instructions until a trap occurs.
112 * Therefore, if interrupts are disabled, the chip can hang forever.
114 * It used to be believed that the memory barrier had to be right in the
115 * delay slot, but a case has been traced recently wherein the memory barrier
116 * was one instruction after the branch delay slot and the chip still hung.
117 * The offending sequence was the following in sym_wakeup_done() of the
118 * sym53c8xx_2 driver:
120 * call sym_ccb_from_dsa, 0
122 * brz,pn %o0, .LL1303
126 * The branch has to be mispredicted for the bug to occur. Therefore, we put
127 * the memory barrier explicitly into a "branch always, predicted taken"
128 * delay slot to avoid the problem case.
130 #define membar_safe(type) \
131 do { __asm__ __volatile__("ba,pt %%xcc, 1f\n\t" \
132 " membar " type "\n" \
137 membar_safe("#LoadLoad | #LoadStore | #StoreStore | #StoreLoad")
139 membar_safe("#LoadLoad")
141 membar_safe("#StoreStore")
142 #define set_mb(__var, __value) \
143 do { __var = __value; \
144 membar_safe("#StoreLoad | #StoreStore"); \
146 #define set_wmb(__var, __value) \
147 do { __var = __value; \
148 membar_safe("#StoreStore"); \
152 #define smp_mb() mb()
153 #define smp_rmb() rmb()
154 #define smp_wmb() wmb()
156 #define smp_mb() __asm__ __volatile__("":::"memory")
157 #define smp_rmb() __asm__ __volatile__("":::"memory")
158 #define smp_wmb() __asm__ __volatile__("":::"memory")
161 #define flushi(addr) __asm__ __volatile__ ("flush %0" : : "r" (addr) : "memory")
163 #define flushw_all() __asm__ __volatile__("flushw")
165 /* Performance counter register access. */
166 #define read_pcr(__p) __asm__ __volatile__("rd %%pcr, %0" : "=r" (__p))
167 #define write_pcr(__p) __asm__ __volatile__("wr %0, 0x0, %%pcr" : : "r" (__p));
168 #define read_pic(__p) __asm__ __volatile__("rd %%pic, %0" : "=r" (__p))
170 /* Blackbird errata workaround. See commentary in
171 * arch/sparc64/kernel/smp.c:smp_percpu_timer_interrupt()
172 * for more information.
174 #define reset_pic() \
175 __asm__ __volatile__("ba,pt %xcc, 99f\n\t" \
177 "99:wr %g0, 0x0, %pic\n\t" \
182 extern void synchronize_user_stack(void);
184 extern void __flushw_user(void);
185 #define flushw_user() __flushw_user()
187 #define flush_user_windows flushw_user
188 #define flush_register_windows flushw_all
189 #define prepare_to_switch flushw_all
191 #ifndef CONFIG_DEBUG_SPINLOCK
192 #define CHECK_LOCKS(PREV) do { } while(0)
193 #else /* CONFIG_DEBUG_SPINLOCK */
194 #define CHECK_LOCKS(PREV) \
195 if ((PREV)->thread.smp_lock_count) { \
197 __asm__ __volatile__("mov %%i7, %0" : "=r" (rpc)); \
198 printk(KERN_CRIT "(%s)[%d]: Sleeping with %d locks held!\n", \
199 (PREV)->comm, (PREV)->pid, \
200 (PREV)->thread.smp_lock_count); \
201 printk(KERN_CRIT "(%s)[%d]: Last lock at %08x\n", \
202 (PREV)->comm, (PREV)->pid, \
203 (PREV)->thread.smp_lock_pc); \
204 printk(KERN_CRIT "(%s)[%d]: Sched caller %016lx\n", \
205 (PREV)->comm, (PREV)->pid, rpc); \
207 #endif /* !(CONFIG_DEBUG_SPINLOCK) */
209 /* See what happens when you design the chip correctly?
211 * We tell gcc we clobber all non-fixed-usage registers except
212 * for l0/l1. It will use one for 'next' and the other to hold
213 * the output value of 'last'. 'next' is not referenced again
214 * past the invocation of switch_to in the scheduler, so we need
215 * not preserve it's value. Hairy, but it lets us remove 2 loads
216 * and 2 stores in this critical code path. -DaveM
218 #define switch_to(prev, next, last) \
219 do { CHECK_LOCKS(prev); \
220 if (current->thread.flags & SPARC_FLAG_PERFCTR) { \
221 unsigned long __tmp; \
223 current->thread.pcr_reg = __tmp; \
225 current->thread.kernel_cntd0 += (unsigned int)(__tmp); \
226 current->thread.kernel_cntd1 += ((__tmp) >> 32); \
228 save_and_clear_fpu(); \
229 /* If you are tempted to conditionalize the following */ \
230 /* so that ASI is only written if it changes, think again. */ \
231 __asm__ __volatile__("wr %%g0, %0, %%asi" \
232 : : "r" (next->thread.current_ds.seg)); \
233 __asm__ __volatile__( \
234 "mov %%g6, %%g5\n\t" \
235 "wrpr %%g0, 0x95, %%pstate\n\t" \
236 "stx %%i6, [%%sp + 2047 + 0x70]\n\t" \
237 "stx %%i7, [%%sp + 2047 + 0x78]\n\t" \
238 "rdpr %%wstate, %%o5\n\t" \
239 "stx %%o6, [%%g6 + %3]\n\t" \
240 "stb %%o5, [%%g6 + %2]\n\t" \
241 "rdpr %%cwp, %%o5\n\t" \
242 "stb %%o5, [%%g6 + %5]\n\t" \
244 "ldub [%1 + %5], %%g1\n\t" \
245 "wrpr %%g1, %%cwp\n\t" \
246 "ldx [%%g6 + %3], %%o6\n\t" \
247 "ldub [%%g6 + %2], %%o5\n\t" \
248 "ldub [%%g6 + %4], %%o7\n\t" \
249 "mov %%g6, %%l2\n\t" \
250 "wrpr %%o5, 0x0, %%wstate\n\t" \
251 "ldx [%%sp + 2047 + 0x70], %%i6\n\t" \
252 "ldx [%%sp + 2047 + 0x78], %%i7\n\t" \
253 "wrpr %%g0, 0x94, %%pstate\n\t" \
254 "mov %%l2, %%g6\n\t" \
255 "wrpr %%g0, 0x96, %%pstate\n\t" \
256 "andcc %%o7, %6, %%g0\n\t" \
257 "bne,pn %%icc, ret_from_syscall\n\t" \
258 " mov %%g5, %0\n\t" \
261 "i" ((const unsigned long)(&((struct task_struct *)0)->thread.wstate)),\
262 "i" ((const unsigned long)(&((struct task_struct *)0)->thread.ksp)), \
263 "i" ((const unsigned long)(&((struct task_struct *)0)->thread.flags)),\
264 "i" ((const unsigned long)(&((struct task_struct *)0)->thread.cwp)), \
265 "i" (SPARC_FLAG_NEWCHILD) \
267 "g1", "g2", "g3", "g5", "g7", \
268 "l2", "l3", "l4", "l5", "l6", "l7", \
269 "i0", "i1", "i2", "i3", "i4", "i5", \
270 "o0", "o1", "o2", "o3", "o4", "o5", "o7"); \
271 /* If you fuck with this, update ret_from_syscall code too. */ \
272 if (current->thread.flags & SPARC_FLAG_PERFCTR) { \
273 write_pcr(current->thread.pcr_reg); \
278 extern __inline__ unsigned long xchg32(__volatile__ unsigned int *m, unsigned int val)
280 __asm__ __volatile__(
281 " membar #StoreLoad | #LoadLoad\n"
283 "1: lduw [%2], %%g7\n"
284 " cas [%2], %%g7, %0\n"
286 " bne,a,pn %%icc, 1b\n"
288 " membar #StoreLoad | #StoreStore\n"
291 : "g5", "g7", "cc", "memory");
295 extern __inline__ unsigned long xchg64(__volatile__ unsigned long *m, unsigned long val)
297 __asm__ __volatile__(
298 " membar #StoreLoad | #LoadLoad\n"
300 "1: ldx [%2], %%g7\n"
301 " casx [%2], %%g7, %0\n"
303 " bne,a,pn %%xcc, 1b\n"
305 " membar #StoreLoad | #StoreStore\n"
308 : "g5", "g7", "cc", "memory");
312 #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
313 #define tas(ptr) (xchg((ptr),1))
315 extern void __xchg_called_with_bad_pointer(void);
317 static __inline__ unsigned long __xchg(unsigned long x, __volatile__ void * ptr,
322 return xchg32(ptr, x);
324 return xchg64(ptr, x);
326 __xchg_called_with_bad_pointer();
330 extern void die_if_kernel(char *str, struct pt_regs *regs) __attribute__ ((noreturn));
333 * Atomic compare and exchange. Compare OLD with MEM, if identical,
334 * store NEW in MEM. Return the initial value in MEM. Success is
335 * indicated by comparing RETURN with OLD.
338 #define __HAVE_ARCH_CMPXCHG 1
340 extern __inline__ unsigned long
341 __cmpxchg_u32(volatile int *m, int old, int new)
343 __asm__ __volatile__("membar #StoreLoad | #LoadLoad\n"
344 "cas [%2], %3, %0\n\t"
345 "membar #StoreLoad | #StoreStore"
347 : "0" (new), "r" (m), "r" (old)
353 extern __inline__ unsigned long
354 __cmpxchg_u64(volatile long *m, unsigned long old, unsigned long new)
356 __asm__ __volatile__("membar #StoreLoad | #LoadLoad\n"
357 "casx [%2], %3, %0\n\t"
358 "membar #StoreLoad | #StoreStore"
360 : "0" (new), "r" (m), "r" (old)
366 /* This function doesn't exist, so you'll get a linker error
367 if something tries to do an invalid cmpxchg(). */
368 extern void __cmpxchg_called_with_bad_pointer(void);
370 static __inline__ unsigned long
371 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
375 return __cmpxchg_u32(ptr, old, new);
377 return __cmpxchg_u64(ptr, old, new);
379 __cmpxchg_called_with_bad_pointer();
383 #define cmpxchg(ptr,o,n) \
385 __typeof__(*(ptr)) _o_ = (o); \
386 __typeof__(*(ptr)) _n_ = (n); \
387 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
388 (unsigned long)_n_, sizeof(*(ptr))); \
391 #endif /* !(__ASSEMBLY__) */
393 #endif /* !(__SPARC64_SYSTEM_H) */