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);
40 static inline void tick_broadcast_setup_oneshot(struct clock_event_device *bc) { BUG(); }
41 static inline void tick_broadcast_clear_oneshot(int cpu) { }
42 static inline void tick_resume_broadcast_oneshot(struct clock_event_device *bc) { }
46 * Debugging: see timer_list.c
48 struct tick_device *tick_get_broadcast_device(void)
50 return &tick_broadcast_device;
53 struct cpumask *tick_get_broadcast_mask(void)
55 return tick_broadcast_mask;
59 * Start the device in periodic mode
61 static void tick_broadcast_start_periodic(struct clock_event_device *bc)
64 tick_setup_periodic(bc, 1);
68 * Check, if the device can be utilized as broadcast device:
70 static bool tick_check_broadcast_device(struct clock_event_device *curdev,
71 struct clock_event_device *newdev)
73 if ((newdev->features & CLOCK_EVT_FEAT_DUMMY) ||
74 (newdev->features & CLOCK_EVT_FEAT_PERCPU) ||
75 (newdev->features & CLOCK_EVT_FEAT_C3STOP))
78 if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT &&
79 !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
82 return !curdev || newdev->rating > curdev->rating;
86 * Conditionally install/replace broadcast device
88 void tick_install_broadcast_device(struct clock_event_device *dev)
90 struct clock_event_device *cur = tick_broadcast_device.evtdev;
92 if (!tick_check_broadcast_device(cur, dev))
95 if (!try_module_get(dev->owner))
98 clockevents_exchange_device(cur, dev);
100 cur->event_handler = clockevents_handle_noop;
101 tick_broadcast_device.evtdev = dev;
102 if (!cpumask_empty(tick_broadcast_mask))
103 tick_broadcast_start_periodic(dev);
105 * Inform all cpus about this. We might be in a situation
106 * where we did not switch to oneshot mode because the per cpu
107 * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
108 * of a oneshot capable broadcast device. Without that
109 * notification the systems stays stuck in periodic mode
112 if (dev->features & CLOCK_EVT_FEAT_ONESHOT)
117 * Check, if the device is the broadcast device
119 int tick_is_broadcast_device(struct clock_event_device *dev)
121 return (dev && tick_broadcast_device.evtdev == dev);
124 int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq)
128 if (tick_is_broadcast_device(dev)) {
129 raw_spin_lock(&tick_broadcast_lock);
130 ret = __clockevents_update_freq(dev, freq);
131 raw_spin_unlock(&tick_broadcast_lock);
137 static void err_broadcast(const struct cpumask *mask)
139 pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
142 static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
145 dev->broadcast = tick_broadcast;
146 if (!dev->broadcast) {
147 pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
149 dev->broadcast = err_broadcast;
154 * Check, if the device is disfunctional and a place holder, which
155 * needs to be handled by the broadcast device.
157 int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
159 struct clock_event_device *bc = tick_broadcast_device.evtdev;
163 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
166 * Devices might be registered with both periodic and oneshot
167 * mode disabled. This signals, that the device needs to be
168 * operated from the broadcast device and is a placeholder for
169 * the cpu local device.
171 if (!tick_device_is_functional(dev)) {
172 dev->event_handler = tick_handle_periodic;
173 tick_device_setup_broadcast_func(dev);
174 cpumask_set_cpu(cpu, tick_broadcast_mask);
175 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
176 tick_broadcast_start_periodic(bc);
178 tick_broadcast_setup_oneshot(bc);
182 * Clear the broadcast bit for this cpu if the
183 * device is not power state affected.
185 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
186 cpumask_clear_cpu(cpu, tick_broadcast_mask);
188 tick_device_setup_broadcast_func(dev);
191 * Clear the broadcast bit if the CPU is not in
192 * periodic broadcast on state.
194 if (!cpumask_test_cpu(cpu, tick_broadcast_on))
195 cpumask_clear_cpu(cpu, tick_broadcast_mask);
197 switch (tick_broadcast_device.mode) {
198 case TICKDEV_MODE_ONESHOT:
200 * If the system is in oneshot mode we can
201 * unconditionally clear the oneshot mask bit,
202 * because the CPU is running and therefore
203 * not in an idle state which causes the power
204 * state affected device to stop. Let the
205 * caller initialize the device.
207 tick_broadcast_clear_oneshot(cpu);
211 case TICKDEV_MODE_PERIODIC:
213 * If the system is in periodic mode, check
214 * whether the broadcast device can be
217 if (cpumask_empty(tick_broadcast_mask) && bc)
218 clockevents_shutdown(bc);
220 * If we kept the cpu in the broadcast mask,
221 * tell the caller to leave the per cpu device
222 * in shutdown state. The periodic interrupt
223 * is delivered by the broadcast device, if
224 * the broadcast device exists and is not
227 if (bc && !(bc->features & CLOCK_EVT_FEAT_HRTIMER))
228 ret = cpumask_test_cpu(cpu, tick_broadcast_mask);
234 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
238 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
239 int tick_receive_broadcast(void)
241 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
242 struct clock_event_device *evt = td->evtdev;
247 if (!evt->event_handler)
250 evt->event_handler(evt);
256 * Broadcast the event to the cpus, which are set in the mask (mangled).
258 static bool tick_do_broadcast(struct cpumask *mask)
260 int cpu = smp_processor_id();
261 struct tick_device *td;
265 * Check, if the current cpu is in the mask
267 if (cpumask_test_cpu(cpu, mask)) {
268 struct clock_event_device *bc = tick_broadcast_device.evtdev;
270 cpumask_clear_cpu(cpu, mask);
272 * We only run the local handler, if the broadcast
273 * device is not hrtimer based. Otherwise we run into
274 * a hrtimer recursion.
276 * local timer_interrupt()
283 local = !(bc->features & CLOCK_EVT_FEAT_HRTIMER);
286 if (!cpumask_empty(mask)) {
288 * It might be necessary to actually check whether the devices
289 * have different broadcast functions. For now, just use the
290 * one of the first device. This works as long as we have this
291 * misfeature only on x86 (lapic)
293 td = &per_cpu(tick_cpu_device, cpumask_first(mask));
294 td->evtdev->broadcast(mask);
300 * Periodic broadcast:
301 * - invoke the broadcast handlers
303 static bool tick_do_periodic_broadcast(void)
305 cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
306 return tick_do_broadcast(tmpmask);
310 * Event handler for periodic broadcast ticks
312 static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
314 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
317 raw_spin_lock(&tick_broadcast_lock);
319 /* Handle spurious interrupts gracefully */
320 if (clockevent_state_shutdown(tick_broadcast_device.evtdev)) {
321 raw_spin_unlock(&tick_broadcast_lock);
325 bc_local = tick_do_periodic_broadcast();
327 if (clockevent_state_oneshot(dev)) {
328 ktime_t next = ktime_add(dev->next_event, tick_period);
330 clockevents_program_event(dev, next, true);
332 raw_spin_unlock(&tick_broadcast_lock);
335 * We run the handler of the local cpu after dropping
336 * tick_broadcast_lock because the handler might deadlock when
337 * trying to switch to oneshot mode.
340 td->evtdev->event_handler(td->evtdev);
344 * tick_broadcast_control - Enable/disable or force broadcast mode
345 * @mode: The selected broadcast mode
347 * Called when the system enters a state where affected tick devices
348 * might stop. Note: TICK_BROADCAST_FORCE cannot be undone.
350 void tick_broadcast_control(enum tick_broadcast_mode mode)
352 struct clock_event_device *bc, *dev;
353 struct tick_device *td;
357 /* Protects also the local clockevent device. */
358 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
359 td = this_cpu_ptr(&tick_cpu_device);
363 * Is the device not affected by the powerstate ?
365 if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
368 if (!tick_device_is_functional(dev))
371 cpu = smp_processor_id();
372 bc = tick_broadcast_device.evtdev;
373 bc_stopped = cpumask_empty(tick_broadcast_mask);
376 case TICK_BROADCAST_FORCE:
377 tick_broadcast_forced = 1;
379 case TICK_BROADCAST_ON:
380 cpumask_set_cpu(cpu, tick_broadcast_on);
381 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
383 * Only shutdown the cpu local device, if:
385 * - the broadcast device exists
386 * - the broadcast device is not a hrtimer based one
387 * - the broadcast device is in periodic mode to
388 * avoid a hickup during switch to oneshot mode
390 if (bc && !(bc->features & CLOCK_EVT_FEAT_HRTIMER) &&
391 tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
392 clockevents_shutdown(dev);
396 case TICK_BROADCAST_OFF:
397 if (tick_broadcast_forced)
399 cpumask_clear_cpu(cpu, tick_broadcast_on);
400 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
401 if (tick_broadcast_device.mode ==
402 TICKDEV_MODE_PERIODIC)
403 tick_setup_periodic(dev, 0);
409 if (cpumask_empty(tick_broadcast_mask)) {
411 clockevents_shutdown(bc);
412 } else if (bc_stopped) {
413 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
414 tick_broadcast_start_periodic(bc);
416 tick_broadcast_setup_oneshot(bc);
420 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
422 EXPORT_SYMBOL_GPL(tick_broadcast_control);
425 * Set the periodic handler depending on broadcast on/off
427 void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
430 dev->event_handler = tick_handle_periodic;
432 dev->event_handler = tick_handle_periodic_broadcast;
435 #ifdef CONFIG_HOTPLUG_CPU
437 * Remove a CPU from broadcasting
439 void tick_shutdown_broadcast(unsigned int cpu)
441 struct clock_event_device *bc;
444 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
446 bc = tick_broadcast_device.evtdev;
447 cpumask_clear_cpu(cpu, tick_broadcast_mask);
448 cpumask_clear_cpu(cpu, tick_broadcast_on);
450 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
451 if (bc && cpumask_empty(tick_broadcast_mask))
452 clockevents_shutdown(bc);
455 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
459 void tick_suspend_broadcast(void)
461 struct clock_event_device *bc;
464 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
466 bc = tick_broadcast_device.evtdev;
468 clockevents_shutdown(bc);
470 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
474 * This is called from tick_resume_local() on a resuming CPU. That's
475 * called from the core resume function, tick_unfreeze() and the magic XEN
478 * In none of these cases the broadcast device mode can change and the
479 * bit of the resuming CPU in the broadcast mask is safe as well.
481 bool tick_resume_check_broadcast(void)
483 if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT)
486 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_mask);
489 void tick_resume_broadcast(void)
491 struct clock_event_device *bc;
494 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
496 bc = tick_broadcast_device.evtdev;
499 clockevents_tick_resume(bc);
501 switch (tick_broadcast_device.mode) {
502 case TICKDEV_MODE_PERIODIC:
503 if (!cpumask_empty(tick_broadcast_mask))
504 tick_broadcast_start_periodic(bc);
506 case TICKDEV_MODE_ONESHOT:
507 if (!cpumask_empty(tick_broadcast_mask))
508 tick_resume_broadcast_oneshot(bc);
512 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
515 #ifdef CONFIG_TICK_ONESHOT
517 static cpumask_var_t tick_broadcast_oneshot_mask __cpumask_var_read_mostly;
518 static cpumask_var_t tick_broadcast_pending_mask __cpumask_var_read_mostly;
519 static cpumask_var_t tick_broadcast_force_mask __cpumask_var_read_mostly;
522 * Exposed for debugging: see timer_list.c
524 struct cpumask *tick_get_broadcast_oneshot_mask(void)
526 return tick_broadcast_oneshot_mask;
530 * Called before going idle with interrupts disabled. Checks whether a
531 * broadcast event from the other core is about to happen. We detected
532 * that in tick_broadcast_oneshot_control(). The callsite can use this
533 * to avoid a deep idle transition as we are about to get the
534 * broadcast IPI right away.
536 int tick_check_broadcast_expired(void)
538 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
542 * Set broadcast interrupt affinity
544 static void tick_broadcast_set_affinity(struct clock_event_device *bc,
545 const struct cpumask *cpumask)
547 if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
550 if (cpumask_equal(bc->cpumask, cpumask))
553 bc->cpumask = cpumask;
554 irq_set_affinity(bc->irq, bc->cpumask);
557 static void tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
560 if (!clockevent_state_oneshot(bc))
561 clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
563 clockevents_program_event(bc, expires, 1);
564 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
567 static void tick_resume_broadcast_oneshot(struct clock_event_device *bc)
569 clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
573 * Called from irq_enter() when idle was interrupted to reenable the
576 void tick_check_oneshot_broadcast_this_cpu(void)
578 if (cpumask_test_cpu(smp_processor_id(), tick_broadcast_oneshot_mask)) {
579 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
582 * We might be in the middle of switching over from
583 * periodic to oneshot. If the CPU has not yet
584 * switched over, leave the device alone.
586 if (td->mode == TICKDEV_MODE_ONESHOT) {
587 clockevents_switch_state(td->evtdev,
588 CLOCK_EVT_STATE_ONESHOT);
594 * Handle oneshot mode broadcasting
596 static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
598 struct tick_device *td;
599 ktime_t now, next_event;
600 int cpu, next_cpu = 0;
603 raw_spin_lock(&tick_broadcast_lock);
604 dev->next_event = KTIME_MAX;
605 next_event = KTIME_MAX;
606 cpumask_clear(tmpmask);
608 /* Find all expired events */
609 for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
611 * Required for !SMP because for_each_cpu() reports
612 * unconditionally CPU0 as set on UP kernels.
614 if (!IS_ENABLED(CONFIG_SMP) &&
615 cpumask_empty(tick_broadcast_oneshot_mask))
618 td = &per_cpu(tick_cpu_device, cpu);
619 if (td->evtdev->next_event <= now) {
620 cpumask_set_cpu(cpu, tmpmask);
622 * Mark the remote cpu in the pending mask, so
623 * it can avoid reprogramming the cpu local
624 * timer in tick_broadcast_oneshot_control().
626 cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
627 } else if (td->evtdev->next_event < next_event) {
628 next_event = td->evtdev->next_event;
634 * Remove the current cpu from the pending mask. The event is
635 * delivered immediately in tick_do_broadcast() !
637 cpumask_clear_cpu(smp_processor_id(), tick_broadcast_pending_mask);
639 /* Take care of enforced broadcast requests */
640 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
641 cpumask_clear(tick_broadcast_force_mask);
644 * Sanity check. Catch the case where we try to broadcast to
647 if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask)))
648 cpumask_and(tmpmask, tmpmask, cpu_online_mask);
651 * Wakeup the cpus which have an expired event.
653 bc_local = tick_do_broadcast(tmpmask);
656 * Two reasons for reprogram:
658 * - The global event did not expire any CPU local
659 * events. This happens in dyntick mode, as the maximum PIT
660 * delta is quite small.
662 * - There are pending events on sleeping CPUs which were not
665 if (next_event != KTIME_MAX)
666 tick_broadcast_set_event(dev, next_cpu, next_event);
668 raw_spin_unlock(&tick_broadcast_lock);
671 td = this_cpu_ptr(&tick_cpu_device);
672 td->evtdev->event_handler(td->evtdev);
676 static int broadcast_needs_cpu(struct clock_event_device *bc, int cpu)
678 if (!(bc->features & CLOCK_EVT_FEAT_HRTIMER))
680 if (bc->next_event == KTIME_MAX)
682 return bc->bound_on == cpu ? -EBUSY : 0;
685 static void broadcast_shutdown_local(struct clock_event_device *bc,
686 struct clock_event_device *dev)
689 * For hrtimer based broadcasting we cannot shutdown the cpu
690 * local device if our own event is the first one to expire or
691 * if we own the broadcast timer.
693 if (bc->features & CLOCK_EVT_FEAT_HRTIMER) {
694 if (broadcast_needs_cpu(bc, smp_processor_id()))
696 if (dev->next_event < bc->next_event)
699 clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
702 int __tick_broadcast_oneshot_control(enum tick_broadcast_state state)
704 struct clock_event_device *bc, *dev;
709 * If there is no broadcast device, tell the caller not to go
712 if (!tick_broadcast_device.evtdev)
715 dev = this_cpu_ptr(&tick_cpu_device)->evtdev;
717 raw_spin_lock(&tick_broadcast_lock);
718 bc = tick_broadcast_device.evtdev;
719 cpu = smp_processor_id();
721 if (state == TICK_BROADCAST_ENTER) {
723 * If the current CPU owns the hrtimer broadcast
724 * mechanism, it cannot go deep idle and we do not add
725 * the CPU to the broadcast mask. We don't have to go
726 * through the EXIT path as the local timer is not
729 ret = broadcast_needs_cpu(bc, cpu);
734 * If the broadcast device is in periodic mode, we
737 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
738 /* If it is a hrtimer based broadcast, return busy */
739 if (bc->features & CLOCK_EVT_FEAT_HRTIMER)
744 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
745 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
747 /* Conditionally shut down the local timer. */
748 broadcast_shutdown_local(bc, dev);
751 * We only reprogram the broadcast timer if we
752 * did not mark ourself in the force mask and
753 * if the cpu local event is earlier than the
754 * broadcast event. If the current CPU is in
755 * the force mask, then we are going to be
756 * woken by the IPI right away; we return
757 * busy, so the CPU does not try to go deep
760 if (cpumask_test_cpu(cpu, tick_broadcast_force_mask)) {
762 } else if (dev->next_event < bc->next_event) {
763 tick_broadcast_set_event(bc, cpu, dev->next_event);
765 * In case of hrtimer broadcasts the
766 * programming might have moved the
767 * timer to this cpu. If yes, remove
768 * us from the broadcast mask and
771 ret = broadcast_needs_cpu(bc, cpu);
773 cpumask_clear_cpu(cpu,
774 tick_broadcast_oneshot_mask);
779 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
780 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
782 * The cpu which was handling the broadcast
783 * timer marked this cpu in the broadcast
784 * pending mask and fired the broadcast
785 * IPI. So we are going to handle the expired
786 * event anyway via the broadcast IPI
787 * handler. No need to reprogram the timer
788 * with an already expired event.
790 if (cpumask_test_and_clear_cpu(cpu,
791 tick_broadcast_pending_mask))
795 * Bail out if there is no next event.
797 if (dev->next_event == KTIME_MAX)
800 * If the pending bit is not set, then we are
801 * either the CPU handling the broadcast
802 * interrupt or we got woken by something else.
804 * We are not longer in the broadcast mask, so
805 * if the cpu local expiry time is already
806 * reached, we would reprogram the cpu local
807 * timer with an already expired event.
809 * This can lead to a ping-pong when we return
810 * to idle and therefor rearm the broadcast
811 * timer before the cpu local timer was able
812 * to fire. This happens because the forced
813 * reprogramming makes sure that the event
814 * will happen in the future and depending on
815 * the min_delta setting this might be far
816 * enough out that the ping-pong starts.
818 * If the cpu local next_event has expired
819 * then we know that the broadcast timer
820 * next_event has expired as well and
821 * broadcast is about to be handled. So we
822 * avoid reprogramming and enforce that the
823 * broadcast handler, which did not run yet,
824 * will invoke the cpu local handler.
826 * We cannot call the handler directly from
827 * here, because we might be in a NOHZ phase
828 * and we did not go through the irq_enter()
832 if (dev->next_event <= now) {
833 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
837 * We got woken by something else. Reprogram
838 * the cpu local timer device.
840 tick_program_event(dev->next_event, 1);
844 raw_spin_unlock(&tick_broadcast_lock);
849 * Reset the one shot broadcast for a cpu
851 * Called with tick_broadcast_lock held
853 static void tick_broadcast_clear_oneshot(int cpu)
855 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
856 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
859 static void tick_broadcast_init_next_event(struct cpumask *mask,
862 struct tick_device *td;
865 for_each_cpu(cpu, mask) {
866 td = &per_cpu(tick_cpu_device, cpu);
868 td->evtdev->next_event = expires;
873 * tick_broadcast_setup_oneshot - setup the broadcast device
875 static void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
877 int cpu = smp_processor_id();
882 /* Set it up only once ! */
883 if (bc->event_handler != tick_handle_oneshot_broadcast) {
884 int was_periodic = clockevent_state_periodic(bc);
886 bc->event_handler = tick_handle_oneshot_broadcast;
889 * We must be careful here. There might be other CPUs
890 * waiting for periodic broadcast. We need to set the
891 * oneshot_mask bits for those and program the
892 * broadcast device to fire.
894 cpumask_copy(tmpmask, tick_broadcast_mask);
895 cpumask_clear_cpu(cpu, tmpmask);
896 cpumask_or(tick_broadcast_oneshot_mask,
897 tick_broadcast_oneshot_mask, tmpmask);
899 if (was_periodic && !cpumask_empty(tmpmask)) {
900 clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
901 tick_broadcast_init_next_event(tmpmask,
903 tick_broadcast_set_event(bc, cpu, tick_next_period);
905 bc->next_event = KTIME_MAX;
908 * The first cpu which switches to oneshot mode sets
909 * the bit for all other cpus which are in the general
910 * (periodic) broadcast mask. So the bit is set and
911 * would prevent the first broadcast enter after this
912 * to program the bc device.
914 tick_broadcast_clear_oneshot(cpu);
919 * Select oneshot operating mode for the broadcast device
921 void tick_broadcast_switch_to_oneshot(void)
923 struct clock_event_device *bc;
926 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
928 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
929 bc = tick_broadcast_device.evtdev;
931 tick_broadcast_setup_oneshot(bc);
933 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
936 #ifdef CONFIG_HOTPLUG_CPU
937 void hotplug_cpu__broadcast_tick_pull(int deadcpu)
939 struct clock_event_device *bc;
942 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
943 bc = tick_broadcast_device.evtdev;
945 if (bc && broadcast_needs_cpu(bc, deadcpu)) {
946 /* This moves the broadcast assignment to this CPU: */
947 clockevents_program_event(bc, bc->next_event, 1);
949 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
953 * Remove a dead CPU from broadcasting
955 void tick_shutdown_broadcast_oneshot(unsigned int cpu)
959 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
962 * Clear the broadcast masks for the dead cpu, but do not stop
963 * the broadcast device!
965 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
966 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
967 cpumask_clear_cpu(cpu, tick_broadcast_force_mask);
969 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
974 * Check, whether the broadcast device is in one shot mode
976 int tick_broadcast_oneshot_active(void)
978 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
982 * Check whether the broadcast device supports oneshot.
984 bool tick_broadcast_oneshot_available(void)
986 struct clock_event_device *bc = tick_broadcast_device.evtdev;
988 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
992 int __tick_broadcast_oneshot_control(enum tick_broadcast_state state)
994 struct clock_event_device *bc = tick_broadcast_device.evtdev;
996 if (!bc || (bc->features & CLOCK_EVT_FEAT_HRTIMER))
1003 void __init tick_broadcast_init(void)
1005 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
1006 zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT);
1007 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
1008 #ifdef CONFIG_TICK_ONESHOT
1009 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
1010 zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
1011 zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);