Merge to Fedora kernel-2.6.7-1.441
[linux-2.6.git] / include / asm-ppc64 / system.h
1 #ifndef __PPC64_SYSTEM_H
2 #define __PPC64_SYSTEM_H
3
4 /*
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version
8  * 2 of the License, or (at your option) any later version.
9  */
10
11 #include <linux/config.h>
12 #include <linux/compiler.h>
13 #include <asm/page.h>
14 #include <asm/processor.h>
15 #include <asm/hw_irq.h>
16 #include <asm/memory.h>
17
18 /*
19  * Memory barrier.
20  * The sync instruction guarantees that all memory accesses initiated
21  * by this processor have been performed (with respect to all other
22  * mechanisms that access memory).  The eieio instruction is a barrier
23  * providing an ordering (separately) for (a) cacheable stores and (b)
24  * loads and stores to non-cacheable memory (e.g. I/O devices).
25  *
26  * mb() prevents loads and stores being reordered across this point.
27  * rmb() prevents loads being reordered across this point.
28  * wmb() prevents stores being reordered across this point.
29  * read_barrier_depends() prevents data-dependent loads being reordered
30  *      across this point (nop on PPC).
31  *
32  * We have to use the sync instructions for mb(), since lwsync doesn't
33  * order loads with respect to previous stores.  Lwsync is fine for
34  * rmb(), though.
35  * For wmb(), we use sync since wmb is used in drivers to order
36  * stores to system memory with respect to writes to the device.
37  * However, smp_wmb() can be a lighter-weight eieio barrier on
38  * SMP since it is only used to order updates to system memory.
39  */
40 #define mb()   __asm__ __volatile__ ("sync" : : : "memory")
41 #define rmb()  __asm__ __volatile__ ("lwsync" : : : "memory")
42 #define wmb()  __asm__ __volatile__ ("sync" : : : "memory")
43 #define read_barrier_depends()  do { } while(0)
44
45 #define set_mb(var, value)      do { var = value; smp_mb(); } while (0)
46 #define set_wmb(var, value)     do { var = value; smp_wmb(); } while (0)
47
48 #ifdef CONFIG_SMP
49 #define smp_mb()        mb()
50 #define smp_rmb()       rmb()
51 #define smp_wmb()       __asm__ __volatile__ ("eieio" : : : "memory")
52 #define smp_read_barrier_depends()  read_barrier_depends()
53 #else
54 #define smp_mb()        __asm__ __volatile__("": : :"memory")
55 #define smp_rmb()       __asm__ __volatile__("": : :"memory")
56 #define smp_wmb()       __asm__ __volatile__("": : :"memory")
57 #define smp_read_barrier_depends()  do { } while(0)
58 #endif /* CONFIG_SMP */
59
60 #ifdef __KERNEL__
61 struct task_struct;
62 struct pt_regs;
63
64 #ifdef CONFIG_DEBUGGER
65
66 extern int (*__debugger)(struct pt_regs *regs);
67 extern int (*__debugger_ipi)(struct pt_regs *regs);
68 extern int (*__debugger_bpt)(struct pt_regs *regs);
69 extern int (*__debugger_sstep)(struct pt_regs *regs);
70 extern int (*__debugger_iabr_match)(struct pt_regs *regs);
71 extern int (*__debugger_dabr_match)(struct pt_regs *regs);
72 extern int (*__debugger_fault_handler)(struct pt_regs *regs);
73
74 #define DEBUGGER_BOILERPLATE(__NAME) \
75 static inline int __NAME(struct pt_regs *regs) \
76 { \
77         if (unlikely(__ ## __NAME)) \
78                 return __ ## __NAME(regs); \
79         return 0; \
80 }
81
82 DEBUGGER_BOILERPLATE(debugger)
83 DEBUGGER_BOILERPLATE(debugger_ipi)
84 DEBUGGER_BOILERPLATE(debugger_bpt)
85 DEBUGGER_BOILERPLATE(debugger_sstep)
86 DEBUGGER_BOILERPLATE(debugger_iabr_match)
87 DEBUGGER_BOILERPLATE(debugger_dabr_match)
88 DEBUGGER_BOILERPLATE(debugger_fault_handler)
89
90 #ifdef CONFIG_XMON
91 extern void xmon_init(void);
92 #endif
93
94 #else
95 static inline int debugger(struct pt_regs *regs) { return 0; }
96 static inline int debugger_ipi(struct pt_regs *regs) { return 0; }
97 static inline int debugger_bpt(struct pt_regs *regs) { return 0; }
98 static inline int debugger_sstep(struct pt_regs *regs) { return 0; }
99 static inline int debugger_iabr_match(struct pt_regs *regs) { return 0; }
100 static inline int debugger_dabr_match(struct pt_regs *regs) { return 0; }
101 static inline int debugger_fault_handler(struct pt_regs *regs) { return 0; }
102 #endif
103
104 extern int fix_alignment(struct pt_regs *regs);
105 extern void bad_page_fault(struct pt_regs *regs, unsigned long address,
106                            int sig);
107 extern void show_regs(struct pt_regs * regs);
108 extern int die(const char *str, struct pt_regs *regs, long err);
109
110 extern void flush_instruction_cache(void);
111 extern int _get_PVR(void);
112 extern void giveup_fpu(struct task_struct *);
113 extern void disable_kernel_fp(void);
114 extern void flush_fp_to_thread(struct task_struct *);
115 extern void flush_altivec_to_thread(struct task_struct *);
116 extern void enable_kernel_fp(void);
117 extern void giveup_altivec(struct task_struct *);
118 extern void disable_kernel_altivec(void);
119 extern void enable_kernel_altivec(void);
120 extern void cvt_fd(float *from, double *to, unsigned long *fpscr);
121 extern void cvt_df(double *from, float *to, unsigned long *fpscr);
122 extern int abs(int);
123
124 extern struct task_struct *__switch_to(struct task_struct *,
125                                        struct task_struct *);
126 #define switch_to(prev, next, last)     ((last) = __switch_to((prev), (next)))
127
128 struct thread_struct;
129 extern struct task_struct * _switch(struct thread_struct *prev,
130                                     struct thread_struct *next);
131
132 static inline int __is_processor(unsigned long pv)
133 {
134         unsigned long pvr;
135         asm("mfspr %0, 0x11F" : "=r" (pvr)); 
136         return(PVR_VER(pvr) == pv);
137 }
138
139 /*
140  * Atomic exchange
141  *
142  * Changes the memory location '*ptr' to be val and returns
143  * the previous value stored there.
144  *
145  * Inline asm pulled from arch/ppc/kernel/misc.S so ppc64
146  * is more like most of the other architectures.
147  */
148 static __inline__ unsigned long
149 __xchg_u32(volatile int *m, unsigned long val)
150 {
151         unsigned long dummy;
152
153         __asm__ __volatile__(
154         EIEIO_ON_SMP
155 "1:     lwarx %0,0,%3           # __xchg_u32\n\
156         stwcx. %2,0,%3\n\
157 2:      bne- 1b"
158         ISYNC_ON_SMP
159         : "=&r" (dummy), "=m" (*m)
160         : "r" (val), "r" (m)
161         : "cc", "memory");
162
163         return (dummy);
164 }
165
166 static __inline__ unsigned long
167 __xchg_u64(volatile long *m, unsigned long val)
168 {
169         unsigned long dummy;
170
171         __asm__ __volatile__(
172         EIEIO_ON_SMP
173 "1:     ldarx %0,0,%3           # __xchg_u64\n\
174         stdcx. %2,0,%3\n\
175 2:      bne- 1b"
176         ISYNC_ON_SMP
177         : "=&r" (dummy), "=m" (*m)
178         : "r" (val), "r" (m)
179         : "cc", "memory");
180
181         return (dummy);
182 }
183
184 /*
185  * This function doesn't exist, so you'll get a linker error
186  * if something tries to do an invalid xchg().
187  */
188 extern void __xchg_called_with_bad_pointer(void);
189
190 static __inline__ unsigned long
191 __xchg(volatile void *ptr, unsigned long x, int size)
192 {
193         switch (size) {
194         case 4:
195                 return __xchg_u32(ptr, x);
196         case 8:
197                 return __xchg_u64(ptr, x);
198         }
199         __xchg_called_with_bad_pointer();
200         return x;
201 }
202
203 #define xchg(ptr,x)                                                          \
204   ({                                                                         \
205      __typeof__(*(ptr)) _x_ = (x);                                           \
206      (__typeof__(*(ptr))) __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
207   })
208
209 #define tas(ptr) (xchg((ptr),1))
210
211 #define __HAVE_ARCH_CMPXCHG     1
212
213 static __inline__ unsigned long
214 __cmpxchg_u32(volatile int *p, int old, int new)
215 {
216         unsigned int prev;
217
218         __asm__ __volatile__ (
219         EIEIO_ON_SMP
220 "1:     lwarx   %0,0,%2         # __cmpxchg_u32\n\
221         cmpw    0,%0,%3\n\
222         bne-    2f\n\
223         stwcx.  %4,0,%2\n\
224         bne-    1b"
225         ISYNC_ON_SMP
226         "\n\
227 2:"
228         : "=&r" (prev), "=m" (*p)
229         : "r" (p), "r" (old), "r" (new), "m" (*p)
230         : "cc", "memory");
231
232         return prev;
233 }
234
235 static __inline__ unsigned long
236 __cmpxchg_u64(volatile long *p, unsigned long old, unsigned long new)
237 {
238         unsigned long prev;
239
240         __asm__ __volatile__ (
241         EIEIO_ON_SMP
242 "1:     ldarx   %0,0,%2         # __cmpxchg_u64\n\
243         cmpd    0,%0,%3\n\
244         bne-    2f\n\
245         stdcx.  %4,0,%2\n\
246         bne-    1b"
247         ISYNC_ON_SMP
248         "\n\
249 2:"
250         : "=&r" (prev), "=m" (*p)
251         : "r" (p), "r" (old), "r" (new), "m" (*p)
252         : "cc", "memory");
253
254         return prev;
255 }
256
257 /* This function doesn't exist, so you'll get a linker error
258    if something tries to do an invalid cmpxchg().  */
259 extern void __cmpxchg_called_with_bad_pointer(void);
260
261 static __inline__ unsigned long
262 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
263 {
264         switch (size) {
265         case 4:
266                 return __cmpxchg_u32(ptr, old, new);
267         case 8:
268                 return __cmpxchg_u64(ptr, old, new);
269         }
270         __cmpxchg_called_with_bad_pointer();
271         return old;
272 }
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 /*
283  * We handle most unaligned accesses in hardware. On the other hand 
284  * unaligned DMA can be very expensive on some ppc64 IO chips (it does
285  * powers of 2 writes until it reaches sufficient alignment).
286  *
287  * Based on this we disable the IP header alignment in network drivers.
288  */
289 #define NET_IP_ALIGN   0
290
291 #endif /* __KERNEL__ */
292 #endif