time.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Common time prototypes and such for all ppc machines.
  3. *
  4. * Written by Cort Dougan (cort@cs.nmt.edu) to merge
  5. * Paul Mackerras' version and mine for PReP and Pmac.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #ifndef __POWERPC_TIME_H
  13. #define __POWERPC_TIME_H
  14. #ifdef __KERNEL__
  15. #include <linux/config.h>
  16. #include <linux/types.h>
  17. #include <linux/percpu.h>
  18. #include <asm/processor.h>
  19. #ifdef CONFIG_PPC64
  20. #include <asm/paca.h>
  21. #include <asm/iseries/hv_call.h>
  22. #endif
  23. /* time.c */
  24. extern unsigned long tb_ticks_per_jiffy;
  25. extern unsigned long tb_ticks_per_usec;
  26. extern unsigned long tb_ticks_per_sec;
  27. extern u64 tb_to_xs;
  28. extern unsigned tb_to_us;
  29. extern unsigned long tb_last_stamp;
  30. extern u64 tb_last_jiffy;
  31. DECLARE_PER_CPU(unsigned long, last_jiffy);
  32. struct rtc_time;
  33. extern void to_tm(int tim, struct rtc_time * tm);
  34. extern time_t last_rtc_update;
  35. extern void generic_calibrate_decr(void);
  36. extern void wakeup_decrementer(void);
  37. /* Some sane defaults: 125 MHz timebase, 1GHz processor */
  38. extern unsigned long ppc_proc_freq;
  39. #define DEFAULT_PROC_FREQ (DEFAULT_TB_FREQ * 8)
  40. extern unsigned long ppc_tb_freq;
  41. #define DEFAULT_TB_FREQ 125000000UL
  42. /*
  43. * By putting all of this stuff into a single struct we
  44. * reduce the number of cache lines touched by do_gettimeofday.
  45. * Both by collecting all of the data in one cache line and
  46. * by touching only one TOC entry on ppc64.
  47. */
  48. struct gettimeofday_vars {
  49. u64 tb_to_xs;
  50. u64 stamp_xsec;
  51. u64 tb_orig_stamp;
  52. };
  53. struct gettimeofday_struct {
  54. unsigned long tb_ticks_per_sec;
  55. struct gettimeofday_vars vars[2];
  56. struct gettimeofday_vars * volatile varp;
  57. unsigned var_idx;
  58. unsigned tb_to_us;
  59. };
  60. struct div_result {
  61. u64 result_high;
  62. u64 result_low;
  63. };
  64. /* Accessor functions for the timebase (RTC on 601) registers. */
  65. /* If one day CONFIG_POWER is added just define __USE_RTC as 1 */
  66. #ifdef CONFIG_6xx
  67. #define __USE_RTC() (!cpu_has_feature(CPU_FTR_USE_TB))
  68. #else
  69. #define __USE_RTC() 0
  70. #endif
  71. /* On ppc64 this gets us the whole timebase; on ppc32 just the lower half */
  72. static inline unsigned long get_tbl(void)
  73. {
  74. unsigned long tbl;
  75. #if defined(CONFIG_403GCX)
  76. asm volatile("mfspr %0, 0x3dd" : "=r" (tbl));
  77. #else
  78. asm volatile("mftb %0" : "=r" (tbl));
  79. #endif
  80. return tbl;
  81. }
  82. static inline unsigned int get_tbu(void)
  83. {
  84. unsigned int tbu;
  85. #if defined(CONFIG_403GCX)
  86. asm volatile("mfspr %0, 0x3dc" : "=r" (tbu));
  87. #else
  88. asm volatile("mftbu %0" : "=r" (tbu));
  89. #endif
  90. return tbu;
  91. }
  92. static inline unsigned int get_rtcl(void)
  93. {
  94. unsigned int rtcl;
  95. asm volatile("mfrtcl %0" : "=r" (rtcl));
  96. return rtcl;
  97. }
  98. static inline u64 get_rtc(void)
  99. {
  100. unsigned int hi, lo, hi2;
  101. do {
  102. asm volatile("mfrtcu %0; mfrtcl %1; mfrtcu %2"
  103. : "=r" (hi), "=r" (lo), "=r" (hi2));
  104. } while (hi2 != hi);
  105. return (u64)hi * 1000000000 + lo;
  106. }
  107. #ifdef CONFIG_PPC64
  108. static inline u64 get_tb(void)
  109. {
  110. return mftb();
  111. }
  112. #else
  113. static inline u64 get_tb(void)
  114. {
  115. unsigned int tbhi, tblo, tbhi2;
  116. do {
  117. tbhi = get_tbu();
  118. tblo = get_tbl();
  119. tbhi2 = get_tbu();
  120. } while (tbhi != tbhi2);
  121. return ((u64)tbhi << 32) | tblo;
  122. }
  123. #endif
  124. static inline void set_tb(unsigned int upper, unsigned int lower)
  125. {
  126. mtspr(SPRN_TBWL, 0);
  127. mtspr(SPRN_TBWU, upper);
  128. mtspr(SPRN_TBWL, lower);
  129. }
  130. /* Accessor functions for the decrementer register.
  131. * The 4xx doesn't even have a decrementer. I tried to use the
  132. * generic timer interrupt code, which seems OK, with the 4xx PIT
  133. * in auto-reload mode. The problem is PIT stops counting when it
  134. * hits zero. If it would wrap, we could use it just like a decrementer.
  135. */
  136. static inline unsigned int get_dec(void)
  137. {
  138. #if defined(CONFIG_40x)
  139. return (mfspr(SPRN_PIT));
  140. #else
  141. return (mfspr(SPRN_DEC));
  142. #endif
  143. }
  144. static inline void set_dec(int val)
  145. {
  146. #if defined(CONFIG_40x)
  147. return; /* Have to let it auto-reload */
  148. #elif defined(CONFIG_8xx_CPU6)
  149. set_dec_cpu6(val);
  150. #else
  151. #ifdef CONFIG_PPC_ISERIES
  152. int cur_dec;
  153. if (get_lppaca()->shared_proc) {
  154. get_lppaca()->virtual_decr = val;
  155. cur_dec = get_dec();
  156. if (cur_dec > val)
  157. HvCall_setVirtualDecr();
  158. } else
  159. #endif
  160. mtspr(SPRN_DEC, val);
  161. #endif /* not 40x or 8xx_CPU6 */
  162. }
  163. static inline unsigned long tb_ticks_since(unsigned long tstamp)
  164. {
  165. if (__USE_RTC()) {
  166. int delta = get_rtcl() - (unsigned int) tstamp;
  167. return delta < 0 ? delta + 1000000000 : delta;
  168. }
  169. return get_tbl() - tstamp;
  170. }
  171. #define mulhwu(x,y) \
  172. ({unsigned z; asm ("mulhwu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
  173. #ifdef CONFIG_PPC64
  174. #define mulhdu(x,y) \
  175. ({unsigned long z; asm ("mulhdu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
  176. #else
  177. extern u64 mulhdu(u64, u64);
  178. #endif
  179. extern void smp_space_timers(unsigned int);
  180. extern unsigned mulhwu_scale_factor(unsigned, unsigned);
  181. extern void div128_by_32(u64 dividend_high, u64 dividend_low,
  182. unsigned divisor, struct div_result *dr);
  183. /* Used to store Processor Utilization register (purr) values */
  184. struct cpu_usage {
  185. u64 current_tb; /* Holds the current purr register values */
  186. };
  187. DECLARE_PER_CPU(struct cpu_usage, cpu_usage_array);
  188. #endif /* __KERNEL__ */
  189. #endif /* __PPC64_TIME_H */