time_64.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * "High Precision Event Timer" based timekeeping.
  3. *
  4. * Copyright (c) 1991,1992,1995 Linus Torvalds
  5. * Copyright (c) 1994 Alan Modra
  6. * Copyright (c) 1995 Markus Kuhn
  7. * Copyright (c) 1996 Ingo Molnar
  8. * Copyright (c) 1998 Andrea Arcangeli
  9. * Copyright (c) 2002,2006 Vojtech Pavlik
  10. * Copyright (c) 2003 Andi Kleen
  11. * RTC support code taken from arch/i386/kernel/timers/time_hpet.c
  12. */
  13. #include <linux/clockchips.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/time.h>
  18. #include <asm/i8253.h>
  19. #include <asm/hpet.h>
  20. #include <asm/nmi.h>
  21. #include <asm/vgtod.h>
  22. volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
  23. unsigned long profile_pc(struct pt_regs *regs)
  24. {
  25. unsigned long pc = instruction_pointer(regs);
  26. /* Assume the lock function has either no stack frame or a copy
  27. of eflags from PUSHF
  28. Eflags always has bits 22 and up cleared unlike kernel addresses. */
  29. if (!user_mode(regs) && in_lock_functions(pc)) {
  30. unsigned long *sp = (unsigned long *)regs->rsp;
  31. if (sp[0] >> 22)
  32. return sp[0];
  33. if (sp[1] >> 22)
  34. return sp[1];
  35. }
  36. return pc;
  37. }
  38. EXPORT_SYMBOL(profile_pc);
  39. static irqreturn_t timer_event_interrupt(int irq, void *dev_id)
  40. {
  41. add_pda(irq0_irqs, 1);
  42. global_clock_event->event_handler(global_clock_event);
  43. return IRQ_HANDLED;
  44. }
  45. /* calibrate_cpu is used on systems with fixed rate TSCs to determine
  46. * processor frequency */
  47. #define TICK_COUNT 100000000
  48. static unsigned int __init tsc_calibrate_cpu_khz(void)
  49. {
  50. int tsc_start, tsc_now;
  51. int i, no_ctr_free;
  52. unsigned long evntsel3 = 0, pmc3 = 0, pmc_now = 0;
  53. unsigned long flags;
  54. for (i = 0; i < 4; i++)
  55. if (avail_to_resrv_perfctr_nmi_bit(i))
  56. break;
  57. no_ctr_free = (i == 4);
  58. if (no_ctr_free) {
  59. i = 3;
  60. rdmsrl(MSR_K7_EVNTSEL3, evntsel3);
  61. wrmsrl(MSR_K7_EVNTSEL3, 0);
  62. rdmsrl(MSR_K7_PERFCTR3, pmc3);
  63. } else {
  64. reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  65. reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  66. }
  67. local_irq_save(flags);
  68. /* start meauring cycles, incrementing from 0 */
  69. wrmsrl(MSR_K7_PERFCTR0 + i, 0);
  70. wrmsrl(MSR_K7_EVNTSEL0 + i, 1 << 22 | 3 << 16 | 0x76);
  71. rdtscl(tsc_start);
  72. do {
  73. rdmsrl(MSR_K7_PERFCTR0 + i, pmc_now);
  74. tsc_now = get_cycles_sync();
  75. } while ((tsc_now - tsc_start) < TICK_COUNT);
  76. local_irq_restore(flags);
  77. if (no_ctr_free) {
  78. wrmsrl(MSR_K7_EVNTSEL3, 0);
  79. wrmsrl(MSR_K7_PERFCTR3, pmc3);
  80. wrmsrl(MSR_K7_EVNTSEL3, evntsel3);
  81. } else {
  82. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  83. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  84. }
  85. return pmc_now * tsc_khz / (tsc_now - tsc_start);
  86. }
  87. static struct irqaction irq0 = {
  88. .handler = timer_event_interrupt,
  89. .flags = IRQF_DISABLED | IRQF_IRQPOLL | IRQF_NOBALANCING,
  90. .mask = CPU_MASK_NONE,
  91. .name = "timer"
  92. };
  93. void __init time_init(void)
  94. {
  95. if (!hpet_enable())
  96. setup_pit_timer();
  97. setup_irq(0, &irq0);
  98. tsc_calibrate();
  99. cpu_khz = tsc_khz;
  100. if (cpu_has(&boot_cpu_data, X86_FEATURE_CONSTANT_TSC) &&
  101. boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  102. boot_cpu_data.x86 == 16)
  103. cpu_khz = tsc_calibrate_cpu_khz();
  104. if (unsynchronized_tsc())
  105. mark_tsc_unstable("TSCs unsynchronized");
  106. if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  107. vgetcpu_mode = VGETCPU_RDTSCP;
  108. else
  109. vgetcpu_mode = VGETCPU_LSL;
  110. printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
  111. cpu_khz / 1000, cpu_khz % 1000);
  112. init_tsc_clocksource();
  113. }