tick-oneshot.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "tick-internal.h"
  22. /* Limit min_delta to a jiffie */
  23. #define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
  24. static int tick_increase_min_delta(struct clock_event_device *dev)
  25. {
  26. /* Nothing to do if we already reached the limit */
  27. if (dev->min_delta_ns >= MIN_DELTA_LIMIT)
  28. return -ETIME;
  29. if (dev->min_delta_ns < 5000)
  30. dev->min_delta_ns = 5000;
  31. else
  32. dev->min_delta_ns += dev->min_delta_ns >> 1;
  33. if (dev->min_delta_ns > MIN_DELTA_LIMIT)
  34. dev->min_delta_ns = MIN_DELTA_LIMIT;
  35. printk(KERN_WARNING "CE: %s increased min_delta_ns to %llu nsec\n",
  36. dev->name ? dev->name : "?",
  37. (unsigned long long) dev->min_delta_ns);
  38. return 0;
  39. }
  40. /**
  41. * tick_program_event internal worker function
  42. */
  43. int tick_dev_program_event(struct clock_event_device *dev, ktime_t expires,
  44. int force)
  45. {
  46. ktime_t now = ktime_get();
  47. int i;
  48. for (i = 0;;) {
  49. int ret = clockevents_program_event(dev, expires, now);
  50. if (!ret || !force)
  51. return ret;
  52. dev->retries++;
  53. /*
  54. * We tried 3 times to program the device with the given
  55. * min_delta_ns. If that's not working then we increase it
  56. * and emit a warning.
  57. */
  58. if (++i > 2) {
  59. /* Increase the min. delta and try again */
  60. if (tick_increase_min_delta(dev)) {
  61. /*
  62. * Get out of the loop if min_delta_ns
  63. * hit the limit already. That's
  64. * better than staying here forever.
  65. *
  66. * We clear next_event so we have a
  67. * chance that the box survives.
  68. */
  69. printk(KERN_WARNING
  70. "CE: Reprogramming failure. Giving up\n");
  71. dev->next_event.tv64 = KTIME_MAX;
  72. return -ETIME;
  73. }
  74. i = 0;
  75. }
  76. now = ktime_get();
  77. expires = ktime_add_ns(now, dev->min_delta_ns);
  78. }
  79. }
  80. /**
  81. * tick_program_event
  82. */
  83. int tick_program_event(ktime_t expires, int force)
  84. {
  85. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  86. return tick_dev_program_event(dev, expires, force);
  87. }
  88. /**
  89. * tick_resume_onshot - resume oneshot mode
  90. */
  91. void tick_resume_oneshot(void)
  92. {
  93. struct tick_device *td = &__get_cpu_var(tick_cpu_device);
  94. struct clock_event_device *dev = td->evtdev;
  95. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  96. tick_program_event(ktime_get(), 1);
  97. }
  98. /**
  99. * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
  100. */
  101. void tick_setup_oneshot(struct clock_event_device *newdev,
  102. void (*handler)(struct clock_event_device *),
  103. ktime_t next_event)
  104. {
  105. newdev->event_handler = handler;
  106. clockevents_set_mode(newdev, CLOCK_EVT_MODE_ONESHOT);
  107. tick_dev_program_event(newdev, next_event, 1);
  108. }
  109. /**
  110. * tick_switch_to_oneshot - switch to oneshot mode
  111. */
  112. int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *))
  113. {
  114. struct tick_device *td = &__get_cpu_var(tick_cpu_device);
  115. struct clock_event_device *dev = td->evtdev;
  116. if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) ||
  117. !tick_device_is_functional(dev)) {
  118. printk(KERN_INFO "Clockevents: "
  119. "could not switch to one-shot mode:");
  120. if (!dev) {
  121. printk(" no tick device\n");
  122. } else {
  123. if (!tick_device_is_functional(dev))
  124. printk(" %s is not functional.\n", dev->name);
  125. else
  126. printk(" %s does not support one-shot mode.\n",
  127. dev->name);
  128. }
  129. return -EINVAL;
  130. }
  131. td->mode = TICKDEV_MODE_ONESHOT;
  132. dev->event_handler = handler;
  133. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  134. tick_broadcast_switch_to_oneshot();
  135. return 0;
  136. }
  137. /**
  138. * tick_check_oneshot_mode - check whether the system is in oneshot mode
  139. *
  140. * returns 1 when either nohz or highres are enabled. otherwise 0.
  141. */
  142. int tick_oneshot_mode_active(void)
  143. {
  144. unsigned long flags;
  145. int ret;
  146. local_irq_save(flags);
  147. ret = __this_cpu_read(tick_cpu_device.mode) == TICKDEV_MODE_ONESHOT;
  148. local_irq_restore(flags);
  149. return ret;
  150. }
  151. #ifdef CONFIG_HIGH_RES_TIMERS
  152. /**
  153. * tick_init_highres - switch to high resolution mode
  154. *
  155. * Called with interrupts disabled.
  156. */
  157. int tick_init_highres(void)
  158. {
  159. return tick_switch_to_oneshot(hrtimer_interrupt);
  160. }
  161. #endif