APICTimer.cpp 5.4 KB

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