delay_64.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Precise Delay Loops for x86-64
  3. *
  4. * Copyright (C) 1993 Linus Torvalds
  5. * Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  6. *
  7. * The __delay function must _NOT_ be inlined as its execution time
  8. * depends wildly on alignment on many x86 processors.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/timex.h>
  13. #include <linux/preempt.h>
  14. #include <linux/delay.h>
  15. #include <linux/init.h>
  16. #include <asm/delay.h>
  17. #include <asm/msr.h>
  18. #ifdef CONFIG_SMP
  19. #include <asm/smp.h>
  20. #endif
  21. int __devinit read_current_timer(unsigned long *timer_value)
  22. {
  23. rdtscll(*timer_value);
  24. return 0;
  25. }
  26. void __delay(unsigned long loops)
  27. {
  28. unsigned bclock, now;
  29. int cpu;
  30. preempt_disable();
  31. cpu = smp_processor_id();
  32. rdtscl(bclock);
  33. for (;;) {
  34. rdtscl(now);
  35. if ((now - bclock) >= loops)
  36. break;
  37. /* Allow RT tasks to run */
  38. preempt_enable();
  39. rep_nop();
  40. preempt_disable();
  41. /*
  42. * It is possible that we moved to another CPU, and
  43. * since TSC's are per-cpu we need to calculate
  44. * that. The delay must guarantee that we wait "at
  45. * least" the amount of time. Being moved to another
  46. * CPU could make the wait longer but we just need to
  47. * make sure we waited long enough. Rebalance the
  48. * counter for this CPU.
  49. */
  50. if (unlikely(cpu != smp_processor_id())) {
  51. loops -= (now - bclock);
  52. cpu = smp_processor_id();
  53. rdtscl(bclock);
  54. }
  55. }
  56. preempt_enable();
  57. }
  58. EXPORT_SYMBOL(__delay);
  59. inline void __const_udelay(unsigned long xloops)
  60. {
  61. __delay(((xloops * HZ *
  62. cpu_data(raw_smp_processor_id()).loops_per_jiffy) >> 32) + 1);
  63. }
  64. EXPORT_SYMBOL(__const_udelay);
  65. void __udelay(unsigned long usecs)
  66. {
  67. __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
  68. }
  69. EXPORT_SYMBOL(__udelay);
  70. void __ndelay(unsigned long nsecs)
  71. {
  72. __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
  73. }
  74. EXPORT_SYMBOL(__ndelay);