Merge tag 'docs-6.5-2' of git://git.lwn.net/linux
[platform/kernel/linux-rpi.git] / kernel / time / alarmtimer.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Alarmtimer interface
4  *
5  * This interface provides a timer which is similar to hrtimers,
6  * but triggers a RTC alarm if the box is suspend.
7  *
8  * This interface is influenced by the Android RTC Alarm timer
9  * interface.
10  *
11  * Copyright (C) 2010 IBM Corporation
12  *
13  * Author: John Stultz <john.stultz@linaro.org>
14  */
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>
30
31 #include "posix-timers.h"
32
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/alarmtimer.h>
35
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
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
43  */
44 static struct alarm_base {
45         spinlock_t              lock;
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];
51
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);
58 #endif
59
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);
65
66 /**
67  * alarmtimer_get_rtcdev - Return selected rtcdevice
68  *
69  * This function returns the rtc device to use for wakealarms.
70  */
71 struct rtc_device *alarmtimer_get_rtcdev(void)
72 {
73         unsigned long flags;
74         struct rtc_device *ret;
75
76         spin_lock_irqsave(&rtcdev_lock, flags);
77         ret = rtcdev;
78         spin_unlock_irqrestore(&rtcdev_lock, flags);
79
80         return ret;
81 }
82 EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
83
84 static int alarmtimer_rtc_add_device(struct device *dev)
85 {
86         unsigned long flags;
87         struct rtc_device *rtc = to_rtc_device(dev);
88         struct platform_device *pdev;
89         int ret = 0;
90
91         if (rtcdev)
92                 return -EBUSY;
93
94         if (!test_bit(RTC_FEATURE_ALARM, rtc->features))
95                 return -1;
96         if (!device_may_wakeup(rtc->dev.parent))
97                 return -1;
98
99         pdev = platform_device_register_data(dev, "alarmtimer",
100                                              PLATFORM_DEVID_AUTO, NULL, 0);
101         if (!IS_ERR(pdev))
102                 device_init_wakeup(&pdev->dev, true);
103
104         spin_lock_irqsave(&rtcdev_lock, flags);
105         if (!IS_ERR(pdev) && !rtcdev) {
106                 if (!try_module_get(rtc->owner)) {
107                         ret = -1;
108                         goto unlock;
109                 }
110
111                 rtcdev = rtc;
112                 /* hold a reference so it doesn't go away */
113                 get_device(dev);
114                 pdev = NULL;
115         } else {
116                 ret = -1;
117         }
118 unlock:
119         spin_unlock_irqrestore(&rtcdev_lock, flags);
120
121         platform_device_unregister(pdev);
122
123         return ret;
124 }
125
126 static inline void alarmtimer_rtc_timer_init(void)
127 {
128         rtc_timer_init(&rtctimer, NULL, NULL);
129 }
130
131 static struct class_interface alarmtimer_rtc_interface = {
132         .add_dev = &alarmtimer_rtc_add_device,
133 };
134
135 static int alarmtimer_rtc_interface_setup(void)
136 {
137         alarmtimer_rtc_interface.class = rtc_class;
138         return class_interface_register(&alarmtimer_rtc_interface);
139 }
140 static void alarmtimer_rtc_interface_remove(void)
141 {
142         class_interface_unregister(&alarmtimer_rtc_interface);
143 }
144 #else
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) { }
148 #endif
149
150 /**
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.
154  *
155  * Adds alarm to a alarm_base timerqueue
156  *
157  * Must hold base->lock when calling.
158  */
159 static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
160 {
161         if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
162                 timerqueue_del(&base->timerqueue, &alarm->node);
163
164         timerqueue_add(&base->timerqueue, &alarm->node);
165         alarm->state |= ALARMTIMER_STATE_ENQUEUED;
166 }
167
168 /**
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
172  *
173  * Removes alarm to a alarm_base timerqueue
174  *
175  * Must hold base->lock when calling.
176  */
177 static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
178 {
179         if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
180                 return;
181
182         timerqueue_del(&base->timerqueue, &alarm->node);
183         alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
184 }
185
186
187 /**
188  * alarmtimer_fired - Handles alarm hrtimer being fired.
189  * @timer: pointer to hrtimer being run
190  *
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.
195  */
196 static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
197 {
198         struct alarm *alarm = container_of(timer, struct alarm, timer);
199         struct alarm_base *base = &alarm_bases[alarm->type];
200         unsigned long flags;
201         int ret = HRTIMER_NORESTART;
202         int restart = ALARMTIMER_NORESTART;
203
204         spin_lock_irqsave(&base->lock, flags);
205         alarmtimer_dequeue(base, alarm);
206         spin_unlock_irqrestore(&base->lock, flags);
207
208         if (alarm->function)
209                 restart = alarm->function(alarm, base->get_ktime());
210
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;
216         }
217         spin_unlock_irqrestore(&base->lock, flags);
218
219         trace_alarmtimer_fired(alarm, base->get_ktime());
220         return ret;
221
222 }
223
224 ktime_t alarm_expires_remaining(const struct alarm *alarm)
225 {
226         struct alarm_base *base = &alarm_bases[alarm->type];
227         return ktime_sub(alarm->node.expires, base->get_ktime());
228 }
229 EXPORT_SYMBOL_GPL(alarm_expires_remaining);
230
231 #ifdef CONFIG_RTC_CLASS
232 /**
233  * alarmtimer_suspend - Suspend time callback
234  * @dev: unused
235  *
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.
240  */
241 static int alarmtimer_suspend(struct device *dev)
242 {
243         ktime_t min, now, expires;
244         int i, ret, type;
245         struct rtc_device *rtc;
246         unsigned long flags;
247         struct rtc_time tm;
248
249         spin_lock_irqsave(&freezer_delta_lock, flags);
250         min = freezer_delta;
251         expires = freezer_expires;
252         type = freezer_alarmtype;
253         freezer_delta = 0;
254         spin_unlock_irqrestore(&freezer_delta_lock, flags);
255
256         rtc = alarmtimer_get_rtcdev();
257         /* If we have no rtcdev, just return */
258         if (!rtc)
259                 return 0;
260
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;
265                 ktime_t delta;
266
267                 spin_lock_irqsave(&base->lock, flags);
268                 next = timerqueue_getnext(&base->timerqueue);
269                 spin_unlock_irqrestore(&base->lock, flags);
270                 if (!next)
271                         continue;
272                 delta = ktime_sub(next->expires, base->get_ktime());
273                 if (!min || (delta < min)) {
274                         expires = next->expires;
275                         min = delta;
276                         type = i;
277                 }
278         }
279         if (min == 0)
280                 return 0;
281
282         if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
283                 pm_wakeup_event(dev, 2 * MSEC_PER_SEC);
284                 return -EBUSY;
285         }
286
287         trace_alarmtimer_suspend(expires, type);
288
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);
294
295         /* Set alarm, if in the past reject suspend briefly to handle */
296         ret = rtc_timer_start(rtc, &rtctimer, now, 0);
297         if (ret < 0)
298                 pm_wakeup_event(dev, MSEC_PER_SEC);
299         return ret;
300 }
301
302 static int alarmtimer_resume(struct device *dev)
303 {
304         struct rtc_device *rtc;
305
306         rtc = alarmtimer_get_rtcdev();
307         if (rtc)
308                 rtc_timer_cancel(rtc, &rtctimer);
309         return 0;
310 }
311
312 #else
313 static int alarmtimer_suspend(struct device *dev)
314 {
315         return 0;
316 }
317
318 static int alarmtimer_resume(struct device *dev)
319 {
320         return 0;
321 }
322 #endif
323
324 static void
325 __alarm_init(struct alarm *alarm, enum alarmtimer_type type,
326              enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
327 {
328         timerqueue_init(&alarm->node);
329         alarm->timer.function = alarmtimer_fired;
330         alarm->function = function;
331         alarm->type = type;
332         alarm->state = ALARMTIMER_STATE_INACTIVE;
333 }
334
335 /**
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
340  */
341 void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
342                 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
343 {
344         hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
345                      HRTIMER_MODE_ABS);
346         __alarm_init(alarm, type, function);
347 }
348 EXPORT_SYMBOL_GPL(alarm_init);
349
350 /**
351  * alarm_start - Sets an absolute alarm to fire
352  * @alarm: ptr to alarm to set
353  * @start: time to run the alarm
354  */
355 void alarm_start(struct alarm *alarm, ktime_t start)
356 {
357         struct alarm_base *base = &alarm_bases[alarm->type];
358         unsigned long flags;
359
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);
365
366         trace_alarmtimer_start(alarm, base->get_ktime());
367 }
368 EXPORT_SYMBOL_GPL(alarm_start);
369
370 /**
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
374  */
375 void alarm_start_relative(struct alarm *alarm, ktime_t start)
376 {
377         struct alarm_base *base = &alarm_bases[alarm->type];
378
379         start = ktime_add_safe(start, base->get_ktime());
380         alarm_start(alarm, start);
381 }
382 EXPORT_SYMBOL_GPL(alarm_start_relative);
383
384 void alarm_restart(struct alarm *alarm)
385 {
386         struct alarm_base *base = &alarm_bases[alarm->type];
387         unsigned long flags;
388
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);
394 }
395 EXPORT_SYMBOL_GPL(alarm_restart);
396
397 /**
398  * alarm_try_to_cancel - Tries to cancel an alarm timer
399  * @alarm: ptr to alarm to be canceled
400  *
401  * Returns 1 if the timer was canceled, 0 if it was not running,
402  * and -1 if the callback was running
403  */
404 int alarm_try_to_cancel(struct alarm *alarm)
405 {
406         struct alarm_base *base = &alarm_bases[alarm->type];
407         unsigned long flags;
408         int ret;
409
410         spin_lock_irqsave(&base->lock, flags);
411         ret = hrtimer_try_to_cancel(&alarm->timer);
412         if (ret >= 0)
413                 alarmtimer_dequeue(base, alarm);
414         spin_unlock_irqrestore(&base->lock, flags);
415
416         trace_alarmtimer_cancel(alarm, base->get_ktime());
417         return ret;
418 }
419 EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
420
421
422 /**
423  * alarm_cancel - Spins trying to cancel an alarm timer until it is done
424  * @alarm: ptr to alarm to be canceled
425  *
426  * Returns 1 if the timer was canceled, 0 if it was not active.
427  */
428 int alarm_cancel(struct alarm *alarm)
429 {
430         for (;;) {
431                 int ret = alarm_try_to_cancel(alarm);
432                 if (ret >= 0)
433                         return ret;
434                 hrtimer_cancel_wait_running(&alarm->timer);
435         }
436 }
437 EXPORT_SYMBOL_GPL(alarm_cancel);
438
439
440 u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
441 {
442         u64 overrun = 1;
443         ktime_t delta;
444
445         delta = ktime_sub(now, alarm->node.expires);
446
447         if (delta < 0)
448                 return 0;
449
450         if (unlikely(delta >= interval)) {
451                 s64 incr = ktime_to_ns(interval);
452
453                 overrun = ktime_divns(delta, incr);
454
455                 alarm->node.expires = ktime_add_ns(alarm->node.expires,
456                                                         incr*overrun);
457
458                 if (alarm->node.expires > now)
459                         return overrun;
460                 /*
461                  * This (and the ktime_add() below) is the
462                  * correction for exact:
463                  */
464                 overrun++;
465         }
466
467         alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
468         return overrun;
469 }
470 EXPORT_SYMBOL_GPL(alarm_forward);
471
472 static u64 __alarm_forward_now(struct alarm *alarm, ktime_t interval, bool throttle)
473 {
474         struct alarm_base *base = &alarm_bases[alarm->type];
475         ktime_t now = base->get_ktime();
476
477         if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS) && throttle) {
478                 /*
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.
484                  *
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).
488                  */
489                 ktime_t kj = NSEC_PER_SEC / HZ;
490
491                 if (interval < kj)
492                         now = ktime_add(now, kj);
493         }
494
495         return alarm_forward(alarm, now, interval);
496 }
497
498 u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
499 {
500         return __alarm_forward_now(alarm, interval, false);
501 }
502 EXPORT_SYMBOL_GPL(alarm_forward_now);
503
504 #ifdef CONFIG_POSIX_TIMERS
505
506 static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
507 {
508         struct alarm_base *base;
509         unsigned long flags;
510         ktime_t delta;
511
512         switch(type) {
513         case ALARM_REALTIME:
514                 base = &alarm_bases[ALARM_REALTIME];
515                 type = ALARM_REALTIME_FREEZER;
516                 break;
517         case ALARM_BOOTTIME:
518                 base = &alarm_bases[ALARM_BOOTTIME];
519                 type = ALARM_BOOTTIME_FREEZER;
520                 break;
521         default:
522                 WARN_ONCE(1, "Invalid alarm type: %d\n", type);
523                 return;
524         }
525
526         delta = ktime_sub(absexp, base->get_ktime());
527
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;
533         }
534         spin_unlock_irqrestore(&freezer_delta_lock, flags);
535 }
536
537 /**
538  * clock2alarm - helper that converts from clockid to alarmtypes
539  * @clockid: clockid.
540  */
541 static enum alarmtimer_type clock2alarm(clockid_t clockid)
542 {
543         if (clockid == CLOCK_REALTIME_ALARM)
544                 return ALARM_REALTIME;
545         if (clockid == CLOCK_BOOTTIME_ALARM)
546                 return ALARM_BOOTTIME;
547         return -1;
548 }
549
550 /**
551  * alarm_handle_timer - Callback for posix timers
552  * @alarm: alarm that fired
553  * @now: time at the timer expiration
554  *
555  * Posix timer callback for expired alarm timers.
556  *
557  * Return: whether the timer is to be restarted
558  */
559 static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
560                                                         ktime_t now)
561 {
562         struct k_itimer *ptr = container_of(alarm, struct k_itimer,
563                                             it.alarm.alarmtimer);
564         enum alarmtimer_restart result = ALARMTIMER_NORESTART;
565         unsigned long flags;
566         int si_private = 0;
567
568         spin_lock_irqsave(&ptr->it_lock, flags);
569
570         ptr->it_active = 0;
571         if (ptr->it_interval)
572                 si_private = ++ptr->it_requeue_pending;
573
574         if (posix_timer_event(ptr, si_private) && ptr->it_interval) {
575                 /*
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.
579                  */
580                 ptr->it_overrun += __alarm_forward_now(alarm, ptr->it_interval, true);
581                 ++ptr->it_requeue_pending;
582                 ptr->it_active = 1;
583                 result = ALARMTIMER_RESTART;
584         }
585         spin_unlock_irqrestore(&ptr->it_lock, flags);
586
587         return result;
588 }
589
590 /**
591  * alarm_timer_rearm - Posix timer callback for rearming timer
592  * @timr:       Pointer to the posixtimer data struct
593  */
594 static void alarm_timer_rearm(struct k_itimer *timr)
595 {
596         struct alarm *alarm = &timr->it.alarm.alarmtimer;
597
598         timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
599         alarm_start(alarm, alarm->node.expires);
600 }
601
602 /**
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
606  */
607 static s64 alarm_timer_forward(struct k_itimer *timr, ktime_t now)
608 {
609         struct alarm *alarm = &timr->it.alarm.alarmtimer;
610
611         return alarm_forward(alarm, timr->it_interval, now);
612 }
613
614 /**
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
618  */
619 static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
620 {
621         struct alarm *alarm = &timr->it.alarm.alarmtimer;
622
623         return ktime_sub(alarm->node.expires, now);
624 }
625
626 /**
627  * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
628  * @timr:       Pointer to the posixtimer data struct
629  */
630 static int alarm_timer_try_to_cancel(struct k_itimer *timr)
631 {
632         return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
633 }
634
635 /**
636  * alarm_timer_wait_running - Posix timer callback to wait for a timer
637  * @timr:       Pointer to the posixtimer data struct
638  *
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
641  * from being freed.
642  */
643 static void alarm_timer_wait_running(struct k_itimer *timr)
644 {
645         hrtimer_cancel_wait_running(&timr->it.alarm.alarmtimer.timer);
646 }
647
648 /**
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
654  */
655 static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
656                             bool absolute, bool sigev_none)
657 {
658         struct alarm *alarm = &timr->it.alarm.alarmtimer;
659         struct alarm_base *base = &alarm_bases[alarm->type];
660
661         if (!absolute)
662                 expires = ktime_add_safe(expires, base->get_ktime());
663         if (sigev_none)
664                 alarm->node.expires = expires;
665         else
666                 alarm_start(&timr->it.alarm.alarmtimer, expires);
667 }
668
669 /**
670  * alarm_clock_getres - posix getres interface
671  * @which_clock: clockid
672  * @tp: timespec to fill
673  *
674  * Returns the granularity of underlying alarm base clock
675  */
676 static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
677 {
678         if (!alarmtimer_get_rtcdev())
679                 return -EINVAL;
680
681         tp->tv_sec = 0;
682         tp->tv_nsec = hrtimer_resolution;
683         return 0;
684 }
685
686 /**
687  * alarm_clock_get_timespec - posix clock_get_timespec interface
688  * @which_clock: clockid
689  * @tp: timespec to fill.
690  *
691  * Provides the underlying alarm base time in a tasks time namespace.
692  */
693 static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tp)
694 {
695         struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
696
697         if (!alarmtimer_get_rtcdev())
698                 return -EINVAL;
699
700         base->get_timespec(tp);
701
702         return 0;
703 }
704
705 /**
706  * alarm_clock_get_ktime - posix clock_get_ktime interface
707  * @which_clock: clockid
708  *
709  * Provides the underlying alarm base time in the root namespace.
710  */
711 static ktime_t alarm_clock_get_ktime(clockid_t which_clock)
712 {
713         struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
714
715         if (!alarmtimer_get_rtcdev())
716                 return -EINVAL;
717
718         return base->get_ktime();
719 }
720
721 /**
722  * alarm_timer_create - posix timer_create interface
723  * @new_timer: k_itimer pointer to manage
724  *
725  * Initializes the k_itimer structure.
726  */
727 static int alarm_timer_create(struct k_itimer *new_timer)
728 {
729         enum  alarmtimer_type type;
730
731         if (!alarmtimer_get_rtcdev())
732                 return -EOPNOTSUPP;
733
734         if (!capable(CAP_WAKE_ALARM))
735                 return -EPERM;
736
737         type = clock2alarm(new_timer->it_clock);
738         alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
739         return 0;
740 }
741
742 /**
743  * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
744  * @alarm: ptr to alarm that fired
745  * @now: time at the timer expiration
746  *
747  * Wakes up the task that set the alarmtimer
748  *
749  * Return: ALARMTIMER_NORESTART
750  */
751 static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
752                                                                 ktime_t now)
753 {
754         struct task_struct *task = alarm->data;
755
756         alarm->data = NULL;
757         if (task)
758                 wake_up_process(task);
759         return ALARMTIMER_NORESTART;
760 }
761
762 /**
763  * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
764  * @alarm: ptr to alarmtimer
765  * @absexp: absolute expiration time
766  * @type: alarm type (BOOTTIME/REALTIME).
767  *
768  * Sets the alarm timer and sleeps until it is fired or interrupted.
769  */
770 static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
771                                 enum alarmtimer_type type)
772 {
773         struct restart_block *restart;
774         alarm->data = (void *)current;
775         do {
776                 set_current_state(TASK_INTERRUPTIBLE);
777                 alarm_start(alarm, absexp);
778                 if (likely(alarm->data))
779                         schedule();
780
781                 alarm_cancel(alarm);
782         } while (alarm->data && !signal_pending(current));
783
784         __set_current_state(TASK_RUNNING);
785
786         destroy_hrtimer_on_stack(&alarm->timer);
787
788         if (!alarm->data)
789                 return 0;
790
791         if (freezing(current))
792                 alarmtimer_freezerset(absexp, type);
793         restart = &current->restart_block;
794         if (restart->nanosleep.type != TT_NONE) {
795                 struct timespec64 rmt;
796                 ktime_t rem;
797
798                 rem = ktime_sub(absexp, alarm_bases[type].get_ktime());
799
800                 if (rem <= 0)
801                         return 0;
802                 rmt = ktime_to_timespec64(rem);
803
804                 return nanosleep_copyout(restart, &rmt);
805         }
806         return -ERESTART_RESTARTBLOCK;
807 }
808
809 static void
810 alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type,
811                     enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
812 {
813         hrtimer_init_on_stack(&alarm->timer, alarm_bases[type].base_clockid,
814                               HRTIMER_MODE_ABS);
815         __alarm_init(alarm, type, function);
816 }
817
818 /**
819  * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
820  * @restart: ptr to restart block
821  *
822  * Handles restarted clock_nanosleep calls
823  */
824 static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
825 {
826         enum  alarmtimer_type type = restart->nanosleep.clockid;
827         ktime_t exp = restart->nanosleep.expires;
828         struct alarm alarm;
829
830         alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
831
832         return alarmtimer_do_nsleep(&alarm, exp, type);
833 }
834
835 /**
836  * alarm_timer_nsleep - alarmtimer nanosleep
837  * @which_clock: clockid
838  * @flags: determines abstime or relative
839  * @tsreq: requested sleep time (abs or rel)
840  *
841  * Handles clock_nanosleep calls against _ALARM clockids
842  */
843 static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
844                               const struct timespec64 *tsreq)
845 {
846         enum  alarmtimer_type type = clock2alarm(which_clock);
847         struct restart_block *restart = &current->restart_block;
848         struct alarm alarm;
849         ktime_t exp;
850         int ret;
851
852         if (!alarmtimer_get_rtcdev())
853                 return -EOPNOTSUPP;
854
855         if (flags & ~TIMER_ABSTIME)
856                 return -EINVAL;
857
858         if (!capable(CAP_WAKE_ALARM))
859                 return -EPERM;
860
861         alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
862
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();
867
868                 exp = ktime_add_safe(now, exp);
869         } else {
870                 exp = timens_ktime_to_host(which_clock, exp);
871         }
872
873         ret = alarmtimer_do_nsleep(&alarm, exp, type);
874         if (ret != -ERESTART_RESTARTBLOCK)
875                 return ret;
876
877         /* abs timers don't set remaining time or restart */
878         if (flags == TIMER_ABSTIME)
879                 return -ERESTARTNOHAND;
880
881         restart->nanosleep.clockid = type;
882         restart->nanosleep.expires = exp;
883         set_restart_fn(restart, alarm_timer_nsleep_restart);
884         return ret;
885 }
886
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,
902 };
903 #endif /* CONFIG_POSIX_TIMERS */
904
905
906 /* Suspend hook structures */
907 static const struct dev_pm_ops alarmtimer_pm_ops = {
908         .suspend = alarmtimer_suspend,
909         .resume = alarmtimer_resume,
910 };
911
912 static struct platform_driver alarmtimer_driver = {
913         .driver = {
914                 .name = "alarmtimer",
915                 .pm = &alarmtimer_pm_ops,
916         }
917 };
918
919 static void get_boottime_timespec(struct timespec64 *tp)
920 {
921         ktime_get_boottime_ts64(tp);
922         timens_add_boottime(tp);
923 }
924
925 /**
926  * alarmtimer_init - Initialize alarm timer code
927  *
928  * This function initializes the alarm bases and registers
929  * the posix clock ids.
930  */
931 static int __init alarmtimer_init(void)
932 {
933         int error;
934         int i;
935
936         alarmtimer_rtc_timer_init();
937
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);
948         }
949
950         error = alarmtimer_rtc_interface_setup();
951         if (error)
952                 return error;
953
954         error = platform_driver_register(&alarmtimer_driver);
955         if (error)
956                 goto out_if;
957
958         return 0;
959 out_if:
960         alarmtimer_rtc_interface_remove();
961         return error;
962 }
963 device_initcall(alarmtimer_init);