xref: /linux/kernel/time/tick-oneshot.c (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * 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
9  */
10 #include <linux/cpu.h>
11 #include <linux/err.h>
12 #include <linux/hrtimer.h>
13 #include <linux/interrupt.h>
14 #include <linux/percpu.h>
15 #include <linux/profile.h>
16 #include <linux/sched.h>
17 
18 #include "tick-internal.h"
19 
20 /**
21  * tick_program_event - program the CPU local timer device for the next event
22  * @expires: the time at which the next timer event should occur
23  * @force: flag to force reprograming even if the event time hasn't changed
24  *
25  * Return: 0 on success, negative error code on failure
26  */
27 int tick_program_event(ktime_t expires, int force)
28 {
29 	struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
30 
31 	if (unlikely(expires == KTIME_MAX)) {
32 		/*
33 		 * We don't need the clock event device any more, stop it.
34 		 */
35 		clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT_STOPPED);
36 		dev->next_event = KTIME_MAX;
37 		return 0;
38 	}
39 
40 	if (unlikely(clockevent_state_oneshot_stopped(dev))) {
41 		/*
42 		 * We need the clock event again, configure it in ONESHOT mode
43 		 * before using it.
44 		 */
45 		clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
46 	}
47 
48 	return clockevents_program_event(dev, expires, force);
49 }
50 
51 /**
52  * tick_resume_oneshot - resume oneshot mode
53  */
54 void tick_resume_oneshot(void)
55 {
56 	struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
57 
58 	clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
59 	clockevents_program_event(dev, ktime_get(), true);
60 }
61 
62 /**
63  * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
64  * @newdev: Pointer to the clock event device to configure
65  * @handler: Function to be called when the event device triggers an interrupt
66  * @next_event: Initial expiry time for the next event (in ktime)
67  *
68  * Configures the specified clock event device for onshot mode,
69  * assigns the given handler as its event callback, and programs
70  * the device to trigger at the specified next event time.
71  */
72 void tick_setup_oneshot(struct clock_event_device *newdev,
73 			void (*handler)(struct clock_event_device *),
74 			ktime_t next_event)
75 {
76 	newdev->event_handler = handler;
77 	clockevents_switch_state(newdev, CLOCK_EVT_STATE_ONESHOT);
78 	clockevents_program_event(newdev, next_event, true);
79 }
80 
81 /**
82  * tick_switch_to_oneshot - switch to oneshot mode
83  * @handler: function to call when an event occurs on the tick device
84  *
85  * Return: 0 on success, -EINVAL if the tick device is not present,
86  *         not functional, or does not support oneshot mode.
87  */
88 int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *))
89 {
90 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
91 	struct clock_event_device *dev = td->evtdev;
92 
93 	if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) ||
94 		    !tick_device_is_functional(dev)) {
95 
96 		pr_info("Clockevents: could not switch to one-shot mode:");
97 		if (!dev) {
98 			pr_cont(" no tick device\n");
99 		} else {
100 			if (!tick_device_is_functional(dev))
101 				pr_cont(" %s is not functional.\n", dev->name);
102 			else
103 				pr_cont(" %s does not support one-shot mode.\n",
104 					dev->name);
105 		}
106 		return -EINVAL;
107 	}
108 
109 	td->mode = TICKDEV_MODE_ONESHOT;
110 	dev->event_handler = handler;
111 	clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
112 	tick_broadcast_switch_to_oneshot();
113 	return 0;
114 }
115 
116 /**
117  * tick_oneshot_mode_active - check whether the system is in oneshot mode
118  *
119  * Return: 1 when either nohz or highres are enabled, otherwise 0.
120  */
121 int tick_oneshot_mode_active(void)
122 {
123 	unsigned long flags;
124 	int ret;
125 
126 	local_irq_save(flags);
127 	ret = __this_cpu_read(tick_cpu_device.mode) == TICKDEV_MODE_ONESHOT;
128 	local_irq_restore(flags);
129 
130 	return ret;
131 }
132 
133 #ifdef CONFIG_HIGH_RES_TIMERS
134 /**
135  * tick_init_highres - switch to high resolution mode
136  *
137  * Called with interrupts disabled.
138  *
139  * Return: 0 on success, -EINVAL if the tick device cannot switch
140  *         to oneshot/high-resolution mode.
141  */
142 int tick_init_highres(void)
143 {
144 	return tick_switch_to_oneshot(hrtimer_interrupt);
145 }
146 #endif
147