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 int alarmtimer_rtc_interface_setup(void)
105 alarmtimer_rtc_interface
.class = rtc_class
;
106 return class_interface_register(&alarmtimer_rtc_interface
);
108 static void alarmtimer_rtc_interface_remove(void)
110 class_interface_unregister(&alarmtimer_rtc_interface
);
113 static inline struct rtc_device
*alarmtimer_get_rtcdev(void)
117 #define rtcdev (NULL)
118 static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
119 static inline void alarmtimer_rtc_interface_remove(void) { }
123 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
124 * @base: pointer to the base where the timer is being run
125 * @alarm: pointer to alarm being enqueued.
127 * Adds alarm to a alarm_base timerqueue and if necessary sets
130 * Must hold base->lock when calling.
132 static void alarmtimer_enqueue(struct alarm_base
*base
, struct alarm
*alarm
)
134 timerqueue_add(&base
->timerqueue
, &alarm
->node
);
135 alarm
->state
|= ALARMTIMER_STATE_ENQUEUED
;
137 if (&alarm
->node
== timerqueue_getnext(&base
->timerqueue
)) {
138 hrtimer_try_to_cancel(&base
->timer
);
139 hrtimer_start(&base
->timer
, alarm
->node
.expires
,
145 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
146 * @base: pointer to the base where the timer is running
147 * @alarm: pointer to alarm being removed
149 * Removes alarm to a alarm_base timerqueue and if necessary sets
150 * a new timer to run.
152 * Must hold base->lock when calling.
154 static void alarmtimer_remove(struct alarm_base
*base
, struct alarm
*alarm
)
156 struct timerqueue_node
*next
= timerqueue_getnext(&base
->timerqueue
);
158 if (!(alarm
->state
& ALARMTIMER_STATE_ENQUEUED
))
161 timerqueue_del(&base
->timerqueue
, &alarm
->node
);
162 alarm
->state
&= ~ALARMTIMER_STATE_ENQUEUED
;
164 if (next
== &alarm
->node
) {
165 hrtimer_try_to_cancel(&base
->timer
);
166 next
= timerqueue_getnext(&base
->timerqueue
);
169 hrtimer_start(&base
->timer
, next
->expires
, HRTIMER_MODE_ABS
);
175 * alarmtimer_fired - Handles alarm hrtimer being fired.
176 * @timer: pointer to hrtimer being run
178 * When a alarm timer fires, this runs through the timerqueue to
179 * see which alarms expired, and runs those. If there are more alarm
180 * timers queued for the future, we set the hrtimer to fire when
181 * when the next future alarm timer expires.
183 static enum hrtimer_restart
alarmtimer_fired(struct hrtimer
*timer
)
185 struct alarm_base
*base
= container_of(timer
, struct alarm_base
, timer
);
186 struct timerqueue_node
*next
;
189 int ret
= HRTIMER_NORESTART
;
190 int restart
= ALARMTIMER_NORESTART
;
192 spin_lock_irqsave(&base
->lock
, flags
);
193 now
= base
->gettime();
194 while ((next
= timerqueue_getnext(&base
->timerqueue
))) {
196 ktime_t expired
= next
->expires
;
198 if (expired
.tv64
>= now
.tv64
)
201 alarm
= container_of(next
, struct alarm
, node
);
203 timerqueue_del(&base
->timerqueue
, &alarm
->node
);
204 alarm
->state
&= ~ALARMTIMER_STATE_ENQUEUED
;
206 alarm
->state
|= ALARMTIMER_STATE_CALLBACK
;
207 spin_unlock_irqrestore(&base
->lock
, flags
);
209 restart
= alarm
->function(alarm
, now
);
210 spin_lock_irqsave(&base
->lock
, flags
);
211 alarm
->state
&= ~ALARMTIMER_STATE_CALLBACK
;
213 if (restart
!= ALARMTIMER_NORESTART
) {
214 timerqueue_add(&base
->timerqueue
, &alarm
->node
);
215 alarm
->state
|= ALARMTIMER_STATE_ENQUEUED
;
220 hrtimer_set_expires(&base
->timer
, next
->expires
);
221 ret
= HRTIMER_RESTART
;
223 spin_unlock_irqrestore(&base
->lock
, flags
);
229 #ifdef CONFIG_RTC_CLASS
231 * alarmtimer_suspend - Suspend time callback
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.
240 static int alarmtimer_suspend(struct device
*dev
)
245 struct rtc_device
*rtc
;
248 spin_lock_irqsave(&freezer_delta_lock
, flags
);
250 freezer_delta
= ktime_set(0, 0);
251 spin_unlock_irqrestore(&freezer_delta_lock
, flags
);
253 rtc
= alarmtimer_get_rtcdev();
254 /* If we have no rtcdev, just return */
258 /* Find the soonest timer to expire*/
259 for (i
= 0; i
< ALARM_NUMTYPE
; i
++) {
260 struct alarm_base
*base
= &alarm_bases
[i
];
261 struct timerqueue_node
*next
;
264 spin_lock_irqsave(&base
->lock
, flags
);
265 next
= timerqueue_getnext(&base
->timerqueue
);
266 spin_unlock_irqrestore(&base
->lock
, flags
);
269 delta
= ktime_sub(next
->expires
, base
->gettime());
270 if (!min
.tv64
|| (delta
.tv64
< min
.tv64
))
276 /* XXX - Should we enforce a minimum sleep time? */
277 WARN_ON(min
.tv64
< NSEC_PER_SEC
);
279 /* Setup an rtc timer to fire that far in the future */
280 rtc_timer_cancel(rtc
, &rtctimer
);
281 rtc_read_time(rtc
, &tm
);
282 now
= rtc_tm_to_ktime(tm
);
283 now
= ktime_add(now
, min
);
285 rtc_timer_start(rtc
, &rtctimer
, now
, ktime_set(0, 0));
290 static int alarmtimer_suspend(struct device
*dev
)
296 static void alarmtimer_freezerset(ktime_t absexp
, enum alarmtimer_type type
)
300 struct alarm_base
*base
= &alarm_bases
[type
];
302 delta
= ktime_sub(absexp
, base
->gettime());
304 spin_lock_irqsave(&freezer_delta_lock
, flags
);
305 if (!freezer_delta
.tv64
|| (delta
.tv64
< freezer_delta
.tv64
))
306 freezer_delta
= delta
;
307 spin_unlock_irqrestore(&freezer_delta_lock
, flags
);
312 * alarm_init - Initialize an alarm structure
313 * @alarm: ptr to alarm to be initialized
314 * @type: the type of the alarm
315 * @function: callback that is run when the alarm fires
317 void alarm_init(struct alarm
*alarm
, enum alarmtimer_type type
,
318 enum alarmtimer_restart (*function
)(struct alarm
*, ktime_t
))
320 timerqueue_init(&alarm
->node
);
321 alarm
->function
= function
;
323 alarm
->state
= ALARMTIMER_STATE_INACTIVE
;
327 * alarm_start - Sets an alarm to fire
328 * @alarm: ptr to alarm to set
329 * @start: time to run the alarm
331 void alarm_start(struct alarm
*alarm
, ktime_t start
)
333 struct alarm_base
*base
= &alarm_bases
[alarm
->type
];
336 spin_lock_irqsave(&base
->lock
, flags
);
337 if (alarmtimer_active(alarm
))
338 alarmtimer_remove(base
, alarm
);
339 alarm
->node
.expires
= start
;
340 alarmtimer_enqueue(base
, alarm
);
341 spin_unlock_irqrestore(&base
->lock
, flags
);
345 * alarm_try_to_cancel - Tries to cancel an alarm timer
346 * @alarm: ptr to alarm to be canceled
348 * Returns 1 if the timer was canceled, 0 if it was not running,
349 * and -1 if the callback was running
351 int alarm_try_to_cancel(struct alarm
*alarm
)
353 struct alarm_base
*base
= &alarm_bases
[alarm
->type
];
356 spin_lock_irqsave(&base
->lock
, flags
);
358 if (alarmtimer_callback_running(alarm
))
361 if (alarmtimer_is_queued(alarm
)) {
362 alarmtimer_remove(base
, alarm
);
367 spin_unlock_irqrestore(&base
->lock
, flags
);
373 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
374 * @alarm: ptr to alarm to be canceled
376 * Returns 1 if the timer was canceled, 0 if it was not active.
378 int alarm_cancel(struct alarm
*alarm
)
381 int ret
= alarm_try_to_cancel(alarm
);
389 u64
alarm_forward(struct alarm
*alarm
, ktime_t now
, ktime_t interval
)
394 delta
= ktime_sub(now
, alarm
->node
.expires
);
399 if (unlikely(delta
.tv64
>= interval
.tv64
)) {
400 s64 incr
= ktime_to_ns(interval
);
402 overrun
= ktime_divns(delta
, incr
);
404 alarm
->node
.expires
= ktime_add_ns(alarm
->node
.expires
,
407 if (alarm
->node
.expires
.tv64
> now
.tv64
)
410 * This (and the ktime_add() below) is the
411 * correction for exact:
416 alarm
->node
.expires
= ktime_add(alarm
->node
.expires
, interval
);
424 * clock2alarm - helper that converts from clockid to alarmtypes
427 static enum alarmtimer_type
clock2alarm(clockid_t clockid
)
429 if (clockid
== CLOCK_REALTIME_ALARM
)
430 return ALARM_REALTIME
;
431 if (clockid
== CLOCK_BOOTTIME_ALARM
)
432 return ALARM_BOOTTIME
;
437 * alarm_handle_timer - Callback for posix timers
438 * @alarm: alarm that fired
440 * Posix timer callback for expired alarm timers.
442 static enum alarmtimer_restart
alarm_handle_timer(struct alarm
*alarm
,
445 struct k_itimer
*ptr
= container_of(alarm
, struct k_itimer
,
446 it
.alarm
.alarmtimer
);
447 if (posix_timer_event(ptr
, 0) != 0)
450 /* Re-add periodic timers */
451 if (ptr
->it
.alarm
.interval
.tv64
) {
452 ptr
->it_overrun
+= alarm_forward(alarm
, now
,
453 ptr
->it
.alarm
.interval
);
454 return ALARMTIMER_RESTART
;
456 return ALARMTIMER_NORESTART
;
460 * alarm_clock_getres - posix getres interface
461 * @which_clock: clockid
462 * @tp: timespec to fill
464 * Returns the granularity of underlying alarm base clock
466 static int alarm_clock_getres(const clockid_t which_clock
, struct timespec
*tp
)
468 clockid_t baseid
= alarm_bases
[clock2alarm(which_clock
)].base_clockid
;
470 if (!alarmtimer_get_rtcdev())
473 return hrtimer_get_res(baseid
, tp
);
477 * alarm_clock_get - posix clock_get interface
478 * @which_clock: clockid
479 * @tp: timespec to fill.
481 * Provides the underlying alarm base time.
483 static int alarm_clock_get(clockid_t which_clock
, struct timespec
*tp
)
485 struct alarm_base
*base
= &alarm_bases
[clock2alarm(which_clock
)];
487 if (!alarmtimer_get_rtcdev())
490 *tp
= ktime_to_timespec(base
->gettime());
495 * alarm_timer_create - posix timer_create interface
496 * @new_timer: k_itimer pointer to manage
498 * Initializes the k_itimer structure.
500 static int alarm_timer_create(struct k_itimer
*new_timer
)
502 enum alarmtimer_type type
;
503 struct alarm_base
*base
;
505 if (!alarmtimer_get_rtcdev())
508 if (!capable(CAP_WAKE_ALARM
))
511 type
= clock2alarm(new_timer
->it_clock
);
512 base
= &alarm_bases
[type
];
513 alarm_init(&new_timer
->it
.alarm
.alarmtimer
, type
, alarm_handle_timer
);
518 * alarm_timer_get - posix timer_get interface
519 * @new_timer: k_itimer pointer
520 * @cur_setting: itimerspec data to fill
522 * Copies the itimerspec data out from the k_itimer
524 static void alarm_timer_get(struct k_itimer
*timr
,
525 struct itimerspec
*cur_setting
)
527 memset(cur_setting
, 0, sizeof(struct itimerspec
));
529 cur_setting
->it_interval
=
530 ktime_to_timespec(timr
->it
.alarm
.interval
);
531 cur_setting
->it_value
=
532 ktime_to_timespec(timr
->it
.alarm
.alarmtimer
.node
.expires
);
537 * alarm_timer_del - posix timer_del interface
538 * @timr: k_itimer pointer to be deleted
540 * Cancels any programmed alarms for the given timer.
542 static int alarm_timer_del(struct k_itimer
*timr
)
547 if (alarm_try_to_cancel(&timr
->it
.alarm
.alarmtimer
) < 0)
554 * alarm_timer_set - posix timer_set interface
555 * @timr: k_itimer pointer to be deleted
556 * @flags: timer flags
557 * @new_setting: itimerspec to be used
558 * @old_setting: itimerspec being replaced
560 * Sets the timer to new_setting, and starts the timer.
562 static int alarm_timer_set(struct k_itimer
*timr
, int flags
,
563 struct itimerspec
*new_setting
,
564 struct itimerspec
*old_setting
)
570 alarm_timer_get(timr
, old_setting
);
572 /* If the timer was already set, cancel it */
573 if (alarm_try_to_cancel(&timr
->it
.alarm
.alarmtimer
) < 0)
576 /* start the timer */
577 timr
->it
.alarm
.interval
= timespec_to_ktime(new_setting
->it_interval
);
578 alarm_start(&timr
->it
.alarm
.alarmtimer
,
579 timespec_to_ktime(new_setting
->it_value
));
584 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
585 * @alarm: ptr to alarm that fired
587 * Wakes up the task that set the alarmtimer
589 static enum alarmtimer_restart
alarmtimer_nsleep_wakeup(struct alarm
*alarm
,
592 struct task_struct
*task
= (struct task_struct
*)alarm
->data
;
596 wake_up_process(task
);
597 return ALARMTIMER_NORESTART
;
601 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
602 * @alarm: ptr to alarmtimer
603 * @absexp: absolute expiration time
605 * Sets the alarm timer and sleeps until it is fired or interrupted.
607 static int alarmtimer_do_nsleep(struct alarm
*alarm
, ktime_t absexp
)
609 alarm
->data
= (void *)current
;
611 set_current_state(TASK_INTERRUPTIBLE
);
612 alarm_start(alarm
, absexp
);
613 if (likely(alarm
->data
))
617 } while (alarm
->data
&& !signal_pending(current
));
619 __set_current_state(TASK_RUNNING
);
621 return (alarm
->data
== NULL
);
626 * update_rmtp - Update remaining timespec value
627 * @exp: expiration time
629 * @rmtp: user pointer to remaining timepsec value
631 * Helper function that fills in rmtp value with time between
632 * now and the exp value
634 static int update_rmtp(ktime_t exp
, enum alarmtimer_type type
,
635 struct timespec __user
*rmtp
)
640 rem
= ktime_sub(exp
, alarm_bases
[type
].gettime());
644 rmt
= ktime_to_timespec(rem
);
646 if (copy_to_user(rmtp
, &rmt
, sizeof(*rmtp
)))
654 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
655 * @restart: ptr to restart block
657 * Handles restarted clock_nanosleep calls
659 static long __sched
alarm_timer_nsleep_restart(struct restart_block
*restart
)
661 enum alarmtimer_type type
= restart
->nanosleep
.clockid
;
663 struct timespec __user
*rmtp
;
667 exp
.tv64
= restart
->nanosleep
.expires
;
668 alarm_init(&alarm
, type
, alarmtimer_nsleep_wakeup
);
670 if (alarmtimer_do_nsleep(&alarm
, exp
))
673 if (freezing(current
))
674 alarmtimer_freezerset(exp
, type
);
676 rmtp
= restart
->nanosleep
.rmtp
;
678 ret
= update_rmtp(exp
, type
, rmtp
);
684 /* The other values in restart are already filled in */
685 ret
= -ERESTART_RESTARTBLOCK
;
691 * alarm_timer_nsleep - alarmtimer nanosleep
692 * @which_clock: clockid
693 * @flags: determins abstime or relative
694 * @tsreq: requested sleep time (abs or rel)
695 * @rmtp: remaining sleep time saved
697 * Handles clock_nanosleep calls against _ALARM clockids
699 static int alarm_timer_nsleep(const clockid_t which_clock
, int flags
,
700 struct timespec
*tsreq
, struct timespec __user
*rmtp
)
702 enum alarmtimer_type type
= clock2alarm(which_clock
);
706 struct restart_block
*restart
;
708 if (!alarmtimer_get_rtcdev())
711 if (!capable(CAP_WAKE_ALARM
))
714 alarm_init(&alarm
, type
, alarmtimer_nsleep_wakeup
);
716 exp
= timespec_to_ktime(*tsreq
);
717 /* Convert (if necessary) to absolute time */
718 if (flags
!= TIMER_ABSTIME
) {
719 ktime_t now
= alarm_bases
[type
].gettime();
720 exp
= ktime_add(now
, exp
);
723 if (alarmtimer_do_nsleep(&alarm
, exp
))
726 if (freezing(current
))
727 alarmtimer_freezerset(exp
, type
);
729 /* abs timers don't set remaining time or restart */
730 if (flags
== TIMER_ABSTIME
) {
731 ret
= -ERESTARTNOHAND
;
736 ret
= update_rmtp(exp
, type
, rmtp
);
741 restart
= ¤t_thread_info()->restart_block
;
742 restart
->fn
= alarm_timer_nsleep_restart
;
743 restart
->nanosleep
.clockid
= type
;
744 restart
->nanosleep
.expires
= exp
.tv64
;
745 restart
->nanosleep
.rmtp
= rmtp
;
746 ret
= -ERESTART_RESTARTBLOCK
;
753 /* Suspend hook structures */
754 static const struct dev_pm_ops alarmtimer_pm_ops
= {
755 .suspend
= alarmtimer_suspend
,
758 static struct platform_driver alarmtimer_driver
= {
760 .name
= "alarmtimer",
761 .pm
= &alarmtimer_pm_ops
,
766 * alarmtimer_init - Initialize alarm timer code
768 * This function initializes the alarm bases and registers
769 * the posix clock ids.
771 static int __init
alarmtimer_init(void)
773 struct platform_device
*pdev
;
776 struct k_clock alarm_clock
= {
777 .clock_getres
= alarm_clock_getres
,
778 .clock_get
= alarm_clock_get
,
779 .timer_create
= alarm_timer_create
,
780 .timer_set
= alarm_timer_set
,
781 .timer_del
= alarm_timer_del
,
782 .timer_get
= alarm_timer_get
,
783 .nsleep
= alarm_timer_nsleep
,
786 posix_timers_register_clock(CLOCK_REALTIME_ALARM
, &alarm_clock
);
787 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM
, &alarm_clock
);
789 /* Initialize alarm bases */
790 alarm_bases
[ALARM_REALTIME
].base_clockid
= CLOCK_REALTIME
;
791 alarm_bases
[ALARM_REALTIME
].gettime
= &ktime_get_real
;
792 alarm_bases
[ALARM_BOOTTIME
].base_clockid
= CLOCK_BOOTTIME
;
793 alarm_bases
[ALARM_BOOTTIME
].gettime
= &ktime_get_boottime
;
794 for (i
= 0; i
< ALARM_NUMTYPE
; i
++) {
795 timerqueue_init_head(&alarm_bases
[i
].timerqueue
);
796 spin_lock_init(&alarm_bases
[i
].lock
);
797 hrtimer_init(&alarm_bases
[i
].timer
,
798 alarm_bases
[i
].base_clockid
,
800 alarm_bases
[i
].timer
.function
= alarmtimer_fired
;
803 error
= alarmtimer_rtc_interface_setup();
807 error
= platform_driver_register(&alarmtimer_driver
);
811 pdev
= platform_device_register_simple("alarmtimer", -1, NULL
, 0);
813 error
= PTR_ERR(pdev
);
819 platform_driver_unregister(&alarmtimer_driver
);
821 alarmtimer_rtc_interface_remove();
824 device_initcall(alarmtimer_init
);