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