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