timex.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /* The value of the TOD clock for 1.1.1970. */
  12. #define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
  13. /* Inline functions for clock register access. */
  14. static inline int set_tod_clock(__u64 time)
  15. {
  16. int cc;
  17. asm volatile(
  18. " sck %1\n"
  19. " ipm %0\n"
  20. " srl %0,28\n"
  21. : "=d" (cc) : "Q" (time) : "cc");
  22. return cc;
  23. }
  24. static inline int store_tod_clock(__u64 *time)
  25. {
  26. int cc;
  27. asm volatile(
  28. " stck %1\n"
  29. " ipm %0\n"
  30. " srl %0,28\n"
  31. : "=d" (cc), "=Q" (*time) : : "cc");
  32. return cc;
  33. }
  34. static inline void set_clock_comparator(__u64 time)
  35. {
  36. asm volatile("sckc %0" : : "Q" (time));
  37. }
  38. static inline void store_clock_comparator(__u64 *time)
  39. {
  40. asm volatile("stckc %0" : "=Q" (*time));
  41. }
  42. void clock_comparator_work(void);
  43. static inline unsigned long long local_tick_disable(void)
  44. {
  45. unsigned long long old;
  46. old = S390_lowcore.clock_comparator;
  47. S390_lowcore.clock_comparator = -1ULL;
  48. set_clock_comparator(S390_lowcore.clock_comparator);
  49. return old;
  50. }
  51. static inline void local_tick_enable(unsigned long long comp)
  52. {
  53. S390_lowcore.clock_comparator = comp;
  54. set_clock_comparator(S390_lowcore.clock_comparator);
  55. }
  56. #define CLOCK_TICK_RATE 1193180 /* Underlying HZ */
  57. #define STORE_CLOCK_EXT_SIZE 16 /* stcke writes 16 bytes */
  58. typedef unsigned long long cycles_t;
  59. static inline void get_tod_clock_ext(char *clk)
  60. {
  61. typedef struct { char _[STORE_CLOCK_EXT_SIZE]; } addrtype;
  62. asm volatile("stcke %0" : "=Q" (*(addrtype *) clk) : : "cc");
  63. }
  64. static inline unsigned long long get_tod_clock(void)
  65. {
  66. unsigned char clk[STORE_CLOCK_EXT_SIZE];
  67. get_tod_clock_ext(clk);
  68. return *((unsigned long long *)&clk[1]);
  69. }
  70. static inline unsigned long long get_tod_clock_fast(void)
  71. {
  72. #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
  73. unsigned long long clk;
  74. asm volatile("stckf %0" : "=Q" (clk) : : "cc");
  75. return clk;
  76. #else
  77. return get_tod_clock();
  78. #endif
  79. }
  80. static inline cycles_t get_cycles(void)
  81. {
  82. return (cycles_t) get_tod_clock() >> 2;
  83. }
  84. int get_sync_clock(unsigned long long *clock);
  85. void init_cpu_timer(void);
  86. unsigned long long monotonic_clock(void);
  87. void tod_to_timeval(__u64, struct timespec *);
  88. static inline
  89. void stck_to_timespec(unsigned long long stck, struct timespec *ts)
  90. {
  91. tod_to_timeval(stck - TOD_UNIX_EPOCH, ts);
  92. }
  93. extern u64 sched_clock_base_cc;
  94. /**
  95. * get_clock_monotonic - returns current time in clock rate units
  96. *
  97. * The caller must ensure that preemption is disabled.
  98. * The clock and sched_clock_base get changed via stop_machine.
  99. * Therefore preemption must be disabled when calling this
  100. * function, otherwise the returned value is not guaranteed to
  101. * be monotonic.
  102. */
  103. static inline unsigned long long get_tod_clock_monotonic(void)
  104. {
  105. return get_tod_clock() - sched_clock_base_cc;
  106. }
  107. /**
  108. * tod_to_ns - convert a TOD format value to nanoseconds
  109. * @todval: to be converted TOD format value
  110. * Returns: number of nanoseconds that correspond to the TOD format value
  111. *
  112. * Converting a 64 Bit TOD format value to nanoseconds means that the value
  113. * must be divided by 4.096. In order to achieve that we multiply with 125
  114. * and divide by 512:
  115. *
  116. * ns = (todval * 125) >> 9;
  117. *
  118. * In order to avoid an overflow with the multiplication we can rewrite this.
  119. * With a split todval == 2^32 * th + tl (th upper 32 bits, tl lower 32 bits)
  120. * we end up with
  121. *
  122. * ns = ((2^32 * th + tl) * 125 ) >> 9;
  123. * -> ns = (2^23 * th * 125) + ((tl * 125) >> 9);
  124. *
  125. */
  126. static inline unsigned long long tod_to_ns(unsigned long long todval)
  127. {
  128. unsigned long long ns;
  129. ns = ((todval >> 32) << 23) * 125;
  130. ns += ((todval & 0xffffffff) * 125) >> 9;
  131. return ns;
  132. }
  133. #endif