blob: f7fe6fe361731642677d7958a85b6295fb819c96 [file] [log] [blame]
Thomas Gleixner35728b82018-10-31 19:21:09 +01001// SPDX-License-Identifier: GPL-2.0
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -08002/*
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -08003 * This file contains functions which emulate a local clock-event
4 * device via a broadcast event source.
5 *
6 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
7 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
8 * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -08009 */
10#include <linux/cpu.h>
11#include <linux/err.h>
12#include <linux/hrtimer.h>
Russell Kingd7b90682008-04-17 07:46:24 +020013#include <linux/interrupt.h>
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080014#include <linux/percpu.h>
15#include <linux/profile.h>
16#include <linux/sched.h>
Mark Rutland12ad1002013-01-14 17:05:22 +000017#include <linux/smp.h>
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000018#include <linux/module.h>
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080019
20#include "tick-internal.h"
21
22/*
23 * Broadcast support for broken x86 hardware, where the local apic
24 * timer stops in C3 state.
25 */
26
Dmitri Vorobieva52f5c52009-05-01 13:10:21 -070027static struct tick_device tick_broadcast_device;
Waiman Long668802c2017-01-30 12:57:43 -050028static cpumask_var_t tick_broadcast_mask __cpumask_var_read_mostly;
29static cpumask_var_t tick_broadcast_on __cpumask_var_read_mostly;
30static cpumask_var_t tmpmask __cpumask_var_read_mostly;
Thomas Gleixner592a4382015-04-03 02:01:10 +020031static int tick_broadcast_forced;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080032
Waiman Long668802c2017-01-30 12:57:43 -050033static __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
34
Thomas Gleixner5590a532007-07-21 04:37:35 -070035#ifdef CONFIG_TICK_ONESHOT
Will Deaconc94a8532021-05-24 23:18:16 +010036static DEFINE_PER_CPU(struct clock_event_device *, tick_oneshot_wakeup_device);
37
Stephen Boyd94114c32017-06-07 23:36:03 -070038static void tick_broadcast_setup_oneshot(struct clock_event_device *bc);
Thomas Gleixner5590a532007-07-21 04:37:35 -070039static void tick_broadcast_clear_oneshot(int cpu);
Thomas Gleixner080873c2015-03-25 13:09:55 +010040static void tick_resume_broadcast_oneshot(struct clock_event_device *bc);
Borislav Petkovaba095432019-03-29 11:28:52 +010041# ifdef CONFIG_HOTPLUG_CPU
Thomas Gleixner1b72d432019-03-21 16:39:20 +010042static void tick_broadcast_oneshot_offline(unsigned int cpu);
Borislav Petkovaba095432019-03-29 11:28:52 +010043# endif
Thomas Gleixner5590a532007-07-21 04:37:35 -070044#else
Stephen Boyd94114c32017-06-07 23:36:03 -070045static inline void tick_broadcast_setup_oneshot(struct clock_event_device *bc) { BUG(); }
Thomas Gleixner5590a532007-07-21 04:37:35 -070046static inline void tick_broadcast_clear_oneshot(int cpu) { }
Thomas Gleixner080873c2015-03-25 13:09:55 +010047static inline void tick_resume_broadcast_oneshot(struct clock_event_device *bc) { }
Borislav Petkovaba095432019-03-29 11:28:52 +010048# ifdef CONFIG_HOTPLUG_CPU
Thomas Gleixner1b72d432019-03-21 16:39:20 +010049static inline void tick_broadcast_oneshot_offline(unsigned int cpu) { }
Borislav Petkovaba095432019-03-29 11:28:52 +010050# endif
Thomas Gleixner5590a532007-07-21 04:37:35 -070051#endif
52
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080053/*
Ingo Molnar289f4802007-02-16 01:28:15 -080054 * Debugging: see timer_list.c
55 */
56struct tick_device *tick_get_broadcast_device(void)
57{
58 return &tick_broadcast_device;
59}
60
Rusty Russell6b954822009-01-01 10:12:25 +103061struct cpumask *tick_get_broadcast_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -080062{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +010063 return tick_broadcast_mask;
Ingo Molnar289f4802007-02-16 01:28:15 -080064}
65
Will Deacon245a0572021-05-24 23:18:18 +010066static struct clock_event_device *tick_get_oneshot_wakeup_device(int cpu);
67
68const struct clock_event_device *tick_get_wakeup_device(int cpu)
69{
70 return tick_get_oneshot_wakeup_device(cpu);
71}
72
Ingo Molnar289f4802007-02-16 01:28:15 -080073/*
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080074 * Start the device in periodic mode
75 */
76static void tick_broadcast_start_periodic(struct clock_event_device *bc)
77{
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070078 if (bc)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080079 tick_setup_periodic(bc, 1);
80}
81
82/*
83 * Check, if the device can be utilized as broadcast device:
84 */
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000085static bool tick_check_broadcast_device(struct clock_event_device *curdev,
86 struct clock_event_device *newdev)
87{
88 if ((newdev->features & CLOCK_EVT_FEAT_DUMMY) ||
Soren Brinkmann245a3492013-09-18 11:48:37 -070089 (newdev->features & CLOCK_EVT_FEAT_PERCPU) ||
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000090 (newdev->features & CLOCK_EVT_FEAT_C3STOP))
91 return false;
92
93 if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT &&
94 !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
95 return false;
96
97 return !curdev || newdev->rating > curdev->rating;
98}
99
Will Deaconc94a8532021-05-24 23:18:16 +0100100#ifdef CONFIG_TICK_ONESHOT
101static struct clock_event_device *tick_get_oneshot_wakeup_device(int cpu)
102{
103 return per_cpu(tick_oneshot_wakeup_device, cpu);
104}
105
Will Deaconea5c7f12021-05-24 23:18:17 +0100106static void tick_oneshot_wakeup_handler(struct clock_event_device *wd)
107{
108 /*
109 * If we woke up early and the tick was reprogrammed in the
110 * meantime then this may be spurious but harmless.
111 */
112 tick_receive_broadcast();
113}
114
Will Deaconc94a8532021-05-24 23:18:16 +0100115static bool tick_set_oneshot_wakeup_device(struct clock_event_device *newdev,
116 int cpu)
117{
118 struct clock_event_device *curdev = tick_get_oneshot_wakeup_device(cpu);
119
120 if (!newdev)
121 goto set_device;
122
123 if ((newdev->features & CLOCK_EVT_FEAT_DUMMY) ||
124 (newdev->features & CLOCK_EVT_FEAT_C3STOP))
125 return false;
126
127 if (!(newdev->features & CLOCK_EVT_FEAT_PERCPU) ||
128 !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
129 return false;
130
131 if (!cpumask_equal(newdev->cpumask, cpumask_of(cpu)))
132 return false;
133
134 if (curdev && newdev->rating <= curdev->rating)
135 return false;
136
137 if (!try_module_get(newdev->owner))
138 return false;
139
Will Deaconea5c7f12021-05-24 23:18:17 +0100140 newdev->event_handler = tick_oneshot_wakeup_handler;
Will Deaconc94a8532021-05-24 23:18:16 +0100141set_device:
142 clockevents_exchange_device(curdev, newdev);
143 per_cpu(tick_oneshot_wakeup_device, cpu) = newdev;
144 return true;
145}
146#else
147static struct clock_event_device *tick_get_oneshot_wakeup_device(int cpu)
148{
149 return NULL;
150}
151
152static bool tick_set_oneshot_wakeup_device(struct clock_event_device *newdev,
153 int cpu)
154{
155 return false;
156}
157#endif
158
Thomas Gleixner45cb8e02013-04-25 20:31:50 +0000159/*
160 * Conditionally install/replace broadcast device
161 */
Will Deaconc94a8532021-05-24 23:18:16 +0100162void tick_install_broadcast_device(struct clock_event_device *dev, int cpu)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800163{
Thomas Gleixner6f7a05d72013-04-25 11:45:53 +0200164 struct clock_event_device *cur = tick_broadcast_device.evtdev;
165
Will Deaconc94a8532021-05-24 23:18:16 +0100166 if (tick_set_oneshot_wakeup_device(dev, cpu))
167 return;
168
Thomas Gleixner45cb8e02013-04-25 20:31:50 +0000169 if (!tick_check_broadcast_device(cur, dev))
Thomas Gleixner7172a2862013-04-25 20:31:47 +0000170 return;
Thomas Gleixner45cb8e02013-04-25 20:31:50 +0000171
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000172 if (!try_module_get(dev->owner))
173 return;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800174
Thomas Gleixner45cb8e02013-04-25 20:31:50 +0000175 clockevents_exchange_device(cur, dev);
Thomas Gleixner6f7a05d72013-04-25 11:45:53 +0200176 if (cur)
177 cur->event_handler = clockevents_handle_noop;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800178 tick_broadcast_device.evtdev = dev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100179 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800180 tick_broadcast_start_periodic(dev);
Jindong Yue9c336c92021-03-31 16:33:18 +0800181
182 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
183 return;
184
185 /*
186 * If the system already runs in oneshot mode, switch the newly
187 * registered broadcast device to oneshot mode explicitly.
188 */
189 if (tick_broadcast_oneshot_active()) {
190 tick_broadcast_switch_to_oneshot();
191 return;
192 }
193
Stephen Boydc038c1c2013-04-17 10:26:06 -0700194 /*
195 * Inform all cpus about this. We might be in a situation
196 * where we did not switch to oneshot mode because the per cpu
197 * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
198 * of a oneshot capable broadcast device. Without that
199 * notification the systems stays stuck in periodic mode
200 * forever.
201 */
Jindong Yue9c336c92021-03-31 16:33:18 +0800202 tick_clock_notify();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800203}
204
205/*
206 * Check, if the device is the broadcast device
207 */
208int tick_is_broadcast_device(struct clock_event_device *dev)
209{
210 return (dev && tick_broadcast_device.evtdev == dev);
211}
212
Thomas Gleixner627ee792014-02-03 14:34:31 -0800213int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq)
214{
215 int ret = -ENODEV;
216
217 if (tick_is_broadcast_device(dev)) {
218 raw_spin_lock(&tick_broadcast_lock);
219 ret = __clockevents_update_freq(dev, freq);
220 raw_spin_unlock(&tick_broadcast_lock);
221 }
222 return ret;
223}
224
225
Mark Rutland12ad1002013-01-14 17:05:22 +0000226static void err_broadcast(const struct cpumask *mask)
227{
228 pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
229}
230
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000231static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
232{
233 if (!dev->broadcast)
234 dev->broadcast = tick_broadcast;
235 if (!dev->broadcast) {
236 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
237 dev->name);
238 dev->broadcast = err_broadcast;
239 }
240}
241
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800242/*
Ingo Molnar4bf07f62021-03-22 22:39:03 +0100243 * Check, if the device is dysfunctional and a placeholder, which
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800244 * needs to be handled by the broadcast device.
245 */
246int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
247{
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200248 struct clock_event_device *bc = tick_broadcast_device.evtdev;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800249 unsigned long flags;
Thomas Gleixnere0454312015-07-07 14:07:27 +0200250 int ret = 0;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800251
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100252 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800253
254 /*
255 * Devices might be registered with both periodic and oneshot
256 * mode disabled. This signals, that the device needs to be
257 * operated from the broadcast device and is a placeholder for
258 * the cpu local device.
259 */
260 if (!tick_device_is_functional(dev)) {
261 dev->event_handler = tick_handle_periodic;
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000262 tick_device_setup_broadcast_func(dev);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100263 cpumask_set_cpu(cpu, tick_broadcast_mask);
Stephen Boyda272dcc2013-07-11 07:00:59 -0700264 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
265 tick_broadcast_start_periodic(bc);
266 else
267 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800268 ret = 1;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700269 } else {
270 /*
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200271 * Clear the broadcast bit for this cpu if the
272 * device is not power state affected.
Thomas Gleixner5590a532007-07-21 04:37:35 -0700273 */
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200274 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100275 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200276 else
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000277 tick_device_setup_broadcast_func(dev);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200278
279 /*
280 * Clear the broadcast bit if the CPU is not in
281 * periodic broadcast on state.
282 */
283 if (!cpumask_test_cpu(cpu, tick_broadcast_on))
284 cpumask_clear_cpu(cpu, tick_broadcast_mask);
285
286 switch (tick_broadcast_device.mode) {
287 case TICKDEV_MODE_ONESHOT:
288 /*
289 * If the system is in oneshot mode we can
290 * unconditionally clear the oneshot mask bit,
291 * because the CPU is running and therefore
292 * not in an idle state which causes the power
293 * state affected device to stop. Let the
294 * caller initialize the device.
295 */
296 tick_broadcast_clear_oneshot(cpu);
297 ret = 0;
298 break;
299
300 case TICKDEV_MODE_PERIODIC:
301 /*
302 * If the system is in periodic mode, check
303 * whether the broadcast device can be
304 * switched off now.
305 */
306 if (cpumask_empty(tick_broadcast_mask) && bc)
307 clockevents_shutdown(bc);
308 /*
309 * If we kept the cpu in the broadcast mask,
310 * tell the caller to leave the per cpu device
311 * in shutdown state. The periodic interrupt
Thomas Gleixnere0454312015-07-07 14:07:27 +0200312 * is delivered by the broadcast device, if
313 * the broadcast device exists and is not
314 * hrtimer based.
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200315 */
Thomas Gleixnere0454312015-07-07 14:07:27 +0200316 if (bc && !(bc->features & CLOCK_EVT_FEAT_HRTIMER))
317 ret = cpumask_test_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200318 break;
319 default:
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200320 break;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700321 }
322 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100323 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800324 return ret;
325}
326
Mark Rutland12572db2013-01-14 17:05:21 +0000327int tick_receive_broadcast(void)
328{
329 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
330 struct clock_event_device *evt = td->evtdev;
331
332 if (!evt)
333 return -ENODEV;
334
335 if (!evt->event_handler)
336 return -EINVAL;
337
338 evt->event_handler(evt);
339 return 0;
340}
Mark Rutland12572db2013-01-14 17:05:21 +0000341
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800342/*
Rusty Russell6b954822009-01-01 10:12:25 +1030343 * Broadcast the event to the cpus, which are set in the mask (mangled).
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800344 */
Thomas Gleixner2951d5c2015-05-05 10:00:13 +0200345static bool tick_do_broadcast(struct cpumask *mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800346{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100347 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800348 struct tick_device *td;
Thomas Gleixner2951d5c2015-05-05 10:00:13 +0200349 bool local = false;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800350
351 /*
352 * Check, if the current cpu is in the mask
353 */
Rusty Russell6b954822009-01-01 10:12:25 +1030354 if (cpumask_test_cpu(cpu, mask)) {
Thomas Gleixner8eb23122015-07-07 14:11:00 +0200355 struct clock_event_device *bc = tick_broadcast_device.evtdev;
356
Rusty Russell6b954822009-01-01 10:12:25 +1030357 cpumask_clear_cpu(cpu, mask);
Thomas Gleixner8eb23122015-07-07 14:11:00 +0200358 /*
359 * We only run the local handler, if the broadcast
360 * device is not hrtimer based. Otherwise we run into
361 * a hrtimer recursion.
362 *
363 * local timer_interrupt()
364 * local_handler()
365 * expire_hrtimers()
366 * bc_handler()
367 * local_handler()
368 * expire_hrtimers()
369 */
370 local = !(bc->features & CLOCK_EVT_FEAT_HRTIMER);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800371 }
372
Rusty Russell6b954822009-01-01 10:12:25 +1030373 if (!cpumask_empty(mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800374 /*
375 * It might be necessary to actually check whether the devices
376 * have different broadcast functions. For now, just use the
377 * one of the first device. This works as long as we have this
378 * misfeature only on x86 (lapic)
379 */
Rusty Russell6b954822009-01-01 10:12:25 +1030380 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
381 td->evtdev->broadcast(mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800382 }
Thomas Gleixner2951d5c2015-05-05 10:00:13 +0200383 return local;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800384}
385
386/*
387 * Periodic broadcast:
388 * - invoke the broadcast handlers
389 */
Thomas Gleixner2951d5c2015-05-05 10:00:13 +0200390static bool tick_do_periodic_broadcast(void)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800391{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100392 cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
Thomas Gleixner2951d5c2015-05-05 10:00:13 +0200393 return tick_do_broadcast(tmpmask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800394}
395
396/*
397 * Event handler for periodic broadcast ticks
398 */
399static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
400{
Thomas Gleixner2951d5c2015-05-05 10:00:13 +0200401 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
402 bool bc_local;
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000403
Thomas Gleixner627ee792014-02-03 14:34:31 -0800404 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixnerc4288332015-07-05 20:53:17 +0000405
406 /* Handle spurious interrupts gracefully */
407 if (clockevent_state_shutdown(tick_broadcast_device.evtdev)) {
408 raw_spin_unlock(&tick_broadcast_lock);
409 return;
410 }
411
Thomas Gleixner2951d5c2015-05-05 10:00:13 +0200412 bc_local = tick_do_periodic_broadcast();
Thomas Gleixner627ee792014-02-03 14:34:31 -0800413
Viresh Kumar472c4a92015-05-21 13:33:46 +0530414 if (clockevent_state_oneshot(dev)) {
Thomas Gleixnerb9965442020-11-17 14:19:49 +0100415 ktime_t next = ktime_add_ns(dev->next_event, TICK_NSEC);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800416
Thomas Gleixner2951d5c2015-05-05 10:00:13 +0200417 clockevents_program_event(dev, next, true);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800418 }
Thomas Gleixner627ee792014-02-03 14:34:31 -0800419 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner2951d5c2015-05-05 10:00:13 +0200420
421 /*
422 * We run the handler of the local cpu after dropping
423 * tick_broadcast_lock because the handler might deadlock when
424 * trying to switch to oneshot mode.
425 */
426 if (bc_local)
427 td->evtdev->event_handler(td->evtdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800428}
429
Thomas Gleixner592a4382015-04-03 02:01:10 +0200430/**
431 * tick_broadcast_control - Enable/disable or force broadcast mode
432 * @mode: The selected broadcast mode
433 *
434 * Called when the system enters a state where affected tick devices
435 * might stop. Note: TICK_BROADCAST_FORCE cannot be undone.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800436 */
Thomas Gleixner592a4382015-04-03 02:01:10 +0200437void tick_broadcast_control(enum tick_broadcast_mode mode)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800438{
439 struct clock_event_device *bc, *dev;
440 struct tick_device *td;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000441 int cpu, bc_stopped;
Mike Galbraith202461e2017-02-13 03:31:55 +0100442 unsigned long flags;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800443
Mike Galbraith202461e2017-02-13 03:31:55 +0100444 /* Protects also the local clockevent device. */
445 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner592a4382015-04-03 02:01:10 +0200446 td = this_cpu_ptr(&tick_cpu_device);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800447 dev = td->evtdev;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800448
449 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200450 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800451 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200452 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Mike Galbraith202461e2017-02-13 03:31:55 +0100453 goto out;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800454
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200455 if (!tick_device_is_functional(dev))
Mike Galbraith202461e2017-02-13 03:31:55 +0100456 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200457
Thomas Gleixner592a4382015-04-03 02:01:10 +0200458 cpu = smp_processor_id();
459 bc = tick_broadcast_device.evtdev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100460 bc_stopped = cpumask_empty(tick_broadcast_mask);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000461
Thomas Gleixner592a4382015-04-03 02:01:10 +0200462 switch (mode) {
463 case TICK_BROADCAST_FORCE:
464 tick_broadcast_forced = 1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500465 fallthrough;
Thomas Gleixner592a4382015-04-03 02:01:10 +0200466 case TICK_BROADCAST_ON:
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200467 cpumask_set_cpu(cpu, tick_broadcast_on);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100468 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixnere0454312015-07-07 14:07:27 +0200469 /*
470 * Only shutdown the cpu local device, if:
471 *
472 * - the broadcast device exists
473 * - the broadcast device is not a hrtimer based one
474 * - the broadcast device is in periodic mode to
Ingo Molnar4bf07f62021-03-22 22:39:03 +0100475 * avoid a hiccup during switch to oneshot mode
Thomas Gleixnere0454312015-07-07 14:07:27 +0200476 */
477 if (bc && !(bc->features & CLOCK_EVT_FEAT_HRTIMER) &&
478 tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700479 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800480 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200481 break;
Thomas Gleixner592a4382015-04-03 02:01:10 +0200482
483 case TICK_BROADCAST_OFF:
484 if (tick_broadcast_forced)
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200485 break;
486 cpumask_clear_cpu(cpu, tick_broadcast_on);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200487 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200488 if (tick_broadcast_device.mode ==
489 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800490 tick_setup_periodic(dev, 0);
491 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200492 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800493 }
494
Thomas Gleixnerc4d029f2015-07-11 14:26:34 +0200495 if (bc) {
496 if (cpumask_empty(tick_broadcast_mask)) {
497 if (!bc_stopped)
498 clockevents_shutdown(bc);
499 } else if (bc_stopped) {
500 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
501 tick_broadcast_start_periodic(bc);
502 else
503 tick_broadcast_setup_oneshot(bc);
504 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800505 }
Mike Galbraith202461e2017-02-13 03:31:55 +0100506out:
507 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800508}
Thomas Gleixner592a4382015-04-03 02:01:10 +0200509EXPORT_SYMBOL_GPL(tick_broadcast_control);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800510
511/*
512 * Set the periodic handler depending on broadcast on/off
513 */
514void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
515{
516 if (!broadcast)
517 dev->event_handler = tick_handle_periodic;
518 else
519 dev->event_handler = tick_handle_periodic_broadcast;
520}
521
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200522#ifdef CONFIG_HOTPLUG_CPU
Thomas Gleixner1b72d432019-03-21 16:39:20 +0100523static void tick_shutdown_broadcast(void)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800524{
Thomas Gleixner1b72d432019-03-21 16:39:20 +0100525 struct clock_event_device *bc = tick_broadcast_device.evtdev;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800526
527 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100528 if (bc && cpumask_empty(tick_broadcast_mask))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700529 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800530 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800531}
Thomas Gleixner1b72d432019-03-21 16:39:20 +0100532
533/*
534 * Remove a CPU from broadcasting
535 */
536void tick_broadcast_offline(unsigned int cpu)
537{
538 raw_spin_lock(&tick_broadcast_lock);
539 cpumask_clear_cpu(cpu, tick_broadcast_mask);
540 cpumask_clear_cpu(cpu, tick_broadcast_on);
541 tick_broadcast_oneshot_offline(cpu);
542 tick_shutdown_broadcast();
543 raw_spin_unlock(&tick_broadcast_lock);
544}
545
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200546#endif
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800547
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100548void tick_suspend_broadcast(void)
549{
550 struct clock_event_device *bc;
551 unsigned long flags;
552
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100553 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100554
555 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700556 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700557 clockevents_shutdown(bc);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100558
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100559 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100560}
561
Thomas Gleixnerf46481d2015-03-25 13:11:04 +0100562/*
563 * This is called from tick_resume_local() on a resuming CPU. That's
564 * called from the core resume function, tick_unfreeze() and the magic XEN
565 * resume hackery.
566 *
567 * In none of these cases the broadcast device mode can change and the
568 * bit of the resuming CPU in the broadcast mask is safe as well.
569 */
570bool tick_resume_check_broadcast(void)
571{
572 if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT)
573 return false;
574 else
575 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_mask);
576}
577
578void tick_resume_broadcast(void)
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100579{
580 struct clock_event_device *bc;
581 unsigned long flags;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100582
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100583 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100584
585 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100586
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100587 if (bc) {
Viresh Kumar554ef382015-02-27 17:21:32 +0530588 clockevents_tick_resume(bc);
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700589
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100590 switch (tick_broadcast_device.mode) {
591 case TICKDEV_MODE_PERIODIC:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100592 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100593 tick_broadcast_start_periodic(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100594 break;
595 case TICKDEV_MODE_ONESHOT:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100596 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixner080873c2015-03-25 13:09:55 +0100597 tick_resume_broadcast_oneshot(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100598 break;
599 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100600 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100601 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100602}
603
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800604#ifdef CONFIG_TICK_ONESHOT
605
Waiman Long668802c2017-01-30 12:57:43 -0500606static cpumask_var_t tick_broadcast_oneshot_mask __cpumask_var_read_mostly;
607static cpumask_var_t tick_broadcast_pending_mask __cpumask_var_read_mostly;
608static cpumask_var_t tick_broadcast_force_mask __cpumask_var_read_mostly;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800609
Ingo Molnar289f4802007-02-16 01:28:15 -0800610/*
Rusty Russell6b954822009-01-01 10:12:25 +1030611 * Exposed for debugging: see timer_list.c
Ingo Molnar289f4802007-02-16 01:28:15 -0800612 */
Rusty Russell6b954822009-01-01 10:12:25 +1030613struct cpumask *tick_get_broadcast_oneshot_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -0800614{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100615 return tick_broadcast_oneshot_mask;
Ingo Molnar289f4802007-02-16 01:28:15 -0800616}
617
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100618/*
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000619 * Called before going idle with interrupts disabled. Checks whether a
620 * broadcast event from the other core is about to happen. We detected
621 * that in tick_broadcast_oneshot_control(). The callsite can use this
622 * to avoid a deep idle transition as we are about to get the
623 * broadcast IPI right away.
624 */
625int tick_check_broadcast_expired(void)
626{
627 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
628}
629
630/*
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100631 * Set broadcast interrupt affinity
632 */
633static void tick_broadcast_set_affinity(struct clock_event_device *bc,
634 const struct cpumask *cpumask)
635{
636 if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
637 return;
638
639 if (cpumask_equal(bc->cpumask, cpumask))
640 return;
641
642 bc->cpumask = cpumask;
643 irq_set_affinity(bc->irq, bc->cpumask);
644}
645
Thomas Gleixner298dbd12015-05-05 09:44:24 +0200646static void tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
647 ktime_t expires)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800648{
Viresh Kumar472c4a92015-05-21 13:33:46 +0530649 if (!clockevent_state_oneshot(bc))
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200650 clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixnerb9a6a2352012-04-18 17:31:58 +0200651
Thomas Gleixner298dbd12015-05-05 09:44:24 +0200652 clockevents_program_event(bc, expires, 1);
653 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800654}
655
Thomas Gleixner080873c2015-03-25 13:09:55 +0100656static void tick_resume_broadcast_oneshot(struct clock_event_device *bc)
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100657{
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200658 clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100659}
660
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800661/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200662 * Called from irq_enter() when idle was interrupted to reenable the
663 * per cpu device.
664 */
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200665void tick_check_oneshot_broadcast_this_cpu(void)
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200666{
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200667 if (cpumask_test_cpu(smp_processor_id(), tick_broadcast_oneshot_mask)) {
Christoph Lameter22127e92014-08-17 12:30:25 -0500668 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200669
Thomas Gleixner1f73a982013-07-01 22:14:10 +0200670 /*
671 * We might be in the middle of switching over from
672 * periodic to oneshot. If the CPU has not yet
673 * switched over, leave the device alone.
674 */
675 if (td->mode == TICKDEV_MODE_ONESHOT) {
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200676 clockevents_switch_state(td->evtdev,
Viresh Kumar77e32c82015-02-27 17:21:33 +0530677 CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixner1f73a982013-07-01 22:14:10 +0200678 }
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200679 }
680}
681
682/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800683 * Handle oneshot mode broadcasting
684 */
685static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
686{
687 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100688 ktime_t now, next_event;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100689 int cpu, next_cpu = 0;
Thomas Gleixner298dbd12015-05-05 09:44:24 +0200690 bool bc_local;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800691
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100692 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100693 dev->next_event = KTIME_MAX;
694 next_event = KTIME_MAX;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100695 cpumask_clear(tmpmask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800696 now = ktime_get();
697 /* Find all expired events */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100698 for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
Dexuan Cui5596fe32018-05-15 19:52:50 +0000699 /*
700 * Required for !SMP because for_each_cpu() reports
701 * unconditionally CPU0 as set on UP kernels.
702 */
703 if (!IS_ENABLED(CONFIG_SMP) &&
704 cpumask_empty(tick_broadcast_oneshot_mask))
705 break;
706
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800707 td = &per_cpu(tick_cpu_device, cpu);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100708 if (td->evtdev->next_event <= now) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100709 cpumask_set_cpu(cpu, tmpmask);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000710 /*
711 * Mark the remote cpu in the pending mask, so
712 * it can avoid reprogramming the cpu local
713 * timer in tick_broadcast_oneshot_control().
714 */
715 cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100716 } else if (td->evtdev->next_event < next_event) {
717 next_event = td->evtdev->next_event;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100718 next_cpu = cpu;
719 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800720 }
721
Thomas Gleixner2938d272013-05-28 09:33:01 +0200722 /*
723 * Remove the current cpu from the pending mask. The event is
724 * delivered immediately in tick_do_broadcast() !
725 */
726 cpumask_clear_cpu(smp_processor_id(), tick_broadcast_pending_mask);
727
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000728 /* Take care of enforced broadcast requests */
729 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
730 cpumask_clear(tick_broadcast_force_mask);
731
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800732 /*
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200733 * Sanity check. Catch the case where we try to broadcast to
734 * offline cpus.
735 */
736 if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask)))
737 cpumask_and(tmpmask, tmpmask, cpu_online_mask);
738
739 /*
Thomas Gleixner298dbd12015-05-05 09:44:24 +0200740 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800741 */
Thomas Gleixner298dbd12015-05-05 09:44:24 +0200742 bc_local = tick_do_broadcast(tmpmask);
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100743
744 /*
745 * Two reasons for reprogram:
746 *
747 * - The global event did not expire any CPU local
748 * events. This happens in dyntick mode, as the maximum PIT
749 * delta is quite small.
750 *
751 * - There are pending events on sleeping CPUs which were not
752 * in the event mask
753 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100754 if (next_event != KTIME_MAX)
Thomas Gleixner298dbd12015-05-05 09:44:24 +0200755 tick_broadcast_set_event(dev, next_cpu, next_event);
756
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100757 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner298dbd12015-05-05 09:44:24 +0200758
759 if (bc_local) {
760 td = this_cpu_ptr(&tick_cpu_device);
761 td->evtdev->event_handler(td->evtdev);
762 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800763}
764
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530765static int broadcast_needs_cpu(struct clock_event_device *bc, int cpu)
766{
767 if (!(bc->features & CLOCK_EVT_FEAT_HRTIMER))
768 return 0;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100769 if (bc->next_event == KTIME_MAX)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530770 return 0;
771 return bc->bound_on == cpu ? -EBUSY : 0;
772}
773
774static void broadcast_shutdown_local(struct clock_event_device *bc,
775 struct clock_event_device *dev)
776{
777 /*
778 * For hrtimer based broadcasting we cannot shutdown the cpu
779 * local device if our own event is the first one to expire or
780 * if we own the broadcast timer.
781 */
782 if (bc->features & CLOCK_EVT_FEAT_HRTIMER) {
783 if (broadcast_needs_cpu(bc, smp_processor_id()))
784 return;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100785 if (dev->next_event < bc->next_event)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530786 return;
787 }
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200788 clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530789}
790
Will Deacone5007c22021-05-24 23:18:15 +0100791static int ___tick_broadcast_oneshot_control(enum tick_broadcast_state state,
792 struct tick_device *td,
793 int cpu)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800794{
Will Deacone5007c22021-05-24 23:18:15 +0100795 struct clock_event_device *bc, *dev = td->evtdev;
796 int ret = 0;
Thomas Gleixner1fe5d5c32015-04-03 02:05:15 +0200797 ktime_t now;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800798
Thomas Gleixner1fe5d5c32015-04-03 02:05:15 +0200799 raw_spin_lock(&tick_broadcast_lock);
Andi Kleen7372b0b2011-05-04 15:09:27 -0700800 bc = tick_broadcast_device.evtdev;
801
Thomas Gleixner1fe5d5c32015-04-03 02:05:15 +0200802 if (state == TICK_BROADCAST_ENTER) {
Thomas Gleixnere3ac79e2015-07-07 16:38:11 +0200803 /*
Thomas Gleixnerd5113e12015-07-07 16:43:04 +0200804 * If the current CPU owns the hrtimer broadcast
805 * mechanism, it cannot go deep idle and we do not add
806 * the CPU to the broadcast mask. We don't have to go
807 * through the EXIT path as the local timer is not
808 * shutdown.
809 */
810 ret = broadcast_needs_cpu(bc, cpu);
811 if (ret)
812 goto out;
813
814 /*
Thomas Gleixnere3ac79e2015-07-07 16:38:11 +0200815 * If the broadcast device is in periodic mode, we
816 * return.
817 */
Thomas Gleixnerd3325722015-07-07 17:45:22 +0200818 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
819 /* If it is a hrtimer based broadcast, return busy */
820 if (bc->features & CLOCK_EVT_FEAT_HRTIMER)
821 ret = -EBUSY;
Thomas Gleixnere3ac79e2015-07-07 16:38:11 +0200822 goto out;
Thomas Gleixnerd3325722015-07-07 17:45:22 +0200823 }
Thomas Gleixnere3ac79e2015-07-07 16:38:11 +0200824
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100825 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner2938d272013-05-28 09:33:01 +0200826 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
Thomas Gleixnerd5113e12015-07-07 16:43:04 +0200827
828 /* Conditionally shut down the local timer. */
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530829 broadcast_shutdown_local(bc, dev);
Thomas Gleixnerd5113e12015-07-07 16:43:04 +0200830
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000831 /*
832 * We only reprogram the broadcast timer if we
833 * did not mark ourself in the force mask and
834 * if the cpu local event is earlier than the
835 * broadcast event. If the current CPU is in
836 * the force mask, then we are going to be
Thomas Gleixner0cc52812015-07-07 16:45:15 +0200837 * woken by the IPI right away; we return
838 * busy, so the CPU does not try to go deep
839 * idle.
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000840 */
Thomas Gleixner0cc52812015-07-07 16:45:15 +0200841 if (cpumask_test_cpu(cpu, tick_broadcast_force_mask)) {
842 ret = -EBUSY;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100843 } else if (dev->next_event < bc->next_event) {
Thomas Gleixner298dbd12015-05-05 09:44:24 +0200844 tick_broadcast_set_event(bc, cpu, dev->next_event);
Thomas Gleixnerd5113e12015-07-07 16:43:04 +0200845 /*
846 * In case of hrtimer broadcasts the
847 * programming might have moved the
848 * timer to this cpu. If yes, remove
849 * us from the broadcast mask and
850 * return busy.
851 */
852 ret = broadcast_needs_cpu(bc, cpu);
853 if (ret) {
854 cpumask_clear_cpu(cpu,
855 tick_broadcast_oneshot_mask);
856 }
Thomas Gleixner0cc52812015-07-07 16:45:15 +0200857 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800858 }
859 } else {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100860 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200861 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000862 /*
863 * The cpu which was handling the broadcast
864 * timer marked this cpu in the broadcast
865 * pending mask and fired the broadcast
866 * IPI. So we are going to handle the expired
867 * event anyway via the broadcast IPI
868 * handler. No need to reprogram the timer
869 * with an already expired event.
870 */
871 if (cpumask_test_and_clear_cpu(cpu,
872 tick_broadcast_pending_mask))
873 goto out;
874
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000875 /*
Daniel Lezcanoea8deb82013-06-17 18:15:35 +0200876 * Bail out if there is no next event.
877 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100878 if (dev->next_event == KTIME_MAX)
Daniel Lezcanoea8deb82013-06-17 18:15:35 +0200879 goto out;
880 /*
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000881 * If the pending bit is not set, then we are
882 * either the CPU handling the broadcast
883 * interrupt or we got woken by something else.
884 *
Laurent Gauthier13e792a2019-01-05 00:07:45 +0100885 * We are no longer in the broadcast mask, so
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000886 * if the cpu local expiry time is already
887 * reached, we would reprogram the cpu local
888 * timer with an already expired event.
889 *
890 * This can lead to a ping-pong when we return
Laurent Gauthier13e792a2019-01-05 00:07:45 +0100891 * to idle and therefore rearm the broadcast
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000892 * timer before the cpu local timer was able
893 * to fire. This happens because the forced
894 * reprogramming makes sure that the event
895 * will happen in the future and depending on
896 * the min_delta setting this might be far
897 * enough out that the ping-pong starts.
898 *
899 * If the cpu local next_event has expired
900 * then we know that the broadcast timer
901 * next_event has expired as well and
902 * broadcast is about to be handled. So we
903 * avoid reprogramming and enforce that the
904 * broadcast handler, which did not run yet,
905 * will invoke the cpu local handler.
906 *
907 * We cannot call the handler directly from
908 * here, because we might be in a NOHZ phase
909 * and we did not go through the irq_enter()
910 * nohz fixups.
911 */
912 now = ktime_get();
Thomas Gleixner2456e852016-12-25 11:38:40 +0100913 if (dev->next_event <= now) {
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000914 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
915 goto out;
916 }
917 /*
918 * We got woken by something else. Reprogram
919 * the cpu local timer device.
920 */
Thomas Gleixner26517f32013-03-06 11:18:35 +0000921 tick_program_event(dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800922 }
923 }
Thomas Gleixner26517f32013-03-06 11:18:35 +0000924out:
Thomas Gleixner1fe5d5c32015-04-03 02:05:15 +0200925 raw_spin_unlock(&tick_broadcast_lock);
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530926 return ret;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800927}
928
Will Deaconea5c7f12021-05-24 23:18:17 +0100929static int tick_oneshot_wakeup_control(enum tick_broadcast_state state,
930 struct tick_device *td,
931 int cpu)
932{
933 struct clock_event_device *dev, *wd;
934
935 dev = td->evtdev;
936 if (td->mode != TICKDEV_MODE_ONESHOT)
937 return -EINVAL;
938
939 wd = tick_get_oneshot_wakeup_device(cpu);
940 if (!wd)
941 return -ENODEV;
942
943 switch (state) {
944 case TICK_BROADCAST_ENTER:
945 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT_STOPPED);
946 clockevents_switch_state(wd, CLOCK_EVT_STATE_ONESHOT);
947 clockevents_program_event(wd, dev->next_event, 1);
948 break;
949 case TICK_BROADCAST_EXIT:
950 /* We may have transitioned to oneshot mode while idle */
951 if (clockevent_get_state(wd) != CLOCK_EVT_STATE_ONESHOT)
952 return -ENODEV;
953 }
954
955 return 0;
956}
957
Will Deacone5007c22021-05-24 23:18:15 +0100958int __tick_broadcast_oneshot_control(enum tick_broadcast_state state)
959{
960 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
961 int cpu = smp_processor_id();
962
Will Deaconea5c7f12021-05-24 23:18:17 +0100963 if (!tick_oneshot_wakeup_control(state, td, cpu))
964 return 0;
965
Will Deacone5007c22021-05-24 23:18:15 +0100966 if (tick_broadcast_device.evtdev)
967 return ___tick_broadcast_oneshot_control(state, td, cpu);
968
969 /*
Will Deaconea5c7f12021-05-24 23:18:17 +0100970 * If there is no broadcast or wakeup device, tell the caller not
Will Deacone5007c22021-05-24 23:18:15 +0100971 * to go into deep idle.
972 */
973 return -EBUSY;
974}
975
Thomas Gleixner5590a532007-07-21 04:37:35 -0700976/*
977 * Reset the one shot broadcast for a cpu
978 *
979 * Called with tick_broadcast_lock held
980 */
981static void tick_broadcast_clear_oneshot(int cpu)
982{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100983 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixnerdd5fd9b2014-02-11 14:35:40 +0100984 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700985}
986
Rusty Russell6b954822009-01-01 10:12:25 +1030987static void tick_broadcast_init_next_event(struct cpumask *mask,
988 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200989{
990 struct tick_device *td;
991 int cpu;
992
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030993 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200994 td = &per_cpu(tick_cpu_device, cpu);
995 if (td->evtdev)
996 td->evtdev->next_event = expires;
997 }
998}
999
Thomas Gleixnerf73f64d2020-11-17 14:19:43 +01001000static inline ktime_t tick_get_next_period(void)
1001{
1002 ktime_t next;
1003
1004 /*
1005 * Protect against concurrent updates (store /load tearing on
1006 * 32bit). It does not matter if the time is already in the
1007 * past. The broadcast device which is about to be programmed will
1008 * fire in any case.
1009 */
1010 raw_spin_lock(&jiffies_lock);
1011 next = tick_next_period;
1012 raw_spin_unlock(&jiffies_lock);
1013 return next;
1014}
1015
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001016/**
Li Zefan8dce39c2007-11-05 14:51:10 -08001017 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001018 */
Stephen Boyd94114c32017-06-07 23:36:03 -07001019static void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001020{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +02001021 int cpu = smp_processor_id();
1022
Thomas Gleixnerc1a9eeb2016-12-15 12:10:37 +01001023 if (!bc)
1024 return;
1025
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +00001026 /* Set it up only once ! */
1027 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Viresh Kumar472c4a92015-05-21 13:33:46 +05301028 int was_periodic = clockevent_state_periodic(bc);
Thomas Gleixner73007112008-09-06 03:01:45 +02001029
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +00001030 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +02001031
Thomas Gleixner73007112008-09-06 03:01:45 +02001032 /*
1033 * We must be careful here. There might be other CPUs
1034 * waiting for periodic broadcast. We need to set the
1035 * oneshot_mask bits for those and program the
1036 * broadcast device to fire.
1037 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +01001038 cpumask_copy(tmpmask, tick_broadcast_mask);
1039 cpumask_clear_cpu(cpu, tmpmask);
1040 cpumask_or(tick_broadcast_oneshot_mask,
1041 tick_broadcast_oneshot_mask, tmpmask);
Thomas Gleixner73007112008-09-06 03:01:45 +02001042
Thomas Gleixnerb352bc12013-03-05 14:25:32 +01001043 if (was_periodic && !cpumask_empty(tmpmask)) {
Thomas Gleixnerf73f64d2020-11-17 14:19:43 +01001044 ktime_t nextevt = tick_get_next_period();
1045
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +02001046 clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixnerf73f64d2020-11-17 14:19:43 +01001047 tick_broadcast_init_next_event(tmpmask, nextevt);
1048 tick_broadcast_set_event(bc, cpu, nextevt);
Thomas Gleixner73007112008-09-06 03:01:45 +02001049 } else
Thomas Gleixner2456e852016-12-25 11:38:40 +01001050 bc->next_event = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +02001051 } else {
1052 /*
1053 * The first cpu which switches to oneshot mode sets
1054 * the bit for all other cpus which are in the general
1055 * (periodic) broadcast mask. So the bit is set and
1056 * would prevent the first broadcast enter after this
1057 * to program the bc device.
1058 */
1059 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +00001060 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001061}
1062
1063/*
1064 * Select oneshot operating mode for the broadcast device
1065 */
1066void tick_broadcast_switch_to_oneshot(void)
1067{
1068 struct clock_event_device *bc;
1069 unsigned long flags;
1070
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +01001071 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -07001072
1073 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001074 bc = tick_broadcast_device.evtdev;
1075 if (bc)
1076 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -07001077
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +01001078 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001079}
1080
Thomas Gleixnera49b1162015-04-03 02:38:05 +02001081#ifdef CONFIG_HOTPLUG_CPU
1082void hotplug_cpu__broadcast_tick_pull(int deadcpu)
1083{
1084 struct clock_event_device *bc;
1085 unsigned long flags;
1086
1087 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
1088 bc = tick_broadcast_device.evtdev;
1089
1090 if (bc && broadcast_needs_cpu(bc, deadcpu)) {
1091 /* This moves the broadcast assignment to this CPU: */
1092 clockevents_program_event(bc, bc->next_event, 1);
1093 }
1094 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
1095}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001096
1097/*
Thomas Gleixner1b72d432019-03-21 16:39:20 +01001098 * Remove a dying CPU from broadcasting
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001099 */
Thomas Gleixner1b72d432019-03-21 16:39:20 +01001100static void tick_broadcast_oneshot_offline(unsigned int cpu)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001101{
Will Deaconc94a8532021-05-24 23:18:16 +01001102 if (tick_get_oneshot_wakeup_device(cpu))
1103 tick_set_oneshot_wakeup_device(NULL, cpu);
1104
Thomas Gleixner31d9b392007-09-16 15:36:43 +02001105 /*
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +02001106 * Clear the broadcast masks for the dead cpu, but do not stop
1107 * the broadcast device!
Thomas Gleixner31d9b392007-09-16 15:36:43 +02001108 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +01001109 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +02001110 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
1111 cpumask_clear_cpu(cpu, tick_broadcast_force_mask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001112}
Thomas Gleixnera49b1162015-04-03 02:38:05 +02001113#endif
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001114
Thomas Gleixner27ce4cb4a2008-09-22 19:04:02 +02001115/*
1116 * Check, whether the broadcast device is in one shot mode
1117 */
1118int tick_broadcast_oneshot_active(void)
1119{
1120 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
1121}
1122
Thomas Gleixner3a142a02011-02-25 22:34:23 +01001123/*
1124 * Check whether the broadcast device supports oneshot.
1125 */
1126bool tick_broadcast_oneshot_available(void)
1127{
1128 struct clock_event_device *bc = tick_broadcast_device.evtdev;
1129
1130 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
1131}
1132
Thomas Gleixnerf32dd112015-07-07 16:29:38 +02001133#else
1134int __tick_broadcast_oneshot_control(enum tick_broadcast_state state)
1135{
1136 struct clock_event_device *bc = tick_broadcast_device.evtdev;
1137
1138 if (!bc || (bc->features & CLOCK_EVT_FEAT_HRTIMER))
1139 return -EBUSY;
1140
1141 return 0;
1142}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001143#endif
Thomas Gleixnerb352bc12013-03-05 14:25:32 +01001144
1145void __init tick_broadcast_init(void)
1146{
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +02001147 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
Thomas Gleixner07bd1172013-07-01 22:14:10 +02001148 zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT);
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +02001149 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +01001150#ifdef CONFIG_TICK_ONESHOT
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +02001151 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
1152 zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
1153 zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +01001154#endif
1155}