2 * linux/kernel/time/tick-broadcast.c
4 * This file contains functions which emulate a local clock-event
5 * device via a broadcast event source.
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
11 * This code is licenced under the GPL version 2. For details see
12 * kernel-base/COPYING.
14 #include <linux/cpu.h>
15 #include <linux/err.h>
16 #include <linux/hrtimer.h>
17 #include <linux/interrupt.h>
18 #include <linux/percpu.h>
19 #include <linux/profile.h>
20 #include <linux/sched.h>
21 #include <linux/tick.h>
23 #include "tick-internal.h"
26 * Broadcast support for broken x86 hardware, where the local apic
27 * timer stops in C3 state.
30 struct tick_device tick_broadcast_device;
31 static cpumask_t tick_broadcast_mask;
32 static DEFINE_SPINLOCK(tick_broadcast_lock);
33 static int tick_broadcast_force;
35 #ifdef CONFIG_TICK_ONESHOT
36 static void tick_broadcast_clear_oneshot(int cpu);
38 static inline void tick_broadcast_clear_oneshot(int cpu) { }
42 * Debugging: see timer_list.c
44 struct tick_device *tick_get_broadcast_device(void)
46 return &tick_broadcast_device;
49 cpumask_t *tick_get_broadcast_mask(void)
51 return &tick_broadcast_mask;
55 * Start the device in periodic mode
57 static void tick_broadcast_start_periodic(struct clock_event_device *bc)
60 tick_setup_periodic(bc, 1);
64 * Check, if the device can be utilized as broadcast device:
66 int tick_check_broadcast_device(struct clock_event_device *dev)
68 if ((tick_broadcast_device.evtdev &&
69 tick_broadcast_device.evtdev->rating >= dev->rating) ||
70 (dev->features & CLOCK_EVT_FEAT_C3STOP))
73 clockevents_exchange_device(NULL, dev);
74 tick_broadcast_device.evtdev = dev;
75 if (!cpus_empty(tick_broadcast_mask))
76 tick_broadcast_start_periodic(dev);
81 * Check, if the device is the broadcast device
83 int tick_is_broadcast_device(struct clock_event_device *dev)
85 return (dev && tick_broadcast_device.evtdev == dev);
89 * Check, if the device is disfunctional and a place holder, which
90 * needs to be handled by the broadcast device.
92 int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
97 spin_lock_irqsave(&tick_broadcast_lock, flags);
100 * Devices might be registered with both periodic and oneshot
101 * mode disabled. This signals, that the device needs to be
102 * operated from the broadcast device and is a placeholder for
103 * the cpu local device.
105 if (!tick_device_is_functional(dev)) {
106 dev->event_handler = tick_handle_periodic;
107 cpu_set(cpu, tick_broadcast_mask);
108 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
112 * When the new device is not affected by the stop
113 * feature and the cpu is marked in the broadcast mask
114 * then clear the broadcast bit.
116 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
117 int cpu = smp_processor_id();
119 cpu_clear(cpu, tick_broadcast_mask);
120 tick_broadcast_clear_oneshot(cpu);
123 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
128 * Broadcast the event to the cpus, which are set in the mask
130 static void tick_do_broadcast(cpumask_t mask)
132 int cpu = smp_processor_id();
133 struct tick_device *td;
136 * Check, if the current cpu is in the mask
138 if (cpu_isset(cpu, mask)) {
139 cpu_clear(cpu, mask);
140 td = &per_cpu(tick_cpu_device, cpu);
141 td->evtdev->event_handler(td->evtdev);
144 if (!cpus_empty(mask)) {
146 * It might be necessary to actually check whether the devices
147 * have different broadcast functions. For now, just use the
148 * one of the first device. This works as long as we have this
149 * misfeature only on x86 (lapic)
151 cpu = first_cpu(mask);
152 td = &per_cpu(tick_cpu_device, cpu);
153 td->evtdev->broadcast(mask);
158 * Periodic broadcast:
159 * - invoke the broadcast handlers
161 static void tick_do_periodic_broadcast(void)
165 spin_lock(&tick_broadcast_lock);
167 cpus_and(mask, cpu_online_map, tick_broadcast_mask);
168 tick_do_broadcast(mask);
170 spin_unlock(&tick_broadcast_lock);
174 * Event handler for periodic broadcast ticks
176 static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
180 tick_do_periodic_broadcast();
183 * The device is in periodic mode. No reprogramming necessary:
185 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
189 * Setup the next period for devices, which do not have
190 * periodic mode. We read dev->next_event first and add to it
191 * when the event alrady expired. clockevents_program_event()
192 * sets dev->next_event only when the event is really
193 * programmed to the device.
195 for (next = dev->next_event; ;) {
196 next = ktime_add(next, tick_period);
198 if (!clockevents_program_event(dev, next, ktime_get()))
200 tick_do_periodic_broadcast();
205 * Powerstate information: The system enters/leaves a state, where
206 * affected devices might stop
208 static void tick_do_broadcast_on_off(void *why)
210 struct clock_event_device *bc, *dev;
211 struct tick_device *td;
212 unsigned long flags, *reason = why;
215 spin_lock_irqsave(&tick_broadcast_lock, flags);
217 cpu = smp_processor_id();
218 td = &per_cpu(tick_cpu_device, cpu);
220 bc = tick_broadcast_device.evtdev;
223 * Is the device not affected by the powerstate ?
225 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
228 if (!tick_device_is_functional(dev))
232 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
233 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
234 if (!cpu_isset(cpu, tick_broadcast_mask)) {
235 cpu_set(cpu, tick_broadcast_mask);
236 if (td->mode == TICKDEV_MODE_PERIODIC)
237 clockevents_set_mode(dev,
238 CLOCK_EVT_MODE_SHUTDOWN);
240 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
241 tick_broadcast_force = 1;
243 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
244 if (!tick_broadcast_force &&
245 cpu_isset(cpu, tick_broadcast_mask)) {
246 cpu_clear(cpu, tick_broadcast_mask);
247 if (td->mode == TICKDEV_MODE_PERIODIC)
248 tick_setup_periodic(dev, 0);
253 if (cpus_empty(tick_broadcast_mask))
254 clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
256 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
257 tick_broadcast_start_periodic(bc);
259 tick_broadcast_setup_oneshot(bc);
262 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
266 * Powerstate information: The system enters/leaves a state, where
267 * affected devices might stop.
269 void tick_broadcast_on_off(unsigned long reason, int *oncpu)
271 if (!cpu_isset(*oncpu, cpu_online_map))
272 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
273 "offline CPU #%d\n", *oncpu);
275 smp_call_function_single(*oncpu, tick_do_broadcast_on_off,
280 * Set the periodic handler depending on broadcast on/off
282 void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
285 dev->event_handler = tick_handle_periodic;
287 dev->event_handler = tick_handle_periodic_broadcast;
291 * Remove a CPU from broadcasting
293 void tick_shutdown_broadcast(unsigned int *cpup)
295 struct clock_event_device *bc;
297 unsigned int cpu = *cpup;
299 spin_lock_irqsave(&tick_broadcast_lock, flags);
301 bc = tick_broadcast_device.evtdev;
302 cpu_clear(cpu, tick_broadcast_mask);
304 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
305 if (bc && cpus_empty(tick_broadcast_mask))
306 clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
309 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
312 void tick_suspend_broadcast(void)
314 struct clock_event_device *bc;
317 spin_lock_irqsave(&tick_broadcast_lock, flags);
319 bc = tick_broadcast_device.evtdev;
321 clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
323 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
326 int tick_resume_broadcast(void)
328 struct clock_event_device *bc;
332 spin_lock_irqsave(&tick_broadcast_lock, flags);
334 bc = tick_broadcast_device.evtdev;
337 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
339 switch (tick_broadcast_device.mode) {
340 case TICKDEV_MODE_PERIODIC:
341 if(!cpus_empty(tick_broadcast_mask))
342 tick_broadcast_start_periodic(bc);
343 broadcast = cpu_isset(smp_processor_id(),
344 tick_broadcast_mask);
346 case TICKDEV_MODE_ONESHOT:
347 broadcast = tick_resume_broadcast_oneshot(bc);
351 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
357 #ifdef CONFIG_TICK_ONESHOT
359 static cpumask_t tick_broadcast_oneshot_mask;
362 * Debugging: see timer_list.c
364 cpumask_t *tick_get_broadcast_oneshot_mask(void)
366 return &tick_broadcast_oneshot_mask;
369 static int tick_broadcast_set_event(ktime_t expires, int force)
371 struct clock_event_device *bc = tick_broadcast_device.evtdev;
372 ktime_t now = ktime_get();
376 res = clockevents_program_event(bc, expires, now);
380 expires = ktime_add(now, ktime_set(0, bc->min_delta_ns));
384 int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
386 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
391 * Handle oneshot mode broadcasting
393 static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
395 struct tick_device *td;
397 ktime_t now, next_event;
400 spin_lock(&tick_broadcast_lock);
402 dev->next_event.tv64 = KTIME_MAX;
403 next_event.tv64 = KTIME_MAX;
404 mask = CPU_MASK_NONE;
406 /* Find all expired events */
407 for_each_cpu_mask_nr(cpu, tick_broadcast_oneshot_mask) {
408 td = &per_cpu(tick_cpu_device, cpu);
409 if (td->evtdev->next_event.tv64 <= now.tv64)
411 else if (td->evtdev->next_event.tv64 < next_event.tv64)
412 next_event.tv64 = td->evtdev->next_event.tv64;
416 * Wakeup the cpus which have an expired event.
418 tick_do_broadcast(mask);
421 * Two reasons for reprogram:
423 * - The global event did not expire any CPU local
424 * events. This happens in dyntick mode, as the maximum PIT
425 * delta is quite small.
427 * - There are pending events on sleeping CPUs which were not
430 if (next_event.tv64 != KTIME_MAX) {
432 * Rearm the broadcast device. If event expired,
435 if (tick_broadcast_set_event(next_event, 0))
438 spin_unlock(&tick_broadcast_lock);
442 * Powerstate information: The system enters/leaves a state, where
443 * affected devices might stop
445 void tick_broadcast_oneshot_control(unsigned long reason)
447 struct clock_event_device *bc, *dev;
448 struct tick_device *td;
452 spin_lock_irqsave(&tick_broadcast_lock, flags);
455 * Periodic mode does not care about the enter/exit of power
458 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
461 bc = tick_broadcast_device.evtdev;
462 cpu = smp_processor_id();
463 td = &per_cpu(tick_cpu_device, cpu);
466 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
469 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
470 if (!cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
471 cpu_set(cpu, tick_broadcast_oneshot_mask);
472 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
473 if (dev->next_event.tv64 < bc->next_event.tv64)
474 tick_broadcast_set_event(dev->next_event, 1);
477 if (cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
478 cpu_clear(cpu, tick_broadcast_oneshot_mask);
479 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
480 if (dev->next_event.tv64 != KTIME_MAX)
481 tick_program_event(dev->next_event, 1);
486 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
490 * Reset the one shot broadcast for a cpu
492 * Called with tick_broadcast_lock held
494 static void tick_broadcast_clear_oneshot(int cpu)
496 cpu_clear(cpu, tick_broadcast_oneshot_mask);
500 * tick_broadcast_setup_oneshot - setup the broadcast device
502 void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
504 bc->event_handler = tick_handle_oneshot_broadcast;
505 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
506 bc->next_event.tv64 = KTIME_MAX;
510 * Select oneshot operating mode for the broadcast device
512 void tick_broadcast_switch_to_oneshot(void)
514 struct clock_event_device *bc;
517 spin_lock_irqsave(&tick_broadcast_lock, flags);
519 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
520 bc = tick_broadcast_device.evtdev;
522 tick_broadcast_setup_oneshot(bc);
523 spin_unlock_irqrestore(&tick_broadcast_lock, flags);
528 * Remove a dead CPU from broadcasting
530 void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
533 unsigned int cpu = *cpup;
535 spin_lock_irqsave(&tick_broadcast_lock, flags);
538 * Clear the broadcast mask flag for the dead cpu, but do not
539 * stop the broadcast device!
541 cpu_clear(cpu, tick_broadcast_oneshot_mask);
543 spin_unlock_irqrestore(&tick_broadcast_lock, flags);