timer.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef _ASM_X86_TIMER_H
  2. #define _ASM_X86_TIMER_H
  3. #include <linux/init.h>
  4. #include <linux/pm.h>
  5. #include <linux/percpu.h>
  6. #include <linux/interrupt.h>
  7. #define TICK_SIZE (tick_nsec / 1000)
  8. unsigned long long native_sched_clock(void);
  9. unsigned long native_calibrate_tsc(void);
  10. #ifdef CONFIG_X86_32
  11. extern int timer_ack;
  12. extern irqreturn_t timer_interrupt(int irq, void *dev_id);
  13. #endif /* CONFIG_X86_32 */
  14. extern int recalibrate_cpu_khz(void);
  15. extern int no_timer_check;
  16. #ifndef CONFIG_PARAVIRT
  17. #define calibrate_tsc() native_calibrate_tsc()
  18. #endif
  19. /* Accelerators for sched_clock()
  20. * convert from cycles(64bits) => nanoseconds (64bits)
  21. * basic equation:
  22. * ns = cycles / (freq / ns_per_sec)
  23. * ns = cycles * (ns_per_sec / freq)
  24. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  25. * ns = cycles * (10^6 / cpu_khz)
  26. *
  27. * Then we use scaling math (suggested by george@mvista.com) to get:
  28. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  29. * ns = cycles * cyc2ns_scale / SC
  30. *
  31. * And since SC is a constant power of two, we can convert the div
  32. * into a shift.
  33. *
  34. * We can use khz divisor instead of mhz to keep a better precision, since
  35. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  36. * (mathieu.desnoyers@polymtl.ca)
  37. *
  38. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  39. */
  40. DECLARE_PER_CPU(unsigned long, cyc2ns);
  41. DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);
  42. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  43. static inline unsigned long long __cycles_2_ns(unsigned long long cyc)
  44. {
  45. int cpu = smp_processor_id();
  46. unsigned long long ns = per_cpu(cyc2ns_offset, cpu);
  47. ns += cyc * per_cpu(cyc2ns, cpu) >> CYC2NS_SCALE_FACTOR;
  48. return ns;
  49. }
  50. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  51. {
  52. unsigned long long ns;
  53. unsigned long flags;
  54. local_irq_save(flags);
  55. ns = __cycles_2_ns(cyc);
  56. local_irq_restore(flags);
  57. return ns;
  58. }
  59. #endif /* _ASM_X86_TIMER_H */