Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Tick related global functions
0004  */
0005 #ifndef _LINUX_TICK_H
0006 #define _LINUX_TICK_H
0007 
0008 #include <linux/clockchips.h>
0009 #include <linux/irqflags.h>
0010 #include <linux/percpu.h>
0011 #include <linux/context_tracking_state.h>
0012 #include <linux/cpumask.h>
0013 #include <linux/sched.h>
0014 #include <linux/rcupdate.h>
0015 
0016 #ifdef CONFIG_GENERIC_CLOCKEVENTS
0017 extern void __init tick_init(void);
0018 /* Should be core only, but ARM BL switcher requires it */
0019 extern void tick_suspend_local(void);
0020 /* Should be core only, but XEN resume magic and ARM BL switcher require it */
0021 extern void tick_resume_local(void);
0022 extern void tick_handover_do_timer(void);
0023 extern void tick_cleanup_dead_cpu(int cpu);
0024 #else /* CONFIG_GENERIC_CLOCKEVENTS */
0025 static inline void tick_init(void) { }
0026 static inline void tick_suspend_local(void) { }
0027 static inline void tick_resume_local(void) { }
0028 static inline void tick_handover_do_timer(void) { }
0029 static inline void tick_cleanup_dead_cpu(int cpu) { }
0030 #endif /* !CONFIG_GENERIC_CLOCKEVENTS */
0031 
0032 #if defined(CONFIG_GENERIC_CLOCKEVENTS) && defined(CONFIG_SUSPEND)
0033 extern void tick_freeze(void);
0034 extern void tick_unfreeze(void);
0035 #else
0036 static inline void tick_freeze(void) { }
0037 static inline void tick_unfreeze(void) { }
0038 #endif
0039 
0040 #ifdef CONFIG_TICK_ONESHOT
0041 extern void tick_irq_enter(void);
0042 #  ifndef arch_needs_cpu
0043 #   define arch_needs_cpu() (0)
0044 #  endif
0045 # else
0046 static inline void tick_irq_enter(void) { }
0047 #endif
0048 
0049 #if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
0050 extern void hotplug_cpu__broadcast_tick_pull(int dead_cpu);
0051 #else
0052 static inline void hotplug_cpu__broadcast_tick_pull(int dead_cpu) { }
0053 #endif
0054 
0055 enum tick_broadcast_mode {
0056     TICK_BROADCAST_OFF,
0057     TICK_BROADCAST_ON,
0058     TICK_BROADCAST_FORCE,
0059 };
0060 
0061 enum tick_broadcast_state {
0062     TICK_BROADCAST_EXIT,
0063     TICK_BROADCAST_ENTER,
0064 };
0065 
0066 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
0067 extern void tick_broadcast_control(enum tick_broadcast_mode mode);
0068 #else
0069 static inline void tick_broadcast_control(enum tick_broadcast_mode mode) { }
0070 #endif /* BROADCAST */
0071 
0072 #if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_HOTPLUG_CPU)
0073 extern void tick_offline_cpu(unsigned int cpu);
0074 #else
0075 static inline void tick_offline_cpu(unsigned int cpu) { }
0076 #endif
0077 
0078 #ifdef CONFIG_GENERIC_CLOCKEVENTS
0079 extern int tick_broadcast_oneshot_control(enum tick_broadcast_state state);
0080 #else
0081 static inline int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
0082 {
0083     return 0;
0084 }
0085 #endif
0086 
0087 static inline void tick_broadcast_enable(void)
0088 {
0089     tick_broadcast_control(TICK_BROADCAST_ON);
0090 }
0091 static inline void tick_broadcast_disable(void)
0092 {
0093     tick_broadcast_control(TICK_BROADCAST_OFF);
0094 }
0095 static inline void tick_broadcast_force(void)
0096 {
0097     tick_broadcast_control(TICK_BROADCAST_FORCE);
0098 }
0099 static inline int tick_broadcast_enter(void)
0100 {
0101     return tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER);
0102 }
0103 static inline void tick_broadcast_exit(void)
0104 {
0105     tick_broadcast_oneshot_control(TICK_BROADCAST_EXIT);
0106 }
0107 
0108 enum tick_dep_bits {
0109     TICK_DEP_BIT_POSIX_TIMER    = 0,
0110     TICK_DEP_BIT_PERF_EVENTS    = 1,
0111     TICK_DEP_BIT_SCHED      = 2,
0112     TICK_DEP_BIT_CLOCK_UNSTABLE = 3,
0113     TICK_DEP_BIT_RCU        = 4,
0114     TICK_DEP_BIT_RCU_EXP        = 5
0115 };
0116 #define TICK_DEP_BIT_MAX TICK_DEP_BIT_RCU_EXP
0117 
0118 #define TICK_DEP_MASK_NONE      0
0119 #define TICK_DEP_MASK_POSIX_TIMER   (1 << TICK_DEP_BIT_POSIX_TIMER)
0120 #define TICK_DEP_MASK_PERF_EVENTS   (1 << TICK_DEP_BIT_PERF_EVENTS)
0121 #define TICK_DEP_MASK_SCHED     (1 << TICK_DEP_BIT_SCHED)
0122 #define TICK_DEP_MASK_CLOCK_UNSTABLE    (1 << TICK_DEP_BIT_CLOCK_UNSTABLE)
0123 #define TICK_DEP_MASK_RCU       (1 << TICK_DEP_BIT_RCU)
0124 #define TICK_DEP_MASK_RCU_EXP       (1 << TICK_DEP_BIT_RCU_EXP)
0125 
0126 #ifdef CONFIG_NO_HZ_COMMON
0127 extern bool tick_nohz_enabled;
0128 extern bool tick_nohz_tick_stopped(void);
0129 extern bool tick_nohz_tick_stopped_cpu(int cpu);
0130 extern void tick_nohz_idle_stop_tick(void);
0131 extern void tick_nohz_idle_retain_tick(void);
0132 extern void tick_nohz_idle_restart_tick(void);
0133 extern void tick_nohz_idle_enter(void);
0134 extern void tick_nohz_idle_exit(void);
0135 extern void tick_nohz_irq_exit(void);
0136 extern bool tick_nohz_idle_got_tick(void);
0137 extern ktime_t tick_nohz_get_next_hrtimer(void);
0138 extern ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next);
0139 extern unsigned long tick_nohz_get_idle_calls(void);
0140 extern unsigned long tick_nohz_get_idle_calls_cpu(int cpu);
0141 extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
0142 extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
0143 
0144 static inline void tick_nohz_idle_stop_tick_protected(void)
0145 {
0146     local_irq_disable();
0147     tick_nohz_idle_stop_tick();
0148     local_irq_enable();
0149 }
0150 
0151 #else /* !CONFIG_NO_HZ_COMMON */
0152 #define tick_nohz_enabled (0)
0153 static inline int tick_nohz_tick_stopped(void) { return 0; }
0154 static inline int tick_nohz_tick_stopped_cpu(int cpu) { return 0; }
0155 static inline void tick_nohz_idle_stop_tick(void) { }
0156 static inline void tick_nohz_idle_retain_tick(void) { }
0157 static inline void tick_nohz_idle_restart_tick(void) { }
0158 static inline void tick_nohz_idle_enter(void) { }
0159 static inline void tick_nohz_idle_exit(void) { }
0160 static inline bool tick_nohz_idle_got_tick(void) { return false; }
0161 static inline ktime_t tick_nohz_get_next_hrtimer(void)
0162 {
0163     /* Next wake up is the tick period, assume it starts now */
0164     return ktime_add(ktime_get(), TICK_NSEC);
0165 }
0166 static inline ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
0167 {
0168     *delta_next = TICK_NSEC;
0169     return *delta_next;
0170 }
0171 static inline u64 get_cpu_idle_time_us(int cpu, u64 *unused) { return -1; }
0172 static inline u64 get_cpu_iowait_time_us(int cpu, u64 *unused) { return -1; }
0173 
0174 static inline void tick_nohz_idle_stop_tick_protected(void) { }
0175 #endif /* !CONFIG_NO_HZ_COMMON */
0176 
0177 #ifdef CONFIG_NO_HZ_FULL
0178 extern bool tick_nohz_full_running;
0179 extern cpumask_var_t tick_nohz_full_mask;
0180 
0181 static inline bool tick_nohz_full_enabled(void)
0182 {
0183     if (!context_tracking_enabled())
0184         return false;
0185 
0186     return tick_nohz_full_running;
0187 }
0188 
0189 /*
0190  * Check if a CPU is part of the nohz_full subset. Arrange for evaluating
0191  * the cpu expression (typically smp_processor_id()) _after_ the static
0192  * key.
0193  */
0194 #define tick_nohz_full_cpu(_cpu) ({                 \
0195     bool __ret = false;                     \
0196     if (tick_nohz_full_enabled())                   \
0197         __ret = cpumask_test_cpu((_cpu), tick_nohz_full_mask);  \
0198     __ret;                              \
0199 })
0200 
0201 static inline void tick_nohz_full_add_cpus_to(struct cpumask *mask)
0202 {
0203     if (tick_nohz_full_enabled())
0204         cpumask_or(mask, mask, tick_nohz_full_mask);
0205 }
0206 
0207 extern void tick_nohz_dep_set(enum tick_dep_bits bit);
0208 extern void tick_nohz_dep_clear(enum tick_dep_bits bit);
0209 extern void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit);
0210 extern void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit);
0211 extern void tick_nohz_dep_set_task(struct task_struct *tsk,
0212                    enum tick_dep_bits bit);
0213 extern void tick_nohz_dep_clear_task(struct task_struct *tsk,
0214                      enum tick_dep_bits bit);
0215 extern void tick_nohz_dep_set_signal(struct task_struct *tsk,
0216                      enum tick_dep_bits bit);
0217 extern void tick_nohz_dep_clear_signal(struct signal_struct *signal,
0218                        enum tick_dep_bits bit);
0219 
0220 /*
0221  * The below are tick_nohz_[set,clear]_dep() wrappers that optimize off-cases
0222  * on top of static keys.
0223  */
0224 static inline void tick_dep_set(enum tick_dep_bits bit)
0225 {
0226     if (tick_nohz_full_enabled())
0227         tick_nohz_dep_set(bit);
0228 }
0229 
0230 static inline void tick_dep_clear(enum tick_dep_bits bit)
0231 {
0232     if (tick_nohz_full_enabled())
0233         tick_nohz_dep_clear(bit);
0234 }
0235 
0236 static inline void tick_dep_set_cpu(int cpu, enum tick_dep_bits bit)
0237 {
0238     if (tick_nohz_full_cpu(cpu))
0239         tick_nohz_dep_set_cpu(cpu, bit);
0240 }
0241 
0242 static inline void tick_dep_clear_cpu(int cpu, enum tick_dep_bits bit)
0243 {
0244     if (tick_nohz_full_cpu(cpu))
0245         tick_nohz_dep_clear_cpu(cpu, bit);
0246 }
0247 
0248 static inline void tick_dep_set_task(struct task_struct *tsk,
0249                      enum tick_dep_bits bit)
0250 {
0251     if (tick_nohz_full_enabled())
0252         tick_nohz_dep_set_task(tsk, bit);
0253 }
0254 static inline void tick_dep_clear_task(struct task_struct *tsk,
0255                        enum tick_dep_bits bit)
0256 {
0257     if (tick_nohz_full_enabled())
0258         tick_nohz_dep_clear_task(tsk, bit);
0259 }
0260 static inline void tick_dep_set_signal(struct task_struct *tsk,
0261                        enum tick_dep_bits bit)
0262 {
0263     if (tick_nohz_full_enabled())
0264         tick_nohz_dep_set_signal(tsk, bit);
0265 }
0266 static inline void tick_dep_clear_signal(struct signal_struct *signal,
0267                      enum tick_dep_bits bit)
0268 {
0269     if (tick_nohz_full_enabled())
0270         tick_nohz_dep_clear_signal(signal, bit);
0271 }
0272 
0273 extern void tick_nohz_full_kick_cpu(int cpu);
0274 extern void __tick_nohz_task_switch(void);
0275 extern void __init tick_nohz_full_setup(cpumask_var_t cpumask);
0276 #else
0277 static inline bool tick_nohz_full_enabled(void) { return false; }
0278 static inline bool tick_nohz_full_cpu(int cpu) { return false; }
0279 static inline void tick_nohz_full_add_cpus_to(struct cpumask *mask) { }
0280 
0281 static inline void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit) { }
0282 static inline void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit) { }
0283 
0284 static inline void tick_dep_set(enum tick_dep_bits bit) { }
0285 static inline void tick_dep_clear(enum tick_dep_bits bit) { }
0286 static inline void tick_dep_set_cpu(int cpu, enum tick_dep_bits bit) { }
0287 static inline void tick_dep_clear_cpu(int cpu, enum tick_dep_bits bit) { }
0288 static inline void tick_dep_set_task(struct task_struct *tsk,
0289                      enum tick_dep_bits bit) { }
0290 static inline void tick_dep_clear_task(struct task_struct *tsk,
0291                        enum tick_dep_bits bit) { }
0292 static inline void tick_dep_set_signal(struct task_struct *tsk,
0293                        enum tick_dep_bits bit) { }
0294 static inline void tick_dep_clear_signal(struct signal_struct *signal,
0295                      enum tick_dep_bits bit) { }
0296 
0297 static inline void tick_nohz_full_kick_cpu(int cpu) { }
0298 static inline void __tick_nohz_task_switch(void) { }
0299 static inline void tick_nohz_full_setup(cpumask_var_t cpumask) { }
0300 #endif
0301 
0302 static inline void tick_nohz_task_switch(void)
0303 {
0304     if (tick_nohz_full_enabled())
0305         __tick_nohz_task_switch();
0306 }
0307 
0308 static inline void tick_nohz_user_enter_prepare(void)
0309 {
0310     if (tick_nohz_full_cpu(smp_processor_id()))
0311         rcu_nocb_flush_deferred_wakeup();
0312 }
0313 
0314 #endif