projects/linux·blob master · refs

216 lines · 8.5 KiB

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
12struct 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
22DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
23extern ktime_t tick_next_period;
24extern int tick_do_timer_cpu __read_mostly;
25
26extern void tick_setup_periodic(struct clock_event_device *dev, int broadcast);
27extern void tick_handle_periodic(struct clock_event_device *dev);
28extern void tick_check_new_device(struct clock_event_device *dev);
29extern void tick_offline_cpu(unsigned int cpu);
30extern void tick_shutdown(void);
31extern void tick_suspend(void);
32extern void tick_resume(void);
33extern bool tick_check_replacement(struct clock_event_device *curdev,
34 struct clock_event_device *newdev);
35extern void tick_install_replacement(struct clock_event_device *dev);
36extern int tick_is_oneshot_available(void);
37extern struct tick_device *tick_get_device(int cpu);
38
39extern int clockevents_tick_resume(struct clock_event_device *dev);
40/* Check, if the device is functional or a dummy for broadcast */
41static inline int tick_device_is_functional(struct clock_event_device *dev)
42{
43 return !(dev->features & CLOCK_EVT_FEAT_DUMMY);
44}
45
46static inline enum clock_event_state clockevent_get_state(struct clock_event_device *dev)
47{
48 return dev->state_use_accessors;
49}
50
51static 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
57extern void clockevents_shutdown(struct clock_event_device *dev);
58extern void __clockevents_exchange_device(struct clock_event_device *old,
59 struct clock_event_device *new);
60extern void clockevents_exchange_device(struct clock_event_device *old,
61 struct clock_event_device *new);
62extern void clockevents_switch_state(struct clock_event_device *dev,
63 enum clock_event_state state);
64extern int clockevents_program_event(struct clock_event_device *dev,
65 ktime_t expires, bool force);
66extern void clockevents_handle_noop(struct clock_event_device *dev);
67extern int __clockevents_update_freq(struct clock_event_device *dev, u32 freq);
68
69/* Broadcasting support */
70# ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
71extern int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu);
72extern void tick_install_broadcast_device(struct clock_event_device *dev, int cpu);
73extern int tick_is_broadcast_device(struct clock_event_device *dev);
74extern void tick_suspend_broadcast(void);
75extern void tick_resume_broadcast(void);
76extern bool tick_resume_check_broadcast(void);
77extern void tick_broadcast_init(void);
78extern void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast);
79extern int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq);
80extern struct tick_device *tick_get_broadcast_device(void);
81extern struct cpumask *tick_get_broadcast_mask(void);
82extern const struct clock_event_device *tick_get_wakeup_device(int cpu);
83# else /* !CONFIG_GENERIC_CLOCKEVENTS_BROADCAST: */
84static inline void tick_install_broadcast_device(struct clock_event_device *dev, int cpu) { }
85static inline int tick_is_broadcast_device(struct clock_event_device *dev) { return 0; }
86static inline int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu) { return 0; }
87static inline void tick_do_periodic_broadcast(struct clock_event_device *d) { }
88static inline void tick_suspend_broadcast(void) { }
89static inline void tick_resume_broadcast(void) { }
90static inline bool tick_resume_check_broadcast(void) { return false; }
91static inline void tick_broadcast_init(void) { }
92static 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 */
95static 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: */
102static inline void tick_suspend(void) { }
103static inline void tick_resume(void) { }
104#endif /* !GENERIC_CLOCKEVENTS */
105
106/* Oneshot related functions */
107#ifdef CONFIG_TICK_ONESHOT
108extern void tick_setup_oneshot(struct clock_event_device *newdev,
109 void (*handler)(struct clock_event_device *),
110 ktime_t nextevt);
111extern int tick_program_event(ktime_t expires, int force);
112extern void tick_oneshot_notify(void);
113extern int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *));
114extern void tick_resume_oneshot(void);
115static inline bool tick_oneshot_possible(void) { return true; }
116extern int tick_oneshot_mode_active(void);
117extern void tick_clock_notify(void);
118extern int tick_check_oneshot_change(int allow_nohz);
119extern int tick_init_highres(void);
120#else /* !CONFIG_TICK_ONESHOT: */
121static inline
122void tick_setup_oneshot(struct clock_event_device *newdev,
123 void (*handler)(struct clock_event_device *),
124 ktime_t nextevt) { BUG(); }
125static inline void tick_resume_oneshot(void) { BUG(); }
126static inline int tick_program_event(ktime_t expires, int force) { return 0; }
127static inline void tick_oneshot_notify(void) { }
128static inline bool tick_oneshot_possible(void) { return false; }
129static inline int tick_oneshot_mode_active(void) { return 0; }
130static inline void tick_clock_notify(void) { }
131static 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)
136extern void tick_broadcast_switch_to_oneshot(void);
137extern int tick_broadcast_oneshot_active(void);
138extern void tick_check_oneshot_broadcast_this_cpu(void);
139bool tick_broadcast_oneshot_available(void);
140extern struct cpumask *tick_get_broadcast_oneshot_mask(void);
141#else /* !(BROADCAST && ONESHOT): */
142static inline void tick_broadcast_switch_to_oneshot(void) { }
143static inline int tick_broadcast_oneshot_active(void) { return 0; }
144static inline void tick_check_oneshot_broadcast_this_cpu(void) { }
145static 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)
149extern void tick_broadcast_offline(unsigned int cpu);
150#else
151static inline void tick_broadcast_offline(unsigned int cpu) { }
152#endif
153
154/* NO_HZ_FULL internal */
155#ifdef CONFIG_NO_HZ_FULL
156extern void tick_nohz_init(void);
157# else
158static inline void tick_nohz_init(void) { }
159#endif
160
161#ifdef CONFIG_NO_HZ_COMMON
162extern void timers_update_nohz(void);
163extern u64 get_jiffies_update(unsigned long *basej);
164# ifdef CONFIG_SMP
165extern struct static_key_false timers_migration_enabled;
166extern void fetch_next_timer_interrupt_remote(unsigned long basej, u64 basem,
167 struct timer_events *tevt,
168 unsigned int cpu);
169extern void timer_lock_remote_bases(unsigned int cpu);
170extern void timer_unlock_remote_bases(unsigned int cpu);
171extern bool timer_base_is_idle(void);
172extern void timer_expire_remote(unsigned int cpu);
173# endif
174#else /* CONFIG_NO_HZ_COMMON */
175static inline void timers_update_nohz(void) { }
176#endif
177
178DECLARE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases);
179
180extern u64 get_next_timer_interrupt(unsigned long basej, u64 basem);
181u64 timer_base_try_to_set_idle(unsigned long basej, u64 basem, bool *idle);
182void 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
191void clock_was_set(unsigned int bases);
192void clock_was_set_delayed(void);
193
194void 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
216extern ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt);