1 #ifndef _ASM_M32R_SYSTEM_H
2 #define _ASM_M32R_SYSTEM_H
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file "COPYING" in the main directory of this archive
9 * Copyright (C) 2001 Hiroyuki Kondo, Hirokazu Takata, and Hitoshi Yamamoto
10 * Copyright (C) 2004, 2006 Hirokazu Takata <takata at linux-m32r.org>
13 #include <linux/compiler.h>
14 #include <asm/assembler.h>
19 * switch_to(prev, next) should switch from task `prev' to `next'
20 * `prev' will never be the same as `next'.
22 * `next' and `prev' should be struct task_struct, but it isn't always defined
25 #if defined(CONFIG_FRAME_POINTER) || \
26 !defined(CONFIG_SCHED_NO_NO_OMIT_FRAME_POINTER)
27 #define M32R_PUSH_FP " push fp\n"
28 #define M32R_POP_FP " pop fp\n"
30 #define M32R_PUSH_FP ""
31 #define M32R_POP_FP ""
34 #define switch_to(prev, next, last) do { \
35 __asm__ __volatile__ ( \
36 " seth lr, #high(1f) \n" \
37 " or3 lr, lr, #low(1f) \n" \
38 " st lr, @%4 ; store old LR \n" \
39 " ld lr, @%5 ; load new LR \n" \
41 " st sp, @%2 ; store old SP \n" \
42 " ld sp, @%3 ; load new SP \n" \
43 " push %1 ; store `prev' on new stack \n" \
47 " pop %0 ; restore `__last' from new stack \n" \
51 "r" (&(prev->thread.sp)), "r" (&(next->thread.sp)), \
52 "r" (&(prev->thread.lr)), "r" (&(next->thread.lr)) \
58 * On SMP systems, when the scheduler does migration-cost autodetection,
59 * it needs a way to flush as much of the CPU's caches as possible.
63 static inline void sched_cacheflush(void)
67 /* Interrupt Control */
68 #if !defined(CONFIG_CHIP_M32102) && !defined(CONFIG_CHIP_M32104)
69 #define local_irq_enable() \
70 __asm__ __volatile__ ("setpsw #0x40 -> nop": : :"memory")
71 #define local_irq_disable() \
72 __asm__ __volatile__ ("clrpsw #0x40 -> nop": : :"memory")
73 #else /* CONFIG_CHIP_M32102 || CONFIG_CHIP_M32104 */
74 static inline void local_irq_enable(void)
79 "or3 %0, %0, #0x0040; \n\t"
81 : "=&r" (tmpreg) : : "cbit", "memory");
84 static inline void local_irq_disable(void)
86 unsigned long tmpreg0, tmpreg1;
88 "ld24 %0, #0 ; Use 32-bit insn. \n\t"
89 "mvfc %1, psw ; No interrupt can be accepted here. \n\t"
91 "and3 %0, %1, #0xffbf \n\t"
93 : "=&r" (tmpreg0), "=&r" (tmpreg1) : : "cbit", "memory");
95 #endif /* CONFIG_CHIP_M32102 || CONFIG_CHIP_M32104 */
97 #define local_save_flags(x) \
98 __asm__ __volatile__("mvfc %0,psw" : "=r"(x) : /* no input */)
100 #define local_irq_restore(x) \
101 __asm__ __volatile__("mvtc %0,psw" : /* no outputs */ \
102 : "r" (x) : "cbit", "memory")
104 #if !(defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_M32104))
105 #define local_irq_save(x) \
106 __asm__ __volatile__( \
107 "mvfc %0, psw; \n\t" \
108 "clrpsw #0x40 -> nop; \n\t" \
109 : "=r" (x) : /* no input */ : "memory")
110 #else /* CONFIG_CHIP_M32102 || CONFIG_CHIP_M32104 */
111 #define local_irq_save(x) \
113 unsigned long tmpreg; \
114 __asm__ __volatile__( \
116 "mvfc %0, psw \n\t" \
117 "mvtc %1, psw \n\t" \
118 "and3 %1, %0, #0xffbf \n\t" \
119 "mvtc %1, psw \n\t" \
120 : "=r" (x), "=&r" (tmpreg) \
121 : : "cbit", "memory"); \
123 #endif /* CONFIG_CHIP_M32102 || CONFIG_CHIP_M32104 */
125 #define irqs_disabled() \
127 unsigned long flags; \
128 local_save_flags(flags); \
132 #define nop() __asm__ __volatile__ ("nop" : : )
134 #define xchg(ptr,x) \
135 ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
138 extern void __xchg_called_with_bad_pointer(void);
141 #ifdef CONFIG_CHIP_M32700_TS1
142 #define DCACHE_CLEAR(reg0, reg1, addr) \
143 "seth "reg1", #high(dcache_dummy); \n\t" \
144 "or3 "reg1", "reg1", #low(dcache_dummy); \n\t" \
145 "lock "reg0", @"reg1"; \n\t" \
146 "add3 "reg0", "addr", #0x1000; \n\t" \
147 "ld "reg0", @"reg0"; \n\t" \
148 "add3 "reg0", "addr", #0x2000; \n\t" \
149 "ld "reg0", @"reg0"; \n\t" \
150 "unlock "reg0", @"reg1"; \n\t"
151 /* FIXME: This workaround code cannot handle kernel modules
152 * correctly under SMP environment.
154 #else /* CONFIG_CHIP_M32700_TS1 */
155 #define DCACHE_CLEAR(reg0, reg1, addr)
156 #endif /* CONFIG_CHIP_M32700_TS1 */
158 static __always_inline unsigned long
159 __xchg(unsigned long x, volatile void * ptr, int size)
162 unsigned long tmp = 0;
164 local_irq_save(flags);
169 __asm__ __volatile__ (
172 : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
175 __asm__ __volatile__ (
178 : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
181 __asm__ __volatile__ (
184 : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
186 #else /* CONFIG_SMP */
188 __asm__ __volatile__ (
189 DCACHE_CLEAR("%0", "r4", "%2")
191 "unlock %1, @%2; \n\t"
192 : "=&r" (tmp) : "r" (x), "r" (ptr)
194 #ifdef CONFIG_CHIP_M32700_TS1
196 #endif /* CONFIG_CHIP_M32700_TS1 */
200 __xchg_called_with_bad_pointer();
201 #endif /* CONFIG_SMP */
204 local_irq_restore(flags);
209 #define __HAVE_ARCH_CMPXCHG 1
211 static inline unsigned long
212 __cmpxchg_u32(volatile unsigned int *p, unsigned int old, unsigned int new)
217 local_irq_save(flags);
218 __asm__ __volatile__ (
219 DCACHE_CLEAR("%0", "r4", "%1")
220 M32R_LOCK" %0, @%1; \n"
221 " bne %0, %2, 1f; \n"
222 M32R_UNLOCK" %3, @%1; \n"
226 M32R_UNLOCK" %0, @%1; \n"
230 : "r" (p), "r" (old), "r" (new)
232 #ifdef CONFIG_CHIP_M32700_TS1
234 #endif /* CONFIG_CHIP_M32700_TS1 */
236 local_irq_restore(flags);
241 /* This function doesn't exist, so you'll get a linker error
242 if something tries to do an invalid cmpxchg(). */
243 extern void __cmpxchg_called_with_bad_pointer(void);
245 static inline unsigned long
246 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
250 return __cmpxchg_u32(ptr, old, new);
251 #if 0 /* we don't have __cmpxchg_u64 */
253 return __cmpxchg_u64(ptr, old, new);
256 __cmpxchg_called_with_bad_pointer();
260 #define cmpxchg(ptr,o,n) \
262 __typeof__(*(ptr)) _o_ = (o); \
263 __typeof__(*(ptr)) _n_ = (n); \
264 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
265 (unsigned long)_n_, sizeof(*(ptr))); \
268 #endif /* __KERNEL__ */
273 * mb() prevents loads and stores being reordered across this point.
274 * rmb() prevents loads being reordered across this point.
275 * wmb() prevents stores being reordered across this point.
277 #define mb() barrier()
282 * read_barrier_depends - Flush all pending reads that subsequents reads
285 * No data-dependent reads from memory-like regions are ever reordered
286 * over this barrier. All reads preceding this primitive are guaranteed
287 * to access memory (but not necessarily other CPUs' caches) before any
288 * reads following this primitive that depend on the data return by
289 * any of the preceding reads. This primitive is much lighter weight than
290 * rmb() on most CPUs, and is never heavier weight than is
293 * These ordering constraints are respected by both the local CPU
296 * Ordering is not guaranteed by anything other than these primitives,
297 * not even by data dependencies. See the documentation for
298 * memory_barrier() for examples and URLs to more information.
300 * For example, the following code would force ordering (the initial
301 * value of "a" is zero, "b" is one, and "p" is "&a"):
309 * read_barrier_depends();
314 * because the read of "*q" depends on the read of "p" and these
315 * two reads are separated by a read_barrier_depends(). However,
316 * the following code, with the same initial values for "a" and "b":
324 * read_barrier_depends();
328 * does not enforce ordering, since there is no data dependency between
329 * the read of "a" and the read of "b". Therefore, on some CPUs, such
330 * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
331 * in cases like this where there are no data dependencies.
334 #define read_barrier_depends() do { } while (0)
337 #define smp_mb() mb()
338 #define smp_rmb() rmb()
339 #define smp_wmb() wmb()
340 #define smp_read_barrier_depends() read_barrier_depends()
341 #define set_mb(var, value) do { (void) xchg(&var, value); } while (0)
343 #define smp_mb() barrier()
344 #define smp_rmb() barrier()
345 #define smp_wmb() barrier()
346 #define smp_read_barrier_depends() do { } while (0)
347 #define set_mb(var, value) do { var = value; barrier(); } while (0)
350 #define arch_align_stack(x) (x)
352 #endif /* _ASM_M32R_SYSTEM_H */