APICTimer.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/x86/CPU.h>
  27. #include <Kernel/IO.h>
  28. #include <Kernel/Interrupts/APIC.h>
  29. #include <Kernel/Panic.h>
  30. #include <Kernel/Scheduler.h>
  31. #include <Kernel/Thread.h>
  32. #include <Kernel/Time/APICTimer.h>
  33. #include <Kernel/Time/TimeManagement.h>
  34. namespace Kernel {
  35. #define APIC_TIMER_MEASURE_CPU_CLOCK
  36. UNMAP_AFTER_INIT APICTimer* APICTimer::initialize(u8 interrupt_number, HardwareTimerBase& calibration_source)
  37. {
  38. auto timer = adopt(*new APICTimer(interrupt_number, nullptr));
  39. timer->register_interrupt_handler();
  40. if (!timer->calibrate(calibration_source)) {
  41. return nullptr;
  42. }
  43. return &timer.leak_ref();
  44. }
  45. UNMAP_AFTER_INIT APICTimer::APICTimer(u8 interrupt_number, Function<void(const RegisterState&)> callback)
  46. : HardwareTimer<GenericInterruptHandler>(interrupt_number, move(callback))
  47. {
  48. disable_remap();
  49. }
  50. UNMAP_AFTER_INIT bool APICTimer::calibrate(HardwareTimerBase& calibration_source)
  51. {
  52. VERIFY_INTERRUPTS_DISABLED();
  53. dmesgln("APICTimer: Using {} as calibration source", calibration_source.model());
  54. auto& apic = APIC::the();
  55. #ifdef APIC_TIMER_MEASURE_CPU_CLOCK
  56. bool supports_tsc = Processor::current().has_feature(CPUFeature::TSC);
  57. #endif
  58. // temporarily replace the timer callbacks
  59. const size_t ticks_in_100ms = calibration_source.ticks_per_second() / 10;
  60. Atomic<size_t, AK::memory_order_relaxed> calibration_ticks = 0;
  61. #ifdef APIC_TIMER_MEASURE_CPU_CLOCK
  62. volatile u64 start_tsc = 0, end_tsc = 0;
  63. #endif
  64. volatile u64 start_reference = 0, end_reference = 0;
  65. volatile u32 start_apic_count = 0, end_apic_count = 0;
  66. bool query_reference = calibration_source.can_query_raw();
  67. auto original_source_callback = calibration_source.set_callback([&](const RegisterState&) {
  68. u32 current_timer_count = apic.get_timer_current_count();
  69. #ifdef APIC_TIMER_MEASURE_CPU_CLOCK
  70. u64 current_tsc = supports_tsc ? read_tsc() : 0;
  71. #endif
  72. u64 current_reference = query_reference ? calibration_source.current_raw() : 0;
  73. auto prev_tick = calibration_ticks.fetch_add(1);
  74. if (prev_tick == 0) {
  75. #ifdef APIC_TIMER_MEASURE_CPU_CLOCK
  76. start_tsc = current_tsc;
  77. #endif
  78. start_apic_count = current_timer_count;
  79. start_reference = current_reference;
  80. } else if (prev_tick + 1 == ticks_in_100ms + 1) {
  81. #ifdef APIC_TIMER_MEASURE_CPU_CLOCK
  82. end_tsc = current_tsc;
  83. #endif
  84. end_apic_count = current_timer_count;
  85. end_reference = current_reference;
  86. }
  87. });
  88. // Setup a counter that should be much longer than our calibration time.
  89. // We don't want the APIC timer to actually fire. We do however want the
  90. // calbibration_source timer to fire so that we can read the current
  91. // tick count from the APIC timer
  92. auto original_callback = set_callback([&](const RegisterState&) {
  93. // TODO: How should we handle this?
  94. PANIC("APICTimer: Timer fired during calibration!");
  95. });
  96. apic.setup_local_timer(0xffffffff, APIC::TimerMode::Periodic, true);
  97. sti();
  98. // Loop for about 100 ms
  99. while (calibration_ticks.load() <= ticks_in_100ms)
  100. ;
  101. cli();
  102. // Restore timer callbacks
  103. calibration_source.set_callback(move(original_source_callback));
  104. set_callback(move(original_callback));
  105. disable_local_timer();
  106. if (query_reference) {
  107. u64 one_tick_ns = calibration_source.raw_to_ns((end_reference - start_reference) / ticks_in_100ms);
  108. m_frequency = (u32)(1000000000ull / one_tick_ns);
  109. dmesgln("APICTimer: Ticks per second: {} ({}.{}ms)", m_frequency, one_tick_ns / 1000000, one_tick_ns % 1000000);
  110. } else {
  111. // For now, assume the frequency is exactly the same
  112. m_frequency = calibration_source.ticks_per_second();
  113. dmesgln("APICTimer: Ticks per second: {} (assume same frequency as reference clock)", m_frequency);
  114. }
  115. auto delta_apic_count = start_apic_count - end_apic_count; // The APIC current count register decrements!
  116. m_timer_period = (delta_apic_count * apic.get_timer_divisor()) / ticks_in_100ms;
  117. u64 apic_freq = delta_apic_count * apic.get_timer_divisor() * 10;
  118. dmesgln("APICTimer: Bus clock speed: {}.{} MHz", apic_freq / 1000000, apic_freq % 1000000);
  119. if (apic_freq < 1000000) {
  120. dmesgln("APICTimer: Frequency too slow!");
  121. return false;
  122. }
  123. #ifdef APIC_TIMER_MEASURE_CPU_CLOCK
  124. if (supports_tsc) {
  125. auto delta_tsc = (end_tsc - start_tsc) * 10;
  126. dmesgln("APICTimer: CPU clock speed: {}.{} MHz", delta_tsc / 1000000, delta_tsc % 1000000);
  127. }
  128. #endif
  129. enable_local_timer();
  130. return true;
  131. }
  132. void APICTimer::enable_local_timer()
  133. {
  134. APIC::the().setup_local_timer(m_timer_period, m_timer_mode, true);
  135. }
  136. void APICTimer::disable_local_timer()
  137. {
  138. APIC::the().setup_local_timer(0, APIC::TimerMode::OneShot, false);
  139. }
  140. size_t APICTimer::ticks_per_second() const
  141. {
  142. return m_frequency;
  143. }
  144. void APICTimer::set_periodic()
  145. {
  146. // FIXME: Implement it...
  147. VERIFY_NOT_REACHED();
  148. }
  149. void APICTimer::set_non_periodic()
  150. {
  151. // FIXME: Implement it...
  152. VERIFY_NOT_REACHED();
  153. }
  154. void APICTimer::reset_to_default_ticks_per_second()
  155. {
  156. }
  157. bool APICTimer::try_to_set_frequency([[maybe_unused]] size_t frequency)
  158. {
  159. return true;
  160. }
  161. bool APICTimer::is_capable_of_frequency([[maybe_unused]] size_t frequency) const
  162. {
  163. return false;
  164. }
  165. size_t APICTimer::calculate_nearest_possible_frequency([[maybe_unused]] size_t frequency) const
  166. {
  167. return 0;
  168. }
  169. }