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