1 // SPDX-License-Identifier: GPL-2.0
3 * This file contains functions which emulate a local clock-event
4 * device via a broadcast event source.
6 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
7 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
8 * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
10 #include <linux/cpu.h>
11 #include <linux/err.h>
12 #include <linux/hrtimer.h>
13 #include <linux/interrupt.h>
14 #include <linux/percpu.h>
15 #include <linux/profile.h>
16 #include <linux/sched.h>
17 #include <linux/smp.h>
18 #include <linux/module.h>
20 #include "tick-internal.h"
23 * Broadcast support for broken x86 hardware, where the local apic
24 * timer stops in C3 state.
27 static struct tick_device tick_broadcast_device;
28 static cpumask_var_t tick_broadcast_mask __cpumask_var_read_mostly;
29 static cpumask_var_t tick_broadcast_on __cpumask_var_read_mostly;
30 static cpumask_var_t tmpmask __cpumask_var_read_mostly;
31 static int tick_broadcast_forced;
33 static __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
35 #ifdef CONFIG_TICK_ONESHOT
36 static void tick_broadcast_setup_oneshot(struct clock_event_device *bc);
37 static void tick_broadcast_clear_oneshot(int cpu);
38 static void tick_resume_broadcast_oneshot(struct clock_event_device *bc);
39 # ifdef CONFIG_HOTPLUG_CPU
40 static void tick_broadcast_oneshot_offline(unsigned int cpu);
43 static inline void tick_broadcast_setup_oneshot(struct clock_event_device *bc) { BUG(); }
44 static inline void tick_broadcast_clear_oneshot(int cpu) { }
45 static inline void tick_resume_broadcast_oneshot(struct clock_event_device *bc) { }
46 # ifdef CONFIG_HOTPLUG_CPU
47 static inline void tick_broadcast_oneshot_offline(unsigned int cpu) { }
52 * Debugging: see timer_list.c
54 struct tick_device *tick_get_broadcast_device(void)
56 return &tick_broadcast_device;
59 struct cpumask *tick_get_broadcast_mask(void)
61 return tick_broadcast_mask;
65 * Start the device in periodic mode
67 static void tick_broadcast_start_periodic(struct clock_event_device *bc)
70 tick_setup_periodic(bc, 1);
74 * Check, if the device can be utilized as broadcast device:
76 static bool tick_check_broadcast_device(struct clock_event_device *curdev,
77 struct clock_event_device *newdev)
79 if ((newdev->features & CLOCK_EVT_FEAT_DUMMY) ||
80 (newdev->features & CLOCK_EVT_FEAT_PERCPU) ||
81 (newdev->features & CLOCK_EVT_FEAT_C3STOP))
84 if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT &&
85 !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
88 return !curdev || newdev->rating > curdev->rating;
92 * Conditionally install/replace broadcast device
94 void tick_install_broadcast_device(struct clock_event_device *dev)
96 struct clock_event_device *cur = tick_broadcast_device.evtdev;
98 if (!tick_check_broadcast_device(cur, dev))
101 if (!try_module_get(dev->owner))
104 clockevents_exchange_device(cur, dev);
106 cur->event_handler = clockevents_handle_noop;
107 tick_broadcast_device.evtdev = dev;
108 if (!cpumask_empty(tick_broadcast_mask))
109 tick_broadcast_start_periodic(dev);
111 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
115 * If the system already runs in oneshot mode, switch the newly
116 * registered broadcast device to oneshot mode explicitly.
118 if (tick_broadcast_oneshot_active()) {
119 tick_broadcast_switch_to_oneshot();
124 * Inform all cpus about this. We might be in a situation
125 * where we did not switch to oneshot mode because the per cpu
126 * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
127 * of a oneshot capable broadcast device. Without that
128 * notification the systems stays stuck in periodic mode
135 * Check, if the device is the broadcast device
137 int tick_is_broadcast_device(struct clock_event_device *dev)
139 return (dev && tick_broadcast_device.evtdev == dev);
142 int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq)
146 if (tick_is_broadcast_device(dev)) {
147 raw_spin_lock(&tick_broadcast_lock);
148 ret = __clockevents_update_freq(dev, freq);
149 raw_spin_unlock(&tick_broadcast_lock);
155 static void err_broadcast(const struct cpumask *mask)
157 pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
160 static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
163 dev->broadcast = tick_broadcast;
164 if (!dev->broadcast) {
165 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
167 dev->broadcast = err_broadcast;
172 * Check, if the device is dysfunctional and a placeholder, which
173 * needs to be handled by the broadcast device.
175 int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
177 struct clock_event_device *bc = tick_broadcast_device.evtdev;
181 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
184 * Devices might be registered with both periodic and oneshot
185 * mode disabled. This signals, that the device needs to be
186 * operated from the broadcast device and is a placeholder for
187 * the cpu local device.
189 if (!tick_device_is_functional(dev)) {
190 dev->event_handler = tick_handle_periodic;
191 tick_device_setup_broadcast_func(dev);
192 cpumask_set_cpu(cpu, tick_broadcast_mask);
193 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
194 tick_broadcast_start_periodic(bc);
196 tick_broadcast_setup_oneshot(bc);
200 * Clear the broadcast bit for this cpu if the
201 * device is not power state affected.
203 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
204 cpumask_clear_cpu(cpu, tick_broadcast_mask);
206 tick_device_setup_broadcast_func(dev);
209 * Clear the broadcast bit if the CPU is not in
210 * periodic broadcast on state.
212 if (!cpumask_test_cpu(cpu, tick_broadcast_on))
213 cpumask_clear_cpu(cpu, tick_broadcast_mask);
215 switch (tick_broadcast_device.mode) {
216 case TICKDEV_MODE_ONESHOT:
218 * If the system is in oneshot mode we can
219 * unconditionally clear the oneshot mask bit,
220 * because the CPU is running and therefore
221 * not in an idle state which causes the power
222 * state affected device to stop. Let the
223 * caller initialize the device.
225 tick_broadcast_clear_oneshot(cpu);
229 case TICKDEV_MODE_PERIODIC:
231 * If the system is in periodic mode, check
232 * whether the broadcast device can be
235 if (cpumask_empty(tick_broadcast_mask) && bc)
236 clockevents_shutdown(bc);
238 * If we kept the cpu in the broadcast mask,
239 * tell the caller to leave the per cpu device
240 * in shutdown state. The periodic interrupt
241 * is delivered by the broadcast device, if
242 * the broadcast device exists and is not
245 if (bc && !(bc->features & CLOCK_EVT_FEAT_HRTIMER))
246 ret = cpumask_test_cpu(cpu, tick_broadcast_mask);
252 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
256 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
257 int tick_receive_broadcast(void)
259 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
260 struct clock_event_device *evt = td->evtdev;
265 if (!evt->event_handler)
268 evt->event_handler(evt);
274 * Broadcast the event to the cpus, which are set in the mask (mangled).
276 static bool tick_do_broadcast(struct cpumask *mask)
278 int cpu = smp_processor_id();
279 struct tick_device *td;
283 * Check, if the current cpu is in the mask
285 if (cpumask_test_cpu(cpu, mask)) {
286 struct clock_event_device *bc = tick_broadcast_device.evtdev;
288 cpumask_clear_cpu(cpu, mask);
290 * We only run the local handler, if the broadcast
291 * device is not hrtimer based. Otherwise we run into
292 * a hrtimer recursion.
294 * local timer_interrupt()
301 local = !(bc->features & CLOCK_EVT_FEAT_HRTIMER);
304 if (!cpumask_empty(mask)) {
306 * It might be necessary to actually check whether the devices
307 * have different broadcast functions. For now, just use the
308 * one of the first device. This works as long as we have this
309 * misfeature only on x86 (lapic)
311 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
312 td->evtdev->broadcast(mask);
318 * Periodic broadcast:
319 * - invoke the broadcast handlers
321 static bool tick_do_periodic_broadcast(void)
323 cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
324 return tick_do_broadcast(tmpmask);
328 * Event handler for periodic broadcast ticks
330 static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
332 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
335 raw_spin_lock(&tick_broadcast_lock);
337 /* Handle spurious interrupts gracefully */
338 if (clockevent_state_shutdown(tick_broadcast_device.evtdev)) {
339 raw_spin_unlock(&tick_broadcast_lock);
343 bc_local = tick_do_periodic_broadcast();
345 if (clockevent_state_oneshot(dev)) {
346 ktime_t next = ktime_add_ns(dev->next_event, TICK_NSEC);
348 clockevents_program_event(dev, next, true);
350 raw_spin_unlock(&tick_broadcast_lock);
353 * We run the handler of the local cpu after dropping
354 * tick_broadcast_lock because the handler might deadlock when
355 * trying to switch to oneshot mode.
358 td->evtdev->event_handler(td->evtdev);
362 * tick_broadcast_control - Enable/disable or force broadcast mode
363 * @mode: The selected broadcast mode
365 * Called when the system enters a state where affected tick devices
366 * might stop. Note: TICK_BROADCAST_FORCE cannot be undone.
368 void tick_broadcast_control(enum tick_broadcast_mode mode)
370 struct clock_event_device *bc, *dev;
371 struct tick_device *td;
375 /* Protects also the local clockevent device. */
376 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
377 td = this_cpu_ptr(&tick_cpu_device);
381 * Is the device not affected by the powerstate ?
383 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
386 if (!tick_device_is_functional(dev))
389 cpu = smp_processor_id();
390 bc = tick_broadcast_device.evtdev;
391 bc_stopped = cpumask_empty(tick_broadcast_mask);
394 case TICK_BROADCAST_FORCE:
395 tick_broadcast_forced = 1;
397 case TICK_BROADCAST_ON:
398 cpumask_set_cpu(cpu, tick_broadcast_on);
399 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
401 * Only shutdown the cpu local device, if:
403 * - the broadcast device exists
404 * - the broadcast device is not a hrtimer based one
405 * - the broadcast device is in periodic mode to
406 * avoid a hiccup during switch to oneshot mode
408 if (bc && !(bc->features & CLOCK_EVT_FEAT_HRTIMER) &&
409 tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
410 clockevents_shutdown(dev);
414 case TICK_BROADCAST_OFF:
415 if (tick_broadcast_forced)
417 cpumask_clear_cpu(cpu, tick_broadcast_on);
418 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
419 if (tick_broadcast_device.mode ==
420 TICKDEV_MODE_PERIODIC)
421 tick_setup_periodic(dev, 0);
427 if (cpumask_empty(tick_broadcast_mask)) {
429 clockevents_shutdown(bc);
430 } else if (bc_stopped) {
431 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
432 tick_broadcast_start_periodic(bc);
434 tick_broadcast_setup_oneshot(bc);
438 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
440 EXPORT_SYMBOL_GPL(tick_broadcast_control);
443 * Set the periodic handler depending on broadcast on/off
445 void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
448 dev->event_handler = tick_handle_periodic;
450 dev->event_handler = tick_handle_periodic_broadcast;
453 #ifdef CONFIG_HOTPLUG_CPU
454 static void tick_shutdown_broadcast(void)
456 struct clock_event_device *bc = tick_broadcast_device.evtdev;
458 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
459 if (bc && cpumask_empty(tick_broadcast_mask))
460 clockevents_shutdown(bc);
465 * Remove a CPU from broadcasting
467 void tick_broadcast_offline(unsigned int cpu)
469 raw_spin_lock(&tick_broadcast_lock);
470 cpumask_clear_cpu(cpu, tick_broadcast_mask);
471 cpumask_clear_cpu(cpu, tick_broadcast_on);
472 tick_broadcast_oneshot_offline(cpu);
473 tick_shutdown_broadcast();
474 raw_spin_unlock(&tick_broadcast_lock);
479 void tick_suspend_broadcast(void)
481 struct clock_event_device *bc;
484 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
486 bc = tick_broadcast_device.evtdev;
488 clockevents_shutdown(bc);
490 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
494 * This is called from tick_resume_local() on a resuming CPU. That's
495 * called from the core resume function, tick_unfreeze() and the magic XEN
498 * In none of these cases the broadcast device mode can change and the
499 * bit of the resuming CPU in the broadcast mask is safe as well.
501 bool tick_resume_check_broadcast(void)
503 if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT)
506 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_mask);
509 void tick_resume_broadcast(void)
511 struct clock_event_device *bc;
514 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
516 bc = tick_broadcast_device.evtdev;
519 clockevents_tick_resume(bc);
521 switch (tick_broadcast_device.mode) {
522 case TICKDEV_MODE_PERIODIC:
523 if (!cpumask_empty(tick_broadcast_mask))
524 tick_broadcast_start_periodic(bc);
526 case TICKDEV_MODE_ONESHOT:
527 if (!cpumask_empty(tick_broadcast_mask))
528 tick_resume_broadcast_oneshot(bc);
532 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
535 #ifdef CONFIG_TICK_ONESHOT
537 static cpumask_var_t tick_broadcast_oneshot_mask __cpumask_var_read_mostly;
538 static cpumask_var_t tick_broadcast_pending_mask __cpumask_var_read_mostly;
539 static cpumask_var_t tick_broadcast_force_mask __cpumask_var_read_mostly;
542 * Exposed for debugging: see timer_list.c
544 struct cpumask *tick_get_broadcast_oneshot_mask(void)
546 return tick_broadcast_oneshot_mask;
550 * Called before going idle with interrupts disabled. Checks whether a
551 * broadcast event from the other core is about to happen. We detected
552 * that in tick_broadcast_oneshot_control(). The callsite can use this
553 * to avoid a deep idle transition as we are about to get the
554 * broadcast IPI right away.
556 int tick_check_broadcast_expired(void)
558 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
562 * Set broadcast interrupt affinity
564 static void tick_broadcast_set_affinity(struct clock_event_device *bc,
565 const struct cpumask *cpumask)
567 if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
570 if (cpumask_equal(bc->cpumask, cpumask))
573 bc->cpumask = cpumask;
574 irq_set_affinity(bc->irq, bc->cpumask);
577 static void tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
580 if (!clockevent_state_oneshot(bc))
581 clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
583 clockevents_program_event(bc, expires, 1);
584 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
587 static void tick_resume_broadcast_oneshot(struct clock_event_device *bc)
589 clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
593 * Called from irq_enter() when idle was interrupted to reenable the
596 void tick_check_oneshot_broadcast_this_cpu(void)
598 if (cpumask_test_cpu(smp_processor_id(), tick_broadcast_oneshot_mask)) {
599 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
602 * We might be in the middle of switching over from
603 * periodic to oneshot. If the CPU has not yet
604 * switched over, leave the device alone.
606 if (td->mode == TICKDEV_MODE_ONESHOT) {
607 clockevents_switch_state(td->evtdev,
608 CLOCK_EVT_STATE_ONESHOT);
614 * Handle oneshot mode broadcasting
616 static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
618 struct tick_device *td;
619 ktime_t now, next_event;
620 int cpu, next_cpu = 0;
623 raw_spin_lock(&tick_broadcast_lock);
624 dev->next_event = KTIME_MAX;
625 next_event = KTIME_MAX;
626 cpumask_clear(tmpmask);
628 /* Find all expired events */
629 for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
631 * Required for !SMP because for_each_cpu() reports
632 * unconditionally CPU0 as set on UP kernels.
634 if (!IS_ENABLED(CONFIG_SMP) &&
635 cpumask_empty(tick_broadcast_oneshot_mask))
638 td = &per_cpu(tick_cpu_device, cpu);
639 if (td->evtdev->next_event <= now) {
640 cpumask_set_cpu(cpu, tmpmask);
642 * Mark the remote cpu in the pending mask, so
643 * it can avoid reprogramming the cpu local
644 * timer in tick_broadcast_oneshot_control().
646 cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
647 } else if (td->evtdev->next_event < next_event) {
648 next_event = td->evtdev->next_event;
654 * Remove the current cpu from the pending mask. The event is
655 * delivered immediately in tick_do_broadcast() !
657 cpumask_clear_cpu(smp_processor_id(), tick_broadcast_pending_mask);
659 /* Take care of enforced broadcast requests */
660 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
661 cpumask_clear(tick_broadcast_force_mask);
664 * Sanity check. Catch the case where we try to broadcast to
667 if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask)))
668 cpumask_and(tmpmask, tmpmask, cpu_online_mask);
671 * Wakeup the cpus which have an expired event.
673 bc_local = tick_do_broadcast(tmpmask);
676 * Two reasons for reprogram:
678 * - The global event did not expire any CPU local
679 * events. This happens in dyntick mode, as the maximum PIT
680 * delta is quite small.
682 * - There are pending events on sleeping CPUs which were not
685 if (next_event != KTIME_MAX)
686 tick_broadcast_set_event(dev, next_cpu, next_event);
688 raw_spin_unlock(&tick_broadcast_lock);
691 td = this_cpu_ptr(&tick_cpu_device);
692 td->evtdev->event_handler(td->evtdev);
696 static int broadcast_needs_cpu(struct clock_event_device *bc, int cpu)
698 if (!(bc->features & CLOCK_EVT_FEAT_HRTIMER))
700 if (bc->next_event == KTIME_MAX)
702 return bc->bound_on == cpu ? -EBUSY : 0;
705 static void broadcast_shutdown_local(struct clock_event_device *bc,
706 struct clock_event_device *dev)
709 * For hrtimer based broadcasting we cannot shutdown the cpu
710 * local device if our own event is the first one to expire or
711 * if we own the broadcast timer.
713 if (bc->features & CLOCK_EVT_FEAT_HRTIMER) {
714 if (broadcast_needs_cpu(bc, smp_processor_id()))
716 if (dev->next_event < bc->next_event)
719 clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
722 int __tick_broadcast_oneshot_control(enum tick_broadcast_state state)
724 struct clock_event_device *bc, *dev;
729 * If there is no broadcast device, tell the caller not to go
732 if (!tick_broadcast_device.evtdev)
735 dev = this_cpu_ptr(&tick_cpu_device)->evtdev;
737 raw_spin_lock(&tick_broadcast_lock);
738 bc = tick_broadcast_device.evtdev;
739 cpu = smp_processor_id();
741 if (state == TICK_BROADCAST_ENTER) {
743 * If the current CPU owns the hrtimer broadcast
744 * mechanism, it cannot go deep idle and we do not add
745 * the CPU to the broadcast mask. We don't have to go
746 * through the EXIT path as the local timer is not
749 ret = broadcast_needs_cpu(bc, cpu);
754 * If the broadcast device is in periodic mode, we
757 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
758 /* If it is a hrtimer based broadcast, return busy */
759 if (bc->features & CLOCK_EVT_FEAT_HRTIMER)
764 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
765 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
767 /* Conditionally shut down the local timer. */
768 broadcast_shutdown_local(bc, dev);
771 * We only reprogram the broadcast timer if we
772 * did not mark ourself in the force mask and
773 * if the cpu local event is earlier than the
774 * broadcast event. If the current CPU is in
775 * the force mask, then we are going to be
776 * woken by the IPI right away; we return
777 * busy, so the CPU does not try to go deep
780 if (cpumask_test_cpu(cpu, tick_broadcast_force_mask)) {
782 } else if (dev->next_event < bc->next_event) {
783 tick_broadcast_set_event(bc, cpu, dev->next_event);
785 * In case of hrtimer broadcasts the
786 * programming might have moved the
787 * timer to this cpu. If yes, remove
788 * us from the broadcast mask and
791 ret = broadcast_needs_cpu(bc, cpu);
793 cpumask_clear_cpu(cpu,
794 tick_broadcast_oneshot_mask);
799 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
800 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
802 * The cpu which was handling the broadcast
803 * timer marked this cpu in the broadcast
804 * pending mask and fired the broadcast
805 * IPI. So we are going to handle the expired
806 * event anyway via the broadcast IPI
807 * handler. No need to reprogram the timer
808 * with an already expired event.
810 if (cpumask_test_and_clear_cpu(cpu,
811 tick_broadcast_pending_mask))
815 * Bail out if there is no next event.
817 if (dev->next_event == KTIME_MAX)
820 * If the pending bit is not set, then we are
821 * either the CPU handling the broadcast
822 * interrupt or we got woken by something else.
824 * We are no longer in the broadcast mask, so
825 * if the cpu local expiry time is already
826 * reached, we would reprogram the cpu local
827 * timer with an already expired event.
829 * This can lead to a ping-pong when we return
830 * to idle and therefore rearm the broadcast
831 * timer before the cpu local timer was able
832 * to fire. This happens because the forced
833 * reprogramming makes sure that the event
834 * will happen in the future and depending on
835 * the min_delta setting this might be far
836 * enough out that the ping-pong starts.
838 * If the cpu local next_event has expired
839 * then we know that the broadcast timer
840 * next_event has expired as well and
841 * broadcast is about to be handled. So we
842 * avoid reprogramming and enforce that the
843 * broadcast handler, which did not run yet,
844 * will invoke the cpu local handler.
846 * We cannot call the handler directly from
847 * here, because we might be in a NOHZ phase
848 * and we did not go through the irq_enter()
852 if (dev->next_event <= now) {
853 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
857 * We got woken by something else. Reprogram
858 * the cpu local timer device.
860 tick_program_event(dev->next_event, 1);
864 raw_spin_unlock(&tick_broadcast_lock);
869 * Reset the one shot broadcast for a cpu
871 * Called with tick_broadcast_lock held
873 static void tick_broadcast_clear_oneshot(int cpu)
875 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
876 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
879 static void tick_broadcast_init_next_event(struct cpumask *mask,
882 struct tick_device *td;
885 for_each_cpu(cpu, mask) {
886 td = &per_cpu(tick_cpu_device, cpu);
888 td->evtdev->next_event = expires;
892 static inline ktime_t tick_get_next_period(void)
897 * Protect against concurrent updates (store /load tearing on
898 * 32bit). It does not matter if the time is already in the
899 * past. The broadcast device which is about to be programmed will
902 raw_spin_lock(&jiffies_lock);
903 next = tick_next_period;
904 raw_spin_unlock(&jiffies_lock);
909 * tick_broadcast_setup_oneshot - setup the broadcast device
911 static void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
913 int cpu = smp_processor_id();
918 /* Set it up only once ! */
919 if (bc->event_handler != tick_handle_oneshot_broadcast) {
920 int was_periodic = clockevent_state_periodic(bc);
922 bc->event_handler = tick_handle_oneshot_broadcast;
925 * We must be careful here. There might be other CPUs
926 * waiting for periodic broadcast. We need to set the
927 * oneshot_mask bits for those and program the
928 * broadcast device to fire.
930 cpumask_copy(tmpmask, tick_broadcast_mask);
931 cpumask_clear_cpu(cpu, tmpmask);
932 cpumask_or(tick_broadcast_oneshot_mask,
933 tick_broadcast_oneshot_mask, tmpmask);
935 if (was_periodic && !cpumask_empty(tmpmask)) {
936 ktime_t nextevt = tick_get_next_period();
938 clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
939 tick_broadcast_init_next_event(tmpmask, nextevt);
940 tick_broadcast_set_event(bc, cpu, nextevt);
942 bc->next_event = KTIME_MAX;
945 * The first cpu which switches to oneshot mode sets
946 * the bit for all other cpus which are in the general
947 * (periodic) broadcast mask. So the bit is set and
948 * would prevent the first broadcast enter after this
949 * to program the bc device.
951 tick_broadcast_clear_oneshot(cpu);
956 * Select oneshot operating mode for the broadcast device
958 void tick_broadcast_switch_to_oneshot(void)
960 struct clock_event_device *bc;
963 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
965 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
966 bc = tick_broadcast_device.evtdev;
968 tick_broadcast_setup_oneshot(bc);
970 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
973 #ifdef CONFIG_HOTPLUG_CPU
974 void hotplug_cpu__broadcast_tick_pull(int deadcpu)
976 struct clock_event_device *bc;
979 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
980 bc = tick_broadcast_device.evtdev;
982 if (bc && broadcast_needs_cpu(bc, deadcpu)) {
983 /* This moves the broadcast assignment to this CPU: */
984 clockevents_program_event(bc, bc->next_event, 1);
986 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
990 * Remove a dying CPU from broadcasting
992 static void tick_broadcast_oneshot_offline(unsigned int cpu)
995 * Clear the broadcast masks for the dead cpu, but do not stop
996 * the broadcast device!
998 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
999 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
1000 cpumask_clear_cpu(cpu, tick_broadcast_force_mask);
1005 * Check, whether the broadcast device is in one shot mode
1007 int tick_broadcast_oneshot_active(void)
1009 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
1013 * Check whether the broadcast device supports oneshot.
1015 bool tick_broadcast_oneshot_available(void)
1017 struct clock_event_device *bc = tick_broadcast_device.evtdev;
1019 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
1023 int __tick_broadcast_oneshot_control(enum tick_broadcast_state state)
1025 struct clock_event_device *bc = tick_broadcast_device.evtdev;
1027 if (!bc || (bc->features & CLOCK_EVT_FEAT_HRTIMER))
1034 void __init tick_broadcast_init(void)
1036 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
1037 zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT);
1038 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
1039 #ifdef CONFIG_TICK_ONESHOT
1040 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
1041 zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
1042 zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);