blob: 218bcb565fed0f6e8f867b9e3e2264812d9797bf [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);
Stephen Boyda272dcc2013-07-11 07:00:59 -0700160 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
161 tick_broadcast_start_periodic(bc);
162 else
163 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800164 ret = 1;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700165 } else {
166 /*
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200167 * Clear the broadcast bit for this cpu if the
168 * device is not power state affected.
Thomas Gleixner5590a532007-07-21 04:37:35 -0700169 */
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200170 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100171 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200172 else
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000173 tick_device_setup_broadcast_func(dev);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200174
175 /*
176 * Clear the broadcast bit if the CPU is not in
177 * periodic broadcast on state.
178 */
179 if (!cpumask_test_cpu(cpu, tick_broadcast_on))
180 cpumask_clear_cpu(cpu, tick_broadcast_mask);
181
182 switch (tick_broadcast_device.mode) {
183 case TICKDEV_MODE_ONESHOT:
184 /*
185 * If the system is in oneshot mode we can
186 * unconditionally clear the oneshot mask bit,
187 * because the CPU is running and therefore
188 * not in an idle state which causes the power
189 * state affected device to stop. Let the
190 * caller initialize the device.
191 */
192 tick_broadcast_clear_oneshot(cpu);
193 ret = 0;
194 break;
195
196 case TICKDEV_MODE_PERIODIC:
197 /*
198 * If the system is in periodic mode, check
199 * whether the broadcast device can be
200 * switched off now.
201 */
202 if (cpumask_empty(tick_broadcast_mask) && bc)
203 clockevents_shutdown(bc);
204 /*
205 * If we kept the cpu in the broadcast mask,
206 * tell the caller to leave the per cpu device
207 * in shutdown state. The periodic interrupt
208 * is delivered by the broadcast device.
209 */
210 ret = cpumask_test_cpu(cpu, tick_broadcast_mask);
211 break;
212 default:
213 /* Nothing to do */
214 ret = 0;
215 break;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700216 }
217 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100218 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800219 return ret;
220}
221
Mark Rutland12572db2013-01-14 17:05:21 +0000222#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
223int tick_receive_broadcast(void)
224{
225 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
226 struct clock_event_device *evt = td->evtdev;
227
228 if (!evt)
229 return -ENODEV;
230
231 if (!evt->event_handler)
232 return -EINVAL;
233
234 evt->event_handler(evt);
235 return 0;
236}
237#endif
238
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800239/*
Rusty Russell6b954822009-01-01 10:12:25 +1030240 * Broadcast the event to the cpus, which are set in the mask (mangled).
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800241 */
Rusty Russell6b954822009-01-01 10:12:25 +1030242static void tick_do_broadcast(struct cpumask *mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800243{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100244 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800245 struct tick_device *td;
246
247 /*
248 * Check, if the current cpu is in the mask
249 */
Rusty Russell6b954822009-01-01 10:12:25 +1030250 if (cpumask_test_cpu(cpu, mask)) {
251 cpumask_clear_cpu(cpu, mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800252 td = &per_cpu(tick_cpu_device, cpu);
253 td->evtdev->event_handler(td->evtdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800254 }
255
Rusty Russell6b954822009-01-01 10:12:25 +1030256 if (!cpumask_empty(mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800257 /*
258 * It might be necessary to actually check whether the devices
259 * have different broadcast functions. For now, just use the
260 * one of the first device. This works as long as we have this
261 * misfeature only on x86 (lapic)
262 */
Rusty Russell6b954822009-01-01 10:12:25 +1030263 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
264 td->evtdev->broadcast(mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800265 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800266}
267
268/*
269 * Periodic broadcast:
270 * - invoke the broadcast handlers
271 */
272static void tick_do_periodic_broadcast(void)
273{
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100274 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800275
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100276 cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
277 tick_do_broadcast(tmpmask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800278
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100279 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800280}
281
282/*
283 * Event handler for periodic broadcast ticks
284 */
285static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
286{
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000287 ktime_t next;
288
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800289 tick_do_periodic_broadcast();
290
291 /*
292 * The device is in periodic mode. No reprogramming necessary:
293 */
294 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
295 return;
296
297 /*
298 * Setup the next period for devices, which do not have
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000299 * periodic mode. We read dev->next_event first and add to it
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200300 * when the event already expired. clockevents_program_event()
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000301 * sets dev->next_event only when the event is really
302 * programmed to the device.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800303 */
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000304 for (next = dev->next_event; ;) {
305 next = ktime_add(next, tick_period);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800306
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200307 if (!clockevents_program_event(dev, next, false))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800308 return;
309 tick_do_periodic_broadcast();
310 }
311}
312
313/*
314 * Powerstate information: The system enters/leaves a state, where
315 * affected devices might stop
316 */
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700317static void tick_do_broadcast_on_off(unsigned long *reason)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800318{
319 struct clock_event_device *bc, *dev;
320 struct tick_device *td;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700321 unsigned long flags;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000322 int cpu, bc_stopped;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800323
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100324 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800325
326 cpu = smp_processor_id();
327 td = &per_cpu(tick_cpu_device, cpu);
328 dev = td->evtdev;
329 bc = tick_broadcast_device.evtdev;
330
331 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200332 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800333 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200334 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800335 goto out;
336
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200337 if (!tick_device_is_functional(dev))
338 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200339
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100340 bc_stopped = cpumask_empty(tick_broadcast_mask);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000341
Thomas Gleixner1595f452007-10-14 22:57:45 +0200342 switch (*reason) {
343 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
344 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200345 cpumask_set_cpu(cpu, tick_broadcast_on);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100346 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200347 if (tick_broadcast_device.mode ==
348 TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700349 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800350 }
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200351 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200352 tick_broadcast_force = 1;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200353 break;
354 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200355 if (tick_broadcast_force)
356 break;
357 cpumask_clear_cpu(cpu, tick_broadcast_on);
358 if (!tick_device_is_functional(dev))
359 break;
360 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200361 if (tick_broadcast_device.mode ==
362 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800363 tick_setup_periodic(dev, 0);
364 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200365 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800366 }
367
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100368 if (cpumask_empty(tick_broadcast_mask)) {
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000369 if (!bc_stopped)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700370 clockevents_shutdown(bc);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000371 } else if (bc_stopped) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800372 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
373 tick_broadcast_start_periodic(bc);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800374 else
375 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800376 }
377out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100378 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800379}
380
381/*
382 * Powerstate information: The system enters/leaves a state, where
383 * affected devices might stop.
384 */
385void tick_broadcast_on_off(unsigned long reason, int *oncpu)
386{
Rusty Russell6b954822009-01-01 10:12:25 +1030387 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
Glauber Costa833df312008-04-18 13:38:58 -0700388 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
Thomas Gleixner72fcde92007-05-23 13:57:30 -0700389 "offline CPU #%d\n", *oncpu);
Avi Kivitybf020cb2007-10-16 23:26:24 -0700390 else
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700391 tick_do_broadcast_on_off(&reason);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800392}
393
394/*
395 * Set the periodic handler depending on broadcast on/off
396 */
397void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
398{
399 if (!broadcast)
400 dev->event_handler = tick_handle_periodic;
401 else
402 dev->event_handler = tick_handle_periodic_broadcast;
403}
404
405/*
406 * Remove a CPU from broadcasting
407 */
408void tick_shutdown_broadcast(unsigned int *cpup)
409{
410 struct clock_event_device *bc;
411 unsigned long flags;
412 unsigned int cpu = *cpup;
413
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100414 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800415
416 bc = tick_broadcast_device.evtdev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100417 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200418 cpumask_clear_cpu(cpu, tick_broadcast_on);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800419
420 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100421 if (bc && cpumask_empty(tick_broadcast_mask))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700422 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800423 }
424
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100425 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800426}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800427
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100428void tick_suspend_broadcast(void)
429{
430 struct clock_event_device *bc;
431 unsigned long flags;
432
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100433 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100434
435 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700436 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700437 clockevents_shutdown(bc);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100438
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100439 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100440}
441
442int tick_resume_broadcast(void)
443{
444 struct clock_event_device *bc;
445 unsigned long flags;
446 int broadcast = 0;
447
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100448 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100449
450 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100451
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100452 if (bc) {
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700453 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
454
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100455 switch (tick_broadcast_device.mode) {
456 case TICKDEV_MODE_PERIODIC:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100457 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100458 tick_broadcast_start_periodic(bc);
Rusty Russell6b954822009-01-01 10:12:25 +1030459 broadcast = cpumask_test_cpu(smp_processor_id(),
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100460 tick_broadcast_mask);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100461 break;
462 case TICKDEV_MODE_ONESHOT:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100463 if (!cpumask_empty(tick_broadcast_mask))
Suresh Siddhaa6371f82012-04-18 19:27:39 -0700464 broadcast = tick_resume_broadcast_oneshot(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100465 break;
466 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100467 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100468 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100469
470 return broadcast;
471}
472
473
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800474#ifdef CONFIG_TICK_ONESHOT
475
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100476static cpumask_var_t tick_broadcast_oneshot_mask;
Thomas Gleixner26517f32013-03-06 11:18:35 +0000477static cpumask_var_t tick_broadcast_pending_mask;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000478static cpumask_var_t tick_broadcast_force_mask;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800479
Ingo Molnar289f4802007-02-16 01:28:15 -0800480/*
Rusty Russell6b954822009-01-01 10:12:25 +1030481 * Exposed for debugging: see timer_list.c
Ingo Molnar289f4802007-02-16 01:28:15 -0800482 */
Rusty Russell6b954822009-01-01 10:12:25 +1030483struct cpumask *tick_get_broadcast_oneshot_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -0800484{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100485 return tick_broadcast_oneshot_mask;
Ingo Molnar289f4802007-02-16 01:28:15 -0800486}
487
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100488/*
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000489 * Called before going idle with interrupts disabled. Checks whether a
490 * broadcast event from the other core is about to happen. We detected
491 * that in tick_broadcast_oneshot_control(). The callsite can use this
492 * to avoid a deep idle transition as we are about to get the
493 * broadcast IPI right away.
494 */
495int tick_check_broadcast_expired(void)
496{
497 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
498}
499
500/*
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100501 * Set broadcast interrupt affinity
502 */
503static void tick_broadcast_set_affinity(struct clock_event_device *bc,
504 const struct cpumask *cpumask)
505{
506 if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
507 return;
508
509 if (cpumask_equal(bc->cpumask, cpumask))
510 return;
511
512 bc->cpumask = cpumask;
513 irq_set_affinity(bc->irq, bc->cpumask);
514}
515
516static int tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
Daniel Lezcanof9ae39d2013-03-02 11:10:10 +0100517 ktime_t expires, int force)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800518{
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100519 int ret;
520
Thomas Gleixnerb9a6a2352012-04-18 17:31:58 +0200521 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
522 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
523
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100524 ret = clockevents_program_event(bc, expires, force);
525 if (!ret)
526 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
527 return ret;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800528}
529
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100530int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
531{
532 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb7e113d2007-09-22 22:29:06 +0000533 return 0;
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100534}
535
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800536/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200537 * Called from irq_enter() when idle was interrupted to reenable the
538 * per cpu device.
539 */
540void tick_check_oneshot_broadcast(int cpu)
541{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100542 if (cpumask_test_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200543 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
544
Thomas Gleixner1f73a982013-07-01 22:14:10 +0200545 /*
546 * We might be in the middle of switching over from
547 * periodic to oneshot. If the CPU has not yet
548 * switched over, leave the device alone.
549 */
550 if (td->mode == TICKDEV_MODE_ONESHOT) {
551 clockevents_set_mode(td->evtdev,
552 CLOCK_EVT_MODE_ONESHOT);
553 }
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200554 }
555}
556
557/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800558 * Handle oneshot mode broadcasting
559 */
560static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
561{
562 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100563 ktime_t now, next_event;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100564 int cpu, next_cpu = 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800565
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100566 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800567again:
568 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100569 next_event.tv64 = KTIME_MAX;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100570 cpumask_clear(tmpmask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800571 now = ktime_get();
572 /* Find all expired events */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100573 for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800574 td = &per_cpu(tick_cpu_device, cpu);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100575 if (td->evtdev->next_event.tv64 <= now.tv64) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100576 cpumask_set_cpu(cpu, tmpmask);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000577 /*
578 * Mark the remote cpu in the pending mask, so
579 * it can avoid reprogramming the cpu local
580 * timer in tick_broadcast_oneshot_control().
581 */
582 cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100583 } else if (td->evtdev->next_event.tv64 < next_event.tv64) {
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100584 next_event.tv64 = td->evtdev->next_event.tv64;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100585 next_cpu = cpu;
586 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800587 }
588
Thomas Gleixner2938d272013-05-28 09:33:01 +0200589 /*
590 * Remove the current cpu from the pending mask. The event is
591 * delivered immediately in tick_do_broadcast() !
592 */
593 cpumask_clear_cpu(smp_processor_id(), tick_broadcast_pending_mask);
594
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000595 /* Take care of enforced broadcast requests */
596 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
597 cpumask_clear(tick_broadcast_force_mask);
598
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800599 /*
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200600 * Sanity check. Catch the case where we try to broadcast to
601 * offline cpus.
602 */
603 if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask)))
604 cpumask_and(tmpmask, tmpmask, cpu_online_mask);
605
606 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100607 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800608 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100609 tick_do_broadcast(tmpmask);
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100610
611 /*
612 * Two reasons for reprogram:
613 *
614 * - The global event did not expire any CPU local
615 * events. This happens in dyntick mode, as the maximum PIT
616 * delta is quite small.
617 *
618 * - There are pending events on sleeping CPUs which were not
619 * in the event mask
620 */
621 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800622 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100623 * Rearm the broadcast device. If event expired,
624 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800625 */
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100626 if (tick_broadcast_set_event(dev, next_cpu, next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800627 goto again;
628 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100629 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800630}
631
632/*
633 * Powerstate information: The system enters/leaves a state, where
634 * affected devices might stop
635 */
636void tick_broadcast_oneshot_control(unsigned long reason)
637{
638 struct clock_event_device *bc, *dev;
639 struct tick_device *td;
640 unsigned long flags;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000641 ktime_t now;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800642 int cpu;
643
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800644 /*
645 * Periodic mode does not care about the enter/exit of power
646 * states
647 */
648 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Andi Kleen7372b0b2011-05-04 15:09:27 -0700649 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800650
Andi Kleen7372b0b2011-05-04 15:09:27 -0700651 /*
652 * We are called with preemtion disabled from the depth of the
653 * idle code, so we can't be moved away.
654 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800655 cpu = smp_processor_id();
656 td = &per_cpu(tick_cpu_device, cpu);
657 dev = td->evtdev;
658
659 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Andi Kleen7372b0b2011-05-04 15:09:27 -0700660 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800661
Andi Kleen7372b0b2011-05-04 15:09:27 -0700662 bc = tick_broadcast_device.evtdev;
663
664 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800665 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100666 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner2938d272013-05-28 09:33:01 +0200667 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800668 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000669 /*
670 * We only reprogram the broadcast timer if we
671 * did not mark ourself in the force mask and
672 * if the cpu local event is earlier than the
673 * broadcast event. If the current CPU is in
674 * the force mask, then we are going to be
675 * woken by the IPI right away.
676 */
677 if (!cpumask_test_cpu(cpu, tick_broadcast_force_mask) &&
678 dev->next_event.tv64 < bc->next_event.tv64)
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100679 tick_broadcast_set_event(bc, cpu, dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800680 }
681 } else {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100682 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800683 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000684 /*
685 * The cpu which was handling the broadcast
686 * timer marked this cpu in the broadcast
687 * pending mask and fired the broadcast
688 * IPI. So we are going to handle the expired
689 * event anyway via the broadcast IPI
690 * handler. No need to reprogram the timer
691 * with an already expired event.
692 */
693 if (cpumask_test_and_clear_cpu(cpu,
694 tick_broadcast_pending_mask))
695 goto out;
696
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000697 /*
Daniel Lezcanoea8deb82013-06-17 18:15:35 +0200698 * Bail out if there is no next event.
699 */
700 if (dev->next_event.tv64 == KTIME_MAX)
701 goto out;
702 /*
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000703 * If the pending bit is not set, then we are
704 * either the CPU handling the broadcast
705 * interrupt or we got woken by something else.
706 *
707 * We are not longer in the broadcast mask, so
708 * if the cpu local expiry time is already
709 * reached, we would reprogram the cpu local
710 * timer with an already expired event.
711 *
712 * This can lead to a ping-pong when we return
713 * to idle and therefor rearm the broadcast
714 * timer before the cpu local timer was able
715 * to fire. This happens because the forced
716 * reprogramming makes sure that the event
717 * will happen in the future and depending on
718 * the min_delta setting this might be far
719 * enough out that the ping-pong starts.
720 *
721 * If the cpu local next_event has expired
722 * then we know that the broadcast timer
723 * next_event has expired as well and
724 * broadcast is about to be handled. So we
725 * avoid reprogramming and enforce that the
726 * broadcast handler, which did not run yet,
727 * will invoke the cpu local handler.
728 *
729 * We cannot call the handler directly from
730 * here, because we might be in a NOHZ phase
731 * and we did not go through the irq_enter()
732 * nohz fixups.
733 */
734 now = ktime_get();
735 if (dev->next_event.tv64 <= now.tv64) {
736 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
737 goto out;
738 }
739 /*
740 * We got woken by something else. Reprogram
741 * the cpu local timer device.
742 */
Thomas Gleixner26517f32013-03-06 11:18:35 +0000743 tick_program_event(dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800744 }
745 }
Thomas Gleixner26517f32013-03-06 11:18:35 +0000746out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100747 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800748}
749
Thomas Gleixner5590a532007-07-21 04:37:35 -0700750/*
751 * Reset the one shot broadcast for a cpu
752 *
753 * Called with tick_broadcast_lock held
754 */
755static void tick_broadcast_clear_oneshot(int cpu)
756{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100757 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700758}
759
Rusty Russell6b954822009-01-01 10:12:25 +1030760static void tick_broadcast_init_next_event(struct cpumask *mask,
761 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200762{
763 struct tick_device *td;
764 int cpu;
765
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030766 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200767 td = &per_cpu(tick_cpu_device, cpu);
768 if (td->evtdev)
769 td->evtdev->next_event = expires;
770 }
771}
772
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800773/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800774 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800775 */
776void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
777{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200778 int cpu = smp_processor_id();
779
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000780 /* Set it up only once ! */
781 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200782 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200783
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000784 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200785
Thomas Gleixner73007112008-09-06 03:01:45 +0200786 /*
787 * We must be careful here. There might be other CPUs
788 * waiting for periodic broadcast. We need to set the
789 * oneshot_mask bits for those and program the
790 * broadcast device to fire.
791 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100792 cpumask_copy(tmpmask, tick_broadcast_mask);
793 cpumask_clear_cpu(cpu, tmpmask);
794 cpumask_or(tick_broadcast_oneshot_mask,
795 tick_broadcast_oneshot_mask, tmpmask);
Thomas Gleixner73007112008-09-06 03:01:45 +0200796
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100797 if (was_periodic && !cpumask_empty(tmpmask)) {
Thomas Gleixnerb4350922012-04-18 12:08:23 +0200798 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100799 tick_broadcast_init_next_event(tmpmask,
Rusty Russell6b954822009-01-01 10:12:25 +1030800 tick_next_period);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100801 tick_broadcast_set_event(bc, cpu, tick_next_period, 1);
Thomas Gleixner73007112008-09-06 03:01:45 +0200802 } else
803 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200804 } else {
805 /*
806 * The first cpu which switches to oneshot mode sets
807 * the bit for all other cpus which are in the general
808 * (periodic) broadcast mask. So the bit is set and
809 * would prevent the first broadcast enter after this
810 * to program the bc device.
811 */
812 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000813 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800814}
815
816/*
817 * Select oneshot operating mode for the broadcast device
818 */
819void tick_broadcast_switch_to_oneshot(void)
820{
821 struct clock_event_device *bc;
822 unsigned long flags;
823
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100824 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700825
826 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800827 bc = tick_broadcast_device.evtdev;
828 if (bc)
829 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700830
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100831 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800832}
833
834
835/*
836 * Remove a dead CPU from broadcasting
837 */
838void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
839{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800840 unsigned long flags;
841 unsigned int cpu = *cpup;
842
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100843 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800844
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200845 /*
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200846 * Clear the broadcast masks for the dead cpu, but do not stop
847 * the broadcast device!
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200848 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100849 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200850 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
851 cpumask_clear_cpu(cpu, tick_broadcast_force_mask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800852
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100853 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800854}
855
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200856/*
857 * Check, whether the broadcast device is in one shot mode
858 */
859int tick_broadcast_oneshot_active(void)
860{
861 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
862}
863
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100864/*
865 * Check whether the broadcast device supports oneshot.
866 */
867bool tick_broadcast_oneshot_available(void)
868{
869 struct clock_event_device *bc = tick_broadcast_device.evtdev;
870
871 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
872}
873
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800874#endif
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100875
876void __init tick_broadcast_init(void)
877{
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200878 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200879 zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT);
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200880 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100881#ifdef CONFIG_TICK_ONESHOT
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200882 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
883 zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
884 zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100885#endif
886}