xref: /openbmc/linux/kernel/time/tick-common.c (revision f46481d0)
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>
22906568c9SThomas Gleixner 
23d7b90689SRussell King #include <asm/irq_regs.h>
24d7b90689SRussell King 
25f8381cbaSThomas Gleixner #include "tick-internal.h"
26f8381cbaSThomas Gleixner 
27906568c9SThomas Gleixner /*
28906568c9SThomas Gleixner  * Tick devices
29906568c9SThomas Gleixner  */
30f8381cbaSThomas Gleixner DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
31906568c9SThomas Gleixner /*
32906568c9SThomas Gleixner  * Tick next event: keeps track of the tick time
33906568c9SThomas Gleixner  */
34f8381cbaSThomas Gleixner ktime_t tick_next_period;
35f8381cbaSThomas Gleixner ktime_t tick_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;
52906568c9SThomas Gleixner 
53289f480aSIngo Molnar /*
54289f480aSIngo Molnar  * Debugging: see timer_list.c
55289f480aSIngo Molnar  */
56289f480aSIngo Molnar struct tick_device *tick_get_device(int cpu)
57289f480aSIngo Molnar {
58289f480aSIngo Molnar 	return &per_cpu(tick_cpu_device, cpu);
59289f480aSIngo Molnar }
60289f480aSIngo Molnar 
6179bf2bb3SThomas Gleixner /**
6279bf2bb3SThomas Gleixner  * tick_is_oneshot_available - check for a oneshot capable event device
6379bf2bb3SThomas Gleixner  */
6479bf2bb3SThomas Gleixner int tick_is_oneshot_available(void)
6579bf2bb3SThomas Gleixner {
66909ea964SChristoph Lameter 	struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
6779bf2bb3SThomas Gleixner 
683a142a06SThomas Gleixner 	if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
693a142a06SThomas Gleixner 		return 0;
703a142a06SThomas Gleixner 	if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
713a142a06SThomas Gleixner 		return 1;
723a142a06SThomas Gleixner 	return tick_broadcast_oneshot_available();
7379bf2bb3SThomas Gleixner }
7479bf2bb3SThomas Gleixner 
75906568c9SThomas Gleixner /*
76906568c9SThomas Gleixner  * Periodic tick
77906568c9SThomas Gleixner  */
78906568c9SThomas Gleixner static void tick_periodic(int cpu)
79906568c9SThomas Gleixner {
80906568c9SThomas Gleixner 	if (tick_do_timer_cpu == cpu) {
81d6ad4187SJohn Stultz 		write_seqlock(&jiffies_lock);
82906568c9SThomas Gleixner 
83906568c9SThomas Gleixner 		/* Keep track of the next tick event */
84906568c9SThomas Gleixner 		tick_next_period = ktime_add(tick_next_period, tick_period);
85906568c9SThomas Gleixner 
86906568c9SThomas Gleixner 		do_timer(1);
87d6ad4187SJohn Stultz 		write_sequnlock(&jiffies_lock);
8847a1b796SJohn Stultz 		update_wall_time();
89906568c9SThomas Gleixner 	}
90906568c9SThomas Gleixner 
91906568c9SThomas Gleixner 	update_process_times(user_mode(get_irq_regs()));
92906568c9SThomas Gleixner 	profile_tick(CPU_PROFILING);
93906568c9SThomas Gleixner }
94906568c9SThomas Gleixner 
95906568c9SThomas Gleixner /*
96906568c9SThomas Gleixner  * Event handler for periodic ticks
97906568c9SThomas Gleixner  */
98906568c9SThomas Gleixner void tick_handle_periodic(struct clock_event_device *dev)
99906568c9SThomas Gleixner {
100906568c9SThomas Gleixner 	int cpu = smp_processor_id();
101b97f0291SViresh Kumar 	ktime_t next = dev->next_event;
102906568c9SThomas Gleixner 
103906568c9SThomas Gleixner 	tick_periodic(cpu);
104906568c9SThomas Gleixner 
10577e32c89SViresh Kumar 	if (dev->state != CLOCK_EVT_STATE_ONESHOT)
106906568c9SThomas Gleixner 		return;
107b97f0291SViresh Kumar 	for (;;) {
108906568c9SThomas Gleixner 		/*
109906568c9SThomas Gleixner 		 * Setup the next period for devices, which do not have
110906568c9SThomas Gleixner 		 * periodic mode:
111906568c9SThomas Gleixner 		 */
112b97f0291SViresh Kumar 		next = ktime_add(next, tick_period);
113b97f0291SViresh Kumar 
114d1748302SMartin Schwidefsky 		if (!clockevents_program_event(dev, next, false))
115906568c9SThomas Gleixner 			return;
11674a03b69Sjohn stultz 		/*
11774a03b69Sjohn stultz 		 * Have to be careful here. If we're in oneshot mode,
11874a03b69Sjohn stultz 		 * before we call tick_periodic() in a loop, we need
11974a03b69Sjohn stultz 		 * to be sure we're using a real hardware clocksource.
12074a03b69Sjohn stultz 		 * Otherwise we could get trapped in an infinite
12174a03b69Sjohn stultz 		 * loop, as the tick_periodic() increments jiffies,
122cacb3c76SViresh Kumar 		 * which then will increment time, possibly causing
12374a03b69Sjohn stultz 		 * the loop to trigger again and again.
12474a03b69Sjohn stultz 		 */
12574a03b69Sjohn stultz 		if (timekeeping_valid_for_hres())
126906568c9SThomas Gleixner 			tick_periodic(cpu);
127906568c9SThomas Gleixner 	}
128906568c9SThomas Gleixner }
129906568c9SThomas Gleixner 
130906568c9SThomas Gleixner /*
131906568c9SThomas Gleixner  * Setup the device for a periodic tick
132906568c9SThomas Gleixner  */
133f8381cbaSThomas Gleixner void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
134906568c9SThomas Gleixner {
135f8381cbaSThomas Gleixner 	tick_set_periodic_handler(dev, broadcast);
136f8381cbaSThomas Gleixner 
137f8381cbaSThomas Gleixner 	/* Broadcast setup ? */
138f8381cbaSThomas Gleixner 	if (!tick_device_is_functional(dev))
139f8381cbaSThomas Gleixner 		return;
140906568c9SThomas Gleixner 
14127ce4cb4SThomas Gleixner 	if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
14227ce4cb4SThomas Gleixner 	    !tick_broadcast_oneshot_active()) {
14377e32c89SViresh Kumar 		clockevents_set_state(dev, CLOCK_EVT_STATE_PERIODIC);
144906568c9SThomas Gleixner 	} else {
145906568c9SThomas Gleixner 		unsigned long seq;
146906568c9SThomas Gleixner 		ktime_t next;
147906568c9SThomas Gleixner 
148906568c9SThomas Gleixner 		do {
149d6ad4187SJohn Stultz 			seq = read_seqbegin(&jiffies_lock);
150906568c9SThomas Gleixner 			next = tick_next_period;
151d6ad4187SJohn Stultz 		} while (read_seqretry(&jiffies_lock, seq));
152906568c9SThomas Gleixner 
15377e32c89SViresh Kumar 		clockevents_set_state(dev, CLOCK_EVT_STATE_ONESHOT);
154906568c9SThomas Gleixner 
155906568c9SThomas Gleixner 		for (;;) {
156d1748302SMartin Schwidefsky 			if (!clockevents_program_event(dev, next, false))
157906568c9SThomas Gleixner 				return;
158906568c9SThomas Gleixner 			next = ktime_add(next, tick_period);
159906568c9SThomas Gleixner 		}
160906568c9SThomas Gleixner 	}
161906568c9SThomas Gleixner }
162906568c9SThomas Gleixner 
163906568c9SThomas Gleixner /*
164906568c9SThomas Gleixner  * Setup the tick device
165906568c9SThomas Gleixner  */
166906568c9SThomas Gleixner static void tick_setup_device(struct tick_device *td,
167906568c9SThomas Gleixner 			      struct clock_event_device *newdev, int cpu,
1680de26520SRusty Russell 			      const struct cpumask *cpumask)
169906568c9SThomas Gleixner {
170906568c9SThomas Gleixner 	ktime_t next_event;
171906568c9SThomas Gleixner 	void (*handler)(struct clock_event_device *) = NULL;
172906568c9SThomas Gleixner 
173906568c9SThomas Gleixner 	/*
174906568c9SThomas Gleixner 	 * First device setup ?
175906568c9SThomas Gleixner 	 */
176906568c9SThomas Gleixner 	if (!td->evtdev) {
177906568c9SThomas Gleixner 		/*
178906568c9SThomas Gleixner 		 * If no cpu took the do_timer update, assign it to
179906568c9SThomas Gleixner 		 * this cpu:
180906568c9SThomas Gleixner 		 */
1816441402bSThomas Gleixner 		if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) {
182c5bfece2SFrederic Weisbecker 			if (!tick_nohz_full_cpu(cpu))
183906568c9SThomas Gleixner 				tick_do_timer_cpu = cpu;
184a382bf93SFrederic Weisbecker 			else
185a382bf93SFrederic Weisbecker 				tick_do_timer_cpu = TICK_DO_TIMER_NONE;
186906568c9SThomas Gleixner 			tick_next_period = ktime_get();
187906568c9SThomas Gleixner 			tick_period = ktime_set(0, NSEC_PER_SEC / HZ);
188906568c9SThomas Gleixner 		}
189906568c9SThomas Gleixner 
190906568c9SThomas Gleixner 		/*
191906568c9SThomas Gleixner 		 * Startup in periodic mode first.
192906568c9SThomas Gleixner 		 */
193906568c9SThomas Gleixner 		td->mode = TICKDEV_MODE_PERIODIC;
194906568c9SThomas Gleixner 	} else {
195906568c9SThomas Gleixner 		handler = td->evtdev->event_handler;
196906568c9SThomas Gleixner 		next_event = td->evtdev->next_event;
1977c1e7689SVenkatesh Pallipadi 		td->evtdev->event_handler = clockevents_handle_noop;
198906568c9SThomas Gleixner 	}
199906568c9SThomas Gleixner 
200906568c9SThomas Gleixner 	td->evtdev = newdev;
201906568c9SThomas Gleixner 
202906568c9SThomas Gleixner 	/*
203906568c9SThomas Gleixner 	 * When the device is not per cpu, pin the interrupt to the
204906568c9SThomas Gleixner 	 * current cpu:
205906568c9SThomas Gleixner 	 */
206320ab2b0SRusty Russell 	if (!cpumask_equal(newdev->cpumask, cpumask))
2070de26520SRusty Russell 		irq_set_affinity(newdev->irq, cpumask);
208906568c9SThomas Gleixner 
209f8381cbaSThomas Gleixner 	/*
210f8381cbaSThomas Gleixner 	 * When global broadcasting is active, check if the current
211f8381cbaSThomas Gleixner 	 * device is registered as a placeholder for broadcast mode.
212f8381cbaSThomas Gleixner 	 * This allows us to handle this x86 misfeature in a generic
21307bd1172SThomas Gleixner 	 * way. This function also returns !=0 when we keep the
21407bd1172SThomas Gleixner 	 * current active broadcast state for this CPU.
215f8381cbaSThomas Gleixner 	 */
216f8381cbaSThomas Gleixner 	if (tick_device_uses_broadcast(newdev, cpu))
217f8381cbaSThomas Gleixner 		return;
218f8381cbaSThomas Gleixner 
219906568c9SThomas Gleixner 	if (td->mode == TICKDEV_MODE_PERIODIC)
220906568c9SThomas Gleixner 		tick_setup_periodic(newdev, 0);
22179bf2bb3SThomas Gleixner 	else
22279bf2bb3SThomas Gleixner 		tick_setup_oneshot(newdev, handler, next_event);
223906568c9SThomas Gleixner }
224906568c9SThomas Gleixner 
22503e13cf5SThomas Gleixner void tick_install_replacement(struct clock_event_device *newdev)
22603e13cf5SThomas Gleixner {
22722127e93SChristoph Lameter 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
22803e13cf5SThomas Gleixner 	int cpu = smp_processor_id();
22903e13cf5SThomas Gleixner 
23003e13cf5SThomas Gleixner 	clockevents_exchange_device(td->evtdev, newdev);
23103e13cf5SThomas Gleixner 	tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
23203e13cf5SThomas Gleixner 	if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
23303e13cf5SThomas Gleixner 		tick_oneshot_notify();
23403e13cf5SThomas Gleixner }
23503e13cf5SThomas Gleixner 
23645cb8e01SThomas Gleixner static bool tick_check_percpu(struct clock_event_device *curdev,
23745cb8e01SThomas Gleixner 			      struct clock_event_device *newdev, int cpu)
23845cb8e01SThomas Gleixner {
23945cb8e01SThomas Gleixner 	if (!cpumask_test_cpu(cpu, newdev->cpumask))
24045cb8e01SThomas Gleixner 		return false;
24145cb8e01SThomas Gleixner 	if (cpumask_equal(newdev->cpumask, cpumask_of(cpu)))
24245cb8e01SThomas Gleixner 		return true;
24345cb8e01SThomas Gleixner 	/* Check if irq affinity can be set */
24445cb8e01SThomas Gleixner 	if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq))
24545cb8e01SThomas Gleixner 		return false;
24645cb8e01SThomas Gleixner 	/* Prefer an existing cpu local device */
24745cb8e01SThomas Gleixner 	if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
24845cb8e01SThomas Gleixner 		return false;
24945cb8e01SThomas Gleixner 	return true;
25045cb8e01SThomas Gleixner }
25145cb8e01SThomas Gleixner 
25245cb8e01SThomas Gleixner static bool tick_check_preferred(struct clock_event_device *curdev,
25345cb8e01SThomas Gleixner 				 struct clock_event_device *newdev)
25445cb8e01SThomas Gleixner {
25545cb8e01SThomas Gleixner 	/* Prefer oneshot capable device */
25645cb8e01SThomas Gleixner 	if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) {
25745cb8e01SThomas Gleixner 		if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT))
25845cb8e01SThomas Gleixner 			return false;
25945cb8e01SThomas Gleixner 		if (tick_oneshot_mode_active())
26045cb8e01SThomas Gleixner 			return false;
26145cb8e01SThomas Gleixner 	}
26245cb8e01SThomas Gleixner 
26370e5975dSStephen Boyd 	/*
26470e5975dSStephen Boyd 	 * Use the higher rated one, but prefer a CPU local device with a lower
26570e5975dSStephen Boyd 	 * rating than a non-CPU local device
26670e5975dSStephen Boyd 	 */
26770e5975dSStephen Boyd 	return !curdev ||
26870e5975dSStephen Boyd 		newdev->rating > curdev->rating ||
26970e5975dSStephen Boyd 	       !cpumask_equal(curdev->cpumask, newdev->cpumask);
27045cb8e01SThomas Gleixner }
27145cb8e01SThomas Gleixner 
272906568c9SThomas Gleixner /*
27303e13cf5SThomas Gleixner  * Check whether the new device is a better fit than curdev. curdev
27403e13cf5SThomas Gleixner  * can be NULL !
27503e13cf5SThomas Gleixner  */
27603e13cf5SThomas Gleixner bool tick_check_replacement(struct clock_event_device *curdev,
27703e13cf5SThomas Gleixner 			    struct clock_event_device *newdev)
27803e13cf5SThomas Gleixner {
279521c4299SViresh Kumar 	if (!tick_check_percpu(curdev, newdev, smp_processor_id()))
28003e13cf5SThomas Gleixner 		return false;
28103e13cf5SThomas Gleixner 
28203e13cf5SThomas Gleixner 	return tick_check_preferred(curdev, newdev);
28303e13cf5SThomas Gleixner }
28403e13cf5SThomas Gleixner 
28503e13cf5SThomas Gleixner /*
2867126cac4SThomas Gleixner  * Check, if the new registered device should be used. Called with
2877126cac4SThomas Gleixner  * clockevents_lock held and interrupts disabled.
288906568c9SThomas Gleixner  */
2897172a286SThomas Gleixner void tick_check_new_device(struct clock_event_device *newdev)
290906568c9SThomas Gleixner {
291906568c9SThomas Gleixner 	struct clock_event_device *curdev;
292906568c9SThomas Gleixner 	struct tick_device *td;
2937172a286SThomas Gleixner 	int cpu;
294906568c9SThomas Gleixner 
295906568c9SThomas Gleixner 	cpu = smp_processor_id();
296320ab2b0SRusty Russell 	if (!cpumask_test_cpu(cpu, newdev->cpumask))
2974a93232dSVenki Pallipadi 		goto out_bc;
298906568c9SThomas Gleixner 
299906568c9SThomas Gleixner 	td = &per_cpu(tick_cpu_device, cpu);
300906568c9SThomas Gleixner 	curdev = td->evtdev;
301906568c9SThomas Gleixner 
302906568c9SThomas Gleixner 	/* cpu local device ? */
30345cb8e01SThomas Gleixner 	if (!tick_check_percpu(curdev, newdev, cpu))
304906568c9SThomas Gleixner 		goto out_bc;
305906568c9SThomas Gleixner 
30645cb8e01SThomas Gleixner 	/* Preference decision */
30745cb8e01SThomas Gleixner 	if (!tick_check_preferred(curdev, newdev))
308906568c9SThomas Gleixner 		goto out_bc;
309906568c9SThomas Gleixner 
310ccf33d68SThomas Gleixner 	if (!try_module_get(newdev->owner))
311ccf33d68SThomas Gleixner 		return;
312ccf33d68SThomas Gleixner 
313906568c9SThomas Gleixner 	/*
314906568c9SThomas Gleixner 	 * Replace the eventually existing device by the new
315f8381cbaSThomas Gleixner 	 * device. If the current device is the broadcast device, do
316f8381cbaSThomas Gleixner 	 * not give it back to the clockevents layer !
317906568c9SThomas Gleixner 	 */
318f8381cbaSThomas Gleixner 	if (tick_is_broadcast_device(curdev)) {
3192344abbcSThomas Gleixner 		clockevents_shutdown(curdev);
320f8381cbaSThomas Gleixner 		curdev = NULL;
321f8381cbaSThomas Gleixner 	}
322906568c9SThomas Gleixner 	clockevents_exchange_device(curdev, newdev);
3236b954823SRusty Russell 	tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
32479bf2bb3SThomas Gleixner 	if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
32579bf2bb3SThomas Gleixner 		tick_oneshot_notify();
3267172a286SThomas Gleixner 	return;
327f8381cbaSThomas Gleixner 
328f8381cbaSThomas Gleixner out_bc:
329f8381cbaSThomas Gleixner 	/*
330f8381cbaSThomas Gleixner 	 * Can the new device be used as a broadcast device ?
331f8381cbaSThomas Gleixner 	 */
3327172a286SThomas Gleixner 	tick_install_broadcast_device(newdev);
333906568c9SThomas Gleixner }
334906568c9SThomas Gleixner 
335906568c9SThomas Gleixner /*
33694df7de0SSebastien Dugue  * Transfer the do_timer job away from a dying cpu.
33794df7de0SSebastien Dugue  *
33894df7de0SSebastien Dugue  * Called with interrupts disabled.
33994df7de0SSebastien Dugue  */
3408c53daf6SThomas Gleixner void tick_handover_do_timer(int *cpup)
34194df7de0SSebastien Dugue {
34294df7de0SSebastien Dugue 	if (*cpup == tick_do_timer_cpu) {
34394df7de0SSebastien Dugue 		int cpu = cpumask_first(cpu_online_mask);
34494df7de0SSebastien Dugue 
34594df7de0SSebastien Dugue 		tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu :
34694df7de0SSebastien Dugue 			TICK_DO_TIMER_NONE;
34794df7de0SSebastien Dugue 	}
34894df7de0SSebastien Dugue }
34994df7de0SSebastien Dugue 
35094df7de0SSebastien Dugue /*
351906568c9SThomas Gleixner  * Shutdown an event device on a given cpu:
352906568c9SThomas Gleixner  *
353906568c9SThomas Gleixner  * This is called on a life CPU, when a CPU is dead. So we cannot
354906568c9SThomas Gleixner  * access the hardware device itself.
355906568c9SThomas Gleixner  * We just set the mode and remove it from the lists.
356906568c9SThomas Gleixner  */
3578c53daf6SThomas Gleixner void tick_shutdown(unsigned int *cpup)
358906568c9SThomas Gleixner {
359906568c9SThomas Gleixner 	struct tick_device *td = &per_cpu(tick_cpu_device, *cpup);
360906568c9SThomas Gleixner 	struct clock_event_device *dev = td->evtdev;
361906568c9SThomas Gleixner 
362906568c9SThomas Gleixner 	td->mode = TICKDEV_MODE_PERIODIC;
363906568c9SThomas Gleixner 	if (dev) {
364906568c9SThomas Gleixner 		/*
365906568c9SThomas Gleixner 		 * Prevent that the clock events layer tries to call
366906568c9SThomas Gleixner 		 * the set mode function!
367906568c9SThomas Gleixner 		 */
36877e32c89SViresh Kumar 		dev->state = CLOCK_EVT_STATE_DETACHED;
369906568c9SThomas Gleixner 		dev->mode = CLOCK_EVT_MODE_UNUSED;
370906568c9SThomas Gleixner 		clockevents_exchange_device(dev, NULL);
3716f7a05d7SThomas Gleixner 		dev->event_handler = clockevents_handle_noop;
372906568c9SThomas Gleixner 		td->evtdev = NULL;
373906568c9SThomas Gleixner 	}
374906568c9SThomas Gleixner }
375906568c9SThomas Gleixner 
3764ffee521SThomas Gleixner /**
377f46481d0SThomas Gleixner  * tick_suspend_local - Suspend the local tick device
378f46481d0SThomas Gleixner  *
379f46481d0SThomas Gleixner  * Called from the local cpu for freeze with interrupts disabled.
380f46481d0SThomas Gleixner  *
381f46481d0SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
382f46481d0SThomas Gleixner  */
383f46481d0SThomas Gleixner static void tick_suspend_local(void)
384f46481d0SThomas Gleixner {
385f46481d0SThomas Gleixner 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
386f46481d0SThomas Gleixner 
387f46481d0SThomas Gleixner 	clockevents_shutdown(td->evtdev);
388f46481d0SThomas Gleixner }
389f46481d0SThomas Gleixner 
390f46481d0SThomas Gleixner /**
391f46481d0SThomas Gleixner  * tick_resume_local - Resume the local tick device
392f46481d0SThomas Gleixner  *
393f46481d0SThomas Gleixner  * Called from the local CPU for unfreeze or XEN resume magic.
394f46481d0SThomas Gleixner  *
395f46481d0SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
396f46481d0SThomas Gleixner  */
397f46481d0SThomas Gleixner void tick_resume_local(void)
398f46481d0SThomas Gleixner {
399f46481d0SThomas Gleixner 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
400f46481d0SThomas Gleixner 	bool broadcast = tick_resume_check_broadcast();
401f46481d0SThomas Gleixner 
402f46481d0SThomas Gleixner 	clockevents_tick_resume(td->evtdev);
403f46481d0SThomas Gleixner 	if (!broadcast) {
404f46481d0SThomas Gleixner 		if (td->mode == TICKDEV_MODE_PERIODIC)
405f46481d0SThomas Gleixner 			tick_setup_periodic(td->evtdev, 0);
406f46481d0SThomas Gleixner 		else
407f46481d0SThomas Gleixner 			tick_resume_oneshot();
408f46481d0SThomas Gleixner 	}
409f46481d0SThomas Gleixner }
410f46481d0SThomas Gleixner 
411f46481d0SThomas Gleixner /**
4124ffee521SThomas Gleixner  * tick_suspend - Suspend the tick and the broadcast device
4134ffee521SThomas Gleixner  *
4144ffee521SThomas Gleixner  * Called from syscore_suspend() via timekeeping_suspend with only one
4154ffee521SThomas Gleixner  * CPU online and interrupts disabled or from tick_unfreeze() under
4164ffee521SThomas Gleixner  * tick_freeze_lock.
4174ffee521SThomas Gleixner  *
4184ffee521SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
4194ffee521SThomas Gleixner  */
4208c53daf6SThomas Gleixner void tick_suspend(void)
4216321dd60SThomas Gleixner {
422f46481d0SThomas Gleixner 	tick_suspend_local();
4234ffee521SThomas Gleixner 	tick_suspend_broadcast();
4246321dd60SThomas Gleixner }
4256321dd60SThomas Gleixner 
4264ffee521SThomas Gleixner /**
4274ffee521SThomas Gleixner  * tick_resume - Resume the tick and the broadcast device
4284ffee521SThomas Gleixner  *
4294ffee521SThomas Gleixner  * Called from syscore_resume() via timekeeping_resume with only one
430f46481d0SThomas Gleixner  * CPU online and interrupts disabled.
4314ffee521SThomas Gleixner  *
4324ffee521SThomas Gleixner  * No locks required. Nothing can change the per cpu device.
4334ffee521SThomas Gleixner  */
4348c53daf6SThomas Gleixner void tick_resume(void)
4356321dd60SThomas Gleixner {
436f46481d0SThomas Gleixner 	tick_resume_broadcast();
437f46481d0SThomas Gleixner 	tick_resume_local();
4386321dd60SThomas Gleixner }
4396321dd60SThomas Gleixner 
440124cf911SRafael J. Wysocki static DEFINE_RAW_SPINLOCK(tick_freeze_lock);
441124cf911SRafael J. Wysocki static unsigned int tick_freeze_depth;
442124cf911SRafael J. Wysocki 
443124cf911SRafael J. Wysocki /**
444124cf911SRafael J. Wysocki  * tick_freeze - Suspend the local tick and (possibly) timekeeping.
445124cf911SRafael J. Wysocki  *
446124cf911SRafael J. Wysocki  * Check if this is the last online CPU executing the function and if so,
447124cf911SRafael J. Wysocki  * suspend timekeeping.  Otherwise suspend the local tick.
448124cf911SRafael J. Wysocki  *
449124cf911SRafael J. Wysocki  * Call with interrupts disabled.  Must be balanced with %tick_unfreeze().
450124cf911SRafael J. Wysocki  * Interrupts must not be enabled before the subsequent %tick_unfreeze().
451124cf911SRafael J. Wysocki  */
452124cf911SRafael J. Wysocki void tick_freeze(void)
453124cf911SRafael J. Wysocki {
454124cf911SRafael J. Wysocki 	raw_spin_lock(&tick_freeze_lock);
455124cf911SRafael J. Wysocki 
456124cf911SRafael J. Wysocki 	tick_freeze_depth++;
457124cf911SRafael J. Wysocki 	if (tick_freeze_depth == num_online_cpus()) {
458124cf911SRafael J. Wysocki 		timekeeping_suspend();
459124cf911SRafael J. Wysocki 	} else {
460f46481d0SThomas Gleixner 		tick_suspend_local();
461124cf911SRafael J. Wysocki 	}
462124cf911SRafael J. Wysocki 
463124cf911SRafael J. Wysocki 	raw_spin_unlock(&tick_freeze_lock);
464124cf911SRafael J. Wysocki }
465124cf911SRafael J. Wysocki 
466124cf911SRafael J. Wysocki /**
467124cf911SRafael J. Wysocki  * tick_unfreeze - Resume the local tick and (possibly) timekeeping.
468124cf911SRafael J. Wysocki  *
469124cf911SRafael J. Wysocki  * Check if this is the first CPU executing the function and if so, resume
470124cf911SRafael J. Wysocki  * timekeeping.  Otherwise resume the local tick.
471124cf911SRafael J. Wysocki  *
472124cf911SRafael J. Wysocki  * Call with interrupts disabled.  Must be balanced with %tick_freeze().
473124cf911SRafael J. Wysocki  * Interrupts must not be enabled after the preceding %tick_freeze().
474124cf911SRafael J. Wysocki  */
475124cf911SRafael J. Wysocki void tick_unfreeze(void)
476124cf911SRafael J. Wysocki {
477124cf911SRafael J. Wysocki 	raw_spin_lock(&tick_freeze_lock);
478124cf911SRafael J. Wysocki 
479124cf911SRafael J. Wysocki 	if (tick_freeze_depth == num_online_cpus())
480124cf911SRafael J. Wysocki 		timekeeping_resume();
481124cf911SRafael J. Wysocki 	else
482124cf911SRafael J. Wysocki 		tick_resume();
483124cf911SRafael J. Wysocki 
484124cf911SRafael J. Wysocki 	tick_freeze_depth--;
485124cf911SRafael J. Wysocki 
486124cf911SRafael J. Wysocki 	raw_spin_unlock(&tick_freeze_lock);
487124cf911SRafael J. Wysocki }
488124cf911SRafael J. Wysocki 
489906568c9SThomas Gleixner /**
490906568c9SThomas Gleixner  * tick_init - initialize the tick control
491906568c9SThomas Gleixner  */
492906568c9SThomas Gleixner void __init tick_init(void)
493906568c9SThomas Gleixner {
494b352bc1cSThomas Gleixner 	tick_broadcast_init();
495a80e49e2SFrederic Weisbecker 	tick_nohz_init();
496906568c9SThomas Gleixner }
497