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