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