blob: 60e6c23ce1c787e2584a07a7bf59d1b85f63f279 [file] [log] [blame]
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -08001/*
2 * linux/kernel/time/tick-broadcast.c
3 *
4 * This file contains functions which emulate a local clock-event
5 * device via a broadcast event source.
6 *
7 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
8 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
9 * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
10 *
11 * This code is licenced under the GPL version 2. For details see
12 * kernel-base/COPYING.
13 */
14#include <linux/cpu.h>
15#include <linux/err.h>
16#include <linux/hrtimer.h>
Russell Kingd7b90682008-04-17 07:46:24 +020017#include <linux/interrupt.h>
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080018#include <linux/percpu.h>
19#include <linux/profile.h>
20#include <linux/sched.h>
Mark Rutland12ad1002013-01-14 17:05:22 +000021#include <linux/smp.h>
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000022#include <linux/module.h>
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080023
24#include "tick-internal.h"
25
26/*
27 * Broadcast support for broken x86 hardware, where the local apic
28 * timer stops in C3 state.
29 */
30
Dmitri Vorobieva52f5c52009-05-01 13:10:21 -070031static struct tick_device tick_broadcast_device;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +010032static cpumask_var_t tick_broadcast_mask;
Thomas Gleixner07bd1172013-07-01 22:14:10 +020033static cpumask_var_t tick_broadcast_on;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +010034static cpumask_var_t tmpmask;
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +010035static DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
Thomas Gleixneraa276e12008-06-09 19:15:00 +020036static int tick_broadcast_force;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080037
Thomas Gleixner5590a532007-07-21 04:37:35 -070038#ifdef CONFIG_TICK_ONESHOT
39static 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);
Thomas Gleixner5590a532007-07-21 04:37:35 -070041#else
42static inline void tick_broadcast_clear_oneshot(int cpu) { }
Thomas Gleixner080873c2015-03-25 13:09:55 +010043static inline void tick_resume_broadcast_oneshot(struct clock_event_device *bc) { }
Thomas Gleixner5590a532007-07-21 04:37:35 -070044#endif
45
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080046/*
Ingo Molnar289f4802007-02-16 01:28:15 -080047 * Debugging: see timer_list.c
48 */
49struct tick_device *tick_get_broadcast_device(void)
50{
51 return &tick_broadcast_device;
52}
53
Rusty Russell6b954822009-01-01 10:12:25 +103054struct cpumask *tick_get_broadcast_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -080055{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +010056 return tick_broadcast_mask;
Ingo Molnar289f4802007-02-16 01:28:15 -080057}
58
59/*
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080060 * Start the device in periodic mode
61 */
62static void tick_broadcast_start_periodic(struct clock_event_device *bc)
63{
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070064 if (bc)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080065 tick_setup_periodic(bc, 1);
66}
67
68/*
69 * Check, if the device can be utilized as broadcast device:
70 */
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000071static bool tick_check_broadcast_device(struct clock_event_device *curdev,
72 struct clock_event_device *newdev)
73{
74 if ((newdev->features & CLOCK_EVT_FEAT_DUMMY) ||
Soren Brinkmann245a3492013-09-18 11:48:37 -070075 (newdev->features & CLOCK_EVT_FEAT_PERCPU) ||
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000076 (newdev->features & CLOCK_EVT_FEAT_C3STOP))
77 return false;
78
79 if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT &&
80 !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
81 return false;
82
83 return !curdev || newdev->rating > curdev->rating;
84}
85
86/*
87 * Conditionally install/replace broadcast device
88 */
Thomas Gleixner7172a282013-04-25 20:31:47 +000089void tick_install_broadcast_device(struct clock_event_device *dev)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080090{
Thomas Gleixner6f7a05d2013-04-25 11:45:53 +020091 struct clock_event_device *cur = tick_broadcast_device.evtdev;
92
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000093 if (!tick_check_broadcast_device(cur, dev))
Thomas Gleixner7172a282013-04-25 20:31:47 +000094 return;
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000095
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000096 if (!try_module_get(dev->owner))
97 return;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080098
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000099 clockevents_exchange_device(cur, dev);
Thomas Gleixner6f7a05d2013-04-25 11:45:53 +0200100 if (cur)
101 cur->event_handler = clockevents_handle_noop;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800102 tick_broadcast_device.evtdev = dev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100103 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800104 tick_broadcast_start_periodic(dev);
Stephen Boydc038c1c2013-04-17 10:26:06 -0700105 /*
106 * Inform all cpus about this. We might be in a situation
107 * where we did not switch to oneshot mode because the per cpu
108 * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
109 * of a oneshot capable broadcast device. Without that
110 * notification the systems stays stuck in periodic mode
111 * forever.
112 */
113 if (dev->features & CLOCK_EVT_FEAT_ONESHOT)
114 tick_clock_notify();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800115}
116
117/*
118 * Check, if the device is the broadcast device
119 */
120int tick_is_broadcast_device(struct clock_event_device *dev)
121{
122 return (dev && tick_broadcast_device.evtdev == dev);
123}
124
Thomas Gleixner627ee792014-02-03 14:34:31 -0800125int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq)
126{
127 int ret = -ENODEV;
128
129 if (tick_is_broadcast_device(dev)) {
130 raw_spin_lock(&tick_broadcast_lock);
131 ret = __clockevents_update_freq(dev, freq);
132 raw_spin_unlock(&tick_broadcast_lock);
133 }
134 return ret;
135}
136
137
Mark Rutland12ad1002013-01-14 17:05:22 +0000138static void err_broadcast(const struct cpumask *mask)
139{
140 pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
141}
142
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000143static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
144{
145 if (!dev->broadcast)
146 dev->broadcast = tick_broadcast;
147 if (!dev->broadcast) {
148 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
149 dev->name);
150 dev->broadcast = err_broadcast;
151 }
152}
153
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800154/*
155 * Check, if the device is disfunctional and a place holder, which
156 * needs to be handled by the broadcast device.
157 */
158int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
159{
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200160 struct clock_event_device *bc = tick_broadcast_device.evtdev;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800161 unsigned long flags;
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200162 int ret;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800163
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100164 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800165
166 /*
167 * Devices might be registered with both periodic and oneshot
168 * mode disabled. This signals, that the device needs to be
169 * operated from the broadcast device and is a placeholder for
170 * the cpu local device.
171 */
172 if (!tick_device_is_functional(dev)) {
173 dev->event_handler = tick_handle_periodic;
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000174 tick_device_setup_broadcast_func(dev);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100175 cpumask_set_cpu(cpu, tick_broadcast_mask);
Stephen Boyda272dcc2013-07-11 07:00:59 -0700176 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
177 tick_broadcast_start_periodic(bc);
178 else
179 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800180 ret = 1;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700181 } else {
182 /*
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200183 * Clear the broadcast bit for this cpu if the
184 * device is not power state affected.
Thomas Gleixner5590a532007-07-21 04:37:35 -0700185 */
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200186 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100187 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200188 else
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000189 tick_device_setup_broadcast_func(dev);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200190
191 /*
192 * Clear the broadcast bit if the CPU is not in
193 * periodic broadcast on state.
194 */
195 if (!cpumask_test_cpu(cpu, tick_broadcast_on))
196 cpumask_clear_cpu(cpu, tick_broadcast_mask);
197
198 switch (tick_broadcast_device.mode) {
199 case TICKDEV_MODE_ONESHOT:
200 /*
201 * If the system is in oneshot mode we can
202 * unconditionally clear the oneshot mask bit,
203 * because the CPU is running and therefore
204 * not in an idle state which causes the power
205 * state affected device to stop. Let the
206 * caller initialize the device.
207 */
208 tick_broadcast_clear_oneshot(cpu);
209 ret = 0;
210 break;
211
212 case TICKDEV_MODE_PERIODIC:
213 /*
214 * If the system is in periodic mode, check
215 * whether the broadcast device can be
216 * switched off now.
217 */
218 if (cpumask_empty(tick_broadcast_mask) && bc)
219 clockevents_shutdown(bc);
220 /*
221 * If we kept the cpu in the broadcast mask,
222 * tell the caller to leave the per cpu device
223 * in shutdown state. The periodic interrupt
224 * is delivered by the broadcast device.
225 */
226 ret = cpumask_test_cpu(cpu, tick_broadcast_mask);
227 break;
228 default:
229 /* Nothing to do */
230 ret = 0;
231 break;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700232 }
233 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100234 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800235 return ret;
236}
237
Mark Rutland12572db2013-01-14 17:05:21 +0000238#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
239int tick_receive_broadcast(void)
240{
241 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
242 struct clock_event_device *evt = td->evtdev;
243
244 if (!evt)
245 return -ENODEV;
246
247 if (!evt->event_handler)
248 return -EINVAL;
249
250 evt->event_handler(evt);
251 return 0;
252}
253#endif
254
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800255/*
Rusty Russell6b954822009-01-01 10:12:25 +1030256 * Broadcast the event to the cpus, which are set in the mask (mangled).
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800257 */
Rusty Russell6b954822009-01-01 10:12:25 +1030258static void tick_do_broadcast(struct cpumask *mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800259{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100260 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800261 struct tick_device *td;
262
263 /*
264 * Check, if the current cpu is in the mask
265 */
Rusty Russell6b954822009-01-01 10:12:25 +1030266 if (cpumask_test_cpu(cpu, mask)) {
267 cpumask_clear_cpu(cpu, mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800268 td = &per_cpu(tick_cpu_device, cpu);
269 td->evtdev->event_handler(td->evtdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800270 }
271
Rusty Russell6b954822009-01-01 10:12:25 +1030272 if (!cpumask_empty(mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800273 /*
274 * It might be necessary to actually check whether the devices
275 * have different broadcast functions. For now, just use the
276 * one of the first device. This works as long as we have this
277 * misfeature only on x86 (lapic)
278 */
Rusty Russell6b954822009-01-01 10:12:25 +1030279 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
280 td->evtdev->broadcast(mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800281 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800282}
283
284/*
285 * Periodic broadcast:
286 * - invoke the broadcast handlers
287 */
288static void tick_do_periodic_broadcast(void)
289{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100290 cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
291 tick_do_broadcast(tmpmask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800292}
293
294/*
295 * Event handler for periodic broadcast ticks
296 */
297static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
298{
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000299 ktime_t next;
300
Thomas Gleixner627ee792014-02-03 14:34:31 -0800301 raw_spin_lock(&tick_broadcast_lock);
302
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800303 tick_do_periodic_broadcast();
304
305 /*
306 * The device is in periodic mode. No reprogramming necessary:
307 */
Viresh Kumar77e32c82015-02-27 17:21:33 +0530308 if (dev->state == CLOCK_EVT_STATE_PERIODIC)
Thomas Gleixner627ee792014-02-03 14:34:31 -0800309 goto unlock;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800310
311 /*
312 * Setup the next period for devices, which do not have
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000313 * periodic mode. We read dev->next_event first and add to it
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200314 * when the event already expired. clockevents_program_event()
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000315 * sets dev->next_event only when the event is really
316 * programmed to the device.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800317 */
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000318 for (next = dev->next_event; ;) {
319 next = ktime_add(next, tick_period);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800320
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200321 if (!clockevents_program_event(dev, next, false))
Thomas Gleixner627ee792014-02-03 14:34:31 -0800322 goto unlock;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800323 tick_do_periodic_broadcast();
324 }
Thomas Gleixner627ee792014-02-03 14:34:31 -0800325unlock:
326 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800327}
328
329/*
330 * Powerstate information: The system enters/leaves a state, where
331 * affected devices might stop
332 */
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700333static void tick_do_broadcast_on_off(unsigned long *reason)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800334{
335 struct clock_event_device *bc, *dev;
336 struct tick_device *td;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700337 unsigned long flags;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000338 int cpu, bc_stopped;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800339
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100340 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800341
342 cpu = smp_processor_id();
343 td = &per_cpu(tick_cpu_device, cpu);
344 dev = td->evtdev;
345 bc = tick_broadcast_device.evtdev;
346
347 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200348 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800349 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200350 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800351 goto out;
352
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200353 if (!tick_device_is_functional(dev))
354 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200355
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100356 bc_stopped = cpumask_empty(tick_broadcast_mask);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000357
Thomas Gleixner1595f452007-10-14 22:57:45 +0200358 switch (*reason) {
359 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
360 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200361 cpumask_set_cpu(cpu, tick_broadcast_on);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100362 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200363 if (tick_broadcast_device.mode ==
364 TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700365 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800366 }
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200367 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200368 tick_broadcast_force = 1;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200369 break;
370 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200371 if (tick_broadcast_force)
372 break;
373 cpumask_clear_cpu(cpu, tick_broadcast_on);
374 if (!tick_device_is_functional(dev))
375 break;
376 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200377 if (tick_broadcast_device.mode ==
378 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800379 tick_setup_periodic(dev, 0);
380 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200381 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800382 }
383
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100384 if (cpumask_empty(tick_broadcast_mask)) {
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000385 if (!bc_stopped)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700386 clockevents_shutdown(bc);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000387 } else if (bc_stopped) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800388 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
389 tick_broadcast_start_periodic(bc);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800390 else
391 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800392 }
393out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100394 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800395}
396
397/*
398 * Powerstate information: The system enters/leaves a state, where
399 * affected devices might stop.
400 */
401void tick_broadcast_on_off(unsigned long reason, int *oncpu)
402{
Rusty Russell6b954822009-01-01 10:12:25 +1030403 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
Glauber Costa833df312008-04-18 13:38:58 -0700404 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
Thomas Gleixner72fcde92007-05-23 13:57:30 -0700405 "offline CPU #%d\n", *oncpu);
Avi Kivitybf020cb2007-10-16 23:26:24 -0700406 else
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700407 tick_do_broadcast_on_off(&reason);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800408}
409
410/*
411 * Set the periodic handler depending on broadcast on/off
412 */
413void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
414{
415 if (!broadcast)
416 dev->event_handler = tick_handle_periodic;
417 else
418 dev->event_handler = tick_handle_periodic_broadcast;
419}
420
421/*
422 * Remove a CPU from broadcasting
423 */
424void tick_shutdown_broadcast(unsigned int *cpup)
425{
426 struct clock_event_device *bc;
427 unsigned long flags;
428 unsigned int cpu = *cpup;
429
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100430 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800431
432 bc = tick_broadcast_device.evtdev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100433 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200434 cpumask_clear_cpu(cpu, tick_broadcast_on);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800435
436 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100437 if (bc && cpumask_empty(tick_broadcast_mask))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700438 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800439 }
440
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100441 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800442}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800443
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100444void tick_suspend_broadcast(void)
445{
446 struct clock_event_device *bc;
447 unsigned long flags;
448
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100449 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100450
451 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700452 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700453 clockevents_shutdown(bc);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100454
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100455 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100456}
457
458int tick_resume_broadcast(void)
459{
460 struct clock_event_device *bc;
461 unsigned long flags;
462 int broadcast = 0;
463
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100464 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100465
466 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100467
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100468 if (bc) {
Viresh Kumar554ef382015-02-27 17:21:32 +0530469 clockevents_tick_resume(bc);
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700470
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100471 switch (tick_broadcast_device.mode) {
472 case TICKDEV_MODE_PERIODIC:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100473 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100474 tick_broadcast_start_periodic(bc);
Rusty Russell6b954822009-01-01 10:12:25 +1030475 broadcast = cpumask_test_cpu(smp_processor_id(),
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100476 tick_broadcast_mask);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100477 break;
478 case TICKDEV_MODE_ONESHOT:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100479 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixner080873c2015-03-25 13:09:55 +0100480 tick_resume_broadcast_oneshot(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100481 break;
482 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100483 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100484 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100485
486 return broadcast;
487}
488
489
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800490#ifdef CONFIG_TICK_ONESHOT
491
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100492static cpumask_var_t tick_broadcast_oneshot_mask;
Thomas Gleixner26517f32013-03-06 11:18:35 +0000493static cpumask_var_t tick_broadcast_pending_mask;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000494static cpumask_var_t tick_broadcast_force_mask;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800495
Ingo Molnar289f4802007-02-16 01:28:15 -0800496/*
Rusty Russell6b954822009-01-01 10:12:25 +1030497 * Exposed for debugging: see timer_list.c
Ingo Molnar289f4802007-02-16 01:28:15 -0800498 */
Rusty Russell6b954822009-01-01 10:12:25 +1030499struct cpumask *tick_get_broadcast_oneshot_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -0800500{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100501 return tick_broadcast_oneshot_mask;
Ingo Molnar289f4802007-02-16 01:28:15 -0800502}
503
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100504/*
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000505 * Called before going idle with interrupts disabled. Checks whether a
506 * broadcast event from the other core is about to happen. We detected
507 * that in tick_broadcast_oneshot_control(). The callsite can use this
508 * to avoid a deep idle transition as we are about to get the
509 * broadcast IPI right away.
510 */
511int tick_check_broadcast_expired(void)
512{
513 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
514}
515
516/*
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100517 * Set broadcast interrupt affinity
518 */
519static void tick_broadcast_set_affinity(struct clock_event_device *bc,
520 const struct cpumask *cpumask)
521{
522 if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
523 return;
524
525 if (cpumask_equal(bc->cpumask, cpumask))
526 return;
527
528 bc->cpumask = cpumask;
529 irq_set_affinity(bc->irq, bc->cpumask);
530}
531
532static int tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
Daniel Lezcanof9ae39d2013-03-02 11:10:10 +0100533 ktime_t expires, int force)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800534{
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100535 int ret;
536
Viresh Kumar77e32c82015-02-27 17:21:33 +0530537 if (bc->state != CLOCK_EVT_STATE_ONESHOT)
538 clockevents_set_state(bc, CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixnerb9a6a2352012-04-18 17:31:58 +0200539
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100540 ret = clockevents_program_event(bc, expires, force);
541 if (!ret)
542 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
543 return ret;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800544}
545
Thomas Gleixner080873c2015-03-25 13:09:55 +0100546static void tick_resume_broadcast_oneshot(struct clock_event_device *bc)
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100547{
Viresh Kumar77e32c82015-02-27 17:21:33 +0530548 clockevents_set_state(bc, CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100549}
550
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800551/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200552 * Called from irq_enter() when idle was interrupted to reenable the
553 * per cpu device.
554 */
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200555void tick_check_oneshot_broadcast_this_cpu(void)
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200556{
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200557 if (cpumask_test_cpu(smp_processor_id(), tick_broadcast_oneshot_mask)) {
Christoph Lameter22127e92014-08-17 12:30:25 -0500558 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200559
Thomas Gleixner1f73a982013-07-01 22:14:10 +0200560 /*
561 * We might be in the middle of switching over from
562 * periodic to oneshot. If the CPU has not yet
563 * switched over, leave the device alone.
564 */
565 if (td->mode == TICKDEV_MODE_ONESHOT) {
Viresh Kumar77e32c82015-02-27 17:21:33 +0530566 clockevents_set_state(td->evtdev,
567 CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixner1f73a982013-07-01 22:14:10 +0200568 }
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200569 }
570}
571
572/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800573 * Handle oneshot mode broadcasting
574 */
575static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
576{
577 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100578 ktime_t now, next_event;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100579 int cpu, next_cpu = 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800580
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100581 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800582again:
583 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100584 next_event.tv64 = KTIME_MAX;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100585 cpumask_clear(tmpmask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800586 now = ktime_get();
587 /* Find all expired events */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100588 for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800589 td = &per_cpu(tick_cpu_device, cpu);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100590 if (td->evtdev->next_event.tv64 <= now.tv64) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100591 cpumask_set_cpu(cpu, tmpmask);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000592 /*
593 * Mark the remote cpu in the pending mask, so
594 * it can avoid reprogramming the cpu local
595 * timer in tick_broadcast_oneshot_control().
596 */
597 cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100598 } else if (td->evtdev->next_event.tv64 < next_event.tv64) {
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100599 next_event.tv64 = td->evtdev->next_event.tv64;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100600 next_cpu = cpu;
601 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800602 }
603
Thomas Gleixner2938d272013-05-28 09:33:01 +0200604 /*
605 * Remove the current cpu from the pending mask. The event is
606 * delivered immediately in tick_do_broadcast() !
607 */
608 cpumask_clear_cpu(smp_processor_id(), tick_broadcast_pending_mask);
609
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000610 /* Take care of enforced broadcast requests */
611 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
612 cpumask_clear(tick_broadcast_force_mask);
613
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800614 /*
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200615 * Sanity check. Catch the case where we try to broadcast to
616 * offline cpus.
617 */
618 if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask)))
619 cpumask_and(tmpmask, tmpmask, cpu_online_mask);
620
621 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100622 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800623 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100624 tick_do_broadcast(tmpmask);
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100625
626 /*
627 * Two reasons for reprogram:
628 *
629 * - The global event did not expire any CPU local
630 * events. This happens in dyntick mode, as the maximum PIT
631 * delta is quite small.
632 *
633 * - There are pending events on sleeping CPUs which were not
634 * in the event mask
635 */
636 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800637 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100638 * Rearm the broadcast device. If event expired,
639 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800640 */
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100641 if (tick_broadcast_set_event(dev, next_cpu, next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800642 goto again;
643 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100644 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800645}
646
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530647static int broadcast_needs_cpu(struct clock_event_device *bc, int cpu)
648{
649 if (!(bc->features & CLOCK_EVT_FEAT_HRTIMER))
650 return 0;
651 if (bc->next_event.tv64 == KTIME_MAX)
652 return 0;
653 return bc->bound_on == cpu ? -EBUSY : 0;
654}
655
656static void broadcast_shutdown_local(struct clock_event_device *bc,
657 struct clock_event_device *dev)
658{
659 /*
660 * For hrtimer based broadcasting we cannot shutdown the cpu
661 * local device if our own event is the first one to expire or
662 * if we own the broadcast timer.
663 */
664 if (bc->features & CLOCK_EVT_FEAT_HRTIMER) {
665 if (broadcast_needs_cpu(bc, smp_processor_id()))
666 return;
667 if (dev->next_event.tv64 < bc->next_event.tv64)
668 return;
669 }
Viresh Kumar77e32c82015-02-27 17:21:33 +0530670 clockevents_set_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530671}
672
673static void broadcast_move_bc(int deadcpu)
674{
675 struct clock_event_device *bc = tick_broadcast_device.evtdev;
676
677 if (!bc || !broadcast_needs_cpu(bc, deadcpu))
678 return;
679 /* This moves the broadcast assignment to this cpu */
680 clockevents_program_event(bc, bc->next_event, 1);
681}
682
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800683/*
684 * Powerstate information: The system enters/leaves a state, where
685 * affected devices might stop
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530686 * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800687 */
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530688int tick_broadcast_oneshot_control(unsigned long reason)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800689{
690 struct clock_event_device *bc, *dev;
691 struct tick_device *td;
692 unsigned long flags;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000693 ktime_t now;
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530694 int cpu, ret = 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800695
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800696 /*
697 * Periodic mode does not care about the enter/exit of power
698 * states
699 */
700 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530701 return 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800702
Andi Kleen7372b0b2011-05-04 15:09:27 -0700703 /*
704 * We are called with preemtion disabled from the depth of the
705 * idle code, so we can't be moved away.
706 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800707 cpu = smp_processor_id();
708 td = &per_cpu(tick_cpu_device, cpu);
709 dev = td->evtdev;
710
711 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530712 return 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800713
Andi Kleen7372b0b2011-05-04 15:09:27 -0700714 bc = tick_broadcast_device.evtdev;
715
716 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800717 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100718 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner2938d272013-05-28 09:33:01 +0200719 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530720 broadcast_shutdown_local(bc, dev);
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000721 /*
722 * We only reprogram the broadcast timer if we
723 * did not mark ourself in the force mask and
724 * if the cpu local event is earlier than the
725 * broadcast event. If the current CPU is in
726 * the force mask, then we are going to be
727 * woken by the IPI right away.
728 */
729 if (!cpumask_test_cpu(cpu, tick_broadcast_force_mask) &&
730 dev->next_event.tv64 < bc->next_event.tv64)
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100731 tick_broadcast_set_event(bc, cpu, dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800732 }
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530733 /*
734 * If the current CPU owns the hrtimer broadcast
735 * mechanism, it cannot go deep idle and we remove the
736 * CPU from the broadcast mask. We don't have to go
737 * through the EXIT path as the local timer is not
738 * shutdown.
739 */
740 ret = broadcast_needs_cpu(bc, cpu);
741 if (ret)
742 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800743 } else {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100744 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
Viresh Kumar77e32c82015-02-27 17:21:33 +0530745 clockevents_set_state(dev, CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000746 /*
747 * The cpu which was handling the broadcast
748 * timer marked this cpu in the broadcast
749 * pending mask and fired the broadcast
750 * IPI. So we are going to handle the expired
751 * event anyway via the broadcast IPI
752 * handler. No need to reprogram the timer
753 * with an already expired event.
754 */
755 if (cpumask_test_and_clear_cpu(cpu,
756 tick_broadcast_pending_mask))
757 goto out;
758
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000759 /*
Daniel Lezcanoea8deb82013-06-17 18:15:35 +0200760 * Bail out if there is no next event.
761 */
762 if (dev->next_event.tv64 == KTIME_MAX)
763 goto out;
764 /*
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000765 * If the pending bit is not set, then we are
766 * either the CPU handling the broadcast
767 * interrupt or we got woken by something else.
768 *
769 * We are not longer in the broadcast mask, so
770 * if the cpu local expiry time is already
771 * reached, we would reprogram the cpu local
772 * timer with an already expired event.
773 *
774 * This can lead to a ping-pong when we return
775 * to idle and therefor rearm the broadcast
776 * timer before the cpu local timer was able
777 * to fire. This happens because the forced
778 * reprogramming makes sure that the event
779 * will happen in the future and depending on
780 * the min_delta setting this might be far
781 * enough out that the ping-pong starts.
782 *
783 * If the cpu local next_event has expired
784 * then we know that the broadcast timer
785 * next_event has expired as well and
786 * broadcast is about to be handled. So we
787 * avoid reprogramming and enforce that the
788 * broadcast handler, which did not run yet,
789 * will invoke the cpu local handler.
790 *
791 * We cannot call the handler directly from
792 * here, because we might be in a NOHZ phase
793 * and we did not go through the irq_enter()
794 * nohz fixups.
795 */
796 now = ktime_get();
797 if (dev->next_event.tv64 <= now.tv64) {
798 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
799 goto out;
800 }
801 /*
802 * We got woken by something else. Reprogram
803 * the cpu local timer device.
804 */
Thomas Gleixner26517f32013-03-06 11:18:35 +0000805 tick_program_event(dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800806 }
807 }
Thomas Gleixner26517f32013-03-06 11:18:35 +0000808out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100809 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530810 return ret;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800811}
812
Thomas Gleixner5590a532007-07-21 04:37:35 -0700813/*
814 * Reset the one shot broadcast for a cpu
815 *
816 * Called with tick_broadcast_lock held
817 */
818static void tick_broadcast_clear_oneshot(int cpu)
819{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100820 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixnerdd5fd9b2014-02-11 14:35:40 +0100821 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700822}
823
Rusty Russell6b954822009-01-01 10:12:25 +1030824static void tick_broadcast_init_next_event(struct cpumask *mask,
825 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200826{
827 struct tick_device *td;
828 int cpu;
829
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030830 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200831 td = &per_cpu(tick_cpu_device, cpu);
832 if (td->evtdev)
833 td->evtdev->next_event = expires;
834 }
835}
836
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800837/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800838 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800839 */
840void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
841{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200842 int cpu = smp_processor_id();
843
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000844 /* Set it up only once ! */
845 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Viresh Kumar77e32c82015-02-27 17:21:33 +0530846 int was_periodic = bc->state == CLOCK_EVT_STATE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200847
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000848 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200849
Thomas Gleixner73007112008-09-06 03:01:45 +0200850 /*
851 * We must be careful here. There might be other CPUs
852 * waiting for periodic broadcast. We need to set the
853 * oneshot_mask bits for those and program the
854 * broadcast device to fire.
855 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100856 cpumask_copy(tmpmask, tick_broadcast_mask);
857 cpumask_clear_cpu(cpu, tmpmask);
858 cpumask_or(tick_broadcast_oneshot_mask,
859 tick_broadcast_oneshot_mask, tmpmask);
Thomas Gleixner73007112008-09-06 03:01:45 +0200860
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100861 if (was_periodic && !cpumask_empty(tmpmask)) {
Viresh Kumar77e32c82015-02-27 17:21:33 +0530862 clockevents_set_state(bc, CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100863 tick_broadcast_init_next_event(tmpmask,
Rusty Russell6b954822009-01-01 10:12:25 +1030864 tick_next_period);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100865 tick_broadcast_set_event(bc, cpu, tick_next_period, 1);
Thomas Gleixner73007112008-09-06 03:01:45 +0200866 } else
867 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200868 } else {
869 /*
870 * The first cpu which switches to oneshot mode sets
871 * the bit for all other cpus which are in the general
872 * (periodic) broadcast mask. So the bit is set and
873 * would prevent the first broadcast enter after this
874 * to program the bc device.
875 */
876 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000877 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800878}
879
880/*
881 * Select oneshot operating mode for the broadcast device
882 */
883void tick_broadcast_switch_to_oneshot(void)
884{
885 struct clock_event_device *bc;
886 unsigned long flags;
887
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100888 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700889
890 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800891 bc = tick_broadcast_device.evtdev;
892 if (bc)
893 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700894
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100895 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800896}
897
898
899/*
900 * Remove a dead CPU from broadcasting
901 */
902void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
903{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800904 unsigned long flags;
905 unsigned int cpu = *cpup;
906
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100907 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800908
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200909 /*
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200910 * Clear the broadcast masks for the dead cpu, but do not stop
911 * the broadcast device!
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200912 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100913 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200914 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
915 cpumask_clear_cpu(cpu, tick_broadcast_force_mask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800916
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530917 broadcast_move_bc(cpu);
918
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100919 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800920}
921
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200922/*
923 * Check, whether the broadcast device is in one shot mode
924 */
925int tick_broadcast_oneshot_active(void)
926{
927 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
928}
929
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100930/*
931 * Check whether the broadcast device supports oneshot.
932 */
933bool tick_broadcast_oneshot_available(void)
934{
935 struct clock_event_device *bc = tick_broadcast_device.evtdev;
936
937 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
938}
939
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800940#endif
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100941
942void __init tick_broadcast_init(void)
943{
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200944 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200945 zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT);
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200946 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100947#ifdef CONFIG_TICK_ONESHOT
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200948 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
949 zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
950 zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100951#endif
952}