timex.h 6.1 KB

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