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 by Hiroyuki Kondo, Hirokazu Takata, and Hitoshi Yamamoto
10 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
13 #include <linux/config.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
26 #define prepare_to_switch() do { } while(0)
27 #endif /* not CONFIG_SMP */
29 #define switch_to(prev, next, last) do { \
30 register unsigned long arg0 __asm__ ("r0") = (unsigned long)prev; \
31 register unsigned long arg1 __asm__ ("r1") = (unsigned long)next; \
32 register unsigned long *oldsp __asm__ ("r2") = &(prev->thread.sp); \
33 register unsigned long *newsp __asm__ ("r3") = &(next->thread.sp); \
34 register unsigned long *oldlr __asm__ ("r4") = &(prev->thread.lr); \
35 register unsigned long *newlr __asm__ ("r5") = &(next->thread.lr); \
36 register struct task_struct *__last __asm__ ("r6"); \
37 __asm__ __volatile__ ( \
40 "st r10, @-r15 \n\t" \
41 "st r11, @-r15 \n\t" \
42 "st r12, @-r15 \n\t" \
43 "st r13, @-r15 \n\t" \
44 "st r14, @-r15 \n\t" \
45 "seth r14, #high(1f) \n\t" \
46 "or3 r14, r14, #low(1f) \n\t" \
47 "st r14, @r4 ; store old LR \n\t" \
48 "st r15, @r2 ; store old SP \n\t" \
49 "ld r15, @r3 ; load new SP \n\t" \
50 "st r0, @-r15 ; store 'prev' onto new stack \n\t" \
51 "ld r14, @r5 ; load new LR \n\t" \
55 "ld r6, @r15+ ; load 'prev' from new stack \n\t" \
56 "ld r14, @r15+ \n\t" \
57 "ld r13, @r15+ \n\t" \
58 "ld r12, @r15+ \n\t" \
59 "ld r11, @r15+ \n\t" \
60 "ld r10, @r15+ \n\t" \
64 : "r" (arg0), "r" (arg1), "r" (oldsp), "r" (newsp), \
65 "r" (oldlr), "r" (newlr) \
72 * On SMP systems, when the scheduler does migration-cost autodetection,
73 * it needs a way to flush as much of the CPU's caches as possible.
77 static inline void sched_cacheflush(void)
81 /* Interrupt Control */
82 #if !defined(CONFIG_CHIP_M32102) && !defined(CONFIG_CHIP_M32104)
83 #define local_irq_enable() \
84 __asm__ __volatile__ ("setpsw #0x40 -> nop": : :"memory")
85 #define local_irq_disable() \
86 __asm__ __volatile__ ("clrpsw #0x40 -> nop": : :"memory")
87 #else /* CONFIG_CHIP_M32102 || CONFIG_CHIP_M32104 */
88 static inline void local_irq_enable(void)
93 "or3 %0, %0, #0x0040; \n\t"
95 : "=&r" (tmpreg) : : "cbit", "memory");
98 static inline void local_irq_disable(void)
100 unsigned long tmpreg0, tmpreg1;
101 __asm__ __volatile__(
102 "ld24 %0, #0 ; Use 32-bit insn. \n\t"
103 "mvfc %1, psw ; No interrupt can be accepted here. \n\t"
105 "and3 %0, %1, #0xffbf \n\t"
107 : "=&r" (tmpreg0), "=&r" (tmpreg1) : : "cbit", "memory");
109 #endif /* CONFIG_CHIP_M32102 || CONFIG_CHIP_M32104 */
111 #define local_save_flags(x) \
112 __asm__ __volatile__("mvfc %0,psw" : "=r"(x) : /* no input */)
114 #define local_irq_restore(x) \
115 __asm__ __volatile__("mvtc %0,psw" : /* no outputs */ \
116 : "r" (x) : "cbit", "memory")
118 #if !(defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_M32104))
119 #define local_irq_save(x) \
120 __asm__ __volatile__( \
121 "mvfc %0, psw; \n\t" \
122 "clrpsw #0x40 -> nop; \n\t" \
123 : "=r" (x) : /* no input */ : "memory")
124 #else /* CONFIG_CHIP_M32102 || CONFIG_CHIP_M32104 */
125 #define local_irq_save(x) \
127 unsigned long tmpreg; \
128 __asm__ __volatile__( \
130 "mvfc %0, psw \n\t" \
131 "mvtc %1, psw \n\t" \
132 "and3 %1, %0, #0xffbf \n\t" \
133 "mvtc %1, psw \n\t" \
134 : "=r" (x), "=&r" (tmpreg) \
135 : : "cbit", "memory"); \
137 #endif /* CONFIG_CHIP_M32102 || CONFIG_CHIP_M32104 */
139 #define irqs_disabled() \
141 unsigned long flags; \
142 local_save_flags(flags); \
146 #define nop() __asm__ __volatile__ ("nop" : : )
148 #define xchg(ptr,x) \
149 ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
151 #define tas(ptr) (xchg((ptr),1))
154 extern void __xchg_called_with_bad_pointer(void);
157 #ifdef CONFIG_CHIP_M32700_TS1
158 #define DCACHE_CLEAR(reg0, reg1, addr) \
159 "seth "reg1", #high(dcache_dummy); \n\t" \
160 "or3 "reg1", "reg1", #low(dcache_dummy); \n\t" \
161 "lock "reg0", @"reg1"; \n\t" \
162 "add3 "reg0", "addr", #0x1000; \n\t" \
163 "ld "reg0", @"reg0"; \n\t" \
164 "add3 "reg0", "addr", #0x2000; \n\t" \
165 "ld "reg0", @"reg0"; \n\t" \
166 "unlock "reg0", @"reg1"; \n\t"
167 /* FIXME: This workaround code cannot handle kenrel modules
168 * correctly under SMP environment.
170 #else /* CONFIG_CHIP_M32700_TS1 */
171 #define DCACHE_CLEAR(reg0, reg1, addr)
172 #endif /* CONFIG_CHIP_M32700_TS1 */
174 static __inline__ unsigned long __xchg(unsigned long x, volatile void * ptr,
178 unsigned long tmp = 0;
180 local_irq_save(flags);
185 __asm__ __volatile__ (
188 : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
191 __asm__ __volatile__ (
194 : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
197 __asm__ __volatile__ (
200 : "=&r" (tmp) : "r" (x), "r" (ptr) : "memory");
202 #else /* CONFIG_SMP */
204 __asm__ __volatile__ (
205 DCACHE_CLEAR("%0", "r4", "%2")
207 "unlock %1, @%2; \n\t"
208 : "=&r" (tmp) : "r" (x), "r" (ptr)
210 #ifdef CONFIG_CHIP_M32700_TS1
212 #endif /* CONFIG_CHIP_M32700_TS1 */
216 __xchg_called_with_bad_pointer();
217 #endif /* CONFIG_SMP */
220 local_irq_restore(flags);
225 #define __HAVE_ARCH_CMPXCHG 1
227 static __inline__ unsigned long
228 __cmpxchg_u32(volatile unsigned int *p, unsigned int old, unsigned int new)
233 local_irq_save(flags);
234 __asm__ __volatile__ (
235 DCACHE_CLEAR("%0", "r4", "%1")
236 M32R_LOCK" %0, @%1; \n"
237 " bne %0, %2, 1f; \n"
238 M32R_UNLOCK" %3, @%1; \n"
242 M32R_UNLOCK" %0, @%1; \n"
246 : "r" (p), "r" (old), "r" (new)
248 #ifdef CONFIG_CHIP_M32700_TS1
250 #endif /* CONFIG_CHIP_M32700_TS1 */
252 local_irq_restore(flags);
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);
261 static __inline__ unsigned long
262 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
266 return __cmpxchg_u32(ptr, old, new);
267 #if 0 /* we don't have __cmpxchg_u64 */
269 return __cmpxchg_u64(ptr, old, new);
272 __cmpxchg_called_with_bad_pointer();
276 #define cmpxchg(ptr,o,n) \
278 __typeof__(*(ptr)) _o_ = (o); \
279 __typeof__(*(ptr)) _n_ = (n); \
280 (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
281 (unsigned long)_n_, sizeof(*(ptr))); \
284 #endif /* __KERNEL__ */
289 * mb() prevents loads and stores being reordered across this point.
290 * rmb() prevents loads being reordered across this point.
291 * wmb() prevents stores being reordered across this point.
293 #define mb() barrier()
298 * read_barrier_depends - Flush all pending reads that subsequents reads
301 * No data-dependent reads from memory-like regions are ever reordered
302 * over this barrier. All reads preceding this primitive are guaranteed
303 * to access memory (but not necessarily other CPUs' caches) before any
304 * reads following this primitive that depend on the data return by
305 * any of the preceding reads. This primitive is much lighter weight than
306 * rmb() on most CPUs, and is never heavier weight than is
309 * These ordering constraints are respected by both the local CPU
312 * Ordering is not guaranteed by anything other than these primitives,
313 * not even by data dependencies. See the documentation for
314 * memory_barrier() for examples and URLs to more information.
316 * For example, the following code would force ordering (the initial
317 * value of "a" is zero, "b" is one, and "p" is "&a"):
325 * read_barrier_depends();
330 * because the read of "*q" depends on the read of "p" and these
331 * two reads are separated by a read_barrier_depends(). However,
332 * the following code, with the same initial values for "a" and "b":
340 * read_barrier_depends();
344 * does not enforce ordering, since there is no data dependency between
345 * the read of "a" and the read of "b". Therefore, on some CPUs, such
346 * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
347 * in cases like thiswhere there are no data dependencies.
350 #define read_barrier_depends() do { } while (0)
353 #define smp_mb() mb()
354 #define smp_rmb() rmb()
355 #define smp_wmb() wmb()
356 #define smp_read_barrier_depends() read_barrier_depends()
358 #define smp_mb() barrier()
359 #define smp_rmb() barrier()
360 #define smp_wmb() barrier()
361 #define smp_read_barrier_depends() do { } while (0)
364 #define set_mb(var, value) do { xchg(&var, value); } while (0)
365 #define set_wmb(var, value) do { var = value; wmb(); } while (0)
367 #define arch_align_stack(x) (x)
369 #endif /* _ASM_M32R_SYSTEM_H */