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