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