blob: 0e374cd2e0efeec1a5528d96106253477097db5e [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;
33static cpumask_var_t tmpmask;
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +010034static DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
Thomas Gleixneraa276e12008-06-09 19:15:00 +020035static int tick_broadcast_force;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080036
Thomas Gleixner5590a532007-07-21 04:37:35 -070037#ifdef CONFIG_TICK_ONESHOT
38static void tick_broadcast_clear_oneshot(int cpu);
39#else
40static inline void tick_broadcast_clear_oneshot(int cpu) { }
41#endif
42
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080043/*
Ingo Molnar289f4802007-02-16 01:28:15 -080044 * Debugging: see timer_list.c
45 */
46struct tick_device *tick_get_broadcast_device(void)
47{
48 return &tick_broadcast_device;
49}
50
Rusty Russell6b954822009-01-01 10:12:25 +103051struct cpumask *tick_get_broadcast_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -080052{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +010053 return tick_broadcast_mask;
Ingo Molnar289f4802007-02-16 01:28:15 -080054}
55
56/*
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080057 * Start the device in periodic mode
58 */
59static void tick_broadcast_start_periodic(struct clock_event_device *bc)
60{
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070061 if (bc)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080062 tick_setup_periodic(bc, 1);
63}
64
65/*
66 * Check, if the device can be utilized as broadcast device:
67 */
Thomas Gleixner7172a2862013-04-25 20:31:47 +000068void tick_install_broadcast_device(struct clock_event_device *dev)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080069{
Thomas Gleixner6f7a05d2013-04-25 11:45:53 +020070 struct clock_event_device *cur = tick_broadcast_device.evtdev;
71
Mark Rutlanda7dc19b2013-03-07 15:09:24 +000072 if ((dev->features & CLOCK_EVT_FEAT_DUMMY) ||
73 (tick_broadcast_device.evtdev &&
Venki Pallipadi4a932322007-10-12 23:04:23 +020074 tick_broadcast_device.evtdev->rating >= dev->rating) ||
75 (dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixner7172a2862013-04-25 20:31:47 +000076 return;
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000077 if (!try_module_get(dev->owner))
78 return;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080079
Thomas Gleixnerc1be8432011-12-02 12:34:16 +010080 clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
Thomas Gleixner6f7a05d2013-04-25 11:45:53 +020081 if (cur)
82 cur->event_handler = clockevents_handle_noop;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080083 tick_broadcast_device.evtdev = dev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +010084 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080085 tick_broadcast_start_periodic(dev);
Stephen Boydc038c1c2013-04-17 10:26:06 -070086 /*
87 * Inform all cpus about this. We might be in a situation
88 * where we did not switch to oneshot mode because the per cpu
89 * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
90 * of a oneshot capable broadcast device. Without that
91 * notification the systems stays stuck in periodic mode
92 * forever.
93 */
94 if (dev->features & CLOCK_EVT_FEAT_ONESHOT)
95 tick_clock_notify();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080096}
97
98/*
99 * Check, if the device is the broadcast device
100 */
101int tick_is_broadcast_device(struct clock_event_device *dev)
102{
103 return (dev && tick_broadcast_device.evtdev == dev);
104}
105
Mark Rutland12ad1002013-01-14 17:05:22 +0000106static void err_broadcast(const struct cpumask *mask)
107{
108 pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
109}
110
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000111static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
112{
113 if (!dev->broadcast)
114 dev->broadcast = tick_broadcast;
115 if (!dev->broadcast) {
116 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
117 dev->name);
118 dev->broadcast = err_broadcast;
119 }
120}
121
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800122/*
123 * Check, if the device is disfunctional and a place holder, which
124 * needs to be handled by the broadcast device.
125 */
126int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
127{
128 unsigned long flags;
129 int ret = 0;
130
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100131 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800132
133 /*
134 * Devices might be registered with both periodic and oneshot
135 * mode disabled. This signals, that the device needs to be
136 * operated from the broadcast device and is a placeholder for
137 * the cpu local device.
138 */
139 if (!tick_device_is_functional(dev)) {
140 dev->event_handler = tick_handle_periodic;
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000141 tick_device_setup_broadcast_func(dev);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100142 cpumask_set_cpu(cpu, tick_broadcast_mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800143 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
144 ret = 1;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700145 } else {
146 /*
147 * When the new device is not affected by the stop
148 * feature and the cpu is marked in the broadcast mask
149 * then clear the broadcast bit.
150 */
151 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
152 int cpu = smp_processor_id();
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100153 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700154 tick_broadcast_clear_oneshot(cpu);
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000155 } else {
156 tick_device_setup_broadcast_func(dev);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700157 }
158 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100159 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800160 return ret;
161}
162
Mark Rutland12572db2013-01-14 17:05:21 +0000163#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
164int tick_receive_broadcast(void)
165{
166 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
167 struct clock_event_device *evt = td->evtdev;
168
169 if (!evt)
170 return -ENODEV;
171
172 if (!evt->event_handler)
173 return -EINVAL;
174
175 evt->event_handler(evt);
176 return 0;
177}
178#endif
179
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800180/*
Rusty Russell6b954822009-01-01 10:12:25 +1030181 * Broadcast the event to the cpus, which are set in the mask (mangled).
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800182 */
Rusty Russell6b954822009-01-01 10:12:25 +1030183static void tick_do_broadcast(struct cpumask *mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800184{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100185 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800186 struct tick_device *td;
187
188 /*
189 * Check, if the current cpu is in the mask
190 */
Rusty Russell6b954822009-01-01 10:12:25 +1030191 if (cpumask_test_cpu(cpu, mask)) {
192 cpumask_clear_cpu(cpu, mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800193 td = &per_cpu(tick_cpu_device, cpu);
194 td->evtdev->event_handler(td->evtdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800195 }
196
Rusty Russell6b954822009-01-01 10:12:25 +1030197 if (!cpumask_empty(mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800198 /*
199 * It might be necessary to actually check whether the devices
200 * have different broadcast functions. For now, just use the
201 * one of the first device. This works as long as we have this
202 * misfeature only on x86 (lapic)
203 */
Rusty Russell6b954822009-01-01 10:12:25 +1030204 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
205 td->evtdev->broadcast(mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800206 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800207}
208
209/*
210 * Periodic broadcast:
211 * - invoke the broadcast handlers
212 */
213static void tick_do_periodic_broadcast(void)
214{
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100215 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800216
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100217 cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
218 tick_do_broadcast(tmpmask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800219
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100220 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800221}
222
223/*
224 * Event handler for periodic broadcast ticks
225 */
226static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
227{
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000228 ktime_t next;
229
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800230 tick_do_periodic_broadcast();
231
232 /*
233 * The device is in periodic mode. No reprogramming necessary:
234 */
235 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
236 return;
237
238 /*
239 * Setup the next period for devices, which do not have
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000240 * periodic mode. We read dev->next_event first and add to it
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200241 * when the event already expired. clockevents_program_event()
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000242 * sets dev->next_event only when the event is really
243 * programmed to the device.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800244 */
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000245 for (next = dev->next_event; ;) {
246 next = ktime_add(next, tick_period);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800247
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200248 if (!clockevents_program_event(dev, next, false))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800249 return;
250 tick_do_periodic_broadcast();
251 }
252}
253
254/*
255 * Powerstate information: The system enters/leaves a state, where
256 * affected devices might stop
257 */
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700258static void tick_do_broadcast_on_off(unsigned long *reason)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800259{
260 struct clock_event_device *bc, *dev;
261 struct tick_device *td;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700262 unsigned long flags;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000263 int cpu, bc_stopped;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800264
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100265 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800266
267 cpu = smp_processor_id();
268 td = &per_cpu(tick_cpu_device, cpu);
269 dev = td->evtdev;
270 bc = tick_broadcast_device.evtdev;
271
272 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200273 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800274 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200275 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800276 goto out;
277
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200278 if (!tick_device_is_functional(dev))
279 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200280
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100281 bc_stopped = cpumask_empty(tick_broadcast_mask);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000282
Thomas Gleixner1595f452007-10-14 22:57:45 +0200283 switch (*reason) {
284 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
285 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100286 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200287 if (tick_broadcast_device.mode ==
288 TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700289 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800290 }
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200291 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200292 tick_broadcast_force = 1;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200293 break;
294 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200295 if (!tick_broadcast_force &&
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100296 cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200297 if (tick_broadcast_device.mode ==
298 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800299 tick_setup_periodic(dev, 0);
300 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200301 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800302 }
303
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100304 if (cpumask_empty(tick_broadcast_mask)) {
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000305 if (!bc_stopped)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700306 clockevents_shutdown(bc);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000307 } else if (bc_stopped) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800308 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
309 tick_broadcast_start_periodic(bc);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800310 else
311 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800312 }
313out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100314 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800315}
316
317/*
318 * Powerstate information: The system enters/leaves a state, where
319 * affected devices might stop.
320 */
321void tick_broadcast_on_off(unsigned long reason, int *oncpu)
322{
Rusty Russell6b954822009-01-01 10:12:25 +1030323 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
Glauber Costa833df312008-04-18 13:38:58 -0700324 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
Thomas Gleixner72fcde92007-05-23 13:57:30 -0700325 "offline CPU #%d\n", *oncpu);
Avi Kivitybf020cb2007-10-16 23:26:24 -0700326 else
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700327 tick_do_broadcast_on_off(&reason);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800328}
329
330/*
331 * Set the periodic handler depending on broadcast on/off
332 */
333void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
334{
335 if (!broadcast)
336 dev->event_handler = tick_handle_periodic;
337 else
338 dev->event_handler = tick_handle_periodic_broadcast;
339}
340
341/*
342 * Remove a CPU from broadcasting
343 */
344void tick_shutdown_broadcast(unsigned int *cpup)
345{
346 struct clock_event_device *bc;
347 unsigned long flags;
348 unsigned int cpu = *cpup;
349
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100350 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800351
352 bc = tick_broadcast_device.evtdev;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100353 cpumask_clear_cpu(cpu, tick_broadcast_mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800354
355 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100356 if (bc && cpumask_empty(tick_broadcast_mask))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700357 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800358 }
359
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100360 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800361}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800362
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100363void tick_suspend_broadcast(void)
364{
365 struct clock_event_device *bc;
366 unsigned long flags;
367
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100368 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100369
370 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700371 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700372 clockevents_shutdown(bc);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100373
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100374 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100375}
376
377int tick_resume_broadcast(void)
378{
379 struct clock_event_device *bc;
380 unsigned long flags;
381 int broadcast = 0;
382
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100383 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100384
385 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100386
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100387 if (bc) {
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700388 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
389
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100390 switch (tick_broadcast_device.mode) {
391 case TICKDEV_MODE_PERIODIC:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100392 if (!cpumask_empty(tick_broadcast_mask))
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100393 tick_broadcast_start_periodic(bc);
Rusty Russell6b954822009-01-01 10:12:25 +1030394 broadcast = cpumask_test_cpu(smp_processor_id(),
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100395 tick_broadcast_mask);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100396 break;
397 case TICKDEV_MODE_ONESHOT:
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100398 if (!cpumask_empty(tick_broadcast_mask))
Suresh Siddhaa6371f82012-04-18 19:27:39 -0700399 broadcast = tick_resume_broadcast_oneshot(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100400 break;
401 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100402 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100403 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100404
405 return broadcast;
406}
407
408
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800409#ifdef CONFIG_TICK_ONESHOT
410
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100411static cpumask_var_t tick_broadcast_oneshot_mask;
Thomas Gleixner26517f32013-03-06 11:18:35 +0000412static cpumask_var_t tick_broadcast_pending_mask;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000413static cpumask_var_t tick_broadcast_force_mask;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800414
Ingo Molnar289f4802007-02-16 01:28:15 -0800415/*
Rusty Russell6b954822009-01-01 10:12:25 +1030416 * Exposed for debugging: see timer_list.c
Ingo Molnar289f4802007-02-16 01:28:15 -0800417 */
Rusty Russell6b954822009-01-01 10:12:25 +1030418struct cpumask *tick_get_broadcast_oneshot_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -0800419{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100420 return tick_broadcast_oneshot_mask;
Ingo Molnar289f4802007-02-16 01:28:15 -0800421}
422
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100423/*
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000424 * Called before going idle with interrupts disabled. Checks whether a
425 * broadcast event from the other core is about to happen. We detected
426 * that in tick_broadcast_oneshot_control(). The callsite can use this
427 * to avoid a deep idle transition as we are about to get the
428 * broadcast IPI right away.
429 */
430int tick_check_broadcast_expired(void)
431{
432 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
433}
434
435/*
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100436 * Set broadcast interrupt affinity
437 */
438static void tick_broadcast_set_affinity(struct clock_event_device *bc,
439 const struct cpumask *cpumask)
440{
441 if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
442 return;
443
444 if (cpumask_equal(bc->cpumask, cpumask))
445 return;
446
447 bc->cpumask = cpumask;
448 irq_set_affinity(bc->irq, bc->cpumask);
449}
450
451static int tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
Daniel Lezcanof9ae39d2013-03-02 11:10:10 +0100452 ktime_t expires, int force)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800453{
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100454 int ret;
455
Thomas Gleixnerb9a6a2352012-04-18 17:31:58 +0200456 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
457 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
458
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100459 ret = clockevents_program_event(bc, expires, force);
460 if (!ret)
461 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
462 return ret;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800463}
464
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100465int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
466{
467 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb7e113d2007-09-22 22:29:06 +0000468 return 0;
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100469}
470
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800471/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200472 * Called from irq_enter() when idle was interrupted to reenable the
473 * per cpu device.
474 */
475void tick_check_oneshot_broadcast(int cpu)
476{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100477 if (cpumask_test_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200478 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
479
480 clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
481 }
482}
483
484/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800485 * Handle oneshot mode broadcasting
486 */
487static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
488{
489 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100490 ktime_t now, next_event;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100491 int cpu, next_cpu = 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800492
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100493 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800494again:
495 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100496 next_event.tv64 = KTIME_MAX;
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100497 cpumask_clear(tmpmask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800498 now = ktime_get();
499 /* Find all expired events */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100500 for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800501 td = &per_cpu(tick_cpu_device, cpu);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100502 if (td->evtdev->next_event.tv64 <= now.tv64) {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100503 cpumask_set_cpu(cpu, tmpmask);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000504 /*
505 * Mark the remote cpu in the pending mask, so
506 * it can avoid reprogramming the cpu local
507 * timer in tick_broadcast_oneshot_control().
508 */
509 cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100510 } else if (td->evtdev->next_event.tv64 < next_event.tv64) {
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100511 next_event.tv64 = td->evtdev->next_event.tv64;
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100512 next_cpu = cpu;
513 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800514 }
515
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000516 /* Take care of enforced broadcast requests */
517 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
518 cpumask_clear(tick_broadcast_force_mask);
519
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800520 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100521 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800522 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100523 tick_do_broadcast(tmpmask);
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100524
525 /*
526 * Two reasons for reprogram:
527 *
528 * - The global event did not expire any CPU local
529 * events. This happens in dyntick mode, as the maximum PIT
530 * delta is quite small.
531 *
532 * - There are pending events on sleeping CPUs which were not
533 * in the event mask
534 */
535 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800536 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100537 * Rearm the broadcast device. If event expired,
538 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800539 */
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100540 if (tick_broadcast_set_event(dev, next_cpu, next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800541 goto again;
542 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100543 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800544}
545
546/*
547 * Powerstate information: The system enters/leaves a state, where
548 * affected devices might stop
549 */
550void tick_broadcast_oneshot_control(unsigned long reason)
551{
552 struct clock_event_device *bc, *dev;
553 struct tick_device *td;
554 unsigned long flags;
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000555 ktime_t now;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800556 int cpu;
557
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800558 /*
559 * Periodic mode does not care about the enter/exit of power
560 * states
561 */
562 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Andi Kleen7372b0b2011-05-04 15:09:27 -0700563 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800564
Andi Kleen7372b0b2011-05-04 15:09:27 -0700565 /*
566 * We are called with preemtion disabled from the depth of the
567 * idle code, so we can't be moved away.
568 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800569 cpu = smp_processor_id();
570 td = &per_cpu(tick_cpu_device, cpu);
571 dev = td->evtdev;
572
573 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Andi Kleen7372b0b2011-05-04 15:09:27 -0700574 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800575
Andi Kleen7372b0b2011-05-04 15:09:27 -0700576 bc = tick_broadcast_device.evtdev;
577
578 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800579 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Thomas Gleixner26517f32013-03-06 11:18:35 +0000580 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100581 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800582 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000583 /*
584 * We only reprogram the broadcast timer if we
585 * did not mark ourself in the force mask and
586 * if the cpu local event is earlier than the
587 * broadcast event. If the current CPU is in
588 * the force mask, then we are going to be
589 * woken by the IPI right away.
590 */
591 if (!cpumask_test_cpu(cpu, tick_broadcast_force_mask) &&
592 dev->next_event.tv64 < bc->next_event.tv64)
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100593 tick_broadcast_set_event(bc, cpu, dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800594 }
595 } else {
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100596 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800597 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixner26517f32013-03-06 11:18:35 +0000598 if (dev->next_event.tv64 == KTIME_MAX)
599 goto out;
600 /*
601 * The cpu which was handling the broadcast
602 * timer marked this cpu in the broadcast
603 * pending mask and fired the broadcast
604 * IPI. So we are going to handle the expired
605 * event anyway via the broadcast IPI
606 * handler. No need to reprogram the timer
607 * with an already expired event.
608 */
609 if (cpumask_test_and_clear_cpu(cpu,
610 tick_broadcast_pending_mask))
611 goto out;
612
Thomas Gleixner989dcb62013-03-06 11:18:35 +0000613 /*
614 * If the pending bit is not set, then we are
615 * either the CPU handling the broadcast
616 * interrupt or we got woken by something else.
617 *
618 * We are not longer in the broadcast mask, so
619 * if the cpu local expiry time is already
620 * reached, we would reprogram the cpu local
621 * timer with an already expired event.
622 *
623 * This can lead to a ping-pong when we return
624 * to idle and therefor rearm the broadcast
625 * timer before the cpu local timer was able
626 * to fire. This happens because the forced
627 * reprogramming makes sure that the event
628 * will happen in the future and depending on
629 * the min_delta setting this might be far
630 * enough out that the ping-pong starts.
631 *
632 * If the cpu local next_event has expired
633 * then we know that the broadcast timer
634 * next_event has expired as well and
635 * broadcast is about to be handled. So we
636 * avoid reprogramming and enforce that the
637 * broadcast handler, which did not run yet,
638 * will invoke the cpu local handler.
639 *
640 * We cannot call the handler directly from
641 * here, because we might be in a NOHZ phase
642 * and we did not go through the irq_enter()
643 * nohz fixups.
644 */
645 now = ktime_get();
646 if (dev->next_event.tv64 <= now.tv64) {
647 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
648 goto out;
649 }
650 /*
651 * We got woken by something else. Reprogram
652 * the cpu local timer device.
653 */
Thomas Gleixner26517f32013-03-06 11:18:35 +0000654 tick_program_event(dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800655 }
656 }
Thomas Gleixner26517f32013-03-06 11:18:35 +0000657out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100658 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800659}
660
Thomas Gleixner5590a532007-07-21 04:37:35 -0700661/*
662 * Reset the one shot broadcast for a cpu
663 *
664 * Called with tick_broadcast_lock held
665 */
666static void tick_broadcast_clear_oneshot(int cpu)
667{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100668 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700669}
670
Rusty Russell6b954822009-01-01 10:12:25 +1030671static void tick_broadcast_init_next_event(struct cpumask *mask,
672 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200673{
674 struct tick_device *td;
675 int cpu;
676
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030677 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200678 td = &per_cpu(tick_cpu_device, cpu);
679 if (td->evtdev)
680 td->evtdev->next_event = expires;
681 }
682}
683
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800684/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800685 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800686 */
687void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
688{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200689 int cpu = smp_processor_id();
690
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000691 /* Set it up only once ! */
692 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200693 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200694
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000695 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200696
697 /* Take the do_timer update */
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200698 if (!tick_nohz_full_cpu(cpu))
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100699 tick_do_timer_cpu = cpu;
Thomas Gleixner73007112008-09-06 03:01:45 +0200700
701 /*
702 * We must be careful here. There might be other CPUs
703 * waiting for periodic broadcast. We need to set the
704 * oneshot_mask bits for those and program the
705 * broadcast device to fire.
706 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100707 cpumask_copy(tmpmask, tick_broadcast_mask);
708 cpumask_clear_cpu(cpu, tmpmask);
709 cpumask_or(tick_broadcast_oneshot_mask,
710 tick_broadcast_oneshot_mask, tmpmask);
Thomas Gleixner73007112008-09-06 03:01:45 +0200711
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100712 if (was_periodic && !cpumask_empty(tmpmask)) {
Thomas Gleixnerb4350922012-04-18 12:08:23 +0200713 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100714 tick_broadcast_init_next_event(tmpmask,
Rusty Russell6b954822009-01-01 10:12:25 +1030715 tick_next_period);
Daniel Lezcanod2348fb2013-03-02 11:10:11 +0100716 tick_broadcast_set_event(bc, cpu, tick_next_period, 1);
Thomas Gleixner73007112008-09-06 03:01:45 +0200717 } else
718 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200719 } else {
720 /*
721 * The first cpu which switches to oneshot mode sets
722 * the bit for all other cpus which are in the general
723 * (periodic) broadcast mask. So the bit is set and
724 * would prevent the first broadcast enter after this
725 * to program the bc device.
726 */
727 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000728 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800729}
730
731/*
732 * Select oneshot operating mode for the broadcast device
733 */
734void tick_broadcast_switch_to_oneshot(void)
735{
736 struct clock_event_device *bc;
737 unsigned long flags;
738
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100739 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700740
741 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800742 bc = tick_broadcast_device.evtdev;
743 if (bc)
744 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700745
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100746 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800747}
748
749
750/*
751 * Remove a dead CPU from broadcasting
752 */
753void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
754{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800755 unsigned long flags;
756 unsigned int cpu = *cpup;
757
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100758 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800759
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200760 /*
761 * Clear the broadcast mask flag for the dead cpu, but do not
762 * stop the broadcast device!
763 */
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100764 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800765
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100766 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800767}
768
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200769/*
770 * Check, whether the broadcast device is in one shot mode
771 */
772int tick_broadcast_oneshot_active(void)
773{
774 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
775}
776
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100777/*
778 * Check whether the broadcast device supports oneshot.
779 */
780bool tick_broadcast_oneshot_available(void)
781{
782 struct clock_event_device *bc = tick_broadcast_device.evtdev;
783
784 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
785}
786
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800787#endif
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100788
789void __init tick_broadcast_init(void)
790{
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200791 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
792 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100793#ifdef CONFIG_TICK_ONESHOT
Thomas Gleixnerfbd44a62013-05-03 20:22:36 +0200794 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
795 zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
796 zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100797#endif
798}