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> | |
22 | #include <linux/alarmtimer.h> | |
23 | #include <linux/mutex.h> | |
24 | #include <linux/platform_device.h> | |
25 | #include <linux/posix-timers.h> | |
26 | #include <linux/workqueue.h> | |
27 | #include <linux/freezer.h> | |
6fa3eb70 S |
28 | #include <linux/xlog.h> |
29 | #include <linux/module.h> | |
30 | #include <mach/mtk_rtc.h> | |
31 | ||
32 | #define XLOG_MYTAG "Power/Alarm" | |
33 | ||
34 | #define ANDROID_ALARM_PRINT_INFO (1U << 0) | |
35 | #define ANDROID_ALARM_PRINT_IO (1U << 1) | |
36 | #define ANDROID_ALARM_PRINT_INT (1U << 2) | |
37 | ||
38 | static int debug_mask = ANDROID_ALARM_PRINT_INFO; | |
39 | module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP); | |
40 | ||
41 | #define alarm_dbg(debug_level_mask, fmt, args...) \ | |
42 | do { \ | |
43 | if (debug_mask & ANDROID_ALARM_PRINT_##debug_level_mask) \ | |
44 | xlog_printk(ANDROID_LOG_INFO, XLOG_MYTAG, fmt, ##args); \ | |
45 | } while (0) | |
ff3ead96 | 46 | |
180bf812 JS |
47 | /** |
48 | * struct alarm_base - Alarm timer bases | |
49 | * @lock: Lock for syncrhonized access to the base | |
50 | * @timerqueue: Timerqueue head managing the list of events | |
51 | * @timer: hrtimer used to schedule events while running | |
52 | * @gettime: Function to read the time correlating to the base | |
53 | * @base_clockid: clockid for the base | |
180bf812 | 54 | */ |
ff3ead96 JS |
55 | static struct alarm_base { |
56 | spinlock_t lock; | |
57 | struct timerqueue_head timerqueue; | |
ff3ead96 JS |
58 | ktime_t (*gettime)(void); |
59 | clockid_t base_clockid; | |
ff3ead96 JS |
60 | } alarm_bases[ALARM_NUMTYPE]; |
61 | ||
c008ba58 JS |
62 | /* freezer delta & lock used to handle clock_nanosleep triggered wakeups */ |
63 | static ktime_t freezer_delta; | |
64 | static DEFINE_SPINLOCK(freezer_delta_lock); | |
65 | ||
59a93c27 TP |
66 | static struct wakeup_source *ws; |
67 | ||
472647dc | 68 | #ifdef CONFIG_RTC_CLASS |
180bf812 | 69 | /* rtc timer and device for setting alarm wakeups at suspend */ |
c5e14e76 | 70 | static struct rtc_timer rtctimer; |
ff3ead96 | 71 | static struct rtc_device *rtcdev; |
c008ba58 | 72 | static DEFINE_SPINLOCK(rtcdev_lock); |
ff3ead96 | 73 | |
c008ba58 JS |
74 | /** |
75 | * alarmtimer_get_rtcdev - Return selected rtcdevice | |
76 | * | |
77 | * This function returns the rtc device to use for wakealarms. | |
78 | * If one has not already been chosen, it checks to see if a | |
79 | * functional rtc device is available. | |
80 | */ | |
57c498fa | 81 | struct rtc_device *alarmtimer_get_rtcdev(void) |
c008ba58 | 82 | { |
c008ba58 JS |
83 | unsigned long flags; |
84 | struct rtc_device *ret; | |
85 | ||
86 | spin_lock_irqsave(&rtcdev_lock, flags); | |
c008ba58 JS |
87 | ret = rtcdev; |
88 | spin_unlock_irqrestore(&rtcdev_lock, flags); | |
89 | ||
90 | return ret; | |
91 | } | |
8bc0dafb JS |
92 | |
93 | ||
94 | static int alarmtimer_rtc_add_device(struct device *dev, | |
95 | struct class_interface *class_intf) | |
96 | { | |
97 | unsigned long flags; | |
98 | struct rtc_device *rtc = to_rtc_device(dev); | |
99 | ||
100 | if (rtcdev) | |
101 | return -EBUSY; | |
102 | ||
103 | if (!rtc->ops->set_alarm) | |
104 | return -1; | |
6fa3eb70 S |
105 | //if (!device_may_wakeup(rtc->dev.parent)) |
106 | // return -1; | |
8bc0dafb JS |
107 | |
108 | spin_lock_irqsave(&rtcdev_lock, flags); | |
109 | if (!rtcdev) { | |
110 | rtcdev = rtc; | |
111 | /* hold a reference so it doesn't go away */ | |
112 | get_device(dev); | |
113 | } | |
114 | spin_unlock_irqrestore(&rtcdev_lock, flags); | |
115 | return 0; | |
116 | } | |
117 | ||
c5e14e76 TG |
118 | static inline void alarmtimer_rtc_timer_init(void) |
119 | { | |
120 | rtc_timer_init(&rtctimer, NULL, NULL); | |
121 | } | |
122 | ||
8bc0dafb JS |
123 | static struct class_interface alarmtimer_rtc_interface = { |
124 | .add_dev = &alarmtimer_rtc_add_device, | |
125 | }; | |
126 | ||
4523f6ad | 127 | static int alarmtimer_rtc_interface_setup(void) |
8bc0dafb JS |
128 | { |
129 | alarmtimer_rtc_interface.class = rtc_class; | |
4523f6ad TG |
130 | return class_interface_register(&alarmtimer_rtc_interface); |
131 | } | |
132 | static void alarmtimer_rtc_interface_remove(void) | |
133 | { | |
134 | class_interface_unregister(&alarmtimer_rtc_interface); | |
8bc0dafb | 135 | } |
1c6b39ad | 136 | #else |
57c498fa | 137 | struct rtc_device *alarmtimer_get_rtcdev(void) |
4523f6ad TG |
138 | { |
139 | return NULL; | |
140 | } | |
141 | #define rtcdev (NULL) | |
142 | static inline int alarmtimer_rtc_interface_setup(void) { return 0; } | |
143 | static inline void alarmtimer_rtc_interface_remove(void) { } | |
c5e14e76 | 144 | static inline void alarmtimer_rtc_timer_init(void) { } |
c008ba58 | 145 | #endif |
ff3ead96 | 146 | |
6fa3eb70 S |
147 | void alarm_set_power_on(struct timespec new_pwron_time, bool logo) |
148 | { | |
149 | unsigned long pwron_time; | |
150 | struct rtc_wkalrm alm; | |
151 | struct rtc_device *alarm_rtc_dev; | |
152 | // ktime_t now; | |
153 | ||
154 | printk("alarm set power on\n"); | |
155 | ||
156 | #ifdef RTC_PWRON_SEC | |
157 | /* round down the second */ | |
158 | new_pwron_time.tv_sec = (new_pwron_time.tv_sec / 60) * 60; | |
159 | #endif | |
160 | if (new_pwron_time.tv_sec > 0) { | |
161 | pwron_time = new_pwron_time.tv_sec; | |
162 | #ifdef RTC_PWRON_SEC | |
163 | pwron_time += RTC_PWRON_SEC; | |
164 | #endif | |
165 | alm.enabled = (logo ? 3 : 2); | |
166 | } else { | |
167 | pwron_time = 0; | |
168 | alm.enabled = 4; | |
169 | } | |
170 | alarm_rtc_dev = alarmtimer_get_rtcdev(); | |
171 | rtc_time_to_tm(pwron_time, &alm.time); | |
172 | /* | |
173 | rtc_timer_cancel(alarm_rtc_dev, &rtctimer); | |
174 | now = rtc_tm_to_ktime(alm.time); | |
175 | rtc_timer_start(alarm_rtc_dev, &rtctimer, now, ktime_set(0, 0)); | |
176 | */ | |
177 | rtc_timer_cancel(alarm_rtc_dev, &rtctimer); | |
178 | rtc_set_alarm(alarm_rtc_dev, &alm); | |
179 | rtc_set_alarm_poweron(alarm_rtc_dev, &alm); | |
180 | } | |
181 | ||
182 | void alarm_get_power_on(struct rtc_wkalrm *alm) | |
183 | { | |
184 | if (!alm) | |
185 | return; | |
186 | ||
187 | memset(alm, 0, sizeof(struct rtc_wkalrm)); | |
188 | #ifndef CONFIG_MTK_FPGA | |
189 | rtc_read_pwron_alarm(alm); | |
190 | #endif | |
191 | } | |
192 | ||
180bf812 | 193 | /** |
ff3ead96 JS |
194 | * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue |
195 | * @base: pointer to the base where the timer is being run | |
196 | * @alarm: pointer to alarm being enqueued. | |
197 | * | |
dae373be | 198 | * Adds alarm to a alarm_base timerqueue |
ff3ead96 JS |
199 | * |
200 | * Must hold base->lock when calling. | |
201 | */ | |
202 | static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm) | |
203 | { | |
dae373be JS |
204 | if (alarm->state & ALARMTIMER_STATE_ENQUEUED) |
205 | timerqueue_del(&base->timerqueue, &alarm->node); | |
206 | ||
ff3ead96 | 207 | timerqueue_add(&base->timerqueue, &alarm->node); |
a28cde81 | 208 | alarm->state |= ALARMTIMER_STATE_ENQUEUED; |
ff3ead96 JS |
209 | } |
210 | ||
180bf812 | 211 | /** |
a65bcc12 | 212 | * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue |
ff3ead96 JS |
213 | * @base: pointer to the base where the timer is running |
214 | * @alarm: pointer to alarm being removed | |
215 | * | |
dae373be | 216 | * Removes alarm to a alarm_base timerqueue |
ff3ead96 JS |
217 | * |
218 | * Must hold base->lock when calling. | |
219 | */ | |
a65bcc12 | 220 | static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm) |
ff3ead96 | 221 | { |
a28cde81 JS |
222 | if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED)) |
223 | return; | |
224 | ||
ff3ead96 | 225 | timerqueue_del(&base->timerqueue, &alarm->node); |
a28cde81 | 226 | alarm->state &= ~ALARMTIMER_STATE_ENQUEUED; |
ff3ead96 JS |
227 | } |
228 | ||
7068b7a1 | 229 | |
180bf812 | 230 | /** |
7068b7a1 JS |
231 | * alarmtimer_fired - Handles alarm hrtimer being fired. |
232 | * @timer: pointer to hrtimer being run | |
ff3ead96 | 233 | * |
180bf812 JS |
234 | * When a alarm timer fires, this runs through the timerqueue to |
235 | * see which alarms expired, and runs those. If there are more alarm | |
236 | * timers queued for the future, we set the hrtimer to fire when | |
237 | * when the next future alarm timer expires. | |
ff3ead96 | 238 | */ |
7068b7a1 | 239 | static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer) |
ff3ead96 | 240 | { |
dae373be JS |
241 | struct alarm *alarm = container_of(timer, struct alarm, timer); |
242 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
ff3ead96 | 243 | unsigned long flags; |
7068b7a1 | 244 | int ret = HRTIMER_NORESTART; |
54da23b7 | 245 | int restart = ALARMTIMER_NORESTART; |
ff3ead96 | 246 | |
6fa3eb70 S |
247 | alarm_dbg(INT, "alarmtimer_fired \n"); |
248 | ||
ff3ead96 | 249 | spin_lock_irqsave(&base->lock, flags); |
a65bcc12 | 250 | alarmtimer_dequeue(base, alarm); |
dae373be | 251 | spin_unlock_irqrestore(&base->lock, flags); |
54da23b7 | 252 | |
dae373be JS |
253 | if (alarm->function) |
254 | restart = alarm->function(alarm, base->gettime()); | |
ff3ead96 | 255 | |
dae373be JS |
256 | spin_lock_irqsave(&base->lock, flags); |
257 | if (restart != ALARMTIMER_NORESTART) { | |
258 | hrtimer_set_expires(&alarm->timer, alarm->node.expires); | |
259 | alarmtimer_enqueue(base, alarm); | |
7068b7a1 | 260 | ret = HRTIMER_RESTART; |
ff3ead96 JS |
261 | } |
262 | spin_unlock_irqrestore(&base->lock, flags); | |
ff3ead96 | 263 | |
7068b7a1 | 264 | return ret; |
ff3ead96 | 265 | |
ff3ead96 JS |
266 | } |
267 | ||
6fa3eb70 S |
268 | ktime_t alarm_expires_remaining(const struct alarm *alarm) |
269 | { | |
270 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
271 | return ktime_sub(alarm->node.expires, base->gettime()); | |
272 | } | |
273 | ||
472647dc | 274 | #ifdef CONFIG_RTC_CLASS |
180bf812 | 275 | /** |
ff3ead96 JS |
276 | * alarmtimer_suspend - Suspend time callback |
277 | * @dev: unused | |
278 | * @state: unused | |
279 | * | |
280 | * When we are going into suspend, we look through the bases | |
281 | * to see which is the soonest timer to expire. We then | |
282 | * set an rtc timer to fire that far into the future, which | |
283 | * will wake us from suspend. | |
284 | */ | |
285 | static int alarmtimer_suspend(struct device *dev) | |
286 | { | |
287 | struct rtc_time tm; | |
288 | ktime_t min, now; | |
289 | unsigned long flags; | |
c008ba58 | 290 | struct rtc_device *rtc; |
ff3ead96 | 291 | int i; |
59a93c27 | 292 | int ret; |
ff3ead96 JS |
293 | |
294 | spin_lock_irqsave(&freezer_delta_lock, flags); | |
295 | min = freezer_delta; | |
296 | freezer_delta = ktime_set(0, 0); | |
297 | spin_unlock_irqrestore(&freezer_delta_lock, flags); | |
298 | ||
8bc0dafb | 299 | rtc = alarmtimer_get_rtcdev(); |
ff3ead96 | 300 | /* If we have no rtcdev, just return */ |
c008ba58 | 301 | if (!rtc) |
ff3ead96 JS |
302 | return 0; |
303 | ||
304 | /* Find the soonest timer to expire*/ | |
305 | for (i = 0; i < ALARM_NUMTYPE; i++) { | |
306 | struct alarm_base *base = &alarm_bases[i]; | |
307 | struct timerqueue_node *next; | |
308 | ktime_t delta; | |
309 | ||
310 | spin_lock_irqsave(&base->lock, flags); | |
311 | next = timerqueue_getnext(&base->timerqueue); | |
312 | spin_unlock_irqrestore(&base->lock, flags); | |
313 | if (!next) | |
314 | continue; | |
315 | delta = ktime_sub(next->expires, base->gettime()); | |
316 | if (!min.tv64 || (delta.tv64 < min.tv64)) | |
317 | min = delta; | |
318 | } | |
6fa3eb70 S |
319 | if (min.tv64 == 0) { |
320 | alarm_dbg(INT, "min.tv64 == 0\n"); | |
ff3ead96 | 321 | return 0; |
6fa3eb70 | 322 | } |
59a93c27 | 323 | if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) { |
6fa3eb70 | 324 | alarm_dbg(INT, "min.tv64 < 2S, give up suspend\n"); |
59a93c27 TP |
325 | __pm_wakeup_event(ws, 2 * MSEC_PER_SEC); |
326 | return -EBUSY; | |
327 | } | |
ff3ead96 JS |
328 | |
329 | /* Setup an rtc timer to fire that far in the future */ | |
c008ba58 JS |
330 | rtc_timer_cancel(rtc, &rtctimer); |
331 | rtc_read_time(rtc, &tm); | |
ff3ead96 JS |
332 | now = rtc_tm_to_ktime(tm); |
333 | now = ktime_add(now, min); | |
334 | ||
6fa3eb70 S |
335 | alarm_dbg(INFO, "now:%02d=%02d:%02d %02d/%02d/%04d. min.tv64=%lld\n", |
336 | tm.tm_hour, tm.tm_min, | |
337 | tm.tm_sec, tm.tm_mon + 1, | |
338 | tm.tm_mday, | |
339 | tm.tm_year + 1900, min.tv64); | |
59a93c27 TP |
340 | /* Set alarm, if in the past reject suspend briefly to handle */ |
341 | ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0)); | |
342 | if (ret < 0) | |
343 | __pm_wakeup_event(ws, MSEC_PER_SEC); | |
344 | return ret; | |
ff3ead96 | 345 | } |
472647dc JS |
346 | #else |
347 | static int alarmtimer_suspend(struct device *dev) | |
348 | { | |
349 | return 0; | |
350 | } | |
351 | #endif | |
ff3ead96 | 352 | |
9a7adcf5 JS |
353 | static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type) |
354 | { | |
355 | ktime_t delta; | |
356 | unsigned long flags; | |
357 | struct alarm_base *base = &alarm_bases[type]; | |
358 | ||
359 | delta = ktime_sub(absexp, base->gettime()); | |
360 | ||
361 | spin_lock_irqsave(&freezer_delta_lock, flags); | |
362 | if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64)) | |
363 | freezer_delta = delta; | |
364 | spin_unlock_irqrestore(&freezer_delta_lock, flags); | |
365 | } | |
366 | ||
367 | ||
180bf812 | 368 | /** |
ff3ead96 JS |
369 | * alarm_init - Initialize an alarm structure |
370 | * @alarm: ptr to alarm to be initialized | |
371 | * @type: the type of the alarm | |
372 | * @function: callback that is run when the alarm fires | |
ff3ead96 JS |
373 | */ |
374 | void alarm_init(struct alarm *alarm, enum alarmtimer_type type, | |
4b41308d | 375 | enum alarmtimer_restart (*function)(struct alarm *, ktime_t)) |
ff3ead96 JS |
376 | { |
377 | timerqueue_init(&alarm->node); | |
dae373be JS |
378 | hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid, |
379 | HRTIMER_MODE_ABS); | |
380 | alarm->timer.function = alarmtimer_fired; | |
ff3ead96 JS |
381 | alarm->function = function; |
382 | alarm->type = type; | |
a28cde81 | 383 | alarm->state = ALARMTIMER_STATE_INACTIVE; |
ff3ead96 JS |
384 | } |
385 | ||
180bf812 | 386 | /** |
6fa3eb70 | 387 | * alarm_start - Sets an absolute alarm to fire |
ff3ead96 JS |
388 | * @alarm: ptr to alarm to set |
389 | * @start: time to run the alarm | |
ff3ead96 | 390 | */ |
dae373be | 391 | int alarm_start(struct alarm *alarm, ktime_t start) |
ff3ead96 JS |
392 | { |
393 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
394 | unsigned long flags; | |
dae373be | 395 | int ret; |
ff3ead96 JS |
396 | |
397 | spin_lock_irqsave(&base->lock, flags); | |
ff3ead96 | 398 | alarm->node.expires = start; |
ff3ead96 | 399 | alarmtimer_enqueue(base, alarm); |
dae373be JS |
400 | ret = hrtimer_start(&alarm->timer, alarm->node.expires, |
401 | HRTIMER_MODE_ABS); | |
ff3ead96 | 402 | spin_unlock_irqrestore(&base->lock, flags); |
dae373be | 403 | return ret; |
ff3ead96 JS |
404 | } |
405 | ||
6fa3eb70 S |
406 | /** |
407 | * alarm_start_relative - Sets a relative alarm to fire | |
408 | * @alarm: ptr to alarm to set | |
409 | * @start: time relative to now to run the alarm | |
410 | */ | |
411 | int alarm_start_relative(struct alarm *alarm, ktime_t start) | |
412 | { | |
413 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
414 | ||
415 | start = ktime_add(start, base->gettime()); | |
416 | return alarm_start(alarm, start); | |
417 | } | |
418 | ||
419 | void alarm_restart(struct alarm *alarm) | |
420 | { | |
421 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
422 | unsigned long flags; | |
423 | ||
424 | spin_lock_irqsave(&base->lock, flags); | |
425 | hrtimer_set_expires(&alarm->timer, alarm->node.expires); | |
426 | hrtimer_restart(&alarm->timer); | |
427 | alarmtimer_enqueue(base, alarm); | |
428 | spin_unlock_irqrestore(&base->lock, flags); | |
429 | } | |
430 | ||
180bf812 | 431 | /** |
9082c465 | 432 | * alarm_try_to_cancel - Tries to cancel an alarm timer |
ff3ead96 | 433 | * @alarm: ptr to alarm to be canceled |
9082c465 JS |
434 | * |
435 | * Returns 1 if the timer was canceled, 0 if it was not running, | |
436 | * and -1 if the callback was running | |
ff3ead96 | 437 | */ |
9082c465 | 438 | int alarm_try_to_cancel(struct alarm *alarm) |
ff3ead96 JS |
439 | { |
440 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
441 | unsigned long flags; | |
dae373be | 442 | int ret; |
9082c465 | 443 | |
dae373be JS |
444 | spin_lock_irqsave(&base->lock, flags); |
445 | ret = hrtimer_try_to_cancel(&alarm->timer); | |
446 | if (ret >= 0) | |
a65bcc12 | 447 | alarmtimer_dequeue(base, alarm); |
ff3ead96 | 448 | spin_unlock_irqrestore(&base->lock, flags); |
9082c465 | 449 | return ret; |
ff3ead96 JS |
450 | } |
451 | ||
452 | ||
9082c465 JS |
453 | /** |
454 | * alarm_cancel - Spins trying to cancel an alarm timer until it is done | |
455 | * @alarm: ptr to alarm to be canceled | |
456 | * | |
457 | * Returns 1 if the timer was canceled, 0 if it was not active. | |
458 | */ | |
459 | int alarm_cancel(struct alarm *alarm) | |
460 | { | |
461 | for (;;) { | |
462 | int ret = alarm_try_to_cancel(alarm); | |
463 | if (ret >= 0) | |
464 | return ret; | |
465 | cpu_relax(); | |
466 | } | |
467 | } | |
468 | ||
dce75a8c JS |
469 | |
470 | u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval) | |
471 | { | |
472 | u64 overrun = 1; | |
473 | ktime_t delta; | |
474 | ||
475 | delta = ktime_sub(now, alarm->node.expires); | |
476 | ||
477 | if (delta.tv64 < 0) | |
478 | return 0; | |
479 | ||
480 | if (unlikely(delta.tv64 >= interval.tv64)) { | |
481 | s64 incr = ktime_to_ns(interval); | |
482 | ||
483 | overrun = ktime_divns(delta, incr); | |
484 | ||
485 | alarm->node.expires = ktime_add_ns(alarm->node.expires, | |
486 | incr*overrun); | |
487 | ||
488 | if (alarm->node.expires.tv64 > now.tv64) | |
489 | return overrun; | |
490 | /* | |
491 | * This (and the ktime_add() below) is the | |
492 | * correction for exact: | |
493 | */ | |
494 | overrun++; | |
495 | } | |
496 | ||
497 | alarm->node.expires = ktime_add(alarm->node.expires, interval); | |
498 | return overrun; | |
499 | } | |
500 | ||
6fa3eb70 S |
501 | u64 alarm_forward_now(struct alarm *alarm, ktime_t interval) |
502 | { | |
503 | struct alarm_base *base = &alarm_bases[alarm->type]; | |
504 | ||
505 | return alarm_forward(alarm, base->gettime(), interval); | |
506 | } | |
dce75a8c JS |
507 | |
508 | ||
509 | ||
180bf812 | 510 | /** |
9a7adcf5 JS |
511 | * clock2alarm - helper that converts from clockid to alarmtypes |
512 | * @clockid: clockid. | |
9a7adcf5 JS |
513 | */ |
514 | static enum alarmtimer_type clock2alarm(clockid_t clockid) | |
515 | { | |
516 | if (clockid == CLOCK_REALTIME_ALARM) | |
517 | return ALARM_REALTIME; | |
518 | if (clockid == CLOCK_BOOTTIME_ALARM) | |
519 | return ALARM_BOOTTIME; | |
520 | return -1; | |
521 | } | |
522 | ||
180bf812 | 523 | /** |
9a7adcf5 JS |
524 | * alarm_handle_timer - Callback for posix timers |
525 | * @alarm: alarm that fired | |
526 | * | |
527 | * Posix timer callback for expired alarm timers. | |
528 | */ | |
4b41308d JS |
529 | static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm, |
530 | ktime_t now) | |
9a7adcf5 JS |
531 | { |
532 | struct k_itimer *ptr = container_of(alarm, struct k_itimer, | |
9e264762 | 533 | it.alarm.alarmtimer); |
9a7adcf5 JS |
534 | if (posix_timer_event(ptr, 0) != 0) |
535 | ptr->it_overrun++; | |
4b41308d | 536 | |
54da23b7 | 537 | /* Re-add periodic timers */ |
9e264762 JS |
538 | if (ptr->it.alarm.interval.tv64) { |
539 | ptr->it_overrun += alarm_forward(alarm, now, | |
540 | ptr->it.alarm.interval); | |
54da23b7 JS |
541 | return ALARMTIMER_RESTART; |
542 | } | |
4b41308d | 543 | return ALARMTIMER_NORESTART; |
9a7adcf5 JS |
544 | } |
545 | ||
180bf812 | 546 | /** |
9a7adcf5 JS |
547 | * alarm_clock_getres - posix getres interface |
548 | * @which_clock: clockid | |
549 | * @tp: timespec to fill | |
550 | * | |
551 | * Returns the granularity of underlying alarm base clock | |
552 | */ | |
553 | static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp) | |
554 | { | |
555 | clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid; | |
556 | ||
1c6b39ad | 557 | if (!alarmtimer_get_rtcdev()) |
4501cfd0 | 558 | return -EINVAL; |
1c6b39ad | 559 | |
9a7adcf5 JS |
560 | return hrtimer_get_res(baseid, tp); |
561 | } | |
562 | ||
563 | /** | |
564 | * alarm_clock_get - posix clock_get interface | |
565 | * @which_clock: clockid | |
566 | * @tp: timespec to fill. | |
567 | * | |
568 | * Provides the underlying alarm base time. | |
569 | */ | |
570 | static int alarm_clock_get(clockid_t which_clock, struct timespec *tp) | |
571 | { | |
572 | struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)]; | |
573 | ||
1c6b39ad | 574 | if (!alarmtimer_get_rtcdev()) |
4501cfd0 | 575 | return -EINVAL; |
1c6b39ad | 576 | |
9a7adcf5 JS |
577 | *tp = ktime_to_timespec(base->gettime()); |
578 | return 0; | |
579 | } | |
580 | ||
581 | /** | |
582 | * alarm_timer_create - posix timer_create interface | |
583 | * @new_timer: k_itimer pointer to manage | |
584 | * | |
585 | * Initializes the k_itimer structure. | |
586 | */ | |
587 | static int alarm_timer_create(struct k_itimer *new_timer) | |
588 | { | |
589 | enum alarmtimer_type type; | |
590 | struct alarm_base *base; | |
591 | ||
1c6b39ad JS |
592 | if (!alarmtimer_get_rtcdev()) |
593 | return -ENOTSUPP; | |
594 | ||
9a7adcf5 JS |
595 | if (!capable(CAP_WAKE_ALARM)) |
596 | return -EPERM; | |
597 | ||
598 | type = clock2alarm(new_timer->it_clock); | |
599 | base = &alarm_bases[type]; | |
9e264762 | 600 | alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer); |
9a7adcf5 JS |
601 | return 0; |
602 | } | |
603 | ||
604 | /** | |
605 | * alarm_timer_get - posix timer_get interface | |
606 | * @new_timer: k_itimer pointer | |
607 | * @cur_setting: itimerspec data to fill | |
608 | * | |
609 | * Copies the itimerspec data out from the k_itimer | |
610 | */ | |
611 | static void alarm_timer_get(struct k_itimer *timr, | |
612 | struct itimerspec *cur_setting) | |
613 | { | |
ea7802f6 JS |
614 | memset(cur_setting, 0, sizeof(struct itimerspec)); |
615 | ||
9a7adcf5 | 616 | cur_setting->it_interval = |
9e264762 | 617 | ktime_to_timespec(timr->it.alarm.interval); |
9a7adcf5 | 618 | cur_setting->it_value = |
9e264762 | 619 | ktime_to_timespec(timr->it.alarm.alarmtimer.node.expires); |
9a7adcf5 JS |
620 | return; |
621 | } | |
622 | ||
623 | /** | |
624 | * alarm_timer_del - posix timer_del interface | |
625 | * @timr: k_itimer pointer to be deleted | |
626 | * | |
627 | * Cancels any programmed alarms for the given timer. | |
628 | */ | |
629 | static int alarm_timer_del(struct k_itimer *timr) | |
630 | { | |
1c6b39ad JS |
631 | if (!rtcdev) |
632 | return -ENOTSUPP; | |
633 | ||
9082c465 JS |
634 | if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0) |
635 | return TIMER_RETRY; | |
636 | ||
9a7adcf5 JS |
637 | return 0; |
638 | } | |
639 | ||
640 | /** | |
641 | * alarm_timer_set - posix timer_set interface | |
642 | * @timr: k_itimer pointer to be deleted | |
643 | * @flags: timer flags | |
644 | * @new_setting: itimerspec to be used | |
645 | * @old_setting: itimerspec being replaced | |
646 | * | |
647 | * Sets the timer to new_setting, and starts the timer. | |
648 | */ | |
649 | static int alarm_timer_set(struct k_itimer *timr, int flags, | |
650 | struct itimerspec *new_setting, | |
651 | struct itimerspec *old_setting) | |
652 | { | |
c9331927 JS |
653 | ktime_t exp; |
654 | ||
1c6b39ad JS |
655 | if (!rtcdev) |
656 | return -ENOTSUPP; | |
657 | ||
c9331927 JS |
658 | if (flags & ~TIMER_ABSTIME) |
659 | return -EINVAL; | |
660 | ||
971c90bf JS |
661 | if (old_setting) |
662 | alarm_timer_get(timr, old_setting); | |
9a7adcf5 JS |
663 | |
664 | /* If the timer was already set, cancel it */ | |
9082c465 JS |
665 | if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0) |
666 | return TIMER_RETRY; | |
9a7adcf5 JS |
667 | |
668 | /* start the timer */ | |
9e264762 | 669 | timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval); |
c9331927 JS |
670 | exp = timespec_to_ktime(new_setting->it_value); |
671 | /* Convert (if necessary) to absolute time */ | |
672 | if (flags != TIMER_ABSTIME) { | |
673 | ktime_t now; | |
674 | ||
675 | now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime(); | |
676 | exp = ktime_add(now, exp); | |
677 | } | |
678 | ||
679 | alarm_start(&timr->it.alarm.alarmtimer, exp); | |
9a7adcf5 JS |
680 | return 0; |
681 | } | |
682 | ||
683 | /** | |
684 | * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep | |
685 | * @alarm: ptr to alarm that fired | |
686 | * | |
687 | * Wakes up the task that set the alarmtimer | |
688 | */ | |
4b41308d JS |
689 | static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm, |
690 | ktime_t now) | |
9a7adcf5 JS |
691 | { |
692 | struct task_struct *task = (struct task_struct *)alarm->data; | |
693 | ||
694 | alarm->data = NULL; | |
695 | if (task) | |
696 | wake_up_process(task); | |
4b41308d | 697 | return ALARMTIMER_NORESTART; |
9a7adcf5 JS |
698 | } |
699 | ||
700 | /** | |
701 | * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation | |
702 | * @alarm: ptr to alarmtimer | |
703 | * @absexp: absolute expiration time | |
704 | * | |
705 | * Sets the alarm timer and sleeps until it is fired or interrupted. | |
706 | */ | |
707 | static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp) | |
708 | { | |
709 | alarm->data = (void *)current; | |
710 | do { | |
711 | set_current_state(TASK_INTERRUPTIBLE); | |
9e264762 | 712 | alarm_start(alarm, absexp); |
9a7adcf5 JS |
713 | if (likely(alarm->data)) |
714 | schedule(); | |
715 | ||
716 | alarm_cancel(alarm); | |
717 | } while (alarm->data && !signal_pending(current)); | |
718 | ||
719 | __set_current_state(TASK_RUNNING); | |
720 | ||
721 | return (alarm->data == NULL); | |
722 | } | |
723 | ||
724 | ||
725 | /** | |
726 | * update_rmtp - Update remaining timespec value | |
727 | * @exp: expiration time | |
728 | * @type: timer type | |
729 | * @rmtp: user pointer to remaining timepsec value | |
730 | * | |
731 | * Helper function that fills in rmtp value with time between | |
732 | * now and the exp value | |
733 | */ | |
734 | static int update_rmtp(ktime_t exp, enum alarmtimer_type type, | |
735 | struct timespec __user *rmtp) | |
736 | { | |
737 | struct timespec rmt; | |
738 | ktime_t rem; | |
739 | ||
740 | rem = ktime_sub(exp, alarm_bases[type].gettime()); | |
741 | ||
742 | if (rem.tv64 <= 0) | |
743 | return 0; | |
744 | rmt = ktime_to_timespec(rem); | |
745 | ||
746 | if (copy_to_user(rmtp, &rmt, sizeof(*rmtp))) | |
747 | return -EFAULT; | |
748 | ||
749 | return 1; | |
750 | ||
751 | } | |
752 | ||
753 | /** | |
754 | * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep | |
755 | * @restart: ptr to restart block | |
756 | * | |
757 | * Handles restarted clock_nanosleep calls | |
758 | */ | |
759 | static long __sched alarm_timer_nsleep_restart(struct restart_block *restart) | |
760 | { | |
ab8177bc | 761 | enum alarmtimer_type type = restart->nanosleep.clockid; |
9a7adcf5 JS |
762 | ktime_t exp; |
763 | struct timespec __user *rmtp; | |
764 | struct alarm alarm; | |
765 | int ret = 0; | |
766 | ||
767 | exp.tv64 = restart->nanosleep.expires; | |
768 | alarm_init(&alarm, type, alarmtimer_nsleep_wakeup); | |
769 | ||
770 | if (alarmtimer_do_nsleep(&alarm, exp)) | |
771 | goto out; | |
772 | ||
773 | if (freezing(current)) | |
774 | alarmtimer_freezerset(exp, type); | |
775 | ||
776 | rmtp = restart->nanosleep.rmtp; | |
777 | if (rmtp) { | |
778 | ret = update_rmtp(exp, type, rmtp); | |
779 | if (ret <= 0) | |
780 | goto out; | |
781 | } | |
782 | ||
783 | ||
784 | /* The other values in restart are already filled in */ | |
785 | ret = -ERESTART_RESTARTBLOCK; | |
786 | out: | |
787 | return ret; | |
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, | |
800 | struct timespec *tsreq, struct timespec __user *rmtp) | |
801 | { | |
802 | enum alarmtimer_type type = clock2alarm(which_clock); | |
803 | struct alarm alarm; | |
804 | ktime_t exp; | |
805 | int ret = 0; | |
806 | struct restart_block *restart; | |
807 | ||
1c6b39ad JS |
808 | if (!alarmtimer_get_rtcdev()) |
809 | return -ENOTSUPP; | |
810 | ||
c9331927 JS |
811 | if (flags & ~TIMER_ABSTIME) |
812 | return -EINVAL; | |
813 | ||
9a7adcf5 JS |
814 | if (!capable(CAP_WAKE_ALARM)) |
815 | return -EPERM; | |
816 | ||
817 | alarm_init(&alarm, type, alarmtimer_nsleep_wakeup); | |
818 | ||
819 | exp = timespec_to_ktime(*tsreq); | |
820 | /* Convert (if necessary) to absolute time */ | |
821 | if (flags != TIMER_ABSTIME) { | |
822 | ktime_t now = alarm_bases[type].gettime(); | |
823 | exp = ktime_add(now, exp); | |
824 | } | |
825 | ||
826 | if (alarmtimer_do_nsleep(&alarm, exp)) | |
827 | goto out; | |
828 | ||
829 | if (freezing(current)) | |
830 | alarmtimer_freezerset(exp, type); | |
831 | ||
832 | /* abs timers don't set remaining time or restart */ | |
833 | if (flags == TIMER_ABSTIME) { | |
834 | ret = -ERESTARTNOHAND; | |
835 | goto out; | |
836 | } | |
837 | ||
838 | if (rmtp) { | |
839 | ret = update_rmtp(exp, type, rmtp); | |
840 | if (ret <= 0) | |
841 | goto out; | |
842 | } | |
843 | ||
844 | restart = ¤t_thread_info()->restart_block; | |
845 | restart->fn = alarm_timer_nsleep_restart; | |
ab8177bc | 846 | restart->nanosleep.clockid = type; |
9a7adcf5 JS |
847 | restart->nanosleep.expires = exp.tv64; |
848 | restart->nanosleep.rmtp = rmtp; | |
849 | ret = -ERESTART_RESTARTBLOCK; | |
850 | ||
851 | out: | |
852 | return ret; | |
853 | } | |
ff3ead96 | 854 | |
ff3ead96 JS |
855 | |
856 | /* Suspend hook structures */ | |
857 | static const struct dev_pm_ops alarmtimer_pm_ops = { | |
858 | .suspend = alarmtimer_suspend, | |
859 | }; | |
860 | ||
861 | static struct platform_driver alarmtimer_driver = { | |
862 | .driver = { | |
863 | .name = "alarmtimer", | |
864 | .pm = &alarmtimer_pm_ops, | |
865 | } | |
866 | }; | |
867 | ||
868 | /** | |
869 | * alarmtimer_init - Initialize alarm timer code | |
870 | * | |
871 | * This function initializes the alarm bases and registers | |
872 | * the posix clock ids. | |
873 | */ | |
874 | static int __init alarmtimer_init(void) | |
875 | { | |
4523f6ad | 876 | struct platform_device *pdev; |
ff3ead96 JS |
877 | int error = 0; |
878 | int i; | |
9a7adcf5 JS |
879 | struct k_clock alarm_clock = { |
880 | .clock_getres = alarm_clock_getres, | |
881 | .clock_get = alarm_clock_get, | |
882 | .timer_create = alarm_timer_create, | |
883 | .timer_set = alarm_timer_set, | |
884 | .timer_del = alarm_timer_del, | |
885 | .timer_get = alarm_timer_get, | |
886 | .nsleep = alarm_timer_nsleep, | |
887 | }; | |
888 | ||
c5e14e76 | 889 | alarmtimer_rtc_timer_init(); |
ad30dfa9 | 890 | |
9a7adcf5 JS |
891 | posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock); |
892 | posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock); | |
ff3ead96 JS |
893 | |
894 | /* Initialize alarm bases */ | |
895 | alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME; | |
896 | alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real; | |
897 | alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME; | |
898 | alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime; | |
899 | for (i = 0; i < ALARM_NUMTYPE; i++) { | |
900 | timerqueue_init_head(&alarm_bases[i].timerqueue); | |
901 | spin_lock_init(&alarm_bases[i].lock); | |
ff3ead96 | 902 | } |
8bc0dafb | 903 | |
4523f6ad TG |
904 | error = alarmtimer_rtc_interface_setup(); |
905 | if (error) | |
906 | return error; | |
907 | ||
ff3ead96 | 908 | error = platform_driver_register(&alarmtimer_driver); |
4523f6ad TG |
909 | if (error) |
910 | goto out_if; | |
ff3ead96 | 911 | |
4523f6ad TG |
912 | pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0); |
913 | if (IS_ERR(pdev)) { | |
914 | error = PTR_ERR(pdev); | |
915 | goto out_drv; | |
916 | } | |
59a93c27 | 917 | ws = wakeup_source_register("alarmtimer"); |
4523f6ad TG |
918 | return 0; |
919 | ||
920 | out_drv: | |
921 | platform_driver_unregister(&alarmtimer_driver); | |
922 | out_if: | |
923 | alarmtimer_rtc_interface_remove(); | |
ff3ead96 JS |
924 | return error; |
925 | } | |
926 | device_initcall(alarmtimer_init); |