xref: /openbmc/linux/kernel/time/tick-common.c (revision 289f480af87e45f7a6de6ba9b4c061c2e259fe98)
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>
17906568c9SThomas Gleixner #include <linux/irq.h>
18906568c9SThomas Gleixner #include <linux/percpu.h>
19906568c9SThomas Gleixner #include <linux/profile.h>
20906568c9SThomas Gleixner #include <linux/sched.h>
21906568c9SThomas Gleixner #include <linux/tick.h>
22906568c9SThomas Gleixner 
23f8381cbaSThomas Gleixner #include "tick-internal.h"
24f8381cbaSThomas Gleixner 
25906568c9SThomas Gleixner /*
26906568c9SThomas Gleixner  * Tick devices
27906568c9SThomas Gleixner  */
28f8381cbaSThomas Gleixner DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
29906568c9SThomas Gleixner /*
30906568c9SThomas Gleixner  * Tick next event: keeps track of the tick time
31906568c9SThomas Gleixner  */
32f8381cbaSThomas Gleixner ktime_t tick_next_period;
33f8381cbaSThomas Gleixner ktime_t tick_period;
34906568c9SThomas Gleixner static int tick_do_timer_cpu = -1;
35f8381cbaSThomas Gleixner DEFINE_SPINLOCK(tick_device_lock);
36906568c9SThomas Gleixner 
37*289f480aSIngo Molnar /*
38*289f480aSIngo Molnar  * Debugging: see timer_list.c
39*289f480aSIngo Molnar  */
40*289f480aSIngo Molnar struct tick_device *tick_get_device(int cpu)
41*289f480aSIngo Molnar {
42*289f480aSIngo Molnar 	return &per_cpu(tick_cpu_device, cpu);
43*289f480aSIngo Molnar }
44*289f480aSIngo 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 {
5079bf2bb3SThomas Gleixner 	struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
5179bf2bb3SThomas Gleixner 
5279bf2bb3SThomas Gleixner 	return dev && (dev->features & CLOCK_EVT_FEAT_ONESHOT);
5379bf2bb3SThomas Gleixner }
5479bf2bb3SThomas Gleixner 
55906568c9SThomas Gleixner /*
56906568c9SThomas Gleixner  * Periodic tick
57906568c9SThomas Gleixner  */
58906568c9SThomas Gleixner static void tick_periodic(int cpu)
59906568c9SThomas Gleixner {
60906568c9SThomas Gleixner 	if (tick_do_timer_cpu == cpu) {
61906568c9SThomas Gleixner 		write_seqlock(&xtime_lock);
62906568c9SThomas Gleixner 
63906568c9SThomas Gleixner 		/* Keep track of the next tick event */
64906568c9SThomas Gleixner 		tick_next_period = ktime_add(tick_next_period, tick_period);
65906568c9SThomas Gleixner 
66906568c9SThomas Gleixner 		do_timer(1);
67906568c9SThomas Gleixner 		write_sequnlock(&xtime_lock);
68906568c9SThomas Gleixner 	}
69906568c9SThomas Gleixner 
70906568c9SThomas Gleixner 	update_process_times(user_mode(get_irq_regs()));
71906568c9SThomas Gleixner 	profile_tick(CPU_PROFILING);
72906568c9SThomas Gleixner }
73906568c9SThomas Gleixner 
74906568c9SThomas Gleixner /*
75906568c9SThomas Gleixner  * Event handler for periodic ticks
76906568c9SThomas Gleixner  */
77906568c9SThomas Gleixner void tick_handle_periodic(struct clock_event_device *dev)
78906568c9SThomas Gleixner {
79906568c9SThomas Gleixner 	int cpu = smp_processor_id();
80906568c9SThomas Gleixner 
81906568c9SThomas Gleixner 	tick_periodic(cpu);
82906568c9SThomas Gleixner 
83906568c9SThomas Gleixner 	if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
84906568c9SThomas Gleixner 		return;
85906568c9SThomas Gleixner 	/*
86906568c9SThomas Gleixner 	 * Setup the next period for devices, which do not have
87906568c9SThomas Gleixner 	 * periodic mode:
88906568c9SThomas Gleixner 	 */
89906568c9SThomas Gleixner 	for (;;) {
90906568c9SThomas Gleixner 		ktime_t next = ktime_add(dev->next_event, tick_period);
91906568c9SThomas Gleixner 
92906568c9SThomas Gleixner 		if (!clockevents_program_event(dev, next, ktime_get()))
93906568c9SThomas Gleixner 			return;
94906568c9SThomas Gleixner 		tick_periodic(cpu);
95906568c9SThomas Gleixner 	}
96906568c9SThomas Gleixner }
97906568c9SThomas Gleixner 
98906568c9SThomas Gleixner /*
99906568c9SThomas Gleixner  * Setup the device for a periodic tick
100906568c9SThomas Gleixner  */
101f8381cbaSThomas Gleixner void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
102906568c9SThomas Gleixner {
103f8381cbaSThomas Gleixner 	tick_set_periodic_handler(dev, broadcast);
104f8381cbaSThomas Gleixner 
105f8381cbaSThomas Gleixner 	/* Broadcast setup ? */
106f8381cbaSThomas Gleixner 	if (!tick_device_is_functional(dev))
107f8381cbaSThomas Gleixner 		return;
108906568c9SThomas Gleixner 
109906568c9SThomas Gleixner 	if (dev->features & CLOCK_EVT_FEAT_PERIODIC) {
110906568c9SThomas Gleixner 		clockevents_set_mode(dev, CLOCK_EVT_MODE_PERIODIC);
111906568c9SThomas Gleixner 	} else {
112906568c9SThomas Gleixner 		unsigned long seq;
113906568c9SThomas Gleixner 		ktime_t next;
114906568c9SThomas Gleixner 
115906568c9SThomas Gleixner 		do {
116906568c9SThomas Gleixner 			seq = read_seqbegin(&xtime_lock);
117906568c9SThomas Gleixner 			next = tick_next_period;
118906568c9SThomas Gleixner 		} while (read_seqretry(&xtime_lock, seq));
119906568c9SThomas Gleixner 
120906568c9SThomas Gleixner 		clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
121906568c9SThomas Gleixner 
122906568c9SThomas Gleixner 		for (;;) {
123906568c9SThomas Gleixner 			if (!clockevents_program_event(dev, next, ktime_get()))
124906568c9SThomas Gleixner 				return;
125906568c9SThomas Gleixner 			next = ktime_add(next, tick_period);
126906568c9SThomas Gleixner 		}
127906568c9SThomas Gleixner 	}
128906568c9SThomas Gleixner }
129906568c9SThomas Gleixner 
130906568c9SThomas Gleixner /*
131906568c9SThomas Gleixner  * Setup the tick device
132906568c9SThomas Gleixner  */
133906568c9SThomas Gleixner static void tick_setup_device(struct tick_device *td,
134906568c9SThomas Gleixner 			      struct clock_event_device *newdev, int cpu,
135906568c9SThomas Gleixner 			      cpumask_t cpumask)
136906568c9SThomas Gleixner {
137906568c9SThomas Gleixner 	ktime_t next_event;
138906568c9SThomas Gleixner 	void (*handler)(struct clock_event_device *) = NULL;
139906568c9SThomas Gleixner 
140906568c9SThomas Gleixner 	/*
141906568c9SThomas Gleixner 	 * First device setup ?
142906568c9SThomas Gleixner 	 */
143906568c9SThomas Gleixner 	if (!td->evtdev) {
144906568c9SThomas Gleixner 		/*
145906568c9SThomas Gleixner 		 * If no cpu took the do_timer update, assign it to
146906568c9SThomas Gleixner 		 * this cpu:
147906568c9SThomas Gleixner 		 */
148906568c9SThomas Gleixner 		if (tick_do_timer_cpu == -1) {
149906568c9SThomas Gleixner 			tick_do_timer_cpu = cpu;
150906568c9SThomas Gleixner 			tick_next_period = ktime_get();
151906568c9SThomas Gleixner 			tick_period = ktime_set(0, NSEC_PER_SEC / HZ);
152906568c9SThomas Gleixner 		}
153906568c9SThomas Gleixner 
154906568c9SThomas Gleixner 		/*
155906568c9SThomas Gleixner 		 * Startup in periodic mode first.
156906568c9SThomas Gleixner 		 */
157906568c9SThomas Gleixner 		td->mode = TICKDEV_MODE_PERIODIC;
158906568c9SThomas Gleixner 	} else {
159906568c9SThomas Gleixner 		handler = td->evtdev->event_handler;
160906568c9SThomas Gleixner 		next_event = td->evtdev->next_event;
161906568c9SThomas Gleixner 	}
162906568c9SThomas Gleixner 
163906568c9SThomas Gleixner 	td->evtdev = newdev;
164906568c9SThomas Gleixner 
165906568c9SThomas Gleixner 	/*
166906568c9SThomas Gleixner 	 * When the device is not per cpu, pin the interrupt to the
167906568c9SThomas Gleixner 	 * current cpu:
168906568c9SThomas Gleixner 	 */
169906568c9SThomas Gleixner 	if (!cpus_equal(newdev->cpumask, cpumask))
170906568c9SThomas Gleixner 		irq_set_affinity(newdev->irq, cpumask);
171906568c9SThomas Gleixner 
172f8381cbaSThomas Gleixner 	/*
173f8381cbaSThomas Gleixner 	 * When global broadcasting is active, check if the current
174f8381cbaSThomas Gleixner 	 * device is registered as a placeholder for broadcast mode.
175f8381cbaSThomas Gleixner 	 * This allows us to handle this x86 misfeature in a generic
176f8381cbaSThomas Gleixner 	 * way.
177f8381cbaSThomas Gleixner 	 */
178f8381cbaSThomas Gleixner 	if (tick_device_uses_broadcast(newdev, cpu))
179f8381cbaSThomas Gleixner 		return;
180f8381cbaSThomas Gleixner 
181906568c9SThomas Gleixner 	if (td->mode == TICKDEV_MODE_PERIODIC)
182906568c9SThomas Gleixner 		tick_setup_periodic(newdev, 0);
18379bf2bb3SThomas Gleixner 	else
18479bf2bb3SThomas Gleixner 		tick_setup_oneshot(newdev, handler, next_event);
185906568c9SThomas Gleixner }
186906568c9SThomas Gleixner 
187906568c9SThomas Gleixner /*
188906568c9SThomas Gleixner  * Check, if the new registered device should be used.
189906568c9SThomas Gleixner  */
190906568c9SThomas Gleixner static int tick_check_new_device(struct clock_event_device *newdev)
191906568c9SThomas Gleixner {
192906568c9SThomas Gleixner 	struct clock_event_device *curdev;
193906568c9SThomas Gleixner 	struct tick_device *td;
194906568c9SThomas Gleixner 	int cpu, ret = NOTIFY_OK;
195906568c9SThomas Gleixner 	unsigned long flags;
196906568c9SThomas Gleixner 	cpumask_t cpumask;
197906568c9SThomas Gleixner 
198906568c9SThomas Gleixner 	spin_lock_irqsave(&tick_device_lock, flags);
199906568c9SThomas Gleixner 
200906568c9SThomas Gleixner 	cpu = smp_processor_id();
201906568c9SThomas Gleixner 	if (!cpu_isset(cpu, newdev->cpumask))
202906568c9SThomas Gleixner 		goto out;
203906568c9SThomas Gleixner 
204906568c9SThomas Gleixner 	td = &per_cpu(tick_cpu_device, cpu);
205906568c9SThomas Gleixner 	curdev = td->evtdev;
206906568c9SThomas Gleixner 	cpumask = cpumask_of_cpu(cpu);
207906568c9SThomas Gleixner 
208906568c9SThomas Gleixner 	/* cpu local device ? */
209906568c9SThomas Gleixner 	if (!cpus_equal(newdev->cpumask, cpumask)) {
210906568c9SThomas Gleixner 
211906568c9SThomas Gleixner 		/*
212906568c9SThomas Gleixner 		 * If the cpu affinity of the device interrupt can not
213906568c9SThomas Gleixner 		 * be set, ignore it.
214906568c9SThomas Gleixner 		 */
215906568c9SThomas Gleixner 		if (!irq_can_set_affinity(newdev->irq))
216906568c9SThomas Gleixner 			goto out_bc;
217906568c9SThomas Gleixner 
218906568c9SThomas Gleixner 		/*
219906568c9SThomas Gleixner 		 * If we have a cpu local device already, do not replace it
220906568c9SThomas Gleixner 		 * by a non cpu local device
221906568c9SThomas Gleixner 		 */
222906568c9SThomas Gleixner 		if (curdev && cpus_equal(curdev->cpumask, cpumask))
223906568c9SThomas Gleixner 			goto out_bc;
224906568c9SThomas Gleixner 	}
225906568c9SThomas Gleixner 
226906568c9SThomas Gleixner 	/*
227906568c9SThomas Gleixner 	 * If we have an active device, then check the rating and the oneshot
228906568c9SThomas Gleixner 	 * feature.
229906568c9SThomas Gleixner 	 */
230906568c9SThomas Gleixner 	if (curdev) {
231906568c9SThomas Gleixner 		/*
23279bf2bb3SThomas Gleixner 		 * Prefer one shot capable devices !
23379bf2bb3SThomas Gleixner 		 */
23479bf2bb3SThomas Gleixner 		if ((curdev->features & CLOCK_EVT_FEAT_ONESHOT) &&
23579bf2bb3SThomas Gleixner 		    !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
23679bf2bb3SThomas Gleixner 			goto out_bc;
23779bf2bb3SThomas Gleixner 		/*
238906568c9SThomas Gleixner 		 * Check the rating
239906568c9SThomas Gleixner 		 */
240906568c9SThomas Gleixner 		if (curdev->rating >= newdev->rating)
241f8381cbaSThomas Gleixner 			goto out_bc;
242906568c9SThomas Gleixner 	}
243906568c9SThomas Gleixner 
244906568c9SThomas Gleixner 	/*
245906568c9SThomas Gleixner 	 * Replace the eventually existing device by the new
246f8381cbaSThomas Gleixner 	 * device. If the current device is the broadcast device, do
247f8381cbaSThomas Gleixner 	 * not give it back to the clockevents layer !
248906568c9SThomas Gleixner 	 */
249f8381cbaSThomas Gleixner 	if (tick_is_broadcast_device(curdev)) {
250f8381cbaSThomas Gleixner 		clockevents_set_mode(curdev, CLOCK_EVT_MODE_SHUTDOWN);
251f8381cbaSThomas Gleixner 		curdev = NULL;
252f8381cbaSThomas Gleixner 	}
253906568c9SThomas Gleixner 	clockevents_exchange_device(curdev, newdev);
254906568c9SThomas Gleixner 	tick_setup_device(td, newdev, cpu, cpumask);
25579bf2bb3SThomas Gleixner 	if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
25679bf2bb3SThomas Gleixner 		tick_oneshot_notify();
257906568c9SThomas Gleixner 
258f8381cbaSThomas Gleixner 	spin_unlock_irqrestore(&tick_device_lock, flags);
259f8381cbaSThomas Gleixner 	return NOTIFY_STOP;
260f8381cbaSThomas Gleixner 
261f8381cbaSThomas Gleixner out_bc:
262f8381cbaSThomas Gleixner 	/*
263f8381cbaSThomas Gleixner 	 * Can the new device be used as a broadcast device ?
264f8381cbaSThomas Gleixner 	 */
265f8381cbaSThomas Gleixner 	if (tick_check_broadcast_device(newdev))
266f8381cbaSThomas Gleixner 		ret = NOTIFY_STOP;
267906568c9SThomas Gleixner out:
268906568c9SThomas Gleixner 	spin_unlock_irqrestore(&tick_device_lock, flags);
269f8381cbaSThomas Gleixner 
270906568c9SThomas Gleixner 	return ret;
271906568c9SThomas Gleixner }
272906568c9SThomas Gleixner 
273906568c9SThomas Gleixner /*
274906568c9SThomas Gleixner  * Shutdown an event device on a given cpu:
275906568c9SThomas Gleixner  *
276906568c9SThomas Gleixner  * This is called on a life CPU, when a CPU is dead. So we cannot
277906568c9SThomas Gleixner  * access the hardware device itself.
278906568c9SThomas Gleixner  * We just set the mode and remove it from the lists.
279906568c9SThomas Gleixner  */
280906568c9SThomas Gleixner static void tick_shutdown(unsigned int *cpup)
281906568c9SThomas Gleixner {
282906568c9SThomas Gleixner 	struct tick_device *td = &per_cpu(tick_cpu_device, *cpup);
283906568c9SThomas Gleixner 	struct clock_event_device *dev = td->evtdev;
284906568c9SThomas Gleixner 	unsigned long flags;
285906568c9SThomas Gleixner 
286906568c9SThomas Gleixner 	spin_lock_irqsave(&tick_device_lock, flags);
287906568c9SThomas Gleixner 	td->mode = TICKDEV_MODE_PERIODIC;
288906568c9SThomas Gleixner 	if (dev) {
289906568c9SThomas Gleixner 		/*
290906568c9SThomas Gleixner 		 * Prevent that the clock events layer tries to call
291906568c9SThomas Gleixner 		 * the set mode function!
292906568c9SThomas Gleixner 		 */
293906568c9SThomas Gleixner 		dev->mode = CLOCK_EVT_MODE_UNUSED;
294906568c9SThomas Gleixner 		clockevents_exchange_device(dev, NULL);
295906568c9SThomas Gleixner 		td->evtdev = NULL;
296906568c9SThomas Gleixner 	}
297906568c9SThomas Gleixner 	spin_unlock_irqrestore(&tick_device_lock, flags);
298906568c9SThomas Gleixner }
299906568c9SThomas Gleixner 
300906568c9SThomas Gleixner /*
301906568c9SThomas Gleixner  * Notification about clock event devices
302906568c9SThomas Gleixner  */
303906568c9SThomas Gleixner static int tick_notify(struct notifier_block *nb, unsigned long reason,
304906568c9SThomas Gleixner 			       void *dev)
305906568c9SThomas Gleixner {
306906568c9SThomas Gleixner 	switch (reason) {
307906568c9SThomas Gleixner 
308906568c9SThomas Gleixner 	case CLOCK_EVT_NOTIFY_ADD:
309906568c9SThomas Gleixner 		return tick_check_new_device(dev);
310906568c9SThomas Gleixner 
311f8381cbaSThomas Gleixner 	case CLOCK_EVT_NOTIFY_BROADCAST_ON:
312f8381cbaSThomas Gleixner 	case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
313f8381cbaSThomas Gleixner 		tick_broadcast_on_off(reason, dev);
314f8381cbaSThomas Gleixner 		break;
315f8381cbaSThomas Gleixner 
31679bf2bb3SThomas Gleixner 	case CLOCK_EVT_NOTIFY_BROADCAST_ENTER:
31779bf2bb3SThomas Gleixner 	case CLOCK_EVT_NOTIFY_BROADCAST_EXIT:
31879bf2bb3SThomas Gleixner 		tick_broadcast_oneshot_control(reason);
31979bf2bb3SThomas Gleixner 		break;
32079bf2bb3SThomas Gleixner 
321906568c9SThomas Gleixner 	case CLOCK_EVT_NOTIFY_CPU_DEAD:
32279bf2bb3SThomas Gleixner 		tick_shutdown_broadcast_oneshot(dev);
323f8381cbaSThomas Gleixner 		tick_shutdown_broadcast(dev);
324906568c9SThomas Gleixner 		tick_shutdown(dev);
325906568c9SThomas Gleixner 		break;
326906568c9SThomas Gleixner 
327906568c9SThomas Gleixner 	default:
328906568c9SThomas Gleixner 		break;
329906568c9SThomas Gleixner 	}
330906568c9SThomas Gleixner 
331906568c9SThomas Gleixner 	return NOTIFY_OK;
332906568c9SThomas Gleixner }
333906568c9SThomas Gleixner 
334906568c9SThomas Gleixner static struct notifier_block tick_notifier = {
335906568c9SThomas Gleixner 	.notifier_call = tick_notify,
336906568c9SThomas Gleixner };
337906568c9SThomas Gleixner 
338906568c9SThomas Gleixner /**
339906568c9SThomas Gleixner  * tick_init - initialize the tick control
340906568c9SThomas Gleixner  *
341906568c9SThomas Gleixner  * Register the notifier with the clockevents framework
342906568c9SThomas Gleixner  */
343906568c9SThomas Gleixner void __init tick_init(void)
344906568c9SThomas Gleixner {
345906568c9SThomas Gleixner 	clockevents_register_notifier(&tick_notifier);
346906568c9SThomas Gleixner }
347