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 static struct alarm_base {
32 struct timerqueue_head timerqueue;
34 ktime_t (*gettime)(void);
35 clockid_t base_clockid;
36 struct work_struct irqwork;
37 } alarm_bases[ALARM_NUMTYPE];
39 static struct rtc_timer rtctimer;
40 static struct rtc_device *rtcdev;
42 static ktime_t freezer_delta;
43 static DEFINE_SPINLOCK(freezer_delta_lock);
46 /**************************************************************************
47 * alarmtimer management code
51 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
52 * @base: pointer to the base where the timer is being run
53 * @alarm: pointer to alarm being enqueued.
55 * Adds alarm to a alarm_base timerqueue and if necessary sets
58 * Must hold base->lock when calling.
60 static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
62 timerqueue_add(&base->timerqueue, &alarm->node);
63 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
64 hrtimer_try_to_cancel(&base->timer);
65 hrtimer_start(&base->timer, alarm->node.expires,
71 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
72 * @base: pointer to the base where the timer is running
73 * @alarm: pointer to alarm being removed
75 * Removes alarm to a alarm_base timerqueue and if necessary sets
78 * Must hold base->lock when calling.
80 static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
82 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
84 timerqueue_del(&base->timerqueue, &alarm->node);
85 if (next == &alarm->node) {
86 hrtimer_try_to_cancel(&base->timer);
87 next = timerqueue_getnext(&base->timerqueue);
90 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
95 * alarmtimer_do_work - Handles alarm being fired.
96 * @work: pointer to workqueue being run
98 * When a timer fires, this runs through the timerqueue to see
99 * which alarm timers, and run those that expired. If there are
100 * more alarm timers queued, we set the hrtimer to fire in the
103 void alarmtimer_do_work(struct work_struct *work)
105 struct alarm_base *base = container_of(work, struct alarm_base,
107 struct timerqueue_node *next;
111 spin_lock_irqsave(&base->lock, flags);
112 now = base->gettime();
113 while ((next = timerqueue_getnext(&base->timerqueue))) {
115 ktime_t expired = next->expires;
117 if (expired.tv64 >= now.tv64)
120 alarm = container_of(next, struct alarm, node);
122 timerqueue_del(&base->timerqueue, &alarm->node);
124 /* Re-add periodic timers */
125 if (alarm->period.tv64) {
126 alarm->node.expires = ktime_add(expired, alarm->period);
127 timerqueue_add(&base->timerqueue, &alarm->node);
130 spin_unlock_irqrestore(&base->lock, flags);
132 alarm->function(alarm);
133 spin_lock_irqsave(&base->lock, flags);
137 hrtimer_start(&base->timer, next->expires,
140 spin_unlock_irqrestore(&base->lock, flags);
145 * alarmtimer_fired - Handles alarm hrtimer being fired.
146 * @timer: pointer to hrtimer being run
148 * When a timer fires, this schedules the do_work function to
151 static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
153 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
154 schedule_work(&base->irqwork);
155 return HRTIMER_NORESTART;
160 * alarmtimer_suspend - Suspend time callback
164 * When we are going into suspend, we look through the bases
165 * to see which is the soonest timer to expire. We then
166 * set an rtc timer to fire that far into the future, which
167 * will wake us from suspend.
169 static int alarmtimer_suspend(struct device *dev)
176 spin_lock_irqsave(&freezer_delta_lock, flags);
178 freezer_delta = ktime_set(0, 0);
179 spin_unlock_irqrestore(&freezer_delta_lock, flags);
181 /* If we have no rtcdev, just return */
185 /* Find the soonest timer to expire*/
186 for (i = 0; i < ALARM_NUMTYPE; i++) {
187 struct alarm_base *base = &alarm_bases[i];
188 struct timerqueue_node *next;
191 spin_lock_irqsave(&base->lock, flags);
192 next = timerqueue_getnext(&base->timerqueue);
193 spin_unlock_irqrestore(&base->lock, flags);
196 delta = ktime_sub(next->expires, base->gettime());
197 if (!min.tv64 || (delta.tv64 < min.tv64))
203 /* XXX - Should we enforce a minimum sleep time? */
204 WARN_ON(min.tv64 < NSEC_PER_SEC);
206 /* Setup an rtc timer to fire that far in the future */
207 rtc_timer_cancel(rtcdev, &rtctimer);
208 rtc_read_time(rtcdev, &tm);
209 now = rtc_tm_to_ktime(tm);
210 now = ktime_add(now, min);
212 rtc_timer_start(rtcdev, &rtctimer, now, ktime_set(0, 0));
218 static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
222 struct alarm_base *base = &alarm_bases[type];
224 delta = ktime_sub(absexp, base->gettime());
226 spin_lock_irqsave(&freezer_delta_lock, flags);
227 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
228 freezer_delta = delta;
229 spin_unlock_irqrestore(&freezer_delta_lock, flags);
233 /**************************************************************************
234 * alarm kernel interface code
238 * alarm_init - Initialize an alarm structure
239 * @alarm: ptr to alarm to be initialized
240 * @type: the type of the alarm
241 * @function: callback that is run when the alarm fires
243 * In-kernel interface to initializes the alarm structure.
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 * In-kernel interface set an alarm timer.
263 void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period)
265 struct alarm_base *base = &alarm_bases[alarm->type];
268 spin_lock_irqsave(&base->lock, flags);
270 alarmtimer_remove(base, alarm);
271 alarm->node.expires = start;
272 alarm->period = period;
273 alarmtimer_enqueue(base, alarm);
275 spin_unlock_irqrestore(&base->lock, flags);
279 * alarm_cancel - Tries to cancel an alarm timer
280 * @alarm: ptr to alarm to be canceled
282 * In-kernel interface to cancel an alarm timer.
284 void alarm_cancel(struct alarm *alarm)
286 struct alarm_base *base = &alarm_bases[alarm->type];
289 spin_lock_irqsave(&base->lock, flags);
291 alarmtimer_remove(base, alarm);
293 spin_unlock_irqrestore(&base->lock, flags);
297 /**************************************************************************
298 * alarm posix interface code
302 * clock2alarm - helper that converts from clockid to alarmtypes
305 * Helper function that converts from clockids to alarmtypes
307 static enum alarmtimer_type clock2alarm(clockid_t clockid)
309 if (clockid == CLOCK_REALTIME_ALARM)
310 return ALARM_REALTIME;
311 if (clockid == CLOCK_BOOTTIME_ALARM)
312 return ALARM_BOOTTIME;
317 * alarm_handle_timer - Callback for posix timers
318 * @alarm: alarm that fired
320 * Posix timer callback for expired alarm timers.
322 static void alarm_handle_timer(struct alarm *alarm)
324 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
326 if (posix_timer_event(ptr, 0) != 0)
331 * alarm_clock_getres - posix getres interface
332 * @which_clock: clockid
333 * @tp: timespec to fill
335 * Returns the granularity of underlying alarm base clock
337 static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
339 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
341 return hrtimer_get_res(baseid, tp);
345 * alarm_clock_get - posix clock_get interface
346 * @which_clock: clockid
347 * @tp: timespec to fill.
349 * Provides the underlying alarm base time.
351 static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
353 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
355 *tp = ktime_to_timespec(base->gettime());
360 * alarm_timer_create - posix timer_create interface
361 * @new_timer: k_itimer pointer to manage
363 * Initializes the k_itimer structure.
365 static int alarm_timer_create(struct k_itimer *new_timer)
367 enum alarmtimer_type type;
368 struct alarm_base *base;
370 if (!capable(CAP_WAKE_ALARM))
373 type = clock2alarm(new_timer->it_clock);
374 base = &alarm_bases[type];
375 alarm_init(&new_timer->it.alarmtimer, type, alarm_handle_timer);
380 * alarm_timer_get - posix timer_get interface
381 * @new_timer: k_itimer pointer
382 * @cur_setting: itimerspec data to fill
384 * Copies the itimerspec data out from the k_itimer
386 static void alarm_timer_get(struct k_itimer *timr,
387 struct itimerspec *cur_setting)
389 cur_setting->it_interval =
390 ktime_to_timespec(timr->it.alarmtimer.period);
391 cur_setting->it_value =
392 ktime_to_timespec(timr->it.alarmtimer.node.expires);
397 * alarm_timer_del - posix timer_del interface
398 * @timr: k_itimer pointer to be deleted
400 * Cancels any programmed alarms for the given timer.
402 static int alarm_timer_del(struct k_itimer *timr)
404 alarm_cancel(&timr->it.alarmtimer);
409 * alarm_timer_set - posix timer_set interface
410 * @timr: k_itimer pointer to be deleted
411 * @flags: timer flags
412 * @new_setting: itimerspec to be used
413 * @old_setting: itimerspec being replaced
415 * Sets the timer to new_setting, and starts the timer.
417 static int alarm_timer_set(struct k_itimer *timr, int flags,
418 struct itimerspec *new_setting,
419 struct itimerspec *old_setting)
421 /* Save old values */
422 old_setting->it_interval =
423 ktime_to_timespec(timr->it.alarmtimer.period);
424 old_setting->it_value =
425 ktime_to_timespec(timr->it.alarmtimer.node.expires);
427 /* If the timer was already set, cancel it */
428 alarm_cancel(&timr->it.alarmtimer);
430 /* start the timer */
431 alarm_start(&timr->it.alarmtimer,
432 timespec_to_ktime(new_setting->it_value),
433 timespec_to_ktime(new_setting->it_interval));
438 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
439 * @alarm: ptr to alarm that fired
441 * Wakes up the task that set the alarmtimer
443 static void alarmtimer_nsleep_wakeup(struct alarm *alarm)
445 struct task_struct *task = (struct task_struct *)alarm->data;
449 wake_up_process(task);
453 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
454 * @alarm: ptr to alarmtimer
455 * @absexp: absolute expiration time
457 * Sets the alarm timer and sleeps until it is fired or interrupted.
459 static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
461 alarm->data = (void *)current;
463 set_current_state(TASK_INTERRUPTIBLE);
464 alarm_start(alarm, absexp, ktime_set(0, 0));
465 if (likely(alarm->data))
469 } while (alarm->data && !signal_pending(current));
471 __set_current_state(TASK_RUNNING);
473 return (alarm->data == NULL);
478 * update_rmtp - Update remaining timespec value
479 * @exp: expiration time
481 * @rmtp: user pointer to remaining timepsec value
483 * Helper function that fills in rmtp value with time between
484 * now and the exp value
486 static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
487 struct timespec __user *rmtp)
492 rem = ktime_sub(exp, alarm_bases[type].gettime());
496 rmt = ktime_to_timespec(rem);
498 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
506 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
507 * @restart: ptr to restart block
509 * Handles restarted clock_nanosleep calls
511 static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
513 enum alarmtimer_type type = restart->nanosleep.index;
515 struct timespec __user *rmtp;
519 exp.tv64 = restart->nanosleep.expires;
520 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
522 if (alarmtimer_do_nsleep(&alarm, exp))
525 if (freezing(current))
526 alarmtimer_freezerset(exp, type);
528 rmtp = restart->nanosleep.rmtp;
530 ret = update_rmtp(exp, type, rmtp);
536 /* The other values in restart are already filled in */
537 ret = -ERESTART_RESTARTBLOCK;
543 * alarm_timer_nsleep - alarmtimer nanosleep
544 * @which_clock: clockid
545 * @flags: determins abstime or relative
546 * @tsreq: requested sleep time (abs or rel)
547 * @rmtp: remaining sleep time saved
549 * Handles clock_nanosleep calls against _ALARM clockids
551 static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
552 struct timespec *tsreq, struct timespec __user *rmtp)
554 enum alarmtimer_type type = clock2alarm(which_clock);
558 struct restart_block *restart;
560 if (!capable(CAP_WAKE_ALARM))
563 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
565 exp = timespec_to_ktime(*tsreq);
566 /* Convert (if necessary) to absolute time */
567 if (flags != TIMER_ABSTIME) {
568 ktime_t now = alarm_bases[type].gettime();
569 exp = ktime_add(now, exp);
572 if (alarmtimer_do_nsleep(&alarm, exp))
575 if (freezing(current))
576 alarmtimer_freezerset(exp, type);
578 /* abs timers don't set remaining time or restart */
579 if (flags == TIMER_ABSTIME) {
580 ret = -ERESTARTNOHAND;
585 ret = update_rmtp(exp, type, rmtp);
590 restart = ¤t_thread_info()->restart_block;
591 restart->fn = alarm_timer_nsleep_restart;
592 restart->nanosleep.index = type;
593 restart->nanosleep.expires = exp.tv64;
594 restart->nanosleep.rmtp = rmtp;
595 ret = -ERESTART_RESTARTBLOCK;
601 /**************************************************************************
602 * alarmtimer initialization code
605 /* Suspend hook structures */
606 static const struct dev_pm_ops alarmtimer_pm_ops = {
607 .suspend = alarmtimer_suspend,
610 static struct platform_driver alarmtimer_driver = {
612 .name = "alarmtimer",
613 .pm = &alarmtimer_pm_ops,
618 * alarmtimer_init - Initialize alarm timer code
620 * This function initializes the alarm bases and registers
621 * the posix clock ids.
623 static int __init alarmtimer_init(void)
627 struct k_clock alarm_clock = {
628 .clock_getres = alarm_clock_getres,
629 .clock_get = alarm_clock_get,
630 .timer_create = alarm_timer_create,
631 .timer_set = alarm_timer_set,
632 .timer_del = alarm_timer_del,
633 .timer_get = alarm_timer_get,
634 .nsleep = alarm_timer_nsleep,
637 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
638 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
640 /* Initialize alarm bases */
641 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
642 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
643 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
644 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
645 for (i = 0; i < ALARM_NUMTYPE; i++) {
646 timerqueue_init_head(&alarm_bases[i].timerqueue);
647 spin_lock_init(&alarm_bases[i].lock);
648 hrtimer_init(&alarm_bases[i].timer,
649 alarm_bases[i].base_clockid,
651 alarm_bases[i].timer.function = alarmtimer_fired;
652 INIT_WORK(&alarm_bases[i].irqwork, alarmtimer_do_work);
654 error = platform_driver_register(&alarmtimer_driver);
655 platform_device_register_simple("alarmtimer", -1, NULL, 0);
659 device_initcall(alarmtimer_init);
662 * has_wakealarm - check rtc device has wakealarm ability
663 * @dev: current device
664 * @name_ptr: name to be returned
666 * This helper function checks to see if the rtc device can wake
669 static int __init has_wakealarm(struct device *dev, void *name_ptr)
671 struct rtc_device *candidate = to_rtc_device(dev);
673 if (!candidate->ops->set_alarm)
675 if (!device_may_wakeup(candidate->dev.parent))
678 *(const char **)name_ptr = dev_name(dev);
683 * alarmtimer_init_late - Late initializing of alarmtimer code
685 * This function locates a rtc device to use for wakealarms.
686 * Run as late_initcall to make sure rtc devices have been
689 static int __init alarmtimer_init_late(void)
693 /* Find an rtc device and init the rtc_timer */
694 class_find_device(rtc_class, NULL, &str, has_wakealarm);
696 rtcdev = rtc_class_open(str);
698 printk(KERN_WARNING "No RTC device found, ALARM timers will"
699 " not wake from suspend");
701 rtc_timer_init(&rtctimer, NULL, NULL);
705 late_initcall(alarmtimer_init_late);