xref: /openbmc/linux/kernel/time/tick-common.c (revision c1a957d1)
1906568c9SThomas Gleixner /*
2906568c9SThomas Gleixner  * linux/kernel/time/tick-common.c
3906568c9SThomas Gleixner  *
4906568c9SThomas Gleixner  * This file contains the base functions to manage periodic tick
5906568c9SThomas Gleixner  * related events.
6906568c9SThomas Gleixner  *
7906568c9SThomas Gleixner  * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
8906568c9SThomas Gleixner  * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
9906568c9SThomas Gleixner  * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
10906568c9SThomas Gleixner  *
11906568c9SThomas Gleixner  * This code is licenced under the GPL version 2. For details see
12906568c9SThomas Gleixner  * kernel-base/COPYING.
13906568c9SThomas Gleixner  */
14906568c9SThomas Gleixner #include <linux/cpu.h>
15906568c9SThomas Gleixner #include <linux/err.h>
16906568c9SThomas Gleixner #include <linux/hrtimer.h>
17d7b90689SRussell King #include <linux/interrupt.h>
18906568c9SThomas Gleixner #include <linux/percpu.h>
19906568c9SThomas Gleixner #include <linux/profile.h>
20906568c9SThomas Gleixner #include <linux/sched.h>
21ccf33d68SThomas Gleixner #include <linux/module.h>
2275e0678eSRafael J. Wysocki #include <trace/events/power.h>
23906568c9SThomas Gleixner 
24d7b90689SRussell King #include <asm/irq_regs.h>
25d7b90689SRussell King 
26f8381cbaSThomas Gleixner #include "tick-internal.h"
27f8381cbaSThomas Gleixner 
28906568c9SThomas Gleixner /*
29906568c9SThomas Gleixner  * Tick devices
30906568c9SThomas Gleixner  */
31f8381cbaSThomas Gleixner DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
32906568c9SThomas Gleixner /*
33906568c9SThomas Gleixner  * Tick next event: keeps track of the tick time
34906568c9SThomas Gleixner  */
35f8381cbaSThomas Gleixner ktime_t tick_next_period;
36f8381cbaSThomas Gleixner ktime_t tick_period;
37050ded1bSAndrew Morton 
38050ded1bSAndrew Morton /*
39050ded1bSAndrew Morton  * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR
40050ded1bSAndrew Morton  * which is responsible for calling do_timer(), i.e. the timekeeping stuff. This
41050ded1bSAndrew Morton  * variable has two functions:
42050ded1bSAndrew Morton  *
43050ded1bSAndrew Morton  * 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the
44050ded1bSAndrew Morton  *    timekeeping lock all at once. Only the CPU which is assigned to do the
45050ded1bSAndrew Morton  *    update is handling it.
46050ded1bSAndrew Morton  *
47050ded1bSAndrew Morton  * 2) Hand off the duty in the NOHZ idle case by setting the value to
48050ded1bSAndrew Morton  *    TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks
49050ded1bSAndrew Morton  *    at it will take over and keep the time keeping alive.  The handover
50050ded1bSAndrew Morton  *    procedure also covers cpu hotplug.
51050ded1bSAndrew Morton  */
526441402bSThomas Gleixner int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT;
53906568c9SThomas Gleixner 
54289f480aSIngo Molnar /*
55289f480aSIngo Molnar  * Debugging: see timer_list.c
56289f480aSIngo Molnar  */
57289f480aSIngo Molnar struct tick_device *tick_get_device(int cpu)
58289f480aSIngo Molnar {
59289f480aSIngo Molnar 	return &per_cpu(tick_cpu_device, cpu);
60289f480aSIngo Molnar }
61289f480aSIngo Molnar 
6279bf2bb3SThomas Gleixner /**
6379bf2bb3SThomas Gleixner  * tick_is_oneshot_available - check for a oneshot capable event device
6479bf2bb3SThomas Gleixner  */
6579bf2bb3SThomas Gleixner int tick_is_oneshot_available(void)
6679bf2bb3SThomas Gleixner {
67909ea964SChristoph Lameter 	struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
6879bf2bb3SThomas Gleixner 
693a142a06SThomas Gleixner 	if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
703a142a06SThomas Gleixner 		return 0;
713a142a06SThomas Gleixner 	if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
723a142a06SThomas Gleixner 		return 1;
733a142a06SThomas Gleixner 	return tick_broadcast_oneshot_available();
7479bf2bb3SThomas Gleixner }
7579bf2bb3SThomas Gleixner 
76906568c9SThomas Gleixner /*
77906568c9SThomas Gleixner  * Periodic tick
78906568c9SThomas Gleixner  */
79906568c9SThomas Gleixner static void tick_periodic(int cpu)
80906568c9SThomas Gleixner {
81906568c9SThomas Gleixner 	if (tick_do_timer_cpu == cpu) {
82d6ad4187SJohn Stultz 		write_seqlock(&jiffies_lock);
83906568c9SThomas Gleixner 
84906568c9SThomas Gleixner 		/* Keep track of the next tick event */
85906568c9SThomas Gleixner 		tick_next_period = ktime_add(tick_next_period, tick_period);
86906568c9SThomas Gleixner 
87906568c9SThomas Gleixner 		do_timer(1);
88d6ad4187SJohn Stultz 		write_sequnlock(&jiffies_lock);
8947a1b796SJohn Stultz 		update_wall_time();
90906568c9SThomas Gleixner 	}
91906568c9SThomas Gleixner 
92906568c9SThomas Gleixner 	update_process_times(user_mode(get_irq_regs()));
93906568c9SThomas Gleixner 	profile_tick(CPU_PROFILING);
94906568c9SThomas Gleixner }
95906568c9SThomas Gleixner 
96906568c9SThomas Gleixner /*
97906568c9SThomas Gleixner  * Event handler for periodic ticks
98906568c9SThomas Gleixner  */
99906568c9SThomas Gleixner void tick_handle_periodic(struct clock_event_device *dev)
100906568c9SThomas Gleixner {
101906568c9SThomas Gleixner 	int cpu = smp_processor_id();
102b97f0291SViresh Kumar 	ktime_t next = dev->next_event;
103906568c9SThomas Gleixner 
104906568c9SThomas Gleixner 	tick_periodic(cpu);
105906568c9SThomas Gleixner 
106c6eb3f70SThomas Gleixner #if defined(CONFIG_HIGH_RES_TIMERS) || defined(CONFIG_NO_HZ_COMMON)
107c6eb3f70SThomas Gleixner 	/*
108c6eb3f70SThomas Gleixner 	 * The cpu might have transitioned to HIGHRES or NOHZ mode via
109c6eb3f70SThomas Gleixner 	 * update_process_times() -> run_local_timers() ->
110c6eb3f70SThomas Gleixner 	 * hrtimer_run_queues().
111c6eb3f70SThomas Gleixner 	 */
112c6eb3f70SThomas Gleixner 	if (dev->event_handler != tick_handle_periodic)
113c6eb3f70SThomas Gleixner 		return;
114c6eb3f70SThomas Gleixner #endif
115c6eb3f70SThomas Gleixner 
116472c4a94SViresh Kumar 	if (!clockevent_state_oneshot(dev))
117906568c9SThomas Gleixner 		return;
118b97f0291SViresh Kumar 	for (;;) {
119906568c9SThomas Gleixner 		/*
120906568c9SThomas Gleixner 		 * Setup the next period for devices, which do not have
121906568c9SThomas Gleixner 		 * periodic mode:
122906568c9SThomas Gleixner 		 */
123b97f0291SViresh Kumar 		next = ktime_add(next, tick_period);
124b97f0291SViresh Kumar 
125d1748302SMartin Schwidefsky 		if (!clockevents_program_event(dev, next, false))
126906568c9SThomas Gleixner 			return;
12774a03b69Sjohn stultz 		/*
12874a03b69Sjohn stultz 		 * Have to be careful here. If we're in oneshot mode,
12974a03b69Sjohn stultz 		 * before we call tick_periodic() in a loop, we need
13074a03b69Sjohn stultz 		 * to be sure we're using a real hardware clocksource.
13174a03b69Sjohn stultz 		 * Otherwise we could get trapped in an infinite
13274a03b69Sjohn stultz 		 * loop, as the tick_periodic() increments jiffies,
133cacb3c76SViresh Kumar 		 * which then will increment time, possibly causing
13474a03b69Sjohn stultz 		 * the loop to trigger again and again.
13574a03b69Sjohn stultz 		 */
13674a03b69Sjohn stultz 		if (timekeeping_valid_for_hres())
137906568c9SThomas Gleixner 			tick_periodic(cpu);
138906568c9SThomas Gleixner 	}
139906568c9SThomas Gleixner }
140906568c9SThomas Gleixner 
141906568c9SThomas Gleixner /*
142906568c9SThomas Gleixner  * Setup the device for a periodic tick
143906568c9SThomas Gleixner  */
144f8381cbaSThomas Gleixner void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
145906568c9SThomas Gleixner {
146f8381cbaSThomas Gleixner 	tick_set_periodic_handler(dev, broadcast);
147f8381cbaSThomas Gleixner 
148f8381cbaSThomas Gleixner 	/* Broadcast setup ? */
149f8381cbaSThomas Gleixner 	if (!tick_device_is_functional(dev))
150f8381cbaSThomas Gleixner 		return;
151906568c9SThomas Gleixner 
15227ce4cb4SThomas Gleixner 	if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
15327ce4cb4SThomas Gleixner 	    !tick_broadcast_oneshot_active()) {
154d7eb231cSThomas Gleixner 		clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
155906568c9SThomas Gleixner 	} else {
156906568c9SThomas Gleixner 		unsigned long seq;
157906568c9SThomas Gleixner 		ktime_t next;
158906568c9SThomas Gleixner 
159906568c9SThomas Gleixner 		do {
160d6ad4187SJohn Stultz 			seq = read_seqbegin(&jiffies_lock);
161906568c9SThomas Gleixner 			next = tick_next_period;
162d6ad4187SJohn Stultz 		} while (read_seqretry(&jiffies_lock, seq));
163906568c9SThomas Gleixner 
164d7eb231cSThomas Gleixner 		clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
165906568c9SThomas Gleixner 
166906568c9SThomas Gleixner 		for (;;) {
167d1748302SMartin Schwidefsky 			if (!clockevents_program_event(dev, next, false))
168906568c9SThomas Gleixner 				return;
169906568c9SThomas Gleixner 			next = ktime_add(next, tick_period);
170906568c9SThomas Gleixner 		}
171906568c9SThomas Gleixner 	}
172906568c9SThomas Gleixner }
173906568c9SThomas Gleixner 
174906568c9SThomas Gleixner /*
175906568c9SThomas Gleixner  * Setup the tick device
176906568c9SThomas Gleixner  */
177906568c9SThomas Gleixner static void tick_setup_device(struct tick_device *td,
178906568c9SThomas Gleixner 			      struct clock_event_device *newdev, int cpu,
1790de26520SRusty Russell 			      const struct cpumask *cpumask)
180906568c9SThomas Gleixner {
181906568c9SThomas Gleixner 	void (*handler)(struct clock_event_device *) = NULL;
1828b0e1953SThomas Gleixner 	ktime_t next_event = 0;
183906568c9SThomas Gleixner 
184906568c9SThomas Gleixner 	/*
185906568c9SThomas Gleixner 	 * First device setup ?
186906568c9SThomas Gleixner 	 */
187906568c9SThomas Gleixner 	if (!td->evtdev) {
188906568c9SThomas Gleixner 		/*
189906568c9SThomas Gleixner 		 * If no cpu took the do_timer update, assign it to
190906568c9SThomas Gleixner 		 * this cpu:
191906568c9SThomas Gleixner 		 */
1926441402bSThomas Gleixner 		if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) {
193c5bfece2SFrederic Weisbecker 			if (!tick_nohz_full_cpu(cpu))
194906568c9SThomas Gleixner 				tick_do_timer_cpu = cpu;
195a382bf93SFrederic Weisbecker 			else
196a382bf93SFrederic Weisbecker 				tick_do_timer_cpu = TICK_DO_TIMER_NONE;
197906568c9SThomas Gleixner 			tick_next_period = ktime_get();
1988b0e1953SThomas Gleixner 			tick_period = NSEC_PER_SEC / HZ;
199906568c9SThomas Gleixner 		}
200906568c9SThomas Gleixner 
201906568c9SThomas Gleixner 		/*
202906568c9SThomas Gleixner 		 * Startup in periodic mode first.
203906568c9SThomas Gleixner 		 */
204906568c9SThomas Gleixner 		td->mode = TICKDEV_MODE_PERIODIC;
205906568c9SThomas Gleixner 	} else {
206906568c9SThomas Gleixner 		handler = td->evtdev->event_handler;
207906568c9SThomas Gleixner 		next_event = td->evtdev->next_event;
2087c1e7689SVenkatesh Pallipadi 		td->evtdev->event_handler = clockevents_handle_noop;
209906568c9SThomas Gleixner 	}
210906568c9SThomas Gleixner 
211906568c9SThomas Gleixner 	td->evtdev = newdev;
212906568c9SThomas Gleixner 
213906568c9SThomas Gleixner 	/*
214906568c9SThomas Gleixner 	 * When the device is not per cpu, pin the interrupt to the
215906568c9SThomas Gleixner 	 * current cpu:
216906568c9SThomas Gleixner 	 */
217320ab2b0SRusty Russell 	if (!cpumask_equal(newdev->cpumask, cpumask))
2180de26520SRusty Russell 		irq_set_affinity(newdev->irq, cpumask);
219906568c9SThomas Gleixner 
220f8381cbaSThomas Gleixner 	/*
221f8381cbaSThomas Gleixner 	 * When global broadcasting is active, check if the current
222f8381cbaSThomas Gleixner 	 * device is registered as a placeholder for broadcast mode.
223f8381cbaSThomas Gleixner 	 * This allows us to handle this x86 misfeature in a generic
22407bd1172SThomas Gleixner 	 * way. This function also returns !=0 when we keep the
22507bd1172SThomas Gleixner 	 * current active broadcast state for this CPU.
226f8381cbaSThomas Gleixner 	 */
227f8381cbaSThomas Gleixner 	if (tick_device_uses_broadcast(newdev, cpu))
228f8381cbaSThomas Gleixner 		return;
229f8381cbaSThomas Gleixner 
230906568c9SThomas Gleixner 	if (td->mode == TICKDEV_MODE_PERIODIC)
231906568c9SThomas Gleixner 		tick_setup_periodic(newdev, 0);
23279bf2bb3SThomas Gleixner 	else
23379bf2bb3SThomas Gleixner 		tick_setup_oneshot(newdev, handler, next_event);
234906568c9SThomas Gleixner }
235906568c9SThomas Gleixner 
23603e13cf5SThomas Gleixner void tick_install_replacement(struct clock_event_device *newdev)
23703e13cf5SThomas Gleixner {
23822127e93SChristoph Lameter 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
23903e13cf5SThomas Gleixner 	int cpu = smp_processor_id();
24003e13cf5SThomas Gleixner 
24103e13cf5SThomas Gleixner 	clockevents_exchange_device(td->evtdev, newdev);
24203e13cf5SThomas Gleixner 	tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
24303e13cf5SThomas Gleixner 	if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
24403e13cf5SThomas Gleixner 		tick_oneshot_notify();
24503e13cf5SThomas Gleixner }
24603e13cf5SThomas Gleixner 
24745cb8e01SThomas Gleixner static bool tick_check_percpu(struct clock_event_device *curdev,
24845cb8e01SThomas Gleixner 			      struct clock_event_device *newdev, int cpu)
24945cb8e01SThomas Gleixner {
25045cb8e01SThomas Gleixner 	if (!cpumask_test_cpu(cpu, newdev->cpumask))
25145cb8e01SThomas Gleixner 		return false;
25245cb8e01SThomas Gleixner 	if (cpumask_equal(newdev->cpumask, cpumask_of(cpu)))
25345cb8e01SThomas Gleixner 		return true;
25445cb8e01SThomas Gleixner 	/* Check if irq affinity can be set */
25545cb8e01SThomas Gleixner 	if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq))
25645cb8e01SThomas Gleixner 		return false;
25745cb8e01SThomas Gleixner 	/* Prefer an existing cpu local device */
25845cb8e01SThomas Gleixner 	if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
25945cb8e01SThomas Gleixner 		return false;
26045cb8e01SThomas Gleixner 	return true;
26145cb8e01SThomas Gleixner }
26245cb8e01SThomas Gleixner 
26345cb8e01SThomas Gleixner static bool tick_check_preferred(struct clock_event_device *curdev,
26445cb8e01SThomas Gleixner 				 struct clock_event_device *newdev)
26545cb8e01SThomas Gleixner {
26645cb8e01SThomas Gleixner 	/* Prefer oneshot capable device */
26745cb8e01SThomas Gleixner 	if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) {
26845cb8e01SThomas Gleixner 		if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT))
26945cb8e01SThomas Gleixner 			return false;
27045cb8e01SThomas Gleixner 		if (tick_oneshot_mode_active())
27145cb8e01SThomas Gleixner 			return false;
27245cb8e01SThomas Gleixner 	}
27345cb8e01SThomas Gleixner 
27470e5975dSStephen Boyd 	/*
27570e5975dSStephen Boyd 	 * Use the higher rated one, but prefer a CPU local device with a lower
27670e5975dSStephen Boyd 	 * rating than a non-CPU local device
27770e5975dSStephen Boyd 	 */
27870e5975dSStephen Boyd 	return !curdev ||
27970e5975dSStephen Boyd 		newdev->rating > curdev->rating ||
28070e5975dSStephen Boyd 	       !cpumask_equal(curdev->cpumask, newdev->cpumask);
28145cb8e01SThomas Gleixner }
28245cb8e01SThomas Gleixner 
283906568c9SThomas Gleixner /*
28403e13cf5SThomas Gleixner  * Check whether the new device is a better fit than curdev. curdev
28503e13cf5SThomas Gleixner  * can be NULL !
28603e13cf5SThomas Gleixner  */
28703e13cf5SThomas Gleixner bool tick_check_replacement(struct clock_event_device *curdev,
28803e13cf5SThomas Gleixner 			    struct clock_event_device *newdev)
28903e13cf5SThomas Gleixner {
290521c4299SViresh Kumar 	if (!tick_check_percpu(curdev, newdev, smp_processor_id()))
29103e13cf5SThomas Gleixner 		return false;
29203e13cf5SThomas Gleixner 
29303e13cf5SThomas Gleixner 	return tick_check_preferred(curdev, newdev);
29403e13cf5SThomas Gleixner }
29503e13cf5SThomas Gleixner 
29603e13cf5SThomas Gleixner /*
2977126cac4SThomas Gleixner  * Check, if the new registered device should be used. Called with
2987126cac4SThomas Gleixner  * clockevents_lock held and interrupts disabled.
299906568c9SThomas Gleixner  */
3007172a286SThomas Gleixner void tick_check_new_device(struct clock_event_device *newdev)
301906568c9SThomas Gleixner {
302906568c9SThomas Gleixner 	struct clock_event_device *curdev;
303906568c9SThomas Gleixner 	struct tick_device *td;
3047172a286SThomas Gleixner 	int cpu;
305906568c9SThomas Gleixner 
306906568c9SThomas Gleixner 	cpu = smp_processor_id();
307906568c9SThomas Gleixner 	td = &per_cpu(tick_cpu_device, cpu);
308906568c9SThomas Gleixner 	curdev = td->evtdev;
309906568c9SThomas Gleixner 
310906568c9SThomas Gleixner 	/* cpu local device ? */
31145cb8e01SThomas Gleixner 	if (!tick_check_percpu(curdev, newdev, cpu))
312906568c9SThomas Gleixner 		goto out_bc;
313906568c9SThomas Gleixner 
31445cb8e01SThomas Gleixner 	/* Preference decision */
31545cb8e01SThomas Gleixner 	if (!tick_check_preferred(curdev, newdev))
316906568c9SThomas Gleixner 		goto out_bc;
317906568c9SThomas Gleixner 
318ccf33d68SThomas Gleixner 	if (!try_module_get(newdev->owner))
319ccf33d68SThomas Gleixner 		return;
320ccf33d68SThomas Gleixner 
321906568c9SThomas Gleixner 	/*
322906568c9SThomas Gleixner 	 * Replace the eventually existing device by the new
323f8381cbaSThomas Gleixner 	 * device. If the current device is the broadcast device, do
324f8381cbaSThomas Gleixner 	 * not give it back to the clockevents layer !
325906568c9SThomas Gleixner 	 */
326f8381cbaSThomas Gleixner 	if (tick_is_broadcast_device(curdev)) {
3272344abbcSThomas Gleixner 		clockevents_shutdown(curdev);
328f8381cbaSThomas Gleixner 		curdev = NULL;
329f8381cbaSThomas Gleixner 	}
330906568c9SThomas Gleixner 	clockevents_exchange_device(curdev, newdev);
3316b954823SRusty Russell 	tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
33279bf2bb3SThomas Gleixner 	if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
33379bf2bb3SThomas Gleixner 		tick_oneshot_notify();
3347172a286SThomas Gleixner 	return;
335f8381cbaSThomas Gleixner 
336f8381cbaSThomas Gleixner out_bc:
337f8381cbaSThomas Gleixner 	/*
338f8381cbaSThomas Gleixner 	 * Can the new device be used as a broadcast device ?
339f8381cbaSThomas Gleixner 	 */
3407172a286SThomas Gleixner 	tick_install_broadcast_device(newdev);
341906568c9SThomas Gleixner }
342906568c9SThomas Gleixner 
343f32dd117SThomas Gleixner /**
344f32dd117SThomas Gleixner  * tick_broadcast_oneshot_control - Enter/exit broadcast oneshot mode
345f32dd117SThomas Gleixner  * @state:	The target state (enter/exit)
346f32dd117SThomas Gleixner  *
347f32dd117SThomas Gleixner  * The system enters/leaves a state, where affected devices might stop
348f32dd117SThomas Gleixner  * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups.
349f32dd117SThomas Gleixner  *
350f32dd117SThomas Gleixner  * Called with interrupts disabled, so clockevents_lock is not
351f32dd117SThomas Gleixner  * required here because the local clock event device cannot go away
352f32dd117SThomas Gleixner  * under us.
353f32dd117SThomas Gleixner  */
354f32dd117SThomas Gleixner int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
355f32dd117SThomas Gleixner {
356f32dd117SThomas Gleixner 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
357f32dd117SThomas Gleixner 
358f32dd117SThomas Gleixner 	if (!(td->evtdev->features & CLOCK_EVT_FEAT_C3STOP))
359f32dd117SThomas Gleixner 		return 0;
360f32dd117SThomas Gleixner 
361f32dd117SThomas Gleixner 	return __tick_broadcast_oneshot_control(state);
362f32dd117SThomas Gleixner }
3630f447051SThomas Gleixner EXPORT_SYMBOL_GPL(tick_broadcast_oneshot_control);
364f32dd117SThomas Gleixner 
36552c063d1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
366906568c9SThomas Gleixner /*
36794df7de0SSebastien Dugue  * Transfer the do_timer job away from a dying cpu.
36894df7de0SSebastien Dugue  *
36952c063d1SThomas Gleixner  * Called with interrupts disabled. Not locking required. If
37052c063d1SThomas Gleixner  * tick_do_timer_cpu is owned by this cpu, nothing can change it.
37194df7de0SSebastien Dugue  */
37252c063d1SThomas Gleixner void tick_handover_do_timer(void)
37394df7de0SSebastien Dugue {
37452c063d1SThomas Gleixner 	if (tick_do_timer_cpu == smp_processor_id()) {
37594df7de0SSebastien Dugue 		int cpu = cpumask_first(cpu_online_mask);
37694df7de0SSebastien Dugue 
37794df7de0SSebastien Dugue 		tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu :
37894df7de0SSebastien Dugue 			TICK_DO_TIMER_NONE;
37994df7de0SSebastien Dugue 	}
38094df7de0SSebastien Dugue }
38194df7de0SSebastien Dugue 
38294df7de0SSebastien Dugue /*
383906568c9SThomas Gleixner  * Shutdown an event device on a given cpu:
384906568c9SThomas Gleixner  *
385906568c9SThomas Gleixner  * This is called on a life CPU, when a CPU is dead. So we cannot
386906568c9SThomas Gleixner  * access the hardware device itself.
387906568c9SThomas Gleixner  * We just set the mode and remove it from the lists.
388906568c9SThomas Gleixner  */
389a49b116dSThomas Gleixner void tick_shutdown(unsigned int cpu)
390906568c9SThomas Gleixner {
391a49b116dSThomas Gleixner 	struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
392906568c9SThomas Gleixner 	struct clock_event_device *dev = td->evtdev;
393906568c9SThomas Gleixner 
394906568c9SThomas Gleixner 	td->mode = TICKDEV_MODE_PERIODIC;
395906568c9SThomas Gleixner 	if (dev) {
396906568c9SThomas Gleixner 		/*
397906568c9SThomas Gleixner 		 * Prevent that the clock events layer tries to call
398906568c9SThomas Gleixner 		 * the set mode function!
399906568c9SThomas Gleixner 		 */
400051ebd10SThomas Gleixner 		clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
401906568c9SThomas Gleixner 		clockevents_exchange_device(dev, NULL);
4026f7a05d7SThomas Gleixner 		dev->event_handler = clockevents_handle_noop;
403906568c9SThomas Gleixner 		td->evtdev = NULL;
404906568c9SThomas Gleixner 	}
405906568c9SThomas Gleixner }
406a49b116dSThomas Gleixner #endif
407906568c9SThomas Gleixner 
4084ffee521SThomas Gleixner /**
409f46481d0SThomas Gleixner  * tick_suspend_local - Suspend the local tick device
410f46481d0SThomas Gleixner  *
411f46481d0SThomas Gleixner  * Called from the local cpu for freeze with interrupts disabled.
412f46481d0SThomas Gleixner  *
413f46481d0SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
414f46481d0SThomas Gleixner  */
4157270d11cSThomas Gleixner void tick_suspend_local(void)
416f46481d0SThomas Gleixner {
417f46481d0SThomas Gleixner 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
418f46481d0SThomas Gleixner 
419f46481d0SThomas Gleixner 	clockevents_shutdown(td->evtdev);
420f46481d0SThomas Gleixner }
421f46481d0SThomas Gleixner 
422f46481d0SThomas Gleixner /**
423f46481d0SThomas Gleixner  * tick_resume_local - Resume the local tick device
424f46481d0SThomas Gleixner  *
425f46481d0SThomas Gleixner  * Called from the local CPU for unfreeze or XEN resume magic.
426f46481d0SThomas Gleixner  *
427f46481d0SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
428f46481d0SThomas Gleixner  */
429f46481d0SThomas Gleixner void tick_resume_local(void)
430f46481d0SThomas Gleixner {
431f46481d0SThomas Gleixner 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
432f46481d0SThomas Gleixner 	bool broadcast = tick_resume_check_broadcast();
433f46481d0SThomas Gleixner 
434f46481d0SThomas Gleixner 	clockevents_tick_resume(td->evtdev);
435f46481d0SThomas Gleixner 	if (!broadcast) {
436f46481d0SThomas Gleixner 		if (td->mode == TICKDEV_MODE_PERIODIC)
437f46481d0SThomas Gleixner 			tick_setup_periodic(td->evtdev, 0);
438f46481d0SThomas Gleixner 		else
439f46481d0SThomas Gleixner 			tick_resume_oneshot();
440f46481d0SThomas Gleixner 	}
441f46481d0SThomas Gleixner }
442f46481d0SThomas Gleixner 
443f46481d0SThomas Gleixner /**
4444ffee521SThomas Gleixner  * tick_suspend - Suspend the tick and the broadcast device
4454ffee521SThomas Gleixner  *
4464ffee521SThomas Gleixner  * Called from syscore_suspend() via timekeeping_suspend with only one
4474ffee521SThomas Gleixner  * CPU online and interrupts disabled or from tick_unfreeze() under
4484ffee521SThomas Gleixner  * tick_freeze_lock.
4494ffee521SThomas Gleixner  *
4504ffee521SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
4514ffee521SThomas Gleixner  */
4528c53daf6SThomas Gleixner void tick_suspend(void)
4536321dd60SThomas Gleixner {
454f46481d0SThomas Gleixner 	tick_suspend_local();
4554ffee521SThomas Gleixner 	tick_suspend_broadcast();
4566321dd60SThomas Gleixner }
4576321dd60SThomas Gleixner 
4584ffee521SThomas Gleixner /**
4594ffee521SThomas Gleixner  * tick_resume - Resume the tick and the broadcast device
4604ffee521SThomas Gleixner  *
4614ffee521SThomas Gleixner  * Called from syscore_resume() via timekeeping_resume with only one
462f46481d0SThomas Gleixner  * CPU online and interrupts disabled.
4634ffee521SThomas Gleixner  *
4644ffee521SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
4654ffee521SThomas Gleixner  */
4668c53daf6SThomas Gleixner void tick_resume(void)
4676321dd60SThomas Gleixner {
468f46481d0SThomas Gleixner 	tick_resume_broadcast();
469f46481d0SThomas Gleixner 	tick_resume_local();
4706321dd60SThomas Gleixner }
4716321dd60SThomas Gleixner 
47287e9b9f1SRafael J. Wysocki #ifdef CONFIG_SUSPEND
473124cf911SRafael J. Wysocki static DEFINE_RAW_SPINLOCK(tick_freeze_lock);
474124cf911SRafael J. Wysocki static unsigned int tick_freeze_depth;
475124cf911SRafael J. Wysocki 
476124cf911SRafael J. Wysocki /**
477124cf911SRafael J. Wysocki  * tick_freeze - Suspend the local tick and (possibly) timekeeping.
478124cf911SRafael J. Wysocki  *
479124cf911SRafael J. Wysocki  * Check if this is the last online CPU executing the function and if so,
480124cf911SRafael J. Wysocki  * suspend timekeeping.  Otherwise suspend the local tick.
481124cf911SRafael J. Wysocki  *
482124cf911SRafael J. Wysocki  * Call with interrupts disabled.  Must be balanced with %tick_unfreeze().
483124cf911SRafael J. Wysocki  * Interrupts must not be enabled before the subsequent %tick_unfreeze().
484124cf911SRafael J. Wysocki  */
485124cf911SRafael J. Wysocki void tick_freeze(void)
486124cf911SRafael J. Wysocki {
487124cf911SRafael J. Wysocki 	raw_spin_lock(&tick_freeze_lock);
488124cf911SRafael J. Wysocki 
489124cf911SRafael J. Wysocki 	tick_freeze_depth++;
49075e0678eSRafael J. Wysocki 	if (tick_freeze_depth == num_online_cpus()) {
49175e0678eSRafael J. Wysocki 		trace_suspend_resume(TPS("timekeeping_freeze"),
49275e0678eSRafael J. Wysocki 				     smp_processor_id(), true);
493c1a957d1SThomas Gleixner 		system_state = SYSTEM_SUSPEND;
494124cf911SRafael J. Wysocki 		timekeeping_suspend();
49575e0678eSRafael J. Wysocki 	} else {
496f46481d0SThomas Gleixner 		tick_suspend_local();
49775e0678eSRafael J. Wysocki 	}
498124cf911SRafael J. Wysocki 
499124cf911SRafael J. Wysocki 	raw_spin_unlock(&tick_freeze_lock);
500124cf911SRafael J. Wysocki }
501124cf911SRafael J. Wysocki 
502124cf911SRafael J. Wysocki /**
503124cf911SRafael J. Wysocki  * tick_unfreeze - Resume the local tick and (possibly) timekeeping.
504124cf911SRafael J. Wysocki  *
505124cf911SRafael J. Wysocki  * Check if this is the first CPU executing the function and if so, resume
506124cf911SRafael J. Wysocki  * timekeeping.  Otherwise resume the local tick.
507124cf911SRafael J. Wysocki  *
508124cf911SRafael J. Wysocki  * Call with interrupts disabled.  Must be balanced with %tick_freeze().
509124cf911SRafael J. Wysocki  * Interrupts must not be enabled after the preceding %tick_freeze().
510124cf911SRafael J. Wysocki  */
511124cf911SRafael J. Wysocki void tick_unfreeze(void)
512124cf911SRafael J. Wysocki {
513124cf911SRafael J. Wysocki 	raw_spin_lock(&tick_freeze_lock);
514124cf911SRafael J. Wysocki 
51575e0678eSRafael J. Wysocki 	if (tick_freeze_depth == num_online_cpus()) {
516124cf911SRafael J. Wysocki 		timekeeping_resume();
517c1a957d1SThomas Gleixner 		system_state = SYSTEM_RUNNING;
51875e0678eSRafael J. Wysocki 		trace_suspend_resume(TPS("timekeeping_freeze"),
51975e0678eSRafael J. Wysocki 				     smp_processor_id(), false);
52075e0678eSRafael J. Wysocki 	} else {
521422fe750SRafael J. Wysocki 		tick_resume_local();
52275e0678eSRafael J. Wysocki 	}
523124cf911SRafael J. Wysocki 
524124cf911SRafael J. Wysocki 	tick_freeze_depth--;
525124cf911SRafael J. Wysocki 
526124cf911SRafael J. Wysocki 	raw_spin_unlock(&tick_freeze_lock);
527124cf911SRafael J. Wysocki }
52887e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */
529124cf911SRafael J. Wysocki 
530906568c9SThomas Gleixner /**
531906568c9SThomas Gleixner  * tick_init - initialize the tick control
532906568c9SThomas Gleixner  */
533906568c9SThomas Gleixner void __init tick_init(void)
534906568c9SThomas Gleixner {
535b352bc1cSThomas Gleixner 	tick_broadcast_init();
536a80e49e2SFrederic Weisbecker 	tick_nohz_init();
537906568c9SThomas Gleixner }
538