delay.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. rdtscl(bclock);
  50. for (;;) {
  51. rdtscl(now);
  52. if ((now - bclock) >= loops)
  53. break;
  54. /* Allow RT tasks to run */
  55. preempt_enable();
  56. rep_nop();
  57. preempt_disable();
  58. /*
  59. * It is possible that we moved to another CPU, and
  60. * since TSC's are per-cpu we need to calculate
  61. * that. The delay must guarantee that we wait "at
  62. * least" the amount of time. Being moved to another
  63. * CPU could make the wait longer but we just need to
  64. * make sure we waited long enough. Rebalance the
  65. * counter for this CPU.
  66. */
  67. if (unlikely(cpu != smp_processor_id())) {
  68. loops -= (now - bclock);
  69. cpu = smp_processor_id();
  70. rdtscl(bclock);
  71. }
  72. }
  73. preempt_enable();
  74. }
  75. /*
  76. * Since we calibrate only once at boot, this
  77. * function should be set once at boot and not changed
  78. */
  79. static void (*delay_fn)(unsigned long) = delay_loop;
  80. void use_tsc_delay(void)
  81. {
  82. delay_fn = delay_tsc;
  83. }
  84. int __devinit read_current_timer(unsigned long *timer_val)
  85. {
  86. if (delay_fn == delay_tsc) {
  87. rdtscll(*timer_val);
  88. return 0;
  89. }
  90. return -1;
  91. }
  92. void __delay(unsigned long loops)
  93. {
  94. delay_fn(loops);
  95. }
  96. EXPORT_SYMBOL(__delay);
  97. inline void __const_udelay(unsigned long xloops)
  98. {
  99. int d0;
  100. xloops *= 4;
  101. asm("mull %%edx"
  102. :"=d" (xloops), "=&a" (d0)
  103. :"1" (xloops), "0"
  104. (cpu_data(raw_smp_processor_id()).loops_per_jiffy * (HZ/4)));
  105. __delay(++xloops);
  106. }
  107. EXPORT_SYMBOL(__const_udelay);
  108. void __udelay(unsigned long usecs)
  109. {
  110. __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
  111. }
  112. EXPORT_SYMBOL(__udelay);
  113. void __ndelay(unsigned long nsecs)
  114. {
  115. __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
  116. }
  117. EXPORT_SYMBOL(__ndelay);