| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * include/asm-xtensa/system.h | 
|  | 3 | * | 
|  | 4 | * This file is subject to the terms and conditions of the GNU General Public | 
|  | 5 | * License.  See the file "COPYING" in the main directory of this archive | 
|  | 6 | * for more details. | 
|  | 7 | * | 
|  | 8 | * Copyright (C) 2001 - 2005 Tensilica Inc. | 
|  | 9 | */ | 
|  | 10 |  | 
|  | 11 | #ifndef _XTENSA_SYSTEM_H | 
|  | 12 | #define _XTENSA_SYSTEM_H | 
|  | 13 |  | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 14 | #include <linux/stringify.h> | 
|  | 15 |  | 
|  | 16 | #include <asm/processor.h> | 
|  | 17 |  | 
|  | 18 | /* interrupt control */ | 
|  | 19 |  | 
|  | 20 | #define local_save_flags(x)						\ | 
|  | 21 | __asm__ __volatile__ ("rsr %0,"__stringify(PS) : "=a" (x)); | 
|  | 22 | #define local_irq_restore(x)	do {					\ | 
|  | 23 | __asm__ __volatile__ ("wsr %0, "__stringify(PS)" ; rsync" 	\ | 
|  | 24 | :: "a" (x) : "memory"); } while(0); | 
|  | 25 | #define local_irq_save(x)	do {					\ | 
|  | 26 | __asm__ __volatile__ ("rsil %0, "__stringify(LOCKLEVEL) 	\ | 
|  | 27 | : "=a" (x) :: "memory");} while(0); | 
|  | 28 |  | 
|  | 29 | static inline void local_irq_disable(void) | 
|  | 30 | { | 
|  | 31 | unsigned long flags; | 
|  | 32 | __asm__ __volatile__ ("rsil %0, "__stringify(LOCKLEVEL) | 
|  | 33 | : "=a" (flags) :: "memory"); | 
|  | 34 | } | 
|  | 35 | static inline void local_irq_enable(void) | 
|  | 36 | { | 
|  | 37 | unsigned long flags; | 
|  | 38 | __asm__ __volatile__ ("rsil %0, 0" : "=a" (flags) :: "memory"); | 
|  | 39 |  | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | static inline int irqs_disabled(void) | 
|  | 43 | { | 
|  | 44 | unsigned long flags; | 
|  | 45 | local_save_flags(flags); | 
|  | 46 | return flags & 0xf; | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | #define RSR_CPENABLE(x)	do {						  \ | 
|  | 50 | __asm__ __volatile__("rsr %0," __stringify(CPENABLE) : "=a" (x)); \ | 
|  | 51 | } while(0); | 
|  | 52 | #define WSR_CPENABLE(x)	do {						  \ | 
|  | 53 | __asm__ __volatile__("wsr %0," __stringify(CPENABLE)";rsync" 	  \ | 
|  | 54 | :: "a" (x));} while(0); | 
|  | 55 |  | 
|  | 56 | #define clear_cpenable() __clear_cpenable() | 
|  | 57 |  | 
| Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 58 | static inline void __clear_cpenable(void) | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 59 | { | 
|  | 60 | #if XCHAL_HAVE_CP | 
|  | 61 | unsigned long i = 0; | 
|  | 62 | WSR_CPENABLE(i); | 
|  | 63 | #endif | 
|  | 64 | } | 
|  | 65 |  | 
| Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 66 | static inline void enable_coprocessor(int i) | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 67 | { | 
|  | 68 | #if XCHAL_HAVE_CP | 
|  | 69 | int cp; | 
|  | 70 | RSR_CPENABLE(cp); | 
|  | 71 | cp |= 1 << i; | 
|  | 72 | WSR_CPENABLE(cp); | 
|  | 73 | #endif | 
|  | 74 | } | 
|  | 75 |  | 
| Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 76 | static inline void disable_coprocessor(int i) | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 77 | { | 
|  | 78 | #if XCHAL_HAVE_CP | 
|  | 79 | int cp; | 
|  | 80 | RSR_CPENABLE(cp); | 
|  | 81 | cp &= ~(1 << i); | 
|  | 82 | WSR_CPENABLE(cp); | 
|  | 83 | #endif | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | #define smp_read_barrier_depends() do { } while(0) | 
|  | 87 | #define read_barrier_depends() do { } while(0) | 
|  | 88 |  | 
|  | 89 | #define mb()  barrier() | 
|  | 90 | #define rmb() mb() | 
|  | 91 | #define wmb() mb() | 
|  | 92 |  | 
|  | 93 | #ifdef CONFIG_SMP | 
|  | 94 | #error smp_* not defined | 
|  | 95 | #else | 
|  | 96 | #define smp_mb()	barrier() | 
|  | 97 | #define smp_rmb()	barrier() | 
|  | 98 | #define smp_wmb()	barrier() | 
|  | 99 | #endif | 
|  | 100 |  | 
|  | 101 | #define set_mb(var, value)	do { var = value; mb(); } while (0) | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 102 |  | 
|  | 103 | #if !defined (__ASSEMBLY__) | 
|  | 104 |  | 
|  | 105 | /* * switch_to(n) should switch tasks to task nr n, first | 
|  | 106 | * checking that n isn't the current task, in which case it does nothing. | 
|  | 107 | */ | 
|  | 108 | extern void *_switch_to(void *last, void *next); | 
|  | 109 |  | 
|  | 110 | #endif	/* __ASSEMBLY__ */ | 
|  | 111 |  | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 112 | #define switch_to(prev,next,last)		\ | 
|  | 113 | do {						\ | 
|  | 114 | clear_cpenable();			\ | 
|  | 115 | (last) = _switch_to(prev, next);	\ | 
|  | 116 | } while(0) | 
|  | 117 |  | 
|  | 118 | /* | 
|  | 119 | * cmpxchg | 
|  | 120 | */ | 
|  | 121 |  | 
| Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 122 | static inline unsigned long | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 123 | __cmpxchg_u32(volatile int *p, int old, int new) | 
|  | 124 | { | 
|  | 125 | __asm__ __volatile__("rsil    a15, "__stringify(LOCKLEVEL)"\n\t" | 
|  | 126 | "l32i    %0, %1, 0              \n\t" | 
|  | 127 | "bne	%0, %2, 1f             \n\t" | 
|  | 128 | "s32i    %3, %1, 0              \n\t" | 
|  | 129 | "1:                             \n\t" | 
|  | 130 | "wsr     a15, "__stringify(PS)" \n\t" | 
|  | 131 | "rsync                          \n\t" | 
|  | 132 | : "=&a" (old) | 
|  | 133 | : "a" (p), "a" (old), "r" (new) | 
|  | 134 | : "a15", "memory"); | 
|  | 135 | return old; | 
|  | 136 | } | 
|  | 137 | /* This function doesn't exist, so you'll get a linker error | 
|  | 138 | * if something tries to do an invalid cmpxchg(). */ | 
|  | 139 |  | 
|  | 140 | extern void __cmpxchg_called_with_bad_pointer(void); | 
|  | 141 |  | 
|  | 142 | static __inline__ unsigned long | 
|  | 143 | __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size) | 
|  | 144 | { | 
|  | 145 | switch (size) { | 
|  | 146 | case 4:  return __cmpxchg_u32(ptr, old, new); | 
|  | 147 | default: __cmpxchg_called_with_bad_pointer(); | 
|  | 148 | return old; | 
|  | 149 | } | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | #define cmpxchg(ptr,o,n)						      \ | 
|  | 153 | ({ __typeof__(*(ptr)) _o_ = (o);				      \ | 
|  | 154 | __typeof__(*(ptr)) _n_ = (n);				      \ | 
|  | 155 | (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_,	      \ | 
|  | 156 | (unsigned long)_n_, sizeof (*(ptr))); \ | 
|  | 157 | }) | 
|  | 158 |  | 
| Mathieu Desnoyers | 9a7744f | 2008-02-07 00:16:26 -0800 | [diff] [blame] | 159 | #include <asm-generic/cmpxchg-local.h> | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 160 |  | 
| Mathieu Desnoyers | 9a7744f | 2008-02-07 00:16:26 -0800 | [diff] [blame] | 161 | static inline unsigned long __cmpxchg_local(volatile void *ptr, | 
|  | 162 | unsigned long old, | 
|  | 163 | unsigned long new, int size) | 
|  | 164 | { | 
|  | 165 | switch (size) { | 
|  | 166 | case 4: | 
|  | 167 | return __cmpxchg_u32(ptr, old, new); | 
|  | 168 | default: | 
|  | 169 | return __cmpxchg_local_generic(ptr, old, new, size); | 
|  | 170 | } | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 171 |  | 
| Mathieu Desnoyers | 9a7744f | 2008-02-07 00:16:26 -0800 | [diff] [blame] | 172 | return old; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | /* | 
|  | 176 | * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make | 
|  | 177 | * them available. | 
|  | 178 | */ | 
|  | 179 | #define cmpxchg_local(ptr, o, n)				  	       \ | 
|  | 180 | ((__typeof__(*(ptr)))__cmpxchg_local_generic((ptr), (unsigned long)(o),\ | 
|  | 181 | (unsigned long)(n), sizeof(*(ptr)))) | 
|  | 182 | #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n)) | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 183 |  | 
|  | 184 | /* | 
|  | 185 | * xchg_u32 | 
|  | 186 | * | 
|  | 187 | * Note that a15 is used here because the register allocation | 
|  | 188 | * done by the compiler is not guaranteed and a window overflow | 
|  | 189 | * may not occur between the rsil and wsr instructions. By using | 
|  | 190 | * a15 in the rsil, the machine is guaranteed to be in a state | 
|  | 191 | * where no register reference will cause an overflow. | 
|  | 192 | */ | 
|  | 193 |  | 
| Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 194 | static inline unsigned long xchg_u32(volatile int * m, unsigned long val) | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 195 | { | 
|  | 196 | unsigned long tmp; | 
|  | 197 | __asm__ __volatile__("rsil    a15, "__stringify(LOCKLEVEL)"\n\t" | 
|  | 198 | "l32i    %0, %1, 0              \n\t" | 
|  | 199 | "s32i    %2, %1, 0              \n\t" | 
|  | 200 | "wsr     a15, "__stringify(PS)" \n\t" | 
|  | 201 | "rsync                          \n\t" | 
|  | 202 | : "=&a" (tmp) | 
|  | 203 | : "a" (m), "a" (val) | 
|  | 204 | : "a15", "memory"); | 
|  | 205 | return tmp; | 
|  | 206 | } | 
|  | 207 |  | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 208 | #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr)))) | 
|  | 209 |  | 
|  | 210 | /* | 
|  | 211 | * This only works if the compiler isn't horribly bad at optimizing. | 
|  | 212 | * gcc-2.5.8 reportedly can't handle this, but I define that one to | 
|  | 213 | * be dead anyway. | 
|  | 214 | */ | 
|  | 215 |  | 
|  | 216 | extern void __xchg_called_with_bad_pointer(void); | 
|  | 217 |  | 
|  | 218 | static __inline__ unsigned long | 
|  | 219 | __xchg(unsigned long x, volatile void * ptr, int size) | 
|  | 220 | { | 
|  | 221 | switch (size) { | 
|  | 222 | case 4: | 
|  | 223 | return xchg_u32(ptr, x); | 
|  | 224 | } | 
|  | 225 | __xchg_called_with_bad_pointer(); | 
|  | 226 | return x; | 
|  | 227 | } | 
|  | 228 |  | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 229 | extern void set_except_vector(int n, void *addr); | 
|  | 230 |  | 
|  | 231 | static inline void spill_registers(void) | 
|  | 232 | { | 
|  | 233 | unsigned int a0, ps; | 
|  | 234 |  | 
|  | 235 | __asm__ __volatile__ ( | 
| Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 236 | "movi	a14," __stringify (PS_EXCM_BIT) " | 1\n\t" | 
| Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 237 | "mov	a12, a0\n\t" | 
|  | 238 | "rsr	a13," __stringify(SAR) "\n\t" | 
|  | 239 | "xsr	a14," __stringify(PS) "\n\t" | 
|  | 240 | "movi	a0, _spill_registers\n\t" | 
|  | 241 | "rsync\n\t" | 
|  | 242 | "callx0 a0\n\t" | 
|  | 243 | "mov	a0, a12\n\t" | 
|  | 244 | "wsr	a13," __stringify(SAR) "\n\t" | 
|  | 245 | "wsr	a14," __stringify(PS) "\n\t" | 
|  | 246 | :: "a" (&a0), "a" (&ps) | 
|  | 247 | : "a2", "a3", "a12", "a13", "a14", "a15", "memory"); | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | #define arch_align_stack(x) (x) | 
|  | 251 |  | 
|  | 252 | #endif	/* _XTENSA_SYSTEM_H */ |