blob: f726537d24ebdd5132f95bcdf697a20c6e75f177 [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
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080095/*
96 * Check, if the device is disfunctional and a place holder, which
97 * needs to be handled by the broadcast device.
98 */
99int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
100{
101 unsigned long flags;
102 int ret = 0;
103
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100104 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800105
106 /*
107 * Devices might be registered with both periodic and oneshot
108 * mode disabled. This signals, that the device needs to be
109 * operated from the broadcast device and is a placeholder for
110 * the cpu local device.
111 */
112 if (!tick_device_is_functional(dev)) {
113 dev->event_handler = tick_handle_periodic;
Mark Rutland12ad1002013-01-14 17:05:22 +0000114 if (!dev->broadcast)
115 dev->broadcast = tick_broadcast;
116 if (!dev->broadcast) {
117 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
118 dev->name);
119 dev->broadcast = err_broadcast;
120 }
Rusty Russell6b954822009-01-01 10:12:25 +1030121 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800122 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
123 ret = 1;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700124 } else {
125 /*
126 * When the new device is not affected by the stop
127 * feature and the cpu is marked in the broadcast mask
128 * then clear the broadcast bit.
129 */
130 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
131 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800132
Rusty Russell6b954822009-01-01 10:12:25 +1030133 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner5590a532007-07-21 04:37:35 -0700134 tick_broadcast_clear_oneshot(cpu);
135 }
136 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100137 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800138 return ret;
139}
140
Mark Rutland12572db2013-01-14 17:05:21 +0000141#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
142int tick_receive_broadcast(void)
143{
144 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
145 struct clock_event_device *evt = td->evtdev;
146
147 if (!evt)
148 return -ENODEV;
149
150 if (!evt->event_handler)
151 return -EINVAL;
152
153 evt->event_handler(evt);
154 return 0;
155}
156#endif
157
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800158/*
Rusty Russell6b954822009-01-01 10:12:25 +1030159 * Broadcast the event to the cpus, which are set in the mask (mangled).
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800160 */
Rusty Russell6b954822009-01-01 10:12:25 +1030161static void tick_do_broadcast(struct cpumask *mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800162{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100163 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800164 struct tick_device *td;
165
166 /*
167 * Check, if the current cpu is in the mask
168 */
Rusty Russell6b954822009-01-01 10:12:25 +1030169 if (cpumask_test_cpu(cpu, mask)) {
170 cpumask_clear_cpu(cpu, mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800171 td = &per_cpu(tick_cpu_device, cpu);
172 td->evtdev->event_handler(td->evtdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800173 }
174
Rusty Russell6b954822009-01-01 10:12:25 +1030175 if (!cpumask_empty(mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800176 /*
177 * It might be necessary to actually check whether the devices
178 * have different broadcast functions. For now, just use the
179 * one of the first device. This works as long as we have this
180 * misfeature only on x86 (lapic)
181 */
Rusty Russell6b954822009-01-01 10:12:25 +1030182 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
183 td->evtdev->broadcast(mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800184 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800185}
186
187/*
188 * Periodic broadcast:
189 * - invoke the broadcast handlers
190 */
191static void tick_do_periodic_broadcast(void)
192{
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100193 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800194
Rusty Russell6b954822009-01-01 10:12:25 +1030195 cpumask_and(to_cpumask(tmpmask),
196 cpu_online_mask, tick_get_broadcast_mask());
197 tick_do_broadcast(to_cpumask(tmpmask));
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800198
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100199 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800200}
201
202/*
203 * Event handler for periodic broadcast ticks
204 */
205static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
206{
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000207 ktime_t next;
208
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800209 tick_do_periodic_broadcast();
210
211 /*
212 * The device is in periodic mode. No reprogramming necessary:
213 */
214 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
215 return;
216
217 /*
218 * Setup the next period for devices, which do not have
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000219 * periodic mode. We read dev->next_event first and add to it
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200220 * when the event already expired. clockevents_program_event()
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000221 * sets dev->next_event only when the event is really
222 * programmed to the device.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800223 */
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000224 for (next = dev->next_event; ;) {
225 next = ktime_add(next, tick_period);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800226
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200227 if (!clockevents_program_event(dev, next, false))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800228 return;
229 tick_do_periodic_broadcast();
230 }
231}
232
233/*
234 * Powerstate information: The system enters/leaves a state, where
235 * affected devices might stop
236 */
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700237static void tick_do_broadcast_on_off(unsigned long *reason)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800238{
239 struct clock_event_device *bc, *dev;
240 struct tick_device *td;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700241 unsigned long flags;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000242 int cpu, bc_stopped;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800243
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100244 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800245
246 cpu = smp_processor_id();
247 td = &per_cpu(tick_cpu_device, cpu);
248 dev = td->evtdev;
249 bc = tick_broadcast_device.evtdev;
250
251 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200252 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800253 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200254 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800255 goto out;
256
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200257 if (!tick_device_is_functional(dev))
258 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200259
Rusty Russell6b954822009-01-01 10:12:25 +1030260 bc_stopped = cpumask_empty(tick_get_broadcast_mask());
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000261
Thomas Gleixner1595f452007-10-14 22:57:45 +0200262 switch (*reason) {
263 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
264 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
Rusty Russell6b954822009-01-01 10:12:25 +1030265 if (!cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
266 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200267 if (tick_broadcast_device.mode ==
268 TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700269 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800270 }
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200271 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200272 tick_broadcast_force = 1;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200273 break;
274 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200275 if (!tick_broadcast_force &&
Rusty Russell6b954822009-01-01 10:12:25 +1030276 cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
277 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200278 if (tick_broadcast_device.mode ==
279 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800280 tick_setup_periodic(dev, 0);
281 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200282 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800283 }
284
Rusty Russell6b954822009-01-01 10:12:25 +1030285 if (cpumask_empty(tick_get_broadcast_mask())) {
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000286 if (!bc_stopped)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700287 clockevents_shutdown(bc);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000288 } else if (bc_stopped) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800289 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
290 tick_broadcast_start_periodic(bc);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800291 else
292 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800293 }
294out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100295 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800296}
297
298/*
299 * Powerstate information: The system enters/leaves a state, where
300 * affected devices might stop.
301 */
302void tick_broadcast_on_off(unsigned long reason, int *oncpu)
303{
Rusty Russell6b954822009-01-01 10:12:25 +1030304 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
Glauber Costa833df312008-04-18 13:38:58 -0700305 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
Thomas Gleixner72fcde92007-05-23 13:57:30 -0700306 "offline CPU #%d\n", *oncpu);
Avi Kivitybf020cb2007-10-16 23:26:24 -0700307 else
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700308 tick_do_broadcast_on_off(&reason);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800309}
310
311/*
312 * Set the periodic handler depending on broadcast on/off
313 */
314void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
315{
316 if (!broadcast)
317 dev->event_handler = tick_handle_periodic;
318 else
319 dev->event_handler = tick_handle_periodic_broadcast;
320}
321
322/*
323 * Remove a CPU from broadcasting
324 */
325void tick_shutdown_broadcast(unsigned int *cpup)
326{
327 struct clock_event_device *bc;
328 unsigned long flags;
329 unsigned int cpu = *cpup;
330
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100331 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800332
333 bc = tick_broadcast_device.evtdev;
Rusty Russell6b954822009-01-01 10:12:25 +1030334 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800335
336 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
Rusty Russell6b954822009-01-01 10:12:25 +1030337 if (bc && cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700338 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800339 }
340
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100341 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800342}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800343
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100344void tick_suspend_broadcast(void)
345{
346 struct clock_event_device *bc;
347 unsigned long flags;
348
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100349 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100350
351 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700352 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700353 clockevents_shutdown(bc);
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
358int tick_resume_broadcast(void)
359{
360 struct clock_event_device *bc;
361 unsigned long flags;
362 int broadcast = 0;
363
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100364 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100365
366 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100367
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100368 if (bc) {
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700369 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
370
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100371 switch (tick_broadcast_device.mode) {
372 case TICKDEV_MODE_PERIODIC:
Rusty Russell6b954822009-01-01 10:12:25 +1030373 if (!cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100374 tick_broadcast_start_periodic(bc);
Rusty Russell6b954822009-01-01 10:12:25 +1030375 broadcast = cpumask_test_cpu(smp_processor_id(),
376 tick_get_broadcast_mask());
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100377 break;
378 case TICKDEV_MODE_ONESHOT:
Suresh Siddhaa6371f82012-04-18 19:27:39 -0700379 if (!cpumask_empty(tick_get_broadcast_mask()))
380 broadcast = tick_resume_broadcast_oneshot(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100381 break;
382 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100383 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100384 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100385
386 return broadcast;
387}
388
389
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800390#ifdef CONFIG_TICK_ONESHOT
391
Rusty Russell6b954822009-01-01 10:12:25 +1030392/* FIXME: use cpumask_var_t. */
393static DECLARE_BITMAP(tick_broadcast_oneshot_mask, NR_CPUS);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800394
Ingo Molnar289f4802007-02-16 01:28:15 -0800395/*
Rusty Russell6b954822009-01-01 10:12:25 +1030396 * Exposed for debugging: see timer_list.c
Ingo Molnar289f4802007-02-16 01:28:15 -0800397 */
Rusty Russell6b954822009-01-01 10:12:25 +1030398struct cpumask *tick_get_broadcast_oneshot_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -0800399{
Rusty Russell6b954822009-01-01 10:12:25 +1030400 return to_cpumask(tick_broadcast_oneshot_mask);
Ingo Molnar289f4802007-02-16 01:28:15 -0800401}
402
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800403static int tick_broadcast_set_event(ktime_t expires, int force)
404{
405 struct clock_event_device *bc = tick_broadcast_device.evtdev;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800406
Thomas Gleixnerb9a6a2352012-04-18 17:31:58 +0200407 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
408 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
409
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200410 return clockevents_program_event(bc, expires, force);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800411}
412
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100413int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
414{
415 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb7e113d2007-09-22 22:29:06 +0000416 return 0;
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100417}
418
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800419/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200420 * Called from irq_enter() when idle was interrupted to reenable the
421 * per cpu device.
422 */
423void tick_check_oneshot_broadcast(int cpu)
424{
Rusty Russell6b954822009-01-01 10:12:25 +1030425 if (cpumask_test_cpu(cpu, to_cpumask(tick_broadcast_oneshot_mask))) {
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200426 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
427
428 clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
429 }
430}
431
432/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800433 * Handle oneshot mode broadcasting
434 */
435static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
436{
437 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100438 ktime_t now, next_event;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800439 int cpu;
440
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100441 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800442again:
443 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100444 next_event.tv64 = KTIME_MAX;
Rusty Russell6b954822009-01-01 10:12:25 +1030445 cpumask_clear(to_cpumask(tmpmask));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800446 now = ktime_get();
447 /* Find all expired events */
Rusty Russell6b954822009-01-01 10:12:25 +1030448 for_each_cpu(cpu, tick_get_broadcast_oneshot_mask()) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800449 td = &per_cpu(tick_cpu_device, cpu);
450 if (td->evtdev->next_event.tv64 <= now.tv64)
Rusty Russell6b954822009-01-01 10:12:25 +1030451 cpumask_set_cpu(cpu, to_cpumask(tmpmask));
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100452 else if (td->evtdev->next_event.tv64 < next_event.tv64)
453 next_event.tv64 = td->evtdev->next_event.tv64;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800454 }
455
456 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100457 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800458 */
Rusty Russell6b954822009-01-01 10:12:25 +1030459 tick_do_broadcast(to_cpumask(tmpmask));
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100460
461 /*
462 * Two reasons for reprogram:
463 *
464 * - The global event did not expire any CPU local
465 * events. This happens in dyntick mode, as the maximum PIT
466 * delta is quite small.
467 *
468 * - There are pending events on sleeping CPUs which were not
469 * in the event mask
470 */
471 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800472 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100473 * Rearm the broadcast device. If event expired,
474 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800475 */
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100476 if (tick_broadcast_set_event(next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800477 goto again;
478 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100479 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800480}
481
482/*
483 * Powerstate information: The system enters/leaves a state, where
484 * affected devices might stop
485 */
486void tick_broadcast_oneshot_control(unsigned long reason)
487{
488 struct clock_event_device *bc, *dev;
489 struct tick_device *td;
490 unsigned long flags;
491 int cpu;
492
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800493 /*
494 * Periodic mode does not care about the enter/exit of power
495 * states
496 */
497 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Andi Kleen7372b0b2011-05-04 15:09:27 -0700498 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800499
Andi Kleen7372b0b2011-05-04 15:09:27 -0700500 /*
501 * We are called with preemtion disabled from the depth of the
502 * idle code, so we can't be moved away.
503 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800504 cpu = smp_processor_id();
505 td = &per_cpu(tick_cpu_device, cpu);
506 dev = td->evtdev;
507
508 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Andi Kleen7372b0b2011-05-04 15:09:27 -0700509 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800510
Andi Kleen7372b0b2011-05-04 15:09:27 -0700511 bc = tick_broadcast_device.evtdev;
512
513 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800514 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Rusty Russell6b954822009-01-01 10:12:25 +1030515 if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
516 cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800517 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
518 if (dev->next_event.tv64 < bc->next_event.tv64)
519 tick_broadcast_set_event(dev->next_event, 1);
520 }
521 } else {
Rusty Russell6b954822009-01-01 10:12:25 +1030522 if (cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
523 cpumask_clear_cpu(cpu,
524 tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800525 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
526 if (dev->next_event.tv64 != KTIME_MAX)
527 tick_program_event(dev->next_event, 1);
528 }
529 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100530 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800531}
532
Thomas Gleixner5590a532007-07-21 04:37:35 -0700533/*
534 * Reset the one shot broadcast for a cpu
535 *
536 * Called with tick_broadcast_lock held
537 */
538static void tick_broadcast_clear_oneshot(int cpu)
539{
Rusty Russell6b954822009-01-01 10:12:25 +1030540 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner5590a532007-07-21 04:37:35 -0700541}
542
Rusty Russell6b954822009-01-01 10:12:25 +1030543static void tick_broadcast_init_next_event(struct cpumask *mask,
544 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200545{
546 struct tick_device *td;
547 int cpu;
548
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030549 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200550 td = &per_cpu(tick_cpu_device, cpu);
551 if (td->evtdev)
552 td->evtdev->next_event = expires;
553 }
554}
555
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800556/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800557 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800558 */
559void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
560{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200561 int cpu = smp_processor_id();
562
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000563 /* Set it up only once ! */
564 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200565 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200566
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000567 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200568
569 /* Take the do_timer update */
570 tick_do_timer_cpu = cpu;
571
572 /*
573 * We must be careful here. There might be other CPUs
574 * waiting for periodic broadcast. We need to set the
575 * oneshot_mask bits for those and program the
576 * broadcast device to fire.
577 */
Rusty Russell6b954822009-01-01 10:12:25 +1030578 cpumask_copy(to_cpumask(tmpmask), tick_get_broadcast_mask());
579 cpumask_clear_cpu(cpu, to_cpumask(tmpmask));
580 cpumask_or(tick_get_broadcast_oneshot_mask(),
581 tick_get_broadcast_oneshot_mask(),
582 to_cpumask(tmpmask));
Thomas Gleixner73007112008-09-06 03:01:45 +0200583
Rusty Russell6b954822009-01-01 10:12:25 +1030584 if (was_periodic && !cpumask_empty(to_cpumask(tmpmask))) {
Thomas Gleixnerb4350922012-04-18 12:08:23 +0200585 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Rusty Russell6b954822009-01-01 10:12:25 +1030586 tick_broadcast_init_next_event(to_cpumask(tmpmask),
587 tick_next_period);
Thomas Gleixner73007112008-09-06 03:01:45 +0200588 tick_broadcast_set_event(tick_next_period, 1);
589 } else
590 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200591 } else {
592 /*
593 * The first cpu which switches to oneshot mode sets
594 * the bit for all other cpus which are in the general
595 * (periodic) broadcast mask. So the bit is set and
596 * would prevent the first broadcast enter after this
597 * to program the bc device.
598 */
599 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000600 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800601}
602
603/*
604 * Select oneshot operating mode for the broadcast device
605 */
606void tick_broadcast_switch_to_oneshot(void)
607{
608 struct clock_event_device *bc;
609 unsigned long flags;
610
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100611 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700612
613 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800614 bc = tick_broadcast_device.evtdev;
615 if (bc)
616 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700617
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100618 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800619}
620
621
622/*
623 * Remove a dead CPU from broadcasting
624 */
625void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
626{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800627 unsigned long flags;
628 unsigned int cpu = *cpup;
629
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100630 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800631
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200632 /*
633 * Clear the broadcast mask flag for the dead cpu, but do not
634 * stop the broadcast device!
635 */
Rusty Russell6b954822009-01-01 10:12:25 +1030636 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800637
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100638 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800639}
640
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200641/*
642 * Check, whether the broadcast device is in one shot mode
643 */
644int tick_broadcast_oneshot_active(void)
645{
646 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
647}
648
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100649/*
650 * Check whether the broadcast device supports oneshot.
651 */
652bool tick_broadcast_oneshot_available(void)
653{
654 struct clock_event_device *bc = tick_broadcast_device.evtdev;
655
656 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
657}
658
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800659#endif