blob: 654dd5df24175d0187cab4a374d55d39c9ea0ba6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/timer.c
3 *
4 * Kernel internal timers, kernel timekeeping, basic process system calls
5 *
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
8 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
9 *
10 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
11 * "A Kernel Model for Precision Timekeeping" by Dave Mills
12 * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
13 * serialize accesses to xtime/lost_ticks).
14 * Copyright (C) 1998 Andrea Arcangeli
15 * 1999-03-10 Improved NTP compatibility by Ulrich Windl
16 * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
17 * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
18 * Copyright (C) 2000, 2001, 2002 Ingo Molnar
19 * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
20 */
21
22#include <linux/kernel_stat.h>
23#include <linux/module.h>
24#include <linux/interrupt.h>
25#include <linux/percpu.h>
26#include <linux/init.h>
27#include <linux/mm.h>
28#include <linux/swap.h>
29#include <linux/notifier.h>
30#include <linux/thread_info.h>
31#include <linux/time.h>
32#include <linux/jiffies.h>
33#include <linux/posix-timers.h>
34#include <linux/cpu.h>
35#include <linux/syscalls.h>
Adrian Bunk97a41e22006-01-08 01:02:17 -080036#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/uaccess.h>
39#include <asm/unistd.h>
40#include <asm/div64.h>
41#include <asm/timex.h>
42#include <asm/io.h>
43
Thomas Gleixnerecea8d12005-10-30 15:03:00 -080044u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
45
46EXPORT_SYMBOL(jiffies_64);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * per-CPU timer vector definitions:
50 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
52#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
53#define TVN_SIZE (1 << TVN_BITS)
54#define TVR_SIZE (1 << TVR_BITS)
55#define TVN_MASK (TVN_SIZE - 1)
56#define TVR_MASK (TVR_SIZE - 1)
57
58typedef struct tvec_s {
59 struct list_head vec[TVN_SIZE];
60} tvec_t;
61
62typedef struct tvec_root_s {
63 struct list_head vec[TVR_SIZE];
64} tvec_root_t;
65
66struct tvec_t_base_s {
Oleg Nesterov3691c512006-03-31 02:30:30 -080067 spinlock_t lock;
68 struct timer_list *running_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unsigned long timer_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 tvec_root_t tv1;
71 tvec_t tv2;
72 tvec_t tv3;
73 tvec_t tv4;
74 tvec_t tv5;
75} ____cacheline_aligned_in_smp;
76
77typedef struct tvec_t_base_s tvec_base_t;
Andrew Mortonba6edfc2006-04-10 22:53:58 -070078
Oleg Nesterov3691c512006-03-31 02:30:30 -080079tvec_base_t boot_tvec_bases;
80EXPORT_SYMBOL(boot_tvec_bases);
Josh Triplett51d8c5e2006-07-30 03:04:14 -070081static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = &boot_tvec_bases;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83static inline void set_running_timer(tvec_base_t *base,
84 struct timer_list *timer)
85{
86#ifdef CONFIG_SMP
Oleg Nesterov3691c512006-03-31 02:30:30 -080087 base->running_timer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
89}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
92{
93 unsigned long expires = timer->expires;
94 unsigned long idx = expires - base->timer_jiffies;
95 struct list_head *vec;
96
97 if (idx < TVR_SIZE) {
98 int i = expires & TVR_MASK;
99 vec = base->tv1.vec + i;
100 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
101 int i = (expires >> TVR_BITS) & TVN_MASK;
102 vec = base->tv2.vec + i;
103 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
104 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
105 vec = base->tv3.vec + i;
106 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
107 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
108 vec = base->tv4.vec + i;
109 } else if ((signed long) idx < 0) {
110 /*
111 * Can happen if you add a timer with expires == jiffies,
112 * or you set a timer to go off in the past
113 */
114 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
115 } else {
116 int i;
117 /* If the timeout is larger than 0xffffffff on 64-bit
118 * architectures then we use the maximum timeout:
119 */
120 if (idx > 0xffffffffUL) {
121 idx = 0xffffffffUL;
122 expires = idx + base->timer_jiffies;
123 }
124 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
125 vec = base->tv5.vec + i;
126 }
127 /*
128 * Timers are FIFO:
129 */
130 list_add_tail(&timer->entry, vec);
131}
132
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700133/**
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700134 * init_timer - initialize a timer.
135 * @timer: the timer to be initialized
136 *
137 * init_timer() must be done to a timer prior calling *any* of the
138 * other timer functions.
139 */
140void fastcall init_timer(struct timer_list *timer)
141{
142 timer->entry.next = NULL;
Paul Mackerrasbfe5d832006-06-25 05:47:14 -0700143 timer->base = __raw_get_cpu_var(tvec_bases);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700144}
145EXPORT_SYMBOL(init_timer);
146
147static inline void detach_timer(struct timer_list *timer,
148 int clear_pending)
149{
150 struct list_head *entry = &timer->entry;
151
152 __list_del(entry->prev, entry->next);
153 if (clear_pending)
154 entry->next = NULL;
155 entry->prev = LIST_POISON2;
156}
157
158/*
Oleg Nesterov3691c512006-03-31 02:30:30 -0800159 * We are using hashed locking: holding per_cpu(tvec_bases).lock
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700160 * means that all timers which are tied to this base via timer->base are
161 * locked, and the base itself is locked too.
162 *
163 * So __run_timers/migrate_timers can safely modify all timers which could
164 * be found on ->tvX lists.
165 *
166 * When the timer's base is locked, and the timer removed from list, it is
167 * possible to set timer->base = NULL and drop the lock: the timer remains
168 * locked.
169 */
Oleg Nesterov3691c512006-03-31 02:30:30 -0800170static tvec_base_t *lock_timer_base(struct timer_list *timer,
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700171 unsigned long *flags)
Josh Triplett89e7e3742006-09-29 01:59:36 -0700172 __acquires(timer->base->lock)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700173{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800174 tvec_base_t *base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700175
176 for (;;) {
177 base = timer->base;
178 if (likely(base != NULL)) {
179 spin_lock_irqsave(&base->lock, *flags);
180 if (likely(base == timer->base))
181 return base;
182 /* The timer has migrated to another CPU */
183 spin_unlock_irqrestore(&base->lock, *flags);
184 }
185 cpu_relax();
186 }
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189int __mod_timer(struct timer_list *timer, unsigned long expires)
190{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800191 tvec_base_t *base, *new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unsigned long flags;
193 int ret = 0;
194
195 BUG_ON(!timer->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700197 base = lock_timer_base(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700199 if (timer_pending(timer)) {
200 detach_timer(timer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 ret = 1;
202 }
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700203
Jan Beulicha4a61982006-03-24 03:15:54 -0800204 new_base = __get_cpu_var(tvec_bases);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700205
Oleg Nesterov3691c512006-03-31 02:30:30 -0800206 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700207 /*
208 * We are trying to schedule the timer on the local CPU.
209 * However we can't change timer's base while it is running,
210 * otherwise del_timer_sync() can't detect that the timer's
211 * handler yet has not finished. This also guarantees that
212 * the timer is serialized wrt itself.
213 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800214 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700215 /* See the comment in lock_timer_base() */
216 timer->base = NULL;
217 spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800218 base = new_base;
219 spin_lock(&base->lock);
220 timer->base = base;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700221 }
222 }
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 timer->expires = expires;
Oleg Nesterova2c348f2006-03-31 02:30:31 -0800225 internal_add_timer(base, timer);
226 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 return ret;
229}
230
231EXPORT_SYMBOL(__mod_timer);
232
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700233/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * add_timer_on - start a timer on a particular CPU
235 * @timer: the timer to be added
236 * @cpu: the CPU to start it on
237 *
238 * This is not very scalable on SMP. Double adds are not possible.
239 */
240void add_timer_on(struct timer_list *timer, int cpu)
241{
Jan Beulicha4a61982006-03-24 03:15:54 -0800242 tvec_base_t *base = per_cpu(tvec_bases, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 BUG_ON(timer_pending(timer) || !timer->function);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800246 spin_lock_irqsave(&base->lock, flags);
247 timer->base = base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 internal_add_timer(base, timer);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800249 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700253/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 * mod_timer - modify a timer's timeout
255 * @timer: the timer to be modified
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700256 * @expires: new timeout in jiffies
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *
258 * mod_timer is a more efficient way to update the expire field of an
259 * active timer (if the timer is inactive it will be activated)
260 *
261 * mod_timer(timer, expires) is equivalent to:
262 *
263 * del_timer(timer); timer->expires = expires; add_timer(timer);
264 *
265 * Note that if there are multiple unserialized concurrent users of the
266 * same timer, then mod_timer() is the only safe way to modify the timeout,
267 * since add_timer() cannot modify an already running timer.
268 *
269 * The function returns whether it has modified a pending timer or not.
270 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
271 * active timer returns 1.)
272 */
273int mod_timer(struct timer_list *timer, unsigned long expires)
274{
275 BUG_ON(!timer->function);
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /*
278 * This is a common optimization triggered by the
279 * networking code - if the timer is re-modified
280 * to be the same thing then just return:
281 */
282 if (timer->expires == expires && timer_pending(timer))
283 return 1;
284
285 return __mod_timer(timer, expires);
286}
287
288EXPORT_SYMBOL(mod_timer);
289
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700290/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * del_timer - deactive a timer.
292 * @timer: the timer to be deactivated
293 *
294 * del_timer() deactivates a timer - this works on both active and inactive
295 * timers.
296 *
297 * The function returns whether it has deactivated a pending timer or not.
298 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
299 * active timer returns 1.)
300 */
301int del_timer(struct timer_list *timer)
302{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800303 tvec_base_t *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700305 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700307 if (timer_pending(timer)) {
308 base = lock_timer_base(timer, &flags);
309 if (timer_pending(timer)) {
310 detach_timer(timer, 1);
311 ret = 1;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700316 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319EXPORT_SYMBOL(del_timer);
320
321#ifdef CONFIG_SMP
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700322/**
323 * try_to_del_timer_sync - Try to deactivate a timer
324 * @timer: timer do del
325 *
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700326 * This function tries to deactivate a timer. Upon successful (ret >= 0)
327 * exit the timer is not queued and the handler is not running on any CPU.
328 *
329 * It must not be called from interrupt contexts.
330 */
331int try_to_del_timer_sync(struct timer_list *timer)
332{
Oleg Nesterov3691c512006-03-31 02:30:30 -0800333 tvec_base_t *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700334 unsigned long flags;
335 int ret = -1;
336
337 base = lock_timer_base(timer, &flags);
338
339 if (base->running_timer == timer)
340 goto out;
341
342 ret = 0;
343 if (timer_pending(timer)) {
344 detach_timer(timer, 1);
345 ret = 1;
346 }
347out:
348 spin_unlock_irqrestore(&base->lock, flags);
349
350 return ret;
351}
352
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700353/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * del_timer_sync - deactivate a timer and wait for the handler to finish.
355 * @timer: the timer to be deactivated
356 *
357 * This function only differs from del_timer() on SMP: besides deactivating
358 * the timer it also makes sure the handler has finished executing on other
359 * CPUs.
360 *
361 * Synchronization rules: callers must prevent restarting of the timer,
362 * otherwise this function is meaningless. It must not be called from
363 * interrupt contexts. The caller must not hold locks which would prevent
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700364 * completion of the timer's handler. The timer's handler must not call
365 * add_timer_on(). Upon exit the timer is not queued and the handler is
366 * not running on any CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *
368 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 */
370int del_timer_sync(struct timer_list *timer)
371{
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700372 for (;;) {
373 int ret = try_to_del_timer_sync(timer);
374 if (ret >= 0)
375 return ret;
Andrew Mortona0009652006-07-14 00:24:06 -0700376 cpu_relax();
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#endif
382
383static int cascade(tvec_base_t *base, tvec_t *tv, int index)
384{
385 /* cascade all the timers from tv up one level */
Porpoise3439dd82006-06-23 02:05:56 -0700386 struct timer_list *timer, *tmp;
387 struct list_head tv_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Porpoise3439dd82006-06-23 02:05:56 -0700389 list_replace_init(tv->vec + index, &tv_list);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /*
Porpoise3439dd82006-06-23 02:05:56 -0700392 * We are removing _all_ timers from the list, so we
393 * don't have to detach them individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
Porpoise3439dd82006-06-23 02:05:56 -0700395 list_for_each_entry_safe(timer, tmp, &tv_list, entry) {
396 BUG_ON(timer->base != base);
397 internal_add_timer(base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 return index;
401}
402
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700403#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
404
405/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * __run_timers - run all expired timers (if any) on this CPU.
407 * @base: the timer vector to be processed.
408 *
409 * This function cascades all vectors and executes all expired timer
410 * vectors.
411 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412static inline void __run_timers(tvec_base_t *base)
413{
414 struct timer_list *timer;
415
Oleg Nesterov3691c512006-03-31 02:30:30 -0800416 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 while (time_after_eq(jiffies, base->timer_jiffies)) {
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700418 struct list_head work_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct list_head *head = &work_list;
420 int index = base->timer_jiffies & TVR_MASK;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /*
423 * Cascade timers:
424 */
425 if (!index &&
426 (!cascade(base, &base->tv2, INDEX(0))) &&
427 (!cascade(base, &base->tv3, INDEX(1))) &&
428 !cascade(base, &base->tv4, INDEX(2)))
429 cascade(base, &base->tv5, INDEX(3));
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700430 ++base->timer_jiffies;
431 list_replace_init(base->tv1.vec + index, &work_list);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700432 while (!list_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 void (*fn)(unsigned long);
434 unsigned long data;
435
436 timer = list_entry(head->next,struct timer_list,entry);
437 fn = timer->function;
438 data = timer->data;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 set_running_timer(base, timer);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700441 detach_timer(timer, 1);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800442 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700444 int preempt_count = preempt_count();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 fn(data);
446 if (preempt_count != preempt_count()) {
Jesper Juhlbe5b4fb2005-06-23 00:09:09 -0700447 printk(KERN_WARNING "huh, entered %p "
448 "with preempt_count %08x, exited"
449 " with %08x?\n",
450 fn, preempt_count,
451 preempt_count());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 BUG();
453 }
454 }
Oleg Nesterov3691c512006-03-31 02:30:30 -0800455 spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 }
458 set_running_timer(base, NULL);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800459 spin_unlock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462#ifdef CONFIG_NO_IDLE_HZ
463/*
464 * Find out when the next timer event is due to happen. This
465 * is used on S/390 to stop all activity when a cpus is idle.
466 * This functions needs to be called disabled.
467 */
468unsigned long next_timer_interrupt(void)
469{
470 tvec_base_t *base;
471 struct list_head *list;
472 struct timer_list *nte;
473 unsigned long expires;
Tony Lindgren69239742006-03-06 15:42:45 -0800474 unsigned long hr_expires = MAX_JIFFY_OFFSET;
475 ktime_t hr_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 tvec_t *varray[4];
477 int i, j;
478
Tony Lindgren69239742006-03-06 15:42:45 -0800479 hr_delta = hrtimer_get_next_event();
480 if (hr_delta.tv64 != KTIME_MAX) {
481 struct timespec tsdelta;
482 tsdelta = ktime_to_timespec(hr_delta);
483 hr_expires = timespec_to_jiffies(&tsdelta);
484 if (hr_expires < 3)
485 return hr_expires + jiffies;
486 }
487 hr_expires += jiffies;
488
Jan Beulicha4a61982006-03-24 03:15:54 -0800489 base = __get_cpu_var(tvec_bases);
Oleg Nesterov3691c512006-03-31 02:30:30 -0800490 spin_lock(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 expires = base->timer_jiffies + (LONG_MAX >> 1);
Al Viro53f087f2006-02-01 05:56:41 -0500492 list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Look for timer events in tv1. */
495 j = base->timer_jiffies & TVR_MASK;
496 do {
497 list_for_each_entry(nte, base->tv1.vec + j, entry) {
498 expires = nte->expires;
499 if (j < (base->timer_jiffies & TVR_MASK))
500 list = base->tv2.vec + (INDEX(0));
501 goto found;
502 }
503 j = (j + 1) & TVR_MASK;
504 } while (j != (base->timer_jiffies & TVR_MASK));
505
506 /* Check tv2-tv5. */
507 varray[0] = &base->tv2;
508 varray[1] = &base->tv3;
509 varray[2] = &base->tv4;
510 varray[3] = &base->tv5;
511 for (i = 0; i < 4; i++) {
512 j = INDEX(i);
513 do {
514 if (list_empty(varray[i]->vec + j)) {
515 j = (j + 1) & TVN_MASK;
516 continue;
517 }
518 list_for_each_entry(nte, varray[i]->vec + j, entry)
519 if (time_before(nte->expires, expires))
520 expires = nte->expires;
521 if (j < (INDEX(i)) && i < 3)
522 list = varray[i + 1]->vec + (INDEX(i + 1));
523 goto found;
524 } while (j != (INDEX(i)));
525 }
526found:
527 if (list) {
528 /*
529 * The search wrapped. We need to look at the next list
530 * from next tv element that would cascade into tv element
531 * where we found the timer element.
532 */
533 list_for_each_entry(nte, list, entry) {
534 if (time_before(nte->expires, expires))
535 expires = nte->expires;
536 }
537 }
Oleg Nesterov3691c512006-03-31 02:30:30 -0800538 spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -0800539
Zachary Amsden0662b712006-05-20 15:00:24 -0700540 /*
541 * It can happen that other CPUs service timer IRQs and increment
542 * jiffies, but we have not yet got a local timer tick to process
543 * the timer wheels. In that case, the expiry time can be before
544 * jiffies, but since the high-resolution timer here is relative to
545 * jiffies, the default expression when high-resolution timers are
546 * not active,
547 *
548 * time_before(MAX_JIFFY_OFFSET + jiffies, expires)
549 *
550 * would falsely evaluate to true. If that is the case, just
551 * return jiffies so that we can immediately fire the local timer
552 */
553 if (time_before(expires, jiffies))
554 return jiffies;
555
Tony Lindgren69239742006-03-06 15:42:45 -0800556 if (time_before(hr_expires, expires))
557 return hr_expires;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return expires;
560}
561#endif
562
563/******************************************************************/
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565/*
566 * The current time
567 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
568 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
569 * at zero at system boot time, so wall_to_monotonic will be negative,
570 * however, we will ALWAYS keep the tv_nsec part positive so we can use
571 * the usual normalization.
572 */
573struct timespec xtime __attribute__ ((aligned (16)));
574struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
575
576EXPORT_SYMBOL(xtime);
577
Paul Mackerras726c14b2006-02-17 10:30:23 +1100578
john stultzad596172006-06-26 00:25:06 -0700579/* XXX - all of this timekeeping code should be later moved to time.c */
580#include <linux/clocksource.h>
581static struct clocksource *clock; /* pointer to current clocksource */
john stultzcf3c7692006-06-26 00:25:08 -0700582
583#ifdef CONFIG_GENERIC_TIME
584/**
585 * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook
586 *
587 * private function, must hold xtime_lock lock when being
588 * called. Returns the number of nanoseconds since the
589 * last call to update_wall_time() (adjusted by NTP scaling)
590 */
591static inline s64 __get_nsec_offset(void)
592{
593 cycle_t cycle_now, cycle_delta;
594 s64 ns_offset;
595
596 /* read clocksource: */
john stultza2752542006-06-26 00:25:14 -0700597 cycle_now = clocksource_read(clock);
john stultzcf3c7692006-06-26 00:25:08 -0700598
599 /* calculate the delta since the last update_wall_time: */
Roman Zippel19923c12006-06-26 00:25:18 -0700600 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
john stultzcf3c7692006-06-26 00:25:08 -0700601
602 /* convert to nanoseconds: */
603 ns_offset = cyc2ns(clock, cycle_delta);
604
605 return ns_offset;
606}
607
608/**
609 * __get_realtime_clock_ts - Returns the time of day in a timespec
610 * @ts: pointer to the timespec to be set
611 *
612 * Returns the time of day in a timespec. Used by
613 * do_gettimeofday() and get_realtime_clock_ts().
614 */
615static inline void __get_realtime_clock_ts(struct timespec *ts)
616{
617 unsigned long seq;
618 s64 nsecs;
619
620 do {
621 seq = read_seqbegin(&xtime_lock);
622
623 *ts = xtime;
624 nsecs = __get_nsec_offset();
625
626 } while (read_seqretry(&xtime_lock, seq));
627
628 timespec_add_ns(ts, nsecs);
629}
630
631/**
john stultza2752542006-06-26 00:25:14 -0700632 * getnstimeofday - Returns the time of day in a timespec
john stultzcf3c7692006-06-26 00:25:08 -0700633 * @ts: pointer to the timespec to be set
634 *
635 * Returns the time of day in a timespec.
636 */
637void getnstimeofday(struct timespec *ts)
638{
639 __get_realtime_clock_ts(ts);
640}
641
642EXPORT_SYMBOL(getnstimeofday);
643
644/**
645 * do_gettimeofday - Returns the time of day in a timeval
646 * @tv: pointer to the timeval to be set
647 *
648 * NOTE: Users should be converted to using get_realtime_clock_ts()
649 */
650void do_gettimeofday(struct timeval *tv)
651{
652 struct timespec now;
653
654 __get_realtime_clock_ts(&now);
655 tv->tv_sec = now.tv_sec;
656 tv->tv_usec = now.tv_nsec/1000;
657}
658
659EXPORT_SYMBOL(do_gettimeofday);
660/**
661 * do_settimeofday - Sets the time of day
662 * @tv: pointer to the timespec variable containing the new time
663 *
664 * Sets the time of day to the new time and update NTP and notify hrtimers
665 */
666int do_settimeofday(struct timespec *tv)
667{
668 unsigned long flags;
669 time_t wtm_sec, sec = tv->tv_sec;
670 long wtm_nsec, nsec = tv->tv_nsec;
671
672 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
673 return -EINVAL;
674
675 write_seqlock_irqsave(&xtime_lock, flags);
676
677 nsec -= __get_nsec_offset();
678
679 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
680 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
681
682 set_normalized_timespec(&xtime, sec, nsec);
683 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
684
Roman Zippele154ff32006-07-10 04:44:32 -0700685 clock->error = 0;
john stultzcf3c7692006-06-26 00:25:08 -0700686 ntp_clear();
687
688 write_sequnlock_irqrestore(&xtime_lock, flags);
689
690 /* signal hrtimers about time change */
691 clock_was_set();
692
693 return 0;
694}
695
696EXPORT_SYMBOL(do_settimeofday);
697
698/**
699 * change_clocksource - Swaps clocksources if a new one is available
700 *
701 * Accumulates current time interval and initializes new clocksource
702 */
703static int change_clocksource(void)
704{
705 struct clocksource *new;
706 cycle_t now;
707 u64 nsec;
john stultza2752542006-06-26 00:25:14 -0700708 new = clocksource_get_next();
john stultzcf3c7692006-06-26 00:25:08 -0700709 if (clock != new) {
john stultza2752542006-06-26 00:25:14 -0700710 now = clocksource_read(new);
john stultzcf3c7692006-06-26 00:25:08 -0700711 nsec = __get_nsec_offset();
712 timespec_add_ns(&xtime, nsec);
713
714 clock = new;
Roman Zippel19923c12006-06-26 00:25:18 -0700715 clock->cycle_last = now;
john stultzcf3c7692006-06-26 00:25:08 -0700716 printk(KERN_INFO "Time: %s clocksource has been installed.\n",
717 clock->name);
718 return 1;
719 } else if (clock->update_callback) {
720 return clock->update_callback();
721 }
722 return 0;
723}
724#else
725#define change_clocksource() (0)
726#endif
727
728/**
729 * timeofday_is_continuous - check to see if timekeeping is free running
730 */
731int timekeeping_is_continuous(void)
732{
733 unsigned long seq;
734 int ret;
735
736 do {
737 seq = read_seqbegin(&xtime_lock);
738
739 ret = clock->is_continuous;
740
741 } while (read_seqretry(&xtime_lock, seq));
742
743 return ret;
744}
745
Paul Mackerras726c14b2006-02-17 10:30:23 +1100746/*
john stultzad596172006-06-26 00:25:06 -0700747 * timekeeping_init - Initializes the clocksource and common timekeeping values
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
john stultzad596172006-06-26 00:25:06 -0700749void __init timekeeping_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
john stultzad596172006-06-26 00:25:06 -0700751 unsigned long flags;
752
753 write_seqlock_irqsave(&xtime_lock, flags);
Roman Zippelb0ee7552006-09-30 23:28:22 -0700754
755 ntp_clear();
756
john stultza2752542006-06-26 00:25:14 -0700757 clock = clocksource_get_next();
758 clocksource_calculate_interval(clock, tick_nsec);
Roman Zippel19923c12006-06-26 00:25:18 -0700759 clock->cycle_last = clocksource_read(clock);
Roman Zippelb0ee7552006-09-30 23:28:22 -0700760
john stultzad596172006-06-26 00:25:06 -0700761 write_sequnlock_irqrestore(&xtime_lock, flags);
762}
763
764
john stultz3e143472006-07-14 00:24:17 -0700765static int timekeeping_suspended;
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700766/**
john stultzad596172006-06-26 00:25:06 -0700767 * timekeeping_resume - Resumes the generic timekeeping subsystem.
768 * @dev: unused
769 *
770 * This is for the generic clocksource timekeeping.
771 * xtime/wall_to_monotonic/jiffies/wall_jiffies/etc are
772 * still managed by arch specific suspend/resume code.
773 */
774static int timekeeping_resume(struct sys_device *dev)
775{
776 unsigned long flags;
777
778 write_seqlock_irqsave(&xtime_lock, flags);
779 /* restart the last cycle value */
Roman Zippel19923c12006-06-26 00:25:18 -0700780 clock->cycle_last = clocksource_read(clock);
john stultz3e143472006-07-14 00:24:17 -0700781 clock->error = 0;
782 timekeeping_suspended = 0;
783 write_sequnlock_irqrestore(&xtime_lock, flags);
784 return 0;
785}
786
787static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
788{
789 unsigned long flags;
790
791 write_seqlock_irqsave(&xtime_lock, flags);
792 timekeeping_suspended = 1;
john stultzad596172006-06-26 00:25:06 -0700793 write_sequnlock_irqrestore(&xtime_lock, flags);
794 return 0;
795}
796
797/* sysfs resume/suspend bits for timekeeping */
798static struct sysdev_class timekeeping_sysclass = {
799 .resume = timekeeping_resume,
john stultz3e143472006-07-14 00:24:17 -0700800 .suspend = timekeeping_suspend,
john stultzad596172006-06-26 00:25:06 -0700801 set_kset_name("timekeeping"),
802};
803
804static struct sys_device device_timer = {
805 .id = 0,
806 .cls = &timekeeping_sysclass,
807};
808
809static int __init timekeeping_init_device(void)
810{
811 int error = sysdev_class_register(&timekeeping_sysclass);
812 if (!error)
813 error = sysdev_register(&device_timer);
814 return error;
815}
816
817device_initcall(timekeeping_init_device);
818
819/*
Roman Zippele154ff32006-07-10 04:44:32 -0700820 * If the error is already larger, we look ahead even further
Roman Zippel19923c12006-06-26 00:25:18 -0700821 * to compensate for late or lost adjustments.
822 */
Roman Zippele154ff32006-07-10 04:44:32 -0700823static __always_inline int clocksource_bigadjust(s64 error, s64 *interval, s64 *offset)
Roman Zippel19923c12006-06-26 00:25:18 -0700824{
Roman Zippele154ff32006-07-10 04:44:32 -0700825 s64 tick_error, i;
826 u32 look_ahead, adj;
827 s32 error2, mult;
Roman Zippel19923c12006-06-26 00:25:18 -0700828
829 /*
Roman Zippele154ff32006-07-10 04:44:32 -0700830 * Use the current error value to determine how much to look ahead.
831 * The larger the error the slower we adjust for it to avoid problems
832 * with losing too many ticks, otherwise we would overadjust and
833 * produce an even larger error. The smaller the adjustment the
834 * faster we try to adjust for it, as lost ticks can do less harm
835 * here. This is tuned so that an error of about 1 msec is adusted
836 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
Roman Zippel19923c12006-06-26 00:25:18 -0700837 */
Roman Zippele154ff32006-07-10 04:44:32 -0700838 error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ);
839 error2 = abs(error2);
840 for (look_ahead = 0; error2 > 0; look_ahead++)
841 error2 >>= 2;
Roman Zippel19923c12006-06-26 00:25:18 -0700842
843 /*
Roman Zippele154ff32006-07-10 04:44:32 -0700844 * Now calculate the error in (1 << look_ahead) ticks, but first
845 * remove the single look ahead already included in the error.
Roman Zippel19923c12006-06-26 00:25:18 -0700846 */
Roman Zippele154ff32006-07-10 04:44:32 -0700847 tick_error = current_tick_length() >> (TICK_LENGTH_SHIFT - clock->shift + 1);
848 tick_error -= clock->xtime_interval >> 1;
849 error = ((error - tick_error) >> look_ahead) + tick_error;
Roman Zippel19923c12006-06-26 00:25:18 -0700850
Roman Zippele154ff32006-07-10 04:44:32 -0700851 /* Finally calculate the adjustment shift value. */
852 i = *interval;
853 mult = 1;
854 if (error < 0) {
855 error = -error;
856 *interval = -*interval;
857 *offset = -*offset;
858 mult = -1;
Roman Zippel19923c12006-06-26 00:25:18 -0700859 }
Roman Zippele154ff32006-07-10 04:44:32 -0700860 for (adj = 0; error > i; adj++)
861 error >>= 1;
Roman Zippel19923c12006-06-26 00:25:18 -0700862
863 *interval <<= adj;
864 *offset <<= adj;
Roman Zippele154ff32006-07-10 04:44:32 -0700865 return mult << adj;
Roman Zippel19923c12006-06-26 00:25:18 -0700866}
867
868/*
869 * Adjust the multiplier to reduce the error value,
870 * this is optimized for the most common adjustments of -1,0,1,
871 * for other values we can do a bit more work.
872 */
873static void clocksource_adjust(struct clocksource *clock, s64 offset)
874{
875 s64 error, interval = clock->cycle_interval;
876 int adj;
877
878 error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1);
879 if (error > interval) {
Roman Zippele154ff32006-07-10 04:44:32 -0700880 error >>= 2;
881 if (likely(error <= interval))
882 adj = 1;
883 else
884 adj = clocksource_bigadjust(error, &interval, &offset);
Roman Zippel19923c12006-06-26 00:25:18 -0700885 } else if (error < -interval) {
Roman Zippele154ff32006-07-10 04:44:32 -0700886 error >>= 2;
887 if (likely(error >= -interval)) {
888 adj = -1;
889 interval = -interval;
890 offset = -offset;
891 } else
892 adj = clocksource_bigadjust(error, &interval, &offset);
Roman Zippel19923c12006-06-26 00:25:18 -0700893 } else
894 return;
895
896 clock->mult += adj;
897 clock->xtime_interval += interval;
898 clock->xtime_nsec -= offset;
899 clock->error -= (interval - offset) << (TICK_LENGTH_SHIFT - clock->shift);
900}
901
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700902/**
john stultzad596172006-06-26 00:25:06 -0700903 * update_wall_time - Uses the current clocksource to increment the wall time
904 *
905 * Called from the timer interrupt, must hold a write on xtime_lock.
906 */
907static void update_wall_time(void)
908{
Roman Zippel19923c12006-06-26 00:25:18 -0700909 cycle_t offset;
john stultzad596172006-06-26 00:25:06 -0700910
john stultz3e143472006-07-14 00:24:17 -0700911 /* Make sure we're fully resumed: */
912 if (unlikely(timekeeping_suspended))
913 return;
john stultz5eb6d202006-06-26 00:25:07 -0700914
Roman Zippel19923c12006-06-26 00:25:18 -0700915#ifdef CONFIG_GENERIC_TIME
916 offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
917#else
918 offset = clock->cycle_interval;
919#endif
john stultz3e143472006-07-14 00:24:17 -0700920 clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift;
john stultzad596172006-06-26 00:25:06 -0700921
922 /* normally this loop will run just once, however in the
923 * case of lost or late ticks, it will accumulate correctly.
924 */
Roman Zippel19923c12006-06-26 00:25:18 -0700925 while (offset >= clock->cycle_interval) {
john stultzad596172006-06-26 00:25:06 -0700926 /* accumulate one interval */
Roman Zippel19923c12006-06-26 00:25:18 -0700927 clock->xtime_nsec += clock->xtime_interval;
928 clock->cycle_last += clock->cycle_interval;
929 offset -= clock->cycle_interval;
930
931 if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
932 clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
933 xtime.tv_sec++;
934 second_overflow();
935 }
john stultzad596172006-06-26 00:25:06 -0700936
john stultz5eb6d202006-06-26 00:25:07 -0700937 /* interpolator bits */
Roman Zippel19923c12006-06-26 00:25:18 -0700938 time_interpolator_update(clock->xtime_interval
john stultz5eb6d202006-06-26 00:25:07 -0700939 >> clock->shift);
john stultz5eb6d202006-06-26 00:25:07 -0700940
941 /* accumulate error between NTP and clock interval */
Roman Zippel19923c12006-06-26 00:25:18 -0700942 clock->error += current_tick_length();
943 clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift);
john stultzad596172006-06-26 00:25:06 -0700944 }
Roman Zippel19923c12006-06-26 00:25:18 -0700945
946 /* correct the clock when NTP error is too big */
947 clocksource_adjust(clock, offset);
948
john stultz5eb6d202006-06-26 00:25:07 -0700949 /* store full nanoseconds into xtime */
Roman Zippele154ff32006-07-10 04:44:32 -0700950 xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
Roman Zippel19923c12006-06-26 00:25:18 -0700951 clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
john stultzcf3c7692006-06-26 00:25:08 -0700952
953 /* check to see if there is a new clocksource to use */
954 if (change_clocksource()) {
Roman Zippel19923c12006-06-26 00:25:18 -0700955 clock->error = 0;
956 clock->xtime_nsec = 0;
john stultza2752542006-06-26 00:25:14 -0700957 clocksource_calculate_interval(clock, tick_nsec);
john stultzcf3c7692006-06-26 00:25:08 -0700958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961/*
962 * Called from the timer interrupt handler to charge one tick to the current
963 * process. user_tick is 1 if the tick is user time, 0 for system.
964 */
965void update_process_times(int user_tick)
966{
967 struct task_struct *p = current;
968 int cpu = smp_processor_id();
969
970 /* Note: this timer irq context must be accounted for as well. */
971 if (user_tick)
972 account_user_time(p, jiffies_to_cputime(1));
973 else
974 account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
975 run_local_timers();
976 if (rcu_pending(cpu))
977 rcu_check_callbacks(cpu, user_tick);
978 scheduler_tick();
979 run_posix_cpu_timers(p);
980}
981
982/*
983 * Nr of active tasks - counted in fixed-point numbers
984 */
985static unsigned long count_active_tasks(void)
986{
Jack Steinerdb1b1fe2006-03-31 02:31:21 -0800987 return nr_active() * FIXED_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990/*
991 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
992 * imply that avenrun[] is the standard name for this kind of thing.
993 * Nothing else seems to be standardized: the fractional size etc
994 * all seem to differ on different machines.
995 *
996 * Requires xtime_lock to access.
997 */
998unsigned long avenrun[3];
999
1000EXPORT_SYMBOL(avenrun);
1001
1002/*
1003 * calc_load - given tick count, update the avenrun load estimates.
1004 * This is called while holding a write_lock on xtime_lock.
1005 */
1006static inline void calc_load(unsigned long ticks)
1007{
1008 unsigned long active_tasks; /* fixed-point */
1009 static int count = LOAD_FREQ;
1010
Atsushi Nemoto3171a032006-09-29 02:00:32 -07001011 active_tasks = count_active_tasks();
1012 for (count -= ticks; count < 0; count += LOAD_FREQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
1014 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
1015 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
1016 }
1017}
1018
1019/* jiffies at the most recent update of wall time */
1020unsigned long wall_jiffies = INITIAL_JIFFIES;
1021
1022/*
1023 * This read-write spinlock protects us from races in SMP while
1024 * playing with xtime and avenrun.
1025 */
1026#ifndef ARCH_HAVE_XTIME_LOCK
Ingo Molnare4d91912006-07-03 00:24:34 -07001027__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029EXPORT_SYMBOL(xtime_lock);
1030#endif
1031
1032/*
1033 * This function runs timers and the timer-tq in bottom half context.
1034 */
1035static void run_timer_softirq(struct softirq_action *h)
1036{
Jan Beulicha4a61982006-03-24 03:15:54 -08001037 tvec_base_t *base = __get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001039 hrtimer_run_queues();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (time_after_eq(jiffies, base->timer_jiffies))
1041 __run_timers(base);
1042}
1043
1044/*
1045 * Called by the local, per-CPU timer interrupt on SMP.
1046 */
1047void run_local_timers(void)
1048{
1049 raise_softirq(TIMER_SOFTIRQ);
Ingo Molnar6687a972006-03-24 03:18:41 -08001050 softlockup_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053/*
1054 * Called by the timer interrupt. xtime_lock must already be taken
1055 * by the timer IRQ!
1056 */
Atsushi Nemoto3171a032006-09-29 02:00:32 -07001057static inline void update_times(unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
john stultzad596172006-06-26 00:25:06 -07001059 wall_jiffies += ticks;
1060 update_wall_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 calc_load(ticks);
1062}
1063
1064/*
1065 * The 64-bit jiffies value is not atomic - you MUST NOT read it
1066 * without sampling the sequence number in xtime_lock.
1067 * jiffies is defined in the linker script...
1068 */
1069
Atsushi Nemoto3171a032006-09-29 02:00:32 -07001070void do_timer(unsigned long ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Atsushi Nemoto3171a032006-09-29 02:00:32 -07001072 jiffies_64 += ticks;
1073 update_times(ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
1076#ifdef __ARCH_WANT_SYS_ALARM
1077
1078/*
1079 * For backwards compatibility? This can be done in libc so Alpha
1080 * and all newer ports shouldn't need it.
1081 */
1082asmlinkage unsigned long sys_alarm(unsigned int seconds)
1083{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -08001084 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
1087#endif
1088
1089#ifndef __alpha__
1090
1091/*
1092 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
1093 * should be moved into arch/i386 instead?
1094 */
1095
1096/**
1097 * sys_getpid - return the thread group id of the current process
1098 *
1099 * Note, despite the name, this returns the tgid not the pid. The tgid and
1100 * the pid are identical unless CLONE_THREAD was specified on clone() in
1101 * which case the tgid is the same in all threads of the same group.
1102 *
1103 * This is SMP safe as current->tgid does not change.
1104 */
1105asmlinkage long sys_getpid(void)
1106{
1107 return current->tgid;
1108}
1109
1110/*
Kirill Korotaev6997a6f2006-08-13 23:24:23 -07001111 * Accessing ->real_parent is not SMP-safe, it could
1112 * change from under us. However, we can use a stale
1113 * value of ->real_parent under rcu_read_lock(), see
1114 * release_task()->call_rcu(delayed_put_task_struct).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 */
1116asmlinkage long sys_getppid(void)
1117{
1118 int pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Kirill Korotaev6997a6f2006-08-13 23:24:23 -07001120 rcu_read_lock();
1121 pid = rcu_dereference(current->real_parent)->tgid;
1122 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return pid;
1125}
1126
1127asmlinkage long sys_getuid(void)
1128{
1129 /* Only we change this so SMP safe */
1130 return current->uid;
1131}
1132
1133asmlinkage long sys_geteuid(void)
1134{
1135 /* Only we change this so SMP safe */
1136 return current->euid;
1137}
1138
1139asmlinkage long sys_getgid(void)
1140{
1141 /* Only we change this so SMP safe */
1142 return current->gid;
1143}
1144
1145asmlinkage long sys_getegid(void)
1146{
1147 /* Only we change this so SMP safe */
1148 return current->egid;
1149}
1150
1151#endif
1152
1153static void process_timeout(unsigned long __data)
1154{
Ingo Molnar36c8b582006-07-03 00:25:41 -07001155 wake_up_process((struct task_struct *)__data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158/**
1159 * schedule_timeout - sleep until timeout
1160 * @timeout: timeout value in jiffies
1161 *
1162 * Make the current task sleep until @timeout jiffies have
1163 * elapsed. The routine will return immediately unless
1164 * the current task state has been set (see set_current_state()).
1165 *
1166 * You can set the task state as follows -
1167 *
1168 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1169 * pass before the routine returns. The routine will return 0
1170 *
1171 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1172 * delivered to the current task. In this case the remaining time
1173 * in jiffies will be returned, or 0 if the timer expired in time
1174 *
1175 * The current task state is guaranteed to be TASK_RUNNING when this
1176 * routine returns.
1177 *
1178 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1179 * the CPU away without a bound on the timeout. In this case the return
1180 * value will be %MAX_SCHEDULE_TIMEOUT.
1181 *
1182 * In all cases the return value is guaranteed to be non-negative.
1183 */
1184fastcall signed long __sched schedule_timeout(signed long timeout)
1185{
1186 struct timer_list timer;
1187 unsigned long expire;
1188
1189 switch (timeout)
1190 {
1191 case MAX_SCHEDULE_TIMEOUT:
1192 /*
1193 * These two special cases are useful to be comfortable
1194 * in the caller. Nothing more. We could take
1195 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1196 * but I' d like to return a valid offset (>=0) to allow
1197 * the caller to do everything it want with the retval.
1198 */
1199 schedule();
1200 goto out;
1201 default:
1202 /*
1203 * Another bit of PARANOID. Note that the retval will be
1204 * 0 since no piece of kernel is supposed to do a check
1205 * for a negative retval of schedule_timeout() (since it
1206 * should never happens anyway). You just have the printk()
1207 * that will tell you if something is gone wrong and where.
1208 */
1209 if (timeout < 0)
1210 {
1211 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Mortona5a0d522005-10-30 15:01:42 -08001212 "value %lx from %p\n", timeout,
1213 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 current->state = TASK_RUNNING;
1215 goto out;
1216 }
1217 }
1218
1219 expire = timeout + jiffies;
1220
Oleg Nesterova8db2db2005-10-30 15:01:38 -08001221 setup_timer(&timer, process_timeout, (unsigned long)current);
1222 __mod_timer(&timer, expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 schedule();
1224 del_singleshot_timer_sync(&timer);
1225
1226 timeout = expire - jiffies;
1227
1228 out:
1229 return timeout < 0 ? 0 : timeout;
1230}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231EXPORT_SYMBOL(schedule_timeout);
1232
Andrew Morton8a1c1752005-09-13 01:25:15 -07001233/*
1234 * We can use __set_current_state() here because schedule_timeout() calls
1235 * schedule() unconditionally.
1236 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001237signed long __sched schedule_timeout_interruptible(signed long timeout)
1238{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001239 __set_current_state(TASK_INTERRUPTIBLE);
1240 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001241}
1242EXPORT_SYMBOL(schedule_timeout_interruptible);
1243
1244signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1245{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001246 __set_current_state(TASK_UNINTERRUPTIBLE);
1247 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001248}
1249EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251/* Thread ID - the internal kernel "pid" */
1252asmlinkage long sys_gettid(void)
1253{
1254 return current->pid;
1255}
1256
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001257/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 * sys_sysinfo - fill in sysinfo struct
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001259 * @info: pointer to buffer to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 */
1261asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1262{
1263 struct sysinfo val;
1264 unsigned long mem_total, sav_total;
1265 unsigned int mem_unit, bitcount;
1266 unsigned long seq;
1267
1268 memset((char *)&val, 0, sizeof(struct sysinfo));
1269
1270 do {
1271 struct timespec tp;
1272 seq = read_seqbegin(&xtime_lock);
1273
1274 /*
1275 * This is annoying. The below is the same thing
1276 * posix_get_clock_monotonic() does, but it wants to
1277 * take the lock which we want to cover the loads stuff
1278 * too.
1279 */
1280
1281 getnstimeofday(&tp);
1282 tp.tv_sec += wall_to_monotonic.tv_sec;
1283 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1284 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1285 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1286 tp.tv_sec++;
1287 }
1288 val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1289
1290 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1291 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1292 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1293
1294 val.procs = nr_threads;
1295 } while (read_seqretry(&xtime_lock, seq));
1296
1297 si_meminfo(&val);
1298 si_swapinfo(&val);
1299
1300 /*
1301 * If the sum of all the available memory (i.e. ram + swap)
1302 * is less than can be stored in a 32 bit unsigned long then
1303 * we can be binary compatible with 2.2.x kernels. If not,
1304 * well, in that case 2.2.x was broken anyways...
1305 *
1306 * -Erik Andersen <andersee@debian.org>
1307 */
1308
1309 mem_total = val.totalram + val.totalswap;
1310 if (mem_total < val.totalram || mem_total < val.totalswap)
1311 goto out;
1312 bitcount = 0;
1313 mem_unit = val.mem_unit;
1314 while (mem_unit > 1) {
1315 bitcount++;
1316 mem_unit >>= 1;
1317 sav_total = mem_total;
1318 mem_total <<= 1;
1319 if (mem_total < sav_total)
1320 goto out;
1321 }
1322
1323 /*
1324 * If mem_total did not overflow, multiply all memory values by
1325 * val.mem_unit and set it to 1. This leaves things compatible
1326 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1327 * kernels...
1328 */
1329
1330 val.mem_unit = 1;
1331 val.totalram <<= bitcount;
1332 val.freeram <<= bitcount;
1333 val.sharedram <<= bitcount;
1334 val.bufferram <<= bitcount;
1335 val.totalswap <<= bitcount;
1336 val.freeswap <<= bitcount;
1337 val.totalhigh <<= bitcount;
1338 val.freehigh <<= bitcount;
1339
1340 out:
1341 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1342 return -EFAULT;
1343
1344 return 0;
1345}
1346
Ingo Molnard730e882006-07-03 00:25:10 -07001347/*
1348 * lockdep: we want to track each per-CPU base as a separate lock-class,
1349 * but timer-bases are kmalloc()-ed, so we need to attach separate
1350 * keys to them:
1351 */
1352static struct lock_class_key base_lock_keys[NR_CPUS];
1353
Jan Beulicha4a61982006-03-24 03:15:54 -08001354static int __devinit init_timers_cpu(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 int j;
1357 tvec_base_t *base;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001358 static char __devinitdata tvec_base_done[NR_CPUS];
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001359
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001360 if (!tvec_base_done[cpu]) {
Jan Beulicha4a61982006-03-24 03:15:54 -08001361 static char boot_done;
1362
Jan Beulicha4a61982006-03-24 03:15:54 -08001363 if (boot_done) {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001364 /*
1365 * The APs use this path later in boot
1366 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001367 base = kmalloc_node(sizeof(*base), GFP_KERNEL,
1368 cpu_to_node(cpu));
1369 if (!base)
1370 return -ENOMEM;
1371 memset(base, 0, sizeof(*base));
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001372 per_cpu(tvec_bases, cpu) = base;
Jan Beulicha4a61982006-03-24 03:15:54 -08001373 } else {
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001374 /*
1375 * This is for the boot CPU - we use compile-time
1376 * static initialisation because per-cpu memory isn't
1377 * ready yet and because the memory allocators are not
1378 * initialised either.
1379 */
Jan Beulicha4a61982006-03-24 03:15:54 -08001380 boot_done = 1;
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001381 base = &boot_tvec_bases;
Jan Beulicha4a61982006-03-24 03:15:54 -08001382 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001383 tvec_base_done[cpu] = 1;
1384 } else {
1385 base = per_cpu(tvec_bases, cpu);
Jan Beulicha4a61982006-03-24 03:15:54 -08001386 }
Andrew Mortonba6edfc2006-04-10 22:53:58 -07001387
Oleg Nesterov3691c512006-03-31 02:30:30 -08001388 spin_lock_init(&base->lock);
Ingo Molnard730e882006-07-03 00:25:10 -07001389 lockdep_set_class(&base->lock, base_lock_keys + cpu);
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 for (j = 0; j < TVN_SIZE; j++) {
1392 INIT_LIST_HEAD(base->tv5.vec + j);
1393 INIT_LIST_HEAD(base->tv4.vec + j);
1394 INIT_LIST_HEAD(base->tv3.vec + j);
1395 INIT_LIST_HEAD(base->tv2.vec + j);
1396 }
1397 for (j = 0; j < TVR_SIZE; j++)
1398 INIT_LIST_HEAD(base->tv1.vec + j);
1399
1400 base->timer_jiffies = jiffies;
Jan Beulicha4a61982006-03-24 03:15:54 -08001401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
1404#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001405static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 struct timer_list *timer;
1408
1409 while (!list_empty(head)) {
1410 timer = list_entry(head->next, struct timer_list, entry);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001411 detach_timer(timer, 0);
Oleg Nesterov3691c512006-03-31 02:30:30 -08001412 timer->base = new_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
1417static void __devinit migrate_timers(int cpu)
1418{
1419 tvec_base_t *old_base;
1420 tvec_base_t *new_base;
1421 int i;
1422
1423 BUG_ON(cpu_online(cpu));
Jan Beulicha4a61982006-03-24 03:15:54 -08001424 old_base = per_cpu(tvec_bases, cpu);
1425 new_base = get_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 local_irq_disable();
Oleg Nesterov3691c512006-03-31 02:30:30 -08001428 spin_lock(&new_base->lock);
1429 spin_lock(&old_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Oleg Nesterov3691c512006-03-31 02:30:30 -08001431 BUG_ON(old_base->running_timer);
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 for (i = 0; i < TVR_SIZE; i++)
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001434 migrate_timer_list(new_base, old_base->tv1.vec + i);
1435 for (i = 0; i < TVN_SIZE; i++) {
1436 migrate_timer_list(new_base, old_base->tv2.vec + i);
1437 migrate_timer_list(new_base, old_base->tv3.vec + i);
1438 migrate_timer_list(new_base, old_base->tv4.vec + i);
1439 migrate_timer_list(new_base, old_base->tv5.vec + i);
1440 }
1441
Oleg Nesterov3691c512006-03-31 02:30:30 -08001442 spin_unlock(&old_base->lock);
1443 spin_unlock(&new_base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 local_irq_enable();
1445 put_cpu_var(tvec_bases);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447#endif /* CONFIG_HOTPLUG_CPU */
1448
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001449static int __cpuinit timer_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 unsigned long action, void *hcpu)
1451{
1452 long cpu = (long)hcpu;
1453 switch(action) {
1454 case CPU_UP_PREPARE:
Jan Beulicha4a61982006-03-24 03:15:54 -08001455 if (init_timers_cpu(cpu) < 0)
1456 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 break;
1458#ifdef CONFIG_HOTPLUG_CPU
1459 case CPU_DEAD:
1460 migrate_timers(cpu);
1461 break;
1462#endif
1463 default:
1464 break;
1465 }
1466 return NOTIFY_OK;
1467}
1468
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001469static struct notifier_block __cpuinitdata timers_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 .notifier_call = timer_cpu_notify,
1471};
1472
1473
1474void __init init_timers(void)
1475{
Akinobu Mita07dccf32006-09-29 02:00:22 -07001476 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 (void *)(long)smp_processor_id());
Akinobu Mita07dccf32006-09-29 02:00:22 -07001478
1479 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 register_cpu_notifier(&timers_nb);
1481 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1482}
1483
1484#ifdef CONFIG_TIME_INTERPOLATION
1485
Christoph Lameter67890d72006-03-16 23:04:00 -08001486struct time_interpolator *time_interpolator __read_mostly;
1487static struct time_interpolator *time_interpolator_list __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488static DEFINE_SPINLOCK(time_interpolator_lock);
1489
1490static inline u64 time_interpolator_get_cycles(unsigned int src)
1491{
1492 unsigned long (*x)(void);
1493
1494 switch (src)
1495 {
1496 case TIME_SOURCE_FUNCTION:
1497 x = time_interpolator->addr;
1498 return x();
1499
1500 case TIME_SOURCE_MMIO64 :
Christoph Lameter685db652006-03-02 02:54:35 -08001501 return readq_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 case TIME_SOURCE_MMIO32 :
Christoph Lameter685db652006-03-02 02:54:35 -08001504 return readl_relaxed((void __iomem *)time_interpolator->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 default: return get_cycles();
1507 }
1508}
1509
Alex Williamson486d46a2005-09-06 15:17:04 -07001510static inline u64 time_interpolator_get_counter(int writelock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 unsigned int src = time_interpolator->source;
1513
1514 if (time_interpolator->jitter)
1515 {
1516 u64 lcycle;
1517 u64 now;
1518
1519 do {
1520 lcycle = time_interpolator->last_cycle;
1521 now = time_interpolator_get_cycles(src);
1522 if (lcycle && time_after(lcycle, now))
1523 return lcycle;
Alex Williamson486d46a2005-09-06 15:17:04 -07001524
1525 /* When holding the xtime write lock, there's no need
1526 * to add the overhead of the cmpxchg. Readers are
1527 * force to retry until the write lock is released.
1528 */
1529 if (writelock) {
1530 time_interpolator->last_cycle = now;
1531 return now;
1532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /* Keep track of the last timer value returned. The use of cmpxchg here
1534 * will cause contention in an SMP environment.
1535 */
1536 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
1537 return now;
1538 }
1539 else
1540 return time_interpolator_get_cycles(src);
1541}
1542
1543void time_interpolator_reset(void)
1544{
1545 time_interpolator->offset = 0;
Alex Williamson486d46a2005-09-06 15:17:04 -07001546 time_interpolator->last_counter = time_interpolator_get_counter(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
1549#define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
1550
1551unsigned long time_interpolator_get_offset(void)
1552{
1553 /* If we do not have a time interpolator set up then just return zero */
1554 if (!time_interpolator)
1555 return 0;
1556
1557 return time_interpolator->offset +
Alex Williamson486d46a2005-09-06 15:17:04 -07001558 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561#define INTERPOLATOR_ADJUST 65536
1562#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1563
john stultz4c7ee8d2006-09-30 23:28:22 -07001564void time_interpolator_update(long delta_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 u64 counter;
1567 unsigned long offset;
1568
1569 /* If there is no time interpolator set up then do nothing */
1570 if (!time_interpolator)
1571 return;
1572
Andrew Mortona5a0d522005-10-30 15:01:42 -08001573 /*
1574 * The interpolator compensates for late ticks by accumulating the late
1575 * time in time_interpolator->offset. A tick earlier than expected will
1576 * lead to a reset of the offset and a corresponding jump of the clock
1577 * forward. Again this only works if the interpolator clock is running
1578 * slightly slower than the regular clock and the tuning logic insures
1579 * that.
1580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Alex Williamson486d46a2005-09-06 15:17:04 -07001582 counter = time_interpolator_get_counter(1);
Andrew Mortona5a0d522005-10-30 15:01:42 -08001583 offset = time_interpolator->offset +
1584 GET_TI_NSECS(counter, time_interpolator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
1587 time_interpolator->offset = offset - delta_nsec;
1588 else {
1589 time_interpolator->skips++;
1590 time_interpolator->ns_skipped += delta_nsec - offset;
1591 time_interpolator->offset = 0;
1592 }
1593 time_interpolator->last_counter = counter;
1594
1595 /* Tuning logic for time interpolator invoked every minute or so.
1596 * Decrease interpolator clock speed if no skips occurred and an offset is carried.
1597 * Increase interpolator clock speed if we skip too much time.
1598 */
1599 if (jiffies % INTERPOLATOR_ADJUST == 0)
1600 {
Jordan Hargraveb20367a2006-04-07 19:50:18 +02001601 if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 time_interpolator->nsec_per_cyc--;
1603 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
1604 time_interpolator->nsec_per_cyc++;
1605 time_interpolator->skips = 0;
1606 time_interpolator->ns_skipped = 0;
1607 }
1608}
1609
1610static inline int
1611is_better_time_interpolator(struct time_interpolator *new)
1612{
1613 if (!time_interpolator)
1614 return 1;
1615 return new->frequency > 2*time_interpolator->frequency ||
1616 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1617}
1618
1619void
1620register_time_interpolator(struct time_interpolator *ti)
1621{
1622 unsigned long flags;
1623
1624 /* Sanity check */
Eric Sesterhenn9f312522006-04-02 13:45:55 +02001625 BUG_ON(ti->frequency == 0 || ti->mask == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
1628 spin_lock(&time_interpolator_lock);
1629 write_seqlock_irqsave(&xtime_lock, flags);
1630 if (is_better_time_interpolator(ti)) {
1631 time_interpolator = ti;
1632 time_interpolator_reset();
1633 }
1634 write_sequnlock_irqrestore(&xtime_lock, flags);
1635
1636 ti->next = time_interpolator_list;
1637 time_interpolator_list = ti;
1638 spin_unlock(&time_interpolator_lock);
1639}
1640
1641void
1642unregister_time_interpolator(struct time_interpolator *ti)
1643{
1644 struct time_interpolator *curr, **prev;
1645 unsigned long flags;
1646
1647 spin_lock(&time_interpolator_lock);
1648 prev = &time_interpolator_list;
1649 for (curr = *prev; curr; curr = curr->next) {
1650 if (curr == ti) {
1651 *prev = curr->next;
1652 break;
1653 }
1654 prev = &curr->next;
1655 }
1656
1657 write_seqlock_irqsave(&xtime_lock, flags);
1658 if (ti == time_interpolator) {
1659 /* we lost the best time-interpolator: */
1660 time_interpolator = NULL;
1661 /* find the next-best interpolator */
1662 for (curr = time_interpolator_list; curr; curr = curr->next)
1663 if (is_better_time_interpolator(curr))
1664 time_interpolator = curr;
1665 time_interpolator_reset();
1666 }
1667 write_sequnlock_irqrestore(&xtime_lock, flags);
1668 spin_unlock(&time_interpolator_lock);
1669}
1670#endif /* CONFIG_TIME_INTERPOLATION */
1671
1672/**
1673 * msleep - sleep safely even with waitqueue interruptions
1674 * @msecs: Time in milliseconds to sleep for
1675 */
1676void msleep(unsigned int msecs)
1677{
1678 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1679
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001680 while (timeout)
1681 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
1684EXPORT_SYMBOL(msleep);
1685
1686/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07001687 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 * @msecs: Time in milliseconds to sleep for
1689 */
1690unsigned long msleep_interruptible(unsigned int msecs)
1691{
1692 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
1693
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07001694 while (timeout && !signal_pending(current))
1695 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return jiffies_to_msecs(timeout);
1697}
1698
1699EXPORT_SYMBOL(msleep_interruptible);