2006-01-18 20:42:42 -05:00
|
|
|
/*
|
2007-10-16 04:27:00 -04:00
|
|
|
* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
2005-04-16 18:20:36 -04:00
|
|
|
* Licensed under the GPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "linux/interrupt.h"
|
2007-10-16 04:27:00 -04:00
|
|
|
#include "linux/jiffies.h"
|
|
|
|
#include "linux/threads.h"
|
2005-04-16 18:20:36 -04:00
|
|
|
#include "asm/irq.h"
|
|
|
|
#include "asm/param.h"
|
|
|
|
#include "kern_util.h"
|
|
|
|
#include "os.h"
|
|
|
|
|
|
|
|
int hz(void)
|
|
|
|
{
|
2007-10-16 04:27:00 -04:00
|
|
|
return HZ;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scheduler clock - returns current time in nanosec units.
|
|
|
|
*/
|
|
|
|
unsigned long long sched_clock(void)
|
|
|
|
{
|
|
|
|
return (unsigned long long)jiffies_64 * (1000000000 / HZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_UML_REAL_TIME_CLOCK
|
2007-05-06 17:51:51 -04:00
|
|
|
static unsigned long long prev_nsecs[NR_CPUS];
|
2007-02-10 04:44:12 -05:00
|
|
|
static long long delta[NR_CPUS]; /* Deviation per interval */
|
2005-04-16 18:20:36 -04:00
|
|
|
#endif
|
|
|
|
|
2007-10-16 04:26:58 -04:00
|
|
|
void timer_irq(struct uml_pt_regs *regs)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
|
|
|
unsigned long long ticks = 0;
|
|
|
|
#ifdef CONFIG_UML_REAL_TIME_CLOCK
|
2007-02-10 04:44:12 -05:00
|
|
|
int c = cpu();
|
2007-10-16 04:27:00 -04:00
|
|
|
if (prev_nsecs[c]) {
|
2005-04-16 18:20:36 -04:00
|
|
|
/* We've had 1 tick */
|
2006-01-18 20:42:42 -05:00
|
|
|
unsigned long long nsecs = os_nsecs();
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2007-02-10 04:44:12 -05:00
|
|
|
delta[c] += nsecs - prev_nsecs[c];
|
|
|
|
prev_nsecs[c] = nsecs;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
/* Protect against the host clock being set backwards */
|
2007-10-16 04:27:00 -04:00
|
|
|
if (delta[c] < 0)
|
2007-02-10 04:44:12 -05:00
|
|
|
delta[c] = 0;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2007-02-10 04:44:12 -05:00
|
|
|
ticks += (delta[c] * HZ) / BILLION;
|
|
|
|
delta[c] -= (ticks * BILLION) / HZ;
|
2006-07-10 07:45:08 -04:00
|
|
|
}
|
2007-02-10 04:44:12 -05:00
|
|
|
else prev_nsecs[c] = os_nsecs();
|
2005-04-16 18:20:36 -04:00
|
|
|
#else
|
2006-07-10 07:45:08 -04:00
|
|
|
ticks = 1;
|
2005-04-16 18:20:36 -04:00
|
|
|
#endif
|
2007-10-16 04:27:00 -04:00
|
|
|
while (ticks > 0) {
|
2005-04-16 18:20:36 -04:00
|
|
|
do_IRQ(TIMER_IRQ, regs);
|
|
|
|
ticks--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-10 04:44:12 -05:00
|
|
|
/* Protects local_offset */
|
2006-01-18 20:42:42 -05:00
|
|
|
static DEFINE_SPINLOCK(timer_spinlock);
|
|
|
|
static unsigned long long local_offset = 0;
|
|
|
|
|
|
|
|
static inline unsigned long long get_time(void)
|
|
|
|
{
|
|
|
|
unsigned long long nsecs;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&timer_spinlock, flags);
|
|
|
|
nsecs = os_nsecs();
|
|
|
|
nsecs += local_offset;
|
|
|
|
spin_unlock_irqrestore(&timer_spinlock, flags);
|
|
|
|
|
|
|
|
return nsecs;
|
|
|
|
}
|
|
|
|
|
2006-10-08 17:49:34 -04:00
|
|
|
irqreturn_t um_timer(int irq, void *dev)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
2006-01-18 20:42:42 -05:00
|
|
|
unsigned long long nsecs;
|
2005-04-16 18:20:36 -04:00
|
|
|
unsigned long flags;
|
|
|
|
|
2006-06-30 04:55:56 -04:00
|
|
|
write_seqlock_irqsave(&xtime_lock, flags);
|
|
|
|
|
2006-09-29 05:00:32 -04:00
|
|
|
do_timer(1);
|
2006-01-18 20:42:42 -05:00
|
|
|
|
2007-05-06 17:51:51 -04:00
|
|
|
#ifdef CONFIG_UML_REAL_TIME_CLOCK
|
2006-09-27 04:50:42 -04:00
|
|
|
nsecs = get_time();
|
2007-05-06 17:51:51 -04:00
|
|
|
#else
|
|
|
|
nsecs = (unsigned long long) xtime.tv_sec * BILLION + xtime.tv_nsec +
|
|
|
|
BILLION / HZ;
|
|
|
|
#endif
|
2006-01-18 20:42:42 -05:00
|
|
|
xtime.tv_sec = nsecs / NSEC_PER_SEC;
|
|
|
|
xtime.tv_nsec = nsecs - xtime.tv_sec * NSEC_PER_SEC;
|
2006-06-30 04:55:56 -04:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
write_sequnlock_irqrestore(&xtime_lock, flags);
|
2006-01-18 20:42:42 -05:00
|
|
|
|
2006-06-30 04:55:56 -04:00
|
|
|
return IRQ_HANDLED;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2006-07-10 07:45:05 -04:00
|
|
|
static void register_timer(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
|
2007-10-16 04:27:00 -04:00
|
|
|
if (err != 0)
|
2006-09-26 02:33:05 -04:00
|
|
|
printk(KERN_ERR "register_timer : request_irq failed - "
|
2006-07-10 07:45:05 -04:00
|
|
|
"errno = %d\n", -err);
|
|
|
|
|
2006-09-26 02:33:05 -04:00
|
|
|
err = set_interval(1);
|
2007-10-16 04:27:00 -04:00
|
|
|
if (err != 0)
|
2006-09-26 02:33:05 -04:00
|
|
|
printk(KERN_ERR "register_timer : set_interval failed - "
|
|
|
|
"errno = %d\n", -err);
|
2006-07-10 07:45:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
extern void (*late_time_init)(void);
|
|
|
|
|
|
|
|
void time_init(void)
|
|
|
|
{
|
|
|
|
long long nsecs;
|
|
|
|
|
|
|
|
nsecs = os_nsecs();
|
|
|
|
set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
|
|
|
|
-nsecs % BILLION);
|
2007-05-06 17:51:51 -04:00
|
|
|
set_normalized_timespec(&xtime, nsecs / BILLION, nsecs % BILLION);
|
2006-07-10 07:45:05 -04:00
|
|
|
late_time_init = register_timer;
|
|
|
|
}
|
|
|
|
|
2006-01-18 20:42:42 -05:00
|
|
|
void do_gettimeofday(struct timeval *tv)
|
|
|
|
{
|
2007-05-06 17:51:51 -04:00
|
|
|
#ifdef CONFIG_UML_REAL_TIME_CLOCK
|
2006-01-18 20:42:42 -05:00
|
|
|
unsigned long long nsecs = get_time();
|
2007-05-06 17:51:51 -04:00
|
|
|
#else
|
|
|
|
unsigned long long nsecs = (unsigned long long) xtime.tv_sec * BILLION +
|
|
|
|
xtime.tv_nsec;
|
|
|
|
#endif
|
2006-01-18 20:42:42 -05:00
|
|
|
tv->tv_sec = nsecs / NSEC_PER_SEC;
|
2007-10-16 04:27:00 -04:00
|
|
|
/*
|
|
|
|
* Careful about calculations here - this was originally done as
|
2006-01-18 20:42:42 -05:00
|
|
|
* (nsecs - tv->tv_sec * NSEC_PER_SEC) / NSEC_PER_USEC
|
|
|
|
* which gave bogus (> 1000000) values. Dunno why, suspect gcc
|
|
|
|
* (4.0.0) miscompiled it, or there's a subtle 64/32-bit conversion
|
|
|
|
* problem that I missed.
|
|
|
|
*/
|
|
|
|
nsecs -= tv->tv_sec * NSEC_PER_SEC;
|
|
|
|
tv->tv_usec = (unsigned long) nsecs / NSEC_PER_USEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void set_time(unsigned long long nsecs)
|
|
|
|
{
|
|
|
|
unsigned long long now;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&timer_spinlock, flags);
|
|
|
|
now = os_nsecs();
|
|
|
|
local_offset = nsecs - now;
|
|
|
|
spin_unlock_irqrestore(&timer_spinlock, flags);
|
|
|
|
|
|
|
|
clock_was_set();
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2006-01-18 20:42:42 -05:00
|
|
|
int do_settimeofday(struct timespec *tv)
|
|
|
|
{
|
|
|
|
set_time((unsigned long long) tv->tv_sec * NSEC_PER_SEC + tv->tv_nsec);
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-16 04:26:58 -04:00
|
|
|
void timer_handler(int sig, struct uml_pt_regs *regs)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
2007-10-16 04:27:00 -04:00
|
|
|
if (current_thread->cpu == 0)
|
2007-05-08 03:23:22 -04:00
|
|
|
timer_irq(regs);
|
2005-04-16 18:20:36 -04:00
|
|
|
local_irq_disable();
|
2005-07-29 00:16:09 -04:00
|
|
|
irq_enter();
|
2007-10-16 04:26:58 -04:00
|
|
|
update_process_times(regs->is_user);
|
2005-07-29 00:16:09 -04:00
|
|
|
irq_exit();
|
2005-04-16 18:20:36 -04:00
|
|
|
local_irq_enable();
|
|
|
|
}
|