time.c 11 KB

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