[PATCH] uml: make time data per-cpu
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / um / kernel / time.c
blobb1f8b0752419d8514763d6ca297674e6e7ceb03e
1 /*
2 * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
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"
25 int hz(void)
27 return(HZ);
31 * Scheduler clock - returns current time in nanosec units.
33 unsigned long long sched_clock(void)
35 return (unsigned long long)jiffies_64 * (1000000000 / HZ);
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
43 void timer_irq(union uml_pt_regs *regs)
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();
52 delta[c] += nsecs - prev_nsecs[c];
53 prev_nsecs[c] = nsecs;
55 /* Protect against the host clock being set backwards */
56 if(delta[c] < 0)
57 delta[c] = 0;
59 ticks += (delta[c] * HZ) / BILLION;
60 delta[c] -= (ticks * BILLION) / HZ;
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--;
72 /* Protects local_offset */
73 static DEFINE_SPINLOCK(timer_spinlock);
74 static unsigned long long local_offset = 0;
76 static inline unsigned long long get_time(void)
78 unsigned long long nsecs;
79 unsigned long flags;
81 spin_lock_irqsave(&timer_spinlock, flags);
82 nsecs = os_nsecs();
83 nsecs += local_offset;
84 spin_unlock_irqrestore(&timer_spinlock, flags);
86 return nsecs;
89 irqreturn_t um_timer(int irq, void *dev)
91 unsigned long long nsecs;
92 unsigned long flags;
94 write_seqlock_irqsave(&xtime_lock, flags);
96 do_timer(1);
98 nsecs = get_time();
99 xtime.tv_sec = nsecs / NSEC_PER_SEC;
100 xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
102 write_sequnlock_irqrestore(&xtime_lock, flags);
104 return IRQ_HANDLED;
107 static void register_timer(void)
109 int err;
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);
116 err = set_interval(1);
117 if(err != 0)
118 printk(KERN_ERR "register_timer : set_interval failed - "
119 "errno = %d\n", -err);
122 extern void (*late_time_init)(void);
124 void time_init(void)
126 long long nsecs;
128 nsecs = os_nsecs();
129 set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
130 -nsecs % BILLION);
131 late_time_init = register_timer;
134 void do_gettimeofday(struct timeval *tv)
136 unsigned long long nsecs = get_time();
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.
145 nsecs -= tv->tv_sec * NSEC_PER_SEC;
146 tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
149 static inline void set_time(unsigned long long nsecs)
151 unsigned long long now;
152 unsigned long flags;
154 spin_lock_irqsave(&timer_spinlock, flags);
155 now = os_nsecs();
156 local_offset = nsecs - now;
157 spin_unlock_irqrestore(&timer_spinlock, flags);
159 clock_was_set();
162 int do_settimeofday(struct timespec *tv)
164 set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
166 return 0;
169 void timer_handler(int sig, union uml_pt_regs *regs)
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);