time.c 13 KB

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