timex.h 6.6 KB

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