xref: /openbmc/linux/kernel/time/tick-internal.h (revision d25a0252)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2f8381cbaSThomas Gleixner /*
3f8381cbaSThomas Gleixner  * tick internal variable and functions used by low/high res code
4f8381cbaSThomas Gleixner  */
5e2830b5cSTorben Hohn #include <linux/hrtimer.h>
6e2830b5cSTorben Hohn #include <linux/tick.h>
76441402bSThomas Gleixner 
88b094cd0SThomas Gleixner #include "timekeeping.h"
9c1797bafSThomas Gleixner #include "tick-sched.h"
108b094cd0SThomas Gleixner 
119f083b74SThomas Gleixner #ifdef CONFIG_GENERIC_CLOCKEVENTS
126441402bSThomas Gleixner 
136441402bSThomas Gleixner # define TICK_DO_TIMER_NONE	-1
146441402bSThomas Gleixner # define TICK_DO_TIMER_BOOT	-2
156441402bSThomas Gleixner 
16f8381cbaSThomas Gleixner DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
17f8381cbaSThomas Gleixner extern ktime_t tick_next_period;
18d3ed7824SThomas Gleixner extern int tick_do_timer_cpu __read_mostly;
19f8381cbaSThomas Gleixner 
20f8381cbaSThomas Gleixner extern void tick_setup_periodic(struct clock_event_device *dev, int broadcast);
21f8381cbaSThomas Gleixner extern void tick_handle_periodic(struct clock_event_device *dev);
227172a286SThomas Gleixner extern void tick_check_new_device(struct clock_event_device *dev);
23a49b116dSThomas Gleixner extern void tick_shutdown(unsigned int cpu);
248c53daf6SThomas Gleixner extern void tick_suspend(void);
25f46481d0SThomas Gleixner extern void tick_resume(void);
2603e13cf5SThomas Gleixner extern bool tick_check_replacement(struct clock_event_device *curdev,
2703e13cf5SThomas Gleixner 				   struct clock_event_device *newdev);
2803e13cf5SThomas Gleixner extern void tick_install_replacement(struct clock_event_device *dev);
29c1797bafSThomas Gleixner extern int tick_is_oneshot_available(void);
307270d11cSThomas Gleixner extern struct tick_device *tick_get_device(int cpu);
31f8381cbaSThomas Gleixner 
32554ef387SViresh Kumar extern int clockevents_tick_resume(struct clock_event_device *dev);
33b7475eb5SThomas Gleixner /* Check, if the device is functional or a dummy for broadcast */
tick_device_is_functional(struct clock_event_device * dev)34b7475eb5SThomas Gleixner static inline int tick_device_is_functional(struct clock_event_device *dev)
35b7475eb5SThomas Gleixner {
36b7475eb5SThomas Gleixner 	return !(dev->features & CLOCK_EVT_FEAT_DUMMY);
37b7475eb5SThomas Gleixner }
382344abbcSThomas Gleixner 
clockevent_get_state(struct clock_event_device * dev)39d7eb231cSThomas Gleixner static inline enum clock_event_state clockevent_get_state(struct clock_event_device *dev)
40d7eb231cSThomas Gleixner {
41be3ef76eSThomas Gleixner 	return dev->state_use_accessors;
42d7eb231cSThomas Gleixner }
43d7eb231cSThomas Gleixner 
clockevent_set_state(struct clock_event_device * dev,enum clock_event_state state)44d7eb231cSThomas Gleixner static inline void clockevent_set_state(struct clock_event_device *dev,
45d7eb231cSThomas Gleixner 					enum clock_event_state state)
46d7eb231cSThomas Gleixner {
47be3ef76eSThomas Gleixner 	dev->state_use_accessors = state;
48d7eb231cSThomas Gleixner }
49d7eb231cSThomas Gleixner 
50b7475eb5SThomas Gleixner extern void clockevents_shutdown(struct clock_event_device *dev);
51c1797bafSThomas Gleixner extern void clockevents_exchange_device(struct clock_event_device *old,
52c1797bafSThomas Gleixner 					struct clock_event_device *new);
53d7eb231cSThomas Gleixner extern void clockevents_switch_state(struct clock_event_device *dev,
547270d11cSThomas Gleixner 				     enum clock_event_state state);
557270d11cSThomas Gleixner extern int clockevents_program_event(struct clock_event_device *dev,
567270d11cSThomas Gleixner 				     ktime_t expires, bool force);
57c1797bafSThomas Gleixner extern void clockevents_handle_noop(struct clock_event_device *dev);
58b7475eb5SThomas Gleixner extern int __clockevents_update_freq(struct clock_event_device *dev, u32 freq);
59891292a7SPatrick Palka extern ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt);
6079bf2bb3SThomas Gleixner 
61b7475eb5SThomas Gleixner /* Broadcasting support */
62f8381cbaSThomas Gleixner # ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
63f8381cbaSThomas Gleixner extern int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu);
64c94a8537SWill Deacon extern void tick_install_broadcast_device(struct clock_event_device *dev, int cpu);
65f8381cbaSThomas Gleixner extern int tick_is_broadcast_device(struct clock_event_device *dev);
666321dd60SThomas Gleixner extern void tick_suspend_broadcast(void);
67f46481d0SThomas Gleixner extern void tick_resume_broadcast(void);
68f46481d0SThomas Gleixner extern bool tick_resume_check_broadcast(void);
69b352bc1cSThomas Gleixner extern void tick_broadcast_init(void);
70b7475eb5SThomas Gleixner extern void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast);
71b7475eb5SThomas Gleixner extern int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq);
72c1797bafSThomas Gleixner extern struct tick_device *tick_get_broadcast_device(void);
73c1797bafSThomas Gleixner extern struct cpumask *tick_get_broadcast_mask(void);
74245a057fSWill Deacon extern const struct clock_event_device *tick_get_wakeup_device(int cpu);
753ae7a939SIngo Molnar # else /* !CONFIG_GENERIC_CLOCKEVENTS_BROADCAST: */
tick_install_broadcast_device(struct clock_event_device * dev,int cpu)76c94a8537SWill Deacon static inline void tick_install_broadcast_device(struct clock_event_device *dev, int cpu) { }
tick_is_broadcast_device(struct clock_event_device * dev)77b7475eb5SThomas Gleixner static inline int tick_is_broadcast_device(struct clock_event_device *dev) { return 0; }
tick_device_uses_broadcast(struct clock_event_device * dev,int cpu)78b7475eb5SThomas Gleixner static inline int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu) { return 0; }
tick_do_periodic_broadcast(struct clock_event_device * d)79f8381cbaSThomas Gleixner static inline void tick_do_periodic_broadcast(struct clock_event_device *d) { }
tick_suspend_broadcast(void)806321dd60SThomas Gleixner static inline void tick_suspend_broadcast(void) { }
tick_resume_broadcast(void)81f46481d0SThomas Gleixner static inline void tick_resume_broadcast(void) { }
tick_resume_check_broadcast(void)82f46481d0SThomas Gleixner static inline bool tick_resume_check_broadcast(void) { return false; }
tick_broadcast_init(void)83b352bc1cSThomas Gleixner static inline void tick_broadcast_init(void) { }
tick_broadcast_update_freq(struct clock_event_device * dev,u32 freq)84b7475eb5SThomas Gleixner static inline int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq) { return -ENODEV; }
85f8381cbaSThomas Gleixner 
86b7475eb5SThomas Gleixner /* Set the periodic handler in non broadcast mode */
tick_set_periodic_handler(struct clock_event_device * dev,int broadcast)87b7475eb5SThomas Gleixner static inline void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
88f8381cbaSThomas Gleixner {
89f8381cbaSThomas Gleixner 	dev->event_handler = tick_handle_periodic;
90f8381cbaSThomas Gleixner }
913ae7a939SIngo Molnar # endif /* !CONFIG_GENERIC_CLOCKEVENTS_BROADCAST */
923ae7a939SIngo Molnar 
933ae7a939SIngo Molnar #else /* !GENERIC_CLOCKEVENTS: */
tick_suspend(void)943ae7a939SIngo Molnar static inline void tick_suspend(void) { }
tick_resume(void)953ae7a939SIngo Molnar static inline void tick_resume(void) { }
963ae7a939SIngo Molnar #endif /* !GENERIC_CLOCKEVENTS */
973ae7a939SIngo Molnar 
983ae7a939SIngo Molnar /* Oneshot related functions */
993ae7a939SIngo Molnar #ifdef CONFIG_TICK_ONESHOT
1003ae7a939SIngo Molnar extern void tick_setup_oneshot(struct clock_event_device *newdev,
1013ae7a939SIngo Molnar 			       void (*handler)(struct clock_event_device *),
1023ae7a939SIngo Molnar 			       ktime_t nextevt);
1033ae7a939SIngo Molnar extern int tick_program_event(ktime_t expires, int force);
1043ae7a939SIngo Molnar extern void tick_oneshot_notify(void);
1053ae7a939SIngo Molnar extern int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *));
1063ae7a939SIngo Molnar extern void tick_resume_oneshot(void);
tick_oneshot_possible(void)1073ae7a939SIngo Molnar static inline bool tick_oneshot_possible(void) { return true; }
1083ae7a939SIngo Molnar extern int tick_oneshot_mode_active(void);
1093ae7a939SIngo Molnar extern void tick_clock_notify(void);
1103ae7a939SIngo Molnar extern int tick_check_oneshot_change(int allow_nohz);
1113ae7a939SIngo Molnar extern int tick_init_highres(void);
1123ae7a939SIngo Molnar #else /* !CONFIG_TICK_ONESHOT: */
1133ae7a939SIngo Molnar static inline
tick_setup_oneshot(struct clock_event_device * newdev,void (* handler)(struct clock_event_device *),ktime_t nextevt)1143ae7a939SIngo Molnar void tick_setup_oneshot(struct clock_event_device *newdev,
1153ae7a939SIngo Molnar 			void (*handler)(struct clock_event_device *),
1163ae7a939SIngo Molnar 			ktime_t nextevt) { BUG(); }
tick_resume_oneshot(void)1173ae7a939SIngo Molnar static inline void tick_resume_oneshot(void) { BUG(); }
tick_program_event(ktime_t expires,int force)1183ae7a939SIngo Molnar static inline int tick_program_event(ktime_t expires, int force) { return 0; }
tick_oneshot_notify(void)1193ae7a939SIngo Molnar static inline void tick_oneshot_notify(void) { }
tick_oneshot_possible(void)1203ae7a939SIngo Molnar static inline bool tick_oneshot_possible(void) { return false; }
tick_oneshot_mode_active(void)1213ae7a939SIngo Molnar static inline int tick_oneshot_mode_active(void) { return 0; }
tick_clock_notify(void)1223ae7a939SIngo Molnar static inline void tick_clock_notify(void) { }
tick_check_oneshot_change(int allow_nohz)1233ae7a939SIngo Molnar static inline int tick_check_oneshot_change(int allow_nohz) { return 0; }
1243ae7a939SIngo Molnar #endif /* !CONFIG_TICK_ONESHOT */
125f8381cbaSThomas Gleixner 
126b7475eb5SThomas Gleixner /* Functions related to oneshot broadcasting */
127b7475eb5SThomas Gleixner #if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
128b7475eb5SThomas Gleixner extern void tick_broadcast_switch_to_oneshot(void);
129b7475eb5SThomas Gleixner extern int tick_broadcast_oneshot_active(void);
130b7475eb5SThomas Gleixner extern void tick_check_oneshot_broadcast_this_cpu(void);
131b7475eb5SThomas Gleixner bool tick_broadcast_oneshot_available(void);
132c1797bafSThomas Gleixner extern struct cpumask *tick_get_broadcast_oneshot_mask(void);
1333ae7a939SIngo Molnar #else /* !(BROADCAST && ONESHOT): */
tick_broadcast_switch_to_oneshot(void)134b7475eb5SThomas Gleixner static inline void tick_broadcast_switch_to_oneshot(void) { }
tick_broadcast_oneshot_active(void)135b7475eb5SThomas Gleixner static inline int tick_broadcast_oneshot_active(void) { return 0; }
tick_check_oneshot_broadcast_this_cpu(void)136b7475eb5SThomas Gleixner static inline void tick_check_oneshot_broadcast_this_cpu(void) { }
tick_broadcast_oneshot_available(void)137b7475eb5SThomas Gleixner static inline bool tick_broadcast_oneshot_available(void) { return tick_oneshot_possible(); }
1383ae7a939SIngo Molnar #endif /* !(BROADCAST && ONESHOT) */
139e2830b5cSTorben Hohn 
1401b72d432SThomas Gleixner #if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_HOTPLUG_CPU)
1411b72d432SThomas Gleixner extern void tick_broadcast_offline(unsigned int cpu);
1421b72d432SThomas Gleixner #else
tick_broadcast_offline(unsigned int cpu)1431b72d432SThomas Gleixner static inline void tick_broadcast_offline(unsigned int cpu) { }
1441b72d432SThomas Gleixner #endif
1451b72d432SThomas Gleixner 
146b7475eb5SThomas Gleixner /* NO_HZ_FULL internal */
147b7475eb5SThomas Gleixner #ifdef CONFIG_NO_HZ_FULL
148b7475eb5SThomas Gleixner extern void tick_nohz_init(void);
149b7475eb5SThomas Gleixner # else
tick_nohz_init(void)150b7475eb5SThomas Gleixner static inline void tick_nohz_init(void) { }
151b7475eb5SThomas Gleixner #endif
152c1ad348bSThomas Gleixner 
153bc7a34b8SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
154bc7a34b8SThomas Gleixner extern unsigned long tick_nohz_active;
155ae67badaSThomas Gleixner extern void timers_update_nohz(void);
156ae67badaSThomas Gleixner # ifdef CONFIG_SMP
157ae67badaSThomas Gleixner extern struct static_key_false timers_migration_enabled;
158bc7a34b8SThomas Gleixner # endif
159ae67badaSThomas Gleixner #else /* CONFIG_NO_HZ_COMMON */
timers_update_nohz(void)160ae67badaSThomas Gleixner static inline void timers_update_nohz(void) { }
161ae67badaSThomas Gleixner #define tick_nohz_active (0)
162bc7a34b8SThomas Gleixner #endif
163bc7a34b8SThomas Gleixner 
164bc7a34b8SThomas Gleixner DECLARE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases);
165bc7a34b8SThomas Gleixner 
166c1ad348bSThomas Gleixner extern u64 get_next_timer_interrupt(unsigned long basej, u64 basem);
167a683f390SThomas Gleixner void timer_clear_idle(void);
1688c3b5e6eSThomas Gleixner 
16917a1b882SThomas Gleixner #define CLOCK_SET_WALL							\
17017a1b882SThomas Gleixner 	(BIT(HRTIMER_BASE_REALTIME) | BIT(HRTIMER_BASE_REALTIME_SOFT) |	\
17117a1b882SThomas Gleixner 	 BIT(HRTIMER_BASE_TAI) | BIT(HRTIMER_BASE_TAI_SOFT))
17217a1b882SThomas Gleixner 
17317a1b882SThomas Gleixner #define CLOCK_SET_BOOT							\
17417a1b882SThomas Gleixner 	(BIT(HRTIMER_BASE_BOOTTIME) | BIT(HRTIMER_BASE_BOOTTIME_SOFT))
17517a1b882SThomas Gleixner 
17617a1b882SThomas Gleixner void clock_was_set(unsigned int bases);
1778c3b5e6eSThomas Gleixner void clock_was_set_delayed(void);
178a761a67fSThomas Gleixner 
179a761a67fSThomas Gleixner void hrtimers_resume_local(void);
180*d25a0252SPaul E. McKenney 
181*d25a0252SPaul E. McKenney /* Since jiffies uses a simple TICK_NSEC multiplier
182*d25a0252SPaul E. McKenney  * conversion, the .shift value could be zero. However
183*d25a0252SPaul E. McKenney  * this would make NTP adjustments impossible as they are
184*d25a0252SPaul E. McKenney  * in units of 1/2^.shift. Thus we use JIFFIES_SHIFT to
185*d25a0252SPaul E. McKenney  * shift both the nominator and denominator the same
186*d25a0252SPaul E. McKenney  * amount, and give ntp adjustments in units of 1/2^8
187*d25a0252SPaul E. McKenney  *
188*d25a0252SPaul E. McKenney  * The value 8 is somewhat carefully chosen, as anything
189*d25a0252SPaul E. McKenney  * larger can result in overflows. TICK_NSEC grows as HZ
190*d25a0252SPaul E. McKenney  * shrinks, so values greater than 8 overflow 32bits when
191*d25a0252SPaul E. McKenney  * HZ=100.
192*d25a0252SPaul E. McKenney  */
193*d25a0252SPaul E. McKenney #if HZ < 34
194*d25a0252SPaul E. McKenney #define JIFFIES_SHIFT	6
195*d25a0252SPaul E. McKenney #elif HZ < 67
196*d25a0252SPaul E. McKenney #define JIFFIES_SHIFT	7
197*d25a0252SPaul E. McKenney #else
198*d25a0252SPaul E. McKenney #define JIFFIES_SHIFT	8
199*d25a0252SPaul E. McKenney #endif
200