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