xref: /openbmc/linux/kernel/time/alarmtimer.c (revision d2574c33)
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("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  * @state: unused
237  *
238  * When we are going into suspend, we look through the bases
239  * to see which is the soonest timer to expire. We then
240  * set an rtc timer to fire that far into the future, which
241  * will wake us from suspend.
242  */
243 static int alarmtimer_suspend(struct device *dev)
244 {
245 	ktime_t min, now, expires;
246 	int i, ret, type;
247 	struct rtc_device *rtc;
248 	unsigned long flags;
249 	struct rtc_time tm;
250 
251 	spin_lock_irqsave(&freezer_delta_lock, flags);
252 	min = freezer_delta;
253 	expires = freezer_expires;
254 	type = freezer_alarmtype;
255 	freezer_delta = 0;
256 	spin_unlock_irqrestore(&freezer_delta_lock, flags);
257 
258 	rtc = alarmtimer_get_rtcdev();
259 	/* If we have no rtcdev, just return */
260 	if (!rtc)
261 		return 0;
262 
263 	/* Find the soonest timer to expire*/
264 	for (i = 0; i < ALARM_NUMTYPE; i++) {
265 		struct alarm_base *base = &alarm_bases[i];
266 		struct timerqueue_node *next;
267 		ktime_t delta;
268 
269 		spin_lock_irqsave(&base->lock, flags);
270 		next = timerqueue_getnext(&base->timerqueue);
271 		spin_unlock_irqrestore(&base->lock, flags);
272 		if (!next)
273 			continue;
274 		delta = ktime_sub(next->expires, base->gettime());
275 		if (!min || (delta < min)) {
276 			expires = next->expires;
277 			min = delta;
278 			type = i;
279 		}
280 	}
281 	if (min == 0)
282 		return 0;
283 
284 	if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
285 		__pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
286 		return -EBUSY;
287 	}
288 
289 	trace_alarmtimer_suspend(expires, type);
290 
291 	/* Setup an rtc timer to fire that far in the future */
292 	rtc_timer_cancel(rtc, &rtctimer);
293 	rtc_read_time(rtc, &tm);
294 	now = rtc_tm_to_ktime(tm);
295 	now = ktime_add(now, min);
296 
297 	/* Set alarm, if in the past reject suspend briefly to handle */
298 	ret = rtc_timer_start(rtc, &rtctimer, now, 0);
299 	if (ret < 0)
300 		__pm_wakeup_event(ws, MSEC_PER_SEC);
301 	return ret;
302 }
303 
304 static int alarmtimer_resume(struct device *dev)
305 {
306 	struct rtc_device *rtc;
307 
308 	rtc = alarmtimer_get_rtcdev();
309 	if (rtc)
310 		rtc_timer_cancel(rtc, &rtctimer);
311 	return 0;
312 }
313 
314 #else
315 static int alarmtimer_suspend(struct device *dev)
316 {
317 	return 0;
318 }
319 
320 static int alarmtimer_resume(struct device *dev)
321 {
322 	return 0;
323 }
324 #endif
325 
326 static void
327 __alarm_init(struct alarm *alarm, enum alarmtimer_type type,
328 	     enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
329 {
330 	timerqueue_init(&alarm->node);
331 	alarm->timer.function = alarmtimer_fired;
332 	alarm->function = function;
333 	alarm->type = type;
334 	alarm->state = ALARMTIMER_STATE_INACTIVE;
335 }
336 
337 /**
338  * alarm_init - Initialize an alarm structure
339  * @alarm: ptr to alarm to be initialized
340  * @type: the type of the alarm
341  * @function: callback that is run when the alarm fires
342  */
343 void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
344 		enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
345 {
346 	hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
347 		     HRTIMER_MODE_ABS);
348 	__alarm_init(alarm, type, function);
349 }
350 EXPORT_SYMBOL_GPL(alarm_init);
351 
352 /**
353  * alarm_start - Sets an absolute alarm to fire
354  * @alarm: ptr to alarm to set
355  * @start: time to run the alarm
356  */
357 void alarm_start(struct alarm *alarm, ktime_t start)
358 {
359 	struct alarm_base *base = &alarm_bases[alarm->type];
360 	unsigned long flags;
361 
362 	spin_lock_irqsave(&base->lock, flags);
363 	alarm->node.expires = start;
364 	alarmtimer_enqueue(base, alarm);
365 	hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
366 	spin_unlock_irqrestore(&base->lock, flags);
367 
368 	trace_alarmtimer_start(alarm, base->gettime());
369 }
370 EXPORT_SYMBOL_GPL(alarm_start);
371 
372 /**
373  * alarm_start_relative - Sets a relative alarm to fire
374  * @alarm: ptr to alarm to set
375  * @start: time relative to now to run the alarm
376  */
377 void alarm_start_relative(struct alarm *alarm, ktime_t start)
378 {
379 	struct alarm_base *base = &alarm_bases[alarm->type];
380 
381 	start = ktime_add_safe(start, base->gettime());
382 	alarm_start(alarm, start);
383 }
384 EXPORT_SYMBOL_GPL(alarm_start_relative);
385 
386 void alarm_restart(struct alarm *alarm)
387 {
388 	struct alarm_base *base = &alarm_bases[alarm->type];
389 	unsigned long flags;
390 
391 	spin_lock_irqsave(&base->lock, flags);
392 	hrtimer_set_expires(&alarm->timer, alarm->node.expires);
393 	hrtimer_restart(&alarm->timer);
394 	alarmtimer_enqueue(base, alarm);
395 	spin_unlock_irqrestore(&base->lock, flags);
396 }
397 EXPORT_SYMBOL_GPL(alarm_restart);
398 
399 /**
400  * alarm_try_to_cancel - Tries to cancel an alarm timer
401  * @alarm: ptr to alarm to be canceled
402  *
403  * Returns 1 if the timer was canceled, 0 if it was not running,
404  * and -1 if the callback was running
405  */
406 int alarm_try_to_cancel(struct alarm *alarm)
407 {
408 	struct alarm_base *base = &alarm_bases[alarm->type];
409 	unsigned long flags;
410 	int ret;
411 
412 	spin_lock_irqsave(&base->lock, flags);
413 	ret = hrtimer_try_to_cancel(&alarm->timer);
414 	if (ret >= 0)
415 		alarmtimer_dequeue(base, alarm);
416 	spin_unlock_irqrestore(&base->lock, flags);
417 
418 	trace_alarmtimer_cancel(alarm, base->gettime());
419 	return ret;
420 }
421 EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
422 
423 
424 /**
425  * alarm_cancel - Spins trying to cancel an alarm timer until it is done
426  * @alarm: ptr to alarm to be canceled
427  *
428  * Returns 1 if the timer was canceled, 0 if it was not active.
429  */
430 int alarm_cancel(struct alarm *alarm)
431 {
432 	for (;;) {
433 		int ret = alarm_try_to_cancel(alarm);
434 		if (ret >= 0)
435 			return ret;
436 		cpu_relax();
437 	}
438 }
439 EXPORT_SYMBOL_GPL(alarm_cancel);
440 
441 
442 u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
443 {
444 	u64 overrun = 1;
445 	ktime_t delta;
446 
447 	delta = ktime_sub(now, alarm->node.expires);
448 
449 	if (delta < 0)
450 		return 0;
451 
452 	if (unlikely(delta >= interval)) {
453 		s64 incr = ktime_to_ns(interval);
454 
455 		overrun = ktime_divns(delta, incr);
456 
457 		alarm->node.expires = ktime_add_ns(alarm->node.expires,
458 							incr*overrun);
459 
460 		if (alarm->node.expires > now)
461 			return overrun;
462 		/*
463 		 * This (and the ktime_add() below) is the
464 		 * correction for exact:
465 		 */
466 		overrun++;
467 	}
468 
469 	alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
470 	return overrun;
471 }
472 EXPORT_SYMBOL_GPL(alarm_forward);
473 
474 u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
475 {
476 	struct alarm_base *base = &alarm_bases[alarm->type];
477 
478 	return alarm_forward(alarm, base->gettime(), interval);
479 }
480 EXPORT_SYMBOL_GPL(alarm_forward_now);
481 
482 #ifdef CONFIG_POSIX_TIMERS
483 
484 static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
485 {
486 	struct alarm_base *base;
487 	unsigned long flags;
488 	ktime_t delta;
489 
490 	switch(type) {
491 	case ALARM_REALTIME:
492 		base = &alarm_bases[ALARM_REALTIME];
493 		type = ALARM_REALTIME_FREEZER;
494 		break;
495 	case ALARM_BOOTTIME:
496 		base = &alarm_bases[ALARM_BOOTTIME];
497 		type = ALARM_BOOTTIME_FREEZER;
498 		break;
499 	default:
500 		WARN_ONCE(1, "Invalid alarm type: %d\n", type);
501 		return;
502 	}
503 
504 	delta = ktime_sub(absexp, base->gettime());
505 
506 	spin_lock_irqsave(&freezer_delta_lock, flags);
507 	if (!freezer_delta || (delta < freezer_delta)) {
508 		freezer_delta = delta;
509 		freezer_expires = absexp;
510 		freezer_alarmtype = type;
511 	}
512 	spin_unlock_irqrestore(&freezer_delta_lock, flags);
513 }
514 
515 /**
516  * clock2alarm - helper that converts from clockid to alarmtypes
517  * @clockid: clockid.
518  */
519 static enum alarmtimer_type clock2alarm(clockid_t clockid)
520 {
521 	if (clockid == CLOCK_REALTIME_ALARM)
522 		return ALARM_REALTIME;
523 	if (clockid == CLOCK_BOOTTIME_ALARM)
524 		return ALARM_BOOTTIME;
525 	return -1;
526 }
527 
528 /**
529  * alarm_handle_timer - Callback for posix timers
530  * @alarm: alarm that fired
531  *
532  * Posix timer callback for expired alarm timers.
533  */
534 static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
535 							ktime_t now)
536 {
537 	struct k_itimer *ptr = container_of(alarm, struct k_itimer,
538 					    it.alarm.alarmtimer);
539 	enum alarmtimer_restart result = ALARMTIMER_NORESTART;
540 	unsigned long flags;
541 	int si_private = 0;
542 
543 	spin_lock_irqsave(&ptr->it_lock, flags);
544 
545 	ptr->it_active = 0;
546 	if (ptr->it_interval)
547 		si_private = ++ptr->it_requeue_pending;
548 
549 	if (posix_timer_event(ptr, si_private) && ptr->it_interval) {
550 		/*
551 		 * Handle ignored signals and rearm the timer. This will go
552 		 * away once we handle ignored signals proper.
553 		 */
554 		ptr->it_overrun += alarm_forward_now(alarm, ptr->it_interval);
555 		++ptr->it_requeue_pending;
556 		ptr->it_active = 1;
557 		result = ALARMTIMER_RESTART;
558 	}
559 	spin_unlock_irqrestore(&ptr->it_lock, flags);
560 
561 	return result;
562 }
563 
564 /**
565  * alarm_timer_rearm - Posix timer callback for rearming timer
566  * @timr:	Pointer to the posixtimer data struct
567  */
568 static void alarm_timer_rearm(struct k_itimer *timr)
569 {
570 	struct alarm *alarm = &timr->it.alarm.alarmtimer;
571 
572 	timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
573 	alarm_start(alarm, alarm->node.expires);
574 }
575 
576 /**
577  * alarm_timer_forward - Posix timer callback for forwarding timer
578  * @timr:	Pointer to the posixtimer data struct
579  * @now:	Current time to forward the timer against
580  */
581 static s64 alarm_timer_forward(struct k_itimer *timr, ktime_t now)
582 {
583 	struct alarm *alarm = &timr->it.alarm.alarmtimer;
584 
585 	return alarm_forward(alarm, timr->it_interval, now);
586 }
587 
588 /**
589  * alarm_timer_remaining - Posix timer callback to retrieve remaining time
590  * @timr:	Pointer to the posixtimer data struct
591  * @now:	Current time to calculate against
592  */
593 static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
594 {
595 	struct alarm *alarm = &timr->it.alarm.alarmtimer;
596 
597 	return ktime_sub(now, alarm->node.expires);
598 }
599 
600 /**
601  * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
602  * @timr:	Pointer to the posixtimer data struct
603  */
604 static int alarm_timer_try_to_cancel(struct k_itimer *timr)
605 {
606 	return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
607 }
608 
609 /**
610  * alarm_timer_arm - Posix timer callback to arm a timer
611  * @timr:	Pointer to the posixtimer data struct
612  * @expires:	The new expiry time
613  * @absolute:	Expiry value is absolute time
614  * @sigev_none:	Posix timer does not deliver signals
615  */
616 static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
617 			    bool absolute, bool sigev_none)
618 {
619 	struct alarm *alarm = &timr->it.alarm.alarmtimer;
620 	struct alarm_base *base = &alarm_bases[alarm->type];
621 
622 	if (!absolute)
623 		expires = ktime_add_safe(expires, base->gettime());
624 	if (sigev_none)
625 		alarm->node.expires = expires;
626 	else
627 		alarm_start(&timr->it.alarm.alarmtimer, expires);
628 }
629 
630 /**
631  * alarm_clock_getres - posix getres interface
632  * @which_clock: clockid
633  * @tp: timespec to fill
634  *
635  * Returns the granularity of underlying alarm base clock
636  */
637 static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
638 {
639 	if (!alarmtimer_get_rtcdev())
640 		return -EINVAL;
641 
642 	tp->tv_sec = 0;
643 	tp->tv_nsec = hrtimer_resolution;
644 	return 0;
645 }
646 
647 /**
648  * alarm_clock_get - posix clock_get interface
649  * @which_clock: clockid
650  * @tp: timespec to fill.
651  *
652  * Provides the underlying alarm base time.
653  */
654 static int alarm_clock_get(clockid_t which_clock, struct timespec64 *tp)
655 {
656 	struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
657 
658 	if (!alarmtimer_get_rtcdev())
659 		return -EINVAL;
660 
661 	*tp = ktime_to_timespec64(base->gettime());
662 	return 0;
663 }
664 
665 /**
666  * alarm_timer_create - posix timer_create interface
667  * @new_timer: k_itimer pointer to manage
668  *
669  * Initializes the k_itimer structure.
670  */
671 static int alarm_timer_create(struct k_itimer *new_timer)
672 {
673 	enum  alarmtimer_type type;
674 
675 	if (!alarmtimer_get_rtcdev())
676 		return -ENOTSUPP;
677 
678 	if (!capable(CAP_WAKE_ALARM))
679 		return -EPERM;
680 
681 	type = clock2alarm(new_timer->it_clock);
682 	alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
683 	return 0;
684 }
685 
686 /**
687  * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
688  * @alarm: ptr to alarm that fired
689  *
690  * Wakes up the task that set the alarmtimer
691  */
692 static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
693 								ktime_t now)
694 {
695 	struct task_struct *task = (struct task_struct *)alarm->data;
696 
697 	alarm->data = NULL;
698 	if (task)
699 		wake_up_process(task);
700 	return ALARMTIMER_NORESTART;
701 }
702 
703 /**
704  * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
705  * @alarm: ptr to alarmtimer
706  * @absexp: absolute expiration time
707  *
708  * Sets the alarm timer and sleeps until it is fired or interrupted.
709  */
710 static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
711 				enum alarmtimer_type type)
712 {
713 	struct restart_block *restart;
714 	alarm->data = (void *)current;
715 	do {
716 		set_current_state(TASK_INTERRUPTIBLE);
717 		alarm_start(alarm, absexp);
718 		if (likely(alarm->data))
719 			schedule();
720 
721 		alarm_cancel(alarm);
722 	} while (alarm->data && !signal_pending(current));
723 
724 	__set_current_state(TASK_RUNNING);
725 
726 	destroy_hrtimer_on_stack(&alarm->timer);
727 
728 	if (!alarm->data)
729 		return 0;
730 
731 	if (freezing(current))
732 		alarmtimer_freezerset(absexp, type);
733 	restart = &current->restart_block;
734 	if (restart->nanosleep.type != TT_NONE) {
735 		struct timespec64 rmt;
736 		ktime_t rem;
737 
738 		rem = ktime_sub(absexp, alarm_bases[type].gettime());
739 
740 		if (rem <= 0)
741 			return 0;
742 		rmt = ktime_to_timespec64(rem);
743 
744 		return nanosleep_copyout(restart, &rmt);
745 	}
746 	return -ERESTART_RESTARTBLOCK;
747 }
748 
749 static void
750 alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type,
751 		    enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
752 {
753 	hrtimer_init_on_stack(&alarm->timer, alarm_bases[type].base_clockid,
754 			      HRTIMER_MODE_ABS);
755 	__alarm_init(alarm, type, function);
756 }
757 
758 /**
759  * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
760  * @restart: ptr to restart block
761  *
762  * Handles restarted clock_nanosleep calls
763  */
764 static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
765 {
766 	enum  alarmtimer_type type = restart->nanosleep.clockid;
767 	ktime_t exp = restart->nanosleep.expires;
768 	struct alarm alarm;
769 
770 	alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
771 
772 	return alarmtimer_do_nsleep(&alarm, exp, type);
773 }
774 
775 /**
776  * alarm_timer_nsleep - alarmtimer nanosleep
777  * @which_clock: clockid
778  * @flags: determins abstime or relative
779  * @tsreq: requested sleep time (abs or rel)
780  * @rmtp: remaining sleep time saved
781  *
782  * Handles clock_nanosleep calls against _ALARM clockids
783  */
784 static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
785 			      const struct timespec64 *tsreq)
786 {
787 	enum  alarmtimer_type type = clock2alarm(which_clock);
788 	struct restart_block *restart = &current->restart_block;
789 	struct alarm alarm;
790 	ktime_t exp;
791 	int ret = 0;
792 
793 	if (!alarmtimer_get_rtcdev())
794 		return -ENOTSUPP;
795 
796 	if (flags & ~TIMER_ABSTIME)
797 		return -EINVAL;
798 
799 	if (!capable(CAP_WAKE_ALARM))
800 		return -EPERM;
801 
802 	alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
803 
804 	exp = timespec64_to_ktime(*tsreq);
805 	/* Convert (if necessary) to absolute time */
806 	if (flags != TIMER_ABSTIME) {
807 		ktime_t now = alarm_bases[type].gettime();
808 
809 		exp = ktime_add_safe(now, exp);
810 	}
811 
812 	ret = alarmtimer_do_nsleep(&alarm, exp, type);
813 	if (ret != -ERESTART_RESTARTBLOCK)
814 		return ret;
815 
816 	/* abs timers don't set remaining time or restart */
817 	if (flags == TIMER_ABSTIME)
818 		return -ERESTARTNOHAND;
819 
820 	restart->fn = alarm_timer_nsleep_restart;
821 	restart->nanosleep.clockid = type;
822 	restart->nanosleep.expires = exp;
823 	return ret;
824 }
825 
826 const struct k_clock alarm_clock = {
827 	.clock_getres		= alarm_clock_getres,
828 	.clock_get		= alarm_clock_get,
829 	.timer_create		= alarm_timer_create,
830 	.timer_set		= common_timer_set,
831 	.timer_del		= common_timer_del,
832 	.timer_get		= common_timer_get,
833 	.timer_arm		= alarm_timer_arm,
834 	.timer_rearm		= alarm_timer_rearm,
835 	.timer_forward		= alarm_timer_forward,
836 	.timer_remaining	= alarm_timer_remaining,
837 	.timer_try_to_cancel	= alarm_timer_try_to_cancel,
838 	.nsleep			= alarm_timer_nsleep,
839 };
840 #endif /* CONFIG_POSIX_TIMERS */
841 
842 
843 /* Suspend hook structures */
844 static const struct dev_pm_ops alarmtimer_pm_ops = {
845 	.suspend = alarmtimer_suspend,
846 	.resume = alarmtimer_resume,
847 };
848 
849 static struct platform_driver alarmtimer_driver = {
850 	.driver = {
851 		.name = "alarmtimer",
852 		.pm = &alarmtimer_pm_ops,
853 	}
854 };
855 
856 /**
857  * alarmtimer_init - Initialize alarm timer code
858  *
859  * This function initializes the alarm bases and registers
860  * the posix clock ids.
861  */
862 static int __init alarmtimer_init(void)
863 {
864 	struct platform_device *pdev;
865 	int error = 0;
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].gettime = &ktime_get_real;
873 	alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
874 	alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
875 	for (i = 0; i < ALARM_NUMTYPE; i++) {
876 		timerqueue_init_head(&alarm_bases[i].timerqueue);
877 		spin_lock_init(&alarm_bases[i].lock);
878 	}
879 
880 	error = alarmtimer_rtc_interface_setup();
881 	if (error)
882 		return error;
883 
884 	error = platform_driver_register(&alarmtimer_driver);
885 	if (error)
886 		goto out_if;
887 
888 	pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
889 	if (IS_ERR(pdev)) {
890 		error = PTR_ERR(pdev);
891 		goto out_drv;
892 	}
893 	return 0;
894 
895 out_drv:
896 	platform_driver_unregister(&alarmtimer_driver);
897 out_if:
898 	alarmtimer_rtc_interface_remove();
899 	return error;
900 }
901 device_initcall(alarmtimer_init);
902