APICTimer.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/Arch/i386/CPU.h>
  27. #include <Kernel/IO.h>
  28. #include <Kernel/Interrupts/APIC.h>
  29. #include <Kernel/Scheduler.h>
  30. #include <Kernel/Thread.h>
  31. #include <Kernel/Time/APICTimer.h>
  32. #include <Kernel/Time/TimeManagement.h>
  33. namespace Kernel {
  34. #define APIC_TIMER_MEASURE_CPU_CLOCK
  35. APICTimer* APICTimer::initialize(u8 interrupt_number, HardwareTimerBase& calibration_source)
  36. {
  37. auto* timer = new APICTimer(interrupt_number, nullptr);
  38. if (!timer->calibrate(calibration_source)) {
  39. delete timer;
  40. return nullptr;
  41. }
  42. return timer;
  43. }
  44. APICTimer::APICTimer(u8 interrupt_number, Function<void(const RegisterState&)> callback)
  45. : HardwareTimer<GenericInterruptHandler>(interrupt_number, move(callback))
  46. {
  47. disable_remap();
  48. }
  49. bool APICTimer::calibrate(HardwareTimerBase& calibration_source)
  50. {
  51. ASSERT_INTERRUPTS_DISABLED();
  52. klog() << "APICTimer: Using " << calibration_source.model() << " as calibration source";
  53. auto& apic = APIC::the();
  54. #ifdef APIC_TIMER_MEASURE_CPU_CLOCK
  55. bool supports_tsc = Processor::current().has_feature(CPUFeature::TSC);
  56. #endif
  57. // temporarily replace the timer callbacks
  58. const size_t ticks_in_100ms = calibration_source.ticks_per_second() / 10;
  59. Atomic<size_t> calibration_ticks = 0;
  60. #ifdef APIC_TIMER_MEASURE_CPU_CLOCK
  61. volatile u64 start_tsc = 0, end_tsc = 0;
  62. #endif
  63. volatile u32 start_apic_count = 0, end_apic_count = 0;
  64. auto original_source_callback = calibration_source.set_callback([&](const RegisterState&) {
  65. u32 current_timer_count = apic.get_timer_current_count();
  66. #ifdef APIC_TIMER_MEASURE_CPU_CLOCK
  67. u64 current_tsc = supports_tsc ? read_tsc() : 0;
  68. #endif
  69. auto prev_tick = calibration_ticks.fetch_add(1, AK::memory_order_acq_rel);
  70. if (prev_tick == 0) {
  71. #ifdef APIC_TIMER_MEASURE_CPU_CLOCK
  72. start_tsc = current_tsc;
  73. #endif
  74. start_apic_count = current_timer_count;
  75. } else if (prev_tick + 1 == ticks_in_100ms) {
  76. #ifdef APIC_TIMER_MEASURE_CPU_CLOCK
  77. end_tsc = current_tsc;
  78. #endif
  79. end_apic_count = current_timer_count;
  80. }
  81. });
  82. // Setup a counter that should be much longer than our calibration time.
  83. // We don't want the APIC timer to actually fire. We do however want the
  84. // calbibration_source timer to fire so that we can read the current
  85. // tick count from the APIC timer
  86. auto original_callback = set_callback([&](const RegisterState&) {
  87. klog() << "APICTimer: Timer fired during calibration!";
  88. ASSERT_NOT_REACHED(); // TODO: How should we handle this?
  89. });
  90. apic.setup_local_timer(0xffffffff, APIC::TimerMode::Periodic, true);
  91. sti();
  92. // Loop for about 100 ms
  93. while (calibration_ticks.load(AK::memory_order_relaxed) < ticks_in_100ms)
  94. ;
  95. cli();
  96. // Restore timer callbacks
  97. calibration_source.set_callback(move(original_source_callback));
  98. set_callback(move(original_callback));
  99. disable_local_timer();
  100. auto delta_apic_count = end_apic_count - start_apic_count;
  101. m_timer_period = (delta_apic_count * apic.get_timer_divisor()) / ticks_in_100ms;
  102. auto apic_freq = (delta_apic_count * apic.get_timer_divisor()) / apic.get_timer_divisor();
  103. if (apic_freq < 1000000) {
  104. klog() << "APICTimer: Frequency too slow!";
  105. return false;
  106. }
  107. klog() << "APICTimer: Bus clock speed: " << (apic_freq / 1000000) << "." << (apic_freq % 1000000) << " MHz";
  108. #ifdef APIC_TIMER_MEASURE_CPU_CLOCK
  109. if (supports_tsc) {
  110. auto delta_tsc = end_tsc - start_tsc;
  111. klog() << "APICTimer: CPU clock speed: " << (delta_tsc / 1000000) << "." << (delta_tsc % 1000000) << " MHz";
  112. }
  113. #endif
  114. enable_local_timer();
  115. return true;
  116. }
  117. void APICTimer::enable_local_timer()
  118. {
  119. APIC::the().setup_local_timer(m_timer_period, m_timer_mode, true);
  120. }
  121. void APICTimer::disable_local_timer()
  122. {
  123. APIC::the().setup_local_timer(0, APIC::TimerMode::OneShot, false);
  124. }
  125. size_t APICTimer::ticks_per_second() const
  126. {
  127. return m_frequency;
  128. }
  129. void APICTimer::set_periodic()
  130. {
  131. // FIXME: Implement it...
  132. ASSERT_NOT_REACHED();
  133. }
  134. void APICTimer::set_non_periodic()
  135. {
  136. // FIXME: Implement it...
  137. ASSERT_NOT_REACHED();
  138. }
  139. void APICTimer::reset_to_default_ticks_per_second()
  140. {
  141. }
  142. bool APICTimer::try_to_set_frequency(size_t frequency)
  143. {
  144. (void)frequency;
  145. return true;
  146. }
  147. bool APICTimer::is_capable_of_frequency(size_t frequency) const
  148. {
  149. (void)frequency;
  150. return false;
  151. }
  152. size_t APICTimer::calculate_nearest_possible_frequency(size_t frequency) const
  153. {
  154. (void)frequency;
  155. return 0;
  156. }
  157. }