blob: 475ecceda7688cebc11b9afa8f919f70b2cdf23d [file] [log] [blame]
Thomas Gleixner35728b82018-10-31 19:21:09 +01001// SPDX-License-Identifier: GPL-2.0
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08002/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08003 * This file contains functions which manage high resolution tick
4 * related events.
5 *
6 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
7 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
8 * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08009 */
10#include <linux/cpu.h>
11#include <linux/err.h>
12#include <linux/hrtimer.h>
Russell Kingd7b90682008-04-17 07:46:24 +020013#include <linux/interrupt.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080014#include <linux/percpu.h>
15#include <linux/profile.h>
16#include <linux/sched.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080017
18#include "tick-internal.h"
19
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080020/**
Thomas Gleixner72056562008-09-03 21:37:03 +000021 * tick_program_event
22 */
23int tick_program_event(ktime_t expires, int force)
24{
Christoph Lameter909ea962010-12-08 16:22:55 +010025 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
Thomas Gleixner72056562008-09-03 21:37:03 +000026
Thomas Gleixner2456e852016-12-25 11:38:40 +010027 if (unlikely(expires == KTIME_MAX)) {
Viresh Kumard25408752015-04-03 09:04:05 +053028 /*
29 * We don't need the clock event device any more, stop it.
30 */
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +020031 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT_STOPPED);
Prasad Sodagudi39c82ca2017-10-26 11:37:22 -070032 dev->next_event = KTIME_MAX;
Viresh Kumard25408752015-04-03 09:04:05 +053033 return 0;
34 }
35
Viresh Kumar472c4a92015-05-21 13:33:46 +053036 if (unlikely(clockevent_state_oneshot_stopped(dev))) {
Viresh Kumard25408752015-04-03 09:04:05 +053037 /*
38 * We need the clock event again, configure it in ONESHOT mode
39 * before using it.
40 */
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +020041 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
Viresh Kumard25408752015-04-03 09:04:05 +053042 }
43
Martin Schwidefskyd1748302011-08-23 15:29:42 +020044 return clockevents_program_event(dev, expires, force);
Thomas Gleixner72056562008-09-03 21:37:03 +000045}
46
47/**
Ingo Molnar4bf07f62021-03-22 22:39:03 +010048 * tick_resume_oneshot - resume oneshot mode
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +010049 */
50void tick_resume_oneshot(void)
51{
Martin Schwidefskyd1748302011-08-23 15:29:42 +020052 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +010053
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +020054 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
Martin Schwidefskyd1748302011-08-23 15:29:42 +020055 clockevents_program_event(dev, ktime_get(), true);
Thomas Gleixnercd05a1f2007-03-17 00:25:52 +010056}
57
58/**
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080059 * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
60 */
61void tick_setup_oneshot(struct clock_event_device *newdev,
62 void (*handler)(struct clock_event_device *),
63 ktime_t next_event)
64{
65 newdev->event_handler = handler;
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +020066 clockevents_switch_state(newdev, CLOCK_EVT_STATE_ONESHOT);
Martin Schwidefskyd1748302011-08-23 15:29:42 +020067 clockevents_program_event(newdev, next_event, true);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080068}
69
70/**
71 * tick_switch_to_oneshot - switch to oneshot mode
72 */
73int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *))
74{
Christoph Lameter22127e92014-08-17 12:30:25 -050075 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080076 struct clock_event_device *dev = td->evtdev;
77
78 if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) ||
Ingo Molnar820de5c2007-07-21 04:37:36 -070079 !tick_device_is_functional(dev)) {
80
Geert Uytterhoeven4450dc02018-04-05 17:26:58 +020081 pr_info("Clockevents: could not switch to one-shot mode:");
Ingo Molnar820de5c2007-07-21 04:37:36 -070082 if (!dev) {
Geert Uytterhoeven4450dc02018-04-05 17:26:58 +020083 pr_cont(" no tick device\n");
Ingo Molnar820de5c2007-07-21 04:37:36 -070084 } else {
85 if (!tick_device_is_functional(dev))
Geert Uytterhoeven4450dc02018-04-05 17:26:58 +020086 pr_cont(" %s is not functional.\n", dev->name);
Ingo Molnar820de5c2007-07-21 04:37:36 -070087 else
Geert Uytterhoeven4450dc02018-04-05 17:26:58 +020088 pr_cont(" %s does not support one-shot mode.\n",
89 dev->name);
Ingo Molnar820de5c2007-07-21 04:37:36 -070090 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080091 return -EINVAL;
Ingo Molnar820de5c2007-07-21 04:37:36 -070092 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080093
94 td->mode = TICKDEV_MODE_ONESHOT;
95 dev->event_handler = handler;
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +020096 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080097 tick_broadcast_switch_to_oneshot();
98 return 0;
99}
100
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +0200101/**
102 * tick_check_oneshot_mode - check whether the system is in oneshot mode
103 *
104 * returns 1 when either nohz or highres are enabled. otherwise 0.
105 */
106int tick_oneshot_mode_active(void)
107{
108 unsigned long flags;
109 int ret;
110
111 local_irq_save(flags);
Christoph Lameter909ea962010-12-08 16:22:55 +0100112 ret = __this_cpu_read(tick_cpu_device.mode) == TICKDEV_MODE_ONESHOT;
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +0200113 local_irq_restore(flags);
114
115 return ret;
116}
117
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800118#ifdef CONFIG_HIGH_RES_TIMERS
119/**
120 * tick_init_highres - switch to high resolution mode
121 *
122 * Called with interrupts disabled.
123 */
124int tick_init_highres(void)
125{
126 return tick_switch_to_oneshot(hrtimer_interrupt);
127}
128#endif