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 synchronized 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 */
alarmtimer_get_rtcdev(void)71 struct rtc_device *alarmtimer_get_rtcdev(void)
72 {
73 struct rtc_device *ret;
74
75 guard(spinlock_irqsave)(&rtcdev_lock);
76 ret = rtcdev;
77
78 return ret;
79 }
80 EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
81
alarmtimer_rtc_add_device(struct device * dev)82 static int alarmtimer_rtc_add_device(struct device *dev)
83 {
84 struct rtc_device *rtc = to_rtc_device(dev);
85 struct platform_device *pdev;
86 int ret = 0;
87
88 if (rtcdev)
89 return -EBUSY;
90
91 if (!test_bit(RTC_FEATURE_ALARM, rtc->features))
92 return -1;
93 if (!device_may_wakeup(rtc->dev.parent))
94 return -1;
95
96 pdev = platform_device_register_data(dev, "alarmtimer",
97 PLATFORM_DEVID_AUTO, NULL, 0);
98 if (!IS_ERR(pdev))
99 device_init_wakeup(&pdev->dev, true);
100
101 scoped_guard(spinlock_irqsave, &rtcdev_lock) {
102 if (!IS_ERR(pdev) && !rtcdev && try_module_get(rtc->owner)) {
103 rtcdev = rtc;
104 /* hold a reference so it doesn't go away */
105 get_device(dev);
106 pdev = NULL;
107 } else {
108 ret = -1;
109 }
110 }
111
112 platform_device_unregister(pdev);
113 return ret;
114 }
115
alarmtimer_rtc_timer_init(void)116 static inline void alarmtimer_rtc_timer_init(void)
117 {
118 rtc_timer_init(&rtctimer, NULL, NULL);
119 }
120
121 static struct class_interface alarmtimer_rtc_interface = {
122 .add_dev = &alarmtimer_rtc_add_device,
123 };
124
alarmtimer_rtc_interface_setup(void)125 static int alarmtimer_rtc_interface_setup(void)
126 {
127 alarmtimer_rtc_interface.class = &rtc_class;
128 return class_interface_register(&alarmtimer_rtc_interface);
129 }
alarmtimer_rtc_interface_remove(void)130 static void alarmtimer_rtc_interface_remove(void)
131 {
132 class_interface_unregister(&alarmtimer_rtc_interface);
133 }
134 #else
alarmtimer_rtc_interface_setup(void)135 static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
alarmtimer_rtc_interface_remove(void)136 static inline void alarmtimer_rtc_interface_remove(void) { }
alarmtimer_rtc_timer_init(void)137 static inline void alarmtimer_rtc_timer_init(void) { }
138 #endif
139
140 /**
141 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
142 * @base: pointer to the base where the timer is being run
143 * @alarm: pointer to alarm being enqueued.
144 *
145 * Adds alarm to a alarm_base timerqueue
146 *
147 * Must hold base->lock when calling.
148 */
alarmtimer_enqueue(struct alarm_base * base,struct alarm * alarm)149 static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
150 {
151 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
152 timerqueue_del(&base->timerqueue, &alarm->node);
153
154 timerqueue_add(&base->timerqueue, &alarm->node);
155 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
156 }
157
158 /**
159 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
160 * @base: pointer to the base where the timer is running
161 * @alarm: pointer to alarm being removed
162 *
163 * Removes alarm to a alarm_base timerqueue
164 *
165 * Must hold base->lock when calling.
166 */
alarmtimer_dequeue(struct alarm_base * base,struct alarm * alarm)167 static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
168 {
169 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
170 return;
171
172 timerqueue_del(&base->timerqueue, &alarm->node);
173 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
174 }
175
176
177 /**
178 * alarmtimer_fired - Handles alarm hrtimer being fired.
179 * @timer: pointer to hrtimer being run
180 *
181 * When a alarm timer fires, this runs through the timerqueue to
182 * see which alarms expired, and runs those. If there are more alarm
183 * timers queued for the future, we set the hrtimer to fire when
184 * the next future alarm timer expires.
185 */
alarmtimer_fired(struct hrtimer * timer)186 static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
187 {
188 struct alarm *alarm = container_of(timer, struct alarm, timer);
189 struct alarm_base *base = &alarm_bases[alarm->type];
190
191 scoped_guard(spinlock_irqsave, &base->lock)
192 alarmtimer_dequeue(base, alarm);
193
194 if (alarm->function)
195 alarm->function(alarm, base->get_ktime());
196
197 trace_alarmtimer_fired(alarm, base->get_ktime());
198 return HRTIMER_NORESTART;
199 }
200
alarm_expires_remaining(const struct alarm * alarm)201 ktime_t alarm_expires_remaining(const struct alarm *alarm)
202 {
203 struct alarm_base *base = &alarm_bases[alarm->type];
204 return ktime_sub(alarm->node.expires, base->get_ktime());
205 }
206 EXPORT_SYMBOL_GPL(alarm_expires_remaining);
207
208 #ifdef CONFIG_RTC_CLASS
209 /**
210 * alarmtimer_suspend - Suspend time callback
211 * @dev: unused
212 *
213 * When we are going into suspend, we look through the bases
214 * to see which is the soonest timer to expire. We then
215 * set an rtc timer to fire that far into the future, which
216 * will wake us from suspend.
217 */
alarmtimer_suspend(struct device * dev)218 static int alarmtimer_suspend(struct device *dev)
219 {
220 ktime_t min, now, expires;
221 struct rtc_device *rtc;
222 struct rtc_time tm;
223 int i, ret, type;
224
225 scoped_guard(spinlock_irqsave, &freezer_delta_lock) {
226 min = freezer_delta;
227 expires = freezer_expires;
228 type = freezer_alarmtype;
229 freezer_delta = 0;
230 }
231
232 rtc = alarmtimer_get_rtcdev();
233 /* If we have no rtcdev, just return */
234 if (!rtc)
235 return 0;
236
237 /* Find the soonest timer to expire */
238 for (i = 0; i < ALARM_NUMTYPE; i++) {
239 struct alarm_base *base = &alarm_bases[i];
240 struct timerqueue_node *next;
241 ktime_t next_expires;
242 ktime_t delta;
243
244 scoped_guard(spinlock_irqsave, &base->lock) {
245 next = timerqueue_getnext(&base->timerqueue);
246 if (next)
247 next_expires = next->expires;
248 }
249 if (!next)
250 continue;
251 delta = ktime_sub(next_expires, base->get_ktime());
252 if (!min || (delta < min)) {
253 expires = next_expires;
254 min = delta;
255 type = i;
256 }
257 }
258 if (min == 0)
259 return 0;
260
261 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
262 pm_wakeup_event(dev, 2 * MSEC_PER_SEC);
263 return -EBUSY;
264 }
265
266 trace_alarmtimer_suspend(expires, type);
267
268 /* Setup an rtc timer to fire that far in the future */
269 rtc_timer_cancel(rtc, &rtctimer);
270 rtc_read_time(rtc, &tm);
271 now = rtc_tm_to_ktime(tm);
272
273 /*
274 * If the RTC alarm timer only supports a limited time offset, set the
275 * alarm time to the maximum supported value.
276 * The system may wake up earlier (possibly much earlier) than expected
277 * when the alarmtimer runs. This is the best the kernel can do if
278 * the alarmtimer exceeds the time that the rtc device can be programmed
279 * for.
280 */
281 min = rtc_bound_alarmtime(rtc, min);
282
283 now = ktime_add(now, min);
284
285 /* Set alarm, if in the past reject suspend briefly to handle */
286 ret = rtc_timer_start(rtc, &rtctimer, now, 0);
287 if (ret < 0)
288 pm_wakeup_event(dev, MSEC_PER_SEC);
289 return ret;
290 }
291
alarmtimer_resume(struct device * dev)292 static 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
302 #else
alarmtimer_suspend(struct device * dev)303 static int alarmtimer_suspend(struct device *dev)
304 {
305 return 0;
306 }
307
alarmtimer_resume(struct device * dev)308 static int alarmtimer_resume(struct device *dev)
309 {
310 return 0;
311 }
312 #endif
313
314 static void
__alarm_init(struct alarm * alarm,enum alarmtimer_type type,void (* function)(struct alarm *,ktime_t))315 __alarm_init(struct alarm *alarm, enum alarmtimer_type type,
316 void (*function)(struct alarm *, ktime_t))
317 {
318 timerqueue_init(&alarm->node);
319 alarm->function = function;
320 alarm->type = type;
321 alarm->state = ALARMTIMER_STATE_INACTIVE;
322 }
323
324 /**
325 * alarm_init - Initialize an alarm structure
326 * @alarm: ptr to alarm to be initialized
327 * @type: the type of the alarm
328 * @function: callback that is run when the alarm fires
329 */
alarm_init(struct alarm * alarm,enum alarmtimer_type type,void (* function)(struct alarm *,ktime_t))330 void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
331 void (*function)(struct alarm *, ktime_t))
332 {
333 hrtimer_setup(&alarm->timer, alarmtimer_fired, alarm_bases[type].base_clockid,
334 HRTIMER_MODE_ABS);
335 __alarm_init(alarm, type, function);
336 }
337 EXPORT_SYMBOL_GPL(alarm_init);
338
339 /**
340 * alarm_start - Sets an absolute alarm to fire
341 * @alarm: ptr to alarm to set
342 * @start: time to run the alarm
343 */
alarm_start(struct alarm * alarm,ktime_t start)344 void alarm_start(struct alarm *alarm, ktime_t start)
345 {
346 struct alarm_base *base = &alarm_bases[alarm->type];
347
348 scoped_guard(spinlock_irqsave, &base->lock) {
349 alarm->node.expires = start;
350 alarmtimer_enqueue(base, alarm);
351 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
352 }
353
354 trace_alarmtimer_start(alarm, base->get_ktime());
355 }
356 EXPORT_SYMBOL_GPL(alarm_start);
357
358 /**
359 * alarm_start_relative - Sets a relative alarm to fire
360 * @alarm: ptr to alarm to set
361 * @start: time relative to now to run the alarm
362 */
alarm_start_relative(struct alarm * alarm,ktime_t start)363 void alarm_start_relative(struct alarm *alarm, ktime_t start)
364 {
365 struct alarm_base *base = &alarm_bases[alarm->type];
366
367 start = ktime_add_safe(start, base->get_ktime());
368 alarm_start(alarm, start);
369 }
370 EXPORT_SYMBOL_GPL(alarm_start_relative);
371
alarm_restart(struct alarm * alarm)372 void alarm_restart(struct alarm *alarm)
373 {
374 struct alarm_base *base = &alarm_bases[alarm->type];
375
376 guard(spinlock_irqsave)(&base->lock);
377 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
378 hrtimer_restart(&alarm->timer);
379 alarmtimer_enqueue(base, alarm);
380 }
381 EXPORT_SYMBOL_GPL(alarm_restart);
382
383 /**
384 * alarm_try_to_cancel - Tries to cancel an alarm timer
385 * @alarm: ptr to alarm to be canceled
386 *
387 * Returns 1 if the timer was canceled, 0 if it was not running,
388 * and -1 if the callback was running
389 */
alarm_try_to_cancel(struct alarm * alarm)390 int alarm_try_to_cancel(struct alarm *alarm)
391 {
392 struct alarm_base *base = &alarm_bases[alarm->type];
393 int ret;
394
395 scoped_guard(spinlock_irqsave, &base->lock) {
396 ret = hrtimer_try_to_cancel(&alarm->timer);
397 if (ret >= 0)
398 alarmtimer_dequeue(base, alarm);
399 }
400
401 trace_alarmtimer_cancel(alarm, base->get_ktime());
402 return ret;
403 }
404 EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
405
406
407 /**
408 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
409 * @alarm: ptr to alarm to be canceled
410 *
411 * Returns 1 if the timer was canceled, 0 if it was not active.
412 */
alarm_cancel(struct alarm * alarm)413 int alarm_cancel(struct alarm *alarm)
414 {
415 for (;;) {
416 int ret = alarm_try_to_cancel(alarm);
417 if (ret >= 0)
418 return ret;
419 hrtimer_cancel_wait_running(&alarm->timer);
420 }
421 }
422 EXPORT_SYMBOL_GPL(alarm_cancel);
423
424
alarm_forward(struct alarm * alarm,ktime_t now,ktime_t interval)425 u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
426 {
427 u64 overrun = 1;
428 ktime_t delta;
429
430 delta = ktime_sub(now, alarm->node.expires);
431
432 if (delta < 0)
433 return 0;
434
435 if (unlikely(delta >= interval)) {
436 s64 incr = ktime_to_ns(interval);
437
438 overrun = ktime_divns(delta, incr);
439
440 alarm->node.expires = ktime_add_ns(alarm->node.expires,
441 incr*overrun);
442
443 if (alarm->node.expires > now)
444 return overrun;
445 /*
446 * This (and the ktime_add() below) is the
447 * correction for exact:
448 */
449 overrun++;
450 }
451
452 alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
453 return overrun;
454 }
455 EXPORT_SYMBOL_GPL(alarm_forward);
456
alarm_forward_now(struct alarm * alarm,ktime_t interval)457 u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
458 {
459 struct alarm_base *base = &alarm_bases[alarm->type];
460
461 return alarm_forward(alarm, base->get_ktime(), interval);
462 }
463 EXPORT_SYMBOL_GPL(alarm_forward_now);
464
465 #ifdef CONFIG_POSIX_TIMERS
466
alarmtimer_freezerset(ktime_t absexp,enum alarmtimer_type type)467 static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
468 {
469 struct alarm_base *base;
470 ktime_t delta;
471
472 switch(type) {
473 case ALARM_REALTIME:
474 base = &alarm_bases[ALARM_REALTIME];
475 type = ALARM_REALTIME_FREEZER;
476 break;
477 case ALARM_BOOTTIME:
478 base = &alarm_bases[ALARM_BOOTTIME];
479 type = ALARM_BOOTTIME_FREEZER;
480 break;
481 default:
482 WARN_ONCE(1, "Invalid alarm type: %d\n", type);
483 return;
484 }
485
486 delta = ktime_sub(absexp, base->get_ktime());
487
488 guard(spinlock_irqsave)(&freezer_delta_lock);
489 if (!freezer_delta || (delta < freezer_delta)) {
490 freezer_delta = delta;
491 freezer_expires = absexp;
492 freezer_alarmtype = type;
493 }
494 }
495
496 /**
497 * clock2alarm - helper that converts from clockid to alarmtypes
498 * @clockid: clockid.
499 */
clock2alarm(clockid_t clockid)500 static enum alarmtimer_type clock2alarm(clockid_t clockid)
501 {
502 if (clockid == CLOCK_REALTIME_ALARM)
503 return ALARM_REALTIME;
504
505 WARN_ON_ONCE(clockid != CLOCK_BOOTTIME_ALARM);
506 return ALARM_BOOTTIME;
507 }
508
509 /**
510 * alarm_handle_timer - Callback for posix timers
511 * @alarm: alarm that fired
512 * @now: time at the timer expiration
513 *
514 * Posix timer callback for expired alarm timers.
515 *
516 * Return: whether the timer is to be restarted
517 */
alarm_handle_timer(struct alarm * alarm,ktime_t now)518 static void alarm_handle_timer(struct alarm *alarm, ktime_t now)
519 {
520 struct k_itimer *ptr = container_of(alarm, struct k_itimer, it.alarm.alarmtimer);
521
522 guard(spinlock_irqsave)(&ptr->it_lock);
523 posix_timer_queue_signal(ptr);
524 }
525
526 /**
527 * alarm_timer_rearm - Posix timer callback for rearming timer
528 * @timr: Pointer to the posixtimer data struct
529 */
alarm_timer_rearm(struct k_itimer * timr)530 static void alarm_timer_rearm(struct k_itimer *timr)
531 {
532 struct alarm *alarm = &timr->it.alarm.alarmtimer;
533
534 timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
535 alarm_start(alarm, alarm->node.expires);
536 }
537
538 /**
539 * alarm_timer_forward - Posix timer callback for forwarding timer
540 * @timr: Pointer to the posixtimer data struct
541 * @now: Current time to forward the timer against
542 */
alarm_timer_forward(struct k_itimer * timr,ktime_t now)543 static s64 alarm_timer_forward(struct k_itimer *timr, ktime_t now)
544 {
545 struct alarm *alarm = &timr->it.alarm.alarmtimer;
546
547 return alarm_forward(alarm, now, timr->it_interval);
548 }
549
550 /**
551 * alarm_timer_remaining - Posix timer callback to retrieve remaining time
552 * @timr: Pointer to the posixtimer data struct
553 * @now: Current time to calculate against
554 */
alarm_timer_remaining(struct k_itimer * timr,ktime_t now)555 static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
556 {
557 struct alarm *alarm = &timr->it.alarm.alarmtimer;
558
559 return ktime_sub(alarm->node.expires, now);
560 }
561
562 /**
563 * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
564 * @timr: Pointer to the posixtimer data struct
565 */
alarm_timer_try_to_cancel(struct k_itimer * timr)566 static int alarm_timer_try_to_cancel(struct k_itimer *timr)
567 {
568 return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
569 }
570
571 /**
572 * alarm_timer_wait_running - Posix timer callback to wait for a timer
573 * @timr: Pointer to the posixtimer data struct
574 *
575 * Called from the core code when timer cancel detected that the callback
576 * is running. @timr is unlocked and rcu read lock is held to prevent it
577 * from being freed.
578 */
alarm_timer_wait_running(struct k_itimer * timr)579 static void alarm_timer_wait_running(struct k_itimer *timr)
580 {
581 hrtimer_cancel_wait_running(&timr->it.alarm.alarmtimer.timer);
582 }
583
584 /**
585 * alarm_timer_arm - Posix timer callback to arm a timer
586 * @timr: Pointer to the posixtimer data struct
587 * @expires: The new expiry time
588 * @absolute: Expiry value is absolute time
589 * @sigev_none: Posix timer does not deliver signals
590 */
alarm_timer_arm(struct k_itimer * timr,ktime_t expires,bool absolute,bool sigev_none)591 static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
592 bool absolute, bool sigev_none)
593 {
594 struct alarm *alarm = &timr->it.alarm.alarmtimer;
595 struct alarm_base *base = &alarm_bases[alarm->type];
596
597 if (!absolute)
598 expires = ktime_add_safe(expires, base->get_ktime());
599 if (sigev_none)
600 alarm->node.expires = expires;
601 else
602 alarm_start(&timr->it.alarm.alarmtimer, expires);
603 }
604
605 /**
606 * alarm_clock_getres - posix getres interface
607 * @which_clock: clockid
608 * @tp: timespec to fill
609 *
610 * Returns the granularity of underlying alarm base clock
611 */
alarm_clock_getres(const clockid_t which_clock,struct timespec64 * tp)612 static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
613 {
614 if (!alarmtimer_get_rtcdev())
615 return -EINVAL;
616
617 tp->tv_sec = 0;
618 tp->tv_nsec = hrtimer_resolution;
619 return 0;
620 }
621
622 /**
623 * alarm_clock_get_timespec - posix clock_get_timespec interface
624 * @which_clock: clockid
625 * @tp: timespec to fill.
626 *
627 * Provides the underlying alarm base time in a tasks time namespace.
628 */
alarm_clock_get_timespec(clockid_t which_clock,struct timespec64 * tp)629 static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tp)
630 {
631 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
632
633 if (!alarmtimer_get_rtcdev())
634 return -EINVAL;
635
636 base->get_timespec(tp);
637
638 return 0;
639 }
640
641 /**
642 * alarm_clock_get_ktime - posix clock_get_ktime interface
643 * @which_clock: clockid
644 *
645 * Provides the underlying alarm base time in the root namespace.
646 */
alarm_clock_get_ktime(clockid_t which_clock)647 static ktime_t alarm_clock_get_ktime(clockid_t which_clock)
648 {
649 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
650
651 if (!alarmtimer_get_rtcdev())
652 return -EINVAL;
653
654 return base->get_ktime();
655 }
656
657 /**
658 * alarm_timer_create - posix timer_create interface
659 * @new_timer: k_itimer pointer to manage
660 *
661 * Initializes the k_itimer structure.
662 */
alarm_timer_create(struct k_itimer * new_timer)663 static int alarm_timer_create(struct k_itimer *new_timer)
664 {
665 enum alarmtimer_type type;
666
667 if (!alarmtimer_get_rtcdev())
668 return -EOPNOTSUPP;
669
670 if (!capable(CAP_WAKE_ALARM))
671 return -EPERM;
672
673 type = clock2alarm(new_timer->it_clock);
674 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
675 return 0;
676 }
677
678 /**
679 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
680 * @alarm: ptr to alarm that fired
681 * @now: time at the timer expiration
682 *
683 * Wakes up the task that set the alarmtimer
684 */
alarmtimer_nsleep_wakeup(struct alarm * alarm,ktime_t now)685 static void alarmtimer_nsleep_wakeup(struct alarm *alarm, ktime_t now)
686 {
687 struct task_struct *task = alarm->data;
688
689 alarm->data = NULL;
690 if (task)
691 wake_up_process(task);
692 }
693
694 /**
695 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
696 * @alarm: ptr to alarmtimer
697 * @absexp: absolute expiration time
698 * @type: alarm type (BOOTTIME/REALTIME).
699 *
700 * Sets the alarm timer and sleeps until it is fired or interrupted.
701 */
alarmtimer_do_nsleep(struct alarm * alarm,ktime_t absexp,enum alarmtimer_type type)702 static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
703 enum alarmtimer_type type)
704 {
705 struct restart_block *restart;
706 alarm->data = (void *)current;
707 do {
708 set_current_state(TASK_INTERRUPTIBLE);
709 alarm_start(alarm, absexp);
710 if (likely(alarm->data))
711 schedule();
712
713 alarm_cancel(alarm);
714 } while (alarm->data && !signal_pending(current));
715
716 __set_current_state(TASK_RUNNING);
717
718 destroy_hrtimer_on_stack(&alarm->timer);
719
720 if (!alarm->data)
721 return 0;
722
723 if (freezing(current))
724 alarmtimer_freezerset(absexp, type);
725 restart = ¤t->restart_block;
726 if (restart->nanosleep.type != TT_NONE) {
727 struct timespec64 rmt;
728 ktime_t rem;
729
730 rem = ktime_sub(absexp, alarm_bases[type].get_ktime());
731
732 if (rem <= 0)
733 return 0;
734 rmt = ktime_to_timespec64(rem);
735
736 return nanosleep_copyout(restart, &rmt);
737 }
738 return -ERESTART_RESTARTBLOCK;
739 }
740
741 static void
alarm_init_on_stack(struct alarm * alarm,enum alarmtimer_type type,void (* function)(struct alarm *,ktime_t))742 alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type,
743 void (*function)(struct alarm *, ktime_t))
744 {
745 hrtimer_setup_on_stack(&alarm->timer, alarmtimer_fired, alarm_bases[type].base_clockid,
746 HRTIMER_MODE_ABS);
747 __alarm_init(alarm, type, function);
748 }
749
750 /**
751 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
752 * @restart: ptr to restart block
753 *
754 * Handles restarted clock_nanosleep calls
755 */
alarm_timer_nsleep_restart(struct restart_block * restart)756 static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
757 {
758 enum alarmtimer_type type = restart->nanosleep.clockid;
759 ktime_t exp = restart->nanosleep.expires;
760 struct alarm alarm;
761
762 alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
763
764 return alarmtimer_do_nsleep(&alarm, exp, type);
765 }
766
767 /**
768 * alarm_timer_nsleep - alarmtimer nanosleep
769 * @which_clock: clockid
770 * @flags: determines abstime or relative
771 * @tsreq: requested sleep time (abs or rel)
772 *
773 * Handles clock_nanosleep calls against _ALARM clockids
774 */
alarm_timer_nsleep(const clockid_t which_clock,int flags,const struct timespec64 * tsreq)775 static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
776 const struct timespec64 *tsreq)
777 {
778 enum alarmtimer_type type = clock2alarm(which_clock);
779 struct restart_block *restart = ¤t->restart_block;
780 struct alarm alarm;
781 ktime_t exp;
782 int ret;
783
784 if (!alarmtimer_get_rtcdev())
785 return -EOPNOTSUPP;
786
787 if (flags & ~TIMER_ABSTIME)
788 return -EINVAL;
789
790 if (!capable(CAP_WAKE_ALARM))
791 return -EPERM;
792
793 alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
794
795 exp = timespec64_to_ktime(*tsreq);
796 /* Convert (if necessary) to absolute time */
797 if (flags != TIMER_ABSTIME) {
798 ktime_t now = alarm_bases[type].get_ktime();
799
800 exp = ktime_add_safe(now, exp);
801 } else {
802 exp = timens_ktime_to_host(which_clock, exp);
803 }
804
805 ret = alarmtimer_do_nsleep(&alarm, exp, type);
806 if (ret != -ERESTART_RESTARTBLOCK)
807 return ret;
808
809 /* abs timers don't set remaining time or restart */
810 if (flags == TIMER_ABSTIME)
811 return -ERESTARTNOHAND;
812
813 restart->nanosleep.clockid = type;
814 restart->nanosleep.expires = exp;
815 set_restart_fn(restart, alarm_timer_nsleep_restart);
816 return ret;
817 }
818
819 const struct k_clock alarm_clock = {
820 .clock_getres = alarm_clock_getres,
821 .clock_get_ktime = alarm_clock_get_ktime,
822 .clock_get_timespec = alarm_clock_get_timespec,
823 .timer_create = alarm_timer_create,
824 .timer_set = common_timer_set,
825 .timer_del = common_timer_del,
826 .timer_get = common_timer_get,
827 .timer_arm = alarm_timer_arm,
828 .timer_rearm = alarm_timer_rearm,
829 .timer_forward = alarm_timer_forward,
830 .timer_remaining = alarm_timer_remaining,
831 .timer_try_to_cancel = alarm_timer_try_to_cancel,
832 .timer_wait_running = alarm_timer_wait_running,
833 .nsleep = alarm_timer_nsleep,
834 };
835 #endif /* CONFIG_POSIX_TIMERS */
836
837
838 /* Suspend hook structures */
839 static const struct dev_pm_ops alarmtimer_pm_ops = {
840 .suspend = alarmtimer_suspend,
841 .resume = alarmtimer_resume,
842 };
843
844 static struct platform_driver alarmtimer_driver = {
845 .driver = {
846 .name = "alarmtimer",
847 .pm = &alarmtimer_pm_ops,
848 }
849 };
850
get_boottime_timespec(struct timespec64 * tp)851 static void get_boottime_timespec(struct timespec64 *tp)
852 {
853 ktime_get_boottime_ts64(tp);
854 timens_add_boottime(tp);
855 }
856
857 /**
858 * alarmtimer_init - Initialize alarm timer code
859 *
860 * This function initializes the alarm bases and registers
861 * the posix clock ids.
862 */
alarmtimer_init(void)863 static int __init alarmtimer_init(void)
864 {
865 int error;
866 int i;
867
868 alarmtimer_rtc_timer_init();
869
870 /* Initialize alarm bases */
871 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
872 alarm_bases[ALARM_REALTIME].get_ktime = &ktime_get_real;
873 alarm_bases[ALARM_REALTIME].get_timespec = ktime_get_real_ts64;
874 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
875 alarm_bases[ALARM_BOOTTIME].get_ktime = &ktime_get_boottime;
876 alarm_bases[ALARM_BOOTTIME].get_timespec = get_boottime_timespec;
877 for (i = 0; i < ALARM_NUMTYPE; i++) {
878 timerqueue_init_head(&alarm_bases[i].timerqueue);
879 spin_lock_init(&alarm_bases[i].lock);
880 }
881
882 error = alarmtimer_rtc_interface_setup();
883 if (error)
884 return error;
885
886 error = platform_driver_register(&alarmtimer_driver);
887 if (error)
888 goto out_if;
889
890 return 0;
891 out_if:
892 alarmtimer_rtc_interface_remove();
893 return error;
894 }
895 device_initcall(alarmtimer_init);
896