blob: 357be1fe6e1f74a8d0a15ac09a209decd43d69b6 [file] [log] [blame]
Thomas Gleixner35728b82018-10-31 19:21:09 +01001// SPDX-License-Identifier: GPL-2.0
John Stultzff3ead92011-01-11 09:42:13 -08002/*
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>
John Stultzff3ead92011-01-11 09:42:13 -080014 */
15#include <linux/time.h>
16#include <linux/hrtimer.h>
17#include <linux/timerqueue.h>
18#include <linux/rtc.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010019#include <linux/sched/signal.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010020#include <linux/sched/debug.h>
John Stultzff3ead92011-01-11 09:42:13 -080021#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>
Al Viroedbeda42017-06-07 09:42:31 +010027#include <linux/compat.h>
Alexandre Belloni51218292017-08-21 00:01:46 +020028#include <linux/module.h>
John Stultzff3ead92011-01-11 09:42:13 -080029
Thomas Gleixnerbab0aae2017-05-30 23:15:41 +020030#include "posix-timers.h"
31
Baolin Wang4a057542016-11-28 14:35:21 -080032#define CREATE_TRACE_POINTS
33#include <trace/events/alarmtimer.h>
34
John Stultz180bf812011-04-28 12:58:11 -070035/**
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
Andrei Vagin41b3b8d2019-11-12 01:26:56 +000039 * @get_ktime: Function to read the time correlating to the base
Andrei Vagin2f58bf92019-11-12 01:26:57 +000040 * @get_timespec: Function to read the namespace time correlating to the base
John Stultz180bf812011-04-28 12:58:11 -070041 * @base_clockid: clockid for the base
John Stultz180bf812011-04-28 12:58:11 -070042 */
John Stultzff3ead92011-01-11 09:42:13 -080043static struct alarm_base {
44 spinlock_t lock;
45 struct timerqueue_head timerqueue;
Andrei Vagin41b3b8d2019-11-12 01:26:56 +000046 ktime_t (*get_ktime)(void);
Andrei Vagin2f58bf92019-11-12 01:26:57 +000047 void (*get_timespec)(struct timespec64 *tp);
John Stultzff3ead92011-01-11 09:42:13 -080048 clockid_t base_clockid;
John Stultzff3ead92011-01-11 09:42:13 -080049} alarm_bases[ALARM_NUMTYPE];
50
Thomas Gleixnerb6b3b802017-05-27 12:23:47 +020051#if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
Baolin Wang4a057542016-11-28 14:35:21 -080052/* freezer information to handle clock_nanosleep triggered wakeups */
53static enum alarmtimer_type freezer_alarmtype;
54static ktime_t freezer_expires;
John Stultzc008ba582011-06-16 18:27:09 -070055static ktime_t freezer_delta;
56static DEFINE_SPINLOCK(freezer_delta_lock);
Thomas Gleixnerb6b3b802017-05-27 12:23:47 +020057#endif
John Stultzc008ba582011-06-16 18:27:09 -070058
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +020059#ifdef CONFIG_RTC_CLASS
Todd Poynor59a93c22012-08-09 00:37:27 -070060static struct wakeup_source *ws;
61
John Stultz180bf812011-04-28 12:58:11 -070062/* rtc timer and device for setting alarm wakeups at suspend */
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +010063static struct rtc_timer rtctimer;
John Stultzff3ead92011-01-11 09:42:13 -080064static struct rtc_device *rtcdev;
John Stultzc008ba582011-06-16 18:27:09 -070065static DEFINE_SPINLOCK(rtcdev_lock);
John Stultzff3ead92011-01-11 09:42:13 -080066
John Stultzc008ba582011-06-16 18:27:09 -070067/**
John Stultzc008ba582011-06-16 18:27:09 -070068 * alarmtimer_get_rtcdev - Return selected rtcdevice
69 *
70 * This function returns the rtc device to use for wakealarms.
71 * If one has not already been chosen, it checks to see if a
72 * functional rtc device is available.
73 */
John Stultz57c498f2012-04-20 12:31:45 -070074struct rtc_device *alarmtimer_get_rtcdev(void)
John Stultzc008ba582011-06-16 18:27:09 -070075{
John Stultzc008ba582011-06-16 18:27:09 -070076 unsigned long flags;
77 struct rtc_device *ret;
78
79 spin_lock_irqsave(&rtcdev_lock, flags);
John Stultzc008ba582011-06-16 18:27:09 -070080 ret = rtcdev;
81 spin_unlock_irqrestore(&rtcdev_lock, flags);
82
83 return ret;
84}
Pramod Gurav71d5d2b2014-06-13 11:49:42 +053085EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
John Stultz8bc0daf2011-07-14 18:35:13 -070086
87static int alarmtimer_rtc_add_device(struct device *dev,
88 struct class_interface *class_intf)
89{
90 unsigned long flags;
91 struct rtc_device *rtc = to_rtc_device(dev);
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +020092 struct wakeup_source *__ws;
John Stultz8bc0daf2011-07-14 18:35:13 -070093
94 if (rtcdev)
95 return -EBUSY;
96
97 if (!rtc->ops->set_alarm)
98 return -1;
99 if (!device_may_wakeup(rtc->dev.parent))
100 return -1;
101
Tri Voc8377ad2019-08-06 18:48:46 -0700102 __ws = wakeup_source_register(dev, "alarmtimer");
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +0200103
John Stultz8bc0daf2011-07-14 18:35:13 -0700104 spin_lock_irqsave(&rtcdev_lock, flags);
105 if (!rtcdev) {
Alexandre Belloni51218292017-08-21 00:01:46 +0200106 if (!try_module_get(rtc->owner)) {
107 spin_unlock_irqrestore(&rtcdev_lock, flags);
108 return -1;
109 }
110
John Stultz8bc0daf2011-07-14 18:35:13 -0700111 rtcdev = rtc;
112 /* hold a reference so it doesn't go away */
113 get_device(dev);
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +0200114 ws = __ws;
115 __ws = NULL;
John Stultz8bc0daf2011-07-14 18:35:13 -0700116 }
117 spin_unlock_irqrestore(&rtcdev_lock, flags);
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +0200118
119 wakeup_source_unregister(__ws);
120
John Stultz8bc0daf2011-07-14 18:35:13 -0700121 return 0;
122}
123
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100124static inline void alarmtimer_rtc_timer_init(void)
125{
126 rtc_timer_init(&rtctimer, NULL, NULL);
127}
128
John Stultz8bc0daf2011-07-14 18:35:13 -0700129static struct class_interface alarmtimer_rtc_interface = {
130 .add_dev = &alarmtimer_rtc_add_device,
131};
132
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200133static int alarmtimer_rtc_interface_setup(void)
John Stultz8bc0daf2011-07-14 18:35:13 -0700134{
135 alarmtimer_rtc_interface.class = rtc_class;
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200136 return class_interface_register(&alarmtimer_rtc_interface);
137}
138static void alarmtimer_rtc_interface_remove(void)
139{
140 class_interface_unregister(&alarmtimer_rtc_interface);
John Stultz8bc0daf2011-07-14 18:35:13 -0700141}
John Stultz1c6b39a2011-06-16 18:47:37 -0700142#else
John Stultz57c498f2012-04-20 12:31:45 -0700143struct rtc_device *alarmtimer_get_rtcdev(void)
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200144{
145 return NULL;
146}
147#define rtcdev (NULL)
148static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
149static inline void alarmtimer_rtc_interface_remove(void) { }
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100150static inline void alarmtimer_rtc_timer_init(void) { }
John Stultzc008ba582011-06-16 18:27:09 -0700151#endif
John Stultzff3ead92011-01-11 09:42:13 -0800152
John Stultz180bf812011-04-28 12:58:11 -0700153/**
John Stultzff3ead92011-01-11 09:42:13 -0800154 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
155 * @base: pointer to the base where the timer is being run
156 * @alarm: pointer to alarm being enqueued.
157 *
John Stultzdae373b2012-09-13 19:12:16 -0400158 * Adds alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800159 *
160 * Must hold base->lock when calling.
161 */
162static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
163{
John Stultzdae373b2012-09-13 19:12:16 -0400164 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
165 timerqueue_del(&base->timerqueue, &alarm->node);
166
John Stultzff3ead92011-01-11 09:42:13 -0800167 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700168 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800169}
170
John Stultz180bf812011-04-28 12:58:11 -0700171/**
John Stultza65bcc12012-09-13 19:25:22 -0400172 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800173 * @base: pointer to the base where the timer is running
174 * @alarm: pointer to alarm being removed
175 *
John Stultzdae373b2012-09-13 19:12:16 -0400176 * Removes alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800177 *
178 * Must hold base->lock when calling.
179 */
John Stultza65bcc12012-09-13 19:25:22 -0400180static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800181{
John Stultza28cde82011-08-10 12:30:21 -0700182 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
183 return;
184
John Stultzff3ead92011-01-11 09:42:13 -0800185 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700186 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800187}
188
John Stultz7068b7a2011-04-28 13:29:18 -0700189
John Stultz180bf812011-04-28 12:58:11 -0700190/**
John Stultz7068b7a2011-04-28 13:29:18 -0700191 * alarmtimer_fired - Handles alarm hrtimer being fired.
192 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800193 *
John Stultz180bf812011-04-28 12:58:11 -0700194 * When a alarm timer fires, this runs through the timerqueue to
195 * see which alarms expired, and runs those. If there are more alarm
196 * timers queued for the future, we set the hrtimer to fire when
197 * when the next future alarm timer expires.
John Stultzff3ead92011-01-11 09:42:13 -0800198 */
John Stultz7068b7a2011-04-28 13:29:18 -0700199static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800200{
John Stultzdae373b2012-09-13 19:12:16 -0400201 struct alarm *alarm = container_of(timer, struct alarm, timer);
202 struct alarm_base *base = &alarm_bases[alarm->type];
John Stultzff3ead92011-01-11 09:42:13 -0800203 unsigned long flags;
John Stultz7068b7a2011-04-28 13:29:18 -0700204 int ret = HRTIMER_NORESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700205 int restart = ALARMTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800206
207 spin_lock_irqsave(&base->lock, flags);
John Stultza65bcc12012-09-13 19:25:22 -0400208 alarmtimer_dequeue(base, alarm);
John Stultzdae373b2012-09-13 19:12:16 -0400209 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800210
John Stultzdae373b2012-09-13 19:12:16 -0400211 if (alarm->function)
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000212 restart = alarm->function(alarm, base->get_ktime());
John Stultzff3ead92011-01-11 09:42:13 -0800213
John Stultzdae373b2012-09-13 19:12:16 -0400214 spin_lock_irqsave(&base->lock, flags);
215 if (restart != ALARMTIMER_NORESTART) {
216 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
217 alarmtimer_enqueue(base, alarm);
John Stultz7068b7a2011-04-28 13:29:18 -0700218 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800219 }
220 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800221
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000222 trace_alarmtimer_fired(alarm, base->get_ktime());
John Stultz7068b7a2011-04-28 13:29:18 -0700223 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800224
John Stultzff3ead92011-01-11 09:42:13 -0800225}
226
Todd Poynor6cffe002013-05-15 14:38:11 -0700227ktime_t alarm_expires_remaining(const struct alarm *alarm)
228{
229 struct alarm_base *base = &alarm_bases[alarm->type];
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000230 return ktime_sub(alarm->node.expires, base->get_ktime());
Todd Poynor6cffe002013-05-15 14:38:11 -0700231}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200232EXPORT_SYMBOL_GPL(alarm_expires_remaining);
Todd Poynor6cffe002013-05-15 14:38:11 -0700233
John Stultz472647d2011-04-29 15:03:10 -0700234#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700235/**
John Stultzff3ead92011-01-11 09:42:13 -0800236 * alarmtimer_suspend - Suspend time callback
237 * @dev: unused
John Stultzff3ead92011-01-11 09:42:13 -0800238 *
239 * When we are going into suspend, we look through the bases
240 * to see which is the soonest timer to expire. We then
241 * set an rtc timer to fire that far into the future, which
242 * will wake us from suspend.
243 */
244static int alarmtimer_suspend(struct device *dev)
245{
Baolin Wang4a057542016-11-28 14:35:21 -0800246 ktime_t min, now, expires;
247 int i, ret, type;
John Stultzc008ba582011-06-16 18:27:09 -0700248 struct rtc_device *rtc;
Baolin Wang4a057542016-11-28 14:35:21 -0800249 unsigned long flags;
250 struct rtc_time tm;
John Stultzff3ead92011-01-11 09:42:13 -0800251
252 spin_lock_irqsave(&freezer_delta_lock, flags);
253 min = freezer_delta;
Baolin Wang4a057542016-11-28 14:35:21 -0800254 expires = freezer_expires;
255 type = freezer_alarmtype;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100256 freezer_delta = 0;
John Stultzff3ead92011-01-11 09:42:13 -0800257 spin_unlock_irqrestore(&freezer_delta_lock, flags);
258
John Stultz8bc0daf2011-07-14 18:35:13 -0700259 rtc = alarmtimer_get_rtcdev();
John Stultzff3ead92011-01-11 09:42:13 -0800260 /* If we have no rtcdev, just return */
John Stultzc008ba582011-06-16 18:27:09 -0700261 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800262 return 0;
263
264 /* Find the soonest timer to expire*/
265 for (i = 0; i < ALARM_NUMTYPE; i++) {
266 struct alarm_base *base = &alarm_bases[i];
267 struct timerqueue_node *next;
268 ktime_t delta;
269
270 spin_lock_irqsave(&base->lock, flags);
271 next = timerqueue_getnext(&base->timerqueue);
272 spin_unlock_irqrestore(&base->lock, flags);
273 if (!next)
274 continue;
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000275 delta = ktime_sub(next->expires, base->get_ktime());
Thomas Gleixner2456e852016-12-25 11:38:40 +0100276 if (!min || (delta < min)) {
Baolin Wang4a057542016-11-28 14:35:21 -0800277 expires = next->expires;
John Stultzff3ead92011-01-11 09:42:13 -0800278 min = delta;
Baolin Wang4a057542016-11-28 14:35:21 -0800279 type = i;
280 }
John Stultzff3ead92011-01-11 09:42:13 -0800281 }
Thomas Gleixner2456e852016-12-25 11:38:40 +0100282 if (min == 0)
John Stultzff3ead92011-01-11 09:42:13 -0800283 return 0;
284
Todd Poynor59a93c22012-08-09 00:37:27 -0700285 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
286 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
287 return -EBUSY;
288 }
John Stultzff3ead92011-01-11 09:42:13 -0800289
Baolin Wang4a057542016-11-28 14:35:21 -0800290 trace_alarmtimer_suspend(expires, type);
291
John Stultzff3ead92011-01-11 09:42:13 -0800292 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba582011-06-16 18:27:09 -0700293 rtc_timer_cancel(rtc, &rtctimer);
294 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800295 now = rtc_tm_to_ktime(tm);
296 now = ktime_add(now, min);
297
Todd Poynor59a93c22012-08-09 00:37:27 -0700298 /* Set alarm, if in the past reject suspend briefly to handle */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100299 ret = rtc_timer_start(rtc, &rtctimer, now, 0);
Todd Poynor59a93c22012-08-09 00:37:27 -0700300 if (ret < 0)
301 __pm_wakeup_event(ws, MSEC_PER_SEC);
302 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800303}
zhuo-haoa0e32132015-11-17 20:08:07 +0800304
305static int alarmtimer_resume(struct device *dev)
306{
307 struct rtc_device *rtc;
308
309 rtc = alarmtimer_get_rtcdev();
310 if (rtc)
311 rtc_timer_cancel(rtc, &rtctimer);
312 return 0;
313}
314
John Stultz472647d2011-04-29 15:03:10 -0700315#else
316static int alarmtimer_suspend(struct device *dev)
317{
318 return 0;
319}
zhuo-haoa0e32132015-11-17 20:08:07 +0800320
321static int alarmtimer_resume(struct device *dev)
322{
323 return 0;
324}
John Stultz472647d2011-04-29 15:03:10 -0700325#endif
John Stultzff3ead92011-01-11 09:42:13 -0800326
Thomas Gleixnerbd031432018-03-26 15:29:57 +0200327static void
328__alarm_init(struct alarm *alarm, enum alarmtimer_type type,
329 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
330{
331 timerqueue_init(&alarm->node);
332 alarm->timer.function = alarmtimer_fired;
333 alarm->function = function;
334 alarm->type = type;
335 alarm->state = ALARMTIMER_STATE_INACTIVE;
336}
337
John Stultz180bf812011-04-28 12:58:11 -0700338/**
John Stultzff3ead92011-01-11 09:42:13 -0800339 * alarm_init - Initialize an alarm structure
340 * @alarm: ptr to alarm to be initialized
341 * @type: the type of the alarm
342 * @function: callback that is run when the alarm fires
John Stultzff3ead92011-01-11 09:42:13 -0800343 */
344void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
John Stultz4b413082011-08-10 10:37:59 -0700345 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
John Stultzff3ead92011-01-11 09:42:13 -0800346{
John Stultzdae373b2012-09-13 19:12:16 -0400347 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
Thomas Gleixnerbd031432018-03-26 15:29:57 +0200348 HRTIMER_MODE_ABS);
349 __alarm_init(alarm, type, function);
John Stultzff3ead92011-01-11 09:42:13 -0800350}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200351EXPORT_SYMBOL_GPL(alarm_init);
John Stultzff3ead92011-01-11 09:42:13 -0800352
John Stultz180bf812011-04-28 12:58:11 -0700353/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700354 * alarm_start - Sets an absolute alarm to fire
John Stultzff3ead92011-01-11 09:42:13 -0800355 * @alarm: ptr to alarm to set
356 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800357 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000358void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800359{
360 struct alarm_base *base = &alarm_bases[alarm->type];
361 unsigned long flags;
362
363 spin_lock_irqsave(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800364 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800365 alarmtimer_enqueue(base, alarm);
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000366 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
John Stultzff3ead92011-01-11 09:42:13 -0800367 spin_unlock_irqrestore(&base->lock, flags);
Baolin Wang4a057542016-11-28 14:35:21 -0800368
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000369 trace_alarmtimer_start(alarm, base->get_ktime());
John Stultzff3ead92011-01-11 09:42:13 -0800370}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200371EXPORT_SYMBOL_GPL(alarm_start);
John Stultzff3ead92011-01-11 09:42:13 -0800372
John Stultz180bf812011-04-28 12:58:11 -0700373/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700374 * alarm_start_relative - Sets a relative alarm to fire
375 * @alarm: ptr to alarm to set
376 * @start: time relative to now to run the alarm
377 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000378void alarm_start_relative(struct alarm *alarm, ktime_t start)
Todd Poynor6cffe002013-05-15 14:38:11 -0700379{
380 struct alarm_base *base = &alarm_bases[alarm->type];
381
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000382 start = ktime_add_safe(start, base->get_ktime());
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000383 alarm_start(alarm, start);
Todd Poynor6cffe002013-05-15 14:38:11 -0700384}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200385EXPORT_SYMBOL_GPL(alarm_start_relative);
Todd Poynor6cffe002013-05-15 14:38:11 -0700386
387void alarm_restart(struct alarm *alarm)
388{
389 struct alarm_base *base = &alarm_bases[alarm->type];
390 unsigned long flags;
391
392 spin_lock_irqsave(&base->lock, flags);
393 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
394 hrtimer_restart(&alarm->timer);
395 alarmtimer_enqueue(base, alarm);
396 spin_unlock_irqrestore(&base->lock, flags);
397}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200398EXPORT_SYMBOL_GPL(alarm_restart);
Todd Poynor6cffe002013-05-15 14:38:11 -0700399
400/**
John Stultz9082c462011-08-10 12:41:36 -0700401 * alarm_try_to_cancel - Tries to cancel an alarm timer
John Stultzff3ead92011-01-11 09:42:13 -0800402 * @alarm: ptr to alarm to be canceled
John Stultz9082c462011-08-10 12:41:36 -0700403 *
404 * Returns 1 if the timer was canceled, 0 if it was not running,
405 * and -1 if the callback was running
John Stultzff3ead92011-01-11 09:42:13 -0800406 */
John Stultz9082c462011-08-10 12:41:36 -0700407int alarm_try_to_cancel(struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800408{
409 struct alarm_base *base = &alarm_bases[alarm->type];
410 unsigned long flags;
John Stultzdae373b2012-09-13 19:12:16 -0400411 int ret;
412
John Stultzff3ead92011-01-11 09:42:13 -0800413 spin_lock_irqsave(&base->lock, flags);
John Stultzdae373b2012-09-13 19:12:16 -0400414 ret = hrtimer_try_to_cancel(&alarm->timer);
415 if (ret >= 0)
John Stultza65bcc12012-09-13 19:25:22 -0400416 alarmtimer_dequeue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800417 spin_unlock_irqrestore(&base->lock, flags);
Baolin Wang4a057542016-11-28 14:35:21 -0800418
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000419 trace_alarmtimer_cancel(alarm, base->get_ktime());
John Stultz9082c462011-08-10 12:41:36 -0700420 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800421}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200422EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
John Stultzff3ead92011-01-11 09:42:13 -0800423
424
John Stultz9082c462011-08-10 12:41:36 -0700425/**
426 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
427 * @alarm: ptr to alarm to be canceled
428 *
429 * Returns 1 if the timer was canceled, 0 if it was not active.
430 */
431int alarm_cancel(struct alarm *alarm)
432{
433 for (;;) {
434 int ret = alarm_try_to_cancel(alarm);
435 if (ret >= 0)
436 return ret;
Anna-Maria Gleixner51ae3302019-07-31 00:33:49 +0200437 hrtimer_cancel_wait_running(&alarm->timer);
John Stultz9082c462011-08-10 12:41:36 -0700438 }
439}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200440EXPORT_SYMBOL_GPL(alarm_cancel);
John Stultz9082c462011-08-10 12:41:36 -0700441
John Stultzdce75a82011-08-10 11:31:03 -0700442
443u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
444{
445 u64 overrun = 1;
446 ktime_t delta;
447
448 delta = ktime_sub(now, alarm->node.expires);
449
Thomas Gleixner2456e852016-12-25 11:38:40 +0100450 if (delta < 0)
John Stultzdce75a82011-08-10 11:31:03 -0700451 return 0;
452
Thomas Gleixner2456e852016-12-25 11:38:40 +0100453 if (unlikely(delta >= interval)) {
John Stultzdce75a82011-08-10 11:31:03 -0700454 s64 incr = ktime_to_ns(interval);
455
456 overrun = ktime_divns(delta, incr);
457
458 alarm->node.expires = ktime_add_ns(alarm->node.expires,
459 incr*overrun);
460
Thomas Gleixner2456e852016-12-25 11:38:40 +0100461 if (alarm->node.expires > now)
John Stultzdce75a82011-08-10 11:31:03 -0700462 return overrun;
463 /*
464 * This (and the ktime_add() below) is the
465 * correction for exact:
466 */
467 overrun++;
468 }
469
Thomas Gleixnerf4781e72017-05-30 23:15:34 +0200470 alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
John Stultzdce75a82011-08-10 11:31:03 -0700471 return overrun;
472}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200473EXPORT_SYMBOL_GPL(alarm_forward);
John Stultzdce75a82011-08-10 11:31:03 -0700474
Todd Poynor6cffe002013-05-15 14:38:11 -0700475u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
476{
477 struct alarm_base *base = &alarm_bases[alarm->type];
478
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000479 return alarm_forward(alarm, base->get_ktime(), interval);
Todd Poynor6cffe002013-05-15 14:38:11 -0700480}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200481EXPORT_SYMBOL_GPL(alarm_forward_now);
John Stultzdce75a82011-08-10 11:31:03 -0700482
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300483#ifdef CONFIG_POSIX_TIMERS
484
485static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
486{
487 struct alarm_base *base;
488 unsigned long flags;
489 ktime_t delta;
490
491 switch(type) {
492 case ALARM_REALTIME:
493 base = &alarm_bases[ALARM_REALTIME];
494 type = ALARM_REALTIME_FREEZER;
495 break;
496 case ALARM_BOOTTIME:
497 base = &alarm_bases[ALARM_BOOTTIME];
498 type = ALARM_BOOTTIME_FREEZER;
499 break;
500 default:
501 WARN_ONCE(1, "Invalid alarm type: %d\n", type);
502 return;
503 }
504
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000505 delta = ktime_sub(absexp, base->get_ktime());
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300506
507 spin_lock_irqsave(&freezer_delta_lock, flags);
508 if (!freezer_delta || (delta < freezer_delta)) {
509 freezer_delta = delta;
510 freezer_expires = absexp;
511 freezer_alarmtype = type;
512 }
513 spin_unlock_irqrestore(&freezer_delta_lock, flags);
514}
John Stultzdce75a82011-08-10 11:31:03 -0700515
John Stultz180bf812011-04-28 12:58:11 -0700516/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800517 * clock2alarm - helper that converts from clockid to alarmtypes
518 * @clockid: clockid.
John Stultz9a7adcf52011-01-11 09:54:33 -0800519 */
520static enum alarmtimer_type clock2alarm(clockid_t clockid)
521{
522 if (clockid == CLOCK_REALTIME_ALARM)
523 return ALARM_REALTIME;
524 if (clockid == CLOCK_BOOTTIME_ALARM)
525 return ALARM_BOOTTIME;
526 return -1;
527}
528
John Stultz180bf812011-04-28 12:58:11 -0700529/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800530 * alarm_handle_timer - Callback for posix timers
531 * @alarm: alarm that fired
532 *
533 * Posix timer callback for expired alarm timers.
534 */
John Stultz4b413082011-08-10 10:37:59 -0700535static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
536 ktime_t now)
John Stultz9a7adcf52011-01-11 09:54:33 -0800537{
538 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200539 it.alarm.alarmtimer);
Richard Larocque474e941b2014-09-09 18:31:05 -0700540 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200541 unsigned long flags;
542 int si_private = 0;
Richard Larocque474e941b2014-09-09 18:31:05 -0700543
544 spin_lock_irqsave(&ptr->it_lock, flags);
John Stultz4b413082011-08-10 10:37:59 -0700545
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200546 ptr->it_active = 0;
547 if (ptr->it_interval)
548 si_private = ++ptr->it_requeue_pending;
549
550 if (posix_timer_event(ptr, si_private) && ptr->it_interval) {
551 /*
552 * Handle ignored signals and rearm the timer. This will go
553 * away once we handle ignored signals proper.
554 */
555 ptr->it_overrun += alarm_forward_now(alarm, ptr->it_interval);
556 ++ptr->it_requeue_pending;
557 ptr->it_active = 1;
Richard Larocque474e941b2014-09-09 18:31:05 -0700558 result = ALARMTIMER_RESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700559 }
Richard Larocque474e941b2014-09-09 18:31:05 -0700560 spin_unlock_irqrestore(&ptr->it_lock, flags);
561
562 return result;
John Stultz9a7adcf52011-01-11 09:54:33 -0800563}
564
John Stultz180bf812011-04-28 12:58:11 -0700565/**
Thomas Gleixnerb3db80f2017-05-30 23:15:54 +0200566 * alarm_timer_rearm - Posix timer callback for rearming timer
567 * @timr: Pointer to the posixtimer data struct
568 */
569static void alarm_timer_rearm(struct k_itimer *timr)
570{
571 struct alarm *alarm = &timr->it.alarm.alarmtimer;
572
573 timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
574 alarm_start(alarm, alarm->node.expires);
575}
576
577/**
Thomas Gleixnere7561f12017-05-30 23:15:55 +0200578 * alarm_timer_forward - Posix timer callback for forwarding timer
579 * @timr: Pointer to the posixtimer data struct
580 * @now: Current time to forward the timer against
581 */
Thomas Gleixner6fec64e2018-06-26 15:21:31 +0200582static s64 alarm_timer_forward(struct k_itimer *timr, ktime_t now)
Thomas Gleixnere7561f12017-05-30 23:15:55 +0200583{
584 struct alarm *alarm = &timr->it.alarm.alarmtimer;
585
Thomas Gleixner6fec64e2018-06-26 15:21:31 +0200586 return alarm_forward(alarm, timr->it_interval, now);
Thomas Gleixnere7561f12017-05-30 23:15:55 +0200587}
588
589/**
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200590 * alarm_timer_remaining - Posix timer callback to retrieve remaining time
591 * @timr: Pointer to the posixtimer data struct
592 * @now: Current time to calculate against
593 */
594static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
595{
596 struct alarm *alarm = &timr->it.alarm.alarmtimer;
597
Andrei Vagin07d7e122019-04-07 21:15:42 -0700598 return ktime_sub(alarm->node.expires, now);
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200599}
600
601/**
Thomas Gleixnere344c9e2017-05-30 23:15:57 +0200602 * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
603 * @timr: Pointer to the posixtimer data struct
604 */
605static int alarm_timer_try_to_cancel(struct k_itimer *timr)
606{
607 return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
608}
609
610/**
Thomas Gleixnerec8f9542019-08-02 07:35:59 +0200611 * alarm_timer_wait_running - Posix timer callback to wait for a timer
612 * @timr: Pointer to the posixtimer data struct
613 *
614 * Called from the core code when timer cancel detected that the callback
615 * is running. @timr is unlocked and rcu read lock is held to prevent it
616 * from being freed.
617 */
618static void alarm_timer_wait_running(struct k_itimer *timr)
619{
620 hrtimer_cancel_wait_running(&timr->it.alarm.alarmtimer.timer);
621}
622
623/**
Thomas Gleixnerb3bf6f32017-05-30 23:15:58 +0200624 * alarm_timer_arm - Posix timer callback to arm a timer
625 * @timr: Pointer to the posixtimer data struct
626 * @expires: The new expiry time
627 * @absolute: Expiry value is absolute time
628 * @sigev_none: Posix timer does not deliver signals
629 */
630static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
631 bool absolute, bool sigev_none)
632{
633 struct alarm *alarm = &timr->it.alarm.alarmtimer;
634 struct alarm_base *base = &alarm_bases[alarm->type];
635
636 if (!absolute)
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000637 expires = ktime_add_safe(expires, base->get_ktime());
Thomas Gleixnerb3bf6f32017-05-30 23:15:58 +0200638 if (sigev_none)
639 alarm->node.expires = expires;
640 else
641 alarm_start(&timr->it.alarm.alarmtimer, expires);
642}
643
644/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800645 * alarm_clock_getres - posix getres interface
646 * @which_clock: clockid
647 * @tp: timespec to fill
648 *
649 * Returns the granularity of underlying alarm base clock
650 */
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700651static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
John Stultz9a7adcf52011-01-11 09:54:33 -0800652{
John Stultz1c6b39a2011-06-16 18:47:37 -0700653 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400654 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700655
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000656 tp->tv_sec = 0;
657 tp->tv_nsec = hrtimer_resolution;
658 return 0;
John Stultz9a7adcf52011-01-11 09:54:33 -0800659}
660
661/**
Andrei Vagineaf80192019-11-12 01:26:55 +0000662 * alarm_clock_get_timespec - posix clock_get_timespec interface
John Stultz9a7adcf52011-01-11 09:54:33 -0800663 * @which_clock: clockid
664 * @tp: timespec to fill.
665 *
666 * Provides the underlying alarm base time.
667 */
Andrei Vagineaf80192019-11-12 01:26:55 +0000668static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tp)
John Stultz9a7adcf52011-01-11 09:54:33 -0800669{
670 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
671
John Stultz1c6b39a2011-06-16 18:47:37 -0700672 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400673 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700674
Andrei Vagin2f58bf92019-11-12 01:26:57 +0000675 base->get_timespec(tp);
676
John Stultz9a7adcf52011-01-11 09:54:33 -0800677 return 0;
678}
679
680/**
681 * alarm_timer_create - posix timer_create interface
682 * @new_timer: k_itimer pointer to manage
683 *
684 * Initializes the k_itimer structure.
685 */
686static int alarm_timer_create(struct k_itimer *new_timer)
687{
688 enum alarmtimer_type type;
John Stultz9a7adcf52011-01-11 09:54:33 -0800689
John Stultz1c6b39a2011-06-16 18:47:37 -0700690 if (!alarmtimer_get_rtcdev())
Thadeu Lima de Souza Cascardof18ddc132019-09-03 14:18:02 -0300691 return -EOPNOTSUPP;
John Stultz1c6b39a2011-06-16 18:47:37 -0700692
John Stultz9a7adcf52011-01-11 09:54:33 -0800693 if (!capable(CAP_WAKE_ALARM))
694 return -EPERM;
695
696 type = clock2alarm(new_timer->it_clock);
John Stultz9e264762011-08-10 12:09:24 -0700697 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf52011-01-11 09:54:33 -0800698 return 0;
699}
700
701/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800702 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
703 * @alarm: ptr to alarm that fired
704 *
705 * Wakes up the task that set the alarmtimer
706 */
John Stultz4b413082011-08-10 10:37:59 -0700707static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
708 ktime_t now)
John Stultz9a7adcf52011-01-11 09:54:33 -0800709{
710 struct task_struct *task = (struct task_struct *)alarm->data;
711
712 alarm->data = NULL;
713 if (task)
714 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700715 return ALARMTIMER_NORESTART;
John Stultz9a7adcf52011-01-11 09:54:33 -0800716}
717
718/**
719 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
720 * @alarm: ptr to alarmtimer
721 * @absexp: absolute expiration time
722 *
723 * Sets the alarm timer and sleeps until it is fired or interrupted.
724 */
Al Viro15f27ce2017-06-07 09:42:27 +0100725static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
726 enum alarmtimer_type type)
John Stultz9a7adcf52011-01-11 09:54:33 -0800727{
Al Viroedbeda42017-06-07 09:42:31 +0100728 struct restart_block *restart;
John Stultz9a7adcf52011-01-11 09:54:33 -0800729 alarm->data = (void *)current;
730 do {
731 set_current_state(TASK_INTERRUPTIBLE);
John Stultz9e264762011-08-10 12:09:24 -0700732 alarm_start(alarm, absexp);
John Stultz9a7adcf52011-01-11 09:54:33 -0800733 if (likely(alarm->data))
734 schedule();
735
736 alarm_cancel(alarm);
737 } while (alarm->data && !signal_pending(current));
738
739 __set_current_state(TASK_RUNNING);
740
Thomas Gleixnerbd031432018-03-26 15:29:57 +0200741 destroy_hrtimer_on_stack(&alarm->timer);
742
Al Viro15f27ce2017-06-07 09:42:27 +0100743 if (!alarm->data)
John Stultz9a7adcf52011-01-11 09:54:33 -0800744 return 0;
John Stultz9a7adcf52011-01-11 09:54:33 -0800745
Al Viro15f27ce2017-06-07 09:42:27 +0100746 if (freezing(current))
747 alarmtimer_freezerset(absexp, type);
Al Viroedbeda42017-06-07 09:42:31 +0100748 restart = &current->restart_block;
749 if (restart->nanosleep.type != TT_NONE) {
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -0700750 struct timespec64 rmt;
Al Viro15f27ce2017-06-07 09:42:27 +0100751 ktime_t rem;
John Stultz9a7adcf52011-01-11 09:54:33 -0800752
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000753 rem = ktime_sub(absexp, alarm_bases[type].get_ktime());
John Stultz9a7adcf52011-01-11 09:54:33 -0800754
Al Viro15f27ce2017-06-07 09:42:27 +0100755 if (rem <= 0)
756 return 0;
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -0700757 rmt = ktime_to_timespec64(rem);
Al Viro15f27ce2017-06-07 09:42:27 +0100758
Al Viroce41aaf2017-06-07 09:42:32 +0100759 return nanosleep_copyout(restart, &rmt);
Al Viro15f27ce2017-06-07 09:42:27 +0100760 }
761 return -ERESTART_RESTARTBLOCK;
John Stultz9a7adcf52011-01-11 09:54:33 -0800762}
763
Thomas Gleixnerbd031432018-03-26 15:29:57 +0200764static void
765alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type,
766 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
767{
768 hrtimer_init_on_stack(&alarm->timer, alarm_bases[type].base_clockid,
769 HRTIMER_MODE_ABS);
770 __alarm_init(alarm, type, function);
771}
772
John Stultz9a7adcf52011-01-11 09:54:33 -0800773/**
774 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
775 * @restart: ptr to restart block
776 *
777 * Handles restarted clock_nanosleep calls
778 */
779static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
780{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200781 enum alarmtimer_type type = restart->nanosleep.clockid;
Al Viro15f27ce2017-06-07 09:42:27 +0100782 ktime_t exp = restart->nanosleep.expires;
John Stultz9a7adcf52011-01-11 09:54:33 -0800783 struct alarm alarm;
John Stultz9a7adcf52011-01-11 09:54:33 -0800784
Thomas Gleixnerbd031432018-03-26 15:29:57 +0200785 alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
John Stultz9a7adcf52011-01-11 09:54:33 -0800786
Al Viro15f27ce2017-06-07 09:42:27 +0100787 return alarmtimer_do_nsleep(&alarm, exp, type);
John Stultz9a7adcf52011-01-11 09:54:33 -0800788}
789
790/**
791 * alarm_timer_nsleep - alarmtimer nanosleep
792 * @which_clock: clockid
793 * @flags: determins abstime or relative
794 * @tsreq: requested sleep time (abs or rel)
795 * @rmtp: remaining sleep time saved
796 *
797 * Handles clock_nanosleep calls against _ALARM clockids
798 */
799static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner938e7cf2017-06-13 23:34:33 +0200800 const struct timespec64 *tsreq)
John Stultz9a7adcf52011-01-11 09:54:33 -0800801{
802 enum alarmtimer_type type = clock2alarm(which_clock);
Al Viro15f27ce2017-06-07 09:42:27 +0100803 struct restart_block *restart = &current->restart_block;
John Stultz9a7adcf52011-01-11 09:54:33 -0800804 struct alarm alarm;
805 ktime_t exp;
806 int ret = 0;
John Stultz9a7adcf52011-01-11 09:54:33 -0800807
John Stultz1c6b39a2011-06-16 18:47:37 -0700808 if (!alarmtimer_get_rtcdev())
Thadeu Lima de Souza Cascardof18ddc132019-09-03 14:18:02 -0300809 return -EOPNOTSUPP;
John Stultz1c6b39a2011-06-16 18:47:37 -0700810
John Stultz16927772014-07-07 14:06:11 -0700811 if (flags & ~TIMER_ABSTIME)
812 return -EINVAL;
813
John Stultz9a7adcf52011-01-11 09:54:33 -0800814 if (!capable(CAP_WAKE_ALARM))
815 return -EPERM;
816
Thomas Gleixnerbd031432018-03-26 15:29:57 +0200817 alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
John Stultz9a7adcf52011-01-11 09:54:33 -0800818
Deepa Dinamaniad196382017-03-26 12:04:18 -0700819 exp = timespec64_to_ktime(*tsreq);
John Stultz9a7adcf52011-01-11 09:54:33 -0800820 /* Convert (if necessary) to absolute time */
821 if (flags != TIMER_ABSTIME) {
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000822 ktime_t now = alarm_bases[type].get_ktime();
Thomas Gleixner5f936e12018-07-02 09:34:29 +0200823
824 exp = ktime_add_safe(now, exp);
John Stultz9a7adcf52011-01-11 09:54:33 -0800825 }
826
Al Viro15f27ce2017-06-07 09:42:27 +0100827 ret = alarmtimer_do_nsleep(&alarm, exp, type);
828 if (ret != -ERESTART_RESTARTBLOCK)
829 return ret;
John Stultz9a7adcf52011-01-11 09:54:33 -0800830
831 /* abs timers don't set remaining time or restart */
Al Viro15f27ce2017-06-07 09:42:27 +0100832 if (flags == TIMER_ABSTIME)
833 return -ERESTARTNOHAND;
John Stultz9a7adcf52011-01-11 09:54:33 -0800834
John Stultz9a7adcf52011-01-11 09:54:33 -0800835 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200836 restart->nanosleep.clockid = type;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100837 restart->nanosleep.expires = exp;
John Stultz9a7adcf52011-01-11 09:54:33 -0800838 return ret;
839}
John Stultzff3ead92011-01-11 09:42:13 -0800840
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300841const struct k_clock alarm_clock = {
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200842 .clock_getres = alarm_clock_getres,
Andrei Vagineaf80192019-11-12 01:26:55 +0000843 .clock_get_timespec = alarm_clock_get_timespec,
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200844 .timer_create = alarm_timer_create,
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200845 .timer_set = common_timer_set,
846 .timer_del = common_timer_del,
847 .timer_get = common_timer_get,
Thomas Gleixnerb3bf6f32017-05-30 23:15:58 +0200848 .timer_arm = alarm_timer_arm,
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200849 .timer_rearm = alarm_timer_rearm,
850 .timer_forward = alarm_timer_forward,
851 .timer_remaining = alarm_timer_remaining,
Thomas Gleixnere344c9e2017-05-30 23:15:57 +0200852 .timer_try_to_cancel = alarm_timer_try_to_cancel,
Thomas Gleixnerec8f9542019-08-02 07:35:59 +0200853 .timer_wait_running = alarm_timer_wait_running,
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200854 .nsleep = alarm_timer_nsleep,
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300855};
856#endif /* CONFIG_POSIX_TIMERS */
857
John Stultzff3ead92011-01-11 09:42:13 -0800858
859/* Suspend hook structures */
860static const struct dev_pm_ops alarmtimer_pm_ops = {
861 .suspend = alarmtimer_suspend,
zhuo-haoa0e32132015-11-17 20:08:07 +0800862 .resume = alarmtimer_resume,
John Stultzff3ead92011-01-11 09:42:13 -0800863};
864
865static struct platform_driver alarmtimer_driver = {
866 .driver = {
867 .name = "alarmtimer",
868 .pm = &alarmtimer_pm_ops,
869 }
870};
871
872/**
873 * alarmtimer_init - Initialize alarm timer code
874 *
875 * This function initializes the alarm bases and registers
876 * the posix clock ids.
877 */
878static int __init alarmtimer_init(void)
879{
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200880 struct platform_device *pdev;
John Stultzff3ead92011-01-11 09:42:13 -0800881 int error = 0;
882 int i;
John Stultz9a7adcf52011-01-11 09:54:33 -0800883
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100884 alarmtimer_rtc_timer_init();
John Stultzad30dfa2012-03-23 15:52:25 -0700885
John Stultzff3ead92011-01-11 09:42:13 -0800886 /* Initialize alarm bases */
887 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000888 alarm_bases[ALARM_REALTIME].get_ktime = &ktime_get_real;
Andrei Vagin2f58bf92019-11-12 01:26:57 +0000889 alarm_bases[ALARM_REALTIME].get_timespec = ktime_get_real_ts64,
John Stultzff3ead92011-01-11 09:42:13 -0800890 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
Andrei Vagin41b3b8d2019-11-12 01:26:56 +0000891 alarm_bases[ALARM_BOOTTIME].get_ktime = &ktime_get_boottime;
Andrei Vagin2f58bf92019-11-12 01:26:57 +0000892 alarm_bases[ALARM_BOOTTIME].get_timespec = ktime_get_boottime_ts64;
John Stultzff3ead92011-01-11 09:42:13 -0800893 for (i = 0; i < ALARM_NUMTYPE; i++) {
894 timerqueue_init_head(&alarm_bases[i].timerqueue);
895 spin_lock_init(&alarm_bases[i].lock);
John Stultzff3ead92011-01-11 09:42:13 -0800896 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700897
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200898 error = alarmtimer_rtc_interface_setup();
899 if (error)
900 return error;
John Stultzff3ead92011-01-11 09:42:13 -0800901
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200902 error = platform_driver_register(&alarmtimer_driver);
903 if (error)
904 goto out_if;
905
906 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
907 if (IS_ERR(pdev)) {
908 error = PTR_ERR(pdev);
909 goto out_drv;
910 }
911 return 0;
912
913out_drv:
914 platform_driver_unregister(&alarmtimer_driver);
915out_if:
916 alarmtimer_rtc_interface_remove();
John Stultzff3ead92011-01-11 09:42:13 -0800917 return error;
918}
919device_initcall(alarmtimer_init);