time.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * linux/arch/parisc/kernel/time.c
  3. *
  4. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  5. * Modifications for ARM (C) 1994, 1995, 1996,1997 Russell King
  6. * Copyright (C) 1999 SuSE GmbH, (Philipp Rumpf, prumpf@tux.org)
  7. *
  8. * 1994-07-02 Alan Modra
  9. * fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime
  10. * 1998-12-20 Updated NTP code according to technical memorandum Jan '96
  11. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  12. */
  13. #include <linux/errno.h>
  14. #include <linux/module.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel.h>
  17. #include <linux/param.h>
  18. #include <linux/string.h>
  19. #include <linux/mm.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/time.h>
  22. #include <linux/init.h>
  23. #include <linux/smp.h>
  24. #include <linux/profile.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/io.h>
  27. #include <asm/irq.h>
  28. #include <asm/param.h>
  29. #include <asm/pdc.h>
  30. #include <asm/led.h>
  31. #include <linux/timex.h>
  32. static unsigned long clocktick __read_mostly; /* timer cycles per tick */
  33. #ifdef CONFIG_SMP
  34. extern void smp_do_timer(struct pt_regs *regs);
  35. #endif
  36. irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  37. {
  38. unsigned long now;
  39. unsigned long next_tick;
  40. unsigned long cycles_elapsed;
  41. unsigned long cycles_remainder;
  42. unsigned int cpu = smp_processor_id();
  43. /* gcc can optimize for "read-only" case with a local clocktick */
  44. unsigned long cpt = clocktick;
  45. profile_tick(CPU_PROFILING, regs);
  46. /* Initialize next_tick to the expected tick time. */
  47. next_tick = cpu_data[cpu].it_value;
  48. /* Get current interval timer.
  49. * CR16 reads as 64 bits in CPU wide mode.
  50. * CR16 reads as 32 bits in CPU narrow mode.
  51. */
  52. now = mfctl(16);
  53. cycles_elapsed = now - next_tick;
  54. if ((cycles_elapsed >> 5) < cpt) {
  55. /* use "cheap" math (add/subtract) instead
  56. * of the more expensive div/mul method
  57. */
  58. cycles_remainder = cycles_elapsed;
  59. while (cycles_remainder > cpt) {
  60. cycles_remainder -= cpt;
  61. }
  62. } else {
  63. cycles_remainder = cycles_elapsed % cpt;
  64. }
  65. /* Can we differentiate between "early CR16" (aka Scenario 1) and
  66. * "long delay" (aka Scenario 3)? I don't think so.
  67. *
  68. * We expected timer_interrupt to be delivered at least a few hundred
  69. * cycles after the IT fires. But it's arbitrary how much time passes
  70. * before we call it "late". I've picked one second.
  71. */
  72. /* aproximate HZ with shifts. Intended math is "(elapsed/clocktick) > HZ" */
  73. #if HZ == 1000
  74. if (cycles_elapsed > (cpt << 10) )
  75. #elif HZ == 250
  76. if (cycles_elapsed > (cpt << 8) )
  77. #elif HZ == 100
  78. if (cycles_elapsed > (cpt << 7) )
  79. #else
  80. #warn WTF is HZ set to anyway?
  81. if (cycles_elapsed > (HZ * cpt) )
  82. #endif
  83. {
  84. /* Scenario 3: very long delay? bad in any case */
  85. printk (KERN_CRIT "timer_interrupt(CPU %d): delayed!"
  86. " cycles %lX rem %lX "
  87. " next/now %lX/%lX\n",
  88. cpu,
  89. cycles_elapsed, cycles_remainder,
  90. next_tick, now );
  91. }
  92. /* convert from "division remainder" to "remainder of clock tick" */
  93. cycles_remainder = cpt - cycles_remainder;
  94. /* Determine when (in CR16 cycles) next IT interrupt will fire.
  95. * We want IT to fire modulo clocktick even if we miss/skip some.
  96. * But those interrupts don't in fact get delivered that regularly.
  97. */
  98. next_tick = now + cycles_remainder;
  99. cpu_data[cpu].it_value = next_tick;
  100. /* Skip one clocktick on purpose if we are likely to miss next_tick.
  101. * We want to avoid the new next_tick being less than CR16.
  102. * If that happened, itimer wouldn't fire until CR16 wrapped.
  103. * We'll catch the tick we missed on the tick after that.
  104. */
  105. if (!(cycles_remainder >> 13))
  106. next_tick += cpt;
  107. /* Program the IT when to deliver the next interrupt. */
  108. /* Only bottom 32-bits of next_tick are written to cr16. */
  109. mtctl(next_tick, 16);
  110. /* Done mucking with unreliable delivery of interrupts.
  111. * Go do system house keeping.
  112. */
  113. #ifdef CONFIG_SMP
  114. smp_do_timer(regs);
  115. #else
  116. update_process_times(user_mode(regs));
  117. #endif
  118. if (cpu == 0) {
  119. write_seqlock(&xtime_lock);
  120. do_timer(regs);
  121. write_sequnlock(&xtime_lock);
  122. }
  123. /* check soft power switch status */
  124. if (cpu == 0 && !atomic_read(&power_tasklet.count))
  125. tasklet_schedule(&power_tasklet);
  126. return IRQ_HANDLED;
  127. }
  128. unsigned long profile_pc(struct pt_regs *regs)
  129. {
  130. unsigned long pc = instruction_pointer(regs);
  131. if (regs->gr[0] & PSW_N)
  132. pc -= 4;
  133. #ifdef CONFIG_SMP
  134. if (in_lock_functions(pc))
  135. pc = regs->gr[2];
  136. #endif
  137. return pc;
  138. }
  139. EXPORT_SYMBOL(profile_pc);
  140. /*
  141. * Return the number of micro-seconds that elapsed since the last
  142. * update to wall time (aka xtime). The xtime_lock
  143. * must be at least read-locked when calling this routine.
  144. */
  145. static inline unsigned long gettimeoffset (void)
  146. {
  147. #ifndef CONFIG_SMP
  148. /*
  149. * FIXME: This won't work on smp because jiffies are updated by cpu 0.
  150. * Once parisc-linux learns the cr16 difference between processors,
  151. * this could be made to work.
  152. */
  153. unsigned long now;
  154. unsigned long prev_tick;
  155. unsigned long next_tick;
  156. unsigned long elapsed_cycles;
  157. unsigned long usec;
  158. unsigned long cpuid = smp_processor_id();
  159. unsigned long cpt = clocktick;
  160. next_tick = cpu_data[cpuid].it_value;
  161. now = mfctl(16); /* Read the hardware interval timer. */
  162. prev_tick = next_tick - cpt;
  163. /* Assume Scenario 1: "now" is later than prev_tick. */
  164. elapsed_cycles = now - prev_tick;
  165. /* aproximate HZ with shifts. Intended math is "(elapsed/clocktick) > HZ" */
  166. #if HZ == 1000
  167. if (elapsed_cycles > (cpt << 10) )
  168. #elif HZ == 250
  169. if (elapsed_cycles > (cpt << 8) )
  170. #elif HZ == 100
  171. if (elapsed_cycles > (cpt << 7) )
  172. #else
  173. #warn WTF is HZ set to anyway?
  174. if (elapsed_cycles > (HZ * cpt) )
  175. #endif
  176. {
  177. /* Scenario 3: clock ticks are missing. */
  178. printk (KERN_CRIT "gettimeoffset(CPU %ld): missing %ld ticks!"
  179. " cycles %lX prev/now/next %lX/%lX/%lX clock %lX\n",
  180. cpuid, elapsed_cycles / cpt,
  181. elapsed_cycles, prev_tick, now, next_tick, cpt);
  182. }
  183. /* FIXME: Can we improve the precision? Not with PAGE0. */
  184. usec = (elapsed_cycles * 10000) / PAGE0->mem_10msec;
  185. /* add in "lost" jiffies */
  186. usec += cpt * (jiffies - wall_jiffies);
  187. return usec;
  188. #else
  189. return 0;
  190. #endif
  191. }
  192. void
  193. do_gettimeofday (struct timeval *tv)
  194. {
  195. unsigned long flags, seq, usec, sec;
  196. /* Hold xtime_lock and adjust timeval. */
  197. do {
  198. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  199. usec = gettimeoffset();
  200. sec = xtime.tv_sec;
  201. usec += (xtime.tv_nsec / 1000);
  202. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  203. /* Move adjusted usec's into sec's. */
  204. while (usec >= USEC_PER_SEC) {
  205. usec -= USEC_PER_SEC;
  206. ++sec;
  207. }
  208. /* Return adjusted result. */
  209. tv->tv_sec = sec;
  210. tv->tv_usec = usec;
  211. }
  212. EXPORT_SYMBOL(do_gettimeofday);
  213. int
  214. do_settimeofday (struct timespec *tv)
  215. {
  216. time_t wtm_sec, sec = tv->tv_sec;
  217. long wtm_nsec, nsec = tv->tv_nsec;
  218. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  219. return -EINVAL;
  220. write_seqlock_irq(&xtime_lock);
  221. {
  222. /*
  223. * This is revolting. We need to set "xtime"
  224. * correctly. However, the value in this location is
  225. * the value at the most recent update of wall time.
  226. * Discover what correction gettimeofday would have
  227. * done, and then undo it!
  228. */
  229. nsec -= gettimeoffset() * 1000;
  230. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  231. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  232. set_normalized_timespec(&xtime, sec, nsec);
  233. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  234. ntp_clear();
  235. }
  236. write_sequnlock_irq(&xtime_lock);
  237. clock_was_set();
  238. return 0;
  239. }
  240. EXPORT_SYMBOL(do_settimeofday);
  241. /*
  242. * XXX: We can do better than this.
  243. * Returns nanoseconds
  244. */
  245. unsigned long long sched_clock(void)
  246. {
  247. return (unsigned long long)jiffies * (1000000000 / HZ);
  248. }
  249. void __init start_cpu_itimer(void)
  250. {
  251. unsigned int cpu = smp_processor_id();
  252. unsigned long next_tick = mfctl(16) + clocktick;
  253. mtctl(next_tick, 16); /* kick off Interval Timer (CR16) */
  254. cpu_data[cpu].it_value = next_tick;
  255. }
  256. void __init time_init(void)
  257. {
  258. static struct pdc_tod tod_data;
  259. clocktick = (100 * PAGE0->mem_10msec) / HZ;
  260. start_cpu_itimer(); /* get CPU 0 started */
  261. if(pdc_tod_read(&tod_data) == 0) {
  262. write_seqlock_irq(&xtime_lock);
  263. xtime.tv_sec = tod_data.tod_sec;
  264. xtime.tv_nsec = tod_data.tod_usec * 1000;
  265. set_normalized_timespec(&wall_to_monotonic,
  266. -xtime.tv_sec, -xtime.tv_nsec);
  267. write_sequnlock_irq(&xtime_lock);
  268. } else {
  269. printk(KERN_ERR "Error reading tod clock\n");
  270. xtime.tv_sec = 0;
  271. xtime.tv_nsec = 0;
  272. }
  273. }