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