timex.h 6.3 KB

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