time_64.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #ifdef CONFIG_ACPI
  31. #include <acpi/achware.h> /* for PM timer frequency */
  32. #include <acpi/acpi_bus.h>
  33. #endif
  34. #include <asm/i8253.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/vsyscall.h>
  37. #include <asm/timex.h>
  38. #include <asm/proto.h>
  39. #include <asm/hpet.h>
  40. #include <asm/sections.h>
  41. #include <linux/hpet.h>
  42. #include <asm/apic.h>
  43. #include <asm/hpet.h>
  44. #include <asm/mpspec.h>
  45. #include <asm/nmi.h>
  46. #include <asm/vgtod.h>
  47. static char *timename = NULL;
  48. DEFINE_SPINLOCK(rtc_lock);
  49. EXPORT_SYMBOL(rtc_lock);
  50. DEFINE_SPINLOCK(i8253_lock);
  51. EXPORT_SYMBOL(i8253_lock);
  52. volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
  53. unsigned long profile_pc(struct pt_regs *regs)
  54. {
  55. unsigned long pc = instruction_pointer(regs);
  56. /* Assume the lock function has either no stack frame or a copy
  57. of eflags from PUSHF
  58. Eflags always has bits 22 and up cleared unlike kernel addresses. */
  59. if (!user_mode(regs) && in_lock_functions(pc)) {
  60. unsigned long *sp = (unsigned long *)regs->rsp;
  61. if (sp[0] >> 22)
  62. return sp[0];
  63. if (sp[1] >> 22)
  64. return sp[1];
  65. }
  66. return pc;
  67. }
  68. EXPORT_SYMBOL(profile_pc);
  69. /*
  70. * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
  71. * ms after the second nowtime has started, because when nowtime is written
  72. * into the registers of the CMOS clock, it will jump to the next second
  73. * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
  74. * sheet for details.
  75. */
  76. static int set_rtc_mmss(unsigned long nowtime)
  77. {
  78. int retval = 0;
  79. int real_seconds, real_minutes, cmos_minutes;
  80. unsigned char control, freq_select;
  81. /*
  82. * IRQs are disabled when we're called from the timer interrupt,
  83. * no need for spin_lock_irqsave()
  84. */
  85. spin_lock(&rtc_lock);
  86. /*
  87. * Tell the clock it's being set and stop it.
  88. */
  89. control = CMOS_READ(RTC_CONTROL);
  90. CMOS_WRITE(control | RTC_SET, RTC_CONTROL);
  91. freq_select = CMOS_READ(RTC_FREQ_SELECT);
  92. CMOS_WRITE(freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
  93. cmos_minutes = CMOS_READ(RTC_MINUTES);
  94. BCD_TO_BIN(cmos_minutes);
  95. /*
  96. * since we're only adjusting minutes and seconds, don't interfere with hour
  97. * overflow. This avoids messing with unknown time zones but requires your RTC
  98. * not to be off by more than 15 minutes. Since we're calling it only when
  99. * our clock is externally synchronized using NTP, this shouldn't be a problem.
  100. */
  101. real_seconds = nowtime % 60;
  102. real_minutes = nowtime / 60;
  103. if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
  104. real_minutes += 30; /* correct for half hour time zone */
  105. real_minutes %= 60;
  106. if (abs(real_minutes - cmos_minutes) >= 30) {
  107. printk(KERN_WARNING "time.c: can't update CMOS clock "
  108. "from %d to %d\n", cmos_minutes, real_minutes);
  109. retval = -1;
  110. } else {
  111. BIN_TO_BCD(real_seconds);
  112. BIN_TO_BCD(real_minutes);
  113. CMOS_WRITE(real_seconds, RTC_SECONDS);
  114. CMOS_WRITE(real_minutes, RTC_MINUTES);
  115. }
  116. /*
  117. * The following flags have to be released exactly in this order, otherwise the
  118. * DS12887 (popular MC146818A clone with integrated battery and quartz) will
  119. * not reset the oscillator and will not update precisely 500 ms later. You
  120. * won't find this mentioned in the Dallas Semiconductor data sheets, but who
  121. * believes data sheets anyway ... -- Markus Kuhn
  122. */
  123. CMOS_WRITE(control, RTC_CONTROL);
  124. CMOS_WRITE(freq_select, RTC_FREQ_SELECT);
  125. spin_unlock(&rtc_lock);
  126. return retval;
  127. }
  128. int update_persistent_clock(struct timespec now)
  129. {
  130. return set_rtc_mmss(now.tv_sec);
  131. }
  132. void main_timer_handler(void)
  133. {
  134. /*
  135. * Here we are in the timer irq handler. We have irqs locally disabled (so we
  136. * don't need spin_lock_irqsave()) but we don't know if the timer_bh is running
  137. * on the other CPU, so we need a lock. We also need to lock the vsyscall
  138. * variables, because both do_timer() and us change them -arca+vojtech
  139. */
  140. write_seqlock(&xtime_lock);
  141. /*
  142. * Do the timer stuff.
  143. */
  144. do_timer(1);
  145. #ifndef CONFIG_SMP
  146. update_process_times(user_mode(get_irq_regs()));
  147. #endif
  148. /*
  149. * In the SMP case we use the local APIC timer interrupt to do the profiling,
  150. * except when we simulate SMP mode on a uniprocessor system, in that case we
  151. * have to call the local interrupt handler.
  152. */
  153. if (!using_apic_timer)
  154. smp_local_timer_interrupt();
  155. write_sequnlock(&xtime_lock);
  156. }
  157. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  158. {
  159. if (apic_runs_main_timer > 1)
  160. return IRQ_HANDLED;
  161. main_timer_handler();
  162. if (using_apic_timer)
  163. smp_send_timer_broadcast_ipi();
  164. return IRQ_HANDLED;
  165. }
  166. unsigned long read_persistent_clock(void)
  167. {
  168. unsigned int year, mon, day, hour, min, sec;
  169. unsigned long flags;
  170. unsigned century = 0;
  171. spin_lock_irqsave(&rtc_lock, flags);
  172. do {
  173. sec = CMOS_READ(RTC_SECONDS);
  174. min = CMOS_READ(RTC_MINUTES);
  175. hour = CMOS_READ(RTC_HOURS);
  176. day = CMOS_READ(RTC_DAY_OF_MONTH);
  177. mon = CMOS_READ(RTC_MONTH);
  178. year = CMOS_READ(RTC_YEAR);
  179. #ifdef CONFIG_ACPI
  180. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  181. acpi_gbl_FADT.century)
  182. century = CMOS_READ(acpi_gbl_FADT.century);
  183. #endif
  184. } while (sec != CMOS_READ(RTC_SECONDS));
  185. spin_unlock_irqrestore(&rtc_lock, flags);
  186. /*
  187. * We know that x86-64 always uses BCD format, no need to check the
  188. * config register.
  189. */
  190. BCD_TO_BIN(sec);
  191. BCD_TO_BIN(min);
  192. BCD_TO_BIN(hour);
  193. BCD_TO_BIN(day);
  194. BCD_TO_BIN(mon);
  195. BCD_TO_BIN(year);
  196. if (century) {
  197. BCD_TO_BIN(century);
  198. year += century * 100;
  199. printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
  200. } else {
  201. /*
  202. * x86-64 systems only exists since 2002.
  203. * This will work up to Dec 31, 2100
  204. */
  205. year += 2000;
  206. }
  207. return mktime(year, mon, day, hour, min, sec);
  208. }
  209. /* calibrate_cpu is used on systems with fixed rate TSCs to determine
  210. * processor frequency */
  211. #define TICK_COUNT 100000000
  212. static unsigned int __init tsc_calibrate_cpu_khz(void)
  213. {
  214. int tsc_start, tsc_now;
  215. int i, no_ctr_free;
  216. unsigned long evntsel3 = 0, pmc3 = 0, pmc_now = 0;
  217. unsigned long flags;
  218. for (i = 0; i < 4; i++)
  219. if (avail_to_resrv_perfctr_nmi_bit(i))
  220. break;
  221. no_ctr_free = (i == 4);
  222. if (no_ctr_free) {
  223. i = 3;
  224. rdmsrl(MSR_K7_EVNTSEL3, evntsel3);
  225. wrmsrl(MSR_K7_EVNTSEL3, 0);
  226. rdmsrl(MSR_K7_PERFCTR3, pmc3);
  227. } else {
  228. reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  229. reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  230. }
  231. local_irq_save(flags);
  232. /* start meauring cycles, incrementing from 0 */
  233. wrmsrl(MSR_K7_PERFCTR0 + i, 0);
  234. wrmsrl(MSR_K7_EVNTSEL0 + i, 1 << 22 | 3 << 16 | 0x76);
  235. rdtscl(tsc_start);
  236. do {
  237. rdmsrl(MSR_K7_PERFCTR0 + i, pmc_now);
  238. tsc_now = get_cycles_sync();
  239. } while ((tsc_now - tsc_start) < TICK_COUNT);
  240. local_irq_restore(flags);
  241. if (no_ctr_free) {
  242. wrmsrl(MSR_K7_EVNTSEL3, 0);
  243. wrmsrl(MSR_K7_PERFCTR3, pmc3);
  244. wrmsrl(MSR_K7_EVNTSEL3, evntsel3);
  245. } else {
  246. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  247. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  248. }
  249. return pmc_now * tsc_khz / (tsc_now - tsc_start);
  250. }
  251. static void __pit_init(int val, u8 mode)
  252. {
  253. unsigned long flags;
  254. spin_lock_irqsave(&i8253_lock, flags);
  255. outb_p(mode, PIT_MODE);
  256. outb_p(val & 0xff, PIT_CH0); /* LSB */
  257. outb_p(val >> 8, PIT_CH0); /* MSB */
  258. spin_unlock_irqrestore(&i8253_lock, flags);
  259. }
  260. void __init pit_init(void)
  261. {
  262. __pit_init(LATCH, 0x34); /* binary, mode 2, LSB/MSB, ch 0 */
  263. }
  264. void pit_stop_interrupt(void)
  265. {
  266. __pit_init(0, 0x30); /* mode 0 */
  267. }
  268. void stop_timer_interrupt(void)
  269. {
  270. char *name;
  271. if (hpet_address) {
  272. name = "HPET";
  273. hpet_timer_stop_set_go(0);
  274. } else {
  275. name = "PIT";
  276. pit_stop_interrupt();
  277. }
  278. printk(KERN_INFO "timer: %s interrupt stopped.\n", name);
  279. }
  280. static struct irqaction irq0 = {
  281. .handler = timer_interrupt,
  282. .flags = IRQF_DISABLED | IRQF_IRQPOLL | IRQF_NOBALANCING,
  283. .mask = CPU_MASK_NONE,
  284. .name = "timer"
  285. };
  286. void __init time_init(void)
  287. {
  288. if (nohpet)
  289. hpet_address = 0;
  290. if (hpet_arch_init())
  291. hpet_address = 0;
  292. if (hpet_use_timer) {
  293. /* set tick_nsec to use the proper rate for HPET */
  294. tick_nsec = TICK_NSEC_HPET;
  295. timename = "HPET";
  296. } else {
  297. pit_init();
  298. timename = "PIT";
  299. }
  300. tsc_calibrate();
  301. cpu_khz = tsc_khz;
  302. if (cpu_has(&boot_cpu_data, X86_FEATURE_CONSTANT_TSC) &&
  303. boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  304. boot_cpu_data.x86 == 16)
  305. cpu_khz = tsc_calibrate_cpu_khz();
  306. if (unsynchronized_tsc())
  307. mark_tsc_unstable("TSCs unsynchronized");
  308. if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  309. vgetcpu_mode = VGETCPU_RDTSCP;
  310. else
  311. vgetcpu_mode = VGETCPU_LSL;
  312. set_cyc2ns_scale(tsc_khz);
  313. printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
  314. cpu_khz / 1000, cpu_khz % 1000);
  315. init_tsc_clocksource();
  316. setup_irq(0, &irq0);
  317. }
  318. /*
  319. * sysfs support for the timer.
  320. */
  321. static int timer_suspend(struct sys_device *dev, pm_message_t state)
  322. {
  323. return 0;
  324. }
  325. static int timer_resume(struct sys_device *dev)
  326. {
  327. if (hpet_address)
  328. hpet_reenable();
  329. else
  330. i8254_timer_resume();
  331. return 0;
  332. }
  333. static struct sysdev_class timer_sysclass = {
  334. .resume = timer_resume,
  335. .suspend = timer_suspend,
  336. set_kset_name("timer"),
  337. };
  338. /* XXX this sysfs stuff should probably go elsewhere later -john */
  339. static struct sys_device device_timer = {
  340. .id = 0,
  341. .cls = &timer_sysclass,
  342. };
  343. static int time_init_device(void)
  344. {
  345. int error = sysdev_class_register(&timer_sysclass);
  346. if (!error)
  347. error = sysdev_register(&device_timer);
  348. return error;
  349. }
  350. device_initcall(time_init_device);