xref: /openbmc/linux/arch/s390/include/asm/timex.h (revision 8cb5d748)
1 /*
2  *  S390 version
3  *    Copyright IBM Corp. 1999
4  *
5  *  Derived from "include/asm-i386/timex.h"
6  *    Copyright (C) 1992, Linus Torvalds
7  */
8 
9 #ifndef _ASM_S390_TIMEX_H
10 #define _ASM_S390_TIMEX_H
11 
12 #include <asm/lowcore.h>
13 #include <linux/time64.h>
14 
15 /* The value of the TOD clock for 1.1.1970. */
16 #define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
17 
18 extern u64 clock_comparator_max;
19 
20 /* Inline functions for clock register access. */
21 static inline int set_tod_clock(__u64 time)
22 {
23 	int cc;
24 
25 	asm volatile(
26 		"   sck   %1\n"
27 		"   ipm   %0\n"
28 		"   srl   %0,28\n"
29 		: "=d" (cc) : "Q" (time) : "cc");
30 	return cc;
31 }
32 
33 static inline int store_tod_clock(__u64 *time)
34 {
35 	int cc;
36 
37 	asm volatile(
38 		"   stck  %1\n"
39 		"   ipm   %0\n"
40 		"   srl   %0,28\n"
41 		: "=d" (cc), "=Q" (*time) : : "cc");
42 	return cc;
43 }
44 
45 static inline void set_clock_comparator(__u64 time)
46 {
47 	asm volatile("sckc %0" : : "Q" (time));
48 }
49 
50 static inline void store_clock_comparator(__u64 *time)
51 {
52 	asm volatile("stckc %0" : "=Q" (*time));
53 }
54 
55 void clock_comparator_work(void);
56 
57 void __init time_early_init(void);
58 
59 extern unsigned char ptff_function_mask[16];
60 
61 /* Function codes for the ptff instruction. */
62 #define PTFF_QAF	0x00	/* query available functions */
63 #define PTFF_QTO	0x01	/* query tod offset */
64 #define PTFF_QSI	0x02	/* query steering information */
65 #define PTFF_QUI	0x04	/* query UTC information */
66 #define PTFF_ATO	0x40	/* adjust tod offset */
67 #define PTFF_STO	0x41	/* set tod offset */
68 #define PTFF_SFS	0x42	/* set fine steering rate */
69 #define PTFF_SGS	0x43	/* set gross steering rate */
70 
71 /* Query TOD offset result */
72 struct ptff_qto {
73 	unsigned long long physical_clock;
74 	unsigned long long tod_offset;
75 	unsigned long long logical_tod_offset;
76 	unsigned long long tod_epoch_difference;
77 } __packed;
78 
79 static inline int ptff_query(unsigned int nr)
80 {
81 	unsigned char *ptr;
82 
83 	ptr = ptff_function_mask + (nr >> 3);
84 	return (*ptr & (0x80 >> (nr & 7))) != 0;
85 }
86 
87 /* Query UTC information result */
88 struct ptff_qui {
89 	unsigned int tm : 2;
90 	unsigned int ts : 2;
91 	unsigned int : 28;
92 	unsigned int pad_0x04;
93 	unsigned long leap_event;
94 	short old_leap;
95 	short new_leap;
96 	unsigned int pad_0x14;
97 	unsigned long prt[5];
98 	unsigned long cst[3];
99 	unsigned int skew;
100 	unsigned int pad_0x5c[41];
101 } __packed;
102 
103 /*
104  * ptff - Perform timing facility function
105  * @ptff_block: Pointer to ptff parameter block
106  * @len: Length of parameter block
107  * @func: Function code
108  * Returns: Condition code (0 on success)
109  */
110 #define ptff(ptff_block, len, func)					\
111 ({									\
112 	struct addrtype { char _[len]; };				\
113 	register unsigned int reg0 asm("0") = func;			\
114 	register unsigned long reg1 asm("1") = (unsigned long) (ptff_block);\
115 	int rc;								\
116 									\
117 	asm volatile(							\
118 		"	.word	0x0104\n"				\
119 		"	ipm	%0\n"					\
120 		"	srl	%0,28\n"				\
121 		: "=d" (rc), "+m" (*(struct addrtype *) reg1)		\
122 		: "d" (reg0), "d" (reg1) : "cc");			\
123 	rc;								\
124 })
125 
126 static inline unsigned long long local_tick_disable(void)
127 {
128 	unsigned long long old;
129 
130 	old = S390_lowcore.clock_comparator;
131 	S390_lowcore.clock_comparator = clock_comparator_max;
132 	set_clock_comparator(S390_lowcore.clock_comparator);
133 	return old;
134 }
135 
136 static inline void local_tick_enable(unsigned long long comp)
137 {
138 	S390_lowcore.clock_comparator = comp;
139 	set_clock_comparator(S390_lowcore.clock_comparator);
140 }
141 
142 #define CLOCK_TICK_RATE		1193180 /* Underlying HZ */
143 #define STORE_CLOCK_EXT_SIZE	16	/* stcke writes 16 bytes */
144 
145 typedef unsigned long long cycles_t;
146 
147 static inline void get_tod_clock_ext(char *clk)
148 {
149 	typedef struct { char _[STORE_CLOCK_EXT_SIZE]; } addrtype;
150 
151 	asm volatile("stcke %0" : "=Q" (*(addrtype *) clk) : : "cc");
152 }
153 
154 static inline unsigned long long get_tod_clock(void)
155 {
156 	unsigned char clk[STORE_CLOCK_EXT_SIZE];
157 
158 	get_tod_clock_ext(clk);
159 	return *((unsigned long long *)&clk[1]);
160 }
161 
162 static inline unsigned long long get_tod_clock_fast(void)
163 {
164 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
165 	unsigned long long clk;
166 
167 	asm volatile("stckf %0" : "=Q" (clk) : : "cc");
168 	return clk;
169 #else
170 	return get_tod_clock();
171 #endif
172 }
173 
174 static inline cycles_t get_cycles(void)
175 {
176 	return (cycles_t) get_tod_clock() >> 2;
177 }
178 
179 int get_phys_clock(unsigned long *clock);
180 void init_cpu_timer(void);
181 unsigned long long monotonic_clock(void);
182 
183 extern unsigned char tod_clock_base[16] __aligned(8);
184 
185 /**
186  * get_clock_monotonic - returns current time in clock rate units
187  *
188  * The caller must ensure that preemption is disabled.
189  * The clock and tod_clock_base get changed via stop_machine.
190  * Therefore preemption must be disabled when calling this
191  * function, otherwise the returned value is not guaranteed to
192  * be monotonic.
193  */
194 static inline unsigned long long get_tod_clock_monotonic(void)
195 {
196 	return get_tod_clock() - *(unsigned long long *) &tod_clock_base[1];
197 }
198 
199 /**
200  * tod_to_ns - convert a TOD format value to nanoseconds
201  * @todval: to be converted TOD format value
202  * Returns: number of nanoseconds that correspond to the TOD format value
203  *
204  * Converting a 64 Bit TOD format value to nanoseconds means that the value
205  * must be divided by 4.096. In order to achieve that we multiply with 125
206  * and divide by 512:
207  *
208  *    ns = (todval * 125) >> 9;
209  *
210  * In order to avoid an overflow with the multiplication we can rewrite this.
211  * With a split todval == 2^9 * th + tl (th upper 55 bits, tl lower 9 bits)
212  * we end up with
213  *
214  *    ns = ((2^9 * th + tl) * 125 ) >> 9;
215  * -> ns = (th * 125) + ((tl * 125) >> 9);
216  *
217  */
218 static inline unsigned long long tod_to_ns(unsigned long long todval)
219 {
220 	return ((todval >> 9) * 125) + (((todval & 0x1ff) * 125) >> 9);
221 }
222 
223 /**
224  * tod_after - compare two 64 bit TOD values
225  * @a: first 64 bit TOD timestamp
226  * @b: second 64 bit TOD timestamp
227  *
228  * Returns: true if a is later than b
229  */
230 static inline int tod_after(unsigned long long a, unsigned long long b)
231 {
232 	if (MACHINE_HAS_SCC)
233 		return (long long) a > (long long) b;
234 	return a > b;
235 }
236 
237 /**
238  * tod_after_eq - compare two 64 bit TOD values
239  * @a: first 64 bit TOD timestamp
240  * @b: second 64 bit TOD timestamp
241  *
242  * Returns: true if a is later than b
243  */
244 static inline int tod_after_eq(unsigned long long a, unsigned long long b)
245 {
246 	if (MACHINE_HAS_SCC)
247 		return (long long) a >= (long long) b;
248 	return a >= b;
249 }
250 
251 #endif
252