tick-oneshot.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file contains functions which manage high resolution tick
  4. * related events.
  5. *
  6. * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
  7. * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
  8. * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
  9. */
  10. #include <linux/cpu.h>
  11. #include <linux/err.h>
  12. #include <linux/hrtimer.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/percpu.h>
  15. #include <linux/profile.h>
  16. #include <linux/sched.h>
  17. #include "tick-internal.h"
  18. /**
  19. * tick_program_event
  20. */
  21. int tick_program_event(ktime_t expires, int force)
  22. {
  23. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  24. if (unlikely(expires == KTIME_MAX)) {
  25. /*
  26. * We don't need the clock event device any more, stop it.
  27. */
  28. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT_STOPPED);
  29. dev->next_event = KTIME_MAX;
  30. return 0;
  31. }
  32. if (unlikely(clockevent_state_oneshot_stopped(dev))) {
  33. /*
  34. * We need the clock event again, configure it in ONESHOT mode
  35. * before using it.
  36. */
  37. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  38. }
  39. return clockevents_program_event(dev, expires, force);
  40. }
  41. /**
  42. * tick_resume_onshot - resume oneshot mode
  43. */
  44. void tick_resume_oneshot(void)
  45. {
  46. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  47. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  48. clockevents_program_event(dev, ktime_get(), true);
  49. }
  50. /**
  51. * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz)
  52. */
  53. void tick_setup_oneshot(struct clock_event_device *newdev,
  54. void (*handler)(struct clock_event_device *),
  55. ktime_t next_event)
  56. {
  57. newdev->event_handler = handler;
  58. clockevents_switch_state(newdev, CLOCK_EVT_STATE_ONESHOT);
  59. clockevents_program_event(newdev, next_event, true);
  60. }
  61. /**
  62. * tick_switch_to_oneshot - switch to oneshot mode
  63. */
  64. int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *))
  65. {
  66. struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
  67. struct clock_event_device *dev = td->evtdev;
  68. if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) ||
  69. !tick_device_is_functional(dev)) {
  70. pr_info("Clockevents: could not switch to one-shot mode:");
  71. if (!dev) {
  72. pr_cont(" no tick device\n");
  73. } else {
  74. if (!tick_device_is_functional(dev))
  75. pr_cont(" %s is not functional.\n", dev->name);
  76. else
  77. pr_cont(" %s does not support one-shot mode.\n",
  78. dev->name);
  79. }
  80. return -EINVAL;
  81. }
  82. td->mode = TICKDEV_MODE_ONESHOT;
  83. dev->event_handler = handler;
  84. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  85. tick_broadcast_switch_to_oneshot();
  86. return 0;
  87. }
  88. /**
  89. * tick_check_oneshot_mode - check whether the system is in oneshot mode
  90. *
  91. * returns 1 when either nohz or highres are enabled. otherwise 0.
  92. */
  93. int tick_oneshot_mode_active(void)
  94. {
  95. unsigned long flags;
  96. int ret;
  97. local_irq_save(flags);
  98. ret = __this_cpu_read(tick_cpu_device.mode) == TICKDEV_MODE_ONESHOT;
  99. local_irq_restore(flags);
  100. return ret;
  101. }
  102. #ifdef CONFIG_HIGH_RES_TIMERS
  103. /**
  104. * tick_init_highres - switch to high resolution mode
  105. *
  106. * Called with interrupts disabled.
  107. */
  108. int tick_init_highres(void)
  109. {
  110. return tick_switch_to_oneshot(hrtimer_interrupt);
  111. }
  112. #endif