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