time.c 33 KB

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