blob: 259c49da7ff501fd4eca0761deb1b45277172a77 [file] [log] [blame]
Gennady Sharapovcff65c42006-01-18 17:42:42 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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"
Gennady Sharapovcff65c42006-01-18 17:42:42 -080016#include "linux/hrtimer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "asm/irq.h"
18#include "asm/param.h"
19#include "asm/current.h"
20#include "kern_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "mode.h"
22#include "os.h"
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024int hz(void)
25{
26 return(HZ);
27}
28
29/*
30 * Scheduler clock - returns current time in nanosec units.
31 */
32unsigned long long sched_clock(void)
33{
34 return (unsigned long long)jiffies_64 * (1000000000 / HZ);
35}
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#ifdef CONFIG_UML_REAL_TIME_CLOCK
Jeff Dikeb7ec15b2007-05-06 14:51:51 -070038static unsigned long long prev_nsecs[NR_CPUS];
Jeff Dike490ba172007-02-10 01:44:12 -080039static long long delta[NR_CPUS]; /* Deviation per interval */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#endif
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042void timer_irq(union uml_pt_regs *regs)
43{
44 unsigned long long ticks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#ifdef CONFIG_UML_REAL_TIME_CLOCK
Jeff Dike490ba172007-02-10 01:44:12 -080046 int c = cpu();
47 if(prev_nsecs[c]){
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 /* We've had 1 tick */
Gennady Sharapovcff65c42006-01-18 17:42:42 -080049 unsigned long long nsecs = os_nsecs();
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Jeff Dike490ba172007-02-10 01:44:12 -080051 delta[c] += nsecs - prev_nsecs[c];
52 prev_nsecs[c] = nsecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 /* Protect against the host clock being set backwards */
Jeff Dike490ba172007-02-10 01:44:12 -080055 if(delta[c] < 0)
56 delta[c] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jeff Dike490ba172007-02-10 01:44:12 -080058 ticks += (delta[c] * HZ) / BILLION;
59 delta[c] -= (ticks * BILLION) / HZ;
Jeff Dike872aaa62006-07-10 04:45:08 -070060 }
Jeff Dike490ba172007-02-10 01:44:12 -080061 else prev_nsecs[c] = os_nsecs();
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#else
Jeff Dike872aaa62006-07-10 04:45:08 -070063 ticks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 while(ticks > 0){
66 do_IRQ(TIMER_IRQ, regs);
67 ticks--;
68 }
69}
70
Jeff Dike490ba172007-02-10 01:44:12 -080071/* Protects local_offset */
Gennady Sharapovcff65c42006-01-18 17:42:42 -080072static DEFINE_SPINLOCK(timer_spinlock);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080073static unsigned long long local_offset = 0;
74
75static inline unsigned long long get_time(void)
76{
77 unsigned long long nsecs;
78 unsigned long flags;
79
80 spin_lock_irqsave(&timer_spinlock, flags);
81 nsecs = os_nsecs();
82 nsecs += local_offset;
83 spin_unlock_irqrestore(&timer_spinlock, flags);
84
85 return nsecs;
86}
87
Al Viro7bea96f2006-10-08 22:49:34 +010088irqreturn_t um_timer(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Gennady Sharapovcff65c42006-01-18 17:42:42 -080090 unsigned long long nsecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 unsigned long flags;
92
Jeff Dike572e6142006-06-30 01:55:56 -070093 write_seqlock_irqsave(&xtime_lock, flags);
94
Atsushi Nemoto3171a032006-09-29 02:00:32 -070095 do_timer(1);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080096
Jeff Dikeb7ec15b2007-05-06 14:51:51 -070097#ifdef CONFIG_UML_REAL_TIME_CLOCK
Jeff Dikec1b40982006-09-27 01:50:42 -070098 nsecs = get_time();
Jeff Dikeb7ec15b2007-05-06 14:51:51 -070099#else
100 nsecs = (unsigned long long) xtime.tv_sec * BILLION + xtime.tv_nsec +
101 BILLION / HZ;
102#endif
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800103 xtime.tv_sec = nsecs / NSEC_PER_SEC;
104 xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
Jeff Dike572e6142006-06-30 01:55:56 -0700105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 write_sequnlock_irqrestore(&xtime_lock, flags);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800107
Jeff Dike572e6142006-06-30 01:55:56 -0700108 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Jeff Dikeaceb3432006-07-10 04:45:05 -0700111static void register_timer(void)
112{
113 int err;
114
115 err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
116 if(err != 0)
Jeff Dike537ae942006-09-25 23:33:05 -0700117 printk(KERN_ERR "register_timer : request_irq failed - "
Jeff Dikeaceb3432006-07-10 04:45:05 -0700118 "errno = %d\n", -err);
119
Jeff Dike537ae942006-09-25 23:33:05 -0700120 err = set_interval(1);
121 if(err != 0)
122 printk(KERN_ERR "register_timer : set_interval failed - "
123 "errno = %d\n", -err);
Jeff Dikeaceb3432006-07-10 04:45:05 -0700124}
125
126extern void (*late_time_init)(void);
127
128void time_init(void)
129{
130 long long nsecs;
131
132 nsecs = os_nsecs();
133 set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
134 -nsecs % BILLION);
Jeff Dikeb7ec15b2007-05-06 14:51:51 -0700135 set_normalized_timespec(&xtime, nsecs / BILLION, nsecs % BILLION);
Jeff Dikeaceb3432006-07-10 04:45:05 -0700136 late_time_init = register_timer;
137}
138
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800139void do_gettimeofday(struct timeval *tv)
140{
Jeff Dikeb7ec15b2007-05-06 14:51:51 -0700141#ifdef CONFIG_UML_REAL_TIME_CLOCK
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800142 unsigned long long nsecs = get_time();
Jeff Dikeb7ec15b2007-05-06 14:51:51 -0700143#else
144 unsigned long long nsecs = (unsigned long long) xtime.tv_sec * BILLION +
145 xtime.tv_nsec;
146#endif
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800147 tv->tv_sec = nsecs / NSEC_PER_SEC;
148 /* Careful about calculations here - this was originally done as
149 * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
150 * which gave bogus (> 1000000) values. Dunno why, suspect gcc
151 * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
152 * problem that I missed.
153 */
154 nsecs -= tv->tv_sec * NSEC_PER_SEC;
155 tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
156}
157
158static inline void set_time(unsigned long long nsecs)
159{
160 unsigned long long now;
161 unsigned long flags;
162
163 spin_lock_irqsave(&timer_spinlock, flags);
164 now = os_nsecs();
165 local_offset = nsecs - now;
166 spin_unlock_irqrestore(&timer_spinlock, flags);
167
168 clock_was_set();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800171int do_settimeofday(struct timespec *tv)
172{
173 set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178void timer_handler(int sig, union uml_pt_regs *regs)
179{
Jeff Dikec83e4482007-05-08 00:23:22 -0700180 if(current_thread->cpu == 0)
181 timer_irq(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 local_irq_disable();
Jeff Dike7e1f49d2005-07-28 21:16:09 -0700183 irq_enter();
Bodo Stroesser097fdf02006-01-18 17:42:51 -0800184 update_process_times(CHOOSE_MODE(
185 (UPT_SC(regs) && user_context(UPT_SP(regs))),
186 (regs)->skas.is_user));
Jeff Dike7e1f49d2005-07-28 21:16:09 -0700187 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}