time.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Common time prototypes and such for all ppc machines.
  4. *
  5. * Written by Cort Dougan ([email protected]) to merge
  6. * Paul Mackerras' version and mine for PReP and Pmac.
  7. */
  8. #ifndef __POWERPC_TIME_H
  9. #define __POWERPC_TIME_H
  10. #ifdef __KERNEL__
  11. #include <linux/types.h>
  12. #include <linux/percpu.h>
  13. #include <asm/processor.h>
  14. #include <asm/cpu_has_feature.h>
  15. #include <asm/vdso/timebase.h>
  16. /* time.c */
  17. extern u64 decrementer_max;
  18. extern unsigned long tb_ticks_per_jiffy;
  19. extern unsigned long tb_ticks_per_usec;
  20. extern unsigned long tb_ticks_per_sec;
  21. extern struct clock_event_device decrementer_clockevent;
  22. extern u64 decrementer_max;
  23. extern void generic_calibrate_decr(void);
  24. /* Some sane defaults: 125 MHz timebase, 1GHz processor */
  25. extern unsigned long ppc_proc_freq;
  26. #define DEFAULT_PROC_FREQ (DEFAULT_TB_FREQ * 8)
  27. extern unsigned long ppc_tb_freq;
  28. #define DEFAULT_TB_FREQ 125000000UL
  29. extern bool tb_invalid;
  30. struct div_result {
  31. u64 result_high;
  32. u64 result_low;
  33. };
  34. static inline u64 get_vtb(void)
  35. {
  36. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  37. return mfspr(SPRN_VTB);
  38. return 0;
  39. }
  40. /* Accessor functions for the decrementer register.
  41. * The 4xx doesn't even have a decrementer. I tried to use the
  42. * generic timer interrupt code, which seems OK, with the 4xx PIT
  43. * in auto-reload mode. The problem is PIT stops counting when it
  44. * hits zero. If it would wrap, we could use it just like a decrementer.
  45. */
  46. static inline u64 get_dec(void)
  47. {
  48. if (IS_ENABLED(CONFIG_40x))
  49. return mfspr(SPRN_PIT);
  50. return mfspr(SPRN_DEC);
  51. }
  52. /*
  53. * Note: Book E and 4xx processors differ from other PowerPC processors
  54. * in when the decrementer generates its interrupt: on the 1 to 0
  55. * transition for Book E/4xx, but on the 0 to -1 transition for others.
  56. */
  57. static inline void set_dec(u64 val)
  58. {
  59. if (IS_ENABLED(CONFIG_40x))
  60. mtspr(SPRN_PIT, (u32)val);
  61. else if (IS_ENABLED(CONFIG_BOOKE))
  62. mtspr(SPRN_DEC, val);
  63. else
  64. mtspr(SPRN_DEC, val - 1);
  65. }
  66. static inline unsigned long tb_ticks_since(unsigned long tstamp)
  67. {
  68. return mftb() - tstamp;
  69. }
  70. #define mulhwu(x,y) \
  71. ({unsigned z; asm ("mulhwu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
  72. #ifdef CONFIG_PPC64
  73. #define mulhdu(x,y) \
  74. ({unsigned long z; asm ("mulhdu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
  75. #else
  76. extern u64 mulhdu(u64, u64);
  77. #endif
  78. extern void div128_by_32(u64 dividend_high, u64 dividend_low,
  79. unsigned divisor, struct div_result *dr);
  80. extern void secondary_cpu_time_init(void);
  81. extern void __init time_init(void);
  82. DECLARE_PER_CPU(u64, decrementers_next_tb);
  83. static inline u64 timer_get_next_tb(void)
  84. {
  85. return __this_cpu_read(decrementers_next_tb);
  86. }
  87. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  88. void timer_rearm_host_dec(u64 now);
  89. #endif
  90. /* Convert timebase ticks to nanoseconds */
  91. unsigned long long tb_to_ns(unsigned long long tb_ticks);
  92. void timer_broadcast_interrupt(void);
  93. /* SPLPAR and VIRT_CPU_ACCOUNTING_NATIVE */
  94. void pseries_accumulate_stolen_time(void);
  95. u64 pseries_calculate_stolen_time(u64 stop_tb);
  96. #endif /* __KERNEL__ */
  97. #endif /* __POWERPC_TIME_H */