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 <asm/processor.h>
  19. #include <asm/delay.h>
  20. #include <asm/timer.h>
  21. #ifdef CONFIG_SMP
  22. # include <asm/smp.h>
  23. #endif
  24. /* simple loop based delay: */
  25. static void delay_loop(unsigned long loops)
  26. {
  27. asm volatile(
  28. " test %0,%0 \n"
  29. " jz 3f \n"
  30. " jmp 1f \n"
  31. ".align 16 \n"
  32. "1: jmp 2f \n"
  33. ".align 16 \n"
  34. "2: dec %0 \n"
  35. " jnz 2b \n"
  36. "3: dec %0 \n"
  37. : /* we don't need output */
  38. :"a" (loops)
  39. );
  40. }
  41. /* TSC based delay: */
  42. static void delay_tsc(unsigned long __loops)
  43. {
  44. u32 bclock, now, loops = __loops;
  45. int cpu;
  46. preempt_disable();
  47. cpu = smp_processor_id();
  48. rdtsc_barrier();
  49. rdtscl(bclock);
  50. for (;;) {
  51. rdtsc_barrier();
  52. rdtscl(now);
  53. if ((now - bclock) >= loops)
  54. break;
  55. /* Allow RT tasks to run */
  56. preempt_enable();
  57. rep_nop();
  58. preempt_disable();
  59. /*
  60. * It is possible that we moved to another CPU, and
  61. * since TSC's are per-cpu we need to calculate
  62. * that. The delay must guarantee that we wait "at
  63. * least" the amount of time. Being moved to another
  64. * CPU could make the wait longer but we just need to
  65. * make sure we waited long enough. Rebalance the
  66. * counter for this CPU.
  67. */
  68. if (unlikely(cpu != smp_processor_id())) {
  69. loops -= (now - bclock);
  70. cpu = smp_processor_id();
  71. rdtsc_barrier();
  72. rdtscl(bclock);
  73. }
  74. }
  75. preempt_enable();
  76. }
  77. /*
  78. * Since we calibrate only once at boot, this
  79. * function should be set once at boot and not changed
  80. */
  81. static void (*delay_fn)(unsigned long) = delay_loop;
  82. void use_tsc_delay(void)
  83. {
  84. delay_fn = delay_tsc;
  85. }
  86. int read_current_timer(unsigned long *timer_val)
  87. {
  88. if (delay_fn == delay_tsc) {
  89. rdtscll(*timer_val);
  90. return 0;
  91. }
  92. return -1;
  93. }
  94. void __delay(unsigned long loops)
  95. {
  96. delay_fn(loops);
  97. }
  98. EXPORT_SYMBOL(__delay);
  99. inline void __const_udelay(unsigned long xloops)
  100. {
  101. int d0;
  102. xloops *= 4;
  103. asm("mull %%edx"
  104. :"=d" (xloops), "=&a" (d0)
  105. :"1" (xloops), "0"
  106. (this_cpu_read(cpu_info.loops_per_jiffy) * (HZ/4)));
  107. __delay(++xloops);
  108. }
  109. EXPORT_SYMBOL(__const_udelay);
  110. void __udelay(unsigned long usecs)
  111. {
  112. __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
  113. }
  114. EXPORT_SYMBOL(__udelay);
  115. void __ndelay(unsigned long nsecs)
  116. {
  117. __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
  118. }
  119. EXPORT_SYMBOL(__ndelay);