blob: a13987af44fdad01a8f66c82fafc16c720269435 [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>
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080021
22#include "tick-internal.h"
23
24/*
25 * Broadcast support for broken x86 hardware, where the local apic
26 * timer stops in C3 state.
27 */
28
Dmitri Vorobieva52f5c52009-05-01 13:10:21 -070029static struct tick_device tick_broadcast_device;
Rusty Russell6b954822009-01-01 10:12:25 +103030/* FIXME: Use cpumask_var_t. */
31static DECLARE_BITMAP(tick_broadcast_mask, NR_CPUS);
32static DECLARE_BITMAP(tmpmask, NR_CPUS);
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{
Rusty Russell6b954822009-01-01 10:12:25 +103052 return to_cpumask(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 */
67int tick_check_broadcast_device(struct clock_event_device *dev)
68{
Mark Rutland8d96fce2013-03-07 15:09:24 +000069 if ((dev->features & CLOCK_EVT_FEAT_DUMMY) ||
70 (tick_broadcast_device.evtdev &&
Venki Pallipadi4a932322007-10-12 23:04:23 +020071 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
90/*
91 * Check, if the device is disfunctional and a place holder, which
92 * needs to be handled by the broadcast device.
93 */
94int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
95{
96 unsigned long flags;
97 int ret = 0;
98
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +010099 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800100
101 /*
102 * Devices might be registered with both periodic and oneshot
103 * mode disabled. This signals, that the device needs to be
104 * operated from the broadcast device and is a placeholder for
105 * the cpu local device.
106 */
107 if (!tick_device_is_functional(dev)) {
108 dev->event_handler = tick_handle_periodic;
Rusty Russell6b954822009-01-01 10:12:25 +1030109 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800110 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
111 ret = 1;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700112 } else {
113 /*
114 * When the new device is not affected by the stop
115 * feature and the cpu is marked in the broadcast mask
116 * then clear the broadcast bit.
117 */
118 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
119 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800120
Rusty Russell6b954822009-01-01 10:12:25 +1030121 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner5590a532007-07-21 04:37:35 -0700122 tick_broadcast_clear_oneshot(cpu);
123 }
124 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100125 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800126 return ret;
127}
128
129/*
Rusty Russell6b954822009-01-01 10:12:25 +1030130 * Broadcast the event to the cpus, which are set in the mask (mangled).
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800131 */
Rusty Russell6b954822009-01-01 10:12:25 +1030132static void tick_do_broadcast(struct cpumask *mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800133{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100134 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800135 struct tick_device *td;
136
137 /*
138 * Check, if the current cpu is in the mask
139 */
Rusty Russell6b954822009-01-01 10:12:25 +1030140 if (cpumask_test_cpu(cpu, mask)) {
141 cpumask_clear_cpu(cpu, mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800142 td = &per_cpu(tick_cpu_device, cpu);
143 td->evtdev->event_handler(td->evtdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800144 }
145
Rusty Russell6b954822009-01-01 10:12:25 +1030146 if (!cpumask_empty(mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800147 /*
148 * It might be necessary to actually check whether the devices
149 * have different broadcast functions. For now, just use the
150 * one of the first device. This works as long as we have this
151 * misfeature only on x86 (lapic)
152 */
Rusty Russell6b954822009-01-01 10:12:25 +1030153 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
154 td->evtdev->broadcast(mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800155 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800156}
157
158/*
159 * Periodic broadcast:
160 * - invoke the broadcast handlers
161 */
162static void tick_do_periodic_broadcast(void)
163{
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100164 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800165
Rusty Russell6b954822009-01-01 10:12:25 +1030166 cpumask_and(to_cpumask(tmpmask),
167 cpu_online_mask, tick_get_broadcast_mask());
168 tick_do_broadcast(to_cpumask(tmpmask));
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800169
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100170 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800171}
172
173/*
174 * Event handler for periodic broadcast ticks
175 */
176static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
177{
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000178 ktime_t next;
179
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800180 tick_do_periodic_broadcast();
181
182 /*
183 * The device is in periodic mode. No reprogramming necessary:
184 */
185 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
186 return;
187
188 /*
189 * Setup the next period for devices, which do not have
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000190 * periodic mode. We read dev->next_event first and add to it
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200191 * when the event already expired. clockevents_program_event()
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000192 * sets dev->next_event only when the event is really
193 * programmed to the device.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800194 */
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000195 for (next = dev->next_event; ;) {
196 next = ktime_add(next, tick_period);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800197
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200198 if (!clockevents_program_event(dev, next, false))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800199 return;
200 tick_do_periodic_broadcast();
201 }
202}
203
204/*
205 * Powerstate information: The system enters/leaves a state, where
206 * affected devices might stop
207 */
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700208static void tick_do_broadcast_on_off(unsigned long *reason)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800209{
210 struct clock_event_device *bc, *dev;
211 struct tick_device *td;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700212 unsigned long flags;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000213 int cpu, bc_stopped;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800214
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100215 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800216
217 cpu = smp_processor_id();
218 td = &per_cpu(tick_cpu_device, cpu);
219 dev = td->evtdev;
220 bc = tick_broadcast_device.evtdev;
221
222 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200223 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800224 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200225 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800226 goto out;
227
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200228 if (!tick_device_is_functional(dev))
229 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200230
Rusty Russell6b954822009-01-01 10:12:25 +1030231 bc_stopped = cpumask_empty(tick_get_broadcast_mask());
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000232
Thomas Gleixner1595f452007-10-14 22:57:45 +0200233 switch (*reason) {
234 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
235 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
Rusty Russell6b954822009-01-01 10:12:25 +1030236 if (!cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
237 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200238 if (tick_broadcast_device.mode ==
239 TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700240 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800241 }
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200242 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200243 tick_broadcast_force = 1;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200244 break;
245 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200246 if (!tick_broadcast_force &&
Rusty Russell6b954822009-01-01 10:12:25 +1030247 cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
248 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200249 if (tick_broadcast_device.mode ==
250 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800251 tick_setup_periodic(dev, 0);
252 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200253 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800254 }
255
Rusty Russell6b954822009-01-01 10:12:25 +1030256 if (cpumask_empty(tick_get_broadcast_mask())) {
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000257 if (!bc_stopped)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700258 clockevents_shutdown(bc);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000259 } else if (bc_stopped) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800260 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
261 tick_broadcast_start_periodic(bc);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800262 else
263 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800264 }
265out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100266 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800267}
268
269/*
270 * Powerstate information: The system enters/leaves a state, where
271 * affected devices might stop.
272 */
273void tick_broadcast_on_off(unsigned long reason, int *oncpu)
274{
Rusty Russell6b954822009-01-01 10:12:25 +1030275 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
Glauber Costa833df312008-04-18 13:38:58 -0700276 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
Thomas Gleixner72fcde92007-05-23 13:57:30 -0700277 "offline CPU #%d\n", *oncpu);
Avi Kivitybf020cb2007-10-16 23:26:24 -0700278 else
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700279 tick_do_broadcast_on_off(&reason);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800280}
281
282/*
283 * Set the periodic handler depending on broadcast on/off
284 */
285void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
286{
287 if (!broadcast)
288 dev->event_handler = tick_handle_periodic;
289 else
290 dev->event_handler = tick_handle_periodic_broadcast;
291}
292
293/*
294 * Remove a CPU from broadcasting
295 */
296void tick_shutdown_broadcast(unsigned int *cpup)
297{
298 struct clock_event_device *bc;
299 unsigned long flags;
300 unsigned int cpu = *cpup;
301
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100302 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800303
304 bc = tick_broadcast_device.evtdev;
Rusty Russell6b954822009-01-01 10:12:25 +1030305 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800306
307 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
Rusty Russell6b954822009-01-01 10:12:25 +1030308 if (bc && cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700309 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800310 }
311
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100312 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800313}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800314
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100315void tick_suspend_broadcast(void)
316{
317 struct clock_event_device *bc;
318 unsigned long flags;
319
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100320 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100321
322 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700323 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700324 clockevents_shutdown(bc);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100325
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100326 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100327}
328
329int tick_resume_broadcast(void)
330{
331 struct clock_event_device *bc;
332 unsigned long flags;
333 int broadcast = 0;
334
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100335 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100336
337 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100338
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100339 if (bc) {
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700340 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
341
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100342 switch (tick_broadcast_device.mode) {
343 case TICKDEV_MODE_PERIODIC:
Rusty Russell6b954822009-01-01 10:12:25 +1030344 if (!cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100345 tick_broadcast_start_periodic(bc);
Rusty Russell6b954822009-01-01 10:12:25 +1030346 broadcast = cpumask_test_cpu(smp_processor_id(),
347 tick_get_broadcast_mask());
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100348 break;
349 case TICKDEV_MODE_ONESHOT:
Suresh Siddhaa6371f82012-04-18 19:27:39 -0700350 if (!cpumask_empty(tick_get_broadcast_mask()))
351 broadcast = tick_resume_broadcast_oneshot(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100352 break;
353 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100354 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100355 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100356
357 return broadcast;
358}
359
360
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800361#ifdef CONFIG_TICK_ONESHOT
362
Rusty Russell6b954822009-01-01 10:12:25 +1030363/* FIXME: use cpumask_var_t. */
364static DECLARE_BITMAP(tick_broadcast_oneshot_mask, NR_CPUS);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800365
Ingo Molnar289f4802007-02-16 01:28:15 -0800366/*
Rusty Russell6b954822009-01-01 10:12:25 +1030367 * Exposed for debugging: see timer_list.c
Ingo Molnar289f4802007-02-16 01:28:15 -0800368 */
Rusty Russell6b954822009-01-01 10:12:25 +1030369struct cpumask *tick_get_broadcast_oneshot_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -0800370{
Rusty Russell6b954822009-01-01 10:12:25 +1030371 return to_cpumask(tick_broadcast_oneshot_mask);
Ingo Molnar289f4802007-02-16 01:28:15 -0800372}
373
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800374static int tick_broadcast_set_event(ktime_t expires, int force)
375{
376 struct clock_event_device *bc = tick_broadcast_device.evtdev;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800377
Thomas Gleixnerb9a6a232012-04-18 17:31:58 +0200378 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
379 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
380
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200381 return clockevents_program_event(bc, expires, force);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800382}
383
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100384int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
385{
386 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb7e113d2007-09-22 22:29:06 +0000387 return 0;
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100388}
389
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800390/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200391 * Called from irq_enter() when idle was interrupted to reenable the
392 * per cpu device.
393 */
394void tick_check_oneshot_broadcast(int cpu)
395{
Rusty Russell6b954822009-01-01 10:12:25 +1030396 if (cpumask_test_cpu(cpu, to_cpumask(tick_broadcast_oneshot_mask))) {
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200397 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
398
399 clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
400 }
401}
402
403/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800404 * Handle oneshot mode broadcasting
405 */
406static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
407{
408 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100409 ktime_t now, next_event;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800410 int cpu;
411
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100412 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800413again:
414 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100415 next_event.tv64 = KTIME_MAX;
Rusty Russell6b954822009-01-01 10:12:25 +1030416 cpumask_clear(to_cpumask(tmpmask));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800417 now = ktime_get();
418 /* Find all expired events */
Rusty Russell6b954822009-01-01 10:12:25 +1030419 for_each_cpu(cpu, tick_get_broadcast_oneshot_mask()) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800420 td = &per_cpu(tick_cpu_device, cpu);
421 if (td->evtdev->next_event.tv64 <= now.tv64)
Rusty Russell6b954822009-01-01 10:12:25 +1030422 cpumask_set_cpu(cpu, to_cpumask(tmpmask));
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100423 else if (td->evtdev->next_event.tv64 < next_event.tv64)
424 next_event.tv64 = td->evtdev->next_event.tv64;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800425 }
426
427 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100428 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800429 */
Rusty Russell6b954822009-01-01 10:12:25 +1030430 tick_do_broadcast(to_cpumask(tmpmask));
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100431
432 /*
433 * Two reasons for reprogram:
434 *
435 * - The global event did not expire any CPU local
436 * events. This happens in dyntick mode, as the maximum PIT
437 * delta is quite small.
438 *
439 * - There are pending events on sleeping CPUs which were not
440 * in the event mask
441 */
442 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800443 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100444 * Rearm the broadcast device. If event expired,
445 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800446 */
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100447 if (tick_broadcast_set_event(next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800448 goto again;
449 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100450 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800451}
452
453/*
454 * Powerstate information: The system enters/leaves a state, where
455 * affected devices might stop
456 */
457void tick_broadcast_oneshot_control(unsigned long reason)
458{
459 struct clock_event_device *bc, *dev;
460 struct tick_device *td;
461 unsigned long flags;
462 int cpu;
463
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800464 /*
465 * Periodic mode does not care about the enter/exit of power
466 * states
467 */
468 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Andi Kleen7372b0b2011-05-04 15:09:27 -0700469 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800470
Andi Kleen7372b0b2011-05-04 15:09:27 -0700471 /*
472 * We are called with preemtion disabled from the depth of the
473 * idle code, so we can't be moved away.
474 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800475 cpu = smp_processor_id();
476 td = &per_cpu(tick_cpu_device, cpu);
477 dev = td->evtdev;
478
479 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Andi Kleen7372b0b2011-05-04 15:09:27 -0700480 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800481
Andi Kleen7372b0b2011-05-04 15:09:27 -0700482 bc = tick_broadcast_device.evtdev;
483
484 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800485 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Rusty Russell6b954822009-01-01 10:12:25 +1030486 if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
487 cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800488 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
489 if (dev->next_event.tv64 < bc->next_event.tv64)
490 tick_broadcast_set_event(dev->next_event, 1);
491 }
492 } else {
Rusty Russell6b954822009-01-01 10:12:25 +1030493 if (cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
494 cpumask_clear_cpu(cpu,
495 tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800496 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
497 if (dev->next_event.tv64 != KTIME_MAX)
498 tick_program_event(dev->next_event, 1);
499 }
500 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100501 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800502}
503
Thomas Gleixner5590a532007-07-21 04:37:35 -0700504/*
505 * Reset the one shot broadcast for a cpu
506 *
507 * Called with tick_broadcast_lock held
508 */
509static void tick_broadcast_clear_oneshot(int cpu)
510{
Rusty Russell6b954822009-01-01 10:12:25 +1030511 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner5590a532007-07-21 04:37:35 -0700512}
513
Rusty Russell6b954822009-01-01 10:12:25 +1030514static void tick_broadcast_init_next_event(struct cpumask *mask,
515 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200516{
517 struct tick_device *td;
518 int cpu;
519
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030520 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200521 td = &per_cpu(tick_cpu_device, cpu);
522 if (td->evtdev)
523 td->evtdev->next_event = expires;
524 }
525}
526
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800527/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800528 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800529 */
530void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
531{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200532 int cpu = smp_processor_id();
533
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000534 /* Set it up only once ! */
535 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200536 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200537
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000538 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200539
540 /* Take the do_timer update */
541 tick_do_timer_cpu = cpu;
542
543 /*
544 * We must be careful here. There might be other CPUs
545 * waiting for periodic broadcast. We need to set the
546 * oneshot_mask bits for those and program the
547 * broadcast device to fire.
548 */
Rusty Russell6b954822009-01-01 10:12:25 +1030549 cpumask_copy(to_cpumask(tmpmask), tick_get_broadcast_mask());
550 cpumask_clear_cpu(cpu, to_cpumask(tmpmask));
551 cpumask_or(tick_get_broadcast_oneshot_mask(),
552 tick_get_broadcast_oneshot_mask(),
553 to_cpumask(tmpmask));
Thomas Gleixner73007112008-09-06 03:01:45 +0200554
Rusty Russell6b954822009-01-01 10:12:25 +1030555 if (was_periodic && !cpumask_empty(to_cpumask(tmpmask))) {
Thomas Gleixnerb4350922012-04-18 12:08:23 +0200556 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Rusty Russell6b954822009-01-01 10:12:25 +1030557 tick_broadcast_init_next_event(to_cpumask(tmpmask),
558 tick_next_period);
Thomas Gleixner73007112008-09-06 03:01:45 +0200559 tick_broadcast_set_event(tick_next_period, 1);
560 } else
561 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200562 } else {
563 /*
564 * The first cpu which switches to oneshot mode sets
565 * the bit for all other cpus which are in the general
566 * (periodic) broadcast mask. So the bit is set and
567 * would prevent the first broadcast enter after this
568 * to program the bc device.
569 */
570 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000571 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800572}
573
574/*
575 * Select oneshot operating mode for the broadcast device
576 */
577void tick_broadcast_switch_to_oneshot(void)
578{
579 struct clock_event_device *bc;
580 unsigned long flags;
581
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100582 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700583
584 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800585 bc = tick_broadcast_device.evtdev;
586 if (bc)
587 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700588
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100589 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800590}
591
592
593/*
594 * Remove a dead CPU from broadcasting
595 */
596void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
597{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800598 unsigned long flags;
599 unsigned int cpu = *cpup;
600
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100601 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800602
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200603 /*
604 * Clear the broadcast mask flag for the dead cpu, but do not
605 * stop the broadcast device!
606 */
Rusty Russell6b954822009-01-01 10:12:25 +1030607 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800608
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100609 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800610}
611
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200612/*
613 * Check, whether the broadcast device is in one shot mode
614 */
615int tick_broadcast_oneshot_active(void)
616{
617 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
618}
619
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100620/*
621 * Check whether the broadcast device supports oneshot.
622 */
623bool tick_broadcast_oneshot_available(void)
624{
625 struct clock_event_device *bc = tick_broadcast_device.evtdev;
626
627 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
628}
629
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800630#endif