xref: /openbmc/linux/kernel/time/tick-common.c (revision 3f2552f7e9c5abef2775c53f7af66532f8bf65bc)
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>
14906568c9SThomas Gleixner #include <linux/percpu.h>
15906568c9SThomas Gleixner #include <linux/profile.h>
16906568c9SThomas Gleixner #include <linux/sched.h>
17ccf33d68SThomas Gleixner #include <linux/module.h>
1875e0678eSRafael J. Wysocki #include <trace/events/power.h>
19906568c9SThomas Gleixner 
20d7b90689SRussell King #include <asm/irq_regs.h>
21d7b90689SRussell King 
22f8381cbaSThomas Gleixner #include "tick-internal.h"
23f8381cbaSThomas Gleixner 
24906568c9SThomas Gleixner /*
25906568c9SThomas Gleixner  * Tick devices
26906568c9SThomas Gleixner  */
27f8381cbaSThomas Gleixner DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
28906568c9SThomas Gleixner /*
29906568c9SThomas Gleixner  * Tick next event: keeps track of the tick time
30906568c9SThomas Gleixner  */
31f8381cbaSThomas Gleixner ktime_t tick_next_period;
32f8381cbaSThomas Gleixner ktime_t tick_period;
33050ded1bSAndrew Morton 
34050ded1bSAndrew Morton /*
35050ded1bSAndrew Morton  * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR
36050ded1bSAndrew Morton  * which is responsible for calling do_timer(), i.e. the timekeeping stuff. This
37050ded1bSAndrew Morton  * variable has two functions:
38050ded1bSAndrew Morton  *
39050ded1bSAndrew Morton  * 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the
40050ded1bSAndrew Morton  *    timekeeping lock all at once. Only the CPU which is assigned to do the
41050ded1bSAndrew Morton  *    update is handling it.
42050ded1bSAndrew Morton  *
43050ded1bSAndrew Morton  * 2) Hand off the duty in the NOHZ idle case by setting the value to
44050ded1bSAndrew Morton  *    TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks
45050ded1bSAndrew Morton  *    at it will take over and keep the time keeping alive.  The handover
46050ded1bSAndrew Morton  *    procedure also covers cpu hotplug.
47050ded1bSAndrew Morton  */
486441402bSThomas Gleixner int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT;
49906568c9SThomas Gleixner 
50289f480aSIngo Molnar /*
51289f480aSIngo Molnar  * Debugging: see timer_list.c
52289f480aSIngo Molnar  */
53289f480aSIngo Molnar struct tick_device *tick_get_device(int cpu)
54289f480aSIngo Molnar {
55289f480aSIngo Molnar 	return &per_cpu(tick_cpu_device, cpu);
56289f480aSIngo Molnar }
57289f480aSIngo Molnar 
5879bf2bb3SThomas Gleixner /**
5979bf2bb3SThomas Gleixner  * tick_is_oneshot_available - check for a oneshot capable event device
6079bf2bb3SThomas Gleixner  */
6179bf2bb3SThomas Gleixner int tick_is_oneshot_available(void)
6279bf2bb3SThomas Gleixner {
63909ea964SChristoph Lameter 	struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
6479bf2bb3SThomas Gleixner 
653a142a06SThomas Gleixner 	if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
663a142a06SThomas Gleixner 		return 0;
673a142a06SThomas Gleixner 	if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
683a142a06SThomas Gleixner 		return 1;
693a142a06SThomas Gleixner 	return tick_broadcast_oneshot_available();
7079bf2bb3SThomas Gleixner }
7179bf2bb3SThomas Gleixner 
72906568c9SThomas Gleixner /*
73906568c9SThomas Gleixner  * Periodic tick
74906568c9SThomas Gleixner  */
75906568c9SThomas Gleixner static void tick_periodic(int cpu)
76906568c9SThomas Gleixner {
77906568c9SThomas Gleixner 	if (tick_do_timer_cpu == cpu) {
78d6ad4187SJohn Stultz 		write_seqlock(&jiffies_lock);
79906568c9SThomas Gleixner 
80906568c9SThomas Gleixner 		/* Keep track of the next tick event */
81906568c9SThomas Gleixner 		tick_next_period = ktime_add(tick_next_period, tick_period);
82906568c9SThomas Gleixner 
83906568c9SThomas Gleixner 		do_timer(1);
84d6ad4187SJohn Stultz 		write_sequnlock(&jiffies_lock);
8547a1b796SJohn Stultz 		update_wall_time();
86906568c9SThomas Gleixner 	}
87906568c9SThomas Gleixner 
88906568c9SThomas Gleixner 	update_process_times(user_mode(get_irq_regs()));
89906568c9SThomas Gleixner 	profile_tick(CPU_PROFILING);
90906568c9SThomas Gleixner }
91906568c9SThomas Gleixner 
92906568c9SThomas Gleixner /*
93906568c9SThomas Gleixner  * Event handler for periodic ticks
94906568c9SThomas Gleixner  */
95906568c9SThomas Gleixner void tick_handle_periodic(struct clock_event_device *dev)
96906568c9SThomas Gleixner {
97906568c9SThomas Gleixner 	int cpu = smp_processor_id();
98b97f0291SViresh Kumar 	ktime_t next = dev->next_event;
99906568c9SThomas Gleixner 
100906568c9SThomas Gleixner 	tick_periodic(cpu);
101906568c9SThomas Gleixner 
102c6eb3f70SThomas Gleixner #if defined(CONFIG_HIGH_RES_TIMERS) || defined(CONFIG_NO_HZ_COMMON)
103c6eb3f70SThomas Gleixner 	/*
104c6eb3f70SThomas Gleixner 	 * The cpu might have transitioned to HIGHRES or NOHZ mode via
105c6eb3f70SThomas Gleixner 	 * update_process_times() -> run_local_timers() ->
106c6eb3f70SThomas Gleixner 	 * hrtimer_run_queues().
107c6eb3f70SThomas Gleixner 	 */
108c6eb3f70SThomas Gleixner 	if (dev->event_handler != tick_handle_periodic)
109c6eb3f70SThomas Gleixner 		return;
110c6eb3f70SThomas Gleixner #endif
111c6eb3f70SThomas Gleixner 
112472c4a94SViresh Kumar 	if (!clockevent_state_oneshot(dev))
113906568c9SThomas Gleixner 		return;
114b97f0291SViresh Kumar 	for (;;) {
115906568c9SThomas Gleixner 		/*
116906568c9SThomas Gleixner 		 * Setup the next period for devices, which do not have
117906568c9SThomas Gleixner 		 * periodic mode:
118906568c9SThomas Gleixner 		 */
119b97f0291SViresh Kumar 		next = ktime_add(next, tick_period);
120b97f0291SViresh Kumar 
121d1748302SMartin Schwidefsky 		if (!clockevents_program_event(dev, next, false))
122906568c9SThomas Gleixner 			return;
12374a03b69Sjohn stultz 		/*
12474a03b69Sjohn stultz 		 * Have to be careful here. If we're in oneshot mode,
12574a03b69Sjohn stultz 		 * before we call tick_periodic() in a loop, we need
12674a03b69Sjohn stultz 		 * to be sure we're using a real hardware clocksource.
12774a03b69Sjohn stultz 		 * Otherwise we could get trapped in an infinite
12874a03b69Sjohn stultz 		 * loop, as the tick_periodic() increments jiffies,
129cacb3c76SViresh Kumar 		 * which then will increment time, possibly causing
13074a03b69Sjohn stultz 		 * the loop to trigger again and again.
13174a03b69Sjohn stultz 		 */
13274a03b69Sjohn stultz 		if (timekeeping_valid_for_hres())
133906568c9SThomas Gleixner 			tick_periodic(cpu);
134906568c9SThomas Gleixner 	}
135906568c9SThomas Gleixner }
136906568c9SThomas Gleixner 
137906568c9SThomas Gleixner /*
138906568c9SThomas Gleixner  * Setup the device for a periodic tick
139906568c9SThomas Gleixner  */
140f8381cbaSThomas Gleixner void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
141906568c9SThomas Gleixner {
142f8381cbaSThomas Gleixner 	tick_set_periodic_handler(dev, broadcast);
143f8381cbaSThomas Gleixner 
144f8381cbaSThomas Gleixner 	/* Broadcast setup ? */
145f8381cbaSThomas Gleixner 	if (!tick_device_is_functional(dev))
146f8381cbaSThomas Gleixner 		return;
147906568c9SThomas Gleixner 
14827ce4cb4SThomas Gleixner 	if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
14927ce4cb4SThomas Gleixner 	    !tick_broadcast_oneshot_active()) {
150d7eb231cSThomas Gleixner 		clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
151906568c9SThomas Gleixner 	} else {
152906568c9SThomas Gleixner 		unsigned long seq;
153906568c9SThomas Gleixner 		ktime_t next;
154906568c9SThomas Gleixner 
155906568c9SThomas Gleixner 		do {
156d6ad4187SJohn Stultz 			seq = read_seqbegin(&jiffies_lock);
157906568c9SThomas Gleixner 			next = tick_next_period;
158d6ad4187SJohn Stultz 		} while (read_seqretry(&jiffies_lock, seq));
159906568c9SThomas Gleixner 
160d7eb231cSThomas Gleixner 		clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
161906568c9SThomas Gleixner 
162906568c9SThomas Gleixner 		for (;;) {
163d1748302SMartin Schwidefsky 			if (!clockevents_program_event(dev, next, false))
164906568c9SThomas Gleixner 				return;
165906568c9SThomas Gleixner 			next = ktime_add(next, tick_period);
166906568c9SThomas Gleixner 		}
167906568c9SThomas Gleixner 	}
168906568c9SThomas Gleixner }
169906568c9SThomas Gleixner 
170906568c9SThomas Gleixner /*
171906568c9SThomas Gleixner  * Setup the tick device
172906568c9SThomas Gleixner  */
173906568c9SThomas Gleixner static void tick_setup_device(struct tick_device *td,
174906568c9SThomas Gleixner 			      struct clock_event_device *newdev, int cpu,
1750de26520SRusty Russell 			      const struct cpumask *cpumask)
176906568c9SThomas Gleixner {
177906568c9SThomas Gleixner 	void (*handler)(struct clock_event_device *) = NULL;
1788b0e1953SThomas Gleixner 	ktime_t next_event = 0;
179906568c9SThomas Gleixner 
180906568c9SThomas Gleixner 	/*
181906568c9SThomas Gleixner 	 * First device setup ?
182906568c9SThomas Gleixner 	 */
183906568c9SThomas Gleixner 	if (!td->evtdev) {
184906568c9SThomas Gleixner 		/*
185906568c9SThomas Gleixner 		 * If no cpu took the do_timer update, assign it to
186906568c9SThomas Gleixner 		 * this cpu:
187906568c9SThomas Gleixner 		 */
1886441402bSThomas Gleixner 		if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) {
189c5bfece2SFrederic Weisbecker 			if (!tick_nohz_full_cpu(cpu))
190906568c9SThomas Gleixner 				tick_do_timer_cpu = cpu;
191a382bf93SFrederic Weisbecker 			else
192a382bf93SFrederic Weisbecker 				tick_do_timer_cpu = TICK_DO_TIMER_NONE;
193906568c9SThomas Gleixner 			tick_next_period = ktime_get();
1948b0e1953SThomas Gleixner 			tick_period = NSEC_PER_SEC / HZ;
195906568c9SThomas Gleixner 		}
196906568c9SThomas Gleixner 
197906568c9SThomas Gleixner 		/*
198906568c9SThomas Gleixner 		 * Startup in periodic mode first.
199906568c9SThomas Gleixner 		 */
200906568c9SThomas Gleixner 		td->mode = TICKDEV_MODE_PERIODIC;
201906568c9SThomas Gleixner 	} else {
202906568c9SThomas Gleixner 		handler = td->evtdev->event_handler;
203906568c9SThomas Gleixner 		next_event = td->evtdev->next_event;
2047c1e7689SVenkatesh Pallipadi 		td->evtdev->event_handler = clockevents_handle_noop;
205906568c9SThomas Gleixner 	}
206906568c9SThomas Gleixner 
207906568c9SThomas Gleixner 	td->evtdev = newdev;
208906568c9SThomas Gleixner 
209906568c9SThomas Gleixner 	/*
210906568c9SThomas Gleixner 	 * When the device is not per cpu, pin the interrupt to the
211906568c9SThomas Gleixner 	 * current cpu:
212906568c9SThomas Gleixner 	 */
213320ab2b0SRusty Russell 	if (!cpumask_equal(newdev->cpumask, cpumask))
2140de26520SRusty Russell 		irq_set_affinity(newdev->irq, cpumask);
215906568c9SThomas Gleixner 
216f8381cbaSThomas Gleixner 	/*
217f8381cbaSThomas Gleixner 	 * When global broadcasting is active, check if the current
218f8381cbaSThomas Gleixner 	 * device is registered as a placeholder for broadcast mode.
219f8381cbaSThomas Gleixner 	 * This allows us to handle this x86 misfeature in a generic
22007bd1172SThomas Gleixner 	 * way. This function also returns !=0 when we keep the
22107bd1172SThomas Gleixner 	 * current active broadcast state for this CPU.
222f8381cbaSThomas Gleixner 	 */
223f8381cbaSThomas Gleixner 	if (tick_device_uses_broadcast(newdev, cpu))
224f8381cbaSThomas Gleixner 		return;
225f8381cbaSThomas Gleixner 
226906568c9SThomas Gleixner 	if (td->mode == TICKDEV_MODE_PERIODIC)
227906568c9SThomas Gleixner 		tick_setup_periodic(newdev, 0);
22879bf2bb3SThomas Gleixner 	else
22979bf2bb3SThomas Gleixner 		tick_setup_oneshot(newdev, handler, next_event);
230906568c9SThomas Gleixner }
231906568c9SThomas Gleixner 
23203e13cf5SThomas Gleixner void tick_install_replacement(struct clock_event_device *newdev)
23303e13cf5SThomas Gleixner {
23422127e93SChristoph Lameter 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
23503e13cf5SThomas Gleixner 	int cpu = smp_processor_id();
23603e13cf5SThomas Gleixner 
23703e13cf5SThomas Gleixner 	clockevents_exchange_device(td->evtdev, newdev);
23803e13cf5SThomas Gleixner 	tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
23903e13cf5SThomas Gleixner 	if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
24003e13cf5SThomas Gleixner 		tick_oneshot_notify();
24103e13cf5SThomas Gleixner }
24203e13cf5SThomas Gleixner 
24345cb8e01SThomas Gleixner static bool tick_check_percpu(struct clock_event_device *curdev,
24445cb8e01SThomas Gleixner 			      struct clock_event_device *newdev, int cpu)
24545cb8e01SThomas Gleixner {
24645cb8e01SThomas Gleixner 	if (!cpumask_test_cpu(cpu, newdev->cpumask))
24745cb8e01SThomas Gleixner 		return false;
24845cb8e01SThomas Gleixner 	if (cpumask_equal(newdev->cpumask, cpumask_of(cpu)))
24945cb8e01SThomas Gleixner 		return true;
25045cb8e01SThomas Gleixner 	/* Check if irq affinity can be set */
25145cb8e01SThomas Gleixner 	if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq))
25245cb8e01SThomas Gleixner 		return false;
25345cb8e01SThomas Gleixner 	/* Prefer an existing cpu local device */
25445cb8e01SThomas Gleixner 	if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
25545cb8e01SThomas Gleixner 		return false;
25645cb8e01SThomas Gleixner 	return true;
25745cb8e01SThomas Gleixner }
25845cb8e01SThomas Gleixner 
25945cb8e01SThomas Gleixner static bool tick_check_preferred(struct clock_event_device *curdev,
26045cb8e01SThomas Gleixner 				 struct clock_event_device *newdev)
26145cb8e01SThomas Gleixner {
26245cb8e01SThomas Gleixner 	/* Prefer oneshot capable device */
26345cb8e01SThomas Gleixner 	if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) {
26445cb8e01SThomas Gleixner 		if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT))
26545cb8e01SThomas Gleixner 			return false;
26645cb8e01SThomas Gleixner 		if (tick_oneshot_mode_active())
26745cb8e01SThomas Gleixner 			return false;
26845cb8e01SThomas Gleixner 	}
26945cb8e01SThomas Gleixner 
27070e5975dSStephen Boyd 	/*
27170e5975dSStephen Boyd 	 * Use the higher rated one, but prefer a CPU local device with a lower
27270e5975dSStephen Boyd 	 * rating than a non-CPU local device
27370e5975dSStephen Boyd 	 */
27470e5975dSStephen Boyd 	return !curdev ||
27570e5975dSStephen Boyd 		newdev->rating > curdev->rating ||
2765b5ccbc2SSudeep Holla 	       !cpumask_equal(curdev->cpumask, newdev->cpumask);
27745cb8e01SThomas Gleixner }
27845cb8e01SThomas Gleixner 
279906568c9SThomas Gleixner /*
28003e13cf5SThomas Gleixner  * Check whether the new device is a better fit than curdev. curdev
28103e13cf5SThomas Gleixner  * can be NULL !
28203e13cf5SThomas Gleixner  */
28303e13cf5SThomas Gleixner bool tick_check_replacement(struct clock_event_device *curdev,
28403e13cf5SThomas Gleixner 			    struct clock_event_device *newdev)
28503e13cf5SThomas Gleixner {
286521c4299SViresh Kumar 	if (!tick_check_percpu(curdev, newdev, smp_processor_id()))
28703e13cf5SThomas Gleixner 		return false;
28803e13cf5SThomas Gleixner 
28903e13cf5SThomas Gleixner 	return tick_check_preferred(curdev, newdev);
29003e13cf5SThomas Gleixner }
29103e13cf5SThomas Gleixner 
29203e13cf5SThomas Gleixner /*
2937126cac4SThomas Gleixner  * Check, if the new registered device should be used. Called with
2947126cac4SThomas Gleixner  * clockevents_lock held and interrupts disabled.
295906568c9SThomas Gleixner  */
2967172a286SThomas Gleixner void tick_check_new_device(struct clock_event_device *newdev)
297906568c9SThomas Gleixner {
298906568c9SThomas Gleixner 	struct clock_event_device *curdev;
299906568c9SThomas Gleixner 	struct tick_device *td;
3007172a286SThomas Gleixner 	int cpu;
301906568c9SThomas Gleixner 
302906568c9SThomas Gleixner 	cpu = smp_processor_id();
303906568c9SThomas Gleixner 	td = &per_cpu(tick_cpu_device, cpu);
304906568c9SThomas Gleixner 	curdev = td->evtdev;
305906568c9SThomas Gleixner 
306906568c9SThomas Gleixner 	/* cpu local device ? */
30745cb8e01SThomas Gleixner 	if (!tick_check_percpu(curdev, newdev, cpu))
308906568c9SThomas Gleixner 		goto out_bc;
309906568c9SThomas Gleixner 
31045cb8e01SThomas Gleixner 	/* Preference decision */
31145cb8e01SThomas Gleixner 	if (!tick_check_preferred(curdev, newdev))
312906568c9SThomas Gleixner 		goto out_bc;
313906568c9SThomas Gleixner 
314ccf33d68SThomas Gleixner 	if (!try_module_get(newdev->owner))
315ccf33d68SThomas Gleixner 		return;
316ccf33d68SThomas Gleixner 
317906568c9SThomas Gleixner 	/*
318906568c9SThomas Gleixner 	 * Replace the eventually existing device by the new
319f8381cbaSThomas Gleixner 	 * device. If the current device is the broadcast device, do
320f8381cbaSThomas Gleixner 	 * not give it back to the clockevents layer !
321906568c9SThomas Gleixner 	 */
322f8381cbaSThomas Gleixner 	if (tick_is_broadcast_device(curdev)) {
3232344abbcSThomas Gleixner 		clockevents_shutdown(curdev);
324f8381cbaSThomas Gleixner 		curdev = NULL;
325f8381cbaSThomas Gleixner 	}
326906568c9SThomas Gleixner 	clockevents_exchange_device(curdev, newdev);
3276b954823SRusty Russell 	tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
32879bf2bb3SThomas Gleixner 	if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
32979bf2bb3SThomas Gleixner 		tick_oneshot_notify();
3307172a286SThomas Gleixner 	return;
331f8381cbaSThomas Gleixner 
332f8381cbaSThomas Gleixner out_bc:
333f8381cbaSThomas Gleixner 	/*
334f8381cbaSThomas Gleixner 	 * Can the new device be used as a broadcast device ?
335f8381cbaSThomas Gleixner 	 */
3367172a286SThomas Gleixner 	tick_install_broadcast_device(newdev);
337906568c9SThomas Gleixner }
338906568c9SThomas Gleixner 
339f32dd117SThomas Gleixner /**
340f32dd117SThomas Gleixner  * tick_broadcast_oneshot_control - Enter/exit broadcast oneshot mode
341f32dd117SThomas Gleixner  * @state:	The target state (enter/exit)
342f32dd117SThomas Gleixner  *
343f32dd117SThomas Gleixner  * The system enters/leaves a state, where affected devices might stop
344f32dd117SThomas Gleixner  * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups.
345f32dd117SThomas Gleixner  *
346f32dd117SThomas Gleixner  * Called with interrupts disabled, so clockevents_lock is not
347f32dd117SThomas Gleixner  * required here because the local clock event device cannot go away
348f32dd117SThomas Gleixner  * under us.
349f32dd117SThomas Gleixner  */
350f32dd117SThomas Gleixner int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
351f32dd117SThomas Gleixner {
352f32dd117SThomas Gleixner 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
353f32dd117SThomas Gleixner 
354f32dd117SThomas Gleixner 	if (!(td->evtdev->features & CLOCK_EVT_FEAT_C3STOP))
355f32dd117SThomas Gleixner 		return 0;
356f32dd117SThomas Gleixner 
357f32dd117SThomas Gleixner 	return __tick_broadcast_oneshot_control(state);
358f32dd117SThomas Gleixner }
3590f447051SThomas Gleixner EXPORT_SYMBOL_GPL(tick_broadcast_oneshot_control);
360f32dd117SThomas Gleixner 
36152c063d1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
362906568c9SThomas Gleixner /*
36394df7de0SSebastien Dugue  * Transfer the do_timer job away from a dying cpu.
36494df7de0SSebastien Dugue  *
36552c063d1SThomas Gleixner  * Called with interrupts disabled. Not locking required. If
36652c063d1SThomas Gleixner  * tick_do_timer_cpu is owned by this cpu, nothing can change it.
36794df7de0SSebastien Dugue  */
36852c063d1SThomas Gleixner void tick_handover_do_timer(void)
36994df7de0SSebastien Dugue {
37052c063d1SThomas Gleixner 	if (tick_do_timer_cpu == smp_processor_id()) {
37194df7de0SSebastien Dugue 		int cpu = cpumask_first(cpu_online_mask);
37294df7de0SSebastien Dugue 
37394df7de0SSebastien Dugue 		tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu :
37494df7de0SSebastien Dugue 			TICK_DO_TIMER_NONE;
37594df7de0SSebastien Dugue 	}
37694df7de0SSebastien Dugue }
37794df7de0SSebastien Dugue 
37894df7de0SSebastien Dugue /*
379906568c9SThomas Gleixner  * Shutdown an event device on a given cpu:
380906568c9SThomas Gleixner  *
381906568c9SThomas Gleixner  * This is called on a life CPU, when a CPU is dead. So we cannot
382906568c9SThomas Gleixner  * access the hardware device itself.
383906568c9SThomas Gleixner  * We just set the mode and remove it from the lists.
384906568c9SThomas Gleixner  */
385a49b116dSThomas Gleixner void tick_shutdown(unsigned int cpu)
386906568c9SThomas Gleixner {
387a49b116dSThomas Gleixner 	struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
388906568c9SThomas Gleixner 	struct clock_event_device *dev = td->evtdev;
389906568c9SThomas Gleixner 
390906568c9SThomas Gleixner 	td->mode = TICKDEV_MODE_PERIODIC;
391906568c9SThomas Gleixner 	if (dev) {
392906568c9SThomas Gleixner 		/*
393906568c9SThomas Gleixner 		 * Prevent that the clock events layer tries to call
394906568c9SThomas Gleixner 		 * the set mode function!
395906568c9SThomas Gleixner 		 */
396051ebd10SThomas Gleixner 		clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
397906568c9SThomas Gleixner 		clockevents_exchange_device(dev, NULL);
3986f7a05d7SThomas Gleixner 		dev->event_handler = clockevents_handle_noop;
399906568c9SThomas Gleixner 		td->evtdev = NULL;
400906568c9SThomas Gleixner 	}
401906568c9SThomas Gleixner }
402a49b116dSThomas Gleixner #endif
403906568c9SThomas Gleixner 
4044ffee521SThomas Gleixner /**
405f46481d0SThomas Gleixner  * tick_suspend_local - Suspend the local tick device
406f46481d0SThomas Gleixner  *
407f46481d0SThomas Gleixner  * Called from the local cpu for freeze with interrupts disabled.
408f46481d0SThomas Gleixner  *
409f46481d0SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
410f46481d0SThomas Gleixner  */
4117270d11cSThomas Gleixner void tick_suspend_local(void)
412f46481d0SThomas Gleixner {
413f46481d0SThomas Gleixner 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
414f46481d0SThomas Gleixner 
415f46481d0SThomas Gleixner 	clockevents_shutdown(td->evtdev);
416f46481d0SThomas Gleixner }
417f46481d0SThomas Gleixner 
418f46481d0SThomas Gleixner /**
419f46481d0SThomas Gleixner  * tick_resume_local - Resume the local tick device
420f46481d0SThomas Gleixner  *
421f46481d0SThomas Gleixner  * Called from the local CPU for unfreeze or XEN resume magic.
422f46481d0SThomas Gleixner  *
423f46481d0SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
424f46481d0SThomas Gleixner  */
425f46481d0SThomas Gleixner void tick_resume_local(void)
426f46481d0SThomas Gleixner {
427f46481d0SThomas Gleixner 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
428f46481d0SThomas Gleixner 	bool broadcast = tick_resume_check_broadcast();
429f46481d0SThomas Gleixner 
430f46481d0SThomas Gleixner 	clockevents_tick_resume(td->evtdev);
431f46481d0SThomas Gleixner 	if (!broadcast) {
432f46481d0SThomas Gleixner 		if (td->mode == TICKDEV_MODE_PERIODIC)
433f46481d0SThomas Gleixner 			tick_setup_periodic(td->evtdev, 0);
434f46481d0SThomas Gleixner 		else
435f46481d0SThomas Gleixner 			tick_resume_oneshot();
436f46481d0SThomas Gleixner 	}
437f46481d0SThomas Gleixner }
438f46481d0SThomas Gleixner 
439f46481d0SThomas Gleixner /**
4404ffee521SThomas Gleixner  * tick_suspend - Suspend the tick and the broadcast device
4414ffee521SThomas Gleixner  *
4424ffee521SThomas Gleixner  * Called from syscore_suspend() via timekeeping_suspend with only one
4434ffee521SThomas Gleixner  * CPU online and interrupts disabled or from tick_unfreeze() under
4444ffee521SThomas Gleixner  * tick_freeze_lock.
4454ffee521SThomas Gleixner  *
4464ffee521SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
4474ffee521SThomas Gleixner  */
4488c53daf6SThomas Gleixner void tick_suspend(void)
4496321dd60SThomas Gleixner {
450f46481d0SThomas Gleixner 	tick_suspend_local();
4514ffee521SThomas Gleixner 	tick_suspend_broadcast();
4526321dd60SThomas Gleixner }
4536321dd60SThomas Gleixner 
4544ffee521SThomas Gleixner /**
4554ffee521SThomas Gleixner  * tick_resume - Resume the tick and the broadcast device
4564ffee521SThomas Gleixner  *
4574ffee521SThomas Gleixner  * Called from syscore_resume() via timekeeping_resume with only one
458f46481d0SThomas Gleixner  * CPU online and interrupts disabled.
4594ffee521SThomas Gleixner  *
4604ffee521SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
4614ffee521SThomas Gleixner  */
4628c53daf6SThomas Gleixner void tick_resume(void)
4636321dd60SThomas Gleixner {
464f46481d0SThomas Gleixner 	tick_resume_broadcast();
465f46481d0SThomas Gleixner 	tick_resume_local();
4666321dd60SThomas Gleixner }
4676321dd60SThomas Gleixner 
46887e9b9f1SRafael J. Wysocki #ifdef CONFIG_SUSPEND
469124cf911SRafael J. Wysocki static DEFINE_RAW_SPINLOCK(tick_freeze_lock);
470124cf911SRafael J. Wysocki static unsigned int tick_freeze_depth;
471124cf911SRafael J. Wysocki 
472124cf911SRafael J. Wysocki /**
473124cf911SRafael J. Wysocki  * tick_freeze - Suspend the local tick and (possibly) timekeeping.
474124cf911SRafael J. Wysocki  *
475124cf911SRafael J. Wysocki  * Check if this is the last online CPU executing the function and if so,
476124cf911SRafael J. Wysocki  * suspend timekeeping.  Otherwise suspend the local tick.
477124cf911SRafael J. Wysocki  *
478124cf911SRafael J. Wysocki  * Call with interrupts disabled.  Must be balanced with %tick_unfreeze().
479124cf911SRafael J. Wysocki  * Interrupts must not be enabled before the subsequent %tick_unfreeze().
480124cf911SRafael J. Wysocki  */
481124cf911SRafael J. Wysocki void tick_freeze(void)
482124cf911SRafael J. Wysocki {
483124cf911SRafael J. Wysocki 	raw_spin_lock(&tick_freeze_lock);
484124cf911SRafael J. Wysocki 
485124cf911SRafael J. Wysocki 	tick_freeze_depth++;
48675e0678eSRafael J. Wysocki 	if (tick_freeze_depth == num_online_cpus()) {
48775e0678eSRafael J. Wysocki 		trace_suspend_resume(TPS("timekeeping_freeze"),
48875e0678eSRafael J. Wysocki 				     smp_processor_id(), true);
489c1a957d1SThomas Gleixner 		system_state = SYSTEM_SUSPEND;
490*3f2552f7SChang-An Chen 		sched_clock_suspend();
491124cf911SRafael J. Wysocki 		timekeeping_suspend();
49275e0678eSRafael J. Wysocki 	} else {
493f46481d0SThomas Gleixner 		tick_suspend_local();
49475e0678eSRafael J. Wysocki 	}
495124cf911SRafael J. Wysocki 
496124cf911SRafael J. Wysocki 	raw_spin_unlock(&tick_freeze_lock);
497124cf911SRafael J. Wysocki }
498124cf911SRafael J. Wysocki 
499124cf911SRafael J. Wysocki /**
500124cf911SRafael J. Wysocki  * tick_unfreeze - Resume the local tick and (possibly) timekeeping.
501124cf911SRafael J. Wysocki  *
502124cf911SRafael J. Wysocki  * Check if this is the first CPU executing the function and if so, resume
503124cf911SRafael J. Wysocki  * timekeeping.  Otherwise resume the local tick.
504124cf911SRafael J. Wysocki  *
505124cf911SRafael J. Wysocki  * Call with interrupts disabled.  Must be balanced with %tick_freeze().
506124cf911SRafael J. Wysocki  * Interrupts must not be enabled after the preceding %tick_freeze().
507124cf911SRafael J. Wysocki  */
508124cf911SRafael J. Wysocki void tick_unfreeze(void)
509124cf911SRafael J. Wysocki {
510124cf911SRafael J. Wysocki 	raw_spin_lock(&tick_freeze_lock);
511124cf911SRafael J. Wysocki 
51275e0678eSRafael J. Wysocki 	if (tick_freeze_depth == num_online_cpus()) {
513124cf911SRafael J. Wysocki 		timekeeping_resume();
514*3f2552f7SChang-An Chen 		sched_clock_resume();
515c1a957d1SThomas Gleixner 		system_state = SYSTEM_RUNNING;
51675e0678eSRafael J. Wysocki 		trace_suspend_resume(TPS("timekeeping_freeze"),
51775e0678eSRafael J. Wysocki 				     smp_processor_id(), false);
51875e0678eSRafael J. Wysocki 	} else {
519422fe750SRafael J. Wysocki 		tick_resume_local();
52075e0678eSRafael J. Wysocki 	}
521124cf911SRafael J. Wysocki 
522124cf911SRafael J. Wysocki 	tick_freeze_depth--;
523124cf911SRafael J. Wysocki 
524124cf911SRafael J. Wysocki 	raw_spin_unlock(&tick_freeze_lock);
525124cf911SRafael J. Wysocki }
52687e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */
527124cf911SRafael J. Wysocki 
528906568c9SThomas Gleixner /**
529906568c9SThomas Gleixner  * tick_init - initialize the tick control
530906568c9SThomas Gleixner  */
531906568c9SThomas Gleixner void __init tick_init(void)
532906568c9SThomas Gleixner {
533b352bc1cSThomas Gleixner 	tick_broadcast_init();
534a80e49e2SFrederic Weisbecker 	tick_nohz_init();
535906568c9SThomas Gleixner }
536