tick-oneshot.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /**
  23. * tick_program_event
  24. */
  25. int tick_program_event(ktime_t expires, int force)
  26. {
  27. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  28. if (unlikely(expires == KTIME_MAX)) {
  29. /*
  30. * We don't need the clock event device any more, stop it.
  31. */
  32. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT_STOPPED);
  33. return 0;
  34. }
  35. if (unlikely(clockevent_state_oneshot_stopped(dev))) {
  36. /*
  37. * We need the clock event again, configure it in ONESHOT mode
  38. * before using it.
  39. */
  40. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  41. }
  42. return clockevents_program_event(dev, expires, force);
  43. }
  44. /**
  45. * tick_resume_onshot - resume oneshot mode
  46. */
  47. void tick_resume_oneshot(void)
  48. {
  49. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  50. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  51. clockevents_program_event(dev, ktime_get(), true);
  52. }
  53. /**
  54. * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
  55. */
  56. void tick_setup_oneshot(struct clock_event_device *newdev,
  57. void (*handler)(struct clock_event_device *),
  58. ktime_t next_event)
  59. {
  60. newdev->event_handler = handler;
  61. clockevents_switch_state(newdev, CLOCK_EVT_STATE_ONESHOT);
  62. clockevents_program_event(newdev, next_event, true);
  63. }
  64. /**
  65. * tick_switch_to_oneshot - switch to oneshot mode
  66. */
  67. int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *))
  68. {
  69. struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
  70. struct clock_event_device *dev = td->evtdev;
  71. if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) ||
  72. !tick_device_is_functional(dev)) {
  73. printk(KERN_INFO "Clockevents: "
  74. "could not switch to one-shot mode:");
  75. if (!dev) {
  76. printk(" no tick device\n");
  77. } else {
  78. if (!tick_device_is_functional(dev))
  79. printk(" %s is not functional.\n", dev->name);
  80. else
  81. printk(" %s does not support one-shot mode.\n",
  82. dev->name);
  83. }
  84. return -EINVAL;
  85. }
  86. td->mode = TICKDEV_MODE_ONESHOT;
  87. dev->event_handler = handler;
  88. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  89. tick_broadcast_switch_to_oneshot();
  90. return 0;
  91. }
  92. /**
  93. * tick_check_oneshot_mode - check whether the system is in oneshot mode
  94. *
  95. * returns 1 when either nohz or highres are enabled. otherwise 0.
  96. */
  97. int tick_oneshot_mode_active(void)
  98. {
  99. unsigned long flags;
  100. int ret;
  101. local_irq_save(flags);
  102. ret = __this_cpu_read(tick_cpu_device.mode) == TICKDEV_MODE_ONESHOT;
  103. local_irq_restore(flags);
  104. return ret;
  105. }
  106. #ifdef CONFIG_HIGH_RES_TIMERS
  107. /**
  108. * tick_init_highres - switch to high resolution mode
  109. *
  110. * Called with interrupts disabled.
  111. */
  112. int tick_init_highres(void)
  113. {
  114. return tick_switch_to_oneshot(hrtimer_interrupt);
  115. }
  116. #endif