system.h 11.2 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  include/asm-s390/system.h
 *
 *  S390 version
 *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
 *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
 *
 *  Derived from "include/asm-i386/system.h"
 */

#ifndef __ASM_SYSTEM_H
#define __ASM_SYSTEM_H

#include <linux/kernel.h>
15
#include <linux/errno.h>
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18
#include <asm/types.h>
#include <asm/ptrace.h>
#include <asm/setup.h>
19
#include <asm/processor.h>
20
#include <asm/lowcore.h>
Linus Torvalds's avatar
Linus Torvalds committed
21 22 23 24 25 26 27 28 29

#ifdef __KERNEL__

struct task_struct;

extern struct task_struct *__switch_to(void *, void *);

static inline void save_fp_regs(s390_fp_regs *fpregs)
{
30 31 32 33 34 35
	asm volatile(
		"	std	0,8(%1)\n"
		"	std	2,24(%1)\n"
		"	std	4,40(%1)\n"
		"	std	6,56(%1)"
		: "=m" (*fpregs) : "a" (fpregs), "m" (*fpregs) : "memory");
Linus Torvalds's avatar
Linus Torvalds committed
36 37 38
	if (!MACHINE_HAS_IEEE)
		return;
	asm volatile(
39 40 41 42 43 44 45 46 47 48 49 50 51 52
		"	stfpc	0(%1)\n"
		"	std	1,16(%1)\n"
		"	std	3,32(%1)\n"
		"	std	5,48(%1)\n"
		"	std	7,64(%1)\n"
		"	std	8,72(%1)\n"
		"	std	9,80(%1)\n"
		"	std	10,88(%1)\n"
		"	std	11,96(%1)\n"
		"	std	12,104(%1)\n"
		"	std	13,112(%1)\n"
		"	std	14,120(%1)\n"
		"	std	15,128(%1)\n"
		: "=m" (*fpregs) : "a" (fpregs), "m" (*fpregs) : "memory");
Linus Torvalds's avatar
Linus Torvalds committed
53 54 55 56
}

static inline void restore_fp_regs(s390_fp_regs *fpregs)
{
57 58 59 60 61 62
	asm volatile(
		"	ld	0,8(%0)\n"
		"	ld	2,24(%0)\n"
		"	ld	4,40(%0)\n"
		"	ld	6,56(%0)"
		: : "a" (fpregs), "m" (*fpregs));
Linus Torvalds's avatar
Linus Torvalds committed
63 64 65
	if (!MACHINE_HAS_IEEE)
		return;
	asm volatile(
66 67 68 69 70 71 72 73 74 75 76 77 78 79
		"	lfpc	0(%0)\n"
		"	ld	1,16(%0)\n"
		"	ld	3,32(%0)\n"
		"	ld	5,48(%0)\n"
		"	ld	7,64(%0)\n"
		"	ld	8,72(%0)\n"
		"	ld	9,80(%0)\n"
		"	ld	10,88(%0)\n"
		"	ld	11,96(%0)\n"
		"	ld	12,104(%0)\n"
		"	ld	13,112(%0)\n"
		"	ld	14,120(%0)\n"
		"	ld	15,128(%0)\n"
		: : "a" (fpregs), "m" (*fpregs));
Linus Torvalds's avatar
Linus Torvalds committed
80 81 82 83
}

static inline void save_access_regs(unsigned int *acrs)
{
84
	asm volatile("stam 0,15,0(%0)" : : "a" (acrs) : "memory");
Linus Torvalds's avatar
Linus Torvalds committed
85 86 87 88
}

static inline void restore_access_regs(unsigned int *acrs)
{
89
	asm volatile("lam 0,15,0(%0)" : : "a" (acrs));
Linus Torvalds's avatar
Linus Torvalds committed
90 91 92 93 94 95 96 97 98 99 100 101
}

#define switch_to(prev,next,last) do {					     \
	if (prev == next)						     \
		break;							     \
	save_fp_regs(&prev->thread.fp_regs);				     \
	restore_fp_regs(&next->thread.fp_regs);				     \
	save_access_regs(&prev->thread.acrs[0]);			     \
	restore_access_regs(&next->thread.acrs[0]);			     \
	prev = __switch_to(prev,next);					     \
} while (0)

