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
36 * @irqwork Delayed work structure for expiring timers
38 static struct alarm_base {
40 struct timerqueue_head timerqueue;
42 ktime_t (*gettime)(void);
43 clockid_t base_clockid;
44 struct work_struct irqwork;
45 } alarm_bases[ALARM_NUMTYPE];
47 /* rtc timer and device for setting alarm wakeups at suspend */
48 static struct rtc_timer rtctimer;
49 static struct rtc_device *rtcdev;
51 /* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
52 static ktime_t freezer_delta;
53 static DEFINE_SPINLOCK(freezer_delta_lock);
57 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
58 * @base: pointer to the base where the timer is being run
59 * @alarm: pointer to alarm being enqueued.
61 * Adds alarm to a alarm_base timerqueue and if necessary sets
64 * Must hold base->lock when calling.
66 static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
68 timerqueue_add(&base->timerqueue, &alarm->node);
69 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
70 hrtimer_try_to_cancel(&base->timer);
71 hrtimer_start(&base->timer, alarm->node.expires,
77 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
78 * @base: pointer to the base where the timer is running
79 * @alarm: pointer to alarm being removed
81 * Removes alarm to a alarm_base timerqueue and if necessary sets
84 * Must hold base->lock when calling.
86 static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
88 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
90 timerqueue_del(&base->timerqueue, &alarm->node);
91 if (next == &alarm->node) {
92 hrtimer_try_to_cancel(&base->timer);
93 next = timerqueue_getnext(&base->timerqueue);
96 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
101 * alarmtimer_do_work - Handles alarm being fired.
102 * @work: pointer to workqueue being run
104 * When a alarm timer fires, this runs through the timerqueue to
105 * see which alarms expired, and runs those. If there are more alarm
106 * timers queued for the future, we set the hrtimer to fire when
107 * when the next future alarm timer expires.
109 static void alarmtimer_do_work(struct work_struct *work)
111 struct alarm_base *base = container_of(work, struct alarm_base,
113 struct timerqueue_node *next;
117 spin_lock_irqsave(&base->lock, flags);
118 now = base->gettime();
119 while ((next = timerqueue_getnext(&base->timerqueue))) {
121 ktime_t expired = next->expires;
123 if (expired.tv64 >= now.tv64)
126 alarm = container_of(next, struct alarm, node);
128 timerqueue_del(&base->timerqueue, &alarm->node);
130 /* Re-add periodic timers */
131 if (alarm->period.tv64) {
132 alarm->node.expires = ktime_add(expired, alarm->period);
133 timerqueue_add(&base->timerqueue, &alarm->node);
136 spin_unlock_irqrestore(&base->lock, flags);
138 alarm->function(alarm);
139 spin_lock_irqsave(&base->lock, flags);
143 hrtimer_start(&base->timer, next->expires,
146 spin_unlock_irqrestore(&base->lock, flags);
151 * alarmtimer_fired - Handles alarm hrtimer being fired.
152 * @timer: pointer to hrtimer being run
154 * When a timer fires, this schedules the do_work function to
157 static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
159 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
160 schedule_work(&base->irqwork);
161 return HRTIMER_NORESTART;
166 * alarmtimer_suspend - Suspend time callback
170 * When we are going into suspend, we look through the bases
171 * to see which is the soonest timer to expire. We then
172 * set an rtc timer to fire that far into the future, which
173 * will wake us from suspend.
175 static int alarmtimer_suspend(struct device *dev)
182 spin_lock_irqsave(&freezer_delta_lock, flags);
184 freezer_delta = ktime_set(0, 0);
185 spin_unlock_irqrestore(&freezer_delta_lock, flags);
187 /* If we have no rtcdev, just return */
191 /* Find the soonest timer to expire*/
192 for (i = 0; i < ALARM_NUMTYPE; i++) {
193 struct alarm_base *base = &alarm_bases[i];
194 struct timerqueue_node *next;
197 spin_lock_irqsave(&base->lock, flags);
198 next = timerqueue_getnext(&base->timerqueue);
199 spin_unlock_irqrestore(&base->lock, flags);
202 delta = ktime_sub(next->expires, base->gettime());
203 if (!min.tv64 || (delta.tv64 < min.tv64))
209 /* XXX - Should we enforce a minimum sleep time? */
210 WARN_ON(min.tv64 < NSEC_PER_SEC);
212 /* Setup an rtc timer to fire that far in the future */
213 rtc_timer_cancel(rtcdev, &rtctimer);
214 rtc_read_time(rtcdev, &tm);
215 now = rtc_tm_to_ktime(tm);
216 now = ktime_add(now, min);
218 rtc_timer_start(rtcdev, &rtctimer, now, ktime_set(0, 0));
224 static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
228 struct alarm_base *base = &alarm_bases[type];
230 delta = ktime_sub(absexp, base->gettime());
232 spin_lock_irqsave(&freezer_delta_lock, flags);
233 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
234 freezer_delta = delta;
235 spin_unlock_irqrestore(&freezer_delta_lock, flags);
240 * alarm_init - Initialize an alarm structure
241 * @alarm: ptr to alarm to be initialized
242 * @type: the type of the alarm
243 * @function: callback that is run when the alarm fires
245 void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
246 void (*function)(struct alarm *))
248 timerqueue_init(&alarm->node);
249 alarm->period = ktime_set(0, 0);
250 alarm->function = function;
256 * alarm_start - Sets an alarm to fire
257 * @alarm: ptr to alarm to set
258 * @start: time to run the alarm
259 * @period: period at which the alarm will recur
261 void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period)
263 struct alarm_base *base = &alarm_bases[alarm->type];
266 spin_lock_irqsave(&base->lock, flags);
268 alarmtimer_remove(base, alarm);
269 alarm->node.expires = start;
270 alarm->period = period;
271 alarmtimer_enqueue(base, alarm);
273 spin_unlock_irqrestore(&base->lock, flags);
277 * alarm_cancel - Tries to cancel an alarm timer
278 * @alarm: ptr to alarm to be canceled
280 void alarm_cancel(struct alarm *alarm)
282 struct alarm_base *base = &alarm_bases[alarm->type];
285 spin_lock_irqsave(&base->lock, flags);
287 alarmtimer_remove(base, alarm);
289 spin_unlock_irqrestore(&base->lock, flags);
294 * clock2alarm - helper that converts from clockid to alarmtypes
297 static enum alarmtimer_type clock2alarm(clockid_t clockid)
299 if (clockid == CLOCK_REALTIME_ALARM)
300 return ALARM_REALTIME;
301 if (clockid == CLOCK_BOOTTIME_ALARM)
302 return ALARM_BOOTTIME;
307 * alarm_handle_timer - Callback for posix timers
308 * @alarm: alarm that fired
310 * Posix timer callback for expired alarm timers.
312 static void alarm_handle_timer(struct alarm *alarm)
314 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
316 if (posix_timer_event(ptr, 0) != 0)
321 * alarm_clock_getres - posix getres interface
322 * @which_clock: clockid
323 * @tp: timespec to fill
325 * Returns the granularity of underlying alarm base clock
327 static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
329 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
331 return hrtimer_get_res(baseid, tp);
335 * alarm_clock_get - posix clock_get interface
336 * @which_clock: clockid
337 * @tp: timespec to fill.
339 * Provides the underlying alarm base time.
341 static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
343 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
345 *tp = ktime_to_timespec(base->gettime());
350 * alarm_timer_create - posix timer_create interface
351 * @new_timer: k_itimer pointer to manage
353 * Initializes the k_itimer structure.
355 static int alarm_timer_create(struct k_itimer *new_timer)
357 enum alarmtimer_type type;
358 struct alarm_base *base;
360 if (!capable(CAP_WAKE_ALARM))
363 type = clock2alarm(new_timer->it_clock);
364 base = &alarm_bases[type];
365 alarm_init(&new_timer->it.alarmtimer, type, alarm_handle_timer);
370 * alarm_timer_get - posix timer_get interface
371 * @new_timer: k_itimer pointer
372 * @cur_setting: itimerspec data to fill
374 * Copies the itimerspec data out from the k_itimer
376 static void alarm_timer_get(struct k_itimer *timr,
377 struct itimerspec *cur_setting)
379 cur_setting->it_interval =
380 ktime_to_timespec(timr->it.alarmtimer.period);
381 cur_setting->it_value =
382 ktime_to_timespec(timr->it.alarmtimer.node.expires);
387 * alarm_timer_del - posix timer_del interface
388 * @timr: k_itimer pointer to be deleted
390 * Cancels any programmed alarms for the given timer.
392 static int alarm_timer_del(struct k_itimer *timr)
394 alarm_cancel(&timr->it.alarmtimer);
399 * alarm_timer_set - posix timer_set interface
400 * @timr: k_itimer pointer to be deleted
401 * @flags: timer flags
402 * @new_setting: itimerspec to be used
403 * @old_setting: itimerspec being replaced
405 * Sets the timer to new_setting, and starts the timer.
407 static int alarm_timer_set(struct k_itimer *timr, int flags,
408 struct itimerspec *new_setting,
409 struct itimerspec *old_setting)
411 /* Save old values */
412 old_setting->it_interval =
413 ktime_to_timespec(timr->it.alarmtimer.period);
414 old_setting->it_value =
415 ktime_to_timespec(timr->it.alarmtimer.node.expires);
417 /* If the timer was already set, cancel it */
418 alarm_cancel(&timr->it.alarmtimer);
420 /* start the timer */
421 alarm_start(&timr->it.alarmtimer,
422 timespec_to_ktime(new_setting->it_value),
423 timespec_to_ktime(new_setting->it_interval));
428 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
429 * @alarm: ptr to alarm that fired
431 * Wakes up the task that set the alarmtimer
433 static void alarmtimer_nsleep_wakeup(struct alarm *alarm)
435 struct task_struct *task = (struct task_struct *)alarm->data;
439 wake_up_process(task);
443 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
444 * @alarm: ptr to alarmtimer
445 * @absexp: absolute expiration time
447 * Sets the alarm timer and sleeps until it is fired or interrupted.
449 static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
451 alarm->data = (void *)current;
453 set_current_state(TASK_INTERRUPTIBLE);
454 alarm_start(alarm, absexp, ktime_set(0, 0));
455 if (likely(alarm->data))
459 } while (alarm->data && !signal_pending(current));
461 __set_current_state(TASK_RUNNING);
463 return (alarm->data == NULL);
468 * update_rmtp - Update remaining timespec value
469 * @exp: expiration time
471 * @rmtp: user pointer to remaining timepsec value
473 * Helper function that fills in rmtp value with time between
474 * now and the exp value
476 static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
477 struct timespec __user *rmtp)
482 rem = ktime_sub(exp, alarm_bases[type].gettime());
486 rmt = ktime_to_timespec(rem);
488 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
496 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
497 * @restart: ptr to restart block
499 * Handles restarted clock_nanosleep calls
501 static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
503 enum alarmtimer_type type = restart->nanosleep.index;
505 struct timespec __user *rmtp;
509 exp.tv64 = restart->nanosleep.expires;
510 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
512 if (alarmtimer_do_nsleep(&alarm, exp))
515 if (freezing(current))
516 alarmtimer_freezerset(exp, type);
518 rmtp = restart->nanosleep.rmtp;
520 ret = update_rmtp(exp, type, rmtp);
526 /* The other values in restart are already filled in */
527 ret = -ERESTART_RESTARTBLOCK;
533 * alarm_timer_nsleep - alarmtimer nanosleep
534 * @which_clock: clockid
535 * @flags: determins abstime or relative
536 * @tsreq: requested sleep time (abs or rel)
537 * @rmtp: remaining sleep time saved
539 * Handles clock_nanosleep calls against _ALARM clockids
541 static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
542 struct timespec *tsreq, struct timespec __user *rmtp)
544 enum alarmtimer_type type = clock2alarm(which_clock);
548 struct restart_block *restart;
550 if (!capable(CAP_WAKE_ALARM))
553 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
555 exp = timespec_to_ktime(*tsreq);
556 /* Convert (if necessary) to absolute time */
557 if (flags != TIMER_ABSTIME) {
558 ktime_t now = alarm_bases[type].gettime();
559 exp = ktime_add(now, exp);
562 if (alarmtimer_do_nsleep(&alarm, exp))
565 if (freezing(current))
566 alarmtimer_freezerset(exp, type);
568 /* abs timers don't set remaining time or restart */
569 if (flags == TIMER_ABSTIME) {
570 ret = -ERESTARTNOHAND;
575 ret = update_rmtp(exp, type, rmtp);
580 restart = ¤t_thread_info()->restart_block;
581 restart->fn = alarm_timer_nsleep_restart;
582 restart->nanosleep.index = type;
583 restart->nanosleep.expires = exp.tv64;
584 restart->nanosleep.rmtp = rmtp;
585 ret = -ERESTART_RESTARTBLOCK;
592 /* Suspend hook structures */
593 static const struct dev_pm_ops alarmtimer_pm_ops = {
594 .suspend = alarmtimer_suspend,
597 static struct platform_driver alarmtimer_driver = {
599 .name = "alarmtimer",
600 .pm = &alarmtimer_pm_ops,
605 * alarmtimer_init - Initialize alarm timer code
607 * This function initializes the alarm bases and registers
608 * the posix clock ids.
610 static int __init alarmtimer_init(void)
614 struct k_clock alarm_clock = {
615 .clock_getres = alarm_clock_getres,
616 .clock_get = alarm_clock_get,
617 .timer_create = alarm_timer_create,
618 .timer_set = alarm_timer_set,
619 .timer_del = alarm_timer_del,
620 .timer_get = alarm_timer_get,
621 .nsleep = alarm_timer_nsleep,
624 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
625 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
627 /* Initialize alarm bases */
628 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
629 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
630 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
631 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
632 for (i = 0; i < ALARM_NUMTYPE; i++) {
633 timerqueue_init_head(&alarm_bases[i].timerqueue);
634 spin_lock_init(&alarm_bases[i].lock);
635 hrtimer_init(&alarm_bases[i].timer,
636 alarm_bases[i].base_clockid,
638 alarm_bases[i].timer.function = alarmtimer_fired;
639 INIT_WORK(&alarm_bases[i].irqwork, alarmtimer_do_work);
641 error = platform_driver_register(&alarmtimer_driver);
642 platform_device_register_simple("alarmtimer", -1, NULL, 0);
646 device_initcall(alarmtimer_init);
649 * has_wakealarm - check rtc device has wakealarm ability
650 * @dev: current device
651 * @name_ptr: name to be returned
653 * This helper function checks to see if the rtc device can wake
656 static int __init has_wakealarm(struct device *dev, void *name_ptr)
658 struct rtc_device *candidate = to_rtc_device(dev);
660 if (!candidate->ops->set_alarm)
662 if (!device_may_wakeup(candidate->dev.parent))
665 *(const char **)name_ptr = dev_name(dev);
670 * alarmtimer_init_late - Late initializing of alarmtimer code
672 * This function locates a rtc device to use for wakealarms.
673 * Run as late_initcall to make sure rtc devices have been
676 static int __init alarmtimer_init_late(void)
680 /* Find an rtc device and init the rtc_timer */
681 class_find_device(rtc_class, NULL, &str, has_wakealarm);
683 rtcdev = rtc_class_open(str);
685 printk(KERN_WARNING "No RTC device found, ALARM timers will"
686 " not wake from suspend");
688 rtc_timer_init(&rtctimer, NULL, NULL);
692 late_initcall(alarmtimer_init_late);