timer.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. * In:
  33. *
  34. * ns = cycles * cyc2ns_scale / SC
  35. *
  36. * Although we may still have enough bits to store the value of ns,
  37. * in some cases, we may not have enough bits to store cycles * cyc2ns_scale,
  38. * leading to an incorrect result.
  39. *
  40. * To avoid this, we can decompose 'cycles' into quotient and remainder
  41. * of division by SC. Then,
  42. *
  43. * ns = (quot * SC + rem) * cyc2ns_scale / SC
  44. * = quot * cyc2ns_scale + (rem * cyc2ns_scale) / SC
  45. *
  46. * - sqazi@google.com
  47. */
  48. DECLARE_PER_CPU(unsigned long, cyc2ns);
  49. DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);
  50. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  51. static inline unsigned long long __cycles_2_ns(unsigned long long cyc)
  52. {
  53. int cpu = smp_processor_id();
  54. unsigned long long ns = per_cpu(cyc2ns_offset, cpu);
  55. ns += mult_frac(cyc, per_cpu(cyc2ns, cpu),
  56. (1UL << CYC2NS_SCALE_FACTOR));
  57. return ns;
  58. }
  59. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  60. {
  61. unsigned long long ns;
  62. unsigned long flags;
  63. local_irq_save(flags);
  64. ns = __cycles_2_ns(cyc);
  65. local_irq_restore(flags);
  66. return ns;
  67. }
  68. #endif /* _ASM_X86_TIMER_H */