Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/time/tick-oneshot.c |
| 3 | * |
| 4 | * This file contains functions which manage high resolution tick |
| 5 | * related events. |
| 6 | * |
| 7 | * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> |
| 8 | * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar |
| 9 | * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner |
| 10 | * |
| 11 | * This code is licenced under the GPL version 2. For details see |
| 12 | * kernel-base/COPYING. |
| 13 | */ |
| 14 | #include <linux/cpu.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/hrtimer.h> |
Russell King | d7b9068 | 2008-04-17 07:46:24 +0200 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 18 | #include <linux/percpu.h> |
| 19 | #include <linux/profile.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/tick.h> |
| 22 | |
| 23 | #include "tick-internal.h" |
| 24 | |
| 25 | /** |
Thomas Gleixner | 7205656 | 2008-09-03 21:37:03 +0000 | [diff] [blame^] | 26 | * tick_program_event internal worker function |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 27 | */ |
Thomas Gleixner | 7205656 | 2008-09-03 21:37:03 +0000 | [diff] [blame^] | 28 | static int __tick_program_event(struct clock_event_device *dev, |
| 29 | ktime_t expires, int force) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 30 | { |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 31 | ktime_t now = ktime_get(); |
| 32 | |
| 33 | while (1) { |
| 34 | int ret = clockevents_program_event(dev, expires, now); |
| 35 | |
| 36 | if (!ret || !force) |
| 37 | return ret; |
| 38 | now = ktime_get(); |
| 39 | expires = ktime_add(now, ktime_set(0, dev->min_delta_ns)); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
Thomas Gleixner | 7205656 | 2008-09-03 21:37:03 +0000 | [diff] [blame^] | 44 | * tick_program_event |
| 45 | */ |
| 46 | int tick_program_event(ktime_t expires, int force) |
| 47 | { |
| 48 | struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev; |
| 49 | |
| 50 | return __tick_program_event(dev, expires, force); |
| 51 | } |
| 52 | |
| 53 | /** |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 54 | * tick_resume_onshot - resume oneshot mode |
| 55 | */ |
| 56 | void tick_resume_oneshot(void) |
| 57 | { |
| 58 | struct tick_device *td = &__get_cpu_var(tick_cpu_device); |
| 59 | struct clock_event_device *dev = td->evtdev; |
| 60 | |
| 61 | clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT); |
| 62 | tick_program_event(ktime_get(), 1); |
| 63 | } |
| 64 | |
| 65 | /** |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 66 | * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz) |
| 67 | */ |
| 68 | void tick_setup_oneshot(struct clock_event_device *newdev, |
| 69 | void (*handler)(struct clock_event_device *), |
| 70 | ktime_t next_event) |
| 71 | { |
| 72 | newdev->event_handler = handler; |
| 73 | clockevents_set_mode(newdev, CLOCK_EVT_MODE_ONESHOT); |
Thomas Gleixner | 7205656 | 2008-09-03 21:37:03 +0000 | [diff] [blame^] | 74 | __tick_program_event(newdev, next_event, 1); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * tick_switch_to_oneshot - switch to oneshot mode |
| 79 | */ |
| 80 | int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *)) |
| 81 | { |
| 82 | struct tick_device *td = &__get_cpu_var(tick_cpu_device); |
| 83 | struct clock_event_device *dev = td->evtdev; |
| 84 | |
| 85 | if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) || |
Ingo Molnar | 820de5c | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 86 | !tick_device_is_functional(dev)) { |
| 87 | |
| 88 | printk(KERN_INFO "Clockevents: " |
| 89 | "could not switch to one-shot mode:"); |
| 90 | if (!dev) { |
| 91 | printk(" no tick device\n"); |
| 92 | } else { |
| 93 | if (!tick_device_is_functional(dev)) |
| 94 | printk(" %s is not functional.\n", dev->name); |
| 95 | else |
| 96 | printk(" %s does not support one-shot mode.\n", |
| 97 | dev->name); |
| 98 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 99 | return -EINVAL; |
Ingo Molnar | 820de5c | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 100 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 101 | |
| 102 | td->mode = TICKDEV_MODE_ONESHOT; |
| 103 | dev->event_handler = handler; |
| 104 | clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT); |
| 105 | tick_broadcast_switch_to_oneshot(); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 110 | /** |
| 111 | * tick_init_highres - switch to high resolution mode |
| 112 | * |
| 113 | * Called with interrupts disabled. |
| 114 | */ |
| 115 | int tick_init_highres(void) |
| 116 | { |
| 117 | return tick_switch_to_oneshot(hrtimer_interrupt); |
| 118 | } |
| 119 | #endif |