time.h 5.1 KB

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