timex.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * S390 version
  3. * Copyright IBM Corp. 1999
  4. *
  5. * Derived from "include/asm-i386/timex.h"
  6. * Copyright (C) 1992, Linus Torvalds
  7. */
  8. #ifndef _ASM_S390_TIMEX_H
  9. #define _ASM_S390_TIMEX_H
  10. #include <asm/lowcore.h>
  11. #include <linux/time64.h>
  12. /* The value of the TOD clock for 1.1.1970. */
  13. #define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
  14. /* Inline functions for clock register access. */
  15. static inline int set_tod_clock(__u64 time)
  16. {
  17. int cc;
  18. asm volatile(
  19. " sck %1\n"
  20. " ipm %0\n"
  21. " srl %0,28\n"
  22. : "=d" (cc) : "Q" (time) : "cc");
  23. return cc;
  24. }
  25. static inline int store_tod_clock(__u64 *time)
  26. {
  27. int cc;
  28. asm volatile(
  29. " stck %1\n"
  30. " ipm %0\n"
  31. " srl %0,28\n"
  32. : "=d" (cc), "=Q" (*time) : : "cc");
  33. return cc;
  34. }
  35. static inline void set_clock_comparator(__u64 time)
  36. {
  37. asm volatile("sckc %0" : : "Q" (time));
  38. }
  39. static inline void store_clock_comparator(__u64 *time)
  40. {
  41. asm volatile("stckc %0" : "=Q" (*time));
  42. }
  43. void clock_comparator_work(void);
  44. void __init ptff_init(void);
  45. extern unsigned char ptff_function_mask[16];
  46. extern unsigned long lpar_offset;
  47. extern unsigned long initial_leap_seconds;
  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. static inline int ptff(void *ptff_block, size_t len, unsigned int func)
  86. {
  87. typedef struct { char _[len]; } addrtype;
  88. register unsigned int reg0 asm("0") = func;
  89. register unsigned long reg1 asm("1") = (unsigned long) ptff_block;
  90. int rc;
  91. asm volatile(
  92. " .word 0x0104\n"
  93. " ipm %0\n"
  94. " srl %0,28\n"
  95. : "=d" (rc), "+m" (*(addrtype *) ptff_block)
  96. : "d" (reg0), "d" (reg1) : "cc");
  97. return rc;
  98. }
  99. static inline unsigned long long local_tick_disable(void)
  100. {
  101. unsigned long long old;
  102. old = S390_lowcore.clock_comparator;
  103. S390_lowcore.clock_comparator = -1ULL;
  104. set_clock_comparator(S390_lowcore.clock_comparator);
  105. return old;
  106. }
  107. static inline void local_tick_enable(unsigned long long comp)
  108. {
  109. S390_lowcore.clock_comparator = comp;
  110. set_clock_comparator(S390_lowcore.clock_comparator);
  111. }
  112. #define CLOCK_TICK_RATE 1193180 /* Underlying HZ */
  113. #define STORE_CLOCK_EXT_SIZE 16 /* stcke writes 16 bytes */
  114. typedef unsigned long long cycles_t;
  115. static inline void get_tod_clock_ext(char *clk)
  116. {
  117. typedef struct { char _[STORE_CLOCK_EXT_SIZE]; } addrtype;
  118. asm volatile("stcke %0" : "=Q" (*(addrtype *) clk) : : "cc");
  119. }
  120. static inline unsigned long long get_tod_clock(void)
  121. {
  122. unsigned char clk[STORE_CLOCK_EXT_SIZE];
  123. get_tod_clock_ext(clk);
  124. return *((unsigned long long *)&clk[1]);
  125. }
  126. static inline unsigned long long get_tod_clock_fast(void)
  127. {
  128. #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
  129. unsigned long long clk;
  130. asm volatile("stckf %0" : "=Q" (clk) : : "cc");
  131. return clk;
  132. #else
  133. return get_tod_clock();
  134. #endif
  135. }
  136. static inline cycles_t get_cycles(void)
  137. {
  138. return (cycles_t) get_tod_clock() >> 2;
  139. }
  140. int get_phys_clock(unsigned long long *clock);
  141. void init_cpu_timer(void);
  142. unsigned long long monotonic_clock(void);
  143. void tod_to_timeval(__u64 todval, struct timespec64 *xt);
  144. static inline
  145. void stck_to_timespec64(unsigned long long stck, struct timespec64 *ts)
  146. {
  147. tod_to_timeval(stck - TOD_UNIX_EPOCH, ts);
  148. }
  149. extern u64 sched_clock_base_cc;
  150. /**
  151. * get_clock_monotonic - returns current time in clock rate units
  152. *
  153. * The caller must ensure that preemption is disabled.
  154. * The clock and sched_clock_base get changed via stop_machine.
  155. * Therefore preemption must be disabled when calling this
  156. * function, otherwise the returned value is not guaranteed to
  157. * be monotonic.
  158. */
  159. static inline unsigned long long get_tod_clock_monotonic(void)
  160. {
  161. return get_tod_clock() - sched_clock_base_cc;
  162. }
  163. /**
  164. * tod_to_ns - convert a TOD format value to nanoseconds
  165. * @todval: to be converted TOD format value
  166. * Returns: number of nanoseconds that correspond to the TOD format value
  167. *
  168. * Converting a 64 Bit TOD format value to nanoseconds means that the value
  169. * must be divided by 4.096. In order to achieve that we multiply with 125
  170. * and divide by 512:
  171. *
  172. * ns = (todval * 125) >> 9;
  173. *
  174. * In order to avoid an overflow with the multiplication we can rewrite this.
  175. * With a split todval == 2^32 * th + tl (th upper 32 bits, tl lower 32 bits)
  176. * we end up with
  177. *
  178. * ns = ((2^32 * th + tl) * 125 ) >> 9;
  179. * -> ns = (2^23 * th * 125) + ((tl * 125) >> 9);
  180. *
  181. */
  182. static inline unsigned long long tod_to_ns(unsigned long long todval)
  183. {
  184. unsigned long long ns;
  185. ns = ((todval >> 32) << 23) * 125;
  186. ns += ((todval & 0xffffffff) * 125) >> 9;
  187. return ns;
  188. }
  189. #endif