blob: 6d3f91631de62cd94e2c216e96ac56092332e126 [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);
40#else
41static inline void tick_broadcast_clear_oneshot(int cpu) { }
42#endif
43
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080044/*
Ingo Molnar289f4802007-02-16 01:28:15 -080045 * Debugging: see timer_list.c
46 */
47struct tick_device *tick_get_broadcast_device(void)
48{
49 return &tick_broadcast_device;
50}
51
Rusty Russell6b954822009-01-01 10:12:25 +103052struct cpumask *tick_get_broadcast_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -080053{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +010054 return tick_broadcast_mask;
Ingo Molnar289f4802007-02-16 01:28:15 -080055}
56
57/*
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080058 * Start the device in periodic mode
59 */
60static void tick_broadcast_start_periodic(struct clock_event_device *bc)
61{
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070062 if (bc)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080063 tick_setup_periodic(bc, 1);
64}
65
66/*
67 * Check, if the device can be utilized as broadcast device:
68 */
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000069static bool tick_check_broadcast_device(struct clock_event_device *curdev,
70 struct clock_event_device *newdev)
71{
72 if ((newdev->features & CLOCK_EVT_FEAT_DUMMY) ||
73 (newdev->features & CLOCK_EVT_FEAT_C3STOP))
74 return false;
75
76 if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT &&
77 !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
78 return false;
79
80 return !curdev || newdev->rating > curdev->rating;
81}
82
83/*
84 * Conditionally install/replace broadcast device
85 */
Thomas Gleixner7172a282013-04-25 20:31:47 +000086void tick_install_broadcast_device(struct clock_event_device *dev)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080087{
Thomas Gleixner6f7a05d2013-04-25 11:45:53 +020088 struct clock_event_device *cur = tick_broadcast_device.evtdev;
89
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000090 if (!tick_check_broadcast_device(cur, dev))
Thomas Gleixner7172a282013-04-25 20:31:47 +000091 return;
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000092
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000093 if (!try_module_get(dev->owner))
94 return;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080095
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000096 clockevents_exchange_device(cur, dev);
Thomas Gleixner6f7a05d2013-04-25 11:45:53 +020097 if (cur)
98 cur->event_handler = clockevents_handle_noop;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080099 tick_broadcast_device.evtdev = dev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100100 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800101 tick_broadcast_start_periodic(dev);
Stephen Boydc038c1c2013-04-17 10:26:06 -0700102 /*
103 * Inform all cpus about this. We might be in a situation
104 * where we did not switch to oneshot mode because the per cpu
105 * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
106 * of a oneshot capable broadcast device. Without that
107 * notification the systems stays stuck in periodic mode
108 * forever.
109 */
110 if (dev->features & CLOCK_EVT_FEAT_ONESHOT)
111 tick_clock_notify();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800112}
113
114/*
115 * Check, if the device is the broadcast device
116 */
117int tick_is_broadcast_device(struct clock_event_device *dev)
118{
119 return (dev && tick_broadcast_device.evtdev == dev);
120}
121
Mark Rutland12ad1002013-01-14 17:05:22 +0000122static void err_broadcast(const struct cpumask *mask)
123{
124 pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
125}
126
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000127static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
128{
129 if (!dev->broadcast)
130 dev->broadcast = tick_broadcast;
131 if (!dev->broadcast) {
132 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
133 dev->name);
134 dev->broadcast = err_broadcast;
135 }
136}
137
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800138/*
139 * Check, if the device is disfunctional and a place holder, which
140 * needs to be handled by the broadcast device.
141 */
142int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
143{
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200144 struct clock_event_device *bc = tick_broadcast_device.evtdev;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800145 unsigned long flags;
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200146 int ret;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800147
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100148 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800149
150 /*
151 * Devices might be registered with both periodic and oneshot
152 * mode disabled. This signals, that the device needs to be
153 * operated from the broadcast device and is a placeholder for
154 * the cpu local device.
155 */
156 if (!tick_device_is_functional(dev)) {
157 dev->event_handler = tick_handle_periodic;
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000158 tick_device_setup_broadcast_func(dev);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100159 cpumask_set_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200160 tick_broadcast_start_periodic(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800161 ret = 1;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700162 } else {
163 /*
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200164 * Clear the broadcast bit for this cpu if the
165 * device is not power state affected.
Thomas Gleixner5590a532007-07-21 04:37:35 -0700166 */
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200167 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100168 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200169 else
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000170 tick_device_setup_broadcast_func(dev);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200171
172 /*
173 * Clear the broadcast bit if the CPU is not in
174 * periodic broadcast on state.
175 */
176 if (!cpumask_test_cpu(cpu, tick_broadcast_on))
177 cpumask_clear_cpu(cpu, tick_broadcast_mask);
178
179 switch (tick_broadcast_device.mode) {
180 case TICKDEV_MODE_ONESHOT:
181 /*
182 * If the system is in oneshot mode we can
183 * unconditionally clear the oneshot mask bit,
184 * because the CPU is running and therefore
185 * not in an idle state which causes the power
186 * state affected device to stop. Let the
187 * caller initialize the device.
188 */
189 tick_broadcast_clear_oneshot(cpu);
190 ret = 0;
191 break;
192
193 case TICKDEV_MODE_PERIODIC:
194 /*
195 * If the system is in periodic mode, check
196 * whether the broadcast device can be
197 * switched off now.
198 */
199 if (cpumask_empty(tick_broadcast_mask) && bc)
200 clockevents_shutdown(bc);
201 /*
202 * If we kept the cpu in the broadcast mask,
203 * tell the caller to leave the per cpu device
204 * in shutdown state. The periodic interrupt
205 * is delivered by the broadcast device.
206 */
207 ret = cpumask_test_cpu(cpu, tick_broadcast_mask);
208 break;
209 default:
210 /* Nothing to do */
211 ret = 0;
212 break;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700213 }
214 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100215 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800216 return ret;
217}
218
Mark Rutland12572db2013-01-14 17:05:21 +0000219#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
220int tick_receive_broadcast(void)
221{
222 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
223 struct clock_event_device *evt = td->evtdev;
224
225 if (!evt)
226 return -ENODEV;
227
228 if (!evt->event_handler)
229 return -EINVAL;
230
231 evt->event_handler(evt);
232 return 0;
233}
234#endif
235
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800236/*
Rusty Russell6b954822009-01-01 10:12:25 +1030237 * Broadcast the event to the cpus, which are set in the mask (mangled).
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800238 */
Rusty Russell6b954822009-01-01 10:12:25 +1030239static void tick_do_broadcast(struct cpumask *mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800240{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100241 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800242 struct tick_device *td;
243
244 /*
245 * Check, if the current cpu is in the mask
246 */
Rusty Russell6b954822009-01-01 10:12:25 +1030247 if (cpumask_test_cpu(cpu, mask)) {
248 cpumask_clear_cpu(cpu, mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800249 td = &per_cpu(tick_cpu_device, cpu);
250 td->evtdev->event_handler(td->evtdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800251 }
252
Rusty Russell6b954822009-01-01 10:12:25 +1030253 if (!cpumask_empty(mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800254 /*
255 * It might be necessary to actually check whether the devices
256 * have different broadcast functions. For now, just use the
257 * one of the first device. This works as long as we have this
258 * misfeature only on x86 (lapic)
259 */
Rusty Russell6b954822009-01-01 10:12:25 +1030260 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
261 td->evtdev->broadcast(mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800262 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800263}
264
265/*
266 * Periodic broadcast:
267 * - invoke the broadcast handlers
268 */
269static void tick_do_periodic_broadcast(void)
270{
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100271 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800272
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100273 cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
274 tick_do_broadcast(tmpmask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800275
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100276 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800277}
278
279/*
280 * Event handler for periodic broadcast ticks
281 */
282static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
283{
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000284 ktime_t next;
285
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800286 tick_do_periodic_broadcast();
287
288 /*
289 * The device is in periodic mode. No reprogramming necessary:
290 */
291 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
292 return;
293
294 /*
295 * Setup the next period for devices, which do not have
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000296 * periodic mode. We read dev->next_event first and add to it
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200297 * when the event already expired. clockevents_program_event()
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000298 * sets dev->next_event only when the event is really
299 * programmed to the device.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800300 */
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000301 for (next = dev->next_event; ;) {
302 next = ktime_add(next, tick_period);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800303
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200304 if (!clockevents_program_event(dev, next, false))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800305 return;
306 tick_do_periodic_broadcast();
307 }
308}
309
310/*
311 * Powerstate information: The system enters/leaves a state, where
312 * affected devices might stop
313 */
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700314static void tick_do_broadcast_on_off(unsigned long *reason)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800315{
316 struct clock_event_device *bc, *dev;
317 struct tick_device *td;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700318 unsigned long flags;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000319 int cpu, bc_stopped;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800320
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100321 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800322
323 cpu = smp_processor_id();
324 td = &per_cpu(tick_cpu_device, cpu);
325 dev = td->evtdev;
326 bc = tick_broadcast_device.evtdev;
327
328 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200329 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800330 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200331 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800332 goto out;
333
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200334 if (!tick_device_is_functional(dev))
335 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200336
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100337 bc_stopped = cpumask_empty(tick_broadcast_mask);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000338
Thomas Gleixner1595f452007-10-14 22:57:45 +0200339 switch (*reason) {
340 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
341 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200342 cpumask_set_cpu(cpu, tick_broadcast_on);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100343 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200344 if (tick_broadcast_device.mode ==
345 TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700346 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800347 }
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200348 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200349 tick_broadcast_force = 1;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200350 break;
351 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200352 if (tick_broadcast_force)
353 break;
354 cpumask_clear_cpu(cpu, tick_broadcast_on);
355 if (!tick_device_is_functional(dev))
356 break;
357 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200358 if (tick_broadcast_device.mode ==
359 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800360 tick_setup_periodic(dev, 0);
361 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200362 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800363 }
364
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100365 if (cpumask_empty(tick_broadcast_mask)) {
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000366 if (!bc_stopped)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700367 clockevents_shutdown(bc);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000368 } else if (bc_stopped) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800369 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
370 tick_broadcast_start_periodic(bc);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800371 else
372 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800373 }
374out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100375 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800376}
377
378/*
379 * Powerstate information: The system enters/leaves a state, where
380 * affected devices might stop.
381 */
382void tick_broadcast_on_off(unsigned long reason, int *oncpu)
383{
Rusty Russell6b954822009-01-01 10:12:25 +1030384 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
Glauber Costa833df312008-04-18 13:38:58 -0700385 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
Thomas Gleixner72fcde92007-05-23 13:57:30 -0700386 "offline CPU #%d\n", *oncpu);
Avi Kivitybf020cb2007-10-16 23:26:24 -0700387 else
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700388 tick_do_broadcast_on_off(&reason);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800389}
390
391/*
392 * Set the periodic handler depending on broadcast on/off
393 */
394void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
395{
396 if (!broadcast)
397 dev->event_handler = tick_handle_periodic;
398 else
399 dev->event_handler = tick_handle_periodic_broadcast;
400}
401
402/*
403 * Remove a CPU from broadcasting
404 */
405void tick_shutdown_broadcast(unsigned int *cpup)
406{
407 struct clock_event_device *bc;
408 unsigned long flags;
409 unsigned int cpu = *cpup;
410
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100411 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800412
413 bc = tick_broadcast_device.evtdev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100414 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200415 cpumask_clear_cpu(cpu, tick_broadcast_on);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800416
417 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100418 if (bc && cpumask_empty(tick_broadcast_mask))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700419 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800420 }
421
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100422 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800423}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800424
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100425void tick_suspend_broadcast(void)
426{
427 struct clock_event_device *bc;
428 unsigned long flags;
429
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100430 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100431
432 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700433 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700434 clockevents_shutdown(bc);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100435
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100436 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100437}
438
439int tick_resume_broadcast(void)
440{
441 struct clock_event_device *bc;
442 unsigned long flags;
443 int broadcast = 0;
444
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100445 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100446
447 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100448
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100449 if (bc) {
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700450 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
451
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100452 switch (tick_broadcast_device.mode) {
453 case TICKDEV_MODE_PERIODIC:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100454 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100455 tick_broadcast_start_periodic(bc);
Rusty Russell6b954822009-01-01 10:12:25 +1030456 broadcast = cpumask_test_cpu(smp_processor_id(),
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100457 tick_broadcast_mask);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100458 break;
459 case TICKDEV_MODE_ONESHOT:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100460 if (!cpumask_empty(tick_broadcast_mask))
Suresh Siddhaa6371f82012-04-18 19:27:39 -0700461 broadcast = tick_resume_broadcast_oneshot(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100462 break;
463 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100464 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100465 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100466
467 return broadcast;
468}
469
470
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800471#ifdef CONFIG_TICK_ONESHOT
472
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100473static cpumask_var_t tick_broadcast_oneshot_mask;
Thomas Gleixner26517f32013-03-06 11:18:35 +0000474static cpumask_var_t tick_broadcast_pending_mask;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000475static cpumask_var_t tick_broadcast_force_mask;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800476
Ingo Molnar289f4802007-02-16 01:28:15 -0800477/*
Rusty Russell6b954822009-01-01 10:12:25 +1030478 * Exposed for debugging: see timer_list.c
Ingo Molnar289f4802007-02-16 01:28:15 -0800479 */
Rusty Russell6b954822009-01-01 10:12:25 +1030480struct cpumask *tick_get_broadcast_oneshot_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -0800481{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100482 return tick_broadcast_oneshot_mask;
Ingo Molnar289f4802007-02-16 01:28:15 -0800483}
484
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100485/*
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000486 * Called before going idle with interrupts disabled. Checks whether a
487 * broadcast event from the other core is about to happen. We detected
488 * that in tick_broadcast_oneshot_control(). The callsite can use this
489 * to avoid a deep idle transition as we are about to get the
490 * broadcast IPI right away.
491 */
492int tick_check_broadcast_expired(void)
493{
494 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
495}
496
497/*
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100498 * Set broadcast interrupt affinity
499 */
500static void tick_broadcast_set_affinity(struct clock_event_device *bc,
501 const struct cpumask *cpumask)
502{
503 if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
504 return;
505
506 if (cpumask_equal(bc->cpumask, cpumask))
507 return;
508
509 bc->cpumask = cpumask;
510 irq_set_affinity(bc->irq, bc->cpumask);
511}
512
513static int tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
Daniel Lezcanof9ae39d2013-03-02 11:10:10 +0100514 ktime_t expires, int force)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800515{
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100516 int ret;
517
Thomas Gleixnerb9a6a2352012-04-18 17:31:58 +0200518 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
519 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
520
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100521 ret = clockevents_program_event(bc, expires, force);
522 if (!ret)
523 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
524 return ret;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800525}
526
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100527int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
528{
529 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb7e113d2007-09-22 22:29:06 +0000530 return 0;
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100531}
532
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800533/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200534 * Called from irq_enter() when idle was interrupted to reenable the
535 * per cpu device.
536 */
537void tick_check_oneshot_broadcast(int cpu)
538{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100539 if (cpumask_test_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200540 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
541
Thomas Gleixner1f73a982013-07-01 22:14:10 +0200542 /*
543 * We might be in the middle of switching over from
544 * periodic to oneshot. If the CPU has not yet
545 * switched over, leave the device alone.
546 */
547 if (td->mode == TICKDEV_MODE_ONESHOT) {
548 clockevents_set_mode(td->evtdev,
549 CLOCK_EVT_MODE_ONESHOT);
550 }
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200551 }
552}
553
554/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800555 * Handle oneshot mode broadcasting
556 */
557static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
558{
559 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100560 ktime_t now, next_event;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100561 int cpu, next_cpu = 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800562
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100563 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800564again:
565 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100566 next_event.tv64 = KTIME_MAX;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100567 cpumask_clear(tmpmask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800568 now = ktime_get();
569 /* Find all expired events */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100570 for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800571 td = &per_cpu(tick_cpu_device, cpu);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100572 if (td->evtdev->next_event.tv64 <= now.tv64) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100573 cpumask_set_cpu(cpu, tmpmask);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000574 /*
575 * Mark the remote cpu in the pending mask, so
576 * it can avoid reprogramming the cpu local
577 * timer in tick_broadcast_oneshot_control().
578 */
579 cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100580 } else if (td->evtdev->next_event.tv64 < next_event.tv64) {
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100581 next_event.tv64 = td->evtdev->next_event.tv64;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100582 next_cpu = cpu;
583 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800584 }
585
Thomas Gleixner2938d272013-05-28 09:33:01 +0200586 /*
587 * Remove the current cpu from the pending mask. The event is
588 * delivered immediately in tick_do_broadcast() !
589 */
590 cpumask_clear_cpu(smp_processor_id(), tick_broadcast_pending_mask);
591
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000592 /* Take care of enforced broadcast requests */
593 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
594 cpumask_clear(tick_broadcast_force_mask);
595
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800596 /*
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200597 * Sanity check. Catch the case where we try to broadcast to
598 * offline cpus.
599 */
600 if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask)))
601 cpumask_and(tmpmask, tmpmask, cpu_online_mask);
602
603 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100604 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800605 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100606 tick_do_broadcast(tmpmask);
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100607
608 /*
609 * Two reasons for reprogram:
610 *
611 * - The global event did not expire any CPU local
612 * events. This happens in dyntick mode, as the maximum PIT
613 * delta is quite small.
614 *
615 * - There are pending events on sleeping CPUs which were not
616 * in the event mask
617 */
618 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800619 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100620 * Rearm the broadcast device. If event expired,
621 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800622 */
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100623 if (tick_broadcast_set_event(dev, next_cpu, next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800624 goto again;
625 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100626 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800627}
628
629/*
630 * Powerstate information: The system enters/leaves a state, where
631 * affected devices might stop
632 */
633void tick_broadcast_oneshot_control(unsigned long reason)
634{
635 struct clock_event_device *bc, *dev;
636 struct tick_device *td;
637 unsigned long flags;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000638 ktime_t now;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800639 int cpu;
640
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800641 /*
642 * Periodic mode does not care about the enter/exit of power
643 * states
644 */
645 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Andi Kleen7372b0b2011-05-04 15:09:27 -0700646 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800647
Andi Kleen7372b0b2011-05-04 15:09:27 -0700648 /*
649 * We are called with preemtion disabled from the depth of the
650 * idle code, so we can't be moved away.
651 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800652 cpu = smp_processor_id();
653 td = &per_cpu(tick_cpu_device, cpu);
654 dev = td->evtdev;
655
656 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Andi Kleen7372b0b2011-05-04 15:09:27 -0700657 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800658
Andi Kleen7372b0b2011-05-04 15:09:27 -0700659 bc = tick_broadcast_device.evtdev;
660
661 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800662 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100663 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner2938d272013-05-28 09:33:01 +0200664 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800665 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000666 /*
667 * We only reprogram the broadcast timer if we
668 * did not mark ourself in the force mask and
669 * if the cpu local event is earlier than the
670 * broadcast event. If the current CPU is in
671 * the force mask, then we are going to be
672 * woken by the IPI right away.
673 */
674 if (!cpumask_test_cpu(cpu, tick_broadcast_force_mask) &&
675 dev->next_event.tv64 < bc->next_event.tv64)
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100676 tick_broadcast_set_event(bc, cpu, dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800677 }
678 } else {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100679 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800680 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000681 /*
682 * The cpu which was handling the broadcast
683 * timer marked this cpu in the broadcast
684 * pending mask and fired the broadcast
685 * IPI. So we are going to handle the expired
686 * event anyway via the broadcast IPI
687 * handler. No need to reprogram the timer
688 * with an already expired event.
689 */
690 if (cpumask_test_and_clear_cpu(cpu,
691 tick_broadcast_pending_mask))
692 goto out;
693
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000694 /*
Daniel Lezcanoea8deb82013-06-17 18:15:35 +0200695 * Bail out if there is no next event.
696 */
697 if (dev->next_event.tv64 == KTIME_MAX)
698 goto out;
699 /*
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000700 * If the pending bit is not set, then we are
701 * either the CPU handling the broadcast
702 * interrupt or we got woken by something else.
703 *
704 * We are not longer in the broadcast mask, so
705 * if the cpu local expiry time is already
706 * reached, we would reprogram the cpu local
707 * timer with an already expired event.
708 *
709 * This can lead to a ping-pong when we return
710 * to idle and therefor rearm the broadcast
711 * timer before the cpu local timer was able
712 * to fire. This happens because the forced
713 * reprogramming makes sure that the event
714 * will happen in the future and depending on
715 * the min_delta setting this might be far
716 * enough out that the ping-pong starts.
717 *
718 * If the cpu local next_event has expired
719 * then we know that the broadcast timer
720 * next_event has expired as well and
721 * broadcast is about to be handled. So we
722 * avoid reprogramming and enforce that the
723 * broadcast handler, which did not run yet,
724 * will invoke the cpu local handler.
725 *
726 * We cannot call the handler directly from
727 * here, because we might be in a NOHZ phase
728 * and we did not go through the irq_enter()
729 * nohz fixups.
730 */
731 now = ktime_get();
732 if (dev->next_event.tv64 <= now.tv64) {
733 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
734 goto out;
735 }
736 /*
737 * We got woken by something else. Reprogram
738 * the cpu local timer device.
739 */
Thomas Gleixner26517f32013-03-06 11:18:35 +0000740 tick_program_event(dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800741 }
742 }
Thomas Gleixner26517f32013-03-06 11:18:35 +0000743out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100744 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800745}
746
Thomas Gleixner5590a532007-07-21 04:37:35 -0700747/*
748 * Reset the one shot broadcast for a cpu
749 *
750 * Called with tick_broadcast_lock held
751 */
752static void tick_broadcast_clear_oneshot(int cpu)
753{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100754 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700755}
756
Rusty Russell6b954822009-01-01 10:12:25 +1030757static void tick_broadcast_init_next_event(struct cpumask *mask,
758 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200759{
760 struct tick_device *td;
761 int cpu;
762
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030763 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200764 td = &per_cpu(tick_cpu_device, cpu);
765 if (td->evtdev)
766 td->evtdev->next_event = expires;
767 }
768}
769
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800770/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800771 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800772 */
773void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
774{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200775 int cpu = smp_processor_id();
776
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000777 /* Set it up only once ! */
778 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200779 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200780
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000781 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200782
Thomas Gleixner73007112008-09-06 03:01:45 +0200783 /*
784 * We must be careful here. There might be other CPUs
785 * waiting for periodic broadcast. We need to set the
786 * oneshot_mask bits for those and program the
787 * broadcast device to fire.
788 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100789 cpumask_copy(tmpmask, tick_broadcast_mask);
790 cpumask_clear_cpu(cpu, tmpmask);
791 cpumask_or(tick_broadcast_oneshot_mask,
792 tick_broadcast_oneshot_mask, tmpmask);
Thomas Gleixner73007112008-09-06 03:01:45 +0200793
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100794 if (was_periodic && !cpumask_empty(tmpmask)) {
Thomas Gleixnerb4350922012-04-18 12:08:23 +0200795 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100796 tick_broadcast_init_next_event(tmpmask,
Rusty Russell6b954822009-01-01 10:12:25 +1030797 tick_next_period);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100798 tick_broadcast_set_event(bc, cpu, tick_next_period, 1);
Thomas Gleixner73007112008-09-06 03:01:45 +0200799 } else
800 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200801 } else {
802 /*
803 * The first cpu which switches to oneshot mode sets
804 * the bit for all other cpus which are in the general
805 * (periodic) broadcast mask. So the bit is set and
806 * would prevent the first broadcast enter after this
807 * to program the bc device.
808 */
809 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000810 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800811}
812
813/*
814 * Select oneshot operating mode for the broadcast device
815 */
816void tick_broadcast_switch_to_oneshot(void)
817{
818 struct clock_event_device *bc;
819 unsigned long flags;
820
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100821 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700822
823 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800824 bc = tick_broadcast_device.evtdev;
825 if (bc)
826 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700827
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100828 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800829}
830
831
832/*
833 * Remove a dead CPU from broadcasting
834 */
835void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
836{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800837 unsigned long flags;
838 unsigned int cpu = *cpup;
839
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100840 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800841
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200842 /*
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200843 * Clear the broadcast masks for the dead cpu, but do not stop
844 * the broadcast device!
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200845 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100846 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200847 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
848 cpumask_clear_cpu(cpu, tick_broadcast_force_mask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800849
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100850 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800851}
852
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200853/*
854 * Check, whether the broadcast device is in one shot mode
855 */
856int tick_broadcast_oneshot_active(void)
857{
858 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
859}
860
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100861/*
862 * Check whether the broadcast device supports oneshot.
863 */
864bool tick_broadcast_oneshot_available(void)
865{
866 struct clock_event_device *bc = tick_broadcast_device.evtdev;
867
868 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
869}
870
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800871#endif
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100872
873void __init tick_broadcast_init(void)
874{
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200875 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200876 zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT);
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200877 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100878#ifdef CONFIG_TICK_ONESHOT
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200879 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
880 zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
881 zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100882#endif
883}