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