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