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