2 * Copyright IBM Corp. 1999, 2009
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <asm/types.h>
13 #include <asm/ptrace.h>
14 #include <asm/setup.h>
15 #include <asm/processor.h>
16 #include <asm/lowcore.h>
22 extern struct task_struct *__switch_to(void *, void *);
24 static inline void save_fp_regs(s390_fp_regs *fpregs)
28 " std 2,%O0+24(%R0)\n"
29 " std 4,%O0+40(%R0)\n"
31 : "=Q" (*fpregs) : "Q" (*fpregs));
32 if (!MACHINE_HAS_IEEE)
36 " std 1,%O0+16(%R0)\n"
37 " std 3,%O0+32(%R0)\n"
38 " std 5,%O0+48(%R0)\n"
39 " std 7,%O0+64(%R0)\n"
40 " std 8,%O0+72(%R0)\n"
41 " std 9,%O0+80(%R0)\n"
42 " std 10,%O0+88(%R0)\n"
43 " std 11,%O0+96(%R0)\n"
44 " std 12,%O0+104(%R0)\n"
45 " std 13,%O0+112(%R0)\n"
46 " std 14,%O0+120(%R0)\n"
47 " std 15,%O0+128(%R0)\n"
48 : "=Q" (*fpregs) : "Q" (*fpregs));
51 static inline void restore_fp_regs(s390_fp_regs *fpregs)
59 if (!MACHINE_HAS_IEEE)
69 " ld 10,%O0+88(%R0)\n"
70 " ld 11,%O0+96(%R0)\n"
71 " ld 12,%O0+104(%R0)\n"
72 " ld 13,%O0+112(%R0)\n"
73 " ld 14,%O0+120(%R0)\n"
74 " ld 15,%O0+128(%R0)\n"
78 static inline void save_access_regs(unsigned int *acrs)
80 asm volatile("stam 0,15,%0" : "=Q" (*acrs));
83 static inline void restore_access_regs(unsigned int *acrs)
85 asm volatile("lam 0,15,%0" : : "Q" (*acrs));
88 #define switch_to(prev,next,last) do { \
90 save_fp_regs(&prev->thread.fp_regs); \
91 save_access_regs(&prev->thread.acrs[0]); \
94 restore_fp_regs(&next->thread.fp_regs); \
95 restore_access_regs(&next->thread.acrs[0]); \
97 prev = __switch_to(prev,next); \
100 extern void account_vtime(struct task_struct *, struct task_struct *);
101 extern void account_tick_vtime(struct task_struct *);
104 extern void pfault_irq_init(void);
105 extern int pfault_init(void);
106 extern void pfault_fini(void);
107 #else /* CONFIG_PFAULT */
108 #define pfault_irq_init() do { } while (0)
109 #define pfault_init() ({-1;})
110 #define pfault_fini() do { } while (0)
111 #endif /* CONFIG_PFAULT */
113 extern void cmma_init(void);
114 extern int memcpy_real(void *, void *, size_t);
116 #define finish_arch_switch(prev) do { \
117 set_fs(current->thread.mm_segment); \
118 account_vtime(prev, current); \
121 #define nop() asm volatile("nop")
123 #define xchg(ptr,x) \
125 __typeof__(*(ptr)) __ret; \
126 __ret = (__typeof__(*(ptr))) \
127 __xchg((unsigned long)(x), (void *)(ptr),sizeof(*(ptr))); \
131 extern void __xchg_called_with_bad_pointer(void);
133 static inline unsigned long __xchg(unsigned long x, void * ptr, int size)
135 unsigned long addr, old;
140 addr = (unsigned long) ptr;
141 shift = (3 ^ (addr & 3)) << 3;
150 : "=&d" (old), "=Q" (*(int *) addr)
151 : "d" (x << shift), "d" (~(255 << shift)),
152 "Q" (*(int *) addr) : "memory", "cc", "0");
155 addr = (unsigned long) ptr;
156 shift = (2 ^ (addr & 2)) << 3;
165 : "=&d" (old), "=Q" (*(int *) addr)
166 : "d" (x << shift), "d" (~(65535 << shift)),
167 "Q" (*(int *) addr) : "memory", "cc", "0");
174 : "=&d" (old), "=Q" (*(int *) ptr)
175 : "d" (x), "Q" (*(int *) ptr)
184 : "=&d" (old), "=m" (*(long *) ptr)
185 : "d" (x), "Q" (*(long *) ptr)
188 #endif /* __s390x__ */
190 __xchg_called_with_bad_pointer();
195 * Atomic compare and exchange. Compare OLD with MEM, if identical,
196 * store NEW in MEM. Return the initial value in MEM. Success is
197 * indicated by comparing RETURN with OLD.
200 #define __HAVE_ARCH_CMPXCHG 1
202 #define cmpxchg(ptr, o, n) \
203 ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(o), \
204 (unsigned long)(n), sizeof(*(ptr))))
206 extern void __cmpxchg_called_with_bad_pointer(void);
208 static inline unsigned long
209 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
211 unsigned long addr, prev, tmp;
216 addr = (unsigned long) ptr;
217 shift = (3 ^ (addr & 3)) << 3;
231 : "=&d" (prev), "=&d" (tmp), "=Q" (*(int *) ptr)
232 : "d" (old << shift), "d" (new << shift),
233 "d" (~(255 << shift)), "Q" (*(int *) ptr)
235 return prev >> shift;
237 addr = (unsigned long) ptr;
238 shift = (2 ^ (addr & 2)) << 3;
252 : "=&d" (prev), "=&d" (tmp), "=Q" (*(int *) ptr)
253 : "d" (old << shift), "d" (new << shift),
254 "d" (~(65535 << shift)), "Q" (*(int *) ptr)
256 return prev >> shift;
260 : "=&d" (prev), "=Q" (*(int *) ptr)
261 : "0" (old), "d" (new), "Q" (*(int *) ptr)
268 : "=&d" (prev), "=Q" (*(long *) ptr)
269 : "0" (old), "d" (new), "Q" (*(long *) ptr)
272 #endif /* __s390x__ */
274 __cmpxchg_called_with_bad_pointer();
279 * Force strict CPU ordering.
280 * And yes, this is required on UP too when we're talking
283 * This is very similar to the ppc eieio/sync instruction in that is
284 * does a checkpoint syncronisation & makes sure that
285 * all memory ops have completed wrt other CPU's ( see 7-15 POP DJB ).
288 #define eieio() asm volatile("bcr 15,0" : : : "memory")
289 #define SYNC_OTHER_CORES(x) eieio()
291 #define rmb() eieio()
292 #define wmb() eieio()
293 #define read_barrier_depends() do { } while(0)
294 #define smp_mb() mb()
295 #define smp_rmb() rmb()
296 #define smp_wmb() wmb()
297 #define smp_read_barrier_depends() read_barrier_depends()
298 #define smp_mb__before_clear_bit() smp_mb()
299 #define smp_mb__after_clear_bit() smp_mb()
302 #define set_mb(var, value) do { var = value; mb(); } while (0)
306 #define __ctl_load(array, low, high) ({ \
307 typedef struct { char _[sizeof(array)]; } addrtype; \
309 " lctlg %1,%2,%0\n" \
310 : : "Q" (*(addrtype *)(&array)), \
311 "i" (low), "i" (high)); \
314 #define __ctl_store(array, low, high) ({ \
315 typedef struct { char _[sizeof(array)]; } addrtype; \
317 " stctg %1,%2,%0\n" \
318 : "=Q" (*(addrtype *)(&array)) \
319 : "i" (low), "i" (high)); \
322 #else /* __s390x__ */
324 #define __ctl_load(array, low, high) ({ \
325 typedef struct { char _[sizeof(array)]; } addrtype; \
328 : : "Q" (*(addrtype *)(&array)), \
329 "i" (low), "i" (high)); \
332 #define __ctl_store(array, low, high) ({ \
333 typedef struct { char _[sizeof(array)]; } addrtype; \
335 " stctl %1,%2,%0\n" \
336 : "=Q" (*(addrtype *)(&array)) \
337 : "i" (low), "i" (high)); \
340 #endif /* __s390x__ */
342 #define __ctl_set_bit(cr, bit) ({ \
343 unsigned long __dummy; \
344 __ctl_store(__dummy, cr, cr); \
345 __dummy |= 1UL << (bit); \
346 __ctl_load(__dummy, cr, cr); \
349 #define __ctl_clear_bit(cr, bit) ({ \
350 unsigned long __dummy; \
351 __ctl_store(__dummy, cr, cr); \
352 __dummy &= ~(1UL << (bit)); \
353 __ctl_load(__dummy, cr, cr); \
356 #include <linux/irqflags.h>
358 #include <asm-generic/cmpxchg-local.h>
360 static inline unsigned long __cmpxchg_local(volatile void *ptr,
362 unsigned long new, int size)
371 return __cmpxchg(ptr, old, new, size);
373 return __cmpxchg_local_generic(ptr, old, new, size);
380 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
383 #define cmpxchg_local(ptr, o, n) \
384 ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
385 (unsigned long)(n), sizeof(*(ptr))))
387 #define cmpxchg64_local(ptr, o, n) \
389 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
390 cmpxchg_local((ptr), (o), (n)); \
393 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
397 * Use to set psw mask except for the first byte which
398 * won't be changed by this function.
401 __set_psw_mask(unsigned long mask)
403 __load_psw_mask(mask | (arch_local_save_flags() & ~(-1UL >> 8)));
406 #define local_mcck_enable() __set_psw_mask(psw_kernel_bits)
407 #define local_mcck_disable() __set_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK)
411 extern void smp_ctl_set_bit(int cr, int bit);
412 extern void smp_ctl_clear_bit(int cr, int bit);
413 #define ctl_set_bit(cr, bit) smp_ctl_set_bit(cr, bit)
414 #define ctl_clear_bit(cr, bit) smp_ctl_clear_bit(cr, bit)
418 #define ctl_set_bit(cr, bit) __ctl_set_bit(cr, bit)
419 #define ctl_clear_bit(cr, bit) __ctl_clear_bit(cr, bit)
421 #endif /* CONFIG_SMP */
423 #define MAX_FACILITY_BIT (256*8) /* stfle_fac_list has 256 bytes */
426 * The test_facility function uses the bit odering where the MSB is bit 0.
427 * That makes it easier to query facility bits with the bit number as
428 * documented in the Principles of Operation.
430 static inline int test_facility(unsigned long nr)
434 if (nr >= MAX_FACILITY_BIT)
436 ptr = (unsigned char *) &S390_lowcore.stfle_fac_list + (nr >> 3);
437 return (*ptr & (0x80 >> (nr & 7))) != 0;
440 static inline unsigned short stap(void)
442 unsigned short cpu_address;
444 asm volatile("stap %0" : "=m" (cpu_address));
448 extern void (*_machine_restart)(char *command);
449 extern void (*_machine_halt)(void);
450 extern void (*_machine_power_off)(void);
452 #define arch_align_stack(x) (x)
454 static inline int tprot(unsigned long addr)
464 : "+d" (rc) : "a" (addr) : "cc");
468 #endif /* __KERNEL__ */