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