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