tick-oneshot.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * linux/kernel/time/tick-oneshot.c
  3. *
  4. * This file contains functions which manage high resolution tick
  5. * related events.
  6. *
  7. * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
  8. * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
  9. * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
  10. *
  11. * This code is licenced under the GPL version 2. For details see
  12. * kernel-base/COPYING.
  13. */
  14. #include <linux/cpu.h>
  15. #include <linux/err.h>
  16. #include <linux/hrtimer.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/percpu.h>
  19. #include <linux/profile.h>
  20. #include <linux/sched.h>
  21. #include <linux/tick.h>
  22. #include "tick-internal.h"
  23. /**
  24. * tick_program_event internal worker function
  25. */
  26. int tick_dev_program_event(struct clock_event_device *dev, ktime_t expires,
  27. int force)
  28. {
  29. ktime_t now = ktime_get();
  30. int i;
  31. for (i = 0;;) {
  32. int ret = clockevents_program_event(dev, expires, now);
  33. if (!ret || !force)
  34. return ret;
  35. /*
  36. * We tried 2 times to program the device with the given
  37. * min_delta_ns. If that's not working then we double it
  38. * and emit a warning.
  39. */
  40. if (++i > 2) {
  41. printk(KERN_WARNING "CE: __tick_program_event of %s is "
  42. "stuck %llx %llx\n", dev->name ? dev->name : "?",
  43. now.tv64, expires.tv64);
  44. printk(KERN_WARNING
  45. "CE: increasing min_delta_ns %ld to %ld nsec\n",
  46. dev->min_delta_ns, dev->min_delta_ns << 1);
  47. WARN_ON(1);
  48. /* Double the min. delta and try again */
  49. if (!dev->min_delta_ns)
  50. dev->min_delta_ns = 5000;
  51. else
  52. dev->min_delta_ns <<= 1;
  53. i = 0;
  54. }
  55. now = ktime_get();
  56. expires = ktime_add_ns(now, dev->min_delta_ns);
  57. }
  58. }
  59. /**
  60. * tick_program_event
  61. */
  62. int tick_program_event(ktime_t expires, int force)
  63. {
  64. struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
  65. return tick_dev_program_event(dev, expires, force);
  66. }
  67. /**
  68. * tick_resume_onshot - resume oneshot mode
  69. */
  70. void tick_resume_oneshot(void)
  71. {
  72. struct tick_device *td = &__get_cpu_var(tick_cpu_device);
  73. struct clock_event_device *dev = td->evtdev;
  74. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  75. tick_program_event(ktime_get(), 1);
  76. }
  77. /**
  78. * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
  79. */
  80. void tick_setup_oneshot(struct clock_event_device *newdev,
  81. void (*handler)(struct clock_event_device *),
  82. ktime_t next_event)
  83. {
  84. newdev->event_handler = handler;
  85. clockevents_set_mode(newdev, CLOCK_EVT_MODE_ONESHOT);
  86. tick_dev_program_event(newdev, next_event, 1);
  87. }
  88. /**
  89. * tick_switch_to_oneshot - switch to oneshot mode
  90. */
  91. int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *))
  92. {
  93. struct tick_device *td = &__get_cpu_var(tick_cpu_device);
  94. struct clock_event_device *dev = td->evtdev;
  95. if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) ||
  96. !tick_device_is_functional(dev)) {
  97. printk(KERN_INFO "Clockevents: "
  98. "could not switch to one-shot mode:");
  99. if (!dev) {
  100. printk(" no tick device\n");
  101. } else {
  102. if (!tick_device_is_functional(dev))
  103. printk(" %s is not functional.\n", dev->name);
  104. else
  105. printk(" %s does not support one-shot mode.\n",
  106. dev->name);
  107. }
  108. return -EINVAL;
  109. }
  110. td->mode = TICKDEV_MODE_ONESHOT;
  111. dev->event_handler = handler;
  112. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  113. tick_broadcast_switch_to_oneshot();
  114. return 0;
  115. }
  116. #ifdef CONFIG_HIGH_RES_TIMERS
  117. /**
  118. * tick_init_highres - switch to high resolution mode
  119. *
  120. * Called with interrupts disabled.
  121. */
  122. int tick_init_highres(void)
  123. {
  124. return tick_switch_to_oneshot(hrtimer_interrupt);
  125. }
  126. #endif