time.c 11 KB

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