blob: f8961bf973628837410321abd44aa072ae748370 [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
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800378static int tick_broadcast_set_event(ktime_t expires, int force)
379{
380 struct clock_event_device *bc = tick_broadcast_device.evtdev;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800381
Thomas Gleixnerb9a6a232012-04-18 17:31:58 +0200382 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
383 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
384
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200385 return clockevents_program_event(bc, expires, force);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800386}
387
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100388int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
389{
390 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb7e113d2007-09-22 22:29:06 +0000391 return 0;
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100392}
393
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800394/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200395 * Called from irq_enter() when idle was interrupted to reenable the
396 * per cpu device.
397 */
398void tick_check_oneshot_broadcast(int cpu)
399{
Rusty Russell6b954822009-01-01 10:12:25 +1030400 if (cpumask_test_cpu(cpu, to_cpumask(tick_broadcast_oneshot_mask))) {
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200401 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
402
Thomas Gleixner60a0c1a2013-07-01 22:14:10 +0200403 /*
404 * We might be in the middle of switching over from
405 * periodic to oneshot. If the CPU has not yet
406 * switched over, leave the device alone.
407 */
408 if (td->mode == TICKDEV_MODE_ONESHOT) {
409 clockevents_set_mode(td->evtdev,
410 CLOCK_EVT_MODE_ONESHOT);
411 }
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200412 }
413}
414
415/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800416 * Handle oneshot mode broadcasting
417 */
418static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
419{
420 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100421 ktime_t now, next_event;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800422 int cpu;
423
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100424 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800425again:
426 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100427 next_event.tv64 = KTIME_MAX;
Rusty Russell6b954822009-01-01 10:12:25 +1030428 cpumask_clear(to_cpumask(tmpmask));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800429 now = ktime_get();
430 /* Find all expired events */
Rusty Russell6b954822009-01-01 10:12:25 +1030431 for_each_cpu(cpu, tick_get_broadcast_oneshot_mask()) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800432 td = &per_cpu(tick_cpu_device, cpu);
433 if (td->evtdev->next_event.tv64 <= now.tv64)
Rusty Russell6b954822009-01-01 10:12:25 +1030434 cpumask_set_cpu(cpu, to_cpumask(tmpmask));
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100435 else if (td->evtdev->next_event.tv64 < next_event.tv64)
436 next_event.tv64 = td->evtdev->next_event.tv64;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800437 }
438
439 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100440 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800441 */
Rusty Russell6b954822009-01-01 10:12:25 +1030442 tick_do_broadcast(to_cpumask(tmpmask));
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100443
444 /*
445 * Two reasons for reprogram:
446 *
447 * - The global event did not expire any CPU local
448 * events. This happens in dyntick mode, as the maximum PIT
449 * delta is quite small.
450 *
451 * - There are pending events on sleeping CPUs which were not
452 * in the event mask
453 */
454 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800455 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100456 * Rearm the broadcast device. If event expired,
457 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800458 */
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100459 if (tick_broadcast_set_event(next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800460 goto again;
461 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100462 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800463}
464
465/*
466 * Powerstate information: The system enters/leaves a state, where
467 * affected devices might stop
468 */
469void tick_broadcast_oneshot_control(unsigned long reason)
470{
471 struct clock_event_device *bc, *dev;
472 struct tick_device *td;
473 unsigned long flags;
474 int cpu;
475
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800476 /*
477 * Periodic mode does not care about the enter/exit of power
478 * states
479 */
480 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Andi Kleen7372b0b2011-05-04 15:09:27 -0700481 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800482
Andi Kleen7372b0b2011-05-04 15:09:27 -0700483 /*
484 * We are called with preemtion disabled from the depth of the
485 * idle code, so we can't be moved away.
486 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800487 cpu = smp_processor_id();
488 td = &per_cpu(tick_cpu_device, cpu);
489 dev = td->evtdev;
490
491 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Andi Kleen7372b0b2011-05-04 15:09:27 -0700492 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800493
Andi Kleen7372b0b2011-05-04 15:09:27 -0700494 bc = tick_broadcast_device.evtdev;
495
496 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800497 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Rusty Russell6b954822009-01-01 10:12:25 +1030498 if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
499 cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800500 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
501 if (dev->next_event.tv64 < bc->next_event.tv64)
502 tick_broadcast_set_event(dev->next_event, 1);
503 }
504 } else {
Rusty Russell6b954822009-01-01 10:12:25 +1030505 if (cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
506 cpumask_clear_cpu(cpu,
507 tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800508 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
509 if (dev->next_event.tv64 != KTIME_MAX)
510 tick_program_event(dev->next_event, 1);
511 }
512 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100513 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800514}
515
Thomas Gleixner5590a532007-07-21 04:37:35 -0700516/*
517 * Reset the one shot broadcast for a cpu
518 *
519 * Called with tick_broadcast_lock held
520 */
521static void tick_broadcast_clear_oneshot(int cpu)
522{
Rusty Russell6b954822009-01-01 10:12:25 +1030523 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner5590a532007-07-21 04:37:35 -0700524}
525
Rusty Russell6b954822009-01-01 10:12:25 +1030526static void tick_broadcast_init_next_event(struct cpumask *mask,
527 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200528{
529 struct tick_device *td;
530 int cpu;
531
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030532 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200533 td = &per_cpu(tick_cpu_device, cpu);
534 if (td->evtdev)
535 td->evtdev->next_event = expires;
536 }
537}
538
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800539/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800540 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800541 */
542void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
543{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200544 int cpu = smp_processor_id();
545
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000546 /* Set it up only once ! */
547 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200548 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200549
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000550 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200551
552 /* Take the do_timer update */
553 tick_do_timer_cpu = cpu;
554
555 /*
556 * We must be careful here. There might be other CPUs
557 * waiting for periodic broadcast. We need to set the
558 * oneshot_mask bits for those and program the
559 * broadcast device to fire.
560 */
Rusty Russell6b954822009-01-01 10:12:25 +1030561 cpumask_copy(to_cpumask(tmpmask), tick_get_broadcast_mask());
562 cpumask_clear_cpu(cpu, to_cpumask(tmpmask));
563 cpumask_or(tick_get_broadcast_oneshot_mask(),
564 tick_get_broadcast_oneshot_mask(),
565 to_cpumask(tmpmask));
Thomas Gleixner73007112008-09-06 03:01:45 +0200566
Rusty Russell6b954822009-01-01 10:12:25 +1030567 if (was_periodic && !cpumask_empty(to_cpumask(tmpmask))) {
Thomas Gleixnerb4350922012-04-18 12:08:23 +0200568 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Rusty Russell6b954822009-01-01 10:12:25 +1030569 tick_broadcast_init_next_event(to_cpumask(tmpmask),
570 tick_next_period);
Thomas Gleixner73007112008-09-06 03:01:45 +0200571 tick_broadcast_set_event(tick_next_period, 1);
572 } else
573 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200574 } else {
575 /*
576 * The first cpu which switches to oneshot mode sets
577 * the bit for all other cpus which are in the general
578 * (periodic) broadcast mask. So the bit is set and
579 * would prevent the first broadcast enter after this
580 * to program the bc device.
581 */
582 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000583 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800584}
585
586/*
587 * Select oneshot operating mode for the broadcast device
588 */
589void tick_broadcast_switch_to_oneshot(void)
590{
591 struct clock_event_device *bc;
592 unsigned long flags;
593
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100594 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700595
596 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800597 bc = tick_broadcast_device.evtdev;
598 if (bc)
599 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700600
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100601 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800602}
603
604
605/*
606 * Remove a dead CPU from broadcasting
607 */
608void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
609{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800610 unsigned long flags;
611 unsigned int cpu = *cpup;
612
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100613 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800614
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200615 /*
616 * Clear the broadcast mask flag for the dead cpu, but do not
617 * stop the broadcast device!
618 */
Rusty Russell6b954822009-01-01 10:12:25 +1030619 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800620
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100621 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800622}
623
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200624/*
625 * Check, whether the broadcast device is in one shot mode
626 */
627int tick_broadcast_oneshot_active(void)
628{
629 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
630}
631
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100632/*
633 * Check whether the broadcast device supports oneshot.
634 */
635bool tick_broadcast_oneshot_available(void)
636{
637 struct clock_event_device *bc = tick_broadcast_device.evtdev;
638
639 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
640}
641
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800642#endif