blob: 84c7cfca4d7db5893da0a2cb59ff2c1b60ef53cb [file] [log] [blame]
Thomas Gleixner906568c2007-02-16 01:28:01 -08001/*
2 * linux/kernel/time/tick-common.c
3 *
4 * This file contains the base functions to manage periodic tick
5 * related events.
6 *
7 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
8 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
9 * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
10 *
11 * This code is licenced under the GPL version 2. For details see
12 * kernel-base/COPYING.
13 */
14#include <linux/cpu.h>
15#include <linux/err.h>
16#include <linux/hrtimer.h>
Russell Kingd7b90682008-04-17 07:46:24 +020017#include <linux/interrupt.h>
Thomas Gleixner906568c2007-02-16 01:28:01 -080018#include <linux/percpu.h>
19#include <linux/profile.h>
20#include <linux/sched.h>
Thomas Gleixner906568c2007-02-16 01:28:01 -080021
Russell Kingd7b90682008-04-17 07:46:24 +020022#include <asm/irq_regs.h>
23
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080024#include "tick-internal.h"
25
Thomas Gleixner906568c2007-02-16 01:28:01 -080026/*
27 * Tick devices
28 */
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080029DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
Thomas Gleixner906568c2007-02-16 01:28:01 -080030/*
31 * Tick next event: keeps track of the tick time
32 */
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080033ktime_t tick_next_period;
34ktime_t tick_period;
Thomas Gleixner64414022008-09-22 18:46:37 +020035int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT;
Thomas Gleixner906568c2007-02-16 01:28:01 -080036
Ingo Molnar289f4802007-02-16 01:28:15 -080037/*
38 * Debugging: see timer_list.c
39 */
40struct tick_device *tick_get_device(int cpu)
41{
42 return &per_cpu(tick_cpu_device, cpu);
43}
44
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080045/**
46 * tick_is_oneshot_available - check for a oneshot capable event device
47 */
48int tick_is_oneshot_available(void)
49{
Christoph Lameter909ea962010-12-08 16:22:55 +010050 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080051
Thomas Gleixner3a142a02011-02-25 22:34:23 +010052 if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
53 return 0;
54 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
55 return 1;
56 return tick_broadcast_oneshot_available();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080057}
58
Thomas Gleixner906568c2007-02-16 01:28:01 -080059/*
60 * Periodic tick
61 */
62static void tick_periodic(int cpu)
63{
64 if (tick_do_timer_cpu == cpu) {
John Stultzd6ad4182012-02-28 16:50:11 -080065 write_seqlock(&jiffies_lock);
Thomas Gleixner906568c2007-02-16 01:28:01 -080066
67 /* Keep track of the next tick event */
68 tick_next_period = ktime_add(tick_next_period, tick_period);
69
70 do_timer(1);
John Stultzd6ad4182012-02-28 16:50:11 -080071 write_sequnlock(&jiffies_lock);
Thomas Gleixner906568c2007-02-16 01:28:01 -080072 }
73
74 update_process_times(user_mode(get_irq_regs()));
75 profile_tick(CPU_PROFILING);
76}
77
78/*
79 * Event handler for periodic ticks
80 */
81void tick_handle_periodic(struct clock_event_device *dev)
82{
83 int cpu = smp_processor_id();
David S. Miller3494c162007-02-24 22:11:42 -080084 ktime_t next;
Thomas Gleixner906568c2007-02-16 01:28:01 -080085
86 tick_periodic(cpu);
87
88 if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
89 return;
90 /*
91 * Setup the next period for devices, which do not have
92 * periodic mode:
93 */
David S. Miller3494c162007-02-24 22:11:42 -080094 next = ktime_add(dev->next_event, tick_period);
Thomas Gleixner906568c2007-02-16 01:28:01 -080095 for (;;) {
Martin Schwidefskyd1748302011-08-23 15:29:42 +020096 if (!clockevents_program_event(dev, next, false))
Thomas Gleixner906568c2007-02-16 01:28:01 -080097 return;
john stultz74a03b62009-05-01 13:10:25 -070098 /*
99 * Have to be careful here. If we're in oneshot mode,
100 * before we call tick_periodic() in a loop, we need
101 * to be sure we're using a real hardware clocksource.
102 * Otherwise we could get trapped in an infinite
103 * loop, as the tick_periodic() increments jiffies,
104 * when then will increment time, posibly causing
105 * the loop to trigger again and again.
106 */
107 if (timekeeping_valid_for_hres())
108 tick_periodic(cpu);
David S. Miller3494c162007-02-24 22:11:42 -0800109 next = ktime_add(next, tick_period);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800110 }
111}
112
113/*
114 * Setup the device for a periodic tick
115 */
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800116void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
Thomas Gleixner906568c2007-02-16 01:28:01 -0800117{
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800118 tick_set_periodic_handler(dev, broadcast);
119
120 /* Broadcast setup ? */
121 if (!tick_device_is_functional(dev))
122 return;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800123
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200124 if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
125 !tick_broadcast_oneshot_active()) {
Thomas Gleixner906568c2007-02-16 01:28:01 -0800126 clockevents_set_mode(dev, CLOCK_EVT_MODE_PERIODIC);
127 } else {
128 unsigned long seq;
129 ktime_t next;
130
131 do {
John Stultzd6ad4182012-02-28 16:50:11 -0800132 seq = read_seqbegin(&jiffies_lock);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800133 next = tick_next_period;
John Stultzd6ad4182012-02-28 16:50:11 -0800134 } while (read_seqretry(&jiffies_lock, seq));
Thomas Gleixner906568c2007-02-16 01:28:01 -0800135
136 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
137
138 for (;;) {
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200139 if (!clockevents_program_event(dev, next, false))
Thomas Gleixner906568c2007-02-16 01:28:01 -0800140 return;
141 next = ktime_add(next, tick_period);
142 }
143 }
144}
145
146/*
147 * Setup the tick device
148 */
149static void tick_setup_device(struct tick_device *td,
150 struct clock_event_device *newdev, int cpu,
Rusty Russell0de26522008-12-13 21:20:26 +1030151 const struct cpumask *cpumask)
Thomas Gleixner906568c2007-02-16 01:28:01 -0800152{
153 ktime_t next_event;
154 void (*handler)(struct clock_event_device *) = NULL;
155
156 /*
157 * First device setup ?
158 */
159 if (!td->evtdev) {
160 /*
161 * If no cpu took the do_timer update, assign it to
162 * this cpu:
163 */
Thomas Gleixner64414022008-09-22 18:46:37 +0200164 if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) {
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200165 if (!tick_nohz_full_cpu(cpu))
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100166 tick_do_timer_cpu = cpu;
167 else
168 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800169 tick_next_period = ktime_get();
170 tick_period = ktime_set(0, NSEC_PER_SEC / HZ);
171 }
172
173 /*
174 * Startup in periodic mode first.
175 */
176 td->mode = TICKDEV_MODE_PERIODIC;
177 } else {
178 handler = td->evtdev->event_handler;
179 next_event = td->evtdev->next_event;
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000180 td->evtdev->event_handler = clockevents_handle_noop;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800181 }
182
183 td->evtdev = newdev;
184
185 /*
186 * When the device is not per cpu, pin the interrupt to the
187 * current cpu:
188 */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030189 if (!cpumask_equal(newdev->cpumask, cpumask))
Rusty Russell0de26522008-12-13 21:20:26 +1030190 irq_set_affinity(newdev->irq, cpumask);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800191
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800192 /*
193 * When global broadcasting is active, check if the current
194 * device is registered as a placeholder for broadcast mode.
195 * This allows us to handle this x86 misfeature in a generic
196 * way.
197 */
198 if (tick_device_uses_broadcast(newdev, cpu))
199 return;
200
Thomas Gleixner906568c2007-02-16 01:28:01 -0800201 if (td->mode == TICKDEV_MODE_PERIODIC)
202 tick_setup_periodic(newdev, 0);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800203 else
204 tick_setup_oneshot(newdev, handler, next_event);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800205}
206
207/*
Thomas Gleixner7126cac2013-04-25 20:31:48 +0000208 * Check, if the new registered device should be used. Called with
209 * clockevents_lock held and interrupts disabled.
Thomas Gleixner906568c2007-02-16 01:28:01 -0800210 */
Thomas Gleixner7172a282013-04-25 20:31:47 +0000211void tick_check_new_device(struct clock_event_device *newdev)
Thomas Gleixner906568c2007-02-16 01:28:01 -0800212{
213 struct clock_event_device *curdev;
214 struct tick_device *td;
Thomas Gleixner7172a282013-04-25 20:31:47 +0000215 int cpu;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800216
217 cpu = smp_processor_id();
Rusty Russell320ab2b2008-12-13 21:20:26 +1030218 if (!cpumask_test_cpu(cpu, newdev->cpumask))
Venki Pallipadi4a932322007-10-12 23:04:23 +0200219 goto out_bc;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800220
221 td = &per_cpu(tick_cpu_device, cpu);
222 curdev = td->evtdev;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800223
224 /* cpu local device ? */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030225 if (!cpumask_equal(newdev->cpumask, cpumask_of(cpu))) {
Thomas Gleixner906568c2007-02-16 01:28:01 -0800226
227 /*
228 * If the cpu affinity of the device interrupt can not
229 * be set, ignore it.
230 */
231 if (!irq_can_set_affinity(newdev->irq))
232 goto out_bc;
233
234 /*
235 * If we have a cpu local device already, do not replace it
236 * by a non cpu local device
237 */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030238 if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
Thomas Gleixner906568c2007-02-16 01:28:01 -0800239 goto out_bc;
240 }
241
242 /*
243 * If we have an active device, then check the rating and the oneshot
244 * feature.
245 */
246 if (curdev) {
247 /*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800248 * Prefer one shot capable devices !
249 */
250 if ((curdev->features & CLOCK_EVT_FEAT_ONESHOT) &&
251 !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
252 goto out_bc;
253 /*
Thomas Gleixner906568c2007-02-16 01:28:01 -0800254 * Check the rating
255 */
256 if (curdev->rating >= newdev->rating)
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800257 goto out_bc;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800258 }
259
260 /*
261 * Replace the eventually existing device by the new
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800262 * device. If the current device is the broadcast device, do
263 * not give it back to the clockevents layer !
Thomas Gleixner906568c2007-02-16 01:28:01 -0800264 */
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800265 if (tick_is_broadcast_device(curdev)) {
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700266 clockevents_shutdown(curdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800267 curdev = NULL;
268 }
Thomas Gleixner906568c2007-02-16 01:28:01 -0800269 clockevents_exchange_device(curdev, newdev);
Rusty Russell6b954822009-01-01 10:12:25 +1030270 tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800271 if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
272 tick_oneshot_notify();
Thomas Gleixner7172a282013-04-25 20:31:47 +0000273 return;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800274
275out_bc:
276 /*
277 * Can the new device be used as a broadcast device ?
278 */
Thomas Gleixner7172a282013-04-25 20:31:47 +0000279 tick_install_broadcast_device(newdev);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800280}
281
282/*
Sebastien Dugue94df7de2008-12-01 14:09:07 +0100283 * Transfer the do_timer job away from a dying cpu.
284 *
285 * Called with interrupts disabled.
286 */
Thomas Gleixner8c53daf2013-04-25 20:31:48 +0000287void tick_handover_do_timer(int *cpup)
Sebastien Dugue94df7de2008-12-01 14:09:07 +0100288{
289 if (*cpup == tick_do_timer_cpu) {
290 int cpu = cpumask_first(cpu_online_mask);
291
292 tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu :
293 TICK_DO_TIMER_NONE;
294 }
295}
296
297/*
Thomas Gleixner906568c2007-02-16 01:28:01 -0800298 * Shutdown an event device on a given cpu:
299 *
300 * This is called on a life CPU, when a CPU is dead. So we cannot
301 * access the hardware device itself.
302 * We just set the mode and remove it from the lists.
303 */
Thomas Gleixner8c53daf2013-04-25 20:31:48 +0000304void tick_shutdown(unsigned int *cpup)
Thomas Gleixner906568c2007-02-16 01:28:01 -0800305{
306 struct tick_device *td = &per_cpu(tick_cpu_device, *cpup);
307 struct clock_event_device *dev = td->evtdev;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800308
Thomas Gleixner906568c2007-02-16 01:28:01 -0800309 td->mode = TICKDEV_MODE_PERIODIC;
310 if (dev) {
311 /*
312 * Prevent that the clock events layer tries to call
313 * the set mode function!
314 */
315 dev->mode = CLOCK_EVT_MODE_UNUSED;
316 clockevents_exchange_device(dev, NULL);
Thomas Gleixner6f7a05d2013-04-25 11:45:53 +0200317 dev->event_handler = clockevents_handle_noop;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800318 td->evtdev = NULL;
319 }
Thomas Gleixner906568c2007-02-16 01:28:01 -0800320}
321
Thomas Gleixner8c53daf2013-04-25 20:31:48 +0000322void tick_suspend(void)
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100323{
324 struct tick_device *td = &__get_cpu_var(tick_cpu_device);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100325
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700326 clockevents_shutdown(td->evtdev);
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100327}
328
Thomas Gleixner8c53daf2013-04-25 20:31:48 +0000329void tick_resume(void)
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100330{
331 struct tick_device *td = &__get_cpu_var(tick_cpu_device);
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700332 int broadcast = tick_resume_broadcast();
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100333
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700334 clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_RESUME);
335
336 if (!broadcast) {
337 if (td->mode == TICKDEV_MODE_PERIODIC)
338 tick_setup_periodic(td->evtdev, 0);
339 else
340 tick_resume_oneshot();
341 }
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100342}
343
Thomas Gleixner906568c2007-02-16 01:28:01 -0800344/**
345 * tick_init - initialize the tick control
Thomas Gleixner906568c2007-02-16 01:28:01 -0800346 */
347void __init tick_init(void)
348{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100349 tick_broadcast_init();
Thomas Gleixner906568c2007-02-16 01:28:01 -0800350}