cputime.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * include/asm-s390/cputime.h
  3. *
  4. * (C) Copyright IBM Corp. 2004
  5. *
  6. * Author: Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. */
  8. #ifndef _S390_CPUTIME_H
  9. #define _S390_CPUTIME_H
  10. #include <linux/types.h>
  11. #include <linux/percpu.h>
  12. #include <linux/spinlock.h>
  13. #include <asm/div64.h>
  14. /* We want to use full resolution of the CPU timer: 2**-12 micro-seconds. */
  15. typedef unsigned long long __nocast cputime_t;
  16. typedef unsigned long long __nocast cputime64_t;
  17. static inline unsigned long __div(unsigned long long n, unsigned long base)
  18. {
  19. #ifndef __s390x__
  20. register_pair rp;
  21. rp.pair = n >> 1;
  22. asm ("dr %0,%1" : "+d" (rp) : "d" (base >> 1));
  23. return rp.subreg.odd;
  24. #else /* __s390x__ */
  25. return n / base;
  26. #endif /* __s390x__ */
  27. }
  28. #define cputime_one_jiffy jiffies_to_cputime(1)
  29. /*
  30. * Convert cputime to jiffies and back.
  31. */
  32. static inline unsigned long cputime_to_jiffies(const cputime_t cputime)
  33. {
  34. return __div((__force unsigned long long) cputime, 4096000000ULL / HZ);
  35. }
  36. static inline cputime_t jiffies_to_cputime(const unsigned int jif)
  37. {
  38. return (__force cputime_t)(jif * (4096000000ULL / HZ));
  39. }
  40. static inline u64 cputime64_to_jiffies64(cputime64_t cputime)
  41. {
  42. unsigned long long jif = (__force unsigned long long) cputime;
  43. do_div(jif, 4096000000ULL / HZ);
  44. return jif;
  45. }
  46. static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
  47. {
  48. return (__force cputime64_t)(jif * (4096000000ULL / HZ));
  49. }
  50. /*
  51. * Convert cputime to microseconds and back.
  52. */
  53. static inline unsigned int cputime_to_usecs(const cputime_t cputime)
  54. {
  55. return (__force unsigned long long) cputime >> 12;
  56. }
  57. static inline cputime_t usecs_to_cputime(const unsigned int m)
  58. {
  59. return (__force cputime_t)(m * 4096ULL);
  60. }
  61. #define usecs_to_cputime64(m) usecs_to_cputime(m)
  62. /*
  63. * Convert cputime to milliseconds and back.
  64. */
  65. static inline unsigned int cputime_to_secs(const cputime_t cputime)
  66. {
  67. return __div((__force unsigned long long) cputime, 2048000000) >> 1;
  68. }
  69. static inline cputime_t secs_to_cputime(const unsigned int s)
  70. {
  71. return (__force cputime_t)(s * 4096000000ULL);
  72. }
  73. /*
  74. * Convert cputime to timespec and back.
  75. */
  76. static inline cputime_t timespec_to_cputime(const struct timespec *value)
  77. {
  78. unsigned long long ret = value->tv_sec * 4096000000ULL;
  79. return (__force cputime_t)(ret + value->tv_nsec * 4096 / 1000);
  80. }
  81. static inline void cputime_to_timespec(const cputime_t cputime,
  82. struct timespec *value)
  83. {
  84. unsigned long long __cputime = (__force unsigned long long) cputime;
  85. #ifndef __s390x__
  86. register_pair rp;
  87. rp.pair = __cputime >> 1;
  88. asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
  89. value->tv_nsec = rp.subreg.even * 1000 / 4096;
  90. value->tv_sec = rp.subreg.odd;
  91. #else
  92. value->tv_nsec = (__cputime % 4096000000ULL) * 1000 / 4096;
  93. value->tv_sec = __cputime / 4096000000ULL;
  94. #endif
  95. }
  96. /*
  97. * Convert cputime to timeval and back.
  98. * Since cputime and timeval have the same resolution (microseconds)
  99. * this is easy.
  100. */
  101. static inline cputime_t timeval_to_cputime(const struct timeval *value)
  102. {
  103. unsigned long long ret = value->tv_sec * 4096000000ULL;
  104. return (__force cputime_t)(ret + value->tv_usec * 4096ULL);
  105. }
  106. static inline void cputime_to_timeval(const cputime_t cputime,
  107. struct timeval *value)
  108. {
  109. unsigned long long __cputime = (__force unsigned long long) cputime;
  110. #ifndef __s390x__
  111. register_pair rp;
  112. rp.pair = __cputime >> 1;
  113. asm ("dr %0,%1" : "+d" (rp) : "d" (2048000000UL));
  114. value->tv_usec = rp.subreg.even / 4096;
  115. value->tv_sec = rp.subreg.odd;
  116. #else
  117. value->tv_usec = (__cputime % 4096000000ULL) / 4096;
  118. value->tv_sec = __cputime / 4096000000ULL;
  119. #endif
  120. }
  121. /*
  122. * Convert cputime to clock and back.
  123. */
  124. static inline clock_t cputime_to_clock_t(cputime_t cputime)
  125. {
  126. unsigned long long clock = (__force unsigned long long) cputime;
  127. do_div(clock, 4096000000ULL / USER_HZ);
  128. return clock;
  129. }
  130. static inline cputime_t clock_t_to_cputime(unsigned long x)
  131. {
  132. return (__force cputime_t)(x * (4096000000ULL / USER_HZ));
  133. }
  134. /*
  135. * Convert cputime64 to clock.
  136. */
  137. static inline clock_t cputime64_to_clock_t(cputime64_t cputime)
  138. {
  139. unsigned long long clock = (__force unsigned long long) cputime;
  140. do_div(clock, 4096000000ULL / USER_HZ);
  141. return clock;
  142. }
  143. struct s390_idle_data {
  144. unsigned int sequence;
  145. unsigned long long idle_count;
  146. unsigned long long idle_enter;
  147. unsigned long long idle_exit;
  148. unsigned long long idle_time;
  149. int nohz_delay;
  150. };
  151. DECLARE_PER_CPU(struct s390_idle_data, s390_idle);
  152. cputime64_t s390_get_idle_time(int cpu);
  153. #define arch_idle_time(cpu) s390_get_idle_time(cpu)
  154. static inline int s390_nohz_delay(int cpu)
  155. {
  156. return __get_cpu_var(s390_idle).nohz_delay != 0;
  157. }
  158. #define arch_needs_cpu(cpu) s390_nohz_delay(cpu)
  159. #endif /* _S390_CPUTIME_H */