blob: a0ee81f49a87e2e9cfdeb373df8f1ee67d0dc889 [file] [log] [blame]
John Stultzff3ead92011-01-11 09:42:13 -08001/*
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>
22#include <linux/alarmtimer.h>
23#include <linux/mutex.h>
24#include <linux/platform_device.h>
25#include <linux/posix-timers.h>
26#include <linux/workqueue.h>
27#include <linux/freezer.h>
28
John Stultz180bf812011-04-28 12:58:11 -070029/**
30 * struct alarm_base - Alarm timer bases
31 * @lock: Lock for syncrhonized access to the base
32 * @timerqueue: Timerqueue head managing the list of events
John Stultz180bf812011-04-28 12:58:11 -070033 * @gettime: Function to read the time correlating to the base
34 * @base_clockid: clockid for the base
John Stultz180bf812011-04-28 12:58:11 -070035 */
John Stultzff3ead92011-01-11 09:42:13 -080036static struct alarm_base {
37 spinlock_t lock;
38 struct timerqueue_head timerqueue;
John Stultzff3ead92011-01-11 09:42:13 -080039 ktime_t (*gettime)(void);
40 clockid_t base_clockid;
John Stultzff3ead92011-01-11 09:42:13 -080041} alarm_bases[ALARM_NUMTYPE];
42
John Stultzc008ba582011-06-16 18:27:09 -070043/* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
44static ktime_t freezer_delta;
45static DEFINE_SPINLOCK(freezer_delta_lock);
46
Todd Poynor59a93c22012-08-09 00:37:27 -070047static struct wakeup_source *ws;
48
John Stultz472647d2011-04-29 15:03:10 -070049#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -070050/* rtc timer and device for setting alarm wakeups at suspend */
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +010051static struct rtc_timer rtctimer;
John Stultzff3ead92011-01-11 09:42:13 -080052static struct rtc_device *rtcdev;
John Stultzc008ba582011-06-16 18:27:09 -070053static DEFINE_SPINLOCK(rtcdev_lock);
John Stultzff3ead92011-01-11 09:42:13 -080054
John Stultzc008ba582011-06-16 18:27:09 -070055/**
John Stultzc008ba582011-06-16 18:27:09 -070056 * alarmtimer_get_rtcdev - Return selected rtcdevice
57 *
58 * This function returns the rtc device to use for wakealarms.
59 * If one has not already been chosen, it checks to see if a
60 * functional rtc device is available.
61 */
John Stultz57c498f2012-04-20 12:31:45 -070062struct rtc_device *alarmtimer_get_rtcdev(void)
John Stultzc008ba582011-06-16 18:27:09 -070063{
John Stultzc008ba582011-06-16 18:27:09 -070064 unsigned long flags;
65 struct rtc_device *ret;
66
67 spin_lock_irqsave(&rtcdev_lock, flags);
John Stultzc008ba582011-06-16 18:27:09 -070068 ret = rtcdev;
69 spin_unlock_irqrestore(&rtcdev_lock, flags);
70
71 return ret;
72}
Pramod Gurav71d5d2b2014-06-13 11:49:42 +053073EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
John Stultz8bc0daf2011-07-14 18:35:13 -070074
75static int alarmtimer_rtc_add_device(struct device *dev,
76 struct class_interface *class_intf)
77{
78 unsigned long flags;
79 struct rtc_device *rtc = to_rtc_device(dev);
80
81 if (rtcdev)
82 return -EBUSY;
83
84 if (!rtc->ops->set_alarm)
85 return -1;
86 if (!device_may_wakeup(rtc->dev.parent))
87 return -1;
88
89 spin_lock_irqsave(&rtcdev_lock, flags);
90 if (!rtcdev) {
91 rtcdev = rtc;
92 /* hold a reference so it doesn't go away */
93 get_device(dev);
94 }
95 spin_unlock_irqrestore(&rtcdev_lock, flags);
96 return 0;
97}
98
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +010099static inline void alarmtimer_rtc_timer_init(void)
100{
101 rtc_timer_init(&rtctimer, NULL, NULL);
102}
103
John Stultz8bc0daf2011-07-14 18:35:13 -0700104static struct class_interface alarmtimer_rtc_interface = {
105 .add_dev = &alarmtimer_rtc_add_device,
106};
107
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200108static int alarmtimer_rtc_interface_setup(void)
John Stultz8bc0daf2011-07-14 18:35:13 -0700109{
110 alarmtimer_rtc_interface.class = rtc_class;
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200111 return class_interface_register(&alarmtimer_rtc_interface);
112}
113static void alarmtimer_rtc_interface_remove(void)
114{
115 class_interface_unregister(&alarmtimer_rtc_interface);
John Stultz8bc0daf2011-07-14 18:35:13 -0700116}
John Stultz1c6b39a2011-06-16 18:47:37 -0700117#else
John Stultz57c498f2012-04-20 12:31:45 -0700118struct rtc_device *alarmtimer_get_rtcdev(void)
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200119{
120 return NULL;
121}
122#define rtcdev (NULL)
123static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
124static inline void alarmtimer_rtc_interface_remove(void) { }
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100125static inline void alarmtimer_rtc_timer_init(void) { }
John Stultzc008ba582011-06-16 18:27:09 -0700126#endif
John Stultzff3ead92011-01-11 09:42:13 -0800127
John Stultz180bf812011-04-28 12:58:11 -0700128/**
John Stultzff3ead92011-01-11 09:42:13 -0800129 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
130 * @base: pointer to the base where the timer is being run
131 * @alarm: pointer to alarm being enqueued.
132 *
John Stultzdae373b2012-09-13 19:12:16 -0400133 * Adds alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800134 *
135 * Must hold base->lock when calling.
136 */
137static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
138{
John Stultzdae373b2012-09-13 19:12:16 -0400139 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
140 timerqueue_del(&base->timerqueue, &alarm->node);
141
John Stultzff3ead92011-01-11 09:42:13 -0800142 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700143 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800144}
145
John Stultz180bf812011-04-28 12:58:11 -0700146/**
John Stultza65bcc12012-09-13 19:25:22 -0400147 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800148 * @base: pointer to the base where the timer is running
149 * @alarm: pointer to alarm being removed
150 *
John Stultzdae373b2012-09-13 19:12:16 -0400151 * Removes alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800152 *
153 * Must hold base->lock when calling.
154 */
John Stultza65bcc12012-09-13 19:25:22 -0400155static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800156{
John Stultza28cde82011-08-10 12:30:21 -0700157 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
158 return;
159
John Stultzff3ead92011-01-11 09:42:13 -0800160 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700161 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800162}
163
John Stultz7068b7a2011-04-28 13:29:18 -0700164
John Stultz180bf812011-04-28 12:58:11 -0700165/**
John Stultz7068b7a2011-04-28 13:29:18 -0700166 * alarmtimer_fired - Handles alarm hrtimer being fired.
167 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800168 *
John Stultz180bf812011-04-28 12:58:11 -0700169 * When a alarm timer fires, this runs through the timerqueue to
170 * see which alarms expired, and runs those. If there are more alarm
171 * timers queued for the future, we set the hrtimer to fire when
172 * when the next future alarm timer expires.
John Stultzff3ead92011-01-11 09:42:13 -0800173 */
John Stultz7068b7a2011-04-28 13:29:18 -0700174static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800175{
John Stultzdae373b2012-09-13 19:12:16 -0400176 struct alarm *alarm = container_of(timer, struct alarm, timer);
177 struct alarm_base *base = &alarm_bases[alarm->type];
John Stultzff3ead92011-01-11 09:42:13 -0800178 unsigned long flags;
John Stultz7068b7a2011-04-28 13:29:18 -0700179 int ret = HRTIMER_NORESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700180 int restart = ALARMTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800181
182 spin_lock_irqsave(&base->lock, flags);
John Stultza65bcc12012-09-13 19:25:22 -0400183 alarmtimer_dequeue(base, alarm);
John Stultzdae373b2012-09-13 19:12:16 -0400184 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800185
John Stultzdae373b2012-09-13 19:12:16 -0400186 if (alarm->function)
187 restart = alarm->function(alarm, base->gettime());
John Stultzff3ead92011-01-11 09:42:13 -0800188
John Stultzdae373b2012-09-13 19:12:16 -0400189 spin_lock_irqsave(&base->lock, flags);
190 if (restart != ALARMTIMER_NORESTART) {
191 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
192 alarmtimer_enqueue(base, alarm);
John Stultz7068b7a2011-04-28 13:29:18 -0700193 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800194 }
195 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800196
John Stultz7068b7a2011-04-28 13:29:18 -0700197 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800198
John Stultzff3ead92011-01-11 09:42:13 -0800199}
200
Todd Poynor6cffe002013-05-15 14:38:11 -0700201ktime_t alarm_expires_remaining(const struct alarm *alarm)
202{
203 struct alarm_base *base = &alarm_bases[alarm->type];
204 return ktime_sub(alarm->node.expires, base->gettime());
205}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200206EXPORT_SYMBOL_GPL(alarm_expires_remaining);
Todd Poynor6cffe002013-05-15 14:38:11 -0700207
John Stultz472647d2011-04-29 15:03:10 -0700208#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700209/**
John Stultzff3ead92011-01-11 09:42:13 -0800210 * alarmtimer_suspend - Suspend time callback
211 * @dev: unused
212 * @state: unused
213 *
214 * When we are going into suspend, we look through the bases
215 * to see which is the soonest timer to expire. We then
216 * set an rtc timer to fire that far into the future, which
217 * will wake us from suspend.
218 */
219static int alarmtimer_suspend(struct device *dev)
220{
221 struct rtc_time tm;
222 ktime_t min, now;
223 unsigned long flags;
John Stultzc008ba582011-06-16 18:27:09 -0700224 struct rtc_device *rtc;
John Stultzff3ead92011-01-11 09:42:13 -0800225 int i;
Todd Poynor59a93c22012-08-09 00:37:27 -0700226 int ret;
John Stultzff3ead92011-01-11 09:42:13 -0800227
228 spin_lock_irqsave(&freezer_delta_lock, flags);
229 min = freezer_delta;
230 freezer_delta = ktime_set(0, 0);
231 spin_unlock_irqrestore(&freezer_delta_lock, flags);
232
John Stultz8bc0daf2011-07-14 18:35:13 -0700233 rtc = alarmtimer_get_rtcdev();
John Stultzff3ead92011-01-11 09:42:13 -0800234 /* If we have no rtcdev, just return */
John Stultzc008ba582011-06-16 18:27:09 -0700235 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800236 return 0;
237
238 /* Find the soonest timer to expire*/
239 for (i = 0; i < ALARM_NUMTYPE; i++) {
240 struct alarm_base *base = &alarm_bases[i];
241 struct timerqueue_node *next;
242 ktime_t delta;
243
244 spin_lock_irqsave(&base->lock, flags);
245 next = timerqueue_getnext(&base->timerqueue);
246 spin_unlock_irqrestore(&base->lock, flags);
247 if (!next)
248 continue;
249 delta = ktime_sub(next->expires, base->gettime());
250 if (!min.tv64 || (delta.tv64 < min.tv64))
251 min = delta;
252 }
253 if (min.tv64 == 0)
254 return 0;
255
Todd Poynor59a93c22012-08-09 00:37:27 -0700256 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
257 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
258 return -EBUSY;
259 }
John Stultzff3ead92011-01-11 09:42:13 -0800260
261 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba582011-06-16 18:27:09 -0700262 rtc_timer_cancel(rtc, &rtctimer);
263 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800264 now = rtc_tm_to_ktime(tm);
265 now = ktime_add(now, min);
266
Todd Poynor59a93c22012-08-09 00:37:27 -0700267 /* Set alarm, if in the past reject suspend briefly to handle */
268 ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
269 if (ret < 0)
270 __pm_wakeup_event(ws, MSEC_PER_SEC);
271 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800272}
zhuo-haoa0e32132015-11-17 20:08:07 +0800273
274static int alarmtimer_resume(struct device *dev)
275{
276 struct rtc_device *rtc;
277
278 rtc = alarmtimer_get_rtcdev();
279 if (rtc)
280 rtc_timer_cancel(rtc, &rtctimer);
281 return 0;
282}
283
John Stultz472647d2011-04-29 15:03:10 -0700284#else
285static int alarmtimer_suspend(struct device *dev)
286{
287 return 0;
288}
zhuo-haoa0e32132015-11-17 20:08:07 +0800289
290static int alarmtimer_resume(struct device *dev)
291{
292 return 0;
293}
John Stultz472647d2011-04-29 15:03:10 -0700294#endif
John Stultzff3ead92011-01-11 09:42:13 -0800295
John Stultz9a7adcf2011-01-11 09:54:33 -0800296static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
297{
298 ktime_t delta;
299 unsigned long flags;
300 struct alarm_base *base = &alarm_bases[type];
301
302 delta = ktime_sub(absexp, base->gettime());
303
304 spin_lock_irqsave(&freezer_delta_lock, flags);
305 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
306 freezer_delta = delta;
307 spin_unlock_irqrestore(&freezer_delta_lock, flags);
308}
309
310
John Stultz180bf812011-04-28 12:58:11 -0700311/**
John Stultzff3ead92011-01-11 09:42:13 -0800312 * alarm_init - Initialize an alarm structure
313 * @alarm: ptr to alarm to be initialized
314 * @type: the type of the alarm
315 * @function: callback that is run when the alarm fires
John Stultzff3ead92011-01-11 09:42:13 -0800316 */
317void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
John Stultz4b413082011-08-10 10:37:59 -0700318 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
John Stultzff3ead92011-01-11 09:42:13 -0800319{
320 timerqueue_init(&alarm->node);
John Stultzdae373b2012-09-13 19:12:16 -0400321 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
322 HRTIMER_MODE_ABS);
323 alarm->timer.function = alarmtimer_fired;
John Stultzff3ead92011-01-11 09:42:13 -0800324 alarm->function = function;
325 alarm->type = type;
John Stultza28cde82011-08-10 12:30:21 -0700326 alarm->state = ALARMTIMER_STATE_INACTIVE;
John Stultzff3ead92011-01-11 09:42:13 -0800327}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200328EXPORT_SYMBOL_GPL(alarm_init);
John Stultzff3ead92011-01-11 09:42:13 -0800329
John Stultz180bf812011-04-28 12:58:11 -0700330/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700331 * alarm_start - Sets an absolute alarm to fire
John Stultzff3ead92011-01-11 09:42:13 -0800332 * @alarm: ptr to alarm to set
333 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800334 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000335void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800336{
337 struct alarm_base *base = &alarm_bases[alarm->type];
338 unsigned long flags;
339
340 spin_lock_irqsave(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800341 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800342 alarmtimer_enqueue(base, alarm);
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000343 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
John Stultzff3ead92011-01-11 09:42:13 -0800344 spin_unlock_irqrestore(&base->lock, flags);
345}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200346EXPORT_SYMBOL_GPL(alarm_start);
John Stultzff3ead92011-01-11 09:42:13 -0800347
John Stultz180bf812011-04-28 12:58:11 -0700348/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700349 * alarm_start_relative - Sets a relative alarm to fire
350 * @alarm: ptr to alarm to set
351 * @start: time relative to now to run the alarm
352 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000353void alarm_start_relative(struct alarm *alarm, ktime_t start)
Todd Poynor6cffe002013-05-15 14:38:11 -0700354{
355 struct alarm_base *base = &alarm_bases[alarm->type];
356
Thomas Gleixner8ee7f062017-05-30 23:15:34 +0200357 start = ktime_add_safe(start, base->gettime());
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000358 alarm_start(alarm, start);
Todd Poynor6cffe002013-05-15 14:38:11 -0700359}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200360EXPORT_SYMBOL_GPL(alarm_start_relative);
Todd Poynor6cffe002013-05-15 14:38:11 -0700361
362void alarm_restart(struct alarm *alarm)
363{
364 struct alarm_base *base = &alarm_bases[alarm->type];
365 unsigned long flags;
366
367 spin_lock_irqsave(&base->lock, flags);
368 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
369 hrtimer_restart(&alarm->timer);
370 alarmtimer_enqueue(base, alarm);
371 spin_unlock_irqrestore(&base->lock, flags);
372}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200373EXPORT_SYMBOL_GPL(alarm_restart);
Todd Poynor6cffe002013-05-15 14:38:11 -0700374
375/**
John Stultz9082c462011-08-10 12:41:36 -0700376 * alarm_try_to_cancel - Tries to cancel an alarm timer
John Stultzff3ead92011-01-11 09:42:13 -0800377 * @alarm: ptr to alarm to be canceled
John Stultz9082c462011-08-10 12:41:36 -0700378 *
379 * Returns 1 if the timer was canceled, 0 if it was not running,
380 * and -1 if the callback was running
John Stultzff3ead92011-01-11 09:42:13 -0800381 */
John Stultz9082c462011-08-10 12:41:36 -0700382int alarm_try_to_cancel(struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800383{
384 struct alarm_base *base = &alarm_bases[alarm->type];
385 unsigned long flags;
John Stultzdae373b2012-09-13 19:12:16 -0400386 int ret;
387
John Stultzff3ead92011-01-11 09:42:13 -0800388 spin_lock_irqsave(&base->lock, flags);
John Stultzdae373b2012-09-13 19:12:16 -0400389 ret = hrtimer_try_to_cancel(&alarm->timer);
390 if (ret >= 0)
John Stultza65bcc12012-09-13 19:25:22 -0400391 alarmtimer_dequeue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800392 spin_unlock_irqrestore(&base->lock, flags);
John Stultz9082c462011-08-10 12:41:36 -0700393 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800394}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200395EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
John Stultzff3ead92011-01-11 09:42:13 -0800396
397
John Stultz9082c462011-08-10 12:41:36 -0700398/**
399 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
400 * @alarm: ptr to alarm to be canceled
401 *
402 * Returns 1 if the timer was canceled, 0 if it was not active.
403 */
404int alarm_cancel(struct alarm *alarm)
405{
406 for (;;) {
407 int ret = alarm_try_to_cancel(alarm);
408 if (ret >= 0)
409 return ret;
410 cpu_relax();
411 }
412}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200413EXPORT_SYMBOL_GPL(alarm_cancel);
John Stultz9082c462011-08-10 12:41:36 -0700414
John Stultzdce75a82011-08-10 11:31:03 -0700415
416u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
417{
418 u64 overrun = 1;
419 ktime_t delta;
420
421 delta = ktime_sub(now, alarm->node.expires);
422
423 if (delta.tv64 < 0)
424 return 0;
425
426 if (unlikely(delta.tv64 >= interval.tv64)) {
427 s64 incr = ktime_to_ns(interval);
428
429 overrun = ktime_divns(delta, incr);
430
431 alarm->node.expires = ktime_add_ns(alarm->node.expires,
432 incr*overrun);
433
434 if (alarm->node.expires.tv64 > now.tv64)
435 return overrun;
436 /*
437 * This (and the ktime_add() below) is the
438 * correction for exact:
439 */
440 overrun++;
441 }
442
Thomas Gleixner8ee7f062017-05-30 23:15:34 +0200443 alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
John Stultzdce75a82011-08-10 11:31:03 -0700444 return overrun;
445}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200446EXPORT_SYMBOL_GPL(alarm_forward);
John Stultzdce75a82011-08-10 11:31:03 -0700447
Todd Poynor6cffe002013-05-15 14:38:11 -0700448u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
449{
450 struct alarm_base *base = &alarm_bases[alarm->type];
451
452 return alarm_forward(alarm, base->gettime(), interval);
453}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200454EXPORT_SYMBOL_GPL(alarm_forward_now);
John Stultzdce75a82011-08-10 11:31:03 -0700455
456
John Stultz180bf812011-04-28 12:58:11 -0700457/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800458 * clock2alarm - helper that converts from clockid to alarmtypes
459 * @clockid: clockid.
John Stultz9a7adcf2011-01-11 09:54:33 -0800460 */
461static enum alarmtimer_type clock2alarm(clockid_t clockid)
462{
463 if (clockid == CLOCK_REALTIME_ALARM)
464 return ALARM_REALTIME;
465 if (clockid == CLOCK_BOOTTIME_ALARM)
466 return ALARM_BOOTTIME;
467 return -1;
468}
469
John Stultz180bf812011-04-28 12:58:11 -0700470/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800471 * alarm_handle_timer - Callback for posix timers
472 * @alarm: alarm that fired
473 *
474 * Posix timer callback for expired alarm timers.
475 */
John Stultz4b413082011-08-10 10:37:59 -0700476static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
477 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800478{
Richard Larocque474e9412014-09-09 18:31:05 -0700479 unsigned long flags;
John Stultz9a7adcf2011-01-11 09:54:33 -0800480 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
John Stultz9e264762011-08-10 12:09:24 -0700481 it.alarm.alarmtimer);
Richard Larocque474e9412014-09-09 18:31:05 -0700482 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
483
484 spin_lock_irqsave(&ptr->it_lock, flags);
Richard Larocque265b81d2014-09-09 18:31:04 -0700485 if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) {
486 if (posix_timer_event(ptr, 0) != 0)
487 ptr->it_overrun++;
488 }
John Stultz4b413082011-08-10 10:37:59 -0700489
John Stultz54da23b2011-08-10 11:08:07 -0700490 /* Re-add periodic timers */
John Stultz9e264762011-08-10 12:09:24 -0700491 if (ptr->it.alarm.interval.tv64) {
492 ptr->it_overrun += alarm_forward(alarm, now,
493 ptr->it.alarm.interval);
Richard Larocque474e9412014-09-09 18:31:05 -0700494 result = ALARMTIMER_RESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700495 }
Richard Larocque474e9412014-09-09 18:31:05 -0700496 spin_unlock_irqrestore(&ptr->it_lock, flags);
497
498 return result;
John Stultz9a7adcf2011-01-11 09:54:33 -0800499}
500
John Stultz180bf812011-04-28 12:58:11 -0700501/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800502 * alarm_clock_getres - posix getres interface
503 * @which_clock: clockid
504 * @tp: timespec to fill
505 *
506 * Returns the granularity of underlying alarm base clock
507 */
508static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
509{
John Stultz1c6b39a2011-06-16 18:47:37 -0700510 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400511 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700512
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000513 tp->tv_sec = 0;
514 tp->tv_nsec = hrtimer_resolution;
515 return 0;
John Stultz9a7adcf2011-01-11 09:54:33 -0800516}
517
518/**
519 * alarm_clock_get - posix clock_get interface
520 * @which_clock: clockid
521 * @tp: timespec to fill.
522 *
523 * Provides the underlying alarm base time.
524 */
525static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
526{
527 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
528
John Stultz1c6b39a2011-06-16 18:47:37 -0700529 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400530 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700531
John Stultz9a7adcf2011-01-11 09:54:33 -0800532 *tp = ktime_to_timespec(base->gettime());
533 return 0;
534}
535
536/**
537 * alarm_timer_create - posix timer_create interface
538 * @new_timer: k_itimer pointer to manage
539 *
540 * Initializes the k_itimer structure.
541 */
542static int alarm_timer_create(struct k_itimer *new_timer)
543{
544 enum alarmtimer_type type;
John Stultz9a7adcf2011-01-11 09:54:33 -0800545
John Stultz1c6b39a2011-06-16 18:47:37 -0700546 if (!alarmtimer_get_rtcdev())
547 return -ENOTSUPP;
548
John Stultz9a7adcf2011-01-11 09:54:33 -0800549 if (!capable(CAP_WAKE_ALARM))
550 return -EPERM;
551
552 type = clock2alarm(new_timer->it_clock);
John Stultz9e264762011-08-10 12:09:24 -0700553 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf2011-01-11 09:54:33 -0800554 return 0;
555}
556
557/**
558 * alarm_timer_get - posix timer_get interface
559 * @new_timer: k_itimer pointer
560 * @cur_setting: itimerspec data to fill
561 *
Richard Larocquee86fea72014-09-09 18:31:03 -0700562 * Copies out the current itimerspec data
John Stultz9a7adcf2011-01-11 09:54:33 -0800563 */
564static void alarm_timer_get(struct k_itimer *timr,
565 struct itimerspec *cur_setting)
566{
Richard Larocquee86fea72014-09-09 18:31:03 -0700567 ktime_t relative_expiry_time =
568 alarm_expires_remaining(&(timr->it.alarm.alarmtimer));
John Stultzea7802f2011-08-04 07:51:56 -0700569
Richard Larocquee86fea72014-09-09 18:31:03 -0700570 if (ktime_to_ns(relative_expiry_time) > 0) {
571 cur_setting->it_value = ktime_to_timespec(relative_expiry_time);
572 } else {
573 cur_setting->it_value.tv_sec = 0;
574 cur_setting->it_value.tv_nsec = 0;
575 }
576
577 cur_setting->it_interval = ktime_to_timespec(timr->it.alarm.interval);
John Stultz9a7adcf2011-01-11 09:54:33 -0800578}
579
580/**
581 * alarm_timer_del - posix timer_del interface
582 * @timr: k_itimer pointer to be deleted
583 *
584 * Cancels any programmed alarms for the given timer.
585 */
586static int alarm_timer_del(struct k_itimer *timr)
587{
John Stultz1c6b39a2011-06-16 18:47:37 -0700588 if (!rtcdev)
589 return -ENOTSUPP;
590
John Stultz9082c462011-08-10 12:41:36 -0700591 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
592 return TIMER_RETRY;
593
John Stultz9a7adcf2011-01-11 09:54:33 -0800594 return 0;
595}
596
597/**
598 * alarm_timer_set - posix timer_set interface
599 * @timr: k_itimer pointer to be deleted
600 * @flags: timer flags
601 * @new_setting: itimerspec to be used
602 * @old_setting: itimerspec being replaced
603 *
604 * Sets the timer to new_setting, and starts the timer.
605 */
606static int alarm_timer_set(struct k_itimer *timr, int flags,
607 struct itimerspec *new_setting,
608 struct itimerspec *old_setting)
609{
John Stultz16927772014-07-07 14:06:11 -0700610 ktime_t exp;
611
John Stultz1c6b39a2011-06-16 18:47:37 -0700612 if (!rtcdev)
613 return -ENOTSUPP;
614
John Stultz16927772014-07-07 14:06:11 -0700615 if (flags & ~TIMER_ABSTIME)
616 return -EINVAL;
617
John Stultz971c90b2011-08-04 07:25:35 -0700618 if (old_setting)
619 alarm_timer_get(timr, old_setting);
John Stultz9a7adcf2011-01-11 09:54:33 -0800620
621 /* If the timer was already set, cancel it */
John Stultz9082c462011-08-10 12:41:36 -0700622 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
623 return TIMER_RETRY;
John Stultz9a7adcf2011-01-11 09:54:33 -0800624
625 /* start the timer */
John Stultz9e264762011-08-10 12:09:24 -0700626 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
Thomas Gleixner04651042017-05-30 23:15:35 +0200627
628 /*
629 * Rate limit to the tick as a hot fix to prevent DOS. Will be
630 * mopped up later.
631 */
Greg Hackmann91af5f02017-07-24 10:19:24 -0700632 if (timr->it.alarm.interval.tv64 &&
633 ktime_to_ns(timr->it.alarm.interval) < TICK_NSEC)
Thomas Gleixner04651042017-05-30 23:15:35 +0200634 timr->it.alarm.interval = ktime_set(0, TICK_NSEC);
635
John Stultz16927772014-07-07 14:06:11 -0700636 exp = timespec_to_ktime(new_setting->it_value);
637 /* Convert (if necessary) to absolute time */
638 if (flags != TIMER_ABSTIME) {
639 ktime_t now;
640
641 now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
Thomas Gleixner8ee7f062017-05-30 23:15:34 +0200642 exp = ktime_add_safe(now, exp);
John Stultz16927772014-07-07 14:06:11 -0700643 }
644
645 alarm_start(&timr->it.alarm.alarmtimer, exp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800646 return 0;
647}
648
649/**
650 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
651 * @alarm: ptr to alarm that fired
652 *
653 * Wakes up the task that set the alarmtimer
654 */
John Stultz4b413082011-08-10 10:37:59 -0700655static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
656 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800657{
658 struct task_struct *task = (struct task_struct *)alarm->data;
659
660 alarm->data = NULL;
661 if (task)
662 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700663 return ALARMTIMER_NORESTART;
John Stultz9a7adcf2011-01-11 09:54:33 -0800664}
665
666/**
667 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
668 * @alarm: ptr to alarmtimer
669 * @absexp: absolute expiration time
670 *
671 * Sets the alarm timer and sleeps until it is fired or interrupted.
672 */
673static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
674{
675 alarm->data = (void *)current;
676 do {
677 set_current_state(TASK_INTERRUPTIBLE);
John Stultz9e264762011-08-10 12:09:24 -0700678 alarm_start(alarm, absexp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800679 if (likely(alarm->data))
680 schedule();
681
682 alarm_cancel(alarm);
683 } while (alarm->data && !signal_pending(current));
684
685 __set_current_state(TASK_RUNNING);
686
687 return (alarm->data == NULL);
688}
689
690
691/**
692 * update_rmtp - Update remaining timespec value
693 * @exp: expiration time
694 * @type: timer type
695 * @rmtp: user pointer to remaining timepsec value
696 *
697 * Helper function that fills in rmtp value with time between
698 * now and the exp value
699 */
700static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
701 struct timespec __user *rmtp)
702{
703 struct timespec rmt;
704 ktime_t rem;
705
706 rem = ktime_sub(exp, alarm_bases[type].gettime());
707
708 if (rem.tv64 <= 0)
709 return 0;
710 rmt = ktime_to_timespec(rem);
711
712 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
713 return -EFAULT;
714
715 return 1;
716
717}
718
719/**
720 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
721 * @restart: ptr to restart block
722 *
723 * Handles restarted clock_nanosleep calls
724 */
725static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
726{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200727 enum alarmtimer_type type = restart->nanosleep.clockid;
John Stultz9a7adcf2011-01-11 09:54:33 -0800728 ktime_t exp;
729 struct timespec __user *rmtp;
730 struct alarm alarm;
731 int ret = 0;
732
733 exp.tv64 = restart->nanosleep.expires;
734 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
735
736 if (alarmtimer_do_nsleep(&alarm, exp))
737 goto out;
738
739 if (freezing(current))
740 alarmtimer_freezerset(exp, type);
741
742 rmtp = restart->nanosleep.rmtp;
743 if (rmtp) {
744 ret = update_rmtp(exp, type, rmtp);
745 if (ret <= 0)
746 goto out;
747 }
748
749
750 /* The other values in restart are already filled in */
751 ret = -ERESTART_RESTARTBLOCK;
752out:
753 return ret;
754}
755
756/**
757 * alarm_timer_nsleep - alarmtimer nanosleep
758 * @which_clock: clockid
759 * @flags: determins abstime or relative
760 * @tsreq: requested sleep time (abs or rel)
761 * @rmtp: remaining sleep time saved
762 *
763 * Handles clock_nanosleep calls against _ALARM clockids
764 */
765static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
766 struct timespec *tsreq, struct timespec __user *rmtp)
767{
768 enum alarmtimer_type type = clock2alarm(which_clock);
769 struct alarm alarm;
770 ktime_t exp;
771 int ret = 0;
772 struct restart_block *restart;
773
John Stultz1c6b39a2011-06-16 18:47:37 -0700774 if (!alarmtimer_get_rtcdev())
775 return -ENOTSUPP;
776
John Stultz16927772014-07-07 14:06:11 -0700777 if (flags & ~TIMER_ABSTIME)
778 return -EINVAL;
779
John Stultz9a7adcf2011-01-11 09:54:33 -0800780 if (!capable(CAP_WAKE_ALARM))
781 return -EPERM;
782
783 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
784
785 exp = timespec_to_ktime(*tsreq);
786 /* Convert (if necessary) to absolute time */
787 if (flags != TIMER_ABSTIME) {
788 ktime_t now = alarm_bases[type].gettime();
Thomas Gleixner747128e2018-07-02 09:34:29 +0200789
790 exp = ktime_add_safe(now, exp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800791 }
792
793 if (alarmtimer_do_nsleep(&alarm, exp))
794 goto out;
795
796 if (freezing(current))
797 alarmtimer_freezerset(exp, type);
798
799 /* abs timers don't set remaining time or restart */
800 if (flags == TIMER_ABSTIME) {
801 ret = -ERESTARTNOHAND;
802 goto out;
803 }
804
805 if (rmtp) {
806 ret = update_rmtp(exp, type, rmtp);
807 if (ret <= 0)
808 goto out;
809 }
810
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800811 restart = &current->restart_block;
John Stultz9a7adcf2011-01-11 09:54:33 -0800812 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200813 restart->nanosleep.clockid = type;
John Stultz9a7adcf2011-01-11 09:54:33 -0800814 restart->nanosleep.expires = exp.tv64;
815 restart->nanosleep.rmtp = rmtp;
816 ret = -ERESTART_RESTARTBLOCK;
817
818out:
819 return ret;
820}
John Stultzff3ead92011-01-11 09:42:13 -0800821
John Stultzff3ead92011-01-11 09:42:13 -0800822
823/* Suspend hook structures */
824static const struct dev_pm_ops alarmtimer_pm_ops = {
825 .suspend = alarmtimer_suspend,
zhuo-haoa0e32132015-11-17 20:08:07 +0800826 .resume = alarmtimer_resume,
John Stultzff3ead92011-01-11 09:42:13 -0800827};
828
829static struct platform_driver alarmtimer_driver = {
830 .driver = {
831 .name = "alarmtimer",
832 .pm = &alarmtimer_pm_ops,
833 }
834};
835
836/**
837 * alarmtimer_init - Initialize alarm timer code
838 *
839 * This function initializes the alarm bases and registers
840 * the posix clock ids.
841 */
842static int __init alarmtimer_init(void)
843{
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200844 struct platform_device *pdev;
John Stultzff3ead92011-01-11 09:42:13 -0800845 int error = 0;
846 int i;
John Stultz9a7adcf2011-01-11 09:54:33 -0800847 struct k_clock alarm_clock = {
848 .clock_getres = alarm_clock_getres,
849 .clock_get = alarm_clock_get,
850 .timer_create = alarm_timer_create,
851 .timer_set = alarm_timer_set,
852 .timer_del = alarm_timer_del,
853 .timer_get = alarm_timer_get,
854 .nsleep = alarm_timer_nsleep,
855 };
856
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100857 alarmtimer_rtc_timer_init();
John Stultzad30dfa2012-03-23 15:52:25 -0700858
John Stultz9a7adcf2011-01-11 09:54:33 -0800859 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
860 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
John Stultzff3ead92011-01-11 09:42:13 -0800861
862 /* Initialize alarm bases */
863 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
864 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
865 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
866 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
867 for (i = 0; i < ALARM_NUMTYPE; i++) {
868 timerqueue_init_head(&alarm_bases[i].timerqueue);
869 spin_lock_init(&alarm_bases[i].lock);
John Stultzff3ead92011-01-11 09:42:13 -0800870 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700871
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200872 error = alarmtimer_rtc_interface_setup();
873 if (error)
874 return error;
John Stultzff3ead92011-01-11 09:42:13 -0800875
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200876 error = platform_driver_register(&alarmtimer_driver);
877 if (error)
878 goto out_if;
879
880 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
881 if (IS_ERR(pdev)) {
882 error = PTR_ERR(pdev);
883 goto out_drv;
884 }
Todd Poynor59a93c22012-08-09 00:37:27 -0700885 ws = wakeup_source_register("alarmtimer");
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200886 return 0;
887
888out_drv:
889 platform_driver_unregister(&alarmtimer_driver);
890out_if:
891 alarmtimer_rtc_interface_remove();
John Stultzff3ead92011-01-11 09:42:13 -0800892 return error;
893}
894device_initcall(alarmtimer_init);