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