pmtimer.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Ported over from i386 by AK, original copyright was:
  2. *
  3. * (C) Dominik Brodowski <linux@brodo.de> 2003
  4. *
  5. * Driver to use the Power Management Timer (PMTMR) available in some
  6. * southbridges as primary timing source for the Linux kernel.
  7. *
  8. * Based on parts of linux/drivers/acpi/hardware/hwtimer.c, timer_pit.c,
  9. * timer_hpet.c, and on Arjan van de Ven's implementation for 2.4.
  10. *
  11. * This file is licensed under the GPL v2.
  12. *
  13. * Dropped all the hardware bug workarounds for now. Hopefully they
  14. * are not needed on 64bit chipsets.
  15. */
  16. #include <linux/jiffies.h>
  17. #include <linux/kernel.h>
  18. #include <linux/time.h>
  19. #include <linux/init.h>
  20. #include <linux/cpumask.h>
  21. #include <asm/io.h>
  22. #include <asm/proto.h>
  23. #include <asm/msr.h>
  24. #include <asm/vsyscall.h>
  25. /* The I/O port the PMTMR resides at.
  26. * The location is detected during setup_arch(),
  27. * in arch/i386/kernel/acpi/boot.c */
  28. u32 pmtmr_ioport;
  29. /* value of the Power timer at last timer interrupt */
  30. static u32 offset_delay;
  31. static u32 last_pmtmr_tick;
  32. #define ACPI_PM_MASK 0xFFFFFF /* limit it to 24 bits */
  33. static inline u32 cyc2us(u32 cycles)
  34. {
  35. /* The Power Management Timer ticks at 3.579545 ticks per microsecond.
  36. * 1 / PM_TIMER_FREQUENCY == 0.27936511 =~ 286/1024 [error: 0.024%]
  37. *
  38. * Even with HZ = 100, delta is at maximum 35796 ticks, so it can
  39. * easily be multiplied with 286 (=0x11E) without having to fear
  40. * u32 overflows.
  41. */
  42. cycles *= 286;
  43. return (cycles >> 10);
  44. }
  45. int pmtimer_mark_offset(void)
  46. {
  47. static int first_run = 1;
  48. unsigned long tsc;
  49. u32 lost;
  50. u32 tick = inl(pmtmr_ioport);
  51. u32 delta;
  52. delta = cyc2us((tick - last_pmtmr_tick) & ACPI_PM_MASK);
  53. last_pmtmr_tick = tick;
  54. monotonic_base += delta * NSEC_PER_USEC;
  55. delta += offset_delay;
  56. lost = delta / (USEC_PER_SEC / HZ);
  57. offset_delay = delta % (USEC_PER_SEC / HZ);
  58. rdtscll(tsc);
  59. vxtime.last_tsc = tsc - offset_delay * cpu_khz;
  60. /* don't calculate delay for first run,
  61. or if we've got less then a tick */
  62. if (first_run || (lost < 1)) {
  63. first_run = 0;
  64. offset_delay = 0;
  65. }
  66. return lost - 1;
  67. }
  68. unsigned int do_gettimeoffset_pm(void)
  69. {
  70. u32 now, offset, delta = 0;
  71. offset = last_pmtmr_tick;
  72. now = inl(pmtmr_ioport);
  73. delta = (now - offset) & ACPI_PM_MASK;
  74. return offset_delay + cyc2us(delta);
  75. }
  76. static int __init nopmtimer_setup(char *s)
  77. {
  78. pmtmr_ioport = 0;
  79. return 0;
  80. }
  81. __setup("nopmtimer", nopmtimer_setup);