timex.h 6.1 KB

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