blob: 04939053c823aadcdc9fa4ac53a8f775147220f6 [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010022#include <linux/sched/signal.h>
John Stultzff3ead92011-01-11 09:42:13 -080023#include <linux/alarmtimer.h>
24#include <linux/mutex.h>
25#include <linux/platform_device.h>
26#include <linux/posix-timers.h>
27#include <linux/workqueue.h>
28#include <linux/freezer.h>
29
Baolin Wang4a057542016-11-28 14:35:21 -080030#define CREATE_TRACE_POINTS
31#include <trace/events/alarmtimer.h>
32
John Stultz180bf812011-04-28 12:58:11 -070033/**
34 * struct alarm_base - Alarm timer bases
35 * @lock: Lock for syncrhonized access to the base
36 * @timerqueue: Timerqueue head managing the list of events
John Stultz180bf812011-04-28 12:58:11 -070037 * @gettime: Function to read the time correlating to the base
38 * @base_clockid: clockid for the base
John Stultz180bf812011-04-28 12:58:11 -070039 */
John Stultzff3ead92011-01-11 09:42:13 -080040static struct alarm_base {
41 spinlock_t lock;
42 struct timerqueue_head timerqueue;
John Stultzff3ead92011-01-11 09:42:13 -080043 ktime_t (*gettime)(void);
44 clockid_t base_clockid;
John Stultzff3ead92011-01-11 09:42:13 -080045} alarm_bases[ALARM_NUMTYPE];
46
Baolin Wang4a057542016-11-28 14:35:21 -080047/* freezer information to handle clock_nanosleep triggered wakeups */
48static enum alarmtimer_type freezer_alarmtype;
49static ktime_t freezer_expires;
John Stultzc008ba582011-06-16 18:27:09 -070050static ktime_t freezer_delta;
51static DEFINE_SPINLOCK(freezer_delta_lock);
52
Todd Poynor59a93c22012-08-09 00:37:27 -070053static struct wakeup_source *ws;
54
John Stultz472647d2011-04-29 15:03:10 -070055#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -070056/* rtc timer and device for setting alarm wakeups at suspend */
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +010057static struct rtc_timer rtctimer;
John Stultzff3ead92011-01-11 09:42:13 -080058static struct rtc_device *rtcdev;
John Stultzc008ba582011-06-16 18:27:09 -070059static DEFINE_SPINLOCK(rtcdev_lock);
John Stultzff3ead92011-01-11 09:42:13 -080060
John Stultzc008ba582011-06-16 18:27:09 -070061/**
John Stultzc008ba582011-06-16 18:27:09 -070062 * alarmtimer_get_rtcdev - Return selected rtcdevice
63 *
64 * This function returns the rtc device to use for wakealarms.
65 * If one has not already been chosen, it checks to see if a
66 * functional rtc device is available.
67 */
John Stultz57c498f2012-04-20 12:31:45 -070068struct rtc_device *alarmtimer_get_rtcdev(void)
John Stultzc008ba582011-06-16 18:27:09 -070069{
John Stultzc008ba582011-06-16 18:27:09 -070070 unsigned long flags;
71 struct rtc_device *ret;
72
73 spin_lock_irqsave(&rtcdev_lock, flags);
John Stultzc008ba582011-06-16 18:27:09 -070074 ret = rtcdev;
75 spin_unlock_irqrestore(&rtcdev_lock, flags);
76
77 return ret;
78}
Pramod Gurav71d5d2b2014-06-13 11:49:42 +053079EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
John Stultz8bc0daf2011-07-14 18:35:13 -070080
81static int alarmtimer_rtc_add_device(struct device *dev,
82 struct class_interface *class_intf)
83{
84 unsigned long flags;
85 struct rtc_device *rtc = to_rtc_device(dev);
86
87 if (rtcdev)
88 return -EBUSY;
89
90 if (!rtc->ops->set_alarm)
91 return -1;
92 if (!device_may_wakeup(rtc->dev.parent))
93 return -1;
94
95 spin_lock_irqsave(&rtcdev_lock, flags);
96 if (!rtcdev) {
97 rtcdev = rtc;
98 /* hold a reference so it doesn't go away */
99 get_device(dev);
100 }
101 spin_unlock_irqrestore(&rtcdev_lock, flags);
102 return 0;
103}
104
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100105static inline void alarmtimer_rtc_timer_init(void)
106{
107 rtc_timer_init(&rtctimer, NULL, NULL);
108}
109
John Stultz8bc0daf2011-07-14 18:35:13 -0700110static struct class_interface alarmtimer_rtc_interface = {
111 .add_dev = &alarmtimer_rtc_add_device,
112};
113
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200114static int alarmtimer_rtc_interface_setup(void)
John Stultz8bc0daf2011-07-14 18:35:13 -0700115{
116 alarmtimer_rtc_interface.class = rtc_class;
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200117 return class_interface_register(&alarmtimer_rtc_interface);
118}
119static void alarmtimer_rtc_interface_remove(void)
120{
121 class_interface_unregister(&alarmtimer_rtc_interface);
John Stultz8bc0daf2011-07-14 18:35:13 -0700122}
John Stultz1c6b39a2011-06-16 18:47:37 -0700123#else
John Stultz57c498f2012-04-20 12:31:45 -0700124struct rtc_device *alarmtimer_get_rtcdev(void)
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200125{
126 return NULL;
127}
128#define rtcdev (NULL)
129static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
130static inline void alarmtimer_rtc_interface_remove(void) { }
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100131static inline void alarmtimer_rtc_timer_init(void) { }
John Stultzc008ba582011-06-16 18:27:09 -0700132#endif
John Stultzff3ead92011-01-11 09:42:13 -0800133
John Stultz180bf812011-04-28 12:58:11 -0700134/**
John Stultzff3ead92011-01-11 09:42:13 -0800135 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
136 * @base: pointer to the base where the timer is being run
137 * @alarm: pointer to alarm being enqueued.
138 *
John Stultzdae373b2012-09-13 19:12:16 -0400139 * Adds alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800140 *
141 * Must hold base->lock when calling.
142 */
143static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
144{
John Stultzdae373b2012-09-13 19:12:16 -0400145 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
146 timerqueue_del(&base->timerqueue, &alarm->node);
147
John Stultzff3ead92011-01-11 09:42:13 -0800148 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700149 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800150}
151
John Stultz180bf812011-04-28 12:58:11 -0700152/**
John Stultza65bcc12012-09-13 19:25:22 -0400153 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800154 * @base: pointer to the base where the timer is running
155 * @alarm: pointer to alarm being removed
156 *
John Stultzdae373b2012-09-13 19:12:16 -0400157 * Removes alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800158 *
159 * Must hold base->lock when calling.
160 */
John Stultza65bcc12012-09-13 19:25:22 -0400161static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800162{
John Stultza28cde82011-08-10 12:30:21 -0700163 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
164 return;
165
John Stultzff3ead92011-01-11 09:42:13 -0800166 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700167 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800168}
169
John Stultz7068b7a2011-04-28 13:29:18 -0700170
John Stultz180bf812011-04-28 12:58:11 -0700171/**
John Stultz7068b7a2011-04-28 13:29:18 -0700172 * alarmtimer_fired - Handles alarm hrtimer being fired.
173 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800174 *
John Stultz180bf812011-04-28 12:58:11 -0700175 * When a alarm timer fires, this runs through the timerqueue to
176 * see which alarms expired, and runs those. If there are more alarm
177 * timers queued for the future, we set the hrtimer to fire when
178 * when the next future alarm timer expires.
John Stultzff3ead92011-01-11 09:42:13 -0800179 */
John Stultz7068b7a2011-04-28 13:29:18 -0700180static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800181{
John Stultzdae373b2012-09-13 19:12:16 -0400182 struct alarm *alarm = container_of(timer, struct alarm, timer);
183 struct alarm_base *base = &alarm_bases[alarm->type];
John Stultzff3ead92011-01-11 09:42:13 -0800184 unsigned long flags;
John Stultz7068b7a2011-04-28 13:29:18 -0700185 int ret = HRTIMER_NORESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700186 int restart = ALARMTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800187
188 spin_lock_irqsave(&base->lock, flags);
John Stultza65bcc12012-09-13 19:25:22 -0400189 alarmtimer_dequeue(base, alarm);
John Stultzdae373b2012-09-13 19:12:16 -0400190 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800191
John Stultzdae373b2012-09-13 19:12:16 -0400192 if (alarm->function)
193 restart = alarm->function(alarm, base->gettime());
John Stultzff3ead92011-01-11 09:42:13 -0800194
John Stultzdae373b2012-09-13 19:12:16 -0400195 spin_lock_irqsave(&base->lock, flags);
196 if (restart != ALARMTIMER_NORESTART) {
197 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
198 alarmtimer_enqueue(base, alarm);
John Stultz7068b7a2011-04-28 13:29:18 -0700199 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800200 }
201 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800202
Baolin Wang4a057542016-11-28 14:35:21 -0800203 trace_alarmtimer_fired(alarm, base->gettime());
John Stultz7068b7a2011-04-28 13:29:18 -0700204 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800205
John Stultzff3ead92011-01-11 09:42:13 -0800206}
207
Todd Poynor6cffe002013-05-15 14:38:11 -0700208ktime_t alarm_expires_remaining(const struct alarm *alarm)
209{
210 struct alarm_base *base = &alarm_bases[alarm->type];
211 return ktime_sub(alarm->node.expires, base->gettime());
212}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200213EXPORT_SYMBOL_GPL(alarm_expires_remaining);
Todd Poynor6cffe002013-05-15 14:38:11 -0700214
John Stultz472647d2011-04-29 15:03:10 -0700215#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700216/**
John Stultzff3ead92011-01-11 09:42:13 -0800217 * alarmtimer_suspend - Suspend time callback
218 * @dev: unused
219 * @state: unused
220 *
221 * When we are going into suspend, we look through the bases
222 * to see which is the soonest timer to expire. We then
223 * set an rtc timer to fire that far into the future, which
224 * will wake us from suspend.
225 */
226static int alarmtimer_suspend(struct device *dev)
227{
Baolin Wang4a057542016-11-28 14:35:21 -0800228 ktime_t min, now, expires;
229 int i, ret, type;
John Stultzc008ba582011-06-16 18:27:09 -0700230 struct rtc_device *rtc;
Baolin Wang4a057542016-11-28 14:35:21 -0800231 unsigned long flags;
232 struct rtc_time tm;
John Stultzff3ead92011-01-11 09:42:13 -0800233
234 spin_lock_irqsave(&freezer_delta_lock, flags);
235 min = freezer_delta;
Baolin Wang4a057542016-11-28 14:35:21 -0800236 expires = freezer_expires;
237 type = freezer_alarmtype;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100238 freezer_delta = 0;
John Stultzff3ead92011-01-11 09:42:13 -0800239 spin_unlock_irqrestore(&freezer_delta_lock, flags);
240
John Stultz8bc0daf2011-07-14 18:35:13 -0700241 rtc = alarmtimer_get_rtcdev();
John Stultzff3ead92011-01-11 09:42:13 -0800242 /* If we have no rtcdev, just return */
John Stultzc008ba582011-06-16 18:27:09 -0700243 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800244 return 0;
245
246 /* Find the soonest timer to expire*/
247 for (i = 0; i < ALARM_NUMTYPE; i++) {
248 struct alarm_base *base = &alarm_bases[i];
249 struct timerqueue_node *next;
250 ktime_t delta;
251
252 spin_lock_irqsave(&base->lock, flags);
253 next = timerqueue_getnext(&base->timerqueue);
254 spin_unlock_irqrestore(&base->lock, flags);
255 if (!next)
256 continue;
257 delta = ktime_sub(next->expires, base->gettime());
Thomas Gleixner2456e852016-12-25 11:38:40 +0100258 if (!min || (delta < min)) {
Baolin Wang4a057542016-11-28 14:35:21 -0800259 expires = next->expires;
John Stultzff3ead92011-01-11 09:42:13 -0800260 min = delta;
Baolin Wang4a057542016-11-28 14:35:21 -0800261 type = i;
262 }
John Stultzff3ead92011-01-11 09:42:13 -0800263 }
Thomas Gleixner2456e852016-12-25 11:38:40 +0100264 if (min == 0)
John Stultzff3ead92011-01-11 09:42:13 -0800265 return 0;
266
Todd Poynor59a93c22012-08-09 00:37:27 -0700267 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
268 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
269 return -EBUSY;
270 }
John Stultzff3ead92011-01-11 09:42:13 -0800271
Baolin Wang4a057542016-11-28 14:35:21 -0800272 trace_alarmtimer_suspend(expires, type);
273
John Stultzff3ead92011-01-11 09:42:13 -0800274 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba582011-06-16 18:27:09 -0700275 rtc_timer_cancel(rtc, &rtctimer);
276 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800277 now = rtc_tm_to_ktime(tm);
278 now = ktime_add(now, min);
279
Todd Poynor59a93c22012-08-09 00:37:27 -0700280 /* Set alarm, if in the past reject suspend briefly to handle */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100281 ret = rtc_timer_start(rtc, &rtctimer, now, 0);
Todd Poynor59a93c22012-08-09 00:37:27 -0700282 if (ret < 0)
283 __pm_wakeup_event(ws, MSEC_PER_SEC);
284 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800285}
zhuo-haoa0e32132015-11-17 20:08:07 +0800286
287static int alarmtimer_resume(struct device *dev)
288{
289 struct rtc_device *rtc;
290
291 rtc = alarmtimer_get_rtcdev();
292 if (rtc)
293 rtc_timer_cancel(rtc, &rtctimer);
294 return 0;
295}
296
John Stultz472647d2011-04-29 15:03:10 -0700297#else
298static int alarmtimer_suspend(struct device *dev)
299{
300 return 0;
301}
zhuo-haoa0e32132015-11-17 20:08:07 +0800302
303static int alarmtimer_resume(struct device *dev)
304{
305 return 0;
306}
John Stultz472647d2011-04-29 15:03:10 -0700307#endif
John Stultzff3ead92011-01-11 09:42:13 -0800308
John Stultz9a7adcf52011-01-11 09:54:33 -0800309static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
310{
Baolin Wang4a057542016-11-28 14:35:21 -0800311 struct alarm_base *base;
John Stultz9a7adcf52011-01-11 09:54:33 -0800312 unsigned long flags;
Baolin Wang4a057542016-11-28 14:35:21 -0800313 ktime_t delta;
314
315 switch(type) {
316 case ALARM_REALTIME:
317 base = &alarm_bases[ALARM_REALTIME];
318 type = ALARM_REALTIME_FREEZER;
319 break;
320 case ALARM_BOOTTIME:
321 base = &alarm_bases[ALARM_BOOTTIME];
322 type = ALARM_BOOTTIME_FREEZER;
323 break;
324 default:
325 WARN_ONCE(1, "Invalid alarm type: %d\n", type);
326 return;
327 }
John Stultz9a7adcf52011-01-11 09:54:33 -0800328
329 delta = ktime_sub(absexp, base->gettime());
330
331 spin_lock_irqsave(&freezer_delta_lock, flags);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100332 if (!freezer_delta || (delta < freezer_delta)) {
John Stultz9a7adcf52011-01-11 09:54:33 -0800333 freezer_delta = delta;
Baolin Wang4a057542016-11-28 14:35:21 -0800334 freezer_expires = absexp;
335 freezer_alarmtype = type;
336 }
John Stultz9a7adcf52011-01-11 09:54:33 -0800337 spin_unlock_irqrestore(&freezer_delta_lock, flags);
338}
339
340
John Stultz180bf812011-04-28 12:58:11 -0700341/**
John Stultzff3ead92011-01-11 09:42:13 -0800342 * alarm_init - Initialize an alarm structure
343 * @alarm: ptr to alarm to be initialized
344 * @type: the type of the alarm
345 * @function: callback that is run when the alarm fires
John Stultzff3ead92011-01-11 09:42:13 -0800346 */
347void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
John Stultz4b413082011-08-10 10:37:59 -0700348 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
John Stultzff3ead92011-01-11 09:42:13 -0800349{
350 timerqueue_init(&alarm->node);
John Stultzdae373b2012-09-13 19:12:16 -0400351 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
352 HRTIMER_MODE_ABS);
353 alarm->timer.function = alarmtimer_fired;
John Stultzff3ead92011-01-11 09:42:13 -0800354 alarm->function = function;
355 alarm->type = type;
John Stultza28cde82011-08-10 12:30:21 -0700356 alarm->state = ALARMTIMER_STATE_INACTIVE;
John Stultzff3ead92011-01-11 09:42:13 -0800357}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200358EXPORT_SYMBOL_GPL(alarm_init);
John Stultzff3ead92011-01-11 09:42:13 -0800359
John Stultz180bf812011-04-28 12:58:11 -0700360/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700361 * alarm_start - Sets an absolute alarm to fire
John Stultzff3ead92011-01-11 09:42:13 -0800362 * @alarm: ptr to alarm to set
363 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800364 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000365void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800366{
367 struct alarm_base *base = &alarm_bases[alarm->type];
368 unsigned long flags;
369
370 spin_lock_irqsave(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800371 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800372 alarmtimer_enqueue(base, alarm);
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000373 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
John Stultzff3ead92011-01-11 09:42:13 -0800374 spin_unlock_irqrestore(&base->lock, flags);
Baolin Wang4a057542016-11-28 14:35:21 -0800375
376 trace_alarmtimer_start(alarm, base->gettime());
John Stultzff3ead92011-01-11 09:42:13 -0800377}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200378EXPORT_SYMBOL_GPL(alarm_start);
John Stultzff3ead92011-01-11 09:42:13 -0800379
John Stultz180bf812011-04-28 12:58:11 -0700380/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700381 * alarm_start_relative - Sets a relative alarm to fire
382 * @alarm: ptr to alarm to set
383 * @start: time relative to now to run the alarm
384 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000385void alarm_start_relative(struct alarm *alarm, ktime_t start)
Todd Poynor6cffe002013-05-15 14:38:11 -0700386{
387 struct alarm_base *base = &alarm_bases[alarm->type];
388
389 start = ktime_add(start, base->gettime());
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000390 alarm_start(alarm, start);
Todd Poynor6cffe002013-05-15 14:38:11 -0700391}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200392EXPORT_SYMBOL_GPL(alarm_start_relative);
Todd Poynor6cffe002013-05-15 14:38:11 -0700393
394void alarm_restart(struct alarm *alarm)
395{
396 struct alarm_base *base = &alarm_bases[alarm->type];
397 unsigned long flags;
398
399 spin_lock_irqsave(&base->lock, flags);
400 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
401 hrtimer_restart(&alarm->timer);
402 alarmtimer_enqueue(base, alarm);
403 spin_unlock_irqrestore(&base->lock, flags);
404}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200405EXPORT_SYMBOL_GPL(alarm_restart);
Todd Poynor6cffe002013-05-15 14:38:11 -0700406
407/**
John Stultz9082c462011-08-10 12:41:36 -0700408 * alarm_try_to_cancel - Tries to cancel an alarm timer
John Stultzff3ead92011-01-11 09:42:13 -0800409 * @alarm: ptr to alarm to be canceled
John Stultz9082c462011-08-10 12:41:36 -0700410 *
411 * Returns 1 if the timer was canceled, 0 if it was not running,
412 * and -1 if the callback was running
John Stultzff3ead92011-01-11 09:42:13 -0800413 */
John Stultz9082c462011-08-10 12:41:36 -0700414int alarm_try_to_cancel(struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800415{
416 struct alarm_base *base = &alarm_bases[alarm->type];
417 unsigned long flags;
John Stultzdae373b2012-09-13 19:12:16 -0400418 int ret;
419
John Stultzff3ead92011-01-11 09:42:13 -0800420 spin_lock_irqsave(&base->lock, flags);
John Stultzdae373b2012-09-13 19:12:16 -0400421 ret = hrtimer_try_to_cancel(&alarm->timer);
422 if (ret >= 0)
John Stultza65bcc12012-09-13 19:25:22 -0400423 alarmtimer_dequeue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800424 spin_unlock_irqrestore(&base->lock, flags);
Baolin Wang4a057542016-11-28 14:35:21 -0800425
426 trace_alarmtimer_cancel(alarm, base->gettime());
John Stultz9082c462011-08-10 12:41:36 -0700427 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800428}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200429EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
John Stultzff3ead92011-01-11 09:42:13 -0800430
431
John Stultz9082c462011-08-10 12:41:36 -0700432/**
433 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
434 * @alarm: ptr to alarm to be canceled
435 *
436 * Returns 1 if the timer was canceled, 0 if it was not active.
437 */
438int alarm_cancel(struct alarm *alarm)
439{
440 for (;;) {
441 int ret = alarm_try_to_cancel(alarm);
442 if (ret >= 0)
443 return ret;
444 cpu_relax();
445 }
446}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200447EXPORT_SYMBOL_GPL(alarm_cancel);
John Stultz9082c462011-08-10 12:41:36 -0700448
John Stultzdce75a82011-08-10 11:31:03 -0700449
450u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
451{
452 u64 overrun = 1;
453 ktime_t delta;
454
455 delta = ktime_sub(now, alarm->node.expires);
456
Thomas Gleixner2456e852016-12-25 11:38:40 +0100457 if (delta < 0)
John Stultzdce75a82011-08-10 11:31:03 -0700458 return 0;
459
Thomas Gleixner2456e852016-12-25 11:38:40 +0100460 if (unlikely(delta >= interval)) {
John Stultzdce75a82011-08-10 11:31:03 -0700461 s64 incr = ktime_to_ns(interval);
462
463 overrun = ktime_divns(delta, incr);
464
465 alarm->node.expires = ktime_add_ns(alarm->node.expires,
466 incr*overrun);
467
Thomas Gleixner2456e852016-12-25 11:38:40 +0100468 if (alarm->node.expires > now)
John Stultzdce75a82011-08-10 11:31:03 -0700469 return overrun;
470 /*
471 * This (and the ktime_add() below) is the
472 * correction for exact:
473 */
474 overrun++;
475 }
476
477 alarm->node.expires = ktime_add(alarm->node.expires, interval);
478 return overrun;
479}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200480EXPORT_SYMBOL_GPL(alarm_forward);
John Stultzdce75a82011-08-10 11:31:03 -0700481
Todd Poynor6cffe002013-05-15 14:38:11 -0700482u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
483{
484 struct alarm_base *base = &alarm_bases[alarm->type];
485
486 return alarm_forward(alarm, base->gettime(), interval);
487}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200488EXPORT_SYMBOL_GPL(alarm_forward_now);
John Stultzdce75a82011-08-10 11:31:03 -0700489
490
John Stultz180bf812011-04-28 12:58:11 -0700491/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800492 * clock2alarm - helper that converts from clockid to alarmtypes
493 * @clockid: clockid.
John Stultz9a7adcf52011-01-11 09:54:33 -0800494 */
495static enum alarmtimer_type clock2alarm(clockid_t clockid)
496{
497 if (clockid == CLOCK_REALTIME_ALARM)
498 return ALARM_REALTIME;
499 if (clockid == CLOCK_BOOTTIME_ALARM)
500 return ALARM_BOOTTIME;
501 return -1;
502}
503
John Stultz180bf812011-04-28 12:58:11 -0700504/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800505 * alarm_handle_timer - Callback for posix timers
506 * @alarm: alarm that fired
507 *
508 * Posix timer callback for expired alarm timers.
509 */
John Stultz4b413082011-08-10 10:37:59 -0700510static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
511 ktime_t now)
John Stultz9a7adcf52011-01-11 09:54:33 -0800512{
Richard Larocque474e941b2014-09-09 18:31:05 -0700513 unsigned long flags;
John Stultz9a7adcf52011-01-11 09:54:33 -0800514 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
John Stultz9e264762011-08-10 12:09:24 -0700515 it.alarm.alarmtimer);
Richard Larocque474e941b2014-09-09 18:31:05 -0700516 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
517
518 spin_lock_irqsave(&ptr->it_lock, flags);
Richard Larocque265b81d2014-09-09 18:31:04 -0700519 if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) {
Nicolas Pitreb6f8a922016-12-14 15:06:13 -0800520 if (IS_ENABLED(CONFIG_POSIX_TIMERS) &&
521 posix_timer_event(ptr, 0) != 0)
Richard Larocque265b81d2014-09-09 18:31:04 -0700522 ptr->it_overrun++;
523 }
John Stultz4b413082011-08-10 10:37:59 -0700524
John Stultz54da23b2011-08-10 11:08:07 -0700525 /* Re-add periodic timers */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100526 if (ptr->it.alarm.interval) {
John Stultz9e264762011-08-10 12:09:24 -0700527 ptr->it_overrun += alarm_forward(alarm, now,
528 ptr->it.alarm.interval);
Richard Larocque474e941b2014-09-09 18:31:05 -0700529 result = ALARMTIMER_RESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700530 }
Richard Larocque474e941b2014-09-09 18:31:05 -0700531 spin_unlock_irqrestore(&ptr->it_lock, flags);
532
533 return result;
John Stultz9a7adcf52011-01-11 09:54:33 -0800534}
535
John Stultz180bf812011-04-28 12:58:11 -0700536/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800537 * alarm_clock_getres - posix getres interface
538 * @which_clock: clockid
539 * @tp: timespec to fill
540 *
541 * Returns the granularity of underlying alarm base clock
542 */
543static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
544{
John Stultz1c6b39a2011-06-16 18:47:37 -0700545 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400546 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700547
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000548 tp->tv_sec = 0;
549 tp->tv_nsec = hrtimer_resolution;
550 return 0;
John Stultz9a7adcf52011-01-11 09:54:33 -0800551}
552
553/**
554 * alarm_clock_get - posix clock_get interface
555 * @which_clock: clockid
556 * @tp: timespec to fill.
557 *
558 * Provides the underlying alarm base time.
559 */
560static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
561{
562 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
563
John Stultz1c6b39a2011-06-16 18:47:37 -0700564 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400565 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700566
John Stultz9a7adcf52011-01-11 09:54:33 -0800567 *tp = ktime_to_timespec(base->gettime());
568 return 0;
569}
570
571/**
572 * alarm_timer_create - posix timer_create interface
573 * @new_timer: k_itimer pointer to manage
574 *
575 * Initializes the k_itimer structure.
576 */
577static int alarm_timer_create(struct k_itimer *new_timer)
578{
579 enum alarmtimer_type type;
John Stultz9a7adcf52011-01-11 09:54:33 -0800580
John Stultz1c6b39a2011-06-16 18:47:37 -0700581 if (!alarmtimer_get_rtcdev())
582 return -ENOTSUPP;
583
John Stultz9a7adcf52011-01-11 09:54:33 -0800584 if (!capable(CAP_WAKE_ALARM))
585 return -EPERM;
586
587 type = clock2alarm(new_timer->it_clock);
John Stultz9e264762011-08-10 12:09:24 -0700588 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf52011-01-11 09:54:33 -0800589 return 0;
590}
591
592/**
593 * alarm_timer_get - posix timer_get interface
594 * @new_timer: k_itimer pointer
595 * @cur_setting: itimerspec data to fill
596 *
Richard Larocquee86fea72014-09-09 18:31:03 -0700597 * Copies out the current itimerspec data
John Stultz9a7adcf52011-01-11 09:54:33 -0800598 */
599static void alarm_timer_get(struct k_itimer *timr,
600 struct itimerspec *cur_setting)
601{
Richard Larocquee86fea72014-09-09 18:31:03 -0700602 ktime_t relative_expiry_time =
603 alarm_expires_remaining(&(timr->it.alarm.alarmtimer));
John Stultzea7802f2011-08-04 07:51:56 -0700604
Richard Larocquee86fea72014-09-09 18:31:03 -0700605 if (ktime_to_ns(relative_expiry_time) > 0) {
606 cur_setting->it_value = ktime_to_timespec(relative_expiry_time);
607 } else {
608 cur_setting->it_value.tv_sec = 0;
609 cur_setting->it_value.tv_nsec = 0;
610 }
611
612 cur_setting->it_interval = ktime_to_timespec(timr->it.alarm.interval);
John Stultz9a7adcf52011-01-11 09:54:33 -0800613}
614
615/**
616 * alarm_timer_del - posix timer_del interface
617 * @timr: k_itimer pointer to be deleted
618 *
619 * Cancels any programmed alarms for the given timer.
620 */
621static int alarm_timer_del(struct k_itimer *timr)
622{
John Stultz1c6b39a2011-06-16 18:47:37 -0700623 if (!rtcdev)
624 return -ENOTSUPP;
625
John Stultz9082c462011-08-10 12:41:36 -0700626 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
627 return TIMER_RETRY;
628
John Stultz9a7adcf52011-01-11 09:54:33 -0800629 return 0;
630}
631
632/**
633 * alarm_timer_set - posix timer_set interface
634 * @timr: k_itimer pointer to be deleted
635 * @flags: timer flags
636 * @new_setting: itimerspec to be used
637 * @old_setting: itimerspec being replaced
638 *
639 * Sets the timer to new_setting, and starts the timer.
640 */
641static int alarm_timer_set(struct k_itimer *timr, int flags,
642 struct itimerspec *new_setting,
643 struct itimerspec *old_setting)
644{
John Stultz16927772014-07-07 14:06:11 -0700645 ktime_t exp;
646
John Stultz1c6b39a2011-06-16 18:47:37 -0700647 if (!rtcdev)
648 return -ENOTSUPP;
649
John Stultz16927772014-07-07 14:06:11 -0700650 if (flags & ~TIMER_ABSTIME)
651 return -EINVAL;
652
John Stultz971c90b2011-08-04 07:25:35 -0700653 if (old_setting)
654 alarm_timer_get(timr, old_setting);
John Stultz9a7adcf52011-01-11 09:54:33 -0800655
656 /* If the timer was already set, cancel it */
John Stultz9082c462011-08-10 12:41:36 -0700657 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
658 return TIMER_RETRY;
John Stultz9a7adcf52011-01-11 09:54:33 -0800659
660 /* start the timer */
John Stultz9e264762011-08-10 12:09:24 -0700661 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
John Stultz16927772014-07-07 14:06:11 -0700662 exp = timespec_to_ktime(new_setting->it_value);
663 /* Convert (if necessary) to absolute time */
664 if (flags != TIMER_ABSTIME) {
665 ktime_t now;
666
667 now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
668 exp = ktime_add(now, exp);
669 }
670
671 alarm_start(&timr->it.alarm.alarmtimer, exp);
John Stultz9a7adcf52011-01-11 09:54:33 -0800672 return 0;
673}
674
675/**
676 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
677 * @alarm: ptr to alarm that fired
678 *
679 * Wakes up the task that set the alarmtimer
680 */
John Stultz4b413082011-08-10 10:37:59 -0700681static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
682 ktime_t now)
John Stultz9a7adcf52011-01-11 09:54:33 -0800683{
684 struct task_struct *task = (struct task_struct *)alarm->data;
685
686 alarm->data = NULL;
687 if (task)
688 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700689 return ALARMTIMER_NORESTART;
John Stultz9a7adcf52011-01-11 09:54:33 -0800690}
691
692/**
693 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
694 * @alarm: ptr to alarmtimer
695 * @absexp: absolute expiration time
696 *
697 * Sets the alarm timer and sleeps until it is fired or interrupted.
698 */
699static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
700{
701 alarm->data = (void *)current;
702 do {
703 set_current_state(TASK_INTERRUPTIBLE);
John Stultz9e264762011-08-10 12:09:24 -0700704 alarm_start(alarm, absexp);
John Stultz9a7adcf52011-01-11 09:54:33 -0800705 if (likely(alarm->data))
706 schedule();
707
708 alarm_cancel(alarm);
709 } while (alarm->data && !signal_pending(current));
710
711 __set_current_state(TASK_RUNNING);
712
713 return (alarm->data == NULL);
714}
715
716
717/**
718 * update_rmtp - Update remaining timespec value
719 * @exp: expiration time
720 * @type: timer type
721 * @rmtp: user pointer to remaining timepsec value
722 *
723 * Helper function that fills in rmtp value with time between
724 * now and the exp value
725 */
726static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
727 struct timespec __user *rmtp)
728{
729 struct timespec rmt;
730 ktime_t rem;
731
732 rem = ktime_sub(exp, alarm_bases[type].gettime());
733
Thomas Gleixner2456e852016-12-25 11:38:40 +0100734 if (rem <= 0)
John Stultz9a7adcf52011-01-11 09:54:33 -0800735 return 0;
736 rmt = ktime_to_timespec(rem);
737
738 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
739 return -EFAULT;
740
741 return 1;
742
743}
744
745/**
746 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
747 * @restart: ptr to restart block
748 *
749 * Handles restarted clock_nanosleep calls
750 */
751static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
752{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200753 enum alarmtimer_type type = restart->nanosleep.clockid;
John Stultz9a7adcf52011-01-11 09:54:33 -0800754 ktime_t exp;
755 struct timespec __user *rmtp;
756 struct alarm alarm;
757 int ret = 0;
758
Thomas Gleixner2456e852016-12-25 11:38:40 +0100759 exp = restart->nanosleep.expires;
John Stultz9a7adcf52011-01-11 09:54:33 -0800760 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
761
762 if (alarmtimer_do_nsleep(&alarm, exp))
763 goto out;
764
765 if (freezing(current))
766 alarmtimer_freezerset(exp, type);
767
768 rmtp = restart->nanosleep.rmtp;
769 if (rmtp) {
770 ret = update_rmtp(exp, type, rmtp);
771 if (ret <= 0)
772 goto out;
773 }
774
775
776 /* The other values in restart are already filled in */
777 ret = -ERESTART_RESTARTBLOCK;
778out:
779 return ret;
780}
781
782/**
783 * alarm_timer_nsleep - alarmtimer nanosleep
784 * @which_clock: clockid
785 * @flags: determins abstime or relative
786 * @tsreq: requested sleep time (abs or rel)
787 * @rmtp: remaining sleep time saved
788 *
789 * Handles clock_nanosleep calls against _ALARM clockids
790 */
791static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
792 struct timespec *tsreq, struct timespec __user *rmtp)
793{
794 enum alarmtimer_type type = clock2alarm(which_clock);
795 struct alarm alarm;
796 ktime_t exp;
797 int ret = 0;
798 struct restart_block *restart;
799
John Stultz1c6b39a2011-06-16 18:47:37 -0700800 if (!alarmtimer_get_rtcdev())
801 return -ENOTSUPP;
802
John Stultz16927772014-07-07 14:06:11 -0700803 if (flags & ~TIMER_ABSTIME)
804 return -EINVAL;
805
John Stultz9a7adcf52011-01-11 09:54:33 -0800806 if (!capable(CAP_WAKE_ALARM))
807 return -EPERM;
808
809 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
810
811 exp = timespec_to_ktime(*tsreq);
812 /* Convert (if necessary) to absolute time */
813 if (flags != TIMER_ABSTIME) {
814 ktime_t now = alarm_bases[type].gettime();
815 exp = ktime_add(now, exp);
816 }
817
818 if (alarmtimer_do_nsleep(&alarm, exp))
819 goto out;
820
821 if (freezing(current))
822 alarmtimer_freezerset(exp, type);
823
824 /* abs timers don't set remaining time or restart */
825 if (flags == TIMER_ABSTIME) {
826 ret = -ERESTARTNOHAND;
827 goto out;
828 }
829
830 if (rmtp) {
831 ret = update_rmtp(exp, type, rmtp);
832 if (ret <= 0)
833 goto out;
834 }
835
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800836 restart = &current->restart_block;
John Stultz9a7adcf52011-01-11 09:54:33 -0800837 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200838 restart->nanosleep.clockid = type;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100839 restart->nanosleep.expires = exp;
John Stultz9a7adcf52011-01-11 09:54:33 -0800840 restart->nanosleep.rmtp = rmtp;
841 ret = -ERESTART_RESTARTBLOCK;
842
843out:
844 return ret;
845}
John Stultzff3ead92011-01-11 09:42:13 -0800846
John Stultzff3ead92011-01-11 09:42:13 -0800847
848/* Suspend hook structures */
849static const struct dev_pm_ops alarmtimer_pm_ops = {
850 .suspend = alarmtimer_suspend,
zhuo-haoa0e32132015-11-17 20:08:07 +0800851 .resume = alarmtimer_resume,
John Stultzff3ead92011-01-11 09:42:13 -0800852};
853
854static struct platform_driver alarmtimer_driver = {
855 .driver = {
856 .name = "alarmtimer",
857 .pm = &alarmtimer_pm_ops,
858 }
859};
860
861/**
862 * alarmtimer_init - Initialize alarm timer code
863 *
864 * This function initializes the alarm bases and registers
865 * the posix clock ids.
866 */
867static int __init alarmtimer_init(void)
868{
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200869 struct platform_device *pdev;
John Stultzff3ead92011-01-11 09:42:13 -0800870 int error = 0;
871 int i;
John Stultz9a7adcf52011-01-11 09:54:33 -0800872 struct k_clock alarm_clock = {
873 .clock_getres = alarm_clock_getres,
874 .clock_get = alarm_clock_get,
875 .timer_create = alarm_timer_create,
876 .timer_set = alarm_timer_set,
877 .timer_del = alarm_timer_del,
878 .timer_get = alarm_timer_get,
879 .nsleep = alarm_timer_nsleep,
880 };
881
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100882 alarmtimer_rtc_timer_init();
John Stultzad30dfa2012-03-23 15:52:25 -0700883
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500884 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
885 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
886 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
887 }
John Stultzff3ead92011-01-11 09:42:13 -0800888
889 /* Initialize alarm bases */
890 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
891 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
892 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
893 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
894 for (i = 0; i < ALARM_NUMTYPE; i++) {
895 timerqueue_init_head(&alarm_bases[i].timerqueue);
896 spin_lock_init(&alarm_bases[i].lock);
John Stultzff3ead92011-01-11 09:42:13 -0800897 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700898
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200899 error = alarmtimer_rtc_interface_setup();
900 if (error)
901 return error;
John Stultzff3ead92011-01-11 09:42:13 -0800902
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200903 error = platform_driver_register(&alarmtimer_driver);
904 if (error)
905 goto out_if;
906
907 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
908 if (IS_ERR(pdev)) {
909 error = PTR_ERR(pdev);
910 goto out_drv;
911 }
Todd Poynor59a93c22012-08-09 00:37:27 -0700912 ws = wakeup_source_register("alarmtimer");
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200913 return 0;
914
915out_drv:
916 platform_driver_unregister(&alarmtimer_driver);
917out_if:
918 alarmtimer_rtc_interface_remove();
John Stultzff3ead92011-01-11 09:42:13 -0800919 return error;
920}
921device_initcall(alarmtimer_init);