time_64.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. unsigned long flags;
  78. /*
  79. * set_rtc_mmss is called when irqs are enabled, so disable irqs here
  80. */
  81. spin_lock_irqsave(&rtc_lock, flags);
  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_irqrestore(&rtc_lock, flags);
  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. /*
  141. * if UIP is clear, then we have >= 244 microseconds before RTC
  142. * registers will be updated. Spec sheet says that this is the
  143. * reliable way to read RTC - registers invalid (off bus) during update
  144. */
  145. while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  146. cpu_relax();
  147. /* now read all RTC registers while stable with interrupts disabled */
  148. sec = CMOS_READ(RTC_SECONDS);
  149. min = CMOS_READ(RTC_MINUTES);
  150. hour = CMOS_READ(RTC_HOURS);
  151. day = CMOS_READ(RTC_DAY_OF_MONTH);
  152. mon = CMOS_READ(RTC_MONTH);
  153. year = CMOS_READ(RTC_YEAR);
  154. #ifdef CONFIG_ACPI
  155. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  156. acpi_gbl_FADT.century)
  157. century = CMOS_READ(acpi_gbl_FADT.century);
  158. #endif
  159. spin_unlock_irqrestore(&rtc_lock, flags);
  160. /*
  161. * We know that x86-64 always uses BCD format, no need to check the
  162. * config register.
  163. */
  164. BCD_TO_BIN(sec);
  165. BCD_TO_BIN(min);
  166. BCD_TO_BIN(hour);
  167. BCD_TO_BIN(day);
  168. BCD_TO_BIN(mon);
  169. BCD_TO_BIN(year);
  170. if (century) {
  171. BCD_TO_BIN(century);
  172. year += century * 100;
  173. printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
  174. } else {
  175. /*
  176. * x86-64 systems only exists since 2002.
  177. * This will work up to Dec 31, 2100
  178. */
  179. year += 2000;
  180. }
  181. return mktime(year, mon, day, hour, min, sec);
  182. }
  183. /* calibrate_cpu is used on systems with fixed rate TSCs to determine
  184. * processor frequency */
  185. #define TICK_COUNT 100000000
  186. static unsigned int __init tsc_calibrate_cpu_khz(void)
  187. {
  188. int tsc_start, tsc_now;
  189. int i, no_ctr_free;
  190. unsigned long evntsel3 = 0, pmc3 = 0, pmc_now = 0;
  191. unsigned long flags;
  192. for (i = 0; i < 4; i++)
  193. if (avail_to_resrv_perfctr_nmi_bit(i))
  194. break;
  195. no_ctr_free = (i == 4);
  196. if (no_ctr_free) {
  197. i = 3;
  198. rdmsrl(MSR_K7_EVNTSEL3, evntsel3);
  199. wrmsrl(MSR_K7_EVNTSEL3, 0);
  200. rdmsrl(MSR_K7_PERFCTR3, pmc3);
  201. } else {
  202. reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  203. reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  204. }
  205. local_irq_save(flags);
  206. /* start meauring cycles, incrementing from 0 */
  207. wrmsrl(MSR_K7_PERFCTR0 + i, 0);
  208. wrmsrl(MSR_K7_EVNTSEL0 + i, 1 << 22 | 3 << 16 | 0x76);
  209. rdtscl(tsc_start);
  210. do {
  211. rdmsrl(MSR_K7_PERFCTR0 + i, pmc_now);
  212. tsc_now = get_cycles_sync();
  213. } while ((tsc_now - tsc_start) < TICK_COUNT);
  214. local_irq_restore(flags);
  215. if (no_ctr_free) {
  216. wrmsrl(MSR_K7_EVNTSEL3, 0);
  217. wrmsrl(MSR_K7_PERFCTR3, pmc3);
  218. wrmsrl(MSR_K7_EVNTSEL3, evntsel3);
  219. } else {
  220. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  221. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  222. }
  223. return pmc_now * tsc_khz / (tsc_now - tsc_start);
  224. }
  225. static struct irqaction irq0 = {
  226. .handler = timer_event_interrupt,
  227. .flags = IRQF_DISABLED | IRQF_IRQPOLL | IRQF_NOBALANCING,
  228. .mask = CPU_MASK_NONE,
  229. .name = "timer"
  230. };
  231. void __init time_init(void)
  232. {
  233. if (!hpet_enable())
  234. setup_pit_timer();
  235. setup_irq(0, &irq0);
  236. tsc_calibrate();
  237. cpu_khz = tsc_khz;
  238. if (cpu_has(&boot_cpu_data, X86_FEATURE_CONSTANT_TSC) &&
  239. boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  240. boot_cpu_data.x86 == 16)
  241. cpu_khz = tsc_calibrate_cpu_khz();
  242. if (unsynchronized_tsc())
  243. mark_tsc_unstable("TSCs unsynchronized");
  244. if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  245. vgetcpu_mode = VGETCPU_RDTSCP;
  246. else
  247. vgetcpu_mode = VGETCPU_LSL;
  248. printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
  249. cpu_khz / 1000, cpu_khz % 1000);
  250. init_tsc_clocksource();
  251. }