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