timer.h 1.6 KB

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