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 | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 28 | int tick_dev_program_event(struct clock_event_device *dev, ktime_t expires, |
| 29 | 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(); |
Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 32 | int i; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 33 | |
Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 34 | for (i = 0;;) { |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 35 | int ret = clockevents_program_event(dev, expires, now); |
| 36 | |
| 37 | if (!ret || !force) |
| 38 | return ret; |
Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * We tried 2 times to program the device with the given |
| 42 | * min_delta_ns. If that's not working then we double it |
| 43 | * and emit a warning. |
| 44 | */ |
| 45 | if (++i > 2) { |
Thomas Gleixner | 61c22c3 | 2008-09-09 21:38:57 +0200 | [diff] [blame] | 46 | /* Increase the min. delta and try again */ |
Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 47 | if (!dev->min_delta_ns) |
| 48 | dev->min_delta_ns = 5000; |
| 49 | else |
Thomas Gleixner | 61c22c3 | 2008-09-09 21:38:57 +0200 | [diff] [blame] | 50 | dev->min_delta_ns += dev->min_delta_ns >> 1; |
| 51 | |
| 52 | printk(KERN_WARNING |
| 53 | "CE: %s increasing min_delta_ns to %lu nsec\n", |
| 54 | dev->name ? dev->name : "?", |
| 55 | dev->min_delta_ns << 1); |
| 56 | |
Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 57 | i = 0; |
| 58 | } |
| 59 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 60 | now = ktime_get(); |
Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 61 | expires = ktime_add_ns(now, dev->min_delta_ns); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
Thomas Gleixner | 7205656 | 2008-09-03 21:37:03 +0000 | [diff] [blame] | 66 | * tick_program_event |
| 67 | */ |
| 68 | int tick_program_event(ktime_t expires, int force) |
| 69 | { |
| 70 | struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev; |
| 71 | |
Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 72 | return tick_dev_program_event(dev, expires, force); |
Thomas Gleixner | 7205656 | 2008-09-03 21:37:03 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 76 | * tick_resume_onshot - resume oneshot mode |
| 77 | */ |
| 78 | void tick_resume_oneshot(void) |
| 79 | { |
| 80 | struct tick_device *td = &__get_cpu_var(tick_cpu_device); |
| 81 | struct clock_event_device *dev = td->evtdev; |
| 82 | |
| 83 | clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT); |
| 84 | tick_program_event(ktime_get(), 1); |
| 85 | } |
| 86 | |
| 87 | /** |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 88 | * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz) |
| 89 | */ |
| 90 | void tick_setup_oneshot(struct clock_event_device *newdev, |
| 91 | void (*handler)(struct clock_event_device *), |
| 92 | ktime_t next_event) |
| 93 | { |
| 94 | newdev->event_handler = handler; |
| 95 | clockevents_set_mode(newdev, CLOCK_EVT_MODE_ONESHOT); |
Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 96 | tick_dev_program_event(newdev, next_event, 1); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * tick_switch_to_oneshot - switch to oneshot mode |
| 101 | */ |
| 102 | int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *)) |
| 103 | { |
| 104 | struct tick_device *td = &__get_cpu_var(tick_cpu_device); |
| 105 | struct clock_event_device *dev = td->evtdev; |
| 106 | |
| 107 | if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) || |
Ingo Molnar | 820de5c | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 108 | !tick_device_is_functional(dev)) { |
| 109 | |
| 110 | printk(KERN_INFO "Clockevents: " |
| 111 | "could not switch to one-shot mode:"); |
| 112 | if (!dev) { |
| 113 | printk(" no tick device\n"); |
| 114 | } else { |
| 115 | if (!tick_device_is_functional(dev)) |
| 116 | printk(" %s is not functional.\n", dev->name); |
| 117 | else |
| 118 | printk(" %s does not support one-shot mode.\n", |
| 119 | dev->name); |
| 120 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 121 | return -EINVAL; |
Ingo Molnar | 820de5c | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 122 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 123 | |
| 124 | td->mode = TICKDEV_MODE_ONESHOT; |
| 125 | dev->event_handler = handler; |
| 126 | clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT); |
| 127 | tick_broadcast_switch_to_oneshot(); |
| 128 | return 0; |
| 129 | } |
| 130 | |
Thomas Gleixner | cd6d95d | 2009-06-12 11:29:27 +0200 | [diff] [blame] | 131 | /** |
| 132 | * tick_check_oneshot_mode - check whether the system is in oneshot mode |
| 133 | * |
| 134 | * returns 1 when either nohz or highres are enabled. otherwise 0. |
| 135 | */ |
| 136 | int tick_oneshot_mode_active(void) |
| 137 | { |
| 138 | unsigned long flags; |
| 139 | int ret; |
| 140 | |
| 141 | local_irq_save(flags); |
| 142 | ret = __get_cpu_var(tick_cpu_device).mode == TICKDEV_MODE_ONESHOT; |
| 143 | local_irq_restore(flags); |
| 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 148 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 149 | /** |
| 150 | * tick_init_highres - switch to high resolution mode |
| 151 | * |
| 152 | * Called with interrupts disabled. |
| 153 | */ |
| 154 | int tick_init_highres(void) |
| 155 | { |
| 156 | return tick_switch_to_oneshot(hrtimer_interrupt); |
| 157 | } |
| 158 | #endif |