time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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/8253pit.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. static char *timename = NULL;
  47. DEFINE_SPINLOCK(rtc_lock);
  48. EXPORT_SYMBOL(rtc_lock);
  49. DEFINE_SPINLOCK(i8253_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 void set_rtc_mmss(unsigned long nowtime)
  75. {
  76. int real_seconds, real_minutes, cmos_minutes;
  77. unsigned char control, freq_select;
  78. /*
  79. * IRQs are disabled when we're called from the timer interrupt,
  80. * no need for spin_lock_irqsave()
  81. */
  82. spin_lock(&rtc_lock);
  83. /*
  84. * Tell the clock it's being set and stop it.
  85. */
  86. control = CMOS_READ(RTC_CONTROL);
  87. CMOS_WRITE(control | RTC_SET, RTC_CONTROL);
  88. freq_select = CMOS_READ(RTC_FREQ_SELECT);
  89. CMOS_WRITE(freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
  90. cmos_minutes = CMOS_READ(RTC_MINUTES);
  91. BCD_TO_BIN(cmos_minutes);
  92. /*
  93. * since we're only adjusting minutes and seconds, don't interfere with hour
  94. * overflow. This avoids messing with unknown time zones but requires your RTC
  95. * not to be off by more than 15 minutes. Since we're calling it only when
  96. * our clock is externally synchronized using NTP, this shouldn't be a problem.
  97. */
  98. real_seconds = nowtime % 60;
  99. real_minutes = nowtime / 60;
  100. if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
  101. real_minutes += 30; /* correct for half hour time zone */
  102. real_minutes %= 60;
  103. if (abs(real_minutes - cmos_minutes) >= 30) {
  104. printk(KERN_WARNING "time.c: can't update CMOS clock "
  105. "from %d to %d\n", cmos_minutes, real_minutes);
  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. }
  123. void main_timer_handler(void)
  124. {
  125. static unsigned long rtc_update = 0;
  126. /*
  127. * Here we are in the timer irq handler. We have irqs locally disabled (so we
  128. * don't need spin_lock_irqsave()) but we don't know if the timer_bh is running
  129. * on the other CPU, so we need a lock. We also need to lock the vsyscall
  130. * variables, because both do_timer() and us change them -arca+vojtech
  131. */
  132. write_seqlock(&xtime_lock);
  133. /*
  134. * Do the timer stuff.
  135. */
  136. do_timer(1);
  137. #ifndef CONFIG_SMP
  138. update_process_times(user_mode(get_irq_regs()));
  139. #endif
  140. /*
  141. * In the SMP case we use the local APIC timer interrupt to do the profiling,
  142. * except when we simulate SMP mode on a uniprocessor system, in that case we
  143. * have to call the local interrupt handler.
  144. */
  145. if (!using_apic_timer)
  146. smp_local_timer_interrupt();
  147. /*
  148. * If we have an externally synchronized Linux clock, then update CMOS clock
  149. * accordingly every ~11 minutes. set_rtc_mmss() will be called in the jiffy
  150. * closest to exactly 500 ms before the next second. If the update fails, we
  151. * don't care, as it'll be updated on the next turn, and the problem (time way
  152. * off) isn't likely to go away much sooner anyway.
  153. */
  154. if (ntp_synced() && xtime.tv_sec > rtc_update &&
  155. abs(xtime.tv_nsec - 500000000) <= tick_nsec / 2) {
  156. set_rtc_mmss(xtime.tv_sec);
  157. rtc_update = xtime.tv_sec + 660;
  158. }
  159. write_sequnlock(&xtime_lock);
  160. }
  161. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  162. {
  163. if (apic_runs_main_timer > 1)
  164. return IRQ_HANDLED;
  165. main_timer_handler();
  166. if (using_apic_timer)
  167. smp_send_timer_broadcast_ipi();
  168. return IRQ_HANDLED;
  169. }
  170. static unsigned long get_cmos_time(void)
  171. {
  172. unsigned int year, mon, day, hour, min, sec;
  173. unsigned long flags;
  174. unsigned century = 0;
  175. spin_lock_irqsave(&rtc_lock, flags);
  176. do {
  177. sec = CMOS_READ(RTC_SECONDS);
  178. min = CMOS_READ(RTC_MINUTES);
  179. hour = CMOS_READ(RTC_HOURS);
  180. day = CMOS_READ(RTC_DAY_OF_MONTH);
  181. mon = CMOS_READ(RTC_MONTH);
  182. year = CMOS_READ(RTC_YEAR);
  183. #ifdef CONFIG_ACPI
  184. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  185. acpi_gbl_FADT.century)
  186. century = CMOS_READ(acpi_gbl_FADT.century);
  187. #endif
  188. } while (sec != CMOS_READ(RTC_SECONDS));
  189. spin_unlock_irqrestore(&rtc_lock, flags);
  190. /*
  191. * We know that x86-64 always uses BCD format, no need to check the
  192. * config register.
  193. */
  194. BCD_TO_BIN(sec);
  195. BCD_TO_BIN(min);
  196. BCD_TO_BIN(hour);
  197. BCD_TO_BIN(day);
  198. BCD_TO_BIN(mon);
  199. BCD_TO_BIN(year);
  200. if (century) {
  201. BCD_TO_BIN(century);
  202. year += century * 100;
  203. printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
  204. } else {
  205. /*
  206. * x86-64 systems only exists since 2002.
  207. * This will work up to Dec 31, 2100
  208. */
  209. year += 2000;
  210. }
  211. return mktime(year, mon, day, hour, min, sec);
  212. }
  213. /* calibrate_cpu is used on systems with fixed rate TSCs to determine
  214. * processor frequency */
  215. #define TICK_COUNT 100000000
  216. static unsigned int __init tsc_calibrate_cpu_khz(void)
  217. {
  218. int tsc_start, tsc_now;
  219. int i, no_ctr_free;
  220. unsigned long evntsel3 = 0, pmc3 = 0, pmc_now = 0;
  221. unsigned long flags;
  222. for (i = 0; i < 4; i++)
  223. if (avail_to_resrv_perfctr_nmi_bit(i))
  224. break;
  225. no_ctr_free = (i == 4);
  226. if (no_ctr_free) {
  227. i = 3;
  228. rdmsrl(MSR_K7_EVNTSEL3, evntsel3);
  229. wrmsrl(MSR_K7_EVNTSEL3, 0);
  230. rdmsrl(MSR_K7_PERFCTR3, pmc3);
  231. } else {
  232. reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  233. reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  234. }
  235. local_irq_save(flags);
  236. /* start meauring cycles, incrementing from 0 */
  237. wrmsrl(MSR_K7_PERFCTR0 + i, 0);
  238. wrmsrl(MSR_K7_EVNTSEL0 + i, 1 << 22 | 3 << 16 | 0x76);
  239. rdtscl(tsc_start);
  240. do {
  241. rdmsrl(MSR_K7_PERFCTR0 + i, pmc_now);
  242. tsc_now = get_cycles_sync();
  243. } while ((tsc_now - tsc_start) < TICK_COUNT);
  244. local_irq_restore(flags);
  245. if (no_ctr_free) {
  246. wrmsrl(MSR_K7_EVNTSEL3, 0);
  247. wrmsrl(MSR_K7_PERFCTR3, pmc3);
  248. wrmsrl(MSR_K7_EVNTSEL3, evntsel3);
  249. } else {
  250. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  251. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  252. }
  253. return pmc_now * tsc_khz / (tsc_now - tsc_start);
  254. }
  255. /*
  256. * pit_calibrate_tsc() uses the speaker output (channel 2) of
  257. * the PIT. This is better than using the timer interrupt output,
  258. * because we can read the value of the speaker with just one inb(),
  259. * where we need three i/o operations for the interrupt channel.
  260. * We count how many ticks the TSC does in 50 ms.
  261. */
  262. static unsigned int __init pit_calibrate_tsc(void)
  263. {
  264. unsigned long start, end;
  265. unsigned long flags;
  266. spin_lock_irqsave(&i8253_lock, flags);
  267. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  268. outb(0xb0, 0x43);
  269. outb((PIT_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
  270. outb((PIT_TICK_RATE / (1000 / 50)) >> 8, 0x42);
  271. start = get_cycles_sync();
  272. while ((inb(0x61) & 0x20) == 0);
  273. end = get_cycles_sync();
  274. spin_unlock_irqrestore(&i8253_lock, flags);
  275. return (end - start) / 50;
  276. }
  277. #define PIT_MODE 0x43
  278. #define PIT_CH0 0x40
  279. static void __pit_init(int val, u8 mode)
  280. {
  281. unsigned long flags;
  282. spin_lock_irqsave(&i8253_lock, flags);
  283. outb_p(mode, PIT_MODE);
  284. outb_p(val & 0xff, PIT_CH0); /* LSB */
  285. outb_p(val >> 8, PIT_CH0); /* MSB */
  286. spin_unlock_irqrestore(&i8253_lock, flags);
  287. }
  288. void __init pit_init(void)
  289. {
  290. __pit_init(LATCH, 0x34); /* binary, mode 2, LSB/MSB, ch 0 */
  291. }
  292. void pit_stop_interrupt(void)
  293. {
  294. __pit_init(0, 0x30); /* mode 0 */
  295. }
  296. void stop_timer_interrupt(void)
  297. {
  298. char *name;
  299. if (hpet_address) {
  300. name = "HPET";
  301. hpet_timer_stop_set_go(0);
  302. } else {
  303. name = "PIT";
  304. pit_stop_interrupt();
  305. }
  306. printk(KERN_INFO "timer: %s interrupt stopped.\n", name);
  307. }
  308. static struct irqaction irq0 = {
  309. .handler = timer_interrupt,
  310. .flags = IRQF_DISABLED | IRQF_IRQPOLL,
  311. .mask = CPU_MASK_NONE,
  312. .name = "timer"
  313. };
  314. void __init time_init(void)
  315. {
  316. if (nohpet)
  317. hpet_address = 0;
  318. xtime.tv_sec = get_cmos_time();
  319. xtime.tv_nsec = 0;
  320. set_normalized_timespec(&wall_to_monotonic,
  321. -xtime.tv_sec, -xtime.tv_nsec);
  322. if (hpet_arch_init())
  323. hpet_address = 0;
  324. if (hpet_use_timer) {
  325. /* set tick_nsec to use the proper rate for HPET */
  326. tick_nsec = TICK_NSEC_HPET;
  327. tsc_khz = hpet_calibrate_tsc();
  328. timename = "HPET";
  329. } else {
  330. pit_init();
  331. tsc_khz = pit_calibrate_tsc();
  332. timename = "PIT";
  333. }
  334. cpu_khz = tsc_khz;
  335. if (cpu_has(&boot_cpu_data, X86_FEATURE_CONSTANT_TSC) &&
  336. boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
  337. boot_cpu_data.x86 == 16)
  338. cpu_khz = tsc_calibrate_cpu_khz();
  339. if (unsynchronized_tsc())
  340. mark_tsc_unstable("TSCs unsynchronized");
  341. if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  342. vgetcpu_mode = VGETCPU_RDTSCP;
  343. else
  344. vgetcpu_mode = VGETCPU_LSL;
  345. set_cyc2ns_scale(tsc_khz);
  346. printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
  347. cpu_khz / 1000, cpu_khz % 1000);
  348. init_tsc_clocksource();
  349. setup_irq(0, &irq0);
  350. }
  351. static long clock_cmos_diff;
  352. static unsigned long sleep_start;
  353. /*
  354. * sysfs support for the timer.
  355. */
  356. static int timer_suspend(struct sys_device *dev, pm_message_t state)
  357. {
  358. /*
  359. * Estimate time zone so that set_time can update the clock
  360. */
  361. long cmos_time = get_cmos_time();
  362. clock_cmos_diff = -cmos_time;
  363. clock_cmos_diff += get_seconds();
  364. sleep_start = cmos_time;
  365. return 0;
  366. }
  367. static int timer_resume(struct sys_device *dev)
  368. {
  369. unsigned long flags;
  370. unsigned long sec;
  371. unsigned long ctime = get_cmos_time();
  372. long sleep_length = (ctime - sleep_start) * HZ;
  373. if (sleep_length < 0) {
  374. printk(KERN_WARNING "Time skew detected in timer resume!\n");
  375. /* The time after the resume must not be earlier than the time
  376. * before the suspend or some nasty things will happen
  377. */
  378. sleep_length = 0;
  379. ctime = sleep_start;
  380. }
  381. if (hpet_address)
  382. hpet_reenable();
  383. else
  384. i8254_timer_resume();
  385. sec = ctime + clock_cmos_diff;
  386. write_seqlock_irqsave(&xtime_lock,flags);
  387. xtime.tv_sec = sec;
  388. xtime.tv_nsec = 0;
  389. jiffies += sleep_length;
  390. write_sequnlock_irqrestore(&xtime_lock,flags);
  391. touch_softlockup_watchdog();
  392. return 0;
  393. }
  394. static struct sysdev_class timer_sysclass = {
  395. .resume = timer_resume,
  396. .suspend = timer_suspend,
  397. set_kset_name("timer"),
  398. };
  399. /* XXX this sysfs stuff should probably go elsewhere later -john */
  400. static struct sys_device device_timer = {
  401. .id = 0,
  402. .cls = &timer_sysclass,
  403. };
  404. static int time_init_device(void)
  405. {
  406. int error = sysdev_class_register(&timer_sysclass);
  407. if (!error)
  408. error = sysdev_register(&device_timer);
  409. return error;
  410. }
  411. device_initcall(time_init_device);