1 // SPDX-License-Identifier: GPL-2.0
5 * This interface provides a timer which is similar to hrtimers,
6 * but triggers a RTC alarm if the box is suspend.
8 * This interface is influenced by the Android RTC Alarm timer
11 * Copyright (C) 2010 IBM Corporation
13 * Author: John Stultz <john.stultz@linaro.org>
15 #include <linux/time.h>
16 #include <linux/hrtimer.h>
17 #include <linux/timerqueue.h>
18 #include <linux/rtc.h>
19 #include <linux/sched/signal.h>
20 #include <linux/sched/debug.h>
21 #include <linux/alarmtimer.h>
22 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
24 #include <linux/posix-timers.h>
25 #include <linux/workqueue.h>
26 #include <linux/freezer.h>
27 #include <linux/compat.h>
28 #include <linux/module.h>
29 #include <linux/time_namespace.h>
31 #include "posix-timers.h"
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/alarmtimer.h>
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
40 * @get_ktime: Function to read the time correlating to the base
41 * @get_timespec: Function to read the namespace time correlating to the base
42 * @base_clockid: clockid for the base
44 static struct alarm_base {
46 struct timerqueue_head timerqueue;
47 ktime_t (*get_ktime)(void);
48 void (*get_timespec)(struct timespec64 *tp);
49 clockid_t base_clockid;
50 } alarm_bases[ALARM_NUMTYPE];
52 #if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
53 /* freezer information to handle clock_nanosleep triggered wakeups */
54 static enum alarmtimer_type freezer_alarmtype;
55 static ktime_t freezer_expires;
56 static ktime_t freezer_delta;
57 static DEFINE_SPINLOCK(freezer_delta_lock);
60 #ifdef CONFIG_RTC_CLASS
61 /* rtc timer and device for setting alarm wakeups at suspend */
62 static struct rtc_timer rtctimer;
63 static struct rtc_device *rtcdev;
64 static DEFINE_SPINLOCK(rtcdev_lock);
67 * alarmtimer_get_rtcdev - Return selected rtcdevice
69 * This function returns the rtc device to use for wakealarms.
71 struct rtc_device *alarmtimer_get_rtcdev(void)
74 struct rtc_device *ret;
76 spin_lock_irqsave(&rtcdev_lock, flags);
78 spin_unlock_irqrestore(&rtcdev_lock, flags);
82 EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
84 static int alarmtimer_rtc_add_device(struct device *dev)
87 struct rtc_device *rtc = to_rtc_device(dev);
88 struct platform_device *pdev;
94 if (!test_bit(RTC_FEATURE_ALARM, rtc->features))
96 if (!device_may_wakeup(rtc->dev.parent))
99 pdev = platform_device_register_data(dev, "alarmtimer",
100 PLATFORM_DEVID_AUTO, NULL, 0);
102 device_init_wakeup(&pdev->dev, true);
104 spin_lock_irqsave(&rtcdev_lock, flags);
105 if (!IS_ERR(pdev) && !rtcdev) {
106 if (!try_module_get(rtc->owner)) {
112 /* hold a reference so it doesn't go away */
119 spin_unlock_irqrestore(&rtcdev_lock, flags);
121 platform_device_unregister(pdev);
126 static inline void alarmtimer_rtc_timer_init(void)
128 rtc_timer_init(&rtctimer, NULL, NULL);
131 static struct class_interface alarmtimer_rtc_interface = {
132 .add_dev = &alarmtimer_rtc_add_device,
135 static int alarmtimer_rtc_interface_setup(void)
137 alarmtimer_rtc_interface.class = rtc_class;
138 return class_interface_register(&alarmtimer_rtc_interface);
140 static void alarmtimer_rtc_interface_remove(void)
142 class_interface_unregister(&alarmtimer_rtc_interface);
145 static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
146 static inline void alarmtimer_rtc_interface_remove(void) { }
147 static inline void alarmtimer_rtc_timer_init(void) { }
151 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
152 * @base: pointer to the base where the timer is being run
153 * @alarm: pointer to alarm being enqueued.
155 * Adds alarm to a alarm_base timerqueue
157 * Must hold base->lock when calling.
159 static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
161 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
162 timerqueue_del(&base->timerqueue, &alarm->node);
164 timerqueue_add(&base->timerqueue, &alarm->node);
165 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
169 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
170 * @base: pointer to the base where the timer is running
171 * @alarm: pointer to alarm being removed
173 * Removes alarm to a alarm_base timerqueue
175 * Must hold base->lock when calling.
177 static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
179 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
182 timerqueue_del(&base->timerqueue, &alarm->node);
183 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
188 * alarmtimer_fired - Handles alarm hrtimer being fired.
189 * @timer: pointer to hrtimer being run
191 * When a alarm timer fires, this runs through the timerqueue to
192 * see which alarms expired, and runs those. If there are more alarm
193 * timers queued for the future, we set the hrtimer to fire when
194 * the next future alarm timer expires.
196 static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
198 struct alarm *alarm = container_of(timer, struct alarm, timer);
199 struct alarm_base *base = &alarm_bases[alarm->type];
201 int ret = HRTIMER_NORESTART;
202 int restart = ALARMTIMER_NORESTART;
204 spin_lock_irqsave(&base->lock, flags);
205 alarmtimer_dequeue(base, alarm);
206 spin_unlock_irqrestore(&base->lock, flags);
209 restart = alarm->function(alarm, base->get_ktime());
211 spin_lock_irqsave(&base->lock, flags);
212 if (restart != ALARMTIMER_NORESTART) {
213 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
214 alarmtimer_enqueue(base, alarm);
215 ret = HRTIMER_RESTART;
217 spin_unlock_irqrestore(&base->lock, flags);
219 trace_alarmtimer_fired(alarm, base->get_ktime());
224 ktime_t alarm_expires_remaining(const struct alarm *alarm)
226 struct alarm_base *base = &alarm_bases[alarm->type];
227 return ktime_sub(alarm->node.expires, base->get_ktime());
229 EXPORT_SYMBOL_GPL(alarm_expires_remaining);
231 #ifdef CONFIG_RTC_CLASS
233 * alarmtimer_suspend - Suspend time callback
236 * When we are going into suspend, we look through the bases
237 * to see which is the soonest timer to expire. We then
238 * set an rtc timer to fire that far into the future, which
239 * will wake us from suspend.
241 static int alarmtimer_suspend(struct device *dev)
243 ktime_t min, now, expires;
245 struct rtc_device *rtc;
249 spin_lock_irqsave(&freezer_delta_lock, flags);
251 expires = freezer_expires;
252 type = freezer_alarmtype;
254 spin_unlock_irqrestore(&freezer_delta_lock, flags);
256 rtc = alarmtimer_get_rtcdev();
257 /* If we have no rtcdev, just return */
261 /* Find the soonest timer to expire*/
262 for (i = 0; i < ALARM_NUMTYPE; i++) {
263 struct alarm_base *base = &alarm_bases[i];
264 struct timerqueue_node *next;
267 spin_lock_irqsave(&base->lock, flags);
268 next = timerqueue_getnext(&base->timerqueue);
269 spin_unlock_irqrestore(&base->lock, flags);
272 delta = ktime_sub(next->expires, base->get_ktime());
273 if (!min || (delta < min)) {
274 expires = next->expires;
282 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
283 pm_wakeup_event(dev, 2 * MSEC_PER_SEC);
287 trace_alarmtimer_suspend(expires, type);
289 /* Setup an rtc timer to fire that far in the future */
290 rtc_timer_cancel(rtc, &rtctimer);
291 rtc_read_time(rtc, &tm);
292 now = rtc_tm_to_ktime(tm);
293 now = ktime_add(now, min);
295 /* Set alarm, if in the past reject suspend briefly to handle */
296 ret = rtc_timer_start(rtc, &rtctimer, now, 0);
298 pm_wakeup_event(dev, MSEC_PER_SEC);
302 static int alarmtimer_resume(struct device *dev)
304 struct rtc_device *rtc;
306 rtc = alarmtimer_get_rtcdev();
308 rtc_timer_cancel(rtc, &rtctimer);
313 static int alarmtimer_suspend(struct device *dev)
318 static int alarmtimer_resume(struct device *dev)
325 __alarm_init(struct alarm *alarm, enum alarmtimer_type type,
326 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
328 timerqueue_init(&alarm->node);
329 alarm->timer.function = alarmtimer_fired;
330 alarm->function = function;
332 alarm->state = ALARMTIMER_STATE_INACTIVE;
336 * alarm_init - Initialize an alarm structure
337 * @alarm: ptr to alarm to be initialized
338 * @type: the type of the alarm
339 * @function: callback that is run when the alarm fires
341 void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
342 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
344 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
346 __alarm_init(alarm, type, function);
348 EXPORT_SYMBOL_GPL(alarm_init);
351 * alarm_start - Sets an absolute alarm to fire
352 * @alarm: ptr to alarm to set
353 * @start: time to run the alarm
355 void alarm_start(struct alarm *alarm, ktime_t start)
357 struct alarm_base *base = &alarm_bases[alarm->type];
360 spin_lock_irqsave(&base->lock, flags);
361 alarm->node.expires = start;
362 alarmtimer_enqueue(base, alarm);
363 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
364 spin_unlock_irqrestore(&base->lock, flags);
366 trace_alarmtimer_start(alarm, base->get_ktime());
368 EXPORT_SYMBOL_GPL(alarm_start);
371 * alarm_start_relative - Sets a relative alarm to fire
372 * @alarm: ptr to alarm to set
373 * @start: time relative to now to run the alarm
375 void alarm_start_relative(struct alarm *alarm, ktime_t start)
377 struct alarm_base *base = &alarm_bases[alarm->type];
379 start = ktime_add_safe(start, base->get_ktime());
380 alarm_start(alarm, start);
382 EXPORT_SYMBOL_GPL(alarm_start_relative);
384 void alarm_restart(struct alarm *alarm)
386 struct alarm_base *base = &alarm_bases[alarm->type];
389 spin_lock_irqsave(&base->lock, flags);
390 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
391 hrtimer_restart(&alarm->timer);
392 alarmtimer_enqueue(base, alarm);
393 spin_unlock_irqrestore(&base->lock, flags);
395 EXPORT_SYMBOL_GPL(alarm_restart);
398 * alarm_try_to_cancel - Tries to cancel an alarm timer
399 * @alarm: ptr to alarm to be canceled
401 * Returns 1 if the timer was canceled, 0 if it was not running,
402 * and -1 if the callback was running
404 int alarm_try_to_cancel(struct alarm *alarm)
406 struct alarm_base *base = &alarm_bases[alarm->type];
410 spin_lock_irqsave(&base->lock, flags);
411 ret = hrtimer_try_to_cancel(&alarm->timer);
413 alarmtimer_dequeue(base, alarm);
414 spin_unlock_irqrestore(&base->lock, flags);
416 trace_alarmtimer_cancel(alarm, base->get_ktime());
419 EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
423 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
424 * @alarm: ptr to alarm to be canceled
426 * Returns 1 if the timer was canceled, 0 if it was not active.
428 int alarm_cancel(struct alarm *alarm)
431 int ret = alarm_try_to_cancel(alarm);
434 hrtimer_cancel_wait_running(&alarm->timer);
437 EXPORT_SYMBOL_GPL(alarm_cancel);
440 u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
445 delta = ktime_sub(now, alarm->node.expires);
450 if (unlikely(delta >= interval)) {
451 s64 incr = ktime_to_ns(interval);
453 overrun = ktime_divns(delta, incr);
455 alarm->node.expires = ktime_add_ns(alarm->node.expires,
458 if (alarm->node.expires > now)
461 * This (and the ktime_add() below) is the
462 * correction for exact:
467 alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
470 EXPORT_SYMBOL_GPL(alarm_forward);
472 static u64 __alarm_forward_now(struct alarm *alarm, ktime_t interval, bool throttle)
474 struct alarm_base *base = &alarm_bases[alarm->type];
475 ktime_t now = base->get_ktime();
477 if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS) && throttle) {
479 * Same issue as with posix_timer_fn(). Timers which are
480 * periodic but the signal is ignored can starve the system
481 * with a very small interval. The real fix which was
482 * promised in the context of posix_timer_fn() never
483 * materialized, but someone should really work on it.
485 * To prevent DOS fake @now to be 1 jiffie out which keeps
486 * the overrun accounting correct but creates an
487 * inconsistency vs. timer_gettime(2).
489 ktime_t kj = NSEC_PER_SEC / HZ;
492 now = ktime_add(now, kj);
495 return alarm_forward(alarm, now, interval);
498 u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
500 return __alarm_forward_now(alarm, interval, false);
502 EXPORT_SYMBOL_GPL(alarm_forward_now);
504 #ifdef CONFIG_POSIX_TIMERS
506 static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
508 struct alarm_base *base;
514 base = &alarm_bases[ALARM_REALTIME];
515 type = ALARM_REALTIME_FREEZER;
518 base = &alarm_bases[ALARM_BOOTTIME];
519 type = ALARM_BOOTTIME_FREEZER;
522 WARN_ONCE(1, "Invalid alarm type: %d\n", type);
526 delta = ktime_sub(absexp, base->get_ktime());
528 spin_lock_irqsave(&freezer_delta_lock, flags);
529 if (!freezer_delta || (delta < freezer_delta)) {
530 freezer_delta = delta;
531 freezer_expires = absexp;
532 freezer_alarmtype = type;
534 spin_unlock_irqrestore(&freezer_delta_lock, flags);
538 * clock2alarm - helper that converts from clockid to alarmtypes
541 static enum alarmtimer_type clock2alarm(clockid_t clockid)
543 if (clockid == CLOCK_REALTIME_ALARM)
544 return ALARM_REALTIME;
545 if (clockid == CLOCK_BOOTTIME_ALARM)
546 return ALARM_BOOTTIME;
551 * alarm_handle_timer - Callback for posix timers
552 * @alarm: alarm that fired
553 * @now: time at the timer expiration
555 * Posix timer callback for expired alarm timers.
557 * Return: whether the timer is to be restarted
559 static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
562 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
563 it.alarm.alarmtimer);
564 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
568 spin_lock_irqsave(&ptr->it_lock, flags);
571 if (ptr->it_interval)
572 si_private = ++ptr->it_requeue_pending;
574 if (posix_timer_event(ptr, si_private) && ptr->it_interval) {
576 * Handle ignored signals and rearm the timer. This will go
577 * away once we handle ignored signals proper. Ensure that
578 * small intervals cannot starve the system.
580 ptr->it_overrun += __alarm_forward_now(alarm, ptr->it_interval, true);
581 ++ptr->it_requeue_pending;
583 result = ALARMTIMER_RESTART;
585 spin_unlock_irqrestore(&ptr->it_lock, flags);
591 * alarm_timer_rearm - Posix timer callback for rearming timer
592 * @timr: Pointer to the posixtimer data struct
594 static void alarm_timer_rearm(struct k_itimer *timr)
596 struct alarm *alarm = &timr->it.alarm.alarmtimer;
598 timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
599 alarm_start(alarm, alarm->node.expires);
603 * alarm_timer_forward - Posix timer callback for forwarding timer
604 * @timr: Pointer to the posixtimer data struct
605 * @now: Current time to forward the timer against
607 static s64 alarm_timer_forward(struct k_itimer *timr, ktime_t now)
609 struct alarm *alarm = &timr->it.alarm.alarmtimer;
611 return alarm_forward(alarm, timr->it_interval, now);
615 * alarm_timer_remaining - Posix timer callback to retrieve remaining time
616 * @timr: Pointer to the posixtimer data struct
617 * @now: Current time to calculate against
619 static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
621 struct alarm *alarm = &timr->it.alarm.alarmtimer;
623 return ktime_sub(alarm->node.expires, now);
627 * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
628 * @timr: Pointer to the posixtimer data struct
630 static int alarm_timer_try_to_cancel(struct k_itimer *timr)
632 return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
636 * alarm_timer_wait_running - Posix timer callback to wait for a timer
637 * @timr: Pointer to the posixtimer data struct
639 * Called from the core code when timer cancel detected that the callback
640 * is running. @timr is unlocked and rcu read lock is held to prevent it
643 static void alarm_timer_wait_running(struct k_itimer *timr)
645 hrtimer_cancel_wait_running(&timr->it.alarm.alarmtimer.timer);
649 * alarm_timer_arm - Posix timer callback to arm a timer
650 * @timr: Pointer to the posixtimer data struct
651 * @expires: The new expiry time
652 * @absolute: Expiry value is absolute time
653 * @sigev_none: Posix timer does not deliver signals
655 static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
656 bool absolute, bool sigev_none)
658 struct alarm *alarm = &timr->it.alarm.alarmtimer;
659 struct alarm_base *base = &alarm_bases[alarm->type];
662 expires = ktime_add_safe(expires, base->get_ktime());
664 alarm->node.expires = expires;
666 alarm_start(&timr->it.alarm.alarmtimer, expires);
670 * alarm_clock_getres - posix getres interface
671 * @which_clock: clockid
672 * @tp: timespec to fill
674 * Returns the granularity of underlying alarm base clock
676 static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
678 if (!alarmtimer_get_rtcdev())
682 tp->tv_nsec = hrtimer_resolution;
687 * alarm_clock_get_timespec - posix clock_get_timespec interface
688 * @which_clock: clockid
689 * @tp: timespec to fill.
691 * Provides the underlying alarm base time in a tasks time namespace.
693 static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tp)
695 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
697 if (!alarmtimer_get_rtcdev())
700 base->get_timespec(tp);
706 * alarm_clock_get_ktime - posix clock_get_ktime interface
707 * @which_clock: clockid
709 * Provides the underlying alarm base time in the root namespace.
711 static ktime_t alarm_clock_get_ktime(clockid_t which_clock)
713 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
715 if (!alarmtimer_get_rtcdev())
718 return base->get_ktime();
722 * alarm_timer_create - posix timer_create interface
723 * @new_timer: k_itimer pointer to manage
725 * Initializes the k_itimer structure.
727 static int alarm_timer_create(struct k_itimer *new_timer)
729 enum alarmtimer_type type;
731 if (!alarmtimer_get_rtcdev())
734 if (!capable(CAP_WAKE_ALARM))
737 type = clock2alarm(new_timer->it_clock);
738 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
743 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
744 * @alarm: ptr to alarm that fired
745 * @now: time at the timer expiration
747 * Wakes up the task that set the alarmtimer
749 * Return: ALARMTIMER_NORESTART
751 static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
754 struct task_struct *task = alarm->data;
758 wake_up_process(task);
759 return ALARMTIMER_NORESTART;
763 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
764 * @alarm: ptr to alarmtimer
765 * @absexp: absolute expiration time
766 * @type: alarm type (BOOTTIME/REALTIME).
768 * Sets the alarm timer and sleeps until it is fired or interrupted.
770 static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
771 enum alarmtimer_type type)
773 struct restart_block *restart;
774 alarm->data = (void *)current;
776 set_current_state(TASK_INTERRUPTIBLE);
777 alarm_start(alarm, absexp);
778 if (likely(alarm->data))
782 } while (alarm->data && !signal_pending(current));
784 __set_current_state(TASK_RUNNING);
786 destroy_hrtimer_on_stack(&alarm->timer);
791 if (freezing(current))
792 alarmtimer_freezerset(absexp, type);
793 restart = ¤t->restart_block;
794 if (restart->nanosleep.type != TT_NONE) {
795 struct timespec64 rmt;
798 rem = ktime_sub(absexp, alarm_bases[type].get_ktime());
802 rmt = ktime_to_timespec64(rem);
804 return nanosleep_copyout(restart, &rmt);
806 return -ERESTART_RESTARTBLOCK;
810 alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type,
811 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
813 hrtimer_init_on_stack(&alarm->timer, alarm_bases[type].base_clockid,
815 __alarm_init(alarm, type, function);
819 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
820 * @restart: ptr to restart block
822 * Handles restarted clock_nanosleep calls
824 static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
826 enum alarmtimer_type type = restart->nanosleep.clockid;
827 ktime_t exp = restart->nanosleep.expires;
830 alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
832 return alarmtimer_do_nsleep(&alarm, exp, type);
836 * alarm_timer_nsleep - alarmtimer nanosleep
837 * @which_clock: clockid
838 * @flags: determines abstime or relative
839 * @tsreq: requested sleep time (abs or rel)
841 * Handles clock_nanosleep calls against _ALARM clockids
843 static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
844 const struct timespec64 *tsreq)
846 enum alarmtimer_type type = clock2alarm(which_clock);
847 struct restart_block *restart = ¤t->restart_block;
852 if (!alarmtimer_get_rtcdev())
855 if (flags & ~TIMER_ABSTIME)
858 if (!capable(CAP_WAKE_ALARM))
861 alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
863 exp = timespec64_to_ktime(*tsreq);
864 /* Convert (if necessary) to absolute time */
865 if (flags != TIMER_ABSTIME) {
866 ktime_t now = alarm_bases[type].get_ktime();
868 exp = ktime_add_safe(now, exp);
870 exp = timens_ktime_to_host(which_clock, exp);
873 ret = alarmtimer_do_nsleep(&alarm, exp, type);
874 if (ret != -ERESTART_RESTARTBLOCK)
877 /* abs timers don't set remaining time or restart */
878 if (flags == TIMER_ABSTIME)
879 return -ERESTARTNOHAND;
881 restart->nanosleep.clockid = type;
882 restart->nanosleep.expires = exp;
883 set_restart_fn(restart, alarm_timer_nsleep_restart);
887 const struct k_clock alarm_clock = {
888 .clock_getres = alarm_clock_getres,
889 .clock_get_ktime = alarm_clock_get_ktime,
890 .clock_get_timespec = alarm_clock_get_timespec,
891 .timer_create = alarm_timer_create,
892 .timer_set = common_timer_set,
893 .timer_del = common_timer_del,
894 .timer_get = common_timer_get,
895 .timer_arm = alarm_timer_arm,
896 .timer_rearm = alarm_timer_rearm,
897 .timer_forward = alarm_timer_forward,
898 .timer_remaining = alarm_timer_remaining,
899 .timer_try_to_cancel = alarm_timer_try_to_cancel,
900 .timer_wait_running = alarm_timer_wait_running,
901 .nsleep = alarm_timer_nsleep,
903 #endif /* CONFIG_POSIX_TIMERS */
906 /* Suspend hook structures */
907 static const struct dev_pm_ops alarmtimer_pm_ops = {
908 .suspend = alarmtimer_suspend,
909 .resume = alarmtimer_resume,
912 static struct platform_driver alarmtimer_driver = {
914 .name = "alarmtimer",
915 .pm = &alarmtimer_pm_ops,
919 static void get_boottime_timespec(struct timespec64 *tp)
921 ktime_get_boottime_ts64(tp);
922 timens_add_boottime(tp);
926 * alarmtimer_init - Initialize alarm timer code
928 * This function initializes the alarm bases and registers
929 * the posix clock ids.
931 static int __init alarmtimer_init(void)
936 alarmtimer_rtc_timer_init();
938 /* Initialize alarm bases */
939 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
940 alarm_bases[ALARM_REALTIME].get_ktime = &ktime_get_real;
941 alarm_bases[ALARM_REALTIME].get_timespec = ktime_get_real_ts64;
942 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
943 alarm_bases[ALARM_BOOTTIME].get_ktime = &ktime_get_boottime;
944 alarm_bases[ALARM_BOOTTIME].get_timespec = get_boottime_timespec;
945 for (i = 0; i < ALARM_NUMTYPE; i++) {
946 timerqueue_init_head(&alarm_bases[i].timerqueue);
947 spin_lock_init(&alarm_bases[i].lock);
950 error = alarmtimer_rtc_interface_setup();
954 error = platform_driver_register(&alarmtimer_driver);
960 alarmtimer_rtc_interface_remove();
963 device_initcall(alarmtimer_init);