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/smp.h>
22 #include <linux/module.h>
24 #include "tick-internal.h"
27 * Broadcast support for broken x86 hardware, where the local apic
28 * timer stops in C3 state.
31 static struct tick_device tick_broadcast_device;
32 static cpumask_var_t tick_broadcast_mask;
33 static cpumask_var_t tmpmask;
34 static DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
35 static int tick_broadcast_force;
37 #ifdef CONFIG_TICK_ONESHOT
38 static void tick_broadcast_clear_oneshot(int cpu);
40 static inline void tick_broadcast_clear_oneshot(int cpu) { }
44 * Debugging: see timer_list.c
46 struct tick_device *tick_get_broadcast_device(void)
48 return &tick_broadcast_device;
51 struct cpumask *tick_get_broadcast_mask(void)
53 return tick_broadcast_mask;
57 * Start the device in periodic mode
59 static void tick_broadcast_start_periodic(struct clock_event_device *bc)
62 tick_setup_periodic(bc, 1);
66 * Check, if the device can be utilized as broadcast device:
68 void tick_install_broadcast_device(struct clock_event_device *dev)
70 struct clock_event_device *cur = tick_broadcast_device.evtdev;
72 if ((dev->features & CLOCK_EVT_FEAT_DUMMY) ||
73 (tick_broadcast_device.evtdev &&
74 tick_broadcast_device.evtdev->rating >= dev->rating) ||
75 (dev->features & CLOCK_EVT_FEAT_C3STOP))
77 if (!try_module_get(dev->owner))
80 clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
82 cur->event_handler = clockevents_handle_noop;
83 tick_broadcast_device.evtdev = dev;
84 if (!cpumask_empty(tick_broadcast_mask))
85 tick_broadcast_start_periodic(dev);
87 * Inform all cpus about this. We might be in a situation
88 * where we did not switch to oneshot mode because the per cpu
89 * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
90 * of a oneshot capable broadcast device. Without that
91 * notification the systems stays stuck in periodic mode
94 if (dev->features & CLOCK_EVT_FEAT_ONESHOT)
99 * Check, if the device is the broadcast device
101 int tick_is_broadcast_device(struct clock_event_device *dev)
103 return (dev && tick_broadcast_device.evtdev == dev);
106 static void err_broadcast(const struct cpumask *mask)
108 pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
111 static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
114 dev->broadcast = tick_broadcast;
115 if (!dev->broadcast) {
116 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
118 dev->broadcast = err_broadcast;
123 * Check, if the device is disfunctional and a place holder, which
124 * needs to be handled by the broadcast device.
126 int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
131 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
134 * Devices might be registered with both periodic and oneshot
135 * mode disabled. This signals, that the device needs to be
136 * operated from the broadcast device and is a placeholder for
137 * the cpu local device.
139 if (!tick_device_is_functional(dev)) {
140 dev->event_handler = tick_handle_periodic;
141 tick_device_setup_broadcast_func(dev);
142 cpumask_set_cpu(cpu, tick_broadcast_mask);
143 tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
147 * When the new device is not affected by the stop
148 * feature and the cpu is marked in the broadcast mask
149 * then clear the broadcast bit.
151 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
152 int cpu = smp_processor_id();
153 cpumask_clear_cpu(cpu, tick_broadcast_mask);
154 tick_broadcast_clear_oneshot(cpu);
156 tick_device_setup_broadcast_func(dev);
159 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
163 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
164 int tick_receive_broadcast(void)
166 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
167 struct clock_event_device *evt = td->evtdev;
172 if (!evt->event_handler)
175 evt->event_handler(evt);
181 * Broadcast the event to the cpus, which are set in the mask (mangled).
183 static void tick_do_broadcast(struct cpumask *mask)
185 int cpu = smp_processor_id();
186 struct tick_device *td;
189 * Check, if the current cpu is in the mask
191 if (cpumask_test_cpu(cpu, mask)) {
192 cpumask_clear_cpu(cpu, mask);
193 td = &per_cpu(tick_cpu_device, cpu);
194 td->evtdev->event_handler(td->evtdev);
197 if (!cpumask_empty(mask)) {
199 * It might be necessary to actually check whether the devices
200 * have different broadcast functions. For now, just use the
201 * one of the first device. This works as long as we have this
202 * misfeature only on x86 (lapic)
204 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
205 td->evtdev->broadcast(mask);
210 * Periodic broadcast:
211 * - invoke the broadcast handlers
213 static void tick_do_periodic_broadcast(void)
215 raw_spin_lock(&tick_broadcast_lock);
217 cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
218 tick_do_broadcast(tmpmask);
220 raw_spin_unlock(&tick_broadcast_lock);
224 * Event handler for periodic broadcast ticks
226 static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
230 tick_do_periodic_broadcast();
233 * The device is in periodic mode. No reprogramming necessary:
235 if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
239 * Setup the next period for devices, which do not have
240 * periodic mode. We read dev->next_event first and add to it
241 * when the event already expired. clockevents_program_event()
242 * sets dev->next_event only when the event is really
243 * programmed to the device.
245 for (next = dev->next_event; ;) {
246 next = ktime_add(next, tick_period);
248 if (!clockevents_program_event(dev, next, false))
250 tick_do_periodic_broadcast();
255 * Powerstate information: The system enters/leaves a state, where
256 * affected devices might stop
258 static void tick_do_broadcast_on_off(unsigned long *reason)
260 struct clock_event_device *bc, *dev;
261 struct tick_device *td;
265 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
267 cpu = smp_processor_id();
268 td = &per_cpu(tick_cpu_device, cpu);
270 bc = tick_broadcast_device.evtdev;
273 * Is the device not affected by the powerstate ?
275 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
278 if (!tick_device_is_functional(dev))
281 bc_stopped = cpumask_empty(tick_broadcast_mask);
284 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
285 case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
286 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
287 if (tick_broadcast_device.mode ==
288 TICKDEV_MODE_PERIODIC)
289 clockevents_shutdown(dev);
291 if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
292 tick_broadcast_force = 1;
294 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
295 if (!tick_broadcast_force &&
296 cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
297 if (tick_broadcast_device.mode ==
298 TICKDEV_MODE_PERIODIC)
299 tick_setup_periodic(dev, 0);
304 if (cpumask_empty(tick_broadcast_mask)) {
306 clockevents_shutdown(bc);
307 } else if (bc_stopped) {
308 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
309 tick_broadcast_start_periodic(bc);
311 tick_broadcast_setup_oneshot(bc);
314 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
318 * Powerstate information: The system enters/leaves a state, where
319 * affected devices might stop.
321 void tick_broadcast_on_off(unsigned long reason, int *oncpu)
323 if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
324 printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
325 "offline CPU #%d\n", *oncpu);
327 tick_do_broadcast_on_off(&reason);
331 * Set the periodic handler depending on broadcast on/off
333 void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
336 dev->event_handler = tick_handle_periodic;
338 dev->event_handler = tick_handle_periodic_broadcast;
342 * Remove a CPU from broadcasting
344 void tick_shutdown_broadcast(unsigned int *cpup)
346 struct clock_event_device *bc;
348 unsigned int cpu = *cpup;
350 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
352 bc = tick_broadcast_device.evtdev;
353 cpumask_clear_cpu(cpu, tick_broadcast_mask);
355 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
356 if (bc && cpumask_empty(tick_broadcast_mask))
357 clockevents_shutdown(bc);
360 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
363 void tick_suspend_broadcast(void)
365 struct clock_event_device *bc;
368 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
370 bc = tick_broadcast_device.evtdev;
372 clockevents_shutdown(bc);
374 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
377 int tick_resume_broadcast(void)
379 struct clock_event_device *bc;
383 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
385 bc = tick_broadcast_device.evtdev;
388 clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
390 switch (tick_broadcast_device.mode) {
391 case TICKDEV_MODE_PERIODIC:
392 if (!cpumask_empty(tick_broadcast_mask))
393 tick_broadcast_start_periodic(bc);
394 broadcast = cpumask_test_cpu(smp_processor_id(),
395 tick_broadcast_mask);
397 case TICKDEV_MODE_ONESHOT:
398 if (!cpumask_empty(tick_broadcast_mask))
399 broadcast = tick_resume_broadcast_oneshot(bc);
403 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
409 #ifdef CONFIG_TICK_ONESHOT
411 static cpumask_var_t tick_broadcast_oneshot_mask;
412 static cpumask_var_t tick_broadcast_pending_mask;
413 static cpumask_var_t tick_broadcast_force_mask;
416 * Exposed for debugging: see timer_list.c
418 struct cpumask *tick_get_broadcast_oneshot_mask(void)
420 return tick_broadcast_oneshot_mask;
424 * Called before going idle with interrupts disabled. Checks whether a
425 * broadcast event from the other core is about to happen. We detected
426 * that in tick_broadcast_oneshot_control(). The callsite can use this
427 * to avoid a deep idle transition as we are about to get the
428 * broadcast IPI right away.
430 int tick_check_broadcast_expired(void)
432 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
436 * Set broadcast interrupt affinity
438 static void tick_broadcast_set_affinity(struct clock_event_device *bc,
439 const struct cpumask *cpumask)
441 if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
444 if (cpumask_equal(bc->cpumask, cpumask))
447 bc->cpumask = cpumask;
448 irq_set_affinity(bc->irq, bc->cpumask);
451 static int tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
452 ktime_t expires, int force)
456 if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
457 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
459 ret = clockevents_program_event(bc, expires, force);
461 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
465 int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
467 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
472 * Called from irq_enter() when idle was interrupted to reenable the
475 void tick_check_oneshot_broadcast(int cpu)
477 if (cpumask_test_cpu(cpu, tick_broadcast_oneshot_mask)) {
478 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
480 clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
485 * Handle oneshot mode broadcasting
487 static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
489 struct tick_device *td;
490 ktime_t now, next_event;
491 int cpu, next_cpu = 0;
493 raw_spin_lock(&tick_broadcast_lock);
495 dev->next_event.tv64 = KTIME_MAX;
496 next_event.tv64 = KTIME_MAX;
497 cpumask_clear(tmpmask);
499 /* Find all expired events */
500 for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
501 td = &per_cpu(tick_cpu_device, cpu);
502 if (td->evtdev->next_event.tv64 <= now.tv64) {
503 cpumask_set_cpu(cpu, tmpmask);
505 * Mark the remote cpu in the pending mask, so
506 * it can avoid reprogramming the cpu local
507 * timer in tick_broadcast_oneshot_control().
509 cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
510 } else if (td->evtdev->next_event.tv64 < next_event.tv64) {
511 next_event.tv64 = td->evtdev->next_event.tv64;
516 /* Take care of enforced broadcast requests */
517 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
518 cpumask_clear(tick_broadcast_force_mask);
521 * Wakeup the cpus which have an expired event.
523 tick_do_broadcast(tmpmask);
526 * Two reasons for reprogram:
528 * - The global event did not expire any CPU local
529 * events. This happens in dyntick mode, as the maximum PIT
530 * delta is quite small.
532 * - There are pending events on sleeping CPUs which were not
535 if (next_event.tv64 != KTIME_MAX) {
537 * Rearm the broadcast device. If event expired,
540 if (tick_broadcast_set_event(dev, next_cpu, next_event, 0))
543 raw_spin_unlock(&tick_broadcast_lock);
547 * Powerstate information: The system enters/leaves a state, where
548 * affected devices might stop
550 void tick_broadcast_oneshot_control(unsigned long reason)
552 struct clock_event_device *bc, *dev;
553 struct tick_device *td;
559 * Periodic mode does not care about the enter/exit of power
562 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
566 * We are called with preemtion disabled from the depth of the
567 * idle code, so we can't be moved away.
569 cpu = smp_processor_id();
570 td = &per_cpu(tick_cpu_device, cpu);
573 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
576 bc = tick_broadcast_device.evtdev;
578 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
579 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
580 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
581 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
582 clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
584 * We only reprogram the broadcast timer if we
585 * did not mark ourself in the force mask and
586 * if the cpu local event is earlier than the
587 * broadcast event. If the current CPU is in
588 * the force mask, then we are going to be
589 * woken by the IPI right away.
591 if (!cpumask_test_cpu(cpu, tick_broadcast_force_mask) &&
592 dev->next_event.tv64 < bc->next_event.tv64)
593 tick_broadcast_set_event(bc, cpu, dev->next_event, 1);
596 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
597 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
598 if (dev->next_event.tv64 == KTIME_MAX)
601 * The cpu which was handling the broadcast
602 * timer marked this cpu in the broadcast
603 * pending mask and fired the broadcast
604 * IPI. So we are going to handle the expired
605 * event anyway via the broadcast IPI
606 * handler. No need to reprogram the timer
607 * with an already expired event.
609 if (cpumask_test_and_clear_cpu(cpu,
610 tick_broadcast_pending_mask))
614 * If the pending bit is not set, then we are
615 * either the CPU handling the broadcast
616 * interrupt or we got woken by something else.
618 * We are not longer in the broadcast mask, so
619 * if the cpu local expiry time is already
620 * reached, we would reprogram the cpu local
621 * timer with an already expired event.
623 * This can lead to a ping-pong when we return
624 * to idle and therefor rearm the broadcast
625 * timer before the cpu local timer was able
626 * to fire. This happens because the forced
627 * reprogramming makes sure that the event
628 * will happen in the future and depending on
629 * the min_delta setting this might be far
630 * enough out that the ping-pong starts.
632 * If the cpu local next_event has expired
633 * then we know that the broadcast timer
634 * next_event has expired as well and
635 * broadcast is about to be handled. So we
636 * avoid reprogramming and enforce that the
637 * broadcast handler, which did not run yet,
638 * will invoke the cpu local handler.
640 * We cannot call the handler directly from
641 * here, because we might be in a NOHZ phase
642 * and we did not go through the irq_enter()
646 if (dev->next_event.tv64 <= now.tv64) {
647 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
651 * We got woken by something else. Reprogram
652 * the cpu local timer device.
654 tick_program_event(dev->next_event, 1);
658 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
662 * Reset the one shot broadcast for a cpu
664 * Called with tick_broadcast_lock held
666 static void tick_broadcast_clear_oneshot(int cpu)
668 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
671 static void tick_broadcast_init_next_event(struct cpumask *mask,
674 struct tick_device *td;
677 for_each_cpu(cpu, mask) {
678 td = &per_cpu(tick_cpu_device, cpu);
680 td->evtdev->next_event = expires;
685 * tick_broadcast_setup_oneshot - setup the broadcast device
687 void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
689 int cpu = smp_processor_id();
691 /* Set it up only once ! */
692 if (bc->event_handler != tick_handle_oneshot_broadcast) {
693 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
695 bc->event_handler = tick_handle_oneshot_broadcast;
697 /* Take the do_timer update */
698 if (!tick_nohz_full_cpu(cpu))
699 tick_do_timer_cpu = cpu;
702 * We must be careful here. There might be other CPUs
703 * waiting for periodic broadcast. We need to set the
704 * oneshot_mask bits for those and program the
705 * broadcast device to fire.
707 cpumask_copy(tmpmask, tick_broadcast_mask);
708 cpumask_clear_cpu(cpu, tmpmask);
709 cpumask_or(tick_broadcast_oneshot_mask,
710 tick_broadcast_oneshot_mask, tmpmask);
712 if (was_periodic && !cpumask_empty(tmpmask)) {
713 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
714 tick_broadcast_init_next_event(tmpmask,
716 tick_broadcast_set_event(bc, cpu, tick_next_period, 1);
718 bc->next_event.tv64 = KTIME_MAX;
721 * The first cpu which switches to oneshot mode sets
722 * the bit for all other cpus which are in the general
723 * (periodic) broadcast mask. So the bit is set and
724 * would prevent the first broadcast enter after this
725 * to program the bc device.
727 tick_broadcast_clear_oneshot(cpu);
732 * Select oneshot operating mode for the broadcast device
734 void tick_broadcast_switch_to_oneshot(void)
736 struct clock_event_device *bc;
739 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
741 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
742 bc = tick_broadcast_device.evtdev;
744 tick_broadcast_setup_oneshot(bc);
746 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
751 * Remove a dead CPU from broadcasting
753 void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
756 unsigned int cpu = *cpup;
758 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
761 * Clear the broadcast mask flag for the dead cpu, but do not
762 * stop the broadcast device!
764 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
766 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
770 * Check, whether the broadcast device is in one shot mode
772 int tick_broadcast_oneshot_active(void)
774 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
778 * Check whether the broadcast device supports oneshot.
780 bool tick_broadcast_oneshot_available(void)
782 struct clock_event_device *bc = tick_broadcast_device.evtdev;
784 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
789 void __init tick_broadcast_init(void)
791 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
792 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
793 #ifdef CONFIG_TICK_ONESHOT
794 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
795 zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
796 zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);