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> |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 21 | |
| 22 | #include "tick-internal.h" |
| 23 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 24 | /** |
Thomas Gleixner | 7205656 | 2008-09-03 21:37:03 +0000 | [diff] [blame] | 25 | * tick_program_event |
| 26 | */ |
| 27 | int tick_program_event(ktime_t expires, int force) |
| 28 | { |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 29 | struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev); |
Thomas Gleixner | 7205656 | 2008-09-03 21:37:03 +0000 | [diff] [blame] | 30 | |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 31 | if (unlikely(expires == KTIME_MAX)) { |
Viresh Kumar | d2540875 | 2015-04-03 09:04:05 +0530 | [diff] [blame] | 32 | /* |
| 33 | * We don't need the clock event device any more, stop it. |
| 34 | */ |
Thomas Gleixner | d7eb231 | 2015-06-02 14:08:46 +0200 | [diff] [blame] | 35 | clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT_STOPPED); |
Viresh Kumar | d2540875 | 2015-04-03 09:04:05 +0530 | [diff] [blame] | 36 | return 0; |
| 37 | } |
| 38 | |
Viresh Kumar | 472c4a9 | 2015-05-21 13:33:46 +0530 | [diff] [blame] | 39 | if (unlikely(clockevent_state_oneshot_stopped(dev))) { |
Viresh Kumar | d2540875 | 2015-04-03 09:04:05 +0530 | [diff] [blame] | 40 | /* |
| 41 | * We need the clock event again, configure it in ONESHOT mode |
| 42 | * before using it. |
| 43 | */ |
Thomas Gleixner | d7eb231 | 2015-06-02 14:08:46 +0200 | [diff] [blame] | 44 | clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT); |
Viresh Kumar | d2540875 | 2015-04-03 09:04:05 +0530 | [diff] [blame] | 45 | } |
| 46 | |
Martin Schwidefsky | d174830 | 2011-08-23 15:29:42 +0200 | [diff] [blame] | 47 | return clockevents_program_event(dev, expires, force); |
Thomas Gleixner | 7205656 | 2008-09-03 21:37:03 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 51 | * tick_resume_onshot - resume oneshot mode |
| 52 | */ |
| 53 | void tick_resume_oneshot(void) |
| 54 | { |
Martin Schwidefsky | d174830 | 2011-08-23 15:29:42 +0200 | [diff] [blame] | 55 | struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev); |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 56 | |
Thomas Gleixner | d7eb231 | 2015-06-02 14:08:46 +0200 | [diff] [blame] | 57 | clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT); |
Martin Schwidefsky | d174830 | 2011-08-23 15:29:42 +0200 | [diff] [blame] | 58 | clockevents_program_event(dev, ktime_get(), true); |
Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 62 | * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz) |
| 63 | */ |
| 64 | void tick_setup_oneshot(struct clock_event_device *newdev, |
| 65 | void (*handler)(struct clock_event_device *), |
| 66 | ktime_t next_event) |
| 67 | { |
| 68 | newdev->event_handler = handler; |
Thomas Gleixner | d7eb231 | 2015-06-02 14:08:46 +0200 | [diff] [blame] | 69 | clockevents_switch_state(newdev, CLOCK_EVT_STATE_ONESHOT); |
Martin Schwidefsky | d174830 | 2011-08-23 15:29:42 +0200 | [diff] [blame] | 70 | clockevents_program_event(newdev, next_event, true); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * tick_switch_to_oneshot - switch to oneshot mode |
| 75 | */ |
| 76 | int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *)) |
| 77 | { |
Christoph Lameter | 22127e9 | 2014-08-17 12:30:25 -0500 | [diff] [blame] | 78 | struct tick_device *td = this_cpu_ptr(&tick_cpu_device); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 79 | struct clock_event_device *dev = td->evtdev; |
| 80 | |
| 81 | if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) || |
Ingo Molnar | 820de5c | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 82 | !tick_device_is_functional(dev)) { |
| 83 | |
| 84 | printk(KERN_INFO "Clockevents: " |
| 85 | "could not switch to one-shot mode:"); |
| 86 | if (!dev) { |
| 87 | printk(" no tick device\n"); |
| 88 | } else { |
| 89 | if (!tick_device_is_functional(dev)) |
| 90 | printk(" %s is not functional.\n", dev->name); |
| 91 | else |
| 92 | printk(" %s does not support one-shot mode.\n", |
| 93 | dev->name); |
| 94 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 95 | return -EINVAL; |
Ingo Molnar | 820de5c | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 96 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 97 | |
| 98 | td->mode = TICKDEV_MODE_ONESHOT; |
| 99 | dev->event_handler = handler; |
Thomas Gleixner | d7eb231 | 2015-06-02 14:08:46 +0200 | [diff] [blame] | 100 | clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 101 | tick_broadcast_switch_to_oneshot(); |
| 102 | return 0; |
| 103 | } |
| 104 | |
Thomas Gleixner | cd6d95d | 2009-06-12 11:29:27 +0200 | [diff] [blame] | 105 | /** |
| 106 | * tick_check_oneshot_mode - check whether the system is in oneshot mode |
| 107 | * |
| 108 | * returns 1 when either nohz or highres are enabled. otherwise 0. |
| 109 | */ |
| 110 | int tick_oneshot_mode_active(void) |
| 111 | { |
| 112 | unsigned long flags; |
| 113 | int ret; |
| 114 | |
| 115 | local_irq_save(flags); |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 116 | ret = __this_cpu_read(tick_cpu_device.mode) == TICKDEV_MODE_ONESHOT; |
Thomas Gleixner | cd6d95d | 2009-06-12 11:29:27 +0200 | [diff] [blame] | 117 | local_irq_restore(flags); |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 122 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 123 | /** |
| 124 | * tick_init_highres - switch to high resolution mode |
| 125 | * |
| 126 | * Called with interrupts disabled. |
| 127 | */ |
| 128 | int tick_init_highres(void) |
| 129 | { |
| 130 | return tick_switch_to_oneshot(hrtimer_interrupt); |
| 131 | } |
| 132 | #endif |