xref: /openbmc/linux/arch/s390/include/asm/timex.h (revision ca48739e)
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 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 	register unsigned long reg0 asm("0") = val;
79 
80 	asm volatile("sckpf" : : "d" (reg0));
81 }
82 
83 void clock_comparator_work(void);
84 
85 void __init time_early_init(void);
86 
87 extern unsigned char ptff_function_mask[16];
88 
89 /* Function codes for the ptff instruction. */
90 #define PTFF_QAF	0x00	/* query available functions */
91 #define PTFF_QTO	0x01	/* query tod offset */
92 #define PTFF_QSI	0x02	/* query steering information */
93 #define PTFF_QUI	0x04	/* query UTC information */
94 #define PTFF_ATO	0x40	/* adjust tod offset */
95 #define PTFF_STO	0x41	/* set tod offset */
96 #define PTFF_SFS	0x42	/* set fine steering rate */
97 #define PTFF_SGS	0x43	/* set gross steering rate */
98 
99 /* Query TOD offset result */
100 struct ptff_qto {
101 	unsigned long long physical_clock;
102 	unsigned long long tod_offset;
103 	unsigned long long logical_tod_offset;
104 	unsigned long long tod_epoch_difference;
105 } __packed;
106 
107 static inline int ptff_query(unsigned int nr)
108 {
109 	unsigned char *ptr;
110 
111 	ptr = ptff_function_mask + (nr >> 3);
112 	return (*ptr & (0x80 >> (nr & 7))) != 0;
113 }
114 
115 /* Query UTC information result */
116 struct ptff_qui {
117 	unsigned int tm : 2;
118 	unsigned int ts : 2;
119 	unsigned int : 28;
120 	unsigned int pad_0x04;
121 	unsigned long leap_event;
122 	short old_leap;
123 	short new_leap;
124 	unsigned int pad_0x14;
125 	unsigned long prt[5];
126 	unsigned long cst[3];
127 	unsigned int skew;
128 	unsigned int pad_0x5c[41];
129 } __packed;
130 
131 /*
132  * ptff - Perform timing facility function
133  * @ptff_block: Pointer to ptff parameter block
134  * @len: Length of parameter block
135  * @func: Function code
136  * Returns: Condition code (0 on success)
137  */
138 #define ptff(ptff_block, len, func)					\
139 ({									\
140 	struct addrtype { char _[len]; };				\
141 	register unsigned int reg0 asm("0") = func;			\
142 	register unsigned long reg1 asm("1") = (unsigned long) (ptff_block);\
143 	int rc;								\
144 									\
145 	asm volatile(							\
146 		"	.word	0x0104\n"				\
147 		"	ipm	%0\n"					\
148 		"	srl	%0,28\n"				\
149 		: "=d" (rc), "+m" (*(struct addrtype *) reg1)		\
150 		: "d" (reg0), "d" (reg1) : "cc");			\
151 	rc;								\
152 })
153 
154 static inline unsigned long long local_tick_disable(void)
155 {
156 	unsigned long long old;
157 
158 	old = S390_lowcore.clock_comparator;
159 	S390_lowcore.clock_comparator = clock_comparator_max;
160 	set_clock_comparator(S390_lowcore.clock_comparator);
161 	return old;
162 }
163 
164 static inline void local_tick_enable(unsigned long long comp)
165 {
166 	S390_lowcore.clock_comparator = comp;
167 	set_clock_comparator(S390_lowcore.clock_comparator);
168 }
169 
170 #define CLOCK_TICK_RATE		1193180 /* Underlying HZ */
171 
172 typedef unsigned long long cycles_t;
173 
174 static inline unsigned long long get_tod_clock(void)
175 {
176 	union tod_clock clk;
177 
178 	store_tod_clock_ext(&clk);
179 	return clk.tod;
180 }
181 
182 static inline unsigned long long get_tod_clock_fast(void)
183 {
184 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
185 	unsigned long long clk;
186 
187 	asm volatile("stckf %0" : "=Q" (clk) : : "cc");
188 	return clk;
189 #else
190 	return get_tod_clock();
191 #endif
192 }
193 
194 static inline cycles_t get_cycles(void)
195 {
196 	return (cycles_t) get_tod_clock() >> 2;
197 }
198 
199 int get_phys_clock(unsigned long *clock);
200 void init_cpu_timer(void);
201 
202 extern union tod_clock tod_clock_base;
203 
204 /**
205  * get_clock_monotonic - returns current time in clock rate units
206  *
207  * The clock and tod_clock_base get changed via stop_machine.
208  * Therefore preemption must be disabled, otherwise the returned
209  * value is not guaranteed to be monotonic.
210  */
211 static inline unsigned long long get_tod_clock_monotonic(void)
212 {
213 	unsigned long long tod;
214 
215 	preempt_disable_notrace();
216 	tod = get_tod_clock() - tod_clock_base.tod;
217 	preempt_enable_notrace();
218 	return tod;
219 }
220 
221 /**
222  * tod_to_ns - convert a TOD format value to nanoseconds
223  * @todval: to be converted TOD format value
224  * Returns: number of nanoseconds that correspond to the TOD format value
225  *
226  * Converting a 64 Bit TOD format value to nanoseconds means that the value
227  * must be divided by 4.096. In order to achieve that we multiply with 125
228  * and divide by 512:
229  *
230  *    ns = (todval * 125) >> 9;
231  *
232  * In order to avoid an overflow with the multiplication we can rewrite this.
233  * With a split todval == 2^9 * th + tl (th upper 55 bits, tl lower 9 bits)
234  * we end up with
235  *
236  *    ns = ((2^9 * th + tl) * 125 ) >> 9;
237  * -> ns = (th * 125) + ((tl * 125) >> 9);
238  *
239  */
240 static inline unsigned long long tod_to_ns(unsigned long long todval)
241 {
242 	return ((todval >> 9) * 125) + (((todval & 0x1ff) * 125) >> 9);
243 }
244 
245 /**
246  * tod_after - compare two 64 bit TOD values
247  * @a: first 64 bit TOD timestamp
248  * @b: second 64 bit TOD timestamp
249  *
250  * Returns: true if a is later than b
251  */
252 static inline int tod_after(unsigned long long a, unsigned long long b)
253 {
254 	if (MACHINE_HAS_SCC)
255 		return (long long) a > (long long) b;
256 	return a > b;
257 }
258 
259 /**
260  * tod_after_eq - 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_eq(unsigned long long a, unsigned long long b)
267 {
268 	if (MACHINE_HAS_SCC)
269 		return (long long) a >= (long long) b;
270 	return a >= b;
271 }
272 
273 #endif
274