^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Alarmtimer interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This interface provides a timer which is similarto hrtimers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * but triggers a RTC alarm if the box is suspend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This interface is influenced by the Android RTC Alarm timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (C) 2010 IBM Corperation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Author: John Stultz <john.stultz@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/timerqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/alarmtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/posix-timers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/time_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "posix-timers.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <trace/events/alarmtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * struct alarm_base - Alarm timer bases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @lock: Lock for syncrhonized access to the base
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * @timerqueue: Timerqueue head managing the list of events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @get_ktime: Function to read the time correlating to the base
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @get_timespec: Function to read the namespace time correlating to the base
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @base_clockid: clockid for the base
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static struct alarm_base {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct timerqueue_head timerqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ktime_t (*get_ktime)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void (*get_timespec)(struct timespec64 *tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) clockid_t base_clockid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) } alarm_bases[ALARM_NUMTYPE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* freezer information to handle clock_nanosleep triggered wakeups */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static enum alarmtimer_type freezer_alarmtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static ktime_t freezer_expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static ktime_t freezer_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static DEFINE_SPINLOCK(freezer_delta_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #ifdef CONFIG_RTC_CLASS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* rtc timer and device for setting alarm wakeups at suspend */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static struct rtc_timer rtctimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static struct rtc_device *rtcdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static DEFINE_SPINLOCK(rtcdev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * alarmtimer_get_rtcdev - Return selected rtcdevice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * This function returns the rtc device to use for wakealarms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct rtc_device *alarmtimer_get_rtcdev(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct rtc_device *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) spin_lock_irqsave(&rtcdev_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ret = rtcdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) spin_unlock_irqrestore(&rtcdev_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int alarmtimer_rtc_add_device(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct class_interface *class_intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct rtc_device *rtc = to_rtc_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (rtcdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (!rtc->ops->set_alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!device_may_wakeup(rtc->dev.parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) pdev = platform_device_register_data(dev, "alarmtimer",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) PLATFORM_DEVID_AUTO, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!IS_ERR(pdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) device_init_wakeup(&pdev->dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) spin_lock_irqsave(&rtcdev_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (!IS_ERR(pdev) && !rtcdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (!try_module_get(rtc->owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) rtcdev = rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* hold a reference so it doesn't go away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) get_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) pdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) spin_unlock_irqrestore(&rtcdev_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) platform_device_unregister(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static inline void alarmtimer_rtc_timer_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) rtc_timer_init(&rtctimer, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static struct class_interface alarmtimer_rtc_interface = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .add_dev = &alarmtimer_rtc_add_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int alarmtimer_rtc_interface_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) alarmtimer_rtc_interface.class = rtc_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return class_interface_register(&alarmtimer_rtc_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static void alarmtimer_rtc_interface_remove(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) class_interface_unregister(&alarmtimer_rtc_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static inline void alarmtimer_rtc_interface_remove(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static inline void alarmtimer_rtc_timer_init(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * @base: pointer to the base where the timer is being run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * @alarm: pointer to alarm being enqueued.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * Adds alarm to a alarm_base timerqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * Must hold base->lock when calling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) timerqueue_del(&base->timerqueue, &alarm->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) timerqueue_add(&base->timerqueue, &alarm->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) alarm->state |= ALARMTIMER_STATE_ENQUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * @base: pointer to the base where the timer is running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * @alarm: pointer to alarm being removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * Removes alarm to a alarm_base timerqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * Must hold base->lock when calling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) timerqueue_del(&base->timerqueue, &alarm->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * alarmtimer_fired - Handles alarm hrtimer being fired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * @timer: pointer to hrtimer being run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * When a alarm timer fires, this runs through the timerqueue to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * see which alarms expired, and runs those. If there are more alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * timers queued for the future, we set the hrtimer to fire when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * the next future alarm timer expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct alarm *alarm = container_of(timer, struct alarm, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct alarm_base *base = &alarm_bases[alarm->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int ret = HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int restart = ALARMTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) spin_lock_irqsave(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) alarmtimer_dequeue(base, alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) spin_unlock_irqrestore(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (alarm->function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) restart = alarm->function(alarm, base->get_ktime());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) spin_lock_irqsave(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (restart != ALARMTIMER_NORESTART) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) hrtimer_set_expires(&alarm->timer, alarm->node.expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) alarmtimer_enqueue(base, alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ret = HRTIMER_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) spin_unlock_irqrestore(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) trace_alarmtimer_fired(alarm, base->get_ktime());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) ktime_t alarm_expires_remaining(const struct alarm *alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct alarm_base *base = &alarm_bases[alarm->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return ktime_sub(alarm->node.expires, base->get_ktime());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) EXPORT_SYMBOL_GPL(alarm_expires_remaining);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) #ifdef CONFIG_RTC_CLASS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * alarmtimer_suspend - Suspend time callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * @dev: unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * When we are going into suspend, we look through the bases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * to see which is the soonest timer to expire. We then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * set an rtc timer to fire that far into the future, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * will wake us from suspend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static int alarmtimer_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) ktime_t min, now, expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) int i, ret, type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) spin_lock_irqsave(&freezer_delta_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) min = freezer_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) expires = freezer_expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) type = freezer_alarmtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) freezer_delta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) spin_unlock_irqrestore(&freezer_delta_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) rtc = alarmtimer_get_rtcdev();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /* If we have no rtcdev, just return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (!rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /* Find the soonest timer to expire*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) for (i = 0; i < ALARM_NUMTYPE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct alarm_base *base = &alarm_bases[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct timerqueue_node *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) ktime_t delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) spin_lock_irqsave(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) next = timerqueue_getnext(&base->timerqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) spin_unlock_irqrestore(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (!next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) delta = ktime_sub(next->expires, base->get_ktime());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (!min || (delta < min)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) expires = next->expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) min = delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) type = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (min == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) pm_wakeup_event(dev, 2 * MSEC_PER_SEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) trace_alarmtimer_suspend(expires, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /* Setup an rtc timer to fire that far in the future */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) rtc_timer_cancel(rtc, &rtctimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) now = rtc_tm_to_ktime(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) now = ktime_add(now, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) /* Set alarm, if in the past reject suspend briefly to handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) ret = rtc_timer_start(rtc, &rtctimer, now, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) pm_wakeup_event(dev, MSEC_PER_SEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static int alarmtimer_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) rtc = alarmtimer_get_rtcdev();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) rtc_timer_cancel(rtc, &rtctimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static int alarmtimer_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static int alarmtimer_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) __alarm_init(struct alarm *alarm, enum alarmtimer_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) timerqueue_init(&alarm->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) alarm->timer.function = alarmtimer_fired;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) alarm->function = function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) alarm->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) alarm->state = ALARMTIMER_STATE_INACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * alarm_init - Initialize an alarm structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * @alarm: ptr to alarm to be initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * @type: the type of the alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * @function: callback that is run when the alarm fires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) HRTIMER_MODE_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) __alarm_init(alarm, type, function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) EXPORT_SYMBOL_GPL(alarm_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * alarm_start - Sets an absolute alarm to fire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * @alarm: ptr to alarm to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * @start: time to run the alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) void alarm_start(struct alarm *alarm, ktime_t start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) struct alarm_base *base = &alarm_bases[alarm->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) spin_lock_irqsave(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) alarm->node.expires = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) alarmtimer_enqueue(base, alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) spin_unlock_irqrestore(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) trace_alarmtimer_start(alarm, base->get_ktime());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) EXPORT_SYMBOL_GPL(alarm_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * alarm_start_relative - Sets a relative alarm to fire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * @alarm: ptr to alarm to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * @start: time relative to now to run the alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) void alarm_start_relative(struct alarm *alarm, ktime_t start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct alarm_base *base = &alarm_bases[alarm->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) start = ktime_add_safe(start, base->get_ktime());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) alarm_start(alarm, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) EXPORT_SYMBOL_GPL(alarm_start_relative);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) void alarm_restart(struct alarm *alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) struct alarm_base *base = &alarm_bases[alarm->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) spin_lock_irqsave(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) hrtimer_set_expires(&alarm->timer, alarm->node.expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) hrtimer_restart(&alarm->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) alarmtimer_enqueue(base, alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) spin_unlock_irqrestore(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) EXPORT_SYMBOL_GPL(alarm_restart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * alarm_try_to_cancel - Tries to cancel an alarm timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * @alarm: ptr to alarm to be canceled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * Returns 1 if the timer was canceled, 0 if it was not running,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * and -1 if the callback was running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) int alarm_try_to_cancel(struct alarm *alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct alarm_base *base = &alarm_bases[alarm->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) spin_lock_irqsave(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ret = hrtimer_try_to_cancel(&alarm->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) alarmtimer_dequeue(base, alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) spin_unlock_irqrestore(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) trace_alarmtimer_cancel(alarm, base->get_ktime());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * alarm_cancel - Spins trying to cancel an alarm timer until it is done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * @alarm: ptr to alarm to be canceled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * Returns 1 if the timer was canceled, 0 if it was not active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) int alarm_cancel(struct alarm *alarm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) int ret = alarm_try_to_cancel(alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) hrtimer_cancel_wait_running(&alarm->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) EXPORT_SYMBOL_GPL(alarm_cancel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) u64 overrun = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) ktime_t delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) delta = ktime_sub(now, alarm->node.expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (delta < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) if (unlikely(delta >= interval)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) s64 incr = ktime_to_ns(interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) overrun = ktime_divns(delta, incr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) alarm->node.expires = ktime_add_ns(alarm->node.expires,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) incr*overrun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (alarm->node.expires > now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) return overrun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * This (and the ktime_add() below) is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * correction for exact:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) overrun++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return overrun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) EXPORT_SYMBOL_GPL(alarm_forward);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) struct alarm_base *base = &alarm_bases[alarm->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return alarm_forward(alarm, base->get_ktime(), interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) EXPORT_SYMBOL_GPL(alarm_forward_now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) #ifdef CONFIG_POSIX_TIMERS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct alarm_base *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) ktime_t delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) switch(type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) case ALARM_REALTIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) base = &alarm_bases[ALARM_REALTIME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) type = ALARM_REALTIME_FREEZER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) case ALARM_BOOTTIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) base = &alarm_bases[ALARM_BOOTTIME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) type = ALARM_BOOTTIME_FREEZER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) WARN_ONCE(1, "Invalid alarm type: %d\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) delta = ktime_sub(absexp, base->get_ktime());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) spin_lock_irqsave(&freezer_delta_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (!freezer_delta || (delta < freezer_delta)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) freezer_delta = delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) freezer_expires = absexp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) freezer_alarmtype = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) spin_unlock_irqrestore(&freezer_delta_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * clock2alarm - helper that converts from clockid to alarmtypes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) * @clockid: clockid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) static enum alarmtimer_type clock2alarm(clockid_t clockid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (clockid == CLOCK_REALTIME_ALARM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return ALARM_REALTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (clockid == CLOCK_BOOTTIME_ALARM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return ALARM_BOOTTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * alarm_handle_timer - Callback for posix timers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * @alarm: alarm that fired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * Posix timer callback for expired alarm timers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) ktime_t now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) struct k_itimer *ptr = container_of(alarm, struct k_itimer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) it.alarm.alarmtimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) enum alarmtimer_restart result = ALARMTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) int si_private = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) spin_lock_irqsave(&ptr->it_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) ptr->it_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (ptr->it_interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) si_private = ++ptr->it_requeue_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) if (posix_timer_event(ptr, si_private) && ptr->it_interval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * Handle ignored signals and rearm the timer. This will go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * away once we handle ignored signals proper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) ptr->it_overrun += alarm_forward_now(alarm, ptr->it_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) ++ptr->it_requeue_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) ptr->it_active = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) result = ALARMTIMER_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) spin_unlock_irqrestore(&ptr->it_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * alarm_timer_rearm - Posix timer callback for rearming timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * @timr: Pointer to the posixtimer data struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) static void alarm_timer_rearm(struct k_itimer *timr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) struct alarm *alarm = &timr->it.alarm.alarmtimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) alarm_start(alarm, alarm->node.expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * alarm_timer_forward - Posix timer callback for forwarding timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * @timr: Pointer to the posixtimer data struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * @now: Current time to forward the timer against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) static s64 alarm_timer_forward(struct k_itimer *timr, ktime_t now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) struct alarm *alarm = &timr->it.alarm.alarmtimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) return alarm_forward(alarm, timr->it_interval, now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * alarm_timer_remaining - Posix timer callback to retrieve remaining time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * @timr: Pointer to the posixtimer data struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * @now: Current time to calculate against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) struct alarm *alarm = &timr->it.alarm.alarmtimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return ktime_sub(alarm->node.expires, now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) * @timr: Pointer to the posixtimer data struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) static int alarm_timer_try_to_cancel(struct k_itimer *timr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) * alarm_timer_wait_running - Posix timer callback to wait for a timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * @timr: Pointer to the posixtimer data struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) * Called from the core code when timer cancel detected that the callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * is running. @timr is unlocked and rcu read lock is held to prevent it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * from being freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) static void alarm_timer_wait_running(struct k_itimer *timr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) hrtimer_cancel_wait_running(&timr->it.alarm.alarmtimer.timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * alarm_timer_arm - Posix timer callback to arm a timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * @timr: Pointer to the posixtimer data struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * @expires: The new expiry time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * @absolute: Expiry value is absolute time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * @sigev_none: Posix timer does not deliver signals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) bool absolute, bool sigev_none)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) struct alarm *alarm = &timr->it.alarm.alarmtimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) struct alarm_base *base = &alarm_bases[alarm->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) if (!absolute)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) expires = ktime_add_safe(expires, base->get_ktime());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (sigev_none)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) alarm->node.expires = expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) alarm_start(&timr->it.alarm.alarmtimer, expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) * alarm_clock_getres - posix getres interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * @which_clock: clockid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * @tp: timespec to fill
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * Returns the granularity of underlying alarm base clock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (!alarmtimer_get_rtcdev())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) tp->tv_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) tp->tv_nsec = hrtimer_resolution;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) * alarm_clock_get_timespec - posix clock_get_timespec interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * @which_clock: clockid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * @tp: timespec to fill.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) * Provides the underlying alarm base time in a tasks time namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (!alarmtimer_get_rtcdev())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) base->get_timespec(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) * alarm_clock_get_ktime - posix clock_get_ktime interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * @which_clock: clockid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) * Provides the underlying alarm base time in the root namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) static ktime_t alarm_clock_get_ktime(clockid_t which_clock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) if (!alarmtimer_get_rtcdev())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) return base->get_ktime();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) * alarm_timer_create - posix timer_create interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * @new_timer: k_itimer pointer to manage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) * Initializes the k_itimer structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) static int alarm_timer_create(struct k_itimer *new_timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) enum alarmtimer_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (!alarmtimer_get_rtcdev())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (!capable(CAP_WAKE_ALARM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) type = clock2alarm(new_timer->it_clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * @alarm: ptr to alarm that fired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * Wakes up the task that set the alarmtimer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) ktime_t now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) struct task_struct *task = (struct task_struct *)alarm->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) alarm->data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) if (task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) wake_up_process(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) return ALARMTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) * @alarm: ptr to alarmtimer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) * @absexp: absolute expiration time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * Sets the alarm timer and sleeps until it is fired or interrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) enum alarmtimer_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct restart_block *restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) alarm->data = (void *)current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) alarm_start(alarm, absexp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (likely(alarm->data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) alarm_cancel(alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) } while (alarm->data && !signal_pending(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) __set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) destroy_hrtimer_on_stack(&alarm->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) if (!alarm->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (freezing(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) alarmtimer_freezerset(absexp, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) restart = ¤t->restart_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) if (restart->nanosleep.type != TT_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) struct timespec64 rmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) ktime_t rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) rem = ktime_sub(absexp, alarm_bases[type].get_ktime());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) if (rem <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) rmt = ktime_to_timespec64(rem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) return nanosleep_copyout(restart, &rmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) return -ERESTART_RESTARTBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) hrtimer_init_on_stack(&alarm->timer, alarm_bases[type].base_clockid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) HRTIMER_MODE_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) __alarm_init(alarm, type, function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) * @restart: ptr to restart block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) * Handles restarted clock_nanosleep calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) enum alarmtimer_type type = restart->nanosleep.clockid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) ktime_t exp = restart->nanosleep.expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) struct alarm alarm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) return alarmtimer_do_nsleep(&alarm, exp, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) * alarm_timer_nsleep - alarmtimer nanosleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) * @which_clock: clockid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) * @flags: determins abstime or relative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) * @tsreq: requested sleep time (abs or rel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * @rmtp: remaining sleep time saved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) * Handles clock_nanosleep calls against _ALARM clockids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) const struct timespec64 *tsreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) enum alarmtimer_type type = clock2alarm(which_clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) struct restart_block *restart = ¤t->restart_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) struct alarm alarm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) ktime_t exp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) if (!alarmtimer_get_rtcdev())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) if (flags & ~TIMER_ABSTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) if (!capable(CAP_WAKE_ALARM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) exp = timespec64_to_ktime(*tsreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) /* Convert (if necessary) to absolute time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) if (flags != TIMER_ABSTIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) ktime_t now = alarm_bases[type].get_ktime();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) exp = ktime_add_safe(now, exp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) exp = timens_ktime_to_host(which_clock, exp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) ret = alarmtimer_do_nsleep(&alarm, exp, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) if (ret != -ERESTART_RESTARTBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) /* abs timers don't set remaining time or restart */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (flags == TIMER_ABSTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) return -ERESTARTNOHAND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) restart->nanosleep.clockid = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) restart->nanosleep.expires = exp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) set_restart_fn(restart, alarm_timer_nsleep_restart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) const struct k_clock alarm_clock = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) .clock_getres = alarm_clock_getres,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) .clock_get_ktime = alarm_clock_get_ktime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) .clock_get_timespec = alarm_clock_get_timespec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) .timer_create = alarm_timer_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) .timer_set = common_timer_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) .timer_del = common_timer_del,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) .timer_get = common_timer_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) .timer_arm = alarm_timer_arm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) .timer_rearm = alarm_timer_rearm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) .timer_forward = alarm_timer_forward,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) .timer_remaining = alarm_timer_remaining,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) .timer_try_to_cancel = alarm_timer_try_to_cancel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) .timer_wait_running = alarm_timer_wait_running,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) .nsleep = alarm_timer_nsleep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) #endif /* CONFIG_POSIX_TIMERS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) /* Suspend hook structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) static const struct dev_pm_ops alarmtimer_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) .suspend = alarmtimer_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) .resume = alarmtimer_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) static struct platform_driver alarmtimer_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) .name = "alarmtimer",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) .pm = &alarmtimer_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) static void get_boottime_timespec(struct timespec64 *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) ktime_get_boottime_ts64(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) timens_add_boottime(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) * alarmtimer_init - Initialize alarm timer code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) * This function initializes the alarm bases and registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) * the posix clock ids.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) static int __init alarmtimer_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) alarmtimer_rtc_timer_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) /* Initialize alarm bases */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) alarm_bases[ALARM_REALTIME].get_ktime = &ktime_get_real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) alarm_bases[ALARM_REALTIME].get_timespec = ktime_get_real_ts64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) alarm_bases[ALARM_BOOTTIME].get_ktime = &ktime_get_boottime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) alarm_bases[ALARM_BOOTTIME].get_timespec = get_boottime_timespec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) for (i = 0; i < ALARM_NUMTYPE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) timerqueue_init_head(&alarm_bases[i].timerqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) spin_lock_init(&alarm_bases[i].lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) error = alarmtimer_rtc_interface_setup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) error = platform_driver_register(&alarmtimer_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) goto out_if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) out_if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) alarmtimer_rtc_interface_remove();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) device_initcall(alarmtimer_init);