4 * This interface provides a timer which is similarto hrtimers,
5 * but triggers a RTC alarm if the box is suspend.
7 * This interface is influenced by the Android RTC Alarm timer
10 * Copyright (C) 2010 IBM Corperation
12 * Author: John Stultz <john.stultz@linaro.org>
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.
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>
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
33 * @timer: hrtimer used to schedule events while running
34 * @gettime: Function to read the time correlating to the base
35 * @base_clockid: clockid for the base
37 static struct alarm_base {
39 struct timerqueue_head timerqueue;
41 ktime_t (*gettime)(void);
42 clockid_t base_clockid;
43 } alarm_bases[ALARM_NUMTYPE];
45 /* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
46 static ktime_t freezer_delta;
47 static DEFINE_SPINLOCK(freezer_delta_lock);
49 #ifdef CONFIG_RTC_CLASS
50 /* rtc timer and device for setting alarm wakeups at suspend */
51 static struct rtc_timer rtctimer;
52 static struct rtc_device *rtcdev;
53 static DEFINE_SPINLOCK(rtcdev_lock);
56 * alarmtimer_get_rtcdev - Return selected rtcdevice
58 * This function returns the rtc device to use for wakealarms.
59 * If one has not already been chosen, it checks to see if a
60 * functional rtc device is available.
62 static struct rtc_device *alarmtimer_get_rtcdev(void)
65 struct rtc_device *ret;
67 spin_lock_irqsave(&rtcdev_lock, flags);
69 spin_unlock_irqrestore(&rtcdev_lock, flags);
75 static int alarmtimer_rtc_add_device(struct device *dev,
76 struct class_interface *class_intf)
79 struct rtc_device *rtc = to_rtc_device(dev);
84 if (!rtc->ops->set_alarm)
86 if (!device_may_wakeup(rtc->dev.parent))
89 spin_lock_irqsave(&rtcdev_lock, flags);
92 /* hold a reference so it doesn't go away */
95 spin_unlock_irqrestore(&rtcdev_lock, flags);
99 static struct class_interface alarmtimer_rtc_interface = {
100 .add_dev = &alarmtimer_rtc_add_device,
103 static void alarmtimer_rtc_interface_setup(void)
105 alarmtimer_rtc_interface.class = rtc_class;
106 class_interface_register(&alarmtimer_rtc_interface);
109 #define alarmtimer_get_rtcdev() (0)
111 #define alarmtimer_rtc_interface_setup()
117 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
118 * @base: pointer to the base where the timer is being run
119 * @alarm: pointer to alarm being enqueued.
121 * Adds alarm to a alarm_base timerqueue and if necessary sets
124 * Must hold base->lock when calling.
126 static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
128 timerqueue_add(&base->timerqueue, &alarm->node);
129 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
131 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
132 hrtimer_try_to_cancel(&base->timer);
133 hrtimer_start(&base->timer, alarm->node.expires,
139 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
140 * @base: pointer to the base where the timer is running
141 * @alarm: pointer to alarm being removed
143 * Removes alarm to a alarm_base timerqueue and if necessary sets
144 * a new timer to run.
146 * Must hold base->lock when calling.
148 static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
150 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
152 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
155 timerqueue_del(&base->timerqueue, &alarm->node);
156 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
158 if (next == &alarm->node) {
159 hrtimer_try_to_cancel(&base->timer);
160 next = timerqueue_getnext(&base->timerqueue);
163 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
169 * alarmtimer_fired - Handles alarm hrtimer being fired.
170 * @timer: pointer to hrtimer being run
172 * When a alarm timer fires, this runs through the timerqueue to
173 * see which alarms expired, and runs those. If there are more alarm
174 * timers queued for the future, we set the hrtimer to fire when
175 * when the next future alarm timer expires.
177 static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
179 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
180 struct timerqueue_node *next;
183 int ret = HRTIMER_NORESTART;
184 int restart = ALARMTIMER_NORESTART;
186 spin_lock_irqsave(&base->lock, flags);
187 now = base->gettime();
188 while ((next = timerqueue_getnext(&base->timerqueue))) {
190 ktime_t expired = next->expires;
192 if (expired.tv64 >= now.tv64)
195 alarm = container_of(next, struct alarm, node);
197 timerqueue_del(&base->timerqueue, &alarm->node);
198 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
200 alarm->state |= ALARMTIMER_STATE_CALLBACK;
201 spin_unlock_irqrestore(&base->lock, flags);
203 restart = alarm->function(alarm, now);
204 spin_lock_irqsave(&base->lock, flags);
205 alarm->state &= ~ALARMTIMER_STATE_CALLBACK;
207 if (restart != ALARMTIMER_NORESTART) {
208 timerqueue_add(&base->timerqueue, &alarm->node);
209 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
214 hrtimer_set_expires(&base->timer, next->expires);
215 ret = HRTIMER_RESTART;
217 spin_unlock_irqrestore(&base->lock, flags);
223 #ifdef CONFIG_RTC_CLASS
225 * alarmtimer_suspend - Suspend time callback
229 * When we are going into suspend, we look through the bases
230 * to see which is the soonest timer to expire. We then
231 * set an rtc timer to fire that far into the future, which
232 * will wake us from suspend.
234 static int alarmtimer_suspend(struct device *dev)
239 struct rtc_device *rtc;
242 spin_lock_irqsave(&freezer_delta_lock, flags);
244 freezer_delta = ktime_set(0, 0);
245 spin_unlock_irqrestore(&freezer_delta_lock, flags);
247 rtc = alarmtimer_get_rtcdev();
248 /* If we have no rtcdev, just return */
252 /* Find the soonest timer to expire*/
253 for (i = 0; i < ALARM_NUMTYPE; i++) {
254 struct alarm_base *base = &alarm_bases[i];
255 struct timerqueue_node *next;
258 spin_lock_irqsave(&base->lock, flags);
259 next = timerqueue_getnext(&base->timerqueue);
260 spin_unlock_irqrestore(&base->lock, flags);
263 delta = ktime_sub(next->expires, base->gettime());
264 if (!min.tv64 || (delta.tv64 < min.tv64))
270 /* XXX - Should we enforce a minimum sleep time? */
271 WARN_ON(min.tv64 < NSEC_PER_SEC);
273 /* Setup an rtc timer to fire that far in the future */
274 rtc_timer_cancel(rtc, &rtctimer);
275 rtc_read_time(rtc, &tm);
276 now = rtc_tm_to_ktime(tm);
277 now = ktime_add(now, min);
279 rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
284 static int alarmtimer_suspend(struct device *dev)
290 static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
294 struct alarm_base *base = &alarm_bases[type];
296 delta = ktime_sub(absexp, base->gettime());
298 spin_lock_irqsave(&freezer_delta_lock, flags);
299 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
300 freezer_delta = delta;
301 spin_unlock_irqrestore(&freezer_delta_lock, flags);
306 * alarm_init - Initialize an alarm structure
307 * @alarm: ptr to alarm to be initialized
308 * @type: the type of the alarm
309 * @function: callback that is run when the alarm fires
311 void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
312 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
314 timerqueue_init(&alarm->node);
315 alarm->function = function;
317 alarm->state = ALARMTIMER_STATE_INACTIVE;
321 * alarm_start - Sets an alarm to fire
322 * @alarm: ptr to alarm to set
323 * @start: time to run the alarm
325 void alarm_start(struct alarm *alarm, ktime_t start)
327 struct alarm_base *base = &alarm_bases[alarm->type];
330 spin_lock_irqsave(&base->lock, flags);
331 if (alarmtimer_active(alarm))
332 alarmtimer_remove(base, alarm);
333 alarm->node.expires = start;
334 alarmtimer_enqueue(base, alarm);
335 spin_unlock_irqrestore(&base->lock, flags);
339 * alarm_try_to_cancel - Tries to cancel an alarm timer
340 * @alarm: ptr to alarm to be canceled
342 * Returns 1 if the timer was canceled, 0 if it was not running,
343 * and -1 if the callback was running
345 int alarm_try_to_cancel(struct alarm *alarm)
347 struct alarm_base *base = &alarm_bases[alarm->type];
350 spin_lock_irqsave(&base->lock, flags);
352 if (alarmtimer_callback_running(alarm))
355 if (alarmtimer_is_queued(alarm)) {
356 alarmtimer_remove(base, alarm);
361 spin_unlock_irqrestore(&base->lock, flags);
367 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
368 * @alarm: ptr to alarm to be canceled
370 * Returns 1 if the timer was canceled, 0 if it was not active.
372 int alarm_cancel(struct alarm *alarm)
375 int ret = alarm_try_to_cancel(alarm);
383 u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
388 delta = ktime_sub(now, alarm->node.expires);
393 if (unlikely(delta.tv64 >= interval.tv64)) {
394 s64 incr = ktime_to_ns(interval);
396 overrun = ktime_divns(delta, incr);
398 alarm->node.expires = ktime_add_ns(alarm->node.expires,
401 if (alarm->node.expires.tv64 > now.tv64)
404 * This (and the ktime_add() below) is the
405 * correction for exact:
410 alarm->node.expires = ktime_add(alarm->node.expires, interval);
418 * clock2alarm - helper that converts from clockid to alarmtypes
421 static enum alarmtimer_type clock2alarm(clockid_t clockid)
423 if (clockid == CLOCK_REALTIME_ALARM)
424 return ALARM_REALTIME;
425 if (clockid == CLOCK_BOOTTIME_ALARM)
426 return ALARM_BOOTTIME;
431 * alarm_handle_timer - Callback for posix timers
432 * @alarm: alarm that fired
434 * Posix timer callback for expired alarm timers.
436 static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
439 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
440 it.alarm.alarmtimer);
441 if (posix_timer_event(ptr, 0) != 0)
444 /* Re-add periodic timers */
445 if (ptr->it.alarm.interval.tv64) {
446 ptr->it_overrun += alarm_forward(alarm, now,
447 ptr->it.alarm.interval);
448 return ALARMTIMER_RESTART;
450 return ALARMTIMER_NORESTART;
454 * alarm_clock_getres - posix getres interface
455 * @which_clock: clockid
456 * @tp: timespec to fill
458 * Returns the granularity of underlying alarm base clock
460 static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
462 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
464 if (!alarmtimer_get_rtcdev())
467 return hrtimer_get_res(baseid, tp);
471 * alarm_clock_get - posix clock_get interface
472 * @which_clock: clockid
473 * @tp: timespec to fill.
475 * Provides the underlying alarm base time.
477 static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
479 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
481 if (!alarmtimer_get_rtcdev())
484 *tp = ktime_to_timespec(base->gettime());
489 * alarm_timer_create - posix timer_create interface
490 * @new_timer: k_itimer pointer to manage
492 * Initializes the k_itimer structure.
494 static int alarm_timer_create(struct k_itimer *new_timer)
496 enum alarmtimer_type type;
497 struct alarm_base *base;
499 if (!alarmtimer_get_rtcdev())
502 if (!capable(CAP_WAKE_ALARM))
505 type = clock2alarm(new_timer->it_clock);
506 base = &alarm_bases[type];
507 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
512 * alarm_timer_get - posix timer_get interface
513 * @new_timer: k_itimer pointer
514 * @cur_setting: itimerspec data to fill
516 * Copies the itimerspec data out from the k_itimer
518 static void alarm_timer_get(struct k_itimer *timr,
519 struct itimerspec *cur_setting)
521 memset(cur_setting, 0, sizeof(struct itimerspec));
523 cur_setting->it_interval =
524 ktime_to_timespec(timr->it.alarm.interval);
525 cur_setting->it_value =
526 ktime_to_timespec(timr->it.alarm.alarmtimer.node.expires);
531 * alarm_timer_del - posix timer_del interface
532 * @timr: k_itimer pointer to be deleted
534 * Cancels any programmed alarms for the given timer.
536 static int alarm_timer_del(struct k_itimer *timr)
541 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
548 * alarm_timer_set - posix timer_set interface
549 * @timr: k_itimer pointer to be deleted
550 * @flags: timer flags
551 * @new_setting: itimerspec to be used
552 * @old_setting: itimerspec being replaced
554 * Sets the timer to new_setting, and starts the timer.
556 static int alarm_timer_set(struct k_itimer *timr, int flags,
557 struct itimerspec *new_setting,
558 struct itimerspec *old_setting)
564 alarm_timer_get(timr, old_setting);
566 /* If the timer was already set, cancel it */
567 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
570 /* start the timer */
571 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
572 alarm_start(&timr->it.alarm.alarmtimer,
573 timespec_to_ktime(new_setting->it_value));
578 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
579 * @alarm: ptr to alarm that fired
581 * Wakes up the task that set the alarmtimer
583 static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
586 struct task_struct *task = (struct task_struct *)alarm->data;
590 wake_up_process(task);
591 return ALARMTIMER_NORESTART;
595 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
596 * @alarm: ptr to alarmtimer
597 * @absexp: absolute expiration time
599 * Sets the alarm timer and sleeps until it is fired or interrupted.
601 static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
603 alarm->data = (void *)current;
605 set_current_state(TASK_INTERRUPTIBLE);
606 alarm_start(alarm, absexp);
607 if (likely(alarm->data))
611 } while (alarm->data && !signal_pending(current));
613 __set_current_state(TASK_RUNNING);
615 return (alarm->data == NULL);
620 * update_rmtp - Update remaining timespec value
621 * @exp: expiration time
623 * @rmtp: user pointer to remaining timepsec value
625 * Helper function that fills in rmtp value with time between
626 * now and the exp value
628 static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
629 struct timespec __user *rmtp)
634 rem = ktime_sub(exp, alarm_bases[type].gettime());
638 rmt = ktime_to_timespec(rem);
640 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
648 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
649 * @restart: ptr to restart block
651 * Handles restarted clock_nanosleep calls
653 static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
655 enum alarmtimer_type type = restart->nanosleep.clockid;
657 struct timespec __user *rmtp;
661 exp.tv64 = restart->nanosleep.expires;
662 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
664 if (alarmtimer_do_nsleep(&alarm, exp))
667 if (freezing(current))
668 alarmtimer_freezerset(exp, type);
670 rmtp = restart->nanosleep.rmtp;
672 ret = update_rmtp(exp, type, rmtp);
678 /* The other values in restart are already filled in */
679 ret = -ERESTART_RESTARTBLOCK;
685 * alarm_timer_nsleep - alarmtimer nanosleep
686 * @which_clock: clockid
687 * @flags: determins abstime or relative
688 * @tsreq: requested sleep time (abs or rel)
689 * @rmtp: remaining sleep time saved
691 * Handles clock_nanosleep calls against _ALARM clockids
693 static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
694 struct timespec *tsreq, struct timespec __user *rmtp)
696 enum alarmtimer_type type = clock2alarm(which_clock);
700 struct restart_block *restart;
702 if (!alarmtimer_get_rtcdev())
705 if (!capable(CAP_WAKE_ALARM))
708 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
710 exp = timespec_to_ktime(*tsreq);
711 /* Convert (if necessary) to absolute time */
712 if (flags != TIMER_ABSTIME) {
713 ktime_t now = alarm_bases[type].gettime();
714 exp = ktime_add(now, exp);
717 if (alarmtimer_do_nsleep(&alarm, exp))
720 if (freezing(current))
721 alarmtimer_freezerset(exp, type);
723 /* abs timers don't set remaining time or restart */
724 if (flags == TIMER_ABSTIME) {
725 ret = -ERESTARTNOHAND;
730 ret = update_rmtp(exp, type, rmtp);
735 restart = ¤t_thread_info()->restart_block;
736 restart->fn = alarm_timer_nsleep_restart;
737 restart->nanosleep.clockid = type;
738 restart->nanosleep.expires = exp.tv64;
739 restart->nanosleep.rmtp = rmtp;
740 ret = -ERESTART_RESTARTBLOCK;
747 /* Suspend hook structures */
748 static const struct dev_pm_ops alarmtimer_pm_ops = {
749 .suspend = alarmtimer_suspend,
752 static struct platform_driver alarmtimer_driver = {
754 .name = "alarmtimer",
755 .pm = &alarmtimer_pm_ops,
760 * alarmtimer_init - Initialize alarm timer code
762 * This function initializes the alarm bases and registers
763 * the posix clock ids.
765 static int __init alarmtimer_init(void)
769 struct k_clock alarm_clock = {
770 .clock_getres = alarm_clock_getres,
771 .clock_get = alarm_clock_get,
772 .timer_create = alarm_timer_create,
773 .timer_set = alarm_timer_set,
774 .timer_del = alarm_timer_del,
775 .timer_get = alarm_timer_get,
776 .nsleep = alarm_timer_nsleep,
779 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
780 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
782 /* Initialize alarm bases */
783 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
784 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
785 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
786 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
787 for (i = 0; i < ALARM_NUMTYPE; i++) {
788 timerqueue_init_head(&alarm_bases[i].timerqueue);
789 spin_lock_init(&alarm_bases[i].lock);
790 hrtimer_init(&alarm_bases[i].timer,
791 alarm_bases[i].base_clockid,
793 alarm_bases[i].timer.function = alarmtimer_fired;
796 alarmtimer_rtc_interface_setup();
797 error = platform_driver_register(&alarmtimer_driver);
798 platform_device_register_simple("alarmtimer", -1, NULL, 0);
802 device_initcall(alarmtimer_init);