s390/time: convert tod_clock_base to union
[linux-2.6-microblaze.git] / arch / s390 / include / asm / timex.h
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 #define STORE_CLOCK_EXT_SIZE    16      /* stcke writes 16 bytes */
21
22 extern u64 clock_comparator_max;
23
24 union tod_clock {
25         __uint128_t val;
26         struct {
27                 __uint128_t ei  :  8; /* epoch index */
28                 __uint128_t tod : 64; /* bits 0-63 of tod clock */
29                 __uint128_t     : 40;
30                 __uint128_t pf  : 16; /* programmable field */
31         };
32         struct {
33                 __uint128_t eitod : 72; /* epoch index + bits 0-63 tod clock */
34                 __uint128_t       : 56;
35         };
36         struct {
37                 __uint128_t us  : 60; /* micro-seconds */
38                 __uint128_t sus : 12; /* sub-microseconds */
39                 __uint128_t     : 56;
40         };
41 } __packed;
42
43 /* Inline functions for clock register access. */
44 static inline int set_tod_clock(__u64 time)
45 {
46         int cc;
47
48         asm volatile(
49                 "   sck   %1\n"
50                 "   ipm   %0\n"
51                 "   srl   %0,28\n"
52                 : "=d" (cc) : "Q" (time) : "cc");
53         return cc;
54 }
55
56 static inline int store_tod_clock_ext_cc(union tod_clock *clk)
57 {
58         int cc;
59
60         asm volatile(
61                 "   stcke  %1\n"
62                 "   ipm   %0\n"
63                 "   srl   %0,28\n"
64                 : "=d" (cc), "=Q" (*clk) : : "cc");
65         return cc;
66 }
67
68 static inline void store_tod_clock_ext(union tod_clock *tod)
69 {
70         asm volatile("stcke %0" : "=Q" (*tod) : : "cc");
71 }
72
73 static inline void set_clock_comparator(__u64 time)
74 {
75         asm volatile("sckc %0" : : "Q" (time));
76 }
77
78 static inline void set_tod_programmable_field(u16 val)
79 {
80         register unsigned long reg0 asm("0") = val;
81
82         asm volatile("sckpf" : : "d" (reg0));
83 }
84
85 void clock_comparator_work(void);
86
87 void __init time_early_init(void);
88
89 extern unsigned char ptff_function_mask[16];
90
91 /* Function codes for the ptff instruction. */
92 #define PTFF_QAF        0x00    /* query available functions */
93 #define PTFF_QTO        0x01    /* query tod offset */
94 #define PTFF_QSI        0x02    /* query steering information */
95 #define PTFF_QUI        0x04    /* query UTC information */
96 #define PTFF_ATO        0x40    /* adjust tod offset */
97 #define PTFF_STO        0x41    /* set tod offset */
98 #define PTFF_SFS        0x42    /* set fine steering rate */
99 #define PTFF_SGS        0x43    /* set gross steering rate */
100
101 /* Query TOD offset result */
102 struct ptff_qto {
103         unsigned long long physical_clock;
104         unsigned long long tod_offset;
105         unsigned long long logical_tod_offset;
106         unsigned long long tod_epoch_difference;
107 } __packed;
108
109 static inline int ptff_query(unsigned int nr)
110 {
111         unsigned char *ptr;
112
113         ptr = ptff_function_mask + (nr >> 3);
114         return (*ptr & (0x80 >> (nr & 7))) != 0;
115 }
116
117 /* Query UTC information result */
118 struct ptff_qui {
119         unsigned int tm : 2;
120         unsigned int ts : 2;
121         unsigned int : 28;
122         unsigned int pad_0x04;
123         unsigned long leap_event;
124         short old_leap;
125         short new_leap;
126         unsigned int pad_0x14;
127         unsigned long prt[5];
128         unsigned long cst[3];
129         unsigned int skew;
130         unsigned int pad_0x5c[41];
131 } __packed;
132
133 /*
134  * ptff - Perform timing facility function
135  * @ptff_block: Pointer to ptff parameter block
136  * @len: Length of parameter block
137  * @func: Function code
138  * Returns: Condition code (0 on success)
139  */
140 #define ptff(ptff_block, len, func)                                     \
141 ({                                                                      \
142         struct addrtype { char _[len]; };                               \
143         register unsigned int reg0 asm("0") = func;                     \
144         register unsigned long reg1 asm("1") = (unsigned long) (ptff_block);\
145         int rc;                                                         \
146                                                                         \
147         asm volatile(                                                   \
148                 "       .word   0x0104\n"                               \
149                 "       ipm     %0\n"                                   \
150                 "       srl     %0,28\n"                                \
151                 : "=d" (rc), "+m" (*(struct addrtype *) reg1)           \
152                 : "d" (reg0), "d" (reg1) : "cc");                       \
153         rc;                                                             \
154 })
155
156 static inline unsigned long long local_tick_disable(void)
157 {
158         unsigned long long old;
159
160         old = S390_lowcore.clock_comparator;
161         S390_lowcore.clock_comparator = clock_comparator_max;
162         set_clock_comparator(S390_lowcore.clock_comparator);
163         return old;
164 }
165
166 static inline void local_tick_enable(unsigned long long comp)
167 {
168         S390_lowcore.clock_comparator = comp;
169         set_clock_comparator(S390_lowcore.clock_comparator);
170 }
171
172 #define CLOCK_TICK_RATE         1193180 /* Underlying HZ */
173
174 typedef unsigned long long cycles_t;
175
176 static inline void get_tod_clock_ext(char *clk)
177 {
178         typedef struct { char _[STORE_CLOCK_EXT_SIZE]; } addrtype;
179
180         asm volatile("stcke %0" : "=Q" (*(addrtype *) clk) : : "cc");
181 }
182
183 static inline unsigned long long get_tod_clock(void)
184 {
185         char clk[STORE_CLOCK_EXT_SIZE];
186
187         get_tod_clock_ext(clk);
188         return *((unsigned long long *)&clk[1]);
189 }
190
191 static inline unsigned long long get_tod_clock_fast(void)
192 {
193 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
194         unsigned long long clk;
195
196         asm volatile("stckf %0" : "=Q" (clk) : : "cc");
197         return clk;
198 #else
199         return get_tod_clock();
200 #endif
201 }
202
203 static inline cycles_t get_cycles(void)
204 {
205         return (cycles_t) get_tod_clock() >> 2;
206 }
207
208 int get_phys_clock(unsigned long *clock);
209 void init_cpu_timer(void);
210
211 extern union tod_clock tod_clock_base;
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 long get_tod_clock_monotonic(void)
221 {
222         unsigned long long tod;
223
224         preempt_disable_notrace();
225         tod = get_tod_clock() - tod_clock_base.tod;
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 inline unsigned long long tod_to_ns(unsigned long long todval)
250 {
251         return ((todval >> 9) * 125) + (((todval & 0x1ff) * 125) >> 9);
252 }
253
254 /**
255  * tod_after - compare two 64 bit TOD values
256  * @a: first 64 bit TOD timestamp
257  * @b: second 64 bit TOD timestamp
258  *
259  * Returns: true if a is later than b
260  */
261 static inline int tod_after(unsigned long long a, unsigned long long b)
262 {
263         if (MACHINE_HAS_SCC)
264                 return (long long) a > (long long) b;
265         return a > b;
266 }
267
268 /**
269  * tod_after_eq - compare two 64 bit TOD values
270  * @a: first 64 bit TOD timestamp
271  * @b: second 64 bit TOD timestamp
272  *
273  * Returns: true if a is later than b
274  */
275 static inline int tod_after_eq(unsigned long long a, unsigned long long b)
276 {
277         if (MACHINE_HAS_SCC)
278                 return (long long) a >= (long long) b;
279         return a >= b;
280 }
281
282 #endif