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. dev->next_event = KTIME_MAX;
  34. return 0;
  35. }
  36. if (unlikely(clockevent_state_oneshot_stopped(dev))) {
  37. /*
  38. * We need the clock event again, configure it in ONESHOT mode
  39. * before using it.
  40. */
  41. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  42. }
  43. return clockevents_program_event(dev, expires, force);
  44. }
  45. /**
  46. * tick_resume_onshot - resume oneshot mode
  47. */
  48. void tick_resume_oneshot(void)
  49. {
  50. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  51. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  52. clockevents_program_event(dev, ktime_get(), true);
  53. }
  54. /**
  55. * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
  56. */
  57. void tick_setup_oneshot(struct clock_event_device *newdev,
  58. void (*handler)(struct clock_event_device *),
  59. ktime_t next_event)
  60. {
  61. newdev->event_handler = handler;
  62. clockevents_switch_state(newdev, CLOCK_EVT_STATE_ONESHOT);
  63. clockevents_program_event(newdev, next_event, true);
  64. }
  65. /**
  66. * tick_switch_to_oneshot - switch to oneshot mode
  67. */
  68. int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *))
  69. {
  70. struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
  71. struct clock_event_device *dev = td->evtdev;
  72. if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) ||
  73. !tick_device_is_functional(dev)) {
  74. pr_info("Clockevents: could not switch to one-shot mode:");
  75. if (!dev) {
  76. pr_cont(" no tick device\n");
  77. } else {
  78. if (!tick_device_is_functional(dev))
  79. pr_cont(" %s is not functional.\n", dev->name);
  80. else
  81. pr_cont(" %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