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
;
40 ktime_t (*gettime
)(void);
41 clockid_t base_clockid
;
42 } alarm_bases
[ALARM_NUMTYPE
];
44 /* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
45 static ktime_t freezer_delta
;
46 static DEFINE_SPINLOCK(freezer_delta_lock
);
48 static struct wakeup_source
*ws
;
50 #ifdef CONFIG_RTC_CLASS
51 /* rtc timer and device for setting alarm wakeups at suspend */
52 static struct rtc_timer rtctimer
;
53 static struct rtc_device
*rtcdev
;
54 static DEFINE_SPINLOCK(rtcdev_lock
);
57 * alarmtimer_get_rtcdev - Return selected rtcdevice
59 * This function returns the rtc device to use for wakealarms.
60 * If one has not already been chosen, it checks to see if a
61 * functional rtc device is available.
63 struct rtc_device
*alarmtimer_get_rtcdev(void)
66 struct rtc_device
*ret
;
68 spin_lock_irqsave(&rtcdev_lock
, flags
);
70 spin_unlock_irqrestore(&rtcdev_lock
, flags
);
76 static int alarmtimer_rtc_add_device(struct device
*dev
,
77 struct class_interface
*class_intf
)
80 struct rtc_device
*rtc
= to_rtc_device(dev
);
85 if (!rtc
->ops
->set_alarm
)
87 if (!device_may_wakeup(rtc
->dev
.parent
))
90 spin_lock_irqsave(&rtcdev_lock
, flags
);
93 /* hold a reference so it doesn't go away */
96 spin_unlock_irqrestore(&rtcdev_lock
, flags
);
100 static inline void alarmtimer_rtc_timer_init(void)
102 rtc_timer_init(&rtctimer
, NULL
, NULL
);
105 static struct class_interface alarmtimer_rtc_interface
= {
106 .add_dev
= &alarmtimer_rtc_add_device
,
109 static int alarmtimer_rtc_interface_setup(void)
111 alarmtimer_rtc_interface
.class = rtc_class
;
112 return class_interface_register(&alarmtimer_rtc_interface
);
114 static void alarmtimer_rtc_interface_remove(void)
116 class_interface_unregister(&alarmtimer_rtc_interface
);
119 struct rtc_device
*alarmtimer_get_rtcdev(void)
123 #define rtcdev (NULL)
124 static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
125 static inline void alarmtimer_rtc_interface_remove(void) { }
126 static inline void alarmtimer_rtc_timer_init(void) { }
130 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
131 * @base: pointer to the base where the timer is being run
132 * @alarm: pointer to alarm being enqueued.
134 * Adds alarm to a alarm_base timerqueue
136 * Must hold base->lock when calling.
138 static void alarmtimer_enqueue(struct alarm_base
*base
, struct alarm
*alarm
)
140 if (alarm
->state
& ALARMTIMER_STATE_ENQUEUED
)
141 timerqueue_del(&base
->timerqueue
, &alarm
->node
);
143 timerqueue_add(&base
->timerqueue
, &alarm
->node
);
144 alarm
->state
|= ALARMTIMER_STATE_ENQUEUED
;
148 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
149 * @base: pointer to the base where the timer is running
150 * @alarm: pointer to alarm being removed
152 * Removes alarm to a alarm_base timerqueue
154 * Must hold base->lock when calling.
156 static void alarmtimer_dequeue(struct alarm_base
*base
, struct alarm
*alarm
)
158 if (!(alarm
->state
& ALARMTIMER_STATE_ENQUEUED
))
161 timerqueue_del(&base
->timerqueue
, &alarm
->node
);
162 alarm
->state
&= ~ALARMTIMER_STATE_ENQUEUED
;
167 * alarmtimer_fired - Handles alarm hrtimer being fired.
168 * @timer: pointer to hrtimer being run
170 * When a alarm timer fires, this runs through the timerqueue to
171 * see which alarms expired, and runs those. If there are more alarm
172 * timers queued for the future, we set the hrtimer to fire when
173 * when the next future alarm timer expires.
175 static enum hrtimer_restart
alarmtimer_fired(struct hrtimer
*timer
)
177 struct alarm
*alarm
= container_of(timer
, struct alarm
, timer
);
178 struct alarm_base
*base
= &alarm_bases
[alarm
->type
];
180 int ret
= HRTIMER_NORESTART
;
181 int restart
= ALARMTIMER_NORESTART
;
183 spin_lock_irqsave(&base
->lock
, flags
);
184 alarmtimer_dequeue(base
, alarm
);
185 spin_unlock_irqrestore(&base
->lock
, flags
);
188 restart
= alarm
->function(alarm
, base
->gettime());
190 spin_lock_irqsave(&base
->lock
, flags
);
191 if (restart
!= ALARMTIMER_NORESTART
) {
192 hrtimer_set_expires(&alarm
->timer
, alarm
->node
.expires
);
193 alarmtimer_enqueue(base
, alarm
);
194 ret
= HRTIMER_RESTART
;
196 spin_unlock_irqrestore(&base
->lock
, flags
);
202 #ifdef CONFIG_RTC_CLASS
204 * alarmtimer_suspend - Suspend time callback
208 * When we are going into suspend, we look through the bases
209 * to see which is the soonest timer to expire. We then
210 * set an rtc timer to fire that far into the future, which
211 * will wake us from suspend.
213 static int alarmtimer_suspend(struct device
*dev
)
218 struct rtc_device
*rtc
;
222 spin_lock_irqsave(&freezer_delta_lock
, flags
);
224 freezer_delta
= ktime_set(0, 0);
225 spin_unlock_irqrestore(&freezer_delta_lock
, flags
);
227 rtc
= alarmtimer_get_rtcdev();
228 /* If we have no rtcdev, just return */
232 /* Find the soonest timer to expire*/
233 for (i
= 0; i
< ALARM_NUMTYPE
; i
++) {
234 struct alarm_base
*base
= &alarm_bases
[i
];
235 struct timerqueue_node
*next
;
238 spin_lock_irqsave(&base
->lock
, flags
);
239 next
= timerqueue_getnext(&base
->timerqueue
);
240 spin_unlock_irqrestore(&base
->lock
, flags
);
243 delta
= ktime_sub(next
->expires
, base
->gettime());
244 if (!min
.tv64
|| (delta
.tv64
< min
.tv64
))
250 if (ktime_to_ns(min
) < 2 * NSEC_PER_SEC
) {
251 __pm_wakeup_event(ws
, 2 * MSEC_PER_SEC
);
255 /* Setup an rtc timer to fire that far in the future */
256 rtc_timer_cancel(rtc
, &rtctimer
);
257 rtc_read_time(rtc
, &tm
);
258 now
= rtc_tm_to_ktime(tm
);
259 now
= ktime_add(now
, min
);
261 /* Set alarm, if in the past reject suspend briefly to handle */
262 ret
= rtc_timer_start(rtc
, &rtctimer
, now
, ktime_set(0, 0));
264 __pm_wakeup_event(ws
, MSEC_PER_SEC
);
268 static int alarmtimer_suspend(struct device
*dev
)
274 static void alarmtimer_freezerset(ktime_t absexp
, enum alarmtimer_type type
)
278 struct alarm_base
*base
= &alarm_bases
[type
];
280 delta
= ktime_sub(absexp
, base
->gettime());
282 spin_lock_irqsave(&freezer_delta_lock
, flags
);
283 if (!freezer_delta
.tv64
|| (delta
.tv64
< freezer_delta
.tv64
))
284 freezer_delta
= delta
;
285 spin_unlock_irqrestore(&freezer_delta_lock
, flags
);
290 * alarm_init - Initialize an alarm structure
291 * @alarm: ptr to alarm to be initialized
292 * @type: the type of the alarm
293 * @function: callback that is run when the alarm fires
295 void alarm_init(struct alarm
*alarm
, enum alarmtimer_type type
,
296 enum alarmtimer_restart (*function
)(struct alarm
*, ktime_t
))
298 timerqueue_init(&alarm
->node
);
299 hrtimer_init(&alarm
->timer
, alarm_bases
[type
].base_clockid
,
301 alarm
->timer
.function
= alarmtimer_fired
;
302 alarm
->function
= function
;
304 alarm
->state
= ALARMTIMER_STATE_INACTIVE
;
308 * alarm_start - Sets an alarm to fire
309 * @alarm: ptr to alarm to set
310 * @start: time to run the alarm
312 int alarm_start(struct alarm
*alarm
, ktime_t start
)
314 struct alarm_base
*base
= &alarm_bases
[alarm
->type
];
318 spin_lock_irqsave(&base
->lock
, flags
);
319 alarm
->node
.expires
= start
;
320 alarmtimer_enqueue(base
, alarm
);
321 ret
= hrtimer_start(&alarm
->timer
, alarm
->node
.expires
,
323 spin_unlock_irqrestore(&base
->lock
, flags
);
328 * alarm_try_to_cancel - Tries to cancel an alarm timer
329 * @alarm: ptr to alarm to be canceled
331 * Returns 1 if the timer was canceled, 0 if it was not running,
332 * and -1 if the callback was running
334 int alarm_try_to_cancel(struct alarm
*alarm
)
336 struct alarm_base
*base
= &alarm_bases
[alarm
->type
];
340 spin_lock_irqsave(&base
->lock
, flags
);
341 ret
= hrtimer_try_to_cancel(&alarm
->timer
);
343 alarmtimer_dequeue(base
, alarm
);
344 spin_unlock_irqrestore(&base
->lock
, flags
);
350 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
351 * @alarm: ptr to alarm to be canceled
353 * Returns 1 if the timer was canceled, 0 if it was not active.
355 int alarm_cancel(struct alarm
*alarm
)
358 int ret
= alarm_try_to_cancel(alarm
);
366 u64
alarm_forward(struct alarm
*alarm
, ktime_t now
, ktime_t interval
)
371 delta
= ktime_sub(now
, alarm
->node
.expires
);
376 if (unlikely(delta
.tv64
>= interval
.tv64
)) {
377 s64 incr
= ktime_to_ns(interval
);
379 overrun
= ktime_divns(delta
, incr
);
381 alarm
->node
.expires
= ktime_add_ns(alarm
->node
.expires
,
384 if (alarm
->node
.expires
.tv64
> now
.tv64
)
387 * This (and the ktime_add() below) is the
388 * correction for exact:
393 alarm
->node
.expires
= ktime_add(alarm
->node
.expires
, interval
);
401 * clock2alarm - helper that converts from clockid to alarmtypes
404 static enum alarmtimer_type
clock2alarm(clockid_t clockid
)
406 if (clockid
== CLOCK_REALTIME_ALARM
)
407 return ALARM_REALTIME
;
408 if (clockid
== CLOCK_BOOTTIME_ALARM
)
409 return ALARM_BOOTTIME
;
414 * alarm_handle_timer - Callback for posix timers
415 * @alarm: alarm that fired
417 * Posix timer callback for expired alarm timers.
419 static enum alarmtimer_restart
alarm_handle_timer(struct alarm
*alarm
,
423 struct k_itimer
*ptr
= container_of(alarm
, struct k_itimer
,
424 it
.alarm
.alarmtimer
);
425 enum alarmtimer_restart result
= ALARMTIMER_NORESTART
;
427 spin_lock_irqsave(&ptr
->it_lock
, flags
);
428 if ((ptr
->it_sigev_notify
& ~SIGEV_THREAD_ID
) != SIGEV_NONE
) {
429 if (posix_timer_event(ptr
, 0) != 0)
433 /* Re-add periodic timers */
434 if (ptr
->it
.alarm
.interval
.tv64
) {
435 ptr
->it_overrun
+= alarm_forward(alarm
, now
,
436 ptr
->it
.alarm
.interval
);
437 result
= ALARMTIMER_RESTART
;
439 spin_unlock_irqrestore(&ptr
->it_lock
, flags
);
445 * alarm_clock_getres - posix getres interface
446 * @which_clock: clockid
447 * @tp: timespec to fill
449 * Returns the granularity of underlying alarm base clock
451 static int alarm_clock_getres(const clockid_t which_clock
, struct timespec
*tp
)
453 clockid_t baseid
= alarm_bases
[clock2alarm(which_clock
)].base_clockid
;
455 if (!alarmtimer_get_rtcdev())
458 return hrtimer_get_res(baseid
, tp
);
462 * alarm_clock_get - posix clock_get interface
463 * @which_clock: clockid
464 * @tp: timespec to fill.
466 * Provides the underlying alarm base time.
468 static int alarm_clock_get(clockid_t which_clock
, struct timespec
*tp
)
470 struct alarm_base
*base
= &alarm_bases
[clock2alarm(which_clock
)];
472 if (!alarmtimer_get_rtcdev())
475 *tp
= ktime_to_timespec(base
->gettime());
480 * alarm_timer_create - posix timer_create interface
481 * @new_timer: k_itimer pointer to manage
483 * Initializes the k_itimer structure.
485 static int alarm_timer_create(struct k_itimer
*new_timer
)
487 enum alarmtimer_type type
;
488 struct alarm_base
*base
;
490 if (!alarmtimer_get_rtcdev())
493 if (!capable(CAP_WAKE_ALARM
))
496 type
= clock2alarm(new_timer
->it_clock
);
497 base
= &alarm_bases
[type
];
498 alarm_init(&new_timer
->it
.alarm
.alarmtimer
, type
, alarm_handle_timer
);
503 * alarm_timer_get - posix timer_get interface
504 * @new_timer: k_itimer pointer
505 * @cur_setting: itimerspec data to fill
507 * Copies the itimerspec data out from the k_itimer
509 static void alarm_timer_get(struct k_itimer
*timr
,
510 struct itimerspec
*cur_setting
)
512 memset(cur_setting
, 0, sizeof(struct itimerspec
));
514 cur_setting
->it_interval
=
515 ktime_to_timespec(timr
->it
.alarm
.interval
);
516 cur_setting
->it_value
=
517 ktime_to_timespec(timr
->it
.alarm
.alarmtimer
.node
.expires
);
522 * alarm_timer_del - posix timer_del interface
523 * @timr: k_itimer pointer to be deleted
525 * Cancels any programmed alarms for the given timer.
527 static int alarm_timer_del(struct k_itimer
*timr
)
532 if (alarm_try_to_cancel(&timr
->it
.alarm
.alarmtimer
) < 0)
539 * alarm_timer_set - posix timer_set interface
540 * @timr: k_itimer pointer to be deleted
541 * @flags: timer flags
542 * @new_setting: itimerspec to be used
543 * @old_setting: itimerspec being replaced
545 * Sets the timer to new_setting, and starts the timer.
547 static int alarm_timer_set(struct k_itimer
*timr
, int flags
,
548 struct itimerspec
*new_setting
,
549 struct itimerspec
*old_setting
)
556 if (flags
& ~TIMER_ABSTIME
)
560 alarm_timer_get(timr
, old_setting
);
562 /* If the timer was already set, cancel it */
563 if (alarm_try_to_cancel(&timr
->it
.alarm
.alarmtimer
) < 0)
566 /* start the timer */
567 timr
->it
.alarm
.interval
= timespec_to_ktime(new_setting
->it_interval
);
568 exp
= timespec_to_ktime(new_setting
->it_value
);
569 /* Convert (if necessary) to absolute time */
570 if (flags
!= TIMER_ABSTIME
) {
573 now
= alarm_bases
[timr
->it
.alarm
.alarmtimer
.type
].gettime();
574 exp
= ktime_add(now
, exp
);
577 alarm_start(&timr
->it
.alarm
.alarmtimer
, exp
);
582 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
583 * @alarm: ptr to alarm that fired
585 * Wakes up the task that set the alarmtimer
587 static enum alarmtimer_restart
alarmtimer_nsleep_wakeup(struct alarm
*alarm
,
590 struct task_struct
*task
= (struct task_struct
*)alarm
->data
;
594 wake_up_process(task
);
595 return ALARMTIMER_NORESTART
;
599 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
600 * @alarm: ptr to alarmtimer
601 * @absexp: absolute expiration time
603 * Sets the alarm timer and sleeps until it is fired or interrupted.
605 static int alarmtimer_do_nsleep(struct alarm
*alarm
, ktime_t absexp
)
607 alarm
->data
= (void *)current
;
609 set_current_state(TASK_INTERRUPTIBLE
);
610 alarm_start(alarm
, absexp
);
611 if (likely(alarm
->data
))
615 } while (alarm
->data
&& !signal_pending(current
));
617 __set_current_state(TASK_RUNNING
);
619 return (alarm
->data
== NULL
);
624 * update_rmtp - Update remaining timespec value
625 * @exp: expiration time
627 * @rmtp: user pointer to remaining timepsec value
629 * Helper function that fills in rmtp value with time between
630 * now and the exp value
632 static int update_rmtp(ktime_t exp
, enum alarmtimer_type type
,
633 struct timespec __user
*rmtp
)
638 rem
= ktime_sub(exp
, alarm_bases
[type
].gettime());
642 rmt
= ktime_to_timespec(rem
);
644 if (copy_to_user(rmtp
, &rmt
, sizeof(*rmtp
)))
652 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
653 * @restart: ptr to restart block
655 * Handles restarted clock_nanosleep calls
657 static long __sched
alarm_timer_nsleep_restart(struct restart_block
*restart
)
659 enum alarmtimer_type type
= restart
->nanosleep
.clockid
;
661 struct timespec __user
*rmtp
;
665 exp
.tv64
= restart
->nanosleep
.expires
;
666 alarm_init(&alarm
, type
, alarmtimer_nsleep_wakeup
);
668 if (alarmtimer_do_nsleep(&alarm
, exp
))
671 if (freezing(current
))
672 alarmtimer_freezerset(exp
, type
);
674 rmtp
= restart
->nanosleep
.rmtp
;
676 ret
= update_rmtp(exp
, type
, rmtp
);
682 /* The other values in restart are already filled in */
683 ret
= -ERESTART_RESTARTBLOCK
;
689 * alarm_timer_nsleep - alarmtimer nanosleep
690 * @which_clock: clockid
691 * @flags: determins abstime or relative
692 * @tsreq: requested sleep time (abs or rel)
693 * @rmtp: remaining sleep time saved
695 * Handles clock_nanosleep calls against _ALARM clockids
697 static int alarm_timer_nsleep(const clockid_t which_clock
, int flags
,
698 struct timespec
*tsreq
, struct timespec __user
*rmtp
)
700 enum alarmtimer_type type
= clock2alarm(which_clock
);
704 struct restart_block
*restart
;
706 if (!alarmtimer_get_rtcdev())
709 if (flags
& ~TIMER_ABSTIME
)
712 if (!capable(CAP_WAKE_ALARM
))
715 alarm_init(&alarm
, type
, alarmtimer_nsleep_wakeup
);
717 exp
= timespec_to_ktime(*tsreq
);
718 /* Convert (if necessary) to absolute time */
719 if (flags
!= TIMER_ABSTIME
) {
720 ktime_t now
= alarm_bases
[type
].gettime();
721 exp
= ktime_add(now
, exp
);
724 if (alarmtimer_do_nsleep(&alarm
, exp
))
727 if (freezing(current
))
728 alarmtimer_freezerset(exp
, type
);
730 /* abs timers don't set remaining time or restart */
731 if (flags
== TIMER_ABSTIME
) {
732 ret
= -ERESTARTNOHAND
;
737 ret
= update_rmtp(exp
, type
, rmtp
);
742 restart
= ¤t_thread_info()->restart_block
;
743 restart
->fn
= alarm_timer_nsleep_restart
;
744 restart
->nanosleep
.clockid
= type
;
745 restart
->nanosleep
.expires
= exp
.tv64
;
746 restart
->nanosleep
.rmtp
= rmtp
;
747 ret
= -ERESTART_RESTARTBLOCK
;
754 /* Suspend hook structures */
755 static const struct dev_pm_ops alarmtimer_pm_ops
= {
756 .suspend
= alarmtimer_suspend
,
759 static struct platform_driver alarmtimer_driver
= {
761 .name
= "alarmtimer",
762 .pm
= &alarmtimer_pm_ops
,
767 * alarmtimer_init - Initialize alarm timer code
769 * This function initializes the alarm bases and registers
770 * the posix clock ids.
772 static int __init
alarmtimer_init(void)
774 struct platform_device
*pdev
;
777 struct k_clock alarm_clock
= {
778 .clock_getres
= alarm_clock_getres
,
779 .clock_get
= alarm_clock_get
,
780 .timer_create
= alarm_timer_create
,
781 .timer_set
= alarm_timer_set
,
782 .timer_del
= alarm_timer_del
,
783 .timer_get
= alarm_timer_get
,
784 .nsleep
= alarm_timer_nsleep
,
787 alarmtimer_rtc_timer_init();
789 posix_timers_register_clock(CLOCK_REALTIME_ALARM
, &alarm_clock
);
790 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM
, &alarm_clock
);
792 /* Initialize alarm bases */
793 alarm_bases
[ALARM_REALTIME
].base_clockid
= CLOCK_REALTIME
;
794 alarm_bases
[ALARM_REALTIME
].gettime
= &ktime_get_real
;
795 alarm_bases
[ALARM_BOOTTIME
].base_clockid
= CLOCK_BOOTTIME
;
796 alarm_bases
[ALARM_BOOTTIME
].gettime
= &ktime_get_boottime
;
797 for (i
= 0; i
< ALARM_NUMTYPE
; i
++) {
798 timerqueue_init_head(&alarm_bases
[i
].timerqueue
);
799 spin_lock_init(&alarm_bases
[i
].lock
);
802 error
= alarmtimer_rtc_interface_setup();
806 error
= platform_driver_register(&alarmtimer_driver
);
810 pdev
= platform_device_register_simple("alarmtimer", -1, NULL
, 0);
812 error
= PTR_ERR(pdev
);
815 ws
= wakeup_source_register("alarmtimer");
819 platform_driver_unregister(&alarmtimer_driver
);
821 alarmtimer_rtc_interface_remove();
824 device_initcall(alarmtimer_init
);