blob: 2e354b3ca060649bb0cbd8afef6633637dc357a8 [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"
21#include "user_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "mode.h"
23#include "os.h"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025int hz(void)
26{
27 return(HZ);
28}
29
30/*
31 * Scheduler clock - returns current time in nanosec units.
32 */
33unsigned long long sched_clock(void)
34{
35 return (unsigned long long)jiffies_64 * (1000000000 / HZ);
36}
37
Gennady Sharapovcff65c42006-01-18 17:42:42 -080038static unsigned long long prev_nsecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#ifdef CONFIG_UML_REAL_TIME_CLOCK
40static long long delta; /* Deviation per interval */
41#endif
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043void timer_irq(union uml_pt_regs *regs)
44{
45 unsigned long long ticks = 0;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#ifdef CONFIG_UML_REAL_TIME_CLOCK
Jeff Dike872aaa62006-07-10 04:45:08 -070048 if(prev_nsecs){
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 /* We've had 1 tick */
Gennady Sharapovcff65c42006-01-18 17:42:42 -080050 unsigned long long nsecs = os_nsecs();
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Gennady Sharapovcff65c42006-01-18 17:42:42 -080052 delta += nsecs - prev_nsecs;
53 prev_nsecs = nsecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 /* Protect against the host clock being set backwards */
56 if(delta < 0)
57 delta = 0;
58
Gennady Sharapovcff65c42006-01-18 17:42:42 -080059 ticks += (delta * HZ) / BILLION;
60 delta -= (ticks * BILLION) / HZ;
Jeff Dike872aaa62006-07-10 04:45:08 -070061 }
62 else prev_nsecs = os_nsecs();
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#else
Jeff Dike872aaa62006-07-10 04:45:08 -070064 ticks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 while(ticks > 0){
67 do_IRQ(TIMER_IRQ, regs);
68 ticks--;
69 }
70}
71
Gennady Sharapovcff65c42006-01-18 17:42:42 -080072static DEFINE_SPINLOCK(timer_spinlock);
73
74static unsigned long long local_offset = 0;
75
76static 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
Al Viro7bea96f2006-10-08 22:49:34 +010089irqreturn_t um_timer(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Gennady Sharapovcff65c42006-01-18 17:42:42 -080091 unsigned long long nsecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 unsigned long flags;
93
Jeff Dike572e6142006-06-30 01:55:56 -070094 write_seqlock_irqsave(&xtime_lock, flags);
95
Atsushi Nemoto3171a032006-09-29 02:00:32 -070096 do_timer(1);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080097
Jeff Dikec1b40982006-09-27 01:50:42 -070098 nsecs = get_time();
Gennady Sharapovcff65c42006-01-18 17:42:42 -080099 xtime.tv_sec = nsecs / NSEC_PER_SEC;
100 xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
Jeff Dike572e6142006-06-30 01:55:56 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 write_sequnlock_irqrestore(&xtime_lock, flags);
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800103
Jeff Dike572e6142006-06-30 01:55:56 -0700104 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Jeff Dikeaceb34342006-07-10 04:45:05 -0700107static 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)
Jeff Dike537ae942006-09-25 23:33:05 -0700113 printk(KERN_ERR "register_timer : request_irq failed - "
Jeff Dikeaceb34342006-07-10 04:45:05 -0700114 "errno = %d\n", -err);
115
Jeff Dike537ae942006-09-25 23:33:05 -0700116 err = set_interval(1);
117 if(err != 0)
118 printk(KERN_ERR "register_timer : set_interval failed - "
119 "errno = %d\n", -err);
Jeff Dikeaceb34342006-07-10 04:45:05 -0700120}
121
122extern void (*late_time_init)(void);
123
124void 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
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800134void 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
149static 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();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800162int do_settimeofday(struct timespec *tv)
163{
164 set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return 0;
167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169void timer_handler(int sig, union uml_pt_regs *regs)
170{
171 local_irq_disable();
Jeff Dike7e1f49d2005-07-28 21:16:09 -0700172 irq_enter();
Bodo Stroesser097fdf02006-01-18 17:42:51 -0800173 update_process_times(CHOOSE_MODE(
174 (UPT_SC(regs) && user_context(UPT_SP(regs))),
175 (regs)->skas.is_user));
Jeff Dike7e1f49d2005-07-28 21:16:09 -0700176 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 local_irq_enable();
178 if(current_thread->cpu == 0)
179 timer_irq(regs);
180}