blob: 73a2b476e59f3256387fe48097509bbda44c76af [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>
Ingo Molnarb17b0152017-02-08 18:51:35 +010023#include <linux/sched/debug.h>
John Stultzff3ead92011-01-11 09:42:13 -080024#include <linux/alarmtimer.h>
25#include <linux/mutex.h>
26#include <linux/platform_device.h>
27#include <linux/posix-timers.h>
28#include <linux/workqueue.h>
29#include <linux/freezer.h>
Al Viroedbeda42017-06-07 09:42:31 +010030#include <linux/compat.h>
John Stultzff3ead92011-01-11 09:42:13 -080031
Thomas Gleixnerbab0aae92017-05-30 23:15:41 +020032#include "posix-timers.h"
33
Baolin Wang4a057542016-11-28 14:35:21 -080034#define CREATE_TRACE_POINTS
35#include <trace/events/alarmtimer.h>
36
John Stultz180bf812011-04-28 12:58:11 -070037/**
38 * struct alarm_base - Alarm timer bases
39 * @lock: Lock for syncrhonized access to the base
40 * @timerqueue: Timerqueue head managing the list of events
John Stultz180bf812011-04-28 12:58:11 -070041 * @gettime: Function to read the time correlating to the base
42 * @base_clockid: clockid for the base
John Stultz180bf812011-04-28 12:58:11 -070043 */
John Stultzff3ead92011-01-11 09:42:13 -080044static struct alarm_base {
45 spinlock_t lock;
46 struct timerqueue_head timerqueue;
John Stultzff3ead92011-01-11 09:42:13 -080047 ktime_t (*gettime)(void);
48 clockid_t base_clockid;
John Stultzff3ead92011-01-11 09:42:13 -080049} alarm_bases[ALARM_NUMTYPE];
50
Thomas Gleixnerb6b3b802017-05-27 12:23:47 +020051#if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
Baolin Wang4a057542016-11-28 14:35:21 -080052/* freezer information to handle clock_nanosleep triggered wakeups */
53static enum alarmtimer_type freezer_alarmtype;
54static ktime_t freezer_expires;
John Stultzc008ba582011-06-16 18:27:09 -070055static ktime_t freezer_delta;
56static DEFINE_SPINLOCK(freezer_delta_lock);
Thomas Gleixnerb6b3b802017-05-27 12:23:47 +020057#endif
John Stultzc008ba582011-06-16 18:27:09 -070058
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +020059#ifdef CONFIG_RTC_CLASS
Todd Poynor59a93c22012-08-09 00:37:27 -070060static struct wakeup_source *ws;
61
John Stultz180bf812011-04-28 12:58:11 -070062/* rtc timer and device for setting alarm wakeups at suspend */
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +010063static struct rtc_timer rtctimer;
John Stultzff3ead92011-01-11 09:42:13 -080064static struct rtc_device *rtcdev;
John Stultzc008ba582011-06-16 18:27:09 -070065static DEFINE_SPINLOCK(rtcdev_lock);
John Stultzff3ead92011-01-11 09:42:13 -080066
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;
77 struct rtc_device *ret;
78
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;
91 struct rtc_device *rtc = to_rtc_device(dev);
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +020092 struct wakeup_source *__ws;
John Stultz8bc0daf2011-07-14 18:35:13 -070093
94 if (rtcdev)
95 return -EBUSY;
96
97 if (!rtc->ops->set_alarm)
98 return -1;
99 if (!device_may_wakeup(rtc->dev.parent))
100 return -1;
101
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +0200102 __ws = wakeup_source_register("alarmtimer");
103
John Stultz8bc0daf2011-07-14 18:35:13 -0700104 spin_lock_irqsave(&rtcdev_lock, flags);
105 if (!rtcdev) {
106 rtcdev = rtc;
107 /* hold a reference so it doesn't go away */
108 get_device(dev);
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +0200109 ws = __ws;
110 __ws = NULL;
John Stultz8bc0daf2011-07-14 18:35:13 -0700111 }
112 spin_unlock_irqrestore(&rtcdev_lock, flags);
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +0200113
114 wakeup_source_unregister(__ws);
115
John Stultz8bc0daf2011-07-14 18:35:13 -0700116 return 0;
117}
118
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100119static inline void alarmtimer_rtc_timer_init(void)
120{
121 rtc_timer_init(&rtctimer, NULL, NULL);
122}
123
John Stultz8bc0daf2011-07-14 18:35:13 -0700124static struct class_interface alarmtimer_rtc_interface = {
125 .add_dev = &alarmtimer_rtc_add_device,
126};
127
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200128static int alarmtimer_rtc_interface_setup(void)
John Stultz8bc0daf2011-07-14 18:35:13 -0700129{
130 alarmtimer_rtc_interface.class = rtc_class;
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200131 return class_interface_register(&alarmtimer_rtc_interface);
132}
133static void alarmtimer_rtc_interface_remove(void)
134{
135 class_interface_unregister(&alarmtimer_rtc_interface);
John Stultz8bc0daf2011-07-14 18:35:13 -0700136}
John Stultz1c6b39a2011-06-16 18:47:37 -0700137#else
John Stultz57c498f2012-04-20 12:31:45 -0700138struct rtc_device *alarmtimer_get_rtcdev(void)
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200139{
140 return NULL;
141}
142#define rtcdev (NULL)
143static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
144static inline void alarmtimer_rtc_interface_remove(void) { }
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100145static inline void alarmtimer_rtc_timer_init(void) { }
John Stultzc008ba582011-06-16 18:27:09 -0700146#endif
John Stultzff3ead92011-01-11 09:42:13 -0800147
John Stultz180bf812011-04-28 12:58:11 -0700148/**
John Stultzff3ead92011-01-11 09:42:13 -0800149 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
150 * @base: pointer to the base where the timer is being run
151 * @alarm: pointer to alarm being enqueued.
152 *
John Stultzdae373b2012-09-13 19:12:16 -0400153 * Adds alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800154 *
155 * Must hold base->lock when calling.
156 */
157static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
158{
John Stultzdae373b2012-09-13 19:12:16 -0400159 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
160 timerqueue_del(&base->timerqueue, &alarm->node);
161
John Stultzff3ead92011-01-11 09:42:13 -0800162 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700163 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800164}
165
John Stultz180bf812011-04-28 12:58:11 -0700166/**
John Stultza65bcc12012-09-13 19:25:22 -0400167 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800168 * @base: pointer to the base where the timer is running
169 * @alarm: pointer to alarm being removed
170 *
John Stultzdae373b2012-09-13 19:12:16 -0400171 * Removes alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800172 *
173 * Must hold base->lock when calling.
174 */
John Stultza65bcc12012-09-13 19:25:22 -0400175static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800176{
John Stultza28cde82011-08-10 12:30:21 -0700177 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
178 return;
179
John Stultzff3ead92011-01-11 09:42:13 -0800180 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700181 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800182}
183
John Stultz7068b7a2011-04-28 13:29:18 -0700184
John Stultz180bf812011-04-28 12:58:11 -0700185/**
John Stultz7068b7a2011-04-28 13:29:18 -0700186 * alarmtimer_fired - Handles alarm hrtimer being fired.
187 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800188 *
John Stultz180bf812011-04-28 12:58:11 -0700189 * When a alarm timer fires, this runs through the timerqueue to
190 * see which alarms expired, and runs those. If there are more alarm
191 * timers queued for the future, we set the hrtimer to fire when
192 * when the next future alarm timer expires.
John Stultzff3ead92011-01-11 09:42:13 -0800193 */
John Stultz7068b7a2011-04-28 13:29:18 -0700194static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800195{
John Stultzdae373b2012-09-13 19:12:16 -0400196 struct alarm *alarm = container_of(timer, struct alarm, timer);
197 struct alarm_base *base = &alarm_bases[alarm->type];
John Stultzff3ead92011-01-11 09:42:13 -0800198 unsigned long flags;
John Stultz7068b7a2011-04-28 13:29:18 -0700199 int ret = HRTIMER_NORESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700200 int restart = ALARMTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800201
202 spin_lock_irqsave(&base->lock, flags);
John Stultza65bcc12012-09-13 19:25:22 -0400203 alarmtimer_dequeue(base, alarm);
John Stultzdae373b2012-09-13 19:12:16 -0400204 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800205
John Stultzdae373b2012-09-13 19:12:16 -0400206 if (alarm->function)
207 restart = alarm->function(alarm, base->gettime());
John Stultzff3ead92011-01-11 09:42:13 -0800208
John Stultzdae373b2012-09-13 19:12:16 -0400209 spin_lock_irqsave(&base->lock, flags);
210 if (restart != ALARMTIMER_NORESTART) {
211 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
212 alarmtimer_enqueue(base, alarm);
John Stultz7068b7a2011-04-28 13:29:18 -0700213 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800214 }
215 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800216
Baolin Wang4a057542016-11-28 14:35:21 -0800217 trace_alarmtimer_fired(alarm, base->gettime());
John Stultz7068b7a2011-04-28 13:29:18 -0700218 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800219
John Stultzff3ead92011-01-11 09:42:13 -0800220}
221
Todd Poynor6cffe002013-05-15 14:38:11 -0700222ktime_t alarm_expires_remaining(const struct alarm *alarm)
223{
224 struct alarm_base *base = &alarm_bases[alarm->type];
225 return ktime_sub(alarm->node.expires, base->gettime());
226}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200227EXPORT_SYMBOL_GPL(alarm_expires_remaining);
Todd Poynor6cffe002013-05-15 14:38:11 -0700228
John Stultz472647d2011-04-29 15:03:10 -0700229#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700230/**
John Stultzff3ead92011-01-11 09:42:13 -0800231 * alarmtimer_suspend - Suspend time callback
232 * @dev: unused
233 * @state: unused
234 *
235 * When we are going into suspend, we look through the bases
236 * to see which is the soonest timer to expire. We then
237 * set an rtc timer to fire that far into the future, which
238 * will wake us from suspend.
239 */
240static int alarmtimer_suspend(struct device *dev)
241{
Baolin Wang4a057542016-11-28 14:35:21 -0800242 ktime_t min, now, expires;
243 int i, ret, type;
John Stultzc008ba582011-06-16 18:27:09 -0700244 struct rtc_device *rtc;
Baolin Wang4a057542016-11-28 14:35:21 -0800245 unsigned long flags;
246 struct rtc_time tm;
John Stultzff3ead92011-01-11 09:42:13 -0800247
248 spin_lock_irqsave(&freezer_delta_lock, flags);
249 min = freezer_delta;
Baolin Wang4a057542016-11-28 14:35:21 -0800250 expires = freezer_expires;
251 type = freezer_alarmtype;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100252 freezer_delta = 0;
John Stultzff3ead92011-01-11 09:42:13 -0800253 spin_unlock_irqrestore(&freezer_delta_lock, flags);
254
John Stultz8bc0daf2011-07-14 18:35:13 -0700255 rtc = alarmtimer_get_rtcdev();
John Stultzff3ead92011-01-11 09:42:13 -0800256 /* If we have no rtcdev, just return */
John Stultzc008ba582011-06-16 18:27:09 -0700257 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800258 return 0;
259
260 /* Find the soonest timer to expire*/
261 for (i = 0; i < ALARM_NUMTYPE; i++) {
262 struct alarm_base *base = &alarm_bases[i];
263 struct timerqueue_node *next;
264 ktime_t delta;
265
266 spin_lock_irqsave(&base->lock, flags);
267 next = timerqueue_getnext(&base->timerqueue);
268 spin_unlock_irqrestore(&base->lock, flags);
269 if (!next)
270 continue;
271 delta = ktime_sub(next->expires, base->gettime());
Thomas Gleixner2456e852016-12-25 11:38:40 +0100272 if (!min || (delta < min)) {
Baolin Wang4a057542016-11-28 14:35:21 -0800273 expires = next->expires;
John Stultzff3ead92011-01-11 09:42:13 -0800274 min = delta;
Baolin Wang4a057542016-11-28 14:35:21 -0800275 type = i;
276 }
John Stultzff3ead92011-01-11 09:42:13 -0800277 }
Thomas Gleixner2456e852016-12-25 11:38:40 +0100278 if (min == 0)
John Stultzff3ead92011-01-11 09:42:13 -0800279 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
Baolin Wang4a057542016-11-28 14:35:21 -0800286 trace_alarmtimer_suspend(expires, type);
287
John Stultzff3ead92011-01-11 09:42:13 -0800288 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba582011-06-16 18:27:09 -0700289 rtc_timer_cancel(rtc, &rtctimer);
290 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800291 now = rtc_tm_to_ktime(tm);
292 now = ktime_add(now, min);
293
Todd Poynor59a93c22012-08-09 00:37:27 -0700294 /* Set alarm, if in the past reject suspend briefly to handle */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100295 ret = rtc_timer_start(rtc, &rtctimer, now, 0);
Todd Poynor59a93c22012-08-09 00:37:27 -0700296 if (ret < 0)
297 __pm_wakeup_event(ws, MSEC_PER_SEC);
298 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800299}
zhuo-haoa0e32132015-11-17 20:08:07 +0800300
301static int alarmtimer_resume(struct device *dev)
302{
303 struct rtc_device *rtc;
304
305 rtc = alarmtimer_get_rtcdev();
306 if (rtc)
307 rtc_timer_cancel(rtc, &rtctimer);
308 return 0;
309}
310
John Stultz472647d2011-04-29 15:03:10 -0700311#else
312static int alarmtimer_suspend(struct device *dev)
313{
314 return 0;
315}
zhuo-haoa0e32132015-11-17 20:08:07 +0800316
317static int alarmtimer_resume(struct device *dev)
318{
319 return 0;
320}
John Stultz472647d2011-04-29 15:03:10 -0700321#endif
John Stultzff3ead92011-01-11 09:42:13 -0800322
John Stultz180bf812011-04-28 12:58:11 -0700323/**
John Stultzff3ead92011-01-11 09:42:13 -0800324 * alarm_init - Initialize an alarm structure
325 * @alarm: ptr to alarm to be initialized
326 * @type: the type of the alarm
327 * @function: callback that is run when the alarm fires
John Stultzff3ead92011-01-11 09:42:13 -0800328 */
329void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
John Stultz4b413082011-08-10 10:37:59 -0700330 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
John Stultzff3ead92011-01-11 09:42:13 -0800331{
332 timerqueue_init(&alarm->node);
John Stultzdae373b2012-09-13 19:12:16 -0400333 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
334 HRTIMER_MODE_ABS);
335 alarm->timer.function = alarmtimer_fired;
John Stultzff3ead92011-01-11 09:42:13 -0800336 alarm->function = function;
337 alarm->type = type;
John Stultza28cde82011-08-10 12:30:21 -0700338 alarm->state = ALARMTIMER_STATE_INACTIVE;
John Stultzff3ead92011-01-11 09:42:13 -0800339}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200340EXPORT_SYMBOL_GPL(alarm_init);
John Stultzff3ead92011-01-11 09:42:13 -0800341
John Stultz180bf812011-04-28 12:58:11 -0700342/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700343 * alarm_start - Sets an absolute alarm to fire
John Stultzff3ead92011-01-11 09:42:13 -0800344 * @alarm: ptr to alarm to set
345 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800346 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000347void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800348{
349 struct alarm_base *base = &alarm_bases[alarm->type];
350 unsigned long flags;
351
352 spin_lock_irqsave(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800353 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800354 alarmtimer_enqueue(base, alarm);
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000355 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
John Stultzff3ead92011-01-11 09:42:13 -0800356 spin_unlock_irqrestore(&base->lock, flags);
Baolin Wang4a057542016-11-28 14:35:21 -0800357
358 trace_alarmtimer_start(alarm, base->gettime());
John Stultzff3ead92011-01-11 09:42:13 -0800359}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200360EXPORT_SYMBOL_GPL(alarm_start);
John Stultzff3ead92011-01-11 09:42:13 -0800361
John Stultz180bf812011-04-28 12:58:11 -0700362/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700363 * alarm_start_relative - Sets a relative alarm to fire
364 * @alarm: ptr to alarm to set
365 * @start: time relative to now to run the alarm
366 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000367void alarm_start_relative(struct alarm *alarm, ktime_t start)
Todd Poynor6cffe002013-05-15 14:38:11 -0700368{
369 struct alarm_base *base = &alarm_bases[alarm->type];
370
Thomas Gleixnerf4781e72017-05-30 23:15:34 +0200371 start = ktime_add_safe(start, base->gettime());
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000372 alarm_start(alarm, start);
Todd Poynor6cffe002013-05-15 14:38:11 -0700373}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200374EXPORT_SYMBOL_GPL(alarm_start_relative);
Todd Poynor6cffe002013-05-15 14:38:11 -0700375
376void alarm_restart(struct alarm *alarm)
377{
378 struct alarm_base *base = &alarm_bases[alarm->type];
379 unsigned long flags;
380
381 spin_lock_irqsave(&base->lock, flags);
382 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
383 hrtimer_restart(&alarm->timer);
384 alarmtimer_enqueue(base, alarm);
385 spin_unlock_irqrestore(&base->lock, flags);
386}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200387EXPORT_SYMBOL_GPL(alarm_restart);
Todd Poynor6cffe002013-05-15 14:38:11 -0700388
389/**
John Stultz9082c462011-08-10 12:41:36 -0700390 * alarm_try_to_cancel - Tries to cancel an alarm timer
John Stultzff3ead92011-01-11 09:42:13 -0800391 * @alarm: ptr to alarm to be canceled
John Stultz9082c462011-08-10 12:41:36 -0700392 *
393 * Returns 1 if the timer was canceled, 0 if it was not running,
394 * and -1 if the callback was running
John Stultzff3ead92011-01-11 09:42:13 -0800395 */
John Stultz9082c462011-08-10 12:41:36 -0700396int alarm_try_to_cancel(struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800397{
398 struct alarm_base *base = &alarm_bases[alarm->type];
399 unsigned long flags;
John Stultzdae373b2012-09-13 19:12:16 -0400400 int ret;
401
John Stultzff3ead92011-01-11 09:42:13 -0800402 spin_lock_irqsave(&base->lock, flags);
John Stultzdae373b2012-09-13 19:12:16 -0400403 ret = hrtimer_try_to_cancel(&alarm->timer);
404 if (ret >= 0)
John Stultza65bcc12012-09-13 19:25:22 -0400405 alarmtimer_dequeue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800406 spin_unlock_irqrestore(&base->lock, flags);
Baolin Wang4a057542016-11-28 14:35:21 -0800407
408 trace_alarmtimer_cancel(alarm, base->gettime());
John Stultz9082c462011-08-10 12:41:36 -0700409 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800410}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200411EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
John Stultzff3ead92011-01-11 09:42:13 -0800412
413
John Stultz9082c462011-08-10 12:41:36 -0700414/**
415 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
416 * @alarm: ptr to alarm to be canceled
417 *
418 * Returns 1 if the timer was canceled, 0 if it was not active.
419 */
420int alarm_cancel(struct alarm *alarm)
421{
422 for (;;) {
423 int ret = alarm_try_to_cancel(alarm);
424 if (ret >= 0)
425 return ret;
426 cpu_relax();
427 }
428}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200429EXPORT_SYMBOL_GPL(alarm_cancel);
John Stultz9082c462011-08-10 12:41:36 -0700430
John Stultzdce75a82011-08-10 11:31:03 -0700431
432u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
433{
434 u64 overrun = 1;
435 ktime_t delta;
436
437 delta = ktime_sub(now, alarm->node.expires);
438
Thomas Gleixner2456e852016-12-25 11:38:40 +0100439 if (delta < 0)
John Stultzdce75a82011-08-10 11:31:03 -0700440 return 0;
441
Thomas Gleixner2456e852016-12-25 11:38:40 +0100442 if (unlikely(delta >= interval)) {
John Stultzdce75a82011-08-10 11:31:03 -0700443 s64 incr = ktime_to_ns(interval);
444
445 overrun = ktime_divns(delta, incr);
446
447 alarm->node.expires = ktime_add_ns(alarm->node.expires,
448 incr*overrun);
449
Thomas Gleixner2456e852016-12-25 11:38:40 +0100450 if (alarm->node.expires > now)
John Stultzdce75a82011-08-10 11:31:03 -0700451 return overrun;
452 /*
453 * This (and the ktime_add() below) is the
454 * correction for exact:
455 */
456 overrun++;
457 }
458
Thomas Gleixnerf4781e72017-05-30 23:15:34 +0200459 alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
John Stultzdce75a82011-08-10 11:31:03 -0700460 return overrun;
461}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200462EXPORT_SYMBOL_GPL(alarm_forward);
John Stultzdce75a82011-08-10 11:31:03 -0700463
Todd Poynor6cffe002013-05-15 14:38:11 -0700464u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
465{
466 struct alarm_base *base = &alarm_bases[alarm->type];
467
468 return alarm_forward(alarm, base->gettime(), interval);
469}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200470EXPORT_SYMBOL_GPL(alarm_forward_now);
John Stultzdce75a82011-08-10 11:31:03 -0700471
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300472#ifdef CONFIG_POSIX_TIMERS
473
474static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
475{
476 struct alarm_base *base;
477 unsigned long flags;
478 ktime_t delta;
479
480 switch(type) {
481 case ALARM_REALTIME:
482 base = &alarm_bases[ALARM_REALTIME];
483 type = ALARM_REALTIME_FREEZER;
484 break;
485 case ALARM_BOOTTIME:
486 base = &alarm_bases[ALARM_BOOTTIME];
487 type = ALARM_BOOTTIME_FREEZER;
488 break;
489 default:
490 WARN_ONCE(1, "Invalid alarm type: %d\n", type);
491 return;
492 }
493
494 delta = ktime_sub(absexp, base->gettime());
495
496 spin_lock_irqsave(&freezer_delta_lock, flags);
497 if (!freezer_delta || (delta < freezer_delta)) {
498 freezer_delta = delta;
499 freezer_expires = absexp;
500 freezer_alarmtype = type;
501 }
502 spin_unlock_irqrestore(&freezer_delta_lock, flags);
503}
John Stultzdce75a82011-08-10 11:31:03 -0700504
John Stultz180bf812011-04-28 12:58:11 -0700505/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800506 * clock2alarm - helper that converts from clockid to alarmtypes
507 * @clockid: clockid.
John Stultz9a7adcf52011-01-11 09:54:33 -0800508 */
509static enum alarmtimer_type clock2alarm(clockid_t clockid)
510{
511 if (clockid == CLOCK_REALTIME_ALARM)
512 return ALARM_REALTIME;
513 if (clockid == CLOCK_BOOTTIME_ALARM)
514 return ALARM_BOOTTIME;
515 return -1;
516}
517
John Stultz180bf812011-04-28 12:58:11 -0700518/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800519 * alarm_handle_timer - Callback for posix timers
520 * @alarm: alarm that fired
521 *
522 * Posix timer callback for expired alarm timers.
523 */
John Stultz4b413082011-08-10 10:37:59 -0700524static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
525 ktime_t now)
John Stultz9a7adcf52011-01-11 09:54:33 -0800526{
527 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200528 it.alarm.alarmtimer);
Richard Larocque474e941b2014-09-09 18:31:05 -0700529 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200530 unsigned long flags;
531 int si_private = 0;
Richard Larocque474e941b2014-09-09 18:31:05 -0700532
533 spin_lock_irqsave(&ptr->it_lock, flags);
John Stultz4b413082011-08-10 10:37:59 -0700534
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200535 ptr->it_active = 0;
536 if (ptr->it_interval)
537 si_private = ++ptr->it_requeue_pending;
538
539 if (posix_timer_event(ptr, si_private) && ptr->it_interval) {
540 /*
541 * Handle ignored signals and rearm the timer. This will go
542 * away once we handle ignored signals proper.
543 */
544 ptr->it_overrun += alarm_forward_now(alarm, ptr->it_interval);
545 ++ptr->it_requeue_pending;
546 ptr->it_active = 1;
Richard Larocque474e941b2014-09-09 18:31:05 -0700547 result = ALARMTIMER_RESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700548 }
Richard Larocque474e941b2014-09-09 18:31:05 -0700549 spin_unlock_irqrestore(&ptr->it_lock, flags);
550
551 return result;
John Stultz9a7adcf52011-01-11 09:54:33 -0800552}
553
John Stultz180bf812011-04-28 12:58:11 -0700554/**
Thomas Gleixnerb3db80f2017-05-30 23:15:54 +0200555 * alarm_timer_rearm - Posix timer callback for rearming timer
556 * @timr: Pointer to the posixtimer data struct
557 */
558static void alarm_timer_rearm(struct k_itimer *timr)
559{
560 struct alarm *alarm = &timr->it.alarm.alarmtimer;
561
562 timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
563 alarm_start(alarm, alarm->node.expires);
564}
565
566/**
Thomas Gleixnere7561f12017-05-30 23:15:55 +0200567 * alarm_timer_forward - Posix timer callback for forwarding timer
568 * @timr: Pointer to the posixtimer data struct
569 * @now: Current time to forward the timer against
570 */
571static int alarm_timer_forward(struct k_itimer *timr, ktime_t now)
572{
573 struct alarm *alarm = &timr->it.alarm.alarmtimer;
574
575 return (int) alarm_forward(alarm, timr->it_interval, now);
576}
577
578/**
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200579 * alarm_timer_remaining - Posix timer callback to retrieve remaining time
580 * @timr: Pointer to the posixtimer data struct
581 * @now: Current time to calculate against
582 */
583static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
584{
585 struct alarm *alarm = &timr->it.alarm.alarmtimer;
586
587 return ktime_sub(now, alarm->node.expires);
588}
589
590/**
Thomas Gleixnere344c9e2017-05-30 23:15:57 +0200591 * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
592 * @timr: Pointer to the posixtimer data struct
593 */
594static int alarm_timer_try_to_cancel(struct k_itimer *timr)
595{
596 return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
597}
598
599/**
Thomas Gleixnerb3bf6f32017-05-30 23:15:58 +0200600 * alarm_timer_arm - Posix timer callback to arm a timer
601 * @timr: Pointer to the posixtimer data struct
602 * @expires: The new expiry time
603 * @absolute: Expiry value is absolute time
604 * @sigev_none: Posix timer does not deliver signals
605 */
606static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
607 bool absolute, bool sigev_none)
608{
609 struct alarm *alarm = &timr->it.alarm.alarmtimer;
610 struct alarm_base *base = &alarm_bases[alarm->type];
611
612 if (!absolute)
613 expires = ktime_add_safe(expires, base->gettime());
614 if (sigev_none)
615 alarm->node.expires = expires;
616 else
617 alarm_start(&timr->it.alarm.alarmtimer, expires);
618}
619
620/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800621 * alarm_clock_getres - posix getres interface
622 * @which_clock: clockid
623 * @tp: timespec to fill
624 *
625 * Returns the granularity of underlying alarm base clock
626 */
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700627static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
John Stultz9a7adcf52011-01-11 09:54:33 -0800628{
John Stultz1c6b39a2011-06-16 18:47:37 -0700629 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400630 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700631
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000632 tp->tv_sec = 0;
633 tp->tv_nsec = hrtimer_resolution;
634 return 0;
John Stultz9a7adcf52011-01-11 09:54:33 -0800635}
636
637/**
638 * alarm_clock_get - posix clock_get interface
639 * @which_clock: clockid
640 * @tp: timespec to fill.
641 *
642 * Provides the underlying alarm base time.
643 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700644static int alarm_clock_get(clockid_t which_clock, struct timespec64 *tp)
John Stultz9a7adcf52011-01-11 09:54:33 -0800645{
646 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
647
John Stultz1c6b39a2011-06-16 18:47:37 -0700648 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400649 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700650
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700651 *tp = ktime_to_timespec64(base->gettime());
John Stultz9a7adcf52011-01-11 09:54:33 -0800652 return 0;
653}
654
655/**
656 * alarm_timer_create - posix timer_create interface
657 * @new_timer: k_itimer pointer to manage
658 *
659 * Initializes the k_itimer structure.
660 */
661static int alarm_timer_create(struct k_itimer *new_timer)
662{
663 enum alarmtimer_type type;
John Stultz9a7adcf52011-01-11 09:54:33 -0800664
John Stultz1c6b39a2011-06-16 18:47:37 -0700665 if (!alarmtimer_get_rtcdev())
666 return -ENOTSUPP;
667
John Stultz9a7adcf52011-01-11 09:54:33 -0800668 if (!capable(CAP_WAKE_ALARM))
669 return -EPERM;
670
671 type = clock2alarm(new_timer->it_clock);
John Stultz9e264762011-08-10 12:09:24 -0700672 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf52011-01-11 09:54:33 -0800673 return 0;
674}
675
676/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800677 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
678 * @alarm: ptr to alarm that fired
679 *
680 * Wakes up the task that set the alarmtimer
681 */
John Stultz4b413082011-08-10 10:37:59 -0700682static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
683 ktime_t now)
John Stultz9a7adcf52011-01-11 09:54:33 -0800684{
685 struct task_struct *task = (struct task_struct *)alarm->data;
686
687 alarm->data = NULL;
688 if (task)
689 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700690 return ALARMTIMER_NORESTART;
John Stultz9a7adcf52011-01-11 09:54:33 -0800691}
692
693/**
694 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
695 * @alarm: ptr to alarmtimer
696 * @absexp: absolute expiration time
697 *
698 * Sets the alarm timer and sleeps until it is fired or interrupted.
699 */
Al Viro15f27ce2017-06-07 09:42:27 +0100700static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
701 enum alarmtimer_type type)
John Stultz9a7adcf52011-01-11 09:54:33 -0800702{
Al Viroedbeda42017-06-07 09:42:31 +0100703 struct restart_block *restart;
John Stultz9a7adcf52011-01-11 09:54:33 -0800704 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 Stultz9a7adcf52011-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
Al Viro15f27ce2017-06-07 09:42:27 +0100716 if (!alarm->data)
John Stultz9a7adcf52011-01-11 09:54:33 -0800717 return 0;
John Stultz9a7adcf52011-01-11 09:54:33 -0800718
Al Viro15f27ce2017-06-07 09:42:27 +0100719 if (freezing(current))
720 alarmtimer_freezerset(absexp, type);
Al Viroedbeda42017-06-07 09:42:31 +0100721 restart = &current->restart_block;
722 if (restart->nanosleep.type != TT_NONE) {
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -0700723 struct timespec64 rmt;
Al Viro15f27ce2017-06-07 09:42:27 +0100724 ktime_t rem;
John Stultz9a7adcf52011-01-11 09:54:33 -0800725
Al Viro15f27ce2017-06-07 09:42:27 +0100726 rem = ktime_sub(absexp, alarm_bases[type].gettime());
John Stultz9a7adcf52011-01-11 09:54:33 -0800727
Al Viro15f27ce2017-06-07 09:42:27 +0100728 if (rem <= 0)
729 return 0;
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -0700730 rmt = ktime_to_timespec64(rem);
Al Viro15f27ce2017-06-07 09:42:27 +0100731
Al Viroce41aaf2017-06-07 09:42:32 +0100732 return nanosleep_copyout(restart, &rmt);
Al Viro15f27ce2017-06-07 09:42:27 +0100733 }
734 return -ERESTART_RESTARTBLOCK;
John Stultz9a7adcf52011-01-11 09:54:33 -0800735}
736
737/**
738 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
739 * @restart: ptr to restart block
740 *
741 * Handles restarted clock_nanosleep calls
742 */
743static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
744{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200745 enum alarmtimer_type type = restart->nanosleep.clockid;
Al Viro15f27ce2017-06-07 09:42:27 +0100746 ktime_t exp = restart->nanosleep.expires;
John Stultz9a7adcf52011-01-11 09:54:33 -0800747 struct alarm alarm;
John Stultz9a7adcf52011-01-11 09:54:33 -0800748
John Stultz9a7adcf52011-01-11 09:54:33 -0800749 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
750
Al Viro15f27ce2017-06-07 09:42:27 +0100751 return alarmtimer_do_nsleep(&alarm, exp, type);
John Stultz9a7adcf52011-01-11 09:54:33 -0800752}
753
754/**
755 * alarm_timer_nsleep - alarmtimer nanosleep
756 * @which_clock: clockid
757 * @flags: determins abstime or relative
758 * @tsreq: requested sleep time (abs or rel)
759 * @rmtp: remaining sleep time saved
760 *
761 * Handles clock_nanosleep calls against _ALARM clockids
762 */
763static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner938e7cf2017-06-13 23:34:33 +0200764 const struct timespec64 *tsreq)
John Stultz9a7adcf52011-01-11 09:54:33 -0800765{
766 enum alarmtimer_type type = clock2alarm(which_clock);
Al Viro15f27ce2017-06-07 09:42:27 +0100767 struct restart_block *restart = &current->restart_block;
John Stultz9a7adcf52011-01-11 09:54:33 -0800768 struct alarm alarm;
769 ktime_t exp;
770 int ret = 0;
John Stultz9a7adcf52011-01-11 09:54:33 -0800771
John Stultz1c6b39a2011-06-16 18:47:37 -0700772 if (!alarmtimer_get_rtcdev())
773 return -ENOTSUPP;
774
John Stultz16927772014-07-07 14:06:11 -0700775 if (flags & ~TIMER_ABSTIME)
776 return -EINVAL;
777
John Stultz9a7adcf52011-01-11 09:54:33 -0800778 if (!capable(CAP_WAKE_ALARM))
779 return -EPERM;
780
781 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
782
Deepa Dinamaniad196382017-03-26 12:04:18 -0700783 exp = timespec64_to_ktime(*tsreq);
John Stultz9a7adcf52011-01-11 09:54:33 -0800784 /* Convert (if necessary) to absolute time */
785 if (flags != TIMER_ABSTIME) {
786 ktime_t now = alarm_bases[type].gettime();
787 exp = ktime_add(now, exp);
788 }
789
Al Viro15f27ce2017-06-07 09:42:27 +0100790 ret = alarmtimer_do_nsleep(&alarm, exp, type);
791 if (ret != -ERESTART_RESTARTBLOCK)
792 return ret;
John Stultz9a7adcf52011-01-11 09:54:33 -0800793
794 /* abs timers don't set remaining time or restart */
Al Viro15f27ce2017-06-07 09:42:27 +0100795 if (flags == TIMER_ABSTIME)
796 return -ERESTARTNOHAND;
John Stultz9a7adcf52011-01-11 09:54:33 -0800797
John Stultz9a7adcf52011-01-11 09:54:33 -0800798 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200799 restart->nanosleep.clockid = type;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100800 restart->nanosleep.expires = exp;
John Stultz9a7adcf52011-01-11 09:54:33 -0800801 return ret;
802}
John Stultzff3ead92011-01-11 09:42:13 -0800803
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300804const struct k_clock alarm_clock = {
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200805 .clock_getres = alarm_clock_getres,
806 .clock_get = alarm_clock_get,
807 .timer_create = alarm_timer_create,
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200808 .timer_set = common_timer_set,
809 .timer_del = common_timer_del,
810 .timer_get = common_timer_get,
Thomas Gleixnerb3bf6f32017-05-30 23:15:58 +0200811 .timer_arm = alarm_timer_arm,
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200812 .timer_rearm = alarm_timer_rearm,
813 .timer_forward = alarm_timer_forward,
814 .timer_remaining = alarm_timer_remaining,
Thomas Gleixnere344c9e2017-05-30 23:15:57 +0200815 .timer_try_to_cancel = alarm_timer_try_to_cancel,
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200816 .nsleep = alarm_timer_nsleep,
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300817};
818#endif /* CONFIG_POSIX_TIMERS */
819
John Stultzff3ead92011-01-11 09:42:13 -0800820
821/* Suspend hook structures */
822static const struct dev_pm_ops alarmtimer_pm_ops = {
823 .suspend = alarmtimer_suspend,
zhuo-haoa0e32132015-11-17 20:08:07 +0800824 .resume = alarmtimer_resume,
John Stultzff3ead92011-01-11 09:42:13 -0800825};
826
827static struct platform_driver alarmtimer_driver = {
828 .driver = {
829 .name = "alarmtimer",
830 .pm = &alarmtimer_pm_ops,
831 }
832};
833
834/**
835 * alarmtimer_init - Initialize alarm timer code
836 *
837 * This function initializes the alarm bases and registers
838 * the posix clock ids.
839 */
840static int __init alarmtimer_init(void)
841{
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200842 struct platform_device *pdev;
John Stultzff3ead92011-01-11 09:42:13 -0800843 int error = 0;
844 int i;
John Stultz9a7adcf52011-01-11 09:54:33 -0800845
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100846 alarmtimer_rtc_timer_init();
John Stultzad30dfa2012-03-23 15:52:25 -0700847
John Stultzff3ead92011-01-11 09:42:13 -0800848 /* Initialize alarm bases */
849 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
850 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
851 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
852 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
853 for (i = 0; i < ALARM_NUMTYPE; i++) {
854 timerqueue_init_head(&alarm_bases[i].timerqueue);
855 spin_lock_init(&alarm_bases[i].lock);
John Stultzff3ead92011-01-11 09:42:13 -0800856 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700857
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200858 error = alarmtimer_rtc_interface_setup();
859 if (error)
860 return error;
John Stultzff3ead92011-01-11 09:42:13 -0800861
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200862 error = platform_driver_register(&alarmtimer_driver);
863 if (error)
864 goto out_if;
865
866 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
867 if (IS_ERR(pdev)) {
868 error = PTR_ERR(pdev);
869 goto out_drv;
870 }
871 return 0;
872
873out_drv:
874 platform_driver_unregister(&alarmtimer_driver);
875out_if:
876 alarmtimer_rtc_interface_remove();
John Stultzff3ead92011-01-11 09:42:13 -0800877 return error;
878}
879device_initcall(alarmtimer_init);