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>
17 /* The value of the TOD clock for 1.1.1970. */
18 #define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
20 extern u64 clock_comparator_max;
25 __uint128_t ei : 8; /* epoch index */
26 __uint128_t tod : 64; /* bits 0-63 of tod clock */
28 __uint128_t pf : 16; /* programmable field */
31 __uint128_t eitod : 72; /* epoch index + bits 0-63 tod clock */
35 __uint128_t us : 60; /* micro-seconds */
36 __uint128_t sus : 12; /* sub-microseconds */
41 /* Inline functions for clock register access. */
42 static inline int set_tod_clock(__u64 time)
50 : "=d" (cc) : "Q" (time) : "cc");
54 static inline int store_tod_clock_ext_cc(union tod_clock *clk)
62 : "=d" (cc), "=Q" (*clk) : : "cc");
66 static inline void store_tod_clock_ext(union tod_clock *tod)
68 asm volatile("stcke %0" : "=Q" (*tod) : : "cc");
71 static inline void set_clock_comparator(__u64 time)
73 asm volatile("sckc %0" : : "Q" (time));
76 static inline void set_tod_programmable_field(u16 val)
82 : [val] "d" ((unsigned long)val)
86 void clock_comparator_work(void);
88 void __init time_early_init(void);
90 extern unsigned char ptff_function_mask[16];
92 /* Function codes for the ptff instruction. */
93 #define PTFF_QAF 0x00 /* query available functions */
94 #define PTFF_QTO 0x01 /* query tod offset */
95 #define PTFF_QSI 0x02 /* query steering information */
96 #define PTFF_QUI 0x04 /* query UTC information */
97 #define PTFF_ATO 0x40 /* adjust tod offset */
98 #define PTFF_STO 0x41 /* set tod offset */
99 #define PTFF_SFS 0x42 /* set fine steering rate */
100 #define PTFF_SGS 0x43 /* set gross steering rate */
102 /* Query TOD offset result */
104 unsigned long physical_clock;
105 unsigned long tod_offset;
106 unsigned long logical_tod_offset;
107 unsigned long tod_epoch_difference;
110 static inline int ptff_query(unsigned int nr)
114 ptr = ptff_function_mask + (nr >> 3);
115 return (*ptr & (0x80 >> (nr & 7))) != 0;
118 /* Query UTC information result */
123 unsigned int pad_0x04;
124 unsigned long leap_event;
127 unsigned int pad_0x14;
128 unsigned long prt[5];
129 unsigned long cst[3];
131 unsigned int pad_0x5c[41];
135 * ptff - Perform timing facility function
136 * @ptff_block: Pointer to ptff parameter block
137 * @len: Length of parameter block
138 * @func: Function code
139 * Returns: Condition code (0 on success)
141 #define ptff(ptff_block, len, func) \
143 struct addrtype { char _[len]; }; \
144 unsigned int reg0 = func; \
145 unsigned long reg1 = (unsigned long)(ptff_block); \
151 " .insn e,0x0104\n" \
154 : [rc] "=&d" (rc), "+m" (*(struct addrtype *)reg1) \
155 : [reg0] "d" (reg0), [reg1] "d" (reg1) \
160 static inline unsigned long local_tick_disable(void)
164 old = S390_lowcore.clock_comparator;
165 S390_lowcore.clock_comparator = clock_comparator_max;
166 set_clock_comparator(S390_lowcore.clock_comparator);
170 static inline void local_tick_enable(unsigned long comp)
172 S390_lowcore.clock_comparator = comp;
173 set_clock_comparator(S390_lowcore.clock_comparator);
176 #define CLOCK_TICK_RATE 1193180 /* Underlying HZ */
178 typedef unsigned long cycles_t;
180 static inline unsigned long get_tod_clock(void)
184 store_tod_clock_ext(&clk);
188 static inline unsigned long get_tod_clock_fast(void)
190 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
193 asm volatile("stckf %0" : "=Q" (clk) : : "cc");
196 return get_tod_clock();
200 static inline cycles_t get_cycles(void)
202 return (cycles_t) get_tod_clock() >> 2;
204 #define get_cycles get_cycles
206 int get_phys_clock(unsigned long *clock);
207 void init_cpu_timer(void);
209 extern union tod_clock tod_clock_base;
212 * get_clock_monotonic - returns current time in clock rate units
214 * The clock and tod_clock_base get changed via stop_machine.
215 * Therefore preemption must be disabled, otherwise the returned
216 * value is not guaranteed to be monotonic.
218 static inline unsigned long get_tod_clock_monotonic(void)
222 preempt_disable_notrace();
223 tod = get_tod_clock() - tod_clock_base.tod;
224 preempt_enable_notrace();
229 * tod_to_ns - convert a TOD format value to nanoseconds
230 * @todval: to be converted TOD format value
231 * Returns: number of nanoseconds that correspond to the TOD format value
233 * Converting a 64 Bit TOD format value to nanoseconds means that the value
234 * must be divided by 4.096. In order to achieve that we multiply with 125
237 * ns = (todval * 125) >> 9;
239 * In order to avoid an overflow with the multiplication we can rewrite this.
240 * With a split todval == 2^9 * th + tl (th upper 55 bits, tl lower 9 bits)
243 * ns = ((2^9 * th + tl) * 125 ) >> 9;
244 * -> ns = (th * 125) + ((tl * 125) >> 9);
247 static inline unsigned long tod_to_ns(unsigned long todval)
249 return ((todval >> 9) * 125) + (((todval & 0x1ff) * 125) >> 9);
253 * tod_after - compare two 64 bit TOD values
254 * @a: first 64 bit TOD timestamp
255 * @b: second 64 bit TOD timestamp
257 * Returns: true if a is later than b
259 static inline int tod_after(unsigned long a, unsigned long b)
262 return (long) a > (long) b;
267 * tod_after_eq - compare two 64 bit TOD values
268 * @a: first 64 bit TOD timestamp
269 * @b: second 64 bit TOD timestamp
271 * Returns: true if a is later than b
273 static inline int tod_after_eq(unsigned long a, unsigned long b)
276 return (long) a >= (long) b;