time.c 11 KB

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