Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[pandora-kernel.git] / arch / um / kernel / time_kern.c
1 /*
2  * 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"
16 #include "linux/hrtimer.h"
17 #include "asm/irq.h"
18 #include "asm/param.h"
19 #include "asm/current.h"
20 #include "kern_util.h"
21 #include "user_util.h"
22 #include "mode.h"
23 #include "os.h"
24
25 int hz(void)
26 {
27         return(HZ);
28 }
29
30 /*
31  * Scheduler clock - returns current time in nanosec units.
32  */
33 unsigned long long sched_clock(void)
34 {
35         return (unsigned long long)jiffies_64 * (1000000000 / HZ);
36 }
37
38 /* Changed at early boot */
39 int timer_irq_inited = 0;
40
41 static int first_tick;
42 static unsigned long long prev_nsecs;
43 #ifdef CONFIG_UML_REAL_TIME_CLOCK
44 static long long delta;                 /* Deviation per interval */
45 #endif
46
47 void timer_irq(union uml_pt_regs *regs)
48 {
49         unsigned long long ticks = 0;
50
51         if(!timer_irq_inited){
52                 /* This is to ensure that ticks don't pile up when
53                  * the timer handler is suspended */
54                 first_tick = 0;
55                 return;
56         }
57
58         if(first_tick){
59 #ifdef CONFIG_UML_REAL_TIME_CLOCK
60                 /* We've had 1 tick */
61                 unsigned long long nsecs = os_nsecs();
62
63                 delta += nsecs - prev_nsecs;
64                 prev_nsecs = nsecs;
65
66                 /* Protect against the host clock being set backwards */
67                 if(delta < 0)
68                         delta = 0;
69
70                 ticks += (delta * HZ) / BILLION;
71                 delta -= (ticks * BILLION) / HZ;
72 #else
73                 ticks = 1;
74 #endif
75         }
76         else {
77                 prev_nsecs = os_nsecs();
78                 first_tick = 1;
79         }
80
81         while(ticks > 0){
82                 do_IRQ(TIMER_IRQ, regs);
83                 ticks--;
84         }
85 }
86
87
88 void time_init_kern(void)
89 {
90         long long nsecs;
91
92         nsecs = os_nsecs();
93         set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
94                                 -nsecs % BILLION);
95 }
96
97 void do_boot_timer_handler(struct sigcontext * sc)
98 {
99         unsigned long flags;
100         struct pt_regs regs;
101
102         CHOOSE_MODE((void) (UPT_SC(&regs.regs) = sc),
103                     (void) (regs.regs.skas.is_user = 0));
104
105         write_seqlock_irqsave(&xtime_lock, flags);
106         do_timer(&regs);
107         write_sequnlock_irqrestore(&xtime_lock, flags);
108 }
109
110 static DEFINE_SPINLOCK(timer_spinlock);
111
112 static unsigned long long local_offset = 0;
113
114 static inline unsigned long long get_time(void)
115 {
116         unsigned long long nsecs;
117         unsigned long flags;
118
119         spin_lock_irqsave(&timer_spinlock, flags);
120         nsecs = os_nsecs();
121         nsecs += local_offset;
122         spin_unlock_irqrestore(&timer_spinlock, flags);
123
124         return nsecs;
125 }
126
127 irqreturn_t um_timer(int irq, void *dev, struct pt_regs *regs)
128 {
129         unsigned long long nsecs;
130         unsigned long flags;
131
132         write_seqlock_irqsave(&xtime_lock, flags);
133
134         do_timer(regs);
135
136         nsecs = get_time() + local_offset;
137         xtime.tv_sec = nsecs / NSEC_PER_SEC;
138         xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
139
140         write_sequnlock_irqrestore(&xtime_lock, flags);
141
142         return IRQ_HANDLED;
143 }
144
145 void do_gettimeofday(struct timeval *tv)
146 {
147         unsigned long long nsecs = get_time();
148
149         tv->tv_sec = nsecs / NSEC_PER_SEC;
150         /* Careful about calculations here - this was originally done as
151          * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
152          * which gave bogus (> 1000000) values.  Dunno why, suspect gcc
153          * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
154          * problem that I missed.
155          */
156         nsecs -= tv->tv_sec * NSEC_PER_SEC;
157         tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
158 }
159
160 static inline void set_time(unsigned long long nsecs)
161 {
162         unsigned long long now;
163         unsigned long flags;
164
165         spin_lock_irqsave(&timer_spinlock, flags);
166         now = os_nsecs();
167         local_offset = nsecs - now;
168         spin_unlock_irqrestore(&timer_spinlock, flags);
169
170         clock_was_set();
171 }
172
173 int do_settimeofday(struct timespec *tv)
174 {
175         set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
176
177         return 0;
178 }
179
180 void timer_handler(int sig, union uml_pt_regs *regs)
181 {
182         local_irq_disable();
183         irq_enter();
184         update_process_times(CHOOSE_MODE(
185                              (UPT_SC(regs) && user_context(UPT_SP(regs))),
186                              (regs)->skas.is_user));
187         irq_exit();
188         local_irq_enable();
189         if(current_thread->cpu == 0)
190                 timer_irq(regs);
191 }
192
193 int __init timer_init(void)
194 {
195         int err;
196
197         user_time_init();
198         err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
199         if(err != 0)
200                 printk(KERN_ERR "timer_init : request_irq failed - "
201                        "errno = %d\n", -err);
202         timer_irq_inited = 1;
203         return(0);
204 }
205
206 arch_initcall(timer_init);