1 // SPDX-License-Identifier: GPL-2.0
3 * This file contains the base functions to manage periodic tick
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/nmi.h>
15 #include <linux/percpu.h>
16 #include <linux/profile.h>
17 #include <linux/sched.h>
18 #include <linux/module.h>
19 #include <trace/events/power.h>
21 #include <asm/irq_regs.h>
23 #include "tick-internal.h"
28 DEFINE_PER_CPU(struct tick_device
, tick_cpu_device
);
30 * Tick next event: keeps track of the tick time
32 ktime_t tick_next_period
;
36 * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR
37 * which is responsible for calling do_timer(), i.e. the timekeeping stuff. This
38 * variable has two functions:
40 * 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the
41 * timekeeping lock all at once. Only the CPU which is assigned to do the
42 * update is handling it.
44 * 2) Hand off the duty in the NOHZ idle case by setting the value to
45 * TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks
46 * at it will take over and keep the time keeping alive. The handover
47 * procedure also covers cpu hotplug.
49 int tick_do_timer_cpu __read_mostly
= TICK_DO_TIMER_BOOT
;
50 #ifdef CONFIG_NO_HZ_FULL
52 * tick_do_timer_boot_cpu indicates the boot CPU temporarily owns
53 * tick_do_timer_cpu and it should be taken over by an eligible secondary
54 * when one comes online.
56 static int tick_do_timer_boot_cpu __read_mostly
= -1;
60 * Debugging: see timer_list.c
62 struct tick_device
*tick_get_device(int cpu
)
64 return &per_cpu(tick_cpu_device
, cpu
);
68 * tick_is_oneshot_available - check for a oneshot capable event device
70 int tick_is_oneshot_available(void)
72 struct clock_event_device
*dev
= __this_cpu_read(tick_cpu_device
.evtdev
);
74 if (!dev
|| !(dev
->features
& CLOCK_EVT_FEAT_ONESHOT
))
76 if (!(dev
->features
& CLOCK_EVT_FEAT_C3STOP
))
78 return tick_broadcast_oneshot_available();
84 static void tick_periodic(int cpu
)
86 if (tick_do_timer_cpu
== cpu
) {
87 write_seqlock(&jiffies_lock
);
89 /* Keep track of the next tick event */
90 tick_next_period
= ktime_add(tick_next_period
, tick_period
);
93 write_sequnlock(&jiffies_lock
);
97 update_process_times(user_mode(get_irq_regs()));
98 profile_tick(CPU_PROFILING
);
102 * Event handler for periodic ticks
104 void tick_handle_periodic(struct clock_event_device
*dev
)
106 int cpu
= smp_processor_id();
107 ktime_t next
= dev
->next_event
;
111 #if defined(CONFIG_HIGH_RES_TIMERS) || defined(CONFIG_NO_HZ_COMMON)
113 * The cpu might have transitioned to HIGHRES or NOHZ mode via
114 * update_process_times() -> run_local_timers() ->
115 * hrtimer_run_queues().
117 if (dev
->event_handler
!= tick_handle_periodic
)
121 if (!clockevent_state_oneshot(dev
))
125 * Setup the next period for devices, which do not have
128 next
= ktime_add(next
, tick_period
);
130 if (!clockevents_program_event(dev
, next
, false))
133 * Have to be careful here. If we're in oneshot mode,
134 * before we call tick_periodic() in a loop, we need
135 * to be sure we're using a real hardware clocksource.
136 * Otherwise we could get trapped in an infinite
137 * loop, as the tick_periodic() increments jiffies,
138 * which then will increment time, possibly causing
139 * the loop to trigger again and again.
141 if (timekeeping_valid_for_hres())
147 * Setup the device for a periodic tick
149 void tick_setup_periodic(struct clock_event_device
*dev
, int broadcast
)
151 tick_set_periodic_handler(dev
, broadcast
);
153 /* Broadcast setup ? */
154 if (!tick_device_is_functional(dev
))
157 if ((dev
->features
& CLOCK_EVT_FEAT_PERIODIC
) &&
158 !tick_broadcast_oneshot_active()) {
159 clockevents_switch_state(dev
, CLOCK_EVT_STATE_PERIODIC
);
165 seq
= read_seqbegin(&jiffies_lock
);
166 next
= tick_next_period
;
167 } while (read_seqretry(&jiffies_lock
, seq
));
169 clockevents_switch_state(dev
, CLOCK_EVT_STATE_ONESHOT
);
172 if (!clockevents_program_event(dev
, next
, false))
174 next
= ktime_add(next
, tick_period
);
179 #ifdef CONFIG_NO_HZ_FULL
180 static void giveup_do_timer(void *info
)
182 int cpu
= *(unsigned int *)info
;
184 WARN_ON(tick_do_timer_cpu
!= smp_processor_id());
186 tick_do_timer_cpu
= cpu
;
189 static void tick_take_do_timer_from_boot(void)
191 int cpu
= smp_processor_id();
192 int from
= tick_do_timer_boot_cpu
;
194 if (from
>= 0 && from
!= cpu
)
195 smp_call_function_single(from
, giveup_do_timer
, &cpu
, 1);
200 * Setup the tick device
202 static void tick_setup_device(struct tick_device
*td
,
203 struct clock_event_device
*newdev
, int cpu
,
204 const struct cpumask
*cpumask
)
206 void (*handler
)(struct clock_event_device
*) = NULL
;
207 ktime_t next_event
= 0;
210 * First device setup ?
214 * If no cpu took the do_timer update, assign it to
217 if (tick_do_timer_cpu
== TICK_DO_TIMER_BOOT
) {
218 tick_do_timer_cpu
= cpu
;
220 tick_next_period
= ktime_get();
221 tick_period
= NSEC_PER_SEC
/ HZ
;
222 #ifdef CONFIG_NO_HZ_FULL
224 * The boot CPU may be nohz_full, in which case set
225 * tick_do_timer_boot_cpu so the first housekeeping
226 * secondary that comes up will take do_timer from
229 if (tick_nohz_full_cpu(cpu
))
230 tick_do_timer_boot_cpu
= cpu
;
232 } else if (tick_do_timer_boot_cpu
!= -1 &&
233 !tick_nohz_full_cpu(cpu
)) {
234 tick_take_do_timer_from_boot();
235 tick_do_timer_boot_cpu
= -1;
236 WARN_ON(tick_do_timer_cpu
!= cpu
);
241 * Startup in periodic mode first.
243 td
->mode
= TICKDEV_MODE_PERIODIC
;
245 handler
= td
->evtdev
->event_handler
;
246 next_event
= td
->evtdev
->next_event
;
247 td
->evtdev
->event_handler
= clockevents_handle_noop
;
253 * When the device is not per cpu, pin the interrupt to the
256 if (!cpumask_equal(newdev
->cpumask
, cpumask
))
257 irq_set_affinity(newdev
->irq
, cpumask
);
260 * When global broadcasting is active, check if the current
261 * device is registered as a placeholder for broadcast mode.
262 * This allows us to handle this x86 misfeature in a generic
263 * way. This function also returns !=0 when we keep the
264 * current active broadcast state for this CPU.
266 if (tick_device_uses_broadcast(newdev
, cpu
))
269 if (td
->mode
== TICKDEV_MODE_PERIODIC
)
270 tick_setup_periodic(newdev
, 0);
272 tick_setup_oneshot(newdev
, handler
, next_event
);
275 void tick_install_replacement(struct clock_event_device
*newdev
)
277 struct tick_device
*td
= this_cpu_ptr(&tick_cpu_device
);
278 int cpu
= smp_processor_id();
280 clockevents_exchange_device(td
->evtdev
, newdev
);
281 tick_setup_device(td
, newdev
, cpu
, cpumask_of(cpu
));
282 if (newdev
->features
& CLOCK_EVT_FEAT_ONESHOT
)
283 tick_oneshot_notify();
286 static bool tick_check_percpu(struct clock_event_device
*curdev
,
287 struct clock_event_device
*newdev
, int cpu
)
289 if (!cpumask_test_cpu(cpu
, newdev
->cpumask
))
291 if (cpumask_equal(newdev
->cpumask
, cpumask_of(cpu
)))
293 /* Check if irq affinity can be set */
294 if (newdev
->irq
>= 0 && !irq_can_set_affinity(newdev
->irq
))
296 /* Prefer an existing cpu local device */
297 if (curdev
&& cpumask_equal(curdev
->cpumask
, cpumask_of(cpu
)))
302 static bool tick_check_preferred(struct clock_event_device
*curdev
,
303 struct clock_event_device
*newdev
)
305 /* Prefer oneshot capable device */
306 if (!(newdev
->features
& CLOCK_EVT_FEAT_ONESHOT
)) {
307 if (curdev
&& (curdev
->features
& CLOCK_EVT_FEAT_ONESHOT
))
309 if (tick_oneshot_mode_active())
314 * Use the higher rated one, but prefer a CPU local device with a lower
315 * rating than a non-CPU local device
318 newdev
->rating
> curdev
->rating
||
319 !cpumask_equal(curdev
->cpumask
, newdev
->cpumask
);
323 * Check whether the new device is a better fit than curdev. curdev
326 bool tick_check_replacement(struct clock_event_device
*curdev
,
327 struct clock_event_device
*newdev
)
329 if (!tick_check_percpu(curdev
, newdev
, smp_processor_id()))
332 return tick_check_preferred(curdev
, newdev
);
336 * Check, if the new registered device should be used. Called with
337 * clockevents_lock held and interrupts disabled.
339 void tick_check_new_device(struct clock_event_device
*newdev
)
341 struct clock_event_device
*curdev
;
342 struct tick_device
*td
;
345 cpu
= smp_processor_id();
346 td
= &per_cpu(tick_cpu_device
, cpu
);
349 /* cpu local device ? */
350 if (!tick_check_percpu(curdev
, newdev
, cpu
))
353 /* Preference decision */
354 if (!tick_check_preferred(curdev
, newdev
))
357 if (!try_module_get(newdev
->owner
))
361 * Replace the eventually existing device by the new
362 * device. If the current device is the broadcast device, do
363 * not give it back to the clockevents layer !
365 if (tick_is_broadcast_device(curdev
)) {
366 clockevents_shutdown(curdev
);
369 clockevents_exchange_device(curdev
, newdev
);
370 tick_setup_device(td
, newdev
, cpu
, cpumask_of(cpu
));
371 if (newdev
->features
& CLOCK_EVT_FEAT_ONESHOT
)
372 tick_oneshot_notify();
377 * Can the new device be used as a broadcast device ?
379 tick_install_broadcast_device(newdev
);
383 * tick_broadcast_oneshot_control - Enter/exit broadcast oneshot mode
384 * @state: The target state (enter/exit)
386 * The system enters/leaves a state, where affected devices might stop
387 * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups.
389 * Called with interrupts disabled, so clockevents_lock is not
390 * required here because the local clock event device cannot go away
393 int tick_broadcast_oneshot_control(enum tick_broadcast_state state
)
395 struct tick_device
*td
= this_cpu_ptr(&tick_cpu_device
);
397 if (!(td
->evtdev
->features
& CLOCK_EVT_FEAT_C3STOP
))
400 return __tick_broadcast_oneshot_control(state
);
402 EXPORT_SYMBOL_GPL(tick_broadcast_oneshot_control
);
404 #ifdef CONFIG_HOTPLUG_CPU
406 * Transfer the do_timer job away from a dying cpu.
408 * Called with interrupts disabled. Not locking required. If
409 * tick_do_timer_cpu is owned by this cpu, nothing can change it.
411 void tick_handover_do_timer(void)
413 if (tick_do_timer_cpu
== smp_processor_id()) {
414 int cpu
= cpumask_first(cpu_online_mask
);
416 tick_do_timer_cpu
= (cpu
< nr_cpu_ids
) ? cpu
:
422 * Shutdown an event device on a given cpu:
424 * This is called on a life CPU, when a CPU is dead. So we cannot
425 * access the hardware device itself.
426 * We just set the mode and remove it from the lists.
428 void tick_shutdown(unsigned int cpu
)
430 struct tick_device
*td
= &per_cpu(tick_cpu_device
, cpu
);
431 struct clock_event_device
*dev
= td
->evtdev
;
433 td
->mode
= TICKDEV_MODE_PERIODIC
;
436 * Prevent that the clock events layer tries to call
437 * the set mode function!
439 clockevent_set_state(dev
, CLOCK_EVT_STATE_DETACHED
);
440 clockevents_exchange_device(dev
, NULL
);
441 dev
->event_handler
= clockevents_handle_noop
;
448 * tick_suspend_local - Suspend the local tick device
450 * Called from the local cpu for freeze with interrupts disabled.
452 * No locks required. Nothing can change the per cpu device.
454 void tick_suspend_local(void)
456 struct tick_device
*td
= this_cpu_ptr(&tick_cpu_device
);
458 clockevents_shutdown(td
->evtdev
);
462 * tick_resume_local - Resume the local tick device
464 * Called from the local CPU for unfreeze or XEN resume magic.
466 * No locks required. Nothing can change the per cpu device.
468 void tick_resume_local(void)
470 struct tick_device
*td
= this_cpu_ptr(&tick_cpu_device
);
471 bool broadcast
= tick_resume_check_broadcast();
473 clockevents_tick_resume(td
->evtdev
);
475 if (td
->mode
== TICKDEV_MODE_PERIODIC
)
476 tick_setup_periodic(td
->evtdev
, 0);
478 tick_resume_oneshot();
483 * tick_suspend - Suspend the tick and the broadcast device
485 * Called from syscore_suspend() via timekeeping_suspend with only one
486 * CPU online and interrupts disabled or from tick_unfreeze() under
489 * No locks required. Nothing can change the per cpu device.
491 void tick_suspend(void)
493 tick_suspend_local();
494 tick_suspend_broadcast();
498 * tick_resume - Resume the tick and the broadcast device
500 * Called from syscore_resume() via timekeeping_resume with only one
501 * CPU online and interrupts disabled.
503 * No locks required. Nothing can change the per cpu device.
505 void tick_resume(void)
507 tick_resume_broadcast();
511 #ifdef CONFIG_SUSPEND
512 static DEFINE_RAW_SPINLOCK(tick_freeze_lock
);
513 static unsigned int tick_freeze_depth
;
516 * tick_freeze - Suspend the local tick and (possibly) timekeeping.
518 * Check if this is the last online CPU executing the function and if so,
519 * suspend timekeeping. Otherwise suspend the local tick.
521 * Call with interrupts disabled. Must be balanced with %tick_unfreeze().
522 * Interrupts must not be enabled before the subsequent %tick_unfreeze().
524 void tick_freeze(void)
526 raw_spin_lock(&tick_freeze_lock
);
529 if (tick_freeze_depth
== num_online_cpus()) {
530 trace_suspend_resume(TPS("timekeeping_freeze"),
531 smp_processor_id(), true);
532 system_state
= SYSTEM_SUSPEND
;
533 sched_clock_suspend();
534 timekeeping_suspend();
536 tick_suspend_local();
539 raw_spin_unlock(&tick_freeze_lock
);
543 * tick_unfreeze - Resume the local tick and (possibly) timekeeping.
545 * Check if this is the first CPU executing the function and if so, resume
546 * timekeeping. Otherwise resume the local tick.
548 * Call with interrupts disabled. Must be balanced with %tick_freeze().
549 * Interrupts must not be enabled after the preceding %tick_freeze().
551 void tick_unfreeze(void)
553 raw_spin_lock(&tick_freeze_lock
);
555 if (tick_freeze_depth
== num_online_cpus()) {
556 timekeeping_resume();
557 sched_clock_resume();
558 system_state
= SYSTEM_RUNNING
;
559 trace_suspend_resume(TPS("timekeeping_freeze"),
560 smp_processor_id(), false);
562 touch_softlockup_watchdog();
568 raw_spin_unlock(&tick_freeze_lock
);
570 #endif /* CONFIG_SUSPEND */
573 * tick_init - initialize the tick control
575 void __init
tick_init(void)
577 tick_broadcast_init();