HPETComparator.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Arch/x86/InterruptDisabler.h>
  7. #include <Kernel/Assertions.h>
  8. #include <Kernel/Debug.h>
  9. #include <Kernel/Sections.h>
  10. #include <Kernel/Time/HPETComparator.h>
  11. #include <Kernel/Time/TimeManagement.h>
  12. namespace Kernel {
  13. UNMAP_AFTER_INIT NonnullRefPtr<HPETComparator> HPETComparator::create(u8 number, u8 irq, bool periodic_capable, bool is_64bit_capable)
  14. {
  15. auto timer = adopt_ref(*new HPETComparator(number, irq, periodic_capable, is_64bit_capable));
  16. timer->register_interrupt_handler();
  17. return timer;
  18. }
  19. UNMAP_AFTER_INIT HPETComparator::HPETComparator(u8 number, u8 irq, bool periodic_capable, bool is_64bit_capable)
  20. : HardwareTimer(irq)
  21. , m_periodic(false)
  22. , m_periodic_capable(periodic_capable)
  23. , m_enabled(false)
  24. , m_is_64bit_capable(is_64bit_capable)
  25. , m_comparator_number(number)
  26. {
  27. }
  28. void HPETComparator::disable()
  29. {
  30. if (!m_enabled)
  31. return;
  32. m_enabled = false;
  33. HPET::the().disable(*this);
  34. }
  35. void HPETComparator::set_periodic()
  36. {
  37. VERIFY(m_periodic_capable);
  38. m_periodic = true;
  39. m_enabled = true;
  40. HPET::the().enable_periodic_interrupt(*this);
  41. }
  42. void HPETComparator::set_non_periodic()
  43. {
  44. VERIFY(m_periodic_capable);
  45. m_periodic = false;
  46. m_enabled = true;
  47. HPET::the().disable_periodic_interrupt(*this);
  48. }
  49. bool HPETComparator::handle_irq(const RegisterState& regs)
  50. {
  51. auto result = HardwareTimer::handle_irq(regs);
  52. if (!is_periodic())
  53. set_new_countdown();
  54. return result;
  55. }
  56. void HPETComparator::set_new_countdown()
  57. {
  58. VERIFY_INTERRUPTS_DISABLED();
  59. VERIFY(m_frequency <= HPET::the().frequency());
  60. HPET::the().update_non_periodic_comparator_value(*this);
  61. }
  62. size_t HPETComparator::ticks_per_second() const
  63. {
  64. return m_frequency;
  65. }
  66. void HPETComparator::reset_to_default_ticks_per_second()
  67. {
  68. dbgln("reset_to_default_ticks_per_second");
  69. m_frequency = OPTIMAL_TICKS_PER_SECOND_RATE;
  70. if (!is_periodic())
  71. set_new_countdown();
  72. else
  73. try_to_set_frequency(m_frequency);
  74. }
  75. bool HPETComparator::try_to_set_frequency(size_t frequency)
  76. {
  77. InterruptDisabler disabler;
  78. if (!is_capable_of_frequency(frequency)) {
  79. dbgln("HPETComparator: not capable of frequency: {}", frequency);
  80. return false;
  81. }
  82. auto hpet_frequency = HPET::the().frequency();
  83. VERIFY(frequency <= hpet_frequency);
  84. m_frequency = frequency;
  85. m_enabled = true;
  86. dbgln_if(HPET_COMPARATOR_DEBUG, "HPET Comparator: Max frequency {} Hz, want to set {} Hz, periodic: {}", hpet_frequency, frequency, is_periodic());
  87. if (is_periodic()) {
  88. HPET::the().update_periodic_comparator_value();
  89. } else {
  90. HPET::the().update_non_periodic_comparator_value(*this);
  91. }
  92. HPET::the().enable(*this);
  93. enable_irq(); // Enable if we haven't already
  94. return true;
  95. }
  96. bool HPETComparator::is_capable_of_frequency(size_t frequency) const
  97. {
  98. if (frequency > HPET::the().frequency())
  99. return false;
  100. // HPET::update_periodic_comparator_value and HPET::update_non_periodic_comparator_value
  101. // calculate the best counter based on the desired frequency.
  102. return true;
  103. }
  104. size_t HPETComparator::calculate_nearest_possible_frequency(size_t frequency) const
  105. {
  106. if (frequency > HPET::the().frequency())
  107. return HPET::the().frequency();
  108. // HPET::update_periodic_comparator_value and HPET::update_non_periodic_comparator_value
  109. // calculate the best counter based on the desired frequency.
  110. return frequency;
  111. }
  112. u64 HPETComparator::current_raw() const
  113. {
  114. return HPET::the().read_main_counter();
  115. }
  116. u64 HPETComparator::raw_to_ns(u64 raw_delta) const
  117. {
  118. return HPET::the().raw_counter_ticks_to_ns(raw_delta);
  119. }
  120. }