Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
alarmtimer.c
Go to the documentation of this file.
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 <[email protected]>
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>
28 
37 static struct alarm_base {
39  struct timerqueue_head timerqueue;
40  ktime_t (*gettime)(void);
41  clockid_t base_clockid;
42 } alarm_bases[ALARM_NUMTYPE];
43 
44 /* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
45 static ktime_t freezer_delta;
46 static DEFINE_SPINLOCK(freezer_delta_lock);
47 
48 static struct wakeup_source *ws;
49 
50 #ifdef CONFIG_RTC_CLASS
51 /* rtc timer and device for setting alarm wakeups at suspend */
52 static struct rtc_timer rtctimer;
53 static struct rtc_device *rtcdev;
54 static DEFINE_SPINLOCK(rtcdev_lock);
55 
64 {
65  unsigned long flags;
66  struct rtc_device *ret;
67 
68  spin_lock_irqsave(&rtcdev_lock, flags);
69  ret = rtcdev;
70  spin_unlock_irqrestore(&rtcdev_lock, flags);
71 
72  return ret;
73 }
74 
75 
76 static int alarmtimer_rtc_add_device(struct device *dev,
77  struct class_interface *class_intf)
78 {
79  unsigned long flags;
80  struct rtc_device *rtc = to_rtc_device(dev);
81 
82  if (rtcdev)
83  return -EBUSY;
84 
85  if (!rtc->ops->set_alarm)
86  return -1;
87  if (!device_may_wakeup(rtc->dev.parent))
88  return -1;
89 
90  spin_lock_irqsave(&rtcdev_lock, flags);
91  if (!rtcdev) {
92  rtcdev = rtc;
93  /* hold a reference so it doesn't go away */
94  get_device(dev);
95  }
96  spin_unlock_irqrestore(&rtcdev_lock, flags);
97  return 0;
98 }
99 
100 static inline void alarmtimer_rtc_timer_init(void)
101 {
102  rtc_timer_init(&rtctimer, NULL, NULL);
103 }
104 
105 static struct class_interface alarmtimer_rtc_interface = {
106  .add_dev = &alarmtimer_rtc_add_device,
107 };
108 
109 static int alarmtimer_rtc_interface_setup(void)
110 {
111  alarmtimer_rtc_interface.class = rtc_class;
112  return class_interface_register(&alarmtimer_rtc_interface);
113 }
114 static void alarmtimer_rtc_interface_remove(void)
115 {
116  class_interface_unregister(&alarmtimer_rtc_interface);
117 }
118 #else
120 {
121  return NULL;
122 }
123 #define rtcdev (NULL)
124 static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
125 static inline void alarmtimer_rtc_interface_remove(void) { }
126 static inline void alarmtimer_rtc_timer_init(void) { }
127 #endif
128 
138 static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
139 {
140  if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
141  timerqueue_del(&base->timerqueue, &alarm->node);
142 
143  timerqueue_add(&base->timerqueue, &alarm->node);
145 }
146 
156 static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
157 {
158  if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
159  return;
160 
161  timerqueue_del(&base->timerqueue, &alarm->node);
162  alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
163 }
164 
165 
175 static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
176 {
177  struct alarm *alarm = container_of(timer, struct alarm, timer);
178  struct alarm_base *base = &alarm_bases[alarm->type];
179  unsigned long flags;
180  int ret = HRTIMER_NORESTART;
182 
183  spin_lock_irqsave(&base->lock, flags);
184  alarmtimer_dequeue(base, alarm);
185  spin_unlock_irqrestore(&base->lock, flags);
186 
187  if (alarm->function)
188  restart = alarm->function(alarm, base->gettime());
189 
190  spin_lock_irqsave(&base->lock, flags);
191  if (restart != ALARMTIMER_NORESTART) {
192  hrtimer_set_expires(&alarm->timer, alarm->node.expires);
193  alarmtimer_enqueue(base, alarm);
194  ret = HRTIMER_RESTART;
195  }
196  spin_unlock_irqrestore(&base->lock, flags);
197 
198  return ret;
199 
200 }
201 
202 #ifdef CONFIG_RTC_CLASS
203 
213 static int alarmtimer_suspend(struct device *dev)
214 {
215  struct rtc_time tm;
216  ktime_t min, now;
217  unsigned long flags;
218  struct rtc_device *rtc;
219  int i;
220  int ret;
221 
222  spin_lock_irqsave(&freezer_delta_lock, flags);
223  min = freezer_delta;
224  freezer_delta = ktime_set(0, 0);
225  spin_unlock_irqrestore(&freezer_delta_lock, flags);
226 
227  rtc = alarmtimer_get_rtcdev();
228  /* If we have no rtcdev, just return */
229  if (!rtc)
230  return 0;
231 
232  /* Find the soonest timer to expire*/
233  for (i = 0; i < ALARM_NUMTYPE; i++) {
234  struct alarm_base *base = &alarm_bases[i];
235  struct timerqueue_node *next;
236  ktime_t delta;
237 
238  spin_lock_irqsave(&base->lock, flags);
239  next = timerqueue_getnext(&base->timerqueue);
240  spin_unlock_irqrestore(&base->lock, flags);
241  if (!next)
242  continue;
243  delta = ktime_sub(next->expires, base->gettime());
244  if (!min.tv64 || (delta.tv64 < min.tv64))
245  min = delta;
246  }
247  if (min.tv64 == 0)
248  return 0;
249 
250  if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
252  return -EBUSY;
253  }
254 
255  /* Setup an rtc timer to fire that far in the future */
256  rtc_timer_cancel(rtc, &rtctimer);
257  rtc_read_time(rtc, &tm);
258  now = rtc_tm_to_ktime(tm);
259  now = ktime_add(now, min);
260 
261  /* Set alarm, if in the past reject suspend briefly to handle */
262  ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
263  if (ret < 0)
265  return ret;
266 }
267 #else
268 static int alarmtimer_suspend(struct device *dev)
269 {
270  return 0;
271 }
272 #endif
273 
274 static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
275 {
276  ktime_t delta;
277  unsigned long flags;
278  struct alarm_base *base = &alarm_bases[type];
279 
280  delta = ktime_sub(absexp, base->gettime());
281 
282  spin_lock_irqsave(&freezer_delta_lock, flags);
283  if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
284  freezer_delta = delta;
285  spin_unlock_irqrestore(&freezer_delta_lock, flags);
286 }
287 
288 
295 void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
296  enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
297 {
298  timerqueue_init(&alarm->node);
299  hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
301  alarm->timer.function = alarmtimer_fired;
302  alarm->function = function;
303  alarm->type = type;
305 }
306 
312 int alarm_start(struct alarm *alarm, ktime_t start)
313 {
314  struct alarm_base *base = &alarm_bases[alarm->type];
315  unsigned long flags;
316  int ret;
317 
318  spin_lock_irqsave(&base->lock, flags);
319  alarm->node.expires = start;
320  alarmtimer_enqueue(base, alarm);
321  ret = hrtimer_start(&alarm->timer, alarm->node.expires,
323  spin_unlock_irqrestore(&base->lock, flags);
324  return ret;
325 }
326 
334 int alarm_try_to_cancel(struct alarm *alarm)
335 {
336  struct alarm_base *base = &alarm_bases[alarm->type];
337  unsigned long flags;
338  int ret;
339 
340  spin_lock_irqsave(&base->lock, flags);
341  ret = hrtimer_try_to_cancel(&alarm->timer);
342  if (ret >= 0)
343  alarmtimer_dequeue(base, alarm);
344  spin_unlock_irqrestore(&base->lock, flags);
345  return ret;
346 }
347 
348 
355 int alarm_cancel(struct alarm *alarm)
356 {
357  for (;;) {
358  int ret = alarm_try_to_cancel(alarm);
359  if (ret >= 0)
360  return ret;
361  cpu_relax();
362  }
363 }
364 
365 
366 u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
367 {
368  u64 overrun = 1;
369  ktime_t delta;
370 
371  delta = ktime_sub(now, alarm->node.expires);
372 
373  if (delta.tv64 < 0)
374  return 0;
375 
376  if (unlikely(delta.tv64 >= interval.tv64)) {
377  s64 incr = ktime_to_ns(interval);
378 
379  overrun = ktime_divns(delta, incr);
380 
381  alarm->node.expires = ktime_add_ns(alarm->node.expires,
382  incr*overrun);
383 
384  if (alarm->node.expires.tv64 > now.tv64)
385  return overrun;
386  /*
387  * This (and the ktime_add() below) is the
388  * correction for exact:
389  */
390  overrun++;
391  }
392 
393  alarm->node.expires = ktime_add(alarm->node.expires, interval);
394  return overrun;
395 }
396 
397 
398 
399 
404 static enum alarmtimer_type clock2alarm(clockid_t clockid)
405 {
406  if (clockid == CLOCK_REALTIME_ALARM)
407  return ALARM_REALTIME;
408  if (clockid == CLOCK_BOOTTIME_ALARM)
409  return ALARM_BOOTTIME;
410  return -1;
411 }
412 
419 static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
420  ktime_t now)
421 {
422  struct k_itimer *ptr = container_of(alarm, struct k_itimer,
423  it.alarm.alarmtimer);
424  if (posix_timer_event(ptr, 0) != 0)
425  ptr->it_overrun++;
426 
427  /* Re-add periodic timers */
428  if (ptr->it.alarm.interval.tv64) {
429  ptr->it_overrun += alarm_forward(alarm, now,
430  ptr->it.alarm.interval);
431  return ALARMTIMER_RESTART;
432  }
433  return ALARMTIMER_NORESTART;
434 }
435 
443 static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
444 {
445  clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
446 
447  if (!alarmtimer_get_rtcdev())
448  return -ENOTSUPP;
449 
450  return hrtimer_get_res(baseid, tp);
451 }
452 
460 static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
461 {
462  struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
463 
464  if (!alarmtimer_get_rtcdev())
465  return -ENOTSUPP;
466 
467  *tp = ktime_to_timespec(base->gettime());
468  return 0;
469 }
470 
477 static int alarm_timer_create(struct k_itimer *new_timer)
478 {
479  enum alarmtimer_type type;
480  struct alarm_base *base;
481 
482  if (!alarmtimer_get_rtcdev())
483  return -ENOTSUPP;
484 
485  if (!capable(CAP_WAKE_ALARM))
486  return -EPERM;
487 
488  type = clock2alarm(new_timer->it_clock);
489  base = &alarm_bases[type];
490  alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
491  return 0;
492 }
493 
501 static void alarm_timer_get(struct k_itimer *timr,
502  struct itimerspec *cur_setting)
503 {
504  memset(cur_setting, 0, sizeof(struct itimerspec));
505 
506  cur_setting->it_interval =
507  ktime_to_timespec(timr->it.alarm.interval);
508  cur_setting->it_value =
509  ktime_to_timespec(timr->it.alarm.alarmtimer.node.expires);
510  return;
511 }
512 
519 static int alarm_timer_del(struct k_itimer *timr)
520 {
521  if (!rtcdev)
522  return -ENOTSUPP;
523 
524  if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
525  return TIMER_RETRY;
526 
527  return 0;
528 }
529 
539 static int alarm_timer_set(struct k_itimer *timr, int flags,
540  struct itimerspec *new_setting,
541  struct itimerspec *old_setting)
542 {
543  if (!rtcdev)
544  return -ENOTSUPP;
545 
546  if (old_setting)
547  alarm_timer_get(timr, old_setting);
548 
549  /* If the timer was already set, cancel it */
550  if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
551  return TIMER_RETRY;
552 
553  /* start the timer */
554  timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
555  alarm_start(&timr->it.alarm.alarmtimer,
556  timespec_to_ktime(new_setting->it_value));
557  return 0;
558 }
559 
566 static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
567  ktime_t now)
568 {
569  struct task_struct *task = (struct task_struct *)alarm->data;
570 
571  alarm->data = NULL;
572  if (task)
573  wake_up_process(task);
574  return ALARMTIMER_NORESTART;
575 }
576 
584 static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
585 {
586  alarm->data = (void *)current;
587  do {
589  alarm_start(alarm, absexp);
590  if (likely(alarm->data))
591  schedule();
592 
593  alarm_cancel(alarm);
594  } while (alarm->data && !signal_pending(current));
595 
597 
598  return (alarm->data == NULL);
599 }
600 
601 
611 static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
612  struct timespec __user *rmtp)
613 {
614  struct timespec rmt;
615  ktime_t rem;
616 
617  rem = ktime_sub(exp, alarm_bases[type].gettime());
618 
619  if (rem.tv64 <= 0)
620  return 0;
621  rmt = ktime_to_timespec(rem);
622 
623  if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
624  return -EFAULT;
625 
626  return 1;
627 
628 }
629 
636 static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
637 {
638  enum alarmtimer_type type = restart->nanosleep.clockid;
639  ktime_t exp;
640  struct timespec __user *rmtp;
641  struct alarm alarm;
642  int ret = 0;
643 
644  exp.tv64 = restart->nanosleep.expires;
645  alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
646 
647  if (alarmtimer_do_nsleep(&alarm, exp))
648  goto out;
649 
650  if (freezing(current))
651  alarmtimer_freezerset(exp, type);
652 
653  rmtp = restart->nanosleep.rmtp;
654  if (rmtp) {
655  ret = update_rmtp(exp, type, rmtp);
656  if (ret <= 0)
657  goto out;
658  }
659 
660 
661  /* The other values in restart are already filled in */
662  ret = -ERESTART_RESTARTBLOCK;
663 out:
664  return ret;
665 }
666 
676 static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
677  struct timespec *tsreq, struct timespec __user *rmtp)
678 {
679  enum alarmtimer_type type = clock2alarm(which_clock);
680  struct alarm alarm;
681  ktime_t exp;
682  int ret = 0;
683  struct restart_block *restart;
684 
685  if (!alarmtimer_get_rtcdev())
686  return -ENOTSUPP;
687 
688  if (!capable(CAP_WAKE_ALARM))
689  return -EPERM;
690 
691  alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
692 
693  exp = timespec_to_ktime(*tsreq);
694  /* Convert (if necessary) to absolute time */
695  if (flags != TIMER_ABSTIME) {
696  ktime_t now = alarm_bases[type].gettime();
697  exp = ktime_add(now, exp);
698  }
699 
700  if (alarmtimer_do_nsleep(&alarm, exp))
701  goto out;
702 
703  if (freezing(current))
704  alarmtimer_freezerset(exp, type);
705 
706  /* abs timers don't set remaining time or restart */
707  if (flags == TIMER_ABSTIME) {
708  ret = -ERESTARTNOHAND;
709  goto out;
710  }
711 
712  if (rmtp) {
713  ret = update_rmtp(exp, type, rmtp);
714  if (ret <= 0)
715  goto out;
716  }
717 
718  restart = &current_thread_info()->restart_block;
719  restart->fn = alarm_timer_nsleep_restart;
720  restart->nanosleep.clockid = type;
721  restart->nanosleep.expires = exp.tv64;
722  restart->nanosleep.rmtp = rmtp;
723  ret = -ERESTART_RESTARTBLOCK;
724 
725 out:
726  return ret;
727 }
728 
729 
730 /* Suspend hook structures */
731 static const struct dev_pm_ops alarmtimer_pm_ops = {
732  .suspend = alarmtimer_suspend,
733 };
734 
735 static struct platform_driver alarmtimer_driver = {
736  .driver = {
737  .name = "alarmtimer",
738  .pm = &alarmtimer_pm_ops,
739  }
740 };
741 
748 static int __init alarmtimer_init(void)
749 {
750  struct platform_device *pdev;
751  int error = 0;
752  int i;
753  struct k_clock alarm_clock = {
754  .clock_getres = alarm_clock_getres,
755  .clock_get = alarm_clock_get,
756  .timer_create = alarm_timer_create,
757  .timer_set = alarm_timer_set,
758  .timer_del = alarm_timer_del,
759  .timer_get = alarm_timer_get,
760  .nsleep = alarm_timer_nsleep,
761  };
762 
763  alarmtimer_rtc_timer_init();
764 
767 
768  /* Initialize alarm bases */
769  alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
770  alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
771  alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
772  alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
773  for (i = 0; i < ALARM_NUMTYPE; i++) {
774  timerqueue_init_head(&alarm_bases[i].timerqueue);
775  spin_lock_init(&alarm_bases[i].lock);
776  }
777 
778  error = alarmtimer_rtc_interface_setup();
779  if (error)
780  return error;
781 
782  error = platform_driver_register(&alarmtimer_driver);
783  if (error)
784  goto out_if;
785 
786  pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
787  if (IS_ERR(pdev)) {
788  error = PTR_ERR(pdev);
789  goto out_drv;
790  }
791  ws = wakeup_source_register("alarmtimer");
792  return 0;
793 
794 out_drv:
795  platform_driver_unregister(&alarmtimer_driver);
796 out_if:
797  alarmtimer_rtc_interface_remove();
798  return error;
799 }
800 device_initcall(alarmtimer_init);