2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1994, 95, 96, 97, 98, 99, 2003 by Ralf Baechle
7 * Copyright (C) 1996 by Paul M. Antoine
8 * Copyright (C) 1999 Silicon Graphics
9 * Kevin D. Kissell, kevink@mips.org and Carsten Langgaard, carstenl@mips.com
10 * Copyright (C) 2000 MIPS Technologies, Inc.
15 #include <linux/config.h>
16 #include <linux/types.h>
18 #include <asm/addrspace.h>
19 #include <asm/cpu-features.h>
21 #include <asm/ptrace.h>
23 #include <asm/interrupt.h>
26 * read_barrier_depends - Flush all pending reads that subsequents reads
29 * No data-dependent reads from memory-like regions are ever reordered
30 * over this barrier. All reads preceding this primitive are guaranteed
31 * to access memory (but not necessarily other CPUs' caches) before any
32 * reads following this primitive that depend on the data return by
33 * any of the preceding reads. This primitive is much lighter weight than
34 * rmb() on most CPUs, and is never heavier weight than is
37 * These ordering constraints are respected by both the local CPU
40 * Ordering is not guaranteed by anything other than these primitives,
41 * not even by data dependencies. See the documentation for
42 * memory_barrier() for examples and URLs to more information.
44 * For example, the following code would force ordering (the initial
45 * value of "a" is zero, "b" is one, and "p" is "&a"):
53 * read_barrier_depends();
57 * because the read of "*q" depends on the read of "p" and these
58 * two reads are separated by a read_barrier_depends(). However,
59 * the following code, with the same initial values for "a" and "b":
67 * read_barrier_depends();
71 * does not enforce ordering, since there is no data dependency between
72 * the read of "a" and the read of "b". Therefore, on some CPUs, such
73 * as Alpha, "y" could be set to 3 and "x" to 0. Use rmb()
74 * in cases like this where there are no data dependencies.
77 #define read_barrier_depends() do { } while(0)
79 #ifdef CONFIG_CPU_HAS_SYNC
81 __asm__ __volatile__( \
83 ".set noreorder\n\t" \
91 #define __sync() do { } while(0)
94 #define __fast_iob() \
95 __asm__ __volatile__( \
97 ".set noreorder\n\t" \
102 : "m" (*(int *)CKSEG1) \
105 #define fast_wmb() __sync()
106 #define fast_rmb() __sync()
107 #define fast_mb() __sync()
114 #ifdef CONFIG_CPU_HAS_WB
116 #include <asm/wbflush.h>
118 #define wmb() fast_wmb()
119 #define rmb() fast_rmb()
120 #define mb() wbflush()
121 #define iob() wbflush()
123 #else /* !CONFIG_CPU_HAS_WB */
125 #define wmb() fast_wmb()
126 #define rmb() fast_rmb()
127 #define mb() fast_mb()
128 #define iob() fast_iob()
130 #endif /* !CONFIG_CPU_HAS_WB */
133 #define smp_mb() mb()
134 #define smp_rmb() rmb()
135 #define smp_wmb() wmb()
136 #define smp_read_barrier_depends() read_barrier_depends()
138 #define smp_mb() barrier()
139 #define smp_rmb() barrier()
140 #define smp_wmb() barrier()
141 #define smp_read_barrier_depends() do { } while(0)
144 #define set_mb(var, value) \
145 do { var = value; mb(); } while (0)
147 #define set_wmb(var, value) \
148 do { var = value; wmb(); } while (0)
151 * switch_to(n) should switch tasks to task nr n, first
152 * checking that n isn't the current task, in which case it does nothing.
154 extern asmlinkage void *resume(void *last, void *next, void *next_ti);
158 #ifdef CONFIG_MIPS_MT_FPAFF
161 * Handle the scheduler resume end of FPU affinity management. We do this
162 * inline to try to keep the overhead down. If we have been forced to run on
163 * a "CPU" with an FPU because of a previous high level of FP computation,
164 * but did not actually use the FPU during the most recent time-slice (CU1
165 * isn't set), we undo the restriction on cpus_allowed.
167 * We're not calling set_cpus_allowed() here, because we have no need to
168 * force prompt migration - we're already switching the current CPU to a
172 #define switch_to(prev,next,last) \
175 (prev->thread.mflags & MF_FPUBOUND) && \
176 (!(KSTK_STATUS(prev) & ST0_CU1))) { \
177 prev->thread.mflags &= ~MF_FPUBOUND; \
178 prev->cpus_allowed = prev->thread.user_cpus_allowed; \
182 next->thread.emulated_fp = 0; \
183 (last) = resume(prev, next, next->thread_info); \
185 __restore_dsp(current); \
189 #define switch_to(prev,next,last) \
193 (last) = resume(prev, next, task_thread_info(next)); \
195 __restore_dsp(current); \
200 * On SMP systems, when the scheduler does migration-cost autodetection,
201 * it needs a way to flush as much of the CPU's caches as possible.
203 * TODO: fill this in!
205 static inline void sched_cacheflush(void)
209 static inline unsigned long __xchg_u32(volatile int * m, unsigned int val)
213 if (cpu_has_llsc && R10000_LLSC_WAR) {
216 __asm__ __volatile__(
218 "1: ll %0, %3 # xchg_u32 \n"
228 : "=&r" (retval), "=m" (*m), "=&r" (dummy)
229 : "R" (*m), "Jr" (val)
231 } else if (cpu_has_llsc) {
234 __asm__ __volatile__(
236 "1: ll %0, %3 # xchg_u32 \n"
246 : "=&r" (retval), "=m" (*m), "=&r" (dummy)
247 : "R" (*m), "Jr" (val)
252 local_irq_save(flags);
255 local_irq_restore(flags); /* implies memory barrier */
262 static inline __u64 __xchg_u64(volatile __u64 * m, __u64 val)
266 if (cpu_has_llsc && R10000_LLSC_WAR) {
269 __asm__ __volatile__(
271 "1: lld %0, %3 # xchg_u64 \n"
279 : "=&r" (retval), "=m" (*m), "=&r" (dummy)
280 : "R" (*m), "Jr" (val)
282 } else if (cpu_has_llsc) {
285 __asm__ __volatile__(
287 "1: lld %0, %3 # xchg_u64 \n"
295 : "=&r" (retval), "=m" (*m), "=&r" (dummy)
296 : "R" (*m), "Jr" (val)
301 local_irq_save(flags);
304 local_irq_restore(flags); /* implies memory barrier */
310 extern __u64 __xchg_u64_unsupported_on_32bit_kernels(volatile __u64 * m, __u64 val);
311 #define __xchg_u64 __xchg_u64_unsupported_on_32bit_kernels
314 /* This function doesn't exist, so you'll get a linker error
315 if something tries to do an invalid xchg(). */
316 extern void __xchg_called_with_bad_pointer(void);
318 static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
322 return __xchg_u32(ptr, x);
324 return __xchg_u64(ptr, x);
326 __xchg_called_with_bad_pointer();
330 #define xchg(ptr,x) ((__typeof__(*(ptr)))__xchg((unsigned long)(x),(ptr),sizeof(*(ptr))))
331 #define tas(ptr) (xchg((ptr),1))
333 #define __HAVE_ARCH_CMPXCHG 1
335 static inline unsigned long __cmpxchg_u32(volatile int * m, unsigned long old,
340 if (cpu_has_llsc && R10000_LLSC_WAR) {
341 __asm__ __volatile__(
345 "1: ll %0, %2 # __cmpxchg_u32 \n"
346 " bne %0, %z3, 2f \n"
357 : "=&r" (retval), "=R" (*m)
358 : "R" (*m), "Jr" (old), "Jr" (new)
360 } else if (cpu_has_llsc) {
361 __asm__ __volatile__(
365 "1: ll %0, %2 # __cmpxchg_u32 \n"
366 " bne %0, %z3, 2f \n"
377 : "=&r" (retval), "=R" (*m)
378 : "R" (*m), "Jr" (old), "Jr" (new)
383 local_irq_save(flags);
387 local_irq_restore(flags); /* implies memory barrier */
394 static inline unsigned long __cmpxchg_u64(volatile int * m, unsigned long old,
400 __asm__ __volatile__(
404 "1: lld %0, %2 # __cmpxchg_u64 \n"
405 " bne %0, %z3, 2f \n"
414 : "=&r" (retval), "=R" (*m)
415 : "R" (*m), "Jr" (old), "Jr" (new)
417 } else if (cpu_has_llsc) {
418 __asm__ __volatile__(
422 "1: lld %0, %2 # __cmpxchg_u64 \n"
423 " bne %0, %z3, 2f \n"
432 : "=&r" (retval), "=R" (*m)
433 : "R" (*m), "Jr" (old), "Jr" (new)
438 local_irq_save(flags);
442 local_irq_restore(flags); /* implies memory barrier */
448 extern unsigned long __cmpxchg_u64_unsupported_on_32bit_kernels(
449 volatile int * m, unsigned long old, unsigned long new);
450 #define __cmpxchg_u64 __cmpxchg_u64_unsupported_on_32bit_kernels
453 /* This function doesn't exist, so you'll get a linker error
454 if something tries to do an invalid cmpxchg(). */
455 extern void __cmpxchg_called_with_bad_pointer(void);
457 static inline unsigned long __cmpxchg(volatile void * ptr, unsigned long old,
458 unsigned long new, int size)
462 return __cmpxchg_u32(ptr, old, new);
464 return __cmpxchg_u64(ptr, old, new);
466 __cmpxchg_called_with_bad_pointer();
470 #define cmpxchg(ptr,old,new) ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(old), (unsigned long)(new),sizeof(*(ptr))))
472 extern void set_handler (unsigned long offset, void *addr, unsigned long len);
473 extern void set_uncached_handler (unsigned long offset, void *addr, unsigned long len);
474 extern void *set_vi_handler (int n, void *addr);
475 extern void *set_except_vector(int n, void *addr);
476 extern unsigned long ebase;
477 extern void per_cpu_trap_init(void);
479 extern NORET_TYPE void die(const char *, struct pt_regs *);
481 static inline void die_if_kernel(const char *str, struct pt_regs *regs)
483 if (unlikely(!user_mode(regs)))
487 extern int stop_a_enabled;
490 * See include/asm-ia64/system.h; prevents deadlock on SMP
493 #define __ARCH_WANT_UNLOCKED_CTXSW
495 #define arch_align_stack(x) (x)
497 #endif /* _ASM_SYSTEM_H */