time_64.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 1991,1992,1995 Linus Torvalds
  3. * Copyright (c) 1994 Alan Modra
  4. * Copyright (c) 1995 Markus Kuhn
  5. * Copyright (c) 1996 Ingo Molnar
  6. * Copyright (c) 1998 Andrea Arcangeli
  7. * Copyright (c) 2002,2006 Vojtech Pavlik
  8. * Copyright (c) 2003 Andi Kleen
  9. *
  10. */
  11. #include <linux/clockchips.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/time.h>
  14. #include <linux/mca.h>
  15. #include <asm/vsyscall.h>
  16. #include <asm/x86_init.h>
  17. #include <asm/i8259.h>
  18. #include <asm/i8253.h>
  19. #include <asm/timer.h>
  20. #include <asm/hpet.h>
  21. #include <asm/time.h>
  22. #include <asm/nmi.h>
  23. volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
  24. unsigned long profile_pc(struct pt_regs *regs)
  25. {
  26. unsigned long pc = instruction_pointer(regs);
  27. /* Assume the lock function has either no stack frame or a copy
  28. of flags from PUSHF
  29. Eflags always has bits 22 and up cleared unlike kernel addresses. */
  30. if (!user_mode_vm(regs) && in_lock_functions(pc)) {
  31. #ifdef CONFIG_FRAME_POINTER
  32. return *(unsigned long *)(regs->bp + sizeof(long));
  33. #else
  34. unsigned long *sp = (unsigned long *)regs->sp;
  35. if (sp[0] >> 22)
  36. return sp[0];
  37. if (sp[1] >> 22)
  38. return sp[1];
  39. #endif
  40. }
  41. return pc;
  42. }
  43. EXPORT_SYMBOL(profile_pc);
  44. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  45. {
  46. inc_irq_stat(irq0_irqs);
  47. /* Optimized out for !IO_APIC and x86_64 */
  48. if (timer_ack) {
  49. /*
  50. * Subtle, when I/O APICs are used we have to ack timer IRQ
  51. * manually to deassert NMI lines for the watchdog if run
  52. * on an 82489DX-based system.
  53. */
  54. spin_lock(&i8259A_lock);
  55. outb(0x0c, PIC_MASTER_OCW3);
  56. /* Ack the IRQ; AEOI will end it automatically. */
  57. inb(PIC_MASTER_POLL);
  58. spin_unlock(&i8259A_lock);
  59. }
  60. global_clock_event->event_handler(global_clock_event);
  61. /* MCA bus quirk: Acknowledge irq0 by setting bit 7 in port 0x61 */
  62. if (MCA_bus)
  63. outb_p(inb_p(0x61)| 0x80, 0x61);
  64. return IRQ_HANDLED;
  65. }
  66. /* calibrate_cpu is used on systems with fixed rate TSCs to determine
  67. * processor frequency */
  68. #define TICK_COUNT 100000000
  69. unsigned long __init calibrate_cpu(void)
  70. {
  71. int tsc_start, tsc_now;
  72. int i, no_ctr_free;
  73. unsigned long evntsel3 = 0, pmc3 = 0, pmc_now = 0;
  74. unsigned long flags;
  75. for (i = 0; i < 4; i++)
  76. if (avail_to_resrv_perfctr_nmi_bit(i))
  77. break;
  78. no_ctr_free = (i == 4);
  79. if (no_ctr_free) {
  80. WARN(1, KERN_WARNING "Warning: AMD perfctrs busy ... "
  81. "cpu_khz value may be incorrect.\n");
  82. i = 3;
  83. rdmsrl(MSR_K7_EVNTSEL3, evntsel3);
  84. wrmsrl(MSR_K7_EVNTSEL3, 0);
  85. rdmsrl(MSR_K7_PERFCTR3, pmc3);
  86. } else {
  87. reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  88. reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  89. }
  90. local_irq_save(flags);
  91. /* start measuring cycles, incrementing from 0 */
  92. wrmsrl(MSR_K7_PERFCTR0 + i, 0);
  93. wrmsrl(MSR_K7_EVNTSEL0 + i, 1 << 22 | 3 << 16 | 0x76);
  94. rdtscl(tsc_start);
  95. do {
  96. rdmsrl(MSR_K7_PERFCTR0 + i, pmc_now);
  97. tsc_now = get_cycles();
  98. } while ((tsc_now - tsc_start) < TICK_COUNT);
  99. local_irq_restore(flags);
  100. if (no_ctr_free) {
  101. wrmsrl(MSR_K7_EVNTSEL3, 0);
  102. wrmsrl(MSR_K7_PERFCTR3, pmc3);
  103. wrmsrl(MSR_K7_EVNTSEL3, evntsel3);
  104. } else {
  105. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  106. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  107. }
  108. return pmc_now * tsc_khz / (tsc_now - tsc_start);
  109. }
  110. static struct irqaction irq0 = {
  111. .handler = timer_interrupt,
  112. .flags = IRQF_DISABLED | IRQF_IRQPOLL | IRQF_NOBALANCING | IRQF_TIMER,
  113. .name = "timer"
  114. };
  115. void __init hpet_time_init(void)
  116. {
  117. if (!hpet_enable())
  118. setup_pit_timer();
  119. setup_irq(0, &irq0);
  120. }
  121. static void x86_late_time_init(void)
  122. {
  123. x86_init.timers.timer_init();
  124. }
  125. void __init time_init(void)
  126. {
  127. tsc_init();
  128. late_time_init = x86_late_time_init;
  129. }