time.h 4.8 KB

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