time.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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. #ifdef CONFIG_CPU_FREQ
  45. static void cpufreq_delayed_get(void);
  46. #endif
  47. extern void i8254_timer_resume(void);
  48. extern int using_apic_timer;
  49. static char *timename = NULL;
  50. DEFINE_SPINLOCK(rtc_lock);
  51. EXPORT_SYMBOL(rtc_lock);
  52. DEFINE_SPINLOCK(i8253_lock);
  53. int nohpet __initdata = 0;
  54. static int notsc __initdata = 0;
  55. #define USEC_PER_TICK (USEC_PER_SEC / HZ)
  56. #define NSEC_PER_TICK (NSEC_PER_SEC / HZ)
  57. #define FSEC_PER_TICK (FSEC_PER_SEC / HZ)
  58. #define NS_SCALE 10 /* 2^10, carefully chosen */
  59. #define US_SCALE 32 /* 2^32, arbitralrily chosen */
  60. unsigned int cpu_khz; /* TSC clocks / usec, not used here */
  61. EXPORT_SYMBOL(cpu_khz);
  62. static unsigned long hpet_period; /* fsecs / HPET clock */
  63. unsigned long hpet_tick; /* HPET clocks / interrupt */
  64. int hpet_use_timer; /* Use counter of hpet for time keeping, otherwise PIT */
  65. unsigned long vxtime_hz = PIT_TICK_RATE;
  66. int report_lost_ticks; /* command line option */
  67. unsigned long long monotonic_base;
  68. struct vxtime_data __vxtime __section_vxtime; /* for vsyscalls */
  69. volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
  70. unsigned long __wall_jiffies __section_wall_jiffies = INITIAL_JIFFIES;
  71. struct timespec __xtime __section_xtime;
  72. struct timezone __sys_tz __section_sys_tz;
  73. /*
  74. * do_gettimeoffset() returns microseconds since last timer interrupt was
  75. * triggered by hardware. A memory read of HPET is slower than a register read
  76. * of TSC, but much more reliable. It's also synchronized to the timer
  77. * interrupt. Note that do_gettimeoffset() may return more than hpet_tick, if a
  78. * timer interrupt has happened already, but vxtime.trigger wasn't updated yet.
  79. * This is not a problem, because jiffies hasn't updated either. They are bound
  80. * together by xtime_lock.
  81. */
  82. static inline unsigned int do_gettimeoffset_tsc(void)
  83. {
  84. unsigned long t;
  85. unsigned long x;
  86. t = get_cycles_sync();
  87. if (t < vxtime.last_tsc)
  88. t = vxtime.last_tsc; /* hack */
  89. x = ((t - vxtime.last_tsc) * vxtime.tsc_quot) >> US_SCALE;
  90. return x;
  91. }
  92. static inline unsigned int do_gettimeoffset_hpet(void)
  93. {
  94. /* cap counter read to one tick to avoid inconsistencies */
  95. unsigned long counter = hpet_readl(HPET_COUNTER) - vxtime.last;
  96. return (min(counter,hpet_tick) * vxtime.quot) >> US_SCALE;
  97. }
  98. unsigned int (*do_gettimeoffset)(void) = do_gettimeoffset_tsc;
  99. /*
  100. * This version of gettimeofday() has microsecond resolution and better than
  101. * microsecond precision, as we're using at least a 10 MHz (usually 14.31818
  102. * MHz) HPET timer.
  103. */
  104. void do_gettimeofday(struct timeval *tv)
  105. {
  106. unsigned long seq, t;
  107. unsigned int sec, usec;
  108. do {
  109. seq = read_seqbegin(&xtime_lock);
  110. sec = xtime.tv_sec;
  111. usec = xtime.tv_nsec / NSEC_PER_USEC;
  112. /* i386 does some correction here to keep the clock
  113. monotonous even when ntpd is fixing drift.
  114. But they didn't work for me, there is a non monotonic
  115. clock anyways with ntp.
  116. I dropped all corrections now until a real solution can
  117. be found. Note when you fix it here you need to do the same
  118. in arch/x86_64/kernel/vsyscall.c and export all needed
  119. variables in vmlinux.lds. -AK */
  120. t = (jiffies - wall_jiffies) * USEC_PER_TICK +
  121. do_gettimeoffset();
  122. usec += t;
  123. } while (read_seqretry(&xtime_lock, seq));
  124. tv->tv_sec = sec + usec / USEC_PER_SEC;
  125. tv->tv_usec = usec % USEC_PER_SEC;
  126. }
  127. EXPORT_SYMBOL(do_gettimeofday);
  128. /*
  129. * settimeofday() first undoes the correction that gettimeofday would do
  130. * on the time, and then saves it. This is ugly, but has been like this for
  131. * ages already.
  132. */
  133. int do_settimeofday(struct timespec *tv)
  134. {
  135. time_t wtm_sec, sec = tv->tv_sec;
  136. long wtm_nsec, nsec = tv->tv_nsec;
  137. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  138. return -EINVAL;
  139. write_seqlock_irq(&xtime_lock);
  140. nsec -= do_gettimeoffset() * NSEC_PER_USEC +
  141. (jiffies - wall_jiffies) * NSEC_PER_TICK;
  142. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  143. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  144. set_normalized_timespec(&xtime, sec, nsec);
  145. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  146. ntp_clear();
  147. write_sequnlock_irq(&xtime_lock);
  148. clock_was_set();
  149. return 0;
  150. }
  151. EXPORT_SYMBOL(do_settimeofday);
  152. unsigned long profile_pc(struct pt_regs *regs)
  153. {
  154. unsigned long pc = instruction_pointer(regs);
  155. /* Assume the lock function has either no stack frame or a copy
  156. of eflags from PUSHF
  157. Eflags always has bits 22 and up cleared unlike kernel addresses. */
  158. if (!user_mode(regs) && in_lock_functions(pc)) {
  159. unsigned long *sp = (unsigned long *)regs->rsp;
  160. if (sp[0] >> 22)
  161. return sp[0];
  162. if (sp[1] >> 22)
  163. return sp[1];
  164. }
  165. return pc;
  166. }
  167. EXPORT_SYMBOL(profile_pc);
  168. /*
  169. * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
  170. * ms after the second nowtime has started, because when nowtime is written
  171. * into the registers of the CMOS clock, it will jump to the next second
  172. * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
  173. * sheet for details.
  174. */
  175. static void set_rtc_mmss(unsigned long nowtime)
  176. {
  177. int real_seconds, real_minutes, cmos_minutes;
  178. unsigned char control, freq_select;
  179. /*
  180. * IRQs are disabled when we're called from the timer interrupt,
  181. * no need for spin_lock_irqsave()
  182. */
  183. spin_lock(&rtc_lock);
  184. /*
  185. * Tell the clock it's being set and stop it.
  186. */
  187. control = CMOS_READ(RTC_CONTROL);
  188. CMOS_WRITE(control | RTC_SET, RTC_CONTROL);
  189. freq_select = CMOS_READ(RTC_FREQ_SELECT);
  190. CMOS_WRITE(freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
  191. cmos_minutes = CMOS_READ(RTC_MINUTES);
  192. BCD_TO_BIN(cmos_minutes);
  193. /*
  194. * since we're only adjusting minutes and seconds, don't interfere with hour
  195. * overflow. This avoids messing with unknown time zones but requires your RTC
  196. * not to be off by more than 15 minutes. Since we're calling it only when
  197. * our clock is externally synchronized using NTP, this shouldn't be a problem.
  198. */
  199. real_seconds = nowtime % 60;
  200. real_minutes = nowtime / 60;
  201. if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
  202. real_minutes += 30; /* correct for half hour time zone */
  203. real_minutes %= 60;
  204. if (abs(real_minutes - cmos_minutes) >= 30) {
  205. printk(KERN_WARNING "time.c: can't update CMOS clock "
  206. "from %d to %d\n", cmos_minutes, real_minutes);
  207. } else {
  208. BIN_TO_BCD(real_seconds);
  209. BIN_TO_BCD(real_minutes);
  210. CMOS_WRITE(real_seconds, RTC_SECONDS);
  211. CMOS_WRITE(real_minutes, RTC_MINUTES);
  212. }
  213. /*
  214. * The following flags have to be released exactly in this order, otherwise the
  215. * DS12887 (popular MC146818A clone with integrated battery and quartz) will
  216. * not reset the oscillator and will not update precisely 500 ms later. You
  217. * won't find this mentioned in the Dallas Semiconductor data sheets, but who
  218. * believes data sheets anyway ... -- Markus Kuhn
  219. */
  220. CMOS_WRITE(control, RTC_CONTROL);
  221. CMOS_WRITE(freq_select, RTC_FREQ_SELECT);
  222. spin_unlock(&rtc_lock);
  223. }
  224. /* monotonic_clock(): returns # of nanoseconds passed since time_init()
  225. * Note: This function is required to return accurate
  226. * time even in the absence of multiple timer ticks.
  227. */
  228. static inline unsigned long long cycles_2_ns(unsigned long long cyc);
  229. unsigned long long monotonic_clock(void)
  230. {
  231. unsigned long seq;
  232. u32 last_offset, this_offset, offset;
  233. unsigned long long base;
  234. if (vxtime.mode == VXTIME_HPET) {
  235. do {
  236. seq = read_seqbegin(&xtime_lock);
  237. last_offset = vxtime.last;
  238. base = monotonic_base;
  239. this_offset = hpet_readl(HPET_COUNTER);
  240. } while (read_seqretry(&xtime_lock, seq));
  241. offset = (this_offset - last_offset);
  242. offset *= NSEC_PER_TICK / hpet_tick;
  243. } else {
  244. do {
  245. seq = read_seqbegin(&xtime_lock);
  246. last_offset = vxtime.last_tsc;
  247. base = monotonic_base;
  248. } while (read_seqretry(&xtime_lock, seq));
  249. this_offset = get_cycles_sync();
  250. offset = cycles_2_ns(this_offset - last_offset);
  251. }
  252. return base + offset;
  253. }
  254. EXPORT_SYMBOL(monotonic_clock);
  255. static noinline void handle_lost_ticks(int lost, struct pt_regs *regs)
  256. {
  257. static long lost_count;
  258. static int warned;
  259. if (report_lost_ticks) {
  260. printk(KERN_WARNING "time.c: Lost %d timer tick(s)! ", lost);
  261. print_symbol("rip %s)\n", regs->rip);
  262. }
  263. if (lost_count == 1000 && !warned) {
  264. printk(KERN_WARNING "warning: many lost ticks.\n"
  265. KERN_WARNING "Your time source seems to be instable or "
  266. "some driver is hogging interupts\n");
  267. print_symbol("rip %s\n", regs->rip);
  268. if (vxtime.mode == VXTIME_TSC && vxtime.hpet_address) {
  269. printk(KERN_WARNING "Falling back to HPET\n");
  270. if (hpet_use_timer)
  271. vxtime.last = hpet_readl(HPET_T0_CMP) -
  272. hpet_tick;
  273. else
  274. vxtime.last = hpet_readl(HPET_COUNTER);
  275. vxtime.mode = VXTIME_HPET;
  276. do_gettimeoffset = do_gettimeoffset_hpet;
  277. }
  278. /* else should fall back to PIT, but code missing. */
  279. warned = 1;
  280. } else
  281. lost_count++;
  282. #ifdef CONFIG_CPU_FREQ
  283. /* In some cases the CPU can change frequency without us noticing
  284. Give cpufreq a change to catch up. */
  285. if ((lost_count+1) % 25 == 0)
  286. cpufreq_delayed_get();
  287. #endif
  288. }
  289. void main_timer_handler(struct pt_regs *regs)
  290. {
  291. static unsigned long rtc_update = 0;
  292. unsigned long tsc;
  293. int delay = 0, offset = 0, lost = 0;
  294. /*
  295. * Here we are in the timer irq handler. We have irqs locally disabled (so we
  296. * don't need spin_lock_irqsave()) but we don't know if the timer_bh is running
  297. * on the other CPU, so we need a lock. We also need to lock the vsyscall
  298. * variables, because both do_timer() and us change them -arca+vojtech
  299. */
  300. write_seqlock(&xtime_lock);
  301. if (vxtime.hpet_address)
  302. offset = hpet_readl(HPET_COUNTER);
  303. if (hpet_use_timer) {
  304. /* if we're using the hpet timer functionality,
  305. * we can more accurately know the counter value
  306. * when the timer interrupt occured.
  307. */
  308. offset = hpet_readl(HPET_T0_CMP) - hpet_tick;
  309. delay = hpet_readl(HPET_COUNTER) - offset;
  310. } else if (!pmtmr_ioport) {
  311. spin_lock(&i8253_lock);
  312. outb_p(0x00, 0x43);
  313. delay = inb_p(0x40);
  314. delay |= inb(0x40) << 8;
  315. spin_unlock(&i8253_lock);
  316. delay = LATCH - 1 - delay;
  317. }
  318. tsc = get_cycles_sync();
  319. if (vxtime.mode == VXTIME_HPET) {
  320. if (offset - vxtime.last > hpet_tick) {
  321. lost = (offset - vxtime.last) / hpet_tick - 1;
  322. }
  323. monotonic_base +=
  324. (offset - vxtime.last) * NSEC_PER_TICK / hpet_tick;
  325. vxtime.last = offset;
  326. #ifdef CONFIG_X86_PM_TIMER
  327. } else if (vxtime.mode == VXTIME_PMTMR) {
  328. lost = pmtimer_mark_offset();
  329. #endif
  330. } else {
  331. offset = (((tsc - vxtime.last_tsc) *
  332. vxtime.tsc_quot) >> US_SCALE) - USEC_PER_TICK;
  333. if (offset < 0)
  334. offset = 0;
  335. if (offset > USEC_PER_TICK) {
  336. lost = offset / USEC_PER_TICK;
  337. offset %= USEC_PER_TICK;
  338. }
  339. monotonic_base += cycles_2_ns(tsc - vxtime.last_tsc);
  340. vxtime.last_tsc = tsc - vxtime.quot * delay / vxtime.tsc_quot;
  341. if ((((tsc - vxtime.last_tsc) *
  342. vxtime.tsc_quot) >> US_SCALE) < offset)
  343. vxtime.last_tsc = tsc -
  344. (((long) offset << US_SCALE) / vxtime.tsc_quot) - 1;
  345. }
  346. if (lost > 0) {
  347. handle_lost_ticks(lost, regs);
  348. jiffies += lost;
  349. }
  350. /*
  351. * Do the timer stuff.
  352. */
  353. do_timer(regs);
  354. #ifndef CONFIG_SMP
  355. update_process_times(user_mode(regs));
  356. #endif
  357. /*
  358. * In the SMP case we use the local APIC timer interrupt to do the profiling,
  359. * except when we simulate SMP mode on a uniprocessor system, in that case we
  360. * have to call the local interrupt handler.
  361. */
  362. if (!using_apic_timer)
  363. smp_local_timer_interrupt(regs);
  364. /*
  365. * If we have an externally synchronized Linux clock, then update CMOS clock
  366. * accordingly every ~11 minutes. set_rtc_mmss() will be called in the jiffy
  367. * closest to exactly 500 ms before the next second. If the update fails, we
  368. * don't care, as it'll be updated on the next turn, and the problem (time way
  369. * off) isn't likely to go away much sooner anyway.
  370. */
  371. if (ntp_synced() && xtime.tv_sec > rtc_update &&
  372. abs(xtime.tv_nsec - 500000000) <= tick_nsec / 2) {
  373. set_rtc_mmss(xtime.tv_sec);
  374. rtc_update = xtime.tv_sec + 660;
  375. }
  376. write_sequnlock(&xtime_lock);
  377. }
  378. static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  379. {
  380. if (apic_runs_main_timer > 1)
  381. return IRQ_HANDLED;
  382. main_timer_handler(regs);
  383. if (using_apic_timer)
  384. smp_send_timer_broadcast_ipi();
  385. return IRQ_HANDLED;
  386. }
  387. static unsigned int cyc2ns_scale __read_mostly;
  388. static inline void set_cyc2ns_scale(unsigned long cpu_khz)
  389. {
  390. cyc2ns_scale = (NSEC_PER_MSEC << NS_SCALE) / cpu_khz;
  391. }
  392. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  393. {
  394. return (cyc * cyc2ns_scale) >> NS_SCALE;
  395. }
  396. unsigned long long sched_clock(void)
  397. {
  398. unsigned long a = 0;
  399. #if 0
  400. /* Don't do a HPET read here. Using TSC always is much faster
  401. and HPET may not be mapped yet when the scheduler first runs.
  402. Disadvantage is a small drift between CPUs in some configurations,
  403. but that should be tolerable. */
  404. if (__vxtime.mode == VXTIME_HPET)
  405. return (hpet_readl(HPET_COUNTER) * vxtime.quot) >> US_SCALE;
  406. #endif
  407. /* Could do CPU core sync here. Opteron can execute rdtsc speculatively,
  408. which means it is not completely exact and may not be monotonous between
  409. CPUs. But the errors should be too small to matter for scheduling
  410. purposes. */
  411. rdtscll(a);
  412. return cycles_2_ns(a);
  413. }
  414. static unsigned long get_cmos_time(void)
  415. {
  416. unsigned int year, mon, day, hour, min, sec;
  417. unsigned long flags;
  418. unsigned extyear = 0;
  419. spin_lock_irqsave(&rtc_lock, flags);
  420. do {
  421. sec = CMOS_READ(RTC_SECONDS);
  422. min = CMOS_READ(RTC_MINUTES);
  423. hour = CMOS_READ(RTC_HOURS);
  424. day = CMOS_READ(RTC_DAY_OF_MONTH);
  425. mon = CMOS_READ(RTC_MONTH);
  426. year = CMOS_READ(RTC_YEAR);
  427. #ifdef CONFIG_ACPI
  428. if (acpi_fadt.revision >= FADT2_REVISION_ID &&
  429. acpi_fadt.century)
  430. extyear = CMOS_READ(acpi_fadt.century);
  431. #endif
  432. } while (sec != CMOS_READ(RTC_SECONDS));
  433. spin_unlock_irqrestore(&rtc_lock, flags);
  434. /*
  435. * We know that x86-64 always uses BCD format, no need to check the
  436. * config register.
  437. */
  438. BCD_TO_BIN(sec);
  439. BCD_TO_BIN(min);
  440. BCD_TO_BIN(hour);
  441. BCD_TO_BIN(day);
  442. BCD_TO_BIN(mon);
  443. BCD_TO_BIN(year);
  444. if (extyear) {
  445. BCD_TO_BIN(extyear);
  446. year += extyear;
  447. printk(KERN_INFO "Extended CMOS year: %d\n", extyear);
  448. } else {
  449. /*
  450. * x86-64 systems only exists since 2002.
  451. * This will work up to Dec 31, 2100
  452. */
  453. year += 2000;
  454. }
  455. return mktime(year, mon, day, hour, min, sec);
  456. }
  457. #ifdef CONFIG_CPU_FREQ
  458. /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
  459. changes.
  460. RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
  461. not that important because current Opteron setups do not support
  462. scaling on SMP anyroads.
  463. Should fix up last_tsc too. Currently gettimeofday in the
  464. first tick after the change will be slightly wrong. */
  465. #include <linux/workqueue.h>
  466. static unsigned int cpufreq_delayed_issched = 0;
  467. static unsigned int cpufreq_init = 0;
  468. static struct work_struct cpufreq_delayed_get_work;
  469. static void handle_cpufreq_delayed_get(void *v)
  470. {
  471. unsigned int cpu;
  472. for_each_online_cpu(cpu) {
  473. cpufreq_get(cpu);
  474. }
  475. cpufreq_delayed_issched = 0;
  476. }
  477. /* if we notice lost ticks, schedule a call to cpufreq_get() as it tries
  478. * to verify the CPU frequency the timing core thinks the CPU is running
  479. * at is still correct.
  480. */
  481. static void cpufreq_delayed_get(void)
  482. {
  483. static int warned;
  484. if (cpufreq_init && !cpufreq_delayed_issched) {
  485. cpufreq_delayed_issched = 1;
  486. if (!warned) {
  487. warned = 1;
  488. printk(KERN_DEBUG
  489. "Losing some ticks... checking if CPU frequency changed.\n");
  490. }
  491. schedule_work(&cpufreq_delayed_get_work);
  492. }
  493. }
  494. static unsigned int ref_freq = 0;
  495. static unsigned long loops_per_jiffy_ref = 0;
  496. static unsigned long cpu_khz_ref = 0;
  497. static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  498. void *data)
  499. {
  500. struct cpufreq_freqs *freq = data;
  501. unsigned long *lpj, dummy;
  502. if (cpu_has(&cpu_data[freq->cpu], X86_FEATURE_CONSTANT_TSC))
  503. return 0;
  504. lpj = &dummy;
  505. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  506. #ifdef CONFIG_SMP
  507. lpj = &cpu_data[freq->cpu].loops_per_jiffy;
  508. #else
  509. lpj = &boot_cpu_data.loops_per_jiffy;
  510. #endif
  511. if (!ref_freq) {
  512. ref_freq = freq->old;
  513. loops_per_jiffy_ref = *lpj;
  514. cpu_khz_ref = cpu_khz;
  515. }
  516. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  517. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  518. (val == CPUFREQ_RESUMECHANGE)) {
  519. *lpj =
  520. cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
  521. cpu_khz = cpufreq_scale(cpu_khz_ref, ref_freq, freq->new);
  522. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  523. vxtime.tsc_quot = (USEC_PER_MSEC << US_SCALE) / cpu_khz;
  524. }
  525. set_cyc2ns_scale(cpu_khz_ref);
  526. return 0;
  527. }
  528. static struct notifier_block time_cpufreq_notifier_block = {
  529. .notifier_call = time_cpufreq_notifier
  530. };
  531. static int __init cpufreq_tsc(void)
  532. {
  533. INIT_WORK(&cpufreq_delayed_get_work, handle_cpufreq_delayed_get, NULL);
  534. if (!cpufreq_register_notifier(&time_cpufreq_notifier_block,
  535. CPUFREQ_TRANSITION_NOTIFIER))
  536. cpufreq_init = 1;
  537. return 0;
  538. }
  539. core_initcall(cpufreq_tsc);
  540. #endif
  541. /*
  542. * calibrate_tsc() calibrates the processor TSC in a very simple way, comparing
  543. * it to the HPET timer of known frequency.
  544. */
  545. #define TICK_COUNT 100000000
  546. static unsigned int __init hpet_calibrate_tsc(void)
  547. {
  548. int tsc_start, hpet_start;
  549. int tsc_now, hpet_now;
  550. unsigned long flags;
  551. local_irq_save(flags);
  552. local_irq_disable();
  553. hpet_start = hpet_readl(HPET_COUNTER);
  554. rdtscl(tsc_start);
  555. do {
  556. local_irq_disable();
  557. hpet_now = hpet_readl(HPET_COUNTER);
  558. tsc_now = get_cycles_sync();
  559. local_irq_restore(flags);
  560. } while ((tsc_now - tsc_start) < TICK_COUNT &&
  561. (hpet_now - hpet_start) < TICK_COUNT);
  562. return (tsc_now - tsc_start) * 1000000000L
  563. / ((hpet_now - hpet_start) * hpet_period / 1000);
  564. }
  565. /*
  566. * pit_calibrate_tsc() uses the speaker output (channel 2) of
  567. * the PIT. This is better than using the timer interrupt output,
  568. * because we can read the value of the speaker with just one inb(),
  569. * where we need three i/o operations for the interrupt channel.
  570. * We count how many ticks the TSC does in 50 ms.
  571. */
  572. static unsigned int __init pit_calibrate_tsc(void)
  573. {
  574. unsigned long start, end;
  575. unsigned long flags;
  576. spin_lock_irqsave(&i8253_lock, flags);
  577. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  578. outb(0xb0, 0x43);
  579. outb((PIT_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
  580. outb((PIT_TICK_RATE / (1000 / 50)) >> 8, 0x42);
  581. start = get_cycles_sync();
  582. while ((inb(0x61) & 0x20) == 0);
  583. end = get_cycles_sync();
  584. spin_unlock_irqrestore(&i8253_lock, flags);
  585. return (end - start) / 50;
  586. }
  587. #ifdef CONFIG_HPET
  588. static __init int late_hpet_init(void)
  589. {
  590. struct hpet_data hd;
  591. unsigned int ntimer;
  592. if (!vxtime.hpet_address)
  593. return 0;
  594. memset(&hd, 0, sizeof (hd));
  595. ntimer = hpet_readl(HPET_ID);
  596. ntimer = (ntimer & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
  597. ntimer++;
  598. /*
  599. * Register with driver.
  600. * Timer0 and Timer1 is used by platform.
  601. */
  602. hd.hd_phys_address = vxtime.hpet_address;
  603. hd.hd_address = (void __iomem *)fix_to_virt(FIX_HPET_BASE);
  604. hd.hd_nirqs = ntimer;
  605. hd.hd_flags = HPET_DATA_PLATFORM;
  606. hpet_reserve_timer(&hd, 0);
  607. #ifdef CONFIG_HPET_EMULATE_RTC
  608. hpet_reserve_timer(&hd, 1);
  609. #endif
  610. hd.hd_irq[0] = HPET_LEGACY_8254;
  611. hd.hd_irq[1] = HPET_LEGACY_RTC;
  612. if (ntimer > 2) {
  613. struct hpet *hpet;
  614. struct hpet_timer *timer;
  615. int i;
  616. hpet = (struct hpet *) fix_to_virt(FIX_HPET_BASE);
  617. timer = &hpet->hpet_timers[2];
  618. for (i = 2; i < ntimer; timer++, i++)
  619. hd.hd_irq[i] = (timer->hpet_config &
  620. Tn_INT_ROUTE_CNF_MASK) >>
  621. Tn_INT_ROUTE_CNF_SHIFT;
  622. }
  623. hpet_alloc(&hd);
  624. return 0;
  625. }
  626. fs_initcall(late_hpet_init);
  627. #endif
  628. static int hpet_timer_stop_set_go(unsigned long tick)
  629. {
  630. unsigned int cfg;
  631. /*
  632. * Stop the timers and reset the main counter.
  633. */
  634. cfg = hpet_readl(HPET_CFG);
  635. cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY);
  636. hpet_writel(cfg, HPET_CFG);
  637. hpet_writel(0, HPET_COUNTER);
  638. hpet_writel(0, HPET_COUNTER + 4);
  639. /*
  640. * Set up timer 0, as periodic with first interrupt to happen at hpet_tick,
  641. * and period also hpet_tick.
  642. */
  643. if (hpet_use_timer) {
  644. hpet_writel(HPET_TN_ENABLE | HPET_TN_PERIODIC | HPET_TN_SETVAL |
  645. HPET_TN_32BIT, HPET_T0_CFG);
  646. hpet_writel(hpet_tick, HPET_T0_CMP); /* next interrupt */
  647. hpet_writel(hpet_tick, HPET_T0_CMP); /* period */
  648. cfg |= HPET_CFG_LEGACY;
  649. }
  650. /*
  651. * Go!
  652. */
  653. cfg |= HPET_CFG_ENABLE;
  654. hpet_writel(cfg, HPET_CFG);
  655. return 0;
  656. }
  657. static int hpet_init(void)
  658. {
  659. unsigned int id;
  660. if (!vxtime.hpet_address)
  661. return -1;
  662. set_fixmap_nocache(FIX_HPET_BASE, vxtime.hpet_address);
  663. __set_fixmap(VSYSCALL_HPET, vxtime.hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
  664. /*
  665. * Read the period, compute tick and quotient.
  666. */
  667. id = hpet_readl(HPET_ID);
  668. if (!(id & HPET_ID_VENDOR) || !(id & HPET_ID_NUMBER))
  669. return -1;
  670. hpet_period = hpet_readl(HPET_PERIOD);
  671. if (hpet_period < 100000 || hpet_period > 100000000)
  672. return -1;
  673. hpet_tick = (FSEC_PER_TICK + hpet_period / 2) / hpet_period;
  674. hpet_use_timer = (id & HPET_ID_LEGSUP);
  675. return hpet_timer_stop_set_go(hpet_tick);
  676. }
  677. static int hpet_reenable(void)
  678. {
  679. return hpet_timer_stop_set_go(hpet_tick);
  680. }
  681. #define PIT_MODE 0x43
  682. #define PIT_CH0 0x40
  683. static void __init __pit_init(int val, u8 mode)
  684. {
  685. unsigned long flags;
  686. spin_lock_irqsave(&i8253_lock, flags);
  687. outb_p(mode, PIT_MODE);
  688. outb_p(val & 0xff, PIT_CH0); /* LSB */
  689. outb_p(val >> 8, PIT_CH0); /* MSB */
  690. spin_unlock_irqrestore(&i8253_lock, flags);
  691. }
  692. void __init pit_init(void)
  693. {
  694. __pit_init(LATCH, 0x34); /* binary, mode 2, LSB/MSB, ch 0 */
  695. }
  696. void __init pit_stop_interrupt(void)
  697. {
  698. __pit_init(0, 0x30); /* mode 0 */
  699. }
  700. void __init stop_timer_interrupt(void)
  701. {
  702. char *name;
  703. if (vxtime.hpet_address) {
  704. name = "HPET";
  705. hpet_timer_stop_set_go(0);
  706. } else {
  707. name = "PIT";
  708. pit_stop_interrupt();
  709. }
  710. printk(KERN_INFO "timer: %s interrupt stopped.\n", name);
  711. }
  712. int __init time_setup(char *str)
  713. {
  714. report_lost_ticks = 1;
  715. return 1;
  716. }
  717. static struct irqaction irq0 = {
  718. timer_interrupt, IRQF_DISABLED, CPU_MASK_NONE, "timer", NULL, NULL
  719. };
  720. static int __cpuinit
  721. time_cpu_notifier(struct notifier_block *nb, unsigned long action, void *hcpu)
  722. {
  723. unsigned cpu = (unsigned long) hcpu;
  724. if (action == CPU_ONLINE)
  725. vsyscall_set_cpu(cpu);
  726. return NOTIFY_DONE;
  727. }
  728. void __init time_init(void)
  729. {
  730. if (nohpet)
  731. vxtime.hpet_address = 0;
  732. xtime.tv_sec = get_cmos_time();
  733. xtime.tv_nsec = 0;
  734. set_normalized_timespec(&wall_to_monotonic,
  735. -xtime.tv_sec, -xtime.tv_nsec);
  736. if (!hpet_init())
  737. vxtime_hz = (FSEC_PER_SEC + hpet_period / 2) / hpet_period;
  738. else
  739. vxtime.hpet_address = 0;
  740. if (hpet_use_timer) {
  741. /* set tick_nsec to use the proper rate for HPET */
  742. tick_nsec = TICK_NSEC_HPET;
  743. cpu_khz = hpet_calibrate_tsc();
  744. timename = "HPET";
  745. #ifdef CONFIG_X86_PM_TIMER
  746. } else if (pmtmr_ioport && !vxtime.hpet_address) {
  747. vxtime_hz = PM_TIMER_FREQUENCY;
  748. timename = "PM";
  749. pit_init();
  750. cpu_khz = pit_calibrate_tsc();
  751. #endif
  752. } else {
  753. pit_init();
  754. cpu_khz = pit_calibrate_tsc();
  755. timename = "PIT";
  756. }
  757. vxtime.mode = VXTIME_TSC;
  758. vxtime.quot = (USEC_PER_SEC << US_SCALE) / vxtime_hz;
  759. vxtime.tsc_quot = (USEC_PER_MSEC << US_SCALE) / cpu_khz;
  760. vxtime.last_tsc = get_cycles_sync();
  761. set_cyc2ns_scale(cpu_khz);
  762. setup_irq(0, &irq0);
  763. hotcpu_notifier(time_cpu_notifier, 0);
  764. time_cpu_notifier(NULL, CPU_ONLINE, (void *)(long)smp_processor_id());
  765. #ifndef CONFIG_SMP
  766. time_init_gtod();
  767. #endif
  768. }
  769. /*
  770. * Make an educated guess if the TSC is trustworthy and synchronized
  771. * over all CPUs.
  772. */
  773. __cpuinit int unsynchronized_tsc(void)
  774. {
  775. #ifdef CONFIG_SMP
  776. if (apic_is_clustered_box())
  777. return 1;
  778. #endif
  779. /* Most intel systems have synchronized TSCs except for
  780. multi node systems */
  781. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
  782. #ifdef CONFIG_ACPI
  783. /* But TSC doesn't tick in C3 so don't use it there */
  784. if (acpi_fadt.length > 0 && acpi_fadt.plvl3_lat < 100)
  785. return 1;
  786. #endif
  787. return 0;
  788. }
  789. /* Assume multi socket systems are not synchronized */
  790. return num_present_cpus() > 1;
  791. }
  792. /*
  793. * Decide what mode gettimeofday should use.
  794. */
  795. void time_init_gtod(void)
  796. {
  797. char *timetype;
  798. if (unsynchronized_tsc())
  799. notsc = 1;
  800. if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  801. vgetcpu_mode = VGETCPU_RDTSCP;
  802. else
  803. vgetcpu_mode = VGETCPU_LSL;
  804. if (vxtime.hpet_address && notsc) {
  805. timetype = hpet_use_timer ? "HPET" : "PIT/HPET";
  806. if (hpet_use_timer)
  807. vxtime.last = hpet_readl(HPET_T0_CMP) - hpet_tick;
  808. else
  809. vxtime.last = hpet_readl(HPET_COUNTER);
  810. vxtime.mode = VXTIME_HPET;
  811. do_gettimeoffset = do_gettimeoffset_hpet;
  812. #ifdef CONFIG_X86_PM_TIMER
  813. /* Using PM for gettimeofday is quite slow, but we have no other
  814. choice because the TSC is too unreliable on some systems. */
  815. } else if (pmtmr_ioport && !vxtime.hpet_address && notsc) {
  816. timetype = "PM";
  817. do_gettimeoffset = do_gettimeoffset_pm;
  818. vxtime.mode = VXTIME_PMTMR;
  819. sysctl_vsyscall = 0;
  820. printk(KERN_INFO "Disabling vsyscall due to use of PM timer\n");
  821. #endif
  822. } else {
  823. timetype = hpet_use_timer ? "HPET/TSC" : "PIT/TSC";
  824. vxtime.mode = VXTIME_TSC;
  825. }
  826. printk(KERN_INFO "time.c: Using %ld.%06ld MHz WALL %s GTOD %s timer.\n",
  827. vxtime_hz / 1000000, vxtime_hz % 1000000, timename, timetype);
  828. printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
  829. cpu_khz / 1000, cpu_khz % 1000);
  830. vxtime.quot = (USEC_PER_SEC << US_SCALE) / vxtime_hz;
  831. vxtime.tsc_quot = (USEC_PER_MSEC << US_SCALE) / cpu_khz;
  832. vxtime.last_tsc = get_cycles_sync();
  833. set_cyc2ns_scale(cpu_khz);
  834. }
  835. __setup("report_lost_ticks", time_setup);
  836. static long clock_cmos_diff;
  837. static unsigned long sleep_start;
  838. /*
  839. * sysfs support for the timer.
  840. */
  841. static int timer_suspend(struct sys_device *dev, pm_message_t state)
  842. {
  843. /*
  844. * Estimate time zone so that set_time can update the clock
  845. */
  846. long cmos_time = get_cmos_time();
  847. clock_cmos_diff = -cmos_time;
  848. clock_cmos_diff += get_seconds();
  849. sleep_start = cmos_time;
  850. return 0;
  851. }
  852. static int timer_resume(struct sys_device *dev)
  853. {
  854. unsigned long flags;
  855. unsigned long sec;
  856. unsigned long ctime = get_cmos_time();
  857. long sleep_length = (ctime - sleep_start) * HZ;
  858. if (sleep_length < 0) {
  859. printk(KERN_WARNING "Time skew detected in timer resume!\n");
  860. /* The time after the resume must not be earlier than the time
  861. * before the suspend or some nasty things will happen
  862. */
  863. sleep_length = 0;
  864. ctime = sleep_start;
  865. }
  866. if (vxtime.hpet_address)
  867. hpet_reenable();
  868. else
  869. i8254_timer_resume();
  870. sec = ctime + clock_cmos_diff;
  871. write_seqlock_irqsave(&xtime_lock,flags);
  872. xtime.tv_sec = sec;
  873. xtime.tv_nsec = 0;
  874. if (vxtime.mode == VXTIME_HPET) {
  875. if (hpet_use_timer)
  876. vxtime.last = hpet_readl(HPET_T0_CMP) - hpet_tick;
  877. else
  878. vxtime.last = hpet_readl(HPET_COUNTER);
  879. #ifdef CONFIG_X86_PM_TIMER
  880. } else if (vxtime.mode == VXTIME_PMTMR) {
  881. pmtimer_resume();
  882. #endif
  883. } else
  884. vxtime.last_tsc = get_cycles_sync();
  885. write_sequnlock_irqrestore(&xtime_lock,flags);
  886. jiffies += sleep_length;
  887. wall_jiffies += sleep_length;
  888. monotonic_base += sleep_length * (NSEC_PER_SEC/HZ);
  889. touch_softlockup_watchdog();
  890. return 0;
  891. }
  892. static struct sysdev_class timer_sysclass = {
  893. .resume = timer_resume,
  894. .suspend = timer_suspend,
  895. set_kset_name("timer"),
  896. };
  897. /* XXX this driverfs stuff should probably go elsewhere later -john */
  898. static struct sys_device device_timer = {
  899. .id = 0,
  900. .cls = &timer_sysclass,
  901. };
  902. static int time_init_device(void)
  903. {
  904. int error = sysdev_class_register(&timer_sysclass);
  905. if (!error)
  906. error = sysdev_register(&device_timer);
  907. return error;
  908. }
  909. device_initcall(time_init_device);
  910. #ifdef CONFIG_HPET_EMULATE_RTC
  911. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  912. * is enabled, we support RTC interrupt functionality in software.
  913. * RTC has 3 kinds of interrupts:
  914. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  915. * is updated
  916. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  917. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  918. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  919. * (1) and (2) above are implemented using polling at a frequency of
  920. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  921. * overhead. (DEFAULT_RTC_INT_FREQ)
  922. * For (3), we use interrupts at 64Hz or user specified periodic
  923. * frequency, whichever is higher.
  924. */
  925. #include <linux/rtc.h>
  926. #define DEFAULT_RTC_INT_FREQ 64
  927. #define RTC_NUM_INTS 1
  928. static unsigned long UIE_on;
  929. static unsigned long prev_update_sec;
  930. static unsigned long AIE_on;
  931. static struct rtc_time alarm_time;
  932. static unsigned long PIE_on;
  933. static unsigned long PIE_freq = DEFAULT_RTC_INT_FREQ;
  934. static unsigned long PIE_count;
  935. static unsigned long hpet_rtc_int_freq; /* RTC interrupt frequency */
  936. static unsigned int hpet_t1_cmp; /* cached comparator register */
  937. int is_hpet_enabled(void)
  938. {
  939. return vxtime.hpet_address != 0;
  940. }
  941. /*
  942. * Timer 1 for RTC, we do not use periodic interrupt feature,
  943. * even if HPET supports periodic interrupts on Timer 1.
  944. * The reason being, to set up a periodic interrupt in HPET, we need to
  945. * stop the main counter. And if we do that everytime someone diables/enables
  946. * RTC, we will have adverse effect on main kernel timer running on Timer 0.
  947. * So, for the time being, simulate the periodic interrupt in software.
  948. *
  949. * hpet_rtc_timer_init() is called for the first time and during subsequent
  950. * interuppts reinit happens through hpet_rtc_timer_reinit().
  951. */
  952. int hpet_rtc_timer_init(void)
  953. {
  954. unsigned int cfg, cnt;
  955. unsigned long flags;
  956. if (!is_hpet_enabled())
  957. return 0;
  958. /*
  959. * Set the counter 1 and enable the interrupts.
  960. */
  961. if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
  962. hpet_rtc_int_freq = PIE_freq;
  963. else
  964. hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
  965. local_irq_save(flags);
  966. cnt = hpet_readl(HPET_COUNTER);
  967. cnt += ((hpet_tick*HZ)/hpet_rtc_int_freq);
  968. hpet_writel(cnt, HPET_T1_CMP);
  969. hpet_t1_cmp = cnt;
  970. cfg = hpet_readl(HPET_T1_CFG);
  971. cfg &= ~HPET_TN_PERIODIC;
  972. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  973. hpet_writel(cfg, HPET_T1_CFG);
  974. local_irq_restore(flags);
  975. return 1;
  976. }
  977. static void hpet_rtc_timer_reinit(void)
  978. {
  979. unsigned int cfg, cnt, ticks_per_int, lost_ints;
  980. if (unlikely(!(PIE_on | AIE_on | UIE_on))) {
  981. cfg = hpet_readl(HPET_T1_CFG);
  982. cfg &= ~HPET_TN_ENABLE;
  983. hpet_writel(cfg, HPET_T1_CFG);
  984. return;
  985. }
  986. if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
  987. hpet_rtc_int_freq = PIE_freq;
  988. else
  989. hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
  990. /* It is more accurate to use the comparator value than current count.*/
  991. ticks_per_int = hpet_tick * HZ / hpet_rtc_int_freq;
  992. hpet_t1_cmp += ticks_per_int;
  993. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  994. /*
  995. * If the interrupt handler was delayed too long, the write above tries
  996. * to schedule the next interrupt in the past and the hardware would
  997. * not interrupt until the counter had wrapped around.
  998. * So we have to check that the comparator wasn't set to a past time.
  999. */
  1000. cnt = hpet_readl(HPET_COUNTER);
  1001. if (unlikely((int)(cnt - hpet_t1_cmp) > 0)) {
  1002. lost_ints = (cnt - hpet_t1_cmp) / ticks_per_int + 1;
  1003. /* Make sure that, even with the time needed to execute
  1004. * this code, the next scheduled interrupt has been moved
  1005. * back to the future: */
  1006. lost_ints++;
  1007. hpet_t1_cmp += lost_ints * ticks_per_int;
  1008. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  1009. if (PIE_on)
  1010. PIE_count += lost_ints;
  1011. printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n",
  1012. hpet_rtc_int_freq);
  1013. }
  1014. }
  1015. /*
  1016. * The functions below are called from rtc driver.
  1017. * Return 0 if HPET is not being used.
  1018. * Otherwise do the necessary changes and return 1.
  1019. */
  1020. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  1021. {
  1022. if (!is_hpet_enabled())
  1023. return 0;
  1024. if (bit_mask & RTC_UIE)
  1025. UIE_on = 0;
  1026. if (bit_mask & RTC_PIE)
  1027. PIE_on = 0;
  1028. if (bit_mask & RTC_AIE)
  1029. AIE_on = 0;
  1030. return 1;
  1031. }
  1032. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  1033. {
  1034. int timer_init_reqd = 0;
  1035. if (!is_hpet_enabled())
  1036. return 0;
  1037. if (!(PIE_on | AIE_on | UIE_on))
  1038. timer_init_reqd = 1;
  1039. if (bit_mask & RTC_UIE) {
  1040. UIE_on = 1;
  1041. }
  1042. if (bit_mask & RTC_PIE) {
  1043. PIE_on = 1;
  1044. PIE_count = 0;
  1045. }
  1046. if (bit_mask & RTC_AIE) {
  1047. AIE_on = 1;
  1048. }
  1049. if (timer_init_reqd)
  1050. hpet_rtc_timer_init();
  1051. return 1;
  1052. }
  1053. int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
  1054. {
  1055. if (!is_hpet_enabled())
  1056. return 0;
  1057. alarm_time.tm_hour = hrs;
  1058. alarm_time.tm_min = min;
  1059. alarm_time.tm_sec = sec;
  1060. return 1;
  1061. }
  1062. int hpet_set_periodic_freq(unsigned long freq)
  1063. {
  1064. if (!is_hpet_enabled())
  1065. return 0;
  1066. PIE_freq = freq;
  1067. PIE_count = 0;
  1068. return 1;
  1069. }
  1070. int hpet_rtc_dropped_irq(void)
  1071. {
  1072. if (!is_hpet_enabled())
  1073. return 0;
  1074. return 1;
  1075. }
  1076. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1077. {
  1078. struct rtc_time curr_time;
  1079. unsigned long rtc_int_flag = 0;
  1080. int call_rtc_interrupt = 0;
  1081. hpet_rtc_timer_reinit();
  1082. if (UIE_on | AIE_on) {
  1083. rtc_get_rtc_time(&curr_time);
  1084. }
  1085. if (UIE_on) {
  1086. if (curr_time.tm_sec != prev_update_sec) {
  1087. /* Set update int info, call real rtc int routine */
  1088. call_rtc_interrupt = 1;
  1089. rtc_int_flag = RTC_UF;
  1090. prev_update_sec = curr_time.tm_sec;
  1091. }
  1092. }
  1093. if (PIE_on) {
  1094. PIE_count++;
  1095. if (PIE_count >= hpet_rtc_int_freq/PIE_freq) {
  1096. /* Set periodic int info, call real rtc int routine */
  1097. call_rtc_interrupt = 1;
  1098. rtc_int_flag |= RTC_PF;
  1099. PIE_count = 0;
  1100. }
  1101. }
  1102. if (AIE_on) {
  1103. if ((curr_time.tm_sec == alarm_time.tm_sec) &&
  1104. (curr_time.tm_min == alarm_time.tm_min) &&
  1105. (curr_time.tm_hour == alarm_time.tm_hour)) {
  1106. /* Set alarm int info, call real rtc int routine */
  1107. call_rtc_interrupt = 1;
  1108. rtc_int_flag |= RTC_AF;
  1109. }
  1110. }
  1111. if (call_rtc_interrupt) {
  1112. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  1113. rtc_interrupt(rtc_int_flag, dev_id, regs);
  1114. }
  1115. return IRQ_HANDLED;
  1116. }
  1117. #endif
  1118. static int __init nohpet_setup(char *s)
  1119. {
  1120. nohpet = 1;
  1121. return 1;
  1122. }
  1123. __setup("nohpet", nohpet_setup);
  1124. int __init notsc_setup(char *s)
  1125. {
  1126. notsc = 1;
  1127. return 1;
  1128. }
  1129. __setup("notsc", notsc_setup);