time.c 7.8 KB

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