i8253.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/apic.h>
  11. #include <asm/hpet.h>
  12. #include <asm/time.h>
  13. #include <asm/smp.h>
  14. /*
  15. * HPET replaces the PIT, when enabled. So we need to know, which of
  16. * the two timers is used
  17. */
  18. struct clock_event_device *global_clock_event;
  19. /*
  20. * Modern chipsets can disable the PIT clock which makes it unusable. It
  21. * would be possible to enable the clock but the registers are chipset
  22. * specific and not discoverable. Avoid the whack a mole game.
  23. *
  24. * These platforms have discoverable TSC/CPU frequencies but this also
  25. * requires to know the local APIC timer frequency as it normally is
  26. * calibrated against the PIT interrupt.
  27. */
  28. static bool __init use_pit(void)
  29. {
  30. if (!IS_ENABLED(CONFIG_X86_TSC) || !boot_cpu_has(X86_FEATURE_TSC))
  31. return true;
  32. /* This also returns true when APIC is disabled */
  33. return apic_needs_pit();
  34. }
  35. bool __init pit_timer_init(void)
  36. {
  37. if (!use_pit())
  38. return false;
  39. clockevent_i8253_init(true);
  40. global_clock_event = &i8253_clockevent;
  41. return true;
  42. }
  43. #ifndef CONFIG_X86_64
  44. static int __init init_pit_clocksource(void)
  45. {
  46. /*
  47. * Several reasons not to register PIT as a clocksource:
  48. *
  49. * - On SMP PIT does not scale due to i8253_lock
  50. * - when HPET is enabled
  51. * - when local APIC timer is active (PIT is switched off)
  52. */
  53. if (num_possible_cpus() > 1 || is_hpet_enabled() ||
  54. !clockevent_state_periodic(&i8253_clockevent))
  55. return 0;
  56. return clocksource_i8253_init();
  57. }
  58. arch_initcall(init_pit_clocksource);
  59. #endif /* !CONFIG_X86_64 */