timer.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  42. static inline unsigned long long __cycles_2_ns(unsigned long long cyc)
  43. {
  44. return cyc * per_cpu(cyc2ns, smp_processor_id()) >> CYC2NS_SCALE_FACTOR;
  45. }
  46. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  47. {
  48. unsigned long long ns;
  49. unsigned long flags;
  50. local_irq_save(flags);
  51. ns = __cycles_2_ns(cyc);
  52. local_irq_restore(flags);
  53. return ns;
  54. }
  55. #endif /* _ASM_X86_TIMER_H */