blob: 7f32fe0e52cd46489c8d90e4b85f9d74204aab16 [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{
Mark Rutlanda7dc19b2013-03-07 15:09:24 +000070 if ((dev->features & CLOCK_EVT_FEAT_DUMMY) ||
71 (tick_broadcast_device.evtdev &&
Venki Pallipadi4a932322007-10-12 23:04:23 +020072 tick_broadcast_device.evtdev->rating >= dev->rating) ||
73 (dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080074 return 0;
75
Thomas Gleixnerc1be8432011-12-02 12:34:16 +010076 clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080077 tick_broadcast_device.evtdev = dev;
Rusty Russell6b954822009-01-01 10:12:25 +103078 if (!cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080079 tick_broadcast_start_periodic(dev);
80 return 1;
81}
82
83/*
84 * Check, if the device is the broadcast device
85 */
86int tick_is_broadcast_device(struct clock_event_device *dev)
87{
88 return (dev && tick_broadcast_device.evtdev == dev);
89}
90
Mark Rutland12ad1002013-01-14 17:05:22 +000091static void err_broadcast(const struct cpumask *mask)
92{
93 pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
94}
95
Mark Rutland5d1d9a22013-02-08 15:24:07 +000096static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
97{
98 if (!dev->broadcast)
99 dev->broadcast = tick_broadcast;
100 if (!dev->broadcast) {
101 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
102 dev->name);
103 dev->broadcast = err_broadcast;
104 }
105}
106
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800107/*
108 * Check, if the device is disfunctional and a place holder, which
109 * needs to be handled by the broadcast device.
110 */
111int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
112{
113 unsigned long flags;
114 int ret = 0;
115
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100116 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800117
118 /*
119 * Devices might be registered with both periodic and oneshot
120 * mode disabled. This signals, that the device needs to be
121 * operated from the broadcast device and is a placeholder for
122 * the cpu local device.
123 */
124 if (!tick_device_is_functional(dev)) {
125 dev->event_handler = tick_handle_periodic;
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000126 tick_device_setup_broadcast_func(dev);
Rusty Russell6b954822009-01-01 10:12:25 +1030127 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800128 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
129 ret = 1;
Thomas Gleixner5590a532007-07-21 04:37:35 -0700130 } else {
131 /*
132 * When the new device is not affected by the stop
133 * feature and the cpu is marked in the broadcast mask
134 * then clear the broadcast bit.
135 */
136 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
137 int cpu = smp_processor_id();
Rusty Russell6b954822009-01-01 10:12:25 +1030138 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner5590a532007-07-21 04:37:35 -0700139 tick_broadcast_clear_oneshot(cpu);
Mark Rutland5d1d9a22013-02-08 15:24:07 +0000140 } else {
141 tick_device_setup_broadcast_func(dev);
Thomas Gleixner5590a532007-07-21 04:37:35 -0700142 }
143 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100144 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800145 return ret;
146}
147
Mark Rutland12572db2013-01-14 17:05:21 +0000148#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
149int tick_receive_broadcast(void)
150{
151 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
152 struct clock_event_device *evt = td->evtdev;
153
154 if (!evt)
155 return -ENODEV;
156
157 if (!evt->event_handler)
158 return -EINVAL;
159
160 evt->event_handler(evt);
161 return 0;
162}
163#endif
164
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800165/*
Rusty Russell6b954822009-01-01 10:12:25 +1030166 * Broadcast the event to the cpus, which are set in the mask (mangled).
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800167 */
Rusty Russell6b954822009-01-01 10:12:25 +1030168static void tick_do_broadcast(struct cpumask *mask)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800169{
Thomas Gleixner186e3cb2008-01-30 13:30:01 +0100170 int cpu = smp_processor_id();
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800171 struct tick_device *td;
172
173 /*
174 * Check, if the current cpu is in the mask
175 */
Rusty Russell6b954822009-01-01 10:12:25 +1030176 if (cpumask_test_cpu(cpu, mask)) {
177 cpumask_clear_cpu(cpu, mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800178 td = &per_cpu(tick_cpu_device, cpu);
179 td->evtdev->event_handler(td->evtdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800180 }
181
Rusty Russell6b954822009-01-01 10:12:25 +1030182 if (!cpumask_empty(mask)) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800183 /*
184 * It might be necessary to actually check whether the devices
185 * have different broadcast functions. For now, just use the
186 * one of the first device. This works as long as we have this
187 * misfeature only on x86 (lapic)
188 */
Rusty Russell6b954822009-01-01 10:12:25 +1030189 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
190 td->evtdev->broadcast(mask);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800191 }
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800192}
193
194/*
195 * Periodic broadcast:
196 * - invoke the broadcast handlers
197 */
198static void tick_do_periodic_broadcast(void)
199{
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100200 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800201
Rusty Russell6b954822009-01-01 10:12:25 +1030202 cpumask_and(to_cpumask(tmpmask),
203 cpu_online_mask, tick_get_broadcast_mask());
204 tick_do_broadcast(to_cpumask(tmpmask));
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800205
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100206 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800207}
208
209/*
210 * Event handler for periodic broadcast ticks
211 */
212static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
213{
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000214 ktime_t next;
215
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800216 tick_do_periodic_broadcast();
217
218 /*
219 * The device is in periodic mode. No reprogramming necessary:
220 */
221 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
222 return;
223
224 /*
225 * Setup the next period for devices, which do not have
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000226 * periodic mode. We read dev->next_event first and add to it
Uwe Kleine-König698f9312010-07-02 20:41:51 +0200227 * when the event already expired. clockevents_program_event()
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000228 * sets dev->next_event only when the event is really
229 * programmed to the device.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800230 */
Thomas Gleixnerd4496b32008-09-03 21:36:57 +0000231 for (next = dev->next_event; ;) {
232 next = ktime_add(next, tick_period);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800233
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200234 if (!clockevents_program_event(dev, next, false))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800235 return;
236 tick_do_periodic_broadcast();
237 }
238}
239
240/*
241 * Powerstate information: The system enters/leaves a state, where
242 * affected devices might stop
243 */
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700244static void tick_do_broadcast_on_off(unsigned long *reason)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800245{
246 struct clock_event_device *bc, *dev;
247 struct tick_device *td;
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700248 unsigned long flags;
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000249 int cpu, bc_stopped;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800250
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100251 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800252
253 cpu = smp_processor_id();
254 td = &per_cpu(tick_cpu_device, cpu);
255 dev = td->evtdev;
256 bc = tick_broadcast_device.evtdev;
257
258 /*
Thomas Gleixner1595f452007-10-14 22:57:45 +0200259 * Is the device not affected by the powerstate ?
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800260 */
Thomas Gleixner1595f452007-10-14 22:57:45 +0200261 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800262 goto out;
263
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200264 if (!tick_device_is_functional(dev))
265 goto out;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200266
Rusty Russell6b954822009-01-01 10:12:25 +1030267 bc_stopped = cpumask_empty(tick_get_broadcast_mask());
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000268
Thomas Gleixner1595f452007-10-14 22:57:45 +0200269 switch (*reason) {
270 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
271 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
Rusty Russell6b954822009-01-01 10:12:25 +1030272 if (!cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
273 cpumask_set_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200274 if (tick_broadcast_device.mode ==
275 TICKDEV_MODE_PERIODIC)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700276 clockevents_shutdown(dev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800277 }
Thomas Gleixner3dfbc882007-10-17 18:04:32 +0200278 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200279 tick_broadcast_force = 1;
Thomas Gleixner1595f452007-10-14 22:57:45 +0200280 break;
281 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200282 if (!tick_broadcast_force &&
Rusty Russell6b954822009-01-01 10:12:25 +1030283 cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
284 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixner07454bf2008-10-04 10:51:07 +0200285 if (tick_broadcast_device.mode ==
286 TICKDEV_MODE_PERIODIC)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800287 tick_setup_periodic(dev, 0);
288 }
Thomas Gleixner1595f452007-10-14 22:57:45 +0200289 break;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800290 }
291
Rusty Russell6b954822009-01-01 10:12:25 +1030292 if (cpumask_empty(tick_get_broadcast_mask())) {
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000293 if (!bc_stopped)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700294 clockevents_shutdown(bc);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000295 } else if (bc_stopped) {
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800296 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
297 tick_broadcast_start_periodic(bc);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800298 else
299 tick_broadcast_setup_oneshot(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800300 }
301out:
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100302 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800303}
304
305/*
306 * Powerstate information: The system enters/leaves a state, where
307 * affected devices might stop.
308 */
309void tick_broadcast_on_off(unsigned long reason, int *oncpu)
310{
Rusty Russell6b954822009-01-01 10:12:25 +1030311 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
Glauber Costa833df312008-04-18 13:38:58 -0700312 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
Thomas Gleixner72fcde92007-05-23 13:57:30 -0700313 "offline CPU #%d\n", *oncpu);
Avi Kivitybf020cb2007-10-16 23:26:24 -0700314 else
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700315 tick_do_broadcast_on_off(&reason);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800316}
317
318/*
319 * Set the periodic handler depending on broadcast on/off
320 */
321void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
322{
323 if (!broadcast)
324 dev->event_handler = tick_handle_periodic;
325 else
326 dev->event_handler = tick_handle_periodic_broadcast;
327}
328
329/*
330 * Remove a CPU from broadcasting
331 */
332void tick_shutdown_broadcast(unsigned int *cpup)
333{
334 struct clock_event_device *bc;
335 unsigned long flags;
336 unsigned int cpu = *cpup;
337
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100338 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800339
340 bc = tick_broadcast_device.evtdev;
Rusty Russell6b954822009-01-01 10:12:25 +1030341 cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800342
343 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
Rusty Russell6b954822009-01-01 10:12:25 +1030344 if (bc && cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700345 clockevents_shutdown(bc);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800346 }
347
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100348 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800349}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800350
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100351void tick_suspend_broadcast(void)
352{
353 struct clock_event_device *bc;
354 unsigned long flags;
355
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100356 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100357
358 bc = tick_broadcast_device.evtdev;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700359 if (bc)
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700360 clockevents_shutdown(bc);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100361
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100362 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100363}
364
365int tick_resume_broadcast(void)
366{
367 struct clock_event_device *bc;
368 unsigned long flags;
369 int broadcast = 0;
370
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100371 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100372
373 bc = tick_broadcast_device.evtdev;
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100374
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100375 if (bc) {
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700376 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
377
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100378 switch (tick_broadcast_device.mode) {
379 case TICKDEV_MODE_PERIODIC:
Rusty Russell6b954822009-01-01 10:12:25 +1030380 if (!cpumask_empty(tick_get_broadcast_mask()))
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100381 tick_broadcast_start_periodic(bc);
Rusty Russell6b954822009-01-01 10:12:25 +1030382 broadcast = cpumask_test_cpu(smp_processor_id(),
383 tick_get_broadcast_mask());
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100384 break;
385 case TICKDEV_MODE_ONESHOT:
Suresh Siddhaa6371f82012-04-18 19:27:39 -0700386 if (!cpumask_empty(tick_get_broadcast_mask()))
387 broadcast = tick_resume_broadcast_oneshot(bc);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100388 break;
389 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100390 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100391 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100392
393 return broadcast;
394}
395
396
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800397#ifdef CONFIG_TICK_ONESHOT
398
Rusty Russell6b954822009-01-01 10:12:25 +1030399/* FIXME: use cpumask_var_t. */
400static DECLARE_BITMAP(tick_broadcast_oneshot_mask, NR_CPUS);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800401
Ingo Molnar289f4802007-02-16 01:28:15 -0800402/*
Rusty Russell6b954822009-01-01 10:12:25 +1030403 * Exposed for debugging: see timer_list.c
Ingo Molnar289f4802007-02-16 01:28:15 -0800404 */
Rusty Russell6b954822009-01-01 10:12:25 +1030405struct cpumask *tick_get_broadcast_oneshot_mask(void)
Ingo Molnar289f4802007-02-16 01:28:15 -0800406{
Rusty Russell6b954822009-01-01 10:12:25 +1030407 return to_cpumask(tick_broadcast_oneshot_mask);
Ingo Molnar289f4802007-02-16 01:28:15 -0800408}
409
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800410static int tick_broadcast_set_event(ktime_t expires, int force)
411{
412 struct clock_event_device *bc = tick_broadcast_device.evtdev;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800413
Thomas Gleixnerb9a6a2352012-04-18 17:31:58 +0200414 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
415 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
416
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200417 return clockevents_program_event(bc, expires, force);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800418}
419
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100420int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
421{
422 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixnerb7e113d2007-09-22 22:29:06 +0000423 return 0;
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100424}
425
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800426/*
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200427 * Called from irq_enter() when idle was interrupted to reenable the
428 * per cpu device.
429 */
430void tick_check_oneshot_broadcast(int cpu)
431{
Rusty Russell6b954822009-01-01 10:12:25 +1030432 if (cpumask_test_cpu(cpu, to_cpumask(tick_broadcast_oneshot_mask))) {
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200433 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
434
435 clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
436 }
437}
438
439/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800440 * Handle oneshot mode broadcasting
441 */
442static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
443{
444 struct tick_device *td;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100445 ktime_t now, next_event;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800446 int cpu;
447
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100448 raw_spin_lock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800449again:
450 dev->next_event.tv64 = KTIME_MAX;
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100451 next_event.tv64 = KTIME_MAX;
Rusty Russell6b954822009-01-01 10:12:25 +1030452 cpumask_clear(to_cpumask(tmpmask));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800453 now = ktime_get();
454 /* Find all expired events */
Rusty Russell6b954822009-01-01 10:12:25 +1030455 for_each_cpu(cpu, tick_get_broadcast_oneshot_mask()) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800456 td = &per_cpu(tick_cpu_device, cpu);
457 if (td->evtdev->next_event.tv64 <= now.tv64)
Rusty Russell6b954822009-01-01 10:12:25 +1030458 cpumask_set_cpu(cpu, to_cpumask(tmpmask));
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100459 else if (td->evtdev->next_event.tv64 < next_event.tv64)
460 next_event.tv64 = td->evtdev->next_event.tv64;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800461 }
462
463 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100464 * Wakeup the cpus which have an expired event.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800465 */
Rusty Russell6b954822009-01-01 10:12:25 +1030466 tick_do_broadcast(to_cpumask(tmpmask));
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100467
468 /*
469 * Two reasons for reprogram:
470 *
471 * - The global event did not expire any CPU local
472 * events. This happens in dyntick mode, as the maximum PIT
473 * delta is quite small.
474 *
475 * - There are pending events on sleeping CPUs which were not
476 * in the event mask
477 */
478 if (next_event.tv64 != KTIME_MAX) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800479 /*
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100480 * Rearm the broadcast device. If event expired,
481 * repeat the above
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800482 */
Thomas Gleixnercdc6f272007-12-18 18:05:58 +0100483 if (tick_broadcast_set_event(next_event, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800484 goto again;
485 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100486 raw_spin_unlock(&tick_broadcast_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800487}
488
489/*
490 * Powerstate information: The system enters/leaves a state, where
491 * affected devices might stop
492 */
493void tick_broadcast_oneshot_control(unsigned long reason)
494{
495 struct clock_event_device *bc, *dev;
496 struct tick_device *td;
497 unsigned long flags;
498 int cpu;
499
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800500 /*
501 * Periodic mode does not care about the enter/exit of power
502 * states
503 */
504 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
Andi Kleen7372b0b2011-05-04 15:09:27 -0700505 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800506
Andi Kleen7372b0b2011-05-04 15:09:27 -0700507 /*
508 * We are called with preemtion disabled from the depth of the
509 * idle code, so we can't be moved away.
510 */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800511 cpu = smp_processor_id();
512 td = &per_cpu(tick_cpu_device, cpu);
513 dev = td->evtdev;
514
515 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
Andi Kleen7372b0b2011-05-04 15:09:27 -0700516 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800517
Andi Kleen7372b0b2011-05-04 15:09:27 -0700518 bc = tick_broadcast_device.evtdev;
519
520 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800521 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
Rusty Russell6b954822009-01-01 10:12:25 +1030522 if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
523 cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800524 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
525 if (dev->next_event.tv64 < bc->next_event.tv64)
526 tick_broadcast_set_event(dev->next_event, 1);
527 }
528 } else {
Rusty Russell6b954822009-01-01 10:12:25 +1030529 if (cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
530 cpumask_clear_cpu(cpu,
531 tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800532 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
533 if (dev->next_event.tv64 != KTIME_MAX)
534 tick_program_event(dev->next_event, 1);
535 }
536 }
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100537 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800538}
539
Thomas Gleixner5590a532007-07-21 04:37:35 -0700540/*
541 * Reset the one shot broadcast for a cpu
542 *
543 * Called with tick_broadcast_lock held
544 */
545static void tick_broadcast_clear_oneshot(int cpu)
546{
Rusty Russell6b954822009-01-01 10:12:25 +1030547 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner5590a532007-07-21 04:37:35 -0700548}
549
Rusty Russell6b954822009-01-01 10:12:25 +1030550static void tick_broadcast_init_next_event(struct cpumask *mask,
551 ktime_t expires)
Thomas Gleixner73007112008-09-06 03:01:45 +0200552{
553 struct tick_device *td;
554 int cpu;
555
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030556 for_each_cpu(cpu, mask) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200557 td = &per_cpu(tick_cpu_device, cpu);
558 if (td->evtdev)
559 td->evtdev->next_event = expires;
560 }
561}
562
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800563/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800564 * tick_broadcast_setup_oneshot - setup the broadcast device
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800565 */
566void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
567{
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200568 int cpu = smp_processor_id();
569
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000570 /* Set it up only once ! */
571 if (bc->event_handler != tick_handle_oneshot_broadcast) {
Thomas Gleixner73007112008-09-06 03:01:45 +0200572 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
Thomas Gleixner73007112008-09-06 03:01:45 +0200573
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000574 bc->event_handler = tick_handle_oneshot_broadcast;
Thomas Gleixner73007112008-09-06 03:01:45 +0200575
576 /* Take the do_timer update */
577 tick_do_timer_cpu = cpu;
578
579 /*
580 * We must be careful here. There might be other CPUs
581 * waiting for periodic broadcast. We need to set the
582 * oneshot_mask bits for those and program the
583 * broadcast device to fire.
584 */
Rusty Russell6b954822009-01-01 10:12:25 +1030585 cpumask_copy(to_cpumask(tmpmask), tick_get_broadcast_mask());
586 cpumask_clear_cpu(cpu, to_cpumask(tmpmask));
587 cpumask_or(tick_get_broadcast_oneshot_mask(),
588 tick_get_broadcast_oneshot_mask(),
589 to_cpumask(tmpmask));
Thomas Gleixner73007112008-09-06 03:01:45 +0200590
Rusty Russell6b954822009-01-01 10:12:25 +1030591 if (was_periodic && !cpumask_empty(to_cpumask(tmpmask))) {
Thomas Gleixnerb4350922012-04-18 12:08:23 +0200592 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
Rusty Russell6b954822009-01-01 10:12:25 +1030593 tick_broadcast_init_next_event(to_cpumask(tmpmask),
594 tick_next_period);
Thomas Gleixner73007112008-09-06 03:01:45 +0200595 tick_broadcast_set_event(tick_next_period, 1);
596 } else
597 bc->next_event.tv64 = KTIME_MAX;
Thomas Gleixner07f4beb2011-05-16 11:07:48 +0200598 } else {
599 /*
600 * The first cpu which switches to oneshot mode sets
601 * the bit for all other cpus which are in the general
602 * (periodic) broadcast mask. So the bit is set and
603 * would prevent the first broadcast enter after this
604 * to program the bc device.
605 */
606 tick_broadcast_clear_oneshot(cpu);
Thomas Gleixner9c17bcd2008-09-03 21:37:08 +0000607 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800608}
609
610/*
611 * Select oneshot operating mode for the broadcast device
612 */
613void tick_broadcast_switch_to_oneshot(void)
614{
615 struct clock_event_device *bc;
616 unsigned long flags;
617
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100618 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Suresh Siddhafa4da362012-04-09 15:41:44 -0700619
620 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800621 bc = tick_broadcast_device.evtdev;
622 if (bc)
623 tick_broadcast_setup_oneshot(bc);
Suresh Siddha77b0d602011-11-04 17:18:21 -0700624
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100625 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800626}
627
628
629/*
630 * Remove a dead CPU from broadcasting
631 */
632void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
633{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800634 unsigned long flags;
635 unsigned int cpu = *cpup;
636
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100637 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800638
Thomas Gleixner31d9b392007-09-16 15:36:43 +0200639 /*
640 * Clear the broadcast mask flag for the dead cpu, but do not
641 * stop the broadcast device!
642 */
Rusty Russell6b954822009-01-01 10:12:25 +1030643 cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800644
Thomas Gleixnerb5f91da2009-12-08 12:40:31 +0100645 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800646}
647
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200648/*
649 * Check, whether the broadcast device is in one shot mode
650 */
651int tick_broadcast_oneshot_active(void)
652{
653 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
654}
655
Thomas Gleixner3a142a02011-02-25 22:34:23 +0100656/*
657 * Check whether the broadcast device supports oneshot.
658 */
659bool tick_broadcast_oneshot_available(void)
660{
661 struct clock_event_device *bc = tick_broadcast_device.evtdev;
662
663 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
664}
665
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800666#endif