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