blob: 024ef42ed6d73e44d510466680f7fbc54710b05c [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>
Heiko Carstens320c04c2008-12-25 13:38:40 +010015#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/types.h>
17#include <asm/ptrace.h>
18#include <asm/setup.h>
Heiko Carstens77fa2242005-06-25 14:55:30 -070019#include <asm/processor.h>
Heiko Carstens484875b2008-04-30 13:38:43 +020020#include <asm/lowcore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#ifdef __KERNEL__
23
24struct task_struct;
25
26extern struct task_struct *__switch_to(void *, void *);
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static inline void save_fp_regs(s390_fp_regs *fpregs)
29{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020030 asm volatile(
31 " std 0,8(%1)\n"
32 " std 2,24(%1)\n"
33 " std 4,40(%1)\n"
34 " std 6,56(%1)"
35 : "=m" (*fpregs) : "a" (fpregs), "m" (*fpregs) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 if (!MACHINE_HAS_IEEE)
37 return;
38 asm volatile(
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020039 " stfpc 0(%1)\n"
40 " std 1,16(%1)\n"
41 " std 3,32(%1)\n"
42 " std 5,48(%1)\n"
43 " std 7,64(%1)\n"
44 " std 8,72(%1)\n"
45 " std 9,80(%1)\n"
46 " std 10,88(%1)\n"
47 " std 11,96(%1)\n"
48 " std 12,104(%1)\n"
49 " std 13,112(%1)\n"
50 " std 14,120(%1)\n"
51 " std 15,128(%1)\n"
52 : "=m" (*fpregs) : "a" (fpregs), "m" (*fpregs) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
55static inline void restore_fp_regs(s390_fp_regs *fpregs)
56{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020057 asm volatile(
58 " ld 0,8(%0)\n"
59 " ld 2,24(%0)\n"
60 " ld 4,40(%0)\n"
61 " ld 6,56(%0)"
62 : : "a" (fpregs), "m" (*fpregs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (!MACHINE_HAS_IEEE)
64 return;
65 asm volatile(
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020066 " lfpc 0(%0)\n"
67 " ld 1,16(%0)\n"
68 " ld 3,32(%0)\n"
69 " ld 5,48(%0)\n"
70 " ld 7,64(%0)\n"
71 " ld 8,72(%0)\n"
72 " ld 9,80(%0)\n"
73 " ld 10,88(%0)\n"
74 " ld 11,96(%0)\n"
75 " ld 12,104(%0)\n"
76 " ld 13,112(%0)\n"
77 " ld 14,120(%0)\n"
78 " ld 15,128(%0)\n"
79 : : "a" (fpregs), "m" (*fpregs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static inline void save_access_regs(unsigned int *acrs)
83{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020084 asm volatile("stam 0,15,0(%0)" : : "a" (acrs) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87static inline void restore_access_regs(unsigned int *acrs)
88{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020089 asm volatile("lam 0,15,0(%0)" : : "a" (acrs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92#define switch_to(prev,next,last) do { \
93 if (prev == next) \
94 break; \
95 save_fp_regs(&prev->thread.fp_regs); \
96 restore_fp_regs(&next->thread.fp_regs); \
97 save_access_regs(&prev->thread.acrs[0]); \
98 restore_access_regs(&next->thread.acrs[0]); \
99 prev = __switch_to(prev,next); \
100} while (0)
101
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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Heiko Carstens29b08d22006-12-04 15:40:40 +0100106#ifdef CONFIG_PFAULT
107extern void pfault_irq_init(void);
108extern int pfault_init(void);
109extern void pfault_fini(void);
110#else /* CONFIG_PFAULT */
111#define pfault_irq_init() do { } while (0)
112#define pfault_init() ({-1;})
113#define pfault_fini() do { } while (0)
114#endif /* CONFIG_PFAULT */
115
Martin Schwidefsky45e576b2008-05-07 09:22:59 +0200116#ifdef CONFIG_PAGE_STATES
117extern void cmma_init(void);
118#else
119static inline void cmma_init(void) { }
120#endif
121
Heiko Carstens5ee24d952005-06-30 02:58:48 -0700122#define finish_arch_switch(prev) do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 set_fs(current->thread.mm_segment); \
Martin Schwidefsky1f1c12a2006-01-14 13:21:03 -0800124 account_vtime(prev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125} while (0)
126
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200127#define nop() asm volatile("nop")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Heiko Carstens5a651c92006-07-17 16:09:18 +0200129#define xchg(ptr,x) \
130({ \
131 __typeof__(*(ptr)) __ret; \
132 __ret = (__typeof__(*(ptr))) \
133 __xchg((unsigned long)(x), (void *)(ptr),sizeof(*(ptr))); \
134 __ret; \
135})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Heiko Carstens210d3a92007-10-12 16:11:41 +0200137extern void __xchg_called_with_bad_pointer(void);
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static inline unsigned long __xchg(unsigned long x, void * ptr, int size)
140{
141 unsigned long addr, old;
142 int shift;
143
144 switch (size) {
145 case 1:
146 addr = (unsigned long) ptr;
147 shift = (3 ^ (addr & 3)) << 3;
148 addr ^= addr & 3;
149 asm volatile(
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200150 " l %0,0(%4)\n"
151 "0: lr 0,%0\n"
152 " nr 0,%3\n"
153 " or 0,%2\n"
154 " cs %0,0,0(%4)\n"
155 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 : "=&d" (old), "=m" (*(int *) addr)
157 : "d" (x << shift), "d" (~(255 << shift)), "a" (addr),
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200158 "m" (*(int *) addr) : "memory", "cc", "0");
Heiko Carstens210d3a92007-10-12 16:11:41 +0200159 return old >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 case 2:
161 addr = (unsigned long) ptr;
162 shift = (2 ^ (addr & 2)) << 3;
163 addr ^= addr & 2;
164 asm volatile(
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200165 " l %0,0(%4)\n"
166 "0: lr 0,%0\n"
167 " nr 0,%3\n"
168 " or 0,%2\n"
169 " cs %0,0,0(%4)\n"
170 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 : "=&d" (old), "=m" (*(int *) addr)
172 : "d" (x << shift), "d" (~(65535 << shift)), "a" (addr),
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200173 "m" (*(int *) addr) : "memory", "cc", "0");
Heiko Carstens210d3a92007-10-12 16:11:41 +0200174 return old >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 case 4:
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200176 asm volatile(
177 " l %0,0(%3)\n"
178 "0: cs %0,%2,0(%3)\n"
179 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 : "=&d" (old), "=m" (*(int *) ptr)
181 : "d" (x), "a" (ptr), "m" (*(int *) ptr)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200182 : "memory", "cc");
Heiko Carstens210d3a92007-10-12 16:11:41 +0200183 return old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#ifdef __s390x__
185 case 8:
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200186 asm volatile(
187 " lg %0,0(%3)\n"
188 "0: csg %0,%2,0(%3)\n"
189 " jl 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 : "=&d" (old), "=m" (*(long *) ptr)
191 : "d" (x), "a" (ptr), "m" (*(long *) ptr)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200192 : "memory", "cc");
Heiko Carstens210d3a92007-10-12 16:11:41 +0200193 return old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#endif /* __s390x__ */
Heiko Carstens210d3a92007-10-12 16:11:41 +0200195 }
196 __xchg_called_with_bad_pointer();
197 return x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200/*
201 * Atomic compare and exchange. Compare OLD with MEM, if identical,
202 * store NEW in MEM. Return the initial value in MEM. Success is
203 * indicated by comparing RETURN with OLD.
204 */
205
206#define __HAVE_ARCH_CMPXCHG 1
207
Mathieu Desnoyersfe413012008-02-07 00:16:24 -0800208#define cmpxchg(ptr, o, n) \
209 ((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(o), \
210 (unsigned long)(n), sizeof(*(ptr))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Heiko Carstens210d3a92007-10-12 16:11:41 +0200212extern void __cmpxchg_called_with_bad_pointer(void);
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static inline unsigned long
215__cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
216{
217 unsigned long addr, prev, tmp;
218 int shift;
219
220 switch (size) {
221 case 1:
222 addr = (unsigned long) ptr;
223 shift = (3 ^ (addr & 3)) << 3;
224 addr ^= addr & 3;
225 asm volatile(
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200226 " l %0,0(%4)\n"
227 "0: nr %0,%5\n"
228 " lr %1,%0\n"
229 " or %0,%2\n"
230 " or %1,%3\n"
231 " cs %0,%1,0(%4)\n"
232 " jnl 1f\n"
233 " xr %1,%0\n"
234 " nr %1,%5\n"
235 " jnz 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 "1:"
237 : "=&d" (prev), "=&d" (tmp)
238 : "d" (old << shift), "d" (new << shift), "a" (ptr),
239 "d" (~(255 << shift))
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200240 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return prev >> shift;
242 case 2:
243 addr = (unsigned long) ptr;
244 shift = (2 ^ (addr & 2)) << 3;
245 addr ^= addr & 2;
246 asm volatile(
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200247 " l %0,0(%4)\n"
248 "0: nr %0,%5\n"
249 " lr %1,%0\n"
250 " or %0,%2\n"
251 " or %1,%3\n"
252 " cs %0,%1,0(%4)\n"
253 " jnl 1f\n"
254 " xr %1,%0\n"
255 " nr %1,%5\n"
256 " jnz 0b\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 "1:"
258 : "=&d" (prev), "=&d" (tmp)
259 : "d" (old << shift), "d" (new << shift), "a" (ptr),
260 "d" (~(65535 << shift))
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200261 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return prev >> shift;
263 case 4:
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200264 asm volatile(
265 " cs %0,%2,0(%3)\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 : "=&d" (prev) : "0" (old), "d" (new), "a" (ptr)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200267 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return prev;
269#ifdef __s390x__
270 case 8:
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200271 asm volatile(
272 " csg %0,%2,0(%3)\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 : "=&d" (prev) : "0" (old), "d" (new), "a" (ptr)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200274 : "memory", "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return prev;
276#endif /* __s390x__ */
277 }
Heiko Carstens210d3a92007-10-12 16:11:41 +0200278 __cmpxchg_called_with_bad_pointer();
279 return old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/*
283 * Force strict CPU ordering.
284 * And yes, this is required on UP too when we're talking
285 * to devices.
286 *
287 * This is very similar to the ppc eieio/sync instruction in that is
288 * does a checkpoint syncronisation & makes sure that
289 * all memory ops have completed wrt other CPU's ( see 7-15 POP DJB ).
290 */
291
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200292#define eieio() asm volatile("bcr 15,0" : : : "memory")
293#define SYNC_OTHER_CORES(x) eieio()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294#define mb() eieio()
295#define rmb() eieio()
296#define wmb() eieio()
297#define read_barrier_depends() do { } while(0)
298#define smp_mb() mb()
299#define smp_rmb() rmb()
300#define smp_wmb() wmb()
301#define smp_read_barrier_depends() read_barrier_depends()
302#define smp_mb__before_clear_bit() smp_mb()
303#define smp_mb__after_clear_bit() smp_mb()
304
305
306#define set_mb(var, value) do { var = value; mb(); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308#ifdef __s390x__
309
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200310#define __ctl_load(array, low, high) ({ \
311 typedef struct { char _[sizeof(array)]; } addrtype; \
312 asm volatile( \
313 " lctlg %1,%2,0(%0)\n" \
314 : : "a" (&array), "i" (low), "i" (high), \
Martin Schwidefskyb57838e2008-06-10 10:03:24 +0200315 "m" (*(addrtype *)(&array))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 })
317
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200318#define __ctl_store(array, low, high) ({ \
319 typedef struct { char _[sizeof(array)]; } addrtype; \
320 asm volatile( \
321 " stctg %2,%3,0(%1)\n" \
Martin Schwidefskyb57838e2008-06-10 10:03:24 +0200322 : "=m" (*(addrtype *)(&array)) \
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200323 : "a" (&array), "i" (low), "i" (high)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 })
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#else /* __s390x__ */
327
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200328#define __ctl_load(array, low, high) ({ \
329 typedef struct { char _[sizeof(array)]; } addrtype; \
330 asm volatile( \
331 " lctl %1,%2,0(%0)\n" \
332 : : "a" (&array), "i" (low), "i" (high), \
Martin Schwidefskyb57838e2008-06-10 10:03:24 +0200333 "m" (*(addrtype *)(&array))); \
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200334})
335
336#define __ctl_store(array, low, high) ({ \
337 typedef struct { char _[sizeof(array)]; } addrtype; \
338 asm volatile( \
339 " stctl %2,%3,0(%1)\n" \
Martin Schwidefskyb57838e2008-06-10 10:03:24 +0200340 : "=m" (*(addrtype *)(&array)) \
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200341 : "a" (&array), "i" (low), "i" (high)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 })
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344#endif /* __s390x__ */
345
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200346#define __ctl_set_bit(cr, bit) ({ \
347 unsigned long __dummy; \
348 __ctl_store(__dummy, cr, cr); \
349 __dummy |= 1UL << (bit); \
350 __ctl_load(__dummy, cr, cr); \
351})
352
353#define __ctl_clear_bit(cr, bit) ({ \
354 unsigned long __dummy; \
355 __ctl_store(__dummy, cr, cr); \
356 __dummy &= ~(1UL << (bit)); \
357 __ctl_load(__dummy, cr, cr); \
358})
359
Heiko Carstens1f194a42006-07-03 00:24:46 -0700360#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Mathieu Desnoyersfe413012008-02-07 00:16:24 -0800362#include <asm-generic/cmpxchg-local.h>
363
364static inline unsigned long __cmpxchg_local(volatile void *ptr,
365 unsigned long old,
366 unsigned long new, int size)
367{
368 switch (size) {
369 case 1:
370 case 2:
371 case 4:
372#ifdef __s390x__
373 case 8:
374#endif
375 return __cmpxchg(ptr, old, new, size);
376 default:
377 return __cmpxchg_local_generic(ptr, old, new, size);
378 }
379
380 return old;
381}
382
383/*
384 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
385 * them available.
386 */
387#define cmpxchg_local(ptr, o, n) \
388 ((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o), \
389 (unsigned long)(n), sizeof(*(ptr))))
390#ifdef __s390x__
391#define cmpxchg64_local(ptr, o, n) \
392 ({ \
393 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
394 cmpxchg_local((ptr), (o), (n)); \
395 })
396#else
397#define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
398#endif
399
Heiko Carstens77fa2242005-06-25 14:55:30 -0700400/*
401 * Use to set psw mask except for the first byte which
402 * won't be changed by this function.
403 */
404static inline void
405__set_psw_mask(unsigned long mask)
406{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200407 __load_psw_mask(mask | (__raw_local_irq_stosm(0x00) & ~(-1UL >> 8)));
Heiko Carstens77fa2242005-06-25 14:55:30 -0700408}
409
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100410#define local_mcck_enable() __set_psw_mask(psw_kernel_bits)
411#define local_mcck_disable() __set_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK)
Heiko Carstens77fa2242005-06-25 14:55:30 -0700412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413#ifdef CONFIG_SMP
414
415extern void smp_ctl_set_bit(int cr, int bit);
416extern void smp_ctl_clear_bit(int cr, int bit);
417#define ctl_set_bit(cr, bit) smp_ctl_set_bit(cr, bit)
418#define ctl_clear_bit(cr, bit) smp_ctl_clear_bit(cr, bit)
419
420#else
421
422#define ctl_set_bit(cr, bit) __ctl_set_bit(cr, bit)
423#define ctl_clear_bit(cr, bit) __ctl_clear_bit(cr, bit)
424
425#endif /* CONFIG_SMP */
426
Heiko Carstens484875b2008-04-30 13:38:43 +0200427static inline unsigned int stfl(void)
428{
429 asm volatile(
430 " .insn s,0xb2b10000,0(0)\n" /* stfl */
431 "0:\n"
432 EX_TABLE(0b,0b));
433 return S390_lowcore.stfl_fac_list;
434}
435
Heiko Carstens320c04c2008-12-25 13:38:40 +0100436static inline int __stfle(unsigned long long *list, int doublewords)
437{
438 typedef struct { unsigned long long _[doublewords]; } addrtype;
439 register unsigned long __nr asm("0") = doublewords - 1;
440
441 asm volatile(".insn s,0xb2b00000,%0" /* stfle */
442 : "=m" (*(addrtype *) list), "+d" (__nr) : : "cc");
443 return __nr + 1;
444}
445
446static inline int stfle(unsigned long long *list, int doublewords)
447{
448 if (!(stfl() & (1UL << 24)))
449 return -EOPNOTSUPP;
450 return __stfle(list, doublewords);
451}
452
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200453static inline unsigned short stap(void)
454{
455 unsigned short cpu_address;
456
457 asm volatile("stap %0" : "=m" (cpu_address));
458 return cpu_address;
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461extern void (*_machine_restart)(char *command);
462extern void (*_machine_halt)(void);
463extern void (*_machine_power_off)(void);
464
465#define arch_align_stack(x) (x)
466
Heiko Carstens411788e2007-11-20 11:13:32 +0100467#ifdef CONFIG_TRACE_IRQFLAGS
468extern psw_t sysc_restore_trace_psw;
469extern psw_t io_restore_trace_psw;
470#endif
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#endif /* __KERNEL__ */
473
474#endif