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