time.h 5.3 KB

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