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 | |
| 38 | /* Changed at early boot */ |
| 39 | int timer_irq_inited = 0; |
| 40 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 41 | static unsigned long long prev_nsecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #ifdef CONFIG_UML_REAL_TIME_CLOCK |
| 43 | static long long delta; /* Deviation per interval */ |
| 44 | #endif |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | void timer_irq(union uml_pt_regs *regs) |
| 47 | { |
| 48 | unsigned long long ticks = 0; |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #ifdef CONFIG_UML_REAL_TIME_CLOCK |
Jeff Dike | 872aaa6 | 2006-07-10 04:45:08 -0700 | [diff] [blame^] | 51 | if(prev_nsecs){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* We've had 1 tick */ |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 53 | unsigned long long nsecs = os_nsecs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 55 | delta += nsecs - prev_nsecs; |
| 56 | prev_nsecs = nsecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* Protect against the host clock being set backwards */ |
| 59 | if(delta < 0) |
| 60 | delta = 0; |
| 61 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 62 | ticks += (delta * HZ) / BILLION; |
| 63 | delta -= (ticks * BILLION) / HZ; |
Jeff Dike | 872aaa6 | 2006-07-10 04:45:08 -0700 | [diff] [blame^] | 64 | } |
| 65 | else prev_nsecs = os_nsecs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #else |
Jeff Dike | 872aaa6 | 2006-07-10 04:45:08 -0700 | [diff] [blame^] | 67 | ticks = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | while(ticks > 0){ |
| 70 | do_IRQ(TIMER_IRQ, regs); |
| 71 | ticks--; |
| 72 | } |
| 73 | } |
| 74 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 75 | static DEFINE_SPINLOCK(timer_spinlock); |
| 76 | |
| 77 | static unsigned long long local_offset = 0; |
| 78 | |
| 79 | static inline unsigned long long get_time(void) |
| 80 | { |
| 81 | unsigned long long nsecs; |
| 82 | unsigned long flags; |
| 83 | |
| 84 | spin_lock_irqsave(&timer_spinlock, flags); |
| 85 | nsecs = os_nsecs(); |
| 86 | nsecs += local_offset; |
| 87 | spin_unlock_irqrestore(&timer_spinlock, flags); |
| 88 | |
| 89 | return nsecs; |
| 90 | } |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | irqreturn_t um_timer(int irq, void *dev, struct pt_regs *regs) |
| 93 | { |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 94 | unsigned long long nsecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | unsigned long flags; |
| 96 | |
Jeff Dike | 572e614 | 2006-06-30 01:55:56 -0700 | [diff] [blame] | 97 | write_seqlock_irqsave(&xtime_lock, flags); |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | do_timer(regs); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 100 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 101 | nsecs = get_time() + local_offset; |
| 102 | xtime.tv_sec = nsecs / NSEC_PER_SEC; |
| 103 | xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC; |
Jeff Dike | 572e614 | 2006-06-30 01:55:56 -0700 | [diff] [blame] | 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | write_sequnlock_irqrestore(&xtime_lock, flags); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 106 | |
Jeff Dike | 572e614 | 2006-06-30 01:55:56 -0700 | [diff] [blame] | 107 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 110 | static void register_timer(void) |
| 111 | { |
| 112 | int err; |
| 113 | |
| 114 | err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL); |
| 115 | if(err != 0) |
| 116 | printk(KERN_ERR "timer_init : request_irq failed - " |
| 117 | "errno = %d\n", -err); |
| 118 | |
| 119 | timer_irq_inited = 1; |
| 120 | |
| 121 | user_time_init(); |
| 122 | } |
| 123 | |
| 124 | extern void (*late_time_init)(void); |
| 125 | |
| 126 | void time_init(void) |
| 127 | { |
| 128 | long long nsecs; |
| 129 | |
| 130 | nsecs = os_nsecs(); |
| 131 | set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION, |
| 132 | -nsecs % BILLION); |
| 133 | late_time_init = register_timer; |
| 134 | } |
| 135 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 136 | void do_gettimeofday(struct timeval *tv) |
| 137 | { |
| 138 | unsigned long long nsecs = get_time(); |
| 139 | |
| 140 | tv->tv_sec = nsecs / NSEC_PER_SEC; |
| 141 | /* Careful about calculations here - this was originally done as |
| 142 | * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC |
| 143 | * which gave bogus (> 1000000) values. Dunno why, suspect gcc |
| 144 | * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion |
| 145 | * problem that I missed. |
| 146 | */ |
| 147 | nsecs -= tv->tv_sec * NSEC_PER_SEC; |
| 148 | tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC; |
| 149 | } |
| 150 | |
| 151 | static inline void set_time(unsigned long long nsecs) |
| 152 | { |
| 153 | unsigned long long now; |
| 154 | unsigned long flags; |
| 155 | |
| 156 | spin_lock_irqsave(&timer_spinlock, flags); |
| 157 | now = os_nsecs(); |
| 158 | local_offset = nsecs - now; |
| 159 | spin_unlock_irqrestore(&timer_spinlock, flags); |
| 160 | |
| 161 | clock_was_set(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 164 | int do_settimeofday(struct timespec *tv) |
| 165 | { |
| 166 | set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec); |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | void timer_handler(int sig, union uml_pt_regs *regs) |
| 172 | { |
| 173 | local_irq_disable(); |
Jeff Dike | 7e1f49d | 2005-07-28 21:16:09 -0700 | [diff] [blame] | 174 | irq_enter(); |
Bodo Stroesser | 097fdf0 | 2006-01-18 17:42:51 -0800 | [diff] [blame] | 175 | update_process_times(CHOOSE_MODE( |
| 176 | (UPT_SC(regs) && user_context(UPT_SP(regs))), |
| 177 | (regs)->skas.is_user)); |
Jeff Dike | 7e1f49d | 2005-07-28 21:16:09 -0700 | [diff] [blame] | 178 | irq_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | local_irq_enable(); |
| 180 | if(current_thread->cpu == 0) |
| 181 | timer_irq(regs); |
| 182 | } |