xref: /linux-6.15/include/linux/tick.h (revision ef8969bb)
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 #ifdef CONFIG_GENERIC_CLOCKEVENTS
77 extern int tick_broadcast_oneshot_control(enum tick_broadcast_state state);
78 #else
79 static inline int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
80 {
81 	return 0;
82 }
83 #endif
84 
85 static inline void tick_broadcast_enable(void)
86 {
87 	tick_broadcast_control(TICK_BROADCAST_ON);
88 }
89 static inline void tick_broadcast_disable(void)
90 {
91 	tick_broadcast_control(TICK_BROADCAST_OFF);
92 }
93 static inline void tick_broadcast_force(void)
94 {
95 	tick_broadcast_control(TICK_BROADCAST_FORCE);
96 }
97 static inline int tick_broadcast_enter(void)
98 {
99 	return tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER);
100 }
101 static inline void tick_broadcast_exit(void)
102 {
103 	tick_broadcast_oneshot_control(TICK_BROADCAST_EXIT);
104 }
105 
106 enum tick_dep_bits {
107 	TICK_DEP_BIT_POSIX_TIMER	= 0,
108 	TICK_DEP_BIT_PERF_EVENTS	= 1,
109 	TICK_DEP_BIT_SCHED		= 2,
110 	TICK_DEP_BIT_CLOCK_UNSTABLE	= 3,
111 	TICK_DEP_BIT_RCU		= 4,
112 	TICK_DEP_BIT_RCU_EXP		= 5
113 };
114 #define TICK_DEP_BIT_MAX TICK_DEP_BIT_RCU_EXP
115 
116 #define TICK_DEP_MASK_NONE		0
117 #define TICK_DEP_MASK_POSIX_TIMER	(1 << TICK_DEP_BIT_POSIX_TIMER)
118 #define TICK_DEP_MASK_PERF_EVENTS	(1 << TICK_DEP_BIT_PERF_EVENTS)
119 #define TICK_DEP_MASK_SCHED		(1 << TICK_DEP_BIT_SCHED)
120 #define TICK_DEP_MASK_CLOCK_UNSTABLE	(1 << TICK_DEP_BIT_CLOCK_UNSTABLE)
121 #define TICK_DEP_MASK_RCU		(1 << TICK_DEP_BIT_RCU)
122 #define TICK_DEP_MASK_RCU_EXP		(1 << TICK_DEP_BIT_RCU_EXP)
123 
124 #ifdef CONFIG_NO_HZ_COMMON
125 extern bool tick_nohz_enabled;
126 extern bool tick_nohz_tick_stopped(void);
127 extern bool tick_nohz_tick_stopped_cpu(int cpu);
128 extern void tick_nohz_idle_stop_tick(void);
129 extern void tick_nohz_idle_retain_tick(void);
130 extern void tick_nohz_idle_restart_tick(void);
131 extern void tick_nohz_idle_enter(void);
132 extern void tick_nohz_idle_exit(void);
133 extern void tick_nohz_irq_exit(void);
134 extern bool tick_nohz_idle_got_tick(void);
135 extern ktime_t tick_nohz_get_next_hrtimer(void);
136 extern ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next);
137 extern unsigned long tick_nohz_get_idle_calls(void);
138 extern unsigned long tick_nohz_get_idle_calls_cpu(int cpu);
139 extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
140 extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
141 #else /* !CONFIG_NO_HZ_COMMON */
142 #define tick_nohz_enabled (0)
143 static inline int tick_nohz_tick_stopped(void) { return 0; }
144 static inline int tick_nohz_tick_stopped_cpu(int cpu) { return 0; }
145 static inline void tick_nohz_idle_stop_tick(void) { }
146 static inline void tick_nohz_idle_retain_tick(void) { }
147 static inline void tick_nohz_idle_restart_tick(void) { }
148 static inline void tick_nohz_idle_enter(void) { }
149 static inline void tick_nohz_idle_exit(void) { }
150 static inline bool tick_nohz_idle_got_tick(void) { return false; }
151 static inline ktime_t tick_nohz_get_next_hrtimer(void)
152 {
153 	/* Next wake up is the tick period, assume it starts now */
154 	return ktime_add(ktime_get(), TICK_NSEC);
155 }
156 static inline ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
157 {
158 	*delta_next = TICK_NSEC;
159 	return *delta_next;
160 }
161 static inline u64 get_cpu_idle_time_us(int cpu, u64 *unused) { return -1; }
162 static inline u64 get_cpu_iowait_time_us(int cpu, u64 *unused) { return -1; }
163 #endif /* !CONFIG_NO_HZ_COMMON */
164 
165 #ifdef CONFIG_NO_HZ_FULL
166 extern bool tick_nohz_full_running;
167 extern cpumask_var_t tick_nohz_full_mask;
168 
169 static inline bool tick_nohz_full_enabled(void)
170 {
171 	if (!context_tracking_enabled())
172 		return false;
173 
174 	return tick_nohz_full_running;
175 }
176 
177 /*
178  * Check if a CPU is part of the nohz_full subset. Arrange for evaluating
179  * the cpu expression (typically smp_processor_id()) _after_ the static
180  * key.
181  */
182 #define tick_nohz_full_cpu(_cpu) ({					\
183 	bool __ret = false;						\
184 	if (tick_nohz_full_enabled())					\
185 		__ret = cpumask_test_cpu((_cpu), tick_nohz_full_mask);	\
186 	__ret;								\
187 })
188 
189 static inline void tick_nohz_full_add_cpus_to(struct cpumask *mask)
190 {
191 	if (tick_nohz_full_enabled())
192 		cpumask_or(mask, mask, tick_nohz_full_mask);
193 }
194 
195 extern void tick_nohz_dep_set(enum tick_dep_bits bit);
196 extern void tick_nohz_dep_clear(enum tick_dep_bits bit);
197 extern void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit);
198 extern void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit);
199 extern void tick_nohz_dep_set_task(struct task_struct *tsk,
200 				   enum tick_dep_bits bit);
201 extern void tick_nohz_dep_clear_task(struct task_struct *tsk,
202 				     enum tick_dep_bits bit);
203 extern void tick_nohz_dep_set_signal(struct task_struct *tsk,
204 				     enum tick_dep_bits bit);
205 extern void tick_nohz_dep_clear_signal(struct signal_struct *signal,
206 				       enum tick_dep_bits bit);
207 extern bool tick_nohz_cpu_hotpluggable(unsigned int cpu);
208 
209 /*
210  * The below are tick_nohz_[set,clear]_dep() wrappers that optimize off-cases
211  * on top of static keys.
212  */
213 static inline void tick_dep_set(enum tick_dep_bits bit)
214 {
215 	if (tick_nohz_full_enabled())
216 		tick_nohz_dep_set(bit);
217 }
218 
219 static inline void tick_dep_clear(enum tick_dep_bits bit)
220 {
221 	if (tick_nohz_full_enabled())
222 		tick_nohz_dep_clear(bit);
223 }
224 
225 static inline void tick_dep_set_cpu(int cpu, enum tick_dep_bits bit)
226 {
227 	if (tick_nohz_full_cpu(cpu))
228 		tick_nohz_dep_set_cpu(cpu, bit);
229 }
230 
231 static inline void tick_dep_clear_cpu(int cpu, enum tick_dep_bits bit)
232 {
233 	if (tick_nohz_full_cpu(cpu))
234 		tick_nohz_dep_clear_cpu(cpu, bit);
235 }
236 
237 static inline void tick_dep_set_task(struct task_struct *tsk,
238 				     enum tick_dep_bits bit)
239 {
240 	if (tick_nohz_full_enabled())
241 		tick_nohz_dep_set_task(tsk, bit);
242 }
243 static inline void tick_dep_clear_task(struct task_struct *tsk,
244 				       enum tick_dep_bits bit)
245 {
246 	if (tick_nohz_full_enabled())
247 		tick_nohz_dep_clear_task(tsk, bit);
248 }
249 static inline void tick_dep_set_signal(struct task_struct *tsk,
250 				       enum tick_dep_bits bit)
251 {
252 	if (tick_nohz_full_enabled())
253 		tick_nohz_dep_set_signal(tsk, bit);
254 }
255 static inline void tick_dep_clear_signal(struct signal_struct *signal,
256 					 enum tick_dep_bits bit)
257 {
258 	if (tick_nohz_full_enabled())
259 		tick_nohz_dep_clear_signal(signal, bit);
260 }
261 
262 extern void tick_nohz_full_kick_cpu(int cpu);
263 extern void __tick_nohz_task_switch(void);
264 extern void __init tick_nohz_full_setup(cpumask_var_t cpumask);
265 #else
266 static inline bool tick_nohz_full_enabled(void) { return false; }
267 static inline bool tick_nohz_full_cpu(int cpu) { return false; }
268 static inline void tick_nohz_full_add_cpus_to(struct cpumask *mask) { }
269 
270 static inline void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit) { }
271 static inline void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit) { }
272 static inline bool tick_nohz_cpu_hotpluggable(unsigned int cpu) { return true; }
273 
274 static inline void tick_dep_set(enum tick_dep_bits bit) { }
275 static inline void tick_dep_clear(enum tick_dep_bits bit) { }
276 static inline void tick_dep_set_cpu(int cpu, enum tick_dep_bits bit) { }
277 static inline void tick_dep_clear_cpu(int cpu, enum tick_dep_bits bit) { }
278 static inline void tick_dep_set_task(struct task_struct *tsk,
279 				     enum tick_dep_bits bit) { }
280 static inline void tick_dep_clear_task(struct task_struct *tsk,
281 				       enum tick_dep_bits bit) { }
282 static inline void tick_dep_set_signal(struct task_struct *tsk,
283 				       enum tick_dep_bits bit) { }
284 static inline void tick_dep_clear_signal(struct signal_struct *signal,
285 					 enum tick_dep_bits bit) { }
286 
287 static inline void tick_nohz_full_kick_cpu(int cpu) { }
288 static inline void __tick_nohz_task_switch(void) { }
289 static inline void tick_nohz_full_setup(cpumask_var_t cpumask) { }
290 #endif
291 
292 static inline void tick_nohz_task_switch(void)
293 {
294 	if (tick_nohz_full_enabled())
295 		__tick_nohz_task_switch();
296 }
297 
298 static inline void tick_nohz_user_enter_prepare(void)
299 {
300 	if (tick_nohz_full_cpu(smp_processor_id()))
301 		rcu_nocb_flush_deferred_wakeup();
302 }
303 
304 #endif
305