time.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/profile.h>
  21. #include <linux/timex.h>
  22. #include <asm/machvec.h>
  23. #include <asm/delay.h>
  24. #include <asm/hw_irq.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/sal.h>
  27. #include <asm/sections.h>
  28. #include <asm/system.h>
  29. volatile int time_keeper_id = 0; /* smp_processor_id() of time-keeper */
  30. #ifdef CONFIG_IA64_DEBUG_IRQ
  31. unsigned long last_cli_ip;
  32. EXPORT_SYMBOL(last_cli_ip);
  33. #endif
  34. static struct time_interpolator itc_interpolator = {
  35. .shift = 16,
  36. .mask = 0xffffffffffffffffLL,
  37. .source = TIME_SOURCE_CPU
  38. };
  39. static irqreturn_t
  40. timer_interrupt (int irq, void *dev_id)
  41. {
  42. unsigned long new_itm;
  43. if (unlikely(cpu_is_offline(smp_processor_id()))) {
  44. return IRQ_HANDLED;
  45. }
  46. platform_timer_interrupt(irq, dev_id);
  47. new_itm = local_cpu_data->itm_next;
  48. if (!time_after(ia64_get_itc(), new_itm))
  49. printk(KERN_ERR "Oops: timer tick before it's due (itc=%lx,itm=%lx)\n",
  50. ia64_get_itc(), new_itm);
  51. profile_tick(CPU_PROFILING);
  52. while (1) {
  53. update_process_times(user_mode(get_irq_regs()));
  54. new_itm += local_cpu_data->itm_delta;
  55. if (smp_processor_id() == time_keeper_id) {
  56. /*
  57. * Here we are in the timer irq handler. We have irqs locally
  58. * disabled, but we don't know if the timer_bh is running on
  59. * another CPU. We need to avoid to SMP race by acquiring the
  60. * xtime_lock.
  61. */
  62. write_seqlock(&xtime_lock);
  63. do_timer(1);
  64. local_cpu_data->itm_next = new_itm;
  65. write_sequnlock(&xtime_lock);
  66. } else
  67. local_cpu_data->itm_next = new_itm;
  68. if (time_after(new_itm, ia64_get_itc()))
  69. break;
  70. /*
  71. * Allow IPIs to interrupt the timer loop.
  72. */
  73. local_irq_enable();
  74. local_irq_disable();
  75. }
  76. do {
  77. /*
  78. * If we're too close to the next clock tick for
  79. * comfort, we increase the safety margin by
  80. * intentionally dropping the next tick(s). We do NOT
  81. * update itm.next because that would force us to call
  82. * do_timer() which in turn would let our clock run
  83. * too fast (with the potentially devastating effect
  84. * of losing monotony of time).
  85. */
  86. while (!time_after(new_itm, ia64_get_itc() + local_cpu_data->itm_delta/2))
  87. new_itm += local_cpu_data->itm_delta;
  88. ia64_set_itm(new_itm);
  89. /* double check, in case we got hit by a (slow) PMI: */
  90. } while (time_after_eq(ia64_get_itc(), new_itm));
  91. return IRQ_HANDLED;
  92. }
  93. /*
  94. * Encapsulate access to the itm structure for SMP.
  95. */
  96. void
  97. ia64_cpu_local_tick (void)
  98. {
  99. int cpu = smp_processor_id();
  100. unsigned long shift = 0, delta;
  101. /* arrange for the cycle counter to generate a timer interrupt: */
  102. ia64_set_itv(IA64_TIMER_VECTOR);
  103. delta = local_cpu_data->itm_delta;
  104. /*
  105. * Stagger the timer tick for each CPU so they don't occur all at (almost) the
  106. * same time:
  107. */
  108. if (cpu) {
  109. unsigned long hi = 1UL << ia64_fls(cpu);
  110. shift = (2*(cpu - hi) + 1) * delta/hi/2;
  111. }
  112. local_cpu_data->itm_next = ia64_get_itc() + delta + shift;
  113. ia64_set_itm(local_cpu_data->itm_next);
  114. }
  115. static int nojitter;
  116. static int __init nojitter_setup(char *str)
  117. {
  118. nojitter = 1;
  119. printk("Jitter checking for ITC timers disabled\n");
  120. return 1;
  121. }
  122. __setup("nojitter", nojitter_setup);
  123. void __devinit
  124. ia64_init_itm (void)
  125. {
  126. unsigned long platform_base_freq, itc_freq;
  127. struct pal_freq_ratio itc_ratio, proc_ratio;
  128. long status, platform_base_drift, itc_drift;
  129. /*
  130. * According to SAL v2.6, we need to use a SAL call to determine the platform base
  131. * frequency and then a PAL call to determine the frequency ratio between the ITC
  132. * and the base frequency.
  133. */
  134. status = ia64_sal_freq_base(SAL_FREQ_BASE_PLATFORM,
  135. &platform_base_freq, &platform_base_drift);
  136. if (status != 0) {
  137. printk(KERN_ERR "SAL_FREQ_BASE_PLATFORM failed: %s\n", ia64_sal_strerror(status));
  138. } else {
  139. status = ia64_pal_freq_ratios(&proc_ratio, NULL, &itc_ratio);
  140. if (status != 0)
  141. printk(KERN_ERR "PAL_FREQ_RATIOS failed with status=%ld\n", status);
  142. }
  143. if (status != 0) {
  144. /* invent "random" values */
  145. printk(KERN_ERR
  146. "SAL/PAL failed to obtain frequency info---inventing reasonable values\n");
  147. platform_base_freq = 100000000;
  148. platform_base_drift = -1; /* no drift info */
  149. itc_ratio.num = 3;
  150. itc_ratio.den = 1;
  151. }
  152. if (platform_base_freq < 40000000) {
  153. printk(KERN_ERR "Platform base frequency %lu bogus---resetting to 75MHz!\n",
  154. platform_base_freq);
  155. platform_base_freq = 75000000;
  156. platform_base_drift = -1;
  157. }
  158. if (!proc_ratio.den)
  159. proc_ratio.den = 1; /* avoid division by zero */
  160. if (!itc_ratio.den)
  161. itc_ratio.den = 1; /* avoid division by zero */
  162. itc_freq = (platform_base_freq*itc_ratio.num)/itc_ratio.den;
  163. local_cpu_data->itm_delta = (itc_freq + HZ/2) / HZ;
  164. printk(KERN_DEBUG "CPU %d: base freq=%lu.%03luMHz, ITC ratio=%u/%u, "
  165. "ITC freq=%lu.%03luMHz", smp_processor_id(),
  166. platform_base_freq / 1000000, (platform_base_freq / 1000) % 1000,
  167. itc_ratio.num, itc_ratio.den, itc_freq / 1000000, (itc_freq / 1000) % 1000);
  168. if (platform_base_drift != -1) {
  169. itc_drift = platform_base_drift*itc_ratio.num/itc_ratio.den;
  170. printk("+/-%ldppm\n", itc_drift);
  171. } else {
  172. itc_drift = -1;
  173. printk("\n");
  174. }
  175. local_cpu_data->proc_freq = (platform_base_freq*proc_ratio.num)/proc_ratio.den;
  176. local_cpu_data->itc_freq = itc_freq;
  177. local_cpu_data->cyc_per_usec = (itc_freq + USEC_PER_SEC/2) / USEC_PER_SEC;
  178. local_cpu_data->nsec_per_cyc = ((NSEC_PER_SEC<<IA64_NSEC_PER_CYC_SHIFT)
  179. + itc_freq/2)/itc_freq;
  180. if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
  181. itc_interpolator.frequency = local_cpu_data->itc_freq;
  182. itc_interpolator.drift = itc_drift;
  183. #ifdef CONFIG_SMP
  184. /* On IA64 in an SMP configuration ITCs are never accurately synchronized.
  185. * Jitter compensation requires a cmpxchg which may limit
  186. * the scalability of the syscalls for retrieving time.
  187. * The ITC synchronization is usually successful to within a few
  188. * ITC ticks but this is not a sure thing. If you need to improve
  189. * timer performance in SMP situations then boot the kernel with the
  190. * "nojitter" option. However, doing so may result in time fluctuating (maybe
  191. * even going backward) if the ITC offsets between the individual CPUs
  192. * are too large.
  193. */
  194. if (!nojitter) itc_interpolator.jitter = 1;
  195. #endif
  196. register_time_interpolator(&itc_interpolator);
  197. }
  198. /* Setup the CPU local timer tick */
  199. ia64_cpu_local_tick();
  200. }
  201. static struct irqaction timer_irqaction = {
  202. .handler = timer_interrupt,
  203. .flags = IRQF_DISABLED,
  204. .name = "timer"
  205. };
  206. void __devinit ia64_disable_timer(void)
  207. {
  208. ia64_set_itv(1 << 16);
  209. }
  210. void __init
  211. time_init (void)
  212. {
  213. register_percpu_irq(IA64_TIMER_VECTOR, &timer_irqaction);
  214. efi_gettimeofday(&xtime);
  215. ia64_init_itm();
  216. /*
  217. * Initialize wall_to_monotonic such that adding it to xtime will yield zero, the
  218. * tv_nsec field must be normalized (i.e., 0 <= nsec < NSEC_PER_SEC).
  219. */
  220. set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);
  221. }
  222. /*
  223. * Generic udelay assumes that if preemption is allowed and the thread
  224. * migrates to another CPU, that the ITC values are synchronized across
  225. * all CPUs.
  226. */
  227. static void
  228. ia64_itc_udelay (unsigned long usecs)
  229. {
  230. unsigned long start = ia64_get_itc();
  231. unsigned long end = start + usecs*local_cpu_data->cyc_per_usec;
  232. while (time_before(ia64_get_itc(), end))
  233. cpu_relax();
  234. }
  235. void (*ia64_udelay)(unsigned long usecs) = &ia64_itc_udelay;
  236. void
  237. udelay (unsigned long usecs)
  238. {
  239. (*ia64_udelay)(usecs);
  240. }
  241. EXPORT_SYMBOL(udelay);
  242. static unsigned long long ia64_itc_printk_clock(void)
  243. {
  244. if (ia64_get_kr(IA64_KR_PER_CPU_DATA))
  245. return sched_clock();
  246. return 0;
  247. }
  248. static unsigned long long ia64_default_printk_clock(void)
  249. {
  250. return (unsigned long long)(jiffies_64 - INITIAL_JIFFIES) *
  251. (1000000000/HZ);
  252. }
  253. unsigned long long (*ia64_printk_clock)(void) = &ia64_default_printk_clock;
  254. unsigned long long printk_clock(void)
  255. {
  256. return ia64_printk_clock();
  257. }
  258. void __init
  259. ia64_setup_printk_clock(void)
  260. {
  261. if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT))
  262. ia64_printk_clock = ia64_itc_printk_clock;
  263. }