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