time.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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/cpufreq.h>
  42. #include <linux/hpet.h>
  43. #include <asm/apic.h>
  44. #include <asm/hpet.h>
  45. #ifdef CONFIG_CPU_FREQ
  46. extern void cpufreq_delayed_get(void);
  47. #endif
  48. extern void i8254_timer_resume(void);
  49. extern int using_apic_timer;
  50. static char *timename = NULL;
  51. DEFINE_SPINLOCK(rtc_lock);
  52. EXPORT_SYMBOL(rtc_lock);
  53. DEFINE_SPINLOCK(i8253_lock);
  54. unsigned long vxtime_hz = PIT_TICK_RATE;
  55. int report_lost_ticks; /* command line option */
  56. unsigned long long monotonic_base;
  57. struct vxtime_data __vxtime __section_vxtime; /* for vsyscalls */
  58. volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
  59. struct timespec __xtime __section_xtime;
  60. struct timezone __sys_tz __section_sys_tz;
  61. unsigned int (*do_gettimeoffset)(void) = do_gettimeoffset_tsc;
  62. /*
  63. * This version of gettimeofday() has microsecond resolution and better than
  64. * microsecond precision, as we're using at least a 10 MHz (usually 14.31818
  65. * MHz) HPET timer.
  66. */
  67. void do_gettimeofday(struct timeval *tv)
  68. {
  69. unsigned long seq;
  70. unsigned int sec, usec;
  71. do {
  72. seq = read_seqbegin(&xtime_lock);
  73. sec = xtime.tv_sec;
  74. usec = xtime.tv_nsec / NSEC_PER_USEC;
  75. /* i386 does some correction here to keep the clock
  76. monotonous even when ntpd is fixing drift.
  77. But they didn't work for me, there is a non monotonic
  78. clock anyways with ntp.
  79. I dropped all corrections now until a real solution can
  80. be found. Note when you fix it here you need to do the same
  81. in arch/x86_64/kernel/vsyscall.c and export all needed
  82. variables in vmlinux.lds. -AK */
  83. usec += do_gettimeoffset();
  84. } while (read_seqretry(&xtime_lock, seq));
  85. tv->tv_sec = sec + usec / USEC_PER_SEC;
  86. tv->tv_usec = usec % USEC_PER_SEC;
  87. }
  88. EXPORT_SYMBOL(do_gettimeofday);
  89. /*
  90. * settimeofday() first undoes the correction that gettimeofday would do
  91. * on the time, and then saves it. This is ugly, but has been like this for
  92. * ages already.
  93. */
  94. int do_settimeofday(struct timespec *tv)
  95. {
  96. time_t wtm_sec, sec = tv->tv_sec;
  97. long wtm_nsec, nsec = tv->tv_nsec;
  98. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  99. return -EINVAL;
  100. write_seqlock_irq(&xtime_lock);
  101. nsec -= do_gettimeoffset() * NSEC_PER_USEC;
  102. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  103. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  104. set_normalized_timespec(&xtime, sec, nsec);
  105. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  106. ntp_clear();
  107. write_sequnlock_irq(&xtime_lock);
  108. clock_was_set();
  109. return 0;
  110. }
  111. EXPORT_SYMBOL(do_settimeofday);
  112. unsigned long profile_pc(struct pt_regs *regs)
  113. {
  114. unsigned long pc = instruction_pointer(regs);
  115. /* Assume the lock function has either no stack frame or a copy
  116. of eflags from PUSHF
  117. Eflags always has bits 22 and up cleared unlike kernel addresses. */
  118. if (!user_mode(regs) && in_lock_functions(pc)) {
  119. unsigned long *sp = (unsigned long *)regs->rsp;
  120. if (sp[0] >> 22)
  121. return sp[0];
  122. if (sp[1] >> 22)
  123. return sp[1];
  124. }
  125. return pc;
  126. }
  127. EXPORT_SYMBOL(profile_pc);
  128. /*
  129. * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
  130. * ms after the second nowtime has started, because when nowtime is written
  131. * into the registers of the CMOS clock, it will jump to the next second
  132. * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
  133. * sheet for details.
  134. */
  135. static void set_rtc_mmss(unsigned long nowtime)
  136. {
  137. int real_seconds, real_minutes, cmos_minutes;
  138. unsigned char control, freq_select;
  139. /*
  140. * IRQs are disabled when we're called from the timer interrupt,
  141. * no need for spin_lock_irqsave()
  142. */
  143. spin_lock(&rtc_lock);
  144. /*
  145. * Tell the clock it's being set and stop it.
  146. */
  147. control = CMOS_READ(RTC_CONTROL);
  148. CMOS_WRITE(control | RTC_SET, RTC_CONTROL);
  149. freq_select = CMOS_READ(RTC_FREQ_SELECT);
  150. CMOS_WRITE(freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
  151. cmos_minutes = CMOS_READ(RTC_MINUTES);
  152. BCD_TO_BIN(cmos_minutes);
  153. /*
  154. * since we're only adjusting minutes and seconds, don't interfere with hour
  155. * overflow. This avoids messing with unknown time zones but requires your RTC
  156. * not to be off by more than 15 minutes. Since we're calling it only when
  157. * our clock is externally synchronized using NTP, this shouldn't be a problem.
  158. */
  159. real_seconds = nowtime % 60;
  160. real_minutes = nowtime / 60;
  161. if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
  162. real_minutes += 30; /* correct for half hour time zone */
  163. real_minutes %= 60;
  164. if (abs(real_minutes - cmos_minutes) >= 30) {
  165. printk(KERN_WARNING "time.c: can't update CMOS clock "
  166. "from %d to %d\n", cmos_minutes, real_minutes);
  167. } else {
  168. BIN_TO_BCD(real_seconds);
  169. BIN_TO_BCD(real_minutes);
  170. CMOS_WRITE(real_seconds, RTC_SECONDS);
  171. CMOS_WRITE(real_minutes, RTC_MINUTES);
  172. }
  173. /*
  174. * The following flags have to be released exactly in this order, otherwise the
  175. * DS12887 (popular MC146818A clone with integrated battery and quartz) will
  176. * not reset the oscillator and will not update precisely 500 ms later. You
  177. * won't find this mentioned in the Dallas Semiconductor data sheets, but who
  178. * believes data sheets anyway ... -- Markus Kuhn
  179. */
  180. CMOS_WRITE(control, RTC_CONTROL);
  181. CMOS_WRITE(freq_select, RTC_FREQ_SELECT);
  182. spin_unlock(&rtc_lock);
  183. }
  184. /* monotonic_clock(): returns # of nanoseconds passed since time_init()
  185. * Note: This function is required to return accurate
  186. * time even in the absence of multiple timer ticks.
  187. */
  188. extern unsigned long long cycles_2_ns(unsigned long long cyc);
  189. unsigned long long monotonic_clock(void)
  190. {
  191. unsigned long seq;
  192. u32 last_offset, this_offset, offset;
  193. unsigned long long base;
  194. if (vxtime.mode == VXTIME_HPET) {
  195. do {
  196. seq = read_seqbegin(&xtime_lock);
  197. last_offset = vxtime.last;
  198. base = monotonic_base;
  199. this_offset = hpet_readl(HPET_COUNTER);
  200. } while (read_seqretry(&xtime_lock, seq));
  201. offset = (this_offset - last_offset);
  202. offset *= NSEC_PER_TICK / hpet_tick;
  203. } else {
  204. do {
  205. seq = read_seqbegin(&xtime_lock);
  206. last_offset = vxtime.last_tsc;
  207. base = monotonic_base;
  208. } while (read_seqretry(&xtime_lock, seq));
  209. this_offset = get_cycles_sync();
  210. offset = cycles_2_ns(this_offset - last_offset);
  211. }
  212. return base + offset;
  213. }
  214. EXPORT_SYMBOL(monotonic_clock);
  215. static noinline void handle_lost_ticks(int lost)
  216. {
  217. static long lost_count;
  218. static int warned;
  219. if (report_lost_ticks) {
  220. printk(KERN_WARNING "time.c: Lost %d timer tick(s)! ", lost);
  221. print_symbol("rip %s)\n", get_irq_regs()->rip);
  222. }
  223. if (lost_count == 1000 && !warned) {
  224. printk(KERN_WARNING "warning: many lost ticks.\n"
  225. KERN_WARNING "Your time source seems to be instable or "
  226. "some driver is hogging interupts\n");
  227. print_symbol("rip %s\n", get_irq_regs()->rip);
  228. if (vxtime.mode == VXTIME_TSC && hpet_address) {
  229. printk(KERN_WARNING "Falling back to HPET\n");
  230. if (hpet_use_timer)
  231. vxtime.last = hpet_readl(HPET_T0_CMP) -
  232. hpet_tick;
  233. else
  234. vxtime.last = hpet_readl(HPET_COUNTER);
  235. vxtime.mode = VXTIME_HPET;
  236. vxtime.hpet_address = hpet_address;
  237. do_gettimeoffset = do_gettimeoffset_hpet;
  238. }
  239. /* else should fall back to PIT, but code missing. */
  240. warned = 1;
  241. } else
  242. lost_count++;
  243. #ifdef CONFIG_CPU_FREQ
  244. /* In some cases the CPU can change frequency without us noticing
  245. Give cpufreq a change to catch up. */
  246. if ((lost_count+1) % 25 == 0)
  247. cpufreq_delayed_get();
  248. #endif
  249. }
  250. void main_timer_handler(void)
  251. {
  252. static unsigned long rtc_update = 0;
  253. unsigned long tsc;
  254. int delay = 0, offset = 0, lost = 0;
  255. /*
  256. * Here we are in the timer irq handler. We have irqs locally disabled (so we
  257. * don't need spin_lock_irqsave()) but we don't know if the timer_bh is running
  258. * on the other CPU, so we need a lock. We also need to lock the vsyscall
  259. * variables, because both do_timer() and us change them -arca+vojtech
  260. */
  261. write_seqlock(&xtime_lock);
  262. if (hpet_address)
  263. offset = hpet_readl(HPET_COUNTER);
  264. if (hpet_use_timer) {
  265. /* if we're using the hpet timer functionality,
  266. * we can more accurately know the counter value
  267. * when the timer interrupt occured.
  268. */
  269. offset = hpet_readl(HPET_T0_CMP) - hpet_tick;
  270. delay = hpet_readl(HPET_COUNTER) - offset;
  271. } else if (!pmtmr_ioport) {
  272. spin_lock(&i8253_lock);
  273. outb_p(0x00, 0x43);
  274. delay = inb_p(0x40);
  275. delay |= inb(0x40) << 8;
  276. spin_unlock(&i8253_lock);
  277. delay = LATCH - 1 - delay;
  278. }
  279. tsc = get_cycles_sync();
  280. if (vxtime.mode == VXTIME_HPET) {
  281. if (offset - vxtime.last > hpet_tick) {
  282. lost = (offset - vxtime.last) / hpet_tick - 1;
  283. }
  284. monotonic_base +=
  285. (offset - vxtime.last) * NSEC_PER_TICK / hpet_tick;
  286. vxtime.last = offset;
  287. #ifdef CONFIG_X86_PM_TIMER
  288. } else if (vxtime.mode == VXTIME_PMTMR) {
  289. lost = pmtimer_mark_offset();
  290. #endif
  291. } else {
  292. offset = (((tsc - vxtime.last_tsc) *
  293. vxtime.tsc_quot) >> US_SCALE) - USEC_PER_TICK;
  294. if (offset < 0)
  295. offset = 0;
  296. if (offset > USEC_PER_TICK) {
  297. lost = offset / USEC_PER_TICK;
  298. offset %= USEC_PER_TICK;
  299. }
  300. monotonic_base += cycles_2_ns(tsc - vxtime.last_tsc);
  301. vxtime.last_tsc = tsc - vxtime.quot * delay / vxtime.tsc_quot;
  302. if ((((tsc - vxtime.last_tsc) *
  303. vxtime.tsc_quot) >> US_SCALE) < offset)
  304. vxtime.last_tsc = tsc -
  305. (((long) offset << US_SCALE) / vxtime.tsc_quot) - 1;
  306. }
  307. if (lost > 0)
  308. handle_lost_ticks(lost);
  309. else
  310. lost = 0;
  311. /*
  312. * Do the timer stuff.
  313. */
  314. do_timer(lost + 1);
  315. #ifndef CONFIG_SMP
  316. update_process_times(user_mode(get_irq_regs()));
  317. #endif
  318. /*
  319. * In the SMP case we use the local APIC timer interrupt to do the profiling,
  320. * except when we simulate SMP mode on a uniprocessor system, in that case we
  321. * have to call the local interrupt handler.
  322. */
  323. if (!using_apic_timer)
  324. smp_local_timer_interrupt();
  325. /*
  326. * If we have an externally synchronized Linux clock, then update CMOS clock
  327. * accordingly every ~11 minutes. set_rtc_mmss() will be called in the jiffy
  328. * closest to exactly 500 ms before the next second. If the update fails, we
  329. * don't care, as it'll be updated on the next turn, and the problem (time way
  330. * off) isn't likely to go away much sooner anyway.
  331. */
  332. if (ntp_synced() && xtime.tv_sec > rtc_update &&
  333. abs(xtime.tv_nsec - 500000000) <= tick_nsec / 2) {
  334. set_rtc_mmss(xtime.tv_sec);
  335. rtc_update = xtime.tv_sec + 660;
  336. }
  337. write_sequnlock(&xtime_lock);
  338. }
  339. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  340. {
  341. if (apic_runs_main_timer > 1)
  342. return IRQ_HANDLED;
  343. main_timer_handler();
  344. if (using_apic_timer)
  345. smp_send_timer_broadcast_ipi();
  346. return IRQ_HANDLED;
  347. }
  348. static unsigned long get_cmos_time(void)
  349. {
  350. unsigned int year, mon, day, hour, min, sec;
  351. unsigned long flags;
  352. unsigned century = 0;
  353. spin_lock_irqsave(&rtc_lock, flags);
  354. do {
  355. sec = CMOS_READ(RTC_SECONDS);
  356. min = CMOS_READ(RTC_MINUTES);
  357. hour = CMOS_READ(RTC_HOURS);
  358. day = CMOS_READ(RTC_DAY_OF_MONTH);
  359. mon = CMOS_READ(RTC_MONTH);
  360. year = CMOS_READ(RTC_YEAR);
  361. #ifdef CONFIG_ACPI
  362. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  363. acpi_gbl_FADT.century)
  364. century = CMOS_READ(acpi_gbl_FADT.century);
  365. #endif
  366. } while (sec != CMOS_READ(RTC_SECONDS));
  367. spin_unlock_irqrestore(&rtc_lock, flags);
  368. /*
  369. * We know that x86-64 always uses BCD format, no need to check the
  370. * config register.
  371. */
  372. BCD_TO_BIN(sec);
  373. BCD_TO_BIN(min);
  374. BCD_TO_BIN(hour);
  375. BCD_TO_BIN(day);
  376. BCD_TO_BIN(mon);
  377. BCD_TO_BIN(year);
  378. if (century) {
  379. BCD_TO_BIN(century);
  380. year += century * 100;
  381. printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
  382. } else {
  383. /*
  384. * x86-64 systems only exists since 2002.
  385. * This will work up to Dec 31, 2100
  386. */
  387. year += 2000;
  388. }
  389. return mktime(year, mon, day, hour, min, sec);
  390. }
  391. /*
  392. * pit_calibrate_tsc() uses the speaker output (channel 2) of
  393. * the PIT. This is better than using the timer interrupt output,
  394. * because we can read the value of the speaker with just one inb(),
  395. * where we need three i/o operations for the interrupt channel.
  396. * We count how many ticks the TSC does in 50 ms.
  397. */
  398. static unsigned int __init pit_calibrate_tsc(void)
  399. {
  400. unsigned long start, end;
  401. unsigned long flags;
  402. spin_lock_irqsave(&i8253_lock, flags);
  403. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  404. outb(0xb0, 0x43);
  405. outb((PIT_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
  406. outb((PIT_TICK_RATE / (1000 / 50)) >> 8, 0x42);
  407. start = get_cycles_sync();
  408. while ((inb(0x61) & 0x20) == 0);
  409. end = get_cycles_sync();
  410. spin_unlock_irqrestore(&i8253_lock, flags);
  411. return (end - start) / 50;
  412. }
  413. #define PIT_MODE 0x43
  414. #define PIT_CH0 0x40
  415. static void __init __pit_init(int val, u8 mode)
  416. {
  417. unsigned long flags;
  418. spin_lock_irqsave(&i8253_lock, flags);
  419. outb_p(mode, PIT_MODE);
  420. outb_p(val & 0xff, PIT_CH0); /* LSB */
  421. outb_p(val >> 8, PIT_CH0); /* MSB */
  422. spin_unlock_irqrestore(&i8253_lock, flags);
  423. }
  424. void __init pit_init(void)
  425. {
  426. __pit_init(LATCH, 0x34); /* binary, mode 2, LSB/MSB, ch 0 */
  427. }
  428. void __init pit_stop_interrupt(void)
  429. {
  430. __pit_init(0, 0x30); /* mode 0 */
  431. }
  432. void __init stop_timer_interrupt(void)
  433. {
  434. char *name;
  435. if (hpet_address) {
  436. name = "HPET";
  437. hpet_timer_stop_set_go(0);
  438. } else {
  439. name = "PIT";
  440. pit_stop_interrupt();
  441. }
  442. printk(KERN_INFO "timer: %s interrupt stopped.\n", name);
  443. }
  444. int __init time_setup(char *str)
  445. {
  446. report_lost_ticks = 1;
  447. return 1;
  448. }
  449. static struct irqaction irq0 = {
  450. timer_interrupt, IRQF_DISABLED, CPU_MASK_NONE, "timer", NULL, NULL
  451. };
  452. void __init time_init(void)
  453. {
  454. if (nohpet)
  455. hpet_address = 0;
  456. xtime.tv_sec = get_cmos_time();
  457. xtime.tv_nsec = 0;
  458. set_normalized_timespec(&wall_to_monotonic,
  459. -xtime.tv_sec, -xtime.tv_nsec);
  460. if (!hpet_arch_init())
  461. vxtime_hz = (FSEC_PER_SEC + hpet_period / 2) / hpet_period;
  462. else
  463. hpet_address = 0;
  464. if (hpet_use_timer) {
  465. /* set tick_nsec to use the proper rate for HPET */
  466. tick_nsec = TICK_NSEC_HPET;
  467. cpu_khz = hpet_calibrate_tsc();
  468. timename = "HPET";
  469. #ifdef CONFIG_X86_PM_TIMER
  470. } else if (pmtmr_ioport && !hpet_address) {
  471. vxtime_hz = PM_TIMER_FREQUENCY;
  472. timename = "PM";
  473. pit_init();
  474. cpu_khz = pit_calibrate_tsc();
  475. #endif
  476. } else {
  477. pit_init();
  478. cpu_khz = pit_calibrate_tsc();
  479. timename = "PIT";
  480. }
  481. vxtime.mode = VXTIME_TSC;
  482. vxtime.quot = (USEC_PER_SEC << US_SCALE) / vxtime_hz;
  483. vxtime.tsc_quot = (USEC_PER_MSEC << US_SCALE) / cpu_khz;
  484. vxtime.last_tsc = get_cycles_sync();
  485. set_cyc2ns_scale(cpu_khz);
  486. setup_irq(0, &irq0);
  487. #ifndef CONFIG_SMP
  488. time_init_gtod();
  489. #endif
  490. }
  491. /*
  492. * Decide what mode gettimeofday should use.
  493. */
  494. void time_init_gtod(void)
  495. {
  496. char *timetype;
  497. if (unsynchronized_tsc())
  498. notsc = 1;
  499. if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  500. vgetcpu_mode = VGETCPU_RDTSCP;
  501. else
  502. vgetcpu_mode = VGETCPU_LSL;
  503. if (hpet_address && notsc) {
  504. timetype = hpet_use_timer ? "HPET" : "PIT/HPET";
  505. if (hpet_use_timer)
  506. vxtime.last = hpet_readl(HPET_T0_CMP) - hpet_tick;
  507. else
  508. vxtime.last = hpet_readl(HPET_COUNTER);
  509. vxtime.mode = VXTIME_HPET;
  510. vxtime.hpet_address = hpet_address;
  511. do_gettimeoffset = do_gettimeoffset_hpet;
  512. #ifdef CONFIG_X86_PM_TIMER
  513. /* Using PM for gettimeofday is quite slow, but we have no other
  514. choice because the TSC is too unreliable on some systems. */
  515. } else if (pmtmr_ioport && !hpet_address && notsc) {
  516. timetype = "PM";
  517. do_gettimeoffset = do_gettimeoffset_pm;
  518. vxtime.mode = VXTIME_PMTMR;
  519. sysctl_vsyscall = 0;
  520. printk(KERN_INFO "Disabling vsyscall due to use of PM timer\n");
  521. #endif
  522. } else {
  523. timetype = hpet_use_timer ? "HPET/TSC" : "PIT/TSC";
  524. vxtime.mode = VXTIME_TSC;
  525. }
  526. printk(KERN_INFO "time.c: Using %ld.%06ld MHz WALL %s GTOD %s timer.\n",
  527. vxtime_hz / 1000000, vxtime_hz % 1000000, timename, timetype);
  528. printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
  529. cpu_khz / 1000, cpu_khz % 1000);
  530. vxtime.quot = (USEC_PER_SEC << US_SCALE) / vxtime_hz;
  531. vxtime.tsc_quot = (USEC_PER_MSEC << US_SCALE) / cpu_khz;
  532. vxtime.last_tsc = get_cycles_sync();
  533. set_cyc2ns_scale(cpu_khz);
  534. }
  535. __setup("report_lost_ticks", time_setup);
  536. static long clock_cmos_diff;
  537. static unsigned long sleep_start;
  538. /*
  539. * sysfs support for the timer.
  540. */
  541. static int timer_suspend(struct sys_device *dev, pm_message_t state)
  542. {
  543. /*
  544. * Estimate time zone so that set_time can update the clock
  545. */
  546. long cmos_time = get_cmos_time();
  547. clock_cmos_diff = -cmos_time;
  548. clock_cmos_diff += get_seconds();
  549. sleep_start = cmos_time;
  550. return 0;
  551. }
  552. static int timer_resume(struct sys_device *dev)
  553. {
  554. unsigned long flags;
  555. unsigned long sec;
  556. unsigned long ctime = get_cmos_time();
  557. long sleep_length = (ctime - sleep_start) * HZ;
  558. if (sleep_length < 0) {
  559. printk(KERN_WARNING "Time skew detected in timer resume!\n");
  560. /* The time after the resume must not be earlier than the time
  561. * before the suspend or some nasty things will happen
  562. */
  563. sleep_length = 0;
  564. ctime = sleep_start;
  565. }
  566. if (hpet_address)
  567. hpet_reenable();
  568. else
  569. i8254_timer_resume();
  570. sec = ctime + clock_cmos_diff;
  571. write_seqlock_irqsave(&xtime_lock,flags);
  572. xtime.tv_sec = sec;
  573. xtime.tv_nsec = 0;
  574. if (vxtime.mode == VXTIME_HPET) {
  575. if (hpet_use_timer)
  576. vxtime.last = hpet_readl(HPET_T0_CMP) - hpet_tick;
  577. else
  578. vxtime.last = hpet_readl(HPET_COUNTER);
  579. #ifdef CONFIG_X86_PM_TIMER
  580. } else if (vxtime.mode == VXTIME_PMTMR) {
  581. pmtimer_resume();
  582. #endif
  583. } else
  584. vxtime.last_tsc = get_cycles_sync();
  585. write_sequnlock_irqrestore(&xtime_lock,flags);
  586. jiffies += sleep_length;
  587. monotonic_base += sleep_length * (NSEC_PER_SEC/HZ);
  588. touch_softlockup_watchdog();
  589. return 0;
  590. }
  591. static struct sysdev_class timer_sysclass = {
  592. .resume = timer_resume,
  593. .suspend = timer_suspend,
  594. set_kset_name("timer"),
  595. };
  596. /* XXX this driverfs stuff should probably go elsewhere later -john */
  597. static struct sys_device device_timer = {
  598. .id = 0,
  599. .cls = &timer_sysclass,
  600. };
  601. static int time_init_device(void)
  602. {
  603. int error = sysdev_class_register(&timer_sysclass);
  604. if (!error)
  605. error = sysdev_register(&device_timer);
  606. return error;
  607. }
  608. device_initcall(time_init_device);