alarmtimer: Implement minimum alarm interval for allowing suspend
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / time / alarmtimer.c
CommitLineData
ff3ead96
JS
1/*
2 * Alarmtimer interface
3 *
4 * This interface provides a timer which is similarto hrtimers,
5 * but triggers a RTC alarm if the box is suspend.
6 *
7 * This interface is influenced by the Android RTC Alarm timer
8 * interface.
9 *
10 * Copyright (C) 2010 IBM Corperation
11 *
12 * Author: John Stultz <john.stultz@linaro.org>
13 *
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.
17 */
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>
28
180bf812
JS
29/**
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
180bf812 36 */
ff3ead96
JS
37static struct alarm_base {
38 spinlock_t lock;
39 struct timerqueue_head timerqueue;
40 struct hrtimer timer;
41 ktime_t (*gettime)(void);
42 clockid_t base_clockid;
ff3ead96
JS
43} alarm_bases[ALARM_NUMTYPE];
44
c008ba58
JS
45/* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
46static ktime_t freezer_delta;
47static DEFINE_SPINLOCK(freezer_delta_lock);
48
59a93c27
TP
49static struct wakeup_source *ws;
50
472647dc 51#ifdef CONFIG_RTC_CLASS
180bf812 52/* rtc timer and device for setting alarm wakeups at suspend */
c5e14e76 53static struct rtc_timer rtctimer;
ff3ead96 54static struct rtc_device *rtcdev;
c008ba58 55static DEFINE_SPINLOCK(rtcdev_lock);
ff3ead96 56
c008ba58
JS
57/**
58 * alarmtimer_get_rtcdev - Return selected rtcdevice
59 *
60 * This function returns the rtc device to use for wakealarms.
61 * If one has not already been chosen, it checks to see if a
62 * functional rtc device is available.
63 */
57c498fa 64struct rtc_device *alarmtimer_get_rtcdev(void)
c008ba58 65{
c008ba58
JS
66 unsigned long flags;
67 struct rtc_device *ret;
68
69 spin_lock_irqsave(&rtcdev_lock, flags);
c008ba58
JS
70 ret = rtcdev;
71 spin_unlock_irqrestore(&rtcdev_lock, flags);
72
73 return ret;
74}
8bc0dafb
JS
75
76
77static int alarmtimer_rtc_add_device(struct device *dev,
78 struct class_interface *class_intf)
79{
80 unsigned long flags;
81 struct rtc_device *rtc = to_rtc_device(dev);
82
83 if (rtcdev)
84 return -EBUSY;
85
86 if (!rtc->ops->set_alarm)
87 return -1;
88 if (!device_may_wakeup(rtc->dev.parent))
89 return -1;
90
91 spin_lock_irqsave(&rtcdev_lock, flags);
92 if (!rtcdev) {
93 rtcdev = rtc;
94 /* hold a reference so it doesn't go away */
95 get_device(dev);
96 }
97 spin_unlock_irqrestore(&rtcdev_lock, flags);
98 return 0;
99}
100
c5e14e76
TG
101static inline void alarmtimer_rtc_timer_init(void)
102{
103 rtc_timer_init(&rtctimer, NULL, NULL);
104}
105
8bc0dafb
JS
106static struct class_interface alarmtimer_rtc_interface = {
107 .add_dev = &alarmtimer_rtc_add_device,
108};
109
4523f6ad 110static int alarmtimer_rtc_interface_setup(void)
8bc0dafb
JS
111{
112 alarmtimer_rtc_interface.class = rtc_class;
4523f6ad
TG
113 return class_interface_register(&alarmtimer_rtc_interface);
114}
115static void alarmtimer_rtc_interface_remove(void)
116{
117 class_interface_unregister(&alarmtimer_rtc_interface);
8bc0dafb 118}
1c6b39ad 119#else
57c498fa 120struct rtc_device *alarmtimer_get_rtcdev(void)
4523f6ad
TG
121{
122 return NULL;
123}
124#define rtcdev (NULL)
125static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
126static inline void alarmtimer_rtc_interface_remove(void) { }
c5e14e76 127static inline void alarmtimer_rtc_timer_init(void) { }
c008ba58 128#endif
ff3ead96 129
180bf812 130/**
ff3ead96
JS
131 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
132 * @base: pointer to the base where the timer is being run
133 * @alarm: pointer to alarm being enqueued.
134 *
135 * Adds alarm to a alarm_base timerqueue and if necessary sets
136 * an hrtimer to run.
137 *
138 * Must hold base->lock when calling.
139 */
140static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
141{
142 timerqueue_add(&base->timerqueue, &alarm->node);
a28cde81
JS
143 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
144
ff3ead96
JS
145 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
146 hrtimer_try_to_cancel(&base->timer);
147 hrtimer_start(&base->timer, alarm->node.expires,
148 HRTIMER_MODE_ABS);
149 }
150}
151
180bf812 152/**
ff3ead96
JS
153 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
154 * @base: pointer to the base where the timer is running
155 * @alarm: pointer to alarm being removed
156 *
157 * Removes alarm to a alarm_base timerqueue and if necessary sets
158 * a new timer to run.
159 *
160 * Must hold base->lock when calling.
161 */
162static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
163{
164 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
165
a28cde81
JS
166 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
167 return;
168
ff3ead96 169 timerqueue_del(&base->timerqueue, &alarm->node);
a28cde81
JS
170 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
171
ff3ead96
JS
172 if (next == &alarm->node) {
173 hrtimer_try_to_cancel(&base->timer);
174 next = timerqueue_getnext(&base->timerqueue);
175 if (!next)
176 return;
177 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
178 }
179}
180
7068b7a1 181
180bf812 182/**
7068b7a1
JS
183 * alarmtimer_fired - Handles alarm hrtimer being fired.
184 * @timer: pointer to hrtimer being run
ff3ead96 185 *
180bf812
JS
186 * When a alarm timer fires, this runs through the timerqueue to
187 * see which alarms expired, and runs those. If there are more alarm
188 * timers queued for the future, we set the hrtimer to fire when
189 * when the next future alarm timer expires.
ff3ead96 190 */
7068b7a1 191static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
ff3ead96 192{
7068b7a1 193 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
ff3ead96
JS
194 struct timerqueue_node *next;
195 unsigned long flags;
196 ktime_t now;
7068b7a1 197 int ret = HRTIMER_NORESTART;
54da23b7 198 int restart = ALARMTIMER_NORESTART;
ff3ead96
JS
199
200 spin_lock_irqsave(&base->lock, flags);
201 now = base->gettime();
202 while ((next = timerqueue_getnext(&base->timerqueue))) {
203 struct alarm *alarm;
204 ktime_t expired = next->expires;
205
c9c024b3 206 if (expired.tv64 > now.tv64)
ff3ead96
JS
207 break;
208
209 alarm = container_of(next, struct alarm, node);
210
211 timerqueue_del(&base->timerqueue, &alarm->node);
a28cde81 212 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
54da23b7 213
a28cde81 214 alarm->state |= ALARMTIMER_STATE_CALLBACK;
ff3ead96
JS
215 spin_unlock_irqrestore(&base->lock, flags);
216 if (alarm->function)
54da23b7 217 restart = alarm->function(alarm, now);
ff3ead96 218 spin_lock_irqsave(&base->lock, flags);
a28cde81 219 alarm->state &= ~ALARMTIMER_STATE_CALLBACK;
54da23b7
JS
220
221 if (restart != ALARMTIMER_NORESTART) {
222 timerqueue_add(&base->timerqueue, &alarm->node);
a28cde81 223 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
54da23b7 224 }
ff3ead96
JS
225 }
226
227 if (next) {
7068b7a1
JS
228 hrtimer_set_expires(&base->timer, next->expires);
229 ret = HRTIMER_RESTART;
ff3ead96
JS
230 }
231 spin_unlock_irqrestore(&base->lock, flags);
ff3ead96 232
7068b7a1 233 return ret;
ff3ead96 234
ff3ead96
JS
235}
236
472647dc 237#ifdef CONFIG_RTC_CLASS
180bf812 238/**
ff3ead96
JS
239 * alarmtimer_suspend - Suspend time callback
240 * @dev: unused
241 * @state: unused
242 *
243 * When we are going into suspend, we look through the bases
244 * to see which is the soonest timer to expire. We then
245 * set an rtc timer to fire that far into the future, which
246 * will wake us from suspend.
247 */
248static int alarmtimer_suspend(struct device *dev)
249{
250 struct rtc_time tm;
251 ktime_t min, now;
252 unsigned long flags;
c008ba58 253 struct rtc_device *rtc;
ff3ead96 254 int i;
59a93c27 255 int ret;
ff3ead96
JS
256
257 spin_lock_irqsave(&freezer_delta_lock, flags);
258 min = freezer_delta;
259 freezer_delta = ktime_set(0, 0);
260 spin_unlock_irqrestore(&freezer_delta_lock, flags);
261
8bc0dafb 262 rtc = alarmtimer_get_rtcdev();
ff3ead96 263 /* If we have no rtcdev, just return */
c008ba58 264 if (!rtc)
ff3ead96
JS
265 return 0;
266
267 /* Find the soonest timer to expire*/
268 for (i = 0; i < ALARM_NUMTYPE; i++) {
269 struct alarm_base *base = &alarm_bases[i];
270 struct timerqueue_node *next;
271 ktime_t delta;
272
273 spin_lock_irqsave(&base->lock, flags);
274 next = timerqueue_getnext(&base->timerqueue);
275 spin_unlock_irqrestore(&base->lock, flags);
276 if (!next)
277 continue;
278 delta = ktime_sub(next->expires, base->gettime());
279 if (!min.tv64 || (delta.tv64 < min.tv64))
280 min = delta;
281 }
282 if (min.tv64 == 0)
283 return 0;
284
59a93c27
TP
285 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
286 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
287 return -EBUSY;
288 }
ff3ead96
JS
289
290 /* Setup an rtc timer to fire that far in the future */
c008ba58
JS
291 rtc_timer_cancel(rtc, &rtctimer);
292 rtc_read_time(rtc, &tm);
ff3ead96
JS
293 now = rtc_tm_to_ktime(tm);
294 now = ktime_add(now, min);
295
59a93c27
TP
296 /* Set alarm, if in the past reject suspend briefly to handle */
297 ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
298 if (ret < 0)
299 __pm_wakeup_event(ws, MSEC_PER_SEC);
300 return ret;
ff3ead96 301}
472647dc
JS
302#else
303static int alarmtimer_suspend(struct device *dev)
304{
305 return 0;
306}
307#endif
ff3ead96 308
9a7adcf5
JS
309static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
310{
311 ktime_t delta;
312 unsigned long flags;
313 struct alarm_base *base = &alarm_bases[type];
314
315 delta = ktime_sub(absexp, base->gettime());
316
317 spin_lock_irqsave(&freezer_delta_lock, flags);
318 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
319 freezer_delta = delta;
320 spin_unlock_irqrestore(&freezer_delta_lock, flags);
321}
322
323
180bf812 324/**
ff3ead96
JS
325 * alarm_init - Initialize an alarm structure
326 * @alarm: ptr to alarm to be initialized
327 * @type: the type of the alarm
328 * @function: callback that is run when the alarm fires
ff3ead96
JS
329 */
330void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
4b41308d 331 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
ff3ead96
JS
332{
333 timerqueue_init(&alarm->node);
ff3ead96
JS
334 alarm->function = function;
335 alarm->type = type;
a28cde81 336 alarm->state = ALARMTIMER_STATE_INACTIVE;
ff3ead96
JS
337}
338
180bf812 339/**
ff3ead96
JS
340 * alarm_start - Sets an alarm to fire
341 * @alarm: ptr to alarm to set
342 * @start: time to run the alarm
ff3ead96 343 */
9e264762 344void alarm_start(struct alarm *alarm, ktime_t start)
ff3ead96
JS
345{
346 struct alarm_base *base = &alarm_bases[alarm->type];
347 unsigned long flags;
348
349 spin_lock_irqsave(&base->lock, flags);
a28cde81 350 if (alarmtimer_active(alarm))
ff3ead96
JS
351 alarmtimer_remove(base, alarm);
352 alarm->node.expires = start;
ff3ead96 353 alarmtimer_enqueue(base, alarm);
ff3ead96
JS
354 spin_unlock_irqrestore(&base->lock, flags);
355}
356
180bf812 357/**
9082c465 358 * alarm_try_to_cancel - Tries to cancel an alarm timer
ff3ead96 359 * @alarm: ptr to alarm to be canceled
9082c465
JS
360 *
361 * Returns 1 if the timer was canceled, 0 if it was not running,
362 * and -1 if the callback was running
ff3ead96 363 */
9082c465 364int alarm_try_to_cancel(struct alarm *alarm)
ff3ead96
JS
365{
366 struct alarm_base *base = &alarm_bases[alarm->type];
367 unsigned long flags;
9082c465 368 int ret = -1;
ff3ead96 369 spin_lock_irqsave(&base->lock, flags);
9082c465
JS
370
371 if (alarmtimer_callback_running(alarm))
372 goto out;
373
374 if (alarmtimer_is_queued(alarm)) {
ff3ead96 375 alarmtimer_remove(base, alarm);
9082c465
JS
376 ret = 1;
377 } else
378 ret = 0;
379out:
ff3ead96 380 spin_unlock_irqrestore(&base->lock, flags);
9082c465 381 return ret;
ff3ead96
JS
382}
383
384
9082c465
JS
385/**
386 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
387 * @alarm: ptr to alarm to be canceled
388 *
389 * Returns 1 if the timer was canceled, 0 if it was not active.
390 */
391int alarm_cancel(struct alarm *alarm)
392{
393 for (;;) {
394 int ret = alarm_try_to_cancel(alarm);
395 if (ret >= 0)
396 return ret;
397 cpu_relax();
398 }
399}
400
dce75a8c
JS
401
402u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
403{
404 u64 overrun = 1;
405 ktime_t delta;
406
407 delta = ktime_sub(now, alarm->node.expires);
408
409 if (delta.tv64 < 0)
410 return 0;
411
412 if (unlikely(delta.tv64 >= interval.tv64)) {
413 s64 incr = ktime_to_ns(interval);
414
415 overrun = ktime_divns(delta, incr);
416
417 alarm->node.expires = ktime_add_ns(alarm->node.expires,
418 incr*overrun);
419
420 if (alarm->node.expires.tv64 > now.tv64)
421 return overrun;
422 /*
423 * This (and the ktime_add() below) is the
424 * correction for exact:
425 */
426 overrun++;
427 }
428
429 alarm->node.expires = ktime_add(alarm->node.expires, interval);
430 return overrun;
431}
432
433
434
435
180bf812 436/**
9a7adcf5
JS
437 * clock2alarm - helper that converts from clockid to alarmtypes
438 * @clockid: clockid.
9a7adcf5
JS
439 */
440static enum alarmtimer_type clock2alarm(clockid_t clockid)
441{
442 if (clockid == CLOCK_REALTIME_ALARM)
443 return ALARM_REALTIME;
444 if (clockid == CLOCK_BOOTTIME_ALARM)
445 return ALARM_BOOTTIME;
446 return -1;
447}
448
180bf812 449/**
9a7adcf5
JS
450 * alarm_handle_timer - Callback for posix timers
451 * @alarm: alarm that fired
452 *
453 * Posix timer callback for expired alarm timers.
454 */
4b41308d
JS
455static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
456 ktime_t now)
9a7adcf5
JS
457{
458 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
9e264762 459 it.alarm.alarmtimer);
9a7adcf5
JS
460 if (posix_timer_event(ptr, 0) != 0)
461 ptr->it_overrun++;
4b41308d 462
54da23b7 463 /* Re-add periodic timers */
9e264762
JS
464 if (ptr->it.alarm.interval.tv64) {
465 ptr->it_overrun += alarm_forward(alarm, now,
466 ptr->it.alarm.interval);
54da23b7
JS
467 return ALARMTIMER_RESTART;
468 }
4b41308d 469 return ALARMTIMER_NORESTART;
9a7adcf5
JS
470}
471
180bf812 472/**
9a7adcf5
JS
473 * alarm_clock_getres - posix getres interface
474 * @which_clock: clockid
475 * @tp: timespec to fill
476 *
477 * Returns the granularity of underlying alarm base clock
478 */
479static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
480{
481 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
482
1c6b39ad
JS
483 if (!alarmtimer_get_rtcdev())
484 return -ENOTSUPP;
485
9a7adcf5
JS
486 return hrtimer_get_res(baseid, tp);
487}
488
489/**
490 * alarm_clock_get - posix clock_get interface
491 * @which_clock: clockid
492 * @tp: timespec to fill.
493 *
494 * Provides the underlying alarm base time.
495 */
496static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
497{
498 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
499
1c6b39ad
JS
500 if (!alarmtimer_get_rtcdev())
501 return -ENOTSUPP;
502
9a7adcf5
JS
503 *tp = ktime_to_timespec(base->gettime());
504 return 0;
505}
506
507/**
508 * alarm_timer_create - posix timer_create interface
509 * @new_timer: k_itimer pointer to manage
510 *
511 * Initializes the k_itimer structure.
512 */
513static int alarm_timer_create(struct k_itimer *new_timer)
514{
515 enum alarmtimer_type type;
516 struct alarm_base *base;
517
1c6b39ad
JS
518 if (!alarmtimer_get_rtcdev())
519 return -ENOTSUPP;
520
9a7adcf5
JS
521 if (!capable(CAP_WAKE_ALARM))
522 return -EPERM;
523
524 type = clock2alarm(new_timer->it_clock);
525 base = &alarm_bases[type];
9e264762 526 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
9a7adcf5
JS
527 return 0;
528}
529
530/**
531 * alarm_timer_get - posix timer_get interface
532 * @new_timer: k_itimer pointer
533 * @cur_setting: itimerspec data to fill
534 *
535 * Copies the itimerspec data out from the k_itimer
536 */
537static void alarm_timer_get(struct k_itimer *timr,
538 struct itimerspec *cur_setting)
539{
ea7802f6
JS
540 memset(cur_setting, 0, sizeof(struct itimerspec));
541
9a7adcf5 542 cur_setting->it_interval =
9e264762 543 ktime_to_timespec(timr->it.alarm.interval);
9a7adcf5 544 cur_setting->it_value =
9e264762 545 ktime_to_timespec(timr->it.alarm.alarmtimer.node.expires);
9a7adcf5
JS
546 return;
547}
548
549/**
550 * alarm_timer_del - posix timer_del interface
551 * @timr: k_itimer pointer to be deleted
552 *
553 * Cancels any programmed alarms for the given timer.
554 */
555static int alarm_timer_del(struct k_itimer *timr)
556{
1c6b39ad
JS
557 if (!rtcdev)
558 return -ENOTSUPP;
559
9082c465
JS
560 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
561 return TIMER_RETRY;
562
9a7adcf5
JS
563 return 0;
564}
565
566/**
567 * alarm_timer_set - posix timer_set interface
568 * @timr: k_itimer pointer to be deleted
569 * @flags: timer flags
570 * @new_setting: itimerspec to be used
571 * @old_setting: itimerspec being replaced
572 *
573 * Sets the timer to new_setting, and starts the timer.
574 */
575static int alarm_timer_set(struct k_itimer *timr, int flags,
576 struct itimerspec *new_setting,
577 struct itimerspec *old_setting)
578{
1c6b39ad
JS
579 if (!rtcdev)
580 return -ENOTSUPP;
581
971c90bf
JS
582 if (old_setting)
583 alarm_timer_get(timr, old_setting);
9a7adcf5
JS
584
585 /* If the timer was already set, cancel it */
9082c465
JS
586 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
587 return TIMER_RETRY;
9a7adcf5
JS
588
589 /* start the timer */
9e264762
JS
590 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
591 alarm_start(&timr->it.alarm.alarmtimer,
592 timespec_to_ktime(new_setting->it_value));
9a7adcf5
JS
593 return 0;
594}
595
596/**
597 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
598 * @alarm: ptr to alarm that fired
599 *
600 * Wakes up the task that set the alarmtimer
601 */
4b41308d
JS
602static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
603 ktime_t now)
9a7adcf5
JS
604{
605 struct task_struct *task = (struct task_struct *)alarm->data;
606
607 alarm->data = NULL;
608 if (task)
609 wake_up_process(task);
4b41308d 610 return ALARMTIMER_NORESTART;
9a7adcf5
JS
611}
612
613/**
614 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
615 * @alarm: ptr to alarmtimer
616 * @absexp: absolute expiration time
617 *
618 * Sets the alarm timer and sleeps until it is fired or interrupted.
619 */
620static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
621{
622 alarm->data = (void *)current;
623 do {
624 set_current_state(TASK_INTERRUPTIBLE);
9e264762 625 alarm_start(alarm, absexp);
9a7adcf5
JS
626 if (likely(alarm->data))
627 schedule();
628
629 alarm_cancel(alarm);
630 } while (alarm->data && !signal_pending(current));
631
632 __set_current_state(TASK_RUNNING);
633
634 return (alarm->data == NULL);
635}
636
637
638/**
639 * update_rmtp - Update remaining timespec value
640 * @exp: expiration time
641 * @type: timer type
642 * @rmtp: user pointer to remaining timepsec value
643 *
644 * Helper function that fills in rmtp value with time between
645 * now and the exp value
646 */
647static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
648 struct timespec __user *rmtp)
649{
650 struct timespec rmt;
651 ktime_t rem;
652
653 rem = ktime_sub(exp, alarm_bases[type].gettime());
654
655 if (rem.tv64 <= 0)
656 return 0;
657 rmt = ktime_to_timespec(rem);
658
659 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
660 return -EFAULT;
661
662 return 1;
663
664}
665
666/**
667 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
668 * @restart: ptr to restart block
669 *
670 * Handles restarted clock_nanosleep calls
671 */
672static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
673{
ab8177bc 674 enum alarmtimer_type type = restart->nanosleep.clockid;
9a7adcf5
JS
675 ktime_t exp;
676 struct timespec __user *rmtp;
677 struct alarm alarm;
678 int ret = 0;
679
680 exp.tv64 = restart->nanosleep.expires;
681 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
682
683 if (alarmtimer_do_nsleep(&alarm, exp))
684 goto out;
685
686 if (freezing(current))
687 alarmtimer_freezerset(exp, type);
688
689 rmtp = restart->nanosleep.rmtp;
690 if (rmtp) {
691 ret = update_rmtp(exp, type, rmtp);
692 if (ret <= 0)
693 goto out;
694 }
695
696
697 /* The other values in restart are already filled in */
698 ret = -ERESTART_RESTARTBLOCK;
699out:
700 return ret;
701}
702
703/**
704 * alarm_timer_nsleep - alarmtimer nanosleep
705 * @which_clock: clockid
706 * @flags: determins abstime or relative
707 * @tsreq: requested sleep time (abs or rel)
708 * @rmtp: remaining sleep time saved
709 *
710 * Handles clock_nanosleep calls against _ALARM clockids
711 */
712static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
713 struct timespec *tsreq, struct timespec __user *rmtp)
714{
715 enum alarmtimer_type type = clock2alarm(which_clock);
716 struct alarm alarm;
717 ktime_t exp;
718 int ret = 0;
719 struct restart_block *restart;
720
1c6b39ad
JS
721 if (!alarmtimer_get_rtcdev())
722 return -ENOTSUPP;
723
9a7adcf5
JS
724 if (!capable(CAP_WAKE_ALARM))
725 return -EPERM;
726
727 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
728
729 exp = timespec_to_ktime(*tsreq);
730 /* Convert (if necessary) to absolute time */
731 if (flags != TIMER_ABSTIME) {
732 ktime_t now = alarm_bases[type].gettime();
733 exp = ktime_add(now, exp);
734 }
735
736 if (alarmtimer_do_nsleep(&alarm, exp))
737 goto out;
738
739 if (freezing(current))
740 alarmtimer_freezerset(exp, type);
741
742 /* abs timers don't set remaining time or restart */
743 if (flags == TIMER_ABSTIME) {
744 ret = -ERESTARTNOHAND;
745 goto out;
746 }
747
748 if (rmtp) {
749 ret = update_rmtp(exp, type, rmtp);
750 if (ret <= 0)
751 goto out;
752 }
753
754 restart = &current_thread_info()->restart_block;
755 restart->fn = alarm_timer_nsleep_restart;
ab8177bc 756 restart->nanosleep.clockid = type;
9a7adcf5
JS
757 restart->nanosleep.expires = exp.tv64;
758 restart->nanosleep.rmtp = rmtp;
759 ret = -ERESTART_RESTARTBLOCK;
760
761out:
762 return ret;
763}
ff3ead96 764
ff3ead96
JS
765
766/* Suspend hook structures */
767static const struct dev_pm_ops alarmtimer_pm_ops = {
768 .suspend = alarmtimer_suspend,
769};
770
771static struct platform_driver alarmtimer_driver = {
772 .driver = {
773 .name = "alarmtimer",
774 .pm = &alarmtimer_pm_ops,
775 }
776};
777
778/**
779 * alarmtimer_init - Initialize alarm timer code
780 *
781 * This function initializes the alarm bases and registers
782 * the posix clock ids.
783 */
784static int __init alarmtimer_init(void)
785{
4523f6ad 786 struct platform_device *pdev;
ff3ead96
JS
787 int error = 0;
788 int i;
9a7adcf5
JS
789 struct k_clock alarm_clock = {
790 .clock_getres = alarm_clock_getres,
791 .clock_get = alarm_clock_get,
792 .timer_create = alarm_timer_create,
793 .timer_set = alarm_timer_set,
794 .timer_del = alarm_timer_del,
795 .timer_get = alarm_timer_get,
796 .nsleep = alarm_timer_nsleep,
797 };
798
c5e14e76 799 alarmtimer_rtc_timer_init();
ad30dfa9 800
9a7adcf5
JS
801 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
802 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
ff3ead96
JS
803
804 /* Initialize alarm bases */
805 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
806 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
807 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
808 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
809 for (i = 0; i < ALARM_NUMTYPE; i++) {
810 timerqueue_init_head(&alarm_bases[i].timerqueue);
811 spin_lock_init(&alarm_bases[i].lock);
812 hrtimer_init(&alarm_bases[i].timer,
813 alarm_bases[i].base_clockid,
814 HRTIMER_MODE_ABS);
815 alarm_bases[i].timer.function = alarmtimer_fired;
ff3ead96 816 }
8bc0dafb 817
4523f6ad
TG
818 error = alarmtimer_rtc_interface_setup();
819 if (error)
820 return error;
821
ff3ead96 822 error = platform_driver_register(&alarmtimer_driver);
4523f6ad
TG
823 if (error)
824 goto out_if;
ff3ead96 825
4523f6ad
TG
826 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
827 if (IS_ERR(pdev)) {
828 error = PTR_ERR(pdev);
829 goto out_drv;
830 }
59a93c27 831 ws = wakeup_source_register("alarmtimer");
4523f6ad
TG
832 return 0;
833
834out_drv:
835 platform_driver_unregister(&alarmtimer_driver);
836out_if:
837 alarmtimer_rtc_interface_remove();
ff3ead96
JS
838 return error;
839}
840device_initcall(alarmtimer_init);