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