timer.h 1.7 KB

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