xref: /linux-6.15/kernel/time/tick-common.c (revision 92e250c6)
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 <[email protected]>
7906568c9SThomas Gleixner  * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
8906568c9SThomas Gleixner  * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
9906568c9SThomas Gleixner  */
10f87cbcb3SThomas Gleixner #include <linux/compiler.h>
11906568c9SThomas Gleixner #include <linux/cpu.h>
12906568c9SThomas Gleixner #include <linux/err.h>
13906568c9SThomas Gleixner #include <linux/hrtimer.h>
14d7b90689SRussell King #include <linux/interrupt.h>
155167c506SChunyan Zhang #include <linux/nmi.h>
16906568c9SThomas Gleixner #include <linux/percpu.h>
17906568c9SThomas Gleixner #include <linux/profile.h>
18906568c9SThomas Gleixner #include <linux/sched.h>
19ccf33d68SThomas Gleixner #include <linux/module.h>
2075e0678eSRafael J. Wysocki #include <trace/events/power.h>
21906568c9SThomas Gleixner 
22d7b90689SRussell King #include <asm/irq_regs.h>
23d7b90689SRussell King 
24f8381cbaSThomas Gleixner #include "tick-internal.h"
25f8381cbaSThomas Gleixner 
26906568c9SThomas Gleixner /*
27906568c9SThomas Gleixner  * Tick devices
28906568c9SThomas Gleixner  */
29f8381cbaSThomas Gleixner DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
30906568c9SThomas Gleixner /*
31c398960cSThomas Gleixner  * Tick next event: keeps track of the tick time. It's updated by the
32c398960cSThomas Gleixner  * CPU which handles the tick and protected by jiffies_lock. There is
33c398960cSThomas Gleixner  * no requirement to write hold the jiffies seqcount for it.
34906568c9SThomas Gleixner  */
35f8381cbaSThomas Gleixner ktime_t tick_next_period;
36050ded1bSAndrew Morton 
37050ded1bSAndrew Morton /*
38050ded1bSAndrew Morton  * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR
39050ded1bSAndrew Morton  * which is responsible for calling do_timer(), i.e. the timekeeping stuff. This
40050ded1bSAndrew Morton  * variable has two functions:
41050ded1bSAndrew Morton  *
42050ded1bSAndrew Morton  * 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the
43050ded1bSAndrew Morton  *    timekeeping lock all at once. Only the CPU which is assigned to do the
44050ded1bSAndrew Morton  *    update is handling it.
45050ded1bSAndrew Morton  *
46050ded1bSAndrew Morton  * 2) Hand off the duty in the NOHZ idle case by setting the value to
47050ded1bSAndrew Morton  *    TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks
48050ded1bSAndrew Morton  *    at it will take over and keep the time keeping alive.  The handover
49050ded1bSAndrew Morton  *    procedure also covers cpu hotplug.
50050ded1bSAndrew Morton  */
516441402bSThomas Gleixner int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT;
5208ae95f4SNicholas Piggin #ifdef CONFIG_NO_HZ_FULL
5308ae95f4SNicholas Piggin /*
5408ae95f4SNicholas Piggin  * tick_do_timer_boot_cpu indicates the boot CPU temporarily owns
5508ae95f4SNicholas Piggin  * tick_do_timer_cpu and it should be taken over by an eligible secondary
5608ae95f4SNicholas Piggin  * when one comes online.
5708ae95f4SNicholas Piggin  */
5808ae95f4SNicholas Piggin static int tick_do_timer_boot_cpu __read_mostly = -1;
5908ae95f4SNicholas Piggin #endif
60906568c9SThomas Gleixner 
61289f480aSIngo Molnar /*
62289f480aSIngo Molnar  * Debugging: see timer_list.c
63289f480aSIngo Molnar  */
tick_get_device(int cpu)64289f480aSIngo Molnar struct tick_device *tick_get_device(int cpu)
65289f480aSIngo Molnar {
66289f480aSIngo Molnar 	return &per_cpu(tick_cpu_device, cpu);
67289f480aSIngo Molnar }
68289f480aSIngo Molnar 
6979bf2bb3SThomas Gleixner /**
7079bf2bb3SThomas Gleixner  * tick_is_oneshot_available - check for a oneshot capable event device
7179bf2bb3SThomas Gleixner  */
tick_is_oneshot_available(void)7279bf2bb3SThomas Gleixner int tick_is_oneshot_available(void)
7379bf2bb3SThomas Gleixner {
74909ea964SChristoph Lameter 	struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
7579bf2bb3SThomas Gleixner 
763a142a06SThomas Gleixner 	if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
773a142a06SThomas Gleixner 		return 0;
783a142a06SThomas Gleixner 	if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
793a142a06SThomas Gleixner 		return 1;
803a142a06SThomas Gleixner 	return tick_broadcast_oneshot_available();
8179bf2bb3SThomas Gleixner }
8279bf2bb3SThomas Gleixner 
83906568c9SThomas Gleixner /*
84906568c9SThomas Gleixner  * Periodic tick
85906568c9SThomas Gleixner  */
tick_periodic(int cpu)86906568c9SThomas Gleixner static void tick_periodic(int cpu)
87906568c9SThomas Gleixner {
88f87cbcb3SThomas Gleixner 	if (READ_ONCE(tick_do_timer_cpu) == cpu) {
89e5d4d175SThomas Gleixner 		raw_spin_lock(&jiffies_lock);
90e5d4d175SThomas Gleixner 		write_seqcount_begin(&jiffies_seq);
91906568c9SThomas Gleixner 
92906568c9SThomas Gleixner 		/* Keep track of the next tick event */
93b9965449SThomas Gleixner 		tick_next_period = ktime_add_ns(tick_next_period, TICK_NSEC);
94906568c9SThomas Gleixner 
95906568c9SThomas Gleixner 		do_timer(1);
96e5d4d175SThomas Gleixner 		write_seqcount_end(&jiffies_seq);
97e5d4d175SThomas Gleixner 		raw_spin_unlock(&jiffies_lock);
9847a1b796SJohn Stultz 		update_wall_time();
99906568c9SThomas Gleixner 	}
100906568c9SThomas Gleixner 
101906568c9SThomas Gleixner 	update_process_times(user_mode(get_irq_regs()));
102906568c9SThomas Gleixner 	profile_tick(CPU_PROFILING);
103906568c9SThomas Gleixner }
104906568c9SThomas Gleixner 
105906568c9SThomas Gleixner /*
106906568c9SThomas Gleixner  * Event handler for periodic ticks
107906568c9SThomas Gleixner  */
tick_handle_periodic(struct clock_event_device * dev)108906568c9SThomas Gleixner void tick_handle_periodic(struct clock_event_device *dev)
109906568c9SThomas Gleixner {
110906568c9SThomas Gleixner 	int cpu = smp_processor_id();
111b97f0291SViresh Kumar 	ktime_t next = dev->next_event;
112906568c9SThomas Gleixner 
113906568c9SThomas Gleixner 	tick_periodic(cpu);
114906568c9SThomas Gleixner 
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 	 */
12027dc0809SFrederic Weisbecker 	if (IS_ENABLED(CONFIG_TICK_ONESHOT) && dev->event_handler != tick_handle_periodic)
121c6eb3f70SThomas Gleixner 		return;
122c6eb3f70SThomas Gleixner 
123472c4a94SViresh Kumar 	if (!clockevent_state_oneshot(dev))
124906568c9SThomas Gleixner 		return;
125b97f0291SViresh Kumar 	for (;;) {
126906568c9SThomas Gleixner 		/*
127906568c9SThomas Gleixner 		 * Setup the next period for devices, which do not have
128906568c9SThomas Gleixner 		 * periodic mode:
129906568c9SThomas Gleixner 		 */
130b9965449SThomas Gleixner 		next = ktime_add_ns(next, TICK_NSEC);
131b97f0291SViresh Kumar 
132d1748302SMartin Schwidefsky 		if (!clockevents_program_event(dev, next, false))
133906568c9SThomas Gleixner 			return;
13474a03b69Sjohn stultz 		/*
13574a03b69Sjohn stultz 		 * Have to be careful here. If we're in oneshot mode,
13674a03b69Sjohn stultz 		 * before we call tick_periodic() in a loop, we need
13774a03b69Sjohn stultz 		 * to be sure we're using a real hardware clocksource.
13874a03b69Sjohn stultz 		 * Otherwise we could get trapped in an infinite
13974a03b69Sjohn stultz 		 * loop, as the tick_periodic() increments jiffies,
140cacb3c76SViresh Kumar 		 * which then will increment time, possibly causing
14174a03b69Sjohn stultz 		 * the loop to trigger again and again.
14274a03b69Sjohn stultz 		 */
14374a03b69Sjohn stultz 		if (timekeeping_valid_for_hres())
144906568c9SThomas Gleixner 			tick_periodic(cpu);
145906568c9SThomas Gleixner 	}
146906568c9SThomas Gleixner }
147906568c9SThomas Gleixner 
148906568c9SThomas Gleixner /*
149906568c9SThomas Gleixner  * Setup the device for a periodic tick
150906568c9SThomas Gleixner  */
tick_setup_periodic(struct clock_event_device * dev,int broadcast)151f8381cbaSThomas Gleixner void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
152906568c9SThomas Gleixner {
153f8381cbaSThomas Gleixner 	tick_set_periodic_handler(dev, broadcast);
154f8381cbaSThomas Gleixner 
155f8381cbaSThomas Gleixner 	/* Broadcast setup ? */
156f8381cbaSThomas Gleixner 	if (!tick_device_is_functional(dev))
157f8381cbaSThomas Gleixner 		return;
158906568c9SThomas Gleixner 
15927ce4cb4SThomas Gleixner 	if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
16027ce4cb4SThomas Gleixner 	    !tick_broadcast_oneshot_active()) {
161d7eb231cSThomas Gleixner 		clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
162906568c9SThomas Gleixner 	} else {
163e1e41b6cSRasmus Villemoes 		unsigned int seq;
164906568c9SThomas Gleixner 		ktime_t next;
165906568c9SThomas Gleixner 
166906568c9SThomas Gleixner 		do {
167e5d4d175SThomas Gleixner 			seq = read_seqcount_begin(&jiffies_seq);
168906568c9SThomas Gleixner 			next = tick_next_period;
169e5d4d175SThomas Gleixner 		} while (read_seqcount_retry(&jiffies_seq, seq));
170906568c9SThomas Gleixner 
171d7eb231cSThomas Gleixner 		clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
172906568c9SThomas Gleixner 
173906568c9SThomas Gleixner 		for (;;) {
174d1748302SMartin Schwidefsky 			if (!clockevents_program_event(dev, next, false))
175906568c9SThomas Gleixner 				return;
176b9965449SThomas Gleixner 			next = ktime_add_ns(next, TICK_NSEC);
177906568c9SThomas Gleixner 		}
178906568c9SThomas Gleixner 	}
179906568c9SThomas Gleixner }
180906568c9SThomas Gleixner 
181906568c9SThomas Gleixner /*
182906568c9SThomas Gleixner  * Setup the tick device
183906568c9SThomas Gleixner  */
tick_setup_device(struct tick_device * td,struct clock_event_device * newdev,int cpu,const struct cpumask * cpumask)184906568c9SThomas Gleixner static void tick_setup_device(struct tick_device *td,
185906568c9SThomas Gleixner 			      struct clock_event_device *newdev, int cpu,
1860de26520SRusty Russell 			      const struct cpumask *cpumask)
187906568c9SThomas Gleixner {
188906568c9SThomas Gleixner 	void (*handler)(struct clock_event_device *) = NULL;
1898b0e1953SThomas Gleixner 	ktime_t next_event = 0;
190906568c9SThomas Gleixner 
191906568c9SThomas Gleixner 	/*
192906568c9SThomas Gleixner 	 * First device setup ?
193906568c9SThomas Gleixner 	 */
194906568c9SThomas Gleixner 	if (!td->evtdev) {
195906568c9SThomas Gleixner 		/*
196906568c9SThomas Gleixner 		 * If no cpu took the do_timer update, assign it to
197906568c9SThomas Gleixner 		 * this cpu:
198906568c9SThomas Gleixner 		 */
199f87cbcb3SThomas Gleixner 		if (READ_ONCE(tick_do_timer_cpu) == TICK_DO_TIMER_BOOT) {
200f87cbcb3SThomas Gleixner 			WRITE_ONCE(tick_do_timer_cpu, cpu);
20113bb06f8SThomas Gleixner 			tick_next_period = ktime_get();
20208ae95f4SNicholas Piggin #ifdef CONFIG_NO_HZ_FULL
20308ae95f4SNicholas Piggin 			/*
20407c54cc5SOleg Nesterov 			 * The boot CPU may be nohz_full, in which case the
20507c54cc5SOleg Nesterov 			 * first housekeeping secondary will take do_timer()
20607c54cc5SOleg Nesterov 			 * from it.
20708ae95f4SNicholas Piggin 			 */
20808ae95f4SNicholas Piggin 			if (tick_nohz_full_cpu(cpu))
20908ae95f4SNicholas Piggin 				tick_do_timer_boot_cpu = cpu;
21008ae95f4SNicholas Piggin 
21107c54cc5SOleg Nesterov 		} else if (tick_do_timer_boot_cpu != -1 && !tick_nohz_full_cpu(cpu)) {
21208ae95f4SNicholas Piggin 			tick_do_timer_boot_cpu = -1;
21307c54cc5SOleg Nesterov 			/*
21407c54cc5SOleg Nesterov 			 * The boot CPU will stay in periodic (NOHZ disabled)
21507c54cc5SOleg Nesterov 			 * mode until clocksource_done_booting() called after
21607c54cc5SOleg Nesterov 			 * smp_init() selects a high resolution clocksource and
21707c54cc5SOleg Nesterov 			 * timekeeping_notify() kicks the NOHZ stuff alive.
21807c54cc5SOleg Nesterov 			 *
21907c54cc5SOleg Nesterov 			 * So this WRITE_ONCE can only race with the READ_ONCE
22007c54cc5SOleg Nesterov 			 * check in tick_periodic() but this race is harmless.
22107c54cc5SOleg Nesterov 			 */
22207c54cc5SOleg Nesterov 			WRITE_ONCE(tick_do_timer_cpu, cpu);
22308ae95f4SNicholas Piggin #endif
224906568c9SThomas Gleixner 		}
225906568c9SThomas Gleixner 
226906568c9SThomas Gleixner 		/*
227906568c9SThomas Gleixner 		 * Startup in periodic mode first.
228906568c9SThomas Gleixner 		 */
229906568c9SThomas Gleixner 		td->mode = TICKDEV_MODE_PERIODIC;
230906568c9SThomas Gleixner 	} else {
231906568c9SThomas Gleixner 		handler = td->evtdev->event_handler;
232906568c9SThomas Gleixner 		next_event = td->evtdev->next_event;
2337c1e7689SVenkatesh Pallipadi 		td->evtdev->event_handler = clockevents_handle_noop;
234906568c9SThomas Gleixner 	}
235906568c9SThomas Gleixner 
236906568c9SThomas Gleixner 	td->evtdev = newdev;
237906568c9SThomas Gleixner 
238906568c9SThomas Gleixner 	/*
239906568c9SThomas Gleixner 	 * When the device is not per cpu, pin the interrupt to the
240906568c9SThomas Gleixner 	 * current cpu:
241906568c9SThomas Gleixner 	 */
242320ab2b0SRusty Russell 	if (!cpumask_equal(newdev->cpumask, cpumask))
2430de26520SRusty Russell 		irq_set_affinity(newdev->irq, cpumask);
244906568c9SThomas Gleixner 
245f8381cbaSThomas Gleixner 	/*
246f8381cbaSThomas Gleixner 	 * When global broadcasting is active, check if the current
247f8381cbaSThomas Gleixner 	 * device is registered as a placeholder for broadcast mode.
248f8381cbaSThomas Gleixner 	 * This allows us to handle this x86 misfeature in a generic
24907bd1172SThomas Gleixner 	 * way. This function also returns !=0 when we keep the
25007bd1172SThomas Gleixner 	 * current active broadcast state for this CPU.
251f8381cbaSThomas Gleixner 	 */
252f8381cbaSThomas Gleixner 	if (tick_device_uses_broadcast(newdev, cpu))
253f8381cbaSThomas Gleixner 		return;
254f8381cbaSThomas Gleixner 
255906568c9SThomas Gleixner 	if (td->mode == TICKDEV_MODE_PERIODIC)
256906568c9SThomas Gleixner 		tick_setup_periodic(newdev, 0);
25779bf2bb3SThomas Gleixner 	else
25879bf2bb3SThomas Gleixner 		tick_setup_oneshot(newdev, handler, next_event);
259906568c9SThomas Gleixner }
260906568c9SThomas Gleixner 
tick_install_replacement(struct clock_event_device * newdev)26103e13cf5SThomas Gleixner void tick_install_replacement(struct clock_event_device *newdev)
26203e13cf5SThomas Gleixner {
26322127e93SChristoph Lameter 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
26403e13cf5SThomas Gleixner 	int cpu = smp_processor_id();
26503e13cf5SThomas Gleixner 
26603e13cf5SThomas Gleixner 	clockevents_exchange_device(td->evtdev, newdev);
26703e13cf5SThomas Gleixner 	tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
26803e13cf5SThomas Gleixner 	if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
26903e13cf5SThomas Gleixner 		tick_oneshot_notify();
27003e13cf5SThomas Gleixner }
27103e13cf5SThomas Gleixner 
tick_check_percpu(struct clock_event_device * curdev,struct clock_event_device * newdev,int cpu)27245cb8e01SThomas Gleixner static bool tick_check_percpu(struct clock_event_device *curdev,
27345cb8e01SThomas Gleixner 			      struct clock_event_device *newdev, int cpu)
27445cb8e01SThomas Gleixner {
27545cb8e01SThomas Gleixner 	if (!cpumask_test_cpu(cpu, newdev->cpumask))
27645cb8e01SThomas Gleixner 		return false;
27745cb8e01SThomas Gleixner 	if (cpumask_equal(newdev->cpumask, cpumask_of(cpu)))
27845cb8e01SThomas Gleixner 		return true;
27945cb8e01SThomas Gleixner 	/* Check if irq affinity can be set */
28045cb8e01SThomas Gleixner 	if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq))
28145cb8e01SThomas Gleixner 		return false;
28245cb8e01SThomas Gleixner 	/* Prefer an existing cpu local device */
28345cb8e01SThomas Gleixner 	if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
28445cb8e01SThomas Gleixner 		return false;
28545cb8e01SThomas Gleixner 	return true;
28645cb8e01SThomas Gleixner }
28745cb8e01SThomas Gleixner 
tick_check_preferred(struct clock_event_device * curdev,struct clock_event_device * newdev)28845cb8e01SThomas Gleixner static bool tick_check_preferred(struct clock_event_device *curdev,
28945cb8e01SThomas Gleixner 				 struct clock_event_device *newdev)
29045cb8e01SThomas Gleixner {
29145cb8e01SThomas Gleixner 	/* Prefer oneshot capable device */
29245cb8e01SThomas Gleixner 	if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) {
29345cb8e01SThomas Gleixner 		if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT))
29445cb8e01SThomas Gleixner 			return false;
29545cb8e01SThomas Gleixner 		if (tick_oneshot_mode_active())
29645cb8e01SThomas Gleixner 			return false;
29745cb8e01SThomas Gleixner 	}
29845cb8e01SThomas Gleixner 
29970e5975dSStephen Boyd 	/*
30070e5975dSStephen Boyd 	 * Use the higher rated one, but prefer a CPU local device with a lower
30170e5975dSStephen Boyd 	 * rating than a non-CPU local device
30270e5975dSStephen Boyd 	 */
30370e5975dSStephen Boyd 	return !curdev ||
30470e5975dSStephen Boyd 		newdev->rating > curdev->rating ||
3055b5ccbc2SSudeep Holla 	       !cpumask_equal(curdev->cpumask, newdev->cpumask);
30645cb8e01SThomas Gleixner }
30745cb8e01SThomas Gleixner 
308906568c9SThomas Gleixner /*
30903e13cf5SThomas Gleixner  * Check whether the new device is a better fit than curdev. curdev
31003e13cf5SThomas Gleixner  * can be NULL !
31103e13cf5SThomas Gleixner  */
tick_check_replacement(struct clock_event_device * curdev,struct clock_event_device * newdev)31203e13cf5SThomas Gleixner bool tick_check_replacement(struct clock_event_device *curdev,
31303e13cf5SThomas Gleixner 			    struct clock_event_device *newdev)
31403e13cf5SThomas Gleixner {
315521c4299SViresh Kumar 	if (!tick_check_percpu(curdev, newdev, smp_processor_id()))
31603e13cf5SThomas Gleixner 		return false;
31703e13cf5SThomas Gleixner 
31803e13cf5SThomas Gleixner 	return tick_check_preferred(curdev, newdev);
31903e13cf5SThomas Gleixner }
32003e13cf5SThomas Gleixner 
32103e13cf5SThomas Gleixner /*
3227126cac4SThomas Gleixner  * Check, if the new registered device should be used. Called with
3237126cac4SThomas Gleixner  * clockevents_lock held and interrupts disabled.
324906568c9SThomas Gleixner  */
tick_check_new_device(struct clock_event_device * newdev)3257172a286SThomas Gleixner void tick_check_new_device(struct clock_event_device *newdev)
326906568c9SThomas Gleixner {
327906568c9SThomas Gleixner 	struct clock_event_device *curdev;
328906568c9SThomas Gleixner 	struct tick_device *td;
3297172a286SThomas Gleixner 	int cpu;
330906568c9SThomas Gleixner 
331906568c9SThomas Gleixner 	cpu = smp_processor_id();
332906568c9SThomas Gleixner 	td = &per_cpu(tick_cpu_device, cpu);
333906568c9SThomas Gleixner 	curdev = td->evtdev;
334906568c9SThomas Gleixner 
335d7840aaaSWang Wensheng 	if (!tick_check_replacement(curdev, newdev))
336906568c9SThomas Gleixner 		goto out_bc;
337906568c9SThomas Gleixner 
338ccf33d68SThomas Gleixner 	if (!try_module_get(newdev->owner))
339ccf33d68SThomas Gleixner 		return;
340ccf33d68SThomas Gleixner 
341906568c9SThomas Gleixner 	/*
342906568c9SThomas Gleixner 	 * Replace the eventually existing device by the new
343f8381cbaSThomas Gleixner 	 * device. If the current device is the broadcast device, do
344f8381cbaSThomas Gleixner 	 * not give it back to the clockevents layer !
345906568c9SThomas Gleixner 	 */
346f8381cbaSThomas Gleixner 	if (tick_is_broadcast_device(curdev)) {
3472344abbcSThomas Gleixner 		clockevents_shutdown(curdev);
348f8381cbaSThomas Gleixner 		curdev = NULL;
349f8381cbaSThomas Gleixner 	}
350906568c9SThomas Gleixner 	clockevents_exchange_device(curdev, newdev);
3516b954823SRusty Russell 	tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
35279bf2bb3SThomas Gleixner 	if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
35379bf2bb3SThomas Gleixner 		tick_oneshot_notify();
3547172a286SThomas Gleixner 	return;
355f8381cbaSThomas Gleixner 
356f8381cbaSThomas Gleixner out_bc:
357f8381cbaSThomas Gleixner 	/*
358f8381cbaSThomas Gleixner 	 * Can the new device be used as a broadcast device ?
359f8381cbaSThomas Gleixner 	 */
360c94a8537SWill Deacon 	tick_install_broadcast_device(newdev, cpu);
361906568c9SThomas Gleixner }
362906568c9SThomas Gleixner 
363f32dd117SThomas Gleixner /**
364f32dd117SThomas Gleixner  * tick_broadcast_oneshot_control - Enter/exit broadcast oneshot mode
365f32dd117SThomas Gleixner  * @state:	The target state (enter/exit)
366f32dd117SThomas Gleixner  *
367f32dd117SThomas Gleixner  * The system enters/leaves a state, where affected devices might stop
368f32dd117SThomas Gleixner  * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups.
369f32dd117SThomas Gleixner  *
370f32dd117SThomas Gleixner  * Called with interrupts disabled, so clockevents_lock is not
371f32dd117SThomas Gleixner  * required here because the local clock event device cannot go away
372f32dd117SThomas Gleixner  * under us.
373f32dd117SThomas Gleixner  */
tick_broadcast_oneshot_control(enum tick_broadcast_state state)374f32dd117SThomas Gleixner int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
375f32dd117SThomas Gleixner {
376f32dd117SThomas Gleixner 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
377f32dd117SThomas Gleixner 
378f32dd117SThomas Gleixner 	if (!(td->evtdev->features & CLOCK_EVT_FEAT_C3STOP))
379f32dd117SThomas Gleixner 		return 0;
380f32dd117SThomas Gleixner 
381f32dd117SThomas Gleixner 	return __tick_broadcast_oneshot_control(state);
382f32dd117SThomas Gleixner }
3830f447051SThomas Gleixner EXPORT_SYMBOL_GPL(tick_broadcast_oneshot_control);
384f32dd117SThomas Gleixner 
38552c063d1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
tick_assert_timekeeping_handover(void)386500f8f9bSFrederic Weisbecker void tick_assert_timekeeping_handover(void)
387500f8f9bSFrederic Weisbecker {
388500f8f9bSFrederic Weisbecker 	WARN_ON_ONCE(tick_do_timer_cpu == smp_processor_id());
389500f8f9bSFrederic Weisbecker }
390906568c9SThomas Gleixner /*
3913ad6eb06SFrederic Weisbecker  * Stop the tick and transfer the timekeeping job away from a dying cpu.
39294df7de0SSebastien Dugue  */
tick_cpu_dying(unsigned int dying_cpu)3933ad6eb06SFrederic Weisbecker int tick_cpu_dying(unsigned int dying_cpu)
39494df7de0SSebastien Dugue {
3953ad6eb06SFrederic Weisbecker 	/*
396f87cbcb3SThomas Gleixner 	 * If the current CPU is the timekeeper, it's the only one that can
397f87cbcb3SThomas Gleixner 	 * safely hand over its duty. Also all online CPUs are in stop
398f87cbcb3SThomas Gleixner 	 * machine, guaranteed not to be idle, therefore there is no
399f87cbcb3SThomas Gleixner 	 * concurrency and it's safe to pick any online successor.
4003ad6eb06SFrederic Weisbecker 	 */
4013ad6eb06SFrederic Weisbecker 	if (tick_do_timer_cpu == dying_cpu)
402f12ad423SThomas Gleixner 		tick_do_timer_cpu = cpumask_first(cpu_online_mask);
4033ad6eb06SFrederic Weisbecker 
4043f69d04eSFrederic Weisbecker 	/* Make sure the CPU won't try to retake the timekeeping duty */
4053f69d04eSFrederic Weisbecker 	tick_sched_timer_dying(dying_cpu);
406f04e5122SFrederic Weisbecker 
407ef8969bbSFrederic Weisbecker 	/* Remove CPU from timer broadcasting */
408ef8969bbSFrederic Weisbecker 	tick_offline_cpu(dying_cpu);
409ef8969bbSFrederic Weisbecker 
4103ad6eb06SFrederic Weisbecker 	return 0;
41194df7de0SSebastien Dugue }
41294df7de0SSebastien Dugue 
41394df7de0SSebastien Dugue /*
414906568c9SThomas Gleixner  * Shutdown an event device on a given cpu:
415906568c9SThomas Gleixner  *
416906568c9SThomas Gleixner  * This is called on a life CPU, when a CPU is dead. So we cannot
417906568c9SThomas Gleixner  * access the hardware device itself.
418906568c9SThomas Gleixner  * We just set the mode and remove it from the lists.
419906568c9SThomas Gleixner  */
tick_shutdown(unsigned int cpu)420a49b116dSThomas Gleixner void tick_shutdown(unsigned int cpu)
421906568c9SThomas Gleixner {
422a49b116dSThomas Gleixner 	struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
423906568c9SThomas Gleixner 	struct clock_event_device *dev = td->evtdev;
424906568c9SThomas Gleixner 
425906568c9SThomas Gleixner 	td->mode = TICKDEV_MODE_PERIODIC;
426906568c9SThomas Gleixner 	if (dev) {
427906568c9SThomas Gleixner 		/*
428906568c9SThomas Gleixner 		 * Prevent that the clock events layer tries to call
429906568c9SThomas Gleixner 		 * the set mode function!
430906568c9SThomas Gleixner 		 */
431051ebd10SThomas Gleixner 		clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
432906568c9SThomas Gleixner 		clockevents_exchange_device(dev, NULL);
4336f7a05d7SThomas Gleixner 		dev->event_handler = clockevents_handle_noop;
434906568c9SThomas Gleixner 		td->evtdev = NULL;
435906568c9SThomas Gleixner 	}
436906568c9SThomas Gleixner }
437a49b116dSThomas Gleixner #endif
438906568c9SThomas Gleixner 
4394ffee521SThomas Gleixner /**
440f46481d0SThomas Gleixner  * tick_suspend_local - Suspend the local tick device
441f46481d0SThomas Gleixner  *
442f46481d0SThomas Gleixner  * Called from the local cpu for freeze with interrupts disabled.
443f46481d0SThomas Gleixner  *
444f46481d0SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
445f46481d0SThomas Gleixner  */
tick_suspend_local(void)4467270d11cSThomas Gleixner void tick_suspend_local(void)
447f46481d0SThomas Gleixner {
448f46481d0SThomas Gleixner 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
449f46481d0SThomas Gleixner 
450f46481d0SThomas Gleixner 	clockevents_shutdown(td->evtdev);
451f46481d0SThomas Gleixner }
452f46481d0SThomas Gleixner 
453f46481d0SThomas Gleixner /**
454f46481d0SThomas Gleixner  * tick_resume_local - Resume the local tick device
455f46481d0SThomas Gleixner  *
456f46481d0SThomas Gleixner  * Called from the local CPU for unfreeze or XEN resume magic.
457f46481d0SThomas Gleixner  *
458f46481d0SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
459f46481d0SThomas Gleixner  */
tick_resume_local(void)460f46481d0SThomas Gleixner void tick_resume_local(void)
461f46481d0SThomas Gleixner {
462f46481d0SThomas Gleixner 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
463f46481d0SThomas Gleixner 	bool broadcast = tick_resume_check_broadcast();
464f46481d0SThomas Gleixner 
465f46481d0SThomas Gleixner 	clockevents_tick_resume(td->evtdev);
466f46481d0SThomas Gleixner 	if (!broadcast) {
467f46481d0SThomas Gleixner 		if (td->mode == TICKDEV_MODE_PERIODIC)
468f46481d0SThomas Gleixner 			tick_setup_periodic(td->evtdev, 0);
469f46481d0SThomas Gleixner 		else
470f46481d0SThomas Gleixner 			tick_resume_oneshot();
471f46481d0SThomas Gleixner 	}
472a761a67fSThomas Gleixner 
473a761a67fSThomas Gleixner 	/*
474a761a67fSThomas Gleixner 	 * Ensure that hrtimers are up to date and the clockevents device
475a761a67fSThomas Gleixner 	 * is reprogrammed correctly when high resolution timers are
476a761a67fSThomas Gleixner 	 * enabled.
477a761a67fSThomas Gleixner 	 */
478a761a67fSThomas Gleixner 	hrtimers_resume_local();
479f46481d0SThomas Gleixner }
480f46481d0SThomas Gleixner 
481f46481d0SThomas Gleixner /**
4824ffee521SThomas Gleixner  * tick_suspend - Suspend the tick and the broadcast device
4834ffee521SThomas Gleixner  *
4844ffee521SThomas Gleixner  * Called from syscore_suspend() via timekeeping_suspend with only one
4854ffee521SThomas Gleixner  * CPU online and interrupts disabled or from tick_unfreeze() under
4864ffee521SThomas Gleixner  * tick_freeze_lock.
4874ffee521SThomas Gleixner  *
4884ffee521SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
4894ffee521SThomas Gleixner  */
tick_suspend(void)4908c53daf6SThomas Gleixner void tick_suspend(void)
4916321dd60SThomas Gleixner {
492f46481d0SThomas Gleixner 	tick_suspend_local();
4934ffee521SThomas Gleixner 	tick_suspend_broadcast();
4946321dd60SThomas Gleixner }
4956321dd60SThomas Gleixner 
4964ffee521SThomas Gleixner /**
4974ffee521SThomas Gleixner  * tick_resume - Resume the tick and the broadcast device
4984ffee521SThomas Gleixner  *
4994ffee521SThomas Gleixner  * Called from syscore_resume() via timekeeping_resume with only one
500f46481d0SThomas Gleixner  * CPU online and interrupts disabled.
5014ffee521SThomas Gleixner  *
5024ffee521SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
5034ffee521SThomas Gleixner  */
tick_resume(void)5048c53daf6SThomas Gleixner void tick_resume(void)
5056321dd60SThomas Gleixner {
506f46481d0SThomas Gleixner 	tick_resume_broadcast();
507f46481d0SThomas Gleixner 	tick_resume_local();
5086321dd60SThomas Gleixner }
5096321dd60SThomas Gleixner 
51087e9b9f1SRafael J. Wysocki #ifdef CONFIG_SUSPEND
511124cf911SRafael J. Wysocki static DEFINE_RAW_SPINLOCK(tick_freeze_lock);
512*92e250c6SSebastian Andrzej Siewior static DEFINE_WAIT_OVERRIDE_MAP(tick_freeze_map, LD_WAIT_SLEEP);
513124cf911SRafael J. Wysocki static unsigned int tick_freeze_depth;
514124cf911SRafael J. Wysocki 
515124cf911SRafael J. Wysocki /**
516124cf911SRafael J. Wysocki  * tick_freeze - Suspend the local tick and (possibly) timekeeping.
517124cf911SRafael J. Wysocki  *
518124cf911SRafael J. Wysocki  * Check if this is the last online CPU executing the function and if so,
519124cf911SRafael J. Wysocki  * suspend timekeeping.  Otherwise suspend the local tick.
520124cf911SRafael J. Wysocki  *
521124cf911SRafael J. Wysocki  * Call with interrupts disabled.  Must be balanced with %tick_unfreeze().
522124cf911SRafael J. Wysocki  * Interrupts must not be enabled before the subsequent %tick_unfreeze().
523124cf911SRafael J. Wysocki  */
tick_freeze(void)524124cf911SRafael J. Wysocki void tick_freeze(void)
525124cf911SRafael J. Wysocki {
526124cf911SRafael J. Wysocki 	raw_spin_lock(&tick_freeze_lock);
527124cf911SRafael J. Wysocki 
528124cf911SRafael J. Wysocki 	tick_freeze_depth++;
52975e0678eSRafael J. Wysocki 	if (tick_freeze_depth == num_online_cpus()) {
53075e0678eSRafael J. Wysocki 		trace_suspend_resume(TPS("timekeeping_freeze"),
53175e0678eSRafael J. Wysocki 				     smp_processor_id(), true);
532*92e250c6SSebastian Andrzej Siewior 		/*
533*92e250c6SSebastian Andrzej Siewior 		 * All other CPUs have their interrupts disabled and are
534*92e250c6SSebastian Andrzej Siewior 		 * suspended to idle. Other tasks have been frozen so there
535*92e250c6SSebastian Andrzej Siewior 		 * is no scheduling happening. This means that there is no
536*92e250c6SSebastian Andrzej Siewior 		 * concurrency in the system at this point. Therefore it is
537*92e250c6SSebastian Andrzej Siewior 		 * okay to acquire a sleeping lock on PREEMPT_RT, such as a
538*92e250c6SSebastian Andrzej Siewior 		 * spinlock, because the lock cannot be held by other CPUs
539*92e250c6SSebastian Andrzej Siewior 		 * or threads and acquiring it cannot block.
540*92e250c6SSebastian Andrzej Siewior 		 *
541*92e250c6SSebastian Andrzej Siewior 		 * Inform lockdep about the situation.
542*92e250c6SSebastian Andrzej Siewior 		 */
543*92e250c6SSebastian Andrzej Siewior 		lock_map_acquire_try(&tick_freeze_map);
544c1a957d1SThomas Gleixner 		system_state = SYSTEM_SUSPEND;
5453f2552f7SChang-An Chen 		sched_clock_suspend();
546124cf911SRafael J. Wysocki 		timekeeping_suspend();
547*92e250c6SSebastian Andrzej Siewior 		lock_map_release(&tick_freeze_map);
54875e0678eSRafael J. Wysocki 	} else {
549f46481d0SThomas Gleixner 		tick_suspend_local();
55075e0678eSRafael J. Wysocki 	}
551124cf911SRafael J. Wysocki 
552124cf911SRafael J. Wysocki 	raw_spin_unlock(&tick_freeze_lock);
553124cf911SRafael J. Wysocki }
554124cf911SRafael J. Wysocki 
555124cf911SRafael J. Wysocki /**
556124cf911SRafael J. Wysocki  * tick_unfreeze - Resume the local tick and (possibly) timekeeping.
557124cf911SRafael J. Wysocki  *
558124cf911SRafael J. Wysocki  * Check if this is the first CPU executing the function and if so, resume
559124cf911SRafael J. Wysocki  * timekeeping.  Otherwise resume the local tick.
560124cf911SRafael J. Wysocki  *
561124cf911SRafael J. Wysocki  * Call with interrupts disabled.  Must be balanced with %tick_freeze().
562124cf911SRafael J. Wysocki  * Interrupts must not be enabled after the preceding %tick_freeze().
563124cf911SRafael J. Wysocki  */
tick_unfreeze(void)564124cf911SRafael J. Wysocki void tick_unfreeze(void)
565124cf911SRafael J. Wysocki {
566124cf911SRafael J. Wysocki 	raw_spin_lock(&tick_freeze_lock);
567124cf911SRafael J. Wysocki 
56875e0678eSRafael J. Wysocki 	if (tick_freeze_depth == num_online_cpus()) {
569*92e250c6SSebastian Andrzej Siewior 		/*
570*92e250c6SSebastian Andrzej Siewior 		 * Similar to tick_freeze(). On resumption the first CPU may
571*92e250c6SSebastian Andrzej Siewior 		 * acquire uncontended sleeping locks while other CPUs block on
572*92e250c6SSebastian Andrzej Siewior 		 * tick_freeze_lock.
573*92e250c6SSebastian Andrzej Siewior 		 */
574*92e250c6SSebastian Andrzej Siewior 		lock_map_acquire_try(&tick_freeze_map);
575124cf911SRafael J. Wysocki 		timekeeping_resume();
5763f2552f7SChang-An Chen 		sched_clock_resume();
577*92e250c6SSebastian Andrzej Siewior 		lock_map_release(&tick_freeze_map);
578*92e250c6SSebastian Andrzej Siewior 
579c1a957d1SThomas Gleixner 		system_state = SYSTEM_RUNNING;
58075e0678eSRafael J. Wysocki 		trace_suspend_resume(TPS("timekeeping_freeze"),
58175e0678eSRafael J. Wysocki 				     smp_processor_id(), false);
58275e0678eSRafael J. Wysocki 	} else {
5835167c506SChunyan Zhang 		touch_softlockup_watchdog();
584422fe750SRafael J. Wysocki 		tick_resume_local();
58575e0678eSRafael J. Wysocki 	}
586124cf911SRafael J. Wysocki 
587124cf911SRafael J. Wysocki 	tick_freeze_depth--;
588124cf911SRafael J. Wysocki 
589124cf911SRafael J. Wysocki 	raw_spin_unlock(&tick_freeze_lock);
590124cf911SRafael J. Wysocki }
59187e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */
592124cf911SRafael J. Wysocki 
593906568c9SThomas Gleixner /**
594906568c9SThomas Gleixner  * tick_init - initialize the tick control
595906568c9SThomas Gleixner  */
tick_init(void)596906568c9SThomas Gleixner void __init tick_init(void)
597906568c9SThomas Gleixner {
598b352bc1cSThomas Gleixner 	tick_broadcast_init();
599a80e49e2SFrederic Weisbecker 	tick_nohz_init();
600906568c9SThomas Gleixner }
601