time.c 11 KB

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