102 103
extern void account_vtime(struct task_struct *);
extern void account_tick_vtime(struct task_struct *);
Linus Torvalds's avatar
Linus Torvalds committed
104 105
extern void account_system_vtime(struct task_struct *);

Heiko Carstens's avatar
Heiko Carstens committed
106 107 108 109 110 111 112 113 114 115
#ifdef CONFIG_PFAULT
extern void pfault_irq_init(void);
extern int pfault_init(void);
extern void pfault_fini(void);
#else /* CONFIG_PFAULT */
#define pfault_irq_init()	do { } while (0)
#define pfault_init()		({-1;})
#define pfault_fini()		do { } while (0)
#endif /* CONFIG_PFAULT */

116 117 118 119 120 121
#ifdef CONFIG_PAGE_STATES
extern void cmma_init(void);
#else
static inline void cmma_init(void) { }
#endif

122
#define finish_arch_switch(prev) do {					     \
Linus Torvalds's avatar
Linus Torvalds committed
123
	set_fs(current->thread.mm_segment);				     \
124
	account_vtime(prev);						     \
Linus Torvalds's avatar
Linus Torvalds committed
125 126
} while (0)

127
#define nop() asm volatile("nop")
Linus Torvalds's avatar
Linus Torvalds committed
128

129 130 131 132 133 134 135
#define xchg(ptr,x)							  \
({									  \
	__typeof__(*(ptr)) __ret;					  \
	__ret = (__typeof__(*(ptr)))					  \
		__xchg((unsigned long)(x), (void *)(ptr),sizeof(*(ptr))); \
	__ret;								  \
})
Linus Torvalds's avatar
Linus Torvalds committed
136

137 138
extern void __xchg_called_with_bad_pointer(void);

Linus Torvalds's avatar
Linus Torvalds committed
139 140 141 142 143 144 145 146 147 148 149
static inline unsigned long __xchg(unsigned long x, void * ptr, int size)
{
	unsigned long addr, old;
	int shift;

        switch (size) {
	case 1:
		addr = (unsigned long) ptr;
		shift = (3 ^ (addr & 3)) << 3;
		addr ^= addr & 3;
		asm volatile(
150 151 152 153 154 155
			"	l	%0,0(%4)\n"
			"0:	lr	0,%0\n"
			"	nr	0,%3\n"
			"	or	0,%2\n"
			"	cs	%0,0,0(%4)\n"
			"	jl	0b\n"
Linus Torvalds's avatar
Linus Torvalds committed
156 157
			: "=&d" (old), "=m" (*(int *) addr)
			: "d" (x << shift), "d" (~(255 << shift)), "a" (addr),
158
			  "m" (*(int *) addr) : "memory", "cc", "0");
159
		return old >> shift;
Linus Torvalds's avatar
Linus Torvalds committed
160 161 162 163 164
	case 2:
		addr = (unsigned long) ptr;
		shift = (2 ^ (addr & 2)) << 3;
		addr ^= addr & 2;
		asm volatile(
165 166 167 168 169 170
			"	l	%0,0(%4)\n"
			"0:	lr	0,%0\n"
			"	nr	0,%3\n"
			"	or	0,%2\n"
			"	cs	%0,0,0(%4)\n"
			"	jl	0b\n"
Linus Torvalds's avatar
Linus Torvalds committed
171 172
			: "=&d" (old), "=m" (*(int *) addr)
			: "d" (x << shift), "d" (~(65535 << shift)), "a" (addr),
173
			  "m" (*(int *) addr) : "memory", "cc", "0");
174
		return old >> shift;
Linus Torvalds's avatar
Linus Torvalds committed
175
	case 4:
176 177 178 179
		asm volatile(
			"	l	%0,0(%3)\n"
			"0:	cs	%0,%2,0(%3)\n"
			"	jl	0b\n"
Linus Torvalds's avatar
Linus Torvalds committed
180 181
			: "=&d" (old), "=m" (*(int *) ptr)
			: "d" (x), "a" (ptr), "m" (*(int *) ptr)
182
			: "memory", "cc");
183
		return old;
Linus Torvalds's avatar
Linus Torvalds committed
184 185
#ifdef __s390x__
	case 8:
186 187 188 189
		asm volatile(
			"	lg	%0,0(%3)\n"
			"0:	csg	%0,%2,0(%3)\n"
			"	jl	0b\n"
Linus Torvalds's avatar
Linus Torvalds committed
190 191
			: "=&d" (old), "=m" (*(long *) ptr)
			: "d" (x), "a" (ptr), "m" (*(long *) ptr)
192
			: "memory", "cc");
193
		return old;
Linus Torvalds's avatar
Linus Torvalds committed
194
#endif /* __s390x__ */
195 196 197
	}
	__xchg_called_with_bad_pointer();
	return x;
Linus Torvalds's avatar
Linus Torvalds committed
198 199 200 201 202 203 204 205 206 207
}

