]>
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> | |
30 | ||
bab0aae9 TG |
31 | #include "posix-timers.h" |
32 | ||
4a057549 BW |
33 | #define CREATE_TRACE_POINTS |
34 | #include <trace/events/alarmtimer.h> | |
35 | ||
180bf812 JS |
36 | /** |
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 | |
180bf812 JS |
40 | * @gettime: Function to read the time correlating to the base |
41 | * @base_clockid: clockid for the base | |
180bf812 | 42 | */ |
ff3ead96 JS |
43 | static struct alarm_base { |
44 | spinlock_t lock; | |
45 | struct timerqueue_head timerqueue; | |
ff3ead96 JS |
46 | ktime_t (*gettime)(void); |
47 | clockid_t base_clockid; | |
ff3ead96 JS |
48 | } alarm_bases[ALARM_NUMTYPE]; |
49 | ||
b6b3b80f | 50 | #if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS) |
4a057549 BW |
51 | /* freezer information to handle clock_nanosleep triggered wakeups */ |
52 | static enum alarmtimer_type freezer_alarmtype; | |
53 | static ktime_t freezer_expires; | |
c008ba58 JS |
54 | static ktime_t freezer_delta; |
55 | static DEFINE_SPINLOCK(freezer_delta_lock); | |
b6b3b80f | 56 | #endif |
c008ba58 | 57 | |
59a93c27 TP |
58 | static struct wakeup_source *ws; |
59 | ||
472647dc | 60 | #ifdef CONFIG_RTC_CLASS |
180bf812 | 61 | /* rtc timer and device for setting alarm wakeups at suspend */ |
c5e14e76 | 62 | static struct rtc_timer rtctimer; |
ff3ead96 | 63 | static struct rtc_device *rtcdev; |
c008ba58 | 64 | static DEFINE_SPINLOCK(rtcdev_lock); |
ff3ead96 | 65 | |
c008ba58 JS |
66 | /** |
67 | * 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 | */ | |
57c498fa | 73 | struct rtc_device *alarmtimer_get_rtcdev(void) |
c008ba58 | 74 | { |
c008ba58 JS |
75 | unsigned long flags; |
76 | struct rtc_device *ret; | |
77 | ||
78 | spin_lock_irqsave(&rtcdev_lock, flags); | |
c008ba58 JS |
79 | ret = rtcdev; |
80 | spin_unlock_irqrestore(&rtcdev_lock, flags); | |
81 | ||
82 | return ret; | |
83 | } | |
71d5d2b7 | 84 | EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev); |
8bc0dafb JS |
85 | |
86 | static 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 | ||
c5e14e76 TG |
110 | static inline void alarmtimer_rtc_timer_init(void) |
111 | { | |
112 | rtc_timer_init(&rtctimer, NULL, NULL); | |
113 | } | |
114 | ||
8bc0dafb JS |
115 | static struct class_interface alarmtimer_rtc_interface = { |
116 | .add_dev = &alarmtimer_rtc_add_device, | |
117 | }; | |
118 | ||
4523f6ad | 119 | static int alarmtimer_rtc_interface_setup(void) |
8bc0dafb JS |
120 | { |
121 | alarmtimer_rtc_interface.class = rtc_class; | |
4523f6ad TG |
122 | return class_interface_register(&alarmtimer_rtc_interface); |
123 | } | |
124 | static void alarmtimer_rtc_interface_remove(void) | |
125 | { | |
126 | class_interface_unregister(&alarmtimer_rtc_interface); | |
8bc0dafb | 127 | } |
1c6b39ad | 128 | #else |
57c498fa | 129 | struct rtc_device *alarmtimer_get_rtcdev(void) |
4523f6ad TG |
130 | { |
131 | return NULL; | |
132 | } | |
133 | #define rtcdev (NULL) | |
134 | static inline int alarmtimer_rtc_interface_setup(void) { return 0; } | |
135 | static inline void alarmtimer_rtc_interface_remove(void) { } | |
c5e14e76 | 136 | static inline void alarmtimer_rtc_timer_init(void) { } |
c008ba58 | 137 | #endif |
ff3ead96 | 138 | |
180bf812 | 139 | /** |
ff3ead96 JS |
140 | * 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 | * | |
dae373be | 144 | * Adds alarm to a alarm_base timerqueue |
ff3ead96 JS |
145 | * |
146 | * Must hold base->lock when calling. | |
147 | */ | |
148 | static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm) | |
149 | { | |
dae373be JS |
150 | if (alarm->state & ALARMTIMER_STATE_ENQUEUED) |
151 | timerqueue_del(&base->timerqueue, &alarm->node); | |
152 | ||
ff3ead96 | 153 | timerqueue_add(&base->timerqueue, &alarm->node); |
a28cde81 | 154 | alarm->state |= ALARMTIMER_STATE_ENQUEUED; |
ff3ead96 JS |
155 | } |
156 | ||
180bf812 | 157 | /** |
a65bcc12 | 158 | * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue |
ff3ead96 JS |
159 | * @base: pointer to the base where the timer is running |
160 | * @alarm: pointer to alarm being removed | |
161 | * | |
dae373be | 162 | * Removes alarm to a alarm_base timerqueue |
ff3ead96 JS |
163 | * |
164 | * Must hold base->lock when calling. | |
165 | */ | |
a65bcc12 | 166 | static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm) |
ff3ead96 | 167 | { |
a28cde81 JS |
168 | if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED)) |
169 | return; | |
170 | ||
ff3ead96 | 171 | timerqueue_del(&base->timerqueue, &alarm->node); |
a28cde81 | 172 | alarm->state &= ~ALARMTIMER_STATE_ENQUEUED; |
ff3ead96 JS |
173 | } |
174 | ||
7068b7a1 | 175 | |
180bf812 | 176 | /** |
7068b7a1 JS |
177 | * alarmtimer_fired - Handles alarm hrtimer being fired. |
178 | * @timer: pointer to hrtimer being run | |
ff3ead96 | 179 | * |
180bf812 JS |
180 | * 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. | |
ff3ead96 | 184 | */ |
7068b7a1 | 185 | static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer) |
ff3ead96 | 186 | { |
dae373be JS |
187 | struct alarm *alarm = container_of(timer, struct alarm, timer); |
188 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
ff3ead96 | 189 | unsigned long flags; |
7068b7a1 | 190 | int ret = HRTIMER_NORESTART; |
54da23b7 | 191 | int restart = ALARMTIMER_NORESTART; |
ff3ead96 JS |
192 | |
193 | spin_lock_irqsave(&base->lock, flags); | |
a65bcc12 | 194 | alarmtimer_dequeue(base, alarm); |
dae373be | 195 | spin_unlock_irqrestore(&base->lock, flags); |
54da23b7 | 196 | |
dae373be JS |
197 | if (alarm->function) |
198 | restart = alarm->function(alarm, base->gettime()); | |
ff3ead96 | 199 | |
dae373be JS |
200 | 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); | |
7068b7a1 | 204 | ret = HRTIMER_RESTART; |
ff3ead96 JS |
205 | } |
206 | spin_unlock_irqrestore(&base->lock, flags); | |
ff3ead96 | 207 | |
4a057549 | 208 | trace_alarmtimer_fired(alarm, base->gettime()); |
7068b7a1 | 209 | return ret; |
ff3ead96 | 210 | |
ff3ead96 JS |
211 | } |
212 | ||
6cffe00f TP |
213 | ktime_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 | } | |
11682a41 | 218 | EXPORT_SYMBOL_GPL(alarm_expires_remaining); |
6cffe00f | 219 | |
472647dc | 220 | #ifdef CONFIG_RTC_CLASS |
180bf812 | 221 | /** |
ff3ead96 JS |
222 | * 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 | */ | |
231 | static int alarmtimer_suspend(struct device *dev) | |
232 | { | |
4a057549 BW |
233 | ktime_t min, now, expires; |
234 | int i, ret, type; | |
c008ba58 | 235 | struct rtc_device *rtc; |
4a057549 BW |
236 | unsigned long flags; |
237 | struct rtc_time tm; | |
ff3ead96 JS |
238 | |
239 | spin_lock_irqsave(&freezer_delta_lock, flags); | |
240 | min = freezer_delta; | |
4a057549 BW |
241 | expires = freezer_expires; |
242 | type = freezer_alarmtype; | |
8b0e1953 | 243 | freezer_delta = 0; |
ff3ead96 JS |
244 | spin_unlock_irqrestore(&freezer_delta_lock, flags); |
245 | ||
8bc0dafb | 246 | rtc = alarmtimer_get_rtcdev(); |
ff3ead96 | 247 | /* If we have no rtcdev, just return */ |
c008ba58 | 248 | if (!rtc) |
ff3ead96 JS |
249 | 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()); | |
2456e855 | 263 | if (!min || (delta < min)) { |
4a057549 | 264 | expires = next->expires; |
ff3ead96 | 265 | min = delta; |
4a057549 BW |
266 | type = i; |
267 | } | |
ff3ead96 | 268 | } |
2456e855 | 269 | if (min == 0) |
ff3ead96 JS |
270 | return 0; |
271 | ||
59a93c27 TP |
272 | if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) { |
273 | __pm_wakeup_event(ws, 2 * MSEC_PER_SEC); | |
274 | return -EBUSY; | |
275 | } | |
ff3ead96 | 276 | |
4a057549 BW |
277 | trace_alarmtimer_suspend(expires, type); |
278 | ||
ff3ead96 | 279 | /* Setup an rtc timer to fire that far in the future */ |
c008ba58 JS |
280 | rtc_timer_cancel(rtc, &rtctimer); |
281 | rtc_read_time(rtc, &tm); | |
ff3ead96 JS |
282 | now = rtc_tm_to_ktime(tm); |
283 | now = ktime_add(now, min); | |
284 | ||
59a93c27 | 285 | /* Set alarm, if in the past reject suspend briefly to handle */ |
8b0e1953 | 286 | ret = rtc_timer_start(rtc, &rtctimer, now, 0); |
59a93c27 TP |
287 | if (ret < 0) |
288 | __pm_wakeup_event(ws, MSEC_PER_SEC); | |
289 | return ret; | |
ff3ead96 | 290 | } |
a0e3213f | 291 | |
292 | static 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 | ||
472647dc JS |
302 | #else |
303 | static int alarmtimer_suspend(struct device *dev) | |
304 | { | |
305 | return 0; | |
306 | } | |
a0e3213f | 307 | |
308 | static int alarmtimer_resume(struct device *dev) | |
309 | { | |
310 | return 0; | |
311 | } | |
472647dc | 312 | #endif |
ff3ead96 | 313 | |
180bf812 | 314 | /** |
ff3ead96 JS |
315 | * 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 | |
ff3ead96 JS |
319 | */ |
320 | void alarm_init(struct alarm *alarm, enum alarmtimer_type type, | |
4b41308d | 321 | enum alarmtimer_restart (*function)(struct alarm *, ktime_t)) |
ff3ead96 JS |
322 | { |
323 | timerqueue_init(&alarm->node); | |
dae373be JS |
324 | hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid, |
325 | HRTIMER_MODE_ABS); | |
326 | alarm->timer.function = alarmtimer_fired; | |
ff3ead96 JS |
327 | alarm->function = function; |
328 | alarm->type = type; | |
a28cde81 | 329 | alarm->state = ALARMTIMER_STATE_INACTIVE; |
ff3ead96 | 330 | } |
11682a41 | 331 | EXPORT_SYMBOL_GPL(alarm_init); |
ff3ead96 | 332 | |
180bf812 | 333 | /** |
6cffe00f | 334 | * alarm_start - Sets an absolute alarm to fire |
ff3ead96 JS |
335 | * @alarm: ptr to alarm to set |
336 | * @start: time to run the alarm | |
ff3ead96 | 337 | */ |
b193217e | 338 | void alarm_start(struct alarm *alarm, ktime_t start) |
ff3ead96 JS |
339 | { |
340 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
341 | unsigned long flags; | |
342 | ||
343 | spin_lock_irqsave(&base->lock, flags); | |
ff3ead96 | 344 | alarm->node.expires = start; |
ff3ead96 | 345 | alarmtimer_enqueue(base, alarm); |
b193217e | 346 | hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS); |
ff3ead96 | 347 | spin_unlock_irqrestore(&base->lock, flags); |
4a057549 BW |
348 | |
349 | trace_alarmtimer_start(alarm, base->gettime()); | |
ff3ead96 | 350 | } |
11682a41 | 351 | EXPORT_SYMBOL_GPL(alarm_start); |
ff3ead96 | 352 | |
6cffe00f TP |
353 | /** |
354 | * 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 | */ | |
b193217e | 358 | void alarm_start_relative(struct alarm *alarm, ktime_t start) |
6cffe00f TP |
359 | { |
360 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
361 | ||
f4781e76 | 362 | start = ktime_add_safe(start, base->gettime()); |
b193217e | 363 | alarm_start(alarm, start); |
6cffe00f | 364 | } |
11682a41 | 365 | EXPORT_SYMBOL_GPL(alarm_start_relative); |
6cffe00f TP |
366 | |
367 | void 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 | } | |
11682a41 | 378 | EXPORT_SYMBOL_GPL(alarm_restart); |
6cffe00f | 379 | |
180bf812 | 380 | /** |
9082c465 | 381 | * alarm_try_to_cancel - Tries to cancel an alarm timer |
ff3ead96 | 382 | * @alarm: ptr to alarm to be canceled |
9082c465 JS |
383 | * |
384 | * Returns 1 if the timer was canceled, 0 if it was not running, | |
385 | * and -1 if the callback was running | |
ff3ead96 | 386 | */ |
9082c465 | 387 | int alarm_try_to_cancel(struct alarm *alarm) |
ff3ead96 JS |
388 | { |
389 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
390 | unsigned long flags; | |
dae373be | 391 | int ret; |
9082c465 | 392 | |
dae373be JS |
393 | spin_lock_irqsave(&base->lock, flags); |
394 | ret = hrtimer_try_to_cancel(&alarm->timer); | |
395 | if (ret >= 0) | |
a65bcc12 | 396 | alarmtimer_dequeue(base, alarm); |
ff3ead96 | 397 | spin_unlock_irqrestore(&base->lock, flags); |
4a057549 BW |
398 | |
399 | trace_alarmtimer_cancel(alarm, base->gettime()); | |
9082c465 | 400 | return ret; |
ff3ead96 | 401 | } |
11682a41 | 402 | EXPORT_SYMBOL_GPL(alarm_try_to_cancel); |
ff3ead96 JS |
403 | |
404 | ||
9082c465 JS |
405 | /** |
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 | */ | |
411 | int 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 | } | |
11682a41 | 420 | EXPORT_SYMBOL_GPL(alarm_cancel); |
9082c465 | 421 | |
dce75a8c JS |
422 | |
423 | u64 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 | ||
2456e855 | 430 | if (delta < 0) |
dce75a8c JS |
431 | return 0; |
432 | ||
2456e855 | 433 | if (unlikely(delta >= interval)) { |
dce75a8c JS |
434 | 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 | ||
2456e855 | 441 | if (alarm->node.expires > now) |
dce75a8c JS |
442 | return overrun; |
443 | /* | |
444 | * This (and the ktime_add() below) is the | |
445 | * correction for exact: | |
446 | */ | |
447 | overrun++; | |
448 | } | |
449 | ||
f4781e76 | 450 | alarm->node.expires = ktime_add_safe(alarm->node.expires, interval); |
dce75a8c JS |
451 | return overrun; |
452 | } | |
11682a41 | 453 | EXPORT_SYMBOL_GPL(alarm_forward); |
dce75a8c | 454 | |
6cffe00f TP |
455 | u64 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 | } | |
11682a41 | 461 | EXPORT_SYMBOL_GPL(alarm_forward_now); |
dce75a8c | 462 | |
d3ba5a9a CH |
463 | #ifdef CONFIG_POSIX_TIMERS |
464 | ||
465 | static 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 | } | |
dce75a8c | 495 | |
180bf812 | 496 | /** |
9a7adcf5 JS |
497 | * clock2alarm - helper that converts from clockid to alarmtypes |
498 | * @clockid: clockid. | |
9a7adcf5 JS |
499 | */ |
500 | static 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 | ||
180bf812 | 509 | /** |
9a7adcf5 JS |
510 | * alarm_handle_timer - Callback for posix timers |
511 | * @alarm: alarm that fired | |
512 | * | |
513 | * Posix timer callback for expired alarm timers. | |
514 | */ | |
4b41308d JS |
515 | static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm, |
516 | ktime_t now) | |
9a7adcf5 | 517 | { |
474e941b | 518 | unsigned long flags; |
9a7adcf5 | 519 | struct k_itimer *ptr = container_of(alarm, struct k_itimer, |
9e264762 | 520 | it.alarm.alarmtimer); |
474e941b RL |
521 | enum alarmtimer_restart result = ALARMTIMER_NORESTART; |
522 | ||
523 | spin_lock_irqsave(&ptr->it_lock, flags); | |
265b81d2 | 524 | if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) { |
18c700c4 | 525 | if (posix_timer_event(ptr, 0)) |
265b81d2 RL |
526 | ptr->it_overrun++; |
527 | } | |
4b41308d | 528 | |
54da23b7 | 529 | /* Re-add periodic timers */ |
80105cd0 TG |
530 | if (ptr->it_interval) { |
531 | ptr->it_overrun += alarm_forward(alarm, now, ptr->it_interval); | |
474e941b | 532 | result = ALARMTIMER_RESTART; |
54da23b7 | 533 | } |
474e941b RL |
534 | spin_unlock_irqrestore(&ptr->it_lock, flags); |
535 | ||
536 | return result; | |
9a7adcf5 JS |
537 | } |
538 | ||
b3db80f7 TG |
539 | /** |
540 | * alarm_timer_rearm - Posix timer callback for rearming timer | |
541 | * @timr: Pointer to the posixtimer data struct | |
542 | */ | |
543 | static 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 | ||
e7561f16 TG |
551 | /** |
552 | * 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 | */ | |
556 | static 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 | ||
d653d845 TG |
563 | /** |
564 | * 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 | */ | |
568 | static 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 | ||
180bf812 | 575 | /** |
9a7adcf5 JS |
576 | * alarm_clock_getres - posix getres interface |
577 | * @which_clock: clockid | |
578 | * @tp: timespec to fill | |
579 | * | |
580 | * Returns the granularity of underlying alarm base clock | |
581 | */ | |
d2e3e0ca | 582 | static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp) |
9a7adcf5 | 583 | { |
1c6b39ad | 584 | if (!alarmtimer_get_rtcdev()) |
98d6f4dd | 585 | return -EINVAL; |
1c6b39ad | 586 | |
056a3cac TG |
587 | tp->tv_sec = 0; |
588 | tp->tv_nsec = hrtimer_resolution; | |
589 | return 0; | |
9a7adcf5 JS |
590 | } |
591 | ||
592 | /** | |
593 | * alarm_clock_get - posix clock_get interface | |
594 | * @which_clock: clockid | |
595 | * @tp: timespec to fill. | |
596 | * | |
597 | * Provides the underlying alarm base time. | |
598 | */ | |
3c9c12f4 | 599 | static int alarm_clock_get(clockid_t which_clock, struct timespec64 *tp) |
9a7adcf5 JS |
600 | { |
601 | struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)]; | |
602 | ||
1c6b39ad | 603 | if (!alarmtimer_get_rtcdev()) |
98d6f4dd | 604 | return -EINVAL; |
1c6b39ad | 605 | |
3c9c12f4 | 606 | *tp = ktime_to_timespec64(base->gettime()); |
9a7adcf5 JS |
607 | return 0; |
608 | } | |
609 | ||
610 | /** | |
611 | * alarm_timer_create - posix timer_create interface | |
612 | * @new_timer: k_itimer pointer to manage | |
613 | * | |
614 | * Initializes the k_itimer structure. | |
615 | */ | |
616 | static int alarm_timer_create(struct k_itimer *new_timer) | |
617 | { | |
618 | enum alarmtimer_type type; | |
9a7adcf5 | 619 | |
1c6b39ad JS |
620 | if (!alarmtimer_get_rtcdev()) |
621 | return -ENOTSUPP; | |
622 | ||
9a7adcf5 JS |
623 | if (!capable(CAP_WAKE_ALARM)) |
624 | return -EPERM; | |
625 | ||
626 | type = clock2alarm(new_timer->it_clock); | |
9e264762 | 627 | alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer); |
9a7adcf5 JS |
628 | return 0; |
629 | } | |
630 | ||
631 | /** | |
632 | * alarm_timer_get - posix timer_get interface | |
b3db80f7 | 633 | * @timr: k_itimer pointer |
9a7adcf5 JS |
634 | * @cur_setting: itimerspec data to fill |
635 | * | |
e86fea76 | 636 | * Copies out the current itimerspec data |
9a7adcf5 JS |
637 | */ |
638 | static void alarm_timer_get(struct k_itimer *timr, | |
5f252b32 | 639 | struct itimerspec64 *cur_setting) |
9a7adcf5 | 640 | { |
e86fea76 RL |
641 | ktime_t relative_expiry_time = |
642 | alarm_expires_remaining(&(timr->it.alarm.alarmtimer)); | |
643 | ||
644 | if (ktime_to_ns(relative_expiry_time) > 0) { | |
5f252b32 | 645 | cur_setting->it_value = ktime_to_timespec64(relative_expiry_time); |
e86fea76 RL |
646 | } else { |
647 | cur_setting->it_value.tv_sec = 0; | |
648 | cur_setting->it_value.tv_nsec = 0; | |
649 | } | |
ea7802f6 | 650 | |
80105cd0 | 651 | cur_setting->it_interval = ktime_to_timespec64(timr->it_interval); |
9a7adcf5 JS |
652 | } |
653 | ||
654 | /** | |
655 | * alarm_timer_del - posix timer_del interface | |
656 | * @timr: k_itimer pointer to be deleted | |
657 | * | |
658 | * Cancels any programmed alarms for the given timer. | |
659 | */ | |
660 | static int alarm_timer_del(struct k_itimer *timr) | |
661 | { | |
1c6b39ad JS |
662 | if (!rtcdev) |
663 | return -ENOTSUPP; | |
664 | ||
9082c465 JS |
665 | if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0) |
666 | return TIMER_RETRY; | |
667 | ||
9a7adcf5 JS |
668 | return 0; |
669 | } | |
670 | ||
671 | /** | |
672 | * alarm_timer_set - posix timer_set interface | |
673 | * @timr: k_itimer pointer to be deleted | |
674 | * @flags: timer flags | |
675 | * @new_setting: itimerspec to be used | |
676 | * @old_setting: itimerspec being replaced | |
677 | * | |
678 | * Sets the timer to new_setting, and starts the timer. | |
679 | */ | |
680 | static int alarm_timer_set(struct k_itimer *timr, int flags, | |
5f252b32 DD |
681 | struct itimerspec64 *new_setting, |
682 | struct itimerspec64 *old_setting) | |
9a7adcf5 | 683 | { |
16927776 JS |
684 | ktime_t exp; |
685 | ||
1c6b39ad JS |
686 | if (!rtcdev) |
687 | return -ENOTSUPP; | |
688 | ||
16927776 JS |
689 | if (flags & ~TIMER_ABSTIME) |
690 | return -EINVAL; | |
691 | ||
971c90bf JS |
692 | if (old_setting) |
693 | alarm_timer_get(timr, old_setting); | |
9a7adcf5 JS |
694 | |
695 | /* If the timer was already set, cancel it */ | |
9082c465 JS |
696 | if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0) |
697 | return TIMER_RETRY; | |
9a7adcf5 JS |
698 | |
699 | /* start the timer */ | |
80105cd0 | 700 | timr->it_interval = timespec64_to_ktime(new_setting->it_interval); |
ff86bf0c TG |
701 | |
702 | /* | |
703 | * Rate limit to the tick as a hot fix to prevent DOS. Will be | |
704 | * mopped up later. | |
705 | */ | |
80105cd0 TG |
706 | if (timr->it_interval < TICK_NSEC) |
707 | timr->it_interval = TICK_NSEC; | |
ff86bf0c | 708 | |
5f252b32 | 709 | exp = timespec64_to_ktime(new_setting->it_value); |
16927776 JS |
710 | /* Convert (if necessary) to absolute time */ |
711 | if (flags != TIMER_ABSTIME) { | |
712 | ktime_t now; | |
713 | ||
714 | now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime(); | |
f4781e76 | 715 | exp = ktime_add_safe(now, exp); |
16927776 JS |
716 | } |
717 | ||
718 | alarm_start(&timr->it.alarm.alarmtimer, exp); | |
9a7adcf5 JS |
719 | return 0; |
720 | } | |
721 | ||
722 | /** | |
723 | * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep | |
724 | * @alarm: ptr to alarm that fired | |
725 | * | |
726 | * Wakes up the task that set the alarmtimer | |
727 | */ | |
4b41308d JS |
728 | static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm, |
729 | ktime_t now) | |
9a7adcf5 JS |
730 | { |
731 | struct task_struct *task = (struct task_struct *)alarm->data; | |
732 | ||
733 | alarm->data = NULL; | |
734 | if (task) | |
735 | wake_up_process(task); | |
4b41308d | 736 | return ALARMTIMER_NORESTART; |
9a7adcf5 JS |
737 | } |
738 | ||
739 | /** | |
740 | * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation | |
741 | * @alarm: ptr to alarmtimer | |
742 | * @absexp: absolute expiration time | |
743 | * | |
744 | * Sets the alarm timer and sleeps until it is fired or interrupted. | |
745 | */ | |
746 | static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp) | |
747 | { | |
748 | alarm->data = (void *)current; | |
749 | do { | |
750 | set_current_state(TASK_INTERRUPTIBLE); | |
9e264762 | 751 | alarm_start(alarm, absexp); |
9a7adcf5 JS |
752 | if (likely(alarm->data)) |
753 | schedule(); | |
754 | ||
755 | alarm_cancel(alarm); | |
756 | } while (alarm->data && !signal_pending(current)); | |
757 | ||
758 | __set_current_state(TASK_RUNNING); | |
759 | ||
760 | return (alarm->data == NULL); | |
761 | } | |
762 | ||
763 | ||
764 | /** | |
765 | * update_rmtp - Update remaining timespec value | |
766 | * @exp: expiration time | |
767 | * @type: timer type | |
768 | * @rmtp: user pointer to remaining timepsec value | |
769 | * | |
770 | * Helper function that fills in rmtp value with time between | |
771 | * now and the exp value | |
772 | */ | |
773 | static int update_rmtp(ktime_t exp, enum alarmtimer_type type, | |
774 | struct timespec __user *rmtp) | |
775 | { | |
776 | struct timespec rmt; | |
777 | ktime_t rem; | |
778 | ||
779 | rem = ktime_sub(exp, alarm_bases[type].gettime()); | |
780 | ||
2456e855 | 781 | if (rem <= 0) |
9a7adcf5 JS |
782 | return 0; |
783 | rmt = ktime_to_timespec(rem); | |
784 | ||
785 | if (copy_to_user(rmtp, &rmt, sizeof(*rmtp))) | |
786 | return -EFAULT; | |
787 | ||
788 | return 1; | |
789 | ||
790 | } | |
791 | ||
792 | /** | |
793 | * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep | |
794 | * @restart: ptr to restart block | |
795 | * | |
796 | * Handles restarted clock_nanosleep calls | |
797 | */ | |
798 | static long __sched alarm_timer_nsleep_restart(struct restart_block *restart) | |
799 | { | |
ab8177bc | 800 | enum alarmtimer_type type = restart->nanosleep.clockid; |
9a7adcf5 JS |
801 | ktime_t exp; |
802 | struct timespec __user *rmtp; | |
803 | struct alarm alarm; | |
804 | int ret = 0; | |
805 | ||
2456e855 | 806 | exp = restart->nanosleep.expires; |
9a7adcf5 JS |
807 | alarm_init(&alarm, type, alarmtimer_nsleep_wakeup); |
808 | ||
809 | if (alarmtimer_do_nsleep(&alarm, exp)) | |
810 | goto out; | |
811 | ||
812 | if (freezing(current)) | |
813 | alarmtimer_freezerset(exp, type); | |
814 | ||
815 | rmtp = restart->nanosleep.rmtp; | |
816 | if (rmtp) { | |
817 | ret = update_rmtp(exp, type, rmtp); | |
818 | if (ret <= 0) | |
819 | goto out; | |
820 | } | |
821 | ||
822 | ||
823 | /* The other values in restart are already filled in */ | |
824 | ret = -ERESTART_RESTARTBLOCK; | |
825 | out: | |
826 | return ret; | |
827 | } | |
828 | ||
829 | /** | |
830 | * alarm_timer_nsleep - alarmtimer nanosleep | |
831 | * @which_clock: clockid | |
832 | * @flags: determins abstime or relative | |
833 | * @tsreq: requested sleep time (abs or rel) | |
834 | * @rmtp: remaining sleep time saved | |
835 | * | |
836 | * Handles clock_nanosleep calls against _ALARM clockids | |
837 | */ | |
838 | static int alarm_timer_nsleep(const clockid_t which_clock, int flags, | |
ad196384 DD |
839 | struct timespec64 *tsreq, |
840 | struct timespec __user *rmtp) | |
9a7adcf5 JS |
841 | { |
842 | enum alarmtimer_type type = clock2alarm(which_clock); | |
ad196384 | 843 | struct restart_block *restart; |
9a7adcf5 JS |
844 | struct alarm alarm; |
845 | ktime_t exp; | |
846 | int ret = 0; | |
9a7adcf5 | 847 | |
1c6b39ad JS |
848 | if (!alarmtimer_get_rtcdev()) |
849 | return -ENOTSUPP; | |
850 | ||
16927776 JS |
851 | if (flags & ~TIMER_ABSTIME) |
852 | return -EINVAL; | |
853 | ||
9a7adcf5 JS |
854 | if (!capable(CAP_WAKE_ALARM)) |
855 | return -EPERM; | |
856 | ||
857 | alarm_init(&alarm, type, alarmtimer_nsleep_wakeup); | |
858 | ||
ad196384 | 859 | exp = timespec64_to_ktime(*tsreq); |
9a7adcf5 JS |
860 | /* Convert (if necessary) to absolute time */ |
861 | if (flags != TIMER_ABSTIME) { | |
862 | ktime_t now = alarm_bases[type].gettime(); | |
863 | exp = ktime_add(now, exp); | |
864 | } | |
865 | ||
866 | if (alarmtimer_do_nsleep(&alarm, exp)) | |
867 | goto out; | |
868 | ||
869 | if (freezing(current)) | |
870 | alarmtimer_freezerset(exp, type); | |
871 | ||
872 | /* abs timers don't set remaining time or restart */ | |
873 | if (flags == TIMER_ABSTIME) { | |
874 | ret = -ERESTARTNOHAND; | |
875 | goto out; | |
876 | } | |
877 | ||
878 | if (rmtp) { | |
879 | ret = update_rmtp(exp, type, rmtp); | |
880 | if (ret <= 0) | |
881 | goto out; | |
882 | } | |
883 | ||
f56141e3 | 884 | restart = ¤t->restart_block; |
9a7adcf5 | 885 | restart->fn = alarm_timer_nsleep_restart; |
ab8177bc | 886 | restart->nanosleep.clockid = type; |
2456e855 | 887 | restart->nanosleep.expires = exp; |
9a7adcf5 JS |
888 | restart->nanosleep.rmtp = rmtp; |
889 | ret = -ERESTART_RESTARTBLOCK; | |
890 | ||
891 | out: | |
892 | return ret; | |
893 | } | |
ff3ead96 | 894 | |
d3ba5a9a | 895 | const struct k_clock alarm_clock = { |
d653d845 TG |
896 | .clock_getres = alarm_clock_getres, |
897 | .clock_get = alarm_clock_get, | |
898 | .timer_create = alarm_timer_create, | |
899 | .timer_set = alarm_timer_set, | |
900 | .timer_del = alarm_timer_del, | |
901 | .timer_get = alarm_timer_get, | |
902 | .timer_rearm = alarm_timer_rearm, | |
903 | .timer_forward = alarm_timer_forward, | |
904 | .timer_remaining = alarm_timer_remaining, | |
905 | .nsleep = alarm_timer_nsleep, | |
d3ba5a9a CH |
906 | }; |
907 | #endif /* CONFIG_POSIX_TIMERS */ | |
908 | ||
ff3ead96 JS |
909 | |
910 | /* Suspend hook structures */ | |
911 | static const struct dev_pm_ops alarmtimer_pm_ops = { | |
912 | .suspend = alarmtimer_suspend, | |
a0e3213f | 913 | .resume = alarmtimer_resume, |
ff3ead96 JS |
914 | }; |
915 | ||
916 | static struct platform_driver alarmtimer_driver = { | |
917 | .driver = { | |
918 | .name = "alarmtimer", | |
919 | .pm = &alarmtimer_pm_ops, | |
920 | } | |
921 | }; | |
922 | ||
923 | /** | |
924 | * alarmtimer_init - Initialize alarm timer code | |
925 | * | |
926 | * This function initializes the alarm bases and registers | |
927 | * the posix clock ids. | |
928 | */ | |
929 | static int __init alarmtimer_init(void) | |
930 | { | |
4523f6ad | 931 | struct platform_device *pdev; |
ff3ead96 JS |
932 | int error = 0; |
933 | int i; | |
9a7adcf5 | 934 | |
c5e14e76 | 935 | alarmtimer_rtc_timer_init(); |
ad30dfa9 | 936 | |
ff3ead96 JS |
937 | /* Initialize alarm bases */ |
938 | alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME; | |
939 | alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real; | |
940 | alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME; | |
941 | alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime; | |
942 | for (i = 0; i < ALARM_NUMTYPE; i++) { | |
943 | timerqueue_init_head(&alarm_bases[i].timerqueue); | |
944 | spin_lock_init(&alarm_bases[i].lock); | |
ff3ead96 | 945 | } |
8bc0dafb | 946 | |
4523f6ad TG |
947 | error = alarmtimer_rtc_interface_setup(); |
948 | if (error) | |
949 | return error; | |
950 | ||
ff3ead96 | 951 | error = platform_driver_register(&alarmtimer_driver); |
4523f6ad TG |
952 | if (error) |
953 | goto out_if; | |
ff3ead96 | 954 | |
4523f6ad TG |
955 | pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0); |
956 | if (IS_ERR(pdev)) { | |
957 | error = PTR_ERR(pdev); | |
958 | goto out_drv; | |
959 | } | |
59a93c27 | 960 | ws = wakeup_source_register("alarmtimer"); |
4523f6ad TG |
961 | return 0; |
962 | ||
963 | out_drv: | |
964 | platform_driver_unregister(&alarmtimer_driver); | |
965 | out_if: | |
966 | alarmtimer_rtc_interface_remove(); | |
ff3ead96 JS |
967 | return error; |
968 | } | |
969 | device_initcall(alarmtimer_init); |