delay_32.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. *
  7. * The __delay function must _NOT_ be inlined as its execution time
  8. * depends wildly on alignment on many x86 processors. The additional
  9. * jump magic is needed to get the timing stable on all the CPU's
  10. * we have to worry about.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/timex.h>
  15. #include <linux/preempt.h>
  16. #include <linux/delay.h>
  17. #include <linux/init.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. int d0;
  28. __asm__ __volatile__(
  29. "\tjmp 1f\n"
  30. ".align 16\n"
  31. "1:\tjmp 2f\n"
  32. ".align 16\n"
  33. "2:\tdecl %0\n\tjns 2b"
  34. :"=&a" (d0)
  35. :"0" (loops));
  36. }
  37. /* TSC based delay: */
  38. static void delay_tsc(unsigned long loops)
  39. {
  40. unsigned long bclock, now;
  41. int cpu;
  42. preempt_disable();
  43. cpu = smp_processor_id();
  44. rdtscl(bclock);
  45. for (;;) {
  46. rdtscl(now);
  47. if ((now - bclock) >= loops)
  48. break;
  49. /* Allow RT tasks to run */
  50. preempt_enable();
  51. rep_nop();
  52. preempt_disable();
  53. /*
  54. * It is possible that we moved to another CPU, and
  55. * since TSC's are per-cpu we need to calculate
  56. * that. The delay must guarantee that we wait "at
  57. * least" the amount of time. Being moved to another
  58. * CPU could make the wait longer but we just need to
  59. * make sure we waited long enough. Rebalance the
  60. * counter for this CPU.
  61. */
  62. if (unlikely(cpu != smp_processor_id())) {
  63. loops -= (now - bclock);
  64. cpu = smp_processor_id();
  65. rdtscl(bclock);
  66. }
  67. }
  68. preempt_enable();
  69. }
  70. /*
  71. * Since we calibrate only once at boot, this
  72. * function should be set once at boot and not changed
  73. */
  74. static void (*delay_fn)(unsigned long) = delay_loop;
  75. void use_tsc_delay(void)
  76. {
  77. delay_fn = delay_tsc;
  78. }
  79. int __devinit read_current_timer(unsigned long *timer_val)
  80. {
  81. if (delay_fn == delay_tsc) {
  82. rdtscl(*timer_val);
  83. return 0;
  84. }
  85. return -1;
  86. }
  87. void __delay(unsigned long loops)
  88. {
  89. delay_fn(loops);
  90. }
  91. inline void __const_udelay(unsigned long xloops)
  92. {
  93. int d0;
  94. xloops *= 4;
  95. __asm__("mull %0"
  96. :"=d" (xloops), "=&a" (d0)
  97. :"1" (xloops), "0"
  98. (cpu_data(raw_smp_processor_id()).loops_per_jiffy * (HZ/4)));
  99. __delay(++xloops);
  100. }
  101. void __udelay(unsigned long usecs)
  102. {
  103. __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
  104. }
  105. void __ndelay(unsigned long nsecs)
  106. {
  107. __const_udelay(nsecs * 0x00005); /* 2**32 / 1000000000 (rounded up) */
  108. }
  109. EXPORT_SYMBOL(__delay);
  110. EXPORT_SYMBOL(__const_udelay);
  111. EXPORT_SYMBOL(__udelay);
  112. EXPORT_SYMBOL(__ndelay);