time.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. extern void i8254_timer_resume(void);
  46. extern int using_apic_timer;
  47. static char *timename = NULL;
  48. DEFINE_SPINLOCK(rtc_lock);
  49. EXPORT_SYMBOL(rtc_lock);
  50. DEFINE_SPINLOCK(i8253_lock);
  51. volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
  52. unsigned long profile_pc(struct pt_regs *regs)
  53. {
  54. unsigned long pc = instruction_pointer(regs);
  55. /* Assume the lock function has either no stack frame or a copy
  56. of eflags from PUSHF
  57. Eflags always has bits 22 and up cleared unlike kernel addresses. */
  58. if (!user_mode(regs) && in_lock_functions(pc)) {
  59. unsigned long *sp = (unsigned long *)regs->rsp;
  60. if (sp[0] >> 22)
  61. return sp[0];
  62. if (sp[1] >> 22)
  63. return sp[1];
  64. }
  65. return pc;
  66. }
  67. EXPORT_SYMBOL(profile_pc);
  68. /*
  69. * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
  70. * ms after the second nowtime has started, because when nowtime is written
  71. * into the registers of the CMOS clock, it will jump to the next second
  72. * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
  73. * sheet for details.
  74. */
  75. static void set_rtc_mmss(unsigned long nowtime)
  76. {
  77. int real_seconds, real_minutes, cmos_minutes;
  78. unsigned char control, freq_select;
  79. /*
  80. * IRQs are disabled when we're called from the timer interrupt,
  81. * no need for spin_lock_irqsave()
  82. */
  83. spin_lock(&rtc_lock);
  84. /*
  85. * Tell the clock it's being set and stop it.
  86. */
  87. control = CMOS_READ(RTC_CONTROL);
  88. CMOS_WRITE(control | RTC_SET, RTC_CONTROL);
  89. freq_select = CMOS_READ(RTC_FREQ_SELECT);
  90. CMOS_WRITE(freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
  91. cmos_minutes = CMOS_READ(RTC_MINUTES);
  92. BCD_TO_BIN(cmos_minutes);
  93. /*
  94. * since we're only adjusting minutes and seconds, don't interfere with hour
  95. * overflow. This avoids messing with unknown time zones but requires your RTC
  96. * not to be off by more than 15 minutes. Since we're calling it only when
  97. * our clock is externally synchronized using NTP, this shouldn't be a problem.
  98. */
  99. real_seconds = nowtime % 60;
  100. real_minutes = nowtime / 60;
  101. if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
  102. real_minutes += 30; /* correct for half hour time zone */
  103. real_minutes %= 60;
  104. if (abs(real_minutes - cmos_minutes) >= 30) {
  105. printk(KERN_WARNING "time.c: can't update CMOS clock "
  106. "from %d to %d\n", cmos_minutes, real_minutes);
  107. } else {
  108. BIN_TO_BCD(real_seconds);
  109. BIN_TO_BCD(real_minutes);
  110. CMOS_WRITE(real_seconds, RTC_SECONDS);
  111. CMOS_WRITE(real_minutes, RTC_MINUTES);
  112. }
  113. /*
  114. * The following flags have to be released exactly in this order, otherwise the
  115. * DS12887 (popular MC146818A clone with integrated battery and quartz) will
  116. * not reset the oscillator and will not update precisely 500 ms later. You
  117. * won't find this mentioned in the Dallas Semiconductor data sheets, but who
  118. * believes data sheets anyway ... -- Markus Kuhn
  119. */
  120. CMOS_WRITE(control, RTC_CONTROL);
  121. CMOS_WRITE(freq_select, RTC_FREQ_SELECT);
  122. spin_unlock(&rtc_lock);
  123. }
  124. void main_timer_handler(void)
  125. {
  126. static unsigned long rtc_update = 0;
  127. /*
  128. * Here we are in the timer irq handler. We have irqs locally disabled (so we
  129. * don't need spin_lock_irqsave()) but we don't know if the timer_bh is running
  130. * on the other CPU, so we need a lock. We also need to lock the vsyscall
  131. * variables, because both do_timer() and us change them -arca+vojtech
  132. */
  133. write_seqlock(&xtime_lock);
  134. /*
  135. * Do the timer stuff.
  136. */
  137. do_timer(1);
  138. #ifndef CONFIG_SMP
  139. update_process_times(user_mode(get_irq_regs()));
  140. #endif
  141. /*
  142. * In the SMP case we use the local APIC timer interrupt to do the profiling,
  143. * except when we simulate SMP mode on a uniprocessor system, in that case we
  144. * have to call the local interrupt handler.
  145. */
  146. if (!using_apic_timer)
  147. smp_local_timer_interrupt();
  148. /*
  149. * If we have an externally synchronized Linux clock, then update CMOS clock
  150. * accordingly every ~11 minutes. set_rtc_mmss() will be called in the jiffy
  151. * closest to exactly 500 ms before the next second. If the update fails, we
  152. * don't care, as it'll be updated on the next turn, and the problem (time way
  153. * off) isn't likely to go away much sooner anyway.
  154. */
  155. if (ntp_synced() && xtime.tv_sec > rtc_update &&
  156. abs(xtime.tv_nsec - 500000000) <= tick_nsec / 2) {
  157. set_rtc_mmss(xtime.tv_sec);
  158. rtc_update = xtime.tv_sec + 660;
  159. }
  160. write_sequnlock(&xtime_lock);
  161. }
  162. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  163. {
  164. if (apic_runs_main_timer > 1)
  165. return IRQ_HANDLED;
  166. main_timer_handler();
  167. if (using_apic_timer)
  168. smp_send_timer_broadcast_ipi();
  169. return IRQ_HANDLED;
  170. }
  171. static unsigned long get_cmos_time(void)
  172. {
  173. unsigned int year, mon, day, hour, min, sec;
  174. unsigned long flags;
  175. unsigned century = 0;
  176. spin_lock_irqsave(&rtc_lock, flags);
  177. do {
  178. sec = CMOS_READ(RTC_SECONDS);
  179. min = CMOS_READ(RTC_MINUTES);
  180. hour = CMOS_READ(RTC_HOURS);
  181. day = CMOS_READ(RTC_DAY_OF_MONTH);
  182. mon = CMOS_READ(RTC_MONTH);
  183. year = CMOS_READ(RTC_YEAR);
  184. #ifdef CONFIG_ACPI
  185. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  186. acpi_gbl_FADT.century)
  187. century = CMOS_READ(acpi_gbl_FADT.century);
  188. #endif
  189. } while (sec != CMOS_READ(RTC_SECONDS));
  190. spin_unlock_irqrestore(&rtc_lock, flags);
  191. /*
  192. * We know that x86-64 always uses BCD format, no need to check the
  193. * config register.
  194. */
  195. BCD_TO_BIN(sec);
  196. BCD_TO_BIN(min);
  197. BCD_TO_BIN(hour);
  198. BCD_TO_BIN(day);
  199. BCD_TO_BIN(mon);
  200. BCD_TO_BIN(year);
  201. if (century) {
  202. BCD_TO_BIN(century);
  203. year += century * 100;
  204. printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
  205. } else {
  206. /*
  207. * x86-64 systems only exists since 2002.
  208. * This will work up to Dec 31, 2100
  209. */
  210. year += 2000;
  211. }
  212. return mktime(year, mon, day, hour, min, sec);
  213. }
  214. /*
  215. * pit_calibrate_tsc() uses the speaker output (channel 2) of
  216. * the PIT. This is better than using the timer interrupt output,
  217. * because we can read the value of the speaker with just one inb(),
  218. * where we need three i/o operations for the interrupt channel.
  219. * We count how many ticks the TSC does in 50 ms.
  220. */
  221. static unsigned int __init pit_calibrate_tsc(void)
  222. {
  223. unsigned long start, end;
  224. unsigned long flags;
  225. spin_lock_irqsave(&i8253_lock, flags);
  226. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  227. outb(0xb0, 0x43);
  228. outb((PIT_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
  229. outb((PIT_TICK_RATE / (1000 / 50)) >> 8, 0x42);
  230. start = get_cycles_sync();
  231. while ((inb(0x61) & 0x20) == 0);
  232. end = get_cycles_sync();
  233. spin_unlock_irqrestore(&i8253_lock, flags);
  234. return (end - start) / 50;
  235. }
  236. #define PIT_MODE 0x43
  237. #define PIT_CH0 0x40
  238. static void __init __pit_init(int val, u8 mode)
  239. {
  240. unsigned long flags;
  241. spin_lock_irqsave(&i8253_lock, flags);
  242. outb_p(mode, PIT_MODE);
  243. outb_p(val & 0xff, PIT_CH0); /* LSB */
  244. outb_p(val >> 8, PIT_CH0); /* MSB */
  245. spin_unlock_irqrestore(&i8253_lock, flags);
  246. }
  247. void __init pit_init(void)
  248. {
  249. __pit_init(LATCH, 0x34); /* binary, mode 2, LSB/MSB, ch 0 */
  250. }
  251. void __init pit_stop_interrupt(void)
  252. {
  253. __pit_init(0, 0x30); /* mode 0 */
  254. }
  255. void __init stop_timer_interrupt(void)
  256. {
  257. char *name;
  258. if (hpet_address) {
  259. name = "HPET";
  260. hpet_timer_stop_set_go(0);
  261. } else {
  262. name = "PIT";
  263. pit_stop_interrupt();
  264. }
  265. printk(KERN_INFO "timer: %s interrupt stopped.\n", name);
  266. }
  267. static struct irqaction irq0 = {
  268. timer_interrupt, IRQF_DISABLED, CPU_MASK_NONE, "timer", NULL, NULL
  269. };
  270. void __init time_init(void)
  271. {
  272. if (nohpet)
  273. hpet_address = 0;
  274. xtime.tv_sec = get_cmos_time();
  275. xtime.tv_nsec = 0;
  276. set_normalized_timespec(&wall_to_monotonic,
  277. -xtime.tv_sec, -xtime.tv_nsec);
  278. if (hpet_arch_init())
  279. hpet_address = 0;
  280. if (hpet_use_timer) {
  281. /* set tick_nsec to use the proper rate for HPET */
  282. tick_nsec = TICK_NSEC_HPET;
  283. cpu_khz = hpet_calibrate_tsc();
  284. timename = "HPET";
  285. } else {
  286. pit_init();
  287. cpu_khz = pit_calibrate_tsc();
  288. timename = "PIT";
  289. }
  290. if (unsynchronized_tsc())
  291. mark_tsc_unstable();
  292. if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  293. vgetcpu_mode = VGETCPU_RDTSCP;
  294. else
  295. vgetcpu_mode = VGETCPU_LSL;
  296. set_cyc2ns_scale(cpu_khz);
  297. printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
  298. cpu_khz / 1000, cpu_khz % 1000);
  299. init_tsc_clocksource();
  300. setup_irq(0, &irq0);
  301. }
  302. static long clock_cmos_diff;
  303. static unsigned long sleep_start;
  304. /*
  305. * sysfs support for the timer.
  306. */
  307. static int timer_suspend(struct sys_device *dev, pm_message_t state)
  308. {
  309. /*
  310. * Estimate time zone so that set_time can update the clock
  311. */
  312. long cmos_time = get_cmos_time();
  313. clock_cmos_diff = -cmos_time;
  314. clock_cmos_diff += get_seconds();
  315. sleep_start = cmos_time;
  316. return 0;
  317. }
  318. static int timer_resume(struct sys_device *dev)
  319. {
  320. unsigned long flags;
  321. unsigned long sec;
  322. unsigned long ctime = get_cmos_time();
  323. long sleep_length = (ctime - sleep_start) * HZ;
  324. if (sleep_length < 0) {
  325. printk(KERN_WARNING "Time skew detected in timer resume!\n");
  326. /* The time after the resume must not be earlier than the time
  327. * before the suspend or some nasty things will happen
  328. */
  329. sleep_length = 0;
  330. ctime = sleep_start;
  331. }
  332. if (hpet_address)
  333. hpet_reenable();
  334. else
  335. i8254_timer_resume();
  336. sec = ctime + clock_cmos_diff;
  337. write_seqlock_irqsave(&xtime_lock,flags);
  338. xtime.tv_sec = sec;
  339. xtime.tv_nsec = 0;
  340. jiffies += sleep_length;
  341. write_sequnlock_irqrestore(&xtime_lock,flags);
  342. touch_softlockup_watchdog();
  343. return 0;
  344. }
  345. static struct sysdev_class timer_sysclass = {
  346. .resume = timer_resume,
  347. .suspend = timer_suspend,
  348. set_kset_name("timer"),
  349. };
  350. /* XXX this sysfs stuff should probably go elsewhere later -john */
  351. static struct sys_device device_timer = {
  352. .id = 0,
  353. .cls = &timer_sysclass,
  354. };
  355. static int time_init_device(void)
  356. {
  357. int error = sysdev_class_register(&timer_sysclass);
  358. if (!error)
  359. error = sysdev_register(&device_timer);
  360. return error;
  361. }
  362. device_initcall(time_init_device);