2 * linux/kernel/time/tick-oneshot.c
4 * This file contains functions which manage high resolution 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/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"
26 * tick_program_event internal worker function
28 int tick_dev_program_event(struct clock_event_device
*dev
, ktime_t expires
,
31 ktime_t now
= ktime_get();
35 int ret
= clockevents_program_event(dev
, expires
, now
);
41 * We tried 2 times to program the device with the given
42 * min_delta_ns. If that's not working then we double it
46 /* Increase the min. delta and try again */
47 if (!dev
->min_delta_ns
)
48 dev
->min_delta_ns
= 5000;
50 dev
->min_delta_ns
+= dev
->min_delta_ns
>> 1;
53 "CE: %s increasing min_delta_ns to %lu nsec\n",
54 dev
->name
? dev
->name
: "?",
55 dev
->min_delta_ns
<< 1);
61 expires
= ktime_add_ns(now
, dev
->min_delta_ns
);
68 int tick_program_event(ktime_t expires
, int force
)
70 struct clock_event_device
*dev
= __get_cpu_var(tick_cpu_device
).evtdev
;
72 return tick_dev_program_event(dev
, expires
, force
);
76 * tick_resume_onshot - resume oneshot mode
78 void tick_resume_oneshot(void)
80 struct tick_device
*td
= &__get_cpu_var(tick_cpu_device
);
81 struct clock_event_device
*dev
= td
->evtdev
;
83 clockevents_set_mode(dev
, CLOCK_EVT_MODE_ONESHOT
);
84 tick_program_event(ktime_get(), 1);
88 * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
90 void tick_setup_oneshot(struct clock_event_device
*newdev
,
91 void (*handler
)(struct clock_event_device
*),
94 newdev
->event_handler
= handler
;
95 clockevents_set_mode(newdev
, CLOCK_EVT_MODE_ONESHOT
);
96 tick_dev_program_event(newdev
, next_event
, 1);
100 * tick_switch_to_oneshot - switch to oneshot mode
102 int tick_switch_to_oneshot(void (*handler
)(struct clock_event_device
*))
104 struct tick_device
*td
= &__get_cpu_var(tick_cpu_device
);
105 struct clock_event_device
*dev
= td
->evtdev
;
107 if (!dev
|| !(dev
->features
& CLOCK_EVT_FEAT_ONESHOT
) ||
108 !tick_device_is_functional(dev
)) {
110 printk(KERN_INFO
"Clockevents: "
111 "could not switch to one-shot mode:");
113 printk(" no tick device\n");
115 if (!tick_device_is_functional(dev
))
116 printk(" %s is not functional.\n", dev
->name
);
118 printk(" %s does not support one-shot mode.\n",
124 td
->mode
= TICKDEV_MODE_ONESHOT
;
125 dev
->event_handler
= handler
;
126 clockevents_set_mode(dev
, CLOCK_EVT_MODE_ONESHOT
);
127 tick_broadcast_switch_to_oneshot();
132 * tick_check_oneshot_mode - check whether the system is in oneshot mode
134 * returns 1 when either nohz or highres are enabled. otherwise 0.
136 int tick_oneshot_mode_active(void)
141 local_irq_save(flags
);
142 ret
= __get_cpu_var(tick_cpu_device
).mode
== TICKDEV_MODE_ONESHOT
;
143 local_irq_restore(flags
);
148 #ifdef CONFIG_HIGH_RES_TIMERS
150 * tick_init_highres - switch to high resolution mode
152 * Called with interrupts disabled.
154 int tick_init_highres(void)
156 return tick_switch_to_oneshot(hrtimer_interrupt
);