time.c 2.7 KB
Newer Older
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5
 * Licensed under the GPL
 */

J
Jeff Dike 已提交
6
#include "linux/clockchips.h"
L
Linus Torvalds 已提交
7
#include "linux/interrupt.h"
J
Jeff Dike 已提交
8 9
#include "linux/jiffies.h"
#include "linux/threads.h"
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22
#include "asm/irq.h"
#include "asm/param.h"
#include "kern_util.h"
#include "os.h"

/*
 * Scheduler clock - returns current time in nanosec units.
 */
unsigned long long sched_clock(void)
{
	return (unsigned long long)jiffies_64 * (1000000000 / HZ);
}

J
Jeff Dike 已提交
23
void timer_handler(int sig, struct uml_pt_regs *regs)
L
Linus Torvalds 已提交
24
{
J
Jeff Dike 已提交
25 26 27
	unsigned long flags;

	local_irq_save(flags);
J
Jeff Dike 已提交
28
	do_IRQ(TIMER_IRQ, regs);
J
Jeff Dike 已提交
29
	local_irq_restore(flags);
L
Linus Torvalds 已提交
30 31
}

J
Jeff Dike 已提交
32 33
static void itimer_set_mode(enum clock_event_mode mode,
			    struct clock_event_device *evt)
34
{
J
Jeff Dike 已提交
35 36 37 38 39 40 41 42
	switch(mode) {
	case CLOCK_EVT_MODE_PERIODIC:
		set_interval();
		break;

	case CLOCK_EVT_MODE_SHUTDOWN:
	case CLOCK_EVT_MODE_UNUSED:
	case CLOCK_EVT_MODE_ONESHOT:
J
Jeff Dike 已提交
43
		disable_timer();
J
Jeff Dike 已提交
44 45 46 47 48
		break;

	case CLOCK_EVT_MODE_RESUME:
		break;
	}
49 50
}

J
Jeff Dike 已提交
51 52 53 54 55 56
static int itimer_next_event(unsigned long delta,
			     struct clock_event_device *evt)
{
	return timer_one_shot(delta + 1);
}

J
Jeff Dike 已提交
57 58 59 60
static struct clock_event_device itimer_clockevent = {
	.name		= "itimer",
	.rating		= 250,
	.cpumask	= CPU_MASK_ALL,
J
Jeff Dike 已提交
61
	.features	= CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
J
Jeff Dike 已提交
62
	.set_mode	= itimer_set_mode,
J
Jeff Dike 已提交
63
	.set_next_event = itimer_next_event,
J
Jeff Dike 已提交
64 65 66 67 68
	.shift		= 32,
	.irq		= 0,
};

static irqreturn_t um_timer(int irq, void *dev)
L
Linus Torvalds 已提交
69
{
J
Jeff Dike 已提交
70
	(*itimer_clockevent.event_handler)(&itimer_clockevent);
71

72
	return IRQ_HANDLED;
L
Linus Torvalds 已提交
73 74
}

J
Jeff Dike 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
static cycle_t itimer_read(void)
{
	return os_nsecs();
}

static struct clocksource itimer_clocksource = {
	.name		= "itimer",
	.rating		= 300,
	.read		= itimer_read,
	.mask		= CLOCKSOURCE_MASK(64),
	.mult		= 1,
	.shift		= 0,
	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
};

J
Jeff Dike 已提交
90
static void __init setup_itimer(void)
91 92 93 94
{
	int err;

	err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL);
J
Jeff Dike 已提交
95
	if (err != 0)
J
Jeff Dike 已提交
96
		printk(KERN_ERR "register_timer : request_irq failed - "
97 98
		       "errno = %d\n", -err);

J
Jeff Dike 已提交
99 100 101 102 103
	itimer_clockevent.mult = div_sc(HZ, NSEC_PER_SEC, 32);
	itimer_clockevent.max_delta_ns =
		clockevent_delta2ns(60 * HZ, &itimer_clockevent);
	itimer_clockevent.min_delta_ns =
		clockevent_delta2ns(1, &itimer_clockevent);
J
Jeff Dike 已提交
104 105 106 107 108
	err = clocksource_register(&itimer_clocksource);
	if (err) {
		printk(KERN_ERR "clocksource_register returned %d\n", err);
		return;
	}
J
Jeff Dike 已提交
109
	clockevents_register_device(&itimer_clockevent);
110 111 112 113
}

extern void (*late_time_init)(void);

J
Jeff Dike 已提交
114
void __init time_init(void)
115 116 117
{
	long long nsecs;

J
Jeff Dike 已提交
118 119
	timer_init();

120 121 122
	nsecs = os_nsecs();
	set_normalized_timespec(&wall_to_monotonic, -nsecs / BILLION,
				-nsecs % BILLION);
J
Jeff Dike 已提交
123
	set_normalized_timespec(&xtime, nsecs / BILLION, nsecs % BILLION);
J
Jeff Dike 已提交
124
	late_time_init = setup_itimer;
L
Linus Torvalds 已提交
125
}