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.
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;
378 case TICK_BROADCAST_ON:
379 cpumask_set_cpu(cpu, tick_broadcast_on);
380 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
382 * Only shutdown the cpu local device, if:
384 * - the broadcast device exists
385 * - the broadcast device is not a hrtimer based one
386 * - the broadcast device is in periodic mode to
387 * avoid a hickup during switch to oneshot mode
389 if (bc && !(bc->features & CLOCK_EVT_FEAT_HRTIMER) &&
390 tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
391 clockevents_shutdown(dev);
395 case TICK_BROADCAST_OFF:
396 if (tick_broadcast_forced)
398 cpumask_clear_cpu(cpu, tick_broadcast_on);
399 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
400 if (tick_broadcast_device.mode ==
401 TICKDEV_MODE_PERIODIC)
402 tick_setup_periodic(dev, 0);
408 if (cpumask_empty(tick_broadcast_mask)) {
410 clockevents_shutdown(bc);
411 } else if (bc_stopped) {
412 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
413 tick_broadcast_start_periodic(bc);
415 tick_broadcast_setup_oneshot(bc);
419 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
421 EXPORT_SYMBOL_GPL(tick_broadcast_control);
424 * Set the periodic handler depending on broadcast on/off
426 void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
429 dev->event_handler = tick_handle_periodic;
431 dev->event_handler = tick_handle_periodic_broadcast;
434 #ifdef CONFIG_HOTPLUG_CPU
436 * Remove a CPU from broadcasting
438 void tick_shutdown_broadcast(unsigned int cpu)
440 struct clock_event_device *bc;
443 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
445 bc = tick_broadcast_device.evtdev;
446 cpumask_clear_cpu(cpu, tick_broadcast_mask);
447 cpumask_clear_cpu(cpu, tick_broadcast_on);
449 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
450 if (bc && cpumask_empty(tick_broadcast_mask))
451 clockevents_shutdown(bc);
454 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
458 void tick_suspend_broadcast(void)
460 struct clock_event_device *bc;
463 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
465 bc = tick_broadcast_device.evtdev;
467 clockevents_shutdown(bc);
469 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
473 * This is called from tick_resume_local() on a resuming CPU. That's
474 * called from the core resume function, tick_unfreeze() and the magic XEN
477 * In none of these cases the broadcast device mode can change and the
478 * bit of the resuming CPU in the broadcast mask is safe as well.
480 bool tick_resume_check_broadcast(void)
482 if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT)
485 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_mask);
488 void tick_resume_broadcast(void)
490 struct clock_event_device *bc;
493 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
495 bc = tick_broadcast_device.evtdev;
498 clockevents_tick_resume(bc);
500 switch (tick_broadcast_device.mode) {
501 case TICKDEV_MODE_PERIODIC:
502 if (!cpumask_empty(tick_broadcast_mask))
503 tick_broadcast_start_periodic(bc);
505 case TICKDEV_MODE_ONESHOT:
506 if (!cpumask_empty(tick_broadcast_mask))
507 tick_resume_broadcast_oneshot(bc);
511 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
514 #ifdef CONFIG_TICK_ONESHOT
516 static cpumask_var_t tick_broadcast_oneshot_mask __cpumask_var_read_mostly;
517 static cpumask_var_t tick_broadcast_pending_mask __cpumask_var_read_mostly;
518 static cpumask_var_t tick_broadcast_force_mask __cpumask_var_read_mostly;
521 * Exposed for debugging: see timer_list.c
523 struct cpumask *tick_get_broadcast_oneshot_mask(void)
525 return tick_broadcast_oneshot_mask;
529 * Called before going idle with interrupts disabled. Checks whether a
530 * broadcast event from the other core is about to happen. We detected
531 * that in tick_broadcast_oneshot_control(). The callsite can use this
532 * to avoid a deep idle transition as we are about to get the
533 * broadcast IPI right away.
535 int tick_check_broadcast_expired(void)
537 return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
541 * Set broadcast interrupt affinity
543 static void tick_broadcast_set_affinity(struct clock_event_device *bc,
544 const struct cpumask *cpumask)
546 if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
549 if (cpumask_equal(bc->cpumask, cpumask))
552 bc->cpumask = cpumask;
553 irq_set_affinity(bc->irq, bc->cpumask);
556 static void tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
559 if (!clockevent_state_oneshot(bc))
560 clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
562 clockevents_program_event(bc, expires, 1);
563 tick_broadcast_set_affinity(bc, cpumask_of(cpu));
566 static void tick_resume_broadcast_oneshot(struct clock_event_device *bc)
568 clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
572 * Called from irq_enter() when idle was interrupted to reenable the
575 void tick_check_oneshot_broadcast_this_cpu(void)
577 if (cpumask_test_cpu(smp_processor_id(), tick_broadcast_oneshot_mask)) {
578 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
581 * We might be in the middle of switching over from
582 * periodic to oneshot. If the CPU has not yet
583 * switched over, leave the device alone.
585 if (td->mode == TICKDEV_MODE_ONESHOT) {
586 clockevents_switch_state(td->evtdev,
587 CLOCK_EVT_STATE_ONESHOT);
593 * Handle oneshot mode broadcasting
595 static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
597 struct tick_device *td;
598 ktime_t now, next_event;
599 int cpu, next_cpu = 0;
602 raw_spin_lock(&tick_broadcast_lock);
603 dev->next_event = KTIME_MAX;
604 next_event = KTIME_MAX;
605 cpumask_clear(tmpmask);
607 /* Find all expired events */
608 for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
610 * Required for !SMP because for_each_cpu() reports
611 * unconditionally CPU0 as set on UP kernels.
613 if (!IS_ENABLED(CONFIG_SMP) &&
614 cpumask_empty(tick_broadcast_oneshot_mask))
617 td = &per_cpu(tick_cpu_device, cpu);
618 if (td->evtdev->next_event <= now) {
619 cpumask_set_cpu(cpu, tmpmask);
621 * Mark the remote cpu in the pending mask, so
622 * it can avoid reprogramming the cpu local
623 * timer in tick_broadcast_oneshot_control().
625 cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
626 } else if (td->evtdev->next_event < next_event) {
627 next_event = td->evtdev->next_event;
633 * Remove the current cpu from the pending mask. The event is
634 * delivered immediately in tick_do_broadcast() !
636 cpumask_clear_cpu(smp_processor_id(), tick_broadcast_pending_mask);
638 /* Take care of enforced broadcast requests */
639 cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
640 cpumask_clear(tick_broadcast_force_mask);
643 * Sanity check. Catch the case where we try to broadcast to
646 if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask)))
647 cpumask_and(tmpmask, tmpmask, cpu_online_mask);
650 * Wakeup the cpus which have an expired event.
652 bc_local = tick_do_broadcast(tmpmask);
655 * Two reasons for reprogram:
657 * - The global event did not expire any CPU local
658 * events. This happens in dyntick mode, as the maximum PIT
659 * delta is quite small.
661 * - There are pending events on sleeping CPUs which were not
664 if (next_event != KTIME_MAX)
665 tick_broadcast_set_event(dev, next_cpu, next_event);
667 raw_spin_unlock(&tick_broadcast_lock);
670 td = this_cpu_ptr(&tick_cpu_device);
671 td->evtdev->event_handler(td->evtdev);
675 static int broadcast_needs_cpu(struct clock_event_device *bc, int cpu)
677 if (!(bc->features & CLOCK_EVT_FEAT_HRTIMER))
679 if (bc->next_event == KTIME_MAX)
681 return bc->bound_on == cpu ? -EBUSY : 0;
684 static void broadcast_shutdown_local(struct clock_event_device *bc,
685 struct clock_event_device *dev)
688 * For hrtimer based broadcasting we cannot shutdown the cpu
689 * local device if our own event is the first one to expire or
690 * if we own the broadcast timer.
692 if (bc->features & CLOCK_EVT_FEAT_HRTIMER) {
693 if (broadcast_needs_cpu(bc, smp_processor_id()))
695 if (dev->next_event < bc->next_event)
698 clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
701 int __tick_broadcast_oneshot_control(enum tick_broadcast_state state)
703 struct clock_event_device *bc, *dev;
708 * If there is no broadcast device, tell the caller not to go
711 if (!tick_broadcast_device.evtdev)
714 dev = this_cpu_ptr(&tick_cpu_device)->evtdev;
716 raw_spin_lock(&tick_broadcast_lock);
717 bc = tick_broadcast_device.evtdev;
718 cpu = smp_processor_id();
720 if (state == TICK_BROADCAST_ENTER) {
722 * If the current CPU owns the hrtimer broadcast
723 * mechanism, it cannot go deep idle and we do not add
724 * the CPU to the broadcast mask. We don't have to go
725 * through the EXIT path as the local timer is not
728 ret = broadcast_needs_cpu(bc, cpu);
733 * If the broadcast device is in periodic mode, we
736 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
737 /* If it is a hrtimer based broadcast, return busy */
738 if (bc->features & CLOCK_EVT_FEAT_HRTIMER)
743 if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
744 WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
746 /* Conditionally shut down the local timer. */
747 broadcast_shutdown_local(bc, dev);
750 * We only reprogram the broadcast timer if we
751 * did not mark ourself in the force mask and
752 * if the cpu local event is earlier than the
753 * broadcast event. If the current CPU is in
754 * the force mask, then we are going to be
755 * woken by the IPI right away; we return
756 * busy, so the CPU does not try to go deep
759 if (cpumask_test_cpu(cpu, tick_broadcast_force_mask)) {
761 } else if (dev->next_event < bc->next_event) {
762 tick_broadcast_set_event(bc, cpu, dev->next_event);
764 * In case of hrtimer broadcasts the
765 * programming might have moved the
766 * timer to this cpu. If yes, remove
767 * us from the broadcast mask and
770 ret = broadcast_needs_cpu(bc, cpu);
772 cpumask_clear_cpu(cpu,
773 tick_broadcast_oneshot_mask);
778 if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
779 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
781 * The cpu which was handling the broadcast
782 * timer marked this cpu in the broadcast
783 * pending mask and fired the broadcast
784 * IPI. So we are going to handle the expired
785 * event anyway via the broadcast IPI
786 * handler. No need to reprogram the timer
787 * with an already expired event.
789 if (cpumask_test_and_clear_cpu(cpu,
790 tick_broadcast_pending_mask))
794 * Bail out if there is no next event.
796 if (dev->next_event == KTIME_MAX)
799 * If the pending bit is not set, then we are
800 * either the CPU handling the broadcast
801 * interrupt or we got woken by something else.
803 * We are not longer in the broadcast mask, so
804 * if the cpu local expiry time is already
805 * reached, we would reprogram the cpu local
806 * timer with an already expired event.
808 * This can lead to a ping-pong when we return
809 * to idle and therefor rearm the broadcast
810 * timer before the cpu local timer was able
811 * to fire. This happens because the forced
812 * reprogramming makes sure that the event
813 * will happen in the future and depending on
814 * the min_delta setting this might be far
815 * enough out that the ping-pong starts.
817 * If the cpu local next_event has expired
818 * then we know that the broadcast timer
819 * next_event has expired as well and
820 * broadcast is about to be handled. So we
821 * avoid reprogramming and enforce that the
822 * broadcast handler, which did not run yet,
823 * will invoke the cpu local handler.
825 * We cannot call the handler directly from
826 * here, because we might be in a NOHZ phase
827 * and we did not go through the irq_enter()
831 if (dev->next_event <= now) {
832 cpumask_set_cpu(cpu, tick_broadcast_force_mask);
836 * We got woken by something else. Reprogram
837 * the cpu local timer device.
839 tick_program_event(dev->next_event, 1);
843 raw_spin_unlock(&tick_broadcast_lock);
848 * Reset the one shot broadcast for a cpu
850 * Called with tick_broadcast_lock held
852 static void tick_broadcast_clear_oneshot(int cpu)
854 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
855 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
858 static void tick_broadcast_init_next_event(struct cpumask *mask,
861 struct tick_device *td;
864 for_each_cpu(cpu, mask) {
865 td = &per_cpu(tick_cpu_device, cpu);
867 td->evtdev->next_event = expires;
872 * tick_broadcast_setup_oneshot - setup the broadcast device
874 static void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
876 int cpu = smp_processor_id();
881 /* Set it up only once ! */
882 if (bc->event_handler != tick_handle_oneshot_broadcast) {
883 int was_periodic = clockevent_state_periodic(bc);
885 bc->event_handler = tick_handle_oneshot_broadcast;
888 * We must be careful here. There might be other CPUs
889 * waiting for periodic broadcast. We need to set the
890 * oneshot_mask bits for those and program the
891 * broadcast device to fire.
893 cpumask_copy(tmpmask, tick_broadcast_mask);
894 cpumask_clear_cpu(cpu, tmpmask);
895 cpumask_or(tick_broadcast_oneshot_mask,
896 tick_broadcast_oneshot_mask, tmpmask);
898 if (was_periodic && !cpumask_empty(tmpmask)) {
899 clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
900 tick_broadcast_init_next_event(tmpmask,
902 tick_broadcast_set_event(bc, cpu, tick_next_period);
904 bc->next_event = KTIME_MAX;
907 * The first cpu which switches to oneshot mode sets
908 * the bit for all other cpus which are in the general
909 * (periodic) broadcast mask. So the bit is set and
910 * would prevent the first broadcast enter after this
911 * to program the bc device.
913 tick_broadcast_clear_oneshot(cpu);
918 * Select oneshot operating mode for the broadcast device
920 void tick_broadcast_switch_to_oneshot(void)
922 struct clock_event_device *bc;
925 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
927 tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
928 bc = tick_broadcast_device.evtdev;
930 tick_broadcast_setup_oneshot(bc);
932 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
935 #ifdef CONFIG_HOTPLUG_CPU
936 void hotplug_cpu__broadcast_tick_pull(int deadcpu)
938 struct clock_event_device *bc;
941 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
942 bc = tick_broadcast_device.evtdev;
944 if (bc && broadcast_needs_cpu(bc, deadcpu)) {
945 /* This moves the broadcast assignment to this CPU: */
946 clockevents_program_event(bc, bc->next_event, 1);
948 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
952 * Remove a dead CPU from broadcasting
954 void tick_shutdown_broadcast_oneshot(unsigned int cpu)
958 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
961 * Clear the broadcast masks for the dead cpu, but do not stop
962 * the broadcast device!
964 cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
965 cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
966 cpumask_clear_cpu(cpu, tick_broadcast_force_mask);
968 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
973 * Check, whether the broadcast device is in one shot mode
975 int tick_broadcast_oneshot_active(void)
977 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
981 * Check whether the broadcast device supports oneshot.
983 bool tick_broadcast_oneshot_available(void)
985 struct clock_event_device *bc = tick_broadcast_device.evtdev;
987 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
991 int __tick_broadcast_oneshot_control(enum tick_broadcast_state state)
993 struct clock_event_device *bc = tick_broadcast_device.evtdev;
995 if (!bc || (bc->features & CLOCK_EVT_FEAT_HRTIMER))
1002 void __init tick_broadcast_init(void)
1004 zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
1005 zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT);
1006 zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
1007 #ifdef CONFIG_TICK_ONESHOT
1008 zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
1009 zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
1010 zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);