/*
 * Atomic compare and exchange.  Compare OLD with MEM, if identical,
 * store NEW in MEM.  Return the initial value in MEM.  Success is
 * indicated by comparing RETURN with OLD.
 */

#define __HAVE_ARCH_CMPXCHG 1

Mathieu Desnoyers's avatar
Mathieu Desnoyers committed
208 209 210
#define cmpxchg(ptr, o, n)						\
	((__typeof__(*(ptr)))__cmpxchg((ptr), (unsigned long)(o),	\
					(unsigned long)(n), sizeof(*(ptr))))
Linus Torvalds's avatar
Linus Torvalds committed
211

212 213
extern void __cmpxchg_called_with_bad_pointer(void);

Linus Torvalds's avatar
Linus Torvalds committed
214 215 216 217 218 219 220 221 222 223 224 225
static inline unsigned long
__cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
{
	unsigned long addr, prev, tmp;
	int shift;

        switch (size) {
	case 1:
		addr = (unsigned long) ptr;
		shift = (3 ^ (addr & 3)) << 3;
		addr ^= addr & 3;
		asm volatile(
226 227 228 229 230 231 232 233 234 235
			"	l	%0,0(%4)\n"
			"0:	nr	%0,%5\n"
			"	lr	%1,%0\n"
			"	or	%0,%2\n"
			"	or	%1,%3\n"
			"	cs	%0,%1,0(%4)\n"
			"	jnl	1f\n"
			"	xr	%1,%0\n"
			"	nr	%1,%5\n"
			"	jnz	0b\n"
Linus Torvalds's avatar
Linus Torvalds committed
236 237 238 239
			"1:"
			: "=&d" (prev), "=&d" (tmp)
			: "d" (old << shift), "d" (new << shift), "a" (ptr),
			  "d" (~(255 << shift))
240
			: "memory", "cc");
Linus Torvalds's avatar
Linus Torvalds committed
241 242 243 244 245 246
		return prev >> shift;
	case 2:
		addr = (unsigned long) ptr;
		shift = (2 ^ (addr & 2)) << 3;
		addr ^= addr & 2;
		asm volatile(
247 248 249 250 251 252 253 254 255 256
			"	l	%0,0(%4)\n"
			"0:	nr	%0,%5\n"
			"	lr	%1,%0\n"
			"	or	%0,%2\n"
			"	or	%1,%3\n"
			"	cs	%0,%1,0(%4)\n"
			"	jnl	1f\n"
			"	xr	%1,%0\n"
			"	nr	%1,%5\n"
			"	jnz	0b\n"
Linus Torvalds's avatar
Linus Torvalds committed
257 258 259 260
			"1:"
			: "=&d" (prev), "=&d" (tmp)
			: "d" (old << shift), "d" (new << shift), "a" (ptr),
			  "d" (~(65535 << shift))
261
			: "memory", "cc");
Linus Torvalds's avatar
Linus Torvalds committed
262 263
		return prev >> shift;
	case 4:
264 265
		asm volatile(
			"	cs	%0,%2,0(%3)\n"
Linus Torvalds's avatar
Linus Torvalds committed
266
			: "=&d" (prev) : "0" (old), "d" (new), "a" (ptr)
267
			: "memory", "cc");
Linus Torvalds's avatar
Linus Torvalds committed
268 269 270
		return prev;
#ifdef __s390x__
	case 8:
271 272
		asm volatile(
			"	csg	%0,%2,0(%3)\n"
Linus Torvalds's avatar
Linus Torvalds committed
273
			: "=&d" (prev) : "0" (old), "d" (new), "a" (ptr)
274
			: "memory", "cc");
Linus Torvalds's avatar
Linus Torvalds committed
275 276 277
		return prev;
#endif /* __s390x__ */
        }
278 279
	__cmpxchg_called_with_bad_pointer();
	return old;
Linus Torvalds's avatar
Linus Torvalds committed
280 281 282 283 284 285 286 287 288 289 290 291
}

