xref: /linux/kernel/time/tick-internal.h (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
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 
12 struct 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 
22 DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
23 extern ktime_t tick_next_period;
24 extern int tick_do_timer_cpu __read_mostly;
25 
26 extern void tick_setup_periodic(struct clock_event_device *dev, int broadcast);
27 extern void tick_handle_periodic(struct clock_event_device *dev);
28 extern void tick_check_new_device(struct clock_event_device *dev);
29 extern void tick_offline_cpu(unsigned int cpu);
30 extern void tick_shutdown(void);
31 extern void tick_suspend(void);
32 extern void tick_resume(void);
33 extern bool tick_check_replacement(struct clock_event_device *curdev,
34 				   struct clock_event_device *newdev);
35 extern void tick_install_replacement(struct clock_event_device *dev);
36 extern int tick_is_oneshot_available(void);
37 extern struct tick_device *tick_get_device(int cpu);
38 
39 extern int clockevents_tick_resume(struct clock_event_device *dev);
40 /* Check, if the device is functional or a dummy for broadcast */
41 static inline int tick_device_is_functional(struct clock_event_device *dev)
42 {
43 	return !(dev->features & CLOCK_EVT_FEAT_DUMMY);
44 }
45 
46 static inline enum clock_event_state clockevent_get_state(struct clock_event_device *dev)
47 {
48 	return dev->state_use_accessors;
49 }
50 
51 static 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 
57 extern void clockevents_shutdown(struct clock_event_device *dev);
58 extern void clockevents_exchange_device(struct clock_event_device *old,
59 					struct clock_event_device *new);
60 extern void clockevents_switch_state(struct clock_event_device *dev,
61 				     enum clock_event_state state);
62 extern int clockevents_program_event(struct clock_event_device *dev,
63 				     ktime_t expires, bool force);
64 extern void clockevents_handle_noop(struct clock_event_device *dev);
65 extern int __clockevents_update_freq(struct clock_event_device *dev, u32 freq);
66 
67 /* Broadcasting support */
68 # ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
69 extern int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu);
70 extern void tick_install_broadcast_device(struct clock_event_device *dev, int cpu);
71 extern int tick_is_broadcast_device(struct clock_event_device *dev);
72 extern void tick_suspend_broadcast(void);
73 extern void tick_resume_broadcast(void);
74 extern bool tick_resume_check_broadcast(void);
75 extern void tick_broadcast_init(void);
76 extern void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast);
77 extern int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq);
78 extern struct tick_device *tick_get_broadcast_device(void);
79 extern struct cpumask *tick_get_broadcast_mask(void);
80 extern const struct clock_event_device *tick_get_wakeup_device(int cpu);
81 # else /* !CONFIG_GENERIC_CLOCKEVENTS_BROADCAST: */
82 static inline void tick_install_broadcast_device(struct clock_event_device *dev, int cpu) { }
83 static inline int tick_is_broadcast_device(struct clock_event_device *dev) { return 0; }
84 static inline int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu) { return 0; }
85 static inline void tick_do_periodic_broadcast(struct clock_event_device *d) { }
86 static inline void tick_suspend_broadcast(void) { }
87 static inline void tick_resume_broadcast(void) { }
88 static inline bool tick_resume_check_broadcast(void) { return false; }
89 static inline void tick_broadcast_init(void) { }
90 static inline int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq) { return -ENODEV; }
91 
92 /* Set the periodic handler in non broadcast mode */
93 static inline void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
94 {
95 	dev->event_handler = tick_handle_periodic;
96 }
97 # endif /* !CONFIG_GENERIC_CLOCKEVENTS_BROADCAST */
98 
99 #else /* !GENERIC_CLOCKEVENTS: */
100 static inline void tick_suspend(void) { }
101 static inline void tick_resume(void) { }
102 #endif /* !GENERIC_CLOCKEVENTS */
103 
104 /* Oneshot related functions */
105 #ifdef CONFIG_TICK_ONESHOT
106 extern void tick_setup_oneshot(struct clock_event_device *newdev,
107 			       void (*handler)(struct clock_event_device *),
108 			       ktime_t nextevt);
109 extern int tick_program_event(ktime_t expires, int force);
110 extern void tick_oneshot_notify(void);
111 extern int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *));
112 extern void tick_resume_oneshot(void);
113 static inline bool tick_oneshot_possible(void) { return true; }
114 extern int tick_oneshot_mode_active(void);
115 extern void tick_clock_notify(void);
116 extern int tick_check_oneshot_change(int allow_nohz);
117 extern int tick_init_highres(void);
118 #else /* !CONFIG_TICK_ONESHOT: */
119 static inline
120 void tick_setup_oneshot(struct clock_event_device *newdev,
121 			void (*handler)(struct clock_event_device *),
122 			ktime_t nextevt) { BUG(); }
123 static inline void tick_resume_oneshot(void) { BUG(); }
124 static inline int tick_program_event(ktime_t expires, int force) { return 0; }
125 static inline void tick_oneshot_notify(void) { }
126 static inline bool tick_oneshot_possible(void) { return false; }
127 static inline int tick_oneshot_mode_active(void) { return 0; }
128 static inline void tick_clock_notify(void) { }
129 static inline int tick_check_oneshot_change(int allow_nohz) { return 0; }
130 #endif /* !CONFIG_TICK_ONESHOT */
131 
132 /* Functions related to oneshot broadcasting */
133 #if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
134 extern void tick_broadcast_switch_to_oneshot(void);
135 extern int tick_broadcast_oneshot_active(void);
136 extern void tick_check_oneshot_broadcast_this_cpu(void);
137 bool tick_broadcast_oneshot_available(void);
138 extern struct cpumask *tick_get_broadcast_oneshot_mask(void);
139 #else /* !(BROADCAST && ONESHOT): */
140 static inline void tick_broadcast_switch_to_oneshot(void) { }
141 static inline int tick_broadcast_oneshot_active(void) { return 0; }
142 static inline void tick_check_oneshot_broadcast_this_cpu(void) { }
143 static inline bool tick_broadcast_oneshot_available(void) { return tick_oneshot_possible(); }
144 #endif /* !(BROADCAST && ONESHOT) */
145 
146 #if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_HOTPLUG_CPU)
147 extern void tick_broadcast_offline(unsigned int cpu);
148 #else
149 static inline void tick_broadcast_offline(unsigned int cpu) { }
150 #endif
151 
152 /* NO_HZ_FULL internal */
153 #ifdef CONFIG_NO_HZ_FULL
154 extern void tick_nohz_init(void);
155 # else
156 static inline void tick_nohz_init(void) { }
157 #endif
158 
159 #ifdef CONFIG_NO_HZ_COMMON
160 extern void timers_update_nohz(void);
161 extern u64 get_jiffies_update(unsigned long *basej);
162 # ifdef CONFIG_SMP
163 extern struct static_key_false timers_migration_enabled;
164 extern void fetch_next_timer_interrupt_remote(unsigned long basej, u64 basem,
165 					      struct timer_events *tevt,
166 					      unsigned int cpu);
167 extern void timer_lock_remote_bases(unsigned int cpu);
168 extern void timer_unlock_remote_bases(unsigned int cpu);
169 extern bool timer_base_is_idle(void);
170 extern void timer_expire_remote(unsigned int cpu);
171 # endif
172 #else /* CONFIG_NO_HZ_COMMON */
173 static inline void timers_update_nohz(void) { }
174 #endif
175 
176 DECLARE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases);
177 
178 extern u64 get_next_timer_interrupt(unsigned long basej, u64 basem);
179 u64 timer_base_try_to_set_idle(unsigned long basej, u64 basem, bool *idle);
180 void timer_clear_idle(void);
181 
182 #define CLOCK_SET_WALL							\
183 	(BIT(HRTIMER_BASE_REALTIME) | BIT(HRTIMER_BASE_REALTIME_SOFT) |	\
184 	 BIT(HRTIMER_BASE_TAI) | BIT(HRTIMER_BASE_TAI_SOFT))
185 
186 #define CLOCK_SET_BOOT							\
187 	(BIT(HRTIMER_BASE_BOOTTIME) | BIT(HRTIMER_BASE_BOOTTIME_SOFT))
188 
189 void clock_was_set(unsigned int bases);
190 void clock_was_set_delayed(void);
191 
192 void hrtimers_resume_local(void);
193 
194 /* Since jiffies uses a simple TICK_NSEC multiplier
195  * conversion, the .shift value could be zero. However
196  * this would make NTP adjustments impossible as they are
197  * in units of 1/2^.shift. Thus we use JIFFIES_SHIFT to
198  * shift both the nominator and denominator the same
199  * amount, and give ntp adjustments in units of 1/2^8
200  *
201  * The value 8 is somewhat carefully chosen, as anything
202  * larger can result in overflows. TICK_NSEC grows as HZ
203  * shrinks, so values greater than 8 overflow 32bits when
204  * HZ=100.
205  */
206 #if HZ < 34
207 #define JIFFIES_SHIFT	6
208 #elif HZ < 67
209 #define JIFFIES_SHIFT	7
210 #else
211 #define JIFFIES_SHIFT	8
212 #endif
213 
214 extern ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt);
215