blob: edb173f3c91b7b4c2292a144c9b9397298e14054 [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{
Thomas Gleixner357093a2013-04-25 11:45:53 +020069 struct clock_event_device *cur = tick_broadcast_device.evtdev;
70
Mark Rutland8d96fce2013-03-07 15:09:24 +000071 if ((dev->features & CLOCK_EVT_FEAT_DUMMY) ||
72 (tick_broadcast_device.evtdev &&
Venki Pallipadi4a932322007-10-12 23:04:23 +020073 tick_broadcast_device.evtdev->rating >= dev->rating) ||
74 (dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080075 return 0;
76
Thomas Gleixnerc1be8432011-12-02 12:34:16 +010077 clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
Thomas Gleixner357093a2013-04-25 11:45:53 +020078 if (cur)
79 cur->event_handler = clockevents_handle_noop;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080080 tick_broadcast_device.evtdev = dev;
Rusty Russell6b954822009-01-01 10:12:25 +103081 if (!cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080082 tick_broadcast_start_periodic(dev);
83 return 1;
84}
85
86/*
87 * Check, if the device is the broadcast device
88 */
89int tick_is_broadcast_device(struct clock_event_device *dev)
90{
91 return (dev && tick_broadcast_device.evtdev == dev);
92}
93
94/*
95 * Check, if the device is disfunctional and a place holder, which
96 * needs to be handled by the broadcast device.
97 */
98int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
99{
100 unsigned long flags;
101 int ret = 0;
102
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100103 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800104
105 /*
106 * Devices might be registered with both periodic and oneshot
107 * mode disabled. This signals, that the device needs to be
108 * operated from the broadcast device and is a placeholder for
109 * the cpu local device.
110 */
111 if (!tick_device_is_functional(dev)) {
112 dev->event_handler = tick_handle_periodic;
Rusty Russell6b954822009-01-01 10:12:25 +1030113 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800114 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
115 ret = 1;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700116 } else {
117 /*
118 * When the new device is not affected by the stop
119 * feature and the cpu is marked in the broadcast mask
120 * then clear the broadcast bit.
121 */
122 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
123 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800124
Rusty Russell6b954822009-01-01 10:12:25 +1030125 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner5590a532007-07-21 04:37:35 -0700126 tick_broadcast_clear_oneshot(cpu);
127 }
128 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100129 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800130 return ret;
131}
132
133/*
Rusty Russell6b954822009-01-01 10:12:25 +1030134 * Broadcast the event to the cpus, which are set in the mask (mangled).
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800135 */
Rusty Russell6b954822009-01-01 10:12:25 +1030136static void tick_do_broadcast(struct cpumask *mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800137{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100138 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800139 struct tick_device *td;
140
141 /*
142 * Check, if the current cpu is in the mask
143 */
Rusty Russell6b954822009-01-01 10:12:25 +1030144 if (cpumask_test_cpu(cpu, mask)) {
145 cpumask_clear_cpu(cpu, mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800146 td = &per_cpu(tick_cpu_device, cpu);
147 td->evtdev->event_handler(td->evtdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800148 }
149
Rusty Russell6b954822009-01-01 10:12:25 +1030150 if (!cpumask_empty(mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800151 /*
152 * It might be necessary to actually check whether the devices
153 * have different broadcast functions. For now, just use the
154 * one of the first device. This works as long as we have this
155 * misfeature only on x86 (lapic)
156 */
Rusty Russell6b954822009-01-01 10:12:25 +1030157 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
158 td->evtdev->broadcast(mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800159 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800160}
161
162/*
163 * Periodic broadcast:
164 * - invoke the broadcast handlers
165 */
166static void tick_do_periodic_broadcast(void)
167{
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100168 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800169
Rusty Russell6b954822009-01-01 10:12:25 +1030170 cpumask_and(to_cpumask(tmpmask),
171 cpu_online_mask, tick_get_broadcast_mask());
172 tick_do_broadcast(to_cpumask(tmpmask));
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800173
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100174 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800175}
176
177/*
178 * Event handler for periodic broadcast ticks
179 */
180static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
181{
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000182 ktime_t next;
183
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800184 tick_do_periodic_broadcast();
185
186 /*
187 * The device is in periodic mode. No reprogramming necessary:
188 */
189 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
190 return;
191
192 /*
193 * Setup the next period for devices, which do not have
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000194 * periodic mode. We read dev->next_event first and add to it
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200195 * when the event already expired. clockevents_program_event()
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000196 * sets dev->next_event only when the event is really
197 * programmed to the device.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800198 */
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000199 for (next = dev->next_event; ;) {
200 next = ktime_add(next, tick_period);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800201
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200202 if (!clockevents_program_event(dev, next, false))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800203 return;
204 tick_do_periodic_broadcast();
205 }
206}
207
208/*
209 * Powerstate information: The system enters/leaves a state, where
210 * affected devices might stop
211 */
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700212static void tick_do_broadcast_on_off(unsigned long *reason)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800213{
214 struct clock_event_device *bc, *dev;
215 struct tick_device *td;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700216 unsigned long flags;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000217 int cpu, bc_stopped;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800218
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100219 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800220
221 cpu = smp_processor_id();
222 td = &per_cpu(tick_cpu_device, cpu);
223 dev = td->evtdev;
224 bc = tick_broadcast_device.evtdev;
225
226 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200227 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800228 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200229 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800230 goto out;
231
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200232 if (!tick_device_is_functional(dev))
233 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200234
Rusty Russell6b954822009-01-01 10:12:25 +1030235 bc_stopped = cpumask_empty(tick_get_broadcast_mask());
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000236
Thomas Gleixner1595f452007-10-14 22:57:45 +0200237 switch (*reason) {
238 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
239 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
Rusty Russell6b954822009-01-01 10:12:25 +1030240 if (!cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
241 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200242 if (tick_broadcast_device.mode ==
243 TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700244 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800245 }
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200246 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200247 tick_broadcast_force = 1;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200248 break;
249 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200250 if (!tick_broadcast_force &&
Rusty Russell6b954822009-01-01 10:12:25 +1030251 cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
252 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200253 if (tick_broadcast_device.mode ==
254 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800255 tick_setup_periodic(dev, 0);
256 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200257 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800258 }
259
Rusty Russell6b954822009-01-01 10:12:25 +1030260 if (cpumask_empty(tick_get_broadcast_mask())) {
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000261 if (!bc_stopped)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700262 clockevents_shutdown(bc);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000263 } else if (bc_stopped) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800264 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
265 tick_broadcast_start_periodic(bc);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800266 else
267 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800268 }
269out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100270 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800271}
272
273/*
274 * Powerstate information: The system enters/leaves a state, where
275 * affected devices might stop.
276 */
277void tick_broadcast_on_off(unsigned long reason, int *oncpu)
278{
Rusty Russell6b954822009-01-01 10:12:25 +1030279 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
Glauber Costa833df312008-04-18 13:38:58 -0700280 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
Thomas Gleixner72fcde92007-05-23 13:57:30 -0700281 "offline CPU #%d\n", *oncpu);
Avi Kivitybf020cb2007-10-16 23:26:24 -0700282 else
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700283 tick_do_broadcast_on_off(&reason);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800284}
285
286/*
287 * Set the periodic handler depending on broadcast on/off
288 */
289void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
290{
291 if (!broadcast)
292 dev->event_handler = tick_handle_periodic;
293 else
294 dev->event_handler = tick_handle_periodic_broadcast;
295}
296
297/*
298 * Remove a CPU from broadcasting
299 */
300void tick_shutdown_broadcast(unsigned int *cpup)
301{
302 struct clock_event_device *bc;
303 unsigned long flags;
304 unsigned int cpu = *cpup;
305
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100306 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800307
308 bc = tick_broadcast_device.evtdev;
Rusty Russell6b954822009-01-01 10:12:25 +1030309 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800310
311 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
Rusty Russell6b954822009-01-01 10:12:25 +1030312 if (bc && cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700313 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800314 }
315
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100316 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800317}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800318
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100319void tick_suspend_broadcast(void)
320{
321 struct clock_event_device *bc;
322 unsigned long flags;
323
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100324 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100325
326 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700327 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700328 clockevents_shutdown(bc);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100329
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100330 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100331}
332
333int tick_resume_broadcast(void)
334{
335 struct clock_event_device *bc;
336 unsigned long flags;
337 int broadcast = 0;
338
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100339 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100340
341 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100342
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100343 if (bc) {
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700344 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
345
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100346 switch (tick_broadcast_device.mode) {
347 case TICKDEV_MODE_PERIODIC:
Rusty Russell6b954822009-01-01 10:12:25 +1030348 if (!cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100349 tick_broadcast_start_periodic(bc);
Rusty Russell6b954822009-01-01 10:12:25 +1030350 broadcast = cpumask_test_cpu(smp_processor_id(),
351 tick_get_broadcast_mask());
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100352 break;
353 case TICKDEV_MODE_ONESHOT:
Suresh Siddhaa6371f82012-04-18 19:27:39 -0700354 if (!cpumask_empty(tick_get_broadcast_mask()))
355 broadcast = tick_resume_broadcast_oneshot(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100356 break;
357 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100358 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100359 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100360
361 return broadcast;
362}
363
364
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800365#ifdef CONFIG_TICK_ONESHOT
366
Rusty Russell6b954822009-01-01 10:12:25 +1030367/* FIXME: use cpumask_var_t. */
368static DECLARE_BITMAP(tick_broadcast_oneshot_mask, NR_CPUS);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800369
Ingo Molnar289f4802007-02-16 01:28:15 -0800370/*
Rusty Russell6b954822009-01-01 10:12:25 +1030371 * Exposed for debugging: see timer_list.c
Ingo Molnar289f4802007-02-16 01:28:15 -0800372 */
Rusty Russell6b954822009-01-01 10:12:25 +1030373struct cpumask *tick_get_broadcast_oneshot_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -0800374{
Rusty Russell6b954822009-01-01 10:12:25 +1030375 return to_cpumask(tick_broadcast_oneshot_mask);
Ingo Molnar289f4802007-02-16 01:28:15 -0800376}
377
Daniel Lezcano38b17152013-03-02 11:10:11 +0100378/*
379 * Set broadcast interrupt affinity
380 */
381static void tick_broadcast_set_affinity(struct clock_event_device *bc,
382 const struct cpumask *cpumask)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800383{
Daniel Lezcano38b17152013-03-02 11:10:11 +0100384 if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
385 return;
386
387 if (cpumask_equal(bc->cpumask, cpumask))
388 return;
389
390 bc->cpumask = cpumask;
391 irq_set_affinity(bc->irq, bc->cpumask);
392}
393
394static int tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
395 ktime_t expires, int force)
396{
397 int ret;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800398
Thomas Gleixnerb9a6a232012-04-18 17:31:58 +0200399 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
400 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
401
Daniel Lezcano38b17152013-03-02 11:10:11 +0100402 ret = clockevents_program_event(bc, expires, force);
403 if (!ret)
404 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
405 return ret;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800406}
407
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100408int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
409{
410 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb7e113d2007-09-22 22:29:06 +0000411 return 0;
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100412}
413
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800414/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200415 * Called from irq_enter() when idle was interrupted to reenable the
416 * per cpu device.
417 */
418void tick_check_oneshot_broadcast(int cpu)
419{
Rusty Russell6b954822009-01-01 10:12:25 +1030420 if (cpumask_test_cpu(cpu, to_cpumask(tick_broadcast_oneshot_mask))) {
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200421 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
422
Thomas Gleixner60a0c1a2013-07-01 22:14:10 +0200423 /*
424 * We might be in the middle of switching over from
425 * periodic to oneshot. If the CPU has not yet
426 * switched over, leave the device alone.
427 */
428 if (td->mode == TICKDEV_MODE_ONESHOT) {
429 clockevents_set_mode(td->evtdev,
430 CLOCK_EVT_MODE_ONESHOT);
431 }
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200432 }
433}
434
435/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800436 * Handle oneshot mode broadcasting
437 */
438static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
439{
440 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100441 ktime_t now, next_event;
Daniel Lezcano38b17152013-03-02 11:10:11 +0100442 int cpu, next_cpu = 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800443
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100444 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800445again:
446 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100447 next_event.tv64 = KTIME_MAX;
Rusty Russell6b954822009-01-01 10:12:25 +1030448 cpumask_clear(to_cpumask(tmpmask));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800449 now = ktime_get();
450 /* Find all expired events */
Rusty Russell6b954822009-01-01 10:12:25 +1030451 for_each_cpu(cpu, tick_get_broadcast_oneshot_mask()) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800452 td = &per_cpu(tick_cpu_device, cpu);
Daniel Lezcano38b17152013-03-02 11:10:11 +0100453 if (td->evtdev->next_event.tv64 <= now.tv64) {
Rusty Russell6b954822009-01-01 10:12:25 +1030454 cpumask_set_cpu(cpu, to_cpumask(tmpmask));
Daniel Lezcano38b17152013-03-02 11:10:11 +0100455 } else if (td->evtdev->next_event.tv64 < next_event.tv64) {
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100456 next_event.tv64 = td->evtdev->next_event.tv64;
Daniel Lezcano38b17152013-03-02 11:10:11 +0100457 next_cpu = cpu;
458 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800459 }
460
461 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100462 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800463 */
Rusty Russell6b954822009-01-01 10:12:25 +1030464 tick_do_broadcast(to_cpumask(tmpmask));
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100465
466 /*
467 * Two reasons for reprogram:
468 *
469 * - The global event did not expire any CPU local
470 * events. This happens in dyntick mode, as the maximum PIT
471 * delta is quite small.
472 *
473 * - There are pending events on sleeping CPUs which were not
474 * in the event mask
475 */
476 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800477 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100478 * Rearm the broadcast device. If event expired,
479 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800480 */
Daniel Lezcano38b17152013-03-02 11:10:11 +0100481 if (tick_broadcast_set_event(dev, next_cpu, next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800482 goto again;
483 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100484 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800485}
486
487/*
488 * Powerstate information: The system enters/leaves a state, where
489 * affected devices might stop
490 */
491void tick_broadcast_oneshot_control(unsigned long reason)
492{
493 struct clock_event_device *bc, *dev;
494 struct tick_device *td;
495 unsigned long flags;
496 int cpu;
497
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800498 /*
499 * Periodic mode does not care about the enter/exit of power
500 * states
501 */
502 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Andi Kleen7372b0b2011-05-04 15:09:27 -0700503 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800504
Andi Kleen7372b0b2011-05-04 15:09:27 -0700505 /*
506 * We are called with preemtion disabled from the depth of the
507 * idle code, so we can't be moved away.
508 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800509 cpu = smp_processor_id();
510 td = &per_cpu(tick_cpu_device, cpu);
511 dev = td->evtdev;
512
513 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Andi Kleen7372b0b2011-05-04 15:09:27 -0700514 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800515
Andi Kleen7372b0b2011-05-04 15:09:27 -0700516 bc = tick_broadcast_device.evtdev;
517
518 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800519 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Rusty Russell6b954822009-01-01 10:12:25 +1030520 if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
521 cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800522 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
523 if (dev->next_event.tv64 < bc->next_event.tv64)
Daniel Lezcano38b17152013-03-02 11:10:11 +0100524 tick_broadcast_set_event(bc, cpu, dev->next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800525 }
526 } else {
Rusty Russell6b954822009-01-01 10:12:25 +1030527 if (cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
528 cpumask_clear_cpu(cpu,
529 tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800530 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
531 if (dev->next_event.tv64 != KTIME_MAX)
532 tick_program_event(dev->next_event, 1);
533 }
534 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100535 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800536}
537
Thomas Gleixner5590a532007-07-21 04:37:35 -0700538/*
539 * Reset the one shot broadcast for a cpu
540 *
541 * Called with tick_broadcast_lock held
542 */
543static void tick_broadcast_clear_oneshot(int cpu)
544{
Rusty Russell6b954822009-01-01 10:12:25 +1030545 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner5590a532007-07-21 04:37:35 -0700546}
547
Rusty Russell6b954822009-01-01 10:12:25 +1030548static void tick_broadcast_init_next_event(struct cpumask *mask,
549 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200550{
551 struct tick_device *td;
552 int cpu;
553
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030554 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200555 td = &per_cpu(tick_cpu_device, cpu);
556 if (td->evtdev)
557 td->evtdev->next_event = expires;
558 }
559}
560
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800561/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800562 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800563 */
564void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
565{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200566 int cpu = smp_processor_id();
567
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000568 /* Set it up only once ! */
569 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200570 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200571
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000572 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200573
574 /* Take the do_timer update */
575 tick_do_timer_cpu = cpu;
576
577 /*
578 * We must be careful here. There might be other CPUs
579 * waiting for periodic broadcast. We need to set the
580 * oneshot_mask bits for those and program the
581 * broadcast device to fire.
582 */
Rusty Russell6b954822009-01-01 10:12:25 +1030583 cpumask_copy(to_cpumask(tmpmask), tick_get_broadcast_mask());
584 cpumask_clear_cpu(cpu, to_cpumask(tmpmask));
585 cpumask_or(tick_get_broadcast_oneshot_mask(),
586 tick_get_broadcast_oneshot_mask(),
587 to_cpumask(tmpmask));
Thomas Gleixner73007112008-09-06 03:01:45 +0200588
Rusty Russell6b954822009-01-01 10:12:25 +1030589 if (was_periodic && !cpumask_empty(to_cpumask(tmpmask))) {
Thomas Gleixnerb4350922012-04-18 12:08:23 +0200590 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Rusty Russell6b954822009-01-01 10:12:25 +1030591 tick_broadcast_init_next_event(to_cpumask(tmpmask),
592 tick_next_period);
Daniel Lezcano38b17152013-03-02 11:10:11 +0100593 tick_broadcast_set_event(bc, cpu, tick_next_period, 1);
Thomas Gleixner73007112008-09-06 03:01:45 +0200594 } else
595 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200596 } else {
597 /*
598 * The first cpu which switches to oneshot mode sets
599 * the bit for all other cpus which are in the general
600 * (periodic) broadcast mask. So the bit is set and
601 * would prevent the first broadcast enter after this
602 * to program the bc device.
603 */
604 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000605 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800606}
607
608/*
609 * Select oneshot operating mode for the broadcast device
610 */
611void tick_broadcast_switch_to_oneshot(void)
612{
613 struct clock_event_device *bc;
614 unsigned long flags;
615
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100616 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700617
618 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800619 bc = tick_broadcast_device.evtdev;
620 if (bc)
621 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700622
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100623 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800624}
625
626
627/*
628 * Remove a dead CPU from broadcasting
629 */
630void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
631{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800632 unsigned long flags;
633 unsigned int cpu = *cpup;
634
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100635 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800636
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200637 /*
638 * Clear the broadcast mask flag for the dead cpu, but do not
639 * stop the broadcast device!
640 */
Rusty Russell6b954822009-01-01 10:12:25 +1030641 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800642
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100643 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800644}
645
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200646/*
647 * Check, whether the broadcast device is in one shot mode
648 */
649int tick_broadcast_oneshot_active(void)
650{
651 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
652}
653
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100654/*
655 * Check whether the broadcast device supports oneshot.
656 */
657bool tick_broadcast_oneshot_available(void)
658{
659 struct clock_event_device *bc = tick_broadcast_device.evtdev;
660
661 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
662}
663
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800664#endif