uml: GENERIC_TIME support
[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         timer_init();
101
102         err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
103         if (err != 0)
104                 printk(KERN_ERR "register_timer : request_irq failed - "
105                        "errno = %d\n", -err);
106
107         err = set_interval();
108         if (err != 0)
109                 printk(KERN_ERR "register_timer : set_interval failed - "
110                        "errno = %d\n", -err);
111 }
112
113 extern void (*late_time_init)(void);
114
115 void time_init(void)
116 {
117         long long nsecs;
118
119         nsecs = os_nsecs();
120         set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
121                                 -nsecs % BILLION);
122         set_normalized_timespec(&xtime, nsecs / BILLION, nsecs % BILLION);
123         late_time_init = register_timer;
124 }
125
126 void timer_handler(int sig, struct uml_pt_regs *regs)
127 {
128         if (current_thread->cpu == 0)
129                 timer_irq(regs);
130         local_irq_disable();
131         irq_enter();
132         update_process_times(regs->is_user);
133         irq_exit();
134         local_irq_enable();
135 }