blob: 374bd855a48874ca697ef0b0463c5140b90da30b [file] [log] [blame]
John Stultzff3ead92011-01-11 09:42:13 -08001/*
2 * Alarmtimer interface
3 *
4 * This interface provides a timer which is similarto hrtimers,
5 * but triggers a RTC alarm if the box is suspend.
6 *
7 * This interface is influenced by the Android RTC Alarm timer
8 * interface.
9 *
10 * Copyright (C) 2010 IBM Corperation
11 *
12 * Author: John Stultz <john.stultz@linaro.org>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18#include <linux/time.h>
19#include <linux/hrtimer.h>
20#include <linux/timerqueue.h>
21#include <linux/rtc.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010022#include <linux/sched/signal.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010023#include <linux/sched/debug.h>
John Stultzff3ead92011-01-11 09:42:13 -080024#include <linux/alarmtimer.h>
25#include <linux/mutex.h>
26#include <linux/platform_device.h>
27#include <linux/posix-timers.h>
28#include <linux/workqueue.h>
29#include <linux/freezer.h>
30
Thomas Gleixnerbab0aae92017-05-30 23:15:41 +020031#include "posix-timers.h"
32
Baolin Wang4a057542016-11-28 14:35:21 -080033#define CREATE_TRACE_POINTS
34#include <trace/events/alarmtimer.h>
35
John Stultz180bf812011-04-28 12:58:11 -070036/**
37 * struct alarm_base - Alarm timer bases
38 * @lock: Lock for syncrhonized access to the base
39 * @timerqueue: Timerqueue head managing the list of events
John Stultz180bf812011-04-28 12:58:11 -070040 * @gettime: Function to read the time correlating to the base
41 * @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;
John Stultzff3ead92011-01-11 09:42:13 -080046 ktime_t (*gettime)(void);
47 clockid_t base_clockid;
John Stultzff3ead92011-01-11 09:42:13 -080048} alarm_bases[ALARM_NUMTYPE];
49
Thomas Gleixnerb6b3b802017-05-27 12:23:47 +020050#if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
Baolin Wang4a057542016-11-28 14:35:21 -080051/* freezer information to handle clock_nanosleep triggered wakeups */
52static enum alarmtimer_type freezer_alarmtype;
53static ktime_t freezer_expires;
John Stultzc008ba582011-06-16 18:27:09 -070054static ktime_t freezer_delta;
55static DEFINE_SPINLOCK(freezer_delta_lock);
Thomas Gleixnerb6b3b802017-05-27 12:23:47 +020056#endif
John Stultzc008ba582011-06-16 18:27:09 -070057
Todd Poynor59a93c22012-08-09 00:37:27 -070058static struct wakeup_source *ws;
59
John Stultz472647d2011-04-29 15:03:10 -070060#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -070061/* rtc timer and device for setting alarm wakeups at suspend */
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +010062static struct rtc_timer rtctimer;
John Stultzff3ead92011-01-11 09:42:13 -080063static struct rtc_device *rtcdev;
John Stultzc008ba582011-06-16 18:27:09 -070064static DEFINE_SPINLOCK(rtcdev_lock);
John Stultzff3ead92011-01-11 09:42:13 -080065
John Stultzc008ba582011-06-16 18:27:09 -070066/**
John Stultzc008ba582011-06-16 18:27:09 -070067 * alarmtimer_get_rtcdev - Return selected rtcdevice
68 *
69 * This function returns the rtc device to use for wakealarms.
70 * If one has not already been chosen, it checks to see if a
71 * functional rtc device is available.
72 */
John Stultz57c498f2012-04-20 12:31:45 -070073struct rtc_device *alarmtimer_get_rtcdev(void)
John Stultzc008ba582011-06-16 18:27:09 -070074{
John Stultzc008ba582011-06-16 18:27:09 -070075 unsigned long flags;
76 struct rtc_device *ret;
77
78 spin_lock_irqsave(&rtcdev_lock, flags);
John Stultzc008ba582011-06-16 18:27:09 -070079 ret = rtcdev;
80 spin_unlock_irqrestore(&rtcdev_lock, flags);
81
82 return ret;
83}
Pramod Gurav71d5d2b2014-06-13 11:49:42 +053084EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
John Stultz8bc0daf2011-07-14 18:35:13 -070085
86static int alarmtimer_rtc_add_device(struct device *dev,
87 struct class_interface *class_intf)
88{
89 unsigned long flags;
90 struct rtc_device *rtc = to_rtc_device(dev);
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 spin_lock_irqsave(&rtcdev_lock, flags);
101 if (!rtcdev) {
102 rtcdev = rtc;
103 /* hold a reference so it doesn't go away */
104 get_device(dev);
105 }
106 spin_unlock_irqrestore(&rtcdev_lock, flags);
107 return 0;
108}
109
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100110static inline void alarmtimer_rtc_timer_init(void)
111{
112 rtc_timer_init(&rtctimer, NULL, NULL);
113}
114
John Stultz8bc0daf2011-07-14 18:35:13 -0700115static struct class_interface alarmtimer_rtc_interface = {
116 .add_dev = &alarmtimer_rtc_add_device,
117};
118
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200119static int alarmtimer_rtc_interface_setup(void)
John Stultz8bc0daf2011-07-14 18:35:13 -0700120{
121 alarmtimer_rtc_interface.class = rtc_class;
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200122 return class_interface_register(&alarmtimer_rtc_interface);
123}
124static void alarmtimer_rtc_interface_remove(void)
125{
126 class_interface_unregister(&alarmtimer_rtc_interface);
John Stultz8bc0daf2011-07-14 18:35:13 -0700127}
John Stultz1c6b39a2011-06-16 18:47:37 -0700128#else
John Stultz57c498f2012-04-20 12:31:45 -0700129struct rtc_device *alarmtimer_get_rtcdev(void)
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200130{
131 return NULL;
132}
133#define rtcdev (NULL)
134static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
135static inline void alarmtimer_rtc_interface_remove(void) { }
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100136static inline void alarmtimer_rtc_timer_init(void) { }
John Stultzc008ba582011-06-16 18:27:09 -0700137#endif
John Stultzff3ead92011-01-11 09:42:13 -0800138
John Stultz180bf812011-04-28 12:58:11 -0700139/**
John Stultzff3ead92011-01-11 09:42:13 -0800140 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
141 * @base: pointer to the base where the timer is being run
142 * @alarm: pointer to alarm being enqueued.
143 *
John Stultzdae373b2012-09-13 19:12:16 -0400144 * Adds alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800145 *
146 * Must hold base->lock when calling.
147 */
148static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
149{
John Stultzdae373b2012-09-13 19:12:16 -0400150 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
151 timerqueue_del(&base->timerqueue, &alarm->node);
152
John Stultzff3ead92011-01-11 09:42:13 -0800153 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700154 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800155}
156
John Stultz180bf812011-04-28 12:58:11 -0700157/**
John Stultza65bcc12012-09-13 19:25:22 -0400158 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800159 * @base: pointer to the base where the timer is running
160 * @alarm: pointer to alarm being removed
161 *
John Stultzdae373b2012-09-13 19:12:16 -0400162 * Removes alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800163 *
164 * Must hold base->lock when calling.
165 */
John Stultza65bcc12012-09-13 19:25:22 -0400166static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800167{
John Stultza28cde82011-08-10 12:30:21 -0700168 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
169 return;
170
John Stultzff3ead92011-01-11 09:42:13 -0800171 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700172 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800173}
174
John Stultz7068b7a2011-04-28 13:29:18 -0700175
John Stultz180bf812011-04-28 12:58:11 -0700176/**
John Stultz7068b7a2011-04-28 13:29:18 -0700177 * alarmtimer_fired - Handles alarm hrtimer being fired.
178 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800179 *
John Stultz180bf812011-04-28 12:58:11 -0700180 * When a alarm timer fires, this runs through the timerqueue to
181 * see which alarms expired, and runs those. If there are more alarm
182 * timers queued for the future, we set the hrtimer to fire when
183 * when the next future alarm timer expires.
John Stultzff3ead92011-01-11 09:42:13 -0800184 */
John Stultz7068b7a2011-04-28 13:29:18 -0700185static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800186{
John Stultzdae373b2012-09-13 19:12:16 -0400187 struct alarm *alarm = container_of(timer, struct alarm, timer);
188 struct alarm_base *base = &alarm_bases[alarm->type];
John Stultzff3ead92011-01-11 09:42:13 -0800189 unsigned long flags;
John Stultz7068b7a2011-04-28 13:29:18 -0700190 int ret = HRTIMER_NORESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700191 int restart = ALARMTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800192
193 spin_lock_irqsave(&base->lock, flags);
John Stultza65bcc12012-09-13 19:25:22 -0400194 alarmtimer_dequeue(base, alarm);
John Stultzdae373b2012-09-13 19:12:16 -0400195 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800196
John Stultzdae373b2012-09-13 19:12:16 -0400197 if (alarm->function)
198 restart = alarm->function(alarm, base->gettime());
John Stultzff3ead92011-01-11 09:42:13 -0800199
John Stultzdae373b2012-09-13 19:12:16 -0400200 spin_lock_irqsave(&base->lock, flags);
201 if (restart != ALARMTIMER_NORESTART) {
202 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
203 alarmtimer_enqueue(base, alarm);
John Stultz7068b7a2011-04-28 13:29:18 -0700204 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800205 }
206 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800207
Baolin Wang4a057542016-11-28 14:35:21 -0800208 trace_alarmtimer_fired(alarm, base->gettime());
John Stultz7068b7a2011-04-28 13:29:18 -0700209 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800210
John Stultzff3ead92011-01-11 09:42:13 -0800211}
212
Todd Poynor6cffe002013-05-15 14:38:11 -0700213ktime_t alarm_expires_remaining(const struct alarm *alarm)
214{
215 struct alarm_base *base = &alarm_bases[alarm->type];
216 return ktime_sub(alarm->node.expires, base->gettime());
217}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200218EXPORT_SYMBOL_GPL(alarm_expires_remaining);
Todd Poynor6cffe002013-05-15 14:38:11 -0700219
John Stultz472647d2011-04-29 15:03:10 -0700220#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700221/**
John Stultzff3ead92011-01-11 09:42:13 -0800222 * alarmtimer_suspend - Suspend time callback
223 * @dev: unused
224 * @state: unused
225 *
226 * When we are going into suspend, we look through the bases
227 * to see which is the soonest timer to expire. We then
228 * set an rtc timer to fire that far into the future, which
229 * will wake us from suspend.
230 */
231static int alarmtimer_suspend(struct device *dev)
232{
Baolin Wang4a057542016-11-28 14:35:21 -0800233 ktime_t min, now, expires;
234 int i, ret, type;
John Stultzc008ba582011-06-16 18:27:09 -0700235 struct rtc_device *rtc;
Baolin Wang4a057542016-11-28 14:35:21 -0800236 unsigned long flags;
237 struct rtc_time tm;
John Stultzff3ead92011-01-11 09:42:13 -0800238
239 spin_lock_irqsave(&freezer_delta_lock, flags);
240 min = freezer_delta;
Baolin Wang4a057542016-11-28 14:35:21 -0800241 expires = freezer_expires;
242 type = freezer_alarmtype;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100243 freezer_delta = 0;
John Stultzff3ead92011-01-11 09:42:13 -0800244 spin_unlock_irqrestore(&freezer_delta_lock, flags);
245
John Stultz8bc0daf2011-07-14 18:35:13 -0700246 rtc = alarmtimer_get_rtcdev();
John Stultzff3ead92011-01-11 09:42:13 -0800247 /* If we have no rtcdev, just return */
John Stultzc008ba582011-06-16 18:27:09 -0700248 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800249 return 0;
250
251 /* Find the soonest timer to expire*/
252 for (i = 0; i < ALARM_NUMTYPE; i++) {
253 struct alarm_base *base = &alarm_bases[i];
254 struct timerqueue_node *next;
255 ktime_t delta;
256
257 spin_lock_irqsave(&base->lock, flags);
258 next = timerqueue_getnext(&base->timerqueue);
259 spin_unlock_irqrestore(&base->lock, flags);
260 if (!next)
261 continue;
262 delta = ktime_sub(next->expires, base->gettime());
Thomas Gleixner2456e852016-12-25 11:38:40 +0100263 if (!min || (delta < min)) {
Baolin Wang4a057542016-11-28 14:35:21 -0800264 expires = next->expires;
John Stultzff3ead92011-01-11 09:42:13 -0800265 min = delta;
Baolin Wang4a057542016-11-28 14:35:21 -0800266 type = i;
267 }
John Stultzff3ead92011-01-11 09:42:13 -0800268 }
Thomas Gleixner2456e852016-12-25 11:38:40 +0100269 if (min == 0)
John Stultzff3ead92011-01-11 09:42:13 -0800270 return 0;
271
Todd Poynor59a93c22012-08-09 00:37:27 -0700272 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
273 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
274 return -EBUSY;
275 }
John Stultzff3ead92011-01-11 09:42:13 -0800276
Baolin Wang4a057542016-11-28 14:35:21 -0800277 trace_alarmtimer_suspend(expires, type);
278
John Stultzff3ead92011-01-11 09:42:13 -0800279 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba582011-06-16 18:27:09 -0700280 rtc_timer_cancel(rtc, &rtctimer);
281 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800282 now = rtc_tm_to_ktime(tm);
283 now = ktime_add(now, min);
284
Todd Poynor59a93c22012-08-09 00:37:27 -0700285 /* Set alarm, if in the past reject suspend briefly to handle */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100286 ret = rtc_timer_start(rtc, &rtctimer, now, 0);
Todd Poynor59a93c22012-08-09 00:37:27 -0700287 if (ret < 0)
288 __pm_wakeup_event(ws, MSEC_PER_SEC);
289 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800290}
zhuo-haoa0e32132015-11-17 20:08:07 +0800291
292static int alarmtimer_resume(struct device *dev)
293{
294 struct rtc_device *rtc;
295
296 rtc = alarmtimer_get_rtcdev();
297 if (rtc)
298 rtc_timer_cancel(rtc, &rtctimer);
299 return 0;
300}
301
John Stultz472647d2011-04-29 15:03:10 -0700302#else
303static int alarmtimer_suspend(struct device *dev)
304{
305 return 0;
306}
zhuo-haoa0e32132015-11-17 20:08:07 +0800307
308static int alarmtimer_resume(struct device *dev)
309{
310 return 0;
311}
John Stultz472647d2011-04-29 15:03:10 -0700312#endif
John Stultzff3ead92011-01-11 09:42:13 -0800313
John Stultz180bf812011-04-28 12:58:11 -0700314/**
John Stultzff3ead92011-01-11 09:42:13 -0800315 * alarm_init - Initialize an alarm structure
316 * @alarm: ptr to alarm to be initialized
317 * @type: the type of the alarm
318 * @function: callback that is run when the alarm fires
John Stultzff3ead92011-01-11 09:42:13 -0800319 */
320void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
John Stultz4b413082011-08-10 10:37:59 -0700321 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
John Stultzff3ead92011-01-11 09:42:13 -0800322{
323 timerqueue_init(&alarm->node);
John Stultzdae373b2012-09-13 19:12:16 -0400324 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
325 HRTIMER_MODE_ABS);
326 alarm->timer.function = alarmtimer_fired;
John Stultzff3ead92011-01-11 09:42:13 -0800327 alarm->function = function;
328 alarm->type = type;
John Stultza28cde82011-08-10 12:30:21 -0700329 alarm->state = ALARMTIMER_STATE_INACTIVE;
John Stultzff3ead92011-01-11 09:42:13 -0800330}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200331EXPORT_SYMBOL_GPL(alarm_init);
John Stultzff3ead92011-01-11 09:42:13 -0800332
John Stultz180bf812011-04-28 12:58:11 -0700333/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700334 * alarm_start - Sets an absolute alarm to fire
John Stultzff3ead92011-01-11 09:42:13 -0800335 * @alarm: ptr to alarm to set
336 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800337 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000338void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800339{
340 struct alarm_base *base = &alarm_bases[alarm->type];
341 unsigned long flags;
342
343 spin_lock_irqsave(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800344 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800345 alarmtimer_enqueue(base, alarm);
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000346 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
John Stultzff3ead92011-01-11 09:42:13 -0800347 spin_unlock_irqrestore(&base->lock, flags);
Baolin Wang4a057542016-11-28 14:35:21 -0800348
349 trace_alarmtimer_start(alarm, base->gettime());
John Stultzff3ead92011-01-11 09:42:13 -0800350}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200351EXPORT_SYMBOL_GPL(alarm_start);
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_relative - Sets a relative alarm to fire
355 * @alarm: ptr to alarm to set
356 * @start: time relative to now to run the alarm
357 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000358void alarm_start_relative(struct alarm *alarm, ktime_t start)
Todd Poynor6cffe002013-05-15 14:38:11 -0700359{
360 struct alarm_base *base = &alarm_bases[alarm->type];
361
Thomas Gleixnerf4781e72017-05-30 23:15:34 +0200362 start = ktime_add_safe(start, base->gettime());
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000363 alarm_start(alarm, start);
Todd Poynor6cffe002013-05-15 14:38:11 -0700364}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200365EXPORT_SYMBOL_GPL(alarm_start_relative);
Todd Poynor6cffe002013-05-15 14:38:11 -0700366
367void alarm_restart(struct alarm *alarm)
368{
369 struct alarm_base *base = &alarm_bases[alarm->type];
370 unsigned long flags;
371
372 spin_lock_irqsave(&base->lock, flags);
373 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
374 hrtimer_restart(&alarm->timer);
375 alarmtimer_enqueue(base, alarm);
376 spin_unlock_irqrestore(&base->lock, flags);
377}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200378EXPORT_SYMBOL_GPL(alarm_restart);
Todd Poynor6cffe002013-05-15 14:38:11 -0700379
380/**
John Stultz9082c462011-08-10 12:41:36 -0700381 * alarm_try_to_cancel - Tries to cancel an alarm timer
John Stultzff3ead92011-01-11 09:42:13 -0800382 * @alarm: ptr to alarm to be canceled
John Stultz9082c462011-08-10 12:41:36 -0700383 *
384 * Returns 1 if the timer was canceled, 0 if it was not running,
385 * and -1 if the callback was running
John Stultzff3ead92011-01-11 09:42:13 -0800386 */
John Stultz9082c462011-08-10 12:41:36 -0700387int alarm_try_to_cancel(struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800388{
389 struct alarm_base *base = &alarm_bases[alarm->type];
390 unsigned long flags;
John Stultzdae373b2012-09-13 19:12:16 -0400391 int ret;
392
John Stultzff3ead92011-01-11 09:42:13 -0800393 spin_lock_irqsave(&base->lock, flags);
John Stultzdae373b2012-09-13 19:12:16 -0400394 ret = hrtimer_try_to_cancel(&alarm->timer);
395 if (ret >= 0)
John Stultza65bcc12012-09-13 19:25:22 -0400396 alarmtimer_dequeue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800397 spin_unlock_irqrestore(&base->lock, flags);
Baolin Wang4a057542016-11-28 14:35:21 -0800398
399 trace_alarmtimer_cancel(alarm, base->gettime());
John Stultz9082c462011-08-10 12:41:36 -0700400 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800401}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200402EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
John Stultzff3ead92011-01-11 09:42:13 -0800403
404
John Stultz9082c462011-08-10 12:41:36 -0700405/**
406 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
407 * @alarm: ptr to alarm to be canceled
408 *
409 * Returns 1 if the timer was canceled, 0 if it was not active.
410 */
411int alarm_cancel(struct alarm *alarm)
412{
413 for (;;) {
414 int ret = alarm_try_to_cancel(alarm);
415 if (ret >= 0)
416 return ret;
417 cpu_relax();
418 }
419}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200420EXPORT_SYMBOL_GPL(alarm_cancel);
John Stultz9082c462011-08-10 12:41:36 -0700421
John Stultzdce75a82011-08-10 11:31:03 -0700422
423u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
424{
425 u64 overrun = 1;
426 ktime_t delta;
427
428 delta = ktime_sub(now, alarm->node.expires);
429
Thomas Gleixner2456e852016-12-25 11:38:40 +0100430 if (delta < 0)
John Stultzdce75a82011-08-10 11:31:03 -0700431 return 0;
432
Thomas Gleixner2456e852016-12-25 11:38:40 +0100433 if (unlikely(delta >= interval)) {
John Stultzdce75a82011-08-10 11:31:03 -0700434 s64 incr = ktime_to_ns(interval);
435
436 overrun = ktime_divns(delta, incr);
437
438 alarm->node.expires = ktime_add_ns(alarm->node.expires,
439 incr*overrun);
440
Thomas Gleixner2456e852016-12-25 11:38:40 +0100441 if (alarm->node.expires > now)
John Stultzdce75a82011-08-10 11:31:03 -0700442 return overrun;
443 /*
444 * This (and the ktime_add() below) is the
445 * correction for exact:
446 */
447 overrun++;
448 }
449
Thomas Gleixnerf4781e72017-05-30 23:15:34 +0200450 alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
John Stultzdce75a82011-08-10 11:31:03 -0700451 return overrun;
452}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200453EXPORT_SYMBOL_GPL(alarm_forward);
John Stultzdce75a82011-08-10 11:31:03 -0700454
Todd Poynor6cffe002013-05-15 14:38:11 -0700455u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
456{
457 struct alarm_base *base = &alarm_bases[alarm->type];
458
459 return alarm_forward(alarm, base->gettime(), interval);
460}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200461EXPORT_SYMBOL_GPL(alarm_forward_now);
John Stultzdce75a82011-08-10 11:31:03 -0700462
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300463#ifdef CONFIG_POSIX_TIMERS
464
465static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
466{
467 struct alarm_base *base;
468 unsigned long flags;
469 ktime_t delta;
470
471 switch(type) {
472 case ALARM_REALTIME:
473 base = &alarm_bases[ALARM_REALTIME];
474 type = ALARM_REALTIME_FREEZER;
475 break;
476 case ALARM_BOOTTIME:
477 base = &alarm_bases[ALARM_BOOTTIME];
478 type = ALARM_BOOTTIME_FREEZER;
479 break;
480 default:
481 WARN_ONCE(1, "Invalid alarm type: %d\n", type);
482 return;
483 }
484
485 delta = ktime_sub(absexp, base->gettime());
486
487 spin_lock_irqsave(&freezer_delta_lock, flags);
488 if (!freezer_delta || (delta < freezer_delta)) {
489 freezer_delta = delta;
490 freezer_expires = absexp;
491 freezer_alarmtype = type;
492 }
493 spin_unlock_irqrestore(&freezer_delta_lock, flags);
494}
John Stultzdce75a82011-08-10 11:31:03 -0700495
John Stultz180bf812011-04-28 12:58:11 -0700496/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800497 * clock2alarm - helper that converts from clockid to alarmtypes
498 * @clockid: clockid.
John Stultz9a7adcf52011-01-11 09:54:33 -0800499 */
500static enum alarmtimer_type clock2alarm(clockid_t clockid)
501{
502 if (clockid == CLOCK_REALTIME_ALARM)
503 return ALARM_REALTIME;
504 if (clockid == CLOCK_BOOTTIME_ALARM)
505 return ALARM_BOOTTIME;
506 return -1;
507}
508
John Stultz180bf812011-04-28 12:58:11 -0700509/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800510 * alarm_handle_timer - Callback for posix timers
511 * @alarm: alarm that fired
512 *
513 * Posix timer callback for expired alarm timers.
514 */
John Stultz4b413082011-08-10 10:37:59 -0700515static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
516 ktime_t now)
John Stultz9a7adcf52011-01-11 09:54:33 -0800517{
Richard Larocque474e941b2014-09-09 18:31:05 -0700518 unsigned long flags;
John Stultz9a7adcf52011-01-11 09:54:33 -0800519 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
John Stultz9e264762011-08-10 12:09:24 -0700520 it.alarm.alarmtimer);
Richard Larocque474e941b2014-09-09 18:31:05 -0700521 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
522
523 spin_lock_irqsave(&ptr->it_lock, flags);
Richard Larocque265b81d2014-09-09 18:31:04 -0700524 if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) {
Thomas Gleixner18c700c2017-05-30 23:15:36 +0200525 if (posix_timer_event(ptr, 0))
Richard Larocque265b81d2014-09-09 18:31:04 -0700526 ptr->it_overrun++;
527 }
John Stultz4b413082011-08-10 10:37:59 -0700528
John Stultz54da23b2011-08-10 11:08:07 -0700529 /* Re-add periodic timers */
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200530 if (ptr->it_interval) {
531 ptr->it_overrun += alarm_forward(alarm, now, ptr->it_interval);
Richard Larocque474e941b2014-09-09 18:31:05 -0700532 result = ALARMTIMER_RESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700533 }
Richard Larocque474e941b2014-09-09 18:31:05 -0700534 spin_unlock_irqrestore(&ptr->it_lock, flags);
535
536 return result;
John Stultz9a7adcf52011-01-11 09:54:33 -0800537}
538
John Stultz180bf812011-04-28 12:58:11 -0700539/**
Thomas Gleixnerb3db80f2017-05-30 23:15:54 +0200540 * alarm_timer_rearm - Posix timer callback for rearming timer
541 * @timr: Pointer to the posixtimer data struct
542 */
543static void alarm_timer_rearm(struct k_itimer *timr)
544{
545 struct alarm *alarm = &timr->it.alarm.alarmtimer;
546
547 timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
548 alarm_start(alarm, alarm->node.expires);
549}
550
551/**
Thomas Gleixnere7561f12017-05-30 23:15:55 +0200552 * alarm_timer_forward - Posix timer callback for forwarding timer
553 * @timr: Pointer to the posixtimer data struct
554 * @now: Current time to forward the timer against
555 */
556static int alarm_timer_forward(struct k_itimer *timr, ktime_t now)
557{
558 struct alarm *alarm = &timr->it.alarm.alarmtimer;
559
560 return (int) alarm_forward(alarm, timr->it_interval, now);
561}
562
563/**
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200564 * alarm_timer_remaining - Posix timer callback to retrieve remaining time
565 * @timr: Pointer to the posixtimer data struct
566 * @now: Current time to calculate against
567 */
568static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
569{
570 struct alarm *alarm = &timr->it.alarm.alarmtimer;
571
572 return ktime_sub(now, alarm->node.expires);
573}
574
575/**
Thomas Gleixnere344c9e2017-05-30 23:15:57 +0200576 * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
577 * @timr: Pointer to the posixtimer data struct
578 */
579static int alarm_timer_try_to_cancel(struct k_itimer *timr)
580{
581 return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
582}
583
584/**
John Stultz9a7adcf52011-01-11 09:54:33 -0800585 * alarm_clock_getres - posix getres interface
586 * @which_clock: clockid
587 * @tp: timespec to fill
588 *
589 * Returns the granularity of underlying alarm base clock
590 */
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700591static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
John Stultz9a7adcf52011-01-11 09:54:33 -0800592{
John Stultz1c6b39a2011-06-16 18:47:37 -0700593 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400594 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700595
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000596 tp->tv_sec = 0;
597 tp->tv_nsec = hrtimer_resolution;
598 return 0;
John Stultz9a7adcf52011-01-11 09:54:33 -0800599}
600
601/**
602 * alarm_clock_get - posix clock_get interface
603 * @which_clock: clockid
604 * @tp: timespec to fill.
605 *
606 * Provides the underlying alarm base time.
607 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700608static int alarm_clock_get(clockid_t which_clock, struct timespec64 *tp)
John Stultz9a7adcf52011-01-11 09:54:33 -0800609{
610 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
611
John Stultz1c6b39a2011-06-16 18:47:37 -0700612 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400613 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700614
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700615 *tp = ktime_to_timespec64(base->gettime());
John Stultz9a7adcf52011-01-11 09:54:33 -0800616 return 0;
617}
618
619/**
620 * alarm_timer_create - posix timer_create interface
621 * @new_timer: k_itimer pointer to manage
622 *
623 * Initializes the k_itimer structure.
624 */
625static int alarm_timer_create(struct k_itimer *new_timer)
626{
627 enum alarmtimer_type type;
John Stultz9a7adcf52011-01-11 09:54:33 -0800628
John Stultz1c6b39a2011-06-16 18:47:37 -0700629 if (!alarmtimer_get_rtcdev())
630 return -ENOTSUPP;
631
John Stultz9a7adcf52011-01-11 09:54:33 -0800632 if (!capable(CAP_WAKE_ALARM))
633 return -EPERM;
634
635 type = clock2alarm(new_timer->it_clock);
John Stultz9e264762011-08-10 12:09:24 -0700636 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf52011-01-11 09:54:33 -0800637 return 0;
638}
639
640/**
641 * alarm_timer_get - posix timer_get interface
Thomas Gleixnerb3db80f2017-05-30 23:15:54 +0200642 * @timr: k_itimer pointer
John Stultz9a7adcf52011-01-11 09:54:33 -0800643 * @cur_setting: itimerspec data to fill
644 *
Richard Larocquee86fea72014-09-09 18:31:03 -0700645 * Copies out the current itimerspec data
John Stultz9a7adcf52011-01-11 09:54:33 -0800646 */
647static void alarm_timer_get(struct k_itimer *timr,
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700648 struct itimerspec64 *cur_setting)
John Stultz9a7adcf52011-01-11 09:54:33 -0800649{
Richard Larocquee86fea72014-09-09 18:31:03 -0700650 ktime_t relative_expiry_time =
651 alarm_expires_remaining(&(timr->it.alarm.alarmtimer));
John Stultzea7802f2011-08-04 07:51:56 -0700652
Richard Larocquee86fea72014-09-09 18:31:03 -0700653 if (ktime_to_ns(relative_expiry_time) > 0) {
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700654 cur_setting->it_value = ktime_to_timespec64(relative_expiry_time);
Richard Larocquee86fea72014-09-09 18:31:03 -0700655 } else {
656 cur_setting->it_value.tv_sec = 0;
657 cur_setting->it_value.tv_nsec = 0;
658 }
659
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200660 cur_setting->it_interval = ktime_to_timespec64(timr->it_interval);
John Stultz9a7adcf52011-01-11 09:54:33 -0800661}
662
663/**
664 * alarm_timer_del - posix timer_del interface
665 * @timr: k_itimer pointer to be deleted
666 *
667 * Cancels any programmed alarms for the given timer.
668 */
669static int alarm_timer_del(struct k_itimer *timr)
670{
John Stultz1c6b39a2011-06-16 18:47:37 -0700671 if (!rtcdev)
672 return -ENOTSUPP;
673
John Stultz9082c462011-08-10 12:41:36 -0700674 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
675 return TIMER_RETRY;
676
John Stultz9a7adcf52011-01-11 09:54:33 -0800677 return 0;
678}
679
680/**
681 * alarm_timer_set - posix timer_set interface
682 * @timr: k_itimer pointer to be deleted
683 * @flags: timer flags
684 * @new_setting: itimerspec to be used
685 * @old_setting: itimerspec being replaced
686 *
687 * Sets the timer to new_setting, and starts the timer.
688 */
689static int alarm_timer_set(struct k_itimer *timr, int flags,
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700690 struct itimerspec64 *new_setting,
691 struct itimerspec64 *old_setting)
John Stultz9a7adcf52011-01-11 09:54:33 -0800692{
John Stultz16927772014-07-07 14:06:11 -0700693 ktime_t exp;
694
John Stultz1c6b39a2011-06-16 18:47:37 -0700695 if (!rtcdev)
696 return -ENOTSUPP;
697
John Stultz16927772014-07-07 14:06:11 -0700698 if (flags & ~TIMER_ABSTIME)
699 return -EINVAL;
700
John Stultz971c90b2011-08-04 07:25:35 -0700701 if (old_setting)
702 alarm_timer_get(timr, old_setting);
John Stultz9a7adcf52011-01-11 09:54:33 -0800703
704 /* If the timer was already set, cancel it */
John Stultz9082c462011-08-10 12:41:36 -0700705 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
706 return TIMER_RETRY;
John Stultz9a7adcf52011-01-11 09:54:33 -0800707
708 /* start the timer */
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200709 timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
Thomas Gleixnerff86bf0c2017-05-30 23:15:35 +0200710
711 /*
712 * Rate limit to the tick as a hot fix to prevent DOS. Will be
713 * mopped up later.
714 */
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200715 if (timr->it_interval < TICK_NSEC)
716 timr->it_interval = TICK_NSEC;
Thomas Gleixnerff86bf0c2017-05-30 23:15:35 +0200717
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700718 exp = timespec64_to_ktime(new_setting->it_value);
John Stultz16927772014-07-07 14:06:11 -0700719 /* Convert (if necessary) to absolute time */
720 if (flags != TIMER_ABSTIME) {
721 ktime_t now;
722
723 now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
Thomas Gleixnerf4781e72017-05-30 23:15:34 +0200724 exp = ktime_add_safe(now, exp);
John Stultz16927772014-07-07 14:06:11 -0700725 }
726
727 alarm_start(&timr->it.alarm.alarmtimer, exp);
John Stultz9a7adcf52011-01-11 09:54:33 -0800728 return 0;
729}
730
731/**
732 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
733 * @alarm: ptr to alarm that fired
734 *
735 * Wakes up the task that set the alarmtimer
736 */
John Stultz4b413082011-08-10 10:37:59 -0700737static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
738 ktime_t now)
John Stultz9a7adcf52011-01-11 09:54:33 -0800739{
740 struct task_struct *task = (struct task_struct *)alarm->data;
741
742 alarm->data = NULL;
743 if (task)
744 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700745 return ALARMTIMER_NORESTART;
John Stultz9a7adcf52011-01-11 09:54:33 -0800746}
747
748/**
749 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
750 * @alarm: ptr to alarmtimer
751 * @absexp: absolute expiration time
752 *
753 * Sets the alarm timer and sleeps until it is fired or interrupted.
754 */
755static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
756{
757 alarm->data = (void *)current;
758 do {
759 set_current_state(TASK_INTERRUPTIBLE);
John Stultz9e264762011-08-10 12:09:24 -0700760 alarm_start(alarm, absexp);
John Stultz9a7adcf52011-01-11 09:54:33 -0800761 if (likely(alarm->data))
762 schedule();
763
764 alarm_cancel(alarm);
765 } while (alarm->data && !signal_pending(current));
766
767 __set_current_state(TASK_RUNNING);
768
769 return (alarm->data == NULL);
770}
771
772
773/**
774 * update_rmtp - Update remaining timespec value
775 * @exp: expiration time
776 * @type: timer type
777 * @rmtp: user pointer to remaining timepsec value
778 *
779 * Helper function that fills in rmtp value with time between
780 * now and the exp value
781 */
782static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
783 struct timespec __user *rmtp)
784{
785 struct timespec rmt;
786 ktime_t rem;
787
788 rem = ktime_sub(exp, alarm_bases[type].gettime());
789
Thomas Gleixner2456e852016-12-25 11:38:40 +0100790 if (rem <= 0)
John Stultz9a7adcf52011-01-11 09:54:33 -0800791 return 0;
792 rmt = ktime_to_timespec(rem);
793
794 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
795 return -EFAULT;
796
797 return 1;
798
799}
800
801/**
802 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
803 * @restart: ptr to restart block
804 *
805 * Handles restarted clock_nanosleep calls
806 */
807static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
808{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200809 enum alarmtimer_type type = restart->nanosleep.clockid;
John Stultz9a7adcf52011-01-11 09:54:33 -0800810 ktime_t exp;
811 struct timespec __user *rmtp;
812 struct alarm alarm;
813 int ret = 0;
814
Thomas Gleixner2456e852016-12-25 11:38:40 +0100815 exp = restart->nanosleep.expires;
John Stultz9a7adcf52011-01-11 09:54:33 -0800816 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
817
818 if (alarmtimer_do_nsleep(&alarm, exp))
819 goto out;
820
821 if (freezing(current))
822 alarmtimer_freezerset(exp, type);
823
824 rmtp = restart->nanosleep.rmtp;
825 if (rmtp) {
826 ret = update_rmtp(exp, type, rmtp);
827 if (ret <= 0)
828 goto out;
829 }
830
831
832 /* The other values in restart are already filled in */
833 ret = -ERESTART_RESTARTBLOCK;
834out:
835 return ret;
836}
837
838/**
839 * alarm_timer_nsleep - alarmtimer nanosleep
840 * @which_clock: clockid
841 * @flags: determins abstime or relative
842 * @tsreq: requested sleep time (abs or rel)
843 * @rmtp: remaining sleep time saved
844 *
845 * Handles clock_nanosleep calls against _ALARM clockids
846 */
847static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
Deepa Dinamaniad196382017-03-26 12:04:18 -0700848 struct timespec64 *tsreq,
849 struct timespec __user *rmtp)
John Stultz9a7adcf52011-01-11 09:54:33 -0800850{
851 enum alarmtimer_type type = clock2alarm(which_clock);
Deepa Dinamaniad196382017-03-26 12:04:18 -0700852 struct restart_block *restart;
John Stultz9a7adcf52011-01-11 09:54:33 -0800853 struct alarm alarm;
854 ktime_t exp;
855 int ret = 0;
John Stultz9a7adcf52011-01-11 09:54:33 -0800856
John Stultz1c6b39a2011-06-16 18:47:37 -0700857 if (!alarmtimer_get_rtcdev())
858 return -ENOTSUPP;
859
John Stultz16927772014-07-07 14:06:11 -0700860 if (flags & ~TIMER_ABSTIME)
861 return -EINVAL;
862
John Stultz9a7adcf52011-01-11 09:54:33 -0800863 if (!capable(CAP_WAKE_ALARM))
864 return -EPERM;
865
866 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
867
Deepa Dinamaniad196382017-03-26 12:04:18 -0700868 exp = timespec64_to_ktime(*tsreq);
John Stultz9a7adcf52011-01-11 09:54:33 -0800869 /* Convert (if necessary) to absolute time */
870 if (flags != TIMER_ABSTIME) {
871 ktime_t now = alarm_bases[type].gettime();
872 exp = ktime_add(now, exp);
873 }
874
875 if (alarmtimer_do_nsleep(&alarm, exp))
876 goto out;
877
878 if (freezing(current))
879 alarmtimer_freezerset(exp, type);
880
881 /* abs timers don't set remaining time or restart */
882 if (flags == TIMER_ABSTIME) {
883 ret = -ERESTARTNOHAND;
884 goto out;
885 }
886
887 if (rmtp) {
888 ret = update_rmtp(exp, type, rmtp);
889 if (ret <= 0)
890 goto out;
891 }
892
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800893 restart = &current->restart_block;
John Stultz9a7adcf52011-01-11 09:54:33 -0800894 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200895 restart->nanosleep.clockid = type;
Thomas Gleixner2456e852016-12-25 11:38:40 +0100896 restart->nanosleep.expires = exp;
John Stultz9a7adcf52011-01-11 09:54:33 -0800897 restart->nanosleep.rmtp = rmtp;
898 ret = -ERESTART_RESTARTBLOCK;
899
900out:
901 return ret;
902}
John Stultzff3ead92011-01-11 09:42:13 -0800903
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300904const struct k_clock alarm_clock = {
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200905 .clock_getres = alarm_clock_getres,
906 .clock_get = alarm_clock_get,
907 .timer_create = alarm_timer_create,
908 .timer_set = alarm_timer_set,
909 .timer_del = alarm_timer_del,
910 .timer_get = alarm_timer_get,
911 .timer_rearm = alarm_timer_rearm,
912 .timer_forward = alarm_timer_forward,
913 .timer_remaining = alarm_timer_remaining,
Thomas Gleixnere344c9e2017-05-30 23:15:57 +0200914 .timer_try_to_cancel = alarm_timer_try_to_cancel,
Thomas Gleixnerd653d8452017-05-30 23:15:56 +0200915 .nsleep = alarm_timer_nsleep,
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300916};
917#endif /* CONFIG_POSIX_TIMERS */
918
John Stultzff3ead92011-01-11 09:42:13 -0800919
920/* Suspend hook structures */
921static const struct dev_pm_ops alarmtimer_pm_ops = {
922 .suspend = alarmtimer_suspend,
zhuo-haoa0e32132015-11-17 20:08:07 +0800923 .resume = alarmtimer_resume,
John Stultzff3ead92011-01-11 09:42:13 -0800924};
925
926static struct platform_driver alarmtimer_driver = {
927 .driver = {
928 .name = "alarmtimer",
929 .pm = &alarmtimer_pm_ops,
930 }
931};
932
933/**
934 * alarmtimer_init - Initialize alarm timer code
935 *
936 * This function initializes the alarm bases and registers
937 * the posix clock ids.
938 */
939static int __init alarmtimer_init(void)
940{
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200941 struct platform_device *pdev;
John Stultzff3ead92011-01-11 09:42:13 -0800942 int error = 0;
943 int i;
John Stultz9a7adcf52011-01-11 09:54:33 -0800944
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100945 alarmtimer_rtc_timer_init();
John Stultzad30dfa2012-03-23 15:52:25 -0700946
John Stultzff3ead92011-01-11 09:42:13 -0800947 /* Initialize alarm bases */
948 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
949 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
950 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
951 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
952 for (i = 0; i < ALARM_NUMTYPE; i++) {
953 timerqueue_init_head(&alarm_bases[i].timerqueue);
954 spin_lock_init(&alarm_bases[i].lock);
John Stultzff3ead92011-01-11 09:42:13 -0800955 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700956
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200957 error = alarmtimer_rtc_interface_setup();
958 if (error)
959 return error;
John Stultzff3ead92011-01-11 09:42:13 -0800960
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200961 error = platform_driver_register(&alarmtimer_driver);
962 if (error)
963 goto out_if;
964
965 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
966 if (IS_ERR(pdev)) {
967 error = PTR_ERR(pdev);
968 goto out_drv;
969 }
Todd Poynor59a93c22012-08-09 00:37:27 -0700970 ws = wakeup_source_register("alarmtimer");
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200971 return 0;
972
973out_drv:
974 platform_driver_unregister(&alarmtimer_driver);
975out_if:
976 alarmtimer_rtc_interface_remove();
John Stultzff3ead92011-01-11 09:42:13 -0800977 return error;
978}
979device_initcall(alarmtimer_init);