xref: /openbmc/linux/kernel/time/tick-common.c (revision 8c53daf6)
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>
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 /*
31906568c9SThomas Gleixner  * Tick next event: keeps track of the tick time
32906568c9SThomas Gleixner  */
33f8381cbaSThomas Gleixner ktime_t tick_next_period;
34f8381cbaSThomas Gleixner ktime_t tick_period;
356441402bSThomas Gleixner int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT;
36906568c9SThomas Gleixner 
37289f480aSIngo Molnar /*
38289f480aSIngo Molnar  * Debugging: see timer_list.c
39289f480aSIngo Molnar  */
40289f480aSIngo Molnar struct tick_device *tick_get_device(int cpu)
41289f480aSIngo Molnar {
42289f480aSIngo Molnar 	return &per_cpu(tick_cpu_device, cpu);
43289f480aSIngo Molnar }
44289f480aSIngo Molnar 
4579bf2bb3SThomas Gleixner /**
4679bf2bb3SThomas Gleixner  * tick_is_oneshot_available - check for a oneshot capable event device
4779bf2bb3SThomas Gleixner  */
4879bf2bb3SThomas Gleixner int tick_is_oneshot_available(void)
4979bf2bb3SThomas Gleixner {
50909ea964SChristoph Lameter 	struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
5179bf2bb3SThomas Gleixner 
523a142a06SThomas Gleixner 	if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
533a142a06SThomas Gleixner 		return 0;
543a142a06SThomas Gleixner 	if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
553a142a06SThomas Gleixner 		return 1;
563a142a06SThomas Gleixner 	return tick_broadcast_oneshot_available();
5779bf2bb3SThomas Gleixner }
5879bf2bb3SThomas Gleixner 
59906568c9SThomas Gleixner /*
60906568c9SThomas Gleixner  * Periodic tick
61906568c9SThomas Gleixner  */
62906568c9SThomas Gleixner static void tick_periodic(int cpu)
63906568c9SThomas Gleixner {
64906568c9SThomas Gleixner 	if (tick_do_timer_cpu == cpu) {
65d6ad4187SJohn Stultz 		write_seqlock(&jiffies_lock);
66906568c9SThomas Gleixner 
67906568c9SThomas Gleixner 		/* Keep track of the next tick event */
68906568c9SThomas Gleixner 		tick_next_period = ktime_add(tick_next_period, tick_period);
69906568c9SThomas Gleixner 
70906568c9SThomas Gleixner 		do_timer(1);
71d6ad4187SJohn Stultz 		write_sequnlock(&jiffies_lock);
72906568c9SThomas Gleixner 	}
73906568c9SThomas Gleixner 
74906568c9SThomas Gleixner 	update_process_times(user_mode(get_irq_regs()));
75906568c9SThomas Gleixner 	profile_tick(CPU_PROFILING);
76906568c9SThomas Gleixner }
77906568c9SThomas Gleixner 
78906568c9SThomas Gleixner /*
79906568c9SThomas Gleixner  * Event handler for periodic ticks
80906568c9SThomas Gleixner  */
81906568c9SThomas Gleixner void tick_handle_periodic(struct clock_event_device *dev)
82906568c9SThomas Gleixner {
83906568c9SThomas Gleixner 	int cpu = smp_processor_id();
843494c166SDavid S. Miller 	ktime_t next;
85906568c9SThomas Gleixner 
86906568c9SThomas Gleixner 	tick_periodic(cpu);
87906568c9SThomas Gleixner 
88906568c9SThomas Gleixner 	if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
89906568c9SThomas Gleixner 		return;
90906568c9SThomas Gleixner 	/*
91906568c9SThomas Gleixner 	 * Setup the next period for devices, which do not have
92906568c9SThomas Gleixner 	 * periodic mode:
93906568c9SThomas Gleixner 	 */
943494c166SDavid S. Miller 	next = ktime_add(dev->next_event, tick_period);
95906568c9SThomas Gleixner 	for (;;) {
96d1748302SMartin Schwidefsky 		if (!clockevents_program_event(dev, next, false))
97906568c9SThomas Gleixner 			return;
9874a03b69Sjohn stultz 		/*
9974a03b69Sjohn stultz 		 * Have to be careful here. If we're in oneshot mode,
10074a03b69Sjohn stultz 		 * before we call tick_periodic() in a loop, we need
10174a03b69Sjohn stultz 		 * to be sure we're using a real hardware clocksource.
10274a03b69Sjohn stultz 		 * Otherwise we could get trapped in an infinite
10374a03b69Sjohn stultz 		 * loop, as the tick_periodic() increments jiffies,
10474a03b69Sjohn stultz 		 * when then will increment time, posibly causing
10574a03b69Sjohn stultz 		 * the loop to trigger again and again.
10674a03b69Sjohn stultz 		 */
10774a03b69Sjohn stultz 		if (timekeeping_valid_for_hres())
108906568c9SThomas Gleixner 			tick_periodic(cpu);
1093494c166SDavid S. Miller 		next = ktime_add(next, tick_period);
110906568c9SThomas Gleixner 	}
111906568c9SThomas Gleixner }
112906568c9SThomas Gleixner 
113906568c9SThomas Gleixner /*
114906568c9SThomas Gleixner  * Setup the device for a periodic tick
115906568c9SThomas Gleixner  */
116f8381cbaSThomas Gleixner void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
117906568c9SThomas Gleixner {
118f8381cbaSThomas Gleixner 	tick_set_periodic_handler(dev, broadcast);
119f8381cbaSThomas Gleixner 
120f8381cbaSThomas Gleixner 	/* Broadcast setup ? */
121f8381cbaSThomas Gleixner 	if (!tick_device_is_functional(dev))
122f8381cbaSThomas Gleixner 		return;
123906568c9SThomas Gleixner 
12427ce4cb4SThomas Gleixner 	if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
12527ce4cb4SThomas Gleixner 	    !tick_broadcast_oneshot_active()) {
126906568c9SThomas Gleixner 		clockevents_set_mode(dev, CLOCK_EVT_MODE_PERIODIC);
127906568c9SThomas Gleixner 	} else {
128906568c9SThomas Gleixner 		unsigned long seq;
129906568c9SThomas Gleixner 		ktime_t next;
130906568c9SThomas Gleixner 
131906568c9SThomas Gleixner 		do {
132d6ad4187SJohn Stultz 			seq = read_seqbegin(&jiffies_lock);
133906568c9SThomas Gleixner 			next = tick_next_period;
134d6ad4187SJohn Stultz 		} while (read_seqretry(&jiffies_lock, seq));
135906568c9SThomas Gleixner 
136906568c9SThomas Gleixner 		clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
137906568c9SThomas Gleixner 
138906568c9SThomas Gleixner 		for (;;) {
139d1748302SMartin Schwidefsky 			if (!clockevents_program_event(dev, next, false))
140906568c9SThomas Gleixner 				return;
141906568c9SThomas Gleixner 			next = ktime_add(next, tick_period);
142906568c9SThomas Gleixner 		}
143906568c9SThomas Gleixner 	}
144906568c9SThomas Gleixner }
145906568c9SThomas Gleixner 
146906568c9SThomas Gleixner /*
147906568c9SThomas Gleixner  * Setup the tick device
148906568c9SThomas Gleixner  */
149906568c9SThomas Gleixner static void tick_setup_device(struct tick_device *td,
150906568c9SThomas Gleixner 			      struct clock_event_device *newdev, int cpu,
1510de26520SRusty Russell 			      const struct cpumask *cpumask)
152906568c9SThomas Gleixner {
153906568c9SThomas Gleixner 	ktime_t next_event;
154906568c9SThomas Gleixner 	void (*handler)(struct clock_event_device *) = NULL;
155906568c9SThomas Gleixner 
156906568c9SThomas Gleixner 	/*
157906568c9SThomas Gleixner 	 * First device setup ?
158906568c9SThomas Gleixner 	 */
159906568c9SThomas Gleixner 	if (!td->evtdev) {
160906568c9SThomas Gleixner 		/*
161906568c9SThomas Gleixner 		 * If no cpu took the do_timer update, assign it to
162906568c9SThomas Gleixner 		 * this cpu:
163906568c9SThomas Gleixner 		 */
1646441402bSThomas Gleixner 		if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) {
165c5bfece2SFrederic Weisbecker 			if (!tick_nohz_full_cpu(cpu))
166906568c9SThomas Gleixner 				tick_do_timer_cpu = cpu;
167a382bf93SFrederic Weisbecker 			else
168a382bf93SFrederic Weisbecker 				tick_do_timer_cpu = TICK_DO_TIMER_NONE;
169906568c9SThomas Gleixner 			tick_next_period = ktime_get();
170906568c9SThomas Gleixner 			tick_period = ktime_set(0, NSEC_PER_SEC / HZ);
171906568c9SThomas Gleixner 		}
172906568c9SThomas Gleixner 
173906568c9SThomas Gleixner 		/*
174906568c9SThomas Gleixner 		 * Startup in periodic mode first.
175906568c9SThomas Gleixner 		 */
176906568c9SThomas Gleixner 		td->mode = TICKDEV_MODE_PERIODIC;
177906568c9SThomas Gleixner 	} else {
178906568c9SThomas Gleixner 		handler = td->evtdev->event_handler;
179906568c9SThomas Gleixner 		next_event = td->evtdev->next_event;
1807c1e7689SVenkatesh Pallipadi 		td->evtdev->event_handler = clockevents_handle_noop;
181906568c9SThomas Gleixner 	}
182906568c9SThomas Gleixner 
183906568c9SThomas Gleixner 	td->evtdev = newdev;
184906568c9SThomas Gleixner 
185906568c9SThomas Gleixner 	/*
186906568c9SThomas Gleixner 	 * When the device is not per cpu, pin the interrupt to the
187906568c9SThomas Gleixner 	 * current cpu:
188906568c9SThomas Gleixner 	 */
189320ab2b0SRusty Russell 	if (!cpumask_equal(newdev->cpumask, cpumask))
1900de26520SRusty Russell 		irq_set_affinity(newdev->irq, cpumask);
191906568c9SThomas Gleixner 
192f8381cbaSThomas Gleixner 	/*
193f8381cbaSThomas Gleixner 	 * When global broadcasting is active, check if the current
194f8381cbaSThomas Gleixner 	 * device is registered as a placeholder for broadcast mode.
195f8381cbaSThomas Gleixner 	 * This allows us to handle this x86 misfeature in a generic
196f8381cbaSThomas Gleixner 	 * way.
197f8381cbaSThomas Gleixner 	 */
198f8381cbaSThomas Gleixner 	if (tick_device_uses_broadcast(newdev, cpu))
199f8381cbaSThomas Gleixner 		return;
200f8381cbaSThomas Gleixner 
201906568c9SThomas Gleixner 	if (td->mode == TICKDEV_MODE_PERIODIC)
202906568c9SThomas Gleixner 		tick_setup_periodic(newdev, 0);
20379bf2bb3SThomas Gleixner 	else
20479bf2bb3SThomas Gleixner 		tick_setup_oneshot(newdev, handler, next_event);
205906568c9SThomas Gleixner }
206906568c9SThomas Gleixner 
207906568c9SThomas Gleixner /*
2087126cac4SThomas Gleixner  * Check, if the new registered device should be used. Called with
2097126cac4SThomas Gleixner  * clockevents_lock held and interrupts disabled.
210906568c9SThomas Gleixner  */
2117172a286SThomas Gleixner void tick_check_new_device(struct clock_event_device *newdev)
212906568c9SThomas Gleixner {
213906568c9SThomas Gleixner 	struct clock_event_device *curdev;
214906568c9SThomas Gleixner 	struct tick_device *td;
2157172a286SThomas Gleixner 	int cpu;
216906568c9SThomas Gleixner 
217906568c9SThomas Gleixner 	cpu = smp_processor_id();
218320ab2b0SRusty Russell 	if (!cpumask_test_cpu(cpu, newdev->cpumask))
2194a93232dSVenki Pallipadi 		goto out_bc;
220906568c9SThomas Gleixner 
221906568c9SThomas Gleixner 	td = &per_cpu(tick_cpu_device, cpu);
222906568c9SThomas Gleixner 	curdev = td->evtdev;
223906568c9SThomas Gleixner 
224906568c9SThomas Gleixner 	/* cpu local device ? */
225320ab2b0SRusty Russell 	if (!cpumask_equal(newdev->cpumask, cpumask_of(cpu))) {
226906568c9SThomas Gleixner 
227906568c9SThomas Gleixner 		/*
228906568c9SThomas Gleixner 		 * If the cpu affinity of the device interrupt can not
229906568c9SThomas Gleixner 		 * be set, ignore it.
230906568c9SThomas Gleixner 		 */
231906568c9SThomas Gleixner 		if (!irq_can_set_affinity(newdev->irq))
232906568c9SThomas Gleixner 			goto out_bc;
233906568c9SThomas Gleixner 
234906568c9SThomas Gleixner 		/*
235906568c9SThomas Gleixner 		 * If we have a cpu local device already, do not replace it
236906568c9SThomas Gleixner 		 * by a non cpu local device
237906568c9SThomas Gleixner 		 */
238320ab2b0SRusty Russell 		if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
239906568c9SThomas Gleixner 			goto out_bc;
240906568c9SThomas Gleixner 	}
241906568c9SThomas Gleixner 
242906568c9SThomas Gleixner 	/*
243906568c9SThomas Gleixner 	 * If we have an active device, then check the rating and the oneshot
244906568c9SThomas Gleixner 	 * feature.
245906568c9SThomas Gleixner 	 */
246906568c9SThomas Gleixner 	if (curdev) {
247906568c9SThomas Gleixner 		/*
24879bf2bb3SThomas Gleixner 		 * Prefer one shot capable devices !
24979bf2bb3SThomas Gleixner 		 */
25079bf2bb3SThomas Gleixner 		if ((curdev->features & CLOCK_EVT_FEAT_ONESHOT) &&
25179bf2bb3SThomas Gleixner 		    !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
25279bf2bb3SThomas Gleixner 			goto out_bc;
25379bf2bb3SThomas Gleixner 		/*
254906568c9SThomas Gleixner 		 * Check the rating
255906568c9SThomas Gleixner 		 */
256906568c9SThomas Gleixner 		if (curdev->rating >= newdev->rating)
257f8381cbaSThomas Gleixner 			goto out_bc;
258906568c9SThomas Gleixner 	}
259906568c9SThomas Gleixner 
260906568c9SThomas Gleixner 	/*
261906568c9SThomas Gleixner 	 * Replace the eventually existing device by the new
262f8381cbaSThomas Gleixner 	 * device. If the current device is the broadcast device, do
263f8381cbaSThomas Gleixner 	 * not give it back to the clockevents layer !
264906568c9SThomas Gleixner 	 */
265f8381cbaSThomas Gleixner 	if (tick_is_broadcast_device(curdev)) {
2662344abbcSThomas Gleixner 		clockevents_shutdown(curdev);
267f8381cbaSThomas Gleixner 		curdev = NULL;
268f8381cbaSThomas Gleixner 	}
269906568c9SThomas Gleixner 	clockevents_exchange_device(curdev, newdev);
2706b954823SRusty Russell 	tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
27179bf2bb3SThomas Gleixner 	if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
27279bf2bb3SThomas Gleixner 		tick_oneshot_notify();
2737172a286SThomas Gleixner 	return;
274f8381cbaSThomas Gleixner 
275f8381cbaSThomas Gleixner out_bc:
276f8381cbaSThomas Gleixner 	/*
277f8381cbaSThomas Gleixner 	 * Can the new device be used as a broadcast device ?
278f8381cbaSThomas Gleixner 	 */
2797172a286SThomas Gleixner 	tick_install_broadcast_device(newdev);
280906568c9SThomas Gleixner }
281906568c9SThomas Gleixner 
282906568c9SThomas Gleixner /*
28394df7de0SSebastien Dugue  * Transfer the do_timer job away from a dying cpu.
28494df7de0SSebastien Dugue  *
28594df7de0SSebastien Dugue  * Called with interrupts disabled.
28694df7de0SSebastien Dugue  */
2878c53daf6SThomas Gleixner void tick_handover_do_timer(int *cpup)
28894df7de0SSebastien Dugue {
28994df7de0SSebastien Dugue 	if (*cpup == tick_do_timer_cpu) {
29094df7de0SSebastien Dugue 		int cpu = cpumask_first(cpu_online_mask);
29194df7de0SSebastien Dugue 
29294df7de0SSebastien Dugue 		tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu :
29394df7de0SSebastien Dugue 			TICK_DO_TIMER_NONE;
29494df7de0SSebastien Dugue 	}
29594df7de0SSebastien Dugue }
29694df7de0SSebastien Dugue 
29794df7de0SSebastien Dugue /*
298906568c9SThomas Gleixner  * Shutdown an event device on a given cpu:
299906568c9SThomas Gleixner  *
300906568c9SThomas Gleixner  * This is called on a life CPU, when a CPU is dead. So we cannot
301906568c9SThomas Gleixner  * access the hardware device itself.
302906568c9SThomas Gleixner  * We just set the mode and remove it from the lists.
303906568c9SThomas Gleixner  */
3048c53daf6SThomas Gleixner void tick_shutdown(unsigned int *cpup)
305906568c9SThomas Gleixner {
306906568c9SThomas Gleixner 	struct tick_device *td = &per_cpu(tick_cpu_device, *cpup);
307906568c9SThomas Gleixner 	struct clock_event_device *dev = td->evtdev;
308906568c9SThomas Gleixner 
309906568c9SThomas Gleixner 	td->mode = TICKDEV_MODE_PERIODIC;
310906568c9SThomas Gleixner 	if (dev) {
311906568c9SThomas Gleixner 		/*
312906568c9SThomas Gleixner 		 * Prevent that the clock events layer tries to call
313906568c9SThomas Gleixner 		 * the set mode function!
314906568c9SThomas Gleixner 		 */
315906568c9SThomas Gleixner 		dev->mode = CLOCK_EVT_MODE_UNUSED;
316906568c9SThomas Gleixner 		clockevents_exchange_device(dev, NULL);
3176f7a05d7SThomas Gleixner 		dev->event_handler = clockevents_handle_noop;
318906568c9SThomas Gleixner 		td->evtdev = NULL;
319906568c9SThomas Gleixner 	}
320906568c9SThomas Gleixner }
321906568c9SThomas Gleixner 
3228c53daf6SThomas Gleixner void tick_suspend(void)
3236321dd60SThomas Gleixner {
3246321dd60SThomas Gleixner 	struct tick_device *td = &__get_cpu_var(tick_cpu_device);
3256321dd60SThomas Gleixner 
3262344abbcSThomas Gleixner 	clockevents_shutdown(td->evtdev);
3276321dd60SThomas Gleixner }
3286321dd60SThomas Gleixner 
3298c53daf6SThomas Gleixner void tick_resume(void)
3306321dd60SThomas Gleixner {
3316321dd60SThomas Gleixner 	struct tick_device *td = &__get_cpu_var(tick_cpu_device);
33218de5bc4SThomas Gleixner 	int broadcast = tick_resume_broadcast();
3336321dd60SThomas Gleixner 
33418de5bc4SThomas Gleixner 	clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_RESUME);
33518de5bc4SThomas Gleixner 
33618de5bc4SThomas Gleixner 	if (!broadcast) {
3376321dd60SThomas Gleixner 		if (td->mode == TICKDEV_MODE_PERIODIC)
3386321dd60SThomas Gleixner 			tick_setup_periodic(td->evtdev, 0);
339cd05a1f8SThomas Gleixner 		else
340cd05a1f8SThomas Gleixner 			tick_resume_oneshot();
34118de5bc4SThomas Gleixner 	}
3426321dd60SThomas Gleixner }
3436321dd60SThomas Gleixner 
344906568c9SThomas Gleixner /**
345906568c9SThomas Gleixner  * tick_init - initialize the tick control
346906568c9SThomas Gleixner  */
347906568c9SThomas Gleixner void __init tick_init(void)
348906568c9SThomas Gleixner {
349b352bc1cSThomas Gleixner 	tick_broadcast_init();
350906568c9SThomas Gleixner }
351