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