blob: c819ae25a8425ddfd72136dda4d674bf0f8b0ac1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * include/asm-s390/system.h
3 *
4 * S390 version
5 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
7 *
8 * Derived from "include/asm-i386/system.h"
9 */
10
11#ifndef __ASM_SYSTEM_H
12#define __ASM_SYSTEM_H
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <asm/types.h>
16#include <asm/ptrace.h>
17#include <asm/setup.h>
Heiko Carstens77fa2242005-06-25 14:55:30 -070018#include <asm/processor.h>
Heiko Carstens484875b2008-04-30 13:38:43 +020019#include <asm/lowcore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#ifdef __KERNEL__
22
23struct task_struct;
24
25extern struct task_struct *__switch_to(void *, void *);
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static inline void save_fp_regs(s390_fp_regs *fpregs)
28{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020029 asm volatile(
30 " std 0,8(%1)\n"
31 " std 2,24(%1)\n"
32 " std 4,40(%1)\n"
33 " std 6,56(%1)"
34 : "=m" (*fpregs) : "a" (fpregs), "m" (*fpregs) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 if (!MACHINE_HAS_IEEE)
36 return;
37 asm volatile(
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020038 " stfpc 0(%1)\n"
39 " std 1,16(%1)\n"
40 " std 3,32(%1)\n"
41 " std 5,48(%1)\n"
42 " std 7,64(%1)\n"
43 " std 8,72(%1)\n"
44 " std 9,80(%1)\n"
45 " std 10,88(%1)\n"
46 " std 11,96(%1)\n"
47 " std 12,104(%1)\n"
48 " std 13,112(%1)\n"
49 " std 14,120(%1)\n"
50 " std 15,128(%1)\n"
51 : "=m" (*fpregs) : "a" (fpregs), "m" (*fpregs) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
54static inline void restore_fp_regs(s390_fp_regs *fpregs)
55{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020056 asm volatile(
57 " ld 0,8(%0)\n"
58 " ld 2,24(%0)\n"
59 " ld 4,40(%0)\n"
60 " ld 6,56(%0)"
61 : : "a" (fpregs), "m" (*fpregs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (!MACHINE_HAS_IEEE)
63 return;
64 asm volatile(
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020065 " lfpc 0(%0)\n"
66 " ld 1,16(%0)\n"
67 " ld 3,32(%0)\n"
68 " ld 5,48(%0)\n"
69 " ld 7,64(%0)\n"
70 " ld 8,72(%0)\n"
71 " ld 9,80(%0)\n"
72 " ld 10,88(%0)\n"
73 " ld 11,96(%0)\n"
74 " ld 12,104(%0)\n"
75 " ld 13,112(%0)\n"
76 " ld 14,120(%0)\n"
77 " ld 15,128(%0)\n"
78 : : "a" (fpregs), "m" (*fpregs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static inline void save_access_regs(unsigned int *acrs)
82{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020083 asm volatile("stam 0,15,0(%0)" : : "a" (acrs) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86static inline void restore_access_regs(unsigned int *acrs)
87{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020088 asm volatile("lam 0,15,0(%0)" : : "a" (acrs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91#define switch_to(prev,next,last) do { \
92 if (prev == next) \
93 break; \
94 save_fp_regs(&prev->thread.fp_regs); \
95 restore_fp_regs(&next->thread.fp_regs); \
96 save_access_regs(&prev->thread.acrs[0]); \
97 restore_access_regs(&next->thread.acrs[0]); \
98 prev = __switch_to(prev,next); \
99} while (0)
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#ifdef CONFIG_VIRT_CPU_ACCOUNTING
Martin Schwidefsky1f1c12a2006-01-14 13:21:03 -0800102extern void account_vtime(struct task_struct *);
103extern void account_tick_vtime(struct task_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104extern void account_system_vtime(struct task_struct *);
Jan Blunck5c833892006-03-06 15:42:46 -0800105#else
106#define account_vtime(x) do { /* empty */ } while (0)
Nick Piggin4866cde2005-06-25 14:57:23 -0700107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Heiko Carstens29b08d22006-12-04 15:40:40 +0100109#ifdef CONFIG_PFAULT
110extern void pfault_irq_init(void);
111extern int pfault_init(void);
112extern void pfault_fini(void);
113#else /* CONFIG_PFAULT */
114#define pfault_irq_init() do { } while (0)
115#define pfault_init() ({-1;})
116#define pfault_fini() do { } while (0)
117#endif /* CONFIG_PFAULT */
118
Heiko Carstens5ee24d952005-06-30 02:58:48 -0700119#define finish_arch_switch(prev) do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 set_fs(current->thread.mm_segment); \
Martin Schwidefsky1f1c12a2006-01-14 13:21:03 -0800121 account_vtime(prev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122} while (0)
123
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200124#define nop() asm volatile("nop")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Heiko Carstens5a651c92006-07-17 16:09:18 +0200126#define xchg(ptr,x) \
127({ \
128 __typeof__(*(ptr)) __ret; \
129 __ret = (__typeof__(*(ptr))) \
130 __xchg((unsigned long)(x), (void *)(ptr),sizeof(*(ptr))); \
131 __ret; \
132})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Heiko Carstens210d3a92007-10-12 16:11:41 +0200134extern void __xchg_called_with_bad_pointer(void);
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static inline unsigned long __xchg(unsigned long x, void * ptr, int size)
137{
138 unsigned long addr, old;
139 int shift;
140
141 switch (size) {
142 case 1:
143 addr = (unsigned long) ptr;
144 shift = (3 ^ (addr & 3)) << 3;
145 addr ^= addr & 3;
146 asm volatile(
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200147 " l %0,0(%4)\n"
148 "0: lr 0,%0\n"
149 " nr 0,%3\n"
150 " or 0,%2\n"
151 " cs %0,0,0(%4)\n"
152 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 : "=&d" (old), "=m" (*(int *) addr)
154 : "d" (x << shift), "d" (~(255 << shift)), "a" (addr),
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200155 "m" (*(int *) addr) : "memory", "cc", "0");
Heiko Carstens210d3a92007-10-12 16:11:41 +0200156 return old >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 case 2:
158 addr = (unsigned long) ptr;
159 shift = (2 ^ (addr & 2)) << 3;
160 addr ^= addr & 2;
161 asm volatile(
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200162 " l %0,0(%4)\n"
163 "0: lr 0,%0\n"
164 " nr 0,%3\n"
165 " or 0,%2\n"
166 " cs %0,0,0(%4)\n"
167 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 : "=&d" (old), "=m" (*(int *) addr)
169 : "d" (x << shift), "d" (~(65535 << shift)), "a" (addr),
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200170 "m" (*(int *) addr) : "memory", "cc", "0");
Heiko Carstens210d3a92007-10-12 16:11:41 +0200171 return old >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 case 4:
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200173 asm volatile(
174 " l %0,0(%3)\n"
175 "0: cs %0,%2,0(%3)\n"
176 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 : "=&d" (old), "=m" (*(int *) ptr)
178 : "d" (x), "a" (ptr), "m" (*(int *) ptr)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200179 : "memory", "cc");
Heiko Carstens210d3a92007-10-12 16:11:41 +0200180 return old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#ifdef __s390x__
182 case 8:
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200183 asm volatile(
184 " lg %0,0(%3)\n"
185 "0: csg %0,%2,0(%3)\n"
186 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 : "=&d" (old), "=m" (*(long *) ptr)
188 : "d" (x), "a" (ptr), "m" (*(long *) ptr)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200189 : "memory", "cc");
Heiko Carstens210d3a92007-10-12 16:11:41 +0200190 return old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#endif /* __s390x__ */
Heiko Carstens210d3a92007-10-12 16:11:41 +0200192 }
193 __xchg_called_with_bad_pointer();
194 return x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/*
198 * Atomic compare and exchange. Compare OLD with MEM, if identical,
199 * store NEW in MEM. Return the initial value in MEM. Success is
200 * indicated by comparing RETURN with OLD.
201 */
202
203#define __HAVE_ARCH_CMPXCHG 1
204
Mathieu Desnoyersfe413012008-02-07 00:16:24 -0800205#define cmpxchg(ptr, o, n) \
206 ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(o), \
207 (unsigned long)(n), sizeof(*(ptr))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Heiko Carstens210d3a92007-10-12 16:11:41 +0200209extern void __cmpxchg_called_with_bad_pointer(void);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static inline unsigned long
212__cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
213{
214 unsigned long addr, prev, tmp;
215 int shift;
216
217 switch (size) {
218 case 1:
219 addr = (unsigned long) ptr;
220 shift = (3 ^ (addr & 3)) << 3;
221 addr ^= addr & 3;
222 asm volatile(
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200223 " l %0,0(%4)\n"
224 "0: nr %0,%5\n"
225 " lr %1,%0\n"
226 " or %0,%2\n"
227 " or %1,%3\n"
228 " cs %0,%1,0(%4)\n"
229 " jnl 1f\n"
230 " xr %1,%0\n"
231 " nr %1,%5\n"
232 " jnz 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 "1:"
234 : "=&d" (prev), "=&d" (tmp)
235 : "d" (old << shift), "d" (new << shift), "a" (ptr),
236 "d" (~(255 << shift))
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200237 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return prev >> shift;
239 case 2:
240 addr = (unsigned long) ptr;
241 shift = (2 ^ (addr & 2)) << 3;
242 addr ^= addr & 2;
243 asm volatile(
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200244 " l %0,0(%4)\n"
245 "0: nr %0,%5\n"
246 " lr %1,%0\n"
247 " or %0,%2\n"
248 " or %1,%3\n"
249 " cs %0,%1,0(%4)\n"
250 " jnl 1f\n"
251 " xr %1,%0\n"
252 " nr %1,%5\n"
253 " jnz 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 "1:"
255 : "=&d" (prev), "=&d" (tmp)
256 : "d" (old << shift), "d" (new << shift), "a" (ptr),
257 "d" (~(65535 << shift))
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200258 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return prev >> shift;
260 case 4:
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200261 asm volatile(
262 " cs %0,%2,0(%3)\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 : "=&d" (prev) : "0" (old), "d" (new), "a" (ptr)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200264 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return prev;
266#ifdef __s390x__
267 case 8:
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200268 asm volatile(
269 " csg %0,%2,0(%3)\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 : "=&d" (prev) : "0" (old), "d" (new), "a" (ptr)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200271 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return prev;
273#endif /* __s390x__ */
274 }
Heiko Carstens210d3a92007-10-12 16:11:41 +0200275 __cmpxchg_called_with_bad_pointer();
276 return old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279/*
280 * Force strict CPU ordering.
281 * And yes, this is required on UP too when we're talking
282 * to devices.
283 *
284 * This is very similar to the ppc eieio/sync instruction in that is
285 * does a checkpoint syncronisation & makes sure that
286 * all memory ops have completed wrt other CPU's ( see 7-15 POP DJB ).
287 */
288
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200289#define eieio() asm volatile("bcr 15,0" : : : "memory")
290#define SYNC_OTHER_CORES(x) eieio()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291#define mb() eieio()
292#define rmb() eieio()
293#define wmb() eieio()
294#define read_barrier_depends() do { } while(0)
295#define smp_mb() mb()
296#define smp_rmb() rmb()
297#define smp_wmb() wmb()
298#define smp_read_barrier_depends() read_barrier_depends()
299#define smp_mb__before_clear_bit() smp_mb()
300#define smp_mb__after_clear_bit() smp_mb()
301
302
303#define set_mb(var, value) do { var = value; mb(); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#ifdef __s390x__
306
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200307#define __ctl_load(array, low, high) ({ \
308 typedef struct { char _[sizeof(array)]; } addrtype; \
309 asm volatile( \
310 " lctlg %1,%2,0(%0)\n" \
311 : : "a" (&array), "i" (low), "i" (high), \
312 "m" (*(addrtype *)(array))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 })
314
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200315#define __ctl_store(array, low, high) ({ \
316 typedef struct { char _[sizeof(array)]; } addrtype; \
317 asm volatile( \
318 " stctg %2,%3,0(%1)\n" \
319 : "=m" (*(addrtype *)(array)) \
320 : "a" (&array), "i" (low), "i" (high)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 })
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#else /* __s390x__ */
324
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200325#define __ctl_load(array, low, high) ({ \
326 typedef struct { char _[sizeof(array)]; } addrtype; \
327 asm volatile( \
328 " lctl %1,%2,0(%0)\n" \
329 : : "a" (&array), "i" (low), "i" (high), \
330 "m" (*(addrtype *)(array))); \
331})
332
333#define __ctl_store(array, low, high) ({ \
334 typedef struct { char _[sizeof(array)]; } addrtype; \
335 asm volatile( \
336 " stctl %2,%3,0(%1)\n" \
337 : "=m" (*(addrtype *)(array)) \
338 : "a" (&array), "i" (low), "i" (high)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 })
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#endif /* __s390x__ */
342
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200343#define __ctl_set_bit(cr, bit) ({ \
344 unsigned long __dummy; \
345 __ctl_store(__dummy, cr, cr); \
346 __dummy |= 1UL << (bit); \
347 __ctl_load(__dummy, cr, cr); \
348})
349
350#define __ctl_clear_bit(cr, bit) ({ \
351 unsigned long __dummy; \
352 __ctl_store(__dummy, cr, cr); \
353 __dummy &= ~(1UL << (bit)); \
354 __ctl_load(__dummy, cr, cr); \
355})
356
Heiko Carstens1f194a42006-07-03 00:24:46 -0700357#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Mathieu Desnoyersfe413012008-02-07 00:16:24 -0800359#include <asm-generic/cmpxchg-local.h>
360
361static inline unsigned long __cmpxchg_local(volatile void *ptr,
362 unsigned long old,
363 unsigned long new, int size)
364{
365 switch (size) {
366 case 1:
367 case 2:
368 case 4:
369#ifdef __s390x__
370 case 8:
371#endif
372 return __cmpxchg(ptr, old, new, size);
373 default:
374 return __cmpxchg_local_generic(ptr, old, new, size);
375 }
376
377 return old;
378}
379
380/*
381 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
382 * them available.
383 */
384#define cmpxchg_local(ptr, o, n) \
385 ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
386 (unsigned long)(n), sizeof(*(ptr))))
387#ifdef __s390x__
388#define cmpxchg64_local(ptr, o, n) \
389 ({ \
390 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
391 cmpxchg_local((ptr), (o), (n)); \
392 })
393#else
394#define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
395#endif
396
Heiko Carstens77fa2242005-06-25 14:55:30 -0700397/*
398 * Use to set psw mask except for the first byte which
399 * won't be changed by this function.
400 */
401static inline void
402__set_psw_mask(unsigned long mask)
403{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200404 __load_psw_mask(mask | (__raw_local_irq_stosm(0x00) & ~(-1UL >> 8)));
Heiko Carstens77fa2242005-06-25 14:55:30 -0700405}
406
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100407#define local_mcck_enable() __set_psw_mask(psw_kernel_bits)
408#define local_mcck_disable() __set_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK)
Heiko Carstens77fa2242005-06-25 14:55:30 -0700409
Heiko Carstens7b7583892008-04-17 07:46:11 +0200410int stfle(unsigned long long *list, int doublewords);
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#ifdef CONFIG_SMP
413
414extern void smp_ctl_set_bit(int cr, int bit);
415extern void smp_ctl_clear_bit(int cr, int bit);
416#define ctl_set_bit(cr, bit) smp_ctl_set_bit(cr, bit)
417#define ctl_clear_bit(cr, bit) smp_ctl_clear_bit(cr, bit)
418
419#else
420
421#define ctl_set_bit(cr, bit) __ctl_set_bit(cr, bit)
422#define ctl_clear_bit(cr, bit) __ctl_clear_bit(cr, bit)
423
424#endif /* CONFIG_SMP */
425
Heiko Carstens484875b2008-04-30 13:38:43 +0200426static inline unsigned int stfl(void)
427{
428 asm volatile(
429 " .insn s,0xb2b10000,0(0)\n" /* stfl */
430 "0:\n"
431 EX_TABLE(0b,0b));
432 return S390_lowcore.stfl_fac_list;
433}
434
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200435static inline unsigned short stap(void)
436{
437 unsigned short cpu_address;
438
439 asm volatile("stap %0" : "=m" (cpu_address));
440 return cpu_address;
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443extern void (*_machine_restart)(char *command);
444extern void (*_machine_halt)(void);
445extern void (*_machine_power_off)(void);
446
447#define arch_align_stack(x) (x)
448
Heiko Carstens411788e2007-11-20 11:13:32 +0100449#ifdef CONFIG_TRACE_IRQFLAGS
450extern psw_t sysc_restore_trace_psw;
451extern psw_t io_restore_trace_psw;
452#endif
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454#endif /* __KERNEL__ */
455
456#endif