time.c 34 KB

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