aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/time/alarmtimer.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/time/alarmtimer.c')
-rw-r--r--kernel/time/alarmtimer.c266
1 files changed, 181 insertions, 85 deletions
diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index ea5e1a928d5b..c436e790b21b 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -53,27 +53,6 @@ static struct rtc_device *rtcdev;
53static DEFINE_SPINLOCK(rtcdev_lock); 53static DEFINE_SPINLOCK(rtcdev_lock);
54 54
55/** 55/**
56 * has_wakealarm - check rtc device has wakealarm ability
57 * @dev: current device
58 * @name_ptr: name to be returned
59 *
60 * This helper function checks to see if the rtc device can wake
61 * from suspend.
62 */
63static int has_wakealarm(struct device *dev, void *name_ptr)
64{
65 struct rtc_device *candidate = to_rtc_device(dev);
66
67 if (!candidate->ops->set_alarm)
68 return 0;
69 if (!device_may_wakeup(candidate->dev.parent))
70 return 0;
71
72 *(const char **)name_ptr = dev_name(dev);
73 return 1;
74}
75
76/**
77 * alarmtimer_get_rtcdev - Return selected rtcdevice 56 * alarmtimer_get_rtcdev - Return selected rtcdevice
78 * 57 *
79 * This function returns the rtc device to use for wakealarms. 58 * This function returns the rtc device to use for wakealarms.
@@ -82,37 +61,64 @@ static int has_wakealarm(struct device *dev, void *name_ptr)
82 */ 61 */
83static struct rtc_device *alarmtimer_get_rtcdev(void) 62static struct rtc_device *alarmtimer_get_rtcdev(void)
84{ 63{
85 struct device *dev;
86 char *str;
87 unsigned long flags; 64 unsigned long flags;
88 struct rtc_device *ret; 65 struct rtc_device *ret;
89 66
90 spin_lock_irqsave(&rtcdev_lock, flags); 67 spin_lock_irqsave(&rtcdev_lock, flags);
91 if (!rtcdev) {
92 /* Find an rtc device and init the rtc_timer */
93 dev = class_find_device(rtc_class, NULL, &str, has_wakealarm);
94 /* If we have a device then str is valid. See has_wakealarm() */
95 if (dev) {
96 rtcdev = rtc_class_open(str);
97 /*
98 * Drop the reference we got in class_find_device,
99 * rtc_open takes its own.
100 */
101 put_device(dev);
102 rtc_timer_init(&rtctimer, NULL, NULL);
103 }
104 }
105 ret = rtcdev; 68 ret = rtcdev;
106 spin_unlock_irqrestore(&rtcdev_lock, flags); 69 spin_unlock_irqrestore(&rtcdev_lock, flags);
107 70
108 return ret; 71 return ret;
109} 72}
73
74
75static int alarmtimer_rtc_add_device(struct device *dev,
76 struct class_interface *class_intf)
77{
78 unsigned long flags;
79 struct rtc_device *rtc = to_rtc_device(dev);
80
81 if (rtcdev)
82 return -EBUSY;
83
84 if (!rtc->ops->set_alarm)
85 return -1;
86 if (!device_may_wakeup(rtc->dev.parent))
87 return -1;
88
89 spin_lock_irqsave(&rtcdev_lock, flags);
90 if (!rtcdev) {
91 rtcdev = rtc;
92 /* hold a reference so it doesn't go away */
93 get_device(dev);
94 }
95 spin_unlock_irqrestore(&rtcdev_lock, flags);
96 return 0;
97}
98
99static struct class_interface alarmtimer_rtc_interface = {
100 .add_dev = &alarmtimer_rtc_add_device,
101};
102
103static int alarmtimer_rtc_interface_setup(void)
104{
105 alarmtimer_rtc_interface.class = rtc_class;
106 return class_interface_register(&alarmtimer_rtc_interface);
107}
108static void alarmtimer_rtc_interface_remove(void)
109{
110 class_interface_unregister(&alarmtimer_rtc_interface);
111}
110#else 112#else
111#define alarmtimer_get_rtcdev() (0) 113static inline struct rtc_device *alarmtimer_get_rtcdev(void)
112#define rtcdev (0) 114{
115 return NULL;
116}
117#define rtcdev (NULL)
118static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
119static inline void alarmtimer_rtc_interface_remove(void) { }
113#endif 120#endif
114 121
115
116/** 122/**
117 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue 123 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
118 * @base: pointer to the base where the timer is being run 124 * @base: pointer to the base where the timer is being run
@@ -126,6 +132,8 @@ static struct rtc_device *alarmtimer_get_rtcdev(void)
126static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm) 132static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
127{ 133{
128 timerqueue_add(&base->timerqueue, &alarm->node); 134 timerqueue_add(&base->timerqueue, &alarm->node);
135 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
136
129 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) { 137 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
130 hrtimer_try_to_cancel(&base->timer); 138 hrtimer_try_to_cancel(&base->timer);
131 hrtimer_start(&base->timer, alarm->node.expires, 139 hrtimer_start(&base->timer, alarm->node.expires,
@@ -147,7 +155,12 @@ static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
147{ 155{
148 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue); 156 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
149 157
158 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
159 return;
160
150 timerqueue_del(&base->timerqueue, &alarm->node); 161 timerqueue_del(&base->timerqueue, &alarm->node);
162 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
163
151 if (next == &alarm->node) { 164 if (next == &alarm->node) {
152 hrtimer_try_to_cancel(&base->timer); 165 hrtimer_try_to_cancel(&base->timer);
153 next = timerqueue_getnext(&base->timerqueue); 166 next = timerqueue_getnext(&base->timerqueue);
@@ -174,6 +187,7 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
174 unsigned long flags; 187 unsigned long flags;
175 ktime_t now; 188 ktime_t now;
176 int ret = HRTIMER_NORESTART; 189 int ret = HRTIMER_NORESTART;
190 int restart = ALARMTIMER_NORESTART;
177 191
178 spin_lock_irqsave(&base->lock, flags); 192 spin_lock_irqsave(&base->lock, flags);
179 now = base->gettime(); 193 now = base->gettime();
@@ -187,17 +201,19 @@ static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
187 alarm = container_of(next, struct alarm, node); 201 alarm = container_of(next, struct alarm, node);
188 202
189 timerqueue_del(&base->timerqueue, &alarm->node); 203 timerqueue_del(&base->timerqueue, &alarm->node);
190 alarm->enabled = 0; 204 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
191 /* Re-add periodic timers */ 205
192 if (alarm->period.tv64) { 206 alarm->state |= ALARMTIMER_STATE_CALLBACK;
193 alarm->node.expires = ktime_add(expired, alarm->period);
194 timerqueue_add(&base->timerqueue, &alarm->node);
195 alarm->enabled = 1;
196 }
197 spin_unlock_irqrestore(&base->lock, flags); 207 spin_unlock_irqrestore(&base->lock, flags);
198 if (alarm->function) 208 if (alarm->function)
199 alarm->function(alarm); 209 restart = alarm->function(alarm, now);
200 spin_lock_irqsave(&base->lock, flags); 210 spin_lock_irqsave(&base->lock, flags);
211 alarm->state &= ~ALARMTIMER_STATE_CALLBACK;
212
213 if (restart != ALARMTIMER_NORESTART) {
214 timerqueue_add(&base->timerqueue, &alarm->node);
215 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
216 }
201 } 217 }
202 218
203 if (next) { 219 if (next) {
@@ -234,7 +250,7 @@ static int alarmtimer_suspend(struct device *dev)
234 freezer_delta = ktime_set(0, 0); 250 freezer_delta = ktime_set(0, 0);
235 spin_unlock_irqrestore(&freezer_delta_lock, flags); 251 spin_unlock_irqrestore(&freezer_delta_lock, flags);
236 252
237 rtc = rtcdev; 253 rtc = alarmtimer_get_rtcdev();
238 /* If we have no rtcdev, just return */ 254 /* If we have no rtcdev, just return */
239 if (!rtc) 255 if (!rtc)
240 return 0; 256 return 0;
@@ -299,53 +315,111 @@ static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
299 * @function: callback that is run when the alarm fires 315 * @function: callback that is run when the alarm fires
300 */ 316 */
301void alarm_init(struct alarm *alarm, enum alarmtimer_type type, 317void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
302 void (*function)(struct alarm *)) 318 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
303{ 319{
304 timerqueue_init(&alarm->node); 320 timerqueue_init(&alarm->node);
305 alarm->period = ktime_set(0, 0);
306 alarm->function = function; 321 alarm->function = function;
307 alarm->type = type; 322 alarm->type = type;
308 alarm->enabled = 0; 323 alarm->state = ALARMTIMER_STATE_INACTIVE;
309} 324}
310 325
311/** 326/**
312 * alarm_start - Sets an alarm to fire 327 * alarm_start - Sets an alarm to fire
313 * @alarm: ptr to alarm to set 328 * @alarm: ptr to alarm to set
314 * @start: time to run the alarm 329 * @start: time to run the alarm
315 * @period: period at which the alarm will recur
316 */ 330 */
317void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period) 331void alarm_start(struct alarm *alarm, ktime_t start)
318{ 332{
319 struct alarm_base *base = &alarm_bases[alarm->type]; 333 struct alarm_base *base = &alarm_bases[alarm->type];
320 unsigned long flags; 334 unsigned long flags;
321 335
322 spin_lock_irqsave(&base->lock, flags); 336 spin_lock_irqsave(&base->lock, flags);
323 if (alarm->enabled) 337 if (alarmtimer_active(alarm))
324 alarmtimer_remove(base, alarm); 338 alarmtimer_remove(base, alarm);
325 alarm->node.expires = start; 339 alarm->node.expires = start;
326 alarm->period = period;
327 alarmtimer_enqueue(base, alarm); 340 alarmtimer_enqueue(base, alarm);
328 alarm->enabled = 1;
329 spin_unlock_irqrestore(&base->lock, flags); 341 spin_unlock_irqrestore(&base->lock, flags);
330} 342}
331 343
332/** 344/**
333 * alarm_cancel - Tries to cancel an alarm timer 345 * alarm_try_to_cancel - Tries to cancel an alarm timer
334 * @alarm: ptr to alarm to be canceled 346 * @alarm: ptr to alarm to be canceled
347 *
348 * Returns 1 if the timer was canceled, 0 if it was not running,
349 * and -1 if the callback was running
335 */ 350 */
336void alarm_cancel(struct alarm *alarm) 351int alarm_try_to_cancel(struct alarm *alarm)
337{ 352{
338 struct alarm_base *base = &alarm_bases[alarm->type]; 353 struct alarm_base *base = &alarm_bases[alarm->type];
339 unsigned long flags; 354 unsigned long flags;
340 355 int ret = -1;
341 spin_lock_irqsave(&base->lock, flags); 356 spin_lock_irqsave(&base->lock, flags);
342 if (alarm->enabled) 357
358 if (alarmtimer_callback_running(alarm))
359 goto out;
360
361 if (alarmtimer_is_queued(alarm)) {
343 alarmtimer_remove(base, alarm); 362 alarmtimer_remove(base, alarm);
344 alarm->enabled = 0; 363 ret = 1;
364 } else
365 ret = 0;
366out:
345 spin_unlock_irqrestore(&base->lock, flags); 367 spin_unlock_irqrestore(&base->lock, flags);
368 return ret;
369}
370
371
372/**
373 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
374 * @alarm: ptr to alarm to be canceled
375 *
376 * Returns 1 if the timer was canceled, 0 if it was not active.
377 */
378int alarm_cancel(struct alarm *alarm)
379{
380 for (;;) {
381 int ret = alarm_try_to_cancel(alarm);
382 if (ret >= 0)
383 return ret;
384 cpu_relax();
385 }
386}
387
388
389u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
390{
391 u64 overrun = 1;
392 ktime_t delta;
393
394 delta = ktime_sub(now, alarm->node.expires);
395
396 if (delta.tv64 < 0)
397 return 0;
398
399 if (unlikely(delta.tv64 >= interval.tv64)) {
400 s64 incr = ktime_to_ns(interval);
401
402 overrun = ktime_divns(delta, incr);
403
404 alarm->node.expires = ktime_add_ns(alarm->node.expires,
405 incr*overrun);
406
407 if (alarm->node.expires.tv64 > now.tv64)
408 return overrun;
409 /*
410 * This (and the ktime_add() below) is the
411 * correction for exact:
412 */
413 overrun++;
414 }
415
416 alarm->node.expires = ktime_add(alarm->node.expires, interval);
417 return overrun;
346} 418}
347 419
348 420
421
422
349/** 423/**
350 * clock2alarm - helper that converts from clockid to alarmtypes 424 * clock2alarm - helper that converts from clockid to alarmtypes
351 * @clockid: clockid. 425 * @clockid: clockid.
@@ -365,12 +439,21 @@ static enum alarmtimer_type clock2alarm(clockid_t clockid)
365 * 439 *
366 * Posix timer callback for expired alarm timers. 440 * Posix timer callback for expired alarm timers.
367 */ 441 */
368static void alarm_handle_timer(struct alarm *alarm) 442static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
443 ktime_t now)
369{ 444{
370 struct k_itimer *ptr = container_of(alarm, struct k_itimer, 445 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
371 it.alarmtimer); 446 it.alarm.alarmtimer);
372 if (posix_timer_event(ptr, 0) != 0) 447 if (posix_timer_event(ptr, 0) != 0)
373 ptr->it_overrun++; 448 ptr->it_overrun++;
449
450 /* Re-add periodic timers */
451 if (ptr->it.alarm.interval.tv64) {
452 ptr->it_overrun += alarm_forward(alarm, now,
453 ptr->it.alarm.interval);
454 return ALARMTIMER_RESTART;
455 }
456 return ALARMTIMER_NORESTART;
374} 457}
375 458
376/** 459/**
@@ -427,7 +510,7 @@ static int alarm_timer_create(struct k_itimer *new_timer)
427 510
428 type = clock2alarm(new_timer->it_clock); 511 type = clock2alarm(new_timer->it_clock);
429 base = &alarm_bases[type]; 512 base = &alarm_bases[type];
430 alarm_init(&new_timer->it.alarmtimer, type, alarm_handle_timer); 513 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
431 return 0; 514 return 0;
432} 515}
433 516
@@ -444,9 +527,9 @@ static void alarm_timer_get(struct k_itimer *timr,
444 memset(cur_setting, 0, sizeof(struct itimerspec)); 527 memset(cur_setting, 0, sizeof(struct itimerspec));
445 528
446 cur_setting->it_interval = 529 cur_setting->it_interval =
447 ktime_to_timespec(timr->it.alarmtimer.period); 530 ktime_to_timespec(timr->it.alarm.interval);
448 cur_setting->it_value = 531 cur_setting->it_value =
449 ktime_to_timespec(timr->it.alarmtimer.node.expires); 532 ktime_to_timespec(timr->it.alarm.alarmtimer.node.expires);
450 return; 533 return;
451} 534}
452 535
@@ -461,7 +544,9 @@ static int alarm_timer_del(struct k_itimer *timr)
461 if (!rtcdev) 544 if (!rtcdev)
462 return -ENOTSUPP; 545 return -ENOTSUPP;
463 546
464 alarm_cancel(&timr->it.alarmtimer); 547 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
548 return TIMER_RETRY;
549
465 return 0; 550 return 0;
466} 551}
467 552
@@ -481,25 +566,17 @@ static int alarm_timer_set(struct k_itimer *timr, int flags,
481 if (!rtcdev) 566 if (!rtcdev)
482 return -ENOTSUPP; 567 return -ENOTSUPP;
483 568
484 /*
485 * XXX HACK! Currently we can DOS a system if the interval
486 * period on alarmtimers is too small. Cap the interval here
487 * to 100us and solve this properly in a future patch! -jstultz
488 */
489 if ((new_setting->it_interval.tv_sec == 0) &&
490 (new_setting->it_interval.tv_nsec < 100000))
491 new_setting->it_interval.tv_nsec = 100000;
492
493 if (old_setting) 569 if (old_setting)
494 alarm_timer_get(timr, old_setting); 570 alarm_timer_get(timr, old_setting);
495 571
496 /* If the timer was already set, cancel it */ 572 /* If the timer was already set, cancel it */
497 alarm_cancel(&timr->it.alarmtimer); 573 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
574 return TIMER_RETRY;
498 575
499 /* start the timer */ 576 /* start the timer */
500 alarm_start(&timr->it.alarmtimer, 577 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
501 timespec_to_ktime(new_setting->it_value), 578 alarm_start(&timr->it.alarm.alarmtimer,
502 timespec_to_ktime(new_setting->it_interval)); 579 timespec_to_ktime(new_setting->it_value));
503 return 0; 580 return 0;
504} 581}
505 582
@@ -509,13 +586,15 @@ static int alarm_timer_set(struct k_itimer *timr, int flags,
509 * 586 *
510 * Wakes up the task that set the alarmtimer 587 * Wakes up the task that set the alarmtimer
511 */ 588 */
512static void alarmtimer_nsleep_wakeup(struct alarm *alarm) 589static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
590 ktime_t now)
513{ 591{
514 struct task_struct *task = (struct task_struct *)alarm->data; 592 struct task_struct *task = (struct task_struct *)alarm->data;
515 593
516 alarm->data = NULL; 594 alarm->data = NULL;
517 if (task) 595 if (task)
518 wake_up_process(task); 596 wake_up_process(task);
597 return ALARMTIMER_NORESTART;
519} 598}
520 599
521/** 600/**
@@ -530,7 +609,7 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
530 alarm->data = (void *)current; 609 alarm->data = (void *)current;
531 do { 610 do {
532 set_current_state(TASK_INTERRUPTIBLE); 611 set_current_state(TASK_INTERRUPTIBLE);
533 alarm_start(alarm, absexp, ktime_set(0, 0)); 612 alarm_start(alarm, absexp);
534 if (likely(alarm->data)) 613 if (likely(alarm->data))
535 schedule(); 614 schedule();
536 615
@@ -691,6 +770,7 @@ static struct platform_driver alarmtimer_driver = {
691 */ 770 */
692static int __init alarmtimer_init(void) 771static int __init alarmtimer_init(void)
693{ 772{
773 struct platform_device *pdev;
694 int error = 0; 774 int error = 0;
695 int i; 775 int i;
696 struct k_clock alarm_clock = { 776 struct k_clock alarm_clock = {
@@ -719,10 +799,26 @@ static int __init alarmtimer_init(void)
719 HRTIMER_MODE_ABS); 799 HRTIMER_MODE_ABS);
720 alarm_bases[i].timer.function = alarmtimer_fired; 800 alarm_bases[i].timer.function = alarmtimer_fired;
721 } 801 }
802
803 error = alarmtimer_rtc_interface_setup();
804 if (error)
805 return error;
806
722 error = platform_driver_register(&alarmtimer_driver); 807 error = platform_driver_register(&alarmtimer_driver);
723 platform_device_register_simple("alarmtimer", -1, NULL, 0); 808 if (error)
809 goto out_if;
724 810
811 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
812 if (IS_ERR(pdev)) {
813 error = PTR_ERR(pdev);
814 goto out_drv;
815 }
816 return 0;
817
818out_drv:
819 platform_driver_unregister(&alarmtimer_driver);
820out_if:
821 alarmtimer_rtc_interface_remove();
725 return error; 822 return error;
726} 823}
727device_initcall(alarmtimer_init); 824device_initcall(alarmtimer_init);
728