Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 16 | #include "linux/hrtimer.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "mode.h" |
| 23 | #include "os.h" |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | int hz(void) |
| 26 | { |
| 27 | return(HZ); |
| 28 | } |
| 29 | |
| 30 | /* |
| 31 | * Scheduler clock - returns current time in nanosec units. |
| 32 | */ |
| 33 | unsigned long long sched_clock(void) |
| 34 | { |
| 35 | return (unsigned long long)jiffies_64 * (1000000000 / HZ); |
| 36 | } |
| 37 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 38 | static unsigned long long prev_nsecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #ifdef CONFIG_UML_REAL_TIME_CLOCK |
| 40 | static long long delta; /* Deviation per interval */ |
| 41 | #endif |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | void timer_irq(union uml_pt_regs *regs) |
| 44 | { |
| 45 | unsigned long long ticks = 0; |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #ifdef CONFIG_UML_REAL_TIME_CLOCK |
Jeff Dike | 872aaa6 | 2006-07-10 04:45:08 -0700 | [diff] [blame] | 48 | if(prev_nsecs){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* We've had 1 tick */ |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 50 | unsigned long long nsecs = os_nsecs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 52 | delta += nsecs - prev_nsecs; |
| 53 | prev_nsecs = nsecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | /* Protect against the host clock being set backwards */ |
| 56 | if(delta < 0) |
| 57 | delta = 0; |
| 58 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 59 | ticks += (delta * HZ) / BILLION; |
| 60 | delta -= (ticks * BILLION) / HZ; |
Jeff Dike | 872aaa6 | 2006-07-10 04:45:08 -0700 | [diff] [blame] | 61 | } |
| 62 | else prev_nsecs = os_nsecs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #else |
Jeff Dike | 872aaa6 | 2006-07-10 04:45:08 -0700 | [diff] [blame] | 64 | ticks = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | while(ticks > 0){ |
| 67 | do_IRQ(TIMER_IRQ, regs); |
| 68 | ticks--; |
| 69 | } |
| 70 | } |
| 71 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 72 | static DEFINE_SPINLOCK(timer_spinlock); |
| 73 | |
| 74 | static unsigned long long local_offset = 0; |
| 75 | |
| 76 | static 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 Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 89 | irqreturn_t um_timer(int irq, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 91 | unsigned long long nsecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | unsigned long flags; |
| 93 | |
Jeff Dike | 572e614 | 2006-06-30 01:55:56 -0700 | [diff] [blame] | 94 | write_seqlock_irqsave(&xtime_lock, flags); |
| 95 | |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 96 | do_timer(1); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 97 | |
Jeff Dike | c1b4098 | 2006-09-27 01:50:42 -0700 | [diff] [blame] | 98 | nsecs = get_time(); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 99 | xtime.tv_sec = nsecs / NSEC_PER_SEC; |
| 100 | xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC; |
Jeff Dike | 572e614 | 2006-06-30 01:55:56 -0700 | [diff] [blame] | 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | write_sequnlock_irqrestore(&xtime_lock, flags); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 103 | |
Jeff Dike | 572e614 | 2006-06-30 01:55:56 -0700 | [diff] [blame] | 104 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jeff Dike | aceb3434 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 107 | static 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 Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 113 | printk(KERN_ERR "register_timer : request_irq failed - " |
Jeff Dike | aceb3434 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 114 | "errno = %d\n", -err); |
| 115 | |
Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 116 | err = set_interval(1); |
| 117 | if(err != 0) |
| 118 | printk(KERN_ERR "register_timer : set_interval failed - " |
| 119 | "errno = %d\n", -err); |
Jeff Dike | aceb3434 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | extern void (*late_time_init)(void); |
| 123 | |
| 124 | void 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 Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 134 | void 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 | |
| 149 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 162 | int do_settimeofday(struct timespec *tv) |
| 163 | { |
| 164 | set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec); |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | void timer_handler(int sig, union uml_pt_regs *regs) |
| 170 | { |
| 171 | local_irq_disable(); |
Jeff Dike | 7e1f49d | 2005-07-28 21:16:09 -0700 | [diff] [blame] | 172 | irq_enter(); |
Bodo Stroesser | 097fdf0 | 2006-01-18 17:42:51 -0800 | [diff] [blame] | 173 | update_process_times(CHOOSE_MODE( |
| 174 | (UPT_SC(regs) && user_context(UPT_SP(regs))), |
| 175 | (regs)->skas.is_user)); |
Jeff Dike | 7e1f49d | 2005-07-28 21:16:09 -0700 | [diff] [blame] | 176 | irq_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | local_irq_enable(); |
| 178 | if(current_thread->cpu == 0) |
| 179 | timer_irq(regs); |
| 180 | } |