time.c 2.5 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
#include "kern_util.h"
#include "os.h"

J
Jeff Dike 已提交
16
void timer_handler(int sig, struct uml_pt_regs *regs)
L
Linus Torvalds 已提交
17
{
J
Jeff Dike 已提交
18 19 20
	unsigned long flags;

	local_irq_save(flags);
J
Jeff Dike 已提交
21
	do_IRQ(TIMER_IRQ, regs);
J
Jeff Dike 已提交
22
	local_irq_restore(flags);
L
Linus Torvalds 已提交
23 24
}

J
Jeff Dike 已提交
25 26
static void itimer_set_mode(enum clock_event_mode mode,
			    struct clock_event_device *evt)
27
{
J
Jeff Dike 已提交
28
	switch (mode) {
J
Jeff Dike 已提交
29 30 31 32 33 34 35
	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 已提交
36
		disable_timer();
J
Jeff Dike 已提交
37 38 39 40 41
		break;

	case CLOCK_EVT_MODE_RESUME:
		break;
	}
42 43
}

J
Jeff Dike 已提交
44 45 46 47 48 49
static int itimer_next_event(unsigned long delta,
			     struct clock_event_device *evt)
{
	return timer_one_shot(delta + 1);
}

J
Jeff Dike 已提交
50 51 52
static struct clock_event_device itimer_clockevent = {
	.name		= "itimer",
	.rating		= 250,
53
	.cpumask	= cpu_all_mask,
J
Jeff Dike 已提交
54
	.features	= CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
J
Jeff Dike 已提交
55
	.set_mode	= itimer_set_mode,
J
Jeff Dike 已提交
56
	.set_next_event = itimer_next_event,
J
Jeff Dike 已提交
57 58 59 60 61
	.shift		= 32,
	.irq		= 0,
};

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

65
	return IRQ_HANDLED;
L
Linus Torvalds 已提交
66 67
}

68
static cycle_t itimer_read(struct clocksource *cs)
J
Jeff Dike 已提交
69
{
70
	return os_nsecs() / 1000;
J
Jeff Dike 已提交
71 72 73 74 75 76 77
}

static struct clocksource itimer_clocksource = {
	.name		= "itimer",
	.rating		= 300,
	.read		= itimer_read,
	.mask		= CLOCKSOURCE_MASK(64),
78
	.mult		= 1000,
J
Jeff Dike 已提交
79 80 81 82
	.shift		= 0,
	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
};

J
Jeff Dike 已提交
83
static void __init setup_itimer(void)
84 85 86 87
{
	int err;

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

J
Jeff Dike 已提交
92 93 94 95 96
	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 已提交
97 98 99 100 101
	err = clocksource_register(&itimer_clocksource);
	if (err) {
		printk(KERN_ERR "clocksource_register returned %d\n", err);
		return;
	}
J
Jeff Dike 已提交
102
	clockevents_register_device(&itimer_clockevent);
103 104
}

105 106 107 108 109 110 111
void read_persistent_clock(struct timespec *ts)
{
	nsecs = os_nsecs();
	set_normalized_timespec(ts, nsecs / NSEC_PER_SEC,
				nsecs % NSEC_PER_SEC);
}

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

J
Jeff Dike 已提交
116 117
	timer_init();
	late_time_init = setup_itimer;
L
Linus Torvalds 已提交
118
}