blob: 0519a8805aab3f290e3fb437ff0903a0e4722d90 [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 Gleixnerbab0aae92017-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
John Stultz180bf812011-04-28 12:58:11 -070039 * @gettime: Function to read the time correlating to the base
40 * @base_clockid: clockid for the base
John Stultz180bf812011-04-28 12:58:11 -070041 */
John Stultzff3ead92011-01-11 09:42:13 -080042static struct alarm_base {
43 spinlock_t lock;
44 struct timerqueue_head timerqueue;
John Stultzff3ead92011-01-11 09:42:13 -080045 ktime_t (*gettime)(void);
46 clockid_t base_clockid;
John Stultzff3ead92011-01-11 09:42:13 -080047} alarm_bases[ALARM_NUMTYPE];
48
Thomas Gleixnerb6b3b802017-05-27 12:23:47 +020049#if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
Baolin Wang4a057542016-11-28 14:35:21 -080050/* freezer information to handle clock_nanosleep triggered wakeups */
51static enum alarmtimer_type freezer_alarmtype;
52static ktime_t freezer_expires;
John Stultzc008ba582011-06-16 18:27:09 -070053static ktime_t freezer_delta;
54static DEFINE_SPINLOCK(freezer_delta_lock);
Thomas Gleixnerb6b3b802017-05-27 12:23:47 +020055#endif
John Stultzc008ba582011-06-16 18:27:09 -070056
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +020057#ifdef CONFIG_RTC_CLASS
Todd Poynor59a93c22012-08-09 00:37:27 -070058static struct wakeup_source *ws;
59
John Stultz180bf812011-04-28 12:58:11 -070060/* rtc timer and device for setting alarm wakeups at suspend */
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +010061static struct rtc_timer rtctimer;
John Stultzff3ead92011-01-11 09:42:13 -080062static struct rtc_device *rtcdev;
John Stultzc008ba582011-06-16 18:27:09 -070063static DEFINE_SPINLOCK(rtcdev_lock);
John Stultzff3ead92011-01-11 09:42:13 -080064
John Stultzc008ba582011-06-16 18:27:09 -070065/**
John Stultzc008ba582011-06-16 18:27:09 -070066 * 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 */
John Stultz57c498f2012-04-20 12:31:45 -070072struct rtc_device *alarmtimer_get_rtcdev(void)
John Stultzc008ba582011-06-16 18:27:09 -070073{
John Stultzc008ba582011-06-16 18:27:09 -070074 unsigned long flags;
75 struct rtc_device *ret;
76
77 spin_lock_irqsave(&rtcdev_lock, flags);
John Stultzc008ba582011-06-16 18:27:09 -070078 ret = rtcdev;
79 spin_unlock_irqrestore(&rtcdev_lock, flags);
80
81 return ret;
82}
Pramod Gurav71d5d2b2014-06-13 11:49:42 +053083EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
John Stultz8bc0daf2011-07-14 18:35:13 -070084
85static 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);
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +020090 struct wakeup_source *__ws;
John Stultz8bc0daf2011-07-14 18:35:13 -070091
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
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +0200100 __ws = wakeup_source_register("alarmtimer");
101
John Stultz8bc0daf2011-07-14 18:35:13 -0700102 spin_lock_irqsave(&rtcdev_lock, flags);
103 if (!rtcdev) {
Alexandre Belloni51218292017-08-21 00:01:46 +0200104 if (!try_module_get(rtc->owner)) {
105 spin_unlock_irqrestore(&rtcdev_lock, flags);
106 return -1;
107 }
108
John Stultz8bc0daf2011-07-14 18:35:13 -0700109 rtcdev = rtc;
110 /* hold a reference so it doesn't go away */
111 get_device(dev);
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +0200112 ws = __ws;
113 __ws = NULL;
John Stultz8bc0daf2011-07-14 18:35:13 -0700114 }
115 spin_unlock_irqrestore(&rtcdev_lock, flags);
Geert Uytterhoeven47b4a452017-07-05 14:08:35 +0200116
117 wakeup_source_unregister(__ws);
118
John Stultz8bc0daf2011-07-14 18:35:13 -0700119 return 0;
120}
121
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100122static inline void alarmtimer_rtc_timer_init(void)
123{
124 rtc_timer_init(&rtctimer, NULL, NULL);
125}
126
John Stultz8bc0daf2011-07-14 18:35:13 -0700127static struct class_interface alarmtimer_rtc_interface = {
128 .add_dev = &alarmtimer_rtc_add_device,
129};
130
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200131static int alarmtimer_rtc_interface_setup(void)
John Stultz8bc0daf2011-07-14 18:35:13 -0700132{
133 alarmtimer_rtc_interface.class = rtc_class;
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200134 return class_interface_register(&alarmtimer_rtc_interface);
135}
136static void alarmtimer_rtc_interface_remove(void)
137{
138 class_interface_unregister(&alarmtimer_rtc_interface);
John Stultz8bc0daf2011-07-14 18:35:13 -0700139}
John Stultz1c6b39a2011-06-16 18:47:37 -0700140#else
John Stultz57c498f2012-04-20 12:31:45 -0700141struct rtc_device *alarmtimer_get_rtcdev(void)
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200142{
143 return NULL;
144}
145#define rtcdev (NULL)
146static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
147static inline void alarmtimer_rtc_interface_remove(void) { }
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100148static inline void alarmtimer_rtc_timer_init(void) { }
John Stultzc008ba582011-06-16 18:27:09 -0700149#endif
John Stultzff3ead92011-01-11 09:42:13 -0800150
John Stultz180bf812011-04-28 12:58:11 -0700151/**
John Stultzff3ead92011-01-11 09:42:13 -0800152 * 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 *
John Stultzdae373b2012-09-13 19:12:16 -0400156 * Adds alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800157 *
158 * Must hold base->lock when calling.
159 */
160static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
161{
John Stultzdae373b2012-09-13 19:12:16 -0400162 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
163 timerqueue_del(&base->timerqueue, &alarm->node);
164
John Stultzff3ead92011-01-11 09:42:13 -0800165 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700166 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800167}
168
John Stultz180bf812011-04-28 12:58:11 -0700169/**
John Stultza65bcc12012-09-13 19:25:22 -0400170 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800171 * @base: pointer to the base where the timer is running
172 * @alarm: pointer to alarm being removed
173 *
John Stultzdae373b2012-09-13 19:12:16 -0400174 * Removes alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800175 *
176 * Must hold base->lock when calling.
177 */
John Stultza65bcc12012-09-13 19:25:22 -0400178static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800179{
John Stultza28cde82011-08-10 12:30:21 -0700180 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
181 return;
182
John Stultzff3ead92011-01-11 09:42:13 -0800183 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700184 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800185}
186
John Stultz7068b7a2011-04-28 13:29:18 -0700187
John Stultz180bf812011-04-28 12:58:11 -0700188/**
John Stultz7068b7a2011-04-28 13:29:18 -0700189 * alarmtimer_fired - Handles alarm hrtimer being fired.
190 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800191 *
John Stultz180bf812011-04-28 12:58:11 -0700192 * 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.
John Stultzff3ead92011-01-11 09:42:13 -0800196 */
John Stultz7068b7a2011-04-28 13:29:18 -0700197static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800198{
John Stultzdae373b2012-09-13 19:12:16 -0400199 struct alarm *alarm = container_of(timer, struct alarm, timer);
200 struct alarm_base *base = &alarm_bases[alarm->type];
John Stultzff3ead92011-01-11 09:42:13 -0800201 unsigned long flags;
John Stultz7068b7a2011-04-28 13:29:18 -0700202 int ret = HRTIMER_NORESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700203 int restart = ALARMTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800204
205 spin_lock_irqsave(&base->lock, flags);
John Stultza65bcc12012-09-13 19:25:22 -0400206 alarmtimer_dequeue(base, alarm);
John Stultzdae373b2012-09-13 19:12:16 -0400207 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800208
John Stultzdae373b2012-09-13 19:12:16 -0400209 if (alarm->function)
210 restart = alarm->function(alarm, base->gettime());
John Stultzff3ead92011-01-11 09:42:13 -0800211
John Stultzdae373b2012-09-13 19:12:16 -0400212 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);
John Stultz7068b7a2011-04-28 13:29:18 -0700216 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800217 }
218 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800219
Baolin Wang4a057542016-11-28 14:35:21 -0800220 trace_alarmtimer_fired(alarm, base->gettime());
John Stultz7068b7a2011-04-28 13:29:18 -0700221 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800222
John Stultzff3ead92011-01-11 09:42:13 -0800223}
224
Todd Poynor6cffe002013-05-15 14:38:11 -0700225ktime_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}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200230EXPORT_SYMBOL_GPL(alarm_expires_remaining);
Todd Poynor6cffe002013-05-15 14:38:11 -0700231
John Stultz472647d2011-04-29 15:03:10 -0700232#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700233/**
John Stultzff3ead92011-01-11 09:42:13 -0800234 * 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 */
243static int alarmtimer_suspend(struct device *dev)
244{
Baolin Wang4a057542016-11-28 14:35:21 -0800245 ktime_t min, now, expires;
246 int i, ret, type;
John Stultzc008ba582011-06-16 18:27:09 -0700247 struct rtc_device *rtc;
Baolin Wang4a057542016-11-28 14:35:21 -0800248 unsigned long flags;
249 struct rtc_time tm;
John Stultzff3ead92011-01-11 09:42:13 -0800250
251 spin_lock_irqsave(&freezer_delta_lock, flags);
252 min = freezer_delta;
Baolin Wang4a057542016-11-28 14:35:21 -0800253 expires = freezer_expires;
254 type = freezer_alarmtype;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100255 freezer_delta = 0;
John Stultzff3ead92011-01-11 09:42:13 -0800256 spin_unlock_irqrestore(&freezer_delta_lock, flags);
257
John Stultz8bc0daf2011-07-14 18:35:13 -0700258 rtc = alarmtimer_get_rtcdev();
John Stultzff3ead92011-01-11 09:42:13 -0800259 /* If we have no rtcdev, just return */
John Stultzc008ba582011-06-16 18:27:09 -0700260 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800261 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());
Thomas Gleixner2456e852016-12-25 11:38:40 +0100275 if (!min || (delta < min)) {
Baolin Wang4a057542016-11-28 14:35:21 -0800276 expires = next->expires;
John Stultzff3ead92011-01-11 09:42:13 -0800277 min = delta;
Baolin Wang4a057542016-11-28 14:35:21 -0800278 type = i;
279 }
John Stultzff3ead92011-01-11 09:42:13 -0800280 }
Thomas Gleixner2456e852016-12-25 11:38:40 +0100281 if (min == 0)
John Stultzff3ead92011-01-11 09:42:13 -0800282 return 0;
283
Todd Poynor59a93c22012-08-09 00:37:27 -0700284 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
285 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
286 return -EBUSY;
287 }
John Stultzff3ead92011-01-11 09:42:13 -0800288
Baolin Wang4a057542016-11-28 14:35:21 -0800289 trace_alarmtimer_suspend(expires, type);
290
John Stultzff3ead92011-01-11 09:42:13 -0800291 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba582011-06-16 18:27:09 -0700292 rtc_timer_cancel(rtc, &rtctimer);
293 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800294 now = rtc_tm_to_ktime(tm);
295 now = ktime_add(now, min);
296
Todd Poynor59a93c22012-08-09 00:37:27 -0700297 /* Set alarm, if in the past reject suspend briefly to handle */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100298 ret = rtc_timer_start(rtc, &rtctimer, now, 0);
Todd Poynor59a93c22012-08-09 00:37:27 -0700299 if (ret < 0)
300 __pm_wakeup_event(ws, MSEC_PER_SEC);
301 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800302}
zhuo-haoa0e32132015-11-17 20:08:07 +0800303
304static 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
John Stultz472647d2011-04-29 15:03:10 -0700314#else
315static int alarmtimer_suspend(struct device *dev)
316{
317 return 0;
318}
zhuo-haoa0e32132015-11-17 20:08:07 +0800319
320static int alarmtimer_resume(struct device *dev)
321{
322 return 0;
323}
John Stultz472647d2011-04-29 15:03:10 -0700324#endif
John Stultzff3ead92011-01-11 09:42:13 -0800325
Thomas Gleixnerbd031432018-03-26 15:29:57 +0200326static 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
John Stultz180bf812011-04-28 12:58:11 -0700337/**
John Stultzff3ead92011-01-11 09:42:13 -0800338 * 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
John Stultzff3ead92011-01-11 09:42:13 -0800342 */
343void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
John Stultz4b413082011-08-10 10:37:59 -0700344 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
John Stultzff3ead92011-01-11 09:42:13 -0800345{
John Stultzdae373b2012-09-13 19:12:16 -0400346 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
Thomas Gleixnerbd031432018-03-26 15:29:57 +0200347 HRTIMER_MODE_ABS);
348 __alarm_init(alarm, type, function);
John Stultzff3ead92011-01-11 09:42:13 -0800349}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200350EXPORT_SYMBOL_GPL(alarm_init);
John Stultzff3ead92011-01-11 09:42:13 -0800351
John Stultz180bf812011-04-28 12:58:11 -0700352/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700353 * alarm_start - Sets an absolute alarm to fire
John Stultzff3ead92011-01-11 09:42:13 -0800354 * @alarm: ptr to alarm to set
355 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800356 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000357void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800358{
359 struct alarm_base *base = &alarm_bases[alarm->type];
360 unsigned long flags;
361
362 spin_lock_irqsave(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800363 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800364 alarmtimer_enqueue(base, alarm);
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000365 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
John Stultzff3ead92011-01-11 09:42:13 -0800366 spin_unlock_irqrestore(&base->lock, flags);
Baolin Wang4a057542016-11-28 14:35:21 -0800367
368 trace_alarmtimer_start(alarm, base->gettime());
John Stultzff3ead92011-01-11 09:42:13 -0800369}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200370EXPORT_SYMBOL_GPL(alarm_start);
John Stultzff3ead92011-01-11 09:42:13 -0800371
John Stultz180bf812011-04-28 12:58:11 -0700372/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700373 * 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 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000377void alarm_start_relative(struct alarm *alarm, ktime_t start)
Todd Poynor6cffe002013-05-15 14:38:11 -0700378{
379 struct alarm_base *base = &alarm_bases[alarm->type];
380
Thomas Gleixnerf4781e72017-05-30 23:15:34 +0200381 start = ktime_add_safe(start, base->gettime());
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000382 alarm_start(alarm, start);
Todd Poynor6cffe002013-05-15 14:38:11 -0700383}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200384EXPORT_SYMBOL_GPL(alarm_start_relative);
Todd Poynor6cffe002013-05-15 14:38:11 -0700385
386void 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}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200397EXPORT_SYMBOL_GPL(alarm_restart);
Todd Poynor6cffe002013-05-15 14:38:11 -0700398
399/**
John Stultz9082c462011-08-10 12:41:36 -0700400 * alarm_try_to_cancel - Tries to cancel an alarm timer
John Stultzff3ead92011-01-11 09:42:13 -0800401 * @alarm: ptr to alarm to be canceled
John Stultz9082c462011-08-10 12:41:36 -0700402 *
403 * Returns 1 if the timer was canceled, 0 if it was not running,
404 * and -1 if the callback was running
John Stultzff3ead92011-01-11 09:42:13 -0800405 */
John Stultz9082c462011-08-10 12:41:36 -0700406int alarm_try_to_cancel(struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800407{
408 struct alarm_base *base = &alarm_bases[alarm->type];
409 unsigned long flags;
John Stultzdae373b2012-09-13 19:12:16 -0400410 int ret;
411
John Stultzff3ead92011-01-11 09:42:13 -0800412 spin_lock_irqsave(&base->lock, flags);
John Stultzdae373b2012-09-13 19:12:16 -0400413 ret = hrtimer_try_to_cancel(&alarm->timer);
414 if (ret >= 0)
John Stultza65bcc12012-09-13 19:25:22 -0400415 alarmtimer_dequeue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800416 spin_unlock_irqrestore(&base->lock, flags);
Baolin Wang4a057542016-11-28 14:35:21 -0800417
418 trace_alarmtimer_cancel(alarm, base->gettime());
John Stultz9082c462011-08-10 12:41:36 -0700419 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800420}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200421EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
John Stultzff3ead92011-01-11 09:42:13 -0800422
423
John Stultz9082c462011-08-10 12:41:36 -0700424/**
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 */
430int 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}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200439EXPORT_SYMBOL_GPL(alarm_cancel);
John Stultz9082c462011-08-10 12:41:36 -0700440
John Stultzdce75a82011-08-10 11:31:03 -0700441
442u64 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
Thomas Gleixner2456e852016-12-25 11:38:40 +0100449 if (delta < 0)
John Stultzdce75a82011-08-10 11:31:03 -0700450 return 0;
451
Thomas Gleixner2456e852016-12-25 11:38:40 +0100452 if (unlikely(delta >= interval)) {
John Stultzdce75a82011-08-10 11:31:03 -0700453 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
Thomas Gleixner2456e852016-12-25 11:38:40 +0100460 if (alarm->node.expires > now)
John Stultzdce75a82011-08-10 11:31:03 -0700461 return overrun;
462 /*
463 * This (and the ktime_add() below) is the
464 * correction for exact:
465 */
466 overrun++;
467 }
468
Thomas Gleixnerf4781e72017-05-30 23:15:34 +0200469 alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
John Stultzdce75a82011-08-10 11:31:03 -0700470 return overrun;
471}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200472EXPORT_SYMBOL_GPL(alarm_forward);
John Stultzdce75a82011-08-10 11:31:03 -0700473
Todd Poynor6cffe002013-05-15 14:38:11 -0700474u64 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}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200480EXPORT_SYMBOL_GPL(alarm_forward_now);
John Stultzdce75a82011-08-10 11:31:03 -0700481
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300482#ifdef CONFIG_POSIX_TIMERS
483
484static 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}
John Stultzdce75a82011-08-10 11:31:03 -0700514
John Stultz180bf812011-04-28 12:58:11 -0700515/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800516 * clock2alarm - helper that converts from clockid to alarmtypes
517 * @clockid: clockid.
John Stultz9a7adcf52011-01-11 09:54:33 -0800518 */
519static 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
John Stultz180bf812011-04-28 12:58:11 -0700528/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800529 * alarm_handle_timer - Callback for posix timers
530 * @alarm: alarm that fired
531 *
532 * Posix timer callback for expired alarm timers.
533 */
John Stultz4b413082011-08-10 10:37:59 -0700534static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
535 ktime_t now)
John Stultz9a7adcf52011-01-11 09:54:33 -0800536{
537 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200538 it.alarm.alarmtimer);
Richard Larocque474e941b2014-09-09 18:31:05 -0700539 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200540 unsigned long flags;
541 int si_private = 0;
Richard Larocque474e941b2014-09-09 18:31:05 -0700542
543 spin_lock_irqsave(&ptr->it_lock, flags);
John Stultz4b413082011-08-10 10:37:59 -0700544
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200545 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;
Richard Larocque474e941b2014-09-09 18:31:05 -0700557 result = ALARMTIMER_RESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700558 }
Richard Larocque474e941b2014-09-09 18:31:05 -0700559 spin_unlock_irqrestore(&ptr->it_lock, flags);
560
561 return result;
John Stultz9a7adcf52011-01-11 09:54:33 -0800562}
563
John Stultz180bf812011-04-28 12:58:11 -0700564/**
Thomas Gleixnerb3db80f2017-05-30 23:15:54 +0200565 * alarm_timer_rearm - Posix timer callback for rearming timer
566 * @timr: Pointer to the posixtimer data struct
567 */
568static 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/**
Thomas Gleixnere7561f12017-05-30 23:15:55 +0200577 * 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 */
Thomas Gleixner6fec64e2018-06-26 15:21:31 +0200581static s64 alarm_timer_forward(struct k_itimer *timr, ktime_t now)
Thomas Gleixnere7561f12017-05-30 23:15:55 +0200582{
583 struct alarm *alarm = &timr->it.alarm.alarmtimer;
584
Thomas Gleixner6fec64e2018-06-26 15:21:31 +0200585 return alarm_forward(alarm, timr->it_interval, now);
Thomas Gleixnere7561f12017-05-30 23:15:55 +0200586}
587
588/**
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200589 * 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 */
593static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
594{
595 struct alarm *alarm = &timr->it.alarm.alarmtimer;
596
Andrei Vagin07d7e122019-04-07 21:15:42 -0700597 return ktime_sub(alarm->node.expires, now);
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200598}
599
600/**
Thomas Gleixnere344c9e2017-05-30 23:15:57 +0200601 * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
602 * @timr: Pointer to the posixtimer data struct
603 */
604static int alarm_timer_try_to_cancel(struct k_itimer *timr)
605{
606 return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
607}
608
609/**
Thomas Gleixnerb3bf6f32017-05-30 23:15:58 +0200610 * 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 */
616static 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/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800631 * 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 */
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700637static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
John Stultz9a7adcf52011-01-11 09:54:33 -0800638{
John Stultz1c6b39a2011-06-16 18:47:37 -0700639 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400640 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700641
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000642 tp->tv_sec = 0;
643 tp->tv_nsec = hrtimer_resolution;
644 return 0;
John Stultz9a7adcf52011-01-11 09:54:33 -0800645}
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 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700654static int alarm_clock_get(clockid_t which_clock, struct timespec64 *tp)
John Stultz9a7adcf52011-01-11 09:54:33 -0800655{
656 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
657
John Stultz1c6b39a2011-06-16 18:47:37 -0700658 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400659 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700660
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700661 *tp = ktime_to_timespec64(base->gettime());
John Stultz9a7adcf52011-01-11 09:54:33 -0800662 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 */
671static int alarm_timer_create(struct k_itimer *new_timer)
672{
673 enum alarmtimer_type type;
John Stultz9a7adcf52011-01-11 09:54:33 -0800674
John Stultz1c6b39a2011-06-16 18:47:37 -0700675 if (!alarmtimer_get_rtcdev())
676 return -ENOTSUPP;
677
John Stultz9a7adcf52011-01-11 09:54:33 -0800678 if (!capable(CAP_WAKE_ALARM))
679 return -EPERM;
680
681 type = clock2alarm(new_timer->it_clock);
John Stultz9e264762011-08-10 12:09:24 -0700682 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf52011-01-11 09:54:33 -0800683 return 0;
684}
685
686/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800687 * 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 */
John Stultz4b413082011-08-10 10:37:59 -0700692static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
693 ktime_t now)
John Stultz9a7adcf52011-01-11 09:54:33 -0800694{
695 struct task_struct *task = (struct task_struct *)alarm->data;
696
697 alarm->data = NULL;
698 if (task)
699 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700700 return ALARMTIMER_NORESTART;
John Stultz9a7adcf52011-01-11 09:54:33 -0800701}
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 */
Al Viro15f27ce2017-06-07 09:42:27 +0100710static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
711 enum alarmtimer_type type)
John Stultz9a7adcf52011-01-11 09:54:33 -0800712{
Al Viroedbeda42017-06-07 09:42:31 +0100713 struct restart_block *restart;
John Stultz9a7adcf52011-01-11 09:54:33 -0800714 alarm->data = (void *)current;
715 do {
716 set_current_state(TASK_INTERRUPTIBLE);
John Stultz9e264762011-08-10 12:09:24 -0700717 alarm_start(alarm, absexp);
John Stultz9a7adcf52011-01-11 09:54:33 -0800718 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
Thomas Gleixnerbd031432018-03-26 15:29:57 +0200726 destroy_hrtimer_on_stack(&alarm->timer);
727
Al Viro15f27ce2017-06-07 09:42:27 +0100728 if (!alarm->data)
John Stultz9a7adcf52011-01-11 09:54:33 -0800729 return 0;
John Stultz9a7adcf52011-01-11 09:54:33 -0800730
Al Viro15f27ce2017-06-07 09:42:27 +0100731 if (freezing(current))
732 alarmtimer_freezerset(absexp, type);
Al Viroedbeda42017-06-07 09:42:31 +0100733 restart = &current->restart_block;
734 if (restart->nanosleep.type != TT_NONE) {
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -0700735 struct timespec64 rmt;
Al Viro15f27ce2017-06-07 09:42:27 +0100736 ktime_t rem;
John Stultz9a7adcf52011-01-11 09:54:33 -0800737
Al Viro15f27ce2017-06-07 09:42:27 +0100738 rem = ktime_sub(absexp, alarm_bases[type].gettime());
John Stultz9a7adcf52011-01-11 09:54:33 -0800739
Al Viro15f27ce2017-06-07 09:42:27 +0100740 if (rem <= 0)
741 return 0;
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -0700742 rmt = ktime_to_timespec64(rem);
Al Viro15f27ce2017-06-07 09:42:27 +0100743
Al Viroce41aaf2017-06-07 09:42:32 +0100744 return nanosleep_copyout(restart, &rmt);
Al Viro15f27ce2017-06-07 09:42:27 +0100745 }
746 return -ERESTART_RESTARTBLOCK;
John Stultz9a7adcf52011-01-11 09:54:33 -0800747}
748
Thomas Gleixnerbd031432018-03-26 15:29:57 +0200749static void
750alarm_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
John Stultz9a7adcf52011-01-11 09:54:33 -0800758/**
759 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
760 * @restart: ptr to restart block
761 *
762 * Handles restarted clock_nanosleep calls
763 */
764static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
765{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200766 enum alarmtimer_type type = restart->nanosleep.clockid;
Al Viro15f27ce2017-06-07 09:42:27 +0100767 ktime_t exp = restart->nanosleep.expires;
John Stultz9a7adcf52011-01-11 09:54:33 -0800768 struct alarm alarm;
John Stultz9a7adcf52011-01-11 09:54:33 -0800769
Thomas Gleixnerbd031432018-03-26 15:29:57 +0200770 alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
John Stultz9a7adcf52011-01-11 09:54:33 -0800771
Al Viro15f27ce2017-06-07 09:42:27 +0100772 return alarmtimer_do_nsleep(&alarm, exp, type);
John Stultz9a7adcf52011-01-11 09:54:33 -0800773}
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 */
784static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner938e7cf2017-06-13 23:34:33 +0200785 const struct timespec64 *tsreq)
John Stultz9a7adcf52011-01-11 09:54:33 -0800786{
787 enum alarmtimer_type type = clock2alarm(which_clock);
Al Viro15f27ce2017-06-07 09:42:27 +0100788 struct restart_block *restart = &current->restart_block;
John Stultz9a7adcf52011-01-11 09:54:33 -0800789 struct alarm alarm;
790 ktime_t exp;
791 int ret = 0;
John Stultz9a7adcf52011-01-11 09:54:33 -0800792
John Stultz1c6b39a2011-06-16 18:47:37 -0700793 if (!alarmtimer_get_rtcdev())
794 return -ENOTSUPP;
795
John Stultz16927772014-07-07 14:06:11 -0700796 if (flags & ~TIMER_ABSTIME)
797 return -EINVAL;
798
John Stultz9a7adcf52011-01-11 09:54:33 -0800799 if (!capable(CAP_WAKE_ALARM))
800 return -EPERM;
801
Thomas Gleixnerbd031432018-03-26 15:29:57 +0200802 alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
John Stultz9a7adcf52011-01-11 09:54:33 -0800803
Deepa Dinamaniad196382017-03-26 12:04:18 -0700804 exp = timespec64_to_ktime(*tsreq);
John Stultz9a7adcf52011-01-11 09:54:33 -0800805 /* Convert (if necessary) to absolute time */
806 if (flags != TIMER_ABSTIME) {
807 ktime_t now = alarm_bases[type].gettime();
Thomas Gleixner5f936e12018-07-02 09:34:29 +0200808
809 exp = ktime_add_safe(now, exp);
John Stultz9a7adcf52011-01-11 09:54:33 -0800810 }
811
Al Viro15f27ce2017-06-07 09:42:27 +0100812 ret = alarmtimer_do_nsleep(&alarm, exp, type);
813 if (ret != -ERESTART_RESTARTBLOCK)
814 return ret;
John Stultz9a7adcf52011-01-11 09:54:33 -0800815
816 /* abs timers don't set remaining time or restart */
Al Viro15f27ce2017-06-07 09:42:27 +0100817 if (flags == TIMER_ABSTIME)
818 return -ERESTARTNOHAND;
John Stultz9a7adcf52011-01-11 09:54:33 -0800819
John Stultz9a7adcf52011-01-11 09:54:33 -0800820 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200821 restart->nanosleep.clockid = type;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100822 restart->nanosleep.expires = exp;
John Stultz9a7adcf52011-01-11 09:54:33 -0800823 return ret;
824}
John Stultzff3ead92011-01-11 09:42:13 -0800825
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300826const struct k_clock alarm_clock = {
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200827 .clock_getres = alarm_clock_getres,
828 .clock_get = alarm_clock_get,
829 .timer_create = alarm_timer_create,
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200830 .timer_set = common_timer_set,
831 .timer_del = common_timer_del,
832 .timer_get = common_timer_get,
Thomas Gleixnerb3bf6f32017-05-30 23:15:58 +0200833 .timer_arm = alarm_timer_arm,
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200834 .timer_rearm = alarm_timer_rearm,
835 .timer_forward = alarm_timer_forward,
836 .timer_remaining = alarm_timer_remaining,
Thomas Gleixnere344c9e2017-05-30 23:15:57 +0200837 .timer_try_to_cancel = alarm_timer_try_to_cancel,
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200838 .nsleep = alarm_timer_nsleep,
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300839};
840#endif /* CONFIG_POSIX_TIMERS */
841
John Stultzff3ead92011-01-11 09:42:13 -0800842
843/* Suspend hook structures */
844static const struct dev_pm_ops alarmtimer_pm_ops = {
845 .suspend = alarmtimer_suspend,
zhuo-haoa0e32132015-11-17 20:08:07 +0800846 .resume = alarmtimer_resume,
John Stultzff3ead92011-01-11 09:42:13 -0800847};
848
849static 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 */
862static int __init alarmtimer_init(void)
863{
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200864 struct platform_device *pdev;
John Stultzff3ead92011-01-11 09:42:13 -0800865 int error = 0;
866 int i;
John Stultz9a7adcf52011-01-11 09:54:33 -0800867
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100868 alarmtimer_rtc_timer_init();
John Stultzad30dfa2012-03-23 15:52:25 -0700869
John Stultzff3ead92011-01-11 09:42:13 -0800870 /* 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);
John Stultzff3ead92011-01-11 09:42:13 -0800878 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700879
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200880 error = alarmtimer_rtc_interface_setup();
881 if (error)
882 return error;
John Stultzff3ead92011-01-11 09:42:13 -0800883
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200884 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
895out_drv:
896 platform_driver_unregister(&alarmtimer_driver);
897out_if:
898 alarmtimer_rtc_interface_remove();
John Stultzff3ead92011-01-11 09:42:13 -0800899 return error;
900}
901device_initcall(alarmtimer_init);