135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
2906568c9SThomas Gleixner /*
3906568c9SThomas Gleixner * This file contains the base functions to manage periodic tick
4906568c9SThomas Gleixner * related events.
5906568c9SThomas Gleixner *
6906568c9SThomas Gleixner * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
7906568c9SThomas Gleixner * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
8906568c9SThomas Gleixner * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
9906568c9SThomas Gleixner */
10906568c9SThomas Gleixner #include <linux/cpu.h>
11906568c9SThomas Gleixner #include <linux/err.h>
12906568c9SThomas Gleixner #include <linux/hrtimer.h>
13d7b90689SRussell King #include <linux/interrupt.h>
145167c506SChunyan Zhang #include <linux/nmi.h>
15906568c9SThomas Gleixner #include <linux/percpu.h>
16906568c9SThomas Gleixner #include <linux/profile.h>
17906568c9SThomas Gleixner #include <linux/sched.h>
18ccf33d68SThomas Gleixner #include <linux/module.h>
1975e0678eSRafael J. Wysocki #include <trace/events/power.h>
20906568c9SThomas Gleixner
21d7b90689SRussell King #include <asm/irq_regs.h>
22d7b90689SRussell King
23f8381cbaSThomas Gleixner #include "tick-internal.h"
24f8381cbaSThomas Gleixner
25906568c9SThomas Gleixner /*
26906568c9SThomas Gleixner * Tick devices
27906568c9SThomas Gleixner */
28f8381cbaSThomas Gleixner DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
29906568c9SThomas Gleixner /*
30c398960cSThomas Gleixner * Tick next event: keeps track of the tick time. It's updated by the
31c398960cSThomas Gleixner * CPU which handles the tick and protected by jiffies_lock. There is
32c398960cSThomas Gleixner * no requirement to write hold the jiffies seqcount for it.
33906568c9SThomas Gleixner */
34f8381cbaSThomas Gleixner ktime_t tick_next_period;
35050ded1bSAndrew Morton
36050ded1bSAndrew Morton /*
37050ded1bSAndrew Morton * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR
38050ded1bSAndrew Morton * which is responsible for calling do_timer(), i.e. the timekeeping stuff. This
39050ded1bSAndrew Morton * variable has two functions:
40050ded1bSAndrew Morton *
41050ded1bSAndrew Morton * 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the
42050ded1bSAndrew Morton * timekeeping lock all at once. Only the CPU which is assigned to do the
43050ded1bSAndrew Morton * update is handling it.
44050ded1bSAndrew Morton *
45050ded1bSAndrew Morton * 2) Hand off the duty in the NOHZ idle case by setting the value to
46050ded1bSAndrew Morton * TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks
47050ded1bSAndrew Morton * at it will take over and keep the time keeping alive. The handover
48050ded1bSAndrew Morton * procedure also covers cpu hotplug.
49050ded1bSAndrew Morton */
506441402bSThomas Gleixner int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT;
5108ae95f4SNicholas Piggin #ifdef CONFIG_NO_HZ_FULL
5208ae95f4SNicholas Piggin /*
5308ae95f4SNicholas Piggin * tick_do_timer_boot_cpu indicates the boot CPU temporarily owns
5408ae95f4SNicholas Piggin * tick_do_timer_cpu and it should be taken over by an eligible secondary
5508ae95f4SNicholas Piggin * when one comes online.
5608ae95f4SNicholas Piggin */
5708ae95f4SNicholas Piggin static int tick_do_timer_boot_cpu __read_mostly = -1;
5808ae95f4SNicholas Piggin #endif
59906568c9SThomas Gleixner
60289f480aSIngo Molnar /*
61289f480aSIngo Molnar * Debugging: see timer_list.c
62289f480aSIngo Molnar */
tick_get_device(int cpu)63289f480aSIngo Molnar struct tick_device *tick_get_device(int cpu)
64289f480aSIngo Molnar {
65289f480aSIngo Molnar return &per_cpu(tick_cpu_device, cpu);
66289f480aSIngo Molnar }
67289f480aSIngo Molnar
6879bf2bb3SThomas Gleixner /**
6979bf2bb3SThomas Gleixner * tick_is_oneshot_available - check for a oneshot capable event device
7079bf2bb3SThomas Gleixner */
tick_is_oneshot_available(void)7179bf2bb3SThomas Gleixner int tick_is_oneshot_available(void)
7279bf2bb3SThomas Gleixner {
73909ea964SChristoph Lameter struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
7479bf2bb3SThomas Gleixner
753a142a06SThomas Gleixner if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
763a142a06SThomas Gleixner return 0;
773a142a06SThomas Gleixner if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
783a142a06SThomas Gleixner return 1;
793a142a06SThomas Gleixner return tick_broadcast_oneshot_available();
8079bf2bb3SThomas Gleixner }
8179bf2bb3SThomas Gleixner
82906568c9SThomas Gleixner /*
83906568c9SThomas Gleixner * Periodic tick
84906568c9SThomas Gleixner */
tick_periodic(int cpu)85906568c9SThomas Gleixner static void tick_periodic(int cpu)
86906568c9SThomas Gleixner {
87906568c9SThomas Gleixner if (tick_do_timer_cpu == cpu) {
88e5d4d175SThomas Gleixner raw_spin_lock(&jiffies_lock);
89e5d4d175SThomas Gleixner write_seqcount_begin(&jiffies_seq);
90906568c9SThomas Gleixner
91906568c9SThomas Gleixner /* Keep track of the next tick event */
92b9965449SThomas Gleixner tick_next_period = ktime_add_ns(tick_next_period, TICK_NSEC);
93906568c9SThomas Gleixner
94906568c9SThomas Gleixner do_timer(1);
95e5d4d175SThomas Gleixner write_seqcount_end(&jiffies_seq);
96e5d4d175SThomas Gleixner raw_spin_unlock(&jiffies_lock);
9747a1b796SJohn Stultz update_wall_time();
98906568c9SThomas Gleixner }
99906568c9SThomas Gleixner
100906568c9SThomas Gleixner update_process_times(user_mode(get_irq_regs()));
101906568c9SThomas Gleixner profile_tick(CPU_PROFILING);
102906568c9SThomas Gleixner }
103906568c9SThomas Gleixner
104906568c9SThomas Gleixner /*
105906568c9SThomas Gleixner * Event handler for periodic ticks
106906568c9SThomas Gleixner */
tick_handle_periodic(struct clock_event_device * dev)107906568c9SThomas Gleixner void tick_handle_periodic(struct clock_event_device *dev)
108906568c9SThomas Gleixner {
109906568c9SThomas Gleixner int cpu = smp_processor_id();
110b97f0291SViresh Kumar ktime_t next = dev->next_event;
111906568c9SThomas Gleixner
112906568c9SThomas Gleixner tick_periodic(cpu);
113906568c9SThomas Gleixner
114c6eb3f70SThomas Gleixner #if defined(CONFIG_HIGH_RES_TIMERS) || defined(CONFIG_NO_HZ_COMMON)
115c6eb3f70SThomas Gleixner /*
116c6eb3f70SThomas Gleixner * The cpu might have transitioned to HIGHRES or NOHZ mode via
117c6eb3f70SThomas Gleixner * update_process_times() -> run_local_timers() ->
118c6eb3f70SThomas Gleixner * hrtimer_run_queues().
119c6eb3f70SThomas Gleixner */
120c6eb3f70SThomas Gleixner if (dev->event_handler != tick_handle_periodic)
121c6eb3f70SThomas Gleixner return;
122c6eb3f70SThomas Gleixner #endif
123c6eb3f70SThomas Gleixner
124472c4a94SViresh Kumar if (!clockevent_state_oneshot(dev))
125906568c9SThomas Gleixner return;
126b97f0291SViresh Kumar for (;;) {
127906568c9SThomas Gleixner /*
128906568c9SThomas Gleixner * Setup the next period for devices, which do not have
129906568c9SThomas Gleixner * periodic mode:
130906568c9SThomas Gleixner */
131b9965449SThomas Gleixner next = ktime_add_ns(next, TICK_NSEC);
132b97f0291SViresh Kumar
133d1748302SMartin Schwidefsky if (!clockevents_program_event(dev, next, false))
134906568c9SThomas Gleixner return;
13574a03b69Sjohn stultz /*
13674a03b69Sjohn stultz * Have to be careful here. If we're in oneshot mode,
13774a03b69Sjohn stultz * before we call tick_periodic() in a loop, we need
13874a03b69Sjohn stultz * to be sure we're using a real hardware clocksource.
13974a03b69Sjohn stultz * Otherwise we could get trapped in an infinite
14074a03b69Sjohn stultz * loop, as the tick_periodic() increments jiffies,
141cacb3c76SViresh Kumar * which then will increment time, possibly causing
14274a03b69Sjohn stultz * the loop to trigger again and again.
14374a03b69Sjohn stultz */
14474a03b69Sjohn stultz if (timekeeping_valid_for_hres())
145906568c9SThomas Gleixner tick_periodic(cpu);
146906568c9SThomas Gleixner }
147906568c9SThomas Gleixner }
148906568c9SThomas Gleixner
149906568c9SThomas Gleixner /*
150906568c9SThomas Gleixner * Setup the device for a periodic tick
151906568c9SThomas Gleixner */
tick_setup_periodic(struct clock_event_device * dev,int broadcast)152f8381cbaSThomas Gleixner void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
153906568c9SThomas Gleixner {
154f8381cbaSThomas Gleixner tick_set_periodic_handler(dev, broadcast);
155f8381cbaSThomas Gleixner
156f8381cbaSThomas Gleixner /* Broadcast setup ? */
157f8381cbaSThomas Gleixner if (!tick_device_is_functional(dev))
158f8381cbaSThomas Gleixner return;
159906568c9SThomas Gleixner
16027ce4cb4SThomas Gleixner if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
16127ce4cb4SThomas Gleixner !tick_broadcast_oneshot_active()) {
162d7eb231cSThomas Gleixner clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
163906568c9SThomas Gleixner } else {
164e1e41b6cSRasmus Villemoes unsigned int seq;
165906568c9SThomas Gleixner ktime_t next;
166906568c9SThomas Gleixner
167906568c9SThomas Gleixner do {
168e5d4d175SThomas Gleixner seq = read_seqcount_begin(&jiffies_seq);
169906568c9SThomas Gleixner next = tick_next_period;
170e5d4d175SThomas Gleixner } while (read_seqcount_retry(&jiffies_seq, seq));
171906568c9SThomas Gleixner
172d7eb231cSThomas Gleixner clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
173906568c9SThomas Gleixner
174906568c9SThomas Gleixner for (;;) {
175d1748302SMartin Schwidefsky if (!clockevents_program_event(dev, next, false))
176906568c9SThomas Gleixner return;
177b9965449SThomas Gleixner next = ktime_add_ns(next, TICK_NSEC);
178906568c9SThomas Gleixner }
179906568c9SThomas Gleixner }
180906568c9SThomas Gleixner }
181906568c9SThomas Gleixner
182906568c9SThomas Gleixner /*
183906568c9SThomas Gleixner * Setup the tick device
184906568c9SThomas Gleixner */
tick_setup_device(struct tick_device * td,struct clock_event_device * newdev,int cpu,const struct cpumask * cpumask)185906568c9SThomas Gleixner static void tick_setup_device(struct tick_device *td,
186906568c9SThomas Gleixner struct clock_event_device *newdev, int cpu,
1870de26520SRusty Russell const struct cpumask *cpumask)
188906568c9SThomas Gleixner {
189906568c9SThomas Gleixner void (*handler)(struct clock_event_device *) = NULL;
1908b0e1953SThomas Gleixner ktime_t next_event = 0;
191906568c9SThomas Gleixner
192906568c9SThomas Gleixner /*
193906568c9SThomas Gleixner * First device setup ?
194906568c9SThomas Gleixner */
195906568c9SThomas Gleixner if (!td->evtdev) {
196906568c9SThomas Gleixner /*
197906568c9SThomas Gleixner * If no cpu took the do_timer update, assign it to
198906568c9SThomas Gleixner * this cpu:
199906568c9SThomas Gleixner */
2006441402bSThomas Gleixner if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) {
201906568c9SThomas Gleixner tick_do_timer_cpu = cpu;
20213bb06f8SThomas Gleixner tick_next_period = ktime_get();
20308ae95f4SNicholas Piggin #ifdef CONFIG_NO_HZ_FULL
20408ae95f4SNicholas Piggin /*
205*93d61e1bSOleg Nesterov * The boot CPU may be nohz_full, in which case the
206*93d61e1bSOleg Nesterov * first housekeeping secondary will take do_timer()
207*93d61e1bSOleg Nesterov * from it.
20808ae95f4SNicholas Piggin */
20908ae95f4SNicholas Piggin if (tick_nohz_full_cpu(cpu))
21008ae95f4SNicholas Piggin tick_do_timer_boot_cpu = cpu;
21108ae95f4SNicholas Piggin
212*93d61e1bSOleg Nesterov } else if (tick_do_timer_boot_cpu != -1 && !tick_nohz_full_cpu(cpu)) {
21308ae95f4SNicholas Piggin tick_do_timer_boot_cpu = -1;
214*93d61e1bSOleg Nesterov /*
215*93d61e1bSOleg Nesterov * The boot CPU will stay in periodic (NOHZ disabled)
216*93d61e1bSOleg Nesterov * mode until clocksource_done_booting() called after
217*93d61e1bSOleg Nesterov * smp_init() selects a high resolution clocksource and
218*93d61e1bSOleg Nesterov * timekeeping_notify() kicks the NOHZ stuff alive.
219*93d61e1bSOleg Nesterov *
220*93d61e1bSOleg Nesterov * So this WRITE_ONCE can only race with the READ_ONCE
221*93d61e1bSOleg Nesterov * check in tick_periodic() but this race is harmless.
222*93d61e1bSOleg Nesterov */
223*93d61e1bSOleg Nesterov WRITE_ONCE(tick_do_timer_cpu, cpu);
22408ae95f4SNicholas Piggin #endif
225906568c9SThomas Gleixner }
226906568c9SThomas Gleixner
227906568c9SThomas Gleixner /*
228906568c9SThomas Gleixner * Startup in periodic mode first.
229906568c9SThomas Gleixner */
230906568c9SThomas Gleixner td->mode = TICKDEV_MODE_PERIODIC;
231906568c9SThomas Gleixner } else {
232906568c9SThomas Gleixner handler = td->evtdev->event_handler;
233906568c9SThomas Gleixner next_event = td->evtdev->next_event;
2347c1e7689SVenkatesh Pallipadi td->evtdev->event_handler = clockevents_handle_noop;
235906568c9SThomas Gleixner }
236906568c9SThomas Gleixner
237906568c9SThomas Gleixner td->evtdev = newdev;
238906568c9SThomas Gleixner
239906568c9SThomas Gleixner /*
240906568c9SThomas Gleixner * When the device is not per cpu, pin the interrupt to the
241906568c9SThomas Gleixner * current cpu:
242906568c9SThomas Gleixner */
243320ab2b0SRusty Russell if (!cpumask_equal(newdev->cpumask, cpumask))
2440de26520SRusty Russell irq_set_affinity(newdev->irq, cpumask);
245906568c9SThomas Gleixner
246f8381cbaSThomas Gleixner /*
247f8381cbaSThomas Gleixner * When global broadcasting is active, check if the current
248f8381cbaSThomas Gleixner * device is registered as a placeholder for broadcast mode.
249f8381cbaSThomas Gleixner * This allows us to handle this x86 misfeature in a generic
25007bd1172SThomas Gleixner * way. This function also returns !=0 when we keep the
25107bd1172SThomas Gleixner * current active broadcast state for this CPU.
252f8381cbaSThomas Gleixner */
253f8381cbaSThomas Gleixner if (tick_device_uses_broadcast(newdev, cpu))
254f8381cbaSThomas Gleixner return;
255f8381cbaSThomas Gleixner
256906568c9SThomas Gleixner if (td->mode == TICKDEV_MODE_PERIODIC)
257906568c9SThomas Gleixner tick_setup_periodic(newdev, 0);
25879bf2bb3SThomas Gleixner else
25979bf2bb3SThomas Gleixner tick_setup_oneshot(newdev, handler, next_event);
260906568c9SThomas Gleixner }
261906568c9SThomas Gleixner
tick_install_replacement(struct clock_event_device * newdev)26203e13cf5SThomas Gleixner void tick_install_replacement(struct clock_event_device *newdev)
26303e13cf5SThomas Gleixner {
26422127e93SChristoph Lameter struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
26503e13cf5SThomas Gleixner int cpu = smp_processor_id();
26603e13cf5SThomas Gleixner
26703e13cf5SThomas Gleixner clockevents_exchange_device(td->evtdev, newdev);
26803e13cf5SThomas Gleixner tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
26903e13cf5SThomas Gleixner if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
27003e13cf5SThomas Gleixner tick_oneshot_notify();
27103e13cf5SThomas Gleixner }
27203e13cf5SThomas Gleixner
tick_check_percpu(struct clock_event_device * curdev,struct clock_event_device * newdev,int cpu)27345cb8e01SThomas Gleixner static bool tick_check_percpu(struct clock_event_device *curdev,
27445cb8e01SThomas Gleixner struct clock_event_device *newdev, int cpu)
27545cb8e01SThomas Gleixner {
27645cb8e01SThomas Gleixner if (!cpumask_test_cpu(cpu, newdev->cpumask))
27745cb8e01SThomas Gleixner return false;
27845cb8e01SThomas Gleixner if (cpumask_equal(newdev->cpumask, cpumask_of(cpu)))
27945cb8e01SThomas Gleixner return true;
28045cb8e01SThomas Gleixner /* Check if irq affinity can be set */
28145cb8e01SThomas Gleixner if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq))
28245cb8e01SThomas Gleixner return false;
28345cb8e01SThomas Gleixner /* Prefer an existing cpu local device */
28445cb8e01SThomas Gleixner if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
28545cb8e01SThomas Gleixner return false;
28645cb8e01SThomas Gleixner return true;
28745cb8e01SThomas Gleixner }
28845cb8e01SThomas Gleixner
tick_check_preferred(struct clock_event_device * curdev,struct clock_event_device * newdev)28945cb8e01SThomas Gleixner static bool tick_check_preferred(struct clock_event_device *curdev,
29045cb8e01SThomas Gleixner struct clock_event_device *newdev)
29145cb8e01SThomas Gleixner {
29245cb8e01SThomas Gleixner /* Prefer oneshot capable device */
29345cb8e01SThomas Gleixner if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) {
29445cb8e01SThomas Gleixner if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT))
29545cb8e01SThomas Gleixner return false;
29645cb8e01SThomas Gleixner if (tick_oneshot_mode_active())
29745cb8e01SThomas Gleixner return false;
29845cb8e01SThomas Gleixner }
29945cb8e01SThomas Gleixner
30070e5975dSStephen Boyd /*
30170e5975dSStephen Boyd * Use the higher rated one, but prefer a CPU local device with a lower
30270e5975dSStephen Boyd * rating than a non-CPU local device
30370e5975dSStephen Boyd */
30470e5975dSStephen Boyd return !curdev ||
30570e5975dSStephen Boyd newdev->rating > curdev->rating ||
3065b5ccbc2SSudeep Holla !cpumask_equal(curdev->cpumask, newdev->cpumask);
30745cb8e01SThomas Gleixner }
30845cb8e01SThomas Gleixner
309906568c9SThomas Gleixner /*
31003e13cf5SThomas Gleixner * Check whether the new device is a better fit than curdev. curdev
31103e13cf5SThomas Gleixner * can be NULL !
31203e13cf5SThomas Gleixner */
tick_check_replacement(struct clock_event_device * curdev,struct clock_event_device * newdev)31303e13cf5SThomas Gleixner bool tick_check_replacement(struct clock_event_device *curdev,
31403e13cf5SThomas Gleixner struct clock_event_device *newdev)
31503e13cf5SThomas Gleixner {
316521c4299SViresh Kumar if (!tick_check_percpu(curdev, newdev, smp_processor_id()))
31703e13cf5SThomas Gleixner return false;
31803e13cf5SThomas Gleixner
31903e13cf5SThomas Gleixner return tick_check_preferred(curdev, newdev);
32003e13cf5SThomas Gleixner }
32103e13cf5SThomas Gleixner
32203e13cf5SThomas Gleixner /*
3237126cac4SThomas Gleixner * Check, if the new registered device should be used. Called with
3247126cac4SThomas Gleixner * clockevents_lock held and interrupts disabled.
325906568c9SThomas Gleixner */
tick_check_new_device(struct clock_event_device * newdev)3267172a286SThomas Gleixner void tick_check_new_device(struct clock_event_device *newdev)
327906568c9SThomas Gleixner {
328906568c9SThomas Gleixner struct clock_event_device *curdev;
329906568c9SThomas Gleixner struct tick_device *td;
3307172a286SThomas Gleixner int cpu;
331906568c9SThomas Gleixner
332906568c9SThomas Gleixner cpu = smp_processor_id();
333906568c9SThomas Gleixner td = &per_cpu(tick_cpu_device, cpu);
334906568c9SThomas Gleixner curdev = td->evtdev;
335906568c9SThomas Gleixner
336d7840aaaSWang Wensheng if (!tick_check_replacement(curdev, newdev))
337906568c9SThomas Gleixner goto out_bc;
338906568c9SThomas Gleixner
339ccf33d68SThomas Gleixner if (!try_module_get(newdev->owner))
340ccf33d68SThomas Gleixner return;
341ccf33d68SThomas Gleixner
342906568c9SThomas Gleixner /*
343906568c9SThomas Gleixner * Replace the eventually existing device by the new
344f8381cbaSThomas Gleixner * device. If the current device is the broadcast device, do
345f8381cbaSThomas Gleixner * not give it back to the clockevents layer !
346906568c9SThomas Gleixner */
347f8381cbaSThomas Gleixner if (tick_is_broadcast_device(curdev)) {
3482344abbcSThomas Gleixner clockevents_shutdown(curdev);
349f8381cbaSThomas Gleixner curdev = NULL;
350f8381cbaSThomas Gleixner }
351906568c9SThomas Gleixner clockevents_exchange_device(curdev, newdev);
3526b954823SRusty Russell tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
35379bf2bb3SThomas Gleixner if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
35479bf2bb3SThomas Gleixner tick_oneshot_notify();
3557172a286SThomas Gleixner return;
356f8381cbaSThomas Gleixner
357f8381cbaSThomas Gleixner out_bc:
358f8381cbaSThomas Gleixner /*
359f8381cbaSThomas Gleixner * Can the new device be used as a broadcast device ?
360f8381cbaSThomas Gleixner */
361c94a8537SWill Deacon tick_install_broadcast_device(newdev, cpu);
362906568c9SThomas Gleixner }
363906568c9SThomas Gleixner
364f32dd117SThomas Gleixner /**
365f32dd117SThomas Gleixner * tick_broadcast_oneshot_control - Enter/exit broadcast oneshot mode
366f32dd117SThomas Gleixner * @state: The target state (enter/exit)
367f32dd117SThomas Gleixner *
368f32dd117SThomas Gleixner * The system enters/leaves a state, where affected devices might stop
369f32dd117SThomas Gleixner * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups.
370f32dd117SThomas Gleixner *
371f32dd117SThomas Gleixner * Called with interrupts disabled, so clockevents_lock is not
372f32dd117SThomas Gleixner * required here because the local clock event device cannot go away
373f32dd117SThomas Gleixner * under us.
374f32dd117SThomas Gleixner */
tick_broadcast_oneshot_control(enum tick_broadcast_state state)375f32dd117SThomas Gleixner int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
376f32dd117SThomas Gleixner {
377f32dd117SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
378f32dd117SThomas Gleixner
379f32dd117SThomas Gleixner if (!(td->evtdev->features & CLOCK_EVT_FEAT_C3STOP))
380f32dd117SThomas Gleixner return 0;
381f32dd117SThomas Gleixner
382f32dd117SThomas Gleixner return __tick_broadcast_oneshot_control(state);
383f32dd117SThomas Gleixner }
3840f447051SThomas Gleixner EXPORT_SYMBOL_GPL(tick_broadcast_oneshot_control);
385f32dd117SThomas Gleixner
38652c063d1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
387906568c9SThomas Gleixner /*
38894df7de0SSebastien Dugue * Transfer the do_timer job away from a dying cpu.
38994df7de0SSebastien Dugue *
390f12ad423SThomas Gleixner * Called with interrupts disabled. No locking required. If
39152c063d1SThomas Gleixner * tick_do_timer_cpu is owned by this cpu, nothing can change it.
39294df7de0SSebastien Dugue */
tick_handover_do_timer(void)39352c063d1SThomas Gleixner void tick_handover_do_timer(void)
39494df7de0SSebastien Dugue {
395f12ad423SThomas Gleixner if (tick_do_timer_cpu == smp_processor_id())
396f12ad423SThomas Gleixner tick_do_timer_cpu = cpumask_first(cpu_online_mask);
39794df7de0SSebastien Dugue }
39894df7de0SSebastien Dugue
39994df7de0SSebastien Dugue /*
400906568c9SThomas Gleixner * Shutdown an event device on a given cpu:
401906568c9SThomas Gleixner *
402906568c9SThomas Gleixner * This is called on a life CPU, when a CPU is dead. So we cannot
403906568c9SThomas Gleixner * access the hardware device itself.
404906568c9SThomas Gleixner * We just set the mode and remove it from the lists.
405906568c9SThomas Gleixner */
tick_shutdown(unsigned int cpu)406a49b116dSThomas Gleixner void tick_shutdown(unsigned int cpu)
407906568c9SThomas Gleixner {
408a49b116dSThomas Gleixner struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
409906568c9SThomas Gleixner struct clock_event_device *dev = td->evtdev;
410906568c9SThomas Gleixner
411906568c9SThomas Gleixner td->mode = TICKDEV_MODE_PERIODIC;
412906568c9SThomas Gleixner if (dev) {
413906568c9SThomas Gleixner /*
414906568c9SThomas Gleixner * Prevent that the clock events layer tries to call
415906568c9SThomas Gleixner * the set mode function!
416906568c9SThomas Gleixner */
417051ebd10SThomas Gleixner clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
418906568c9SThomas Gleixner clockevents_exchange_device(dev, NULL);
4196f7a05d7SThomas Gleixner dev->event_handler = clockevents_handle_noop;
420906568c9SThomas Gleixner td->evtdev = NULL;
421906568c9SThomas Gleixner }
422906568c9SThomas Gleixner }
423a49b116dSThomas Gleixner #endif
424906568c9SThomas Gleixner
4254ffee521SThomas Gleixner /**
426f46481d0SThomas Gleixner * tick_suspend_local - Suspend the local tick device
427f46481d0SThomas Gleixner *
428f46481d0SThomas Gleixner * Called from the local cpu for freeze with interrupts disabled.
429f46481d0SThomas Gleixner *
430f46481d0SThomas Gleixner * No locks required. Nothing can change the per cpu device.
431f46481d0SThomas Gleixner */
tick_suspend_local(void)4327270d11cSThomas Gleixner void tick_suspend_local(void)
433f46481d0SThomas Gleixner {
434f46481d0SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
435f46481d0SThomas Gleixner
436f46481d0SThomas Gleixner clockevents_shutdown(td->evtdev);
437f46481d0SThomas Gleixner }
438f46481d0SThomas Gleixner
439f46481d0SThomas Gleixner /**
440f46481d0SThomas Gleixner * tick_resume_local - Resume the local tick device
441f46481d0SThomas Gleixner *
442f46481d0SThomas Gleixner * Called from the local CPU for unfreeze or XEN resume magic.
443f46481d0SThomas Gleixner *
444f46481d0SThomas Gleixner * No locks required. Nothing can change the per cpu device.
445f46481d0SThomas Gleixner */
tick_resume_local(void)446f46481d0SThomas Gleixner void tick_resume_local(void)
447f46481d0SThomas Gleixner {
448f46481d0SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
449f46481d0SThomas Gleixner bool broadcast = tick_resume_check_broadcast();
450f46481d0SThomas Gleixner
451f46481d0SThomas Gleixner clockevents_tick_resume(td->evtdev);
452f46481d0SThomas Gleixner if (!broadcast) {
453f46481d0SThomas Gleixner if (td->mode == TICKDEV_MODE_PERIODIC)
454f46481d0SThomas Gleixner tick_setup_periodic(td->evtdev, 0);
455f46481d0SThomas Gleixner else
456f46481d0SThomas Gleixner tick_resume_oneshot();
457f46481d0SThomas Gleixner }
458a761a67fSThomas Gleixner
459a761a67fSThomas Gleixner /*
460a761a67fSThomas Gleixner * Ensure that hrtimers are up to date and the clockevents device
461a761a67fSThomas Gleixner * is reprogrammed correctly when high resolution timers are
462a761a67fSThomas Gleixner * enabled.
463a761a67fSThomas Gleixner */
464a761a67fSThomas Gleixner hrtimers_resume_local();
465f46481d0SThomas Gleixner }
466f46481d0SThomas Gleixner
467f46481d0SThomas Gleixner /**
4684ffee521SThomas Gleixner * tick_suspend - Suspend the tick and the broadcast device
4694ffee521SThomas Gleixner *
4704ffee521SThomas Gleixner * Called from syscore_suspend() via timekeeping_suspend with only one
4714ffee521SThomas Gleixner * CPU online and interrupts disabled or from tick_unfreeze() under
4724ffee521SThomas Gleixner * tick_freeze_lock.
4734ffee521SThomas Gleixner *
4744ffee521SThomas Gleixner * No locks required. Nothing can change the per cpu device.
4754ffee521SThomas Gleixner */
tick_suspend(void)4768c53daf6SThomas Gleixner void tick_suspend(void)
4776321dd60SThomas Gleixner {
478f46481d0SThomas Gleixner tick_suspend_local();
4794ffee521SThomas Gleixner tick_suspend_broadcast();
4806321dd60SThomas Gleixner }
4816321dd60SThomas Gleixner
4824ffee521SThomas Gleixner /**
4834ffee521SThomas Gleixner * tick_resume - Resume the tick and the broadcast device
4844ffee521SThomas Gleixner *
4854ffee521SThomas Gleixner * Called from syscore_resume() via timekeeping_resume with only one
486f46481d0SThomas Gleixner * CPU online and interrupts disabled.
4874ffee521SThomas Gleixner *
4884ffee521SThomas Gleixner * No locks required. Nothing can change the per cpu device.
4894ffee521SThomas Gleixner */
tick_resume(void)4908c53daf6SThomas Gleixner void tick_resume(void)
4916321dd60SThomas Gleixner {
492f46481d0SThomas Gleixner tick_resume_broadcast();
493f46481d0SThomas Gleixner tick_resume_local();
4946321dd60SThomas Gleixner }
4956321dd60SThomas Gleixner
49687e9b9f1SRafael J. Wysocki #ifdef CONFIG_SUSPEND
497124cf911SRafael J. Wysocki static DEFINE_RAW_SPINLOCK(tick_freeze_lock);
498124cf911SRafael J. Wysocki static unsigned int tick_freeze_depth;
499124cf911SRafael J. Wysocki
500124cf911SRafael J. Wysocki /**
501124cf911SRafael J. Wysocki * tick_freeze - Suspend the local tick and (possibly) timekeeping.
502124cf911SRafael J. Wysocki *
503124cf911SRafael J. Wysocki * Check if this is the last online CPU executing the function and if so,
504124cf911SRafael J. Wysocki * suspend timekeeping. Otherwise suspend the local tick.
505124cf911SRafael J. Wysocki *
506124cf911SRafael J. Wysocki * Call with interrupts disabled. Must be balanced with %tick_unfreeze().
507124cf911SRafael J. Wysocki * Interrupts must not be enabled before the subsequent %tick_unfreeze().
508124cf911SRafael J. Wysocki */
tick_freeze(void)509124cf911SRafael J. Wysocki void tick_freeze(void)
510124cf911SRafael J. Wysocki {
511124cf911SRafael J. Wysocki raw_spin_lock(&tick_freeze_lock);
512124cf911SRafael J. Wysocki
513124cf911SRafael J. Wysocki tick_freeze_depth++;
51475e0678eSRafael J. Wysocki if (tick_freeze_depth == num_online_cpus()) {
51575e0678eSRafael J. Wysocki trace_suspend_resume(TPS("timekeeping_freeze"),
51675e0678eSRafael J. Wysocki smp_processor_id(), true);
517c1a957d1SThomas Gleixner system_state = SYSTEM_SUSPEND;
5183f2552f7SChang-An Chen sched_clock_suspend();
519124cf911SRafael J. Wysocki timekeeping_suspend();
52075e0678eSRafael J. Wysocki } else {
521f46481d0SThomas Gleixner tick_suspend_local();
52275e0678eSRafael J. Wysocki }
523124cf911SRafael J. Wysocki
524124cf911SRafael J. Wysocki raw_spin_unlock(&tick_freeze_lock);
525124cf911SRafael J. Wysocki }
526124cf911SRafael J. Wysocki
527124cf911SRafael J. Wysocki /**
528124cf911SRafael J. Wysocki * tick_unfreeze - Resume the local tick and (possibly) timekeeping.
529124cf911SRafael J. Wysocki *
530124cf911SRafael J. Wysocki * Check if this is the first CPU executing the function and if so, resume
531124cf911SRafael J. Wysocki * timekeeping. Otherwise resume the local tick.
532124cf911SRafael J. Wysocki *
533124cf911SRafael J. Wysocki * Call with interrupts disabled. Must be balanced with %tick_freeze().
534124cf911SRafael J. Wysocki * Interrupts must not be enabled after the preceding %tick_freeze().
535124cf911SRafael J. Wysocki */
tick_unfreeze(void)536124cf911SRafael J. Wysocki void tick_unfreeze(void)
537124cf911SRafael J. Wysocki {
538124cf911SRafael J. Wysocki raw_spin_lock(&tick_freeze_lock);
539124cf911SRafael J. Wysocki
54075e0678eSRafael J. Wysocki if (tick_freeze_depth == num_online_cpus()) {
541124cf911SRafael J. Wysocki timekeeping_resume();
5423f2552f7SChang-An Chen sched_clock_resume();
543c1a957d1SThomas Gleixner system_state = SYSTEM_RUNNING;
54475e0678eSRafael J. Wysocki trace_suspend_resume(TPS("timekeeping_freeze"),
54575e0678eSRafael J. Wysocki smp_processor_id(), false);
54675e0678eSRafael J. Wysocki } else {
5475167c506SChunyan Zhang touch_softlockup_watchdog();
548422fe750SRafael J. Wysocki tick_resume_local();
54975e0678eSRafael J. Wysocki }
550124cf911SRafael J. Wysocki
551124cf911SRafael J. Wysocki tick_freeze_depth--;
552124cf911SRafael J. Wysocki
553124cf911SRafael J. Wysocki raw_spin_unlock(&tick_freeze_lock);
554124cf911SRafael J. Wysocki }
55587e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */
556124cf911SRafael J. Wysocki
557906568c9SThomas Gleixner /**
558906568c9SThomas Gleixner * tick_init - initialize the tick control
559906568c9SThomas Gleixner */
tick_init(void)560906568c9SThomas Gleixner void __init tick_init(void)
561906568c9SThomas Gleixner {
562b352bc1cSThomas Gleixner tick_broadcast_init();
563a80e49e2SFrederic Weisbecker tick_nohz_init();
564906568c9SThomas Gleixner }
565