time_64.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * linux/arch/x86-64/kernel/time.c
  3. *
  4. * "High Precision Event Timer" based timekeeping.
  5. *
  6. * Copyright (c) 1991,1992,1995 Linus Torvalds
  7. * Copyright (c) 1994 Alan Modra
  8. * Copyright (c) 1995 Markus Kuhn
  9. * Copyright (c) 1996 Ingo Molnar
  10. * Copyright (c) 1998 Andrea Arcangeli
  11. * Copyright (c) 2002,2006 Vojtech Pavlik
  12. * Copyright (c) 2003 Andi Kleen
  13. * RTC support code taken from arch/i386/kernel/timers/time_hpet.c
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/init.h>
  19. #include <linux/mc146818rtc.h>
  20. #include <linux/time.h>
  21. #include <linux/ioport.h>
  22. #include <linux/module.h>
  23. #include <linux/device.h>
  24. #include <linux/sysdev.h>
  25. #include <linux/bcd.h>
  26. #include <linux/notifier.h>
  27. #include <linux/cpu.h>
  28. #include <linux/kallsyms.h>
  29. #include <linux/acpi.h>
  30. #include <linux/clockchips.h>
  31. #ifdef CONFIG_ACPI
  32. #include <acpi/achware.h> /* for PM timer frequency */
  33. #include <acpi/acpi_bus.h>
  34. #endif
  35. #include <asm/i8253.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/vsyscall.h>
  38. #include <asm/timex.h>
  39. #include <asm/proto.h>
  40. #include <asm/hpet.h>
  41. #include <asm/sections.h>
  42. #include <linux/hpet.h>
  43. #include <asm/apic.h>
  44. #include <asm/hpet.h>
  45. #include <asm/mpspec.h>
  46. #include <asm/nmi.h>
  47. #include <asm/vgtod.h>
  48. DEFINE_SPINLOCK(rtc_lock);
  49. EXPORT_SYMBOL(rtc_lock);
  50. volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
  51. unsigned long profile_pc(struct pt_regs *regs)
  52. {
  53. unsigned long pc = instruction_pointer(regs);
  54. /* Assume the lock function has either no stack frame or a copy
  55. of eflags from PUSHF
  56. Eflags always has bits 22 and up cleared unlike kernel addresses. */
  57. if (!user_mode(regs) && in_lock_functions(pc)) {
  58. unsigned long *sp = (unsigned long *)regs->rsp;
  59. if (sp[0] >> 22)
  60. return sp[0];
  61. if (sp[1] >> 22)
  62. return sp[1];
  63. }
  64. return pc;
  65. }
  66. EXPORT_SYMBOL(profile_pc);
  67. /*
  68. * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
  69. * ms after the second nowtime has started, because when nowtime is written
  70. * into the registers of the CMOS clock, it will jump to the next second
  71. * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
  72. * sheet for details.
  73. */
  74. static int set_rtc_mmss(unsigned long nowtime)
  75. {
  76. int retval = 0;
  77. int real_seconds, real_minutes, cmos_minutes;
  78. unsigned char control, freq_select;
  79. /*
  80. * IRQs are disabled when we're called from the timer interrupt,
  81. * no need for spin_lock_irqsave()
  82. */
  83. spin_lock(&rtc_lock);
  84. /*
  85. * Tell the clock it's being set and stop it.
  86. */
  87. control = CMOS_READ(RTC_CONTROL);
  88. CMOS_WRITE(control | RTC_SET, RTC_CONTROL);
  89. freq_select = CMOS_READ(RTC_FREQ_SELECT);
  90. CMOS_WRITE(freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
  91. cmos_minutes = CMOS_READ(RTC_MINUTES);
  92. BCD_TO_BIN(cmos_minutes);
  93. /*
  94. * since we're only adjusting minutes and seconds, don't interfere with hour
  95. * overflow. This avoids messing with unknown time zones but requires your RTC
  96. * not to be off by more than 15 minutes. Since we're calling it only when
  97. * our clock is externally synchronized using NTP, this shouldn't be a problem.
  98. */
  99. real_seconds = nowtime % 60;
  100. real_minutes = nowtime / 60;
  101. if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
  102. real_minutes += 30; /* correct for half hour time zone */
  103. real_minutes %= 60;
  104. if (abs(real_minutes - cmos_minutes) >= 30) {
  105. printk(KERN_WARNING "time.c: can't update CMOS clock "
  106. "from %d to %d\n", cmos_minutes, real_minutes);
  107. retval = -1;
  108. } else {
  109. BIN_TO_BCD(real_seconds);
  110. BIN_TO_BCD(real_minutes);
  111. CMOS_WRITE(real_seconds, RTC_SECONDS);
  112. CMOS_WRITE(real_minutes, RTC_MINUTES);
  113. }
  114. /*
  115. * The following flags have to be released exactly in this order, otherwise the
  116. * DS12887 (popular MC146818A clone with integrated battery and quartz) will
  117. * not reset the oscillator and will not update precisely 500 ms later. You
  118. * won't find this mentioned in the Dallas Semiconductor data sheets, but who
  119. * believes data sheets anyway ... -- Markus Kuhn
  120. */
  121. CMOS_WRITE(control, RTC_CONTROL);
  122. CMOS_WRITE(freq_select, RTC_FREQ_SELECT);
  123. spin_unlock(&rtc_lock);
  124. return retval;
  125. }
  126. int update_persistent_clock(struct timespec now)
  127. {
  128. return set_rtc_mmss(now.tv_sec);
  129. }
  130. static irqreturn_t timer_event_interrupt(int irq, void *dev_id)
  131. {
  132. add_pda(irq0_irqs, 1);
  133. global_clock_event->event_handler(global_clock_event);
  134. return IRQ_HANDLED;
  135. }
  136. unsigned long read_persistent_clock(void)
  137. {
  138. unsigned int year, mon, day, hour, min, sec;
  139. unsigned long flags;
  140. unsigned century = 0;
  141. spin_lock_irqsave(&rtc_lock, flags);
  142. do {
  143. sec = CMOS_READ(RTC_SECONDS);
  144. min = CMOS_READ(RTC_MINUTES);
  145. hour = CMOS_READ(RTC_HOURS);
  146. day = CMOS_READ(RTC_DAY_OF_MONTH);
  147. mon = CMOS_READ(RTC_MONTH);
  148. year = CMOS_READ(RTC_YEAR);
  149. #ifdef CONFIG_ACPI
  150. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  151. acpi_gbl_FADT.century)
  152. century = CMOS_READ(acpi_gbl_FADT.century);
  153. #endif
  154. } while (sec != CMOS_READ(RTC_SECONDS));
  155. spin_unlock_irqrestore(&rtc_lock, flags);
  156. /*
  157. * We know that x86-64 always uses BCD format, no need to check the
  158. * config register.
  159. */
  160. BCD_TO_BIN(sec);
  161. BCD_TO_BIN(min);
  162. BCD_TO_BIN(hour);
  163. BCD_TO_BIN(day);
  164. BCD_TO_BIN(mon);
  165. BCD_TO_BIN(year);
  166. if (century) {
  167. BCD_TO_BIN(century);
  168. year += century * 100;
  169. printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
  170. } else {
  171. /*
  172. * x86-64 systems only exists since 2002.
  173. * This will work up to Dec 31, 2100
  174. */
  175. year += 2000;
  176. }
  177. return mktime(year, mon, day, hour, min, sec);
  178. }
  179. /* calibrate_cpu is used on systems with fixed rate TSCs to determine
  180. * processor frequency */
  181. #define TICK_COUNT 100000000
  182. static unsigned int __init tsc_calibrate_cpu_khz(void)
  183. {
  184. int tsc_start, tsc_now;
  185. int i, no_ctr_free;
  186. unsigned long evntsel3 = 0, pmc3 = 0, pmc_now = 0;
  187. unsigned long flags;
  188. for (i = 0; i < 4; i++)
  189. if (avail_to_resrv_perfctr_nmi_bit(i))
  190. break;
  191. no_ctr_free = (i == 4);
  192. if (no_ctr_free) {
  193. i = 3;
  194. rdmsrl(MSR_K7_EVNTSEL3, evntsel3);
  195. wrmsrl(MSR_K7_EVNTSEL3, 0);
  196. rdmsrl(MSR_K7_PERFCTR3, pmc3);
  197. } else {
  198. reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  199. reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  200. }
  201. local_irq_save(flags);
  202. /* start meauring cycles, incrementing from 0 */
  203. wrmsrl(MSR_K7_PERFCTR0 + i, 0);
  204. wrmsrl(MSR_K7_EVNTSEL0 + i, 1 << 22 | 3 << 16 | 0x76);
  205. rdtscl(tsc_start);
  206. do {
  207. rdmsrl(MSR_K7_PERFCTR0 + i, pmc_now);
  208. tsc_now = get_cycles_sync();
  209. } while ((tsc_now - tsc_start) < TICK_COUNT);
  210. local_irq_restore(flags);
  211. if (no_ctr_free) {
  212. wrmsrl(MSR_K7_EVNTSEL3, 0);
  213. wrmsrl(MSR_K7_PERFCTR3, pmc3);
  214. wrmsrl(MSR_K7_EVNTSEL3, evntsel3);
  215. } else {
  216. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  217. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  218. }
  219. return pmc_now * tsc_khz / (tsc_now - tsc_start);
  220. }
  221. static struct irqaction irq0 = {
  222. .handler = timer_event_interrupt,
  223. .flags = IRQF_DISABLED | IRQF_IRQPOLL | IRQF_NOBALANCING,
  224. .mask = CPU_MASK_NONE,
  225. .name = "timer"
  226. };
  227. void __init time_init(void)
  228. {
  229. if (!hpet_enable())
  230. setup_pit_timer();
  231. setup_irq(0, &irq0);
  232. tsc_calibrate();
  233. cpu_khz = tsc_khz;
  234. if (cpu_has(&boot_cpu_data, X86_FEATURE_CONSTANT_TSC) &&
  235. boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  236. boot_cpu_data.x86 == 16)
  237. cpu_khz = tsc_calibrate_cpu_khz();
  238. if (unsynchronized_tsc())
  239. mark_tsc_unstable("TSCs unsynchronized");
  240. if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  241. vgetcpu_mode = VGETCPU_RDTSCP;
  242. else
  243. vgetcpu_mode = VGETCPU_LSL;
  244. printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
  245. cpu_khz / 1000, cpu_khz % 1000);
  246. init_tsc_clocksource();
  247. }