Merge branch 'x86/apic' into x86/platform
[linux-2.6/linux-mips.git] / kernel / time / tick-oneshot.c
blob5cbc101f908b8483938c0153fc1ac023bcd1c784
1 /*
2 * linux/kernel/time/tick-oneshot.c
4 * This file contains functions which manage high resolution tick
5 * related events.
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/interrupt.h>
18 #include <linux/percpu.h>
19 #include <linux/profile.h>
20 #include <linux/sched.h>
21 #include <linux/tick.h>
23 #include "tick-internal.h"
25 /* Limit min_delta to a jiffie */
26 #define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
28 static int tick_increase_min_delta(struct clock_event_device *dev)
30 /* Nothing to do if we already reached the limit */
31 if (dev->min_delta_ns >= MIN_DELTA_LIMIT)
32 return -ETIME;
34 if (dev->min_delta_ns < 5000)
35 dev->min_delta_ns = 5000;
36 else
37 dev->min_delta_ns += dev->min_delta_ns >> 1;
39 if (dev->min_delta_ns > MIN_DELTA_LIMIT)
40 dev->min_delta_ns = MIN_DELTA_LIMIT;
42 printk(KERN_WARNING "CE: %s increased min_delta_ns to %llu nsec\n",
43 dev->name ? dev->name : "?",
44 (unsigned long long) dev->min_delta_ns);
45 return 0;
48 /**
49 * tick_program_event internal worker function
51 int tick_dev_program_event(struct clock_event_device *dev, ktime_t expires,
52 int force)
54 ktime_t now = ktime_get();
55 int i;
57 for (i = 0;;) {
58 int ret = clockevents_program_event(dev, expires, now);
60 if (!ret || !force)
61 return ret;
63 dev->retries++;
65 * We tried 3 times to program the device with the given
66 * min_delta_ns. If that's not working then we increase it
67 * and emit a warning.
69 if (++i > 2) {
70 /* Increase the min. delta and try again */
71 if (tick_increase_min_delta(dev)) {
73 * Get out of the loop if min_delta_ns
74 * hit the limit already. That's
75 * better than staying here forever.
77 * We clear next_event so we have a
78 * chance that the box survives.
80 printk(KERN_WARNING
81 "CE: Reprogramming failure. Giving up\n");
82 dev->next_event.tv64 = KTIME_MAX;
83 return -ETIME;
85 i = 0;
88 now = ktime_get();
89 expires = ktime_add_ns(now, dev->min_delta_ns);
93 /**
94 * tick_program_event
96 int tick_program_event(ktime_t expires, int force)
98 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
100 return tick_dev_program_event(dev, expires, force);
104 * tick_resume_onshot - resume oneshot mode
106 void tick_resume_oneshot(void)
108 struct tick_device *td = &__get_cpu_var(tick_cpu_device);
109 struct clock_event_device *dev = td->evtdev;
111 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
112 tick_program_event(ktime_get(), 1);
116 * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
118 void tick_setup_oneshot(struct clock_event_device *newdev,
119 void (*handler)(struct clock_event_device *),
120 ktime_t next_event)
122 newdev->event_handler = handler;
123 clockevents_set_mode(newdev, CLOCK_EVT_MODE_ONESHOT);
124 tick_dev_program_event(newdev, next_event, 1);
128 * tick_switch_to_oneshot - switch to oneshot mode
130 int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *))
132 struct tick_device *td = &__get_cpu_var(tick_cpu_device);
133 struct clock_event_device *dev = td->evtdev;
135 if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) ||
136 !tick_device_is_functional(dev)) {
138 printk(KERN_INFO "Clockevents: "
139 "could not switch to one-shot mode:");
140 if (!dev) {
141 printk(" no tick device\n");
142 } else {
143 if (!tick_device_is_functional(dev))
144 printk(" %s is not functional.\n", dev->name);
145 else
146 printk(" %s does not support one-shot mode.\n",
147 dev->name);
149 return -EINVAL;
152 td->mode = TICKDEV_MODE_ONESHOT;
153 dev->event_handler = handler;
154 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
155 tick_broadcast_switch_to_oneshot();
156 return 0;
160 * tick_check_oneshot_mode - check whether the system is in oneshot mode
162 * returns 1 when either nohz or highres are enabled. otherwise 0.
164 int tick_oneshot_mode_active(void)
166 unsigned long flags;
167 int ret;
169 local_irq_save(flags);
170 ret = __this_cpu_read(tick_cpu_device.mode) == TICKDEV_MODE_ONESHOT;
171 local_irq_restore(flags);
173 return ret;
176 #ifdef CONFIG_HIGH_RES_TIMERS
178 * tick_init_highres - switch to high resolution mode
180 * Called with interrupts disabled.
182 int tick_init_highres(void)
184 return tick_switch_to_oneshot(hrtimer_interrupt);
186 #endif