time.c 13 KB

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