xref: /kvm-unit-tests/lib/s390x/asm/time.h (revision 1cd182cffb2a91a13812bebaa92f89d6d022b442)
1 /*
2  * Clock utilities for s390
3  *
4  * Authors:
5  *  Thomas Huth <thuth@redhat.com>
6  *
7  * Copied from the s390/intercept test by:
8  *  Pierre Morel <pmorel@linux.ibm.com>
9  *
10  * This code is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License version 2.
12  */
13 #ifndef ASM_S390X_TIME_H
14 #define ASM_S390X_TIME_H
15 
16 #define STCK_SHIFT_US	(63 - 51)
17 #define STCK_MAX	((1UL << 52) - 1)
18 
19 static inline uint64_t get_clock_us(void)
20 {
21 	uint64_t clk;
22 
23 	asm volatile(" stck %0 " : : "Q"(clk) : "memory");
24 
25 	return clk >> STCK_SHIFT_US;
26 }
27 
28 static inline uint64_t get_clock_ms(void)
29 {
30 	return get_clock_us() / 1000;
31 }
32 
33 static inline void udelay(unsigned long us)
34 {
35 	unsigned long startclk = get_clock_us();
36 	unsigned long c;
37 
38 	do {
39 		c = get_clock_us();
40 		if (c < startclk)
41 			c += STCK_MAX;
42 	} while (c < startclk + us);
43 }
44 
45 static inline void mdelay(unsigned long ms)
46 {
47 	udelay(ms * 1000);
48 }
49 
50 #endif
51