| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 1 | /* | 
| Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 2 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL | 
|  | 4 | */ | 
|  | 5 |  | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 6 | #include <linux/clockchips.h> | 
|  | 7 | #include <linux/interrupt.h> | 
|  | 8 | #include <linux/jiffies.h> | 
|  | 9 | #include <linux/threads.h> | 
|  | 10 | #include <asm/irq.h> | 
|  | 11 | #include <asm/param.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "kern_util.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "os.h" | 
|  | 14 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | /* | 
|  | 16 | * Scheduler clock - returns current time in nanosec units. | 
|  | 17 | */ | 
|  | 18 | unsigned long long sched_clock(void) | 
|  | 19 | { | 
| Jeff Dike | 1a80521 | 2007-10-16 01:27:28 -0700 | [diff] [blame] | 20 | return (unsigned long long)jiffies_64 * (NSEC_PER_SEC / HZ); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | } | 
|  | 22 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 23 | void timer_handler(int sig, struct uml_pt_regs *regs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 25 | unsigned long flags; | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | local_irq_save(flags); | 
| Jeff Dike | d2753a6 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 28 | do_IRQ(TIMER_IRQ, regs); | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 29 | local_irq_restore(flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | } | 
|  | 31 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 32 | static void itimer_set_mode(enum clock_event_mode mode, | 
|  | 33 | struct clock_event_device *evt) | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 34 | { | 
| Jeff Dike | c5d4bb1 | 2008-02-04 22:31:14 -0800 | [diff] [blame] | 35 | switch (mode) { | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 36 | case CLOCK_EVT_MODE_PERIODIC: | 
|  | 37 | set_interval(); | 
|  | 38 | break; | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 39 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 40 | case CLOCK_EVT_MODE_SHUTDOWN: | 
|  | 41 | case CLOCK_EVT_MODE_UNUSED: | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 42 | case CLOCK_EVT_MODE_ONESHOT: | 
| Jeff Dike | d2753a6 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 43 | disable_timer(); | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 44 | break; | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 45 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 46 | case CLOCK_EVT_MODE_RESUME: | 
|  | 47 | break; | 
|  | 48 | } | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
| Jeff Dike | d2753a6 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 51 | static int itimer_next_event(unsigned long delta, | 
|  | 52 | struct clock_event_device *evt) | 
|  | 53 | { | 
|  | 54 | return timer_one_shot(delta + 1); | 
|  | 55 | } | 
|  | 56 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 57 | static struct clock_event_device itimer_clockevent = { | 
|  | 58 | .name		= "itimer", | 
|  | 59 | .rating		= 250, | 
|  | 60 | .cpumask	= CPU_MASK_ALL, | 
| Jeff Dike | d2753a6 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 61 | .features	= CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 62 | .set_mode	= itimer_set_mode, | 
| Jeff Dike | d2753a6 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 63 | .set_next_event = itimer_next_event, | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 64 | .shift		= 32, | 
|  | 65 | .irq		= 0, | 
|  | 66 | }; | 
|  | 67 |  | 
|  | 68 | static irqreturn_t um_timer(int irq, void *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 70 | (*itimer_clockevent.event_handler)(&itimer_clockevent); | 
| Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 71 |  | 
| Jeff Dike | 572e614 | 2006-06-30 01:55:56 -0700 | [diff] [blame] | 72 | return IRQ_HANDLED; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Jeff Dike | 791a644 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 75 | static cycle_t itimer_read(void) | 
|  | 76 | { | 
|  | 77 | return os_nsecs(); | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | static struct clocksource itimer_clocksource = { | 
|  | 81 | .name		= "itimer", | 
|  | 82 | .rating		= 300, | 
|  | 83 | .read		= itimer_read, | 
|  | 84 | .mask		= CLOCKSOURCE_MASK(64), | 
|  | 85 | .mult		= 1, | 
|  | 86 | .shift		= 0, | 
|  | 87 | .flags		= CLOCK_SOURCE_IS_CONTINUOUS, | 
|  | 88 | }; | 
|  | 89 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 90 | static void __init setup_itimer(void) | 
| Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 91 | { | 
|  | 92 | int err; | 
|  | 93 |  | 
|  | 94 | err = request_irq(TIMER_IRQ, um_timer, IRQF_DISABLED, "timer", NULL); | 
| Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 95 | if (err != 0) | 
| Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 96 | printk(KERN_ERR "register_timer : request_irq failed - " | 
| Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 97 | "errno = %d\n", -err); | 
|  | 98 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 99 | itimer_clockevent.mult = div_sc(HZ, NSEC_PER_SEC, 32); | 
|  | 100 | itimer_clockevent.max_delta_ns = | 
|  | 101 | clockevent_delta2ns(60 * HZ, &itimer_clockevent); | 
|  | 102 | itimer_clockevent.min_delta_ns = | 
|  | 103 | clockevent_delta2ns(1, &itimer_clockevent); | 
| Jeff Dike | 791a644 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 104 | err = clocksource_register(&itimer_clocksource); | 
|  | 105 | if (err) { | 
|  | 106 | printk(KERN_ERR "clocksource_register returned %d\n", err); | 
|  | 107 | return; | 
|  | 108 | } | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 109 | clockevents_register_device(&itimer_clockevent); | 
| Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
|  | 112 | extern void (*late_time_init)(void); | 
|  | 113 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 114 | void __init time_init(void) | 
| Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 115 | { | 
|  | 116 | long long nsecs; | 
|  | 117 |  | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 118 | timer_init(); | 
|  | 119 |  | 
| Jeff Dike | aceb343 | 2006-07-10 04:45:05 -0700 | [diff] [blame] | 120 | nsecs = os_nsecs(); | 
| Jeff Dike | 1a80521 | 2007-10-16 01:27:28 -0700 | [diff] [blame] | 121 | set_normalized_timespec(&wall_to_monotonic, -nsecs / NSEC_PER_SEC, | 
|  | 122 | -nsecs % NSEC_PER_SEC); | 
|  | 123 | set_normalized_timespec(&xtime, nsecs / NSEC_PER_SEC, | 
|  | 124 | nsecs % NSEC_PER_SEC); | 
| Jeff Dike | 31ccc1f | 2007-10-16 01:27:24 -0700 | [diff] [blame] | 125 | late_time_init = setup_itimer; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |