blob: 63c7b2d9ed8ee43d74d00deedbe2b287683f4a0a [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) ||
Soren Brinkmann245a3492013-09-18 11:48:37 -070073 (newdev->features & CLOCK_EVT_FEAT_PERCPU) ||
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000074 (newdev->features & CLOCK_EVT_FEAT_C3STOP))
75 return false;
76
77 if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT &&
78 !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
79 return false;
80
81 return !curdev || newdev->rating > curdev->rating;
82}
83
84/*
85 * Conditionally install/replace broadcast device
86 */
Thomas Gleixner7172a282013-04-25 20:31:47 +000087void tick_install_broadcast_device(struct clock_event_device *dev)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080088{
Thomas Gleixner6f7a05d2013-04-25 11:45:53 +020089 struct clock_event_device *cur = tick_broadcast_device.evtdev;
90
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000091 if (!tick_check_broadcast_device(cur, dev))
Thomas Gleixner7172a282013-04-25 20:31:47 +000092 return;
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000093
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000094 if (!try_module_get(dev->owner))
95 return;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080096
Thomas Gleixner45cb8e02013-04-25 20:31:50 +000097 clockevents_exchange_device(cur, dev);
Thomas Gleixner6f7a05d2013-04-25 11:45:53 +020098 if (cur)
99 cur->event_handler = clockevents_handle_noop;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800100 tick_broadcast_device.evtdev = dev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100101 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800102 tick_broadcast_start_periodic(dev);
Stephen Boydc038c1c2013-04-17 10:26:06 -0700103 /*
104 * Inform all cpus about this. We might be in a situation
105 * where we did not switch to oneshot mode because the per cpu
106 * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
107 * of a oneshot capable broadcast device. Without that
108 * notification the systems stays stuck in periodic mode
109 * forever.
110 */
111 if (dev->features & CLOCK_EVT_FEAT_ONESHOT)
112 tick_clock_notify();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800113}
114
115/*
116 * Check, if the device is the broadcast device
117 */
118int tick_is_broadcast_device(struct clock_event_device *dev)
119{
120 return (dev && tick_broadcast_device.evtdev == dev);
121}
122
Thomas Gleixner627ee792014-02-03 14:34:31 -0800123int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq)
124{
125 int ret = -ENODEV;
126
127 if (tick_is_broadcast_device(dev)) {
128 raw_spin_lock(&tick_broadcast_lock);
129 ret = __clockevents_update_freq(dev, freq);
130 raw_spin_unlock(&tick_broadcast_lock);
131 }
132 return ret;
133}
134
135
Mark Rutland12ad1002013-01-14 17:05:22 +0000136static void err_broadcast(const struct cpumask *mask)
137{
138 pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
139}
140
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000141static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
142{
143 if (!dev->broadcast)
144 dev->broadcast = tick_broadcast;
145 if (!dev->broadcast) {
146 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
147 dev->name);
148 dev->broadcast = err_broadcast;
149 }
150}
151
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800152/*
153 * Check, if the device is disfunctional and a place holder, which
154 * needs to be handled by the broadcast device.
155 */
156int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
157{
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200158 struct clock_event_device *bc = tick_broadcast_device.evtdev;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800159 unsigned long flags;
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200160 int ret;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800161
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100162 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800163
164 /*
165 * Devices might be registered with both periodic and oneshot
166 * mode disabled. This signals, that the device needs to be
167 * operated from the broadcast device and is a placeholder for
168 * the cpu local device.
169 */
170 if (!tick_device_is_functional(dev)) {
171 dev->event_handler = tick_handle_periodic;
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000172 tick_device_setup_broadcast_func(dev);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100173 cpumask_set_cpu(cpu, tick_broadcast_mask);
Stephen Boyda272dcc2013-07-11 07:00:59 -0700174 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
175 tick_broadcast_start_periodic(bc);
176 else
177 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800178 ret = 1;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700179 } else {
180 /*
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200181 * Clear the broadcast bit for this cpu if the
182 * device is not power state affected.
Thomas Gleixner5590a532007-07-21 04:37:35 -0700183 */
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200184 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100185 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200186 else
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000187 tick_device_setup_broadcast_func(dev);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200188
189 /*
190 * Clear the broadcast bit if the CPU is not in
191 * periodic broadcast on state.
192 */
193 if (!cpumask_test_cpu(cpu, tick_broadcast_on))
194 cpumask_clear_cpu(cpu, tick_broadcast_mask);
195
196 switch (tick_broadcast_device.mode) {
197 case TICKDEV_MODE_ONESHOT:
198 /*
199 * If the system is in oneshot mode we can
200 * unconditionally clear the oneshot mask bit,
201 * because the CPU is running and therefore
202 * not in an idle state which causes the power
203 * state affected device to stop. Let the
204 * caller initialize the device.
205 */
206 tick_broadcast_clear_oneshot(cpu);
207 ret = 0;
208 break;
209
210 case TICKDEV_MODE_PERIODIC:
211 /*
212 * If the system is in periodic mode, check
213 * whether the broadcast device can be
214 * switched off now.
215 */
216 if (cpumask_empty(tick_broadcast_mask) && bc)
217 clockevents_shutdown(bc);
218 /*
219 * If we kept the cpu in the broadcast mask,
220 * tell the caller to leave the per cpu device
221 * in shutdown state. The periodic interrupt
222 * is delivered by the broadcast device.
223 */
224 ret = cpumask_test_cpu(cpu, tick_broadcast_mask);
225 break;
226 default:
227 /* Nothing to do */
228 ret = 0;
229 break;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700230 }
231 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100232 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800233 return ret;
234}
235
Mark Rutland12572db2013-01-14 17:05:21 +0000236#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
237int tick_receive_broadcast(void)
238{
239 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
240 struct clock_event_device *evt = td->evtdev;
241
242 if (!evt)
243 return -ENODEV;
244
245 if (!evt->event_handler)
246 return -EINVAL;
247
248 evt->event_handler(evt);
249 return 0;
250}
251#endif
252
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800253/*
Rusty Russell6b954822009-01-01 10:12:25 +1030254 * Broadcast the event to the cpus, which are set in the mask (mangled).
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800255 */
Rusty Russell6b954822009-01-01 10:12:25 +1030256static void tick_do_broadcast(struct cpumask *mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800257{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100258 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800259 struct tick_device *td;
260
261 /*
262 * Check, if the current cpu is in the mask
263 */
Rusty Russell6b954822009-01-01 10:12:25 +1030264 if (cpumask_test_cpu(cpu, mask)) {
265 cpumask_clear_cpu(cpu, mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800266 td = &per_cpu(tick_cpu_device, cpu);
267 td->evtdev->event_handler(td->evtdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800268 }
269
Rusty Russell6b954822009-01-01 10:12:25 +1030270 if (!cpumask_empty(mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800271 /*
272 * It might be necessary to actually check whether the devices
273 * have different broadcast functions. For now, just use the
274 * one of the first device. This works as long as we have this
275 * misfeature only on x86 (lapic)
276 */
Rusty Russell6b954822009-01-01 10:12:25 +1030277 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
278 td->evtdev->broadcast(mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800279 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800280}
281
282/*
283 * Periodic broadcast:
284 * - invoke the broadcast handlers
285 */
286static void tick_do_periodic_broadcast(void)
287{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100288 cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
289 tick_do_broadcast(tmpmask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800290}
291
292/*
293 * Event handler for periodic broadcast ticks
294 */
295static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
296{
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000297 ktime_t next;
298
Thomas Gleixner627ee792014-02-03 14:34:31 -0800299 raw_spin_lock(&tick_broadcast_lock);
300
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800301 tick_do_periodic_broadcast();
302
303 /*
304 * The device is in periodic mode. No reprogramming necessary:
305 */
306 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
Thomas Gleixner627ee792014-02-03 14:34:31 -0800307 goto unlock;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800308
309 /*
310 * Setup the next period for devices, which do not have
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000311 * periodic mode. We read dev->next_event first and add to it
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200312 * when the event already expired. clockevents_program_event()
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000313 * sets dev->next_event only when the event is really
314 * programmed to the device.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800315 */
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000316 for (next = dev->next_event; ;) {
317 next = ktime_add(next, tick_period);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800318
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200319 if (!clockevents_program_event(dev, next, false))
Thomas Gleixner627ee792014-02-03 14:34:31 -0800320 goto unlock;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800321 tick_do_periodic_broadcast();
322 }
Thomas Gleixner627ee792014-02-03 14:34:31 -0800323unlock:
324 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800325}
326
327/*
328 * Powerstate information: The system enters/leaves a state, where
329 * affected devices might stop
330 */
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700331static void tick_do_broadcast_on_off(unsigned long *reason)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800332{
333 struct clock_event_device *bc, *dev;
334 struct tick_device *td;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700335 unsigned long flags;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000336 int cpu, bc_stopped;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800337
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100338 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800339
340 cpu = smp_processor_id();
341 td = &per_cpu(tick_cpu_device, cpu);
342 dev = td->evtdev;
343 bc = tick_broadcast_device.evtdev;
344
345 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200346 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800347 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200348 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800349 goto out;
350
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200351 if (!tick_device_is_functional(dev))
352 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200353
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100354 bc_stopped = cpumask_empty(tick_broadcast_mask);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000355
Thomas Gleixner1595f452007-10-14 22:57:45 +0200356 switch (*reason) {
357 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
358 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200359 cpumask_set_cpu(cpu, tick_broadcast_on);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100360 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200361 if (tick_broadcast_device.mode ==
362 TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700363 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800364 }
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200365 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200366 tick_broadcast_force = 1;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200367 break;
368 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200369 if (tick_broadcast_force)
370 break;
371 cpumask_clear_cpu(cpu, tick_broadcast_on);
372 if (!tick_device_is_functional(dev))
373 break;
374 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200375 if (tick_broadcast_device.mode ==
376 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800377 tick_setup_periodic(dev, 0);
378 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200379 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800380 }
381
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100382 if (cpumask_empty(tick_broadcast_mask)) {
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000383 if (!bc_stopped)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700384 clockevents_shutdown(bc);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000385 } else if (bc_stopped) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800386 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
387 tick_broadcast_start_periodic(bc);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800388 else
389 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800390 }
391out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100392 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800393}
394
395/*
396 * Powerstate information: The system enters/leaves a state, where
397 * affected devices might stop.
398 */
399void tick_broadcast_on_off(unsigned long reason, int *oncpu)
400{
Rusty Russell6b954822009-01-01 10:12:25 +1030401 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
Glauber Costa833df312008-04-18 13:38:58 -0700402 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
Thomas Gleixner72fcde92007-05-23 13:57:30 -0700403 "offline CPU #%d\n", *oncpu);
Avi Kivitybf020cb2007-10-16 23:26:24 -0700404 else
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700405 tick_do_broadcast_on_off(&reason);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800406}
407
408/*
409 * Set the periodic handler depending on broadcast on/off
410 */
411void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
412{
413 if (!broadcast)
414 dev->event_handler = tick_handle_periodic;
415 else
416 dev->event_handler = tick_handle_periodic_broadcast;
417}
418
419/*
420 * Remove a CPU from broadcasting
421 */
422void tick_shutdown_broadcast(unsigned int *cpup)
423{
424 struct clock_event_device *bc;
425 unsigned long flags;
426 unsigned int cpu = *cpup;
427
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100428 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800429
430 bc = tick_broadcast_device.evtdev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100431 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200432 cpumask_clear_cpu(cpu, tick_broadcast_on);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800433
434 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100435 if (bc && cpumask_empty(tick_broadcast_mask))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700436 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800437 }
438
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100439 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800440}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800441
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100442void tick_suspend_broadcast(void)
443{
444 struct clock_event_device *bc;
445 unsigned long flags;
446
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100447 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100448
449 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700450 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700451 clockevents_shutdown(bc);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100452
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100453 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100454}
455
456int tick_resume_broadcast(void)
457{
458 struct clock_event_device *bc;
459 unsigned long flags;
460 int broadcast = 0;
461
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100462 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100463
464 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100465
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100466 if (bc) {
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700467 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
468
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100469 switch (tick_broadcast_device.mode) {
470 case TICKDEV_MODE_PERIODIC:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100471 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100472 tick_broadcast_start_periodic(bc);
Rusty Russell6b954822009-01-01 10:12:25 +1030473 broadcast = cpumask_test_cpu(smp_processor_id(),
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100474 tick_broadcast_mask);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100475 break;
476 case TICKDEV_MODE_ONESHOT:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100477 if (!cpumask_empty(tick_broadcast_mask))
Suresh Siddhaa6371f82012-04-18 19:27:39 -0700478 broadcast = tick_resume_broadcast_oneshot(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100479 break;
480 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100481 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100482 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100483
484 return broadcast;
485}
486
487
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800488#ifdef CONFIG_TICK_ONESHOT
489
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100490static cpumask_var_t tick_broadcast_oneshot_mask;
Thomas Gleixner26517f32013-03-06 11:18:35 +0000491static cpumask_var_t tick_broadcast_pending_mask;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000492static cpumask_var_t tick_broadcast_force_mask;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800493
Ingo Molnar289f4802007-02-16 01:28:15 -0800494/*
Rusty Russell6b954822009-01-01 10:12:25 +1030495 * Exposed for debugging: see timer_list.c
Ingo Molnar289f4802007-02-16 01:28:15 -0800496 */
Rusty Russell6b954822009-01-01 10:12:25 +1030497struct cpumask *tick_get_broadcast_oneshot_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -0800498{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100499 return tick_broadcast_oneshot_mask;
Ingo Molnar289f4802007-02-16 01:28:15 -0800500}
501
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100502/*
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000503 * Called before going idle with interrupts disabled. Checks whether a
504 * broadcast event from the other core is about to happen. We detected
505 * that in tick_broadcast_oneshot_control(). The callsite can use this
506 * to avoid a deep idle transition as we are about to get the
507 * broadcast IPI right away.
508 */
509int tick_check_broadcast_expired(void)
510{
511 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
512}
513
514/*
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100515 * Set broadcast interrupt affinity
516 */
517static void tick_broadcast_set_affinity(struct clock_event_device *bc,
518 const struct cpumask *cpumask)
519{
520 if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
521 return;
522
523 if (cpumask_equal(bc->cpumask, cpumask))
524 return;
525
526 bc->cpumask = cpumask;
527 irq_set_affinity(bc->irq, bc->cpumask);
528}
529
530static int tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
Daniel Lezcanof9ae39d2013-03-02 11:10:10 +0100531 ktime_t expires, int force)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800532{
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100533 int ret;
534
Thomas Gleixnerb9a6a2352012-04-18 17:31:58 +0200535 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
536 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
537
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100538 ret = clockevents_program_event(bc, expires, force);
539 if (!ret)
540 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
541 return ret;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800542}
543
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100544int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
545{
546 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb7e113d2007-09-22 22:29:06 +0000547 return 0;
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100548}
549
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800550/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200551 * Called from irq_enter() when idle was interrupted to reenable the
552 * per cpu device.
553 */
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200554void tick_check_oneshot_broadcast_this_cpu(void)
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200555{
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200556 if (cpumask_test_cpu(smp_processor_id(), tick_broadcast_oneshot_mask)) {
557 struct tick_device *td = &__get_cpu_var(tick_cpu_device);
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200558
Thomas Gleixner1f73a982013-07-01 22:14:10 +0200559 /*
560 * We might be in the middle of switching over from
561 * periodic to oneshot. If the CPU has not yet
562 * switched over, leave the device alone.
563 */
564 if (td->mode == TICKDEV_MODE_ONESHOT) {
565 clockevents_set_mode(td->evtdev,
566 CLOCK_EVT_MODE_ONESHOT);
567 }
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200568 }
569}
570
571/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800572 * Handle oneshot mode broadcasting
573 */
574static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
575{
576 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100577 ktime_t now, next_event;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100578 int cpu, next_cpu = 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800579
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100580 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800581again:
582 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100583 next_event.tv64 = KTIME_MAX;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100584 cpumask_clear(tmpmask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800585 now = ktime_get();
586 /* Find all expired events */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100587 for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800588 td = &per_cpu(tick_cpu_device, cpu);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100589 if (td->evtdev->next_event.tv64 <= now.tv64) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100590 cpumask_set_cpu(cpu, tmpmask);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000591 /*
592 * Mark the remote cpu in the pending mask, so
593 * it can avoid reprogramming the cpu local
594 * timer in tick_broadcast_oneshot_control().
595 */
596 cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100597 } else if (td->evtdev->next_event.tv64 < next_event.tv64) {
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100598 next_event.tv64 = td->evtdev->next_event.tv64;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100599 next_cpu = cpu;
600 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800601 }
602
Thomas Gleixner2938d272013-05-28 09:33:01 +0200603 /*
604 * Remove the current cpu from the pending mask. The event is
605 * delivered immediately in tick_do_broadcast() !
606 */
607 cpumask_clear_cpu(smp_processor_id(), tick_broadcast_pending_mask);
608
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000609 /* Take care of enforced broadcast requests */
610 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
611 cpumask_clear(tick_broadcast_force_mask);
612
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800613 /*
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200614 * Sanity check. Catch the case where we try to broadcast to
615 * offline cpus.
616 */
617 if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask)))
618 cpumask_and(tmpmask, tmpmask, cpu_online_mask);
619
620 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100621 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800622 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100623 tick_do_broadcast(tmpmask);
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100624
625 /*
626 * Two reasons for reprogram:
627 *
628 * - The global event did not expire any CPU local
629 * events. This happens in dyntick mode, as the maximum PIT
630 * delta is quite small.
631 *
632 * - There are pending events on sleeping CPUs which were not
633 * in the event mask
634 */
635 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800636 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100637 * Rearm the broadcast device. If event expired,
638 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800639 */
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100640 if (tick_broadcast_set_event(dev, next_cpu, next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800641 goto again;
642 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100643 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800644}
645
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530646static int broadcast_needs_cpu(struct clock_event_device *bc, int cpu)
647{
648 if (!(bc->features & CLOCK_EVT_FEAT_HRTIMER))
649 return 0;
650 if (bc->next_event.tv64 == KTIME_MAX)
651 return 0;
652 return bc->bound_on == cpu ? -EBUSY : 0;
653}
654
655static void broadcast_shutdown_local(struct clock_event_device *bc,
656 struct clock_event_device *dev)
657{
658 /*
659 * For hrtimer based broadcasting we cannot shutdown the cpu
660 * local device if our own event is the first one to expire or
661 * if we own the broadcast timer.
662 */
663 if (bc->features & CLOCK_EVT_FEAT_HRTIMER) {
664 if (broadcast_needs_cpu(bc, smp_processor_id()))
665 return;
666 if (dev->next_event.tv64 < bc->next_event.tv64)
667 return;
668 }
669 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
670}
671
672static void broadcast_move_bc(int deadcpu)
673{
674 struct clock_event_device *bc = tick_broadcast_device.evtdev;
675
676 if (!bc || !broadcast_needs_cpu(bc, deadcpu))
677 return;
678 /* This moves the broadcast assignment to this cpu */
679 clockevents_program_event(bc, bc->next_event, 1);
680}
681
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800682/*
683 * Powerstate information: The system enters/leaves a state, where
684 * affected devices might stop
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530685 * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800686 */
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530687int tick_broadcast_oneshot_control(unsigned long reason)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800688{
689 struct clock_event_device *bc, *dev;
690 struct tick_device *td;
691 unsigned long flags;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000692 ktime_t now;
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530693 int cpu, ret = 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800694
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800695 /*
696 * Periodic mode does not care about the enter/exit of power
697 * states
698 */
699 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530700 return 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800701
Andi Kleen7372b0b2011-05-04 15:09:27 -0700702 /*
703 * We are called with preemtion disabled from the depth of the
704 * idle code, so we can't be moved away.
705 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800706 cpu = smp_processor_id();
707 td = &per_cpu(tick_cpu_device, cpu);
708 dev = td->evtdev;
709
710 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530711 return 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800712
Andi Kleen7372b0b2011-05-04 15:09:27 -0700713 bc = tick_broadcast_device.evtdev;
714
715 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800716 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100717 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner2938d272013-05-28 09:33:01 +0200718 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530719 broadcast_shutdown_local(bc, dev);
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000720 /*
721 * We only reprogram the broadcast timer if we
722 * did not mark ourself in the force mask and
723 * if the cpu local event is earlier than the
724 * broadcast event. If the current CPU is in
725 * the force mask, then we are going to be
726 * woken by the IPI right away.
727 */
728 if (!cpumask_test_cpu(cpu, tick_broadcast_force_mask) &&
729 dev->next_event.tv64 < bc->next_event.tv64)
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100730 tick_broadcast_set_event(bc, cpu, dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800731 }
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530732 /*
733 * If the current CPU owns the hrtimer broadcast
734 * mechanism, it cannot go deep idle and we remove the
735 * CPU from the broadcast mask. We don't have to go
736 * through the EXIT path as the local timer is not
737 * shutdown.
738 */
739 ret = broadcast_needs_cpu(bc, cpu);
740 if (ret)
741 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800742 } else {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100743 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800744 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000745 /*
746 * The cpu which was handling the broadcast
747 * timer marked this cpu in the broadcast
748 * pending mask and fired the broadcast
749 * IPI. So we are going to handle the expired
750 * event anyway via the broadcast IPI
751 * handler. No need to reprogram the timer
752 * with an already expired event.
753 */
754 if (cpumask_test_and_clear_cpu(cpu,
755 tick_broadcast_pending_mask))
756 goto out;
757
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000758 /*
Daniel Lezcanoea8deb82013-06-17 18:15:35 +0200759 * Bail out if there is no next event.
760 */
761 if (dev->next_event.tv64 == KTIME_MAX)
762 goto out;
763 /*
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000764 * If the pending bit is not set, then we are
765 * either the CPU handling the broadcast
766 * interrupt or we got woken by something else.
767 *
768 * We are not longer in the broadcast mask, so
769 * if the cpu local expiry time is already
770 * reached, we would reprogram the cpu local
771 * timer with an already expired event.
772 *
773 * This can lead to a ping-pong when we return
774 * to idle and therefor rearm the broadcast
775 * timer before the cpu local timer was able
776 * to fire. This happens because the forced
777 * reprogramming makes sure that the event
778 * will happen in the future and depending on
779 * the min_delta setting this might be far
780 * enough out that the ping-pong starts.
781 *
782 * If the cpu local next_event has expired
783 * then we know that the broadcast timer
784 * next_event has expired as well and
785 * broadcast is about to be handled. So we
786 * avoid reprogramming and enforce that the
787 * broadcast handler, which did not run yet,
788 * will invoke the cpu local handler.
789 *
790 * We cannot call the handler directly from
791 * here, because we might be in a NOHZ phase
792 * and we did not go through the irq_enter()
793 * nohz fixups.
794 */
795 now = ktime_get();
796 if (dev->next_event.tv64 <= now.tv64) {
797 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
798 goto out;
799 }
800 /*
801 * We got woken by something else. Reprogram
802 * the cpu local timer device.
803 */
Thomas Gleixner26517f32013-03-06 11:18:35 +0000804 tick_program_event(dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800805 }
806 }
Thomas Gleixner26517f32013-03-06 11:18:35 +0000807out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100808 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530809 return ret;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800810}
811
Thomas Gleixner5590a532007-07-21 04:37:35 -0700812/*
813 * Reset the one shot broadcast for a cpu
814 *
815 * Called with tick_broadcast_lock held
816 */
817static void tick_broadcast_clear_oneshot(int cpu)
818{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100819 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700820}
821
Rusty Russell6b954822009-01-01 10:12:25 +1030822static void tick_broadcast_init_next_event(struct cpumask *mask,
823 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200824{
825 struct tick_device *td;
826 int cpu;
827
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030828 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200829 td = &per_cpu(tick_cpu_device, cpu);
830 if (td->evtdev)
831 td->evtdev->next_event = expires;
832 }
833}
834
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800835/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800836 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800837 */
838void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
839{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200840 int cpu = smp_processor_id();
841
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000842 /* Set it up only once ! */
843 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200844 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200845
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000846 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200847
Thomas Gleixner73007112008-09-06 03:01:45 +0200848 /*
849 * We must be careful here. There might be other CPUs
850 * waiting for periodic broadcast. We need to set the
851 * oneshot_mask bits for those and program the
852 * broadcast device to fire.
853 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100854 cpumask_copy(tmpmask, tick_broadcast_mask);
855 cpumask_clear_cpu(cpu, tmpmask);
856 cpumask_or(tick_broadcast_oneshot_mask,
857 tick_broadcast_oneshot_mask, tmpmask);
Thomas Gleixner73007112008-09-06 03:01:45 +0200858
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100859 if (was_periodic && !cpumask_empty(tmpmask)) {
Thomas Gleixnerb4350922012-04-18 12:08:23 +0200860 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100861 tick_broadcast_init_next_event(tmpmask,
Rusty Russell6b954822009-01-01 10:12:25 +1030862 tick_next_period);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100863 tick_broadcast_set_event(bc, cpu, tick_next_period, 1);
Thomas Gleixner73007112008-09-06 03:01:45 +0200864 } else
865 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200866 } else {
867 /*
868 * The first cpu which switches to oneshot mode sets
869 * the bit for all other cpus which are in the general
870 * (periodic) broadcast mask. So the bit is set and
871 * would prevent the first broadcast enter after this
872 * to program the bc device.
873 */
874 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000875 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800876}
877
878/*
879 * Select oneshot operating mode for the broadcast device
880 */
881void tick_broadcast_switch_to_oneshot(void)
882{
883 struct clock_event_device *bc;
884 unsigned long flags;
885
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100886 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700887
888 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800889 bc = tick_broadcast_device.evtdev;
890 if (bc)
891 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700892
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100893 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800894}
895
896
897/*
898 * Remove a dead CPU from broadcasting
899 */
900void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
901{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800902 unsigned long flags;
903 unsigned int cpu = *cpup;
904
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100905 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800906
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200907 /*
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200908 * Clear the broadcast masks for the dead cpu, but do not stop
909 * the broadcast device!
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200910 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100911 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixnerc9b5a262013-06-26 12:17:32 +0200912 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
913 cpumask_clear_cpu(cpu, tick_broadcast_force_mask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800914
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530915 broadcast_move_bc(cpu);
916
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100917 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800918}
919
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200920/*
921 * Check, whether the broadcast device is in one shot mode
922 */
923int tick_broadcast_oneshot_active(void)
924{
925 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
926}
927
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100928/*
929 * Check whether the broadcast device supports oneshot.
930 */
931bool tick_broadcast_oneshot_available(void)
932{
933 struct clock_event_device *bc = tick_broadcast_device.evtdev;
934
935 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
936}
937
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800938#endif
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100939
940void __init tick_broadcast_init(void)
941{
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200942 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200943 zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT);
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200944 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100945#ifdef CONFIG_TICK_ONESHOT
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200946 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
947 zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
948 zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100949#endif
950}