HPETComparator.cpp 3.5 KB

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