ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 can use the eieio instruction for wmb, but since it doesn't
33  * give any ordering guarantees about loads, we have to use the
34  * stronger but slower sync instruction for mb and rmb.
35  */
36 #define mb()   __asm__ __volatile__ ("sync" : : : "memory")
37 #define rmb()  __asm__ __volatile__ ("lwsync" : : : "memory")
38 #define wmb()  __asm__ __volatile__ ("eieio" : : : "memory")
39 #define read_barrier_depends()  do { } while(0)
40
41 #define set_mb(var, value)      do { var = value; mb(); } while (0)
42 #define set_wmb(var, value)     do { var = value; wmb(); } while (0)
43
44 #ifdef CONFIG_SMP
45 #define smp_mb()        mb()
46 #define smp_rmb()       rmb()
47 #define smp_wmb()       wmb()
48 #define smp_read_barrier_depends()  read_barrier_depends()
49 #else
50 #define smp_mb()        __asm__ __volatile__("": : :"memory")
51 #define smp_rmb()       __asm__ __volatile__("": : :"memory")
52 #define smp_wmb()       __asm__ __volatile__("": : :"memory")
53 #define smp_read_barrier_depends()  do { } while(0)
54 #endif /* CONFIG_SMP */
55
56 #ifdef __KERNEL__
57 struct task_struct;
58 struct pt_regs;
59
60 #ifdef CONFIG_DEBUGGER
61
62 extern int (*__debugger)(struct pt_regs *regs);
63 extern int (*__debugger_bpt)(struct pt_regs *regs);
64 extern int (*__debugger_sstep)(struct pt_regs *regs);
65 extern int (*__debugger_iabr_match)(struct pt_regs *regs);
66 extern int (*__debugger_dabr_match)(struct pt_regs *regs);
67 extern int (*__debugger_fault_handler)(struct pt_regs *regs);
68
69 #define DEBUGGER_BOILERPLATE(__NAME) \
70 static inline int __NAME(struct pt_regs *regs) \
71 { \
72         if (unlikely(__ ## __NAME)) \
73                 return __ ## __NAME(regs); \
74         return 0; \
75 }
76
77 DEBUGGER_BOILERPLATE(debugger)
78 DEBUGGER_BOILERPLATE(debugger_bpt)
79 DEBUGGER_BOILERPLATE(debugger_sstep)
80 DEBUGGER_BOILERPLATE(debugger_iabr_match)
81 DEBUGGER_BOILERPLATE(debugger_dabr_match)
82 DEBUGGER_BOILERPLATE(debugger_fault_handler)
83
84 #ifdef CONFIG_XMON
85 extern void xmon_init(void);
86 #endif
87
88 #else
89 static inline int debugger(struct pt_regs *regs) { return 0; }
90 static inline int debugger_bpt(struct pt_regs *regs) { return 0; }
91 static inline int debugger_sstep(struct pt_regs *regs) { return 0; }
92 static inline int debugger_iabr_match(struct pt_regs *regs) { return 0; }
93 static inline int debugger_dabr_match(struct pt_regs *regs) { return 0; }
94 static inline int debugger_fault_handler(struct pt_regs *regs) { return 0; }
95 #endif
96
97 extern int fix_alignment(struct pt_regs *regs);
98 extern void bad_page_fault(struct pt_regs *regs, unsigned long address,
99                            int sig);
100 extern void show_regs(struct pt_regs * regs);
101 extern int die(const char *str, struct pt_regs *regs, long err);
102
103 extern void flush_instruction_cache(void);
104 extern int _get_PVR(void);
105 extern void giveup_fpu(struct task_struct *);
106 extern void disable_kernel_fp(void);
107 extern void enable_kernel_fp(void);
108 extern void giveup_altivec(struct task_struct *);
109 extern void disable_kernel_altivec(void);
110 extern void enable_kernel_altivec(void);
111 extern void cvt_fd(float *from, double *to, unsigned long *fpscr);
112 extern void cvt_df(double *from, float *to, unsigned long *fpscr);
113 extern int abs(int);
114
115 extern struct task_struct *__switch_to(struct task_struct *,
116                                        struct task_struct *);
117 #define switch_to(prev, next, last)     ((last) = __switch_to((prev), (next)))
118
119 struct thread_struct;
120 extern struct task_struct * _switch(struct thread_struct *prev,
121                                     struct thread_struct *next);
122
123 static inline int __is_processor(unsigned long pv)
124 {
125         unsigned long pvr;
126         asm("mfspr %0, 0x11F" : "=r" (pvr)); 
127         return(PVR_VER(pvr) == pv);
128 }
129
130 /*
131  * Atomic exchange
132  *
133  * Changes the memory location '*ptr' to be val and returns
134  * the previous value stored there.
135  *
136  * Inline asm pulled from arch/ppc/kernel/misc.S so ppc64
137  * is more like most of the other architectures.
138  */
139 static __inline__ unsigned long
140 __xchg_u32(volatile int *m, unsigned long val)
141 {
142         unsigned long dummy;
143
144         __asm__ __volatile__(
145         EIEIO_ON_SMP
146 "1:     lwarx %0,0,%3           # __xchg_u32\n\
147         stwcx. %2,0,%3\n\
148 2:      bne- 1b"
149         ISYNC_ON_SMP
150         : "=&r" (dummy), "=m" (*m)
151         : "r" (val), "r" (m)
152         : "cc", "memory");
153
154         return (dummy);
155 }
156
157 static __inline__ unsigned long
158 __xchg_u64(volatile long *m, unsigned long val)
159 {
160         unsigned long dummy;
161
162         __asm__ __volatile__(
163         EIEIO_ON_SMP
164 "1:     ldarx %0,0,%3           # __xchg_u64\n\
165         stdcx. %2,0,%3\n\
166 2:      bne- 1b"
167         ISYNC_ON_SMP
168         : "=&r" (dummy), "=m" (*m)
169         : "r" (val), "r" (m)
170         : "cc", "memory");
171
172         return (dummy);
173 }
174
175 /*
176  * This function doesn't exist, so you'll get a linker error
177  * if something tries to do an invalid xchg().
178  */
179 extern void __xchg_called_with_bad_pointer(void);
180
181 static __inline__ unsigned long
182 __xchg(volatile void *ptr, unsigned long x, int size)
183 {
184         switch (size) {
185         case 4:
186                 return __xchg_u32(ptr, x);
187         case 8:
188                 return __xchg_u64(ptr, x);
189         }
190         __xchg_called_with_bad_pointer();
191         return x;
192 }
193
194 #define xchg(ptr,x)                                                          \
195   ({                                                                         \
196      __typeof__(*(ptr)) _x_ = (x);                                           \
197      (__typeof__(*(ptr))) __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
198   })
199
200 #define tas(ptr) (xchg((ptr),1))
201
202 #define __HAVE_ARCH_CMPXCHG     1
203
204 static __inline__ unsigned long
205 __cmpxchg_u32(volatile int *p, int old, int new)
206 {
207         unsigned int prev;
208
209         __asm__ __volatile__ (
210         EIEIO_ON_SMP
211 "1:     lwarx   %0,0,%2         # __cmpxchg_u32\n\
212         cmpw    0,%0,%3\n\
213         bne-    2f\n\
214         stwcx.  %4,0,%2\n\
215         bne-    1b"
216         ISYNC_ON_SMP
217         "\n\
218 2:"
219         : "=&r" (prev), "=m" (*p)
220         : "r" (p), "r" (old), "r" (new), "m" (*p)
221         : "cc", "memory");
222
223         return prev;
224 }
225
226 static __inline__ unsigned long
227 __cmpxchg_u64(volatile long *p, unsigned long old, unsigned long new)
228 {
229         unsigned long prev;
230
231         __asm__ __volatile__ (
232         EIEIO_ON_SMP
233 "1:     ldarx   %0,0,%2         # __cmpxchg_u64\n\
234         cmpd    0,%0,%3\n\
235         bne-    2f\n\
236         stdcx.  %4,0,%2\n\
237         bne-    1b"
238         ISYNC_ON_SMP
239         "\n\
240 2:"
241         : "=&r" (prev), "=m" (*p)
242         : "r" (p), "r" (old), "r" (new), "m" (*p)
243         : "cc", "memory");
244
245         return prev;
246 }
247
248 /* This function doesn't exist, so you'll get a linker error
249    if something tries to do an invalid cmpxchg().  */
250 extern void __cmpxchg_called_with_bad_pointer(void);
251
252 static __inline__ unsigned long
253 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
254 {
255         switch (size) {
256         case 4:
257                 return __cmpxchg_u32(ptr, old, new);
258         case 8:
259                 return __cmpxchg_u64(ptr, old, new);
260         }
261         __cmpxchg_called_with_bad_pointer();
262         return old;
263 }
264
265 #define cmpxchg(ptr,o,n)                                                 \
266   ({                                                                     \
267      __typeof__(*(ptr)) _o_ = (o);                                       \
268      __typeof__(*(ptr)) _n_ = (n);                                       \
269      (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_,           \
270                                     (unsigned long)_n_, sizeof(*(ptr))); \
271   })
272
273 #endif /* __KERNEL__ */
274 #endif