/*
 * Force strict CPU ordering.
 * And yes, this is required on UP too when we're talking
 * to devices.
 *
 * This is very similar to the ppc eieio/sync instruction in that is
 * does a checkpoint syncronisation & makes sure that 
 * all memory ops have completed wrt other CPU's ( see 7-15 POP  DJB ).
 */

292 293
#define eieio()	asm volatile("bcr 15,0" : : : "memory")
#define SYNC_OTHER_CORES(x)   eieio()
Linus Torvalds's avatar
Linus Torvalds committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
#define mb()    eieio()
#define rmb()   eieio()
#define wmb()   eieio()
#define read_barrier_depends() do { } while(0)
#define smp_mb()       mb()
#define smp_rmb()      rmb()
#define smp_wmb()      wmb()
#define smp_read_barrier_depends()    read_barrier_depends()
#define smp_mb__before_clear_bit()     smp_mb()
#define smp_mb__after_clear_bit()      smp_mb()


#define set_mb(var, value)      do { var = value; mb(); } while (0)

#ifdef __s390x__

310 311 312 313 314
#define __ctl_load(array, low, high) ({				\
	typedef struct { char _[sizeof(array)]; } addrtype;	\
	asm volatile(						\
		"	lctlg	%1,%2,0(%0)\n"			\
		: : "a" (&array), "i" (low), "i" (high),	\
315
		    "m" (*(addrtype *)(&array)));		\
Linus Torvalds's avatar
Linus Torvalds committed
316 317
	})

318 319 320 321
#define __ctl_store(array, low, high) ({			\
	typedef struct { char _[sizeof(array)]; } addrtype;	\
	asm volatile(						\
		"	stctg	%2,%3,0(%1)\n"			\
322
		: "=m" (*(addrtype *)(&array))			\
323
		: "a" (&array), "i" (low), "i" (high));		\
Linus Torvalds's avatar
Linus Torvalds committed
324 325 326 327
	})

#else /* __s390x__ */

328 329 330 331 332
#define __ctl_load(array, low, high) ({				\
	typedef struct { char _[sizeof(array)]; } addrtype;	\
	asm volatile(						\
		"	lctl	%1,%2,0(%0)\n"			\
		: : "a" (&array), "i" (low), "i" (high),	\
333
		    "m" (*(addrtype *)(&array)));		\
334
})
Linus Torvalds's avatar
Linus Torvalds committed
335

336 337 338 339
#define __ctl_store(array, low, high) ({			\
	typedef struct { char _[sizeof(array)]; } addrtype;	\
	asm volatile(						\
		"	stctl	%2,%3,0(%1)\n"			\
340
		: "=m" (*(addrtype *)(&array))			\
341
		: "a" (&array), "i" (low), "i" (high));		\
Linus Torvalds's avatar
Linus Torvalds committed
342 343 344 345
	})

#endif /* __s390x__ */

346 347 348 349 350 351 352 353 354 355 356 357 358 359
#define __ctl_set_bit(cr, bit) ({	\
	unsigned long __dummy;		\
	__ctl_store(__dummy, cr, cr);	\
	__dummy |= 1UL << (bit);	\
	__ctl_load(__dummy, cr, cr);	\
})

#define __ctl_clear_bit(cr, bit) ({	\
	unsigned long __dummy;		\
	__ctl_store(__dummy, cr, cr);	\
	__dummy &= ~(1UL << (bit));	\
	__ctl_load(__dummy, cr, cr);	\
})

