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>
A
Adrian Bunk 已提交
7
#include <linux/init.h>
J
Jeff Dike 已提交
8 9 10 11 12
#include <linux/interrupt.h>
#include <linux/jiffies.h>
#include <linux/threads.h>
#include <asm/irq.h>
#include <asm/param.h>
L
Linus Torvalds 已提交
13 14 15 16 17 18 19 20
#include "kern_util.h"
#include "os.h"

/*
 * Scheduler clock - returns current time in nanosec units.
 */
unsigned long long sched_clock(void)
{
J
Jeff Dike 已提交
21
	return (unsigned long long)jiffies_64 * (NSEC_PER_SEC / HZ);
L
Linus Torvalds 已提交
22 23
}

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

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

J
Jeff Dike 已提交
33 34
static void itimer_set_mode(enum clock_event_mode mode,
			    struct clock_event_device *evt)
35
{
J
Jeff Dike 已提交
36
	switch (mode) {
J
Jeff Dike 已提交
37 38 39 40 41 42 43
	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 已提交
44
		disable_timer();
J
Jeff Dike 已提交
45 46 47 48 49
		break;

	case CLOCK_EVT_MODE_RESUME:
		break;
	}
50 51
}

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

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

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

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

J
Jeff Dike 已提交
76 77
static cycle_t itimer_read(void)
{
78
	return os_nsecs() / 1000;
J
Jeff Dike 已提交
79 80 81 82 83 84 85
}

static struct clocksource itimer_clocksource = {
	.name		= "itimer",
	.rating		= 300,
	.read		= itimer_read,
	.mask		= CLOCKSOURCE_MASK(64),
86
	.mult		= 1000,
J
Jeff Dike 已提交
87 88 89 90
	.shift		= 0,
	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
};

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

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

J
Jeff Dike 已提交
100 101 102 103 104
	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 已提交
105 106 107 108 109
	err = clocksource_register(&itimer_clocksource);
	if (err) {
		printk(KERN_ERR "clocksource_register returned %d\n", err);
		return;
	}
J
Jeff Dike 已提交
110
	clockevents_register_device(&itimer_clockevent);
111 112
}

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

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

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