1 // SPDX-License-Identifier: GPL-2.0
3 * This file contains the base functions to manage periodic tick
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/module.h>
18 #include <trace/events/power.h>
20 #include <asm/irq_regs.h>
22 #include "tick-internal.h"
27 DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
29 * Tick next event: keeps track of the tick time
31 ktime_t tick_next_period;
35 * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR
36 * which is responsible for calling do_timer(), i.e. the timekeeping stuff. This
37 * variable has two functions:
39 * 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the
40 * timekeeping lock all at once. Only the CPU which is assigned to do the
41 * update is handling it.
43 * 2) Hand off the duty in the NOHZ idle case by setting the value to
44 * TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks
45 * at it will take over and keep the time keeping alive. The handover
46 * procedure also covers cpu hotplug.
48 int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT;
51 * Debugging: see timer_list.c
53 struct tick_device *tick_get_device(int cpu)
55 return &per_cpu(tick_cpu_device, cpu);
59 * tick_is_oneshot_available - check for a oneshot capable event device
61 int tick_is_oneshot_available(void)
63 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
65 if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
67 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
69 return tick_broadcast_oneshot_available();
75 static void tick_periodic(int cpu)
77 if (tick_do_timer_cpu == cpu) {
78 write_seqlock(&jiffies_lock);
80 /* Keep track of the next tick event */
81 tick_next_period = ktime_add(tick_next_period, tick_period);
84 write_sequnlock(&jiffies_lock);
88 update_process_times(user_mode(get_irq_regs()));
89 profile_tick(CPU_PROFILING);
93 * Event handler for periodic ticks
95 void tick_handle_periodic(struct clock_event_device *dev)
97 int cpu = smp_processor_id();
98 ktime_t next = dev->next_event;
102 #if defined(CONFIG_HIGH_RES_TIMERS) || defined(CONFIG_NO_HZ_COMMON)
104 * The cpu might have transitioned to HIGHRES or NOHZ mode via
105 * update_process_times() -> run_local_timers() ->
106 * hrtimer_run_queues().
108 if (dev->event_handler != tick_handle_periodic)
112 if (!clockevent_state_oneshot(dev))
116 * Setup the next period for devices, which do not have
119 next = ktime_add(next, tick_period);
121 if (!clockevents_program_event(dev, next, false))
124 * Have to be careful here. If we're in oneshot mode,
125 * before we call tick_periodic() in a loop, we need
126 * to be sure we're using a real hardware clocksource.
127 * Otherwise we could get trapped in an infinite
128 * loop, as the tick_periodic() increments jiffies,
129 * which then will increment time, possibly causing
130 * the loop to trigger again and again.
132 if (timekeeping_valid_for_hres())
138 * Setup the device for a periodic tick
140 void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
142 tick_set_periodic_handler(dev, broadcast);
144 /* Broadcast setup ? */
145 if (!tick_device_is_functional(dev))
148 if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
149 !tick_broadcast_oneshot_active()) {
150 clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
156 seq = read_seqbegin(&jiffies_lock);
157 next = tick_next_period;
158 } while (read_seqretry(&jiffies_lock, seq));
160 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
163 if (!clockevents_program_event(dev, next, false))
165 next = ktime_add(next, tick_period);
171 * Setup the tick device
173 static void tick_setup_device(struct tick_device *td,
174 struct clock_event_device *newdev, int cpu,
175 const struct cpumask *cpumask)
177 void (*handler)(struct clock_event_device *) = NULL;
178 ktime_t next_event = 0;
181 * First device setup ?
185 * If no cpu took the do_timer update, assign it to
188 if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) {
189 if (!tick_nohz_full_cpu(cpu))
190 tick_do_timer_cpu = cpu;
192 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
193 tick_next_period = ktime_get();
194 tick_period = NSEC_PER_SEC / HZ;
198 * Startup in periodic mode first.
200 td->mode = TICKDEV_MODE_PERIODIC;
202 handler = td->evtdev->event_handler;
203 next_event = td->evtdev->next_event;
204 td->evtdev->event_handler = clockevents_handle_noop;
210 * When the device is not per cpu, pin the interrupt to the
213 if (!cpumask_equal(newdev->cpumask, cpumask))
214 irq_set_affinity(newdev->irq, cpumask);
217 * When global broadcasting is active, check if the current
218 * device is registered as a placeholder for broadcast mode.
219 * This allows us to handle this x86 misfeature in a generic
220 * way. This function also returns !=0 when we keep the
221 * current active broadcast state for this CPU.
223 if (tick_device_uses_broadcast(newdev, cpu))
226 if (td->mode == TICKDEV_MODE_PERIODIC)
227 tick_setup_periodic(newdev, 0);
229 tick_setup_oneshot(newdev, handler, next_event);
232 void tick_install_replacement(struct clock_event_device *newdev)
234 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
235 int cpu = smp_processor_id();
237 clockevents_exchange_device(td->evtdev, newdev);
238 tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
239 if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
240 tick_oneshot_notify();
243 static bool tick_check_percpu(struct clock_event_device *curdev,
244 struct clock_event_device *newdev, int cpu)
246 if (!cpumask_test_cpu(cpu, newdev->cpumask))
248 if (cpumask_equal(newdev->cpumask, cpumask_of(cpu)))
250 /* Check if irq affinity can be set */
251 if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq))
253 /* Prefer an existing cpu local device */
254 if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
259 static bool tick_check_preferred(struct clock_event_device *curdev,
260 struct clock_event_device *newdev)
262 /* Prefer oneshot capable device */
263 if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) {
264 if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT))
266 if (tick_oneshot_mode_active())
271 * Use the higher rated one, but prefer a CPU local device with a lower
272 * rating than a non-CPU local device
275 newdev->rating > curdev->rating ||
276 !cpumask_equal(curdev->cpumask, newdev->cpumask);
280 * Check whether the new device is a better fit than curdev. curdev
283 bool tick_check_replacement(struct clock_event_device *curdev,
284 struct clock_event_device *newdev)
286 if (!tick_check_percpu(curdev, newdev, smp_processor_id()))
289 return tick_check_preferred(curdev, newdev);
293 * Check, if the new registered device should be used. Called with
294 * clockevents_lock held and interrupts disabled.
296 void tick_check_new_device(struct clock_event_device *newdev)
298 struct clock_event_device *curdev;
299 struct tick_device *td;
302 cpu = smp_processor_id();
303 td = &per_cpu(tick_cpu_device, cpu);
306 /* cpu local device ? */
307 if (!tick_check_percpu(curdev, newdev, cpu))
310 /* Preference decision */
311 if (!tick_check_preferred(curdev, newdev))
314 if (!try_module_get(newdev->owner))
318 * Replace the eventually existing device by the new
319 * device. If the current device is the broadcast device, do
320 * not give it back to the clockevents layer !
322 if (tick_is_broadcast_device(curdev)) {
323 clockevents_shutdown(curdev);
326 clockevents_exchange_device(curdev, newdev);
327 tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
328 if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
329 tick_oneshot_notify();
334 * Can the new device be used as a broadcast device ?
336 tick_install_broadcast_device(newdev);
340 * tick_broadcast_oneshot_control - Enter/exit broadcast oneshot mode
341 * @state: The target state (enter/exit)
343 * The system enters/leaves a state, where affected devices might stop
344 * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups.
346 * Called with interrupts disabled, so clockevents_lock is not
347 * required here because the local clock event device cannot go away
350 int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
352 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
354 if (!(td->evtdev->features & CLOCK_EVT_FEAT_C3STOP))
357 return __tick_broadcast_oneshot_control(state);
359 EXPORT_SYMBOL_GPL(tick_broadcast_oneshot_control);
361 #ifdef CONFIG_HOTPLUG_CPU
363 * Transfer the do_timer job away from a dying cpu.
365 * Called with interrupts disabled. Not locking required. If
366 * tick_do_timer_cpu is owned by this cpu, nothing can change it.
368 void tick_handover_do_timer(void)
370 if (tick_do_timer_cpu == smp_processor_id()) {
371 int cpu = cpumask_first(cpu_online_mask);
373 tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu :
379 * Shutdown an event device on a given cpu:
381 * This is called on a life CPU, when a CPU is dead. So we cannot
382 * access the hardware device itself.
383 * We just set the mode and remove it from the lists.
385 void tick_shutdown(unsigned int cpu)
387 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
388 struct clock_event_device *dev = td->evtdev;
390 td->mode = TICKDEV_MODE_PERIODIC;
393 * Prevent that the clock events layer tries to call
394 * the set mode function!
396 clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
397 clockevents_exchange_device(dev, NULL);
398 dev->event_handler = clockevents_handle_noop;
405 * tick_suspend_local - Suspend the local tick device
407 * Called from the local cpu for freeze with interrupts disabled.
409 * No locks required. Nothing can change the per cpu device.
411 void tick_suspend_local(void)
413 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
415 clockevents_shutdown(td->evtdev);
419 * tick_resume_local - Resume the local tick device
421 * Called from the local CPU for unfreeze or XEN resume magic.
423 * No locks required. Nothing can change the per cpu device.
425 void tick_resume_local(void)
427 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
428 bool broadcast = tick_resume_check_broadcast();
430 clockevents_tick_resume(td->evtdev);
432 if (td->mode == TICKDEV_MODE_PERIODIC)
433 tick_setup_periodic(td->evtdev, 0);
435 tick_resume_oneshot();
440 * tick_suspend - Suspend the tick and the broadcast device
442 * Called from syscore_suspend() via timekeeping_suspend with only one
443 * CPU online and interrupts disabled or from tick_unfreeze() under
446 * No locks required. Nothing can change the per cpu device.
448 void tick_suspend(void)
450 tick_suspend_local();
451 tick_suspend_broadcast();
455 * tick_resume - Resume the tick and the broadcast device
457 * Called from syscore_resume() via timekeeping_resume with only one
458 * CPU online and interrupts disabled.
460 * No locks required. Nothing can change the per cpu device.
462 void tick_resume(void)
464 tick_resume_broadcast();
468 #ifdef CONFIG_SUSPEND
469 static DEFINE_RAW_SPINLOCK(tick_freeze_lock);
470 static unsigned int tick_freeze_depth;
473 * tick_freeze - Suspend the local tick and (possibly) timekeeping.
475 * Check if this is the last online CPU executing the function and if so,
476 * suspend timekeeping. Otherwise suspend the local tick.
478 * Call with interrupts disabled. Must be balanced with %tick_unfreeze().
479 * Interrupts must not be enabled before the subsequent %tick_unfreeze().
481 void tick_freeze(void)
483 raw_spin_lock(&tick_freeze_lock);
486 if (tick_freeze_depth == num_online_cpus()) {
487 trace_suspend_resume(TPS("timekeeping_freeze"),
488 smp_processor_id(), true);
489 system_state = SYSTEM_SUSPEND;
490 timekeeping_suspend();
492 tick_suspend_local();
495 raw_spin_unlock(&tick_freeze_lock);
499 * tick_unfreeze - Resume the local tick and (possibly) timekeeping.
501 * Check if this is the first CPU executing the function and if so, resume
502 * timekeeping. Otherwise resume the local tick.
504 * Call with interrupts disabled. Must be balanced with %tick_freeze().
505 * Interrupts must not be enabled after the preceding %tick_freeze().
507 void tick_unfreeze(void)
509 raw_spin_lock(&tick_freeze_lock);
511 if (tick_freeze_depth == num_online_cpus()) {
512 timekeeping_resume();
513 system_state = SYSTEM_RUNNING;
514 trace_suspend_resume(TPS("timekeeping_freeze"),
515 smp_processor_id(), false);
522 raw_spin_unlock(&tick_freeze_lock);
524 #endif /* CONFIG_SUSPEND */
527 * tick_init - initialize the tick control
529 void __init tick_init(void)
531 tick_broadcast_init();