tick-internal.h 8.5 KB

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