2 * linux/kernel/time/tick-common.c
4 * This file contains the base functions to manage periodic tick
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
11 * This code is licenced under the GPL version 2. For details see
12 * kernel-base/COPYING.
14 #include <linux/cpu.h>
15 #include <linux/err.h>
16 #include <linux/hrtimer.h>
17 #include <linux/irq.h>
18 #include <linux/percpu.h>
19 #include <linux/profile.h>
20 #include <linux/sched.h>
21 #include <linux/tick.h>
26 static DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
28 * Tick next event: keeps track of the tick time
30 static ktime_t tick_next_period;
31 static ktime_t tick_period;
32 static int tick_do_timer_cpu = -1;
33 static DEFINE_SPINLOCK(tick_device_lock);
38 static void tick_periodic(int cpu)
40 if (tick_do_timer_cpu == cpu) {
41 write_seqlock(&xtime_lock);
43 /* Keep track of the next tick event */
44 tick_next_period = ktime_add(tick_next_period, tick_period);
47 write_sequnlock(&xtime_lock);
50 update_process_times(user_mode(get_irq_regs()));
51 profile_tick(CPU_PROFILING);
55 * Event handler for periodic ticks
57 void tick_handle_periodic(struct clock_event_device *dev)
59 int cpu = smp_processor_id();
63 if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
66 * Setup the next period for devices, which do not have
70 ktime_t next = ktime_add(dev->next_event, tick_period);
72 if (!clockevents_program_event(dev, next, ktime_get()))
79 * Setup the device for a periodic tick
81 void tick_setup_periodic(struct clock_event_device *dev)
83 dev->event_handler = tick_handle_periodic;
85 if (dev->features & CLOCK_EVT_FEAT_PERIODIC) {
86 clockevents_set_mode(dev, CLOCK_EVT_MODE_PERIODIC);
92 seq = read_seqbegin(&xtime_lock);
93 next = tick_next_period;
94 } while (read_seqretry(&xtime_lock, seq));
96 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
99 if (!clockevents_program_event(dev, next, ktime_get()))
101 next = ktime_add(next, tick_period);
107 * Setup the tick device
109 static void tick_setup_device(struct tick_device *td,
110 struct clock_event_device *newdev, int cpu,
114 void (*handler)(struct clock_event_device *) = NULL;
117 * First device setup ?
121 * If no cpu took the do_timer update, assign it to
124 if (tick_do_timer_cpu == -1) {
125 tick_do_timer_cpu = cpu;
126 tick_next_period = ktime_get();
127 tick_period = ktime_set(0, NSEC_PER_SEC / HZ);
131 * Startup in periodic mode first.
133 td->mode = TICKDEV_MODE_PERIODIC;
135 handler = td->evtdev->event_handler;
136 next_event = td->evtdev->next_event;
142 * When the device is not per cpu, pin the interrupt to the
145 if (!cpus_equal(newdev->cpumask, cpumask))
146 irq_set_affinity(newdev->irq, cpumask);
148 if (td->mode == TICKDEV_MODE_PERIODIC)
149 tick_setup_periodic(newdev, 0);
153 * Check, if the new registered device should be used.
155 static int tick_check_new_device(struct clock_event_device *newdev)
157 struct clock_event_device *curdev;
158 struct tick_device *td;
159 int cpu, ret = NOTIFY_OK;
163 spin_lock_irqsave(&tick_device_lock, flags);
165 cpu = smp_processor_id();
166 if (!cpu_isset(cpu, newdev->cpumask))
169 td = &per_cpu(tick_cpu_device, cpu);
171 cpumask = cpumask_of_cpu(cpu);
173 /* cpu local device ? */
174 if (!cpus_equal(newdev->cpumask, cpumask)) {
177 * If the cpu affinity of the device interrupt can not
180 if (!irq_can_set_affinity(newdev->irq))
184 * If we have a cpu local device already, do not replace it
185 * by a non cpu local device
187 if (curdev && cpus_equal(curdev->cpumask, cpumask))
192 * If we have an active device, then check the rating and the oneshot
199 if (curdev->rating >= newdev->rating)
204 * Replace the eventually existing device by the new
207 clockevents_exchange_device(curdev, newdev);
208 tick_setup_device(td, newdev, cpu, cpumask);
212 spin_unlock_irqrestore(&tick_device_lock, flags);
217 * Shutdown an event device on a given cpu:
219 * This is called on a life CPU, when a CPU is dead. So we cannot
220 * access the hardware device itself.
221 * We just set the mode and remove it from the lists.
223 static void tick_shutdown(unsigned int *cpup)
225 struct tick_device *td = &per_cpu(tick_cpu_device, *cpup);
226 struct clock_event_device *dev = td->evtdev;
229 spin_lock_irqsave(&tick_device_lock, flags);
230 td->mode = TICKDEV_MODE_PERIODIC;
233 * Prevent that the clock events layer tries to call
234 * the set mode function!
236 dev->mode = CLOCK_EVT_MODE_UNUSED;
237 clockevents_exchange_device(dev, NULL);
240 spin_unlock_irqrestore(&tick_device_lock, flags);
244 * Notification about clock event devices
246 static int tick_notify(struct notifier_block *nb, unsigned long reason,
251 case CLOCK_EVT_NOTIFY_ADD:
252 return tick_check_new_device(dev);
254 case CLOCK_EVT_NOTIFY_CPU_DEAD:
265 static struct notifier_block tick_notifier = {
266 .notifier_call = tick_notify,
270 * tick_init - initialize the tick control
272 * Register the notifier with the clockevents framework
274 void __init tick_init(void)
276 clockevents_register_notifier(&tick_notifier);