blob: 1c2dc98ba63df048336cac26309e7ea42f7c3f9d [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;
Vamshi Krishna B Vf045a3d2018-03-15 17:02:36 +0530351 if (type >= ALARM_NUMTYPE) {
352 /* use ALARM_BOOTTIME as the default */
353 alarm->type = ALARM_BOOTTIME;
354 }
John Stultza28cde82011-08-10 12:30:21 -0700355 alarm->state = ALARMTIMER_STATE_INACTIVE;
John Stultzff3ead92011-01-11 09:42:13 -0800356}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200357EXPORT_SYMBOL_GPL(alarm_init);
John Stultzff3ead92011-01-11 09:42:13 -0800358
John Stultz180bf812011-04-28 12:58:11 -0700359/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700360 * alarm_start - Sets an absolute alarm to fire
John Stultzff3ead92011-01-11 09:42:13 -0800361 * @alarm: ptr to alarm to set
362 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800363 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000364void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800365{
366 struct alarm_base *base = &alarm_bases[alarm->type];
367 unsigned long flags;
368
369 spin_lock_irqsave(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800370 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800371 alarmtimer_enqueue(base, alarm);
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000372 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
John Stultzff3ead92011-01-11 09:42:13 -0800373 spin_unlock_irqrestore(&base->lock, flags);
374}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200375EXPORT_SYMBOL_GPL(alarm_start);
John Stultzff3ead92011-01-11 09:42:13 -0800376
John Stultz180bf812011-04-28 12:58:11 -0700377/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700378 * alarm_start_relative - Sets a relative alarm to fire
379 * @alarm: ptr to alarm to set
380 * @start: time relative to now to run the alarm
381 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000382void alarm_start_relative(struct alarm *alarm, ktime_t start)
Todd Poynor6cffe002013-05-15 14:38:11 -0700383{
384 struct alarm_base *base = &alarm_bases[alarm->type];
385
Thomas Gleixner8ee7f062017-05-30 23:15:34 +0200386 start = ktime_add_safe(start, base->gettime());
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000387 alarm_start(alarm, start);
Todd Poynor6cffe002013-05-15 14:38:11 -0700388}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200389EXPORT_SYMBOL_GPL(alarm_start_relative);
Todd Poynor6cffe002013-05-15 14:38:11 -0700390
391void alarm_restart(struct alarm *alarm)
392{
393 struct alarm_base *base = &alarm_bases[alarm->type];
394 unsigned long flags;
395
396 spin_lock_irqsave(&base->lock, flags);
397 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
398 hrtimer_restart(&alarm->timer);
399 alarmtimer_enqueue(base, alarm);
400 spin_unlock_irqrestore(&base->lock, flags);
401}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200402EXPORT_SYMBOL_GPL(alarm_restart);
Todd Poynor6cffe002013-05-15 14:38:11 -0700403
404/**
John Stultz9082c462011-08-10 12:41:36 -0700405 * alarm_try_to_cancel - Tries to cancel an alarm timer
John Stultzff3ead92011-01-11 09:42:13 -0800406 * @alarm: ptr to alarm to be canceled
John Stultz9082c462011-08-10 12:41:36 -0700407 *
408 * Returns 1 if the timer was canceled, 0 if it was not running,
409 * and -1 if the callback was running
John Stultzff3ead92011-01-11 09:42:13 -0800410 */
John Stultz9082c462011-08-10 12:41:36 -0700411int alarm_try_to_cancel(struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800412{
413 struct alarm_base *base = &alarm_bases[alarm->type];
414 unsigned long flags;
John Stultzdae373b2012-09-13 19:12:16 -0400415 int ret;
416
John Stultzff3ead92011-01-11 09:42:13 -0800417 spin_lock_irqsave(&base->lock, flags);
John Stultzdae373b2012-09-13 19:12:16 -0400418 ret = hrtimer_try_to_cancel(&alarm->timer);
419 if (ret >= 0)
John Stultza65bcc12012-09-13 19:25:22 -0400420 alarmtimer_dequeue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800421 spin_unlock_irqrestore(&base->lock, flags);
John Stultz9082c462011-08-10 12:41:36 -0700422 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800423}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200424EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
John Stultzff3ead92011-01-11 09:42:13 -0800425
426
John Stultz9082c462011-08-10 12:41:36 -0700427/**
428 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
429 * @alarm: ptr to alarm to be canceled
430 *
431 * Returns 1 if the timer was canceled, 0 if it was not active.
432 */
433int alarm_cancel(struct alarm *alarm)
434{
435 for (;;) {
436 int ret = alarm_try_to_cancel(alarm);
437 if (ret >= 0)
438 return ret;
439 cpu_relax();
440 }
441}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200442EXPORT_SYMBOL_GPL(alarm_cancel);
John Stultz9082c462011-08-10 12:41:36 -0700443
John Stultzdce75a82011-08-10 11:31:03 -0700444
445u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
446{
447 u64 overrun = 1;
448 ktime_t delta;
449
450 delta = ktime_sub(now, alarm->node.expires);
451
452 if (delta.tv64 < 0)
453 return 0;
454
455 if (unlikely(delta.tv64 >= interval.tv64)) {
456 s64 incr = ktime_to_ns(interval);
457
458 overrun = ktime_divns(delta, incr);
459
460 alarm->node.expires = ktime_add_ns(alarm->node.expires,
461 incr*overrun);
462
463 if (alarm->node.expires.tv64 > now.tv64)
464 return overrun;
465 /*
466 * This (and the ktime_add() below) is the
467 * correction for exact:
468 */
469 overrun++;
470 }
471
Thomas Gleixner8ee7f062017-05-30 23:15:34 +0200472 alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
John Stultzdce75a82011-08-10 11:31:03 -0700473 return overrun;
474}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200475EXPORT_SYMBOL_GPL(alarm_forward);
John Stultzdce75a82011-08-10 11:31:03 -0700476
Todd Poynor6cffe002013-05-15 14:38:11 -0700477u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
478{
479 struct alarm_base *base = &alarm_bases[alarm->type];
480
481 return alarm_forward(alarm, base->gettime(), interval);
482}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200483EXPORT_SYMBOL_GPL(alarm_forward_now);
John Stultzdce75a82011-08-10 11:31:03 -0700484
485
John Stultz180bf812011-04-28 12:58:11 -0700486/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800487 * clock2alarm - helper that converts from clockid to alarmtypes
488 * @clockid: clockid.
John Stultz9a7adcf2011-01-11 09:54:33 -0800489 */
Mao Jinlongea47aaf2017-10-27 11:15:55 +0800490static enum alarmtimer_type clock2alarm(clockid_t clockid)
John Stultz9a7adcf2011-01-11 09:54:33 -0800491{
492 if (clockid == CLOCK_REALTIME_ALARM)
493 return ALARM_REALTIME;
494 if (clockid == CLOCK_BOOTTIME_ALARM)
495 return ALARM_BOOTTIME;
496 return -1;
497}
498
John Stultz180bf812011-04-28 12:58:11 -0700499/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800500 * alarm_handle_timer - Callback for posix timers
501 * @alarm: alarm that fired
502 *
503 * Posix timer callback for expired alarm timers.
504 */
John Stultz4b413082011-08-10 10:37:59 -0700505static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
506 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800507{
Richard Larocque474e9412014-09-09 18:31:05 -0700508 unsigned long flags;
John Stultz9a7adcf2011-01-11 09:54:33 -0800509 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
John Stultz9e264762011-08-10 12:09:24 -0700510 it.alarm.alarmtimer);
Richard Larocque474e9412014-09-09 18:31:05 -0700511 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
512
513 spin_lock_irqsave(&ptr->it_lock, flags);
Richard Larocque265b81d2014-09-09 18:31:04 -0700514 if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) {
515 if (posix_timer_event(ptr, 0) != 0)
516 ptr->it_overrun++;
517 }
John Stultz4b413082011-08-10 10:37:59 -0700518
John Stultz54da23b2011-08-10 11:08:07 -0700519 /* Re-add periodic timers */
John Stultz9e264762011-08-10 12:09:24 -0700520 if (ptr->it.alarm.interval.tv64) {
521 ptr->it_overrun += alarm_forward(alarm, now,
522 ptr->it.alarm.interval);
Richard Larocque474e9412014-09-09 18:31:05 -0700523 result = ALARMTIMER_RESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700524 }
Richard Larocque474e9412014-09-09 18:31:05 -0700525 spin_unlock_irqrestore(&ptr->it_lock, flags);
526
527 return result;
John Stultz9a7adcf2011-01-11 09:54:33 -0800528}
529
John Stultz180bf812011-04-28 12:58:11 -0700530/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800531 * alarm_clock_getres - posix getres interface
532 * @which_clock: clockid
533 * @tp: timespec to fill
534 *
535 * Returns the granularity of underlying alarm base clock
536 */
537static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
538{
John Stultz1c6b39a2011-06-16 18:47:37 -0700539 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400540 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700541
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000542 tp->tv_sec = 0;
543 tp->tv_nsec = hrtimer_resolution;
544 return 0;
John Stultz9a7adcf2011-01-11 09:54:33 -0800545}
546
547/**
548 * alarm_clock_get - posix clock_get interface
549 * @which_clock: clockid
550 * @tp: timespec to fill.
551 *
552 * Provides the underlying alarm base time.
553 */
554static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
555{
556 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
557
John Stultz1c6b39a2011-06-16 18:47:37 -0700558 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400559 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700560
John Stultz9a7adcf2011-01-11 09:54:33 -0800561 *tp = ktime_to_timespec(base->gettime());
562 return 0;
563}
564
565/**
566 * alarm_timer_create - posix timer_create interface
567 * @new_timer: k_itimer pointer to manage
568 *
569 * Initializes the k_itimer structure.
570 */
571static int alarm_timer_create(struct k_itimer *new_timer)
572{
573 enum alarmtimer_type type;
John Stultz9a7adcf2011-01-11 09:54:33 -0800574
John Stultz1c6b39a2011-06-16 18:47:37 -0700575 if (!alarmtimer_get_rtcdev())
Thadeu Lima de Souza Cascardo65b7a5a2019-09-03 14:18:02 -0300576 return -EOPNOTSUPP;
John Stultz1c6b39a2011-06-16 18:47:37 -0700577
John Stultz9a7adcf2011-01-11 09:54:33 -0800578 if (!capable(CAP_WAKE_ALARM))
579 return -EPERM;
580
581 type = clock2alarm(new_timer->it_clock);
John Stultz9e264762011-08-10 12:09:24 -0700582 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf2011-01-11 09:54:33 -0800583 return 0;
584}
585
586/**
587 * alarm_timer_get - posix timer_get interface
588 * @new_timer: k_itimer pointer
589 * @cur_setting: itimerspec data to fill
590 *
Richard Larocquee86fea72014-09-09 18:31:03 -0700591 * Copies out the current itimerspec data
John Stultz9a7adcf2011-01-11 09:54:33 -0800592 */
593static void alarm_timer_get(struct k_itimer *timr,
594 struct itimerspec *cur_setting)
595{
Richard Larocquee86fea72014-09-09 18:31:03 -0700596 ktime_t relative_expiry_time =
597 alarm_expires_remaining(&(timr->it.alarm.alarmtimer));
John Stultzea7802f2011-08-04 07:51:56 -0700598
Richard Larocquee86fea72014-09-09 18:31:03 -0700599 if (ktime_to_ns(relative_expiry_time) > 0) {
600 cur_setting->it_value = ktime_to_timespec(relative_expiry_time);
601 } else {
602 cur_setting->it_value.tv_sec = 0;
603 cur_setting->it_value.tv_nsec = 0;
604 }
605
606 cur_setting->it_interval = ktime_to_timespec(timr->it.alarm.interval);
John Stultz9a7adcf2011-01-11 09:54:33 -0800607}
608
609/**
610 * alarm_timer_del - posix timer_del interface
611 * @timr: k_itimer pointer to be deleted
612 *
613 * Cancels any programmed alarms for the given timer.
614 */
615static int alarm_timer_del(struct k_itimer *timr)
616{
John Stultz1c6b39a2011-06-16 18:47:37 -0700617 if (!rtcdev)
Petr Vorel07abe8c2019-11-08 16:50:50 +0100618 return -EOPNOTSUPP;
John Stultz1c6b39a2011-06-16 18:47:37 -0700619
John Stultz9082c462011-08-10 12:41:36 -0700620 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
621 return TIMER_RETRY;
622
John Stultz9a7adcf2011-01-11 09:54:33 -0800623 return 0;
624}
625
626/**
627 * alarm_timer_set - posix timer_set interface
628 * @timr: k_itimer pointer to be deleted
629 * @flags: timer flags
630 * @new_setting: itimerspec to be used
631 * @old_setting: itimerspec being replaced
632 *
633 * Sets the timer to new_setting, and starts the timer.
634 */
635static int alarm_timer_set(struct k_itimer *timr, int flags,
636 struct itimerspec *new_setting,
637 struct itimerspec *old_setting)
638{
John Stultz16927772014-07-07 14:06:11 -0700639 ktime_t exp;
640
John Stultz1c6b39a2011-06-16 18:47:37 -0700641 if (!rtcdev)
Petr Vorel07abe8c2019-11-08 16:50:50 +0100642 return -EOPNOTSUPP;
John Stultz1c6b39a2011-06-16 18:47:37 -0700643
John Stultz16927772014-07-07 14:06:11 -0700644 if (flags & ~TIMER_ABSTIME)
645 return -EINVAL;
646
John Stultz971c90b2011-08-04 07:25:35 -0700647 if (old_setting)
648 alarm_timer_get(timr, old_setting);
John Stultz9a7adcf2011-01-11 09:54:33 -0800649
650 /* If the timer was already set, cancel it */
John Stultz9082c462011-08-10 12:41:36 -0700651 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
652 return TIMER_RETRY;
John Stultz9a7adcf2011-01-11 09:54:33 -0800653
654 /* start the timer */
John Stultz9e264762011-08-10 12:09:24 -0700655 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
Thomas Gleixner04651042017-05-30 23:15:35 +0200656
657 /*
658 * Rate limit to the tick as a hot fix to prevent DOS. Will be
659 * mopped up later.
660 */
Greg Hackmann91af5f02017-07-24 10:19:24 -0700661 if (timr->it.alarm.interval.tv64 &&
662 ktime_to_ns(timr->it.alarm.interval) < TICK_NSEC)
Thomas Gleixner04651042017-05-30 23:15:35 +0200663 timr->it.alarm.interval = ktime_set(0, TICK_NSEC);
664
John Stultz16927772014-07-07 14:06:11 -0700665 exp = timespec_to_ktime(new_setting->it_value);
666 /* Convert (if necessary) to absolute time */
667 if (flags != TIMER_ABSTIME) {
668 ktime_t now;
669
670 now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
Thomas Gleixner8ee7f062017-05-30 23:15:34 +0200671 exp = ktime_add_safe(now, exp);
John Stultz16927772014-07-07 14:06:11 -0700672 }
673
674 alarm_start(&timr->it.alarm.alarmtimer, exp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800675 return 0;
676}
677
678/**
679 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
680 * @alarm: ptr to alarm that fired
681 *
682 * Wakes up the task that set the alarmtimer
683 */
John Stultz4b413082011-08-10 10:37:59 -0700684static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
685 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800686{
687 struct task_struct *task = (struct task_struct *)alarm->data;
688
689 alarm->data = NULL;
690 if (task)
691 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700692 return ALARMTIMER_NORESTART;
John Stultz9a7adcf2011-01-11 09:54:33 -0800693}
694
695/**
696 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
697 * @alarm: ptr to alarmtimer
698 * @absexp: absolute expiration time
699 *
700 * Sets the alarm timer and sleeps until it is fired or interrupted.
701 */
702static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
703{
704 alarm->data = (void *)current;
705 do {
706 set_current_state(TASK_INTERRUPTIBLE);
John Stultz9e264762011-08-10 12:09:24 -0700707 alarm_start(alarm, absexp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800708 if (likely(alarm->data))
709 schedule();
710
711 alarm_cancel(alarm);
712 } while (alarm->data && !signal_pending(current));
713
714 __set_current_state(TASK_RUNNING);
715
716 return (alarm->data == NULL);
717}
718
719
720/**
721 * update_rmtp - Update remaining timespec value
722 * @exp: expiration time
723 * @type: timer type
724 * @rmtp: user pointer to remaining timepsec value
725 *
726 * Helper function that fills in rmtp value with time between
727 * now and the exp value
728 */
729static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
730 struct timespec __user *rmtp)
731{
732 struct timespec rmt;
733 ktime_t rem;
734
735 rem = ktime_sub(exp, alarm_bases[type].gettime());
736
737 if (rem.tv64 <= 0)
738 return 0;
739 rmt = ktime_to_timespec(rem);
740
741 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
742 return -EFAULT;
743
744 return 1;
745
746}
747
748/**
749 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
750 * @restart: ptr to restart block
751 *
752 * Handles restarted clock_nanosleep calls
753 */
754static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
755{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200756 enum alarmtimer_type type = restart->nanosleep.clockid;
John Stultz9a7adcf2011-01-11 09:54:33 -0800757 ktime_t exp;
758 struct timespec __user *rmtp;
759 struct alarm alarm;
760 int ret = 0;
761
762 exp.tv64 = restart->nanosleep.expires;
763 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
764
765 if (alarmtimer_do_nsleep(&alarm, exp))
766 goto out;
767
768 if (freezing(current))
769 alarmtimer_freezerset(exp, type);
770
771 rmtp = restart->nanosleep.rmtp;
772 if (rmtp) {
773 ret = update_rmtp(exp, type, rmtp);
774 if (ret <= 0)
775 goto out;
776 }
777
778
779 /* The other values in restart are already filled in */
780 ret = -ERESTART_RESTARTBLOCK;
781out:
782 return ret;
783}
784
785/**
786 * alarm_timer_nsleep - alarmtimer nanosleep
787 * @which_clock: clockid
788 * @flags: determins abstime or relative
789 * @tsreq: requested sleep time (abs or rel)
790 * @rmtp: remaining sleep time saved
791 *
792 * Handles clock_nanosleep calls against _ALARM clockids
793 */
794static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
795 struct timespec *tsreq, struct timespec __user *rmtp)
796{
797 enum alarmtimer_type type = clock2alarm(which_clock);
798 struct alarm alarm;
799 ktime_t exp;
800 int ret = 0;
801 struct restart_block *restart;
802
John Stultz1c6b39a2011-06-16 18:47:37 -0700803 if (!alarmtimer_get_rtcdev())
Thadeu Lima de Souza Cascardo65b7a5a2019-09-03 14:18:02 -0300804 return -EOPNOTSUPP;
John Stultz1c6b39a2011-06-16 18:47:37 -0700805
John Stultz16927772014-07-07 14:06:11 -0700806 if (flags & ~TIMER_ABSTIME)
807 return -EINVAL;
808
John Stultz9a7adcf2011-01-11 09:54:33 -0800809 if (!capable(CAP_WAKE_ALARM))
810 return -EPERM;
811
812 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
813
814 exp = timespec_to_ktime(*tsreq);
815 /* Convert (if necessary) to absolute time */
816 if (flags != TIMER_ABSTIME) {
817 ktime_t now = alarm_bases[type].gettime();
Thomas Gleixner747128e2018-07-02 09:34:29 +0200818
819 exp = ktime_add_safe(now, exp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800820 }
821
822 if (alarmtimer_do_nsleep(&alarm, exp))
823 goto out;
824
825 if (freezing(current))
826 alarmtimer_freezerset(exp, type);
827
828 /* abs timers don't set remaining time or restart */
829 if (flags == TIMER_ABSTIME) {
830 ret = -ERESTARTNOHAND;
831 goto out;
832 }
833
834 if (rmtp) {
835 ret = update_rmtp(exp, type, rmtp);
836 if (ret <= 0)
837 goto out;
838 }
839
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800840 restart = &current->restart_block;
John Stultz9a7adcf2011-01-11 09:54:33 -0800841 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200842 restart->nanosleep.clockid = type;
John Stultz9a7adcf2011-01-11 09:54:33 -0800843 restart->nanosleep.expires = exp.tv64;
844 restart->nanosleep.rmtp = rmtp;
845 ret = -ERESTART_RESTARTBLOCK;
846
847out:
848 return ret;
849}
John Stultzff3ead92011-01-11 09:42:13 -0800850
John Stultzff3ead92011-01-11 09:42:13 -0800851
852/* Suspend hook structures */
853static const struct dev_pm_ops alarmtimer_pm_ops = {
854 .suspend = alarmtimer_suspend,
zhuo-haoa0e32132015-11-17 20:08:07 +0800855 .resume = alarmtimer_resume,
John Stultzff3ead92011-01-11 09:42:13 -0800856};
857
858static struct platform_driver alarmtimer_driver = {
859 .driver = {
860 .name = "alarmtimer",
861 .pm = &alarmtimer_pm_ops,
862 }
863};
864
865/**
866 * alarmtimer_init - Initialize alarm timer code
867 *
868 * This function initializes the alarm bases and registers
869 * the posix clock ids.
870 */
871static int __init alarmtimer_init(void)
872{
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200873 struct platform_device *pdev;
John Stultzff3ead92011-01-11 09:42:13 -0800874 int error = 0;
875 int i;
John Stultz9a7adcf2011-01-11 09:54:33 -0800876 struct k_clock alarm_clock = {
877 .clock_getres = alarm_clock_getres,
878 .clock_get = alarm_clock_get,
879 .timer_create = alarm_timer_create,
880 .timer_set = alarm_timer_set,
881 .timer_del = alarm_timer_del,
882 .timer_get = alarm_timer_get,
883 .nsleep = alarm_timer_nsleep,
884 };
885
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100886 alarmtimer_rtc_timer_init();
John Stultzad30dfa2012-03-23 15:52:25 -0700887
John Stultz9a7adcf2011-01-11 09:54:33 -0800888 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
889 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
John Stultzff3ead92011-01-11 09:42:13 -0800890
891 /* Initialize alarm bases */
892 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
893 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
894 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
895 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
896 for (i = 0; i < ALARM_NUMTYPE; i++) {
897 timerqueue_init_head(&alarm_bases[i].timerqueue);
898 spin_lock_init(&alarm_bases[i].lock);
John Stultzff3ead92011-01-11 09:42:13 -0800899 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700900
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200901 error = alarmtimer_rtc_interface_setup();
902 if (error)
903 return error;
John Stultzff3ead92011-01-11 09:42:13 -0800904
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200905 error = platform_driver_register(&alarmtimer_driver);
906 if (error)
907 goto out_if;
908
909 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
910 if (IS_ERR(pdev)) {
911 error = PTR_ERR(pdev);
912 goto out_drv;
913 }
Todd Poynor59a93c22012-08-09 00:37:27 -0700914 ws = wakeup_source_register("alarmtimer");
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800915 return 0;
Mao Jinlongea47aaf2017-10-27 11:15:55 +0800916
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200917out_drv:
918 platform_driver_unregister(&alarmtimer_driver);
919out_if:
920 alarmtimer_rtc_interface_remove();
John Stultzff3ead92011-01-11 09:42:13 -0800921 return error;
922}
923device_initcall(alarmtimer_init);