633b673d57781ee82a1afa7c4b26898c37f76c7d
[linux-2.6.git] / arch / um / kernel / time.c
1 /*
2  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/interrupt.h"
7 #include "linux/jiffies.h"
8 #include "linux/threads.h"
9 #include "asm/irq.h"
10 #include "asm/param.h"
11 #include "kern_util.h"
12 #include "os.h"
13
14 /*
15  * Scheduler clock - returns current time in nanosec units.
16  */
17 unsigned long long sched_clock(void)
18 {
19         return (unsigned long long)jiffies_64 * (1000000000 / HZ);
20 }
21
22 #ifdef CONFIG_UML_REAL_TIME_CLOCK
23 static unsigned long long prev_nsecs[NR_CPUS];
24 static long long delta[NR_CPUS];                /* Deviation per interval */
25 #endif
26
27 void timer_irq(struct uml_pt_regs *regs)
28 {
29         unsigned long long ticks = 0;
30 #ifdef CONFIG_UML_REAL_TIME_CLOCK
31         int c = cpu();
32         if (prev_nsecs[c]) {
33                 /* We've had 1 tick */
34                 unsigned long long nsecs = os_nsecs();
35
36                 delta[c] += nsecs - prev_nsecs[c];
37                 prev_nsecs[c] = nsecs;
38
39                 /* Protect against the host clock being set backwards */
40                 if (delta[c] < 0)
41                         delta[c] = 0;
42
43                 ticks += (delta[c] * HZ) / BILLION;
44                 delta[c] -= (ticks * BILLION) / HZ;
45         }
46         else prev_nsecs[c] = os_nsecs();
47 #else
48         ticks = 1;
49 #endif
50         while (ticks > 0) {
51                 do_IRQ(TIMER_IRQ, regs);
52                 ticks--;
53         }
54 }
55
56 /* Protects local_offset */
57 static DEFINE_SPINLOCK(timer_spinlock);
58 static unsigned long long local_offset = 0;
59
60 static inline unsigned long long get_time(void)
61 {
62         unsigned long long nsecs;
63         unsigned long flags;
64
65         spin_lock_irqsave(&timer_spinlock, flags);
66         nsecs = os_nsecs();
67         nsecs += local_offset;
68         spin_unlock_irqrestore(&timer_spinlock, flags);
69
70         return nsecs;
71 }
72
73 irqreturn_t um_timer(int irq, void *dev)
74 {
75         unsigned long long nsecs;
76         unsigned long flags;
77
78         write_seqlock_irqsave(&xtime_lock, flags);
79
80         do_timer(1);
81
82 #ifdef CONFIG_UML_REAL_TIME_CLOCK
83         nsecs = get_time();
84 #else
85         nsecs = (unsigned long long) xtime.tv_sec * BILLION + xtime.tv_nsec +
86                 BILLION / HZ;
87 #endif
88         xtime.tv_sec = nsecs / NSEC_PER_SEC;
89         xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
90
91         write_sequnlock_irqrestore(&xtime_lock, flags);
92
93         return IRQ_HANDLED;
94 }
95
96 static void register_timer(void)
97 {
98         int err;
99
100         err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
101         if (err != 0)
102                 printk(KERN_ERR "register_timer : request_irq failed - "
103                        "errno = %d\n", -err);
104
105         err = set_interval(1);
106         if (err != 0)
107                 printk(KERN_ERR "register_timer : set_interval failed - "
108                        "errno = %d\n", -err);
109 }
110
111 extern void (*late_time_init)(void);
112
113 void time_init(void)
114 {
115         long long nsecs;
116
117         nsecs = os_nsecs();
118         set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
119                                 -nsecs % BILLION);
120         set_normalized_timespec(&xtime, nsecs / BILLION, nsecs % BILLION);
121         late_time_init = register_timer;
122 }
123
124 void do_gettimeofday(struct timeval *tv)
125 {
126 #ifdef CONFIG_UML_REAL_TIME_CLOCK
127         unsigned long long nsecs = get_time();
128 #else
129         unsigned long long nsecs = (unsigned long long) xtime.tv_sec * BILLION +
130                 xtime.tv_nsec;
131 #endif
132         tv->tv_sec = nsecs / NSEC_PER_SEC;
133         /*
134          * Careful about calculations here - this was originally done as
135          * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
136          * which gave bogus (> 1000000) values.  Dunno why, suspect gcc
137          * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
138          * problem that I missed.
139          */
140         nsecs -= tv->tv_sec * NSEC_PER_SEC;
141         tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
142 }
143
144 static inline void set_time(unsigned long long nsecs)
145 {
146         unsigned long long now;
147         unsigned long flags;
148
149         spin_lock_irqsave(&timer_spinlock, flags);
150         now = os_nsecs();
151         local_offset = nsecs - now;
152         spin_unlock_irqrestore(&timer_spinlock, flags);
153
154         clock_was_set();
155 }
156
157 int do_settimeofday(struct timespec *tv)
158 {
159         set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
160
161         return 0;
162 }
163
164 void timer_handler(int sig, struct uml_pt_regs *regs)
165 {
166         if (current_thread->cpu == 0)
167                 timer_irq(regs);
168         local_irq_disable();
169         irq_enter();
170         update_process_times(regs->is_user);
171         irq_exit();
172         local_irq_enable();
173 }