blob: c1f518e7aa808a281b5dab69f3961a02b2c14315 [file] [log] [blame]
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001/*
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 Kingd7b90682008-04-17 07:46:24 +020017#include <linux/interrupt.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080018#include <linux/percpu.h>
19#include <linux/profile.h>
20#include <linux/sched.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080021
22#include "tick-internal.h"
23
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080024/**
Thomas Gleixner72056562008-09-03 21:37:03 +000025 * tick_program_event
26 */
27int tick_program_event(ktime_t expires, int force)
28{
Christoph Lameter909ea962010-12-08 16:22:55 +010029 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
Thomas Gleixner72056562008-09-03 21:37:03 +000030
Thomas Gleixner2456e852016-12-25 11:38:40 +010031 if (unlikely(expires == KTIME_MAX)) {
Viresh Kumard25408752015-04-03 09:04:05 +053032 /*
33 * We don't need the clock event device any more, stop it.
34 */
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +020035 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT_STOPPED);
Prasad Sodagudi39c82ca2017-10-26 11:37:22 -070036 dev->next_event = KTIME_MAX;
Viresh Kumard25408752015-04-03 09:04:05 +053037 return 0;
38 }
39
Viresh Kumar472c4a92015-05-21 13:33:46 +053040 if (unlikely(clockevent_state_oneshot_stopped(dev))) {
Viresh Kumard25408752015-04-03 09:04:05 +053041 /*
42 * We need the clock event again, configure it in ONESHOT mode
43 * before using it.
44 */
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +020045 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
Viresh Kumard25408752015-04-03 09:04:05 +053046 }
47
Martin Schwidefskyd1748302011-08-23 15:29:42 +020048 return clockevents_program_event(dev, expires, force);
Thomas Gleixner72056562008-09-03 21:37:03 +000049}
50
51/**
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +010052 * tick_resume_onshot - resume oneshot mode
53 */
54void tick_resume_oneshot(void)
55{
Martin Schwidefskyd1748302011-08-23 15:29:42 +020056 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +010057
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +020058 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
Martin Schwidefskyd1748302011-08-23 15:29:42 +020059 clockevents_program_event(dev, ktime_get(), true);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +010060}
61
62/**
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080063 * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
64 */
65void tick_setup_oneshot(struct clock_event_device *newdev,
66 void (*handler)(struct clock_event_device *),
67 ktime_t next_event)
68{
69 newdev->event_handler = handler;
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +020070 clockevents_switch_state(newdev, CLOCK_EVT_STATE_ONESHOT);
Martin Schwidefskyd1748302011-08-23 15:29:42 +020071 clockevents_program_event(newdev, next_event, true);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080072}
73
74/**
75 * tick_switch_to_oneshot - switch to oneshot mode
76 */
77int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *))
78{
Christoph Lameter22127e92014-08-17 12:30:25 -050079 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080080 struct clock_event_device *dev = td->evtdev;
81
82 if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) ||
Ingo Molnar820de5c2007-07-21 04:37:36 -070083 !tick_device_is_functional(dev)) {
84
85 printk(KERN_INFO "Clockevents: "
86 "could not switch to one-shot mode:");
87 if (!dev) {
88 printk(" no tick device\n");
89 } else {
90 if (!tick_device_is_functional(dev))
91 printk(" %s is not functional.\n", dev->name);
92 else
93 printk(" %s does not support one-shot mode.\n",
94 dev->name);
95 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080096 return -EINVAL;
Ingo Molnar820de5c2007-07-21 04:37:36 -070097 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080098
99 td->mode = TICKDEV_MODE_ONESHOT;
100 dev->event_handler = handler;
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200101 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800102 tick_broadcast_switch_to_oneshot();
103 return 0;
104}
105
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +0200106/**
107 * tick_check_oneshot_mode - check whether the system is in oneshot mode
108 *
109 * returns 1 when either nohz or highres are enabled. otherwise 0.
110 */
111int tick_oneshot_mode_active(void)
112{
113 unsigned long flags;
114 int ret;
115
116 local_irq_save(flags);
Christoph Lameter909ea962010-12-08 16:22:55 +0100117 ret = __this_cpu_read(tick_cpu_device.mode) == TICKDEV_MODE_ONESHOT;
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +0200118 local_irq_restore(flags);
119
120 return ret;
121}
122
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800123#ifdef CONFIG_HIGH_RES_TIMERS
124/**
125 * tick_init_highres - switch to high resolution mode
126 *
127 * Called with interrupts disabled.
128 */
129int tick_init_highres(void)
130{
131 return tick_switch_to_oneshot(hrtimer_interrupt);
132}
133#endif