blob: b2df539d4f3adf63c8a7e033986f314b5cb80c5d [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
Mao Jinlonga0adcbc2016-02-17 15:19:08 +080055static void alarmtimer_triggered_func(void *p)
56{
57 struct rtc_device *rtc = rtcdev;
58
59 if (!(rtc->irq_data & RTC_AF))
60 return;
61 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
62}
63
64static struct rtc_task alarmtimer_rtc_task = {
65 .func = alarmtimer_triggered_func
66};
John Stultzc008ba582011-06-16 18:27:09 -070067/**
John Stultzc008ba582011-06-16 18:27:09 -070068 * alarmtimer_get_rtcdev - Return selected rtcdevice
69 *
70 * This function returns the rtc device to use for wakealarms.
71 * If one has not already been chosen, it checks to see if a
72 * functional rtc device is available.
73 */
John Stultz57c498f2012-04-20 12:31:45 -070074struct rtc_device *alarmtimer_get_rtcdev(void)
John Stultzc008ba582011-06-16 18:27:09 -070075{
John Stultzc008ba582011-06-16 18:27:09 -070076 unsigned long flags;
Mao Jinlonga0adcbc2016-02-17 15:19:08 +080077 struct rtc_device *ret = NULL;
John Stultzc008ba582011-06-16 18:27:09 -070078
79 spin_lock_irqsave(&rtcdev_lock, flags);
John Stultzc008ba582011-06-16 18:27:09 -070080 ret = rtcdev;
81 spin_unlock_irqrestore(&rtcdev_lock, flags);
82
83 return ret;
84}
Pramod Gurav71d5d2b2014-06-13 11:49:42 +053085EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
John Stultz8bc0daf2011-07-14 18:35:13 -070086
87static int alarmtimer_rtc_add_device(struct device *dev,
88 struct class_interface *class_intf)
89{
90 unsigned long flags;
Mao Jinlonga0adcbc2016-02-17 15:19:08 +080091 int err = 0;
John Stultz8bc0daf2011-07-14 18:35:13 -070092 struct rtc_device *rtc = to_rtc_device(dev);
John Stultz8bc0daf2011-07-14 18:35:13 -070093 if (rtcdev)
94 return -EBUSY;
John Stultz8bc0daf2011-07-14 18:35:13 -070095 if (!rtc->ops->set_alarm)
96 return -1;
John Stultz8bc0daf2011-07-14 18:35:13 -070097
98 spin_lock_irqsave(&rtcdev_lock, flags);
99 if (!rtcdev) {
Mao Jinlonga0adcbc2016-02-17 15:19:08 +0800100 err = rtc_irq_register(rtc, &alarmtimer_rtc_task);
101 if (err)
102 goto rtc_irq_reg_err;
John Stultz8bc0daf2011-07-14 18:35:13 -0700103 rtcdev = rtc;
104 /* hold a reference so it doesn't go away */
105 get_device(dev);
106 }
Mao Jinlonga0adcbc2016-02-17 15:19:08 +0800107
108rtc_irq_reg_err:
John Stultz8bc0daf2011-07-14 18:35:13 -0700109 spin_unlock_irqrestore(&rtcdev_lock, flags);
Mao Jinlonga0adcbc2016-02-17 15:19:08 +0800110 return err;
111
112}
113
114static void alarmtimer_rtc_remove_device(struct device *dev,
115 struct class_interface *class_intf)
116{
117 if (rtcdev && dev == &rtcdev->dev) {
118 rtc_irq_unregister(rtcdev, &alarmtimer_rtc_task);
119 rtcdev = NULL;
120 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700121}
122
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100123static inline void alarmtimer_rtc_timer_init(void)
124{
125 rtc_timer_init(&rtctimer, NULL, NULL);
126}
127
John Stultz8bc0daf2011-07-14 18:35:13 -0700128static struct class_interface alarmtimer_rtc_interface = {
129 .add_dev = &alarmtimer_rtc_add_device,
Mao Jinlonga0adcbc2016-02-17 15:19:08 +0800130 .remove_dev = &alarmtimer_rtc_remove_device,
John Stultz8bc0daf2011-07-14 18:35:13 -0700131};
132
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200133static int alarmtimer_rtc_interface_setup(void)
John Stultz8bc0daf2011-07-14 18:35:13 -0700134{
135 alarmtimer_rtc_interface.class = rtc_class;
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200136 return class_interface_register(&alarmtimer_rtc_interface);
137}
138static void alarmtimer_rtc_interface_remove(void)
139{
140 class_interface_unregister(&alarmtimer_rtc_interface);
John Stultz8bc0daf2011-07-14 18:35:13 -0700141}
John Stultz1c6b39a2011-06-16 18:47:37 -0700142#else
John Stultz57c498f2012-04-20 12:31:45 -0700143struct rtc_device *alarmtimer_get_rtcdev(void)
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200144{
145 return NULL;
146}
147#define rtcdev (NULL)
148static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
149static inline void alarmtimer_rtc_interface_remove(void) { }
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100150static inline void alarmtimer_rtc_timer_init(void) { }
John Stultzc008ba582011-06-16 18:27:09 -0700151#endif
John Stultzff3ead92011-01-11 09:42:13 -0800152
John Stultz180bf812011-04-28 12:58:11 -0700153/**
John Stultzff3ead92011-01-11 09:42:13 -0800154 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
155 * @base: pointer to the base where the timer is being run
156 * @alarm: pointer to alarm being enqueued.
157 *
John Stultzdae373b2012-09-13 19:12:16 -0400158 * Adds alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800159 *
160 * Must hold base->lock when calling.
161 */
162static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
163{
John Stultzdae373b2012-09-13 19:12:16 -0400164 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
165 timerqueue_del(&base->timerqueue, &alarm->node);
166
John Stultzff3ead92011-01-11 09:42:13 -0800167 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700168 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800169}
170
John Stultz180bf812011-04-28 12:58:11 -0700171/**
John Stultza65bcc12012-09-13 19:25:22 -0400172 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800173 * @base: pointer to the base where the timer is running
174 * @alarm: pointer to alarm being removed
175 *
John Stultzdae373b2012-09-13 19:12:16 -0400176 * Removes alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800177 *
178 * Must hold base->lock when calling.
179 */
John Stultza65bcc12012-09-13 19:25:22 -0400180static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800181{
John Stultza28cde82011-08-10 12:30:21 -0700182 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
183 return;
184
John Stultzff3ead92011-01-11 09:42:13 -0800185 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700186 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800187}
188
John Stultz7068b7a2011-04-28 13:29:18 -0700189
John Stultz180bf812011-04-28 12:58:11 -0700190/**
John Stultz7068b7a2011-04-28 13:29:18 -0700191 * alarmtimer_fired - Handles alarm hrtimer being fired.
192 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800193 *
John Stultz180bf812011-04-28 12:58:11 -0700194 * When a alarm timer fires, this runs through the timerqueue to
195 * see which alarms expired, and runs those. If there are more alarm
196 * timers queued for the future, we set the hrtimer to fire when
197 * when the next future alarm timer expires.
John Stultzff3ead92011-01-11 09:42:13 -0800198 */
John Stultz7068b7a2011-04-28 13:29:18 -0700199static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800200{
John Stultzdae373b2012-09-13 19:12:16 -0400201 struct alarm *alarm = container_of(timer, struct alarm, timer);
202 struct alarm_base *base = &alarm_bases[alarm->type];
John Stultzff3ead92011-01-11 09:42:13 -0800203 unsigned long flags;
John Stultz7068b7a2011-04-28 13:29:18 -0700204 int ret = HRTIMER_NORESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700205 int restart = ALARMTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800206
207 spin_lock_irqsave(&base->lock, flags);
John Stultza65bcc12012-09-13 19:25:22 -0400208 alarmtimer_dequeue(base, alarm);
John Stultzdae373b2012-09-13 19:12:16 -0400209 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800210
John Stultzdae373b2012-09-13 19:12:16 -0400211 if (alarm->function)
212 restart = alarm->function(alarm, base->gettime());
John Stultzff3ead92011-01-11 09:42:13 -0800213
John Stultzdae373b2012-09-13 19:12:16 -0400214 spin_lock_irqsave(&base->lock, flags);
215 if (restart != ALARMTIMER_NORESTART) {
216 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
217 alarmtimer_enqueue(base, alarm);
John Stultz7068b7a2011-04-28 13:29:18 -0700218 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800219 }
220 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800221
John Stultz7068b7a2011-04-28 13:29:18 -0700222 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800223
John Stultzff3ead92011-01-11 09:42:13 -0800224}
225
Todd Poynor6cffe002013-05-15 14:38:11 -0700226ktime_t alarm_expires_remaining(const struct alarm *alarm)
227{
228 struct alarm_base *base = &alarm_bases[alarm->type];
229 return ktime_sub(alarm->node.expires, base->gettime());
230}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200231EXPORT_SYMBOL_GPL(alarm_expires_remaining);
Todd Poynor6cffe002013-05-15 14:38:11 -0700232
John Stultz472647d2011-04-29 15:03:10 -0700233#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700234/**
John Stultzff3ead92011-01-11 09:42:13 -0800235 * alarmtimer_suspend - Suspend time callback
236 * @dev: unused
237 * @state: unused
238 *
239 * When we are going into suspend, we look through the bases
240 * to see which is the soonest timer to expire. We then
241 * set an rtc timer to fire that far into the future, which
242 * will wake us from suspend.
243 */
244static int alarmtimer_suspend(struct device *dev)
245{
246 struct rtc_time tm;
247 ktime_t min, now;
248 unsigned long flags;
John Stultzc008ba582011-06-16 18:27:09 -0700249 struct rtc_device *rtc;
John Stultzff3ead92011-01-11 09:42:13 -0800250 int i;
Todd Poynor59a93c22012-08-09 00:37:27 -0700251 int ret;
John Stultzff3ead92011-01-11 09:42:13 -0800252
253 spin_lock_irqsave(&freezer_delta_lock, flags);
254 min = freezer_delta;
255 freezer_delta = ktime_set(0, 0);
256 spin_unlock_irqrestore(&freezer_delta_lock, flags);
257
John Stultz8bc0daf2011-07-14 18:35:13 -0700258 rtc = alarmtimer_get_rtcdev();
John Stultzff3ead92011-01-11 09:42:13 -0800259 /* If we have no rtcdev, just return */
John Stultzc008ba582011-06-16 18:27:09 -0700260 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800261 return 0;
262
263 /* Find the soonest timer to expire*/
264 for (i = 0; i < ALARM_NUMTYPE; i++) {
265 struct alarm_base *base = &alarm_bases[i];
266 struct timerqueue_node *next;
267 ktime_t delta;
268
269 spin_lock_irqsave(&base->lock, flags);
270 next = timerqueue_getnext(&base->timerqueue);
271 spin_unlock_irqrestore(&base->lock, flags);
272 if (!next)
273 continue;
274 delta = ktime_sub(next->expires, base->gettime());
275 if (!min.tv64 || (delta.tv64 < min.tv64))
276 min = delta;
277 }
278 if (min.tv64 == 0)
279 return 0;
280
Todd Poynor59a93c22012-08-09 00:37:27 -0700281 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
282 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
283 return -EBUSY;
284 }
John Stultzff3ead92011-01-11 09:42:13 -0800285
286 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba582011-06-16 18:27:09 -0700287 rtc_timer_cancel(rtc, &rtctimer);
288 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800289 now = rtc_tm_to_ktime(tm);
290 now = ktime_add(now, min);
291
Todd Poynor59a93c22012-08-09 00:37:27 -0700292 /* Set alarm, if in the past reject suspend briefly to handle */
293 ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
294 if (ret < 0)
295 __pm_wakeup_event(ws, MSEC_PER_SEC);
296 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800297}
Mao Jinlong756157d2017-10-27 11:07:11 +0800298
zhuo-haoa0e32132015-11-17 20:08:07 +0800299static int alarmtimer_resume(struct device *dev)
300{
301 struct rtc_device *rtc;
302
303 rtc = alarmtimer_get_rtcdev();
304 if (rtc)
305 rtc_timer_cancel(rtc, &rtctimer);
306 return 0;
307}
308
John Stultz472647d2011-04-29 15:03:10 -0700309#else
310static int alarmtimer_suspend(struct device *dev)
311{
312 return 0;
313}
zhuo-haoa0e32132015-11-17 20:08:07 +0800314
315static int alarmtimer_resume(struct device *dev)
316{
317 return 0;
318}
John Stultz472647d2011-04-29 15:03:10 -0700319#endif
John Stultzff3ead92011-01-11 09:42:13 -0800320
John Stultz9a7adcf2011-01-11 09:54:33 -0800321static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
322{
323 ktime_t delta;
324 unsigned long flags;
325 struct alarm_base *base = &alarm_bases[type];
326
327 delta = ktime_sub(absexp, base->gettime());
328
329 spin_lock_irqsave(&freezer_delta_lock, flags);
330 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
331 freezer_delta = delta;
332 spin_unlock_irqrestore(&freezer_delta_lock, flags);
333}
334
335
John Stultz180bf812011-04-28 12:58:11 -0700336/**
John Stultzff3ead92011-01-11 09:42:13 -0800337 * alarm_init - Initialize an alarm structure
338 * @alarm: ptr to alarm to be initialized
339 * @type: the type of the alarm
340 * @function: callback that is run when the alarm fires
John Stultzff3ead92011-01-11 09:42:13 -0800341 */
342void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
John Stultz4b413082011-08-10 10:37:59 -0700343 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
John Stultzff3ead92011-01-11 09:42:13 -0800344{
345 timerqueue_init(&alarm->node);
John Stultzdae373b2012-09-13 19:12:16 -0400346 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
347 HRTIMER_MODE_ABS);
348 alarm->timer.function = alarmtimer_fired;
John Stultzff3ead92011-01-11 09:42:13 -0800349 alarm->function = function;
350 alarm->type = type;
John Stultza28cde82011-08-10 12:30:21 -0700351 alarm->state = ALARMTIMER_STATE_INACTIVE;
John Stultzff3ead92011-01-11 09:42:13 -0800352}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200353EXPORT_SYMBOL_GPL(alarm_init);
John Stultzff3ead92011-01-11 09:42:13 -0800354
John Stultz180bf812011-04-28 12:58:11 -0700355/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700356 * alarm_start - Sets an absolute alarm to fire
John Stultzff3ead92011-01-11 09:42:13 -0800357 * @alarm: ptr to alarm to set
358 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800359 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000360void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800361{
362 struct alarm_base *base = &alarm_bases[alarm->type];
363 unsigned long flags;
364
365 spin_lock_irqsave(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800366 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800367 alarmtimer_enqueue(base, alarm);
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000368 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
John Stultzff3ead92011-01-11 09:42:13 -0800369 spin_unlock_irqrestore(&base->lock, flags);
370}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200371EXPORT_SYMBOL_GPL(alarm_start);
John Stultzff3ead92011-01-11 09:42:13 -0800372
John Stultz180bf812011-04-28 12:58:11 -0700373/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700374 * alarm_start_relative - Sets a relative alarm to fire
375 * @alarm: ptr to alarm to set
376 * @start: time relative to now to run the alarm
377 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000378void alarm_start_relative(struct alarm *alarm, ktime_t start)
Todd Poynor6cffe002013-05-15 14:38:11 -0700379{
380 struct alarm_base *base = &alarm_bases[alarm->type];
381
Thomas Gleixner8ee7f062017-05-30 23:15:34 +0200382 start = ktime_add_safe(start, base->gettime());
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000383 alarm_start(alarm, start);
Todd Poynor6cffe002013-05-15 14:38:11 -0700384}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200385EXPORT_SYMBOL_GPL(alarm_start_relative);
Todd Poynor6cffe002013-05-15 14:38:11 -0700386
387void alarm_restart(struct alarm *alarm)
388{
389 struct alarm_base *base = &alarm_bases[alarm->type];
390 unsigned long flags;
391
392 spin_lock_irqsave(&base->lock, flags);
393 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
394 hrtimer_restart(&alarm->timer);
395 alarmtimer_enqueue(base, alarm);
396 spin_unlock_irqrestore(&base->lock, flags);
397}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200398EXPORT_SYMBOL_GPL(alarm_restart);
Todd Poynor6cffe002013-05-15 14:38:11 -0700399
400/**
John Stultz9082c462011-08-10 12:41:36 -0700401 * alarm_try_to_cancel - Tries to cancel an alarm timer
John Stultzff3ead92011-01-11 09:42:13 -0800402 * @alarm: ptr to alarm to be canceled
John Stultz9082c462011-08-10 12:41:36 -0700403 *
404 * Returns 1 if the timer was canceled, 0 if it was not running,
405 * and -1 if the callback was running
John Stultzff3ead92011-01-11 09:42:13 -0800406 */
John Stultz9082c462011-08-10 12:41:36 -0700407int alarm_try_to_cancel(struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800408{
409 struct alarm_base *base = &alarm_bases[alarm->type];
410 unsigned long flags;
John Stultzdae373b2012-09-13 19:12:16 -0400411 int ret;
412
John Stultzff3ead92011-01-11 09:42:13 -0800413 spin_lock_irqsave(&base->lock, flags);
John Stultzdae373b2012-09-13 19:12:16 -0400414 ret = hrtimer_try_to_cancel(&alarm->timer);
415 if (ret >= 0)
John Stultza65bcc12012-09-13 19:25:22 -0400416 alarmtimer_dequeue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800417 spin_unlock_irqrestore(&base->lock, flags);
John Stultz9082c462011-08-10 12:41:36 -0700418 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800419}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200420EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
John Stultzff3ead92011-01-11 09:42:13 -0800421
422
John Stultz9082c462011-08-10 12:41:36 -0700423/**
424 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
425 * @alarm: ptr to alarm to be canceled
426 *
427 * Returns 1 if the timer was canceled, 0 if it was not active.
428 */
429int alarm_cancel(struct alarm *alarm)
430{
431 for (;;) {
432 int ret = alarm_try_to_cancel(alarm);
433 if (ret >= 0)
434 return ret;
435 cpu_relax();
436 }
437}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200438EXPORT_SYMBOL_GPL(alarm_cancel);
John Stultz9082c462011-08-10 12:41:36 -0700439
John Stultzdce75a82011-08-10 11:31:03 -0700440
441u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
442{
443 u64 overrun = 1;
444 ktime_t delta;
445
446 delta = ktime_sub(now, alarm->node.expires);
447
448 if (delta.tv64 < 0)
449 return 0;
450
451 if (unlikely(delta.tv64 >= interval.tv64)) {
452 s64 incr = ktime_to_ns(interval);
453
454 overrun = ktime_divns(delta, incr);
455
456 alarm->node.expires = ktime_add_ns(alarm->node.expires,
457 incr*overrun);
458
459 if (alarm->node.expires.tv64 > now.tv64)
460 return overrun;
461 /*
462 * This (and the ktime_add() below) is the
463 * correction for exact:
464 */
465 overrun++;
466 }
467
Thomas Gleixner8ee7f062017-05-30 23:15:34 +0200468 alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
John Stultzdce75a82011-08-10 11:31:03 -0700469 return overrun;
470}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200471EXPORT_SYMBOL_GPL(alarm_forward);
John Stultzdce75a82011-08-10 11:31:03 -0700472
Todd Poynor6cffe002013-05-15 14:38:11 -0700473u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
474{
475 struct alarm_base *base = &alarm_bases[alarm->type];
476
477 return alarm_forward(alarm, base->gettime(), interval);
478}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200479EXPORT_SYMBOL_GPL(alarm_forward_now);
John Stultzdce75a82011-08-10 11:31:03 -0700480
481
John Stultz180bf812011-04-28 12:58:11 -0700482/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800483 * clock2alarm - helper that converts from clockid to alarmtypes
484 * @clockid: clockid.
John Stultz9a7adcf2011-01-11 09:54:33 -0800485 */
Mao Jinlongea47aaf2017-10-27 11:15:55 +0800486static enum alarmtimer_type clock2alarm(clockid_t clockid)
John Stultz9a7adcf2011-01-11 09:54:33 -0800487{
488 if (clockid == CLOCK_REALTIME_ALARM)
489 return ALARM_REALTIME;
490 if (clockid == CLOCK_BOOTTIME_ALARM)
491 return ALARM_BOOTTIME;
492 return -1;
493}
494
John Stultz180bf812011-04-28 12:58:11 -0700495/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800496 * alarm_handle_timer - Callback for posix timers
497 * @alarm: alarm that fired
498 *
499 * Posix timer callback for expired alarm timers.
500 */
John Stultz4b413082011-08-10 10:37:59 -0700501static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
502 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800503{
Richard Larocque474e9412014-09-09 18:31:05 -0700504 unsigned long flags;
John Stultz9a7adcf2011-01-11 09:54:33 -0800505 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
John Stultz9e264762011-08-10 12:09:24 -0700506 it.alarm.alarmtimer);
Richard Larocque474e9412014-09-09 18:31:05 -0700507 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
508
509 spin_lock_irqsave(&ptr->it_lock, flags);
Richard Larocque265b81d2014-09-09 18:31:04 -0700510 if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) {
511 if (posix_timer_event(ptr, 0) != 0)
512 ptr->it_overrun++;
513 }
John Stultz4b413082011-08-10 10:37:59 -0700514
John Stultz54da23b2011-08-10 11:08:07 -0700515 /* Re-add periodic timers */
John Stultz9e264762011-08-10 12:09:24 -0700516 if (ptr->it.alarm.interval.tv64) {
517 ptr->it_overrun += alarm_forward(alarm, now,
518 ptr->it.alarm.interval);
Richard Larocque474e9412014-09-09 18:31:05 -0700519 result = ALARMTIMER_RESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700520 }
Richard Larocque474e9412014-09-09 18:31:05 -0700521 spin_unlock_irqrestore(&ptr->it_lock, flags);
522
523 return result;
John Stultz9a7adcf2011-01-11 09:54:33 -0800524}
525
John Stultz180bf812011-04-28 12:58:11 -0700526/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800527 * alarm_clock_getres - posix getres interface
528 * @which_clock: clockid
529 * @tp: timespec to fill
530 *
531 * Returns the granularity of underlying alarm base clock
532 */
533static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
534{
John Stultz1c6b39a2011-06-16 18:47:37 -0700535 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400536 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700537
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000538 tp->tv_sec = 0;
539 tp->tv_nsec = hrtimer_resolution;
540 return 0;
John Stultz9a7adcf2011-01-11 09:54:33 -0800541}
542
543/**
544 * alarm_clock_get - posix clock_get interface
545 * @which_clock: clockid
546 * @tp: timespec to fill.
547 *
548 * Provides the underlying alarm base time.
549 */
550static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
551{
552 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
553
John Stultz1c6b39a2011-06-16 18:47:37 -0700554 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400555 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700556
John Stultz9a7adcf2011-01-11 09:54:33 -0800557 *tp = ktime_to_timespec(base->gettime());
558 return 0;
559}
560
561/**
562 * alarm_timer_create - posix timer_create interface
563 * @new_timer: k_itimer pointer to manage
564 *
565 * Initializes the k_itimer structure.
566 */
567static int alarm_timer_create(struct k_itimer *new_timer)
568{
569 enum alarmtimer_type type;
John Stultz9a7adcf2011-01-11 09:54:33 -0800570
John Stultz1c6b39a2011-06-16 18:47:37 -0700571 if (!alarmtimer_get_rtcdev())
572 return -ENOTSUPP;
573
John Stultz9a7adcf2011-01-11 09:54:33 -0800574 if (!capable(CAP_WAKE_ALARM))
575 return -EPERM;
576
577 type = clock2alarm(new_timer->it_clock);
John Stultz9e264762011-08-10 12:09:24 -0700578 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf2011-01-11 09:54:33 -0800579 return 0;
580}
581
582/**
583 * alarm_timer_get - posix timer_get interface
584 * @new_timer: k_itimer pointer
585 * @cur_setting: itimerspec data to fill
586 *
Richard Larocquee86fea72014-09-09 18:31:03 -0700587 * Copies out the current itimerspec data
John Stultz9a7adcf2011-01-11 09:54:33 -0800588 */
589static void alarm_timer_get(struct k_itimer *timr,
590 struct itimerspec *cur_setting)
591{
Richard Larocquee86fea72014-09-09 18:31:03 -0700592 ktime_t relative_expiry_time =
593 alarm_expires_remaining(&(timr->it.alarm.alarmtimer));
John Stultzea7802f2011-08-04 07:51:56 -0700594
Richard Larocquee86fea72014-09-09 18:31:03 -0700595 if (ktime_to_ns(relative_expiry_time) > 0) {
596 cur_setting->it_value = ktime_to_timespec(relative_expiry_time);
597 } else {
598 cur_setting->it_value.tv_sec = 0;
599 cur_setting->it_value.tv_nsec = 0;
600 }
601
602 cur_setting->it_interval = ktime_to_timespec(timr->it.alarm.interval);
John Stultz9a7adcf2011-01-11 09:54:33 -0800603}
604
605/**
606 * alarm_timer_del - posix timer_del interface
607 * @timr: k_itimer pointer to be deleted
608 *
609 * Cancels any programmed alarms for the given timer.
610 */
611static int alarm_timer_del(struct k_itimer *timr)
612{
John Stultz1c6b39a2011-06-16 18:47:37 -0700613 if (!rtcdev)
614 return -ENOTSUPP;
615
John Stultz9082c462011-08-10 12:41:36 -0700616 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
617 return TIMER_RETRY;
618
John Stultz9a7adcf2011-01-11 09:54:33 -0800619 return 0;
620}
621
622/**
623 * alarm_timer_set - posix timer_set interface
624 * @timr: k_itimer pointer to be deleted
625 * @flags: timer flags
626 * @new_setting: itimerspec to be used
627 * @old_setting: itimerspec being replaced
628 *
629 * Sets the timer to new_setting, and starts the timer.
630 */
631static int alarm_timer_set(struct k_itimer *timr, int flags,
632 struct itimerspec *new_setting,
633 struct itimerspec *old_setting)
634{
John Stultz16927772014-07-07 14:06:11 -0700635 ktime_t exp;
636
John Stultz1c6b39a2011-06-16 18:47:37 -0700637 if (!rtcdev)
638 return -ENOTSUPP;
639
John Stultz16927772014-07-07 14:06:11 -0700640 if (flags & ~TIMER_ABSTIME)
641 return -EINVAL;
642
John Stultz971c90b2011-08-04 07:25:35 -0700643 if (old_setting)
644 alarm_timer_get(timr, old_setting);
John Stultz9a7adcf2011-01-11 09:54:33 -0800645
646 /* If the timer was already set, cancel it */
John Stultz9082c462011-08-10 12:41:36 -0700647 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
648 return TIMER_RETRY;
John Stultz9a7adcf2011-01-11 09:54:33 -0800649
650 /* start the timer */
John Stultz9e264762011-08-10 12:09:24 -0700651 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
Thomas Gleixner04651042017-05-30 23:15:35 +0200652
653 /*
654 * Rate limit to the tick as a hot fix to prevent DOS. Will be
655 * mopped up later.
656 */
Greg Hackmann91af5f02017-07-24 10:19:24 -0700657 if (timr->it.alarm.interval.tv64 &&
658 ktime_to_ns(timr->it.alarm.interval) < TICK_NSEC)
Thomas Gleixner04651042017-05-30 23:15:35 +0200659 timr->it.alarm.interval = ktime_set(0, TICK_NSEC);
660
John Stultz16927772014-07-07 14:06:11 -0700661 exp = timespec_to_ktime(new_setting->it_value);
662 /* Convert (if necessary) to absolute time */
663 if (flags != TIMER_ABSTIME) {
664 ktime_t now;
665
666 now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
Thomas Gleixner8ee7f062017-05-30 23:15:34 +0200667 exp = ktime_add_safe(now, exp);
John Stultz16927772014-07-07 14:06:11 -0700668 }
669
670 alarm_start(&timr->it.alarm.alarmtimer, exp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800671 return 0;
672}
673
674/**
675 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
676 * @alarm: ptr to alarm that fired
677 *
678 * Wakes up the task that set the alarmtimer
679 */
John Stultz4b413082011-08-10 10:37:59 -0700680static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
681 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800682{
683 struct task_struct *task = (struct task_struct *)alarm->data;
684
685 alarm->data = NULL;
686 if (task)
687 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700688 return ALARMTIMER_NORESTART;
John Stultz9a7adcf2011-01-11 09:54:33 -0800689}
690
691/**
692 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
693 * @alarm: ptr to alarmtimer
694 * @absexp: absolute expiration time
695 *
696 * Sets the alarm timer and sleeps until it is fired or interrupted.
697 */
698static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
699{
700 alarm->data = (void *)current;
701 do {
702 set_current_state(TASK_INTERRUPTIBLE);
John Stultz9e264762011-08-10 12:09:24 -0700703 alarm_start(alarm, absexp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800704 if (likely(alarm->data))
705 schedule();
706
707 alarm_cancel(alarm);
708 } while (alarm->data && !signal_pending(current));
709
710 __set_current_state(TASK_RUNNING);
711
712 return (alarm->data == NULL);
713}
714
715
716/**
717 * update_rmtp - Update remaining timespec value
718 * @exp: expiration time
719 * @type: timer type
720 * @rmtp: user pointer to remaining timepsec value
721 *
722 * Helper function that fills in rmtp value with time between
723 * now and the exp value
724 */
725static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
726 struct timespec __user *rmtp)
727{
728 struct timespec rmt;
729 ktime_t rem;
730
731 rem = ktime_sub(exp, alarm_bases[type].gettime());
732
733 if (rem.tv64 <= 0)
734 return 0;
735 rmt = ktime_to_timespec(rem);
736
737 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
738 return -EFAULT;
739
740 return 1;
741
742}
743
744/**
745 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
746 * @restart: ptr to restart block
747 *
748 * Handles restarted clock_nanosleep calls
749 */
750static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
751{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200752 enum alarmtimer_type type = restart->nanosleep.clockid;
John Stultz9a7adcf2011-01-11 09:54:33 -0800753 ktime_t exp;
754 struct timespec __user *rmtp;
755 struct alarm alarm;
756 int ret = 0;
757
758 exp.tv64 = restart->nanosleep.expires;
759 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
760
761 if (alarmtimer_do_nsleep(&alarm, exp))
762 goto out;
763
764 if (freezing(current))
765 alarmtimer_freezerset(exp, type);
766
767 rmtp = restart->nanosleep.rmtp;
768 if (rmtp) {
769 ret = update_rmtp(exp, type, rmtp);
770 if (ret <= 0)
771 goto out;
772 }
773
774
775 /* The other values in restart are already filled in */
776 ret = -ERESTART_RESTARTBLOCK;
777out:
778 return ret;
779}
780
781/**
782 * alarm_timer_nsleep - alarmtimer nanosleep
783 * @which_clock: clockid
784 * @flags: determins abstime or relative
785 * @tsreq: requested sleep time (abs or rel)
786 * @rmtp: remaining sleep time saved
787 *
788 * Handles clock_nanosleep calls against _ALARM clockids
789 */
790static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
791 struct timespec *tsreq, struct timespec __user *rmtp)
792{
793 enum alarmtimer_type type = clock2alarm(which_clock);
794 struct alarm alarm;
795 ktime_t exp;
796 int ret = 0;
797 struct restart_block *restart;
798
John Stultz1c6b39a2011-06-16 18:47:37 -0700799 if (!alarmtimer_get_rtcdev())
800 return -ENOTSUPP;
801
John Stultz16927772014-07-07 14:06:11 -0700802 if (flags & ~TIMER_ABSTIME)
803 return -EINVAL;
804
John Stultz9a7adcf2011-01-11 09:54:33 -0800805 if (!capable(CAP_WAKE_ALARM))
806 return -EPERM;
807
808 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
809
810 exp = timespec_to_ktime(*tsreq);
811 /* Convert (if necessary) to absolute time */
812 if (flags != TIMER_ABSTIME) {
813 ktime_t now = alarm_bases[type].gettime();
814 exp = ktime_add(now, exp);
815 }
816
817 if (alarmtimer_do_nsleep(&alarm, exp))
818 goto out;
819
820 if (freezing(current))
821 alarmtimer_freezerset(exp, type);
822
823 /* abs timers don't set remaining time or restart */
824 if (flags == TIMER_ABSTIME) {
825 ret = -ERESTARTNOHAND;
826 goto out;
827 }
828
829 if (rmtp) {
830 ret = update_rmtp(exp, type, rmtp);
831 if (ret <= 0)
832 goto out;
833 }
834
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800835 restart = &current->restart_block;
John Stultz9a7adcf2011-01-11 09:54:33 -0800836 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200837 restart->nanosleep.clockid = type;
John Stultz9a7adcf2011-01-11 09:54:33 -0800838 restart->nanosleep.expires = exp.tv64;
839 restart->nanosleep.rmtp = rmtp;
840 ret = -ERESTART_RESTARTBLOCK;
841
842out:
843 return ret;
844}
John Stultzff3ead92011-01-11 09:42:13 -0800845
John Stultzff3ead92011-01-11 09:42:13 -0800846
847/* Suspend hook structures */
848static const struct dev_pm_ops alarmtimer_pm_ops = {
849 .suspend = alarmtimer_suspend,
zhuo-haoa0e32132015-11-17 20:08:07 +0800850 .resume = alarmtimer_resume,
John Stultzff3ead92011-01-11 09:42:13 -0800851};
852
853static struct platform_driver alarmtimer_driver = {
854 .driver = {
855 .name = "alarmtimer",
856 .pm = &alarmtimer_pm_ops,
857 }
858};
859
860/**
861 * alarmtimer_init - Initialize alarm timer code
862 *
863 * This function initializes the alarm bases and registers
864 * the posix clock ids.
865 */
866static int __init alarmtimer_init(void)
867{
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200868 struct platform_device *pdev;
John Stultzff3ead92011-01-11 09:42:13 -0800869 int error = 0;
870 int i;
John Stultz9a7adcf2011-01-11 09:54:33 -0800871 struct k_clock alarm_clock = {
872 .clock_getres = alarm_clock_getres,
873 .clock_get = alarm_clock_get,
874 .timer_create = alarm_timer_create,
875 .timer_set = alarm_timer_set,
876 .timer_del = alarm_timer_del,
877 .timer_get = alarm_timer_get,
878 .nsleep = alarm_timer_nsleep,
879 };
880
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100881 alarmtimer_rtc_timer_init();
John Stultzad30dfa2012-03-23 15:52:25 -0700882
John Stultz9a7adcf2011-01-11 09:54:33 -0800883 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
884 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
John Stultzff3ead92011-01-11 09:42:13 -0800885
886 /* Initialize alarm bases */
887 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
888 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
889 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
890 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
891 for (i = 0; i < ALARM_NUMTYPE; i++) {
892 timerqueue_init_head(&alarm_bases[i].timerqueue);
893 spin_lock_init(&alarm_bases[i].lock);
John Stultzff3ead92011-01-11 09:42:13 -0800894 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700895
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200896 error = alarmtimer_rtc_interface_setup();
897 if (error)
898 return error;
John Stultzff3ead92011-01-11 09:42:13 -0800899
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200900 error = platform_driver_register(&alarmtimer_driver);
901 if (error)
902 goto out_if;
903
904 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
905 if (IS_ERR(pdev)) {
906 error = PTR_ERR(pdev);
907 goto out_drv;
908 }
Todd Poynor59a93c22012-08-09 00:37:27 -0700909 ws = wakeup_source_register("alarmtimer");
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800910 return 0;
Mao Jinlongea47aaf2017-10-27 11:15:55 +0800911
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200912out_drv:
913 platform_driver_unregister(&alarmtimer_driver);
914out_if:
915 alarmtimer_rtc_interface_remove();
John Stultzff3ead92011-01-11 09:42:13 -0800916 return error;
917}
918device_initcall(alarmtimer_init);