blob: a3a3123f6272b74486ea14503b914ac4e6f17160 [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;
Rusty Russell6b954822009-01-01 10:12:25 +103031/* FIXME: Use cpumask_var_t. */
32static DECLARE_BITMAP(tick_broadcast_mask, NR_CPUS);
33static DECLARE_BITMAP(tmpmask, NR_CPUS);
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{
Rusty Russell6b954822009-01-01 10:12:25 +103053 return to_cpumask(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 */
68int tick_check_broadcast_device(struct clock_event_device *dev)
69{
Venki Pallipadi4a932322007-10-12 23:04:23 +020070 if ((tick_broadcast_device.evtdev &&
71 tick_broadcast_device.evtdev->rating >= dev->rating) ||
72 (dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080073 return 0;
74
Thomas Gleixnerc1be8432011-12-02 12:34:16 +010075 clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080076 tick_broadcast_device.evtdev = dev;
Rusty Russell6b954822009-01-01 10:12:25 +103077 if (!cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080078 tick_broadcast_start_periodic(dev);
79 return 1;
80}
81
82/*
83 * Check, if the device is the broadcast device
84 */
85int tick_is_broadcast_device(struct clock_event_device *dev)
86{
87 return (dev && tick_broadcast_device.evtdev == dev);
88}
89
Mark Rutland12ad1002013-01-14 17:05:22 +000090static void err_broadcast(const struct cpumask *mask)
91{
92 pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
93}
94
Mark Rutland5d1d9a22013-02-08 15:24:07 +000095static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
96{
97 if (!dev->broadcast)
98 dev->broadcast = tick_broadcast;
99 if (!dev->broadcast) {
100 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
101 dev->name);
102 dev->broadcast = err_broadcast;
103 }
104}
105
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800106/*
107 * Check, if the device is disfunctional and a place holder, which
108 * needs to be handled by the broadcast device.
109 */
110int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
111{
112 unsigned long flags;
113 int ret = 0;
114
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100115 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800116
117 /*
118 * Devices might be registered with both periodic and oneshot
119 * mode disabled. This signals, that the device needs to be
120 * operated from the broadcast device and is a placeholder for
121 * the cpu local device.
122 */
123 if (!tick_device_is_functional(dev)) {
124 dev->event_handler = tick_handle_periodic;
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000125 tick_device_setup_broadcast_func(dev);
Rusty Russell6b954822009-01-01 10:12:25 +1030126 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800127 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
128 ret = 1;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700129 } else {
130 /*
131 * When the new device is not affected by the stop
132 * feature and the cpu is marked in the broadcast mask
133 * then clear the broadcast bit.
134 */
135 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
136 int cpu = smp_processor_id();
Rusty Russell6b954822009-01-01 10:12:25 +1030137 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner5590a532007-07-21 04:37:35 -0700138 tick_broadcast_clear_oneshot(cpu);
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000139 } else {
140 tick_device_setup_broadcast_func(dev);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700141 }
142 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100143 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800144 return ret;
145}
146
Mark Rutland12572db2013-01-14 17:05:21 +0000147#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
148int tick_receive_broadcast(void)
149{
150 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
151 struct clock_event_device *evt = td->evtdev;
152
153 if (!evt)
154 return -ENODEV;
155
156 if (!evt->event_handler)
157 return -EINVAL;
158
159 evt->event_handler(evt);
160 return 0;
161}
162#endif
163
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800164/*
Rusty Russell6b954822009-01-01 10:12:25 +1030165 * Broadcast the event to the cpus, which are set in the mask (mangled).
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800166 */
Rusty Russell6b954822009-01-01 10:12:25 +1030167static void tick_do_broadcast(struct cpumask *mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800168{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100169 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800170 struct tick_device *td;
171
172 /*
173 * Check, if the current cpu is in the mask
174 */
Rusty Russell6b954822009-01-01 10:12:25 +1030175 if (cpumask_test_cpu(cpu, mask)) {
176 cpumask_clear_cpu(cpu, mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800177 td = &per_cpu(tick_cpu_device, cpu);
178 td->evtdev->event_handler(td->evtdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800179 }
180
Rusty Russell6b954822009-01-01 10:12:25 +1030181 if (!cpumask_empty(mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800182 /*
183 * It might be necessary to actually check whether the devices
184 * have different broadcast functions. For now, just use the
185 * one of the first device. This works as long as we have this
186 * misfeature only on x86 (lapic)
187 */
Rusty Russell6b954822009-01-01 10:12:25 +1030188 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
189 td->evtdev->broadcast(mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800190 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800191}
192
193/*
194 * Periodic broadcast:
195 * - invoke the broadcast handlers
196 */
197static void tick_do_periodic_broadcast(void)
198{
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100199 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800200
Rusty Russell6b954822009-01-01 10:12:25 +1030201 cpumask_and(to_cpumask(tmpmask),
202 cpu_online_mask, tick_get_broadcast_mask());
203 tick_do_broadcast(to_cpumask(tmpmask));
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800204
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100205 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800206}
207
208/*
209 * Event handler for periodic broadcast ticks
210 */
211static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
212{
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000213 ktime_t next;
214
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800215 tick_do_periodic_broadcast();
216
217 /*
218 * The device is in periodic mode. No reprogramming necessary:
219 */
220 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
221 return;
222
223 /*
224 * Setup the next period for devices, which do not have
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000225 * periodic mode. We read dev->next_event first and add to it
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200226 * when the event already expired. clockevents_program_event()
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000227 * sets dev->next_event only when the event is really
228 * programmed to the device.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800229 */
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000230 for (next = dev->next_event; ;) {
231 next = ktime_add(next, tick_period);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800232
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200233 if (!clockevents_program_event(dev, next, false))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800234 return;
235 tick_do_periodic_broadcast();
236 }
237}
238
239/*
240 * Powerstate information: The system enters/leaves a state, where
241 * affected devices might stop
242 */
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700243static void tick_do_broadcast_on_off(unsigned long *reason)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800244{
245 struct clock_event_device *bc, *dev;
246 struct tick_device *td;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700247 unsigned long flags;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000248 int cpu, bc_stopped;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800249
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100250 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800251
252 cpu = smp_processor_id();
253 td = &per_cpu(tick_cpu_device, cpu);
254 dev = td->evtdev;
255 bc = tick_broadcast_device.evtdev;
256
257 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200258 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800259 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200260 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800261 goto out;
262
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200263 if (!tick_device_is_functional(dev))
264 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200265
Rusty Russell6b954822009-01-01 10:12:25 +1030266 bc_stopped = cpumask_empty(tick_get_broadcast_mask());
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000267
Thomas Gleixner1595f452007-10-14 22:57:45 +0200268 switch (*reason) {
269 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
270 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
Rusty Russell6b954822009-01-01 10:12:25 +1030271 if (!cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
272 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200273 if (tick_broadcast_device.mode ==
274 TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700275 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800276 }
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200277 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200278 tick_broadcast_force = 1;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200279 break;
280 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200281 if (!tick_broadcast_force &&
Rusty Russell6b954822009-01-01 10:12:25 +1030282 cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
283 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200284 if (tick_broadcast_device.mode ==
285 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800286 tick_setup_periodic(dev, 0);
287 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200288 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800289 }
290
Rusty Russell6b954822009-01-01 10:12:25 +1030291 if (cpumask_empty(tick_get_broadcast_mask())) {
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000292 if (!bc_stopped)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700293 clockevents_shutdown(bc);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000294 } else if (bc_stopped) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800295 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
296 tick_broadcast_start_periodic(bc);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800297 else
298 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800299 }
300out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100301 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800302}
303
304/*
305 * Powerstate information: The system enters/leaves a state, where
306 * affected devices might stop.
307 */
308void tick_broadcast_on_off(unsigned long reason, int *oncpu)
309{
Rusty Russell6b954822009-01-01 10:12:25 +1030310 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
Glauber Costa833df312008-04-18 13:38:58 -0700311 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
Thomas Gleixner72fcde92007-05-23 13:57:30 -0700312 "offline CPU #%d\n", *oncpu);
Avi Kivitybf020cb2007-10-16 23:26:24 -0700313 else
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700314 tick_do_broadcast_on_off(&reason);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800315}
316
317/*
318 * Set the periodic handler depending on broadcast on/off
319 */
320void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
321{
322 if (!broadcast)
323 dev->event_handler = tick_handle_periodic;
324 else
325 dev->event_handler = tick_handle_periodic_broadcast;
326}
327
328/*
329 * Remove a CPU from broadcasting
330 */
331void tick_shutdown_broadcast(unsigned int *cpup)
332{
333 struct clock_event_device *bc;
334 unsigned long flags;
335 unsigned int cpu = *cpup;
336
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100337 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800338
339 bc = tick_broadcast_device.evtdev;
Rusty Russell6b954822009-01-01 10:12:25 +1030340 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800341
342 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
Rusty Russell6b954822009-01-01 10:12:25 +1030343 if (bc && cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700344 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800345 }
346
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100347 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800348}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800349
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100350void tick_suspend_broadcast(void)
351{
352 struct clock_event_device *bc;
353 unsigned long flags;
354
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100355 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100356
357 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700358 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700359 clockevents_shutdown(bc);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100360
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100361 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100362}
363
364int tick_resume_broadcast(void)
365{
366 struct clock_event_device *bc;
367 unsigned long flags;
368 int broadcast = 0;
369
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100370 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100371
372 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100373
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100374 if (bc) {
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700375 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
376
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100377 switch (tick_broadcast_device.mode) {
378 case TICKDEV_MODE_PERIODIC:
Rusty Russell6b954822009-01-01 10:12:25 +1030379 if (!cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100380 tick_broadcast_start_periodic(bc);
Rusty Russell6b954822009-01-01 10:12:25 +1030381 broadcast = cpumask_test_cpu(smp_processor_id(),
382 tick_get_broadcast_mask());
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100383 break;
384 case TICKDEV_MODE_ONESHOT:
Suresh Siddhaa6371f82012-04-18 19:27:39 -0700385 if (!cpumask_empty(tick_get_broadcast_mask()))
386 broadcast = tick_resume_broadcast_oneshot(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100387 break;
388 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100389 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100390 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100391
392 return broadcast;
393}
394
395
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800396#ifdef CONFIG_TICK_ONESHOT
397
Rusty Russell6b954822009-01-01 10:12:25 +1030398/* FIXME: use cpumask_var_t. */
399static DECLARE_BITMAP(tick_broadcast_oneshot_mask, NR_CPUS);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800400
Ingo Molnar289f4802007-02-16 01:28:15 -0800401/*
Rusty Russell6b954822009-01-01 10:12:25 +1030402 * Exposed for debugging: see timer_list.c
Ingo Molnar289f4802007-02-16 01:28:15 -0800403 */
Rusty Russell6b954822009-01-01 10:12:25 +1030404struct cpumask *tick_get_broadcast_oneshot_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -0800405{
Rusty Russell6b954822009-01-01 10:12:25 +1030406 return to_cpumask(tick_broadcast_oneshot_mask);
Ingo Molnar289f4802007-02-16 01:28:15 -0800407}
408
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800409static int tick_broadcast_set_event(ktime_t expires, int force)
410{
411 struct clock_event_device *bc = tick_broadcast_device.evtdev;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800412
Thomas Gleixnerb9a6a2352012-04-18 17:31:58 +0200413 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
414 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
415
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200416 return clockevents_program_event(bc, expires, force);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800417}
418
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100419int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
420{
421 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb7e113d2007-09-22 22:29:06 +0000422 return 0;
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100423}
424
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800425/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200426 * Called from irq_enter() when idle was interrupted to reenable the
427 * per cpu device.
428 */
429void tick_check_oneshot_broadcast(int cpu)
430{
Rusty Russell6b954822009-01-01 10:12:25 +1030431 if (cpumask_test_cpu(cpu, to_cpumask(tick_broadcast_oneshot_mask))) {
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200432 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
433
434 clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
435 }
436}
437
438/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800439 * Handle oneshot mode broadcasting
440 */
441static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
442{
443 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100444 ktime_t now, next_event;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800445 int cpu;
446
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100447 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800448again:
449 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100450 next_event.tv64 = KTIME_MAX;
Rusty Russell6b954822009-01-01 10:12:25 +1030451 cpumask_clear(to_cpumask(tmpmask));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800452 now = ktime_get();
453 /* Find all expired events */
Rusty Russell6b954822009-01-01 10:12:25 +1030454 for_each_cpu(cpu, tick_get_broadcast_oneshot_mask()) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800455 td = &per_cpu(tick_cpu_device, cpu);
456 if (td->evtdev->next_event.tv64 <= now.tv64)
Rusty Russell6b954822009-01-01 10:12:25 +1030457 cpumask_set_cpu(cpu, to_cpumask(tmpmask));
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100458 else if (td->evtdev->next_event.tv64 < next_event.tv64)
459 next_event.tv64 = td->evtdev->next_event.tv64;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800460 }
461
462 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100463 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800464 */
Rusty Russell6b954822009-01-01 10:12:25 +1030465 tick_do_broadcast(to_cpumask(tmpmask));
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100466
467 /*
468 * Two reasons for reprogram:
469 *
470 * - The global event did not expire any CPU local
471 * events. This happens in dyntick mode, as the maximum PIT
472 * delta is quite small.
473 *
474 * - There are pending events on sleeping CPUs which were not
475 * in the event mask
476 */
477 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800478 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100479 * Rearm the broadcast device. If event expired,
480 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800481 */
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100482 if (tick_broadcast_set_event(next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800483 goto again;
484 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100485 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800486}
487
488/*
489 * Powerstate information: The system enters/leaves a state, where
490 * affected devices might stop
491 */
492void tick_broadcast_oneshot_control(unsigned long reason)
493{
494 struct clock_event_device *bc, *dev;
495 struct tick_device *td;
496 unsigned long flags;
497 int cpu;
498
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800499 /*
500 * Periodic mode does not care about the enter/exit of power
501 * states
502 */
503 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Andi Kleen7372b0b2011-05-04 15:09:27 -0700504 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800505
Andi Kleen7372b0b2011-05-04 15:09:27 -0700506 /*
507 * We are called with preemtion disabled from the depth of the
508 * idle code, so we can't be moved away.
509 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800510 cpu = smp_processor_id();
511 td = &per_cpu(tick_cpu_device, cpu);
512 dev = td->evtdev;
513
514 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Andi Kleen7372b0b2011-05-04 15:09:27 -0700515 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800516
Andi Kleen7372b0b2011-05-04 15:09:27 -0700517 bc = tick_broadcast_device.evtdev;
518
519 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800520 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Rusty Russell6b954822009-01-01 10:12:25 +1030521 if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
522 cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800523 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
524 if (dev->next_event.tv64 < bc->next_event.tv64)
525 tick_broadcast_set_event(dev->next_event, 1);
526 }
527 } else {
Rusty Russell6b954822009-01-01 10:12:25 +1030528 if (cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
529 cpumask_clear_cpu(cpu,
530 tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800531 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
532 if (dev->next_event.tv64 != KTIME_MAX)
533 tick_program_event(dev->next_event, 1);
534 }
535 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100536 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800537}
538
Thomas Gleixner5590a532007-07-21 04:37:35 -0700539/*
540 * Reset the one shot broadcast for a cpu
541 *
542 * Called with tick_broadcast_lock held
543 */
544static void tick_broadcast_clear_oneshot(int cpu)
545{
Rusty Russell6b954822009-01-01 10:12:25 +1030546 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner5590a532007-07-21 04:37:35 -0700547}
548
Rusty Russell6b954822009-01-01 10:12:25 +1030549static void tick_broadcast_init_next_event(struct cpumask *mask,
550 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200551{
552 struct tick_device *td;
553 int cpu;
554
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030555 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200556 td = &per_cpu(tick_cpu_device, cpu);
557 if (td->evtdev)
558 td->evtdev->next_event = expires;
559 }
560}
561
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800562/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800563 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800564 */
565void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
566{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200567 int cpu = smp_processor_id();
568
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000569 /* Set it up only once ! */
570 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200571 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200572
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000573 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200574
575 /* Take the do_timer update */
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200576 if (!tick_nohz_full_cpu(cpu))
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100577 tick_do_timer_cpu = cpu;
Thomas Gleixner73007112008-09-06 03:01:45 +0200578
579 /*
580 * We must be careful here. There might be other CPUs
581 * waiting for periodic broadcast. We need to set the
582 * oneshot_mask bits for those and program the
583 * broadcast device to fire.
584 */
Rusty Russell6b954822009-01-01 10:12:25 +1030585 cpumask_copy(to_cpumask(tmpmask), tick_get_broadcast_mask());
586 cpumask_clear_cpu(cpu, to_cpumask(tmpmask));
587 cpumask_or(tick_get_broadcast_oneshot_mask(),
588 tick_get_broadcast_oneshot_mask(),
589 to_cpumask(tmpmask));
Thomas Gleixner73007112008-09-06 03:01:45 +0200590
Rusty Russell6b954822009-01-01 10:12:25 +1030591 if (was_periodic && !cpumask_empty(to_cpumask(tmpmask))) {
Thomas Gleixnerb4350922012-04-18 12:08:23 +0200592 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Rusty Russell6b954822009-01-01 10:12:25 +1030593 tick_broadcast_init_next_event(to_cpumask(tmpmask),
594 tick_next_period);
Thomas Gleixner73007112008-09-06 03:01:45 +0200595 tick_broadcast_set_event(tick_next_period, 1);
596 } else
597 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200598 } else {
599 /*
600 * The first cpu which switches to oneshot mode sets
601 * the bit for all other cpus which are in the general
602 * (periodic) broadcast mask. So the bit is set and
603 * would prevent the first broadcast enter after this
604 * to program the bc device.
605 */
606 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000607 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800608}
609
610/*
611 * Select oneshot operating mode for the broadcast device
612 */
613void tick_broadcast_switch_to_oneshot(void)
614{
615 struct clock_event_device *bc;
616 unsigned long flags;
617
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100618 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700619
620 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800621 bc = tick_broadcast_device.evtdev;
622 if (bc)
623 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700624
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100625 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800626}
627
628
629/*
630 * Remove a dead CPU from broadcasting
631 */
632void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
633{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800634 unsigned long flags;
635 unsigned int cpu = *cpup;
636
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100637 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800638
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200639 /*
640 * Clear the broadcast mask flag for the dead cpu, but do not
641 * stop the broadcast device!
642 */
Rusty Russell6b954822009-01-01 10:12:25 +1030643 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800644
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100645 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800646}
647
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200648/*
649 * Check, whether the broadcast device is in one shot mode
650 */
651int tick_broadcast_oneshot_active(void)
652{
653 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
654}
655
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100656/*
657 * Check whether the broadcast device supports oneshot.
658 */
659bool tick_broadcast_oneshot_available(void)
660{
661 struct clock_event_device *bc = tick_broadcast_device.evtdev;
662
663 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
664}
665
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800666#endif