time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * linux/arch/ia64/kernel/time.c
  3. *
  4. * Copyright (C) 1998-2003 Hewlett-Packard Co
  5. * Stephane Eranian <eranian@hpl.hp.com>
  6. * David Mosberger <davidm@hpl.hp.com>
  7. * Copyright (C) 1999 Don Dugger <don.dugger@intel.com>
  8. * Copyright (C) 1999-2000 VA Linux Systems
  9. * Copyright (C) 1999-2000 Walt Drummond <drummond@valinux.com>
  10. */
  11. #include <linux/cpu.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/profile.h>
  16. #include <linux/sched.h>
  17. #include <linux/time.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/efi.h>
  20. #include <linux/timex.h>
  21. #include <linux/timekeeper_internal.h>
  22. #include <linux/platform_device.h>
  23. #include <asm/machvec.h>
  24. #include <asm/delay.h>
  25. #include <asm/hw_irq.h>
  26. #include <asm/paravirt.h>
  27. #include <asm/ptrace.h>
  28. #include <asm/sal.h>
  29. #include <asm/sections.h>
  30. #include "fsyscall_gtod_data.h"
  31. static cycle_t itc_get_cycles(struct clocksource *cs);
  32. struct fsyscall_gtod_data_t fsyscall_gtod_data;
  33. struct itc_jitter_data_t itc_jitter_data;
  34. volatile int time_keeper_id = 0; /* smp_processor_id() of time-keeper */
  35. #ifdef CONFIG_IA64_DEBUG_IRQ
  36. unsigned long last_cli_ip;
  37. EXPORT_SYMBOL(last_cli_ip);
  38. #endif
  39. #ifdef CONFIG_PARAVIRT
  40. /* We need to define a real function for sched_clock, to override the
  41. weak default version */
  42. unsigned long long sched_clock(void)
  43. {
  44. return paravirt_sched_clock();
  45. }
  46. #endif
  47. #ifdef CONFIG_PARAVIRT
  48. static void
  49. paravirt_clocksource_resume(struct clocksource *cs)
  50. {
  51. if (pv_time_ops.clocksource_resume)
  52. pv_time_ops.clocksource_resume();
  53. }
  54. #endif
  55. static struct clocksource clocksource_itc = {
  56. .name = "itc",
  57. .rating = 350,
  58. .read = itc_get_cycles,
  59. .mask = CLOCKSOURCE_MASK(64),
  60. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  61. #ifdef CONFIG_PARAVIRT
  62. .resume = paravirt_clocksource_resume,
  63. #endif
  64. };
  65. static struct clocksource *itc_clocksource;
  66. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  67. #include <linux/kernel_stat.h>
  68. extern cputime_t cycle_to_cputime(u64 cyc);
  69. void vtime_account_user(struct task_struct *tsk)
  70. {
  71. cputime_t delta_utime;
  72. struct thread_info *ti = task_thread_info(tsk);
  73. if (ti->ac_utime) {
  74. delta_utime = cycle_to_cputime(ti->ac_utime);
  75. account_user_time(tsk, delta_utime, delta_utime);
  76. ti->ac_utime = 0;
  77. }
  78. }
  79. /*
  80. * Called from the context switch with interrupts disabled, to charge all
  81. * accumulated times to the current process, and to prepare accounting on
  82. * the next process.
  83. */
  84. void arch_vtime_task_switch(struct task_struct *prev)
  85. {
  86. struct thread_info *pi = task_thread_info(prev);
  87. struct thread_info *ni = task_thread_info(current);
  88. pi->ac_stamp = ni->ac_stamp;
  89. ni->ac_stime = ni->ac_utime = 0;
  90. }
  91. /*
  92. * Account time for a transition between system, hard irq or soft irq state.
  93. * Note that this function is called with interrupts enabled.
  94. */
  95. static cputime_t vtime_delta(struct task_struct *tsk)
  96. {
  97. struct thread_info *ti = task_thread_info(tsk);
  98. cputime_t delta_stime;
  99. __u64 now;
  100. WARN_ON_ONCE(!irqs_disabled());
  101. now = ia64_get_itc();
  102. delta_stime = cycle_to_cputime(ti->ac_stime + (now - ti->ac_stamp));
  103. ti->ac_stime = 0;
  104. ti->ac_stamp = now;
  105. return delta_stime;
  106. }
  107. void vtime_account_system(struct task_struct *tsk)
  108. {
  109. cputime_t delta = vtime_delta(tsk);
  110. account_system_time(tsk, 0, delta, delta);
  111. }
  112. void vtime_account_idle(struct task_struct *tsk)
  113. {
  114. account_idle_time(vtime_delta(tsk));
  115. }
  116. #endif /* CONFIG_VIRT_CPU_ACCOUNTING */
  117. static irqreturn_t
  118. timer_interrupt (int irq, void *dev_id)
  119. {
  120. unsigned long new_itm;
  121. if (cpu_is_offline(smp_processor_id())) {
  122. return IRQ_HANDLED;
  123. }
  124. platform_timer_interrupt(irq, dev_id);
  125. new_itm = local_cpu_data->itm_next;
  126. if (!time_after(ia64_get_itc(), new_itm))
  127. printk(KERN_ERR "Oops: timer tick before it's due (itc=%lx,itm=%lx)\n",
  128. ia64_get_itc(), new_itm);
  129. profile_tick(CPU_PROFILING);
  130. if (paravirt_do_steal_accounting(&new_itm))
  131. goto skip_process_time_accounting;
  132. while (1) {
  133. update_process_times(user_mode(get_irq_regs()));
  134. new_itm += local_cpu_data->itm_delta;
  135. if (smp_processor_id() == time_keeper_id)
  136. xtime_update(1);
  137. local_cpu_data->itm_next = new_itm;
  138. if (time_after(new_itm, ia64_get_itc()))
  139. break;
  140. /*
  141. * Allow IPIs to interrupt the timer loop.
  142. */
  143. local_irq_enable();
  144. local_irq_disable();
  145. }
  146. skip_process_time_accounting:
  147. do {
  148. /*
  149. * If we're too close to the next clock tick for
  150. * comfort, we increase the safety margin by
  151. * intentionally dropping the next tick(s). We do NOT
  152. * update itm.next because that would force us to call
  153. * xtime_update() which in turn would let our clock run
  154. * too fast (with the potentially devastating effect
  155. * of losing monotony of time).
  156. */
  157. while (!time_after(new_itm, ia64_get_itc() + local_cpu_data->itm_delta/2))
  158. new_itm += local_cpu_data->itm_delta;
  159. ia64_set_itm(new_itm);
  160. /* double check, in case we got hit by a (slow) PMI: */
  161. } while (time_after_eq(ia64_get_itc(), new_itm));
  162. return IRQ_HANDLED;
  163. }
  164. /*
  165. * Encapsulate access to the itm structure for SMP.
  166. */
  167. void
  168. ia64_cpu_local_tick (void)
  169. {
  170. int cpu = smp_processor_id();
  171. unsigned long shift = 0, delta;
  172. /* arrange for the cycle counter to generate a timer interrupt: */
  173. ia64_set_itv(IA64_TIMER_VECTOR);
  174. delta = local_cpu_data->itm_delta;
  175. /*
  176. * Stagger the timer tick for each CPU so they don't occur all at (almost) the
  177. * same time:
  178. */
  179. if (cpu) {
  180. unsigned long hi = 1UL << ia64_fls(cpu);
  181. shift = (2*(cpu - hi) + 1) * delta/hi/2;
  182. }
  183. local_cpu_data->itm_next = ia64_get_itc() + delta + shift;
  184. ia64_set_itm(local_cpu_data->itm_next);
  185. }
  186. static int nojitter;
  187. static int __init nojitter_setup(char *str)
  188. {
  189. nojitter = 1;
  190. printk("Jitter checking for ITC timers disabled\n");
  191. return 1;
  192. }
  193. __setup("nojitter", nojitter_setup);
  194. void ia64_init_itm(void)
  195. {
  196. unsigned long platform_base_freq, itc_freq;
  197. struct pal_freq_ratio itc_ratio, proc_ratio;
  198. long status, platform_base_drift, itc_drift;
  199. /*
  200. * According to SAL v2.6, we need to use a SAL call to determine the platform base
  201. * frequency and then a PAL call to determine the frequency ratio between the ITC
  202. * and the base frequency.
  203. */
  204. status = ia64_sal_freq_base(SAL_FREQ_BASE_PLATFORM,
  205. &platform_base_freq, &platform_base_drift);
  206. if (status != 0) {
  207. printk(KERN_ERR "SAL_FREQ_BASE_PLATFORM failed: %s\n", ia64_sal_strerror(status));
  208. } else {
  209. status = ia64_pal_freq_ratios(&proc_ratio, NULL, &itc_ratio);
  210. if (status != 0)
  211. printk(KERN_ERR "PAL_FREQ_RATIOS failed with status=%ld\n", status);
  212. }
  213. if (status != 0) {
  214. /* invent "random" values */
  215. printk(KERN_ERR
  216. "SAL/PAL failed to obtain frequency info---inventing reasonable values\n");
  217. platform_base_freq = 100000000;
  218. platform_base_drift = -1; /* no drift info */
  219. itc_ratio.num = 3;
  220. itc_ratio.den = 1;
  221. }
  222. if (platform_base_freq < 40000000) {
  223. printk(KERN_ERR "Platform base frequency %lu bogus---resetting to 75MHz!\n",
  224. platform_base_freq);
  225. platform_base_freq = 75000000;
  226. platform_base_drift = -1;
  227. }
  228. if (!proc_ratio.den)
  229. proc_ratio.den = 1; /* avoid division by zero */
  230. if (!itc_ratio.den)
  231. itc_ratio.den = 1; /* avoid division by zero */
  232. itc_freq = (platform_base_freq*itc_ratio.num)/itc_ratio.den;
  233. local_cpu_data->itm_delta = (itc_freq + HZ/2) / HZ;
  234. printk(KERN_DEBUG "CPU %d: base freq=%lu.%03luMHz, ITC ratio=%u/%u, "
  235. "ITC freq=%lu.%03luMHz", smp_processor_id(),
  236. platform_base_freq / 1000000, (platform_base_freq / 1000) % 1000,
  237. itc_ratio.num, itc_ratio.den, itc_freq / 1000000, (itc_freq / 1000) % 1000);
  238. if (platform_base_drift != -1) {
  239. itc_drift = platform_base_drift*itc_ratio.num/itc_ratio.den;
  240. printk("+/-%ldppm\n", itc_drift);
  241. } else {
  242. itc_drift = -1;
  243. printk("\n");
  244. }
  245. local_cpu_data->proc_freq = (platform_base_freq*proc_ratio.num)/proc_ratio.den;
  246. local_cpu_data->itc_freq = itc_freq;
  247. local_cpu_data->cyc_per_usec = (itc_freq + USEC_PER_SEC/2) / USEC_PER_SEC;
  248. local_cpu_data->nsec_per_cyc = ((NSEC_PER_SEC<<IA64_NSEC_PER_CYC_SHIFT)
  249. + itc_freq/2)/itc_freq;
  250. if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
  251. #ifdef CONFIG_SMP
  252. /* On IA64 in an SMP configuration ITCs are never accurately synchronized.
  253. * Jitter compensation requires a cmpxchg which may limit
  254. * the scalability of the syscalls for retrieving time.
  255. * The ITC synchronization is usually successful to within a few
  256. * ITC ticks but this is not a sure thing. If you need to improve
  257. * timer performance in SMP situations then boot the kernel with the
  258. * "nojitter" option. However, doing so may result in time fluctuating (maybe
  259. * even going backward) if the ITC offsets between the individual CPUs
  260. * are too large.
  261. */
  262. if (!nojitter)
  263. itc_jitter_data.itc_jitter = 1;
  264. #endif
  265. } else
  266. /*
  267. * ITC is drifty and we have not synchronized the ITCs in smpboot.c.
  268. * ITC values may fluctuate significantly between processors.
  269. * Clock should not be used for hrtimers. Mark itc as only
  270. * useful for boot and testing.
  271. *
  272. * Note that jitter compensation is off! There is no point of
  273. * synchronizing ITCs since they may be large differentials
  274. * that change over time.
  275. *
  276. * The only way to fix this would be to repeatedly sync the
  277. * ITCs. Until that time we have to avoid ITC.
  278. */
  279. clocksource_itc.rating = 50;
  280. paravirt_init_missing_ticks_accounting(smp_processor_id());
  281. /* avoid softlock up message when cpu is unplug and plugged again. */
  282. touch_softlockup_watchdog();
  283. /* Setup the CPU local timer tick */
  284. ia64_cpu_local_tick();
  285. if (!itc_clocksource) {
  286. clocksource_register_hz(&clocksource_itc,
  287. local_cpu_data->itc_freq);
  288. itc_clocksource = &clocksource_itc;
  289. }
  290. }
  291. static cycle_t itc_get_cycles(struct clocksource *cs)
  292. {
  293. unsigned long lcycle, now, ret;
  294. if (!itc_jitter_data.itc_jitter)
  295. return get_cycles();
  296. lcycle = itc_jitter_data.itc_lastcycle;
  297. now = get_cycles();
  298. if (lcycle && time_after(lcycle, now))
  299. return lcycle;
  300. /*
  301. * Keep track of the last timer value returned.
  302. * In an SMP environment, you could lose out in contention of
  303. * cmpxchg. If so, your cmpxchg returns new value which the
  304. * winner of contention updated to. Use the new value instead.
  305. */
  306. ret = cmpxchg(&itc_jitter_data.itc_lastcycle, lcycle, now);
  307. if (unlikely(ret != lcycle))
  308. return ret;
  309. return now;
  310. }
  311. static struct irqaction timer_irqaction = {
  312. .handler = timer_interrupt,
  313. .flags = IRQF_DISABLED | IRQF_IRQPOLL,
  314. .name = "timer"
  315. };
  316. static struct platform_device rtc_efi_dev = {
  317. .name = "rtc-efi",
  318. .id = -1,
  319. };
  320. static int __init rtc_init(void)
  321. {
  322. if (platform_device_register(&rtc_efi_dev) < 0)
  323. printk(KERN_ERR "unable to register rtc device...\n");
  324. /* not necessarily an error */
  325. return 0;
  326. }
  327. module_init(rtc_init);
  328. void read_persistent_clock(struct timespec *ts)
  329. {
  330. efi_gettimeofday(ts);
  331. }
  332. void __init
  333. time_init (void)
  334. {
  335. register_percpu_irq(IA64_TIMER_VECTOR, &timer_irqaction);
  336. ia64_init_itm();
  337. }
  338. /*
  339. * Generic udelay assumes that if preemption is allowed and the thread
  340. * migrates to another CPU, that the ITC values are synchronized across
  341. * all CPUs.
  342. */
  343. static void
  344. ia64_itc_udelay (unsigned long usecs)
  345. {
  346. unsigned long start = ia64_get_itc();
  347. unsigned long end = start + usecs*local_cpu_data->cyc_per_usec;
  348. while (time_before(ia64_get_itc(), end))
  349. cpu_relax();
  350. }
  351. void (*ia64_udelay)(unsigned long usecs) = &ia64_itc_udelay;
  352. void
  353. udelay (unsigned long usecs)
  354. {
  355. (*ia64_udelay)(usecs);
  356. }
  357. EXPORT_SYMBOL(udelay);
  358. /* IA64 doesn't cache the timezone */
  359. void update_vsyscall_tz(void)
  360. {
  361. }
  362. void update_vsyscall_old(struct timespec *wall, struct timespec *wtm,
  363. struct clocksource *c, u32 mult)
  364. {
  365. write_seqcount_begin(&fsyscall_gtod_data.seq);
  366. /* copy fsyscall clock data */
  367. fsyscall_gtod_data.clk_mask = c->mask;
  368. fsyscall_gtod_data.clk_mult = mult;
  369. fsyscall_gtod_data.clk_shift = c->shift;
  370. fsyscall_gtod_data.clk_fsys_mmio = c->archdata.fsys_mmio;
  371. fsyscall_gtod_data.clk_cycle_last = c->cycle_last;
  372. /* copy kernel time structures */
  373. fsyscall_gtod_data.wall_time.tv_sec = wall->tv_sec;
  374. fsyscall_gtod_data.wall_time.tv_nsec = wall->tv_nsec;
  375. fsyscall_gtod_data.monotonic_time.tv_sec = wtm->tv_sec
  376. + wall->tv_sec;
  377. fsyscall_gtod_data.monotonic_time.tv_nsec = wtm->tv_nsec
  378. + wall->tv_nsec;
  379. /* normalize */
  380. while (fsyscall_gtod_data.monotonic_time.tv_nsec >= NSEC_PER_SEC) {
  381. fsyscall_gtod_data.monotonic_time.tv_nsec -= NSEC_PER_SEC;
  382. fsyscall_gtod_data.monotonic_time.tv_sec++;
  383. }
  384. write_seqcount_end(&fsyscall_gtod_data.seq);
  385. }