[PATCH] uml: make time data per-cpu
[linux-2.6] / arch / um / kernel / time.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 static unsigned long long prev_nsecs[NR_CPUS];
39 #ifdef CONFIG_UML_REAL_TIME_CLOCK
40 static long long delta[NR_CPUS];                /* Deviation per interval */
41 #endif
42
43 void timer_irq(union uml_pt_regs *regs)
44 {
45         unsigned long long ticks = 0;
46 #ifdef CONFIG_UML_REAL_TIME_CLOCK
47         int c = cpu();
48         if(prev_nsecs[c]){
49                 /* We've had 1 tick */
50                 unsigned long long nsecs = os_nsecs();
51
52                 delta[c] += nsecs - prev_nsecs[c];
53                 prev_nsecs[c] = nsecs;
54
55                 /* Protect against the host clock being set backwards */
56                 if(delta[c] < 0)
57                         delta[c] = 0;
58
59                 ticks += (delta[c] * HZ) / BILLION;
60                 delta[c] -= (ticks * BILLION) / HZ;
61         }
62         else prev_nsecs[c] = os_nsecs();
63 #else
64         ticks = 1;
65 #endif
66         while(ticks > 0){
67                 do_IRQ(TIMER_IRQ, regs);
68                 ticks--;
69         }
70 }
71
72 /* Protects local_offset */
73 static DEFINE_SPINLOCK(timer_spinlock);
74 static unsigned long long local_offset = 0;
75
76 static inline unsigned long long get_time(void)
77 {
78         unsigned long long nsecs;
79         unsigned long flags;
80
81         spin_lock_irqsave(&timer_spinlock, flags);
82         nsecs = os_nsecs();
83         nsecs += local_offset;
84         spin_unlock_irqrestore(&timer_spinlock, flags);
85
86         return nsecs;
87 }
88
89 irqreturn_t um_timer(int irq, void *dev)
90 {
91         unsigned long long nsecs;
92         unsigned long flags;
93
94         write_seqlock_irqsave(&xtime_lock, flags);
95
96         do_timer(1);
97
98         nsecs = get_time();
99         xtime.tv_sec = nsecs / NSEC_PER_SEC;
100         xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
101
102         write_sequnlock_irqrestore(&xtime_lock, flags);
103
104         return IRQ_HANDLED;
105 }
106
107 static void register_timer(void)
108 {
109         int err;
110
111         err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
112         if(err != 0)
113                 printk(KERN_ERR "register_timer : request_irq failed - "
114                        "errno = %d\n", -err);
115
116         err = set_interval(1);
117         if(err != 0)
118                 printk(KERN_ERR "register_timer : set_interval failed - "
119                        "errno = %d\n", -err);
120 }
121
122 extern void (*late_time_init)(void);
123
124 void time_init(void)
125 {
126         long long nsecs;
127
128         nsecs = os_nsecs();
129         set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
130                                 -nsecs % BILLION);
131         late_time_init = register_timer;
132 }
133
134 void do_gettimeofday(struct timeval *tv)
135 {
136         unsigned long long nsecs = get_time();
137
138         tv->tv_sec = nsecs / NSEC_PER_SEC;
139         /* Careful about calculations here - this was originally done as
140          * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
141          * which gave bogus (> 1000000) values.  Dunno why, suspect gcc
142          * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
143          * problem that I missed.
144          */
145         nsecs -= tv->tv_sec * NSEC_PER_SEC;
146         tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
147 }
148
149 static inline void set_time(unsigned long long nsecs)
150 {
151         unsigned long long now;
152         unsigned long flags;
153
154         spin_lock_irqsave(&timer_spinlock, flags);
155         now = os_nsecs();
156         local_offset = nsecs - now;
157         spin_unlock_irqrestore(&timer_spinlock, flags);
158
159         clock_was_set();
160 }
161
162 int do_settimeofday(struct timespec *tv)
163 {
164         set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
165
166         return 0;
167 }
168
169 void timer_handler(int sig, union uml_pt_regs *regs)
170 {
171         local_irq_disable();
172         irq_enter();
173         update_process_times(CHOOSE_MODE(
174                              (UPT_SC(regs) && user_context(UPT_SP(regs))),
175                              (regs)->skas.is_user));
176         irq_exit();
177         local_irq_enable();
178         if(current_thread->cpu == 0)
179                 timer_irq(regs);
180 }