]>
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 | ||
180bf812 | 563 | /** |
9a7adcf5 JS |
564 | * alarm_clock_getres - posix getres interface |
565 | * @which_clock: clockid | |
566 | * @tp: timespec to fill | |
567 | * | |
568 | * Returns the granularity of underlying alarm base clock | |
569 | */ | |
d2e3e0ca | 570 | static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp) |
9a7adcf5 | 571 | { |
1c6b39ad | 572 | if (!alarmtimer_get_rtcdev()) |
98d6f4dd | 573 | return -EINVAL; |
1c6b39ad | 574 | |
056a3cac TG |
575 | tp->tv_sec = 0; |
576 | tp->tv_nsec = hrtimer_resolution; | |
577 | return 0; | |
9a7adcf5 JS |
578 | } |
579 | ||
580 | /** | |
581 | * alarm_clock_get - posix clock_get interface | |
582 | * @which_clock: clockid | |
583 | * @tp: timespec to fill. | |
584 | * | |
585 | * Provides the underlying alarm base time. | |
586 | */ | |
3c9c12f4 | 587 | static int alarm_clock_get(clockid_t which_clock, struct timespec64 *tp) |
9a7adcf5 JS |
588 | { |
589 | struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)]; | |
590 | ||
1c6b39ad | 591 | if (!alarmtimer_get_rtcdev()) |
98d6f4dd | 592 | return -EINVAL; |
1c6b39ad | 593 | |
3c9c12f4 | 594 | *tp = ktime_to_timespec64(base->gettime()); |
9a7adcf5 JS |
595 | return 0; |
596 | } | |
597 | ||
598 | /** | |
599 | * alarm_timer_create - posix timer_create interface | |
600 | * @new_timer: k_itimer pointer to manage | |
601 | * | |
602 | * Initializes the k_itimer structure. | |
603 | */ | |
604 | static int alarm_timer_create(struct k_itimer *new_timer) | |
605 | { | |
606 | enum alarmtimer_type type; | |
9a7adcf5 | 607 | |
1c6b39ad JS |
608 | if (!alarmtimer_get_rtcdev()) |
609 | return -ENOTSUPP; | |
610 | ||
9a7adcf5 JS |
611 | if (!capable(CAP_WAKE_ALARM)) |
612 | return -EPERM; | |
613 | ||
614 | type = clock2alarm(new_timer->it_clock); | |
9e264762 | 615 | alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer); |
9a7adcf5 JS |
616 | return 0; |
617 | } | |
618 | ||
619 | /** | |
620 | * alarm_timer_get - posix timer_get interface | |
b3db80f7 | 621 | * @timr: k_itimer pointer |
9a7adcf5 JS |
622 | * @cur_setting: itimerspec data to fill |
623 | * | |
e86fea76 | 624 | * Copies out the current itimerspec data |
9a7adcf5 JS |
625 | */ |
626 | static void alarm_timer_get(struct k_itimer *timr, | |
5f252b32 | 627 | struct itimerspec64 *cur_setting) |
9a7adcf5 | 628 | { |
e86fea76 RL |
629 | ktime_t relative_expiry_time = |
630 | alarm_expires_remaining(&(timr->it.alarm.alarmtimer)); | |
631 | ||
632 | if (ktime_to_ns(relative_expiry_time) > 0) { | |
5f252b32 | 633 | cur_setting->it_value = ktime_to_timespec64(relative_expiry_time); |
e86fea76 RL |
634 | } else { |
635 | cur_setting->it_value.tv_sec = 0; | |
636 | cur_setting->it_value.tv_nsec = 0; | |
637 | } | |
ea7802f6 | 638 | |
80105cd0 | 639 | cur_setting->it_interval = ktime_to_timespec64(timr->it_interval); |
9a7adcf5 JS |
640 | } |
641 | ||
642 | /** | |
643 | * alarm_timer_del - posix timer_del interface | |
644 | * @timr: k_itimer pointer to be deleted | |
645 | * | |
646 | * Cancels any programmed alarms for the given timer. | |
647 | */ | |
648 | static int alarm_timer_del(struct k_itimer *timr) | |
649 | { | |
1c6b39ad JS |
650 | if (!rtcdev) |
651 | return -ENOTSUPP; | |
652 | ||
9082c465 JS |
653 | if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0) |
654 | return TIMER_RETRY; | |
655 | ||
9a7adcf5 JS |
656 | return 0; |
657 | } | |
658 | ||
659 | /** | |
660 | * alarm_timer_set - posix timer_set interface | |
661 | * @timr: k_itimer pointer to be deleted | |
662 | * @flags: timer flags | |
663 | * @new_setting: itimerspec to be used | |
664 | * @old_setting: itimerspec being replaced | |
665 | * | |
666 | * Sets the timer to new_setting, and starts the timer. | |
667 | */ | |
668 | static int alarm_timer_set(struct k_itimer *timr, int flags, | |
5f252b32 DD |
669 | struct itimerspec64 *new_setting, |
670 | struct itimerspec64 *old_setting) | |
9a7adcf5 | 671 | { |
16927776 JS |
672 | ktime_t exp; |
673 | ||
1c6b39ad JS |
674 | if (!rtcdev) |
675 | return -ENOTSUPP; | |
676 | ||
16927776 JS |
677 | if (flags & ~TIMER_ABSTIME) |
678 | return -EINVAL; | |
679 | ||
971c90bf JS |
680 | if (old_setting) |
681 | alarm_timer_get(timr, old_setting); | |
9a7adcf5 JS |
682 | |
683 | /* If the timer was already set, cancel it */ | |
9082c465 JS |
684 | if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0) |
685 | return TIMER_RETRY; | |
9a7adcf5 JS |
686 | |
687 | /* start the timer */ | |
80105cd0 | 688 | timr->it_interval = timespec64_to_ktime(new_setting->it_interval); |
ff86bf0c TG |
689 | |
690 | /* | |
691 | * Rate limit to the tick as a hot fix to prevent DOS. Will be | |
692 | * mopped up later. | |
693 | */ | |
80105cd0 TG |
694 | if (timr->it_interval < TICK_NSEC) |
695 | timr->it_interval = TICK_NSEC; | |
ff86bf0c | 696 | |
5f252b32 | 697 | exp = timespec64_to_ktime(new_setting->it_value); |
16927776 JS |
698 | /* Convert (if necessary) to absolute time */ |
699 | if (flags != TIMER_ABSTIME) { | |
700 | ktime_t now; | |
701 | ||
702 | now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime(); | |
f4781e76 | 703 | exp = ktime_add_safe(now, exp); |
16927776 JS |
704 | } |
705 | ||
706 | alarm_start(&timr->it.alarm.alarmtimer, exp); | |
9a7adcf5 JS |
707 | return 0; |
708 | } | |
709 | ||
710 | /** | |
711 | * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep | |
712 | * @alarm: ptr to alarm that fired | |
713 | * | |
714 | * Wakes up the task that set the alarmtimer | |
715 | */ | |
4b41308d JS |
716 | static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm, |
717 | ktime_t now) | |
9a7adcf5 JS |
718 | { |
719 | struct task_struct *task = (struct task_struct *)alarm->data; | |
720 | ||
721 | alarm->data = NULL; | |
722 | if (task) | |
723 | wake_up_process(task); | |
4b41308d | 724 | return ALARMTIMER_NORESTART; |
9a7adcf5 JS |
725 | } |
726 | ||
727 | /** | |
728 | * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation | |
729 | * @alarm: ptr to alarmtimer | |
730 | * @absexp: absolute expiration time | |
731 | * | |
732 | * Sets the alarm timer and sleeps until it is fired or interrupted. | |
733 | */ | |
734 | static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp) | |
735 | { | |
736 | alarm->data = (void *)current; | |
737 | do { | |
738 | set_current_state(TASK_INTERRUPTIBLE); | |
9e264762 | 739 | alarm_start(alarm, absexp); |
9a7adcf5 JS |
740 | if (likely(alarm->data)) |
741 | schedule(); | |
742 | ||
743 | alarm_cancel(alarm); | |
744 | } while (alarm->data && !signal_pending(current)); | |
745 | ||
746 | __set_current_state(TASK_RUNNING); | |
747 | ||
748 | return (alarm->data == NULL); | |
749 | } | |
750 | ||
751 | ||
752 | /** | |
753 | * update_rmtp - Update remaining timespec value | |
754 | * @exp: expiration time | |
755 | * @type: timer type | |
756 | * @rmtp: user pointer to remaining timepsec value | |
757 | * | |
758 | * Helper function that fills in rmtp value with time between | |
759 | * now and the exp value | |
760 | */ | |
761 | static int update_rmtp(ktime_t exp, enum alarmtimer_type type, | |
762 | struct timespec __user *rmtp) | |
763 | { | |
764 | struct timespec rmt; | |
765 | ktime_t rem; | |
766 | ||
767 | rem = ktime_sub(exp, alarm_bases[type].gettime()); | |
768 | ||
2456e855 | 769 | if (rem <= 0) |
9a7adcf5 JS |
770 | return 0; |
771 | rmt = ktime_to_timespec(rem); | |
772 | ||
773 | if (copy_to_user(rmtp, &rmt, sizeof(*rmtp))) | |
774 | return -EFAULT; | |
775 | ||
776 | return 1; | |
777 | ||
778 | } | |
779 | ||
780 | /** | |
781 | * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep | |
782 | * @restart: ptr to restart block | |
783 | * | |
784 | * Handles restarted clock_nanosleep calls | |
785 | */ | |
786 | static long __sched alarm_timer_nsleep_restart(struct restart_block *restart) | |
787 | { | |
ab8177bc | 788 | enum alarmtimer_type type = restart->nanosleep.clockid; |
9a7adcf5 JS |
789 | ktime_t exp; |
790 | struct timespec __user *rmtp; | |
791 | struct alarm alarm; | |
792 | int ret = 0; | |
793 | ||
2456e855 | 794 | exp = restart->nanosleep.expires; |
9a7adcf5 JS |
795 | alarm_init(&alarm, type, alarmtimer_nsleep_wakeup); |
796 | ||
797 | if (alarmtimer_do_nsleep(&alarm, exp)) | |
798 | goto out; | |
799 | ||
800 | if (freezing(current)) | |
801 | alarmtimer_freezerset(exp, type); | |
802 | ||
803 | rmtp = restart->nanosleep.rmtp; | |
804 | if (rmtp) { | |
805 | ret = update_rmtp(exp, type, rmtp); | |
806 | if (ret <= 0) | |
807 | goto out; | |
808 | } | |
809 | ||
810 | ||
811 | /* The other values in restart are already filled in */ | |
812 | ret = -ERESTART_RESTARTBLOCK; | |
813 | out: | |
814 | return ret; | |
815 | } | |
816 | ||
817 | /** | |
818 | * alarm_timer_nsleep - alarmtimer nanosleep | |
819 | * @which_clock: clockid | |
820 | * @flags: determins abstime or relative | |
821 | * @tsreq: requested sleep time (abs or rel) | |
822 | * @rmtp: remaining sleep time saved | |
823 | * | |
824 | * Handles clock_nanosleep calls against _ALARM clockids | |
825 | */ | |
826 | static int alarm_timer_nsleep(const clockid_t which_clock, int flags, | |
ad196384 DD |
827 | struct timespec64 *tsreq, |
828 | struct timespec __user *rmtp) | |
9a7adcf5 JS |
829 | { |
830 | enum alarmtimer_type type = clock2alarm(which_clock); | |
ad196384 | 831 | struct restart_block *restart; |
9a7adcf5 JS |
832 | struct alarm alarm; |
833 | ktime_t exp; | |
834 | int ret = 0; | |
9a7adcf5 | 835 | |
1c6b39ad JS |
836 | if (!alarmtimer_get_rtcdev()) |
837 | return -ENOTSUPP; | |
838 | ||
16927776 JS |
839 | if (flags & ~TIMER_ABSTIME) |
840 | return -EINVAL; | |
841 | ||
9a7adcf5 JS |
842 | if (!capable(CAP_WAKE_ALARM)) |
843 | return -EPERM; | |
844 | ||
845 | alarm_init(&alarm, type, alarmtimer_nsleep_wakeup); | |
846 | ||
ad196384 | 847 | exp = timespec64_to_ktime(*tsreq); |
9a7adcf5 JS |
848 | /* Convert (if necessary) to absolute time */ |
849 | if (flags != TIMER_ABSTIME) { | |
850 | ktime_t now = alarm_bases[type].gettime(); | |
851 | exp = ktime_add(now, exp); | |
852 | } | |
853 | ||
854 | if (alarmtimer_do_nsleep(&alarm, exp)) | |
855 | goto out; | |
856 | ||
857 | if (freezing(current)) | |
858 | alarmtimer_freezerset(exp, type); | |
859 | ||
860 | /* abs timers don't set remaining time or restart */ | |
861 | if (flags == TIMER_ABSTIME) { | |
862 | ret = -ERESTARTNOHAND; | |
863 | goto out; | |
864 | } | |
865 | ||
866 | if (rmtp) { | |
867 | ret = update_rmtp(exp, type, rmtp); | |
868 | if (ret <= 0) | |
869 | goto out; | |
870 | } | |
871 | ||
f56141e3 | 872 | restart = ¤t->restart_block; |
9a7adcf5 | 873 | restart->fn = alarm_timer_nsleep_restart; |
ab8177bc | 874 | restart->nanosleep.clockid = type; |
2456e855 | 875 | restart->nanosleep.expires = exp; |
9a7adcf5 JS |
876 | restart->nanosleep.rmtp = rmtp; |
877 | ret = -ERESTART_RESTARTBLOCK; | |
878 | ||
879 | out: | |
880 | return ret; | |
881 | } | |
ff3ead96 | 882 | |
d3ba5a9a CH |
883 | const struct k_clock alarm_clock = { |
884 | .clock_getres = alarm_clock_getres, | |
885 | .clock_get = alarm_clock_get, | |
886 | .timer_create = alarm_timer_create, | |
887 | .timer_set = alarm_timer_set, | |
888 | .timer_del = alarm_timer_del, | |
889 | .timer_get = alarm_timer_get, | |
b3db80f7 | 890 | .timer_rearm = alarm_timer_rearm, |
e7561f16 | 891 | .timer_forward = alarm_timer_forward, |
d3ba5a9a CH |
892 | .nsleep = alarm_timer_nsleep, |
893 | }; | |
894 | #endif /* CONFIG_POSIX_TIMERS */ | |
895 | ||
ff3ead96 JS |
896 | |
897 | /* Suspend hook structures */ | |
898 | static const struct dev_pm_ops alarmtimer_pm_ops = { | |
899 | .suspend = alarmtimer_suspend, | |
a0e3213f | 900 | .resume = alarmtimer_resume, |
ff3ead96 JS |
901 | }; |
902 | ||
903 | static struct platform_driver alarmtimer_driver = { | |
904 | .driver = { | |
905 | .name = "alarmtimer", | |
906 | .pm = &alarmtimer_pm_ops, | |
907 | } | |
908 | }; | |
909 | ||
910 | /** | |
911 | * alarmtimer_init - Initialize alarm timer code | |
912 | * | |
913 | * This function initializes the alarm bases and registers | |
914 | * the posix clock ids. | |
915 | */ | |
916 | static int __init alarmtimer_init(void) | |
917 | { | |
4523f6ad | 918 | struct platform_device *pdev; |
ff3ead96 JS |
919 | int error = 0; |
920 | int i; | |
9a7adcf5 | 921 | |
c5e14e76 | 922 | alarmtimer_rtc_timer_init(); |
ad30dfa9 | 923 | |
ff3ead96 JS |
924 | /* Initialize alarm bases */ |
925 | alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME; | |
926 | alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real; | |
927 | alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME; | |
928 | alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime; | |
929 | for (i = 0; i < ALARM_NUMTYPE; i++) { | |
930 | timerqueue_init_head(&alarm_bases[i].timerqueue); | |
931 | spin_lock_init(&alarm_bases[i].lock); | |
ff3ead96 | 932 | } |
8bc0dafb | 933 | |
4523f6ad TG |
934 | error = alarmtimer_rtc_interface_setup(); |
935 | if (error) | |
936 | return error; | |
937 | ||
ff3ead96 | 938 | error = platform_driver_register(&alarmtimer_driver); |
4523f6ad TG |
939 | if (error) |
940 | goto out_if; | |
ff3ead96 | 941 | |
4523f6ad TG |
942 | pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0); |
943 | if (IS_ERR(pdev)) { | |
944 | error = PTR_ERR(pdev); | |
945 | goto out_drv; | |
946 | } | |
59a93c27 | 947 | ws = wakeup_source_register("alarmtimer"); |
4523f6ad TG |
948 | return 0; |
949 | ||
950 | out_drv: | |
951 | platform_driver_unregister(&alarmtimer_driver); | |
952 | out_if: | |
953 | alarmtimer_rtc_interface_remove(); | |
ff3ead96 JS |
954 | return error; |
955 | } | |
956 | device_initcall(alarmtimer_init); |