blob: 3500caaa0bfdda96add10ca0d538b09c90842d33 [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 Gleixnerf8381cb2007-02-16 01:28:02 -080022
23#include "tick-internal.h"
24
25/*
26 * Broadcast support for broken x86 hardware, where the local apic
27 * timer stops in C3 state.
28 */
29
Dmitri Vorobieva52f5c52009-05-01 13:10:21 -070030static struct tick_device tick_broadcast_device;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +010031static cpumask_var_t tick_broadcast_mask;
32static cpumask_var_t tmpmask;
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +010033static DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
Thomas Gleixneraa276e12008-06-09 19:15:00 +020034static int tick_broadcast_force;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080035
Thomas Gleixner5590a532007-07-21 04:37:35 -070036#ifdef CONFIG_TICK_ONESHOT
37static void tick_broadcast_clear_oneshot(int cpu);
38#else
39static inline void tick_broadcast_clear_oneshot(int cpu) { }
40#endif
41
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080042/*
Ingo Molnar289f4802007-02-16 01:28:15 -080043 * Debugging: see timer_list.c
44 */
45struct tick_device *tick_get_broadcast_device(void)
46{
47 return &tick_broadcast_device;
48}
49
Rusty Russell6b954822009-01-01 10:12:25 +103050struct cpumask *tick_get_broadcast_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -080051{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +010052 return tick_broadcast_mask;
Ingo Molnar289f4802007-02-16 01:28:15 -080053}
54
55/*
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080056 * Start the device in periodic mode
57 */
58static void tick_broadcast_start_periodic(struct clock_event_device *bc)
59{
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070060 if (bc)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080061 tick_setup_periodic(bc, 1);
62}
63
64/*
65 * Check, if the device can be utilized as broadcast device:
66 */
Thomas Gleixner7172a282013-04-25 20:31:47 +000067void tick_install_broadcast_device(struct clock_event_device *dev)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080068{
Thomas Gleixner6f7a05d2013-04-25 11:45:53 +020069 struct clock_event_device *cur = tick_broadcast_device.evtdev;
70
Mark Rutlanda7dc19b2013-03-07 15:09:24 +000071 if ((dev->features & CLOCK_EVT_FEAT_DUMMY) ||
72 (tick_broadcast_device.evtdev &&
Venki Pallipadi4a932322007-10-12 23:04:23 +020073 tick_broadcast_device.evtdev->rating >= dev->rating) ||
74 (dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixner7172a282013-04-25 20:31:47 +000075 return;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080076
Thomas Gleixnerc1be8432011-12-02 12:34:16 +010077 clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
Thomas Gleixner6f7a05d2013-04-25 11:45:53 +020078 if (cur)
79 cur->event_handler = clockevents_handle_noop;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080080 tick_broadcast_device.evtdev = dev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +010081 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080082 tick_broadcast_start_periodic(dev);
Stephen Boydc038c1c2013-04-17 10:26:06 -070083 /*
84 * Inform all cpus about this. We might be in a situation
85 * where we did not switch to oneshot mode because the per cpu
86 * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
87 * of a oneshot capable broadcast device. Without that
88 * notification the systems stays stuck in periodic mode
89 * forever.
90 */
91 if (dev->features & CLOCK_EVT_FEAT_ONESHOT)
92 tick_clock_notify();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080093}
94
95/*
96 * Check, if the device is the broadcast device
97 */
98int tick_is_broadcast_device(struct clock_event_device *dev)
99{
100 return (dev && tick_broadcast_device.evtdev == dev);
101}
102
Mark Rutland12ad1002013-01-14 17:05:22 +0000103static void err_broadcast(const struct cpumask *mask)
104{
105 pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
106}
107
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000108static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
109{
110 if (!dev->broadcast)
111 dev->broadcast = tick_broadcast;
112 if (!dev->broadcast) {
113 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
114 dev->name);
115 dev->broadcast = err_broadcast;
116 }
117}
118
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800119/*
120 * Check, if the device is disfunctional and a place holder, which
121 * needs to be handled by the broadcast device.
122 */
123int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
124{
125 unsigned long flags;
126 int ret = 0;
127
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100128 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800129
130 /*
131 * Devices might be registered with both periodic and oneshot
132 * mode disabled. This signals, that the device needs to be
133 * operated from the broadcast device and is a placeholder for
134 * the cpu local device.
135 */
136 if (!tick_device_is_functional(dev)) {
137 dev->event_handler = tick_handle_periodic;
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000138 tick_device_setup_broadcast_func(dev);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100139 cpumask_set_cpu(cpu, tick_broadcast_mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800140 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
141 ret = 1;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700142 } else {
143 /*
144 * When the new device is not affected by the stop
145 * feature and the cpu is marked in the broadcast mask
146 * then clear the broadcast bit.
147 */
148 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
149 int cpu = smp_processor_id();
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100150 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700151 tick_broadcast_clear_oneshot(cpu);
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000152 } else {
153 tick_device_setup_broadcast_func(dev);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700154 }
155 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100156 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800157 return ret;
158}
159
Mark Rutland12572db2013-01-14 17:05:21 +0000160#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
161int tick_receive_broadcast(void)
162{
163 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
164 struct clock_event_device *evt = td->evtdev;
165
166 if (!evt)
167 return -ENODEV;
168
169 if (!evt->event_handler)
170 return -EINVAL;
171
172 evt->event_handler(evt);
173 return 0;
174}
175#endif
176
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800177/*
Rusty Russell6b954822009-01-01 10:12:25 +1030178 * Broadcast the event to the cpus, which are set in the mask (mangled).
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800179 */
Rusty Russell6b954822009-01-01 10:12:25 +1030180static void tick_do_broadcast(struct cpumask *mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800181{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100182 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800183 struct tick_device *td;
184
185 /*
186 * Check, if the current cpu is in the mask
187 */
Rusty Russell6b954822009-01-01 10:12:25 +1030188 if (cpumask_test_cpu(cpu, mask)) {
189 cpumask_clear_cpu(cpu, mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800190 td = &per_cpu(tick_cpu_device, cpu);
191 td->evtdev->event_handler(td->evtdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800192 }
193
Rusty Russell6b954822009-01-01 10:12:25 +1030194 if (!cpumask_empty(mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800195 /*
196 * It might be necessary to actually check whether the devices
197 * have different broadcast functions. For now, just use the
198 * one of the first device. This works as long as we have this
199 * misfeature only on x86 (lapic)
200 */
Rusty Russell6b954822009-01-01 10:12:25 +1030201 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
202 td->evtdev->broadcast(mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800203 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800204}
205
206/*
207 * Periodic broadcast:
208 * - invoke the broadcast handlers
209 */
210static void tick_do_periodic_broadcast(void)
211{
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100212 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800213
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100214 cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
215 tick_do_broadcast(tmpmask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800216
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100217 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800218}
219
220/*
221 * Event handler for periodic broadcast ticks
222 */
223static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
224{
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000225 ktime_t next;
226
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800227 tick_do_periodic_broadcast();
228
229 /*
230 * The device is in periodic mode. No reprogramming necessary:
231 */
232 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
233 return;
234
235 /*
236 * Setup the next period for devices, which do not have
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000237 * periodic mode. We read dev->next_event first and add to it
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200238 * when the event already expired. clockevents_program_event()
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000239 * sets dev->next_event only when the event is really
240 * programmed to the device.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800241 */
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000242 for (next = dev->next_event; ;) {
243 next = ktime_add(next, tick_period);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800244
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200245 if (!clockevents_program_event(dev, next, false))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800246 return;
247 tick_do_periodic_broadcast();
248 }
249}
250
251/*
252 * Powerstate information: The system enters/leaves a state, where
253 * affected devices might stop
254 */
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700255static void tick_do_broadcast_on_off(unsigned long *reason)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800256{
257 struct clock_event_device *bc, *dev;
258 struct tick_device *td;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700259 unsigned long flags;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000260 int cpu, bc_stopped;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800261
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100262 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800263
264 cpu = smp_processor_id();
265 td = &per_cpu(tick_cpu_device, cpu);
266 dev = td->evtdev;
267 bc = tick_broadcast_device.evtdev;
268
269 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200270 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800271 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200272 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800273 goto out;
274
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200275 if (!tick_device_is_functional(dev))
276 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200277
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100278 bc_stopped = cpumask_empty(tick_broadcast_mask);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000279
Thomas Gleixner1595f452007-10-14 22:57:45 +0200280 switch (*reason) {
281 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
282 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100283 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200284 if (tick_broadcast_device.mode ==
285 TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700286 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800287 }
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200288 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200289 tick_broadcast_force = 1;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200290 break;
291 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200292 if (!tick_broadcast_force &&
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100293 cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200294 if (tick_broadcast_device.mode ==
295 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800296 tick_setup_periodic(dev, 0);
297 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200298 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800299 }
300
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100301 if (cpumask_empty(tick_broadcast_mask)) {
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000302 if (!bc_stopped)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700303 clockevents_shutdown(bc);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000304 } else if (bc_stopped) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800305 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
306 tick_broadcast_start_periodic(bc);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800307 else
308 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800309 }
310out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100311 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800312}
313
314/*
315 * Powerstate information: The system enters/leaves a state, where
316 * affected devices might stop.
317 */
318void tick_broadcast_on_off(unsigned long reason, int *oncpu)
319{
Rusty Russell6b954822009-01-01 10:12:25 +1030320 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
Glauber Costa833df312008-04-18 13:38:58 -0700321 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
Thomas Gleixner72fcde92007-05-23 13:57:30 -0700322 "offline CPU #%d\n", *oncpu);
Avi Kivitybf020cb2007-10-16 23:26:24 -0700323 else
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700324 tick_do_broadcast_on_off(&reason);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800325}
326
327/*
328 * Set the periodic handler depending on broadcast on/off
329 */
330void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
331{
332 if (!broadcast)
333 dev->event_handler = tick_handle_periodic;
334 else
335 dev->event_handler = tick_handle_periodic_broadcast;
336}
337
338/*
339 * Remove a CPU from broadcasting
340 */
341void tick_shutdown_broadcast(unsigned int *cpup)
342{
343 struct clock_event_device *bc;
344 unsigned long flags;
345 unsigned int cpu = *cpup;
346
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100347 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800348
349 bc = tick_broadcast_device.evtdev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100350 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800351
352 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100353 if (bc && cpumask_empty(tick_broadcast_mask))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700354 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800355 }
356
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100357 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800358}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800359
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100360void tick_suspend_broadcast(void)
361{
362 struct clock_event_device *bc;
363 unsigned long flags;
364
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100365 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100366
367 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700368 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700369 clockevents_shutdown(bc);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100370
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100371 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100372}
373
374int tick_resume_broadcast(void)
375{
376 struct clock_event_device *bc;
377 unsigned long flags;
378 int broadcast = 0;
379
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100380 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100381
382 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100383
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100384 if (bc) {
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700385 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
386
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100387 switch (tick_broadcast_device.mode) {
388 case TICKDEV_MODE_PERIODIC:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100389 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100390 tick_broadcast_start_periodic(bc);
Rusty Russell6b954822009-01-01 10:12:25 +1030391 broadcast = cpumask_test_cpu(smp_processor_id(),
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100392 tick_broadcast_mask);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100393 break;
394 case TICKDEV_MODE_ONESHOT:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100395 if (!cpumask_empty(tick_broadcast_mask))
Suresh Siddhaa6371f82012-04-18 19:27:39 -0700396 broadcast = tick_resume_broadcast_oneshot(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100397 break;
398 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100399 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100400 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100401
402 return broadcast;
403}
404
405
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800406#ifdef CONFIG_TICK_ONESHOT
407
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100408static cpumask_var_t tick_broadcast_oneshot_mask;
Thomas Gleixner26517f32013-03-06 11:18:35 +0000409static cpumask_var_t tick_broadcast_pending_mask;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000410static cpumask_var_t tick_broadcast_force_mask;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800411
Ingo Molnar289f4802007-02-16 01:28:15 -0800412/*
Rusty Russell6b954822009-01-01 10:12:25 +1030413 * Exposed for debugging: see timer_list.c
Ingo Molnar289f4802007-02-16 01:28:15 -0800414 */
Rusty Russell6b954822009-01-01 10:12:25 +1030415struct cpumask *tick_get_broadcast_oneshot_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -0800416{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100417 return tick_broadcast_oneshot_mask;
Ingo Molnar289f4802007-02-16 01:28:15 -0800418}
419
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100420/*
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000421 * Called before going idle with interrupts disabled. Checks whether a
422 * broadcast event from the other core is about to happen. We detected
423 * that in tick_broadcast_oneshot_control(). The callsite can use this
424 * to avoid a deep idle transition as we are about to get the
425 * broadcast IPI right away.
426 */
427int tick_check_broadcast_expired(void)
428{
429 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
430}
431
432/*
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100433 * Set broadcast interrupt affinity
434 */
435static void tick_broadcast_set_affinity(struct clock_event_device *bc,
436 const struct cpumask *cpumask)
437{
438 if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
439 return;
440
441 if (cpumask_equal(bc->cpumask, cpumask))
442 return;
443
444 bc->cpumask = cpumask;
445 irq_set_affinity(bc->irq, bc->cpumask);
446}
447
448static int tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
Daniel Lezcanof9ae39d2013-03-02 11:10:10 +0100449 ktime_t expires, int force)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800450{
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100451 int ret;
452
Thomas Gleixnerb9a6a2352012-04-18 17:31:58 +0200453 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
454 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
455
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100456 ret = clockevents_program_event(bc, expires, force);
457 if (!ret)
458 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
459 return ret;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800460}
461
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100462int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
463{
464 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb7e113d2007-09-22 22:29:06 +0000465 return 0;
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100466}
467
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800468/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200469 * Called from irq_enter() when idle was interrupted to reenable the
470 * per cpu device.
471 */
472void tick_check_oneshot_broadcast(int cpu)
473{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100474 if (cpumask_test_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200475 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
476
477 clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
478 }
479}
480
481/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800482 * Handle oneshot mode broadcasting
483 */
484static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
485{
486 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100487 ktime_t now, next_event;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100488 int cpu, next_cpu = 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800489
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100490 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800491again:
492 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100493 next_event.tv64 = KTIME_MAX;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100494 cpumask_clear(tmpmask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800495 now = ktime_get();
496 /* Find all expired events */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100497 for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800498 td = &per_cpu(tick_cpu_device, cpu);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100499 if (td->evtdev->next_event.tv64 <= now.tv64) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100500 cpumask_set_cpu(cpu, tmpmask);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000501 /*
502 * Mark the remote cpu in the pending mask, so
503 * it can avoid reprogramming the cpu local
504 * timer in tick_broadcast_oneshot_control().
505 */
506 cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100507 } else if (td->evtdev->next_event.tv64 < next_event.tv64) {
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100508 next_event.tv64 = td->evtdev->next_event.tv64;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100509 next_cpu = cpu;
510 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800511 }
512
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000513 /* Take care of enforced broadcast requests */
514 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
515 cpumask_clear(tick_broadcast_force_mask);
516
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800517 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100518 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800519 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100520 tick_do_broadcast(tmpmask);
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100521
522 /*
523 * Two reasons for reprogram:
524 *
525 * - The global event did not expire any CPU local
526 * events. This happens in dyntick mode, as the maximum PIT
527 * delta is quite small.
528 *
529 * - There are pending events on sleeping CPUs which were not
530 * in the event mask
531 */
532 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800533 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100534 * Rearm the broadcast device. If event expired,
535 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800536 */
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100537 if (tick_broadcast_set_event(dev, next_cpu, next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800538 goto again;
539 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100540 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800541}
542
543/*
544 * Powerstate information: The system enters/leaves a state, where
545 * affected devices might stop
546 */
547void tick_broadcast_oneshot_control(unsigned long reason)
548{
549 struct clock_event_device *bc, *dev;
550 struct tick_device *td;
551 unsigned long flags;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000552 ktime_t now;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800553 int cpu;
554
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800555 /*
556 * Periodic mode does not care about the enter/exit of power
557 * states
558 */
559 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Andi Kleen7372b0b2011-05-04 15:09:27 -0700560 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800561
Andi Kleen7372b0b2011-05-04 15:09:27 -0700562 /*
563 * We are called with preemtion disabled from the depth of the
564 * idle code, so we can't be moved away.
565 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800566 cpu = smp_processor_id();
567 td = &per_cpu(tick_cpu_device, cpu);
568 dev = td->evtdev;
569
570 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Andi Kleen7372b0b2011-05-04 15:09:27 -0700571 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800572
Andi Kleen7372b0b2011-05-04 15:09:27 -0700573 bc = tick_broadcast_device.evtdev;
574
575 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800576 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Thomas Gleixner26517f32013-03-06 11:18:35 +0000577 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100578 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800579 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000580 /*
581 * We only reprogram the broadcast timer if we
582 * did not mark ourself in the force mask and
583 * if the cpu local event is earlier than the
584 * broadcast event. If the current CPU is in
585 * the force mask, then we are going to be
586 * woken by the IPI right away.
587 */
588 if (!cpumask_test_cpu(cpu, tick_broadcast_force_mask) &&
589 dev->next_event.tv64 < bc->next_event.tv64)
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100590 tick_broadcast_set_event(bc, cpu, dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800591 }
592 } else {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100593 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800594 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000595 if (dev->next_event.tv64 == KTIME_MAX)
596 goto out;
597 /*
598 * The cpu which was handling the broadcast
599 * timer marked this cpu in the broadcast
600 * pending mask and fired the broadcast
601 * IPI. So we are going to handle the expired
602 * event anyway via the broadcast IPI
603 * handler. No need to reprogram the timer
604 * with an already expired event.
605 */
606 if (cpumask_test_and_clear_cpu(cpu,
607 tick_broadcast_pending_mask))
608 goto out;
609
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000610 /*
611 * If the pending bit is not set, then we are
612 * either the CPU handling the broadcast
613 * interrupt or we got woken by something else.
614 *
615 * We are not longer in the broadcast mask, so
616 * if the cpu local expiry time is already
617 * reached, we would reprogram the cpu local
618 * timer with an already expired event.
619 *
620 * This can lead to a ping-pong when we return
621 * to idle and therefor rearm the broadcast
622 * timer before the cpu local timer was able
623 * to fire. This happens because the forced
624 * reprogramming makes sure that the event
625 * will happen in the future and depending on
626 * the min_delta setting this might be far
627 * enough out that the ping-pong starts.
628 *
629 * If the cpu local next_event has expired
630 * then we know that the broadcast timer
631 * next_event has expired as well and
632 * broadcast is about to be handled. So we
633 * avoid reprogramming and enforce that the
634 * broadcast handler, which did not run yet,
635 * will invoke the cpu local handler.
636 *
637 * We cannot call the handler directly from
638 * here, because we might be in a NOHZ phase
639 * and we did not go through the irq_enter()
640 * nohz fixups.
641 */
642 now = ktime_get();
643 if (dev->next_event.tv64 <= now.tv64) {
644 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
645 goto out;
646 }
647 /*
648 * We got woken by something else. Reprogram
649 * the cpu local timer device.
650 */
Thomas Gleixner26517f32013-03-06 11:18:35 +0000651 tick_program_event(dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800652 }
653 }
Thomas Gleixner26517f32013-03-06 11:18:35 +0000654out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100655 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800656}
657
Thomas Gleixner5590a532007-07-21 04:37:35 -0700658/*
659 * Reset the one shot broadcast for a cpu
660 *
661 * Called with tick_broadcast_lock held
662 */
663static void tick_broadcast_clear_oneshot(int cpu)
664{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100665 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700666}
667
Rusty Russell6b954822009-01-01 10:12:25 +1030668static void tick_broadcast_init_next_event(struct cpumask *mask,
669 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200670{
671 struct tick_device *td;
672 int cpu;
673
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030674 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200675 td = &per_cpu(tick_cpu_device, cpu);
676 if (td->evtdev)
677 td->evtdev->next_event = expires;
678 }
679}
680
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800681/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800682 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800683 */
684void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
685{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200686 int cpu = smp_processor_id();
687
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000688 /* Set it up only once ! */
689 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200690 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200691
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000692 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200693
694 /* Take the do_timer update */
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200695 if (!tick_nohz_full_cpu(cpu))
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100696 tick_do_timer_cpu = cpu;
Thomas Gleixner73007112008-09-06 03:01:45 +0200697
698 /*
699 * We must be careful here. There might be other CPUs
700 * waiting for periodic broadcast. We need to set the
701 * oneshot_mask bits for those and program the
702 * broadcast device to fire.
703 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100704 cpumask_copy(tmpmask, tick_broadcast_mask);
705 cpumask_clear_cpu(cpu, tmpmask);
706 cpumask_or(tick_broadcast_oneshot_mask,
707 tick_broadcast_oneshot_mask, tmpmask);
Thomas Gleixner73007112008-09-06 03:01:45 +0200708
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100709 if (was_periodic && !cpumask_empty(tmpmask)) {
Thomas Gleixnerb4350922012-04-18 12:08:23 +0200710 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100711 tick_broadcast_init_next_event(tmpmask,
Rusty Russell6b954822009-01-01 10:12:25 +1030712 tick_next_period);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100713 tick_broadcast_set_event(bc, cpu, tick_next_period, 1);
Thomas Gleixner73007112008-09-06 03:01:45 +0200714 } else
715 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200716 } else {
717 /*
718 * The first cpu which switches to oneshot mode sets
719 * the bit for all other cpus which are in the general
720 * (periodic) broadcast mask. So the bit is set and
721 * would prevent the first broadcast enter after this
722 * to program the bc device.
723 */
724 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000725 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800726}
727
728/*
729 * Select oneshot operating mode for the broadcast device
730 */
731void tick_broadcast_switch_to_oneshot(void)
732{
733 struct clock_event_device *bc;
734 unsigned long flags;
735
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100736 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700737
738 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800739 bc = tick_broadcast_device.evtdev;
740 if (bc)
741 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700742
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100743 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800744}
745
746
747/*
748 * Remove a dead CPU from broadcasting
749 */
750void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
751{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800752 unsigned long flags;
753 unsigned int cpu = *cpup;
754
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100755 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800756
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200757 /*
758 * Clear the broadcast mask flag for the dead cpu, but do not
759 * stop the broadcast device!
760 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100761 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800762
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100763 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800764}
765
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200766/*
767 * Check, whether the broadcast device is in one shot mode
768 */
769int tick_broadcast_oneshot_active(void)
770{
771 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
772}
773
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100774/*
775 * Check whether the broadcast device supports oneshot.
776 */
777bool tick_broadcast_oneshot_available(void)
778{
779 struct clock_event_device *bc = tick_broadcast_device.evtdev;
780
781 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
782}
783
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800784#endif
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100785
786void __init tick_broadcast_init(void)
787{
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200788 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
789 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100790#ifdef CONFIG_TICK_ONESHOT
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200791 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
792 zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
793 zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100794#endif
795}