time.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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/config.h>
  14. #include <linux/errno.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 <asm/uaccess.h>
  25. #include <asm/io.h>
  26. #include <asm/irq.h>
  27. #include <asm/param.h>
  28. #include <asm/pdc.h>
  29. #include <asm/led.h>
  30. #include <linux/timex.h>
  31. /* xtime and wall_jiffies keep wall-clock time */
  32. extern unsigned long wall_jiffies;
  33. extern rwlock_t xtime_lock;
  34. static long clocktick; /* timer cycles per tick */
  35. static long halftick;
  36. #ifdef CONFIG_SMP
  37. extern void smp_do_timer(struct pt_regs *regs);
  38. #endif
  39. static inline void
  40. parisc_do_profile(unsigned long pc)
  41. {
  42. extern char _stext;
  43. if (!prof_buffer)
  44. return;
  45. pc -= (unsigned long) &_stext;
  46. pc >>= prof_shift;
  47. /*
  48.  * Don't ignore out-of-bounds PC values silently,
  49.  * put them into the last histogram slot, so if
  50.  * present, they will show up as a sharp peak.
  51.  */
  52. if (pc > prof_len - 1)
  53. pc = prof_len - 1;
  54. atomic_inc((atomic_t *)&prof_buffer[pc]);
  55. }
  56. void timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  57. {
  58. long now = mfctl(16);
  59. long next_tick;
  60. int nticks;
  61. int cpu = smp_processor_id();
  62. /* initialize next_tick to time at last clocktick */
  63. next_tick = cpu_data[cpu].it_value;
  64. /* since time passes between the interrupt and the mfctl()
  65.  * above, it is never true that last_tick + clocktick == now.  If we
  66.  * never miss a clocktick, we could set next_tick = last_tick + clocktick
  67.  * but maybe we'll miss ticks, hence the loop.
  68.  *
  69.  * Variables are *signed*.
  70.  */
  71. nticks = 0;
  72. while((next_tick - now) < halftick) {
  73. next_tick += clocktick;
  74. nticks++;
  75. }
  76. mtctl(next_tick, 16);
  77. cpu_data[cpu].it_value = next_tick;
  78. while (nticks--) {
  79. #ifdef CONFIG_SMP
  80. smp_do_timer(regs);
  81. #endif
  82. if (cpu == 0) {
  83. extern int pc_in_user_space;
  84. write_lock(&xtime_lock);
  85. #ifndef CONFIG_SMP
  86. if (!user_mode(regs))
  87. parisc_do_profile(regs->iaoq[0]);
  88. else
  89. parisc_do_profile(&pc_in_user_space);
  90. #endif
  91. do_timer(regs);
  92. write_unlock(&xtime_lock);
  93. }
  94. }
  95.     
  96. #ifdef CONFIG_CHASSIS_LCD_LED
  97. /* Only schedule the led tasklet on cpu 0, and only if it
  98.  * is enabled.
  99.  */
  100. if (cpu == 0 && !atomic_read(&led_tasklet.count))
  101. tasklet_schedule(&led_tasklet);
  102. #endif
  103. /* check soft power switch status */
  104. if (cpu == 0 && !atomic_read(&power_tasklet.count))
  105. tasklet_schedule(&power_tasklet);
  106. }
  107. /*** converted from ia64 ***/
  108. /*
  109.  * Return the number of micro-seconds that elapsed since the last
  110.  * update to wall time (aka xtime aka wall_jiffies).  The xtime_lock
  111.  * must be at least read-locked when calling this routine.
  112.  */
  113. static inline unsigned long
  114. gettimeoffset (void)
  115. {
  116. #ifndef CONFIG_SMP
  117. /*
  118.  * FIXME: This won't work on smp because jiffies are updated by cpu 0.
  119.  *    Once parisc-linux learns the cr16 difference between processors,
  120.  *    this could be made to work.
  121.  */
  122. long last_tick;
  123. long elapsed_cycles;
  124. /* it_value is the intended time of the next tick */
  125. last_tick = cpu_data[smp_processor_id()].it_value;
  126. /* Subtract one tick and account for possible difference between
  127.  * when we expected the tick and when it actually arrived.
  128.  * (aka wall vs real)
  129.  */
  130. last_tick -= clocktick * (jiffies - wall_jiffies + 1);
  131. elapsed_cycles = mfctl(16) - last_tick;
  132. /* the precision of this math could be improved */
  133. return elapsed_cycles / (PAGE0->mem_10msec / 10000);
  134. #else
  135. return 0;
  136. #endif
  137. }
  138. void
  139. do_gettimeofday (struct timeval *tv)
  140. {
  141. unsigned long flags, usec, sec;
  142. read_lock_irqsave(&xtime_lock, flags);
  143. {
  144. usec = gettimeoffset();
  145. sec = xtime.tv_sec;
  146. usec += xtime.tv_usec;
  147. }
  148. read_unlock_irqrestore(&xtime_lock, flags);
  149. while (usec >= 1000000) {
  150. usec -= 1000000;
  151. ++sec;
  152. }
  153. tv->tv_sec = sec;
  154. tv->tv_usec = usec;
  155. }
  156. void
  157. do_settimeofday (struct timeval *tv)
  158. {
  159. write_lock_irq(&xtime_lock);
  160. {
  161. /*
  162.  * This is revolting. We need to set "xtime"
  163.  * correctly. However, the value in this location is
  164.  * the value at the most recent update of wall time.
  165.  * Discover what correction gettimeofday would have
  166.  * done, and then undo it!
  167.  */
  168. tv->tv_usec -= gettimeoffset();
  169. tv->tv_usec -= (jiffies - wall_jiffies) * (1000000 / HZ);
  170. while (tv->tv_usec < 0) {
  171. tv->tv_usec += 1000000;
  172. tv->tv_sec--;
  173. }
  174. xtime = *tv;
  175. time_adjust = 0; /* stop active adjtime() */
  176. time_status |= STA_UNSYNC;
  177. time_maxerror = NTP_PHASE_LIMIT;
  178. time_esterror = NTP_PHASE_LIMIT;
  179. }
  180. write_unlock_irq(&xtime_lock);
  181. }
  182. void __init time_init(void)
  183. {
  184. unsigned long next_tick;
  185. static struct pdc_tod tod_data;
  186. clocktick = (100 * PAGE0->mem_10msec) / HZ;
  187. halftick = clocktick / 2;
  188. /* Setup clock interrupt timing */
  189. next_tick = mfctl(16);
  190. next_tick += clocktick;
  191. cpu_data[smp_processor_id()].it_value = next_tick;
  192. /* kick off Itimer (CR16) */
  193. mtctl(next_tick, 16);
  194. if(pdc_tod_read(&tod_data) == 0) {
  195. write_lock_irq(&xtime_lock);
  196. xtime.tv_sec = tod_data.tod_sec;
  197. xtime.tv_usec = tod_data.tod_usec;
  198. write_unlock_irq(&xtime_lock);
  199. } else {
  200. printk(KERN_ERR "Error reading tod clockn");
  201.         xtime.tv_sec = 0;
  202. xtime.tv_usec = 0;
  203. }
  204. }