blob: d1d799346e04580e4c62155842b8017ec3e2b298 [file] [log] [blame]
Gennady Sharapovcff65c42006-01-18 17:42:42 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include "linux/kernel.h"
7#include "linux/module.h"
8#include "linux/unistd.h"
9#include "linux/stddef.h"
10#include "linux/spinlock.h"
11#include "linux/time.h"
12#include "linux/sched.h"
13#include "linux/interrupt.h"
14#include "linux/init.h"
15#include "linux/delay.h"
Gennady Sharapovcff65c42006-01-18 17:42:42 -080016#include "linux/hrtimer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "asm/irq.h"
18#include "asm/param.h"
19#include "asm/current.h"
20#include "kern_util.h"
21#include "user_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "mode.h"
23#include "os.h"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025int hz(void)
26{
27 return(HZ);
28}
29
30/*
31 * Scheduler clock - returns current time in nanosec units.
32 */
33unsigned long long sched_clock(void)
34{
35 return (unsigned long long)jiffies_64 * (1000000000 / HZ);
36}
37
38/* Changed at early boot */
39int timer_irq_inited = 0;
40
Gennady Sharapovcff65c42006-01-18 17:42:42 -080041static unsigned long long prev_nsecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#ifdef CONFIG_UML_REAL_TIME_CLOCK
43static long long delta; /* Deviation per interval */
44#endif
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046void timer_irq(union uml_pt_regs *regs)
47{
48 unsigned long long ticks = 0;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#ifdef CONFIG_UML_REAL_TIME_CLOCK
Jeff Dike872aaa62006-07-10 04:45:08 -070051 if(prev_nsecs){
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 /* We've had 1 tick */
Gennady Sharapovcff65c42006-01-18 17:42:42 -080053 unsigned long long nsecs = os_nsecs();
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Gennady Sharapovcff65c42006-01-18 17:42:42 -080055 delta += nsecs - prev_nsecs;
56 prev_nsecs = nsecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 /* Protect against the host clock being set backwards */
59 if(delta < 0)
60 delta = 0;
61
Gennady Sharapovcff65c42006-01-18 17:42:42 -080062 ticks += (delta * HZ) / BILLION;
63 delta -= (ticks * BILLION) / HZ;
Jeff Dike872aaa62006-07-10 04:45:08 -070064 }
65 else prev_nsecs = os_nsecs();
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#else
Jeff Dike872aaa62006-07-10 04:45:08 -070067 ticks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 while(ticks > 0){
70 do_IRQ(TIMER_IRQ, regs);
71 ticks--;
72 }
73}
74
Gennady Sharapovcff65c42006-01-18 17:42:42 -080075static DEFINE_SPINLOCK(timer_spinlock);
76
77static unsigned long long local_offset = 0;
78
79static inline unsigned long long get_time(void)
80{
81 unsigned long long nsecs;
82 unsigned long flags;
83
84 spin_lock_irqsave(&timer_spinlock, flags);
85 nsecs = os_nsecs();
86 nsecs += local_offset;
87 spin_unlock_irqrestore(&timer_spinlock, flags);
88
89 return nsecs;
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092irqreturn_t um_timer(int irq, void *dev, struct pt_regs *regs)
93{
Gennady Sharapovcff65c42006-01-18 17:42:42 -080094 unsigned long long nsecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 unsigned long flags;
96
Jeff Dike572e6142006-06-30 01:55:56 -070097 write_seqlock_irqsave(&xtime_lock, flags);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 do_timer(regs);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800100
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800101 nsecs = get_time() + local_offset;
102 xtime.tv_sec = nsecs / NSEC_PER_SEC;
103 xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
Jeff Dike572e6142006-06-30 01:55:56 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 write_sequnlock_irqrestore(&xtime_lock, flags);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800106
Jeff Dike572e6142006-06-30 01:55:56 -0700107 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Jeff Dikeaceb34342006-07-10 04:45:05 -0700110static void register_timer(void)
111{
112 int err;
113
114 err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
115 if(err != 0)
Jeff Dike537ae942006-09-25 23:33:05 -0700116 printk(KERN_ERR "register_timer : request_irq failed - "
Jeff Dikeaceb34342006-07-10 04:45:05 -0700117 "errno = %d\n", -err);
118
119 timer_irq_inited = 1;
120
Jeff Dike537ae942006-09-25 23:33:05 -0700121 err = set_interval(1);
122 if(err != 0)
123 printk(KERN_ERR "register_timer : set_interval failed - "
124 "errno = %d\n", -err);
Jeff Dikeaceb34342006-07-10 04:45:05 -0700125}
126
127extern void (*late_time_init)(void);
128
129void time_init(void)
130{
131 long long nsecs;
132
133 nsecs = os_nsecs();
134 set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
135 -nsecs % BILLION);
136 late_time_init = register_timer;
137}
138
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800139void do_gettimeofday(struct timeval *tv)
140{
141 unsigned long long nsecs = get_time();
142
143 tv->tv_sec = nsecs / NSEC_PER_SEC;
144 /* Careful about calculations here - this was originally done as
145 * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
146 * which gave bogus (> 1000000) values. Dunno why, suspect gcc
147 * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
148 * problem that I missed.
149 */
150 nsecs -= tv->tv_sec * NSEC_PER_SEC;
151 tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
152}
153
154static inline void set_time(unsigned long long nsecs)
155{
156 unsigned long long now;
157 unsigned long flags;
158
159 spin_lock_irqsave(&timer_spinlock, flags);
160 now = os_nsecs();
161 local_offset = nsecs - now;
162 spin_unlock_irqrestore(&timer_spinlock, flags);
163
164 clock_was_set();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800167int do_settimeofday(struct timespec *tv)
168{
169 set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174void timer_handler(int sig, union uml_pt_regs *regs)
175{
176 local_irq_disable();
Jeff Dike7e1f49d2005-07-28 21:16:09 -0700177 irq_enter();
Bodo Stroesser097fdf02006-01-18 17:42:51 -0800178 update_process_times(CHOOSE_MODE(
179 (UPT_SC(regs) && user_context(UPT_SP(regs))),
180 (regs)->skas.is_user));
Jeff Dike7e1f49d2005-07-28 21:16:09 -0700181 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 local_irq_enable();
183 if(current_thread->cpu == 0)
184 timer_irq(regs);
185}