blob: 9459fef5b85736d0c895a5643ae0d87feabbb41d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +01002/*
3 * Tick related global functions
Thomas Gleixner906568c2007-02-16 01:28:01 -08004 */
5#ifndef _LINUX_TICK_H
6#define _LINUX_TICK_H
7
8#include <linux/clockchips.h>
Frederic Weisbecker2bbb6812011-10-08 16:01:00 +02009#include <linux/irqflags.h>
Frederic Weisbecker33a5f622012-10-11 17:52:56 +020010#include <linux/percpu.h>
Frederic Weisbecker460775df2013-07-24 23:52:27 +020011#include <linux/context_tracking_state.h>
12#include <linux/cpumask.h>
Paul E. McKenneyc0f489d2014-06-04 13:46:03 -070013#include <linux/sched.h>
Frederic Weisbeckerf268c372021-05-27 13:34:41 +020014#include <linux/rcupdate.h>
Thomas Gleixner906568c2007-02-16 01:28:01 -080015
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +010016#ifdef CONFIG_GENERIC_CLOCKEVENTS
17extern void __init tick_init(void);
Thomas Gleixner7270d112015-03-25 13:11:52 +010018/* Should be core only, but ARM BL switcher requires it */
19extern void tick_suspend_local(void);
20/* Should be core only, but XEN resume magic and ARM BL switcher require it */
Thomas Gleixnerf46481d2015-03-25 13:11:04 +010021extern void tick_resume_local(void);
Thomas Gleixner52c063d2015-04-03 02:37:24 +020022extern void tick_handover_do_timer(void);
Thomas Gleixnera49b1162015-04-03 02:38:05 +020023extern void tick_cleanup_dead_cpu(int cpu);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080024#else /* CONFIG_GENERIC_CLOCKEVENTS */
Thomas Gleixner906568c2007-02-16 01:28:01 -080025static inline void tick_init(void) { }
Thomas Gleixner7270d112015-03-25 13:11:52 +010026static inline void tick_suspend_local(void) { }
Thomas Gleixnerf46481d2015-03-25 13:11:04 +010027static inline void tick_resume_local(void) { }
Thomas Gleixner52c063d2015-04-03 02:37:24 +020028static inline void tick_handover_do_timer(void) { }
Thomas Gleixnera49b1162015-04-03 02:38:05 +020029static inline void tick_cleanup_dead_cpu(int cpu) { }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080030#endif /* !CONFIG_GENERIC_CLOCKEVENTS */
Thomas Gleixner906568c2007-02-16 01:28:01 -080031
Rafael J. Wysocki87e9b9f2015-05-16 01:38:15 +020032#if defined(CONFIG_GENERIC_CLOCKEVENTS) && defined(CONFIG_SUSPEND)
33extern void tick_freeze(void);
34extern void tick_unfreeze(void);
35#else
36static inline void tick_freeze(void) { }
37static inline void tick_unfreeze(void) { }
38#endif
39
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +010040#ifdef CONFIG_TICK_ONESHOT
41extern void tick_irq_enter(void);
42# ifndef arch_needs_cpu
43# define arch_needs_cpu() (0)
44# endif
45# else
46static inline void tick_irq_enter(void) { }
47#endif
Frederic Weisbecker33a5f622012-10-11 17:52:56 +020048
Preeti U Murthy345527b2015-03-30 14:59:19 +053049#if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
50extern void hotplug_cpu__broadcast_tick_pull(int dead_cpu);
51#else
52static inline void hotplug_cpu__broadcast_tick_pull(int dead_cpu) { }
53#endif
54
Thomas Gleixner592a4382015-04-03 02:01:10 +020055enum tick_broadcast_mode {
56 TICK_BROADCAST_OFF,
57 TICK_BROADCAST_ON,
58 TICK_BROADCAST_FORCE,
59};
60
Thomas Gleixner1fe5d5c32015-04-03 02:05:15 +020061enum tick_broadcast_state {
62 TICK_BROADCAST_EXIT,
63 TICK_BROADCAST_ENTER,
64};
65
Thomas Gleixner592a4382015-04-03 02:01:10 +020066#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
67extern void tick_broadcast_control(enum tick_broadcast_mode mode);
68#else
69static inline void tick_broadcast_control(enum tick_broadcast_mode mode) { }
70#endif /* BROADCAST */
71
Thomas Gleixner1b72d4322019-03-21 16:39:20 +010072#if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_HOTPLUG_CPU)
73extern void tick_offline_cpu(unsigned int cpu);
74#else
75static inline void tick_offline_cpu(unsigned int cpu) { }
76#endif
77
Thomas Gleixner37b64a42015-07-07 21:56:34 +020078#ifdef CONFIG_GENERIC_CLOCKEVENTS
Thomas Gleixner1fe5d5c32015-04-03 02:05:15 +020079extern int tick_broadcast_oneshot_control(enum tick_broadcast_state state);
Thomas Gleixner37b64a42015-07-07 21:56:34 +020080#else
81static inline int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
82{
83 return 0;
84}
85#endif
Thomas Gleixner1fe5d5c32015-04-03 02:05:15 +020086
Thomas Gleixner592a4382015-04-03 02:01:10 +020087static inline void tick_broadcast_enable(void)
88{
89 tick_broadcast_control(TICK_BROADCAST_ON);
90}
91static inline void tick_broadcast_disable(void)
92{
93 tick_broadcast_control(TICK_BROADCAST_OFF);
94}
95static inline void tick_broadcast_force(void)
96{
97 tick_broadcast_control(TICK_BROADCAST_FORCE);
98}
Thomas Gleixner1fe5d5c32015-04-03 02:05:15 +020099static inline int tick_broadcast_enter(void)
100{
101 return tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER);
102}
103static inline void tick_broadcast_exit(void)
104{
105 tick_broadcast_oneshot_control(TICK_BROADCAST_EXIT);
106}
Thomas Gleixner592a4382015-04-03 02:01:10 +0200107
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200108enum tick_dep_bits {
109 TICK_DEP_BIT_POSIX_TIMER = 0,
110 TICK_DEP_BIT_PERF_EVENTS = 1,
111 TICK_DEP_BIT_SCHED = 2,
Frederic Weisbecker01b4c392019-07-24 15:22:59 +0200112 TICK_DEP_BIT_CLOCK_UNSTABLE = 3,
Paul E. McKenneydf1e8492019-11-27 16:36:45 -0800113 TICK_DEP_BIT_RCU = 4,
114 TICK_DEP_BIT_RCU_EXP = 5
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200115};
Paul E. McKenneydf1e8492019-11-27 16:36:45 -0800116#define TICK_DEP_BIT_MAX TICK_DEP_BIT_RCU_EXP
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200117
Frederic Weisbeckere6e6cc22015-12-11 03:27:25 +0100118#define TICK_DEP_MASK_NONE 0
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200119#define TICK_DEP_MASK_POSIX_TIMER (1 << TICK_DEP_BIT_POSIX_TIMER)
120#define TICK_DEP_MASK_PERF_EVENTS (1 << TICK_DEP_BIT_PERF_EVENTS)
121#define TICK_DEP_MASK_SCHED (1 << TICK_DEP_BIT_SCHED)
122#define TICK_DEP_MASK_CLOCK_UNSTABLE (1 << TICK_DEP_BIT_CLOCK_UNSTABLE)
Frederic Weisbecker01b4c392019-07-24 15:22:59 +0200123#define TICK_DEP_MASK_RCU (1 << TICK_DEP_BIT_RCU)
Paul E. McKenneydf1e8492019-11-27 16:36:45 -0800124#define TICK_DEP_MASK_RCU_EXP (1 << TICK_DEP_BIT_RCU_EXP)
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200125
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +0100126#ifdef CONFIG_NO_HZ_COMMON
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700127extern bool tick_nohz_enabled;
Frederic Weisbeckera3642982018-02-21 05:17:24 +0100128extern bool tick_nohz_tick_stopped(void);
Frederic Weisbecker22ab8bc2018-02-21 05:17:25 +0100129extern bool tick_nohz_tick_stopped_cpu(int cpu);
Rafael J. Wysocki0e776762018-04-05 18:58:27 +0200130extern void tick_nohz_idle_stop_tick(void);
Rafael J. Wysocki554c8aa2018-04-03 23:17:11 +0200131extern void tick_nohz_idle_retain_tick(void);
Rafael J. Wysocki2aaf7092018-03-15 23:05:50 +0100132extern void tick_nohz_idle_restart_tick(void);
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +0100133extern void tick_nohz_idle_enter(void);
Frederic Weisbecker280f0672011-10-07 18:22:06 +0200134extern void tick_nohz_idle_exit(void);
135extern void tick_nohz_irq_exit(void);
Rafael J. Wysocki45f1ff52018-03-22 17:50:49 +0100136extern bool tick_nohz_idle_got_tick(void);
Ulf Hansson6f9b83a2019-03-27 15:35:47 +0100137extern ktime_t tick_nohz_get_next_hrtimer(void);
Rafael J. Wysocki296bb1e2018-04-05 19:12:34 +0200138extern ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next);
Rafael J. Wysockib7eaf1a2017-03-22 00:08:50 +0100139extern unsigned long tick_nohz_get_idle_calls(void);
Joel Fernandes466a2b42017-12-21 02:22:45 +0100140extern unsigned long tick_nohz_get_idle_calls_cpu(int cpu);
Venki Pallipadi6378ddb52008-01-30 13:30:04 +0100141extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700142extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
Rafael J. Wysocki0e776762018-04-05 18:58:27 +0200143
144static inline void tick_nohz_idle_stop_tick_protected(void)
145{
146 local_irq_disable();
147 tick_nohz_idle_stop_tick();
148 local_irq_enable();
149}
150
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +0100151#else /* !CONFIG_NO_HZ_COMMON */
Jean Delvare46373a12016-01-11 17:40:31 +0100152#define tick_nohz_enabled (0)
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +0100153static inline int tick_nohz_tick_stopped(void) { return 0; }
Frederic Weisbecker22ab8bc2018-02-21 05:17:25 +0100154static inline int tick_nohz_tick_stopped_cpu(int cpu) { return 0; }
Rafael J. Wysocki0e776762018-04-05 18:58:27 +0200155static inline void tick_nohz_idle_stop_tick(void) { }
Rafael J. Wysocki554c8aa2018-04-03 23:17:11 +0200156static inline void tick_nohz_idle_retain_tick(void) { }
Rafael J. Wysocki2aaf7092018-03-15 23:05:50 +0100157static inline void tick_nohz_idle_restart_tick(void) { }
Frederic Weisbecker2bbb6812011-10-08 16:01:00 +0200158static inline void tick_nohz_idle_enter(void) { }
159static inline void tick_nohz_idle_exit(void) { }
Rafael J. Wysocki45f1ff52018-03-22 17:50:49 +0100160static inline bool tick_nohz_idle_got_tick(void) { return false; }
Ulf Hansson6f9b83a2019-03-27 15:35:47 +0100161static inline ktime_t tick_nohz_get_next_hrtimer(void)
162{
163 /* Next wake up is the tick period, assume it starts now */
164 return ktime_add(ktime_get(), TICK_NSEC);
165}
Rafael J. Wysocki296bb1e2018-04-05 19:12:34 +0200166static inline ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
Len Brown4f86d3a2007-10-03 18:58:00 -0400167{
Rafael J. Wysocki296bb1e2018-04-05 19:12:34 +0200168 *delta_next = TICK_NSEC;
169 return *delta_next;
Len Brown4f86d3a2007-10-03 18:58:00 -0400170}
[email protected]8083e4a2008-08-04 11:59:11 -0700171static inline u64 get_cpu_idle_time_us(int cpu, u64 *unused) { return -1; }
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700172static inline u64 get_cpu_iowait_time_us(int cpu, u64 *unused) { return -1; }
Rafael J. Wysocki0e776762018-04-05 18:58:27 +0200173
174static inline void tick_nohz_idle_stop_tick_protected(void) { }
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +0100175#endif /* !CONFIG_NO_HZ_COMMON */
Thomas Gleixner906568c2007-02-16 01:28:01 -0800176
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200177#ifdef CONFIG_NO_HZ_FULL
Frederic Weisbecker460775df2013-07-24 23:52:27 +0200178extern bool tick_nohz_full_running;
179extern cpumask_var_t tick_nohz_full_mask;
180
181static inline bool tick_nohz_full_enabled(void)
182{
Frederic Weisbecker74c57872019-10-16 04:56:51 +0200183 if (!context_tracking_enabled())
Frederic Weisbecker460775df2013-07-24 23:52:27 +0200184 return false;
185
186 return tick_nohz_full_running;
187}
188
Peter Zijlstraf105dfe2021-05-13 01:29:15 +0200189/*
190 * Check if a CPU is part of the nohz_full subset. Arrange for evaluating
191 * the cpu expression (typically smp_processor_id()) _after_ the static
192 * key.
193 */
194#define tick_nohz_full_cpu(_cpu) ({ \
195 bool __ret = false; \
196 if (tick_nohz_full_enabled()) \
197 __ret = cpumask_test_cpu((_cpu), tick_nohz_full_mask); \
198 __ret; \
199})
Frederic Weisbecker460775df2013-07-24 23:52:27 +0200200
Chris Metcalf83dedea2015-05-06 18:04:25 +0200201static inline void tick_nohz_full_add_cpus_to(struct cpumask *mask)
202{
203 if (tick_nohz_full_enabled())
204 cpumask_or(mask, mask, tick_nohz_full_mask);
205}
206
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200207extern void tick_nohz_dep_set(enum tick_dep_bits bit);
208extern void tick_nohz_dep_clear(enum tick_dep_bits bit);
209extern void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit);
210extern void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit);
211extern void tick_nohz_dep_set_task(struct task_struct *tsk,
212 enum tick_dep_bits bit);
213extern void tick_nohz_dep_clear_task(struct task_struct *tsk,
214 enum tick_dep_bits bit);
Marcelo Tosatti1e4ca262021-05-13 01:29:21 +0200215extern void tick_nohz_dep_set_signal(struct task_struct *tsk,
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200216 enum tick_dep_bits bit);
217extern void tick_nohz_dep_clear_signal(struct signal_struct *signal,
218 enum tick_dep_bits bit);
Joel Fernandes (Google)3e7b8a72023-01-24 17:31:26 +0000219extern bool tick_nohz_cpu_hotpluggable(unsigned int cpu);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200220
221/*
222 * The below are tick_nohz_[set,clear]_dep() wrappers that optimize off-cases
223 * on top of static keys.
224 */
225static inline void tick_dep_set(enum tick_dep_bits bit)
226{
227 if (tick_nohz_full_enabled())
228 tick_nohz_dep_set(bit);
229}
230
231static inline void tick_dep_clear(enum tick_dep_bits bit)
232{
233 if (tick_nohz_full_enabled())
234 tick_nohz_dep_clear(bit);
235}
236
237static inline void tick_dep_set_cpu(int cpu, enum tick_dep_bits bit)
238{
239 if (tick_nohz_full_cpu(cpu))
240 tick_nohz_dep_set_cpu(cpu, bit);
241}
242
243static inline void tick_dep_clear_cpu(int cpu, enum tick_dep_bits bit)
244{
245 if (tick_nohz_full_cpu(cpu))
246 tick_nohz_dep_clear_cpu(cpu, bit);
247}
248
249static inline void tick_dep_set_task(struct task_struct *tsk,
250 enum tick_dep_bits bit)
251{
252 if (tick_nohz_full_enabled())
253 tick_nohz_dep_set_task(tsk, bit);
254}
255static inline void tick_dep_clear_task(struct task_struct *tsk,
256 enum tick_dep_bits bit)
257{
258 if (tick_nohz_full_enabled())
259 tick_nohz_dep_clear_task(tsk, bit);
260}
Marcelo Tosatti1e4ca262021-05-13 01:29:21 +0200261static inline void tick_dep_set_signal(struct task_struct *tsk,
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200262 enum tick_dep_bits bit)
263{
264 if (tick_nohz_full_enabled())
Marcelo Tosatti1e4ca262021-05-13 01:29:21 +0200265 tick_nohz_dep_set_signal(tsk, bit);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200266}
267static inline void tick_dep_clear_signal(struct signal_struct *signal,
268 enum tick_dep_bits bit)
269{
270 if (tick_nohz_full_enabled())
271 tick_nohz_dep_clear_signal(signal, bit);
272}
273
Frederic Weisbecker3d36aeb2014-06-04 16:17:33 +0200274extern void tick_nohz_full_kick_cpu(int cpu);
Frederic Weisbeckerde734f82015-06-11 18:07:12 +0200275extern void __tick_nohz_task_switch(void);
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200276extern void __init tick_nohz_full_setup(cpumask_var_t cpumask);
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100277#else
Frederic Weisbecker460775df2013-07-24 23:52:27 +0200278static inline bool tick_nohz_full_enabled(void) { return false; }
279static inline bool tick_nohz_full_cpu(int cpu) { return false; }
Chris Metcalf83dedea2015-05-06 18:04:25 +0200280static inline void tick_nohz_full_add_cpus_to(struct cpumask *mask) { }
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200281
Frederic Weisbecker01b4c392019-07-24 15:22:59 +0200282static inline void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit) { }
283static inline void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit) { }
Joel Fernandes (Google)3e7b8a72023-01-24 17:31:26 +0000284static inline bool tick_nohz_cpu_hotpluggable(unsigned int cpu) { return true; }
Frederic Weisbecker01b4c392019-07-24 15:22:59 +0200285
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200286static inline void tick_dep_set(enum tick_dep_bits bit) { }
287static inline void tick_dep_clear(enum tick_dep_bits bit) { }
288static inline void tick_dep_set_cpu(int cpu, enum tick_dep_bits bit) { }
289static inline void tick_dep_clear_cpu(int cpu, enum tick_dep_bits bit) { }
290static inline void tick_dep_set_task(struct task_struct *tsk,
291 enum tick_dep_bits bit) { }
292static inline void tick_dep_clear_task(struct task_struct *tsk,
293 enum tick_dep_bits bit) { }
Marcelo Tosatti1e4ca262021-05-13 01:29:21 +0200294static inline void tick_dep_set_signal(struct task_struct *tsk,
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200295 enum tick_dep_bits bit) { }
296static inline void tick_dep_clear_signal(struct signal_struct *signal,
297 enum tick_dep_bits bit) { }
298
Frederic Weisbecker3d36aeb2014-06-04 16:17:33 +0200299static inline void tick_nohz_full_kick_cpu(int cpu) { }
Frederic Weisbeckerde734f82015-06-11 18:07:12 +0200300static inline void __tick_nohz_task_switch(void) { }
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200301static inline void tick_nohz_full_setup(cpumask_var_t cpumask) { }
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100302#endif
303
Frederic Weisbeckerde734f82015-06-11 18:07:12 +0200304static inline void tick_nohz_task_switch(void)
Frederic Weisbeckerd13508f2013-07-24 23:52:27 +0200305{
306 if (tick_nohz_full_enabled())
Frederic Weisbeckerde734f82015-06-11 18:07:12 +0200307 __tick_nohz_task_switch();
Frederic Weisbeckerd13508f2013-07-24 23:52:27 +0200308}
309
Frederic Weisbeckerf268c372021-05-27 13:34:41 +0200310static inline void tick_nohz_user_enter_prepare(void)
311{
312 if (tick_nohz_full_cpu(smp_processor_id()))
313 rcu_nocb_flush_deferred_wakeup();
314}
315
Thomas Gleixner906568c2007-02-16 01:28:01 -0800316#endif