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