tick-internal.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * tick internal variable and functions used by low/high res code
  4. */
  5. #include <linux/hrtimer.h>
  6. #include <linux/tick.h>
  7. #include "timekeeping.h"
  8. #include "tick-sched.h"
  9. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  10. # define TICK_DO_TIMER_NONE -1
  11. # define TICK_DO_TIMER_BOOT -2
  12. DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
  13. extern ktime_t tick_next_period;
  14. extern int tick_do_timer_cpu __read_mostly;
  15. extern void tick_setup_periodic(struct clock_event_device *dev, int broadcast);
  16. extern void tick_handle_periodic(struct clock_event_device *dev);
  17. extern void tick_check_new_device(struct clock_event_device *dev);
  18. extern void tick_shutdown(unsigned int cpu);
  19. extern void tick_suspend(void);
  20. extern void tick_resume(void);
  21. extern bool tick_check_replacement(struct clock_event_device *curdev,
  22. struct clock_event_device *newdev);
  23. extern void tick_install_replacement(struct clock_event_device *dev);
  24. extern int tick_is_oneshot_available(void);
  25. extern struct tick_device *tick_get_device(int cpu);
  26. extern int clockevents_tick_resume(struct clock_event_device *dev);
  27. /* Check, if the device is functional or a dummy for broadcast */
  28. static inline int tick_device_is_functional(struct clock_event_device *dev)
  29. {
  30. return !(dev->features & CLOCK_EVT_FEAT_DUMMY);
  31. }
  32. static inline enum clock_event_state clockevent_get_state(struct clock_event_device *dev)
  33. {
  34. return dev->state_use_accessors;
  35. }
  36. static inline void clockevent_set_state(struct clock_event_device *dev,
  37. enum clock_event_state state)
  38. {
  39. dev->state_use_accessors = state;
  40. }
  41. extern void clockevents_shutdown(struct clock_event_device *dev);
  42. extern void clockevents_exchange_device(struct clock_event_device *old,
  43. struct clock_event_device *new);
  44. extern void clockevents_switch_state(struct clock_event_device *dev,
  45. enum clock_event_state state);
  46. extern int clockevents_program_event(struct clock_event_device *dev,
  47. ktime_t expires, bool force);
  48. extern void clockevents_handle_noop(struct clock_event_device *dev);
  49. extern int __clockevents_update_freq(struct clock_event_device *dev, u32 freq);
  50. extern ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt);
  51. /* Broadcasting support */
  52. # ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  53. extern int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu);
  54. extern void tick_install_broadcast_device(struct clock_event_device *dev, int cpu);
  55. extern int tick_is_broadcast_device(struct clock_event_device *dev);
  56. extern void tick_suspend_broadcast(void);
  57. extern void tick_resume_broadcast(void);
  58. extern bool tick_resume_check_broadcast(void);
  59. extern void tick_broadcast_init(void);
  60. extern void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast);
  61. extern int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq);
  62. extern struct tick_device *tick_get_broadcast_device(void);
  63. extern struct cpumask *tick_get_broadcast_mask(void);
  64. extern const struct clock_event_device *tick_get_wakeup_device(int cpu);
  65. # else /* !CONFIG_GENERIC_CLOCKEVENTS_BROADCAST: */
  66. static inline void tick_install_broadcast_device(struct clock_event_device *dev, int cpu) { }
  67. static inline int tick_is_broadcast_device(struct clock_event_device *dev) { return 0; }
  68. static inline int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu) { return 0; }
  69. static inline void tick_do_periodic_broadcast(struct clock_event_device *d) { }
  70. static inline void tick_suspend_broadcast(void) { }
  71. static inline void tick_resume_broadcast(void) { }
  72. static inline bool tick_resume_check_broadcast(void) { return false; }
  73. static inline void tick_broadcast_init(void) { }
  74. static inline int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq) { return -ENODEV; }
  75. /* Set the periodic handler in non broadcast mode */
  76. static inline void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
  77. {
  78. dev->event_handler = tick_handle_periodic;
  79. }
  80. # endif /* !CONFIG_GENERIC_CLOCKEVENTS_BROADCAST */
  81. #else /* !GENERIC_CLOCKEVENTS: */
  82. static inline void tick_suspend(void) { }
  83. static inline void tick_resume(void) { }
  84. #endif /* !GENERIC_CLOCKEVENTS */
  85. /* Oneshot related functions */
  86. #ifdef CONFIG_TICK_ONESHOT
  87. extern void tick_setup_oneshot(struct clock_event_device *newdev,
  88. void (*handler)(struct clock_event_device *),
  89. ktime_t nextevt);
  90. extern int tick_program_event(ktime_t expires, int force);
  91. extern void tick_oneshot_notify(void);
  92. extern int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *));
  93. extern void tick_resume_oneshot(void);
  94. static inline bool tick_oneshot_possible(void) { return true; }
  95. extern int tick_oneshot_mode_active(void);
  96. extern void tick_clock_notify(void);
  97. extern int tick_check_oneshot_change(int allow_nohz);
  98. extern int tick_init_highres(void);
  99. #else /* !CONFIG_TICK_ONESHOT: */
  100. static inline
  101. void tick_setup_oneshot(struct clock_event_device *newdev,
  102. void (*handler)(struct clock_event_device *),
  103. ktime_t nextevt) { BUG(); }
  104. static inline void tick_resume_oneshot(void) { BUG(); }
  105. static inline int tick_program_event(ktime_t expires, int force) { return 0; }
  106. static inline void tick_oneshot_notify(void) { }
  107. static inline bool tick_oneshot_possible(void) { return false; }
  108. static inline int tick_oneshot_mode_active(void) { return 0; }
  109. static inline void tick_clock_notify(void) { }
  110. static inline int tick_check_oneshot_change(int allow_nohz) { return 0; }
  111. #endif /* !CONFIG_TICK_ONESHOT */
  112. /* Functions related to oneshot broadcasting */
  113. #if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
  114. extern void tick_broadcast_switch_to_oneshot(void);
  115. extern int tick_broadcast_oneshot_active(void);
  116. extern void tick_check_oneshot_broadcast_this_cpu(void);
  117. bool tick_broadcast_oneshot_available(void);
  118. extern struct cpumask *tick_get_broadcast_oneshot_mask(void);
  119. #else /* !(BROADCAST && ONESHOT): */
  120. static inline void tick_broadcast_switch_to_oneshot(void) { }
  121. static inline int tick_broadcast_oneshot_active(void) { return 0; }
  122. static inline void tick_check_oneshot_broadcast_this_cpu(void) { }
  123. static inline bool tick_broadcast_oneshot_available(void) { return tick_oneshot_possible(); }
  124. #endif /* !(BROADCAST && ONESHOT) */
  125. #if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_HOTPLUG_CPU)
  126. extern void tick_broadcast_offline(unsigned int cpu);
  127. #else
  128. static inline void tick_broadcast_offline(unsigned int cpu) { }
  129. #endif
  130. /* NO_HZ_FULL internal */
  131. #ifdef CONFIG_NO_HZ_FULL
  132. extern void tick_nohz_init(void);
  133. # else
  134. static inline void tick_nohz_init(void) { }
  135. #endif
  136. #ifdef CONFIG_NO_HZ_COMMON
  137. extern unsigned long tick_nohz_active;
  138. extern void timers_update_nohz(void);
  139. # ifdef CONFIG_SMP
  140. extern struct static_key_false timers_migration_enabled;
  141. # endif
  142. #else /* CONFIG_NO_HZ_COMMON */
  143. static inline void timers_update_nohz(void) { }
  144. #define tick_nohz_active (0)
  145. #endif
  146. DECLARE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases);
  147. extern u64 get_next_timer_interrupt(unsigned long basej, u64 basem);
  148. void timer_clear_idle(void);
  149. #define CLOCK_SET_WALL \
  150. (BIT(HRTIMER_BASE_REALTIME) | BIT(HRTIMER_BASE_REALTIME_SOFT) | \
  151. BIT(HRTIMER_BASE_TAI) | BIT(HRTIMER_BASE_TAI_SOFT))
  152. #define CLOCK_SET_BOOT \
  153. (BIT(HRTIMER_BASE_BOOTTIME) | BIT(HRTIMER_BASE_BOOTTIME_SOFT))
  154. void clock_was_set(unsigned int bases);
  155. void clock_was_set_delayed(void);
  156. void hrtimers_resume_local(void);
  157. /* Since jiffies uses a simple TICK_NSEC multiplier
  158. * conversion, the .shift value could be zero. However
  159. * this would make NTP adjustments impossible as they are
  160. * in units of 1/2^.shift. Thus we use JIFFIES_SHIFT to
  161. * shift both the nominator and denominator the same
  162. * amount, and give ntp adjustments in units of 1/2^8
  163. *
  164. * The value 8 is somewhat carefully chosen, as anything
  165. * larger can result in overflows. TICK_NSEC grows as HZ
  166. * shrinks, so values greater than 8 overflow 32bits when
  167. * HZ=100.
  168. */
  169. #if HZ < 34
  170. #define JIFFIES_SHIFT 6
  171. #elif HZ < 67
  172. #define JIFFIES_SHIFT 7
  173. #else
  174. #define JIFFIES_SHIFT 8
  175. #endif