xref: /linux-6.15/include/linux/tick.h (revision 3ad6eb06)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Tick related global functions
4  */
5 #ifndef _LINUX_TICK_H
6 #define _LINUX_TICK_H
7 
8 #include <linux/clockchips.h>
9 #include <linux/irqflags.h>
10 #include <linux/percpu.h>
11 #include <linux/context_tracking_state.h>
12 #include <linux/cpumask.h>
13 #include <linux/sched.h>
14 #include <linux/rcupdate.h>
15 
16 #ifdef CONFIG_GENERIC_CLOCKEVENTS
17 extern void __init tick_init(void);
18 /* Should be core only, but ARM BL switcher requires it */
19 extern void tick_suspend_local(void);
20 /* Should be core only, but XEN resume magic and ARM BL switcher require it */
21 extern void tick_resume_local(void);
22 extern void tick_cleanup_dead_cpu(int cpu);
23 #else /* CONFIG_GENERIC_CLOCKEVENTS */
24 static inline void tick_init(void) { }
25 static inline void tick_suspend_local(void) { }
26 static inline void tick_resume_local(void) { }
27 static inline void tick_cleanup_dead_cpu(int cpu) { }
28 #endif /* !CONFIG_GENERIC_CLOCKEVENTS */
29 
30 #if defined(CONFIG_GENERIC_CLOCKEVENTS) && defined(CONFIG_HOTPLUG_CPU)
31 extern int tick_cpu_dying(unsigned int cpu);
32 #else
33 #define tick_cpu_dying	NULL
34 #endif
35 
36 #if defined(CONFIG_GENERIC_CLOCKEVENTS) && defined(CONFIG_SUSPEND)
37 extern void tick_freeze(void);
38 extern void tick_unfreeze(void);
39 #else
40 static inline void tick_freeze(void) { }
41 static inline void tick_unfreeze(void) { }
42 #endif
43 
44 #ifdef CONFIG_TICK_ONESHOT
45 extern void tick_irq_enter(void);
46 #  ifndef arch_needs_cpu
47 #   define arch_needs_cpu() (0)
48 #  endif
49 # else
50 static inline void tick_irq_enter(void) { }
51 #endif
52 
53 #if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
54 extern void hotplug_cpu__broadcast_tick_pull(int dead_cpu);
55 #else
56 static inline void hotplug_cpu__broadcast_tick_pull(int dead_cpu) { }
57 #endif
58 
59 enum tick_broadcast_mode {
60 	TICK_BROADCAST_OFF,
61 	TICK_BROADCAST_ON,
62 	TICK_BROADCAST_FORCE,
63 };
64 
65 enum tick_broadcast_state {
66 	TICK_BROADCAST_EXIT,
67 	TICK_BROADCAST_ENTER,
68 };
69 
70 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
71 extern void tick_broadcast_control(enum tick_broadcast_mode mode);
72 #else
73 static inline void tick_broadcast_control(enum tick_broadcast_mode mode) { }
74 #endif /* BROADCAST */
75 
76 #if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_HOTPLUG_CPU)
77 extern void tick_offline_cpu(unsigned int cpu);
78 #else
79 static inline void tick_offline_cpu(unsigned int cpu) { }
80 #endif
81 
82 #ifdef CONFIG_GENERIC_CLOCKEVENTS
83 extern int tick_broadcast_oneshot_control(enum tick_broadcast_state state);
84 #else
85 static inline int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
86 {
87 	return 0;
88 }
89 #endif
90 
91 static inline void tick_broadcast_enable(void)
92 {
93 	tick_broadcast_control(TICK_BROADCAST_ON);
94 }
95 static inline void tick_broadcast_disable(void)
96 {
97 	tick_broadcast_control(TICK_BROADCAST_OFF);
98 }
99 static inline void tick_broadcast_force(void)
100 {
101 	tick_broadcast_control(TICK_BROADCAST_FORCE);
102 }
103 static inline int tick_broadcast_enter(void)
104 {
105 	return tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER);
106 }
107 static inline void tick_broadcast_exit(void)
108 {
109 	tick_broadcast_oneshot_control(TICK_BROADCAST_EXIT);
110 }
111 
112 enum tick_dep_bits {
113 	TICK_DEP_BIT_POSIX_TIMER	= 0,
114 	TICK_DEP_BIT_PERF_EVENTS	= 1,
115 	TICK_DEP_BIT_SCHED		= 2,
116 	TICK_DEP_BIT_CLOCK_UNSTABLE	= 3,
117 	TICK_DEP_BIT_RCU		= 4,
118 	TICK_DEP_BIT_RCU_EXP		= 5
119 };
120 #define TICK_DEP_BIT_MAX TICK_DEP_BIT_RCU_EXP
121 
122 #define TICK_DEP_MASK_NONE		0
123 #define TICK_DEP_MASK_POSIX_TIMER	(1 << TICK_DEP_BIT_POSIX_TIMER)
124 #define TICK_DEP_MASK_PERF_EVENTS	(1 << TICK_DEP_BIT_PERF_EVENTS)
125 #define TICK_DEP_MASK_SCHED		(1 << TICK_DEP_BIT_SCHED)
126 #define TICK_DEP_MASK_CLOCK_UNSTABLE	(1 << TICK_DEP_BIT_CLOCK_UNSTABLE)
127 #define TICK_DEP_MASK_RCU		(1 << TICK_DEP_BIT_RCU)
128 #define TICK_DEP_MASK_RCU_EXP		(1 << TICK_DEP_BIT_RCU_EXP)
129 
130 #ifdef CONFIG_NO_HZ_COMMON
131 extern bool tick_nohz_enabled;
132 extern bool tick_nohz_tick_stopped(void);
133 extern bool tick_nohz_tick_stopped_cpu(int cpu);
134 extern void tick_nohz_idle_stop_tick(void);
135 extern void tick_nohz_idle_retain_tick(void);
136 extern void tick_nohz_idle_restart_tick(void);
137 extern void tick_nohz_idle_enter(void);
138 extern void tick_nohz_idle_exit(void);
139 extern void tick_nohz_irq_exit(void);
140 extern bool tick_nohz_idle_got_tick(void);
141 extern ktime_t tick_nohz_get_next_hrtimer(void);
142 extern ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next);
143 extern unsigned long tick_nohz_get_idle_calls(void);
144 extern unsigned long tick_nohz_get_idle_calls_cpu(int cpu);
145 extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
146 extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
147 #else /* !CONFIG_NO_HZ_COMMON */
148 #define tick_nohz_enabled (0)
149 static inline int tick_nohz_tick_stopped(void) { return 0; }
150 static inline int tick_nohz_tick_stopped_cpu(int cpu) { return 0; }
151 static inline void tick_nohz_idle_stop_tick(void) { }
152 static inline void tick_nohz_idle_retain_tick(void) { }
153 static inline void tick_nohz_idle_restart_tick(void) { }
154 static inline void tick_nohz_idle_enter(void) { }
155 static inline void tick_nohz_idle_exit(void) { }
156 static inline bool tick_nohz_idle_got_tick(void) { return false; }
157 static inline ktime_t tick_nohz_get_next_hrtimer(void)
158 {
159 	/* Next wake up is the tick period, assume it starts now */
160 	return ktime_add(ktime_get(), TICK_NSEC);
161 }
162 static inline ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
163 {
164 	*delta_next = TICK_NSEC;
165 	return *delta_next;
166 }
167 static inline u64 get_cpu_idle_time_us(int cpu, u64 *unused) { return -1; }
168 static inline u64 get_cpu_iowait_time_us(int cpu, u64 *unused) { return -1; }
169 #endif /* !CONFIG_NO_HZ_COMMON */
170 
171 #ifdef CONFIG_NO_HZ_FULL
172 extern bool tick_nohz_full_running;
173 extern cpumask_var_t tick_nohz_full_mask;
174 
175 static inline bool tick_nohz_full_enabled(void)
176 {
177 	if (!context_tracking_enabled())
178 		return false;
179 
180 	return tick_nohz_full_running;
181 }
182 
183 /*
184  * Check if a CPU is part of the nohz_full subset. Arrange for evaluating
185  * the cpu expression (typically smp_processor_id()) _after_ the static
186  * key.
187  */
188 #define tick_nohz_full_cpu(_cpu) ({					\
189 	bool __ret = false;						\
190 	if (tick_nohz_full_enabled())					\
191 		__ret = cpumask_test_cpu((_cpu), tick_nohz_full_mask);	\
192 	__ret;								\
193 })
194 
195 static inline void tick_nohz_full_add_cpus_to(struct cpumask *mask)
196 {
197 	if (tick_nohz_full_enabled())
198 		cpumask_or(mask, mask, tick_nohz_full_mask);
199 }
200 
201 extern void tick_nohz_dep_set(enum tick_dep_bits bit);
202 extern void tick_nohz_dep_clear(enum tick_dep_bits bit);
203 extern void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit);
204 extern void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit);
205 extern void tick_nohz_dep_set_task(struct task_struct *tsk,
206 				   enum tick_dep_bits bit);
207 extern void tick_nohz_dep_clear_task(struct task_struct *tsk,
208 				     enum tick_dep_bits bit);
209 extern void tick_nohz_dep_set_signal(struct task_struct *tsk,
210 				     enum tick_dep_bits bit);
211 extern void tick_nohz_dep_clear_signal(struct signal_struct *signal,
212 				       enum tick_dep_bits bit);
213 extern bool tick_nohz_cpu_hotpluggable(unsigned int cpu);
214 
215 /*
216  * The below are tick_nohz_[set,clear]_dep() wrappers that optimize off-cases
217  * on top of static keys.
218  */
219 static inline void tick_dep_set(enum tick_dep_bits bit)
220 {
221 	if (tick_nohz_full_enabled())
222 		tick_nohz_dep_set(bit);
223 }
224 
225 static inline void tick_dep_clear(enum tick_dep_bits bit)
226 {
227 	if (tick_nohz_full_enabled())
228 		tick_nohz_dep_clear(bit);
229 }
230 
231 static inline void tick_dep_set_cpu(int cpu, enum tick_dep_bits bit)
232 {
233 	if (tick_nohz_full_cpu(cpu))
234 		tick_nohz_dep_set_cpu(cpu, bit);
235 }
236 
237 static inline void tick_dep_clear_cpu(int cpu, enum tick_dep_bits bit)
238 {
239 	if (tick_nohz_full_cpu(cpu))
240 		tick_nohz_dep_clear_cpu(cpu, bit);
241 }
242 
243 static inline void tick_dep_set_task(struct task_struct *tsk,
244 				     enum tick_dep_bits bit)
245 {
246 	if (tick_nohz_full_enabled())
247 		tick_nohz_dep_set_task(tsk, bit);
248 }
249 static inline void tick_dep_clear_task(struct task_struct *tsk,
250 				       enum tick_dep_bits bit)
251 {
252 	if (tick_nohz_full_enabled())
253 		tick_nohz_dep_clear_task(tsk, bit);
254 }
255 static inline void tick_dep_set_signal(struct task_struct *tsk,
256 				       enum tick_dep_bits bit)
257 {
258 	if (tick_nohz_full_enabled())
259 		tick_nohz_dep_set_signal(tsk, bit);
260 }
261 static inline void tick_dep_clear_signal(struct signal_struct *signal,
262 					 enum tick_dep_bits bit)
263 {
264 	if (tick_nohz_full_enabled())
265 		tick_nohz_dep_clear_signal(signal, bit);
266 }
267 
268 extern void tick_nohz_full_kick_cpu(int cpu);
269 extern void __tick_nohz_task_switch(void);
270 extern void __init tick_nohz_full_setup(cpumask_var_t cpumask);
271 #else
272 static inline bool tick_nohz_full_enabled(void) { return false; }
273 static inline bool tick_nohz_full_cpu(int cpu) { return false; }
274 static inline void tick_nohz_full_add_cpus_to(struct cpumask *mask) { }
275 
276 static inline void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit) { }
277 static inline void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit) { }
278 static inline bool tick_nohz_cpu_hotpluggable(unsigned int cpu) { return true; }
279 
280 static inline void tick_dep_set(enum tick_dep_bits bit) { }
281 static inline void tick_dep_clear(enum tick_dep_bits bit) { }
282 static inline void tick_dep_set_cpu(int cpu, enum tick_dep_bits bit) { }
283 static inline void tick_dep_clear_cpu(int cpu, enum tick_dep_bits bit) { }
284 static inline void tick_dep_set_task(struct task_struct *tsk,
285 				     enum tick_dep_bits bit) { }
286 static inline void tick_dep_clear_task(struct task_struct *tsk,
287 				       enum tick_dep_bits bit) { }
288 static inline void tick_dep_set_signal(struct task_struct *tsk,
289 				       enum tick_dep_bits bit) { }
290 static inline void tick_dep_clear_signal(struct signal_struct *signal,
291 					 enum tick_dep_bits bit) { }
292 
293 static inline void tick_nohz_full_kick_cpu(int cpu) { }
294 static inline void __tick_nohz_task_switch(void) { }
295 static inline void tick_nohz_full_setup(cpumask_var_t cpumask) { }
296 #endif
297 
298 static inline void tick_nohz_task_switch(void)
299 {
300 	if (tick_nohz_full_enabled())
301 		__tick_nohz_task_switch();
302 }
303 
304 static inline void tick_nohz_user_enter_prepare(void)
305 {
306 	if (tick_nohz_full_cpu(smp_processor_id()))
307 		rcu_nocb_flush_deferred_wakeup();
308 }
309 
310 #endif
311