uml-patch-2.6.6-1
[linux-2.6.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 "asm/irq.h"
17 #include "asm/param.h"
18 #include "asm/current.h"
19 #include "kern_util.h"
20 #include "user_util.h"
21 #include "time_user.h"
22 #include "mode.h"
23
24 u64 jiffies_64;
25
26 EXPORT_SYMBOL(jiffies_64);
27
28 int hz(void)
29 {
30         return(HZ);
31 }
32
33 /*
34  * Scheduler clock - returns current time in nanosec units.
35  */
36 unsigned long long sched_clock(void)
37 {
38         return (unsigned long long)jiffies_64 * (1000000000 / HZ);
39 }
40
41 /* Changed at early boot */
42 int timer_irq_inited = 0;
43
44 static int first_tick;
45 static unsigned long long prev_tsc;
46 static long long delta;                 /* Deviation per interval */
47
48 extern unsigned long long host_hz;
49
50 void timer_irq(union uml_pt_regs *regs)
51 {
52         unsigned long long ticks = 0;
53
54         if(!timer_irq_inited){
55                 /* This is to ensure that ticks don't pile up when
56                  * the timer handler is suspended */
57                 first_tick = 0;
58                 return;
59         }
60
61         if(first_tick){
62 #if defined(CONFIG_UML_REAL_TIME_CLOCK)
63                 unsigned long long tsc;
64                 /* We've had 1 tick */
65                 tsc = time_stamp();
66
67                 delta += tsc - prev_tsc;
68                 prev_tsc = tsc;
69
70                 ticks += (delta * HZ) / host_hz;
71                 delta -= (ticks * host_hz) / HZ;
72 #else
73                 ticks = 1;
74 #endif
75         }
76         else {
77                 prev_tsc = time_stamp();
78                 first_tick = 1;
79         }
80
81         while(ticks > 0){
82                 do_IRQ(TIMER_IRQ, regs);
83                 ticks--;
84         }
85 }
86
87 void boot_timer_handler(int sig)
88 {
89         struct pt_regs regs;
90
91         CHOOSE_MODE((void) 
92                     (UPT_SC(&regs.regs) = (struct sigcontext *) (&sig + 1)),
93                     (void) (regs.regs.skas.is_user = 0));
94         do_timer(&regs);
95 }
96
97 irqreturn_t um_timer(int irq, void *dev, struct pt_regs *regs)
98 {
99         unsigned long flags;
100
101         do_timer(regs);
102         write_seqlock_irqsave(&xtime_lock, flags);
103         timer();
104         write_sequnlock_irqrestore(&xtime_lock, flags);
105         return(IRQ_HANDLED);
106 }
107
108 long um_time(int * tloc)
109 {
110         struct timeval now;
111
112         do_gettimeofday(&now);
113         if (tloc) {
114                 if (put_user(now.tv_sec,tloc))
115                         now.tv_sec = -EFAULT;
116         }
117         return now.tv_sec;
118 }
119
120 long um_stime(int * tptr)
121 {
122         int value;
123         struct timespec new;
124
125         if (get_user(value, tptr))
126                 return -EFAULT;
127         new.tv_sec = value;
128         new.tv_nsec = 0;
129         do_settimeofday(&new);
130         return 0;
131 }
132
133 /* XXX Needs to be moved under sys-i386 */
134 void __delay(um_udelay_t time)
135 {
136         /* Stolen from the i386 __loop_delay */
137         int d0;
138         __asm__ __volatile__(
139                 "\tjmp 1f\n"
140                 ".align 16\n"
141                 "1:\tjmp 2f\n"
142                 ".align 16\n"
143                 "2:\tdecl %0\n\tjns 2b"
144                 :"=&a" (d0)
145                 :"0" (time));
146 }
147
148 void __udelay(um_udelay_t usecs)
149 {
150         int i, n;
151
152         n = (loops_per_jiffy * HZ * usecs) / 1000000;
153         for(i=0;i<n;i++) ;
154 }
155
156 void __const_udelay(um_udelay_t usecs)
157 {
158         int i, n;
159
160         n = (loops_per_jiffy * HZ * usecs) / 1000000;
161         for(i=0;i<n;i++) ;
162 }
163
164 void timer_handler(int sig, union uml_pt_regs *regs)
165 {
166 #ifdef CONFIG_SMP
167         local_irq_disable();
168         update_process_times(user_context(UPT_SP(regs)));
169         local_irq_enable();
170 #endif
171         if(current_thread->cpu == 0)
172                 timer_irq(regs);
173 }
174
175 static spinlock_t timer_spinlock = SPIN_LOCK_UNLOCKED;
176
177 unsigned long time_lock(void)
178 {
179         unsigned long flags;
180
181         spin_lock_irqsave(&timer_spinlock, flags);
182         return(flags);
183 }
184
185 void time_unlock(unsigned long flags)
186 {
187         spin_unlock_irqrestore(&timer_spinlock, flags);
188 }
189
190 int __init timer_init(void)
191 {
192         int err;
193
194         CHOOSE_MODE(user_time_init_tt(), user_time_init_skas());
195         err = request_irq(TIMER_IRQ, um_timer, SA_INTERRUPT, "timer", NULL);
196         if(err != 0)
197                 printk(KERN_ERR "timer_init : request_irq failed - "
198                        "errno = %d\n", -err);
199         timer_irq_inited = 1;
200         return(0);
201 }
202
203 __initcall(timer_init);
204
205 /*
206  * Overrides for Emacs so that we follow Linus's tabbing style.
207  * Emacs will notice this stuff at the end of the file and automatically
208  * adjust the settings for this buffer only.  This must remain at the end
209  * of the file.
210  * ---------------------------------------------------------------------------
211  * Local variables:
212  * c-file-style: "linux"
213  * End:
214  */