360
#include <linux/irqflags.h>
Linus Torvalds's avatar
Linus Torvalds committed
361

Mathieu Desnoyers's avatar
Mathieu Desnoyers committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
#include <asm-generic/cmpxchg-local.h>

static inline unsigned long __cmpxchg_local(volatile void *ptr,
				      unsigned long old,
				      unsigned long new, int size)
{
	switch (size) {
	case 1:
	case 2:
	case 4:
#ifdef __s390x__
	case 8:
#endif
		return __cmpxchg(ptr, old, new, size);
	default:
		return __cmpxchg_local_generic(ptr, old, new, size);
	}

	return old;
}

/*
 * cmpxchg_local and cmpxchg64_local are atomic wrt current CPU. Always make
 * them available.
 */
#define cmpxchg_local(ptr, o, n)					\
	((__typeof__(*(ptr)))__cmpxchg_local((ptr), (unsigned long)(o),	\
			(unsigned long)(n), sizeof(*(ptr))))
#ifdef __s390x__
#define cmpxchg64_local(ptr, o, n)					\
  ({									\
	BUILD_BUG_ON(sizeof(*(ptr)) != 8);				\
	cmpxchg_local((ptr), (o), (n));					\
  })
#else
#define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
#endif

400 401 402 403 404 405 406
/*
 * Use to set psw mask except for the first byte which
 * won't be changed by this function.
 */
static inline void
__set_psw_mask(unsigned long mask)
{
407
	__load_psw_mask(mask | (__raw_local_irq_stosm(0x00) & ~(-1UL >> 8)));
408 409
}

Gerald Schaefer's avatar
Gerald Schaefer committed
410 411
#define local_mcck_enable()  __set_psw_mask(psw_kernel_bits)
#define local_mcck_disable() __set_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK)
412

Linus Torvalds's avatar
Linus Torvalds committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426
#ifdef CONFIG_SMP

extern void smp_ctl_set_bit(int cr, int bit);
extern void smp_ctl_clear_bit(int cr, int bit);
#define ctl_set_bit(cr, bit) smp_ctl_set_bit(cr, bit)
#define ctl_clear_bit(cr, bit) smp_ctl_clear_bit(cr, bit)

#else

#define ctl_set_bit(cr, bit) __ctl_set_bit(cr, bit)
#define ctl_clear_bit(cr, bit) __ctl_clear_bit(cr, bit)

#endif /* CONFIG_SMP */

427 428 429 430 431 432 433 434 435
static inline unsigned int stfl(void)
{
	asm volatile(
		"	.insn	s,0xb2b10000,0(0)\n" /* stfl */
		"0:\n"
		EX_TABLE(0b,0b));
	return S390_lowcore.stfl_fac_list;
}

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
static inline int __stfle(unsigned long long *list, int doublewords)
{
	typedef struct { unsigned long long _[doublewords]; } addrtype;
	register unsigned long __nr asm("0") = doublewords - 1;

	asm volatile(".insn s,0xb2b00000,%0" /* stfle */
		     : "=m" (*(addrtype *) list), "+d" (__nr) : : "cc");
	return __nr + 1;
}

static inline int stfle(unsigned long long *list, int doublewords)
{
	if (!(stfl() & (1UL << 24)))
		return -EOPNOTSUPP;
	return __stfle(list, doublewords);
}

453 454 455 456 457 458 459 460
static inline unsigned short stap(void)
{
	unsigned short cpu_address;

	asm volatile("stap %0" : "=m" (cpu_address));
	return cpu_address;
}

Linus Torvalds's avatar
Linus Torvalds committed
461 462 463 464 465 466
extern void (*_machine_restart)(char *command);
extern void (*_machine_halt)(void);
extern void (*_machine_power_off)(void);

#define arch_align_stack(x) (x)

467 468 469 470 471
#ifdef CONFIG_TRACE_IRQFLAGS
extern psw_t sysc_restore_trace_psw;
extern psw_t io_restore_trace_psw;
#endif

Linus Torvalds's avatar
Linus Torvalds committed
472 473 474
#endif /* __KERNEL__ */

#endif