1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Copyright IBM Corp. 1999
6 * Derived from "include/asm-i386/timex.h"
7 * Copyright (C) 1992, Linus Torvalds
10 #ifndef _ASM_S390_TIMEX_H
11 #define _ASM_S390_TIMEX_H
13 #include <linux/preempt.h>
14 #include <linux/time64.h>
15 #include <asm/lowcore.h>
18 /* The value of the TOD clock for 1.1.1970. */
19 #define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
21 extern u64 clock_comparator_max;
26 __uint128_t ei : 8; /* epoch index */
27 __uint128_t tod : 64; /* bits 0-63 of tod clock */
29 __uint128_t pf : 16; /* programmable field */
32 __uint128_t eitod : 72; /* epoch index + bits 0-63 tod clock */
36 __uint128_t us : 60; /* micro-seconds */
37 __uint128_t sus : 12; /* sub-microseconds */
42 /* Inline functions for clock register access. */
43 static inline int set_tod_clock(__u64 time)
53 return CC_TRANSFORM(cc);
56 static inline int store_tod_clock_ext_cc(union tod_clock *clk)
63 : CC_OUT(cc, cc), [clk] "=Q" (*clk)
66 return CC_TRANSFORM(cc);
69 static __always_inline void store_tod_clock_ext(union tod_clock *tod)
71 asm volatile("stcke %0" : "=Q" (*tod) : : "cc");
74 static inline void set_clock_comparator(__u64 time)
76 asm volatile("sckc %0" : : "Q" (time));
79 static inline void set_tod_programmable_field(u16 val)
85 : [val] "d" ((unsigned long)val)
89 void clock_comparator_work(void);
91 void __init time_early_init(void);
93 extern unsigned char ptff_function_mask[16];
95 /* Function codes for the ptff instruction. */
96 #define PTFF_QAF 0x00 /* query available functions */
97 #define PTFF_QTO 0x01 /* query tod offset */
98 #define PTFF_QSI 0x02 /* query steering information */
99 #define PTFF_QPT 0x03 /* query physical clock */
100 #define PTFF_QUI 0x04 /* query UTC information */
101 #define PTFF_ATO 0x40 /* adjust tod offset */
102 #define PTFF_STO 0x41 /* set tod offset */
103 #define PTFF_SFS 0x42 /* set fine steering rate */
104 #define PTFF_SGS 0x43 /* set gross steering rate */
106 /* Query TOD offset result */
108 unsigned long physical_clock;
109 unsigned long tod_offset;
110 unsigned long logical_tod_offset;
111 unsigned long tod_epoch_difference;
114 static inline int ptff_query(unsigned int nr)
118 ptr = ptff_function_mask + (nr >> 3);
119 return (*ptr & (0x80 >> (nr & 7))) != 0;
122 /* Query UTC information result */
127 unsigned int pad_0x04;
128 unsigned long leap_event;
131 unsigned int pad_0x14;
132 unsigned long prt[5];
133 unsigned long cst[3];
135 unsigned int pad_0x5c[41];
139 * ptff - Perform timing facility function
140 * @ptff_block: Pointer to ptff parameter block
141 * @len: Length of parameter block
142 * @func: Function code
143 * Returns: Condition code (0 on success)
145 #define ptff(ptff_block, len, func) \
147 struct addrtype { char _[len]; }; \
148 unsigned int reg0 = func; \
149 unsigned long reg1 = (unsigned long)(ptff_block); \
157 : CC_OUT(rc, rc), "+m" (*(struct addrtype *)reg1) \
158 : [reg0] "d" (reg0), [reg1] "d" (reg1) \
159 : CC_CLOBBER_LIST("0", "1")); \
163 static inline unsigned long local_tick_disable(void)
167 old = get_lowcore()->clock_comparator;
168 get_lowcore()->clock_comparator = clock_comparator_max;
169 set_clock_comparator(get_lowcore()->clock_comparator);
173 static inline void local_tick_enable(unsigned long comp)
175 get_lowcore()->clock_comparator = comp;
176 set_clock_comparator(get_lowcore()->clock_comparator);
179 #define CLOCK_TICK_RATE 1193180 /* Underlying HZ */
181 typedef unsigned long cycles_t;
183 static __always_inline unsigned long get_tod_clock(void)
187 store_tod_clock_ext(&clk);
191 static inline unsigned long get_tod_clock_fast(void)
195 asm volatile("stckf %0" : "=Q" (clk) : : "cc");
199 static inline cycles_t get_cycles(void)
201 return (cycles_t) get_tod_clock() >> 2;
203 #define get_cycles get_cycles
205 int get_phys_clock(unsigned long *clock);
206 void init_cpu_timer(void);
208 extern union tod_clock tod_clock_base;
210 static __always_inline unsigned long __get_tod_clock_monotonic(void)
212 return get_tod_clock() - tod_clock_base.tod;
216 * get_clock_monotonic - returns current time in clock rate units
218 * The clock and tod_clock_base get changed via stop_machine.
219 * Therefore preemption must be disabled, otherwise the returned
220 * value is not guaranteed to be monotonic.
222 static inline unsigned long get_tod_clock_monotonic(void)
226 preempt_disable_notrace();
227 tod = __get_tod_clock_monotonic();
228 preempt_enable_notrace();
233 * tod_to_ns - convert a TOD format value to nanoseconds
234 * @todval: to be converted TOD format value
235 * Returns: number of nanoseconds that correspond to the TOD format value
237 * Converting a 64 Bit TOD format value to nanoseconds means that the value
238 * must be divided by 4.096. In order to achieve that we multiply with 125
241 * ns = (todval * 125) >> 9;
243 * In order to avoid an overflow with the multiplication we can rewrite this.
244 * With a split todval == 2^9 * th + tl (th upper 55 bits, tl lower 9 bits)
247 * ns = ((2^9 * th + tl) * 125 ) >> 9;
248 * -> ns = (th * 125) + ((tl * 125) >> 9);
251 static __always_inline unsigned long tod_to_ns(unsigned long todval)
253 return ((todval >> 9) * 125) + (((todval & 0x1ff) * 125) >> 9);
256 static __always_inline u128 eitod_to_ns(u128 todval)
258 return (todval * 125) >> 9;
262 * tod_after - compare two 64 bit TOD values
263 * @a: first 64 bit TOD timestamp
264 * @b: second 64 bit TOD timestamp
266 * Returns: true if a is later than b
268 static inline int tod_after(unsigned long a, unsigned long b)
271 return (long) a > (long) b;
276 * tod_after_eq - compare two 64 bit TOD values
277 * @a: first 64 bit TOD timestamp
278 * @b: second 64 bit TOD timestamp
280 * Returns: true if a is later than b
282 static inline int tod_after_eq(unsigned long a, unsigned long b)
285 return (long) a >= (long) b;