blob: 4430fa695b48edbeadc632d06be8b922d6426ba6 [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 Gleixner989dcb62013-03-06 11:18:35 +0000586 /* Take care of enforced broadcast requests */
587 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
588 cpumask_clear(tick_broadcast_force_mask);
589
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800590 /*
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200591 * Sanity check. Catch the case where we try to broadcast to
592 * offline cpus.
593 */
594 if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask)))
595 cpumask_and(tmpmask, tmpmask, cpu_online_mask);
596
597 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100598 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800599 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100600 tick_do_broadcast(tmpmask);
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100601
602 /*
603 * Two reasons for reprogram:
604 *
605 * - The global event did not expire any CPU local
606 * events. This happens in dyntick mode, as the maximum PIT
607 * delta is quite small.
608 *
609 * - There are pending events on sleeping CPUs which were not
610 * in the event mask
611 */
612 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800613 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100614 * Rearm the broadcast device. If event expired,
615 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800616 */
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100617 if (tick_broadcast_set_event(dev, next_cpu, next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800618 goto again;
619 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100620 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800621}
622
623/*
624 * Powerstate information: The system enters/leaves a state, where
625 * affected devices might stop
626 */
627void tick_broadcast_oneshot_control(unsigned long reason)
628{
629 struct clock_event_device *bc, *dev;
630 struct tick_device *td;
631 unsigned long flags;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000632 ktime_t now;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800633 int cpu;
634
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800635 /*
636 * Periodic mode does not care about the enter/exit of power
637 * states
638 */
639 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Andi Kleen7372b0b2011-05-04 15:09:27 -0700640 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800641
Andi Kleen7372b0b2011-05-04 15:09:27 -0700642 /*
643 * We are called with preemtion disabled from the depth of the
644 * idle code, so we can't be moved away.
645 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800646 cpu = smp_processor_id();
647 td = &per_cpu(tick_cpu_device, cpu);
648 dev = td->evtdev;
649
650 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Andi Kleen7372b0b2011-05-04 15:09:27 -0700651 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800652
Andi Kleen7372b0b2011-05-04 15:09:27 -0700653 bc = tick_broadcast_device.evtdev;
654
655 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800656 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Thomas Gleixner26517f32013-03-06 11:18:35 +0000657 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100658 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800659 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000660 /*
661 * We only reprogram the broadcast timer if we
662 * did not mark ourself in the force mask and
663 * if the cpu local event is earlier than the
664 * broadcast event. If the current CPU is in
665 * the force mask, then we are going to be
666 * woken by the IPI right away.
667 */
668 if (!cpumask_test_cpu(cpu, tick_broadcast_force_mask) &&
669 dev->next_event.tv64 < bc->next_event.tv64)
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100670 tick_broadcast_set_event(bc, cpu, dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800671 }
672 } else {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100673 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800674 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000675 if (dev->next_event.tv64 == KTIME_MAX)
676 goto out;
677 /*
678 * The cpu which was handling the broadcast
679 * timer marked this cpu in the broadcast
680 * pending mask and fired the broadcast
681 * IPI. So we are going to handle the expired
682 * event anyway via the broadcast IPI
683 * handler. No need to reprogram the timer
684 * with an already expired event.
685 */
686 if (cpumask_test_and_clear_cpu(cpu,
687 tick_broadcast_pending_mask))
688 goto out;
689
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000690 /*
691 * If the pending bit is not set, then we are
692 * either the CPU handling the broadcast
693 * interrupt or we got woken by something else.
694 *
695 * We are not longer in the broadcast mask, so
696 * if the cpu local expiry time is already
697 * reached, we would reprogram the cpu local
698 * timer with an already expired event.
699 *
700 * This can lead to a ping-pong when we return
701 * to idle and therefor rearm the broadcast
702 * timer before the cpu local timer was able
703 * to fire. This happens because the forced
704 * reprogramming makes sure that the event
705 * will happen in the future and depending on
706 * the min_delta setting this might be far
707 * enough out that the ping-pong starts.
708 *
709 * If the cpu local next_event has expired
710 * then we know that the broadcast timer
711 * next_event has expired as well and
712 * broadcast is about to be handled. So we
713 * avoid reprogramming and enforce that the
714 * broadcast handler, which did not run yet,
715 * will invoke the cpu local handler.
716 *
717 * We cannot call the handler directly from
718 * here, because we might be in a NOHZ phase
719 * and we did not go through the irq_enter()
720 * nohz fixups.
721 */
722 now = ktime_get();
723 if (dev->next_event.tv64 <= now.tv64) {
724 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
725 goto out;
726 }
727 /*
728 * We got woken by something else. Reprogram
729 * the cpu local timer device.
730 */
Thomas Gleixner26517f32013-03-06 11:18:35 +0000731 tick_program_event(dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800732 }
733 }
Thomas Gleixner26517f32013-03-06 11:18:35 +0000734out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100735 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800736}
737
Thomas Gleixner5590a532007-07-21 04:37:35 -0700738/*
739 * Reset the one shot broadcast for a cpu
740 *
741 * Called with tick_broadcast_lock held
742 */
743static void tick_broadcast_clear_oneshot(int cpu)
744{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100745 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700746}
747
Rusty Russell6b954822009-01-01 10:12:25 +1030748static void tick_broadcast_init_next_event(struct cpumask *mask,
749 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200750{
751 struct tick_device *td;
752 int cpu;
753
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030754 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200755 td = &per_cpu(tick_cpu_device, cpu);
756 if (td->evtdev)
757 td->evtdev->next_event = expires;
758 }
759}
760
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800761/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800762 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800763 */
764void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
765{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200766 int cpu = smp_processor_id();
767
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000768 /* Set it up only once ! */
769 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200770 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200771
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000772 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200773
774 /* Take the do_timer update */
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200775 if (!tick_nohz_full_cpu(cpu))
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100776 tick_do_timer_cpu = cpu;
Thomas Gleixner73007112008-09-06 03:01:45 +0200777
778 /*
779 * We must be careful here. There might be other CPUs
780 * waiting for periodic broadcast. We need to set the
781 * oneshot_mask bits for those and program the
782 * broadcast device to fire.
783 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100784 cpumask_copy(tmpmask, tick_broadcast_mask);
785 cpumask_clear_cpu(cpu, tmpmask);
786 cpumask_or(tick_broadcast_oneshot_mask,
787 tick_broadcast_oneshot_mask, tmpmask);
Thomas Gleixner73007112008-09-06 03:01:45 +0200788
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100789 if (was_periodic && !cpumask_empty(tmpmask)) {
Thomas Gleixnerb4350922012-04-18 12:08:23 +0200790 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100791 tick_broadcast_init_next_event(tmpmask,
Rusty Russell6b954822009-01-01 10:12:25 +1030792 tick_next_period);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100793 tick_broadcast_set_event(bc, cpu, tick_next_period, 1);
Thomas Gleixner73007112008-09-06 03:01:45 +0200794 } else
795 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200796 } else {
797 /*
798 * The first cpu which switches to oneshot mode sets
799 * the bit for all other cpus which are in the general
800 * (periodic) broadcast mask. So the bit is set and
801 * would prevent the first broadcast enter after this
802 * to program the bc device.
803 */
804 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000805 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800806}
807
808/*
809 * Select oneshot operating mode for the broadcast device
810 */
811void tick_broadcast_switch_to_oneshot(void)
812{
813 struct clock_event_device *bc;
814 unsigned long flags;
815
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100816 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700817
818 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800819 bc = tick_broadcast_device.evtdev;
820 if (bc)
821 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700822
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100823 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800824}
825
826
827/*
828 * Remove a dead CPU from broadcasting
829 */
830void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
831{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800832 unsigned long flags;
833 unsigned int cpu = *cpup;
834
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100835 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800836
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200837 /*
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200838 * Clear the broadcast masks for the dead cpu, but do not stop
839 * the broadcast device!
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200840 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100841 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200842 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
843 cpumask_clear_cpu(cpu, tick_broadcast_force_mask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800844
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100845 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800846}
847
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200848/*
849 * Check, whether the broadcast device is in one shot mode
850 */
851int tick_broadcast_oneshot_active(void)
852{
853 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
854}
855
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100856/*
857 * Check whether the broadcast device supports oneshot.
858 */
859bool tick_broadcast_oneshot_available(void)
860{
861 struct clock_event_device *bc = tick_broadcast_device.evtdev;
862
863 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
864}
865
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800866#endif
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100867
868void __init tick_broadcast_init(void)
869{
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200870 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200871 zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT);
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200872 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100873#ifdef CONFIG_TICK_ONESHOT
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200874 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
875 zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
876 zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100877#endif
878}