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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "os.h" |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | int hz(void) |
| 24 | { |
| 25 | return(HZ); |
| 26 | } |
| 27 | |
| 28 | /* |
| 29 | * Scheduler clock - returns current time in nanosec units. |
| 30 | */ |
| 31 | unsigned long long sched_clock(void) |
| 32 | { |
| 33 | return (unsigned long long)jiffies_64 * (1000000000 / HZ); |
| 34 | } |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #ifdef CONFIG_UML_REAL_TIME_CLOCK |
Jeff Dike | b7ec15b | 2007-05-06 14:51:51 -0700 | [diff] [blame] | 37 | static unsigned long long prev_nsecs[NR_CPUS]; |
Jeff Dike | 490ba17 | 2007-02-10 01:44:12 -0800 | [diff] [blame] | 38 | static long long delta[NR_CPUS]; /* Deviation per interval */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #endif |
| 40 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame^] | 41 | void timer_irq(struct uml_pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
| 43 | unsigned long long ticks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #ifdef CONFIG_UML_REAL_TIME_CLOCK |
Jeff Dike | 490ba17 | 2007-02-10 01:44:12 -0800 | [diff] [blame] | 45 | int c = cpu(); |
| 46 | if(prev_nsecs[c]){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* We've had 1 tick */ |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 48 | unsigned long long nsecs = os_nsecs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Jeff Dike | 490ba17 | 2007-02-10 01:44:12 -0800 | [diff] [blame] | 50 | delta[c] += nsecs - prev_nsecs[c]; |
| 51 | prev_nsecs[c] = nsecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | /* Protect against the host clock being set backwards */ |
Jeff Dike | 490ba17 | 2007-02-10 01:44:12 -0800 | [diff] [blame] | 54 | if(delta[c] < 0) |
| 55 | delta[c] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Jeff Dike | 490ba17 | 2007-02-10 01:44:12 -0800 | [diff] [blame] | 57 | ticks += (delta[c] * HZ) / BILLION; |
| 58 | delta[c] -= (ticks * BILLION) / HZ; |
Jeff Dike | 872aaa6 | 2006-07-10 04:45:08 -0700 | [diff] [blame] | 59 | } |
Jeff Dike | 490ba17 | 2007-02-10 01:44:12 -0800 | [diff] [blame] | 60 | else prev_nsecs[c] = os_nsecs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #else |
Jeff Dike | 872aaa6 | 2006-07-10 04:45:08 -0700 | [diff] [blame] | 62 | ticks = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | while(ticks > 0){ |
| 65 | do_IRQ(TIMER_IRQ, regs); |
| 66 | ticks--; |
| 67 | } |
| 68 | } |
| 69 | |
Jeff Dike | 490ba17 | 2007-02-10 01:44:12 -0800 | [diff] [blame] | 70 | /* Protects local_offset */ |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 71 | static DEFINE_SPINLOCK(timer_spinlock); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 72 | static unsigned long long local_offset = 0; |
| 73 | |
| 74 | static inline unsigned long long get_time(void) |
| 75 | { |
| 76 | unsigned long long nsecs; |
| 77 | unsigned long flags; |
| 78 | |
| 79 | spin_lock_irqsave(&timer_spinlock, flags); |
| 80 | nsecs = os_nsecs(); |
| 81 | nsecs += local_offset; |
| 82 | spin_unlock_irqrestore(&timer_spinlock, flags); |
| 83 | |
| 84 | return nsecs; |
| 85 | } |
| 86 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 87 | irqreturn_t um_timer(int irq, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 89 | unsigned long long nsecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | unsigned long flags; |
| 91 | |
Jeff Dike | 572e614 | 2006-06-30 01:55:56 -0700 | [diff] [blame] | 92 | write_seqlock_irqsave(&xtime_lock, flags); |
| 93 | |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 94 | do_timer(1); |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 95 | |
Jeff Dike | b7ec15b | 2007-05-06 14:51:51 -0700 | [diff] [blame] | 96 | #ifdef CONFIG_UML_REAL_TIME_CLOCK |
Jeff Dike | c1b4098 | 2006-09-27 01:50:42 -0700 | [diff] [blame] | 97 | nsecs = get_time(); |
Jeff Dike | b7ec15b | 2007-05-06 14:51:51 -0700 | [diff] [blame] | 98 | #else |
| 99 | nsecs = (unsigned long long) xtime.tv_sec * BILLION + xtime.tv_nsec + |
| 100 | BILLION / HZ; |
| 101 | #endif |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 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) |
Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 116 | printk(KERN_ERR "register_timer : request_irq failed - " |
Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 117 | "errno = %d\n", -err); |
| 118 | |
Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 119 | err = set_interval(1); |
| 120 | if(err != 0) |
| 121 | printk(KERN_ERR "register_timer : set_interval failed - " |
| 122 | "errno = %d\n", -err); |
Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | extern void (*late_time_init)(void); |
| 126 | |
| 127 | void time_init(void) |
| 128 | { |
| 129 | long long nsecs; |
| 130 | |
| 131 | nsecs = os_nsecs(); |
| 132 | set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION, |
| 133 | -nsecs % BILLION); |
Jeff Dike | b7ec15b | 2007-05-06 14:51:51 -0700 | [diff] [blame] | 134 | set_normalized_timespec(&xtime, nsecs / BILLION, nsecs % BILLION); |
Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 135 | late_time_init = register_timer; |
| 136 | } |
| 137 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 138 | void do_gettimeofday(struct timeval *tv) |
| 139 | { |
Jeff Dike | b7ec15b | 2007-05-06 14:51:51 -0700 | [diff] [blame] | 140 | #ifdef CONFIG_UML_REAL_TIME_CLOCK |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 141 | unsigned long long nsecs = get_time(); |
Jeff Dike | b7ec15b | 2007-05-06 14:51:51 -0700 | [diff] [blame] | 142 | #else |
| 143 | unsigned long long nsecs = (unsigned long long) xtime.tv_sec * BILLION + |
| 144 | xtime.tv_nsec; |
| 145 | #endif |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 146 | tv->tv_sec = nsecs / NSEC_PER_SEC; |
| 147 | /* Careful about calculations here - this was originally done as |
| 148 | * (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC |
| 149 | * which gave bogus (> 1000000) values. Dunno why, suspect gcc |
| 150 | * (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion |
| 151 | * problem that I missed. |
| 152 | */ |
| 153 | nsecs -= tv->tv_sec * NSEC_PER_SEC; |
| 154 | tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC; |
| 155 | } |
| 156 | |
| 157 | static inline void set_time(unsigned long long nsecs) |
| 158 | { |
| 159 | unsigned long long now; |
| 160 | unsigned long flags; |
| 161 | |
| 162 | spin_lock_irqsave(&timer_spinlock, flags); |
| 163 | now = os_nsecs(); |
| 164 | local_offset = nsecs - now; |
| 165 | spin_unlock_irqrestore(&timer_spinlock, flags); |
| 166 | |
| 167 | clock_was_set(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 170 | int do_settimeofday(struct timespec *tv) |
| 171 | { |
| 172 | set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec); |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame^] | 177 | void timer_handler(int sig, struct uml_pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Jeff Dike | c83e448 | 2007-05-08 00:23:22 -0700 | [diff] [blame] | 179 | if(current_thread->cpu == 0) |
| 180 | timer_irq(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | local_irq_disable(); |
Jeff Dike | 7e1f49d | 2005-07-28 21:16:09 -0700 | [diff] [blame] | 182 | irq_enter(); |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame^] | 183 | update_process_times(regs->is_user); |
Jeff Dike | 7e1f49d | 2005-07-28 21:16:09 -0700 | [diff] [blame] | 184 | irq_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |