delay.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Precise Delay Loops for i386
  3. *
  4. * Copyright (C) 1993 Linus Torvalds
  5. * Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  6. * Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com>
  7. *
  8. * The __delay function must _NOT_ be inlined as its execution time
  9. * depends wildly on alignment on many x86 processors. The additional
  10. * jump magic is needed to get the timing stable on all the CPU's
  11. * we have to worry about.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/timex.h>
  16. #include <linux/preempt.h>
  17. #include <linux/delay.h>
  18. #include <linux/init.h>
  19. #include <asm/processor.h>
  20. #include <asm/delay.h>
  21. #include <asm/timer.h>
  22. #ifdef CONFIG_SMP
  23. # include <asm/smp.h>
  24. #endif
  25. /* simple loop based delay: */
  26. static void delay_loop(unsigned long loops)
  27. {
  28. asm volatile(
  29. " test %0,%0 \n"
  30. " jz 3f \n"
  31. " jmp 1f \n"
  32. ".align 16 \n"
  33. "1: jmp 2f \n"
  34. ".align 16 \n"
  35. "2: dec %0 \n"
  36. " jnz 2b \n"
  37. "3: dec %0 \n"
  38. : /* we don't need output */
  39. :"a" (loops)
  40. );
  41. }
  42. /* TSC based delay: */
  43. static void delay_tsc(unsigned long loops)
  44. {
  45. unsigned long bclock, now;
  46. int cpu;
  47. preempt_disable();
  48. cpu = smp_processor_id();
  49. rdtsc_barrier();
  50. rdtscl(bclock);
  51. for (;;) {
  52. rdtsc_barrier();
  53. rdtscl(now);
  54. if ((now - bclock) >= loops)
  55. break;
  56. /* Allow RT tasks to run */
  57. preempt_enable();
  58. rep_nop();
  59. preempt_disable();
  60. /*
  61. * It is possible that we moved to another CPU, and
  62. * since TSC's are per-cpu we need to calculate
  63. * that. The delay must guarantee that we wait "at
  64. * least" the amount of time. Being moved to another
  65. * CPU could make the wait longer but we just need to
  66. * make sure we waited long enough. Rebalance the
  67. * counter for this CPU.
  68. */
  69. if (unlikely(cpu != smp_processor_id())) {
  70. loops -= (now - bclock);
  71. cpu = smp_processor_id();
  72. rdtsc_barrier();
  73. rdtscl(bclock);
  74. }
  75. }
  76. preempt_enable();
  77. }
  78. /*
  79. * Since we calibrate only once at boot, this
  80. * function should be set once at boot and not changed
  81. */
  82. static void (*delay_fn)(unsigned long) = delay_loop;
  83. void use_tsc_delay(void)
  84. {
  85. delay_fn = delay_tsc;
  86. }
  87. int __devinit read_current_timer(unsigned long *timer_val)
  88. {
  89. if (delay_fn == delay_tsc) {
  90. rdtscll(*timer_val);
  91. return 0;
  92. }
  93. return -1;
  94. }
  95. void __delay(unsigned long loops)
  96. {
  97. delay_fn(loops);
  98. }
  99. EXPORT_SYMBOL(__delay);
  100. inline void __const_udelay(unsigned long xloops)
  101. {
  102. int d0;
  103. xloops *= 4;
  104. asm("mull %%edx"
  105. :"=d" (xloops), "=&a" (d0)
  106. :"1" (xloops), "0"
  107. (cpu_data(raw_smp_processor_id()).loops_per_jiffy * (HZ/4)));
  108. __delay(++xloops);
  109. }
  110. EXPORT_SYMBOL(__const_udelay);
  111. void __udelay(unsigned long usecs)
  112. {
  113. __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
  114. }
  115. EXPORT_SYMBOL(__udelay);
  116. void __ndelay(unsigned long nsecs)
  117. {
  118. __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
  119. }
  120. EXPORT_SYMBOL(__ndelay);