2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
6 #include "linux/interrupt.h"
7 #include "linux/jiffies.h"
8 #include "linux/threads.h"
10 #include "asm/param.h"
11 #include "kern_util.h"
20 * Scheduler clock - returns current time in nanosec units.
22 unsigned long long sched_clock(void)
24 return (unsigned long long)jiffies_64 * (1000000000 / HZ);
27 #ifdef CONFIG_UML_REAL_TIME_CLOCK
28 static unsigned long long prev_nsecs[NR_CPUS];
29 static long long delta[NR_CPUS]; /* Deviation per interval */
32 void timer_irq(struct uml_pt_regs *regs)
34 unsigned long long ticks = 0;
35 #ifdef CONFIG_UML_REAL_TIME_CLOCK
38 /* We've had 1 tick */
39 unsigned long long nsecs = os_nsecs();
41 delta[c] += nsecs - prev_nsecs[c];
42 prev_nsecs[c] = nsecs;
44 /* Protect against the host clock being set backwards */
48 ticks += (delta[c] * HZ) / BILLION;
49 delta[c] -= (ticks * BILLION) / HZ;
51 else prev_nsecs[c] = os_nsecs();
56 do_IRQ(TIMER_IRQ, regs);
61 /* Protects local_offset */
62 static DEFINE_SPINLOCK(timer_spinlock);
63 static unsigned long long local_offset = 0;
65 static inline unsigned long long get_time(void)
67 unsigned long long nsecs;
70 spin_lock_irqsave(&timer_spinlock, flags);
72 nsecs += local_offset;
73 spin_unlock_irqrestore(&timer_spinlock, flags);
78 irqreturn_t um_timer(int irq, void *dev)
80 unsigned long long nsecs;
83 write_seqlock_irqsave(&xtime_lock, flags);
87 #ifdef CONFIG_UML_REAL_TIME_CLOCK
90 nsecs = (unsigned long long) xtime.tv_sec * BILLION + xtime.tv_nsec +
93 xtime.tv_sec = nsecs / NSEC_PER_SEC;
94 xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
96 write_sequnlock_irqrestore(&xtime_lock, flags);
101 static void register_timer(void)
105 err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
107 printk(KERN_ERR "register_timer : request_irq failed - "
108 "errno = %d\n", -err);
110 err = set_interval(1);
112 printk(KERN_ERR "register_timer : set_interval failed - "
113 "errno = %d\n", -err);
116 extern void (*late_time_init)(void);
123 set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
125 set_normalized_timespec(&xtime, nsecs / BILLION, nsecs % BILLION);
126 late_time_init = register_timer;
129 void do_gettimeofday(struct timeval *tv)
131 #ifdef CONFIG_UML_REAL_TIME_CLOCK
132 unsigned long long nsecs = get_time();
134 unsigned long long nsecs = (unsigned long long) xtime.tv_sec * BILLION +
137 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.
145 nsecs -= tv->tv_sec * NSEC_PER_SEC;
146 tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
149 static inline void set_time(unsigned long long nsecs)
151 unsigned long long now;
154 spin_lock_irqsave(&timer_spinlock, flags);
156 local_offset = nsecs - now;
157 spin_unlock_irqrestore(&timer_spinlock, flags);
162 int do_settimeofday(struct timespec *tv)
164 set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
169 void timer_handler(int sig, struct uml_pt_regs *regs)
171 if (current_thread->cpu == 0)
175 update_process_times(regs->is_user);