HPETComparator.cpp 3.6 KB

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