i8253.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * 8253/PIT functions
  4. *
  5. */
  6. #include <linux/clockchips.h>
  7. #include <linux/init.h>
  8. #include <linux/timex.h>
  9. #include <linux/i8253.h>
  10. #include <asm/hypervisor.h>
  11. #include <asm/apic.h>
  12. #include <asm/hpet.h>
  13. #include <asm/time.h>
  14. #include <asm/smp.h>
  15. /*
  16. * HPET replaces the PIT, when enabled. So we need to know, which of
  17. * the two timers is used
  18. */
  19. struct clock_event_device *global_clock_event;
  20. /*
  21. * Modern chipsets can disable the PIT clock which makes it unusable. It
  22. * would be possible to enable the clock but the registers are chipset
  23. * specific and not discoverable. Avoid the whack a mole game.
  24. *
  25. * These platforms have discoverable TSC/CPU frequencies but this also
  26. * requires to know the local APIC timer frequency as it normally is
  27. * calibrated against the PIT interrupt.
  28. */
  29. static bool __init use_pit(void)
  30. {
  31. if (!IS_ENABLED(CONFIG_X86_TSC) || !boot_cpu_has(X86_FEATURE_TSC))
  32. return true;
  33. /* This also returns true when APIC is disabled */
  34. return apic_needs_pit();
  35. }
  36. bool __init pit_timer_init(void)
  37. {
  38. if (!use_pit()) {
  39. /*
  40. * Don't just ignore the PIT. Ensure it's stopped, because
  41. * VMMs otherwise steal CPU time just to pointlessly waggle
  42. * the (masked) IRQ.
  43. */
  44. clockevent_i8253_disable();
  45. return false;
  46. }
  47. clockevent_i8253_init(true);
  48. global_clock_event = &i8253_clockevent;
  49. return true;
  50. }
  51. #ifndef CONFIG_X86_64
  52. static int __init init_pit_clocksource(void)
  53. {
  54. /*
  55. * Several reasons not to register PIT as a clocksource:
  56. *
  57. * - On SMP PIT does not scale due to i8253_lock
  58. * - when HPET is enabled
  59. * - when local APIC timer is active (PIT is switched off)
  60. */
  61. if (num_possible_cpus() > 1 || is_hpet_enabled() ||
  62. !clockevent_state_periodic(&i8253_clockevent))
  63. return 0;
  64. return clocksource_i8253_init();
  65. }
  66. arch_initcall(init_pit_clocksource);
  67. #endif /* !CONFIG_X86_64 */