blob: aada0e52680ace6cc7d5e09a111a3879c1c6bda3 [file] [log] [blame]
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001/*
2 * linux/kernel/time/tick-oneshot.c
3 *
4 * This file contains functions which manage high resolution 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 Gleixner79bf2bb2007-02-16 01:28:03 -080018#include <linux/percpu.h>
19#include <linux/profile.h>
20#include <linux/sched.h>
21#include <linux/tick.h>
22
23#include "tick-internal.h"
24
Thomas Gleixner80a05b92010-03-12 17:34:14 +010025/* Limit min_delta to a jiffie */
26#define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
27
28static int tick_increase_min_delta(struct clock_event_device *dev)
29{
30 /* Nothing to do if we already reached the limit */
31 if (dev->min_delta_ns >= MIN_DELTA_LIMIT)
32 return -ETIME;
33
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;
38
39 if (dev->min_delta_ns > MIN_DELTA_LIMIT)
40 dev->min_delta_ns = MIN_DELTA_LIMIT;
41
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;
46}
47
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080048/**
Thomas Gleixner72056562008-09-03 21:37:03 +000049 * tick_program_event internal worker function
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080050 */
Thomas Gleixner1fb9b7d2008-09-03 21:37:14 +000051int tick_dev_program_event(struct clock_event_device *dev, ktime_t expires,
52 int force)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080053{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080054 ktime_t now = ktime_get();
Thomas Gleixner1fb9b7d2008-09-03 21:37:14 +000055 int i;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080056
Thomas Gleixner1fb9b7d2008-09-03 21:37:14 +000057 for (i = 0;;) {
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080058 int ret = clockevents_program_event(dev, expires, now);
59
60 if (!ret || !force)
61 return ret;
Thomas Gleixner1fb9b7d2008-09-03 21:37:14 +000062
Thomas Gleixner80a05b92010-03-12 17:34:14 +010063 dev->retries++;
Thomas Gleixner1fb9b7d2008-09-03 21:37:14 +000064 /*
Thomas Gleixner80a05b92010-03-12 17:34:14 +010065 * We tried 3 times to program the device with the given
66 * min_delta_ns. If that's not working then we increase it
Thomas Gleixner1fb9b7d2008-09-03 21:37:14 +000067 * and emit a warning.
68 */
69 if (++i > 2) {
Thomas Gleixner61c22c32008-09-09 21:38:57 +020070 /* Increase the min. delta and try again */
Thomas Gleixner80a05b92010-03-12 17:34:14 +010071 if (tick_increase_min_delta(dev)) {
72 /*
73 * Get out of the loop if min_delta_ns
74 * hit the limit already. That's
75 * better than staying here forever.
76 *
77 * We clear next_event so we have a
78 * chance that the box survives.
79 */
80 printk(KERN_WARNING
81 "CE: Reprogramming failure. Giving up\n");
82 dev->next_event.tv64 = KTIME_MAX;
83 return -ETIME;
84 }
Thomas Gleixner1fb9b7d2008-09-03 21:37:14 +000085 i = 0;
86 }
87
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080088 now = ktime_get();
Thomas Gleixner1fb9b7d2008-09-03 21:37:14 +000089 expires = ktime_add_ns(now, dev->min_delta_ns);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080090 }
91}
92
93/**
Thomas Gleixner72056562008-09-03 21:37:03 +000094 * tick_program_event
95 */
96int tick_program_event(ktime_t expires, int force)
97{
98 struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
99
Thomas Gleixner1fb9b7d2008-09-03 21:37:14 +0000100 return tick_dev_program_event(dev, expires, force);
Thomas Gleixner72056562008-09-03 21:37:03 +0000101}
102
103/**
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +0100104 * tick_resume_onshot - resume oneshot mode
105 */
106void tick_resume_oneshot(void)
107{
108 struct tick_device *td = &__get_cpu_var(tick_cpu_device);
109 struct clock_event_device *dev = td->evtdev;
110
111 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
112 tick_program_event(ktime_get(), 1);
113}
114
115/**
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800116 * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
117 */
118void tick_setup_oneshot(struct clock_event_device *newdev,
119 void (*handler)(struct clock_event_device *),
120 ktime_t next_event)
121{
122 newdev->event_handler = handler;
123 clockevents_set_mode(newdev, CLOCK_EVT_MODE_ONESHOT);
Thomas Gleixner1fb9b7d2008-09-03 21:37:14 +0000124 tick_dev_program_event(newdev, next_event, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800125}
126
127/**
128 * tick_switch_to_oneshot - switch to oneshot mode
129 */
130int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *))
131{
132 struct tick_device *td = &__get_cpu_var(tick_cpu_device);
133 struct clock_event_device *dev = td->evtdev;
134
135 if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) ||
Ingo Molnar820de5c2007-07-21 04:37:36 -0700136 !tick_device_is_functional(dev)) {
137
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);
148 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800149 return -EINVAL;
Ingo Molnar820de5c2007-07-21 04:37:36 -0700150 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800151
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;
157}
158
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +0200159/**
160 * tick_check_oneshot_mode - check whether the system is in oneshot mode
161 *
162 * returns 1 when either nohz or highres are enabled. otherwise 0.
163 */
164int tick_oneshot_mode_active(void)
165{
166 unsigned long flags;
167 int ret;
168
169 local_irq_save(flags);
170 ret = __get_cpu_var(tick_cpu_device).mode == TICKDEV_MODE_ONESHOT;
171 local_irq_restore(flags);
172
173 return ret;
174}
175
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800176#ifdef CONFIG_HIGH_RES_TIMERS
177/**
178 * tick_init_highres - switch to high resolution mode
179 *
180 * Called with interrupts disabled.
181 */
182int tick_init_highres(void)
183{
184 return tick_switch_to_oneshot(hrtimer_interrupt);
185}
186#endif