tick-oneshot.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/irq.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
  25. */
  26. int tick_program_event(ktime_t expires, int force)
  27. {
  28. struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
  29. ktime_t now = ktime_get();
  30. while (1) {
  31. int ret = clockevents_program_event(dev, expires, now);
  32. if (!ret || !force)
  33. return ret;
  34. now = ktime_get();
  35. expires = ktime_add(now, ktime_set(0, dev->min_delta_ns));
  36. }
  37. }
  38. /**
  39. * tick_resume_onshot - resume oneshot mode
  40. */
  41. void tick_resume_oneshot(void)
  42. {
  43. struct tick_device *td = &__get_cpu_var(tick_cpu_device);
  44. struct clock_event_device *dev = td->evtdev;
  45. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  46. tick_program_event(ktime_get(), 1);
  47. }
  48. /**
  49. * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
  50. */
  51. void tick_setup_oneshot(struct clock_event_device *newdev,
  52. void (*handler)(struct clock_event_device *),
  53. ktime_t next_event)
  54. {
  55. newdev->event_handler = handler;
  56. clockevents_set_mode(newdev, CLOCK_EVT_MODE_ONESHOT);
  57. clockevents_program_event(newdev, next_event, ktime_get());
  58. }
  59. /**
  60. * tick_switch_to_oneshot - switch to oneshot mode
  61. */
  62. int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *))
  63. {
  64. struct tick_device *td = &__get_cpu_var(tick_cpu_device);
  65. struct clock_event_device *dev = td->evtdev;
  66. if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) ||
  67. !tick_device_is_functional(dev))
  68. return -EINVAL;
  69. td->mode = TICKDEV_MODE_ONESHOT;
  70. dev->event_handler = handler;
  71. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  72. tick_broadcast_switch_to_oneshot();
  73. return 0;
  74. }
  75. #ifdef CONFIG_HIGH_RES_TIMERS
  76. /**
  77. * tick_init_highres - switch to high resolution mode
  78. *
  79. * Called with interrupts disabled.
  80. */
  81. int tick_init_highres(void)
  82. {
  83. return tick_switch_to_oneshot(hrtimer_interrupt);
  84. }
  85. #endif