time_64.c 7.4 KB

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