135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
279bf2bb3SThomas Gleixner /*
379bf2bb3SThomas Gleixner * This file contains functions which manage high resolution tick
479bf2bb3SThomas Gleixner * related events.
579bf2bb3SThomas Gleixner *
679bf2bb3SThomas Gleixner * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
779bf2bb3SThomas Gleixner * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
879bf2bb3SThomas Gleixner * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
979bf2bb3SThomas Gleixner */
1079bf2bb3SThomas Gleixner #include <linux/cpu.h>
1179bf2bb3SThomas Gleixner #include <linux/err.h>
1279bf2bb3SThomas Gleixner #include <linux/hrtimer.h>
13d7b90689SRussell King #include <linux/interrupt.h>
1479bf2bb3SThomas Gleixner #include <linux/percpu.h>
1579bf2bb3SThomas Gleixner #include <linux/profile.h>
1679bf2bb3SThomas Gleixner #include <linux/sched.h>
1779bf2bb3SThomas Gleixner
1879bf2bb3SThomas Gleixner #include "tick-internal.h"
1979bf2bb3SThomas Gleixner
2079bf2bb3SThomas Gleixner /**
21*f3cb8080SRandy Dunlap * tick_program_event - program the CPU local timer device for the next event
227205656aSThomas Gleixner */
tick_program_event(ktime_t expires,int force)237205656aSThomas Gleixner int tick_program_event(ktime_t expires, int force)
247205656aSThomas Gleixner {
25909ea964SChristoph Lameter struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
267205656aSThomas Gleixner
272456e855SThomas Gleixner if (unlikely(expires == KTIME_MAX)) {
28d2540875SViresh Kumar /*
29d2540875SViresh Kumar * We don't need the clock event device any more, stop it.
30d2540875SViresh Kumar */
31d7eb231cSThomas Gleixner clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT_STOPPED);
3239c82cafSPrasad Sodagudi dev->next_event = KTIME_MAX;
33d2540875SViresh Kumar return 0;
34d2540875SViresh Kumar }
35d2540875SViresh Kumar
36472c4a94SViresh Kumar if (unlikely(clockevent_state_oneshot_stopped(dev))) {
37d2540875SViresh Kumar /*
38d2540875SViresh Kumar * We need the clock event again, configure it in ONESHOT mode
39d2540875SViresh Kumar * before using it.
40d2540875SViresh Kumar */
41d7eb231cSThomas Gleixner clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
42d2540875SViresh Kumar }
43d2540875SViresh Kumar
44d1748302SMartin Schwidefsky return clockevents_program_event(dev, expires, force);
457205656aSThomas Gleixner }
467205656aSThomas Gleixner
477205656aSThomas Gleixner /**
484bf07f65SIngo Molnar * tick_resume_oneshot - resume oneshot mode
49cd05a1f8SThomas Gleixner */
tick_resume_oneshot(void)50cd05a1f8SThomas Gleixner void tick_resume_oneshot(void)
51cd05a1f8SThomas Gleixner {
52d1748302SMartin Schwidefsky struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
53cd05a1f8SThomas Gleixner
54d7eb231cSThomas Gleixner clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
55d1748302SMartin Schwidefsky clockevents_program_event(dev, ktime_get(), true);
56cd05a1f8SThomas Gleixner }
57cd05a1f8SThomas Gleixner
58cd05a1f8SThomas Gleixner /**
5979bf2bb3SThomas Gleixner * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
6079bf2bb3SThomas Gleixner */
tick_setup_oneshot(struct clock_event_device * newdev,void (* handler)(struct clock_event_device *),ktime_t next_event)6179bf2bb3SThomas Gleixner void tick_setup_oneshot(struct clock_event_device *newdev,
6279bf2bb3SThomas Gleixner void (*handler)(struct clock_event_device *),
6379bf2bb3SThomas Gleixner ktime_t next_event)
6479bf2bb3SThomas Gleixner {
6579bf2bb3SThomas Gleixner newdev->event_handler = handler;
66d7eb231cSThomas Gleixner clockevents_switch_state(newdev, CLOCK_EVT_STATE_ONESHOT);
67d1748302SMartin Schwidefsky clockevents_program_event(newdev, next_event, true);
6879bf2bb3SThomas Gleixner }
6979bf2bb3SThomas Gleixner
7079bf2bb3SThomas Gleixner /**
7179bf2bb3SThomas Gleixner * tick_switch_to_oneshot - switch to oneshot mode
7279bf2bb3SThomas Gleixner */
tick_switch_to_oneshot(void (* handler)(struct clock_event_device *))7379bf2bb3SThomas Gleixner int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *))
7479bf2bb3SThomas Gleixner {
7522127e93SChristoph Lameter struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
7679bf2bb3SThomas Gleixner struct clock_event_device *dev = td->evtdev;
7779bf2bb3SThomas Gleixner
7879bf2bb3SThomas Gleixner if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) ||
79820de5c3SIngo Molnar !tick_device_is_functional(dev)) {
80820de5c3SIngo Molnar
814450dc0aSGeert Uytterhoeven pr_info("Clockevents: could not switch to one-shot mode:");
82820de5c3SIngo Molnar if (!dev) {
834450dc0aSGeert Uytterhoeven pr_cont(" no tick device\n");
84820de5c3SIngo Molnar } else {
85820de5c3SIngo Molnar if (!tick_device_is_functional(dev))
864450dc0aSGeert Uytterhoeven pr_cont(" %s is not functional.\n", dev->name);
87820de5c3SIngo Molnar else
884450dc0aSGeert Uytterhoeven pr_cont(" %s does not support one-shot mode.\n",
89820de5c3SIngo Molnar dev->name);
90820de5c3SIngo Molnar }
9179bf2bb3SThomas Gleixner return -EINVAL;
92820de5c3SIngo Molnar }
9379bf2bb3SThomas Gleixner
9479bf2bb3SThomas Gleixner td->mode = TICKDEV_MODE_ONESHOT;
9579bf2bb3SThomas Gleixner dev->event_handler = handler;
96d7eb231cSThomas Gleixner clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
9779bf2bb3SThomas Gleixner tick_broadcast_switch_to_oneshot();
9879bf2bb3SThomas Gleixner return 0;
9979bf2bb3SThomas Gleixner }
10079bf2bb3SThomas Gleixner
101cd6d95d8SThomas Gleixner /**
102*f3cb8080SRandy Dunlap * tick_oneshot_mode_active - check whether the system is in oneshot mode
103cd6d95d8SThomas Gleixner *
104cd6d95d8SThomas Gleixner * returns 1 when either nohz or highres are enabled. otherwise 0.
105cd6d95d8SThomas Gleixner */
tick_oneshot_mode_active(void)106cd6d95d8SThomas Gleixner int tick_oneshot_mode_active(void)
107cd6d95d8SThomas Gleixner {
108cd6d95d8SThomas Gleixner unsigned long flags;
109cd6d95d8SThomas Gleixner int ret;
110cd6d95d8SThomas Gleixner
111cd6d95d8SThomas Gleixner local_irq_save(flags);
112909ea964SChristoph Lameter ret = __this_cpu_read(tick_cpu_device.mode) == TICKDEV_MODE_ONESHOT;
113cd6d95d8SThomas Gleixner local_irq_restore(flags);
114cd6d95d8SThomas Gleixner
115cd6d95d8SThomas Gleixner return ret;
116cd6d95d8SThomas Gleixner }
117cd6d95d8SThomas Gleixner
11879bf2bb3SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
11979bf2bb3SThomas Gleixner /**
12079bf2bb3SThomas Gleixner * tick_init_highres - switch to high resolution mode
12179bf2bb3SThomas Gleixner *
12279bf2bb3SThomas Gleixner * Called with interrupts disabled.
12379bf2bb3SThomas Gleixner */
tick_init_highres(void)12479bf2bb3SThomas Gleixner int tick_init_highres(void)
12579bf2bb3SThomas Gleixner {
12679bf2bb3SThomas Gleixner return tick_switch_to_oneshot(hrtimer_interrupt);
12779bf2bb3SThomas Gleixner }
12879bf2bb3SThomas Gleixner #endif
129