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