timer.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef _ASM_X86_TIMER_H
  2. #define _ASM_X86_TIMER_H
  3. #include <linux/pm.h>
  4. #include <linux/percpu.h>
  5. #include <linux/interrupt.h>
  6. #define TICK_SIZE (tick_nsec / 1000)
  7. unsigned long long native_sched_clock(void);
  8. extern int recalibrate_cpu_khz(void);
  9. extern int no_timer_check;
  10. /* Accelerators for sched_clock()
  11. * convert from cycles(64bits) => nanoseconds (64bits)
  12. * basic equation:
  13. * ns = cycles / (freq / ns_per_sec)
  14. * ns = cycles * (ns_per_sec / freq)
  15. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  16. * ns = cycles * (10^6 / cpu_khz)
  17. *
  18. * Then we use scaling math (suggested by george@mvista.com) to get:
  19. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  20. * ns = cycles * cyc2ns_scale / SC
  21. *
  22. * And since SC is a constant power of two, we can convert the div
  23. * into a shift.
  24. *
  25. * We can use khz divisor instead of mhz to keep a better precision, since
  26. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  27. * (mathieu.desnoyers@polymtl.ca)
  28. *
  29. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  30. *
  31. * In:
  32. *
  33. * ns = cycles * cyc2ns_scale / SC
  34. *
  35. * Although we may still have enough bits to store the value of ns,
  36. * in some cases, we may not have enough bits to store cycles * cyc2ns_scale,
  37. * leading to an incorrect result.
  38. *
  39. * To avoid this, we can decompose 'cycles' into quotient and remainder
  40. * of division by SC. Then,
  41. *
  42. * ns = (quot * SC + rem) * cyc2ns_scale / SC
  43. * = quot * cyc2ns_scale + (rem * cyc2ns_scale) / SC
  44. *
  45. * - sqazi@google.com
  46. */
  47. DECLARE_PER_CPU(unsigned long, cyc2ns);
  48. DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);
  49. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  50. static inline unsigned long long __cycles_2_ns(unsigned long long cyc)
  51. {
  52. int cpu = smp_processor_id();
  53. unsigned long long ns = per_cpu(cyc2ns_offset, cpu);
  54. ns += mult_frac(cyc, per_cpu(cyc2ns, cpu),
  55. (1UL << CYC2NS_SCALE_FACTOR));
  56. return ns;
  57. }
  58. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  59. {
  60. unsigned long long ns;
  61. unsigned long flags;
  62. local_irq_save(flags);
  63. ns = __cycles_2_ns(cyc);
  64. local_irq_restore(flags);
  65. return ns;
  66. }
  67. #endif /* _ASM_X86_TIMER_H */