time.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/types.h>
  16. #include <linux/percpu.h>
  17. #include <asm/processor.h>
  18. #ifdef CONFIG_PPC_ISERIES
  19. #include <asm/paca.h>
  20. #include <asm/firmware.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. struct rtc_time;
  30. extern void to_tm(int tim, struct rtc_time * tm);
  31. extern time_t last_rtc_update;
  32. extern void generic_calibrate_decr(void);
  33. extern void wakeup_decrementer(void);
  34. extern void snapshot_timebase(void);
  35. extern void set_dec_cpu6(unsigned int val);
  36. /* Some sane defaults: 125 MHz timebase, 1GHz processor */
  37. extern unsigned long ppc_proc_freq;
  38. #define DEFAULT_PROC_FREQ (DEFAULT_TB_FREQ * 8)
  39. extern unsigned long ppc_tb_freq;
  40. #define DEFAULT_TB_FREQ 125000000UL
  41. /*
  42. * By putting all of this stuff into a single struct we
  43. * reduce the number of cache lines touched by do_gettimeofday.
  44. * Both by collecting all of the data in one cache line and
  45. * by touching only one TOC entry on ppc64.
  46. */
  47. struct gettimeofday_vars {
  48. u64 tb_to_xs;
  49. u64 stamp_xsec;
  50. u64 tb_orig_stamp;
  51. };
  52. struct gettimeofday_struct {
  53. unsigned long tb_ticks_per_sec;
  54. struct gettimeofday_vars vars[2];
  55. struct gettimeofday_vars * volatile varp;
  56. unsigned var_idx;
  57. unsigned tb_to_us;
  58. };
  59. struct div_result {
  60. u64 result_high;
  61. u64 result_low;
  62. };
  63. /* Accessor functions for the timebase (RTC on 601) registers. */
  64. /* If one day CONFIG_POWER is added just define __USE_RTC as 1 */
  65. #ifdef CONFIG_6xx
  66. #define __USE_RTC() (!cpu_has_feature(CPU_FTR_USE_TB))
  67. #else
  68. #define __USE_RTC() 0
  69. #endif
  70. #ifdef CONFIG_PPC64
  71. /* For compatibility, get_tbl() is defined as get_tb() on ppc64 */
  72. #define get_tbl get_tb
  73. #else
  74. static inline unsigned long get_tbl(void)
  75. {
  76. #if defined(CONFIG_403GCX)
  77. unsigned long tbl;
  78. asm volatile("mfspr %0, 0x3dd" : "=r" (tbl));
  79. return tbl;
  80. #else
  81. return mftbl();
  82. #endif
  83. }
  84. static inline unsigned int get_tbu(void)
  85. {
  86. #ifdef CONFIG_403GCX
  87. unsigned int tbu;
  88. asm volatile("mfspr %0, 0x3dc" : "=r" (tbu));
  89. return tbu;
  90. #else
  91. return mftbu();
  92. #endif
  93. }
  94. #endif /* !CONFIG_PPC64 */
  95. static inline unsigned int get_rtcl(void)
  96. {
  97. unsigned int rtcl;
  98. asm volatile("mfrtcl %0" : "=r" (rtcl));
  99. return rtcl;
  100. }
  101. static inline u64 get_rtc(void)
  102. {
  103. unsigned int hi, lo, hi2;
  104. do {
  105. asm volatile("mfrtcu %0; mfrtcl %1; mfrtcu %2"
  106. : "=r" (hi), "=r" (lo), "=r" (hi2));
  107. } while (hi2 != hi);
  108. return (u64)hi * 1000000000 + lo;
  109. }
  110. #ifdef CONFIG_PPC64
  111. static inline u64 get_tb(void)
  112. {
  113. return mftb();
  114. }
  115. #else /* CONFIG_PPC64 */
  116. static inline u64 get_tb(void)
  117. {
  118. unsigned int tbhi, tblo, tbhi2;
  119. do {
  120. tbhi = get_tbu();
  121. tblo = get_tbl();
  122. tbhi2 = get_tbu();
  123. } while (tbhi != tbhi2);
  124. return ((u64)tbhi << 32) | tblo;
  125. }
  126. #endif /* !CONFIG_PPC64 */
  127. static inline u64 get_tb_or_rtc(void)
  128. {
  129. return __USE_RTC() ? get_rtc() : get_tb();
  130. }
  131. static inline void set_tb(unsigned int upper, unsigned int lower)
  132. {
  133. mtspr(SPRN_TBWL, 0);
  134. mtspr(SPRN_TBWU, upper);
  135. mtspr(SPRN_TBWL, lower);
  136. }
  137. /* Accessor functions for the decrementer register.
  138. * The 4xx doesn't even have a decrementer. I tried to use the
  139. * generic timer interrupt code, which seems OK, with the 4xx PIT
  140. * in auto-reload mode. The problem is PIT stops counting when it
  141. * hits zero. If it would wrap, we could use it just like a decrementer.
  142. */
  143. static inline unsigned int get_dec(void)
  144. {
  145. #if defined(CONFIG_40x)
  146. return (mfspr(SPRN_PIT));
  147. #else
  148. return (mfspr(SPRN_DEC));
  149. #endif
  150. }
  151. static inline void set_dec(int val)
  152. {
  153. #if defined(CONFIG_40x)
  154. mtspr(SPRN_PIT, val);
  155. #elif defined(CONFIG_8xx_CPU6)
  156. set_dec_cpu6(val);
  157. #else
  158. #ifdef CONFIG_PPC_ISERIES
  159. int cur_dec;
  160. if (firmware_has_feature(FW_FEATURE_ISERIES) &&
  161. get_lppaca()->shared_proc) {
  162. get_lppaca()->virtual_decr = val;
  163. cur_dec = get_dec();
  164. if (cur_dec > val)
  165. HvCall_setVirtualDecr();
  166. } else
  167. #endif
  168. mtspr(SPRN_DEC, val);
  169. #endif /* not 40x or 8xx_CPU6 */
  170. }
  171. static inline unsigned long tb_ticks_since(unsigned long tstamp)
  172. {
  173. if (__USE_RTC()) {
  174. int delta = get_rtcl() - (unsigned int) tstamp;
  175. return delta < 0 ? delta + 1000000000 : delta;
  176. }
  177. return get_tbl() - tstamp;
  178. }
  179. #define mulhwu(x,y) \
  180. ({unsigned z; asm ("mulhwu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
  181. #ifdef CONFIG_PPC64
  182. #define mulhdu(x,y) \
  183. ({unsigned long z; asm ("mulhdu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
  184. #else
  185. extern u64 mulhdu(u64, u64);
  186. #endif
  187. extern void smp_space_timers(unsigned int);
  188. extern unsigned mulhwu_scale_factor(unsigned, unsigned);
  189. extern void div128_by_32(u64 dividend_high, u64 dividend_low,
  190. unsigned divisor, struct div_result *dr);
  191. /* Used to store Processor Utilization register (purr) values */
  192. struct cpu_usage {
  193. u64 current_tb; /* Holds the current purr register values */
  194. };
  195. DECLARE_PER_CPU(struct cpu_usage, cpu_usage_array);
  196. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  197. extern void account_process_vtime(struct task_struct *tsk);
  198. #else
  199. #define account_process_vtime(tsk) do { } while (0)
  200. #endif
  201. #if defined(CONFIG_VIRT_CPU_ACCOUNTING)
  202. extern void calculate_steal_time(void);
  203. extern void snapshot_timebases(void);
  204. #else
  205. #define calculate_steal_time() do { } while (0)
  206. #define snapshot_timebases() do { } while (0)
  207. #endif
  208. extern void secondary_cpu_time_init(void);
  209. extern void iSeries_time_init_early(void);
  210. #endif /* __KERNEL__ */
  211. #endif /* __POWERPC_TIME_H */